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_H 17 #define INPUT_MANAGER_H 18 19 #include <memory> 20 #include <list> 21 22 #include "display_info.h" 23 #include "error_multimodal.h" 24 #include "i_input_event_consumer.h" 25 #include "key_option.h" 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace MMI { 30 class InputManager { 31 public: 32 /** 33 * @brief Obtains an <b>InputManager</b> instance. 34 * @return Returns the pointer to the <b>InputManager</b> instance. 35 * @since 8 36 */ 37 static InputManager *GetInstance(); 38 virtual ~InputManager() = default; 39 40 /** 41 * @brief Updates the screen and window information. 42 * @param physicalDisplays Indicates the physical screen information. 43 * @param logicalDisplays Indicates the logical screen information, which includes the window information. 44 * @since 8 45 */ 46 void UpdateDisplayInfo(const std::vector<PhysicalDisplayInfo> &physicalDisplays, 47 const std::vector<LogicalDisplayInfo> &logicalDisplays); 48 49 /** 50 * @brief Sets a globally unique input event filter. 51 * @param filter Indicates the input event filter to set. When an input event occurs, this filter is 52 * called and returns a value indicating whether to continue processing the input event.If the filter 53 * returns <b>true</b>, the processing of the input event ends. If the filter returns <b>false</b>, 54 * the processing of the input event continues. 55 * @return return Returns a value greater than or equal to <b>0</b> if the input event filter is added 56 * successfully; returns a value less than <b>0</b> otherwise. 57 * @since 8 58 */ 59 int32_t AddInputEventFilter(std::function<bool(std::shared_ptr<PointerEvent>)> filter); 60 61 /** 62 * @brief Sets a consumer for the window input event of the current process. 63 * @param inputEventConsumer Indicates the consumer to set. The window input event of the current process 64 * will be called back to the consumer object for processing. 65 * @since 8 66 */ 67 void SetWindowInputEventConsumer(std::shared_ptr<OHOS::MMI::IInputEventConsumer> inputEventConsumer); 68 69 /** 70 * @brief Subscribes to the key input event that meets a specific condition. When such an event occurs, 71 * the <b>callback</b> specified is invoked to process the event. 72 * @param keyOption Indicates the condition of the key input event. 73 * @param callback Indicates the callback. 74 * @return Returns the subscription ID, which uniquely identifies a subscription in the process. 75 * If the value is greater than or equal to <b>0</b>, 76 * the subscription is successful. Otherwise, the subscription fails. 77 * @since 8 78 */ 79 int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption, 80 std::function<void(std::shared_ptr<KeyEvent>)> callback); 81 82 /** 83 * @brief Unsubscribes from a key input event. 84 * @param subscriberId Indicates the subscription ID, which is the return value of <b>SubscribeKeyEvent</b>. 85 * @return void 86 * @since 8 87 */ 88 void UnsubscribeKeyEvent(int32_t subscriberId); 89 90 /** 91 * @brief Adds an input event monitor. After such a monitor is added, 92 * an input event is copied and distributed to the monitor while being distributed to the original target. 93 * @param monitor Indicates the input event monitor. After an input event is generated, 94 * the functions of the monitor object will be called. 95 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 96 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 97 * the monitor fails to be added. 98 * @since 8 99 */ 100 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 101 102 /** 103 * @brief Adds an input event monitor. After such a monitor is added, 104 * an input event is copied and distributed to the monitor while being distributed to the original target. 105 * @param monitor Indicates the input event monitor. After an input event is generated, 106 * the functions of the monitor object will be called. 107 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 108 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 109 * the monitor fails to be added. 110 * @since 8 111 */ 112 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 113 114 /** 115 * @brief Adds an input event monitor. After such a monitor is added, 116 * an input event is copied and distributed to the monitor while being distributed to the original target. 117 * @param monitor Indicates the input event monitor. After an input event is generated, 118 * the functions of the monitor object will be called. 119 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 120 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 121 * the monitor fails to be added. 122 * @since 8 123 */ 124 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> monitor); 125 126 /** 127 * @brief Removes a monitor. 128 * @param monitorId Indicates the monitor ID, which is the return value of <b>AddMonitor</b>. 129 * @return void 130 * @since 8 131 */ 132 void RemoveMonitor(int32_t monitorId); 133 134 /** 135 * @brief Marks that a monitor has consumed a touchscreen input event. After being consumed, 136 * the touchscreen input event will not be distributed to the original target. 137 * @param monitorId Indicates the monitor ID. 138 * @param eventId Indicates the ID of the consumed touchscreen input event. 139 * @return void 140 * @since 8 141 */ 142 void MarkConsumed(int32_t monitorId, int32_t eventId); 143 144 /** 145 * @brief Adds an input event interceptor. After such an interceptor is added, 146 * an input event will be distributed to the interceptor instead of the original target and monitor. 147 * @param interceptor Indicates the input event interceptor. After an input event is generated, 148 * the functions of the interceptor object will be called. 149 * @return Returns the interceptor ID, which uniquely identifies an interceptor in the process. 150 * If the value is greater than or equal to <b>0</b>,the interceptor is successfully added. Otherwise, 151 * the interceptor fails to be added. 152 * @since 8 153 */ 154 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor); 155 int32_t AddInterceptor(int32_t sourceType, std::function<void(std::shared_ptr<PointerEvent>)> interceptor); 156 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor); 157 158 /** 159 * @brief Removes an interceptor. 160 * @param interceptorId Indicates the interceptor ID, which is the return value of <b>AddInterceptor</b>. 161 * @return void 162 * @since 8 163 */ 164 void RemoveInterceptor(int32_t interceptorId); 165 166 /** 167 * @brief Simulates a key input event. This event will be distributed and 168 * processed in the same way as the event reported by the input device. 169 * @param keyEvent Indicates the key input event to simulate. 170 * @return void 171 * @since 8 172 */ 173 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent); 174 175 /** 176 * @brief Simulates a touchpad input event, touchscreen input event, or mouse device input event. 177 * This event will be distributed and processed in the same way as the event reported by the input device. 178 * @param pointerEvent Indicates the touchpad input event, touchscreen input event, 179 * or mouse device input event to simulate. 180 * @return void 181 * @since 8 182 */ 183 void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 184 185 private: 186 InputManager() = default; 187 DISALLOW_COPY_AND_MOVE(InputManager); 188 static InputManager *instance_; 189 }; 190 } // namespace MMI 191 } // namespace OHOS 192 #endif // INPUT_MANAGER_H