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
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "MultimodalEventHandler"};
32 } // namespace
33
OnConnected(const IfMMIClient & client)34 void OnConnected(const IfMMIClient& client)
35 {
36 CALL_DEBUG_ENTER;
37 InputMgrImpl.OnConnected();
38 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
39 KeyEventInputSubscribeMgr.OnConnected();
40 #endif // OHOS_BUILD_ENABLE_KEYBOARD
41 #ifdef OHOS_BUILD_ENABLE_MONITOR
42 IMonitorMgr->OnConnected();
43 #endif // OHOS_BUILD_ENABLE_MONITOR
44 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
45 InputInterMgr->OnConnected();
46 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
47 }
48
MultimodalEventHandler()49 MultimodalEventHandler::MultimodalEventHandler() {}
~MultimodalEventHandler()50 MultimodalEventHandler::~MultimodalEventHandler() {}
51
52 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
SubscribeKeyEvent(const KeyEventInputSubscribeManager::SubscribeKeyEventInfo & subscribeInfo)53 int32_t MultimodalEventHandler::SubscribeKeyEvent(
54 const KeyEventInputSubscribeManager::SubscribeKeyEventInfo &subscribeInfo)
55 {
56 CALL_DEBUG_ENTER;
57 return MultimodalInputConnMgr->SubscribeKeyEvent(subscribeInfo.GetSubscribeId(), subscribeInfo.GetKeyOption());
58 }
59
UnsubscribeKeyEvent(int32_t subscribeId)60 int32_t MultimodalEventHandler::UnsubscribeKeyEvent(int32_t subscribeId)
61 {
62 CALL_DEBUG_ENTER;
63 return MultimodalInputConnMgr->UnsubscribeKeyEvent(subscribeId);
64 }
65
InjectEvent(const std::shared_ptr<KeyEvent> keyEvent)66 int32_t MultimodalEventHandler::InjectEvent(const std::shared_ptr<KeyEvent> keyEvent)
67 {
68 CALL_DEBUG_ENTER;
69 CHKPR(keyEvent, ERROR_NULL_POINTER);
70 keyEvent->UpdateId();
71 if (keyEvent->GetKeyCode() < 0) {
72 MMI_HILOGE("KeyCode is invalid:%{public}u", keyEvent->GetKeyCode());
73 return RET_ERR;
74 }
75 int32_t ret = MultimodalInputConnMgr->InjectKeyEvent(keyEvent);
76 if (ret != 0) {
77 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
78 return RET_ERR;
79 }
80 return RET_OK;
81 }
82 #endif // OHOS_BUILD_ENABLE_KEYBOARD
83
InitClient(EventHandlerPtr eventHandler)84 bool MultimodalEventHandler::InitClient(EventHandlerPtr eventHandler)
85 {
86 CALL_DEBUG_ENTER;
87 if (client_ != nullptr) {
88 if (eventHandler != nullptr) {
89 client_->MarkIsEventHandlerChanged(eventHandler);
90 }
91 return true;
92 }
93 client_ = std::make_shared<MMIClient>();
94 client_->SetEventHandler(eventHandler);
95 client_->RegisterConnectedFunction(&OnConnected);
96 if (!(client_->Start())) {
97 client_.reset();
98 client_ = nullptr;
99 MMI_HILOGE("The client fails to start");
100 return false;
101 }
102 return true;
103 }
104
GetMMIClient()105 MMIClientPtr MultimodalEventHandler::GetMMIClient()
106 {
107 CHKPP(client_);
108 return client_->GetSharedPtr();
109 }
110
111 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)112 int32_t MultimodalEventHandler::InjectPointerEvent(std::shared_ptr<PointerEvent> pointerEvent)
113 {
114 CALL_DEBUG_ENTER;
115 CHKPR(pointerEvent, ERROR_NULL_POINTER);
116 EventLogHelper::PrintEventData(pointerEvent);
117 int32_t ret = MultimodalInputConnMgr->InjectPointerEvent(pointerEvent);
118 if (ret != 0) {
119 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
120 return RET_ERR;
121 }
122 return RET_OK;
123 }
124 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
125
126 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
MoveMouseEvent(int32_t offsetX,int32_t offsetY)127 int32_t MultimodalEventHandler::MoveMouseEvent(int32_t offsetX, int32_t offsetY)
128 {
129 CALL_DEBUG_ENTER;
130 int32_t ret = MultimodalInputConnMgr->MoveMouseEvent(offsetX, offsetY);
131 if (ret != 0) {
132 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
133 return RET_ERR;
134 }
135 return RET_OK;
136 }
137 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
138 } // namespace MMI
139 } // namespace OHOS
140