1 /* 2 * Copyright (c) 2021-2025 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 SERVER_MSG_HANDLER_H 17 #define SERVER_MSG_HANDLER_H 18 19 #include "client_death_handler.h" 20 #include "event_dispatch_handler.h" 21 #include "ievent_filter.h" 22 #include "inject_notice_manager.h" 23 #include "key_option.h" 24 #include "long_press_event.h" 25 #include "mouse_event_normalize.h" 26 #include "msg_handler.h" 27 #include "pixel_map.h" 28 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 29 #include "sec_comp_enhance_kit.h" 30 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 31 32 namespace OHOS { 33 namespace MMI { 34 enum class AuthorizationStatus : int32_t { 35 UNKNOWN, 36 AUTHORIZED, 37 UNAUTHORIZED 38 }; 39 40 enum class InjectionType : int32_t { 41 UNKNOWN, 42 KEYEVENT, 43 POINTEREVENT 44 }; 45 46 typedef std::function<int32_t(SessionPtr sess, NetPacket& pkt)> ServerMsgFun; 47 class ServerMsgHandler final : public MsgHandler<MmiMessageId, ServerMsgFun> { 48 public: 49 ServerMsgHandler() = default; 50 DISALLOW_COPY_AND_MOVE(ServerMsgHandler); 51 ~ServerMsgHandler() override = default; 52 53 void Init(UDSServer& udsServer); 54 void OnMsgHandler(SessionPtr sess, NetPacket& pkt); 55 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 56 int32_t OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, 57 int32_t priority, uint32_t deviceTags); 58 int32_t OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType, 59 int32_t priority, uint32_t deviceTags); 60 int32_t OnAddGestureMonitor(SessionPtr sess, InputHandlerType handlerType, 61 HandleEventType eventType, TouchGestureType gestureType, int32_t fingers); 62 int32_t OnRemoveGestureMonitor(SessionPtr sess, InputHandlerType handlerType, 63 HandleEventType eventType, TouchGestureType gestureType, int32_t fingers); 64 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR 65 #ifdef OHOS_BUILD_ENABLE_MONITOR 66 int32_t OnMarkConsumed(SessionPtr sess, int32_t eventId); 67 #endif // OHOS_BUILD_ENABLE_MONITOR 68 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 69 int32_t OnSubscribeKeyEvent(IUdsServer *server, int32_t pid, 70 int32_t subscribeId, const std::shared_ptr<KeyOption> option); 71 int32_t OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId); 72 int32_t OnSubscribeHotkey(IUdsServer *server, int32_t pid, 73 int32_t subscribeId, const std::shared_ptr<KeyOption> option); 74 int32_t OnUnsubscribeHotkey(IUdsServer *server, int32_t pid, int32_t subscribeId); 75 #endif // OHOS_BUILD_ENABLE_KEYBOARD 76 #ifdef OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 77 int32_t SubscribeKeyMonitor(int32_t session, const KeyMonitorOption &keyOption); 78 int32_t UnsubscribeKeyMonitor(int32_t session, const KeyMonitorOption &keyOption); 79 #endif // OHOS_BUILD_ENABLE_KEY_PRESSED_HANDLER 80 81 #ifdef OHOS_BUILD_ENABLE_SWITCH 82 int32_t OnSubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId, int32_t switchType); 83 int32_t OnUnsubscribeSwitchEvent(IUdsServer *server, int32_t pid, int32_t subscribeId); 84 int32_t OnQuerySwitchStatus(int32_t switchType, int32_t& state); 85 #endif // OHOS_BUILD_ENABLE_SWITCH 86 int32_t OnSubscribetabletProximity(IUdsServer *server, int32_t pid, int32_t subscribeId); 87 int32_t OnUnsubscribetabletProximity(IUdsServer *server, int32_t pid, int32_t subscribeId); 88 int32_t OnSubscribeLongPressEvent(IUdsServer *server, int32_t pid, int32_t subscribeId, 89 const LongPressRequest &longPressRequest); 90 int32_t OnUnsubscribeLongPressEvent(IUdsServer *server, int32_t pid, int32_t subscribeId); 91 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) 92 int32_t OnMoveMouse(int32_t offsetX, int32_t offsetY); 93 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING 94 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 95 int32_t OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, int32_t pid, bool isNativeInject); 96 int32_t OnGetFunctionKeyState(int32_t funcKey, bool &state); 97 int32_t OnSetFunctionKeyState(int32_t pid, int32_t funcKey, bool enable); 98 #endif // OHOS_BUILD_ENABLE_KEYBOARD 99 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 100 void DealGesturePointers(std::shared_ptr<PointerEvent> pointerEvent); 101 int32_t OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid, 102 bool isNativeInject, bool isShell, int32_t useCoordinate); 103 int32_t OnInjectTouchPadEvent(const std::shared_ptr<PointerEvent> pointerEvent, int32_t pid, 104 const TouchpadCDG &touchpadCDG, bool isNativeInject, bool isShell); 105 int32_t OnInjectPointerEventExt(const std::shared_ptr<PointerEvent> pointerEvent, bool isShell, 106 int32_t useCoordinate); 107 int32_t OnInjectTouchPadEventExt(const std::shared_ptr<PointerEvent> pointerEvent, 108 const TouchpadCDG &touchpadCDG, bool isShell); 109 int32_t SaveTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, bool isShell); 110 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 111 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) || defined(OHOS_BUILD_ENABLE_KEYBOARD) 112 int32_t AddInputEventFilter(sptr<IEventFilter> filter, int32_t filterId, int32_t priority, uint32_t deviceTags, 113 int32_t clientPid); 114 int32_t RemoveInputEventFilter(int32_t clientPid, int32_t filterId); 115 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH || OHOS_BUILD_ENABLE_KEYBOARD 116 #ifdef OHOS_BUILD_ENABLE_KEYBOARD 117 int32_t SetShieldStatus(int32_t shieldMode, bool isShield); 118 int32_t GetShieldStatus(int32_t shieldMode, bool &isShield); 119 #endif // OHOS_BUILD_ENABLE_KEYBOARD 120 int32_t OnAuthorize(bool isAuthorize); 121 int32_t OnCancelInjection(int32_t callPidId = 0); 122 int32_t RequestInjection(const int32_t callingPid, int32_t &status, int32_t &reqId); 123 int32_t QueryAuthorizedStatus(const int32_t callingPid, int32_t &status); 124 bool IsPC() const; 125 int32_t GetRequestInjectionCallbackReqId(); 126 bool CheckForRequestInjectionFrequentAccess(int32_t callingPid, int64_t interval); 127 128 int32_t SetPixelMapData(int32_t infoId, void* pixelMap); 129 bool InitInjectNoticeSource(); 130 bool AddInjectNotice(const InjectNoticeInfo& noticeInfo); 131 int32_t OnTransferBinderClientSrv(const sptr<IRemoteObject> &binderClientObject, int32_t pid); 132 int32_t RegisterWindowStateErrorCallback(SessionPtr sess, NetPacket &pkt); 133 134 protected: 135 int32_t OnRegisterMsgHandler(SessionPtr sess, NetPacket& pkt); 136 int32_t OnDisplayInfo(SessionPtr sess, NetPacket& pkt); 137 int32_t OnWindowGroupInfo(SessionPtr sess, NetPacket &pkt); 138 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT 139 int32_t OnEnhanceConfig(SessionPtr sess, NetPacket& pkt); 140 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT 141 void SetWindowInfo(int32_t infoId, WindowInfo &info); 142 143 private: 144 #ifdef OHOS_BUILD_ENABLE_TOUCH 145 bool FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, bool isShell); 146 int32_t FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, 147 const std::map<int32_t, int32_t>& targetWindowIdMap, bool bNeedResetPointerId = false, 148 int32_t diffPointerId = 0); 149 bool UpdateTouchEvent(std::shared_ptr<PointerEvent> pointerEvent, int32_t action, int32_t targetWindowId); 150 #endif // OHOS_BUILD_ENABLE_TOUCH 151 void LaunchAbility(); 152 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 153 int32_t AccelerateMotion(std::shared_ptr<PointerEvent> pointerEvent); 154 int32_t AccelerateMotionTouchpad(std::shared_ptr<PointerEvent> pointerEvent, const TouchpadCDG &touchpadCDG); 155 float ScreenFactor(const int32_t diagonalInch); 156 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 157 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) 158 void UpdatePointerEvent(std::shared_ptr<PointerEvent> pointerEvent); 159 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH 160 #ifdef OHOS_BUILD_ENABLE_POINTER 161 void CalculateOffset(Direction direction, Offset &offset); 162 #endif // OHOS_BUILD_ENABLE_POINTER 163 int32_t OnUiExtentionWindowInfo(NetPacket &pkt, WindowInfo& info); 164 bool CloseInjectNotice(int32_t pid); 165 bool IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent); 166 int32_t NativeInjectCheck(int32_t pid); 167 int32_t ReadScreensInfo(NetPacket &pkt, UserScreenInfo &userScreenInfo); 168 int32_t ReadDisplayGroupsInfo(NetPacket &pkt, UserScreenInfo &userScreenInfo); 169 int32_t ReadDisplaysInfo(NetPacket &pkt, DisplayGroupInfo &displayGroupInfo); 170 int32_t ReadWindowsInfo(NetPacket &pkt, DisplayGroupInfo &displayGroupInfo, 171 OLD::DisplayGroupInfo &oldDisplayGroupInfo); 172 bool IsCastInject(int32_t deviceid); 173 bool ChangeToOld(const UserScreenInfo& userScreenInfo); 174 void ChangeToOld(size_t num, const std::vector<DisplayInfo>& displaysInfo, 175 const std::vector<ScreenInfo>& screens); 176 void Printf(const UserScreenInfo& userScreenInfo); 177 private: 178 UDSServer *udsServer_ { nullptr }; 179 std::map<int32_t, int32_t> nativeTargetWindowIds_; 180 std::map<int32_t, int32_t> shellTargetWindowIds_; 181 std::map<int32_t, int32_t> accessTargetWindowIds_; 182 std::map<int32_t, int32_t> castTargetWindowIds_; 183 std::map<int32_t, AuthorizationStatus> authorizationCollection_; 184 int32_t CurrentPID_ { -1 }; 185 InjectionType InjectionType_ { InjectionType::UNKNOWN }; 186 std::shared_ptr<KeyEvent> keyEvent_ { nullptr }; 187 std::shared_ptr<PointerEvent> pointerEvent_ { nullptr }; 188 std::map<int32_t, std::unique_ptr<Media::PixelMap>> transparentWins_; 189 std::shared_ptr<InjectNoticeManager> injectNotice_ { nullptr }; 190 ClientDeathHandler clientDeathHandler_; 191 std::map<int32_t, int64_t> mapQueryAuthorizeLastTimestamp_; 192 std::vector<OLD::DisplayGroupInfo> oldDisplayGroupInfos_; 193 }; 194 } // namespace MMI 195 } // namespace OHOS 196 #endif // SERVER_MSG_HANDLER_H 197