1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INPUT_MANAGER_IMPL_H 17 #define INPUT_MANAGER_IMPL_H 18 19 #include <list> 20 #include <vector> 21 22 #include "singleton.h" 23 24 #include "net_packet.h" 25 26 #include "event_filter_service.h" 27 #include "event_handler.h" 28 #include "extra_data.h" 29 #include "i_anco_channel.h" 30 #include "i_anr_observer.h" 31 #include "i_input_event_consumer.h" 32 #include "i_input_service_watcher.h" 33 #include "i_window_checker.h" 34 #include "if_mmi_client.h" 35 #include "infrared_frequency_info.h" 36 #include "input_device_impl.h" 37 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR 38 #include "input_interceptor_manager.h" 39 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR 40 #ifdef OHOS_BUILD_ENABLE_MONITOR 41 #include "input_monitor_manager.h" 42 #endif // OHOS_BUILD_ENABLE_MONITOR 43 #include "key_option.h" 44 #include "mmi_event_observer.h" 45 #include "nap_process.h" 46 #include "pointer_event.h" 47 #include "pointer_style.h" 48 #include "switch_event.h" 49 #include "window_info.h" 50 #include "shift_info.h" 51 52 namespace OHOS { 53 namespace MMI { 54 class InputManagerImpl final { 55 DECLARE_SINGLETON(InputManagerImpl); 56 57 public: 58 DISALLOW_MOVE(InputManagerImpl); 59 60 int32_t GetDisplayBindInfo(DisplayBindInfos &infos); 61 int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas); 62 int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg); 63 int32_t GetWindowPid(int32_t windowId); 64 int32_t UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); 65 int32_t UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo); 66 void SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId); 67 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 68 void SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen); 69 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 70 int32_t SubscribeKeyEvent( 71 std::shared_ptr<KeyOption> keyOption, 72 std::function<void(std::shared_ptr<KeyEvent>)> callback 73 ); 74 void UnsubscribeKeyEvent(int32_t subscriberId); 75 int32_t SubscribeSwitchEvent(int32_t switchType, std::function<void(std::shared_ptr<SwitchEvent>)> callback); 76 void UnsubscribeSwitchEvent(int32_t subscriberId); 77 int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags); 78 int32_t RemoveInputEventFilter(int32_t filterId); 79 int32_t AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer); 80 int32_t RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer); 81 int32_t NotifyNapOnline(); 82 void NotifyBundleName(int32_t pid, int32_t uid, const std::string &bundleName, int32_t syncStatus); 83 int32_t SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer, 84 std::shared_ptr<AppExecFwk::EventHandler> eventHandler); 85 void ClearWindowPointerStyle(int32_t pid, int32_t windowId); 86 int32_t SetNapStatus(int32_t pid, int32_t uid, const std::string &bundleName, int32_t napStatus); 87 88 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 89 void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 90 #endif // OHOS_BUILD_ENABLE_KEYBOARD 91 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 92 void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent); 93 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 94 int32_t PackDisplayData(NetPacket &pkt); 95 96 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 97 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 98 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer, 99 HandleEventType eventType = HANDLE_EVENT_TYPE_KP); 100 101 int32_t RemoveMonitor(int32_t monitorId); 102 void MarkConsumed(int32_t monitorId, int32_t eventId); 103 void MoveMouse(int32_t offsetX, int32_t offsetY); 104 105 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor, 106 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 107 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 108 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor, 109 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 110 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 111 int32_t RemoveInterceptor(int32_t interceptorId); 112 113 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject = false); 114 int32_t SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false); 115 void HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 116 void OnConnected(); 117 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 118 template<typename T> 119 bool RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent); 120 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 121 void OnDisconnected(); 122 123 int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener); 124 int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr); 125 int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback); 126 int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback); 127 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, 128 std::function<void(std::vector<bool>&)> callback); 129 int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback); 130 int32_t SetKeyboardRepeatDelay(int32_t delay); 131 int32_t SetKeyboardRepeatRate(int32_t rate); 132 int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback); 133 int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback); 134 135 int32_t SetMouseScrollRows(int32_t rows); 136 int32_t GetMouseScrollRows(int32_t &rows); 137 int32_t SetPointerSize(int32_t size); 138 int32_t GetPointerSize(int32_t &size); 139 int32_t GetCursorSurfaceId(uint64_t &surfaceId); 140 int32_t SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap); 141 int32_t SetCustomCursor(int32_t windowId, CustomCursor cursor, CursorOptions options); 142 int32_t SetMouseIcon(int32_t windowId, void* pixelMap); 143 int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY); 144 int32_t SetMousePrimaryButton(int32_t primaryButton); 145 int32_t GetMousePrimaryButton(int32_t &primaryButton); 146 int32_t SetHoverScrollState(bool state); 147 int32_t GetHoverScrollState(bool &state); 148 149 int32_t SetPointerVisible(bool visible, int32_t priority); 150 bool IsPointerVisible(); 151 int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle, bool isUiExtension = false); 152 int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension = false); 153 154 int32_t SetPointerColor(int32_t color); 155 int32_t GetPointerColor(int32_t &color); 156 int32_t EnableCombineKey(bool enable); 157 int32_t SetPointerSpeed(int32_t speed); 158 int32_t GetPointerSpeed(int32_t &speed); 159 160 int32_t SetTouchpadScrollSwitch(bool switchFlag); 161 int32_t GetTouchpadScrollSwitch(bool &switchFlag); 162 int32_t SetTouchpadScrollDirection(bool state); 163 int32_t GetTouchpadScrollDirection(bool &state); 164 int32_t SetTouchpadTapSwitch(bool switchFlag); 165 int32_t GetTouchpadTapSwitch(bool &switchFlag); 166 int32_t SetTouchpadPointerSpeed(int32_t speed); 167 int32_t GetTouchpadPointerSpeed(int32_t &speed); 168 int32_t SetTouchpadPinchSwitch(bool switchFlag); 169 int32_t GetTouchpadPinchSwitch(bool &switchFlag); 170 int32_t SetTouchpadSwipeSwitch(bool switchFlag); 171 int32_t GetTouchpadSwipeSwitch(bool &switchFlag); 172 int32_t SetTouchpadRightClickType(int32_t type); 173 int32_t GetTouchpadRightClickType(int32_t &type); 174 int32_t SetTouchpadRotateSwitch(bool rotateSwitch); 175 int32_t GetTouchpadRotateSwitch(bool &rotateSwitch); 176 int32_t SetTouchpadDoubleTapAndDragState(bool switchFlag); 177 int32_t GetTouchpadDoubleTapAndDragState(bool &switchFlag); 178 int32_t EnableHardwareCursorStats(bool enable); 179 int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount); 180 int32_t GetPointerSnapshot(void *pixelMapPtr); 181 182 void SetAnrObserver(std::shared_ptr<IAnrObserver> observer); 183 void OnAnr(int32_t pid, int32_t eventId); 184 185 int32_t EnterCaptureMode(int32_t windowId); 186 int32_t LeaveCaptureMode(int32_t windowId); 187 int32_t GetFunctionKeyState(int32_t funcKey, bool &resultState); 188 int32_t SetFunctionKeyState(int32_t funcKey, bool enable); 189 int32_t SetPointerLocation(int32_t x, int32_t y); 190 int32_t EnableInputDevice(bool enable); 191 // 快捷键拉起Ability 192 int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay); 193 194 void AppendExtraData(const ExtraData& extraData); 195 int32_t SetShieldStatus(int32_t shieldMode, bool isShield); 196 int32_t GetShieldStatus(int32_t shieldMode, bool &isShield); 197 int32_t MarkProcessed(int32_t eventId, int64_t actionTime); 198 199 void AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher); 200 void RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher); 201 202 int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState); 203 void Authorize(bool isAuthorize); 204 int32_t CancelInjection(); 205 int32_t SetPixelMapData(int32_t infoId, void* pixelMap); 206 int32_t HasIrEmitter(bool &hasIrEmitter); 207 int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys); 208 int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern); 209 int32_t SetCurrentUser(int32_t userId); 210 int32_t GetWinSyncBatchSize(int32_t maxAreasCount, int32_t displayCount); 211 int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId); 212 int32_t RemoveVirtualInputDevice(int32_t deviceId); 213 int32_t AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer); 214 int32_t AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer); 215 int32_t SkipPointerLayer(bool isSkip); 216 int32_t RegisterWindowStateErrorCallback(std::function<void(int32_t, int32_t)> callback); 217 void OnWindowStateError(int32_t pid, int32_t windowId); 218 int32_t ConvertToCapiKeyAction(int32_t keyAction); 219 int32_t GetIntervalSinceLastInput(int64_t &timeInterval); 220 int32_t ShiftAppPointerEvent(const ShiftWindowParam ¶m, bool autoGenDown); 221 222 private: 223 int32_t PackWindowInfo(NetPacket &pkt); 224 int32_t PackWindowGroupInfo(NetPacket &pkt); 225 int32_t PackDisplayInfo(NetPacket &pkt); 226 int32_t PackUiExtentionWindowInfo(const std::vector<WindowInfo>& windowsInfo, NetPacket &pkt); 227 void PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo); 228 void PrintForemostThreeWindowInfo(const std::vector<WindowInfo> &windowsInfo); 229 void PrintDisplayInfo(); 230 void PrintWindowGroupInfo(); 231 int32_t SendDisplayInfo(); 232 int32_t SendWindowInfo(); 233 void SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId); 234 bool IsValiadWindowAreas(const std::vector<WindowInfo> &windows); 235 int32_t GetDisplayMaxSize(); 236 int32_t GetWindowMaxSize(int32_t maxAreasCount); 237 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 238 int32_t PackEnhanceConfig(NetPacket &pkt); 239 void SendEnhanceConfig(); 240 void PrintEnhanceConfig(); 241 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 242 void ReAddInputEventFilter(); 243 244 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 245 void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer, 246 std::shared_ptr<KeyEvent> keyEvent); 247 #endif // OHOS_BUILD_ENABLE_KEYBOARD 248 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 249 void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer, 250 std::shared_ptr<PointerEvent> pointerEvent); 251 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 252 #ifdef OHOS_BUILD_ENABLE_ANCO 253 bool IsValidAncoWindow(const std::vector<WindowInfo> &windows); 254 #endif // OHOS_BUILD_ENABLE_ANCO 255 #ifdef OHOS_BUILD_ENABLE_ONE_HAND_MODE 256 void UpdateDisplayXYInOneHandMode(std::shared_ptr<PointerEvent> pointerEvent); 257 #endif // OHOS_BUILD_ENABLE_ONE_HAND_MODE 258 259 private: 260 std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_; 261 std::shared_ptr<MMIEventObserver> eventObserver_ { nullptr }; 262 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 263 std::vector<std::shared_ptr<IAnrObserver>> anrObservers_; 264 std::shared_ptr<IWindowChecker> winChecker_ { nullptr }; 265 DisplayGroupInfo displayGroupInfo_ {}; 266 WindowGroupInfo windowGroupInfo_ {}; 267 std::mutex mtx_; 268 std::mutex handleMtx_; 269 mutable std::mutex resourceMtx_; 270 std::condition_variable cv_; 271 std::thread ehThread_; 272 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr }; 273 std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr }; 274 std::function<void(int32_t, int32_t)> windowStatecallback_; 275 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 276 uint8_t* enhanceCfg_ = nullptr; 277 uint32_t enhanceCfgLen_ = 0; 278 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 279 #ifdef OHOS_BUILD_ENABLE_ANCO 280 std::map<std::shared_ptr<IAncoConsumer>, sptr<IAncoChannel>> ancoChannels_; 281 #endif // OHOS_BUILD_ENABLE_ANCO 282 }; 283 284 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance() 285 } // namespace MMI 286 } // namespace OHOS 287 #endif // INPUT_MANAGER_IMPL_H 288