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 <vector> 20 21 #include "singleton.h" 22 23 #include "net_packet.h" 24 25 #include "display_info.h" 26 #include "event_filter_service.h" 27 #include "event_handler.h" 28 #include "if_mmi_client.h" 29 #include "input_device_impl.h" 30 #include "input_device_cooperate_impl.h" 31 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR 32 #include "input_interceptor_manager.h" 33 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR 34 #ifdef OHOS_BUILD_ENABLE_MONITOR 35 #include "input_monitor_manager.h" 36 #endif // OHOS_BUILD_ENABLE_MONITOR 37 #include "i_anr_observer.h" 38 #include "i_input_event_consumer.h" 39 #include "key_option.h" 40 #include "pointer_event.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 void UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); 51 int32_t SubscribeKeyEvent( 52 std::shared_ptr<KeyOption> keyOption, 53 std::function<void(std::shared_ptr<KeyEvent>)> callback 54 ); 55 void UnsubscribeKeyEvent(int32_t subscriberId); 56 int32_t AddInputEventFilter(std::function<bool(std::shared_ptr<PointerEvent>)> filter); 57 58 void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer, 59 std::shared_ptr<AppExecFwk::EventHandler> eventHandler); 60 61 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 62 void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent); 63 #endif // OHOS_BUILD_ENABLE_KEYBOARD 64 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 65 void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent); 66 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 67 int32_t PackDisplayData(NetPacket &pkt); 68 69 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 70 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 71 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer); 72 73 void RemoveMonitor(int32_t monitorId); 74 void MarkConsumed(int32_t monitorId, int32_t eventId); 75 void MoveMouse(int32_t offsetX, int32_t offsetY); 76 77 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor); 78 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor); 79 void RemoveInterceptor(int32_t interceptorId); 80 81 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent); 82 void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 83 void OnConnected(); 84 85 int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener); 86 int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr); 87 int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback); 88 int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback); 89 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes, 90 std::function<void(std::vector<bool>&)> callback); 91 int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback); 92 93 int32_t SetPointerVisible(bool visible); 94 bool IsPointerVisible(); 95 int32_t SetPointerStyle(int32_t windowId, int32_t pointerStyle); 96 int32_t GetPointerStyle(int32_t windowId, int32_t &pointerStyle); 97 98 int32_t SetPointerSpeed(int32_t speed); 99 int32_t GetPointerSpeed(int32_t &speed); 100 101 void SetAnrObserver(std::shared_ptr<IAnrObserver> observer); 102 void OnAnr(int32_t pid); 103 104 int32_t RegisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener); 105 int32_t UnregisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener = nullptr); 106 int32_t EnableInputDeviceCooperate(bool enabled, std::function<void(std::string, CooperationMessage)> callback); 107 int32_t StartInputDeviceCooperate(const std::string &sinkDeviceId, int32_t srcInputDeviceId, 108 std::function<void(std::string, CooperationMessage)> callback); 109 int32_t StopDeviceCooperate(std::function<void(std::string, CooperationMessage)> callback); 110 int32_t GetInputDeviceCooperateState(const std::string &deviceId, std::function<void(bool)> callback); 111 int32_t SetInputDevice(const std::string& dhid, const std::string& screenId); 112 bool GetFunctionKeyState(int32_t funcKey); 113 int32_t SetFunctionKeyState(int32_t funcKey, bool enable); 114 115 private: 116 int32_t PackWindowInfo(NetPacket &pkt); 117 int32_t PackDisplayInfo(NetPacket &pkt); 118 void PrintDisplayInfo(); 119 void SendDisplayInfo(); 120 121 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 122 void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer, 123 std::shared_ptr<KeyEvent> keyEvent); 124 #endif // OHOS_BUILD_ENABLE_KEYBOARD 125 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 126 void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer, 127 std::shared_ptr<PointerEvent> pointerEvent); 128 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 129 private: 130 sptr<EventFilterService> eventFilterService_ { nullptr }; 131 std::shared_ptr<IInputEventConsumer> consumer_ { nullptr }; 132 std::vector<std::shared_ptr<IAnrObserver>> anrObservers_; 133 134 DisplayGroupInfo displayGroupInfo_ {}; 135 std::mutex mtx_; 136 std::mutex handleMtx_; 137 std::condition_variable cv_; 138 std::thread ehThread_; 139 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr }; 140 }; 141 142 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance() 143 } // namespace MMI 144 } // namespace OHOS 145 #endif // INPUT_MANAGER_IMPL_H