1 /*
2 * Copyright (c) 2021-2022 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 "proto.h"
19
20 #include "event_log_helper.h"
21 #include "input_event.h"
22 #include "input_event_data_transformation.h"
23 #include "input_manager_impl.h"
24 #include "input_handler_manager.h"
25 #include "mmi_client.h"
26 #include "multimodal_input_connect_manager.h"
27 #include "switch_event_input_subscribe_manager.h"
28
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MultimodalEventHandler" };
33 } // namespace
34
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
MultimodalEventHandler()53 MultimodalEventHandler::MultimodalEventHandler() {}
~MultimodalEventHandler()54 MultimodalEventHandler::~MultimodalEventHandler() {}
55
56 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SubscribeKeyEvent(const KeyEventInputSubscribeManager::SubscribeKeyEventInfo & subscribeInfo)57 int32_t MultimodalEventHandler::SubscribeKeyEvent(
58 const KeyEventInputSubscribeManager::SubscribeKeyEventInfo &subscribeInfo)
59 {
60 CALL_DEBUG_ENTER;
61 return MultimodalInputConnMgr->SubscribeKeyEvent(subscribeInfo.GetSubscribeId(), subscribeInfo.GetKeyOption());
62 }
63
UnsubscribeKeyEvent(int32_t subscribeId)64 int32_t MultimodalEventHandler::UnsubscribeKeyEvent(int32_t subscribeId)
65 {
66 CALL_DEBUG_ENTER;
67 return MultimodalInputConnMgr->UnsubscribeKeyEvent(subscribeId);
68 }
69
InjectEvent(const std::shared_ptr<KeyEvent> keyEvent)70 int32_t MultimodalEventHandler::InjectEvent(const std::shared_ptr<KeyEvent> keyEvent)
71 {
72 CALL_DEBUG_ENTER;
73 CHKPR(keyEvent, ERROR_NULL_POINTER);
74 keyEvent->UpdateId();
75 if (keyEvent->GetKeyCode() < 0) {
76 MMI_HILOGE("KeyCode is invalid:%{public}u", keyEvent->GetKeyCode());
77 return RET_ERR;
78 }
79 int32_t ret = MultimodalInputConnMgr->InjectKeyEvent(keyEvent);
80 if (ret != 0) {
81 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
82 return RET_ERR;
83 }
84 return RET_OK;
85 }
86 #endif // OHOS_BUILD_ENABLE_KEYBOARD
87
88 #ifdef OHOS_BUILD_ENABLE_SWITCH
SubscribeSwitchEvent(int32_t subscribeId)89 int32_t MultimodalEventHandler::SubscribeSwitchEvent(int32_t subscribeId)
90 {
91 CALL_DEBUG_ENTER;
92 return MultimodalInputConnMgr->SubscribeSwitchEvent(subscribeId);
93 }
94
UnsubscribeSwitchEvent(int32_t subscribeId)95 int32_t MultimodalEventHandler::UnsubscribeSwitchEvent(int32_t subscribeId)
96 {
97 CALL_DEBUG_ENTER;
98 return MultimodalInputConnMgr->UnsubscribeSwitchEvent(subscribeId);
99 }
100 #endif // OHOS_BUILD_ENABLE_SWITCH
101
InitClient(EventHandlerPtr eventHandler)102 bool MultimodalEventHandler::InitClient(EventHandlerPtr eventHandler)
103 {
104 CALL_DEBUG_ENTER;
105 if (client_ != nullptr) {
106 if (eventHandler != nullptr) {
107 client_->MarkIsEventHandlerChanged(eventHandler);
108 }
109 return true;
110 }
111 client_ = std::make_shared<MMIClient>();
112 client_->SetEventHandler(eventHandler);
113 client_->RegisterConnectedFunction(&OnConnected);
114 if (!(client_->Start())) {
115 client_.reset();
116 client_ = nullptr;
117 MMI_HILOGE("The client fails to start");
118 return false;
119 }
120 return true;
121 }
122
GetMMIClient()123 MMIClientPtr MultimodalEventHandler::GetMMIClient()
124 {
125 CHKPP(client_);
126 return client_->GetSharedPtr();
127 }
128
129 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)130 int32_t MultimodalEventHandler::InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
131 {
132 CALL_DEBUG_ENTER;
133 CHKPR(pointerEvent, ERROR_NULL_POINTER);
134 EventLogHelper::PrintEventData(pointerEvent);
135 int32_t ret = MultimodalInputConnMgr->InjectPointerEvent(pointerEvent);
136 if (ret != 0) {
137 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
138 return RET_ERR;
139 }
140 return RET_OK;
141 }
142 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
143
144 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
MoveMouseEvent(int32_t offsetX,int32_t offsetY)145 int32_t MultimodalEventHandler::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
146 {
147 CALL_DEBUG_ENTER;
148 int32_t ret = MultimodalInputConnMgr->MoveMouseEvent(offsetX, offsetY);
149 if (ret != 0) {
150 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
151 return RET_ERR;
152 }
153 return RET_OK;
154 }
155 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
156 } // namespace MMI
157 } // namespace OHOS
158