diff --git a/frameworks/proxy/event_handler/include/input_handler_manager.h b/frameworks/proxy/event_handler/include/input_handler_manager.h index a06ae4f051a8bad2b0339df873561205f54e0bc7..25ef19e24275fe7aa31721051bfcd3db11bf22dd 100755 --- a/frameworks/proxy/event_handler/include/input_handler_manager.h +++ b/frameworks/proxy/event_handler/include/input_handler_manager.h @@ -29,29 +29,36 @@ namespace OHOS { namespace MMI { -class InputHandlerManager : public Singleton { +class InputHandlerManager { public: InputHandlerManager() = default; + virtual ~InputHandlerManager() = default; DISALLOW_COPY_AND_MOVE(InputHandlerManager); - int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr consumer, - HandleEventType eventType = HandleEventType::ALL); - void RemoveHandler(int32_t handlerId, InputHandlerType handlerType); - void MarkConsumed(int32_t monitorId, int32_t eventId); + +public: #ifdef OHOS_BUILD_ENABLE_KEYBOARD - void OnInputEvent(int32_t handlerId, std::shared_ptr keyEvent); + void OnInputEvent(std::shared_ptr keyEvent); #endif // OHOS_BUILD_ENABLE_KEYBOARD #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH) - void OnInputEvent(int32_t handlerId, std::shared_ptr pointerEvent); + void OnInputEvent(std::shared_ptr pointerEvent); #endif #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) void OnConnected(); #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR + bool HasHandler(int32_t handlerId); + virtual InputHandlerType GetHandlerType() const = 0; + HandleEventType GetEventType() const; + +protected: + int32_t AddHandler(InputHandlerType handlerType, std::shared_ptr consumer, + HandleEventType eventType = HANDLE_EVENT_TYPE_ALL); + void RemoveHandler(int32_t handlerId, InputHandlerType handlerType); private: struct Handler { int32_t handlerId_ { 0 }; InputHandlerType handlerType_ { NONE }; - HandleEventType eventType_ { HandleEventType::ALL }; + HandleEventType eventType_ { HANDLE_EVENT_TYPE_ALL }; std::shared_ptr consumer_ { nullptr }; EventHandlerPtr eventHandler_ { nullptr }; }; @@ -60,10 +67,9 @@ private: int32_t GetNextId(); int32_t AddLocal(int32_t handlerId, InputHandlerType handlerType, HandleEventType eventType, std::shared_ptr monitor); - void AddToServer(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType); + void AddToServer(InputHandlerType handlerType, HandleEventType eventType); int32_t RemoveLocal(int32_t handlerId, InputHandlerType handlerType); - void RemoveFromServer(int32_t handlerId, InputHandlerType handlerType); + void RemoveFromServer(InputHandlerType handlerType, HandleEventType eventType); std::shared_ptr FindHandler(int32_t handlerId); EventHandlerPtr GetEventHandler(int32_t handlerId); @@ -84,5 +90,4 @@ private: }; } // namespace MMI } // namespace OHOS -#define InputHandlerMgr InputHandlerManager::GetInstance() #endif // INPUT_HANDLER_MANAGER_H \ No newline at end of file diff --git a/frameworks/proxy/event_handler/include/input_handler_type.h b/frameworks/proxy/event_handler/include/input_handler_type.h index 53b8c3c1da45f5231bfb2b135261a4ea1e1cc6ca..51a2121c86f6ce2f4752c1a4b0c07ea0cd004c87 100755 --- a/frameworks/proxy/event_handler/include/input_handler_type.h +++ b/frameworks/proxy/event_handler/include/input_handler_type.h @@ -32,11 +32,11 @@ enum InputHandlerType : int32_t { MONITOR, }; -enum HandleEventType : int32_t { - ALL, - KEY, - POINTER, -}; +using HandleEventType = uint32_t; +inline constexpr HandleEventType HANDLE_EVENT_TYPE_NONE { 0x0 }; +inline constexpr HandleEventType HANDLE_EVENT_TYPE_KEY { 0x1 }; +inline constexpr HandleEventType HANDLE_EVENT_TYPE_POINTER { 0x2 }; +inline constexpr HandleEventType HANDLE_EVENT_TYPE_ALL { HANDLE_EVENT_TYPE_KEY | HANDLE_EVENT_TYPE_POINTER }; inline bool IsValidHandlerType(InputHandlerType handlerType) { diff --git a/frameworks/proxy/event_handler/include/input_interceptor_manager.h b/frameworks/proxy/event_handler/include/input_interceptor_manager.h index a055bfd430712de46da9163f01e9b843387c8f03..0dba97fa79927b997a06c948afb008f3efd92c91 100755 --- a/frameworks/proxy/event_handler/include/input_interceptor_manager.h +++ b/frameworks/proxy/event_handler/include/input_interceptor_manager.h @@ -20,19 +20,27 @@ #include "nocopyable.h" +#include "input_handler_manager.h" #include "i_input_event_consumer.h" #include "input_handler_type.h" namespace OHOS { namespace MMI { -class InputInterceptorManager { +class InputInterceptorManager : public InputHandlerManager { + DECLARE_DELAYED_SINGLETON(InputInterceptorManager); public: - InputInterceptorManager() = default; DISALLOW_COPY_AND_MOVE(InputInterceptorManager); - ~InputInterceptorManager() = default; int32_t AddInterceptor(std::shared_ptr interceptor, HandleEventType eventType); void RemoveInterceptor(int32_t interceptorId); + virtual InputHandlerType GetHandlerType() const override; }; + +inline InputHandlerType InputInterceptorManager::GetHandlerType() const +{ + return InputHandlerType::INTERCEPTOR; +} + +#define InputInterMgr DelayedSingleton::GetInstance() } // namespace MMI } // namespace OHOS #endif // INPUT_INTERCEPTOR_MANAGER_H diff --git a/frameworks/proxy/event_handler/include/input_manager_impl.h b/frameworks/proxy/event_handler/include/input_manager_impl.h index 3fbf8714f468939ef7b2f3eb7445bf30a6bf8b44..2890f84738514f1d48bda8c8ec35cc2e3e65cd47 100755 --- a/frameworks/proxy/event_handler/include/input_manager_impl.h +++ b/frameworks/proxy/event_handler/include/input_manager_impl.h @@ -124,13 +124,6 @@ private: std::vector> anrObservers_; DisplayGroupInfo displayGroupInfo_; -#ifdef OHOS_BUILD_ENABLE_MONITOR - InputMonitorManager monitorManager_; -#endif // OHOS_BUILD_ENABLE_MONITOR -#ifdef OHOS_BUILD_ENABLE_INTERCEPTOR - InputInterceptorManager interceptorManager_; -#endif // OHOS_BUILD_ENABLE_INTERCEPTOR - std::mutex mtx_; std::mutex handleMtx_; std::condition_variable cv_; diff --git a/frameworks/proxy/event_handler/include/input_monitor_manager.h b/frameworks/proxy/event_handler/include/input_monitor_manager.h index 28e962a3e0aaf0099178ecc7ddafd29076ba180c..63f214dc9e603d8f8e3d097107780bf0aa9227ac 100755 --- a/frameworks/proxy/event_handler/include/input_monitor_manager.h +++ b/frameworks/proxy/event_handler/include/input_monitor_manager.h @@ -20,29 +20,38 @@ #include "nocopyable.h" +#include "input_handler_manager.h" #include "i_input_event_consumer.h" #include "input_handler_type.h" namespace OHOS { namespace MMI { -class InputMonitorManager { +class InputMonitorManager : public InputHandlerManager { + DECLARE_DELAYED_SINGLETON(InputMonitorManager); public: - InputMonitorManager() = default; DISALLOW_COPY_AND_MOVE(InputMonitorManager); - ~InputMonitorManager() = default; + public: int32_t AddMonitor(std::shared_ptr monitor); void RemoveMonitor(int32_t monitorId); void MarkConsumed(int32_t monitorId, int32_t eventId); + virtual InputHandlerType GetHandlerType() const override; public: static bool IsValidMonitorId(int32_t monitorId); }; +inline InputHandlerType InputMonitorManager::GetHandlerType() const +{ + return InputHandlerType::MONITOR; +} + inline bool InputMonitorManager::IsValidMonitorId(int32_t monitorId) { return IsValidHandlerId(monitorId); } + +#define IMonitorMgr DelayedSingleton::GetInstance() } // namespace MMI } // namespace OHOS #endif // INPUT_MONITOR_MANAGER_H \ No newline at end of file diff --git a/frameworks/proxy/event_handler/src/client_msg_handler.cpp b/frameworks/proxy/event_handler/src/client_msg_handler.cpp index 8d093a13ef47c9ecb302519bf77695d23248b9e6..4851590a535d82cf107710b4dea798241a23ab01 100755 --- a/frameworks/proxy/event_handler/src/client_msg_handler.cpp +++ b/frameworks/proxy/event_handler/src/client_msg_handler.cpp @@ -265,8 +265,8 @@ int32_t ClientMsgHandler::OnDevListener(const UDSClient& client, NetPacket& pkt) int32_t ClientMsgHandler::ReportKeyEvent(const UDSClient& client, NetPacket& pkt) { CALL_DEBUG_ENTER; - int32_t handlerId; - pkt >> handlerId; + InputHandlerType handlerType; + pkt >> handlerType; if (pkt.ChkRWError()) { MMI_HILOGE("Packet read handler failed"); return RET_ERR; @@ -278,7 +278,20 @@ int32_t ClientMsgHandler::ReportKeyEvent(const UDSClient& client, NetPacket& pkt return RET_ERR; } BytraceAdapter::StartBytrace(keyEvent, BytraceAdapter::TRACE_START, BytraceAdapter::KEY_INTERCEPT_EVENT); - InputHandlerMgr.OnInputEvent(handlerId, keyEvent); + switch (handlerType) { + case INTERCEPTOR: { + InputInterMgr->OnInputEvent(keyEvent); + break; + } + case MONITOR: { + IMonitorMgr->OnInputEvent(keyEvent); + break; + } + default: { + MMI_HILOGW("Failed to intercept or monitor on the event"); + break; + } + } return RET_OK; } #endif // OHOS_BUILD_ENABLE_KEYBOARD @@ -287,14 +300,13 @@ int32_t ClientMsgHandler::ReportKeyEvent(const UDSClient& client, NetPacket& pkt int32_t ClientMsgHandler::ReportPointerEvent(const UDSClient& client, NetPacket& pkt) { CALL_DEBUG_ENTER; - int32_t handlerId; InputHandlerType handlerType; - pkt >> handlerId >> handlerType; + pkt >> handlerType; if (pkt.ChkRWError()) { MMI_HILOGE("Packet read Pointer data failed"); return RET_ERR; } - MMI_HILOGD("Client handlerId:%{public}d,handlerType:%{public}d", handlerId, handlerType); + MMI_HILOGD("Client handlerType:%{public}d", handlerType); auto pointerEvent = PointerEvent::Create(); CHKPR(pointerEvent, ERROR_NULL_POINTER); if (InputEventDataTransformation::Unmarshalling(pkt, pointerEvent) != ERR_OK) { @@ -302,7 +314,20 @@ int32_t ClientMsgHandler::ReportPointerEvent(const UDSClient& client, NetPacket& return RET_ERR; } BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_START, BytraceAdapter::POINT_INTERCEPT_EVENT); - InputHandlerMgr.OnInputEvent(handlerId, pointerEvent); + switch (handlerType) { + case INTERCEPTOR: { + InputInterMgr->OnInputEvent(pointerEvent); + break; + } + case MONITOR: { + IMonitorMgr->OnInputEvent(pointerEvent); + break; + } + default: { + MMI_HILOGW("Failed to intercept or monitor on the event"); + break; + } + } return RET_OK; } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH diff --git a/frameworks/proxy/event_handler/src/input_handler_manager.cpp b/frameworks/proxy/event_handler/src/input_handler_manager.cpp index 16c3dc58ee32618310c84dcb1782e54260911132..fcaef7901f3b10257ed5564e0f949a0b5c95f53c 100755 --- a/frameworks/proxy/event_handler/src/input_handler_manager.cpp +++ b/frameworks/proxy/event_handler/src/input_handler_manager.cpp @@ -47,10 +47,19 @@ int32_t InputHandlerManager::AddHandler(InputHandlerType handlerType, MMI_HILOGE("Exceeded limit of 32-bit maximum number of integers"); return INVALID_HANDLER_ID; } + + if (eventType == HANDLE_EVENT_TYPE_NONE) { + MMI_HILOGE("Invalid event type"); + return INVALID_HANDLER_ID; + } + const HandleEventType currentType = GetEventType(); MMI_HILOGD("Register new handler:%{public}d", handlerId); if (RET_OK == AddLocal(handlerId, handlerType, eventType, consumer)) { MMI_HILOGD("New handler successfully registered, report to server"); - AddToServer(handlerId, handlerType, eventType); + const HandleEventType newType = GetEventType(); + if (currentType != newType) { + AddToServer(handlerType, newType); + } } else { handlerId = INVALID_HANDLER_ID; } @@ -62,19 +71,13 @@ void InputHandlerManager::RemoveHandler(int32_t handlerId, InputHandlerType hand CALL_INFO_TRACE; MMI_HILOGD("Unregister handler:%{public}d,type:%{public}d", handlerId, handlerType); std::lock_guard guard(mtxHandlers_); + const HandleEventType currentType = GetEventType(); if (RET_OK == RemoveLocal(handlerId, handlerType)) { MMI_HILOGD("Handler:%{public}d unregistered, report to server", handlerId); - RemoveFromServer(handlerId, handlerType); - } -} - -void InputHandlerManager::MarkConsumed(int32_t monitorId, int32_t eventId) -{ - CALL_INFO_TRACE; - MMI_HILOGD("Mark consumed state, monitor:%{public}d,event:%{public}d", monitorId, eventId); - int32_t ret = MultimodalInputConnMgr->MarkEventConsumed(monitorId, eventId); - if (ret != 0) { - MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + const HandleEventType newType = GetEventType(); + if (currentType != newType) { + RemoveFromServer(handlerType, newType); + } } } @@ -98,10 +101,9 @@ int32_t InputHandlerManager::AddLocal(int32_t handlerId, InputHandlerType handle return RET_OK; } -void InputHandlerManager::AddToServer(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) +void InputHandlerManager::AddToServer(InputHandlerType handlerType, HandleEventType eventType) { - int32_t ret = MultimodalInputConnMgr->AddInputHandler(handlerId, handlerType, eventType); + int32_t ret = MultimodalInputConnMgr->AddInputHandler(handlerType, eventType); if (ret != 0) { MMI_HILOGE("Send to server failed, ret:%{public}d", ret); } @@ -123,10 +125,9 @@ int32_t InputHandlerManager::RemoveLocal(int32_t handlerId, InputHandlerType han return RET_OK; } -void InputHandlerManager::RemoveFromServer(int32_t handlerId, InputHandlerType handlerType) +void InputHandlerManager::RemoveFromServer(InputHandlerType handlerType, HandleEventType eventType) { - MMI_HILOGD("Remove handler:%{public}d from server", handlerId); - int32_t ret = MultimodalInputConnMgr->RemoveInputHandler(handlerId, handlerType); + int32_t ret = MultimodalInputConnMgr->RemoveInputHandler(handlerType, eventType); if (ret != 0) { MMI_HILOGE("Send to server failed, ret:%{public}d", ret); } @@ -177,19 +178,25 @@ void InputHandlerManager::OnKeyEventTask(std::shared_ptr co MMI_HILOGD("Key event callback id:%{public}d keyCode:%{public}d", handlerId, keyEvent->GetKeyCode()); } -void InputHandlerManager::OnInputEvent(int32_t handlerId, std::shared_ptr keyEvent) +void InputHandlerManager::OnInputEvent(std::shared_ptr keyEvent) { CHK_PID_AND_TID(); CHKPV(keyEvent); std::lock_guard guard(mtxHandlers_); BytraceAdapter::StartBytrace(keyEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::KEY_INTERCEPT_EVENT); - auto consumer = FindHandler(handlerId); - CHKPV(consumer); - if (!PostTask(handlerId, - std::bind(&InputHandlerManager::OnKeyEventTask, this, consumer, handlerId, keyEvent))) { - MMI_HILOGE("Post task failed"); + for (const auto &handler : inputHandlers_) { + if ((handler.second.eventType_ & HANDLE_EVENT_TYPE_KEY) != HANDLE_EVENT_TYPE_KEY) { + continue; + } + int32_t handlerId = handler.first; + auto consumer = handler.second.consumer_; + CHKPV(consumer); + if (!PostTask(handlerId, + std::bind(&InputHandlerManager::OnKeyEventTask, this, consumer, handlerId, keyEvent))) { + MMI_HILOGE("Post task failed"); + } + MMI_HILOGD("Key event id:%{public}d keyCode:%{public}d", handlerId, keyEvent->GetKeyCode()); } - MMI_HILOGD("Key event id:%{public}d keyCode:%{public}d", handlerId, keyEvent->GetKeyCode()); } #endif // OHOS_BUILD_ENABLE_KEYBOARD @@ -204,29 +211,57 @@ void InputHandlerManager::OnPointerEventTask(std::shared_ptrGetPointerId()); } -void InputHandlerManager::OnInputEvent(int32_t handlerId, std::shared_ptr pointerEvent) +void InputHandlerManager::OnInputEvent(std::shared_ptr pointerEvent) { CHK_PID_AND_TID(); CHKPV(pointerEvent); std::lock_guard guard(mtxHandlers_); BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::POINT_INTERCEPT_EVENT); - auto consumer = FindHandler(handlerId); - CHKPV(consumer); - if (!PostTask(handlerId, - std::bind(&InputHandlerManager::OnPointerEventTask, this, consumer, handlerId, pointerEvent))) { - MMI_HILOGE("Post task failed"); + for (const auto &iter : inputHandlers_) { + if ((iter.second.eventType_ & HANDLE_EVENT_TYPE_POINTER) != HANDLE_EVENT_TYPE_POINTER) { + continue; + } + int32_t handlerId = iter.first; + auto consumer = iter.second.consumer_; + CHKPV(consumer); + if (!PostTask(handlerId, + std::bind(&InputHandlerManager::OnPointerEventTask, this, consumer, handlerId, pointerEvent))) { + MMI_HILOGE("Post task failed"); + } + MMI_HILOGD("Pointer event id:%{public}d pointerId:%{public}d", handlerId, pointerEvent->GetPointerId()); } - MMI_HILOGD("Pointer event id:%{public}d pointerId:%{public}d", handlerId, pointerEvent->GetPointerId()); } #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH + #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) void InputHandlerManager::OnConnected() { CALL_DEBUG_ENTER; - for (auto &inputHandler : inputHandlers_) { - AddToServer(inputHandler.second.handlerId_, inputHandler.second.handlerType_, inputHandler.second.eventType_); + HandleEventType eventType = GetEventType(); + if (eventType != HANDLE_EVENT_TYPE_NONE) { + AddToServer(GetHandlerType(), eventType); } } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR + +bool InputHandlerManager::HasHandler(int32_t handlerId) +{ + std::lock_guard guard(mtxHandlers_); + auto iter = inputHandlers_.find(handlerId); + return (iter != inputHandlers_.end()); +} + +HandleEventType InputHandlerManager::GetEventType() const +{ + if (inputHandlers_.empty()) { + MMI_HILOGD("InputHandlers is empty"); + return HANDLE_EVENT_TYPE_NONE; + } + HandleEventType eventType { HANDLE_EVENT_TYPE_NONE }; + for (const auto &inputHandler : inputHandlers_) { + eventType |= inputHandler.second.eventType_; + } + return eventType; +} } // namespace MMI } // namespace OHOS diff --git a/frameworks/proxy/event_handler/src/input_interceptor_manager.cpp b/frameworks/proxy/event_handler/src/input_interceptor_manager.cpp index 2739b6ddc8e44fdf65c9ad9d51f6c4130d1c3142..da44746cd2e8ca213aac95f6babf4060579bb3f2 100755 --- a/frameworks/proxy/event_handler/src/input_interceptor_manager.cpp +++ b/frameworks/proxy/event_handler/src/input_interceptor_manager.cpp @@ -24,6 +24,9 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputInterceptorManager" }; } // namespace +InputInterceptorManager::InputInterceptorManager() {} +InputInterceptorManager::~InputInterceptorManager() {} + int32_t InputInterceptorManager::AddInterceptor(std::shared_ptr interceptor, HandleEventType eventType) { @@ -31,12 +34,12 @@ int32_t InputInterceptorManager::AddInterceptor(std::shared_ptr consum MMI_HILOGE("Client init failed"); return RET_ERR; } - return monitorManager_.AddMonitor(consumer); + return IMonitorMgr->AddMonitor(consumer); #else MMI_HILOGI("Monitor function does not support"); return ERROR_UNSUPPORT; @@ -410,7 +410,7 @@ void InputManagerImpl::RemoveMonitor(int32_t monitorId) MMI_HILOGE("Client init failed"); return; } - monitorManager_.RemoveMonitor(monitorId); + IMonitorMgr->RemoveMonitor(monitorId); #else MMI_HILOGI("Monitor function does not support"); #endif // OHOS_BUILD_ENABLE_MONITOR @@ -425,7 +425,7 @@ void InputManagerImpl::MarkConsumed(int32_t monitorId, int32_t eventId) MMI_HILOGE("Client init failed"); return; } - monitorManager_.MarkConsumed(monitorId, eventId); + IMonitorMgr->MarkConsumed(monitorId, eventId); #else MMI_HILOGI("Monitor function does not support"); #endif // OHOS_BUILD_ENABLE_MONITOR @@ -453,7 +453,7 @@ int32_t InputManagerImpl::AddInterceptor(std::shared_ptr in MMI_HILOGE("Client init failed"); return RET_ERR; } - return interceptorManager_.AddInterceptor(interceptor, HandleEventType::ALL); + return InputInterMgr->AddInterceptor(interceptor, HANDLE_EVENT_TYPE_ALL); #else MMI_HILOGW("Interceptor function does not support"); return ERROR_UNSUPPORT; @@ -472,7 +472,7 @@ int32_t InputManagerImpl::AddInterceptor(std::functionAddInterceptor(consumer, HANDLE_EVENT_TYPE_KEY); #else MMI_HILOGW("Keyboard device or interceptor function does not support"); return ERROR_UNSUPPORT; @@ -488,7 +488,7 @@ void InputManagerImpl::RemoveInterceptor(int32_t interceptorId) MMI_HILOGE("Client init failed"); return; } - interceptorManager_.RemoveInterceptor(interceptorId); + InputInterMgr->RemoveInterceptor(interceptorId); #else MMI_HILOGW("Interceptor function does not support"); #endif // OHOS_BUILD_ENABLE_INTERCEPTOR diff --git a/frameworks/proxy/event_handler/src/input_monitor_manager.cpp b/frameworks/proxy/event_handler/src/input_monitor_manager.cpp index c3b31cd78554bb38f86c5a92041a27b8c33c74df..24d87e207d90373537f2cd0b66bab1283ecc8f0f 100755 --- a/frameworks/proxy/event_handler/src/input_monitor_manager.cpp +++ b/frameworks/proxy/event_handler/src/input_monitor_manager.cpp @@ -14,8 +14,7 @@ */ #include "input_monitor_manager.h" - -#include "input_handler_manager.h" +#include "multimodal_input_connect_manager.h" #include "util.h" namespace OHOS { @@ -24,20 +23,31 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputMonitorManager" }; } // namespace +InputMonitorManager::InputMonitorManager() {} +InputMonitorManager::~InputMonitorManager() {} + int32_t InputMonitorManager::AddMonitor(std::shared_ptr monitor) { CHKPR(monitor, INVALID_HANDLER_ID); - return InputHandlerMgr.AddHandler(InputHandlerType::MONITOR, monitor); + return AddHandler(InputHandlerType::MONITOR, monitor); } void InputMonitorManager::RemoveMonitor(int32_t monitorId) { - InputHandlerMgr.RemoveHandler(monitorId, InputHandlerType::MONITOR); + RemoveHandler(monitorId, InputHandlerType::MONITOR); } void InputMonitorManager::MarkConsumed(int32_t monitorId, int32_t eventId) { - InputHandlerMgr.MarkConsumed(monitorId, eventId); + MMI_HILOGD("Mark consumed state, monitor:%{public}d,event:%{public}d", monitorId, eventId); + if (!HasHandler(monitorId)) { + MMI_HILOGW("Failed to find the monitorId"); + return; + } + int32_t ret = MultimodalInputConnMgr->MarkEventConsumed(eventId); + if (ret != RET_OK) { + MMI_HILOGE("Send to server failed, ret:%{public}d", ret); + } } } // namespace MMI } // namespace OHOS diff --git a/frameworks/proxy/event_handler/src/multimodal_event_handler.cpp b/frameworks/proxy/event_handler/src/multimodal_event_handler.cpp index 8b06283ac7466441f1ca047c677ab901cd8246d7..8aba816462f1e0f4d7fd3fef4a1cb0e75cf4d0ff 100755 --- a/frameworks/proxy/event_handler/src/multimodal_event_handler.cpp +++ b/frameworks/proxy/event_handler/src/multimodal_event_handler.cpp @@ -36,7 +36,8 @@ void OnConnected(const IfMMIClient& client) KeyEventInputSubscribeMgr.OnConnected(); #endif // OHOS_BUILD_ENABLE_KEYBOARD #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) - InputHandlerMgr.OnConnected(); + IMonitorMgr->OnConnected(); + InputInterMgr->OnConnected(); #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR } diff --git a/service/connect_manager/include/i_multimodal_input_connect.h b/service/connect_manager/include/i_multimodal_input_connect.h index 95c05025eac23540a22b001f743d465807c4aaf3..3df946b786174a0eeb24c958859fa3686f5198d1 100755 --- a/service/connect_manager/include/i_multimodal_input_connect.h +++ b/service/connect_manager/include/i_multimodal_input_connect.h @@ -42,10 +42,9 @@ public: virtual int32_t RegisterDevListener() = 0; virtual int32_t UnregisterDevListener() = 0; virtual int32_t GetKeyboardType(int32_t userData, int32_t deviceId) = 0; - virtual int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) = 0; - virtual int32_t RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) = 0; - virtual int32_t MarkEventConsumed(int32_t monitorId, int32_t eventId) = 0; + virtual int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) = 0; + virtual int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) = 0; + virtual int32_t MarkEventConsumed(int32_t eventId) = 0; virtual int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY) = 0; virtual int32_t InjectKeyEvent(const std::shared_ptr keyEvent) = 0; virtual int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option) = 0; diff --git a/service/connect_manager/include/multimodal_input_connect_manager.h b/service/connect_manager/include/multimodal_input_connect_manager.h index a0704fe426b2da0b21dc5e5b63f52c8231534338..50f0d3dd278ea9fee83d5add5ddd8d101ecfd2c1 100755 --- a/service/connect_manager/include/multimodal_input_connect_manager.h +++ b/service/connect_manager/include/multimodal_input_connect_manager.h @@ -42,10 +42,9 @@ public: int32_t RegisterDevListener(); int32_t UnregisterDevListener(); int32_t GetKeyboardType(int32_t userData, int32_t deviceId); - int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType); - int32_t RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType); - int32_t MarkEventConsumed(int32_t monitorId, int32_t eventId); + int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType); + int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType); + int32_t MarkEventConsumed(int32_t eventId); int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY); int32_t InjectKeyEvent(const std::shared_ptr keyEvent); int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option); diff --git a/service/connect_manager/include/multimodal_input_connect_proxy.h b/service/connect_manager/include/multimodal_input_connect_proxy.h index 3f483a026ef942b34a8fcbea69b25709aead6ff5..06c5dfe271d02997d21ac584601e09a7eb6ffd27 100755 --- a/service/connect_manager/include/multimodal_input_connect_proxy.h +++ b/service/connect_manager/include/multimodal_input_connect_proxy.h @@ -42,10 +42,9 @@ public: virtual int32_t RegisterDevListener() override; virtual int32_t UnregisterDevListener() override; virtual int32_t GetKeyboardType(int32_t userData, int32_t deviceId) override; - virtual int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) override; - virtual int32_t RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) override; - virtual int32_t MarkEventConsumed(int32_t monitorId, int32_t eventId) override; + virtual int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) override; + virtual int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) override; + virtual int32_t MarkEventConsumed(int32_t eventId) override; virtual int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY) override; virtual int32_t InjectKeyEvent(const std::shared_ptr keyEvent) override; virtual int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option) override; diff --git a/service/connect_manager/src/multimodal_input_connect_manager.cpp b/service/connect_manager/src/multimodal_input_connect_manager.cpp index dbde06b01b630d9a0fef740be91555135966ac24..a37a750517402ab4c03e0f301d9ee6378d12748e 100755 --- a/service/connect_manager/src/multimodal_input_connect_manager.cpp +++ b/service/connect_manager/src/multimodal_input_connect_manager.cpp @@ -131,23 +131,22 @@ int32_t MultimodalInputConnectManager::GetKeyboardType(int32_t userData, int32_t return multimodalInputConnectService_->GetKeyboardType(userData, deviceId); } -int32_t MultimodalInputConnectManager::AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) +int32_t MultimodalInputConnectManager::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CHKPR(multimodalInputConnectService_, INVALID_HANDLER_ID); - return multimodalInputConnectService_->AddInputHandler(handlerId, handlerType, eventType); + return multimodalInputConnectService_->AddInputHandler(handlerType, eventType); } -int32_t MultimodalInputConnectManager::RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) +int32_t MultimodalInputConnectManager::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CHKPR(multimodalInputConnectService_, INVALID_HANDLER_ID); - return multimodalInputConnectService_->RemoveInputHandler(handlerId, handlerType); + return multimodalInputConnectService_->RemoveInputHandler(handlerType, eventType); } -int32_t MultimodalInputConnectManager::MarkEventConsumed(int32_t monitorId, int32_t eventId) +int32_t MultimodalInputConnectManager::MarkEventConsumed(int32_t eventId) { CHKPR(multimodalInputConnectService_, INVALID_HANDLER_ID); - return multimodalInputConnectService_->MarkEventConsumed(monitorId, eventId); + return multimodalInputConnectService_->MarkEventConsumed(eventId); } int32_t MultimodalInputConnectManager::SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option) diff --git a/service/connect_manager/src/multimodal_input_connect_proxy.cpp b/service/connect_manager/src/multimodal_input_connect_proxy.cpp index 55ac63f32a369cb0466890e5cbf7eae925c7825c..2e951a4c38f7c2e134b889ceb17b68a78dc29f8b 100755 --- a/service/connect_manager/src/multimodal_input_connect_proxy.cpp +++ b/service/connect_manager/src/multimodal_input_connect_proxy.cpp @@ -273,7 +273,7 @@ int32_t MultimodalInputConnectProxy::GetKeyboardType(int32_t userData, int32_t d return RET_OK; } -int32_t MultimodalInputConnectProxy::AddInputHandler(int32_t handlerId, InputHandlerType handlerType, +int32_t MultimodalInputConnectProxy::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CALL_DEBUG_ENTER; @@ -282,9 +282,8 @@ int32_t MultimodalInputConnectProxy::AddInputHandler(int32_t handlerId, InputHan MMI_HILOGE("Failed to write descriptor"); return ERR_INVALID_VALUE; } - WRITEINT32(data, handlerId, ERR_INVALID_VALUE); WRITEINT32(data, handlerType, ERR_INVALID_VALUE); - WRITEINT32(data, eventType, ERR_INVALID_VALUE); + WRITEUINT32(data, eventType, ERR_INVALID_VALUE); MessageParcel reply; MessageOption option; sptr remote = Remote(); @@ -297,7 +296,7 @@ int32_t MultimodalInputConnectProxy::AddInputHandler(int32_t handlerId, InputHan return RET_OK; } -int32_t MultimodalInputConnectProxy::RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) +int32_t MultimodalInputConnectProxy::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CALL_DEBUG_ENTER; MessageParcel data; @@ -305,8 +304,8 @@ int32_t MultimodalInputConnectProxy::RemoveInputHandler(int32_t handlerId, Input MMI_HILOGE("Failed to write descriptor"); return ERR_INVALID_VALUE; } - WRITEINT32(data, handlerId, ERR_INVALID_VALUE); WRITEINT32(data, handlerType, ERR_INVALID_VALUE); + WRITEUINT32(data, eventType, ERR_INVALID_VALUE); MessageParcel reply; MessageOption option; sptr remote = Remote(); @@ -319,7 +318,7 @@ int32_t MultimodalInputConnectProxy::RemoveInputHandler(int32_t handlerId, Input return RET_OK; } -int32_t MultimodalInputConnectProxy::MarkEventConsumed(int32_t monitorId, int32_t eventId) +int32_t MultimodalInputConnectProxy::MarkEventConsumed(int32_t eventId) { CALL_DEBUG_ENTER; MessageParcel data; @@ -327,7 +326,6 @@ int32_t MultimodalInputConnectProxy::MarkEventConsumed(int32_t monitorId, int32_ MMI_HILOGE("Failed to write descriptor"); return ERR_INVALID_VALUE; } - WRITEINT32(data, monitorId, ERR_INVALID_VALUE); WRITEINT32(data, eventId, ERR_INVALID_VALUE); MessageParcel reply; MessageOption option; diff --git a/service/connect_manager/src/multimodal_input_connect_stub.cpp b/service/connect_manager/src/multimodal_input_connect_stub.cpp index 935b9e6a237374556d11d70ec0518878d8ba87b0..d93e09c9ffff9f175da32bd3efd52840078fa655 100755 --- a/service/connect_manager/src/multimodal_input_connect_stub.cpp +++ b/service/connect_manager/src/multimodal_input_connect_stub.cpp @@ -233,8 +233,6 @@ int32_t MultimodalInputConnectStub::StubAddInputHandler(MessageParcel& data, Mes MMI_HILOGE("Service is not running"); return MMISERVICE_NOT_RUNNING; } - int32_t handlerId; - READINT32(data, handlerId, IPC_PROXY_DEAD_OBJECT_ERR); int32_t handlerType; READINT32(data, handlerType, IPC_PROXY_DEAD_OBJECT_ERR); if ((handlerType == InputHandlerType::INTERCEPTOR) && @@ -246,10 +244,9 @@ int32_t MultimodalInputConnectStub::StubAddInputHandler(MessageParcel& data, Mes MMI_HILOGE("Monitor permission check failed"); return CHECK_PERMISSION_FAIL; } - int32_t eventType; - READINT32(data, eventType, IPC_PROXY_DEAD_OBJECT_ERR); - int32_t ret = AddInputHandler(handlerId, static_cast(handlerType), - static_cast(eventType)); + uint32_t eventType; + READUINT32(data, eventType, IPC_PROXY_DEAD_OBJECT_ERR); + int32_t ret = AddInputHandler(static_cast(handlerType), eventType); if (ret != RET_OK) { MMI_HILOGE("Call AddInputHandler failed ret:%{public}d", ret); return ret; @@ -264,8 +261,6 @@ int32_t MultimodalInputConnectStub::StubRemoveInputHandler(MessageParcel& data, MMI_HILOGE("Service is not running"); return MMISERVICE_NOT_RUNNING; } - int32_t handlerId; - READINT32(data, handlerId, IPC_PROXY_DEAD_OBJECT_ERR); int32_t handlerType; READINT32(data, handlerType, IPC_PROXY_DEAD_OBJECT_ERR); if ((handlerType == InputHandlerType::INTERCEPTOR) && @@ -277,7 +272,9 @@ int32_t MultimodalInputConnectStub::StubRemoveInputHandler(MessageParcel& data, MMI_HILOGE("Monitor permission check failed"); return CHECK_PERMISSION_FAIL; } - int32_t ret = RemoveInputHandler(handlerId, static_cast(handlerType)); + uint32_t eventType; + READUINT32(data, eventType, IPC_PROXY_DEAD_OBJECT_ERR); + int32_t ret = RemoveInputHandler(static_cast(handlerType), eventType); if (ret != RET_OK) { MMI_HILOGE("Call RemoveInputHandler failed ret:%{public}d", ret); return ret; @@ -297,11 +294,9 @@ int32_t MultimodalInputConnectStub::StubMarkEventConsumed(MessageParcel& data, M MMI_HILOGE("Service is not running"); return MMISERVICE_NOT_RUNNING; } - int32_t monitorId; - READINT32(data, monitorId, IPC_PROXY_DEAD_OBJECT_ERR); int32_t eventId; READINT32(data, eventId, IPC_PROXY_DEAD_OBJECT_ERR); - int32_t ret = MarkEventConsumed(monitorId, eventId); + int32_t ret = MarkEventConsumed(eventId); if (ret != RET_OK) { MMI_HILOGE("Call MarkEventConsumed failed ret:%{public}d", ret); return ret; diff --git a/service/interceptor/include/interceptor_handler_global.h b/service/interceptor/include/interceptor_handler_global.h index 06836916fdfe5e795c340272977a295952af9b3c..e337b7700529319d1f4eddf8e8004452b72975c1 100755 --- a/service/interceptor/include/interceptor_handler_global.h +++ b/service/interceptor/include/interceptor_handler_global.h @@ -41,9 +41,8 @@ public: #ifdef OHOS_BUILD_ENABLE_TOUCH void HandleTouchEvent(std::shared_ptr pointerEvent) override; #endif // OHOS_BUILD_ENABLE_TOUCH - int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType, SessionPtr session); - void RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType, SessionPtr session); + int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); + void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); #ifdef OHOS_BUILD_ENABLE_KEYBOARD bool HandleEvent(std::shared_ptr keyEvent); #endif // OHOS_BUILD_ENABLE_KEYBOARD @@ -57,22 +56,15 @@ private: private: class SessionHandler { public: - SessionHandler(int32_t id, InputHandlerType handlerType, HandleEventType eventType, - SessionPtr session) : id_(id), handlerType_(handlerType), eventType_(eventType), - session_(session) { } + SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session) + : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL), + session_(session) { } void SendToClient(std::shared_ptr keyEvent) const; void SendToClient(std::shared_ptr pointerEvent) const; bool operator<(const SessionHandler& other) const { - if (id_ != other.id_) { - return (id_ < other.id_); - } - if (handlerType_ != other.handlerType_) { - return (handlerType_ < other.handlerType_); - } return (session_ < other.session_); } - int32_t id_; InputHandlerType handlerType_; HandleEventType eventType_; SessionPtr session_ = nullptr; diff --git a/service/interceptor/src/interceptor_handler_global.cpp b/service/interceptor/src/interceptor_handler_global.cpp index 0f036316a1201977919553176cfd439fa8a14512..d8739f58536956b802eb3cacf8f875ca371d31c9 100755 --- a/service/interceptor/src/interceptor_handler_global.cpp +++ b/service/interceptor/src/interceptor_handler_global.cpp @@ -74,33 +74,27 @@ void InterceptorHandlerGlobal::HandleTouchEvent(std::shared_ptr po } #endif // OHOS_BUILD_ENABLE_TOUCH -int32_t InterceptorHandlerGlobal::AddInputHandler(int32_t handlerId, - InputHandlerType handlerType, HandleEventType eventType, SessionPtr session) +int32_t InterceptorHandlerGlobal::AddInputHandler(InputHandlerType handlerType, + HandleEventType eventType, SessionPtr session) { CALL_INFO_TRACE; CHKPR(session, RET_ERR); - if (!IsValidHandlerId(handlerId)) { - MMI_HILOGE("Invalid handler"); - return RET_ERR; - } - if (handlerType != InputHandlerType::INTERCEPTOR) { - MMI_HILOGW("Invalid handler type:%{public}d", handlerType); + if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) { + MMI_HILOGE("Invalid event type"); return RET_ERR; } InitSessionLostCallback(); - MMI_HILOGD("Register interceptor:%{public}d", handlerId); - SessionHandler interceptor { handlerId, handlerType, eventType, session }; + SessionHandler interceptor { handlerType, eventType, session }; return interceptors_.AddInterceptor(interceptor); } -void InterceptorHandlerGlobal::RemoveInputHandler(int32_t handlerId, - InputHandlerType handlerType, SessionPtr session) +void InterceptorHandlerGlobal::RemoveInputHandler(InputHandlerType handlerType, + HandleEventType eventType, SessionPtr session) { CALL_INFO_TRACE; CHKPV(session); if (handlerType == InputHandlerType::INTERCEPTOR) { - MMI_HILOGD("Unregister interceptor:%{public}d", handlerId); - SessionHandler interceptor { handlerId, handlerType, HandleEventType::ALL, session }; + SessionHandler interceptor { handlerType, eventType, session }; interceptors_.RemoveInterceptor(interceptor); } } @@ -152,7 +146,7 @@ void InterceptorHandlerGlobal::SessionHandler::SendToClient(std::shared_ptreventType_ == interceptor.eventType_) { + MMI_HILOGD("Interceptor with event type (%{public}u) already exists", interceptor.eventType_); + return RET_OK; + } + isFound = true; + interceptors_.erase(iter); + } + + auto [sIter, isOk] = interceptors_.insert(interceptor); + if (!isOk) { + if (isFound) { + MMI_HILOGE("Internal error: interceptor has been removed"); + } else { + MMI_HILOGE("Failed to add interceptor"); + } + return RET_ERR; + } + + if (isFound) { + MMI_HILOGD("Event type is updated: %{public}u", interceptor.eventType_); + } else { + MMI_HILOGD("Service AddInterceptor Success"); } - MMI_HILOGD("Register interceptor successfully"); return RET_OK; } void InterceptorHandlerGlobal::InterceptorCollection::RemoveInterceptor(const SessionHandler& interceptor) { - std::set::const_iterator tItr = interceptors_.find(interceptor); - if (tItr != interceptors_.cend()) { - interceptors_.erase(tItr); + std::set::const_iterator iter = interceptors_.find(interceptor); + if (iter == interceptors_.cend()) { + MMI_HILOGE("Interceptor does not exist"); + return; + } + + interceptors_.erase(iter); + if (interceptor.eventType_ == HANDLE_EVENT_TYPE_NONE) { + MMI_HILOGD("Unregister interceptor successfully"); + return; + } + + auto [sIter, isOk] = interceptors_.insert(interceptor); + if (!isOk) { + MMI_HILOGE("Internal error, interceptor has been removed"); + return; } - MMI_HILOGD("Unregister interceptor successfully"); + MMI_HILOGD("Event type is updated: %{public}u", interceptor.eventType_); } void InterceptorHandlerGlobal::InterceptorCollection::OnSessionLost(SessionPtr session) @@ -279,9 +306,9 @@ void InterceptorHandlerGlobal::InterceptorCollection::Dump(int32_t fd, const std SessionPtr session = item.session_; CHKPV(session); mprintf(fd, - "interceptor id:%d | handlerType:%d | eventType:%d | Pid:%d | Uid:%d | Fd:%d " + "handlerType:%d | eventType:%d | Pid:%d | Uid:%d | Fd:%d " "| EarliestEventTime:%" PRId64 " | Descript:%s \t", - item.id_, item.handlerType_, item.eventType_, + item.handlerType_, item.eventType_, session->GetPid(), session->GetUid(), session->GetFd(), session->GetEarliestEventTime(), session->GetDescript().c_str()); diff --git a/service/message_handle/include/server_msg_handler.h b/service/message_handle/include/server_msg_handler.h index b319b681da3f0ee256671296880020c5ee6dec8a..1065656c400952dc00731c0c38f47762756b704b 100755 --- a/service/message_handle/include/server_msg_handler.h +++ b/service/message_handle/include/server_msg_handler.h @@ -35,12 +35,11 @@ public: void Init(UDSServer& udsServer); void OnMsgHandler(SessionPtr sess, NetPacket& pkt); #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) - int32_t OnAddInputHandler(SessionPtr sess, int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType); - int32_t OnRemoveInputHandler(SessionPtr sess, int32_t handlerId, InputHandlerType handlerType); + int32_t OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType); + int32_t OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType); #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR #ifdef OHOS_BUILD_ENABLE_MONITOR - int32_t OnMarkConsumed(SessionPtr sess, int32_t monitorId, int32_t eventId); + int32_t OnMarkConsumed(SessionPtr sess, int32_t eventId); #endif // OHOS_BUILD_ENABLE_MONITOR #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t OnSubscribeKeyEvent(IUdsServer *server, int32_t pid, diff --git a/service/message_handle/src/server_msg_handler.cpp b/service/message_handle/src/server_msg_handler.cpp index 229b40b86ecb6a9753d752362584f902941be2cc..a894fae38b951c30477fee9be0f4c100e91c4ff1 100755 --- a/service/message_handle/src/server_msg_handler.cpp +++ b/service/message_handle/src/server_msg_handler.cpp @@ -226,44 +226,45 @@ int32_t ServerMsgHandler::OnDisplayInfo(SessionPtr sess, NetPacket &pkt) } #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) -int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, int32_t handlerId, InputHandlerType handlerType, +int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType, HandleEventType eventType) { CHKPR(sess, ERROR_NULL_POINTER); - MMI_HILOGD("handler:%{public}d, handlerType:%{public}d", handlerId, handlerType); + MMI_HILOGD("handlerType:%{public}d", handlerType); #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR if (handlerType == InputHandlerType::INTERCEPTOR) { auto interceptorHandler = InputHandler->GetInterceptorHandler(); CHKPR(interceptorHandler, ERROR_NULL_POINTER); - return interceptorHandler->AddInputHandler(handlerId, handlerType, eventType, sess); + return interceptorHandler->AddInputHandler(handlerType, eventType, sess); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR #ifdef OHOS_BUILD_ENABLE_MONITOR if (handlerType == InputHandlerType::MONITOR) { auto monitorHandler = InputHandler->GetMonitorHandler(); CHKPR(monitorHandler, ERROR_NULL_POINTER); - return monitorHandler->AddInputHandler(handlerId, handlerType, sess); + return monitorHandler->AddInputHandler(handlerType, eventType, sess); } #endif // OHOS_BUILD_ENABLE_MONITOR return RET_OK; } -int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, int32_t handlerId, InputHandlerType handlerType) +int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType, + HandleEventType eventType) { CHKPR(sess, ERROR_NULL_POINTER); - MMI_HILOGD("OnRemoveInputHandler handler:%{public}d,handlerType:%{public}d", handlerId, handlerType); + MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d eventType:%{public}u", handlerType, eventType); #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR if (handlerType == InputHandlerType::INTERCEPTOR) { auto interceptorHandler = InputHandler->GetInterceptorHandler(); CHKPR(interceptorHandler, ERROR_NULL_POINTER); - interceptorHandler->RemoveInputHandler(handlerId, handlerType, sess); + interceptorHandler->RemoveInputHandler(handlerType, eventType, sess); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR #ifdef OHOS_BUILD_ENABLE_MONITOR if (handlerType == InputHandlerType::MONITOR) { auto monitorHandler = InputHandler->GetMonitorHandler(); CHKPR(monitorHandler, ERROR_NULL_POINTER); - monitorHandler->RemoveInputHandler(handlerId, handlerType, sess); + monitorHandler->RemoveInputHandler(handlerType, eventType, sess); } #endif // OHOS_BUILD_ENABLE_MONITOR return RET_OK; @@ -271,12 +272,12 @@ int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, int32_t handlerI #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR #ifdef OHOS_BUILD_ENABLE_MONITOR -int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t monitorId, int32_t eventId) +int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t eventId) { CHKPR(sess, ERROR_NULL_POINTER); auto monitorHandler = InputHandler->GetMonitorHandler(); CHKPR(monitorHandler, ERROR_NULL_POINTER); - monitorHandler->MarkConsumed(monitorId, eventId, sess); + monitorHandler->MarkConsumed(eventId, sess); return RET_OK; } #endif // OHOS_BUILD_ENABLE_MONITOR diff --git a/service/module_loader/include/mmi_service.h b/service/module_loader/include/mmi_service.h index a8011ae3e8a75c4de4c7e2bb0489290ee25c7df0..c3e4c8ce8aab024ad8650462fa5afcb06d308a6f 100755 --- a/service/module_loader/include/mmi_service.h +++ b/service/module_loader/include/mmi_service.h @@ -60,10 +60,9 @@ public: virtual int32_t RegisterDevListener() override; virtual int32_t UnregisterDevListener() override; virtual int32_t GetKeyboardType(int32_t userData, int32_t deviceId) override; - virtual int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) override; - virtual int32_t RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) override; - virtual int32_t MarkEventConsumed(int32_t monitorId, int32_t eventId) override; + virtual int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) override; + virtual int32_t RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) override; + virtual int32_t MarkEventConsumed(int32_t eventId) override; virtual int32_t MoveMouseEvent(int32_t offsetX, int32_t offsetY) override; virtual int32_t InjectKeyEvent(const std::shared_ptr keyEvent) override; virtual int32_t SubscribeKeyEvent(int32_t subscribeId, const std::shared_ptr option) override; @@ -92,11 +91,10 @@ protected: int32_t OnSupportKeys(int32_t pid, int32_t userData, int32_t deviceId, std::vector &keys); int32_t OnGetKeyboardType(int32_t pid, int32_t userData, int32_t deviceId); #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) - int32_t CheckAddInput(int32_t pid, int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType); - int32_t CheckRemoveInput(int32_t pid, int32_t handlerId, InputHandlerType handlerType); + int32_t CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType); + int32_t CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType); #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR - int32_t CheckMarkConsumed(int32_t pid, int32_t monitorId, int32_t eventId); + int32_t CheckMarkConsumed(int32_t pid, int32_t eventId); #ifdef OHOS_BUILD_ENABLE_KEYBOARD int32_t CheckInjectKeyEvent(const std::shared_ptr keyEvent); #endif // OHOS_BUILD_ENABLE_KEYBOARD diff --git a/service/module_loader/src/mmi_service.cpp b/service/module_loader/src/mmi_service.cpp index 0d8906f84d435be97174f887f1609180b4091f61..9837fe7a9d53be11b5e45b8c40e1b0e44ef2536c 100755 --- a/service/module_loader/src/mmi_service.cpp +++ b/service/module_loader/src/mmi_service.cpp @@ -609,23 +609,22 @@ int32_t MMIService::GetKeyboardType(int32_t userData, int32_t deviceId) } #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) -int32_t MMIService::CheckAddInput(int32_t pid, int32_t handlerId, InputHandlerType handlerType, +int32_t MMIService::CheckAddInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); - return sMsgHandler_.OnAddInputHandler(sess, handlerId, handlerType, eventType); + return sMsgHandler_.OnAddInputHandler(sess, handlerType, eventType); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR -int32_t MMIService::AddInputHandler(int32_t handlerId, InputHandlerType handlerType, - HandleEventType eventType) +int32_t MMIService::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( - std::bind(&MMIService::CheckAddInput, this, pid, handlerId, handlerType, eventType)); + std::bind(&MMIService::CheckAddInput, this, pid, handlerType, eventType)); if (ret != RET_OK) { MMI_HILOGE("Add input handler failed, ret:%{public}d", ret); return RET_ERR; @@ -635,21 +634,21 @@ int32_t MMIService::AddInputHandler(int32_t handlerId, InputHandlerType handlerT } #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) -int32_t MMIService::CheckRemoveInput(int32_t pid, int32_t handlerId, InputHandlerType handlerType) +int32_t MMIService::CheckRemoveInput(int32_t pid, InputHandlerType handlerType, HandleEventType eventType) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); - return sMsgHandler_.OnRemoveInputHandler(sess, handlerId, handlerType); + return sMsgHandler_.OnRemoveInputHandler(sess, handlerType, eventType); } #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR -int32_t MMIService::RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType) +int32_t MMIService::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType) { CALL_INFO_TRACE; #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( - std::bind(&MMIService::CheckRemoveInput, this, pid, handlerId, handlerType)); + std::bind(&MMIService::CheckRemoveInput, this, pid, handlerType, eventType)); if (ret != RET_OK) { MMI_HILOGE("Remove input handler failed, ret:%{public}d", ret); return RET_ERR; @@ -659,21 +658,21 @@ int32_t MMIService::RemoveInputHandler(int32_t handlerId, InputHandlerType handl } #ifdef OHOS_BUILD_ENABLE_MONITOR -int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t monitorId, int32_t eventId) +int32_t MMIService::CheckMarkConsumed(int32_t pid, int32_t eventId) { auto sess = GetSessionByPid(pid); CHKPR(sess, ERROR_NULL_POINTER); - return sMsgHandler_.OnMarkConsumed(sess, monitorId, eventId); + return sMsgHandler_.OnMarkConsumed(sess, eventId); } #endif // OHOS_BUILD_ENABLE_MONITOR -int32_t MMIService::MarkEventConsumed(int32_t monitorId, int32_t eventId) +int32_t MMIService::MarkEventConsumed(int32_t eventId) { CALL_INFO_TRACE; #ifdef OHOS_BUILD_ENABLE_MONITOR int32_t pid = GetCallingPid(); int32_t ret = delegateTasks_.PostSyncTask( - std::bind(&MMIService::CheckMarkConsumed, this, pid, monitorId, eventId)); + std::bind(&MMIService::CheckMarkConsumed, this, pid, eventId)); if (ret != RET_OK) { MMI_HILOGE("Mark event consumed failed, ret:%{public}d", ret); return RET_ERR; diff --git a/service/monitor/include/input_handler_manager_global.h b/service/monitor/include/input_handler_manager_global.h index 595f839d1f4665d781182c3f993cd532045b1c71..e82c68d92c9f9e1bbcc79991b0142162ed2e22e3 100755 --- a/service/monitor/include/input_handler_manager_global.h +++ b/service/monitor/include/input_handler_manager_global.h @@ -43,9 +43,9 @@ public: #ifdef OHOS_BUILD_ENABLE_TOUCH void HandleTouchEvent(std::shared_ptr pointerEvent) override; #endif // OHOS_BUILD_ENABLE_TOUCH - int32_t AddInputHandler(int32_t handlerId, InputHandlerType handlerType, SessionPtr session); - void RemoveInputHandler(int32_t handlerId, InputHandlerType handlerType, SessionPtr session); - void MarkConsumed(int32_t handlerId, int32_t eventId, SessionPtr session); + int32_t AddInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); + void RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session); + void MarkConsumed(int32_t eventId, SessionPtr session); #ifdef OHOS_BUILD_ENABLE_KEYBOARD bool HandleEvent(std::shared_ptr KeyEvent); #endif // OHOS_BUILD_ENABLE_KEYBOARD @@ -61,22 +61,17 @@ private: private: class SessionHandler { public: - SessionHandler(int32_t id, InputHandlerType handlerType, SessionPtr session) - : id_(id), handlerType_(handlerType), session_(session) { } + SessionHandler(InputHandlerType handlerType, HandleEventType eventType, SessionPtr session) + : handlerType_(handlerType), eventType_(eventType & HANDLE_EVENT_TYPE_ALL), + session_(session) { } void SendToClient(std::shared_ptr keyEvent) const; void SendToClient(std::shared_ptr pointerEvent) const; bool operator<(const SessionHandler& other) const { - if (id_ != other.id_) { - return (id_ < other.id_); - } - if (handlerType_ != other.handlerType_) { - return (handlerType_ < other.handlerType_); - } return (session_ < other.session_); } - int32_t id_; InputHandlerType handlerType_; + HandleEventType eventType_; SessionPtr session_ = nullptr; }; @@ -90,9 +85,9 @@ private: #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH int32_t AddMonitor(const SessionHandler& mon); void RemoveMonitor(const SessionHandler& mon); - void MarkConsumed(int32_t monitorId, int32_t eventId, SessionPtr session); + void MarkConsumed(int32_t eventId, SessionPtr session); - bool HasMonitor(int32_t monitorId, SessionPtr session); + bool HasMonitor(SessionPtr session); #ifdef OHOS_BUILD_ENABLE_TOUCH void UpdateConsumptionState(std::shared_ptr pointerEvent); #endif // OHOS_BUILD_ENABLE_TOUCH diff --git a/service/monitor/src/input_handler_manager_global.cpp b/service/monitor/src/input_handler_manager_global.cpp index 1c7ec2ec65d4aeafcb4e15261b9e89ae424e3c04..d2486082cf3e34b31459bd136c8b695976eded42 100755 --- a/service/monitor/src/input_handler_manager_global.cpp +++ b/service/monitor/src/input_handler_manager_global.cpp @@ -68,41 +68,33 @@ void InputHandlerManagerGlobal::HandleTouchEvent(std::shared_ptr p } #endif // OHOS_BUILD_ENABLE_TOUCH -int32_t InputHandlerManagerGlobal::AddInputHandler(int32_t handlerId, - InputHandlerType handlerType, SessionPtr session) +int32_t InputHandlerManagerGlobal::AddInputHandler(InputHandlerType handlerType, + HandleEventType eventType, SessionPtr session) { CALL_INFO_TRACE; - InitSessionLostCallback(); - if (!IsValidHandlerId(handlerId)) { - MMI_HILOGE("Invalid handler"); - return RET_ERR; - } CHKPR(session, RET_ERR); - if (handlerType == InputHandlerType::MONITOR) { - MMI_HILOGD("Register monitor:%{public}d", handlerId); - SessionHandler mon { handlerId, handlerType, session }; - return monitors_.AddMonitor(mon); + if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) { + MMI_HILOGE("Invalid event type"); + return RET_ERR; } - MMI_HILOGW("Invalid handler type:%{public}d", handlerType); - return RET_ERR; + InitSessionLostCallback(); + SessionHandler mon { handlerType, eventType, session }; + return monitors_.AddMonitor(mon); } -void InputHandlerManagerGlobal::RemoveInputHandler(int32_t handlerId, - InputHandlerType handlerType, SessionPtr session) +void InputHandlerManagerGlobal::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType, + SessionPtr session) { CALL_INFO_TRACE; if (handlerType == InputHandlerType::MONITOR) { - MMI_HILOGD("Unregister monitor:%{public}d", handlerId); - SessionHandler monitor { handlerId, handlerType, session }; + SessionHandler monitor { handlerType, eventType, session }; monitors_.RemoveMonitor(monitor); } } -void InputHandlerManagerGlobal::MarkConsumed(int32_t handlerId, int32_t eventId, SessionPtr session) +void InputHandlerManagerGlobal::MarkConsumed(int32_t eventId, SessionPtr session) { - CALL_INFO_TRACE; - MMI_HILOGD("Mark consumed state, monitor:%{public}d", handlerId); - monitors_.MarkConsumed(handlerId, eventId, session); + monitors_.MarkConsumed(eventId, session); } #ifdef OHOS_BUILD_ENABLE_KEYBOARD @@ -161,7 +153,7 @@ void InputHandlerManagerGlobal::SessionHandler::SendToClient(std::shared_ptreventType_ == monitor.eventType_) { + MMI_HILOGD("Monitor with event type (%{public}u) already exists", monitor.eventType_); + return RET_OK; + } + isFound = true; + monitors_.erase(iter); + } + + auto [sIter, isOk] = monitors_.insert(monitor); + if (!isOk) { + if (isFound) { + MMI_HILOGE("Internal error: monitor has been removed"); + } else { + MMI_HILOGE("Failed to add monitor"); + } + return RET_ERR; + } + + if (isFound) { + MMI_HILOGD("Event type is updated: %{public}u", monitor.eventType_); } else { - MMI_HILOGW("Duplicate monitors"); + MMI_HILOGD("Service Add Monitor Success"); } return RET_OK; } void InputHandlerManagerGlobal::MonitorCollection::RemoveMonitor(const SessionHandler& monitor) { - std::set::const_iterator tItr = monitors_.find(monitor); - if (tItr != monitors_.end()) { - monitors_.erase(tItr); - MMI_HILOGD("Service RemoveMonitor Success"); + auto iter = monitors_.find(monitor); + if (iter == monitors_.cend()) { + MMI_HILOGE("Monitor does not exist"); + return; + } + + monitors_.erase(iter); + if (monitor.eventType_ == HANDLE_EVENT_TYPE_NONE) { + MMI_HILOGD("Unregister monitor successfully"); + return; + } + + auto [sIter, isOk] = monitors_.insert(monitor); + if (!isOk) { + MMI_HILOGE("Internal error, monitor has been removed"); + return; } + MMI_HILOGD("Event type is updated: %{public}u", monitor.eventType_); } -void InputHandlerManagerGlobal::MonitorCollection::MarkConsumed(int32_t monitorId, int32_t eventId, SessionPtr session) +void InputHandlerManagerGlobal::MonitorCollection::MarkConsumed(int32_t eventId, SessionPtr session) { - if (!HasMonitor(monitorId, session)) { - MMI_HILOGW("Specified monitor does not exist, monitor:%{public}d", monitorId); + if (!HasMonitor(session)) { + MMI_HILOGW("Specified monitor does not exist"); return; } if (isMonitorConsumed_) { @@ -258,7 +283,9 @@ bool InputHandlerManagerGlobal::MonitorCollection::HandleEvent(std::shared_ptrGetPid(), + item.handlerType_, session->GetPid(), session->GetUid(), session->GetFd(), session->GetEarliestEventTime(), session->GetDescript().c_str()); }