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 #include "input_handler_manager.h"
16 #include "bytrace.h"
17 #include "input_handler_type.h"
18 #include "mmi_log.h"
19 #include "multimodal_event_handler.h"
20 #include "net_packet.h"
21 #include "proto.h"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputHandlerManager" };
27 }
28
AddHandler(InputHandlerType handlerType,std::shared_ptr<IInputEventConsumer> consumer)29 int32_t InputHandlerManager::AddHandler(InputHandlerType handlerType,
30 std::shared_ptr<IInputEventConsumer> consumer)
31 {
32 CHKPR(consumer, INVALID_HANDLER_ID);
33 if (inputHandlers_.size() >= MAX_N_INPUT_HANDLERS) {
34 MMI_LOGE("The number of handlers exceeds the maximum");
35 return INVALID_HANDLER_ID;
36 }
37 int32_t handlerId = GetNextId();
38 if (handlerId == INVALID_HANDLER_ID) {
39 MMI_LOGE("Exceeded limit of 32-bit maximum number of integers");
40 return INVALID_HANDLER_ID;
41 }
42 MMI_LOGD("Register new handler:%{public}d", handlerId);
43 if (RET_OK == AddLocal(handlerId, handlerType, consumer)) {
44 MMI_LOGD("New handler successfully registered, report to server");
45 AddToServer(handlerId, handlerType);
46 } else {
47 handlerId = INVALID_HANDLER_ID;
48 }
49 return handlerId;
50 }
51
RemoveHandler(int32_t handlerId,InputHandlerType handlerType)52 void InputHandlerManager::RemoveHandler(int32_t handlerId, InputHandlerType handlerType)
53 {
54 MMI_LOGD("Unregister handler:%{public}d,type:%{public}d", handlerId, handlerType);
55 if (RET_OK == RemoveLocal(handlerId, handlerType)) {
56 MMI_LOGD("Handler:%{public}d unregistered, report to server", handlerId);
57 RemoveFromServer(handlerId, handlerType);
58 }
59 }
60
MarkConsumed(int32_t monitorId,int32_t eventId)61 void InputHandlerManager::MarkConsumed(int32_t monitorId, int32_t eventId)
62 {
63 MMI_LOGD("Mark consumed state, monitor:%{public}d,event:%{public}d", monitorId, eventId);
64 MMIClientPtr client = MMIEventHdl.GetMMIClient();
65 CHKPV(client);
66 NetPacket pkt(MmiMessageId::MARK_CONSUMED);
67 CHK(pkt.Write(monitorId), STREAM_BUF_WRITE_FAIL);
68 CHK(pkt.Write(eventId), STREAM_BUF_WRITE_FAIL);
69 CHK(client->SendMessage(pkt), MSG_SEND_FAIL);
70 }
71
AddLocal(int32_t handlerId,InputHandlerType handlerType,std::shared_ptr<IInputEventConsumer> monitor)72 int32_t InputHandlerManager::AddLocal(int32_t handlerId, InputHandlerType handlerType,
73 std::shared_ptr<IInputEventConsumer> monitor)
74 {
75 std::lock_guard<std::mutex> guard(lockHandlers_);
76 InputHandlerManager::Handler handler {
77 .handlerId_ = handlerId,
78 .handlerType_ = handlerType,
79 .consumer_ = monitor
80 };
81 auto ret = inputHandlers_.emplace(handler.handlerId_, handler);
82 if (!ret.second) {
83 MMI_LOGE("Duplicate handler:%{public}d", handler.handlerId_);
84 return RET_ERR;
85 }
86 return RET_OK;
87 }
88
AddToServer(int32_t handlerId,InputHandlerType handlerType)89 void InputHandlerManager::AddToServer(int32_t handlerId, InputHandlerType handlerType)
90 {
91 MMIClientPtr client { MMIEventHdl.GetMMIClient() };
92 CHKPV(client);
93 NetPacket pkt(MmiMessageId::ADD_INPUT_HANDLER);
94 CHK(pkt.Write(handlerId), STREAM_BUF_WRITE_FAIL);
95 CHK(pkt.Write(handlerType), STREAM_BUF_WRITE_FAIL);
96 CHK(client->SendMessage(pkt), MSG_SEND_FAIL);
97 }
98
RemoveLocal(int32_t handlerId,InputHandlerType handlerType)99 int32_t InputHandlerManager::RemoveLocal(int32_t handlerId, InputHandlerType handlerType)
100 {
101 std::lock_guard<std::mutex> guard(lockHandlers_);
102 auto tItr = inputHandlers_.find(handlerId);
103 if (tItr == inputHandlers_.end()) {
104 MMI_LOGE("No handler with specified");
105 return RET_ERR;
106 }
107 if (handlerType != tItr->second.handlerType_) {
108 MMI_LOGE("Unmatched handler type, InputHandlerType:%{public}d,FindHandlerType:%{public}d",
109 handlerType, tItr->second.handlerType_);
110 return RET_ERR;
111 }
112 inputHandlers_.erase(tItr);
113 return RET_OK;
114 }
115
RemoveFromServer(int32_t handlerId,InputHandlerType handlerType)116 void InputHandlerManager::RemoveFromServer(int32_t handlerId, InputHandlerType handlerType)
117 {
118 MMI_LOGD("Remove handler:%{public}d from server", handlerId);
119 MMIClientPtr client { MMIEventHdl.GetMMIClient() };
120 CHKPV(client);
121 NetPacket pkt(MmiMessageId::REMOVE_INPUT_HANDLER);
122 CHK(pkt.Write(handlerId), STREAM_BUF_WRITE_FAIL);
123 CHK(pkt.Write(handlerType), STREAM_BUF_WRITE_FAIL);
124 CHK(client->SendMessage(pkt), MSG_SEND_FAIL);
125 }
126
GetNextId()127 int32_t InputHandlerManager::GetNextId()
128 {
129 if (nextId_ == std::numeric_limits<int32_t>::max()) {
130 MMI_LOGE("Exceeded limit of 32-bit maximum number of integers");
131 return INVALID_HANDLER_ID;
132 }
133 return nextId_++;
134 }
135
FindHandler(int32_t handlerId)136 std::shared_ptr<IInputEventConsumer> InputHandlerManager::FindHandler(int32_t handlerId)
137 {
138 std::lock_guard<std::mutex> guard(lockHandlers_);
139 auto tItr = inputHandlers_.find(handlerId);
140 if (tItr != inputHandlers_.end()) {
141 return tItr->second.consumer_;
142 }
143 return nullptr;
144 }
145
OnInputEvent(int32_t handlerId,std::shared_ptr<KeyEvent> keyEvent)146 void InputHandlerManager::OnInputEvent(int32_t handlerId, std::shared_ptr<KeyEvent> keyEvent)
147 {
148 CHKPV(keyEvent);
149 std::shared_ptr<IInputEventConsumer> consumer = FindHandler(handlerId);
150 if (consumer != nullptr) {
151 consumer->OnInputEvent(keyEvent);
152 }
153 }
154
OnInputEvent(int32_t handlerId,std::shared_ptr<PointerEvent> pointerEvent)155 void InputHandlerManager::OnInputEvent(int32_t handlerId, std::shared_ptr<PointerEvent> pointerEvent)
156 {
157 MMI_LOGD("Enter handler:%{public}d", handlerId);
158 CHKPV(pointerEvent);
159 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
160 int32_t pointerId = pointerEvent->GetId();
161 std::string pointerEventString = "pointerEventFilter";
162 FinishAsyncTrace(BYTRACE_TAG_MULTIMODALINPUT, pointerEventString, pointerId);
163 }
164 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
165 int32_t touchId = pointerEvent->GetId();
166 std::string touchEventString = "touchEventFilter";
167 FinishAsyncTrace(BYTRACE_TAG_MULTIMODALINPUT, touchEventString, touchId);
168 }
169 std::shared_ptr<IInputEventConsumer> consumer = FindHandler(handlerId);
170 if (consumer != nullptr) {
171 consumer->OnInputEvent(pointerEvent);
172 }
173 MMI_LOGD("Leave");
174 }
175
OnConnected()176 void InputHandlerManager::OnConnected()
177 {
178 MMI_LOGD("Enter");
179 for (auto &inputHandler : inputHandlers_) {
180 AddToServer(inputHandler.second.handlerId_, inputHandler.second.handlerType_);
181 }
182 MMI_LOGD("Leave");
183 }
184 } // namespace MMI
185 } // namespace OHOS
186
187