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 "event_filter_service.h" 20 #include "extra_data.h" 21 #include "i_anco_channel.h" 22 #include "i_anr_observer.h" 23 #include "i_input_service_watcher.h" 24 #include "i_window_checker.h" 25 #include "if_mmi_client.h" 26 #include "infrared_frequency_info.h" 27 #include "input_device_impl.h" 28 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR 29 #include "input_interceptor_manager.h" 30 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR 31 #ifdef OHOS_BUILD_ENABLE_MONITOR 32 #include "input_monitor_manager.h" 33 #endif // OHOS_BUILD_ENABLE_MONITOR 34 #include "key_option.h" 35 #include "long_press_event.h" 36 #include "mmi_event_observer.h" 37 #include "nap_process.h" 38 #include "pointer_style.h" 39 #include "touchpad_control_display_gain.h" 40 #include "shift_info.h" 41 42 namespace OHOS { 43 namespace MMI { 44 class InputManagerImpl final { 45 DECLARE_SINGLETON(InputManagerImpl); 46 47 public: 48 DISALLOW_MOVE(InputManagerImpl); 49 50 int32_t GetDisplayBindInfo(DisplayBindInfos &infos); 51 int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas); 52 int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg); 53 int32_t GetWindowPid(int32_t windowId); 54 int32_t UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); 55 int32_t UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo); 56 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 57 void SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen); 58 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 59 int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption, 60 std::function<void(std::shared_ptr<KeyEvent>)> callback); 61 void UnsubscribeKeyEvent(int32_t subscriberId); 62 int32_t SubscribeHotkey(std::shared_ptr<KeyOption> keyOption, 63 std::function<void(std::shared_ptr<KeyEvent>)> callback); 64 void UnsubscribeHotkey(int32_t subscriberId); 65 int32_t SubscribeKeyMonitor(const KeyMonitorOption &keyOption, 66 std::function<void(std::shared_ptr<KeyEvent>)> callback); 67 void UnsubscribeKeyMonitor(int32_t subscriberId); 68 int32_t SubscribeSwitchEvent(int32_t switchType, std::function<void(std::shared_ptr<SwitchEvent>)> callback); 69 void UnsubscribeSwitchEvent(int32_t subscriberId); 70 int32_t SubscribeTabletProximity(std::function<void(std::shared_ptr<PointerEvent>)> callback); 71 void UnsubscribetabletProximity(int32_t subscriberId); 72 int32_t SubscribeLongPressEvent(const LongPressRequest &LongPressRequest, 73 std::function<void(LongPressEvent)> callback); 74 void UnsubscribeLongPressEvent(int32_t subscriberId); 75 int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags); 76 int32_t RemoveInputEventFilter(int32_t filterId); 77 int32_t AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer); 78 int32_t RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer); 79 int32_t NotifyNapOnline(); 80 void NotifyBundleName(int32_t pid, int32_t uid, const std::string &bundleName, int32_t syncStatus); 81 int32_t SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer, 82 std::shared_ptr<AppExecFwk::EventHandler> eventHandler); 83 void ClearWindowPointerStyle(int32_t pid, int32_t windowId); 84 int32_t SetNapStatus(int32_t pid, int32_t uid, const std::string &bundleName, int32_t napStatus); 85 int32_t SetTouchpadThreeFingersTapSwitch(bool switchFlag); 86 int32_t GetTouchpadThreeFingersTapSwitch(bool &switchFlag); 87 void SetMultiWindowScreenId(uint64_t screenId, uint64_t displayNodeScreenId); 88 89 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 90 void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 91 #endif // OHOS_BUILD_ENABLE_KEYBOARD 92 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 93 void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent); 94 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 95 int32_t PackDisplayData(NetPacket &pkt); 96 97 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 98 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 99 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer, 100 HandleEventType eventType = HANDLE_EVENT_TYPE_KP); 101 int32_t AddGestureMonitor(std::shared_ptr<IInputEventConsumer> consumer, 102 TouchGestureType type, int32_t fingers); 103 int32_t RemoveGestureMonitor(int32_t monitorId); 104 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> monitor, std::vector<int32_t> actionsType); 105 106 int32_t RemoveMonitor(int32_t monitorId); 107 void MarkConsumed(int32_t monitorId, int32_t eventId); 108 void MoveMouse(int32_t offsetX, int32_t offsetY); 109 110 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor, 111 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 112 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 113 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor, 114 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 115 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 116 int32_t RemoveInterceptor(int32_t interceptorId); 117 118 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject = false); 119 int32_t SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false); 120 void HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 121 void SimulateTouchPadEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false); 122 void SimulateTouchPadInputEvent(std::shared_ptr<PointerEvent> pointerEvent, 123 const TouchpadCDG &touchpadCDG); 124 void OnConnected(); 125 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 126 template<typename T> 127 bool RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent); 128 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 129 void OnDisconnected(); 130 131 int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener); 132 int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr); 133 int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback); 134 int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback); 135 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, 136 std::function<void(std::vector<bool>&)> callback); 137 int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback); 138 int32_t SetKeyboardRepeatDelay(int32_t delay); 139 int32_t SetKeyboardRepeatRate(int32_t rate); 140 int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback); 141 int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback); 142 143 int32_t SetMouseScrollRows(int32_t rows); 144 int32_t GetMouseScrollRows(int32_t &rows); 145 int32_t SetPointerSize(int32_t size); 146 int32_t GetPointerSize(int32_t &size); 147 int32_t GetCursorSurfaceId(uint64_t &surfaceId); 148 int32_t SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap); 149 int32_t SetCustomCursor(int32_t windowId, CustomCursor cursor, CursorOptions options); 150 int32_t SetMouseIcon(int32_t windowId, void* pixelMap); 151 int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY); 152 int32_t SetMousePrimaryButton(int32_t primaryButton); 153 int32_t GetMousePrimaryButton(int32_t &primaryButton); 154 int32_t SetHoverScrollState(bool state); 155 int32_t GetHoverScrollState(bool &state); 156 157 int32_t SetPointerVisible(bool visible, int32_t priority); 158 bool IsPointerVisible(); 159 int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle, bool isUiExtension = false); 160 int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension = false); 161 162 int32_t SetPointerColor(int32_t color); 163 int32_t GetPointerColor(int32_t &color); 164 int32_t EnableCombineKey(bool enable); 165 int32_t SetPointerSpeed(int32_t speed); 166 int32_t GetPointerSpeed(int32_t &speed); 167 168 int32_t SetTouchpadScrollSwitch(bool switchFlag); 169 int32_t GetTouchpadScrollSwitch(bool &switchFlag); 170 int32_t SetTouchpadScrollDirection(bool state); 171 int32_t GetTouchpadScrollDirection(bool &state); 172 int32_t SetTouchpadTapSwitch(bool switchFlag); 173 int32_t GetTouchpadTapSwitch(bool &switchFlag); 174 int32_t SetTouchpadPointerSpeed(int32_t speed); 175 int32_t GetTouchpadPointerSpeed(int32_t &speed); 176 int32_t GetTouchpadCDG(TouchpadCDG &touchpadCDG); 177 int32_t SetTouchpadPinchSwitch(bool switchFlag); 178 int32_t GetTouchpadPinchSwitch(bool &switchFlag); 179 int32_t SetTouchpadSwipeSwitch(bool switchFlag); 180 int32_t GetTouchpadSwipeSwitch(bool &switchFlag); 181 int32_t SetTouchpadRightClickType(int32_t type); 182 int32_t GetTouchpadRightClickType(int32_t &type); 183 int32_t SetTouchpadRotateSwitch(bool rotateSwitch); 184 int32_t GetTouchpadRotateSwitch(bool &rotateSwitch); 185 int32_t SetTouchpadDoubleTapAndDragState(bool switchFlag); 186 int32_t GetTouchpadDoubleTapAndDragState(bool &switchFlag); 187 int32_t EnableHardwareCursorStats(bool enable); 188 int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount); 189 int32_t GetPointerSnapshot(void *pixelMapPtr); 190 int32_t SetTouchpadScrollRows(int32_t rows); 191 int32_t GetTouchpadScrollRows(int32_t &rows); 192 193 void SetAnrObserver(std::shared_ptr<IAnrObserver> observer); 194 void OnAnr(int32_t pid, int32_t eventId); 195 196 int32_t EnterCaptureMode(int32_t windowId); 197 int32_t LeaveCaptureMode(int32_t windowId); 198 int32_t GetFunctionKeyState(int32_t funcKey, bool &resultState); 199 int32_t SetFunctionKeyState(int32_t funcKey, bool enable); 200 int32_t SetPointerLocation(int32_t x, int32_t y, int32_t displayId); 201 int32_t EnableInputDevice(bool enable); 202 // 快捷键拉起Ability 203 int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay); 204 205 int32_t AppendExtraData(const ExtraData& extraData); 206 int32_t SetShieldStatus(int32_t shieldMode, bool isShield); 207 int32_t GetShieldStatus(int32_t shieldMode, bool &isShield); 208 209 void AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher); 210 void RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher); 211 212 int32_t MarkProcessed(int32_t eventId, int64_t actionTime); 213 214 int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState); 215 void Authorize(bool isAuthorize); 216 int32_t CancelInjection(); 217 218 int32_t HasIrEmitter(bool &hasIrEmitter); 219 int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys); 220 int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern); 221 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD 222 int32_t CreateVKeyboardDevice(sptr<IRemoteObject> &vkeyboardDevice); 223 #endif // OHOS_BUILD_ENABLE_VKEYBOARD 224 int32_t SetPixelMapData(int32_t infoId, void* pixelMap); 225 int32_t SetCurrentUser(int32_t userId); 226 int32_t SetMoveEventFilters(bool flag); 227 int32_t GetWinSyncBatchSize(int32_t maxAreasCount, int32_t displayCount); 228 int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId); 229 int32_t RemoveVirtualInputDevice(int32_t deviceId); 230 int32_t AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer); 231 int32_t AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer); 232 int32_t SkipPointerLayer(bool isSkip); 233 int32_t RegisterWindowStateErrorCallback(std::function<void(int32_t, int32_t)> callback); 234 void OnWindowStateError(int32_t pid, int32_t windowId); 235 int32_t GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &keyOptions, int32_t &count); 236 int32_t GetIntervalSinceLastInput(int64_t &timeInterval); 237 int32_t ConvertToCapiKeyAction(int32_t keyAction); 238 int32_t SetInputDeviceEnabled(int32_t deviceId, bool enable, std::function<void(int32_t)> callback); 239 int32_t ShiftAppPointerEvent(const ShiftWindowParam ¶m, bool autoGenDown); 240 int32_t CheckKnuckleEvent(float pointX, float pointY, bool &touchType); 241 242 private: 243 int32_t PackWindowInfo(NetPacket &pkt); 244 int32_t PackWindowGroupInfo(NetPacket &pkt); 245 int32_t PackDisplayInfo(NetPacket &pkt); 246 int32_t PackUiExtentionWindowInfo(const std::vector<WindowInfo>& windowsInfo, NetPacket &pkt); 247 void PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo); 248 void PrintForemostThreeWindowInfo(const std::vector<WindowInfo> &windowsInfo); 249 void PrintDisplayInfo(); 250 void PrintWindowGroupInfo(); 251 int32_t SendDisplayInfo(); 252 int32_t SendWindowInfo(); 253 void SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId); 254 bool IsValiadWindowAreas(const std::vector<WindowInfo> &windows); 255 int32_t GetDisplayMaxSize(); 256 int32_t GetWindowMaxSize(int32_t maxAreasCount); 257 std::vector<std::string> StringSplit(const std::string &str, char delim); 258 bool IsGRLOrHopper(); 259 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 260 int32_t PackEnhanceConfig(NetPacket &pkt); 261 void SendEnhanceConfig(); 262 void PrintEnhanceConfig(); 263 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 264 void ReAddInputEventFilter(); 265 266 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 267 void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer, 268 std::shared_ptr<KeyEvent> keyEvent); 269 #endif // OHOS_BUILD_ENABLE_KEYBOARD 270 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 271 void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer, 272 std::shared_ptr<PointerEvent> pointerEvent); 273 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 274 #ifdef OHOS_BUILD_ENABLE_ANCO 275 bool IsValidAncoWindow(const std::vector<WindowInfo> &windows); 276 #endif // OHOS_BUILD_ENABLE_ANCO 277 278 private: 279 std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_; 280 std::shared_ptr<MMIEventObserver> eventObserver_ { nullptr }; 281 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 282 std::vector<std::shared_ptr<IAnrObserver>> anrObservers_; 283 std::shared_ptr<IWindowChecker> winChecker_ { nullptr }; 284 DisplayGroupInfo displayGroupInfo_ {}; 285 WindowGroupInfo windowGroupInfo_ {}; 286 std::mutex mtx_; 287 std::mutex eventObserverMtx_; 288 std::mutex winStatecallbackMtx_; 289 mutable std::mutex resourceMtx_; 290 std::condition_variable cv_; 291 std::thread ehThread_; 292 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr }; 293 std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr }; 294 std::function<void(int32_t, int32_t)> windowStatecallback_; 295 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 296 uint8_t* enhanceCfg_ = nullptr; 297 uint32_t enhanceCfgLen_ = 0; 298 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 299 #ifdef OHOS_BUILD_ENABLE_ANCO 300 std::map<std::shared_ptr<IAncoConsumer>, sptr<IAncoChannel>> ancoChannels_; 301 #endif // OHOS_BUILD_ENABLE_ANCO 302 }; 303 304 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance() 305 } // namespace MMI 306 } // namespace OHOS 307 #endif // INPUT_MANAGER_IMPL_H 308