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 "event_filter_service.h"
27 #include "event_handler.h"
28 #include "extra_data.h"
29 #include "i_anco_channel.h"
30 #include "i_anr_observer.h"
31 #include "i_input_event_consumer.h"
32 #include "i_input_service_watcher.h"
33 #include "i_window_checker.h"
34 #include "if_mmi_client.h"
35 #include "infrared_frequency_info.h"
36 #include "input_device_impl.h"
37 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
38 #include "input_interceptor_manager.h"
39 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
40 #ifdef OHOS_BUILD_ENABLE_MONITOR
41 #include "input_monitor_manager.h"
42 #endif // OHOS_BUILD_ENABLE_MONITOR
43 #include "key_option.h"
44 #include "mmi_event_observer.h"
45 #include "nap_process.h"
46 #include "pointer_event.h"
47 #include "pointer_style.h"
48 #include "switch_event.h"
49 #include "window_info.h"
50 
51 namespace OHOS {
52 namespace MMI {
53 class InputManagerImpl final {
54     DECLARE_SINGLETON(InputManagerImpl);
55 
56 public:
57     DISALLOW_MOVE(InputManagerImpl);
58 
59     int32_t GetDisplayBindInfo(DisplayBindInfos &infos);
60     int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>, int32_t> &datas);
61     int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg);
62     int32_t GetWindowPid(int32_t windowId);
63     int32_t UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo);
64     int32_t UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo);
65     void SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId);
66 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
67     void SetEnhanceConfig(uint8_t *cfg, uint32_t cfgLen);
68 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
69     int32_t SubscribeKeyEvent(std::shared_ptr<KeyOption> keyOption,
70         std::function<void(std::shared_ptr<KeyEvent>)> callback);
71     void UnsubscribeKeyEvent(int32_t subscriberId);
72     int32_t SubscribeHotkey(std::shared_ptr<KeyOption> keyOption,
73         std::function<void(std::shared_ptr<KeyEvent>)> callback);
74     void UnsubscribeHotkey(int32_t subscriberId);
75     int32_t SubscribeSwitchEvent(int32_t switchType, std::function<void(std::shared_ptr<SwitchEvent>)> callback);
76     void UnsubscribeSwitchEvent(int32_t subscriberId);
77     int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags);
78     int32_t RemoveInputEventFilter(int32_t filterId);
79     int32_t AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
80     int32_t RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
81     int32_t NotifyNapOnline();
82     void NotifyBundleName(int32_t pid, int32_t uid, const std::string &bundleName, int32_t syncStatus);
83     void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,
84         std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
85     void ClearWindowPointerStyle(int32_t pid, int32_t windowId);
86     int32_t SetNapStatus(int32_t pid, int32_t uid, const std::string &bundleName, int32_t napStatus);
87     int32_t SetTouchpadThreeFingersTapSwitch(bool switchFlag);
88     int32_t GetTouchpadThreeFingersTapSwitch(bool &switchFlag);
89 
90 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
91     void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
92 #endif // OHOS_BUILD_ENABLE_KEYBOARD
93 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
94     void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
95 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
96     int32_t PackDisplayData(NetPacket &pkt);
97 
98     int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor);
99     int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor);
100     int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer,
101         HandleEventType eventType = HANDLE_EVENT_TYPE_KP);
102     int32_t AddGestureMonitor(std::shared_ptr<IInputEventConsumer> consumer,
103         TouchGestureType type, int32_t fingers);
104     int32_t RemoveGestureMonitor(int32_t monitorId);
105     int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> monitor, std::vector<int32_t> actionsType);
106 
107     int32_t RemoveMonitor(int32_t monitorId);
108     void MarkConsumed(int32_t monitorId, int32_t eventId);
109     void MoveMouse(int32_t offsetX, int32_t offsetY);
110 
111     int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor,
112         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
113         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
114     int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor,
115         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
116         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
117     int32_t RemoveInterceptor(int32_t interceptorId);
118 
119     void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject = false);
120     void SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false);
121     void HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent);
122     void SimulateTouchPadEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false);
123     void OnConnected();
124 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
125     template<typename T>
126     bool RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent);
127 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
128     void OnDisconnected();
129 
130     int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener);
131     int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr);
132     int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback);
133     int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback);
134     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes,
135         std::function<void(std::vector<bool>&)> callback);
136     int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback);
137     int32_t SetKeyboardRepeatDelay(int32_t delay);
138     int32_t SetKeyboardRepeatRate(int32_t rate);
139     int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback);
140     int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback);
141 
142     int32_t SetMouseScrollRows(int32_t rows);
143     int32_t GetMouseScrollRows(int32_t &rows);
144     int32_t SetPointerSize(int32_t size);
145     int32_t GetPointerSize(int32_t &size);
146     int32_t SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap);
147     int32_t SetMouseIcon(int32_t windowId, void* pixelMap);
148     int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY);
149     int32_t SetMousePrimaryButton(int32_t primaryButton);
150     int32_t GetMousePrimaryButton(int32_t &primaryButton);
151     int32_t SetHoverScrollState(bool state);
152     int32_t GetHoverScrollState(bool &state);
153 
154     int32_t SetPointerVisible(bool visible, int32_t priority);
155     bool IsPointerVisible();
156     int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle, bool isUiExtension = false);
157     int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension = false);
158 
159     int32_t SetPointerColor(int32_t color);
160     int32_t GetPointerColor(int32_t &color);
161     int32_t EnableCombineKey(bool enable);
162     int32_t SetPointerSpeed(int32_t speed);
163     int32_t GetPointerSpeed(int32_t &speed);
164 
165     int32_t SetTouchpadScrollSwitch(bool switchFlag);
166     int32_t GetTouchpadScrollSwitch(bool &switchFlag);
167     int32_t SetTouchpadScrollDirection(bool state);
168     int32_t GetTouchpadScrollDirection(bool &state);
169     int32_t SetTouchpadTapSwitch(bool switchFlag);
170     int32_t GetTouchpadTapSwitch(bool &switchFlag);
171     int32_t SetTouchpadPointerSpeed(int32_t speed);
172     int32_t GetTouchpadPointerSpeed(int32_t &speed);
173     int32_t SetTouchpadPinchSwitch(bool switchFlag);
174     int32_t GetTouchpadPinchSwitch(bool &switchFlag);
175     int32_t SetTouchpadSwipeSwitch(bool switchFlag);
176     int32_t GetTouchpadSwipeSwitch(bool &switchFlag);
177     int32_t SetTouchpadRightClickType(int32_t type);
178     int32_t GetTouchpadRightClickType(int32_t &type);
179     int32_t SetTouchpadRotateSwitch(bool rotateSwitch);
180     int32_t GetTouchpadRotateSwitch(bool &rotateSwitch);
181     int32_t EnableHardwareCursorStats(bool enable);
182     int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount);
183     int32_t GetPointerSnapshot(void *pixelMapPtr);
184     int32_t SetTouchpadScrollRows(int32_t rows);
185     int32_t GetTouchpadScrollRows(int32_t &rows);
186 
187     void SetAnrObserver(std::shared_ptr<IAnrObserver> observer);
188     void OnAnr(int32_t pid, int32_t eventId);
189 
190     int32_t EnterCaptureMode(int32_t windowId);
191     int32_t LeaveCaptureMode(int32_t windowId);
192     bool GetFunctionKeyState(int32_t funcKey);
193     int32_t SetFunctionKeyState(int32_t funcKey, bool enable);
194     int32_t SetPointerLocation(int32_t x, int32_t y);
195     int32_t EnableInputDevice(bool enable);
196     // 快捷键拉起Ability
197     int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay);
198 
199     void AppendExtraData(const ExtraData& extraData);
200     int32_t SetShieldStatus(int32_t shieldMode, bool isShield);
201     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield);
202 
203     void AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
204     void RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
205 
206     int32_t MarkProcessed(int32_t eventId, int64_t actionTime);
207 
208     int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState);
209     void Authorize(bool isAuthorize);
210     int32_t CancelInjection();
211 
212     int32_t HasIrEmitter(bool &hasIrEmitter);
213     int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys);
214     int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern);
215 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
216     int32_t SetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY);
217     int32_t SetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern);
218 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
219     int32_t SetPixelMapData(int32_t infoId, void* pixelMap);
220     int32_t SetCurrentUser(int32_t userId);
221     int32_t SetMoveEventFilters(bool flag);
222     int32_t GetWinSyncBatchSize(int32_t maxAreasCount, int32_t displayCount);
223     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId);
224     int32_t RemoveVirtualInputDevice(int32_t deviceId);
225     int32_t AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer);
226     int32_t AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer);
227     int32_t SkipPointerLayer(bool isSkip);
228     int32_t RegisterWindowStateErrorCallback(std::function<void(int32_t, int32_t)> callback);
229     void OnWindowStateError(int32_t pid, int32_t windowId);
230     int32_t GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &keyOptions, int32_t &count);
231     int32_t GetIntervalSinceLastInput(int64_t &timeInterval);
232     int32_t ConvertToCapiKeyAction(int32_t keyAction);
233 
234 private:
235     int32_t PackWindowInfo(NetPacket &pkt);
236     int32_t PackWindowGroupInfo(NetPacket &pkt);
237     int32_t PackDisplayInfo(NetPacket &pkt);
238     int32_t PackUiExtentionWindowInfo(const std::vector<WindowInfo>& windowsInfo, NetPacket &pkt);
239     void PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo);
240     void PrintForemostThreeWindowInfo(const std::vector<WindowInfo> &windowsInfo);
241     void PrintDisplayInfo();
242     void PrintWindowGroupInfo();
243     int32_t SendDisplayInfo();
244     int32_t SendWindowInfo();
245     void SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId);
246     bool IsValiadWindowAreas(const std::vector<WindowInfo> &windows);
247     int32_t GetDisplayMaxSize();
248     int32_t GetWindowMaxSize(int32_t maxAreasCount);
249 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
250     int32_t PackEnhanceConfig(NetPacket &pkt);
251     void SendEnhanceConfig();
252     void PrintEnhanceConfig();
253 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
254     void ReAddInputEventFilter();
255 
256 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
257     void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,
258         std::shared_ptr<KeyEvent> keyEvent);
259 #endif // OHOS_BUILD_ENABLE_KEYBOARD
260 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
261     void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,
262         std::shared_ptr<PointerEvent> pointerEvent);
263 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
264 #ifdef OHOS_BUILD_ENABLE_ANCO
265     bool IsValidAncoWindow(const std::vector<WindowInfo> &windows);
266 #endif // OHOS_BUILD_ENABLE_ANCO
267 
268 private:
269     std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_;
270     std::shared_ptr<MMIEventObserver> eventObserver_ { nullptr };
271     std::shared_ptr<IInputEventConsumer> consumer_ { nullptr };
272     std::vector<std::shared_ptr<IAnrObserver>> anrObservers_;
273     std::shared_ptr<IWindowChecker> winChecker_ { nullptr };
274     DisplayGroupInfo displayGroupInfo_ {};
275     WindowGroupInfo windowGroupInfo_ {};
276     std::mutex mtx_;
277     std::mutex handleMtx_;
278     mutable std::mutex resourceMtx_;
279     std::condition_variable cv_;
280     std::thread ehThread_;
281     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr };
282     std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr };
283     std::function<void(int32_t, int32_t)> windowStatecallback_;
284 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
285     uint8_t* enhanceCfg_ = nullptr;
286     uint32_t enhanceCfgLen_ = 0;
287 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
288 #ifdef OHOS_BUILD_ENABLE_ANCO
289     std::map<std::shared_ptr<IAncoConsumer>, sptr<IAncoChannel>> ancoChannels_;
290 #endif // OHOS_BUILD_ENABLE_ANCO
291 };
292 
293 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance()
294 } // namespace MMI
295 } // namespace OHOS
296 #endif // INPUT_MANAGER_IMPL_H
297