1 /*
2 * Copyright (c) 2021-2024 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 #include "multimodal_event_handler.h"
17
18 #include "event_log_helper.h"
19 #include "input_event.h"
20 #include "input_event_data_transformation.h"
21 #include "input_manager_impl.h"
22 #include "input_handler_manager.h"
23 #include "mmi_client.h"
24 #include "multimodal_input_connect_manager.h"
25 #include "proto.h"
26 #include "switch_event_input_subscribe_manager.h"
27 #include "pre_monitor_manager.h"
28
29 #undef MMI_LOG_DOMAIN
30 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
31 #undef MMI_LOG_TAG
32 #define MMI_LOG_TAG "MultimodalEventHandler"
33
34 namespace OHOS {
35 namespace MMI {
OnConnected(const IfMMIClient & client)36 void OnConnected(const IfMMIClient& client)
37 {
38 CALL_DEBUG_ENTER;
39 InputMgrImpl.OnConnected();
40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
41 KeyEventInputSubscribeMgr.OnConnected();
42 #endif // OHOS_BUILD_ENABLE_KEYBOARD
43 #ifdef OHOS_BUILD_ENABLE_SWITCH
44 SWITCH_EVENT_INPUT_SUBSCRIBE_MGR.OnConnected();
45 #endif // OHOS_BUILD_ENABLE_SWITCH
46 #ifdef OHOS_BUILD_ENABLE_MONITOR
47 IMonitorMgr->OnConnected();
48 PRE_MONITOR_MGR.OnConnected();
49 #endif // OHOS_BUILD_ENABLE_MONITOR
50 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
51 InputInterMgr->OnConnected();
52 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
53 }
54
OnDisconnected(const IfMMIClient & client)55 void OnDisconnected(const IfMMIClient &client)
56 {
57 CALL_DEBUG_ENTER;
58 InputMgrImpl.OnDisconnected();
59 #ifdef OHOS_BUILD_ENABLE_MONITOR
60 IMonitorMgr->OnDisconnected();
61 #endif // OHOS_BUILD_ENABLE_MONITOR
62 }
63
MultimodalEventHandler()64 MultimodalEventHandler::MultimodalEventHandler() {}
~MultimodalEventHandler()65 MultimodalEventHandler::~MultimodalEventHandler() {}
66
67 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SubscribeKeyEvent(const KeyEventInputSubscribeManager::SubscribeKeyEventInfo & subscribeInfo)68 int32_t MultimodalEventHandler::SubscribeKeyEvent(
69 const KeyEventInputSubscribeManager::SubscribeKeyEventInfo &subscribeInfo)
70 {
71 CALL_DEBUG_ENTER;
72 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
73 return MULTIMODAL_INPUT_CONNECT_MGR->SubscribeKeyEvent(subscribeInfo.GetSubscribeId(),
74 subscribeInfo.GetKeyOption());
75 }
76
UnsubscribeKeyEvent(int32_t subscribeId)77 int32_t MultimodalEventHandler::UnsubscribeKeyEvent(int32_t subscribeId)
78 {
79 CALL_DEBUG_ENTER;
80 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
81 return MULTIMODAL_INPUT_CONNECT_MGR->UnsubscribeKeyEvent(subscribeId);
82 }
83
InjectEvent(const std::shared_ptr<KeyEvent> keyEvent,bool isNativeInject)84 int32_t MultimodalEventHandler::InjectEvent(const std::shared_ptr<KeyEvent> keyEvent, bool isNativeInject)
85 {
86 CALL_DEBUG_ENTER;
87 CHKPR(keyEvent, ERROR_NULL_POINTER);
88 EndLogTraceId(keyEvent->GetId());
89 keyEvent->UpdateId();
90 LogTracer lt(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
91 if (keyEvent->GetKeyCode() < 0) {
92 if (EventLogHelper::IsBetaVersion()) {
93 MMI_HILOGE("KeyCode is invalid:%{private}u", keyEvent->GetKeyCode());
94 }
95 return RET_ERR;
96 }
97 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
98 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->InjectKeyEvent(keyEvent, isNativeInject);
99 if (ret != 0) {
100 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
101 return RET_ERR;
102 }
103 return RET_OK;
104 }
105 #endif // OHOS_BUILD_ENABLE_KEYBOARD
106
107 #ifdef OHOS_BUILD_ENABLE_SWITCH
SubscribeSwitchEvent(int32_t subscribeId,int32_t switchType)108 int32_t MultimodalEventHandler::SubscribeSwitchEvent(int32_t subscribeId, int32_t switchType)
109 {
110 CALL_DEBUG_ENTER;
111 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
112 return MULTIMODAL_INPUT_CONNECT_MGR->SubscribeSwitchEvent(subscribeId, switchType);
113 }
114
UnsubscribeSwitchEvent(int32_t subscribeId)115 int32_t MultimodalEventHandler::UnsubscribeSwitchEvent(int32_t subscribeId)
116 {
117 CALL_DEBUG_ENTER;
118 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
119 return MULTIMODAL_INPUT_CONNECT_MGR->UnsubscribeSwitchEvent(subscribeId);
120 }
121 #endif // OHOS_BUILD_ENABLE_SWITCH
122
InitClient(EventHandlerPtr eventHandler)123 bool MultimodalEventHandler::InitClient(EventHandlerPtr eventHandler)
124 {
125 CALL_DEBUG_ENTER;
126 if (client_ != nullptr) {
127 if (eventHandler != nullptr) {
128 client_->MarkIsEventHandlerChanged(eventHandler);
129 }
130 return true;
131 }
132 client_ = std::make_shared<MMIClient>();
133 client_->SetEventHandler(eventHandler);
134 client_->RegisterConnectedFunction(&OnConnected);
135 client_->RegisterDisconnectedFunction(&OnDisconnected);
136 if (!client_->Start()) {
137 client_ = nullptr;
138 MMI_HILOGE("The client fails to start");
139 return false;
140 }
141 return true;
142 }
143
GetMMIClient()144 MMIClientPtr MultimodalEventHandler::GetMMIClient()
145 {
146 CHKPP(client_);
147 return client_->GetSharedPtr();
148 }
149
150 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent,bool isNativeInject)151 int32_t MultimodalEventHandler::InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent, bool isNativeInject)
152 {
153 CHKPR(pointerEvent, ERROR_NULL_POINTER);
154 EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_HEADER);
155 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
156 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->InjectPointerEvent(pointerEvent, isNativeInject);
157 if (ret != 0) {
158 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
159 return RET_ERR;
160 }
161 return RET_OK;
162 }
163 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
164
165 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
MoveMouseEvent(int32_t offsetX,int32_t offsetY)166 int32_t MultimodalEventHandler::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
167 {
168 CALL_DEBUG_ENTER;
169 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
170 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->MoveMouseEvent(offsetX, offsetY);
171 if (ret != 0) {
172 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
173 return RET_ERR;
174 }
175 return RET_OK;
176 }
177 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
178
Authorize(bool isAuthorize)179 int32_t MultimodalEventHandler::Authorize(bool isAuthorize)
180 {
181 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
182 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->Authorize(isAuthorize);
183 if (ret != RET_OK) {
184 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
185 return RET_ERR;
186 }
187 return RET_OK;
188 }
189
CancelInjection()190 int32_t MultimodalEventHandler::CancelInjection()
191 {
192 CHKPR(MULTIMODAL_INPUT_CONNECT_MGR, RET_ERR);
193 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->CancelInjection();
194 if (ret != RET_OK) {
195 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
196 return RET_ERR;
197 }
198 return RET_OK;
199 }
200 } // namespace MMI
201 } // namespace OHOS
202