1 /*
2  * Copyright (c) 2021-2023 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 MMI_SERVICE_H
17 #define MMI_SERVICE_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <thread>
22 
23 #include "iremote_object.h"
24 #include "system_ability.h"
25 
26 #include "app_debug_listener.h"
27 #include "delegate_interface.h"
28 #include "delegate_tasks.h"
29 #include "infrared_frequency_info.h"
30 #include "input_event_handler.h"
31 #include "libinput_adapter.h"
32 #include "multimodal_input_connect_stub.h"
33 #include "nap_process.h"
34 #include "server_msg_handler.h"
35 #include "uds_server.h"
36 
37 namespace OHOS {
38 namespace MMI {
39 class TouchGestureAdapter;
40 enum class ServiceRunningState {STATE_NOT_START, STATE_RUNNING, STATE_EXIT};
41 class MMIService final : public UDSServer, public SystemAbility, public MultimodalInputConnectStub {
42     DECLARE_SYSTEM_ABILITY(MMIService);
43 
44 public:
45     void OnStart() override;
46     void OnStop() override;
47     static MMIService* GetInstance();
48     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
49     int32_t AllocSocketFd(const std::string &programName, const int32_t moduleType,
50         int32_t &toReturnClientFd, int32_t &tokenType) override;
51     int32_t AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority,
52         uint32_t deviceTags) override;
53     int32_t RemoveInputEventFilter(int32_t filterId) override;
54     int32_t SetPointerSize(int32_t size) override;
55     int32_t GetPointerSize(int32_t &size) override;
56     int32_t SetMouseScrollRows(int32_t rows) override;
57     int32_t GetMouseScrollRows(int32_t &rows) override;
58     int32_t SetCustomCursor(int32_t pid, int32_t windowId, int32_t focusX, int32_t focusY, void* pixelMap) override;
59     int32_t SetMouseIcon(int32_t windowId, void* pixelMap) override;
60     int32_t ClearWindowPointerStyle(int32_t pid, int32_t windowId) override;
61     int32_t SetMouseHotSpot(int32_t pid, int32_t windowId, int32_t hotSpotX, int32_t hotSpotY) override;
62     int32_t SetNapStatus(int32_t pid, int32_t uid, std::string bundleName, int32_t napState) override;
63     int32_t SetMousePrimaryButton(int32_t primaryButton) override;
64     int32_t GetMousePrimaryButton(int32_t &primaryButton) override;
65     int32_t SetHoverScrollState(bool state) override;
66     int32_t GetHoverScrollState(bool &state) override;
67     int32_t SetPointerVisible(bool visible, int32_t priority) override;
68     int32_t IsPointerVisible(bool &visible) override;
69     int32_t MarkProcessed(int32_t eventType, int32_t eventId) override;
70     int32_t SetPointerColor(int32_t color) override;
71     int32_t GetPointerColor(int32_t &color) override;
72     int32_t EnableCombineKey(bool enable) override;
73     int32_t SetPointerSpeed(int32_t speed) override;
74     int32_t GetPointerSpeed(int32_t &speed) override;
75     int32_t SetPointerStyle(int32_t windowId, PointerStyle pointerStyle, bool isUiExtension = false) override;
76     int32_t NotifyNapOnline() override;
77     int32_t RemoveInputEventObserver() override;
78     int32_t GetPointerStyle(int32_t windowId, PointerStyle &pointerStyle, bool isUiExtension = false) override;
79     int32_t SupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke) override;
80     int32_t GetDeviceIds(std::vector<int32_t> &ids) override;
81     int32_t GetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice) override;
82     int32_t RegisterDevListener() override;
83     int32_t UnregisterDevListener() override;
84     int32_t GetKeyboardType(int32_t deviceId, int32_t &keyboardType) override;
85     int32_t SetKeyboardRepeatDelay(int32_t delay) override;
86     int32_t SetKeyboardRepeatRate(int32_t rate) override;
87     int32_t GetKeyboardRepeatDelay(int32_t &delay) override;
88     int32_t GetKeyboardRepeatRate(int32_t &rate) override;
89     int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType,
90         int32_t priority, uint32_t deviceTags, std::vector<int32_t> actionsType = std::vector<int32_t>()) override;
91     int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
92         int32_t priority, uint32_t deviceTags, std::vector<int32_t> actionsType = std::vector<int32_t>()) override;
93     int32_t AddGestureMonitor(InputHandlerType handlerType,
94         HandleEventType eventType, TouchGestureType gestureType, int32_t fingers) override;
95     int32_t RemoveGestureMonitor(InputHandlerType handlerType,
96         HandleEventType eventType, TouchGestureType gestureType, int32_t fingers) override;
97     int32_t MarkEventConsumed(int32_t eventId) override;
98     int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY) override;
99     int32_t InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject) override;
100     int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option) override;
101     int32_t UnsubscribeKeyEvent(int32_t subscribeId) override;
102     int32_t SubscribeHotkey(int32_t subscribeId, const std::shared_ptr<KeyOption> option) override;
103     int32_t UnsubscribeHotkey(int32_t subscribeId) override;
104     int32_t SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType) override;
105     int32_t UnsubscribeSwitchEvent(int32_t subscribeId) override;
106     int32_t InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject) override;
107     int32_t SetAnrObserver() override;
108     int32_t GetDisplayBindInfo(DisplayBindInfos &infos) override;
109     int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>,
110         int32_t> &datas) override;
111     int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg) override;
112     int32_t GetFunctionKeyState(int32_t funcKey, bool &state) override;
113     int32_t SetFunctionKeyState(int32_t funcKey, bool enable) override;
114     int32_t SetPointerLocation(int32_t x, int32_t y) override;
115     virtual int32_t SetMouseCaptureMode(int32_t windowId, bool isCaptureMode) override;
116     int32_t GetWindowPid(int32_t windowId) override;
117     int32_t AppendExtraData(const ExtraData& extraData) override;
118     int32_t EnableInputDevice(bool enable) override;
119     int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay) override;
120     int32_t SetTouchpadScrollSwitch(bool switchFlag) override;
121     int32_t GetTouchpadScrollSwitch(bool &switchFlag) override;
122     int32_t SetTouchpadScrollDirection(bool state) override;
123     int32_t GetTouchpadScrollDirection(bool &state) override;
124     int32_t SetTouchpadTapSwitch(bool switchFlag) override;
125     int32_t GetTouchpadTapSwitch(bool &switchFlag) override;
126     int32_t SetTouchpadPointerSpeed(int32_t speed) override;
127     int32_t GetTouchpadPointerSpeed(int32_t &speed) override;
128     int32_t SetTouchpadPinchSwitch(bool switchFlag) override;
129     int32_t GetTouchpadPinchSwitch(bool &switchFlag) override;
130     int32_t SetTouchpadSwipeSwitch(bool switchFlag) override;
131     int32_t GetTouchpadSwipeSwitch(bool &switchFlag) override;
132     int32_t SetTouchpadRightClickType(int32_t type) override;
133     int32_t GetTouchpadRightClickType(int32_t &type) override;
134     int32_t SetTouchpadRotateSwitch(bool rotateSwitch) override;
135     int32_t GetTouchpadRotateSwitch(bool &rotateSwitch) override;
136     int32_t SetShieldStatus(int32_t shieldMode, bool isShield) override;
137     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield) override;
138     int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState) override;
139     int32_t Authorize(bool isAuthorize) override;
140     int32_t CancelInjection() override;
141     int32_t SetMoveEventFilters(bool flag) override;
142 #ifdef OHOS_RSS_CLIENT
143     void OnAddResSchedSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
144 #endif // OHOS_RSS_CLIENT
145     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
146     int32_t HasIrEmitter(bool &hasIrEmitter) override;
147     int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& frequencies) override;
148     int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern) override;
149 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
150     int32_t SetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY) override;
151     int32_t OnSetVKeyboardArea(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY);
152     int32_t SetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern) override;
153     int32_t OnSetMotionSpace(std::string& keyName, bool useShift, std::vector<int32_t>& pattern);
154 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
155     int32_t OnHasIrEmitter(bool &hasIrEmitter);
156     int32_t SetPixelMapData(int32_t infoId, void* pixelMap) override;
157     int32_t SetCurrentUser(int32_t userId) override;
158     int32_t SetTouchpadThreeFingersTapSwitch(bool switchFlag) override;
159     int32_t GetTouchpadThreeFingersTapSwitch(bool &switchFlag) override;
160     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId) override;
161     int32_t RemoveVirtualInputDevice(int32_t deviceId) override;
162     int32_t EnableHardwareCursorStats(bool enable) override;
163     int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount) override;
164 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
165     int32_t GetPointerSnapshot(void *pixelMapPtr) override;
166 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
167     int32_t TransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject) override;
168     int32_t SetTouchpadScrollRows(int32_t rows) override;
169     int32_t GetTouchpadScrollRows(int32_t &rows) override;
170     int32_t SkipPointerLayer(bool isSkip) override;
171     void CalculateFuntionRunningTime(std::function<void()> func, const std::string &flag);
172     int32_t SetClientInfo(int32_t pid, uint64_t readThreadId) override;
173     int32_t GetIntervalSinceLastInput(int64_t &timeInterval) override;
174 #ifdef OHOS_BUILD_ENABLE_ANCO
175     void InitAncoUds();
176     void StopAncoUds();
177     int32_t InjectKeyEventExt(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
178     int32_t InjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
179         bool isNativeInject, bool isShell);
180     int32_t AncoAddChannel(sptr<IAncoChannel> channel) override;
181     int32_t AncoRemoveChannel(sptr<IAncoChannel> channel) override;
182 #endif // OHOS_BUILD_ENABLE_ANCO
183 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
184     static void ScreenCaptureCallback(int32_t pid, bool isStart);
185     void RegisterScreenCaptureCallback();
186 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
187 
188     int32_t OnGetAllSystemHotkey(std::vector<std::unique_ptr<KeyOption>> &keyOptions);
189     int32_t GetAllSystemHotkeys(std::vector<std::unique_ptr<KeyOption>> &keyOptions) override;
190 
191 protected:
192     void OnConnected(SessionPtr s) override;
193     void OnDisconnected(SessionPtr s) override;
194     int32_t AddEpoll(EpollEventType type, int32_t fd) override;
195     int32_t DelEpoll(EpollEventType type, int32_t fd);
196     bool IsRunning() const override;
197 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
198     int32_t CheckPointerVisible(bool &visible);
199 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
200 #ifdef OHOS_BUILD_ENABLE_POINTER
201     int32_t ReadMouseScrollRows(int32_t &rows);
202     int32_t ReadPointerSize(int32_t &size);
203     int32_t ReadMousePrimaryButton(int32_t &primaryButton);
204     int32_t ReadPointerSpeed(int32_t &speed);
205     int32_t ReadHoverScrollState(bool &state);
206     int32_t ReadPointerColor(int32_t &color);
207     int32_t ReadTouchpadScrollSwich(bool &switchFlag);
208     int32_t ReadTouchpadScrollDirection(bool &state);
209     int32_t ReadTouchpadTapSwitch(bool &switchFlag);
210     int32_t ReadTouchpadPointerSpeed(int32_t &speed);
211     int32_t ReadTouchpadPinchSwitch(bool &switchFlag);
212     int32_t ReadTouchpadSwipeSwitch(bool &switchFlag);
213     int32_t ReadTouchpadRightMenuType(int32_t &type);
214     int32_t ReadTouchpadRotateSwitch(bool &rotateSwitch);
215     int32_t ReadTouchpadScrollRows(int32_t &rows);
216 #endif // OHOS_BUILD_ENABLE_POINTER
217     int32_t OnRegisterDevListener(int32_t pid);
218     int32_t OnUnregisterDevListener(int32_t pid);
219     int32_t OnGetDeviceIds(std::vector<int32_t> &ids);
220     int32_t OnGetDevice(int32_t deviceId, std::shared_ptr<InputDevice> &inputDevice);
221     int32_t OnSupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke);
222     int32_t OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType);
223     int32_t OnGetWindowPid(int32_t windowId, int32_t &windowPid);
224 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
225     int32_t CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
226         int32_t priority, uint32_t deviceTags);
227     int32_t CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
228         int32_t priority, uint32_t deviceTags);
229     int32_t CheckAddInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType);
230     int32_t CheckRemoveInput(int32_t pid, InputHandlerType handlerType, std::vector<int32_t> actionsType);
231     int32_t ObserverAddInputHandler(int32_t pid);
232 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
233     int32_t CheckMarkConsumed(int32_t pid, int32_t eventId);
234     int32_t CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
235 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
236     int32_t OnGetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState);
237 #endif // OHOS_BUILD_ENABLE_KEYBOARD
238     int32_t CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,
239         int32_t pid, bool isNativeInject, bool isShell);
240     bool InitLibinputService();
241     bool InitService();
242     bool InitSignalHandler();
243     bool InitDelegateTasks();
244     int32_t Init();
245     void InitPreferences();
246 
247     void OnThread();
248     void OnSignalEvent(int32_t signalFd);
249     void OnDelegateTask(epoll_event& ev);
250 
251     void AddReloadDeviceTimer();
252 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
253     int32_t UpdateSettingsXml(const std::string &businessId, int32_t delay);
254 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
255     void AddAppDebugListener();
256     void RemoveAppDebugListener();
257 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
258     int32_t UpdateCombineKeyState(bool enable);
259 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
260     int32_t OnAuthorize(bool isAuthorize);
261     int32_t OnCancelInjection();
262     void InitPrintClientInfo();
263 private:
264     MMIService();
265     ~MMIService();
266 private:
267     int32_t CheckPidPermission(int32_t pid);
268     void PrintLog(const std::string &flag, int32_t duration, int32_t pid, int32_t tid);
269     void OnSessionDelete(SessionPtr session);
270     std::atomic<ServiceRunningState> state_ = ServiceRunningState::STATE_NOT_START;
271     int32_t mmiFd_ { -1 };
272     std::atomic<bool> isCesStart_ { false };
273     std::mutex mu_;
274     std::thread t_;
275     std::thread eventMonitorThread_;
276 #ifdef OHOS_BUILD_ENABLE_ANCO
277     int32_t shellAssitentPid_ { -1 };
278 #endif // OHOS_BUILD_ENABLE_ANCO
279 #ifdef OHOS_RSS_CLIENT
280     std::atomic<uint64_t> tid_ = 0;
281 #endif // OHOS_RSS_CLIENT
282     LibinputAdapter libinputAdapter_;
283     ServerMsgHandler sMsgHandler_;
284     DelegateTasks delegateTasks_;
285 #ifdef OHOS_BUILD_ENABLE_TOUCH
286     std::shared_ptr<TouchGestureAdapter> touchGestureAdapter_ { nullptr };
287 #endif // OHOS_BUILD_ENABLE_TOUCH
288     std::shared_ptr<DelegateInterface> delegateInterface_ { nullptr };
289     sptr<AppDebugListener> appDebugListener_;
290     std::atomic_bool threadStatusFlag_ { false };
291     struct ClientInfo {
292         int32_t pid { -1 };
293         uint64_t readThreadId { -1 };
294     };
295     std::map<std::string, ClientInfo> clientInfos_;
296     std::mutex mutex_;
297 #if defined(OHOS_BUILD_ENABLE_MONITOR) && defined(PLAYER_FRAMEWORK_EXISTS)
298     bool hasRegisterListener_ { false };
299 #endif // OHOS_BUILD_ENABLE_MONITOR && PLAYER_FRAMEWORK_EXISTS
300 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
301     std::atomic_bool isHPR_ { false };
302 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
303 };
304 } // namespace MMI
305 } // namespace OHOS
306 #endif // MMI_SERVICE_H
307