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 "input_event_handler.h"
30 #include "libinput_adapter.h"
31 #include "multimodal_input_connect_stub.h"
32 #include "server_msg_handler.h"
33 #include "uds_server.h"
34 #include "nap_process.h"
35 #include "infrared_frequency_info.h"
36 
37 namespace OHOS {
38 namespace MMI {
39 
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) override;
91     int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
92         int32_t priority, uint32_t deviceTags) override;
93     int32_t MarkEventConsumed(int32_t eventId) override;
94     int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY) override;
95     int32_t InjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject) override;
96     int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr<KeyOption> option) override;
97     int32_t UnsubscribeKeyEvent(int32_t subscribeId) override;
98     int32_t SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType) override;
99     int32_t UnsubscribeSwitchEvent(int32_t subscribeId) override;
100     int32_t InjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject) override;
101     int32_t SetAnrObserver() override;
102     int32_t GetDisplayBindInfo(DisplayBindInfos &infos) override;
103     int32_t GetAllMmiSubscribedEvents(std::map<std::tuple<int32_t, int32_t, std::string>,
104         int32_t> &datas) override;
105     int32_t SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg) override;
106     int32_t GetFunctionKeyState(int32_t funcKey, bool &state) override;
107     int32_t SetFunctionKeyState(int32_t funcKey, bool enable) override;
108     int32_t SetPointerLocation(int32_t x, int32_t y) override;
109     virtual int32_t SetMouseCaptureMode(int32_t windowId, bool isCaptureMode) override;
110     int32_t GetWindowPid(int32_t windowId) override;
111     int32_t AppendExtraData(const ExtraData& extraData) override;
112     int32_t EnableInputDevice(bool enable) override;
113     int32_t SetKeyDownDuration(const std::string &businessId, int32_t delay) override;
114     int32_t SetTouchpadScrollSwitch(bool switchFlag) override;
115     int32_t GetTouchpadScrollSwitch(bool &switchFlag) override;
116     int32_t SetTouchpadScrollDirection(bool state) override;
117     int32_t GetTouchpadScrollDirection(bool &state) override;
118     int32_t SetTouchpadTapSwitch(bool switchFlag) override;
119     int32_t GetTouchpadTapSwitch(bool &switchFlag) override;
120     int32_t SetTouchpadPointerSpeed(int32_t speed) override;
121     int32_t GetTouchpadPointerSpeed(int32_t &speed) override;
122     int32_t SetTouchpadPinchSwitch(bool switchFlag) override;
123     int32_t GetTouchpadPinchSwitch(bool &switchFlag) override;
124     int32_t SetTouchpadSwipeSwitch(bool switchFlag) override;
125     int32_t GetTouchpadSwipeSwitch(bool &switchFlag) override;
126     int32_t SetTouchpadRightClickType(int32_t type) override;
127     int32_t GetTouchpadRightClickType(int32_t &type) override;
128     int32_t SetTouchpadRotateSwitch(bool rotateSwitch) override;
129     int32_t GetTouchpadRotateSwitch(bool &rotateSwitch) override;
130     int32_t SetTouchpadDoubleTapAndDragState(bool switchFlag) override;
131     int32_t GetTouchpadDoubleTapAndDragState(bool &switchFlag) override;
132     int32_t SetShieldStatus(int32_t shieldMode, bool isShield) override;
133     int32_t GetShieldStatus(int32_t shieldMode, bool &isShield) override;
134     int32_t GetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState) override;
135     int32_t Authorize(bool isAuthorize) override;
136     int32_t CancelInjection() override;
137 #ifdef OHOS_RSS_CLIENT
138     void OnAddResSchedSystemAbility(int32_t systemAbilityId, const std::string &deviceId);
139 #endif // OHOS_RSS_CLIENT
140     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
141     int32_t SetPixelMapData(int32_t infoId, void* pixelMap) override;
142     int32_t HasIrEmitter(bool &hasIrEmitter) override;
143     int32_t GetInfraredFrequencies(std::vector<InfraredFrequency>& requencys) override;
144     int32_t TransmitInfrared(int64_t number, std::vector<int64_t>& pattern) override;
145     int32_t OnHasIrEmitter(bool &hasIrEmitter);
146     int32_t OnGetInfraredFrequencies(std::vector<InfraredFrequency>& frequencies);
147     int32_t OnTransmitInfrared(int64_t number, std::vector<int64_t>& pattern);
148     int32_t SetCurrentUser(int32_t userId) override;
149     int32_t AddVirtualInputDevice(std::shared_ptr<InputDevice> device, int32_t &deviceId) override;
150     int32_t RemoveVirtualInputDevice(int32_t deviceId) override;
151     int32_t EnableHardwareCursorStats(bool enable) override;
152     int32_t GetHardwareCursorStats(uint32_t &frameCount, uint32_t &vsyncCount) override;
153 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
154     int32_t GetPointerSnapshot(void *pixelMapPtr) override;
155 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
156     int32_t TransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject) override;
157     int32_t SkipPointerLayer(bool isSkip) override;
158     void CalculateFuntionRunningTime(std::function<void()> func, const std::string &flag);
159     int32_t GetIntervalSinceLastInput(int64_t &timeInterval) override;
160 #ifdef OHOS_BUILD_ENABLE_ANCO
161     void InitAncoUds();
162     void StopAncoUds();
163     int32_t InjectKeyEventExt(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
164     int32_t InjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid,
165         bool isNativeInject, bool isShell);
166     int32_t AncoAddChannel(sptr<IAncoChannel> channel) override;
167     int32_t AncoRemoveChannel(sptr<IAncoChannel> channel) override;
168 #endif // OHOS_BUILD_ENABLE_ANCO
169 
170 protected:
171     void OnConnected(SessionPtr s) override;
172     void OnDisconnected(SessionPtr s) override;
173     int32_t AddEpoll(EpollEventType type, int32_t fd) override;
174     int32_t DelEpoll(EpollEventType type, int32_t fd);
175     bool IsRunning() const override;
176 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
177     int32_t CheckPointerVisible(bool &visible);
178 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
179 #ifdef OHOS_BUILD_ENABLE_POINTER
180     int32_t ReadMouseScrollRows(int32_t &rows);
181     int32_t ReadPointerSize(int32_t &size);
182     int32_t ReadMousePrimaryButton(int32_t &primaryButton);
183     int32_t ReadPointerSpeed(int32_t &speed);
184     int32_t ReadHoverScrollState(bool &state);
185     int32_t ReadPointerColor(int32_t &color);
186     int32_t ReadTouchpadScrollSwich(bool &switchFlag);
187     int32_t ReadTouchpadScrollDirection(bool &state);
188     int32_t ReadTouchpadTapSwitch(bool &switchFlag);
189     int32_t ReadTouchpadPointerSpeed(int32_t &speed);
190     int32_t ReadTouchpadPinchSwitch(bool &switchFlag);
191     int32_t ReadTouchpadSwipeSwitch(bool &switchFlag);
192     int32_t ReadTouchpadRightMenuType(int32_t &type);
193     int32_t ReadTouchpadRotateSwitch(bool &rotateSwitch);
194     int32_t ReadTouchpadDoubleTapAndDragState(bool &switchFlag);
195 #endif // OHOS_BUILD_ENABLE_POINTER
196     int32_t OnRegisterDevListener(int32_t pid);
197     int32_t OnUnregisterDevListener(int32_t pid);
198     int32_t OnGetDeviceIds(std::vector<int32_t> &ids);
199     int32_t OnGetDevice(int32_t deviceId, std::shared_ptr<InputDevice> inputDevice);
200     int32_t OnSupportKeys(int32_t deviceId, std::vector<int32_t> &keys, std::vector<bool> &keystroke);
201     int32_t OnGetKeyboardType(int32_t deviceId, int32_t &keyboardType);
202     int32_t OnGetWindowPid(int32_t windowId, int32_t &windowPid);
203 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
204     int32_t CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
205         int32_t priority, uint32_t deviceTags);
206     int32_t CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType,
207         int32_t priority, uint32_t deviceTags);
208 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
209     int32_t CheckMarkConsumed(int32_t pid, int32_t eventId);
210     int32_t CheckInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject);
211 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
212     int32_t OnGetKeyState(std::vector<int32_t> &pressedKeys, std::map<int32_t, int32_t> &specialKeysState);
213 #endif // OHOS_BUILD_ENABLE_KEYBOARD
214 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
215     int32_t CheckInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent,
216         int32_t pid, bool isNativeInject, bool isShell);
217 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
218     bool InitLibinputService();
219     bool InitService();
220     bool InitSignalHandler();
221     bool InitDelegateTasks();
222     int32_t Init();
223 
224     void OnThread();
225     void OnSignalEvent(int32_t signalFd);
226     void OnDelegateTask(epoll_event& ev);
227 
228     void AddReloadDeviceTimer();
229 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
230     int32_t UpdateSettingsXml(const std::string &businessId, int32_t delay);
231 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
232     void AddAppDebugListener();
233     void RemoveAppDebugListener();
234 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY)
235     int32_t UpdateCombineKeyState(bool enable);
236 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY
237     int32_t OnAuthorize(bool isAuthorize);
238     int32_t OnCancelInjection(int32_t callPid = 0);
239 private:
240     MMIService();
241     ~MMIService();
242 private:
243     int32_t CheckPidPermission(int32_t pid);
244     void PrintLog(const std::string &flag, int32_t duration, int32_t pid, int32_t tid);
245     std::atomic<ServiceRunningState> state_ = ServiceRunningState::STATE_NOT_START;
246     int32_t mmiFd_ { -1 };
247     std::atomic<bool> isCesStart_ { false };
248     std::mutex mu_;
249     std::thread t_;
250 #ifdef OHOS_BUILD_ENABLE_ANCO
251     int32_t shellAssitentPid_ { -1 };
252 #endif // OHOS_BUILD_ENABLE_ANCO
253 #ifdef OHOS_RSS_CLIENT
254     std::atomic<uint64_t> tid_ = 0;
255 #endif
256     LibinputAdapter libinputAdapter_;
257     ServerMsgHandler sMsgHandler_;
258     DelegateTasks delegateTasks_;
259     std::shared_ptr<DelegateInterface> delegateInterface_ {nullptr};
260     sptr<AppDebugListener> appDebugListener_;
261     std::atomic_bool threadStatusFlag_ { false };
262 };
263 } // namespace MMI
264 } // namespace OHOS
265 #endif // MMI_SERVICE_H
266