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