/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INPUT_MANAGER_H #define INPUT_MANAGER_H #include #include #include #include "event_handler.h" #include "nocopyable.h" #include "error_multimodal.h" #include "extra_data.h" #include "i_anr_observer.h" #include "i_input_device_listener.h" #include "i_input_event_consumer.h" #include "i_input_event_filter.h" #include "input_device.h" #include "key_option.h" #include "pointer_style.h" #include "window_info.h" namespace OHOS { namespace MMI { class InputManager { public: /** * @brief Obtains an InputManager instance. * @return Returns the pointer to the InputManager instance. * @since 9 */ static InputManager *GetInstance(); virtual ~InputManager() = default; int32_t GetDisplayBindInfo(DisplayBindInfos &infos); int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg); /** * @brief Updates the screen and window information. * @param displayGroupInfo Indicates the logical screen information. * @since 9 */ void UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo); int32_t AddInputEventFilter(std::shared_ptr filter, int32_t priority, uint32_t deviceTags); int32_t RemoveInputEventFilter(int32_t filterId); /** * @brief Sets a consumer for the window input event of the current process. * @param inputEventConsumer Indicates the consumer to set. The window input event of the current process * will be called back to the consumer object for processing. * @since 9 */ void SetWindowInputEventConsumer(std::shared_ptr inputEventConsumer); /** * @brief Sets a window input event consumer that runs on the specified thread. * @param inputEventConsumer Indicates the consumer to set. * @param eventHandler Indicates the thread running the consumer. * @since 9 */ void SetWindowInputEventConsumer(std::shared_ptr inputEventConsumer, std::shared_ptr eventHandler); /** * @brief Subscribes to the key input event that meets a specific condition. When such an event occurs, * the callback specified is invoked to process the event. * @param keyOption Indicates the condition of the key input event. * @param callback Indicates the callback. * @return Returns the subscription ID, which uniquely identifies a subscription in the process. * If the value is greater than or equal to 0, * the subscription is successful. Otherwise, the subscription fails. * @since 9 */ int32_t SubscribeKeyEvent(std::shared_ptr keyOption, std::function)> callback); /** * @brief Unsubscribes from a key input event. * @param subscriberId Indicates the subscription ID, which is the return value of SubscribeKeyEvent. * @return void * @since 9 */ void UnsubscribeKeyEvent(int32_t subscriberId); /** * @brief Subscribes to the switch input event that meets a specific condition. When such an event occurs, * the callback specified is invoked to process the event. * @param callback Indicates the callback. * @return Returns the subscription ID, which uniquely identifies a subscription in the process. * If the value is greater than or equal to 0, * the subscription is successful. Otherwise, the subscription fails. * @since 9 */ int32_t SubscribeSwitchEvent(std::function)> callback); /** * @brief Unsubscribes from a switch input event. * @param subscriberId Indicates the subscription ID, which is the return value of SubscribeKeyEvent. * @return void * @since 9 */ void UnsubscribeSwitchEvent(int32_t subscriberId); /** * @brief Adds an input event monitor. After such a monitor is added, * an input event is copied and distributed to the monitor while being distributed to the original target. * @param monitor Indicates the input event monitor. After an input event is generated, * the functions of the monitor object will be called. * @return Returns the monitor ID, which uniquely identifies a monitor in the process. * If the value is greater than or equal to 0, the monitor is successfully added. Otherwise, * the monitor fails to be added. * @since 9 */ int32_t AddMonitor(std::function)> monitor); /** * @brief Adds an input event monitor. After such a monitor is added, * an input event is copied and distributed to the monitor while being distributed to the original target. * @param monitor Indicates the input event monitor. After an input event is generated, * the functions of the monitor object will be called. * @return Returns the monitor ID, which uniquely identifies a monitor in the process. * If the value is greater than or equal to 0, the monitor is successfully added. Otherwise, * the monitor fails to be added. * @since 9 */ int32_t AddMonitor(std::function)> monitor); /** * @brief Adds an input event monitor. After such a monitor is added, * an input event is copied and distributed to the monitor while being distributed to the original target. * @param monitor Indicates the input event monitor. After an input event is generated, * the functions of the monitor object will be called. * @return Returns the monitor ID, which uniquely identifies a monitor in the process. * If the value is greater than or equal to 0, the monitor is successfully added. Otherwise, * the monitor fails to be added. * @since 9 */ int32_t AddMonitor(std::shared_ptr monitor); /** * @brief Removes a monitor. * @param monitorId Indicates the monitor ID, which is the return value of AddMonitor. * @return void * @since 9 */ void RemoveMonitor(int32_t monitorId); /** * @brief Marks that a monitor has consumed a touchscreen input event. After being consumed, * the touchscreen input event will not be distributed to the original target. * @param monitorId Indicates the monitor ID. * @param eventId Indicates the ID of the consumed touchscreen input event. * @return void * @since 9 */ void MarkConsumed(int32_t monitorId, int32_t eventId); /** * @brief Moves the cursor to the specified position. * @param offsetX Indicates the offset on the X axis. * @param offsetY Indicates the offset on the Y axis. * @return void * @since 9 */ void MoveMouse(int32_t offsetX, int32_t offsetY); /** * @brief Adds an input event interceptor. After such an interceptor is added, * an input event will be distributed to the interceptor instead of the original target and monitor. * @param interceptor Indicates the input event interceptor. After an input event is generated, * the functions of the interceptor object will be called. * @return Returns the interceptor ID, which uniquely identifies an interceptor in the process. * If the value is greater than or equal to 0,the interceptor is successfully added. Otherwise, * the interceptor fails to be added. * @since 9 */ int32_t AddInterceptor(std::shared_ptr interceptor); int32_t AddInterceptor(std::function)> interceptor); int32_t AddInterceptor(std::shared_ptr interceptor, int32_t priority, uint32_t deviceTags); /** * @brief Removes an interceptor. * @param interceptorId Indicates the interceptor ID, which is the return value of AddInterceptor. * @return void * @since 9 */ void RemoveInterceptor(int32_t interceptorId); /** * @brief Simulates a key input event. This event will be distributed and * processed in the same way as the event reported by the input device. * @param keyEvent Indicates the key input event to simulate. * @return void * @since 9 */ void SimulateInputEvent(std::shared_ptr keyEvent); /** * @brief Simulates a touchpad input event, touchscreen input event, or mouse device input event. * This event will be distributed and processed in the same way as the event reported by the input device. * @param pointerEvent Indicates the touchpad input event, touchscreen input event, * or mouse device input event to simulate. * @return void * @since 9 */ void SimulateInputEvent(std::shared_ptr pointerEvent); /** * @brief Simulates a key input event. * This event will be sent to hmos container. * @param keyEvent Indicates the key input event to simulate. * @return void * @since 9 */ void SimulateInputEventToHmosContainer(std::shared_ptr keyEvent); /** * @brief Simulates a touchpad input event, touchscreen input event, or mouse device input event. * This event will be sent to hmos container. * @param pointerEvent Indicates the touchpad input event, touchscreen input event, * or mouse device input event to simulate. * @return void * @since 9 */ void SimulateInputEventToHmosContainer(std::shared_ptr pointerEvent); /** * @brief Starts listening for an input device event. * @param type Indicates the type of the input device event, which is change. * @param listener Indicates the listener for the input device event. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t RegisterDevListener(std::string type, std::shared_ptr listener); /** * @brief Stops listening for an input device event. * @param type Indicates the type of the input device event, which is change. * @param listener Indicates the listener for the input device event. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t UnregisterDevListener(std::string type, std::shared_ptr listener = nullptr); /** * @brief Obtains the information about an input device. * @param callback Indicates the callback used to receive the reported data. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetDeviceIds(std::function&)> callback); /** * @brief Obtains the information about an input device. * @param deviceId Indicates the ID of the input device whose information is to be obtained. * @param callback Indicates the callback used to receive the reported data. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetDevice(int32_t deviceId, std::function)> callback); /** * @brief Checks whether the specified key codes of an input device are supported. * @param deviceId Indicates the ID of the input device. * @param keyCodes Indicates the key codes of the input device. * @param callback Indicates the callback used to receive the reported data. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SupportKeys(int32_t deviceId, std::vector keyCodes, std::function&)> callback); /** * @brief Sets the number of the mouse scrolling rows. * @param rows Indicates the number of the mouse scrolling rows. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetMouseScrollRows(int32_t rows); /** * @brief Set pixelMap to override ohos mouse icon resouce. * @param windowId Indicates the windowId of the window * @param pixelMap Indicates the image resouce for this mouse icon. which realtype must be OHOS::Media::PixelMap* * @return vint32_t * @since 10 */ int32_t SetMouseIcon(int32_t windowId, void* pixelMap); /** * @brief Set mouse icon hot spot. * @param windowId Indicates the windowId of the window * @param hotSpotX Indicates the hot spot x for this mouse icon. * @param hotSpotY Indicates the hot spot y for this mouse icon. * @return vint32_t * @since 10 */ int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY); /** * @brief Gets the number of the mouse scrolling rows. * @param rows Indicates the number of the mouse scrolling rows. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetMouseScrollRows(int32_t &rows); /** * @brief Sets pointer size. * @param size Indicates pointer size. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetPointerSize(int32_t size); /** * @brief Gets pointer size. * @param size Indicates pointer size. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetPointerSize(int32_t &size); /** * @brief Sets mouse primary button. * @param primaryButton Indicates the ID of the mouse primary button.The value 0 indicates that * the primary button is left button.The value 1 indicates that the primary button is right button. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetMousePrimaryButton(int32_t primaryButton); /** * @brief Gets mouse primary button. * @param primaryButton Indicates the ID of the mouse primary button.The value 0 indicates that * the primary button is left button.The value 1 indicates that the primary button is right button. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetMousePrimaryButton(int32_t &primaryButton); /** * @brief Sets whether the mouse hover scroll is enabled in inactive window. * @param state Indicates whether the mouse hover scroll is enabled in inactive window. The value true * indicates that the mouse hover scroll is enabled, and the value false indicates the opposite. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetHoverScrollState(bool state); /** * @brief Gets a status whether the mouse hover scroll is enabled in inactive window. * @param state Indicates whether the mouse hover scroll is enabled in inactive window. The value true * indicates that the mouse hover scroll is enabled, and the value false indicates the opposite. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetHoverScrollState(bool &state); /** * @brief Sets whether the pointer icon is visible. * @param visible Indicates whether the pointer icon is visible. The value true indicates that * the pointer icon is visible, and the value false indicates the opposite. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetPointerVisible(bool visible); /** * @brief Checks whether the pointer icon is visible. * @return Returns true if the pointer icon is visible; returns false otherwise. * @since 9 */ bool IsPointerVisible(); /** * @brief Sets the mouse pointer style. * @param windowId Indicates the ID of the window for which the mouse pointer style is set. * @param pointerStyle Indicates the ID of the mouse pointer style. * @return Returns 0 if the operation is successful; returns an error code otherwise. * @since 9 */ int32_t SetPointerStyle(int32_t windowId, PointerStyle pointerStyle); /** * @brief Obtains the mouse pointer style. * @param windowId Indicates the ID of the window for which the mouse pointer style is obtained. * @param pointerStyle Indicates the ID of the mouse pointer style. * @return Returns 0 if the operation is successful; returns an error code otherwise. * @since 9 */ int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle); /** * @brief Sets pointer color. * @param color Indicates pointer color. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetPointerColor(int32_t color); /** * @brief Gets pointer color. * @param color Indicates pointer color. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetPointerColor(int32_t &color); /** * @brief Sets the mouse pointer speed, which ranges from 1 to 11. * @param speed Indicates the mouse pointer speed to set. * @return Returns RET_OK if success; returns RET_ERR otherwise. * @since 9 */ int32_t SetPointerSpeed(int32_t speed); /** * @brief Obtains the mouse pointer speed. * @param speed Indicates the mouse pointer speed to get. * @return Returns the mouse pointer speed if the operation is successful; returns RET_ERR otherwise. * @since 9 */ int32_t GetPointerSpeed(int32_t &speed); /** * @brief Queries the keyboard type. * @param deviceId Indicates the keyboard device ID. * @param callback Callback used to return the keyboard type. * @return Returns 0 if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetKeyboardType(int32_t deviceId, std::function callback); /** * @brief Sets the observer for events indicating that the application does not respond. * @param observer Indicates the observer for events indicating that the application does not respond. * @return void * @since 9 */ void SetAnrObserver(std::shared_ptr observer); /** * @brief Obtains the enablement status of the specified function key on the keyboard. * @param funcKey Indicates the function key. Currently, the following function keys are supported: * NUM_LOCK_FUNCTION_KEY * CAPS_LOCK_FUNCTION_KEY * SCROLL_LOCK_FUNCTION_KEY * @return Returns true if the function key is enabled; * returns false otherwise. */ bool GetFunctionKeyState(int32_t funcKey); /** * @brief Sets the enablement status of the specified function key on the keyboard. * @param funcKey Indicates the function key. Currently, the following function keys are supported: * NUM_LOCK_FUNCTION_KEY * CAPS_LOCK_FUNCTION_KEY * SCROLL_LOCK_FUNCTION_KEY * @param isEnable Indicates the enablement status to set. * @return Returns 0 if success; returns a non-0 value otherwise. */ int32_t SetFunctionKeyState(int32_t funcKey, bool enable); /** * @brief Sets the absolute coordinate of mouse. * @param x Specifies the x coordinate of the mouse to be set. * @param y Specifies the y coordinate of the mouse to be set. * @return void */ void SetPointerLocation(int32_t x, int32_t y); /** * @brief 进入捕获模式 * @param windowId 窗口id. * @return 进入捕获模式成功或失败. * @since 9 */ int32_t EnterCaptureMode(int32_t windowId); /** * @brief 退出捕获模式 * @param windowId 窗口id. * @return 退出捕获模式成功或失败. * @since 9 */ int32_t LeaveCaptureMode(int32_t windowId); int32_t GetWindowPid(int32_t windowId); /** * @brief pointer event添加辅助信息 * @param extraData 添加的信息. * @return void * @since 9 */ void AppendExtraData(const ExtraData& extraData); /** * @brief 使能或者禁用输入设备 * @param enable 输入设备的使能状态 * @return 返回0表示接口调用成功,否则,表示接口调用失败。 * @since 9 */ int32_t EnableInputDevice(bool enable); /** * @brief 自定义设置快捷键拉起ability延迟时间 * @param businessId 应用在ability_launch_config.json中注册的唯一标识符. * @param delay 延迟时间 0-4000ms * @return 设置快捷键拉起ability延迟时间成功或失败 * @since 10 */ int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay); /** * @brief Sets the keyboard repeat delay, which ranges from 300 to 1000. * @param delay Indicates the keyboard repeat delay to set. * @return Returns RET_OK if success; returns RET_ERR otherwise. * @since 10 */ int32_t SetKeyboardRepeatDelay(int32_t delay); /** * @brief Sets the keyboard repeat rate, which ranges from 36 to 100. * @param rate Indicates the keyboard repeat rate to set. * @return Returns RET_OK if success; returns RET_ERR otherwise. * @since 10 */ int32_t SetKeyboardRepeatRate(int32_t rate); /** * @brief Gets the keyboard repeat delay. * @param callback Callback used to return the keyboard repeat delay. * @return Returns RET_OK if success; returns RET_ERR otherwise. * @since 10 */ int32_t GetKeyboardRepeatDelay(std::function callback); /** * @brief Gets the keyboard repeat rate. * @param callback Callback used to return the keyboard repeat rate. * @return Returns RET_OK if success; returns RET_ERR otherwise. * @since 10 */ int32_t GetKeyboardRepeatRate(std::function callback); /** * @brief Set the switch of touchpad scroll. * @param switchFlag Indicates the touchpad scroll switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadScrollSwitch(bool switchFlag); /** * @brief Get the switch of touchpad scroll. * @param switchFlag Indicates the touchpad scroll switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadScrollSwitch(bool &switchFlag); /** * @brief Set the switch of touchpad scroll direction. * @param state Indicates the touchpad scroll switch direction state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadScrollDirection(bool state); /** * @brief Get the switch of touchpad scroll direction. * @param state Indicates the touchpad scroll switch direction state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadScrollDirection(bool &state); /** * @brief Set the switch of touchpad tap. * @param switchFlag Indicates the touchpad tap switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadTapSwitch(bool switchFlag); /** * @brief Get the switch of touchpad tap. * @param switchFlag Indicates the touchpad tap switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadTapSwitch(bool &switchFlag); /** * @brief Set the touchpad poniter speed. * @param speed Indicates the touchpad pointer speed. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadPointerSpeed(int32_t speed); /** * @brief Get the touchpad poniter speed. * @param speed Indicates the touchpad pointer speed. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadPointerSpeed(int32_t &speed); /** * @brief Set the switch of touchpad pinch. * @param switchFlag Indicates the touchpad pinch switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadPinchSwitch(bool switchFlag); /** * @brief Get the switch of touchpad pinch. * @param switchFlag Indicates the touchpad pinch switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadPinchSwitch(bool &switchFlag); /** * @brief Set the switch of touchpad swipe. * @param switchFlag Indicates the touchpad swipe switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadSwipeSwitch(bool switchFlag); /** * @brief Get the switch of touchpad swipe. * @param switchFlag Indicates the touchpad swipe switch state. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadSwipeSwitch(bool &switchFlag); /** * @brief Set the touchpad right click type. * @param type Indicates the touchpad right menu type. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t SetTouchpadRightClickType(int32_t type); /** * @brief Get the touchpad right click type. * @param type Indicates the touchpad right menu type. * @return if success; returns a non-0 value otherwise. * @since 9 */ int32_t GetTouchpadRightClickType(int32_t &type); #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT /** * @brief Sets the enhance config of the security component. * @param cfg Indicates the security component enhance config. * @param cfgLen Indicates the security component enhance config len. * @return void. * @since 9 */ void SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen); #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT private: InputManager() = default; DISALLOW_COPY_AND_MOVE(InputManager); static InputManager *instance_; }; } // namespace MMI } // namespace OHOS #endif // INPUT_MANAGER_H