• 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 "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     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
425     pointerEvent->SetActionTime(GetSysClockTime());
426     pointerEvent->UpdateId();
427     pointerEvent->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT | InputEvent::EVENT_FLAG_NO_MONITOR);
428     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
429     CHKPV(inputEventNormalizeHandler);
430     inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
431 #endif // OHOS_BUILD_ENABLE_TOUCH
432 }
433 
434 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleEvent(std::shared_ptr<KeyEvent> keyEvent)435 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<KeyEvent> keyEvent)
436 {
437     CHKPF(keyEvent);
438     MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
439     NetPacket pkt(MmiMessageId::REPORT_KEY_EVENT);
440     pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
441     if (pkt.ChkRWError()) {
442         MMI_HILOGE("Packet write key event failed");
443         return false;
444     }
445     for (const auto &mon : monitors_) {
446         if ((mon.eventType_ & HANDLE_EVENT_TYPE_KEY) != HANDLE_EVENT_TYPE_KEY) {
447             continue;
448         }
449         if (!keyEvent->GetFourceMonitorFlag()) {
450             mon.SendToClient(keyEvent, pkt);
451         } else if (mon.session_ != nullptr && mon.session_->GetUid() == POWER_UID) {
452             mon.SendToClient(keyEvent, pkt);
453         }
454     }
455     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
456         NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
457         for (const auto &mon : monitors_) {
458             OHOS::MMI::NapProcess::NapStatusData napData;
459             auto sess = mon.session_;
460             if (!sess) {
461                 continue;
462             }
463             napData.pid = sess->GetPid();
464             napData.uid = sess->GetUid();
465             napData.bundleName = sess->GetProgramName();
466             if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
467                 int32_t syncState = ACTIVE_EVENT;
468                 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
469                 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
470             }
471         }
472     }
473     return false;
474 }
475 #endif // OHOS_BUILD_ENABLE_KEYBOARD
476 
477 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)478 bool EventMonitorHandler::MonitorCollection::HandleEvent(std::shared_ptr<PointerEvent> pointerEvent)
479 {
480     CHKPF(pointerEvent);
481 #ifdef OHOS_BUILD_ENABLE_TOUCH
482     UpdateConsumptionState(pointerEvent);
483 #endif // OHOS_BUILD_ENABLE_TOUCH
484     Monitor(pointerEvent);
485     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
486         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
487         auto iter = states_.find(pointerEvent->GetDeviceId());
488         return (iter != states_.end() ? iter->second.isMonitorConsumed_ : false);
489     }
490     MMI_HILOGD("This is not a touch-screen event");
491     return false;
492 }
493 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
494 
HasMonitor(SessionPtr session)495 bool EventMonitorHandler::MonitorCollection::HasMonitor(SessionPtr session)
496 {
497     SessionHandler monitor { InputHandlerType::MONITOR, HANDLE_EVENT_TYPE_ALL, session };
498     return (monitors_.find(monitor) != monitors_.end());
499 }
500 
HasScreenCaptureMonitor(SessionPtr session)501 bool EventMonitorHandler::MonitorCollection::HasScreenCaptureMonitor(SessionPtr session)
502 {
503     int32_t pid = session->GetPid();
504     return (endScreenCaptureMonitors_.find(pid) != endScreenCaptureMonitors_.end());
505 }
506 
RemoveScreenCaptureMonitor(SessionPtr session)507 void EventMonitorHandler::MonitorCollection::RemoveScreenCaptureMonitor(SessionPtr session)
508 {
509     if (session->GetTokenType() != TokenType::TOKEN_HAP) {
510         return;
511     }
512     int32_t pid = session->GetPid();
513     std::set<SessionHandler> monitorSet;
514     for (const auto &monitor : monitors_) {
515         if (monitor.session_ == session) {
516             SessionHandler screenCaptureMointor(monitor);
517             monitorSet.insert(screenCaptureMointor);
518         }
519     }
520     for (const auto &monitor : monitorSet) {
521         auto it = monitors_.find(monitor);
522         if (it != monitors_.end()) {
523             monitors_.erase(it);
524         }
525     }
526     endScreenCaptureMonitors_.emplace(pid, monitorSet);
527 }
528 
RecoveryScreenCaptureMonitor(SessionPtr session)529 void EventMonitorHandler::MonitorCollection::RecoveryScreenCaptureMonitor(SessionPtr session)
530 {
531     if (session->GetTokenType() != TokenType::TOKEN_HAP) {
532         return;
533     }
534     int32_t pid = session->GetPid();
535     auto it = endScreenCaptureMonitors_.find(pid);
536     if (it != endScreenCaptureMonitors_.end()) {
537         for (auto &monitor : endScreenCaptureMonitors_[pid]) {
538             SessionHandler screenCaptureMointor(monitor);
539             monitors_.insert(screenCaptureMointor);
540         }
541         endScreenCaptureMonitors_.erase(it);
542     }
543 }
544 
545 #ifdef OHOS_BUILD_ENABLE_TOUCH
UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)546 void EventMonitorHandler::MonitorCollection::UpdateConsumptionState(std::shared_ptr<PointerEvent> pointerEvent)
547 {
548     CALL_DEBUG_ENTER;
549     CHKPV(pointerEvent);
550     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
551         pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD) {
552         return;
553     }
554     auto sIter = states_.find(pointerEvent->GetDeviceId());
555     if (sIter == states_.end()) {
556         auto [tIter, isOk] = states_.emplace(pointerEvent->GetDeviceId(), ConsumptionState());
557         if (!isOk) {
558             MMI_HILOGE("Failed to emplace consumption state");
559             return;
560         }
561         sIter = tIter;
562     }
563     ConsumptionState &state = sIter->second;
564     if (state.eventIds_.size() >= MAX_EVENTIDS_SIZE) {
565         auto iter = state.eventIds_.begin();
566         state.eventIds_.erase(iter);
567     }
568     auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
569     if (!isOk) {
570         MMI_HILOGW("Failed to stash event");
571     }
572     state.lastPointerEvent_ = pointerEvent;
573 
574     if (pointerEvent->GetPointerIds().size() != 1) {
575         MMI_HILOGD("In intermediate process");
576         return;
577     }
578     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
579         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
580         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_BEGIN) {
581         MMI_HILOGD("First press down");
582         state.eventIds_.clear();
583         auto [tIter, isOk] = state.eventIds_.emplace(pointerEvent->GetId());
584         if (!isOk) {
585             MMI_HILOGW("Event number is duplicated");
586         }
587         state.isMonitorConsumed_ = false;
588     } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
589         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END ||
590         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_END) {
591         MMI_HILOGD("Last lift up");
592         state.eventIds_.clear();
593     }
594 }
595 #endif // OHOS_BUILD_ENABLE_TOUCH
596 
597 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
IsSendToClient(const SessionHandler & monitor,std::shared_ptr<PointerEvent> pointerEvent,NetPacket & pkt)598 void EventMonitorHandler::MonitorCollection::IsSendToClient(const SessionHandler &monitor,
599     std::shared_ptr<PointerEvent> pointerEvent, NetPacket &pkt)
600 {
601     if (monitor.Expect(pointerEvent)) {
602         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_BEGIN ||
603             pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_SWIPE_END) {
604             MMI_HILOGI("Swipe event sended in monitor! action type:%{public}d finger count:%{public}d",
605                 pointerEvent->GetPointerAction(),
606                 pointerEvent->GetFingerCount());
607         }
608         if (monitor.session_ && CheckIfNeedSendToClient(monitor, pointerEvent)) {
609             monitor.SendToClient(pointerEvent, pkt);
610             return;
611         }
612         if (monitor.callback_) {
613             monitor.callback_->OnInputEvent(monitor.handlerType_, pointerEvent);
614         }
615     }
616     if (monitor.actionsType_.empty()) {
617         return;
618     }
619     auto iter = std::find(monitor.actionsType_.begin(), monitor.actionsType_.end(),
620     pointerEvent->GetPointerAction());
621     if (iter != monitor.actionsType_.end() && monitor.session_) {
622         monitor.SendToClient(pointerEvent, pkt);
623     }
624 }
625 
Monitor(std::shared_ptr<PointerEvent> pointerEvent)626 void EventMonitorHandler::MonitorCollection::Monitor(std::shared_ptr<PointerEvent> pointerEvent)
627 {
628     CHKPV(pointerEvent);
629     MMI_HILOGD("There are currently %{public}zu monitors", monitors_.size());
630     NetPacket pkt(MmiMessageId::REPORT_POINTER_EVENT);
631     pkt << InputHandlerType::MONITOR << static_cast<uint32_t>(evdev_device_udev_tags::EVDEV_UDEV_TAG_INPUT);
632     if (pkt.ChkRWError()) {
633         MMI_HILOGE("Packet write pointer event failed");
634         return;
635     }
636     if (InputEventDataTransformation::Marshalling(pointerEvent, pkt) != RET_OK) {
637         MMI_HILOGE("Marshalling pointer event failed, errCode:%{public}d", STREAM_BUF_WRITE_FAIL);
638         return;
639     }
640     int32_t pointerId = pointerEvent->GetPointerId();
641     PointerEvent::PointerItem pointerItem;
642     pointerEvent->GetPointerItem(pointerId, pointerItem);
643     int32_t displayX = pointerItem.GetDisplayX();
644     int32_t displayY = pointerItem.GetDisplayY();
645     for (const auto &monitor : monitors_) {
646         IsSendToClient(monitor, pointerEvent, pkt);
647         PointerEvent::PointerItem pointerItem1;
648         pointerEvent->GetPointerItem(pointerId, pointerItem1);
649         int32_t displayX1 = pointerItem1.GetDisplayX();
650         int32_t displayY1 = pointerItem1.GetDisplayY();
651         if (displayX != displayX1 || displayY != displayY1) {
652             MMI_HILOGW("Display coord changed %{public}d, %{public}d, %{public}d, %{public}d, %{public}d",
653                 pointerId, displayX, displayY, displayX1, displayY1);
654         }
655     }
656     if (NapProcess::GetInstance()->GetNapClientPid() != REMOVE_OBSERVER &&
657         NapProcess::GetInstance()->GetNapClientPid() != UNOBSERVED) {
658         for (const auto &mon : monitors_) {
659             OHOS::MMI::NapProcess::NapStatusData napData;
660             auto sess = mon.session_;
661             if (!sess) {
662                 continue;
663             }
664             napData.pid = sess->GetPid();
665             napData.uid = sess->GetUid();
666             napData.bundleName = sess->GetProgramName();
667             if (NapProcess::GetInstance()->IsNeedNotify(napData)) {
668                 int32_t syncState = ACTIVE_EVENT;
669                 NapProcess::GetInstance()->AddMmiSubscribedEventData(napData, syncState);
670                 NapProcess::GetInstance()->NotifyBundleName(napData, syncState);
671             }
672         }
673     }
674 }
675 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
676 
IsPinch(std::shared_ptr<PointerEvent> pointerEvent)677 bool EventMonitorHandler::MonitorCollection::IsPinch(std::shared_ptr<PointerEvent> pointerEvent)
678 {
679     CHKPF(pointerEvent);
680     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE &&
681         pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD) {
682         return false;
683     }
684     if ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
685         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
686         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_END)) {
687         return false;
688     }
689     return true;
690 }
691 
IsRotate(std::shared_ptr<PointerEvent> pointerEvent)692 bool EventMonitorHandler::MonitorCollection::IsRotate(std::shared_ptr<PointerEvent> pointerEvent)
693 {
694     CHKPF(pointerEvent);
695     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE ||
696         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_BEGIN &&
697         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
698         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_END)) {
699         return false;
700     }
701     return true;
702 }
703 
704 
IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)705 bool EventMonitorHandler::MonitorCollection::IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)
706 {
707     CHKPF(pointerEvent);
708     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
709         pointerEvent->GetFingerCount() != THREE_FINGERS ||
710         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_BEGIN &&
711         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
712         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_END)) {
713         return false;
714     }
715     return true;
716 }
717 
IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)718 bool EventMonitorHandler::MonitorCollection::IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent)
719 {
720     CHKPF(pointerEvent);
721     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
722         pointerEvent->GetFingerCount() != FOUR_FINGERS ||
723         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_BEGIN &&
724         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
725         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_END)) {
726         return false;
727     }
728     return true;
729 }
730 
IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent)731 bool EventMonitorHandler::MonitorCollection::IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent)
732 {
733     CHKPR(pointerEvent, ERROR_NULL_POINTER);
734     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHPAD ||
735         pointerEvent->GetFingerCount() != THREE_FINGERS ||
736         (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_TRIPTAP)) {
737         return false;
738     }
739     return true;
740 }
741 
742 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent)743 bool EventMonitorHandler::MonitorCollection::IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent)
744 {
745     CHKPR(pointerEvent, ERROR_NULL_POINTER);
746     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_FINGERPRINT &&
747         (PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN <= pointerEvent->GetPointerAction() &&
748         pointerEvent->GetPointerAction() <= PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK)) {
749             return true;
750     }
751     MMI_HILOGD("not fingerprint event");
752     return false;
753 }
754 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
CheckIfNeedSendToClient(SessionHandler monitor,std::shared_ptr<PointerEvent> pointerEvent)755 bool EventMonitorHandler::MonitorCollection::CheckIfNeedSendToClient(SessionHandler monitor,
756     std::shared_ptr<PointerEvent> pointerEvent)
757 {
758     CHKPF(pointerEvent);
759 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
760     if ((monitor.eventType_ & HANDLE_EVENT_TYPE_FINGERPRINT) ==
761         HANDLE_EVENT_TYPE_FINGERPRINT && IsFingerprint(pointerEvent)) {
762         return true;
763     }
764 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
765     if ((monitor.eventType_ & HANDLE_EVENT_TYPE_POINTER) == HANDLE_EVENT_TYPE_POINTER) {
766         return true;
767     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE) {
768         return true;
769     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_SWIPEINWARD) == HANDLE_EVENT_TYPE_SWIPEINWARD) {
770         return true;
771     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_TOUCH) == HANDLE_EVENT_TYPE_TOUCH &&
772         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
773         return true;
774     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_MOUSE) == HANDLE_EVENT_TYPE_MOUSE &&
775         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
776         return true;
777     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_PINCH) == HANDLE_EVENT_TYPE_PINCH &&
778         IsPinch(pointerEvent)) {
779         return true;
780     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_THREEFINGERSSWIP) == HANDLE_EVENT_TYPE_THREEFINGERSSWIP &&
781         IsThreeFingersSwipe(pointerEvent)) {
782         return true;
783     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_FOURFINGERSSWIP) == HANDLE_EVENT_TYPE_FOURFINGERSSWIP &&
784         IsFourFingersSwipe(pointerEvent)) {
785         return true;
786     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_ROTATE) == HANDLE_EVENT_TYPE_ROTATE &&
787         IsRotate(pointerEvent)) {
788         return true;
789     } else if ((monitor.eventType_ & HANDLE_EVENT_TYPE_THREEFINGERSTAP) == HANDLE_EVENT_TYPE_THREEFINGERSTAP &&
790         IsThreeFingersTap(pointerEvent)) {
791         return true;
792     }
793     return false;
794 }
795 
OnSessionLost(SessionPtr session)796 void EventMonitorHandler::MonitorCollection::OnSessionLost(SessionPtr session)
797 {
798     CALL_INFO_TRACE;
799     std::set<SessionHandler>::const_iterator cItr = monitors_.cbegin();
800     while (cItr != monitors_.cend()) {
801         if (cItr->session_ != session) {
802             ++cItr;
803         } else {
804             cItr = monitors_.erase(cItr);
805         }
806     }
807     CHKPV(session);
808     int32_t pid = session->GetPid();
809     auto it = endScreenCaptureMonitors_.find(pid);
810     if (it != endScreenCaptureMonitors_.end()) {
811         endScreenCaptureMonitors_.erase(it);
812     }
813 }
814 
CheckHasInputHandler(HandleEventType eventType)815 bool EventMonitorHandler::MonitorCollection::CheckHasInputHandler(HandleEventType eventType)
816 {
817     for (const auto &item : monitors_) {
818         if ((item.eventType_ & eventType) == eventType) {
819             return true;
820         }
821     }
822     return false;
823 }
824 
Dump(int32_t fd,const std::vector<std::string> & args)825 void EventMonitorHandler::Dump(int32_t fd, const std::vector<std::string> &args)
826 {
827     return monitors_.Dump(fd, args);
828 }
829 
Dump(int32_t fd,const std::vector<std::string> & args)830 void EventMonitorHandler::MonitorCollection::Dump(int32_t fd, const std::vector<std::string> &args)
831 {
832     CALL_DEBUG_ENTER;
833     mprintf(fd, "Monitor information:\t");
834     mprintf(fd, "monitors: count=%zu", monitors_.size());
835     for (const auto &item : monitors_) {
836         SessionPtr session = item.session_;
837         if (!session) {
838             continue;
839         }
840         mprintf(fd,
841                 "handlerType:%d | Pid:%d | Uid:%d | Fd:%d "
842                 "| EarliestEventTime:%" PRId64 " | Descript:%s "
843                 "| EventType:%u | ProgramName:%s \t",
844                 item.handlerType_, session->GetPid(),
845                 session->GetUid(), session->GetFd(),
846                 session->GetEarliestEventTime(), session->GetDescript().c_str(),
847                 item.eventType_, session->GetProgramName().c_str());
848     }
849 }
850 
851 #ifdef PLAYER_FRAMEWORK_EXISTS
ProcessScreenCapture(int32_t pid,bool isStart)852 void EventMonitorHandler::ProcessScreenCapture(int32_t pid, bool isStart)
853 {
854     auto udsServerPtr = InputHandler->GetUDSServer();
855     CHKPV(udsServerPtr);
856     SessionPtr session = udsServerPtr->GetSessionByPid(pid);
857     CHKPV(session);
858     if (isStart) {
859         if (!monitors_.HasMonitor(session) && !monitors_.HasScreenCaptureMonitor(session)) {
860             MMI_HILOGI("This process has no screen capture monitor");
861             return;
862         }
863         monitors_.RecoveryScreenCaptureMonitor(session);
864     } else {
865         if (!monitors_.HasMonitor(session)) {
866             MMI_HILOGI("This process has no screen capture monitor");
867             return;
868         }
869         monitors_.RemoveScreenCaptureMonitor(session);
870     }
871 }
872 #endif // PLAYER_FRAMEWORK_EXISTS
873 } // namespace MMI
874 } // namespace OHOS
875