1 /*
2 * Copyright (c) 2021-2024 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 "bytrace_adapter.h"
17 #include "input_event_data_transformation.h"
18 #include "input_event_handler.h"
19 #include "util_ex.h"
20
21 #undef MMI_LOG_DOMAIN
22 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "EventMonitorHandler"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 #ifdef OHOS_BUILD_ENABLE_TOUCH
30 constexpr size_t MAX_EVENTIDS_SIZE { 1000 };
31 #endif // OHOS_BUILD_ENABLE_TOUCH
32 constexpr int32_t ACTIVE_EVENT { 2 };
33 constexpr int32_t REMOVE_OBSERVER { -2 };
34 constexpr int32_t UNOBSERVED { -1 };
35 constexpr int32_t POWER_UID { 5528 };
36 constexpr int32_t THREE_FINGERS { 3 };
37 constexpr int32_t FOUR_FINGERS { 4 };
38 } // namespace
39
40 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)41 void EventMonitorHandler::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 EventMonitorHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
52 {
53 CHKPV(pointerEvent);
54 if (OnHandleEvent(pointerEvent)) {
55 BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP);
56 MMI_HILOGD("Monitor is succeeded");
57 return;
58 }
59 CHKPV(nextHandler_);
60 nextHandler_->HandlePointerEvent(pointerEvent);
61 }
62 #endif // OHOS_BUILD_ENABLE_POINTER
63
64 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)65 void EventMonitorHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
66 {
67 CHKPV(pointerEvent);
68 if (OnHandleEvent(pointerEvent)) {
69 BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP);
70 MMI_HILOGD("Monitor is succeeded");
71 return;
72 }
73 int32_t pointerId = pointerEvent->GetPointerId();
74 PointerEvent::PointerItem item;
75 if (pointerEvent->GetPointerItem(pointerId, item)) {
76 if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) {
77 MMI_HILOGD("Knuckle event, skip");
78 return;
79 }
80 }
81 CHKPV(nextHandler_);
82 nextHandler_->HandleTouchEvent(pointerEvent);
83 }
84 #endif // OHOS_BUILD_ENABLE_TOUCH
85
CheckHasInputHandler(HandleEventType eventType)86 bool EventMonitorHandler::CheckHasInputHandler(HandleEventType eventType)
87 {
88 return monitors_.CheckHasInputHandler(eventType);
89 }
90
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType,std::shared_ptr<IInputEventConsumer> callback,TouchGestureType gestureType,int32_t fingers)91 int32_t EventMonitorHandler::AddInputHandler(InputHandlerType handlerType, HandleEventType eventType,
92 std::shared_ptr<IInputEventConsumer> callback, TouchGestureType gestureType, int32_t fingers)
93 {
94 CALL_INFO_TRACE;
95 CHKPR(callback, RET_ERR);
96 if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) {
97 MMI_HILOGE("Invalid event type");
98 return RET_ERR;
99 }
100 InitSessionLostCallback();
101 SessionHandler mon { handlerType, eventType, callback, gestureType, fingers };
102 return monitors_.AddMonitor(mon);
103 }
104
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session,TouchGestureType gestureType,int32_t fingers)105 int32_t EventMonitorHandler::AddInputHandler(InputHandlerType handlerType,
106 HandleEventType eventType, SessionPtr session, TouchGestureType gestureType, int32_t fingers)
107 {
108 CALL_INFO_TRACE;
109 CHKPR(session, RET_ERR);
110 if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) {
111 MMI_HILOGE("Invalid event type");
112 return RET_ERR;
113 }
114 InitSessionLostCallback();
115 SessionHandler mon { handlerType, eventType, session, gestureType, fingers };
116 return monitors_.AddMonitor(mon);
117 }
118
AddInputHandler(InputHandlerType handlerType,std::vector<int32_t> actionsType,SessionPtr session)119 int32_t EventMonitorHandler::AddInputHandler(InputHandlerType handlerType,
120 std::vector<int32_t> actionsType, SessionPtr session)
121 {
122 CALL_INFO_TRACE;
123 CHKPR(session, RET_ERR);
124 InitSessionLostCallback();
125 SessionHandler mon { handlerType, HANDLE_EVENT_TYPE_NONE, session, actionsType };
126 return monitors_.AddMonitor(mon);
127 }
128
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType,std::shared_ptr<IInputEventConsumer> callback,TouchGestureType gestureType,int32_t fingers)129 void EventMonitorHandler::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
130 std::shared_ptr<IInputEventConsumer> callback, TouchGestureType gestureType, int32_t fingers)
131 {
132 CALL_INFO_TRACE;
133 CHKPV(callback);
134 if (handlerType == InputHandlerType::MONITOR) {
135 SessionHandler monitor { handlerType, eventType, callback, gestureType, fingers };
136 monitors_.RemoveMonitor(monitor);
137 }
138 }
139
RemoveInputHandler(InputHandlerType handlerType,std::vector<int32_t> actionsType,SessionPtr session)140 void EventMonitorHandler::RemoveInputHandler(InputHandlerType handlerType, std::vector<int32_t> actionsType,
141 SessionPtr session)
142 {
143 CALL_INFO_TRACE;
144 if (handlerType == InputHandlerType::MONITOR) {
145 SessionHandler monitor { handlerType, HANDLE_EVENT_TYPE_NONE, session, actionsType };
146 monitors_.RemoveMonitor(monitor);
147 }
148 }
149
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session,TouchGestureType gestureType,int32_t fingers)150 void EventMonitorHandler::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
151 SessionPtr session, TouchGestureType gestureType, int32_t fingers)
152 {
153 CALL_INFO_TRACE;
154 if (handlerType == InputHandlerType::MONITOR) {
155 SessionHandler monitor { handlerType, eventType, session, gestureType, fingers };
156 monitors_.RemoveMonitor(monitor);
157 }
158 }
159
MarkConsumed(int32_t eventId,SessionPtr session)160 void EventMonitorHandler::MarkConsumed(int32_t eventId, SessionPtr session)
161 {
162 LogTracer lt(eventId, 0, 0);
163 monitors_.MarkConsumed(eventId, session);
164 }
165
166 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)167 bool EventMonitorHandler::OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)
168 {
169 MMI_HILOGD("Handle KeyEvent");
170 CHKPF(keyEvent);
171 auto keyHandler = InputHandler->GetEventNormalizeHandler();
172 CHKPF(keyHandler);
173 if (keyEvent->GetKeyCode() != keyHandler->GetCurrentHandleKeyCode()) {
174 MMI_HILOGW("Keycode has been changed");
175 }
176 if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) {
177 MMI_HILOGD("This event has been tagged as not to be monitored");
178 } else {
179 if (monitors_.HandleEvent(keyEvent)) {
180 MMI_HILOGD("Key event was consumed");
181 return true;
182 }
183 }
184 return false;
185 }
186 #endif // OHOS_BUILD_ENABLE_KEYBOARD
187
188 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent)189 bool EventMonitorHandler::OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
190 {
191 CHKPF(pointerEvent);
192 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) {
193 MMI_HILOGD("This event has been tagged as not to be monitored");
194 } else {
195 if (monitors_.HandleEvent(pointerEvent)) {
196 MMI_HILOGD("Pointer event was monitor");
197 return true;
198 }
199 }
200 MMI_HILOGD("Interception and monitor failed");
201 return false;
202 }
203 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
204
InitSessionLostCallback()205 void EventMonitorHandler::InitSessionLostCallback()
206 {
207 if (sessionLostCallbackInitialized_) {
208 return;
209 }
210 auto udsServerPtr = InputHandler->GetUDSServer();
211 CHKPV(udsServerPtr);
212 udsServerPtr->AddSessionDeletedCallback([this] (SessionPtr session) {
213 return this->OnSessionLost(session);
214 });
215 sessionLostCallbackInitialized_ = true;
216 MMI_HILOGD("The callback on session deleted is registered successfully");
217 }
218
OnSessionLost(SessionPtr session)219 void EventMonitorHandler::OnSessionLost(SessionPtr session)
220 {
221 monitors_.OnSessionLost(session);
222 }
223
Expect(std::shared_ptr<PointerEvent> pointerEvent) const224 bool EventMonitorHandler::SessionHandler::Expect(std::shared_ptr<PointerEvent> pointerEvent) const
225 {
226 if (GestureMonitorHandler::IsTouchGestureEvent(pointerEvent->GetPointerAction())) {
227 return (((eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE) &&
228 gesture_.IsMatchGesture(pointerEvent->GetPointerAction(), pointerEvent->GetPointerCount()));
229 } else {
230 return ((eventType_ & HANDLE_EVENT_TYPE_ALL) == eventType_);
231 }
232 }
233
SendToClient(std::shared_ptr<KeyEvent> keyEvent,NetPacket & pkt) const234 void EventMonitorHandler::SessionHandler::SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt) const
235 {
236 CHKPV(keyEvent);
237 CHKPV(session_);
238 if (InputEventDataTransformation::KeyEventToNetPacket(keyEvent, pkt) != RET_OK) {
239 MMI_HILOGE("Packet key event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
240 return;
241 }
242 if (!session_->SendMsg(pkt)) {
243 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
244 }
245 }
246
SendToClient(std::shared_ptr<PointerEvent> pointerEvent,NetPacket & pkt) const247 void EventMonitorHandler::SessionHandler::SendToClient(std::shared_ptr<PointerEvent> pointerEvent,
248 NetPacket &pkt) const
249 {
250 CHKPV(pointerEvent);
251 CHKPV(session_);
252 MMI_HILOGD("Service SendToClient InputHandlerType:%{public}d, TokenType:%{public}d, pid:%{public}d",
253 handlerType_, session_->GetTokenType(), session_->GetPid());
254 if (!session_->SendMsg(pkt)) {
255 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
256 }
257 }
258
AddMonitor(const SessionHandler & monitor)259 int32_t EventMonitorHandler::MonitorCollection::AddMonitor(const SessionHandler& monitor)
260 {
261 if (monitors_.size() >= MAX_N_INPUT_MONITORS) {
262 MMI_HILOGE("The number of monitors exceeds the maximum:%{public}zu, monitors errCode:%{public}d",
263 monitors_.size(), INVALID_MONITOR_MON);
264 return RET_ERR;
265 }
266 bool isFound = false;
267 SessionHandler handler = monitor;
268 auto iter = monitors_.find(monitor);
269 if (iter != monitors_.end()) {
270 isFound = true;
271 }
272 if (isFound && iter->actionsType_.empty()) {
273 return UpdateEventTypeMonitor(iter, monitor, handler, isFound);
274 } else if (isFound && !iter->actionsType_.empty()) {
275 return UpdateActionsTypeMonitor(iter, monitor, isFound);
276 }
277
278 if (!monitor.actionsType_.empty()) {
279 for (auto action : monitor.actionsType_) {
280 if (std::find(insertToMonitorsActions_.begin(), insertToMonitorsActions_.end(), action) ==
281 insertToMonitorsActions_.end()) {
282 insertToMonitorsActions_.push_back(action);
283 }
284 }
285 }
286 auto [sIter, isOk] = monitors_.insert(monitor);
287 if (!isOk) {
288 MMI_HILOGE("Failed to add monitor");
289 return RET_ERR;
290 }
291 MMI_HILOGD("Service Add Monitor Success");
292 return RET_OK;
293 }
294
UpdateEventTypeMonitor(const std::set<SessionHandler>::iterator & iter,const SessionHandler & monitor,SessionHandler & handler,bool isFound)295 int32_t EventMonitorHandler::MonitorCollection::UpdateEventTypeMonitor(const std::set<SessionHandler>::iterator &iter,
296 const SessionHandler &monitor, SessionHandler &handler, bool isFound)
297 {
298 if (iter->eventType_ == monitor.eventType_ &&
299 ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) != HANDLE_EVENT_TYPE_TOUCH_GESTURE)) {
300 MMI_HILOGD("Monitor with event type (%{public}u) already exists", monitor.eventType_);
301 return RET_OK;
302 }
303 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE) {
304 auto gestureHandler = iter->gesture_;
305 gestureHandler.AddGestureMonitor(monitor.gesture_.gestureType_, monitor.gesture_.fingers_);
306 handler(gestureHandler);
307 }
308
309 monitors_.erase(iter);
310 auto [sIter, isOk] = monitors_.insert(handler);
311 if (!isOk) {
312 if (isFound) {
313 MMI_HILOGE("Internal error: monitor has been removed");
314 } else {
315 MMI_HILOGE("Failed to add monitor");
316 }
317 return RET_ERR;
318 }
319 MMI_HILOGD("Event type is updated:%{public}u", monitor.eventType_);
320 return RET_OK;
321 }
322
UpdateActionsTypeMonitor(const std::set<SessionHandler>::iterator & iter,const SessionHandler & monitor,bool isFound)323 int32_t EventMonitorHandler::MonitorCollection::UpdateActionsTypeMonitor(const std::set<SessionHandler>::iterator &iter,
324 const SessionHandler &monitor, bool isFound)
325 {
326 if (!IsNeedInsertToMonitors(iter->actionsType_)) {
327 return RET_OK;
328 }
329 monitors_.erase(iter);
330 auto [sIter, isOk] = monitors_.insert(monitor);
331 if (!isOk && isFound) {
332 MMI_HILOGE("Internal error: monitor has been removed");
333 return RET_ERR;
334 } else if (!isOk && !isFound) {
335 MMI_HILOGE("Failed to add monitor");
336 return RET_ERR;
337 }
338 MMI_HILOGD("Actions type is updated");
339 return RET_OK;
340 }
341
IsNeedInsertToMonitors(std::vector<int32_t> actionsType)342 bool EventMonitorHandler::MonitorCollection::IsNeedInsertToMonitors(std::vector<int32_t> actionsType)
343 {
344 bool isNeedInsertToMonitors = false;
345 for (auto action : actionsType) {
346 if (std::find(insertToMonitorsActions_.begin(), insertToMonitorsActions_.end(), action) ==
347 insertToMonitorsActions_.end()) {
348 insertToMonitorsActions_.push_back(action);
349 isNeedInsertToMonitors = true;
350 }
351 }
352 return isNeedInsertToMonitors;
353 }
354
RemoveMonitor(const SessionHandler & monitor)355 void EventMonitorHandler::MonitorCollection::RemoveMonitor(const SessionHandler& monitor)
356 {
357 SessionHandler handler = monitor;
358 auto iter = monitors_.find(monitor);
359 if (iter == monitors_.cend()) {
360 MMI_HILOGE("Monitor does not exist");
361 return;
362 }
363
364 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE) {
365 auto gestureHandler = iter->gesture_;
366 gestureHandler.RemoveGestureMonitor(monitor.gesture_.gestureType_, monitor.gesture_.fingers_);
367 handler(gestureHandler);
368 }
369 monitors_.erase(iter);
370 if (monitor.session_) {
371 int32_t pid = monitor.session_->GetPid();
372 auto it = endScreenCaptureMonitors_.find(pid);
373 if (it != endScreenCaptureMonitors_.end()) {
374 auto setIter = endScreenCaptureMonitors_[pid].find(monitor);
375 if (setIter != endScreenCaptureMonitors_[pid].end()) {
376 endScreenCaptureMonitors_[pid].erase(setIter);
377 }
378 if (endScreenCaptureMonitors_[pid].empty()) {
379 endScreenCaptureMonitors_.erase(it);
380 }
381 }
382 }
383 if (monitor.eventType_ == HANDLE_EVENT_TYPE_NONE) {
384 MMI_HILOGD("Unregister monitor successfully");
385 return;
386 }
387
388 auto [sIter, isOk] = monitors_.insert(handler);
389 if (!isOk) {
390 MMI_HILOGE("Internal error, monitor has been removed");
391 return;
392 }
393 MMI_HILOGD("Event type is updated:%{public}u", monitor.eventType_);
394 }
395
MarkConsumed(int32_t eventId,SessionPtr session)396 void EventMonitorHandler::MonitorCollection::MarkConsumed(int32_t eventId, SessionPtr session)
397 {
398 if (!HasMonitor(session)) {
399 MMI_HILOGW("Specified monitor does not exist");
400 return;
401 }
402 auto tIter = states_.begin();
403 for (; tIter != states_.end(); ++tIter) {
404 const auto &eventIds = tIter->second.eventIds_;
405 if (eventIds.find(eventId) != eventIds.cend()) {
406 break;
407 }
408 }
409 if (tIter == states_.end()) {
410 MMI_HILOGE("No operation corresponding to this event");
411 return;
412 }
413 ConsumptionState &state = tIter->second;
414
415 if (state.isMonitorConsumed_) {
416 MMI_HILOGE("Corresponding operation has been marked as consumed");
417 return;
418 }
419 state.isMonitorConsumed_ = true;
420 CHKPV(state.lastPointerEvent_);
421 #ifdef OHOS_BUILD_ENABLE_TOUCH
422 MMI_HILOGD("Cancel operation");
423 auto pointerEvent = std::make_shared<PointerEvent>(*state.lastPointerEvent_);
424 WIN_MGR->CancelAllTouches(pointerEvent);
425 #endif // OHOS_BUILD_ENABLE_TOUCH
426 }
427
428 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleEvent(std::shared_ptr<KeyEvent> keyEvent)429 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<KeyEvent> keyEvent)
430 {
431 CHKPF(keyEvent);
432 MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
433 NetPacket pkt(MmiMessageId::REPORT_KEY_EVENT);
434 pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
435 if (pkt.ChkRWError()) {
436 MMI_HILOGE("Packet write key event failed");
437 return false;
438 }
439 for (const auto &mon : monitors_) {
440 if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) != HANDLE_EVENT_TYPE_KEY) {
441 continue;
442 }
443 if (!keyEvent->GetFourceMonitorFlag()) {
444 mon.SendToClient(keyEvent, pkt);
445 } else if (mon.session_ != nullptr && mon.session_->GetUid() == POWER_UID) {
446 mon.SendToClient(keyEvent, pkt);
447 }
448 }
449 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
450 NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
451 for (const auto &mon : monitors_) {
452 OHOS::MMI::NapProcess::NapStatusData napData;
453 auto sess = mon.session_;
454 if (!sess) {
455 continue;
456 }
457 napData.pid = sess->GetPid();
458 napData.uid = sess->GetUid();
459 napData.bundleName = sess->GetProgramName();
460 if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
461 int32_t syncState = ACTIVE_EVENT;
462 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
463 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
464 }
465 }
466 }
467 return false;
468 }
469 #endif // OHOS_BUILD_ENABLE_KEYBOARD
470
471 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)472 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
473 {
474 CHKPF(pointerEvent);
475 #ifdef OHOS_BUILD_ENABLE_TOUCH
476 UpdateConsumptionState(pointerEvent);
477 #endif // OHOS_BUILD_ENABLE_TOUCH
478 Monitor(pointerEvent);
479 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
480 pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
481 auto iter = states_.find(pointerEvent->GetDeviceId());
482 return (iter != states_.end() ? iter->second.isMonitorConsumed_ : false);
483 }
484 MMI_HILOGD("This is not a touch-screen event");
485 return false;
486 }
487 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
488
HasMonitor(SessionPtr session)489 bool EventMonitorHandler::MonitorCollection::HasMonitor(SessionPtr session)
490 {
491 SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session };
492 return (monitors_.find(monitor) != monitors_.end());
493 }
494
HasScreenCaptureMonitor(SessionPtr session)495 bool EventMonitorHandler::MonitorCollection::HasScreenCaptureMonitor(SessionPtr session)
496 {
497 int32_t pid = session->GetPid();
498 return (endScreenCaptureMonitors_.find(pid) != endScreenCaptureMonitors_.end());
499 }
500
RemoveScreenCaptureMonitor(SessionPtr session)501 void EventMonitorHandler::MonitorCollection::RemoveScreenCaptureMonitor(SessionPtr session)
502 {
503 if (session->GetTokenType() != TokenType::TOKEN_HAP) {
504 return;
505 }
506 int32_t pid = session->GetPid();
507 std::set<SessionHandler> monitorSet;
508 for (const auto &monitor : monitors_) {
509 if (monitor.session_ == session) {
510 SessionHandler screenCaptureMointor(monitor);
511 monitorSet.insert(screenCaptureMointor);
512 }
513 }
514 for (const auto &monitor : monitorSet) {
515 auto it = monitors_.find(monitor);
516 if (it != monitors_.end()) {
517 monitors_.erase(it);
518 }
519 }
520 endScreenCaptureMonitors_.emplace(pid, monitorSet);
521 }
522
RecoveryScreenCaptureMonitor(SessionPtr session)523 void EventMonitorHandler::MonitorCollection::RecoveryScreenCaptureMonitor(SessionPtr session)
524 {
525 if (session->GetTokenType() != TokenType::TOKEN_HAP) {
526 return;
527 }
528 int32_t pid = session->GetPid();
529 auto it = endScreenCaptureMonitors_.find(pid);
530 if (it != endScreenCaptureMonitors_.end()) {
531 for (auto &monitor : endScreenCaptureMonitors_[pid]) {
532 SessionHandler screenCaptureMointor(monitor);
533 monitors_.insert(screenCaptureMointor);
534 }
535 endScreenCaptureMonitors_.erase(it);
536 }
537 }
538
539 #ifdef OHOS_BUILD_ENABLE_TOUCH
UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)540 void EventMonitorHandler::MonitorCollection::UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)
541 {
542 CALL_DEBUG_ENTER;
543 CHKPV(pointerEvent);
544 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
545 pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD) {
546 return;
547 }
548 auto sIter = states_.find(pointerEvent->GetDeviceId());
549 if (sIter == states_.end()) {
550 auto [tIter, isOk] = states_.emplace(pointerEvent->GetDeviceId(), ConsumptionState());
551 if (!isOk) {
552 MMI_HILOGE("Failed to emplace consumption state");
553 return;
554 }
555 sIter = tIter;
556 }
557 ConsumptionState &state = sIter->second;
558 if (state.eventIds_.size() >= MAX_EVENTIDS_SIZE) {
559 auto iter = state.eventIds_.begin();
560 state.eventIds_.erase(iter);
561 }
562 auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
563 if (!isOk) {
564 MMI_HILOGW("Failed to stash event");
565 }
566 state.lastPointerEvent_ = pointerEvent;
567
568 if (pointerEvent->GetPointerIds().size() != 1) {
569 MMI_HILOGD("In intermediate process");
570 return;
571 }
572 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
573 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
574 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_BEGIN) {
575 MMI_HILOGD("First press down");
576 state.eventIds_.clear();
577 auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
578 if (!isOk) {
579 MMI_HILOGW("Event number is duplicated");
580 }
581 state.isMonitorConsumed_ = false;
582 } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
583 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END ||
584 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_END) {
585 MMI_HILOGD("Last lift up");
586 state.eventIds_.clear();
587 }
588 }
589 #endif // OHOS_BUILD_ENABLE_TOUCH
590
591 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
IsSendToClient(const SessionHandler & monitor,std::shared_ptr<PointerEvent> pointerEvent,NetPacket & pkt,std::unordered_set<int32_t> fingerFocusPidSet)592 void EventMonitorHandler::MonitorCollection::IsSendToClient(const SessionHandler &monitor,
593 std::shared_ptr<PointerEvent> pointerEvent, NetPacket &pkt, std::unordered_set<int32_t> fingerFocusPidSet)
594 {
595 if (monitor.Expect(pointerEvent)) {
596 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_BEGIN ||
597 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_END) {
598 MMI_HILOGI("Swipe event sended in monitor! action type:%{public}d finger count:%{public}d",
599 pointerEvent->GetPointerAction(),
600 pointerEvent->GetFingerCount());
601 }
602 if (monitor.session_ && CheckIfNeedSendToClient(monitor, pointerEvent, fingerFocusPidSet)) {
603 monitor.SendToClient(pointerEvent, pkt);
604 return;
605 }
606 if (monitor.callback_) {
607 monitor.callback_->OnInputEvent(monitor.handlerType_, pointerEvent);
608 }
609 }
610 if (monitor.actionsType_.empty()) {
611 return;
612 }
613 auto iter = std::find(monitor.actionsType_.begin(), monitor.actionsType_.end(),
614 pointerEvent->GetPointerAction());
615 if (iter != monitor.actionsType_.end() && monitor.session_) {
616 monitor.SendToClient(pointerEvent, pkt);
617 }
618 }
619
Monitor(std::shared_ptr<PointerEvent> pointerEvent)620 void EventMonitorHandler::MonitorCollection::Monitor(std::shared_ptr<PointerEvent> pointerEvent)
621 {
622 CHKPV(pointerEvent);
623 MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
624 NetPacket pkt(MmiMessageId::REPORT_POINTER_EVENT);
625 pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
626 if (pkt.ChkRWError()) {
627 MMI_HILOGE("Packet write pointer event failed");
628 return;
629 }
630 if (InputEventDataTransformation::Marshalling(pointerEvent, pkt) != RET_OK) {
631 MMI_HILOGE("Marshalling pointer event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
632 return;
633 }
634 int32_t pointerId = pointerEvent->GetPointerId();
635 PointerEvent::PointerItem pointerItem;
636 pointerEvent->GetPointerItem(pointerId, pointerItem);
637 int32_t displayX = pointerItem.GetDisplayX();
638 int32_t displayY = pointerItem.GetDisplayY();
639 std::unordered_set<int32_t> fingerFocusPidSet;
640 for (const auto &monitor : monitors_) {
641 CHKPC(monitor.session_);
642 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_FINGERPRINT) == HANDLE_EVENT_TYPE_FINGERPRINT &&
643 monitor.session_->GetPid() == WIN_MGR->GetPidByWindowId(WIN_MGR->GetFocusWindowId())) {
644 fingerFocusPidSet.insert(monitor.session_->GetPid());
645 }
646 }
647 for (const auto &monitor : monitors_) {
648 IsSendToClient(monitor, pointerEvent, pkt, fingerFocusPidSet);
649 PointerEvent::PointerItem pointerItem1;
650 pointerEvent->GetPointerItem(pointerId, pointerItem1);
651 int32_t displayX1 = pointerItem1.GetDisplayX();
652 int32_t displayY1 = pointerItem1.GetDisplayY();
653 if (displayX != displayX1 || displayY != displayY1) {
654 MMI_HILOGW("Display coord changed %{public}d, %{private}d, %{private}d, %{private}d, %{private}d",
655 pointerId, displayX, displayY, displayX1, displayY1);
656 }
657 }
658 if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
659 NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
660 for (const auto &mon : monitors_) {
661 OHOS::MMI::NapProcess::NapStatusData napData;
662 auto sess = mon.session_;
663 if (!sess) {
664 continue;
665 }
666 napData.pid = sess->GetPid();
667 napData.uid = sess->GetUid();
668 napData.bundleName = sess->GetProgramName();
669 if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
670 int32_t syncState = ACTIVE_EVENT;
671 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
672 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
673 }
674 }
675 }
676 }
677 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
678
IsPinch(std::shared_ptr<PointerEvent> pointerEvent)679 bool EventMonitorHandler::MonitorCollection::IsPinch(std::shared_ptr<PointerEvent> pointerEvent)
680 {
681 CHKPF(pointerEvent);
682 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE &&
683 pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD) {
684 return false;
685 }
686 if ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
687 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
688 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_END)) {
689 return false;
690 }
691 return true;
692 }
693
IsRotate(std::shared_ptr<PointerEvent> pointerEvent)694 bool EventMonitorHandler::MonitorCollection::IsRotate(std::shared_ptr<PointerEvent> pointerEvent)
695 {
696 CHKPF(pointerEvent);
697 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE ||
698 (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_BEGIN &&
699 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
700 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_END)) {
701 return false;
702 }
703 return true;
704 }
705
706
IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)707 bool EventMonitorHandler::MonitorCollection::IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)
708 {
709 CHKPF(pointerEvent);
710 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
711 pointerEvent->GetFingerCount() != THREE_FINGERS ||
712 (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_BEGIN &&
713 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
714 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_END)) {
715 return false;
716 }
717 return true;
718 }
719
IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)720 bool EventMonitorHandler::MonitorCollection::IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)
721 {
722 CHKPF(pointerEvent);
723 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
724 pointerEvent->GetFingerCount() != FOUR_FINGERS ||
725 (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_BEGIN &&
726 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
727 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_END)) {
728 return false;
729 }
730 return true;
731 }
732
IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent)733 bool EventMonitorHandler::MonitorCollection::IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent)
734 {
735 CHKPF(pointerEvent);
736 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
737 pointerEvent->GetFingerCount() != THREE_FINGERS ||
738 (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_TRIPTAP)) {
739 return false;
740 }
741 return true;
742 }
743
744 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent)745 bool EventMonitorHandler::MonitorCollection::IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent)
746 {
747 CHKPF(pointerEvent);
748 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_FINGERPRINT &&
749 (((PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN <= pointerEvent->GetPointerAction() &&
750 pointerEvent->GetPointerAction() <= PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK)) ||
751 ((PointerEvent::POINTER_ACTION_FINGERPRINT_HOLD <= pointerEvent->GetPointerAction() &&
752 pointerEvent->GetPointerAction() <= PointerEvent::POINTER_ACTION_FINGERPRINT_TOUCH)) ||
753 (PointerEvent::POINTER_ACTION_FINGERPRINT_CANCEL == pointerEvent->GetPointerAction()))
754 ) {
755 return true;
756 }
757 MMI_HILOGD("not fingerprint event");
758 return false;
759 }
760
CheckIfNeedSendFingerprintEvent(SessionHandler & monitor,std::shared_ptr<PointerEvent> pointerEvent,std::unordered_set<int32_t> fingerFocusPidSet)761 bool EventMonitorHandler::MonitorCollection::CheckIfNeedSendFingerprintEvent(
762 SessionHandler &monitor, std::shared_ptr<PointerEvent> pointerEvent, std::unordered_set<int32_t> fingerFocusPidSet)
763 {
764 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_FINGERPRINT) == HANDLE_EVENT_TYPE_FINGERPRINT) {
765 if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE) {
766 MMI_HILOGD("fingerprint event pointer action is:%{public}d", pointerEvent->GetPointerAction());
767 return true;
768 }
769 if (fingerFocusPidSet.empty()) {
770 MMI_HILOGD("fingerprint slide event send all monitor pid:%{public}d", monitor.session_->GetPid());
771 return true;
772 }
773 if (fingerFocusPidSet.count(monitor.session_->GetPid())) {
774 MMI_HILOGD("fingerprint slide event send focus monitor pid:%{public}d", monitor.session_->GetPid());
775 return true;
776 }
777 MMI_HILOGD("fingerprint slide event not send monitor pid:%{public}d, focus pid:%{public}d",
778 monitor.session_->GetPid(),
779 WIN_MGR->GetPidByWindowId(WIN_MGR->GetFocusWindowId()));
780 return false;
781 }
782 MMI_HILOGD("monitor eventType is not fingerprint pid:%{public}d", monitor.session_->GetPid());
783 return false;
784 }
785 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
786
787 #ifdef OHOS_BUILD_ENABLE_X_KEY
IsXKey(std::shared_ptr<PointerEvent> pointerEvent)788 bool EventMonitorHandler::MonitorCollection::IsXKey(std::shared_ptr<PointerEvent> pointerEvent)
789 {
790 CHKPF(pointerEvent);
791 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_X_KEY) {
792 return true;
793 }
794 MMI_HILOGD("not X-key event");
795 return false;
796 }
797 #endif // OHOS_BUILD_ENABLE_X_KEY
798
CheckIfNeedSendToClient(SessionHandler monitor,std::shared_ptr<PointerEvent> pointerEvent,std::unordered_set<int32_t> fingerFocusPidSet)799 bool EventMonitorHandler::MonitorCollection::CheckIfNeedSendToClient(
800 SessionHandler monitor, std::shared_ptr<PointerEvent> pointerEvent, std::unordered_set<int32_t> fingerFocusPidSet)
801 {
802 CHKPF(pointerEvent);
803 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
804 if (IsFingerprint(pointerEvent)) {
805 return CheckIfNeedSendFingerprintEvent(monitor, pointerEvent, fingerFocusPidSet);
806 }
807 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
808 #ifdef OHOS_BUILD_ENABLE_X_KEY
809 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_X_KEY) == HANDLE_EVENT_TYPE_X_KEY && IsXKey(pointerEvent)) {
810 return true;
811 }
812 #endif // OHOS_BUILD_ENABLE_X_KEY
813 if ((monitor.eventType_ & HANDLE_EVENT_TYPE_POINTER) == HANDLE_EVENT_TYPE_POINTER) {
814 return true;
815 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE) {
816 return true;
817 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_SWIPEINWARD) == HANDLE_EVENT_TYPE_SWIPEINWARD) {
818 return true;
819 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH) == HANDLE_EVENT_TYPE_TOUCH &&
820 pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
821 return true;
822 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_MOUSE) == HANDLE_EVENT_TYPE_MOUSE &&
823 pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
824 return true;
825 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_PINCH) == HANDLE_EVENT_TYPE_PINCH &&
826 IsPinch(pointerEvent)) {
827 return true;
828 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_THREEFINGERSSWIP) == HANDLE_EVENT_TYPE_THREEFINGERSSWIP &&
829 IsThreeFingersSwipe(pointerEvent)) {
830 return true;
831 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_FOURFINGERSSWIP) == HANDLE_EVENT_TYPE_FOURFINGERSSWIP &&
832 IsFourFingersSwipe(pointerEvent)) {
833 return true;
834 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_ROTATE) == HANDLE_EVENT_TYPE_ROTATE &&
835 IsRotate(pointerEvent)) {
836 return true;
837 } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_THREEFINGERSTAP) == HANDLE_EVENT_TYPE_THREEFINGERSTAP &&
838 IsThreeFingersTap(pointerEvent)) {
839 return true;
840 }
841 return false;
842 }
843
OnSessionLost(SessionPtr session)844 void EventMonitorHandler::MonitorCollection::OnSessionLost(SessionPtr session)
845 {
846 CALL_INFO_TRACE;
847 std::set<SessionHandler>::const_iterator cItr = monitors_.cbegin();
848 while (cItr != monitors_.cend()) {
849 if (cItr->session_ != session) {
850 ++cItr;
851 } else {
852 cItr = monitors_.erase(cItr);
853 }
854 }
855 CHKPV(session);
856 int32_t pid = session->GetPid();
857 auto it = endScreenCaptureMonitors_.find(pid);
858 if (it != endScreenCaptureMonitors_.end()) {
859 endScreenCaptureMonitors_.erase(it);
860 }
861 }
862
CheckHasInputHandler(HandleEventType eventType)863 bool EventMonitorHandler::MonitorCollection::CheckHasInputHandler(HandleEventType eventType)
864 {
865 for (const auto &item : monitors_) {
866 if ((item.eventType_ & eventType) == eventType) {
867 return true;
868 }
869 }
870 return false;
871 }
872
Dump(int32_t fd,const std::vector<std::string> & args)873 void EventMonitorHandler::Dump(int32_t fd, const std::vector<std::string> &args)
874 {
875 return monitors_.Dump(fd, args);
876 }
877
Dump(int32_t fd,const std::vector<std::string> & args)878 void EventMonitorHandler::MonitorCollection::Dump(int32_t fd, const std::vector<std::string> &args)
879 {
880 CALL_DEBUG_ENTER;
881 mprintf(fd, "Monitor information:\t");
882 mprintf(fd, "monitors: count=%zu", monitors_.size());
883 for (const auto &item : monitors_) {
884 SessionPtr session = item.session_;
885 if (!session) {
886 continue;
887 }
888 mprintf(fd,
889 "handlerType:%d | Pid:%d | Uid:%d | Fd:%d "
890 "| EarliestEventTime:%" PRId64 " | Descript:%s "
891 "| EventType:%u | ProgramName:%s \t",
892 item.handlerType_, session->GetPid(),
893 session->GetUid(), session->GetFd(),
894 session->GetEarliestEventTime(), session->GetDescript().c_str(),
895 item.eventType_, session->GetProgramName().c_str());
896 }
897 }
898
899 #ifdef PLAYER_FRAMEWORK_EXISTS
ProcessScreenCapture(int32_t pid,bool isStart)900 void EventMonitorHandler::ProcessScreenCapture(int32_t pid, bool isStart)
901 {
902 auto udsServerPtr = InputHandler->GetUDSServer();
903 CHKPV(udsServerPtr);
904 SessionPtr session = udsServerPtr->GetSessionByPid(pid);
905 CHKPV(session);
906 if (isStart) {
907 if (!monitors_.HasMonitor(session) && !monitors_.HasScreenCaptureMonitor(session)) {
908 MMI_HILOGI("This process has no screen capture monitor");
909 return;
910 }
911 monitors_.RecoveryScreenCaptureMonitor(session);
912 } else {
913 if (!monitors_.HasMonitor(session)) {
914 MMI_HILOGI("This process has no screen capture monitor");
915 return;
916 }
917 monitors_.RemoveScreenCaptureMonitor(session);
918 }
919 }
920 #endif // PLAYER_FRAMEWORK_EXISTS
921 } // namespace MMI
922 } // namespace OHOS
923