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