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 <map> 20 #include <memory> 21 #include <list> 22 23 #include "event_handler.h" 24 #include "nocopyable.h" 25 26 #include "cooperation_message.h" 27 #include "i_anr_observer.h" 28 #include "display_info.h" 29 #include "error_multimodal.h" 30 #include "i_input_device_listener.h" 31 #include "i_input_device_cooperate_listener.h" 32 #include "i_input_event_consumer.h" 33 #include "input_device.h" 34 #include "key_option.h" 35 36 namespace OHOS { 37 namespace MMI { 38 class InputManager { 39 public: 40 /** 41 * @brief Obtains an <b>InputManager</b> instance. 42 * @return Returns the pointer to the <b>InputManager</b> instance. 43 * @since 9 44 */ 45 static InputManager *GetInstance(); 46 virtual ~InputManager() = default; 47 48 /** 49 * @brief Updates the screen and window information. 50 * @param displayGroupInfo Indicates the logical screen information. 51 * @since 9 52 */ 53 void UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); 54 55 /** 56 * @brief Sets a globally unique input event filter. 57 * @param filter Indicates the input event filter to set. When an input event occurs, this filter is 58 * called and returns a value indicating whether to continue processing the input event.If the filter 59 * returns <b>true</b>, the processing of the input event ends. If the filter returns <b>false</b>, 60 * the processing of the input event continues. 61 * @return return Returns a value greater than or equal to <b>0</b> if the input event filter is added 62 * successfully; returns a value less than <b>0</b> otherwise. 63 * @since 9 64 */ 65 int32_t AddInputEventFilter(std::function<bool(std::shared_ptr<PointerEvent>)> filter); 66 67 /** 68 * @brief Sets a consumer for the window input event of the current process. 69 * @param inputEventConsumer Indicates the consumer to set. The window input event of the current process 70 * will be called back to the consumer object for processing. 71 * @since 9 72 */ 73 void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer); 74 75 /** 76 * @brief Sets a window input event consumer that runs on the specified thread. 77 * @param inputEventConsumer Indicates the consumer to set. 78 * @param eventHandler Indicates the thread running the consumer. 79 * @since 9 80 */ 81 void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer, 82 std::shared_ptr<AppExecFwk::EventHandler> eventHandler); 83 84 /** 85 * @brief Subscribes to the key input event that meets a specific condition. When such an event occurs, 86 * the <b>callback</b> specified is invoked to process the event. 87 * @param keyOption Indicates the condition of the key input event. 88 * @param callback Indicates the callback. 89 * @return Returns the subscription ID, which uniquely identifies a subscription in the process. 90 * If the value is greater than or equal to <b>0</b>, 91 * the subscription is successful. Otherwise, the subscription fails. 92 * @since 9 93 */ 94 int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption, 95 std::function<void(std::shared_ptr<KeyEvent>)> callback); 96 97 /** 98 * @brief Unsubscribes from a key input event. 99 * @param subscriberId Indicates the subscription ID, which is the return value of <b>SubscribeKeyEvent</b>. 100 * @return void 101 * @since 9 102 */ 103 void UnsubscribeKeyEvent(int32_t subscriberId); 104 105 /** 106 * @brief Adds an input event monitor. After such a monitor is added, 107 * an input event is copied and distributed to the monitor while being distributed to the original target. 108 * @param monitor Indicates the input event monitor. After an input event is generated, 109 * the functions of the monitor object will be called. 110 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 111 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 112 * the monitor fails to be added. 113 * @since 9 114 */ 115 int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor); 116 117 /** 118 * @brief Adds an input event monitor. After such a monitor is added, 119 * an input event is copied and distributed to the monitor while being distributed to the original target. 120 * @param monitor Indicates the input event monitor. After an input event is generated, 121 * the functions of the monitor object will be called. 122 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 123 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 124 * the monitor fails to be added. 125 * @since 9 126 */ 127 int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor); 128 129 /** 130 * @brief Adds an input event monitor. After such a monitor is added, 131 * an input event is copied and distributed to the monitor while being distributed to the original target. 132 * @param monitor Indicates the input event monitor. After an input event is generated, 133 * the functions of the monitor object will be called. 134 * @return Returns the monitor ID, which uniquely identifies a monitor in the process. 135 * If the value is greater than or equal to <b>0</b>, the monitor is successfully added. Otherwise, 136 * the monitor fails to be added. 137 * @since 9 138 */ 139 int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> monitor); 140 141 /** 142 * @brief Removes a monitor. 143 * @param monitorId Indicates the monitor ID, which is the return value of <b>AddMonitor</b>. 144 * @return void 145 * @since 9 146 */ 147 void RemoveMonitor(int32_t monitorId); 148 149 /** 150 * @brief Marks that a monitor has consumed a touchscreen input event. After being consumed, 151 * the touchscreen input event will not be distributed to the original target. 152 * @param monitorId Indicates the monitor ID. 153 * @param eventId Indicates the ID of the consumed touchscreen input event. 154 * @return void 155 * @since 9 156 */ 157 void MarkConsumed(int32_t monitorId, int32_t eventId); 158 159 /** 160 * @brief Moves the cursor to the specified position. 161 * @param offsetX Indicates the offset on the X axis. 162 * @param offsetY Indicates the offset on the Y axis. 163 * @return void 164 * @since 9 165 */ 166 void MoveMouse(int32_t offsetX, int32_t offsetY); 167 168 /** 169 * @brief Adds an input event interceptor. After such an interceptor is added, 170 * an input event will be distributed to the interceptor instead of the original target and monitor. 171 * @param interceptor Indicates the input event interceptor. After an input event is generated, 172 * the functions of the interceptor object will be called. 173 * @return Returns the interceptor ID, which uniquely identifies an interceptor in the process. 174 * If the value is greater than or equal to <b>0</b>,the interceptor is successfully added. Otherwise, 175 * the interceptor fails to be added. 176 * @since 9 177 */ 178 int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor); 179 int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor); 180 181 /** 182 * @brief Removes an interceptor. 183 * @param interceptorId Indicates the interceptor ID, which is the return value of <b>AddInterceptor</b>. 184 * @return void 185 * @since 9 186 */ 187 void RemoveInterceptor(int32_t interceptorId); 188 189 /** 190 * @brief Simulates a key input event. This event will be distributed and 191 * processed in the same way as the event reported by the input device. 192 * @param keyEvent Indicates the key input event to simulate. 193 * @return void 194 * @since 9 195 */ 196 void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent); 197 198 /** 199 * @brief Simulates a touchpad input event, touchscreen input event, or mouse device input event. 200 * This event will be distributed and processed in the same way as the event reported by the input device. 201 * @param pointerEvent Indicates the touchpad input event, touchscreen input event, 202 * or mouse device input event to simulate. 203 * @return void 204 * @since 9 205 */ 206 void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent); 207 208 /** 209 * @brief Starts listening for an input device event. 210 * @param type Indicates the type of the input device event, which is <b>change</b>. 211 * @param listener Indicates the listener for the input device event. 212 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 213 * @since 9 214 */ 215 int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener); 216 217 /** 218 * @brief Stops listening for an input device event. 219 * @param type Indicates the type of the input device event, which is <b>change</b>. 220 * @param listener Indicates the listener for the input device event. 221 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 222 * @since 9 223 */ 224 int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr); 225 226 /** 227 * @brief Obtains the information about an input device. 228 * @param callback Indicates the callback used to receive the reported data. 229 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 230 * @since 9 231 */ 232 int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback); 233 234 /** 235 * @brief Obtains the information about an input device. 236 * @param deviceId Indicates the ID of the input device whose information is to be obtained. 237 * @param callback Indicates the callback used to receive the reported data. 238 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 239 * @since 9 240 */ 241 int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback); 242 243 /** 244 * @brief Checks whether the specified key codes of an input device are supported. 245 * @param deviceId Indicates the ID of the input device. 246 * @param keyCodes Indicates the key codes of the input device. 247 * @param callback Callback function, receive reported data. 248 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 249 * @since 9 250 */ 251 int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> keyCodes, 252 std::function<void(std::vector<bool>&)> callback); 253 254 /** 255 * @brief Sets whether the pointer icon is visible. 256 * @param visible Whether the pointer icon is visible. The value <b>true</b> indicates that 257 * the pointer icon is visible, and the value <b>false</b> indicates the opposite. 258 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 259 * @since 9 260 */ 261 int32_t SetPointerVisible(bool visible); 262 263 /** 264 * @brief Checks whether the pointer icon is visible. 265 * @return Returns <b>true</b> if the pointer icon is visible; returns <b>false</b> otherwise. 266 * @since 9 267 */ 268 bool IsPointerVisible(); 269 270 /** 271 * @brief 设置鼠标指针样式 272 * @param windowId 指定要更改鼠标指针样式的窗口ID 273 * @param pointerStyle 指定要更改的鼠标指针样式ID 274 * @return 成功返回0,否则返回失败 275 * @since 9 276 */ 277 int32_t SetPointerStyle(int32_t windowId, int32_t pointerStyle); 278 279 /** 280 * @brief 获取鼠标指针样式 281 * @param windowId 指定要获取鼠标指针样式的窗口ID 282 * @param pointerStyle 返回获取的鼠标指针样式ID 283 * @return 成功返回0,否则返回失败 284 * @since 9 285 */ 286 int32_t GetPointerStyle(int32_t windowId, int32_t &pointerStyle); 287 288 /** 289 * @brief Sets the mouse pointer speed, which ranges from 1 to 11. 290 * @param speed Indicates the mouse pointer speed to set. 291 * @return Returns <b>RET_OK</b> if success; returns <b>RET_ERR</b> otherwise. 292 * @since 9 293 */ 294 int32_t SetPointerSpeed(int32_t speed); 295 296 /** 297 * @brief Obtains the mouse pointer speed. 298 * @param speed Indicates the mouse pointer speed to get. 299 * @return Returns the mouse pointer speed if the operation is successful; returns <b>RET_ERR</b> otherwise. 300 * @since 9 301 */ 302 int32_t GetPointerSpeed(int32_t &speed); 303 304 /** 305 * @brief Queries the keyboard type. 306 * @param deviceId Indicates the keyboard device ID. 307 * @param callback Callback used to return the keyboard type. 308 * @return Returns <b>0</b> if success; returns a non-0 value otherwise. 309 * @since 9 310 */ 311 int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback); 312 313 /** 314 * @brief Sets the observer for events indicating that the application does not respond. 315 * @param observer Indicates the observer for events indicating that the application does not respond. 316 * @return void 317 * @since 9 318 */ 319 void SetAnrObserver(std::shared_ptr<IAnrObserver> observer); 320 321 /** 322 * @brief 设置指定输入设备对应的屏幕ID 323 * @param dhid 输入设备唯一ID 324 * @param screenId 输入设备对应的屏幕ID 325 * @return 0表示返回成功,否则表示返回失败 326 */ 327 int32_t SetInputDevice(const std::string& dhid, const std::string& screenId); 328 329 /** 330 * @brief 注册键鼠穿越管理事件监听。 331 * @param listener 穿越管理事件监听回调。 332 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 333 * @since 9 334 */ 335 int32_t RegisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener); 336 337 /** 338 * @brief 注销键鼠穿越管理事件监听。 339 * @param listener 事件监听回调. 340 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 341 * @since 9 342 */ 343 int32_t UnregisterCooperateListener(std::shared_ptr<IInputDeviceCooperateListener> listener = nullptr); 344 345 /** 346 * @brief 开启/关闭键鼠穿越管理接口。 347 * @param enabled 开启/关闭。 348 * @param callback 开启/关闭键鼠穿越,此回调被调用 349 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 350 * @since 9 351 */ 352 int32_t EnableInputDeviceCooperate(bool enabled, std::function<void(std::string, CooperationMessage)> callback); 353 354 /** 355 * @brief 启动跨设备键鼠穿越。 356 * @param sinkDeviceId 键鼠穿越目标设备描述符(networkID) 357 * @param srcInputDeviceId 键鼠穿越待穿越输入外设标识符(设备ID句柄) 358 * @param callback 启动跨设备键鼠穿越,此回调被调用 359 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 360 * @since 9 361 */ 362 int32_t StartInputDeviceCooperate(const std::string &sinkDeviceId, int32_t srcInputDeviceId, 363 std::function<void(std::string, CooperationMessage)> callback); 364 365 /** 366 * @brief 停止跨设备键鼠穿越。 367 * @param callback 停止跨设备键鼠穿越,此回调被调用 368 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 369 * @since 9 370 */ 371 int32_t StopDeviceCooperate(std::function<void(std::string, CooperationMessage)> callback); 372 373 /** 374 * @brief 获取指定设备键鼠穿越状态。 375 * @param deviceId 指定设备描述符。 376 * @param callback 获取穿越管理设备状态,此回调被调用 377 * @return 返回值如果是0表示接口调用成功,返回其他值表示接口调用失败。 378 * @since 9 379 */ 380 int32_t GetInputDeviceCooperateState(const std::string &deviceId, std::function<void(bool)> callback); 381 382 /** 383 * @brief 获取键盘设备指定功能按键的使能状态。 384 * @param funcKey 指定的功能按键,当前支持的功能按键有: 385 * NUM_LOCK_FUNCTION_KEY 386 * CAPS_LOCK_FUNCTION_KEY 387 * SCROLL_LOCK_FUNCTION_KEY。 388 * @return 返回功能按键的使能状态,true表示功能按键使能, 389 * false表示功能按键未使能。 390 */ 391 bool GetFunctionKeyState(int32_t funcKey); 392 393 /** 394 * @brief 设置键盘设备指定功能按键的使能状态。 395 * @param funcKey 指定的功能按键,当前支持的功能按键有: 396 * NUM_LOCK_FUNCTION_KEY 397 * CAPS_LOCK_FUNCTION_KEY 398 * SCROLL_LOCK_FUNCTION_KEY。 399 * @param isEnable 待设置的使能状态。 400 * @return 0 表示设置成功,其他值表示设置失败。 401 */ 402 int32_t SetFunctionKeyState(int32_t funcKey, bool enable); 403 404 private: 405 InputManager() = default; 406 DISALLOW_COPY_AND_MOVE(InputManager); 407 static InputManager *instance_; 408 }; 409 } // namespace MMI 410 } // namespace OHOS 411 #define InputMgr OHOS::MMI::InputManager::GetInstance() 412 #endif // INPUT_MANAGER_H 413