1 /* 2 * Copyright (c) 2021-2022 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 "window_info.h" 27 #include "event_filter_service.h" 28 #include "event_handler.h" 29 #include "extra_data.h" 30 #include "if_mmi_client.h" 31 #include "input_device_impl.h" 32 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR 33 #include "input_interceptor_manager.h" 34 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR 35 #ifdef OHOS_BUILD_ENABLE_MONITOR 36 #include "input_monitor_manager.h" 37 #endif // OHOS_BUILD_ENABLE_MONITOR 38 #include "i_anr_observer.h" 39 #include "key_option.h" 40 #include "pointer_event.h" 41 #include "pointer_style.h" 42 #include "switch_event.h" 43 44 namespace OHOS { 45 namespace MMI { 46 class InputManagerImpl final { 47 DECLARE_SINGLETON(InputManagerImpl); 48 49 public: 50 DISALLOW_MOVE(InputManagerImpl); 51 52 int32_t GetDisplayBindInfo(DisplayBindInfos &infos); 53 int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg); 54 int32_t GetWindowPid(int32_t windowId); 55 void UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); 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( 60 std::shared_ptr<KeyOption> keyOption, 61 std::function<void(std::shared_ptr<KeyEvent>)> callback 62 ); 63 void UnsubscribeKeyEvent(int32_t subscriberId); 64 int32_t SubscribeSwitchEvent(std::function<void(std::shared_ptr<SwitchEvent>)> callback); 65 void UnsubscribeSwitchEvent(int32_t subscriberId); 66 int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags); 67 int32_t RemoveInputEventFilter(int32_t filterId); 68 69 void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer, 70 std::shared_ptr<AppExecFwk::EventHandler> eventHandler); 71 72 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 73 void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 74 #endif // OHOS_BUILD_ENABLE_KEYBOARD 75 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 76 void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent); 77 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 78 int32_t PackDisplayData(NetPacket &pkt); 79 80 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 81 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 82 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer); 83 84 void RemoveMonitor(int32_t monitorId); 85 void MarkConsumed(int32_t monitorId, int32_t eventId); 86 void MoveMouse(int32_t offsetX, int32_t offsetY); 87 88 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor, 89 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 90 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 91 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor, 92 int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY, 93 uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX)); 94 void RemoveInterceptor(int32_t interceptorId); 95 96 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent); 97 void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 98 void OnConnected(); 99 100 int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener); 101 int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr); 102 int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback); 103 int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback); 104 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, 105 std::function<void(std::vector<bool>&)> callback); 106 int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback); 107 int32_t SetKeyboardRepeatDelay(int32_t delay); 108 int32_t SetKeyboardRepeatRate(int32_t rate); 109 int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback); 110 int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback); 111 112 int32_t SetMouseScrollRows(int32_t rows); 113 int32_t GetMouseScrollRows(int32_t &rows); 114 int32_t SetPointerSize(int32_t size); 115 int32_t GetPointerSize(int32_t &size); 116 int32_t SetMouseIcon(int32_t windowId, void* pixelMap); 117 int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY); 118 int32_t SetMousePrimaryButton(int32_t primaryButton); 119 int32_t GetMousePrimaryButton(int32_t &primaryButton); 120 int32_t SetHoverScrollState(bool state); 121 int32_t GetHoverScrollState(bool &state); 122 123 int32_t SetPointerVisible(bool visible); 124 bool IsPointerVisible(); 125 int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle); 126 int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle); 127 128 int32_t SetPointerColor(int32_t color); 129 int32_t GetPointerColor(int32_t &color); 130 int32_t SetPointerSpeed(int32_t speed); 131 int32_t GetPointerSpeed(int32_t &speed); 132 133 int32_t SetTouchpadScrollSwitch(bool switchFlag); 134 int32_t GetTouchpadScrollSwitch(bool &switchFlag); 135 int32_t SetTouchpadScrollDirection(bool state); 136 int32_t GetTouchpadScrollDirection(bool &state); 137 int32_t SetTouchpadTapSwitch(bool switchFlag); 138 int32_t GetTouchpadTapSwitch(bool &switchFlag); 139 int32_t SetTouchpadPointerSpeed(int32_t speed); 140 int32_t GetTouchpadPointerSpeed(int32_t &speed); 141 int32_t SetTouchpadPinchSwitch(bool switchFlag); 142 int32_t GetTouchpadPinchSwitch(bool &switchFlag); 143 int32_t SetTouchpadSwipeSwitch(bool switchFlag); 144 int32_t GetTouchpadSwipeSwitch(bool &switchFlag); 145 int32_t SetTouchpadRightClickType(int32_t type); 146 int32_t GetTouchpadRightClickType(int32_t &type); 147 148 void SetAnrObserver(std::shared_ptr<IAnrObserver> observer); 149 void OnAnr(int32_t pid); 150 151 int32_t EnterCaptureMode(int32_t windowId); 152 int32_t LeaveCaptureMode(int32_t windowId); 153 bool GetFunctionKeyState(int32_t funcKey); 154 int32_t SetFunctionKeyState(int32_t funcKey, bool enable); 155 void SetPointerLocation(int32_t x, int32_t y); 156 int32_t EnableInputDevice(bool enable); 157 // 快捷键拉起Ability 158 int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay); 159 160 EventHandlerPtr GetEventHandler() const; 161 void AppendExtraData(const ExtraData& extraData); 162 private: 163 int32_t PackWindowInfo(NetPacket &pkt); 164 int32_t PackDisplayInfo(NetPacket &pkt); 165 void PrintDisplayInfo(); 166 void SendDisplayInfo(); 167 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 168 int32_t PackEnhanceConfig(NetPacket &pkt); 169 void SendEnhanceConfig(); 170 void PrintEnhanceConfig(); 171 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 172 void ReAddInputEventFilter(); 173 174 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 175 void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer, 176 std::shared_ptr<KeyEvent> keyEvent); 177 #endif // OHOS_BUILD_ENABLE_KEYBOARD 178 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 179 void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer, 180 std::shared_ptr<PointerEvent> pointerEvent); 181 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 182 private: 183 std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_; 184 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 185 std::vector<std::shared_ptr<IAnrObserver>> anrObservers_; 186 187 DisplayGroupInfo displayGroupInfo_ {}; 188 std::mutex mtx_; 189 std::mutex handleMtx_; 190 std::condition_variable cv_; 191 std::thread ehThread_; 192 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr }; 193 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 194 uint8_t* enhanceCfg_ = nullptr; 195 uint32_t enhanceCfgLen_ = 0; 196 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 197 }; 198 199 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance() 200 } // namespace MMI 201 } // namespace OHOS 202 #endif // INPUT_MANAGER_IMPL_H 203