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