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(
70         std::shared_ptr<KeyOption> keyOption,
71         std::function<void(std::shared_ptr<KeyEvent>)> callback
72     );
73     void UnsubscribeKeyEvent(int32_t subscriberId);
74     int32_t SubscribeSwitchEvent(int32_t switchType, std::function<void(std::shared_ptr<SwitchEvent>)> callback);
75     void UnsubscribeSwitchEvent(int32_t subscriberId);
76     int32_t AddInputEventFilter(std::shared_ptr<IInputEventFilter> filter, int32_t priority, uint32_t deviceTags);
77     int32_t RemoveInputEventFilter(int32_t filterId);
78     int32_t AddInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
79     int32_t RemoveInputEventObserver(std::shared_ptr<MMIEventObserver> observer);
80     int32_t NotifyNapOnline();
81     void NotifyBundleName(int32_t pid, int32_t uid, const std::string &bundleName, int32_t syncStatus);
82     void SetWindowInputEventConsumer(std::shared_ptr<IInputEventConsumer> inputEventConsumer,
83         std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
84     void ClearWindowPointerStyle(int32_t pid, int32_t windowId);
85     int32_t SetNapStatus(int32_t pid, int32_t uid, const std::string &bundleName, int32_t napStatus);
86 
87 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
88     void OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent);
89 #endif // OHOS_BUILD_ENABLE_KEYBOARD
90 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
91     void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
92 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
93     int32_t PackDisplayData(NetPacket &pkt);
94 
95     int32_t AddMonitor(std::function<void(std::shared_ptr<KeyEvent>)> monitor);
96     int32_t AddMonitor(std::function<void(std::shared_ptr<PointerEvent>)> monitor);
97     int32_t AddMonitor(std::shared_ptr<IInputEventConsumer> consumer,
98         HandleEventType eventType = HANDLE_EVENT_TYPE_KP);
99 
100     int32_t RemoveMonitor(int32_t monitorId);
101     void MarkConsumed(int32_t monitorId, int32_t eventId);
102     void MoveMouse(int32_t offsetX, int32_t offsetY);
103 
104     int32_t AddInterceptor(std::shared_ptr<IInputEventConsumer> interceptor,
105         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
106         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
107     int32_t AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor,
108         int32_t priority = DEFUALT_INTERCEPTOR_PRIORITY,
109         uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX));
110     int32_t RemoveInterceptor(int32_t interceptorId);
111 
112     void SimulateInputEvent(std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject = false);
113     int32_t SimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject = false);
114     void HandleSimulateInputEvent(std::shared_ptr<PointerEvent> pointerEvent);
115     void OnConnected();
116 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
117     template<typename T>
118     bool RecoverPointerEvent(std::initializer_list<T> pointerActionEvents, T pointerActionEvent);
119 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
120     void OnDisconnected();
121 
122     int32_t RegisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener);
123     int32_t UnregisterDevListener(std::string type, std::shared_ptr<IInputDeviceListener> listener = nullptr);
124     int32_t GetDeviceIds(std::function<void(std::vector<int32_t>&)> callback);
125     int32_t GetDevice(int32_t deviceId, std::function<void(std::shared_ptr<InputDevice>)> callback);
126     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keyCodes,
127         std::function<void(std::vector<bool>&)> callback);
128     int32_t GetKeyboardType(int32_t deviceId, std::function<void(int32_t)> callback);
129     int32_t SetKeyboardRepeatDelay(int32_t delay);
130     int32_t SetKeyboardRepeatRate(int32_t rate);
131     int32_t GetKeyboardRepeatDelay(std::function<void(int32_t)> callback);
132     int32_t GetKeyboardRepeatRate(std::function<void(int32_t)> callback);
133 
134     int32_t SetMouseScrollRows(int32_t rows);
135     int32_t GetMouseScrollRows(int32_t &rows);
136     int32_t SetPointerSize(int32_t size);
137     int32_t GetPointerSize(int32_t &size);
138     int32_t SetCustomCursor(int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap);
139     int32_t SetMouseIcon(int32_t windowId, void* pixelMap);
140     int32_t SetMouseHotSpot(int32_t windowId, int32_t hotSpotX, int32_t hotSpotY);
141     int32_t SetMousePrimaryButton(int32_t primaryButton);
142     int32_t GetMousePrimaryButton(int32_t &primaryButton);
143     int32_t SetHoverScrollState(bool state);
144     int32_t GetHoverScrollState(bool &state);
145 
146     int32_t SetPointerVisible(bool visible, int32_t priority);
147     bool IsPointerVisible();
148     int32_t SetPointerStyle(int32_t windowId, const PointerStyle& pointerStyle, bool isUiExtension = false);
149     int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension = false);
150 
151     int32_t SetPointerColor(int32_t color);
152     int32_t GetPointerColor(int32_t &color);
153     int32_t EnableCombineKey(bool enable);
154     int32_t SetPointerSpeed(int32_t speed);
155     int32_t GetPointerSpeed(int32_t &speed);
156 
157     int32_t SetTouchpadScrollSwitch(bool switchFlag);
158     int32_t GetTouchpadScrollSwitch(bool &switchFlag);
159     int32_t SetTouchpadScrollDirection(bool state);
160     int32_t GetTouchpadScrollDirection(bool &state);
161     int32_t SetTouchpadTapSwitch(bool switchFlag);
162     int32_t GetTouchpadTapSwitch(bool &switchFlag);
163     int32_t SetTouchpadPointerSpeed(int32_t speed);
164     int32_t GetTouchpadPointerSpeed(int32_t &speed);
165     int32_t SetTouchpadPinchSwitch(bool switchFlag);
166     int32_t GetTouchpadPinchSwitch(bool &switchFlag);
167     int32_t SetTouchpadSwipeSwitch(bool switchFlag);
168     int32_t GetTouchpadSwipeSwitch(bool &switchFlag);
169     int32_t SetTouchpadRightClickType(int32_t type);
170     int32_t GetTouchpadRightClickType(int32_t &type);
171     int32_t SetTouchpadRotateSwitch(bool rotateSwitch);
172     int32_t GetTouchpadRotateSwitch(bool &rotateSwitch);
173     int32_t SetTouchpadDoubleTapAndDragState(bool switchFlag);
174     int32_t GetTouchpadDoubleTapAndDragState(bool &switchFlag);
175     int32_t EnableHardwareCursorStats(bool enable);
176     int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount);
177     int32_t GetPointerSnapshot(void *pixelMapPtr);
178 
179     void SetAnrObserver(std::shared_ptr<IAnrObserver> observer);
180     void OnAnr(int32_t pid, int32_t eventId);
181 
182     int32_t EnterCaptureMode(int32_t windowId);
183     int32_t LeaveCaptureMode(int32_t windowId);
184     bool GetFunctionKeyState(int32_t funcKey);
185     int32_t SetFunctionKeyState(int32_t funcKey, bool enable);
186     int32_t SetPointerLocation(int32_t x, int32_t y);
187     int32_t EnableInputDevice(bool enable);
188     // 快捷键拉起Ability
189     int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay);
190 
191     void AppendExtraData(const ExtraData& extraData);
192     int32_t SetShieldStatus(int32_t shieldMode, bool isShield);
193     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield);
194     int32_t MarkProcessed(int32_t eventId, int64_t actionTime);
195 
196     void AddServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
197     void RemoveServiceWatcher(std::shared_ptr<IInputServiceWatcher> watcher);
198 
199     int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState);
200     void Authorize(bool isAuthorize);
201     int32_t CancelInjection();
202     int32_t SetPixelMapData(int32_t infoId, void* pixelMap);
203     int32_t HasIrEmitter(bool &hasIrEmitter);
204     int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys);
205     int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern);
206     int32_t SetCurrentUser(int32_t userId);
207     int32_t GetWinSyncBatchSize(int32_t maxAreasCount, int32_t displayCount);
208     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId);
209     int32_t RemoveVirtualInputDevice(int32_t deviceId);
210     int32_t AncoAddChannel(std::shared_ptr<IAncoConsumer> consumer);
211     int32_t AncoRemoveChannel(std::shared_ptr<IAncoConsumer> consumer);
212     int32_t SkipPointerLayer(bool isSkip);
213     int32_t RegisterWindowStateErrorCallback(std::function<void(int32_t, int32_t)> callback);
214     void OnWindowStateError(int32_t pid, int32_t windowId);
215     int32_t ConvertToCapiKeyAction(int32_t keyAction);
216     int32_t GetIntervalSinceLastInput(int64_t &timeInterval);
217 
218 private:
219     int32_t PackWindowInfo(NetPacket &pkt);
220     int32_t PackWindowGroupInfo(NetPacket &pkt);
221     int32_t PackDisplayInfo(NetPacket &pkt);
222     int32_t PackUiExtentionWindowInfo(const std::vector<WindowInfo>& windowsInfo, NetPacket &pkt);
223     void PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo);
224     void PrintForemostThreeWindowInfo(const std::vector<WindowInfo> &windowsInfo);
225     void PrintDisplayInfo();
226     void PrintWindowGroupInfo();
227     int32_t SendDisplayInfo();
228     int32_t SendWindowInfo();
229     void SendWindowAreaInfo(WindowArea area, int32_t pid, int32_t windowId);
230     bool IsValiadWindowAreas(const std::vector<WindowInfo> &windows);
231     int32_t GetDisplayMaxSize();
232     int32_t GetWindowMaxSize(int32_t maxAreasCount);
233 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
234     int32_t PackEnhanceConfig(NetPacket &pkt);
235     void SendEnhanceConfig();
236     void PrintEnhanceConfig();
237 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
238     void ReAddInputEventFilter();
239 
240 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
241     void OnKeyEventTask(std::shared_ptr<IInputEventConsumer> consumer,
242         std::shared_ptr<KeyEvent> keyEvent);
243 #endif // OHOS_BUILD_ENABLE_KEYBOARD
244 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
245     void OnPointerEventTask(std::shared_ptr<IInputEventConsumer> consumer,
246         std::shared_ptr<PointerEvent> pointerEvent);
247 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
248 #ifdef OHOS_BUILD_ENABLE_ANCO
249     bool IsValidAncoWindow(const std::vector<WindowInfo> &windows);
250 #endif // OHOS_BUILD_ENABLE_ANCO
251 
252 private:
253     std::map<int32_t, std::tuple<sptr<IEventFilter>, int32_t, uint32_t>> eventFilterServices_;
254     std::shared_ptr<MMIEventObserver> eventObserver_ { nullptr };
255     std::shared_ptr<IInputEventConsumer> consumer_ { nullptr };
256     std::vector<std::shared_ptr<IAnrObserver>> anrObservers_;
257     std::shared_ptr<IWindowChecker> winChecker_ { nullptr };
258     DisplayGroupInfo displayGroupInfo_ {};
259     WindowGroupInfo windowGroupInfo_ {};
260     std::mutex mtx_;
261     std::mutex handleMtx_;
262     mutable std::mutex resourceMtx_;
263     std::condition_variable cv_;
264     std::thread ehThread_;
265     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ { nullptr };
266     std::shared_ptr<PointerEvent> lastPointerEvent_ { nullptr };
267     std::function<void(int32_t, int32_t)> windowStatecallback_;
268 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
269     uint8_t* enhanceCfg_ = nullptr;
270     uint32_t enhanceCfgLen_ = 0;
271 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
272 #ifdef OHOS_BUILD_ENABLE_ANCO
273     std::map<std::shared_ptr<IAncoConsumer>, sptr<IAncoChannel>> ancoChannels_;
274 #endif // OHOS_BUILD_ENABLE_ANCO
275 };
276 
277 #define InputMgrImpl ::OHOS::Singleton<InputManagerImpl>::GetInstance()
278 } // namespace MMI
279 } // namespace OHOS
280 #endif // INPUT_MANAGER_IMPL_H
281