1 /* 2 * Copyright (c) 2023 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_ADAPTER_H 17 #define INPUT_ADAPTER_H 18 19 #include "nocopyable.h" 20 21 #include "i_input_adapter.h" 22 23 #include "i_input_device_listener.h" 24 #include "i_input_event_consumer.h" 25 #include "i_input_event_filter.h" 26 namespace OHOS { 27 namespace Msdp { 28 namespace DeviceStatus { 29 30 namespace { 31 using MMIDevListener = std::function<void(int32_t, const std::string&)>; 32 } 33 class InputAdapter final : public IInputAdapter { 34 public: 35 InputAdapter() = default; 36 ~InputAdapter() = default; 37 DISALLOW_COPY_AND_MOVE(InputAdapter); 38 39 int32_t AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> callback) override; 40 int32_t AddMonitor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> callback) override; 41 int32_t AddMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback, 42 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback, MMI::HandleEventType eventType) override; 43 void RemoveMonitor(int32_t monitorId) override; 44 45 int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback) override; 46 int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback) override; 47 int32_t AddInterceptor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback, 48 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback) override; 49 void RemoveInterceptor(int32_t interceptorId) override; 50 51 int32_t AddFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> callback) override; 52 void RemoveFilter(int32_t filterId) override; 53 54 int32_t SetPointerVisibility(bool visible, int32_t priority = 0) override; 55 int32_t SetPointerLocation(int32_t x, int32_t y, int32_t displayId) override; 56 int32_t EnableInputDevice(bool enable) override; 57 58 void SimulateInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) override; 59 void SimulateInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) override; 60 int32_t AddVirtualInputDevice(std::shared_ptr<MMI::InputDevice> device, int32_t &deviceId) override; 61 int32_t RemoveVirtualInputDevice(int32_t deviceId) override; 62 int32_t GetPointerSpeed(int32_t &speed) override; 63 int32_t SetPointerSpeed(int32_t speed) override; 64 int32_t GetTouchPadSpeed(int32_t &speed) override; 65 int32_t SetTouchPadSpeed(int32_t speed) override; 66 bool HasLocalPointerDevice() override; 67 int32_t RegisterDevListener(MMIDevListener devAddedCallback, MMIDevListener devRemovedCallback) override; 68 int32_t UnregisterDevListener() override; 69 int32_t AddPreMonitor(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback, 70 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback, 71 MMI::HandleEventType eventType, std::vector<int32_t> keys) override; 72 void RemovePreMonitor(int32_t preMonitorId) override; 73 private: 74 bool IsLocalPointerDevice(std::shared_ptr<MMI::InputDevice> device); 75 bool IsVirtualTrackpad(std::shared_ptr<MMI::InputDevice> device); 76 }; 77 78 class PointerFilter : public MMI::IInputEventFilter { 79 public: PointerFilter(std::function<bool (std::shared_ptr<MMI::PointerEvent>)> filter)80 explicit PointerFilter(std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter) 81 : filter_(filter) {} 82 OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)83 bool OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override 84 { 85 return (filter_ != nullptr ? filter_(pointerEvent) : false); 86 } 87 OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)88 bool OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override 89 { 90 return false; 91 } 92 93 private: 94 std::function<bool(std::shared_ptr<MMI::PointerEvent>)> filter_; 95 }; 96 97 class DevListener : public MMI::IInputDeviceListener { 98 public: DevListener(MMIDevListener devAddedCallback,MMIDevListener devRemovedCallback)99 DevListener(MMIDevListener devAddedCallback, MMIDevListener devRemovedCallback) 100 : devAddedCallback_(devAddedCallback), devRemovedCallback_(devRemovedCallback) {} 101 OnDeviceAdded(int32_t deviceId,const std::string & type)102 void OnDeviceAdded(int32_t deviceId, const std::string &type) override 103 { 104 if (devAddedCallback_ != nullptr) { 105 devAddedCallback_(deviceId, type); 106 } 107 } 108 OnDeviceRemoved(int32_t deviceId,const std::string & type)109 void OnDeviceRemoved(int32_t deviceId, const std::string &type) override 110 { 111 if (devRemovedCallback_ != nullptr) { 112 devRemovedCallback_(deviceId, type); 113 } 114 } 115 private: 116 MMIDevListener devAddedCallback_; 117 MMIDevListener devRemovedCallback_; 118 }; 119 120 class InterceptorConsumer : public MMI::IInputEventConsumer { 121 public: InterceptorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback)122 InterceptorConsumer(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback, 123 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback) 124 : pointCallback_(pointCallback), keyCallback_(keyCallback) {} 125 OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)126 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override 127 { 128 if (keyCallback_ != nullptr) { 129 keyCallback_(keyEvent); 130 } 131 } 132 OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)133 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override 134 { 135 if (pointCallback_ != nullptr) { 136 pointCallback_(pointerEvent); 137 } 138 } 139 OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)140 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {} 141 142 private: 143 std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback_; 144 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback_; 145 }; 146 147 class MonitorConsumer : public MMI::IInputEventConsumer { 148 public: MonitorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointCallback,std::function<void (std::shared_ptr<MMI::KeyEvent>)> keyCallback)149 MonitorConsumer(std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback, 150 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback) 151 : pointCallback_(pointCallback), keyCallback_(keyCallback) {} OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)152 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override 153 { 154 if (keyCallback_ != nullptr) { 155 keyCallback_(keyEvent); 156 } 157 } 158 OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)159 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override 160 { 161 if (pointCallback_ != nullptr) { 162 pointCallback_(pointerEvent); 163 } 164 } 165 OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent)166 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {} 167 168 private: 169 std::function<void(std::shared_ptr<MMI::PointerEvent>)> pointCallback_; 170 std::function<void(std::shared_ptr<MMI::KeyEvent>)> keyCallback_; 171 }; 172 } // namespace DeviceStatus 173 } // namespace Msdp 174 } // namespace OHOS 175 #endif // INPUT_ADAPTER_H