• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_monitor_handler.h"
17 
18 #include "anr_manager.h"
19 #include "app_debug_listener.h"
20 #include "bytrace_adapter.h"
21 #include "define_multimodal.h"
22 #include "input_event_data_transformation.h"
23 #include "input_event_handler.h"
24 #include "mmi_log.h"
25 #include "napi_constants.h"
26 #include "net_packet.h"
27 #include "proto.h"
28 #include "util_ex.h"
29 #ifdef PLAYER_FRAMEWORK_EXISTS
30 #include "screen_capture_monitor.h"
31 #endif
32 
33 #undef MMI_LOG_DOMAIN
34 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
35 #undef MMI_LOG_TAG
36 #define MMI_LOG_TAG "EventMonitorHandler"
37 
38 namespace OHOS {
39 namespace MMI {
40 namespace {
41 #ifdef OHOS_BUILD_ENABLE_TOUCH
42 constexpr size_t MAX_EVENTIDS_SIZE { 1000 };
43 #endif // OHOS_BUILD_ENABLE_TOUCH
44 constexpr int32_t ACTIVE_EVENT { 2 };
45 constexpr int32_t REMOVE_OBSERVER { -2 };
46 constexpr int32_t UNOBSERVED { -1 };
47 } // namespace
48 
49 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)50 void EventMonitorHandler::HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
51 {
52     CHKPV(keyEvent);
53     OnHandleEvent(keyEvent);
54     CHKPV(nextHandler_);
55     nextHandler_->HandleKeyEvent(keyEvent);
56 }
57 #endif // OHOS_BUILD_ENABLE_KEYBOARD
58 
59 #ifdef OHOS_BUILD_ENABLE_POINTER
HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)60 void EventMonitorHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
61 {
62     CHKPV(pointerEvent);
63     if (OnHandleEvent(pointerEvent)) {
64         BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP);
65         MMI_HILOGD("Monitor is succeeded");
66         return;
67     }
68     CHKPV(nextHandler_);
69     nextHandler_->HandlePointerEvent(pointerEvent);
70 }
71 #endif // OHOS_BUILD_ENABLE_POINTER
72 
73 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)74 void EventMonitorHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
75 {
76     CHKPV(pointerEvent);
77     if (OnHandleEvent(pointerEvent)) {
78         BytraceAdapter::StartBytrace(pointerEvent, BytraceAdapter::TRACE_STOP);
79         MMI_HILOGD("Monitor is succeeded");
80         return;
81     }
82     int32_t pointerId = pointerEvent->GetPointerId();
83     PointerEvent::PointerItem item;
84     if (pointerEvent->GetPointerItem(pointerId, item)) {
85         if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) {
86             MMI_HILOGD("Knuckle event, skip");
87             return;
88         }
89     }
90     CHKPV(nextHandler_);
91     nextHandler_->HandleTouchEvent(pointerEvent);
92 }
93 #endif // OHOS_BUILD_ENABLE_TOUCH
94 
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType,std::shared_ptr<IInputEventConsumer> callback)95 int32_t EventMonitorHandler::AddInputHandler(InputHandlerType handlerType,
96     HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback)
97 {
98     CALL_INFO_TRACE;
99     CHKPR(callback, RET_ERR);
100     if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) {
101         MMI_HILOGE("Invalid event type");
102         return RET_ERR;
103     }
104     InitSessionLostCallback();
105     SessionHandler mon {handlerType, eventType, nullptr, callback};
106     return monitors_.AddMonitor(mon);
107 }
108 
AddInputHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session)109 int32_t EventMonitorHandler::AddInputHandler(InputHandlerType handlerType,
110     HandleEventType eventType, SessionPtr session)
111 {
112     CALL_INFO_TRACE;
113     CHKPR(session, RET_ERR);
114     if ((eventType & HANDLE_EVENT_TYPE_ALL) == HANDLE_EVENT_TYPE_NONE) {
115         MMI_HILOGE("Invalid event type");
116         return RET_ERR;
117     }
118     InitSessionLostCallback();
119     SessionHandler mon { handlerType, eventType, session };
120     return monitors_.AddMonitor(mon);
121 }
122 
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType,std::shared_ptr<IInputEventConsumer> callback)123 void EventMonitorHandler::RemoveInputHandler(InputHandlerType handlerType,
124     HandleEventType eventType, std::shared_ptr<IInputEventConsumer> callback)
125 {
126     CALL_INFO_TRACE;
127     CHKPV(callback);
128     if (handlerType == InputHandlerType::MONITOR) {
129         SessionHandler monitor {handlerType, eventType, nullptr, callback};
130         monitors_.RemoveMonitor(monitor);
131     }
132 }
133 
RemoveInputHandler(InputHandlerType handlerType,HandleEventType eventType,SessionPtr session)134 void EventMonitorHandler::RemoveInputHandler(InputHandlerType handlerType, HandleEventType eventType,
135     SessionPtr session)
136 {
137     CALL_INFO_TRACE;
138     if (handlerType == InputHandlerType::MONITOR) {
139         SessionHandler monitor { handlerType, eventType, session };
140         monitors_.RemoveMonitor(monitor);
141     }
142 }
143 
MarkConsumed(int32_t eventId,SessionPtr session)144 void EventMonitorHandler::MarkConsumed(int32_t eventId, SessionPtr session)
145 {
146     LogTracer lt(eventId, 0, 0);
147     monitors_.MarkConsumed(eventId, session);
148 }
149 
150 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)151 bool EventMonitorHandler::OnHandleEvent(std::shared_ptr<KeyEvent> keyEvent)
152 {
153     MMI_HILOGD("Handle KeyEvent");
154     CHKPF(keyEvent);
155     auto keyHandler = InputHandler->GetEventNormalizeHandler();
156     CHKPF(keyHandler);
157     if (keyEvent->GetKeyCode() != keyHandler->GetCurrentHandleKeyCode()) {
158         MMI_HILOGW("Keycode has been changed");
159     }
160     if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_NO_MONITOR)) {
161         MMI_HILOGD("This event has been tagged as not to be monitored");
162     } else {
163         if (monitors_.HandleEvent(keyEvent)) {
164             MMI_HILOGD("Key event was consumed");
165             return true;
166         }
167     }
168     return false;
169 }
170 #endif // OHOS_BUILD_ENABLE_KEYBOARD
171 
172 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent)173 bool EventMonitorHandler::OnHandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
174 {
175     CHKPF(pointerEvent);
176     if (pointerEvent->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(pointerEvent)) {
180             MMI_HILOGD("Pointer event was monitor");
181             return true;
182         }
183     }
184     MMI_HILOGD("Interception and monitor failed");
185     return false;
186 }
187 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
188 
InitSessionLostCallback()189 void EventMonitorHandler::InitSessionLostCallback()
190 {
191     if (sessionLostCallbackInitialized_)  {
192         return;
193     }
194     auto udsServerPtr = InputHandler->GetUDSServer();
195     CHKPV(udsServerPtr);
196     udsServerPtr->AddSessionDeletedCallback([this] (SessionPtr session) {
197         return this->OnSessionLost(session);
198     }
199     );
200     sessionLostCallbackInitialized_ = true;
201     MMI_HILOGD("The callback on session deleted is registered successfully");
202 }
203 
OnSessionLost(SessionPtr session)204 void EventMonitorHandler::OnSessionLost(SessionPtr session)
205 {
206     monitors_.OnSessionLost(session);
207 }
208 
SendToClient(std::shared_ptr<KeyEvent> keyEvent,NetPacket & pkt) const209 void EventMonitorHandler::SessionHandler::SendToClient(std::shared_ptr<KeyEvent> keyEvent, NetPacket &pkt) const
210 {
211     CHKPV(keyEvent);
212     CHKPV(session_);
213     if (InputEventDataTransformation::KeyEventToNetPacket(keyEvent, pkt) != RET_OK) {
214         MMI_HILOGE("Packet key event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
215         return;
216     }
217     if (!session_->SendMsg(pkt)) {
218         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
219     }
220 }
221 
SendToClient(std::shared_ptr<PointerEvent> pointerEvent,NetPacket & pkt) const222 void EventMonitorHandler::SessionHandler::SendToClient(std::shared_ptr<PointerEvent> pointerEvent,
223                                                        NetPacket &pkt) const
224 {
225     CHKPV(pointerEvent);
226     CHKPV(session_);
227     MMI_HILOGD("Service SendToClient InputHandlerType:%{public}d, TokenType:%{public}d, pid:%{public}d",
228         handlerType_, session_->GetTokenType(), session_->GetPid());
229     if (!session_->SendMsg(pkt)) {
230         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
231         return;
232     }
233 }
234 
AddMonitor(const SessionHandler & monitor)235 int32_t EventMonitorHandler::MonitorCollection::AddMonitor(const SessionHandler& monitor)
236 {
237     if (monitors_.size() >= MAX_N_INPUT_MONITORS) {
238         MMI_HILOGE("The number of monitors exceeds the maximum:%{public}zu, monitors errCode:%{public}d",
239                    monitors_.size(), INVALID_MONITOR_MON);
240         return RET_ERR;
241     }
242     bool isFound = false;
243     auto iter = monitors_.find(monitor);
244     if (iter != monitors_.end()) {
245         if (iter->eventType_ == monitor.eventType_) {
246             MMI_HILOGD("Monitor with event type (%{public}u) already exists", monitor.eventType_);
247             return RET_OK;
248         }
249         isFound = true;
250         monitors_.erase(iter);
251     }
252 
253     auto [sIter, isOk] = monitors_.insert(monitor);
254     if (!isOk) {
255         if (isFound) {
256             MMI_HILOGE("Internal error: monitor has been removed");
257         } else {
258             MMI_HILOGE("Failed to add monitor");
259         }
260         return RET_ERR;
261     }
262 
263     if (isFound) {
264         MMI_HILOGD("Event type is updated:%{public}u", monitor.eventType_);
265     } else {
266         MMI_HILOGD("Service Add Monitor Success");
267     }
268     return RET_OK;
269 }
270 
RemoveMonitor(const SessionHandler & monitor)271 void EventMonitorHandler::MonitorCollection::RemoveMonitor(const SessionHandler& monitor)
272 {
273     auto iter = monitors_.find(monitor);
274     if (iter == monitors_.cend()) {
275         MMI_HILOGE("Monitor does not exist");
276         return;
277     }
278 
279     monitors_.erase(iter);
280     if (monitor.session_) {
281         int32_t pid = monitor.session_->GetPid();
282         auto it = endScreenCaptureMonitors_.find(pid);
283         if (it != endScreenCaptureMonitors_.end()) {
284             auto setIter = endScreenCaptureMonitors_[pid].find(monitor);
285             if (setIter != endScreenCaptureMonitors_[pid].end()) {
286                 endScreenCaptureMonitors_[pid].erase(setIter);
287             }
288             if (endScreenCaptureMonitors_[pid].empty()) {
289                 endScreenCaptureMonitors_.erase(it);
290             }
291         }
292     }
293     if (monitor.eventType_ == HANDLE_EVENT_TYPE_NONE) {
294         MMI_HILOGD("Unregister monitor successfully");
295         return;
296     }
297 
298     auto [sIter, isOk] = monitors_.insert(monitor);
299     if (!isOk) {
300         MMI_HILOGE("Internal error, monitor has been removed");
301         return;
302     }
303     MMI_HILOGD("Event type is updated:%{public}u", monitor.eventType_);
304 }
305 
MarkConsumed(int32_t eventId,SessionPtr session)306 void EventMonitorHandler::MonitorCollection::MarkConsumed(int32_t eventId, SessionPtr session)
307 {
308     if (!HasMonitor(session)) {
309         MMI_HILOGW("Specified monitor does not exist");
310         return;
311     }
312     auto tIter = states_.begin();
313     for (; tIter != states_.end(); ++tIter) {
314         const auto &eventIds = tIter->second.eventIds_;
315         if (eventIds.find(eventId) != eventIds.cend()) {
316             break;
317         }
318     }
319     if (tIter == states_.end()) {
320         MMI_HILOGE("No operation corresponding to this event");
321         return;
322     }
323     ConsumptionState &state = tIter->second;
324 
325     if (state.isMonitorConsumed_) {
326         MMI_HILOGE("Corresponding operation has been marked as consumed");
327         return;
328     }
329     state.isMonitorConsumed_ = true;
330     CHKPV(state.lastPointerEvent_);
331 #ifdef OHOS_BUILD_ENABLE_TOUCH
332     MMI_HILOGD("Cancel operation");
333     auto pointerEvent = std::make_shared<PointerEvent>(*state.lastPointerEvent_);
334     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
335     pointerEvent->SetActionTime(GetSysClockTime());
336     pointerEvent->UpdateId();
337     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT | InputEvent::EVENT_FLAG_NO_MONITOR);
338     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
339     CHKPV(inputEventNormalizeHandler);
340     inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
341 #endif // OHOS_BUILD_ENABLE_TOUCH
342 }
343 
344 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleEvent(std::shared_ptr<KeyEvent> keyEvent)345 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<KeyEvent> keyEvent)
346 {
347     CHKPF(keyEvent);
348     MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
349     NetPacket pkt(MmiMessageId::REPORT_KEY_EVENT);
350     pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
351     if (pkt.ChkRWError()) {
352         MMI_HILOGE("Packet write key event failed");
353         return false;
354     }
355     for (const auto &mon : monitors_) {
356         if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) == HANDLE_EVENT_TYPE_KEY) {
357             mon.SendToClient(keyEvent, pkt);
358         }
359     }
360     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
361         NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
362         for (const auto &mon : monitors_) {
363             OHOS::MMI::NapProcess::NapStatusData napData;
364             auto sess = mon.session_;
365             if (!sess) {
366                 continue;
367             }
368             napData.pid = sess->GetPid();
369             napData.uid = sess->GetUid();
370             napData.bundleName = sess->GetProgramName();
371             if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
372                 int32_t syncState = ACTIVE_EVENT;
373                 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
374                 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
375             }
376         }
377     }
378     return false;
379 }
380 #endif // OHOS_BUILD_ENABLE_KEYBOARD
381 
382 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)383 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
384 {
385     CHKPF(pointerEvent);
386 #ifdef OHOS_BUILD_ENABLE_TOUCH
387     UpdateConsumptionState(pointerEvent);
388 #endif // OHOS_BUILD_ENABLE_TOUCH
389     Monitor(pointerEvent);
390     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
391         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
392         auto iter = states_.find(pointerEvent->GetDeviceId());
393         return (iter != states_.end() ? iter->second.isMonitorConsumed_ : false);
394     }
395     MMI_HILOGD("This is not a touch-screen event");
396     return false;
397 }
398 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
399 
HasMonitor(SessionPtr session)400 bool EventMonitorHandler::MonitorCollection::HasMonitor(SessionPtr session)
401 {
402     SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session };
403     return (monitors_.find(monitor) != monitors_.end());
404 }
405 
HasScreenCaptureMonitor(SessionPtr session)406 bool EventMonitorHandler::MonitorCollection::HasScreenCaptureMonitor(SessionPtr session)
407 {
408     int32_t pid = session->GetPid();
409     return (endScreenCaptureMonitors_.find(pid) != endScreenCaptureMonitors_.end());
410 }
411 
RemoveScreenCaptureMonitor(SessionPtr session)412 void EventMonitorHandler::MonitorCollection::RemoveScreenCaptureMonitor(SessionPtr session)
413 {
414     if (session->GetTokenType() != TokenType::TOKEN_HAP) {
415         return;
416     }
417     int32_t pid = session->GetPid();
418     std::set<SessionHandler> monitorSet;
419     for (const auto &monitor : monitors_) {
420         if (monitor.session_ == session) {
421             SessionHandler screenCaptureMointor(monitor);
422             monitorSet.insert(screenCaptureMointor);
423         }
424     }
425     for (const auto &monitor : monitorSet) {
426         auto it = monitors_.find(monitor);
427         if (it != monitors_.end()) {
428             monitors_.erase(it);
429         }
430     }
431     endScreenCaptureMonitors_.emplace(pid, monitorSet);
432 }
433 
RecoveryScreenCaptureMonitor(SessionPtr session)434 void EventMonitorHandler::MonitorCollection::RecoveryScreenCaptureMonitor(SessionPtr session)
435 {
436     if (session->GetTokenType() != TokenType::TOKEN_HAP) {
437         return;
438     }
439     int32_t pid = session->GetPid();
440     auto it = endScreenCaptureMonitors_.find(pid);
441     if (it != endScreenCaptureMonitors_.end()) {
442         for (auto &monitor : endScreenCaptureMonitors_[pid]) {
443             SessionHandler screenCaptureMointor(monitor);
444             monitors_.insert(screenCaptureMointor);
445         }
446         endScreenCaptureMonitors_.erase(it);
447     }
448 }
449 
450 #ifdef OHOS_BUILD_ENABLE_TOUCH
UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)451 void EventMonitorHandler::MonitorCollection::UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)
452 {
453     CALL_DEBUG_ENTER;
454     CHKPV(pointerEvent);
455     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
456         pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD) {
457         return;
458     }
459     auto sIter = states_.find(pointerEvent->GetDeviceId());
460     if (sIter == states_.end()) {
461         auto [tIter, isOk] = states_.emplace(pointerEvent->GetDeviceId(), ConsumptionState());
462         if (!isOk) {
463             MMI_HILOGE("Failed to emplace consumption state");
464             return;
465         }
466         sIter = tIter;
467     }
468     ConsumptionState &state = sIter->second;
469     if (state.eventIds_.size() >= MAX_EVENTIDS_SIZE) {
470         auto iter = state.eventIds_.begin();
471         state.eventIds_.erase(iter);
472     }
473     auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
474     if (!isOk) {
475         MMI_HILOGW("Failed to stash event");
476     }
477     state.lastPointerEvent_ = pointerEvent;
478 
479     if (pointerEvent->GetPointerIds().size() != 1) {
480         MMI_HILOGD("In intermediate process");
481         return;
482     }
483     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
484         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
485         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_BEGIN) {
486         MMI_HILOGD("First press down");
487         state.eventIds_.clear();
488         auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
489         if (!isOk) {
490             MMI_HILOGW("Event number is duplicated");
491         }
492         state.isMonitorConsumed_ = false;
493     } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
494         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END ||
495         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_END) {
496         MMI_HILOGD("Last lift up");
497         state.eventIds_.clear();
498     }
499 }
500 #endif // OHOS_BUILD_ENABLE_TOUCH
501 
502 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
Monitor(std::shared_ptr<PointerEvent> pointerEvent)503 void EventMonitorHandler::MonitorCollection::Monitor(std::shared_ptr<PointerEvent> pointerEvent)
504 {
505     CHKPV(pointerEvent);
506     MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
507     NetPacket pkt(MmiMessageId::REPORT_POINTER_EVENT);
508     pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
509     if (pkt.ChkRWError()) {
510         MMI_HILOGE("Packet write pointer event failed");
511         return;
512     }
513     if (InputEventDataTransformation::Marshalling(pointerEvent, pkt) != RET_OK) {
514         MMI_HILOGE("Marshalling pointer event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
515         return;
516     }
517     for (const auto &monitor : monitors_) {
518         if ((monitor.eventType_ & pointerEvent->GetHandlerEventType()) == pointerEvent->GetHandlerEventType()) {
519             if (monitor.session_) {
520                 monitor.SendToClient(pointerEvent, pkt);
521                 continue;
522             }
523             if (monitor.callback) {
524                 monitor.callback->OnInputEvent(monitor.handlerType_, pointerEvent);
525             }
526         }
527     }
528     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
529         NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
530         for (const auto &mon : monitors_) {
531             OHOS::MMI::NapProcess::NapStatusData napData;
532             auto sess = mon.session_;
533             if (!sess) {
534                 continue;
535             }
536             napData.pid = sess->GetPid();
537             napData.uid = sess->GetUid();
538             napData.bundleName = sess->GetProgramName();
539             if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
540                 int32_t syncState = ACTIVE_EVENT;
541                 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
542                 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
543             }
544         }
545     }
546 }
547 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
548 
OnSessionLost(SessionPtr session)549 void EventMonitorHandler::MonitorCollection::OnSessionLost(SessionPtr session)
550 {
551     CALL_INFO_TRACE;
552     std::set<SessionHandler>::const_iterator cItr = monitors_.cbegin();
553     while (cItr != monitors_.cend()) {
554         if (cItr->session_ != session) {
555             ++cItr;
556         } else {
557             cItr = monitors_.erase(cItr);
558         }
559     }
560     CHKPV(session);
561     int32_t pid = session->GetPid();
562     auto it = endScreenCaptureMonitors_.find(pid);
563     if (it != endScreenCaptureMonitors_.end()) {
564         endScreenCaptureMonitors_.erase(it);
565     }
566 }
567 
Dump(int32_t fd,const std::vector<std::string> & args)568 void EventMonitorHandler::Dump(int32_t fd, const std::vector<std::string> &args)
569 {
570     return monitors_.Dump(fd, args);
571 }
572 
Dump(int32_t fd,const std::vector<std::string> & args)573 void EventMonitorHandler::MonitorCollection::Dump(int32_t fd, const std::vector<std::string> &args)
574 {
575     CALL_DEBUG_ENTER;
576     mprintf(fd, "Monitor information:\t");
577     mprintf(fd, "monitors: count=%zu", monitors_.size());
578     for (const auto &item : monitors_) {
579         SessionPtr session = item.session_;
580         if (!session) {
581             continue;
582         }
583         mprintf(fd,
584                 "handlerType:%d | Pid:%d | Uid:%d | Fd:%d "
585                 "| EarliestEventTime:%" PRId64 " | Descript:%s "
586                 "| EventType:%u | ProgramName:%s \t",
587                 item.handlerType_, session->GetPid(),
588                 session->GetUid(), session->GetFd(),
589                 session->GetEarliestEventTime(), session->GetDescript().c_str(),
590                 item.eventType_, session->GetProgramName().c_str());
591     }
592 }
593 
594 #ifdef PLAYER_FRAMEWORK_EXISTS
RegisterScreenCaptureListener()595 void EventMonitorHandler::RegisterScreenCaptureListener()
596 {
597     screenCaptureMonitorListener_ = new (std::nothrow) InputScreenCaptureMonitorListener();
598     CHKPV(screenCaptureMonitorListener_);
599     Media::ScreenCaptureMonitor::GetInstance()->RegisterScreenCaptureMonitorListener(screenCaptureMonitorListener_);
600 }
601 
OnScreenCaptureStarted(SessionPtr session)602 void EventMonitorHandler::OnScreenCaptureStarted(SessionPtr session)
603 {
604     if (!monitors_.HasMonitor(session) && !monitors_.HasScreenCaptureMonitor(session)) {
605         MMI_HILOGI("This process has no screen capture monitor");
606         return;
607     }
608     monitors_.RecoveryScreenCaptureMonitor(session);
609 }
610 
OnScreenCaptureFinished(SessionPtr session)611 void EventMonitorHandler::OnScreenCaptureFinished(SessionPtr session)
612 {
613     if (!monitors_.HasMonitor(session)) {
614         MMI_HILOGI("This process has no screen capture monitor");
615         return;
616     }
617     monitors_.RemoveScreenCaptureMonitor(session);
618 }
619 #endif
620 } // namespace MMI
621 } // namespace OHOS
622