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 "event_pre_monitor_handler.h"
17
18 #include "common_event_support.h"
19
20 #include "anr_manager.h"
21 #include "app_debug_listener.h"
22 #include "bytrace_adapter.h"
23 #include "define_multimodal.h"
24 #include "display_event_monitor.h"
25 #include "input_event_data_transformation.h"
26 #include "input_event_handler.h"
27 #include "mmi_log.h"
28 #include "napi_constants.h"
29 #include "net_packet.h"
30 #include "proto.h"
31 #include "util_ex.h"
32
33 #undef MMI_LOG_DOMAIN
34 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
35 #undef MMI_LOG_TAG
36 #define MMI_LOG_TAG "EventPreMonitorHandler"
37
38 namespace OHOS {
39 namespace MMI {
40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)41 void EventPreMonitorHandler::HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
42 {
43 CHKPV(keyEvent);
44 OnHandleEvent(keyEvent);
45 CHKPV(nextHandler_);
46 nextHandler_->HandleKeyEvent(keyEvent);
47 }
48 #endif // OHOS_BUILD_ENABLE_KEYBOARD
49
50 #ifdef OHOS_BUILD_ENABLE_POINTER
HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)51 void EventPreMonitorHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
52 {
53 CHKPV(pointerEvent);
54 CHKPV(nextHandler_);
55 nextHandler_->HandlePointerEvent(pointerEvent);
56 }
57 #endif // OHOS_BUILD_ENABLE_POINTER
58
59 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)60 void EventPreMonitorHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
61 {
62 CHKPV(pointerEvent);
63 CHKPV(nextHandler_);
64 nextHandler_->HandleTouchEvent(pointerEvent);
65 }
66 #endif // OHOS_BUILD_ENABLE_TOUCH
67
AddInputHandler(SessionPtr session,int32_t handlerId,HandleEventType eventType,std::vector<int32_t> keys)68 int32_t EventPreMonitorHandler::AddInputHandler(
69 SessionPtr session, int32_t handlerId, HandleEventType eventType, std::vector<int32_t> keys)
70 {
71 CALL_INFO_TRACE;
72 CHKPR(session, RET_ERR);
73 if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) {
74 MMI_HILOGE("Invalid event type");
75 return RET_ERR;
76 }
77 InitSessionLostCallback();
78 auto mon = std::make_shared<SessionHandler>(session, handlerId, eventType, keys);
79 return monitors_.AddMonitor(mon, keys);
80 }
81
RemoveInputHandler(SessionPtr sess,int32_t handlerId)82 void EventPreMonitorHandler::RemoveInputHandler(SessionPtr sess, int32_t handlerId)
83 {
84 CALL_INFO_TRACE;
85 monitors_.RemoveMonitor(sess, handlerId);
86 }
87
88 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)89 bool EventPreMonitorHandler::OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)
90 {
91 MMI_HILOGD("Handle KeyEvent");
92 CHKPF(keyEvent);
93 auto keyHandler = InputHandler->GetEventNormalizeHandler();
94 CHKPF(keyHandler);
95 if (keyEvent->GetKeyCode() != keyHandler->GetCurrentHandleKeyCode()) {
96 MMI_HILOGW("Keycode has been changed");
97 }
98 if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) {
99 MMI_HILOGD("This event has been tagged as not to be monitored");
100 } else {
101 if (monitors_.HandleEvent(keyEvent)) {
102 MMI_HILOGD("Key event was consumed");
103 return true;
104 }
105 }
106 return false;
107 }
108 #endif // OHOS_BUILD_ENABLE_KEYBOARD
109
InitSessionLostCallback()110 void EventPreMonitorHandler::InitSessionLostCallback()
111 {
112 if (sessionLostCallbackInitialized_) {
113 return;
114 }
115 auto udsServerPtr = InputHandler->GetUDSServer();
116 CHKPV(udsServerPtr);
117 udsServerPtr->AddSessionDeletedCallback([this] (SessionPtr session) {
118 return this->OnSessionLost(session);
119 });
120 sessionLostCallbackInitialized_ = true;
121 MMI_HILOGD("The callback on session deleted is registered successfully");
122 }
123
OnSessionLost(SessionPtr session)124 void EventPreMonitorHandler::OnSessionLost(SessionPtr session)
125 {
126 monitors_.OnSessionLost(session);
127 }
128
SendToClient(std::shared_ptr<KeyEvent> keyEvent,NetPacket & pkt,int32_t handlerId) const129 void EventPreMonitorHandler::SessionHandler::SendToClient(
130 std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt, int32_t handlerId) const
131 {
132 CHKPV(keyEvent);
133 CHKPV(session_);
134 if (InputEventDataTransformation::KeyEventToNetPacket(keyEvent, pkt) != RET_OK) {
135 MMI_HILOGE("Packet key event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
136 return;
137 }
138 int32_t fd = session_->GetFd();
139 pkt << fd << handlerId;
140 if (!session_->SendMsg(pkt)) {
141 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
142 }
143 }
144
AddMonitor(const std::shared_ptr<SessionHandler> monitor,std::vector<int32_t> keys)145 int32_t EventPreMonitorHandler::MonitorCollection::AddMonitor(
146 const std::shared_ptr<SessionHandler> monitor, std::vector<int32_t> keys)
147 {
148 if (sessionHandlers_.size() >= MAX_N_INPUT_MONITORS) {
149 MMI_HILOGE("The number of monitors exceeds the maximum:%{public}zu, monitors errCode:%{public}d",
150 sessionHandlers_.size(),
151 INVALID_MONITOR_MON);
152 return RET_ERR;
153 }
154 for (auto &iter : sessionHandlers_) {
155 if (IsEqualsKeys(keys, iter.first)) {
156 iter.second.push_back(monitor);
157 return RET_OK;
158 }
159 }
160 sessionHandlers_[keys] = std::list<std::shared_ptr<SessionHandler>>();
161 sessionHandlers_[keys].push_back(monitor);
162
163 return RET_OK;
164 }
165
IsEqualsKeys(std::vector<int32_t> newKeys,std::vector<int32_t> oldKeys)166 bool EventPreMonitorHandler::MonitorCollection::IsEqualsKeys(std::vector<int32_t> newKeys, std::vector<int32_t> oldKeys)
167 {
168 if (newKeys.size() != oldKeys.size()) {
169 MMI_HILOGE("The size of preKeys is not match");
170 return false;
171 }
172
173 for (const auto &newKey : newKeys) {
174 auto it = std::find(oldKeys.begin(), oldKeys.end(), newKey);
175 if (it == oldKeys.end()) {
176 MMI_HILOGE("Can't find the key");
177 return false;
178 }
179 }
180
181 return true;
182 }
183
RemoveMonitor(SessionPtr sess,int32_t handlerId)184 void EventPreMonitorHandler::MonitorCollection::RemoveMonitor(SessionPtr sess, int32_t handlerId)
185 {
186 for (auto iter = sessionHandlers_.begin(); iter != sessionHandlers_.end();) {
187 auto &sessionHandlers = iter->second;
188 for (auto it = sessionHandlers.begin(); it != sessionHandlers.end();) {
189 if ((*it)->handlerId_ == handlerId && (*it)->session_ == sess) {
190 it = sessionHandlers.erase(it);
191 } else {
192 ++it;
193 }
194 }
195 if (sessionHandlers.empty()) {
196 iter = sessionHandlers_.erase(iter);
197 } else {
198 ++iter;
199 }
200 }
201 }
202
203 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleEvent(std::shared_ptr<KeyEvent> keyEvent)204 bool EventPreMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<KeyEvent> keyEvent)
205 {
206 CHKPF(keyEvent);
207 MMI_HILOGD("Handle KeyEvent");
208 NetPacket pkt(MmiMessageId::ON_PRE_KEY_EVENT);
209 if (pkt.ChkRWError()) {
210 MMI_HILOGE("Packet write key event failed");
211 return false;
212 }
213 for (auto iter = sessionHandlers_.begin(); iter != sessionHandlers_.end(); iter++) {
214 auto &sessionHandlers = iter->second;
215 for (auto it = sessionHandlers.begin(); it != sessionHandlers.end(); it++) {
216 auto keys = (*it)->keys_;
217 auto keyIter = std::find(keys.begin(), keys.end(), keyEvent->GetKeyCode());
218 if (keyIter != keys.end()) {
219 (*it)->SendToClient(keyEvent, pkt, (*it)->handlerId_);
220 }
221 }
222 }
223 return false;
224 }
225 #endif // OHOS_BUILD_ENABLE_KEYBOARD
226
227 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)228 bool EventPreMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
229 {
230 return false;
231 }
232 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
233
OnSessionLost(SessionPtr session)234 void EventPreMonitorHandler::MonitorCollection::OnSessionLost(SessionPtr session)
235 {
236 CALL_INFO_TRACE;
237 for (auto iter = sessionHandlers_.begin(); iter != sessionHandlers_.end();) {
238 auto &handlers = iter->second;
239 for (auto inner = handlers.begin(); inner != handlers.end();) {
240 auto handler = *inner;
241 if (handler->session_ == session) {
242 inner = handlers.erase(inner);
243 } else {
244 ++inner;
245 }
246 }
247 if (handlers.empty()) {
248 iter = sessionHandlers_.erase(iter);
249 } else {
250 ++iter;
251 }
252 }
253 }
254
Dump(int32_t fd,const std::vector<std::string> & args)255 void EventPreMonitorHandler::Dump(int32_t fd, const std::vector<std::string> &args)
256 {
257 return monitors_.Dump(fd, args);
258 }
259
Dump(int32_t fd,const std::vector<std::string> & args)260 void EventPreMonitorHandler::MonitorCollection::Dump(int32_t fd, const std::vector<std::string> &args)
261 {
262 CALL_DEBUG_ENTER;
263 mprintf(fd, "Monitor information:\t");
264 mprintf(fd, "monitors: count=%zu", sessionHandlers_.size());
265 for (const auto &item : sessionHandlers_) {
266 const std::list<std::shared_ptr<SessionHandler>> &handlers = item.second;
267 for (const auto &handler : handlers) {
268 SessionPtr session = handler->session_;
269 if (!session) {
270 continue;
271 }
272 mprintf(fd,
273 "EventType:%d | Pid:%d | Uid:%d | Fd:%d "
274 "| EarliestEventTime:%" PRId64 " | Descript:%s "
275 "| ProgramName:%s \t",
276 handler->eventType_, session->GetPid(),
277 session->GetUid(), session->GetFd(),
278 session->GetEarliestEventTime(), session->GetDescript().c_str(),
279 session->GetProgramName().c_str());
280 }
281 }
282 }
283 } // namespace MMI
284 } // namespace OHOS
285