1 /*
2 * Copyright (c) 2025 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 "pre_monitor_manager.h"
17
18 #include "bytrace_adapter.h"
19 #include "multimodal_event_handler.h"
20 #include "multimodal_input_connect_manager.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "PreMonitorManager"
24
25 namespace OHOS {
26 namespace MMI {
PreMonitorManager()27 PreMonitorManager::PreMonitorManager() {}
~PreMonitorManager()28 PreMonitorManager::~PreMonitorManager() {}
29
AddHandler(std::shared_ptr<IInputEventConsumer> consumer,HandleEventType eventType,std::vector<int32_t> keys)30 int32_t PreMonitorManager::AddHandler(
31 std::shared_ptr<IInputEventConsumer> consumer, HandleEventType eventType, std::vector<int32_t> keys)
32 {
33 CALL_DEBUG_ENTER;
34 CHKPR(consumer, INVALID_HANDLER_ID);
35 if (!MMIEventHdl.InitClient()) {
36 MMI_HILOGE("Client init failed");
37 return RET_ERR;
38 }
39 std::lock_guard<std::mutex> guard(mtxHandlers_);
40 int32_t handlerId = GetNextId();
41 if (RET_OK == AddLocal(handlerId, eventType, keys, consumer)) {
42 MMI_HILOGD("New handler successfully registered, report to server");
43 int32_t ret = AddToServer(handlerId, eventType, keys);
44 if (ret != RET_OK) {
45 MMI_HILOGE("Add Handler to server failed");
46 RemoveLocal(handlerId);
47 return ret;
48 }
49 MMI_HILOGI("Finish add Handler");
50 } else {
51 MMI_HILOGE("Add Handler local failed");
52 handlerId = INVALID_HANDLER_ID;
53 }
54 return handlerId;
55 }
56
RemoveHandler(int32_t handlerId)57 int32_t PreMonitorManager::RemoveHandler(int32_t handlerId)
58 {
59 CALL_DEBUG_ENTER;
60 MMI_HILOGD("Unregister handler:%{public}d", handlerId);
61 if (!MMIEventHdl.InitClient()) {
62 MMI_HILOGE("Client init failed");
63 return RET_ERR;
64 }
65 std::lock_guard<std::mutex> guard(mtxHandlers_);
66 const HandleEventType currentType = GetEventType();
67 int32_t ret = RemoveLocal(handlerId);
68 if (ret == RET_OK) {
69 ret = RemoveFromServer(handlerId);
70 if (ret != RET_OK) {
71 return ret;
72 }
73 MMI_HILOGI("Remove Handler Succ");
74 }
75 return ret;
76 }
77
AddLocal(int32_t handlerId,HandleEventType eventType,std::vector<int32_t> keys,std::shared_ptr<IInputEventConsumer> consumer)78 int32_t PreMonitorManager::AddLocal(int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys,
79 std::shared_ptr<IInputEventConsumer> consumer)
80 {
81 PreMonitorManager::Handler handler{
82 .handlerId_ = handlerId,
83 .eventType_ = eventType,
84 .callback_ = consumer,
85 .keys_ = keys,
86 };
87 auto ret = monitorHandlers_.emplace(handler.handlerId_, handler);
88 if (!ret.second) {
89 MMI_HILOGE("Duplicate handler:%{public}d", handler.handlerId_);
90 return RET_ERR;
91 }
92 return RET_OK;
93 }
AddToServer(int32_t handlerId,HandleEventType eventType,std::vector<int32_t> keys)94 int32_t PreMonitorManager::AddToServer(int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys)
95 {
96 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->AddPreInputHandler(handlerId, eventType, keys);
97 if (ret != RET_OK) {
98 MMI_HILOGE("Send to server failed, ret:%{public}d", ret);
99 }
100 return ret;
101 }
RemoveLocal(int32_t handlerId)102 int32_t PreMonitorManager::RemoveLocal(int32_t handlerId)
103 {
104 auto iter = monitorHandlers_.find(handlerId);
105 if (iter == monitorHandlers_.end()) {
106 MMI_HILOGE("No handler with specified");
107 return RET_ERR;
108 }
109 monitorHandlers_.erase(iter);
110 return RET_OK;
111 }
112
RemoveFromServer(int32_t handlerId)113 int32_t PreMonitorManager::RemoveFromServer(int32_t handlerId)
114 {
115 int32_t ret = MULTIMODAL_INPUT_CONNECT_MGR->RemovePreInputHandler(handlerId);
116 if (ret != 0) {
117 MMI_HILOGE("RemoveFromServer failed, ret:%{public}d", ret);
118 }
119 return ret;
120 }
121
GetNextId()122 int32_t PreMonitorManager::GetNextId()
123 {
124 if (nextId_ == std::numeric_limits<int32_t>::max()) {
125 MMI_HILOGE("Exceeded limit of 32-bit maximum number of integers");
126 return INVALID_HANDLER_ID;
127 }
128 return nextId_++;
129 }
130
GetEventType() const131 HandleEventType PreMonitorManager::GetEventType() const
132 {
133 uint32_t eventType{ HANDLE_EVENT_TYPE_NONE };
134 if (monitorHandlers_.empty()) {
135 MMI_HILOGD("The monitorHandlers_ is empty");
136 return HANDLE_EVENT_TYPE_NONE;
137 }
138 for (const auto &inputHandler : monitorHandlers_) {
139 eventType |= inputHandler.second.eventType_;
140 }
141
142 return eventType;
143 }
144
145 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnPreKeyEvent(std::shared_ptr<KeyEvent> keyEvent,int32_t handlerId)146 void PreMonitorManager::OnPreKeyEvent(std::shared_ptr<KeyEvent> keyEvent, int32_t handlerId)
147 {
148 CHK_PID_AND_TID();
149 CHKPV(keyEvent);
150
151 if (handlerId < 0) {
152 MMI_HILOGE("Leave, the handler id is less than 0");
153 return;
154 }
155 std::lock_guard<std::mutex> guard(mtxHandlers_);
156 BytraceAdapter::StartBytrace(keyEvent, BytraceAdapter::TRACE_STOP, BytraceAdapter::KEY_DISPATCH_EVENT);
157 for (const auto &item : monitorHandlers_) {
158 if ((item.second.eventType_ & HANDLE_EVENT_TYPE_PRE_KEY) != HANDLE_EVENT_TYPE_PRE_KEY) {
159 continue;
160 }
161 if (item.first == handlerId) {
162 std::shared_ptr<IInputEventConsumer> consumer = item.second.callback_;
163 CHKPV(consumer);
164 consumer->OnInputEvent(keyEvent);
165 MMI_HILOGD("Key event id:%{public}d keycode:%{private}d", handlerId, keyEvent->GetKeyCode());
166 return;
167 }
168 }
169 }
170 #endif // OHOS_BUILD_ENABLE_KEYBOARD
171
172 #if defined(OHOS_BUILD_ENABLE_MONITOR)
OnConnected()173 void PreMonitorManager::OnConnected()
174 {
175 CALL_DEBUG_ENTER;
176 for (const auto &inputHandler : monitorHandlers_) {
177 int32_t handlerId = inputHandler.first;
178 std::vector<int32_t> keys = inputHandler.second.keys_;
179 AddToServer(handlerId, inputHandler.second.eventType_, keys);
180 }
181 }
182 #endif // OHOS_BUILD_ENABLE_MONITOR
183
FindHandler(int32_t handlerId)184 std::shared_ptr<IInputEventConsumer> PreMonitorManager::FindHandler(int32_t handlerId)
185 {
186 auto iter = monitorHandlers_.find(handlerId);
187 if (iter != monitorHandlers_.end()) {
188 return iter->second.callback_;
189 }
190 return nullptr;
191 }
192 } // namespace MMI
193 } // namespace OHOS
194