• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 <list>
20 #include <vector>
21 
22 #include "singleton.h"
23 
24 #include "net_packet.h"
25 
26 #include "window_info.h"
27 #include "nap_process.h"
28 #include "event_filter_service.h"
29 #include "event_handler.h"
30 #include "extra_data.h"
31 #include "if_mmi_client.h"
32 #include "input_device_impl.h"
33 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
34 #include "input_interceptor_manager.h"
35 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
36 #ifdef OHOS_BUILD_ENABLE_MONITOR
37 #include "input_monitor_manager.h"
38 #endif // OHOS_BUILD_ENABLE_MONITOR
39 #include "i_anr_observer.h"
40 #include "i_input_service_watcher.h"
41 #include "mmi_event_observer.h"
42 #include "i_window_checker.h"
43 #include "key_option.h"
44 #include "pointer_event.h"
45 #include "pointer_style.h"
46 #include "switch_event.h"
47 
48 namespace OHOS {
49 namespace MMI {
50 class InputManagerImpl final {
51     DECLARE_SINGLETON(InputManagerImpl);
52 
53 public:
54     DISALLOW_MOVE(InputManagerImpl);
55 
56     int32_t GetDisplayBindInfo(DisplayBindInfos &infos);
57     int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas);
58     int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg);
59     int32_t GetWindowPid(int32_t windowId);
60     void UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo);
61     void UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo);
62     void SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId);
63 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
64     void SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen);
65 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
66     int32_t SubscribeKeyEvent(
67         std::shared_ptr<KeyOption> keyOption,
68         std::function<void(std::shared_ptr<KeyEvent>)> callback
69     );
70     void UnsubscribeKeyEvent(int32_t subscriberId);
71     int32_t SubscribeSwitchEvent(std::function<void(std::shared_ptr<SwitchEvent>)> callback);
72     void UnsubscribeSwitchEvent(int32_t subscriberId);
73     int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags);
74     int32_t RemoveInputEventFilter(int32_t filterId);
75     int32_t AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
76     int32_t RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
77     int32_t NotifyNapOnline();
78     void NotifyBundleName(int32_t pid, int32_t uid, std::string bundleName, int32_t syncStatus);
79     void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,
80         std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
81     void ClearWindowPointerStyle(int32_t pid, int32_t windowId);
82     void SetWindowCheckerHandler(std::shared_ptr<IWindowChecker> windowChecker);
83     int32_t SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napStatus);
84 
85 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
86     void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
87 #endif // OHOS_BUILD_ENABLE_KEYBOARD
88 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
89     void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
90 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
91     int32_t PackDisplayData(NetPacket &pkt);
92 
93     int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor);
94     int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor);
95     int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer);
96 
97     void RemoveMonitor(int32_t monitorId);
98     void MarkConsumed(int32_t monitorId, int32_t eventId);
99     void MoveMouse(int32_t offsetX, int32_t offsetY);
100 
101     int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor,
102         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
103         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
104     int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor,
105         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
106         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
107     void RemoveInterceptor(int32_t interceptorId);
108 
109     void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent);
110     void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent);
111     void OnConnected();
112 
113     int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener);
114     int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr);
115     int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback);
116     int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback);
117     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes,
118         std::function<void(std::vector<bool>&)> callback);
119     int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback);
120     int32_t SetKeyboardRepeatDelay(int32_t delay);
121     int32_t SetKeyboardRepeatRate(int32_t rate);
122     int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback);
123     int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback);
124 
125     int32_t SetMouseScrollRows(int32_t rows);
126     int32_t GetMouseScrollRows(int32_t &rows);
127     int32_t SetPointerSize(int32_t size);
128     int32_t GetPointerSize(int32_t &size);
129     int32_t SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap);
130     int32_t SetMouseIcon(int32_t windowId, void* pixelMap);
131     int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY);
132     int32_t SetMousePrimaryButton(int32_t primaryButton);
133     int32_t GetMousePrimaryButton(int32_t &primaryButton);
134     int32_t SetHoverScrollState(bool state);
135     int32_t GetHoverScrollState(bool &state);
136 
137     int32_t SetPointerVisible(bool visible);
138     bool IsPointerVisible();
139     int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle);
140     int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle);
141 
142     int32_t SetPointerColor(int32_t color);
143     int32_t GetPointerColor(int32_t &color);
144     int32_t EnableCombineKey(bool enable);
145     int32_t SetPointerSpeed(int32_t speed);
146     int32_t GetPointerSpeed(int32_t &speed);
147 
148     int32_t SetTouchpadScrollSwitch(bool switchFlag);
149     int32_t GetTouchpadScrollSwitch(bool &switchFlag);
150     int32_t SetTouchpadScrollDirection(bool state);
151     int32_t GetTouchpadScrollDirection(bool &state);
152     int32_t SetTouchpadTapSwitch(bool switchFlag);
153     int32_t GetTouchpadTapSwitch(bool &switchFlag);
154     int32_t SetTouchpadPointerSpeed(int32_t speed);
155     int32_t GetTouchpadPointerSpeed(int32_t &speed);
156     int32_t SetTouchpadPinchSwitch(bool switchFlag);
157     int32_t GetTouchpadPinchSwitch(bool &switchFlag);
158     int32_t SetTouchpadSwipeSwitch(bool switchFlag);
159     int32_t GetTouchpadSwipeSwitch(bool &switchFlag);
160     int32_t SetTouchpadRightClickType(int32_t type);
161     int32_t GetTouchpadRightClickType(int32_t &type);
162 
163     void SetAnrObserver(std::shared_ptr<IAnrObserver> observer);
164     void OnAnr(int32_t pid);
165 
166     int32_t EnterCaptureMode(int32_t windowId);
167     int32_t LeaveCaptureMode(int32_t windowId);
168     bool GetFunctionKeyState(int32_t funcKey);
169     int32_t SetFunctionKeyState(int32_t funcKey, bool enable);
170     int32_t SetPointerLocation(int32_t x, int32_t y);
171     int32_t EnableInputDevice(bool enable);
172     // 快捷键拉起Ability
173     int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay);
174 
175     EventHandlerPtr GetEventHandler() const;
176     void AppendExtraData(const ExtraData& extraData);
177     int32_t SetShieldStatus(int32_t shieldMode, bool isShield);
178     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield);
179 
180     void AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
181     void RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
182 
183 private:
184     int32_t PackWindowInfo(NetPacket &pkt);
185     int32_t PackWindowGroupInfo(NetPacket &pkt);
186     int32_t PackDisplayInfo(NetPacket &pkt);
187     void PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo);
188     void PrintDisplayInfo();
189     void PrintWindowGroupInfo();
190     void SendDisplayInfo();
191     void SendWindowInfo();
192     void SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId);
193     bool IsValiadWindowAreas(const std::vector<WindowInfo> &windows);
194 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
195     int32_t PackEnhanceConfig(NetPacket &pkt);
196     void SendEnhanceConfig();
197     void PrintEnhanceConfig();
198 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
199     void ReAddInputEventFilter();
200 
201 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
202     void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,
203         std::shared_ptr<KeyEvent> keyEvent);
204 #endif // OHOS_BUILD_ENABLE_KEYBOARD
205 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
206     void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,
207         std::shared_ptr<PointerEvent> pointerEvent);
208 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
209 
210 #ifdef OHOS_BUILD_ENABLE_ANCO
211     bool IsValidAncoWindow(const std::vector<WindowInfo> &windows);
212 #endif // OHOS_BUILD_ENABLE_ANCO
213 
214 private:
215     std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_;
216     std::shared_ptr<MMIEventObserver> eventObserver_ { nullptr };
217     std::shared_ptr<IInputEventConsumer> consumer_ { nullptr };
218     std::vector<std::shared_ptr<IAnrObserver>> anrObservers_;
219     std::shared_ptr<IWindowChecker> winChecker_ { nullptr };
220     DisplayGroupInfo displayGroupInfo_ {};
221     WindowGroupInfo windowGroupInfo_ {};
222     std::mutex mtx_;
223     std::mutex handleMtx_;
224     std::condition_variable cv_;
225     std::thread ehThread_;
226     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr };
227 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
228     uint8_t* enhanceCfg_ = nullptr;
229     uint32_t enhanceCfgLen_ = 0;
230 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
231 };
232 
233 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance()
234 } // namespace MMI
235 } // namespace OHOS
236 #endif // INPUT_MANAGER_IMPL_H
237