• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_dispatch_handler.h"
17 
18 #include "anr_manager.h"
19 #include "app_debug_listener.h"
20 #include "bytrace_adapter.h"
21 #include "cursor_drawing_component.h"
22 #include "dfx_hisysevent.h"
23 #include "event_log_helper.h"
24 #include "input_event_data_transformation.h"
25 #include "input_event_handler.h"
26 #include "pointer_device_manager.h"
27 
28 #undef MMI_LOG_DOMAIN
29 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "EventDispatchHandler"
32 
33 namespace OHOS {
34 namespace MMI {
35 namespace {
36 constexpr int64_t ERROR_TIME {3000000};
37 constexpr int32_t INTERVAL_TIME { 3000 }; // log time interval is 3 seconds.
38 constexpr int32_t INTERVAL_DURATION { 10 };
39 constexpr int32_t THREE_FINGERS { 3 };
40 const std::string CURRENT_DEVICE_TYPE = system::GetParameter("const.product.devicetype", "unknown");
41 const std::string PRODUCT_TYPE_TABLET = "tablet";
42 } // namespace
43 
44 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)45 void EventDispatchHandler::HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
46 {
47     CHKPV(keyEvent);
48     auto udsServer = InputHandler->GetUDSServer();
49     CHKPV(udsServer);
50     if (CURRENT_DEVICE_TYPE == PRODUCT_TYPE_TABLET) {
51         AddFlagToEsc(keyEvent);
52     }
53     DispatchKeyEventPid(*udsServer, keyEvent);
54 }
55 #endif // OHOS_BUILD_ENABLE_KEYBOARD
56 
57 #ifdef OHOS_BUILD_ENABLE_POINTER
HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)58 void EventDispatchHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
59 {
60     CHKPV(pointerEvent);
61     EnsureMouseEventCycle(pointerEvent);
62     HandlePointerEventInner(pointerEvent);
63     CleanMouseEventCycle(pointerEvent);
64 }
65 #endif // OHOS_BUILD_ENABLE_POINTER
66 
67 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)68 void EventDispatchHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
69 {
70     CHKPV(pointerEvent);
71     HandlePointerEventInner(pointerEvent);
72 }
73 #endif // OHOS_BUILD_ENABLE_TOUCH
74 
75 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointerEvent,int32_t fd)76 void EventDispatchHandler::FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointerEvent, int32_t fd)
77 {
78     CHKPV(pointerEvent);
79     auto udsServer = InputHandler->GetUDSServer();
80     CHKPV(udsServer);
81     auto pointerIdList = pointerEvent->GetPointerIds();
82     if (pointerIdList.size() > 1) {
83         for (const auto& id : pointerIdList) {
84             PointerEvent::PointerItem pointeritem;
85             if (!pointerEvent->GetPointerItem(id, pointeritem)) {
86                 MMI_HILOGW("Can't find this pointerItem");
87                 continue;
88             }
89             auto itemPid = WIN_MGR->GetWindowPid(pointeritem.GetTargetWindowId());
90             if ((itemPid >= 0) && (itemPid != udsServer->GetClientPid(fd))) {
91                 pointerEvent->RemovePointerItem(id);
92                 MMI_HILOGD("pointerIdList size:%{public}zu", pointerEvent->GetPointerIds().size());
93             }
94         }
95     }
96 }
97 
SearchCancelList(int32_t pointerId,int32_t windowId)98 std::shared_ptr<WindowInfo> EventDispatchHandler::SearchCancelList (int32_t pointerId, int32_t windowId)
99 {
100     if (cancelEventList_.find(pointerId) == cancelEventList_.end()) {
101         return nullptr;
102     }
103     auto windowList = cancelEventList_[pointerId];
104     for (auto &info : windowList) {
105         CHKPC(info);
106         if (info->id == windowId) {
107             return info;
108         }
109     }
110     return nullptr;
111 }
112 
ReissueEvent(std::shared_ptr<PointerEvent> & point,int32_t windowId,std::optional<WindowInfo> & windowInfo)113 bool EventDispatchHandler::ReissueEvent(std::shared_ptr<PointerEvent> &point, int32_t windowId,
114     std::optional<WindowInfo> &windowInfo)
115 {
116     int32_t pointerId = point->GetPointerId();
117     if (windowInfo == std::nullopt) {
118         std::shared_ptr<WindowInfo> curInfo = SearchCancelList(pointerId, windowId);
119         if (curInfo != nullptr && (point->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
120             point->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL)) {
121             point->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
122             windowInfo = std::make_optional(*curInfo);
123             MMI_HILOG_DISPATCHI("Touch event send cancel to window:%{public}d", windowId);
124         } else {
125             if (point->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) {
126                 MMI_HILOGE("Window:%{public}d is nullptr", windowId);
127             }
128             return false;
129         }
130     }
131     std::shared_ptr<WindowInfo> curWindowInfo = std::make_shared<WindowInfo>(*windowInfo);
132     if (point->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
133         if (cancelEventList_.find(pointerId) == cancelEventList_.end()) {
134             cancelEventList_[pointerId] = std::vector<std::shared_ptr<WindowInfo>>(0);
135         }
136         cancelEventList_[pointerId].push_back(curWindowInfo);
137     } else if (point->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
138         point->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL) {
139         if (cancelEventList_.find(pointerId) == cancelEventList_.end() ||
140             !SearchWindow(cancelEventList_[pointerId], curWindowInfo)) {
141             return false;
142         }
143     }
144     return true;
145 }
146 
SearchWindow(std::vector<std::shared_ptr<WindowInfo>> & windowList,std::shared_ptr<WindowInfo> targetWindow)147 bool EventDispatchHandler::SearchWindow(std::vector<std::shared_ptr<WindowInfo>> &windowList,
148     std::shared_ptr<WindowInfo> targetWindow)
149 {
150     for (auto &window : windowList) {
151         CHKPC(window);
152         if (window->id == targetWindow->id) {
153             return true;
154         }
155     }
156     return false;
157 }
158 
AddFlagToEsc(const std::shared_ptr<KeyEvent> keyEvent)159 void EventDispatchHandler::AddFlagToEsc(const std::shared_ptr<KeyEvent> keyEvent)
160 {
161     CHKPV(keyEvent);
162     MMI_HILOGD("add Flag to ESC in: %{public}s", keyEvent->ToString().c_str());
163     if (keyEvent->GetKeyCode() != KeyEvent::KEYCODE_ESCAPE) {
164         return;
165     }
166     if (!escToBackFlag_ && keyEvent->HasFlag(InputEvent::EVENT_FLAG_KEYBOARD_ESCAPE)) {
167         keyEvent->ClearFlag(InputEvent::EVENT_FLAG_KEYBOARD_ESCAPE);
168     }
169 
170     if (keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_DOWN) {
171         escToBackFlag_ = true;
172         return;
173     }
174 
175     if (escToBackFlag_ && (keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_UP ||
176         keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_CANCEL) &&
177         keyEvent->GetKeyItems().size() == 1) {
178         MMI_HILOGI("Only esc up or cancel has added flag: %{public}s", keyEvent->ToString().c_str());
179         keyEvent->AddFlag(InputEvent::EVENT_FLAG_KEYBOARD_ESCAPE);
180         escToBackFlag_ = false;
181     }
182 }
183 
HandleMultiWindowPointerEvent(std::shared_ptr<PointerEvent> point,PointerEvent::PointerItem pointerItem)184 void EventDispatchHandler::HandleMultiWindowPointerEvent(std::shared_ptr<PointerEvent> point,
185     PointerEvent::PointerItem pointerItem)
186 {
187     CALL_DEBUG_ENTER;
188     CHKPV(point);
189     std::vector<int32_t> windowIds;
190     WIN_MGR->GetTargetWindowIds(pointerItem.GetPointerId(), point->GetSourceType(), windowIds);
191     int32_t count = 0;
192     int32_t pointerId = point->GetPointerId();
193     if (point->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
194         if (cancelEventList_.find(pointerId) != cancelEventList_.end()) {
195             cancelEventList_.erase(pointerId);
196         }
197     }
198     for (auto windowId : windowIds) {
199         auto pointerEvent = std::make_shared<PointerEvent>(*point);
200         auto windowInfo = WIN_MGR->GetWindowAndDisplayInfo(windowId, point->GetTargetDisplayId());
201         if (!ReissueEvent(pointerEvent, windowId, windowInfo)) {
202             continue;
203         }
204         if (!windowInfo) {
205             continue;
206         }
207         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP &&
208             windowInfo->windowInputType == WindowInputType::TRANSMIT_ALL && windowIds.size() > 1) {
209             MMI_HILOGD("When the drag is finished, the multi-window distribution is canceled. window:%{public}d,"
210                 "windowInputType:%{public}d", windowId, static_cast<int32_t>(windowInfo->windowInputType));
211             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
212         }
213         auto fd = WIN_MGR->GetClientFd(pointerEvent, windowInfo->id);
214         if (fd < 0) {
215             auto udsServer = InputHandler->GetUDSServer();
216             CHKPV(udsServer);
217             fd = udsServer->GetClientFd(windowInfo->pid);
218             MMI_HILOGI("Window:%{public}d exit front desk, windowfd:%{public}d", windowId, fd);
219         }
220         pointerEvent->SetTargetWindowId(windowId);
221         pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
222         double windowX = pointerItem.GetDisplayXPos() - windowInfo->area.x;
223         double windowY = pointerItem.GetDisplayYPos() - windowInfo->area.y;
224         auto physicalDisplayInfo = WIN_MGR->GetPhysicalDisplay(windowInfo->displayId);
225         CHKPV(physicalDisplayInfo);
226         if (!windowInfo->transform.empty()) {
227             auto windowXY = WIN_MGR->TransformWindowXY(*windowInfo,
228                 pointerItem.GetDisplayXPos() + physicalDisplayInfo->x,
229                 pointerItem.GetDisplayYPos() + physicalDisplayInfo->y);
230             windowX = windowXY.first;
231             windowY = windowXY.second;
232         }
233         pointerItem.SetWindowX(static_cast<int32_t>(windowX));
234         pointerItem.SetWindowY(static_cast<int32_t>(windowY));
235         pointerItem.SetWindowXPos(windowX);
236         pointerItem.SetWindowYPos(windowY);
237         pointerItem.SetTargetWindowId(windowId);
238         pointerEvent->UpdatePointerItem(pointerId, pointerItem);
239         pointerEvent->SetDispatchTimes(count++);
240         DispatchPointerEventInner(pointerEvent, fd);
241     }
242     if (point->GetPointerAction() == PointerEvent::POINTER_ACTION_UP ||
243         point->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP ||
244         point->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL ||
245         point->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_THROW ||
246         point->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT) {
247         WIN_MGR->ClearTargetWindowId(pointerId);
248     }
249 }
250 
NotifyPointerEventToRS(int32_t pointAction,const std::string & programName,uint32_t pid,int32_t pointCnt)251 void EventDispatchHandler::NotifyPointerEventToRS(int32_t pointAction, const std::string& programName,
252     uint32_t pid, int32_t pointCnt)
253 {
254     (void)programName;
255     (void)pid;
256 #ifndef OHOS_BUILD_ENABLE_WATCH
257     auto begin = std::chrono::high_resolution_clock::now();
258     if (POINTER_DEV_MGR.isInit) {
259         CursorDrawingComponent::GetInstance().NotifyPointerEventToRS(pointAction, pointCnt);
260     }
261     auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
262         std::chrono::high_resolution_clock::now() - begin).count();
263 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
264     DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::RS_NOTIFY_TOUCH_EVENT, durationMS);
265 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
266 #endif // OHOS_BUILD_ENABLE_WATCH
267 }
268 
AcquireEnableMark(std::shared_ptr<PointerEvent> event)269 bool EventDispatchHandler::AcquireEnableMark(std::shared_ptr<PointerEvent> event)
270 {
271     auto currentEventTime = std::chrono::high_resolution_clock::now();
272     int64_t tm64Cost = std::chrono::duration_cast<std::chrono::milliseconds>(
273         std::chrono::high_resolution_clock::now() - LasteventBeginTime_).count();
274 
275     if (event->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_MOVE
276         || event->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE) {
277         enableMark_ = (tm64Cost > INTERVAL_DURATION) ? true : false;
278         if (enableMark_) {
279             LasteventBeginTime_ = currentEventTime;
280         }
281         MMI_HILOGD("Id:%{public}d, markEnabled:%{public}d", event->GetId(), enableMark_);
282         return enableMark_;
283     }
284     return true;
285 }
286 
SendWindowStateError(int32_t pid,int32_t windowId)287 void EventDispatchHandler::SendWindowStateError(int32_t pid, int32_t windowId)
288 {
289     CALL_DEBUG_ENTER;
290     auto udsServer = InputHandler->GetUDSServer();
291     CHKPV(udsServer);
292     auto sess = udsServer->GetSessionByPid(WIN_MGR->GetWindowStateNotifyPid());
293     if (sess != nullptr) {
294         NetPacket pkt(MmiMessageId::WINDOW_STATE_ERROR_NOTIFY);
295         pkt << pid << windowId;
296         if (!sess->SendMsg(pkt)) {
297             MMI_HILOGE("SendMsg failed");
298             return;
299         }
300         windowStateErrorInfo_.windowId = -1;
301         windowStateErrorInfo_.startTime = -1;
302         windowStateErrorInfo_.pid = -1;
303     }
304 }
305 
HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)306 void EventDispatchHandler::HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)
307 {
308     CALL_DEBUG_ENTER;
309     CHKPV(point);
310     int32_t pointerId = point->GetPointerId();
311     PointerEvent::PointerItem pointerItem;
312     if (!point->GetPointerItem(pointerId, pointerItem)) {
313         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
314         return;
315     }
316     UpdateDisplayXY(point);
317     std::vector<int32_t> windowIds;
318     WIN_MGR->GetTargetWindowIds(pointerItem.GetPointerId(), point->GetSourceType(), windowIds);
319     if (!windowIds.empty()) {
320         HandleMultiWindowPointerEvent(point, pointerItem);
321         ResetDisplayXY(point);
322         return;
323     }
324     auto pid = WIN_MGR->GetPidByWindowId(point->GetTargetWindowId());
325     int32_t fd = GetClientFd(pid, point);
326     auto udsServer = InputHandler->GetUDSServer();
327     if (udsServer == nullptr) {
328         ResetDisplayXY(point);
329         return;
330     }
331     if (WIN_MGR->GetCancelEventFlag(point) && udsServer->GetSession(fd) == nullptr &&
332         pid != -1 && point->GetTargetWindowId() != -1) {
333         if (point->GetTargetWindowId() == windowStateErrorInfo_.windowId && pid == windowStateErrorInfo_.pid) {
334             if (GetSysClockTime() - windowStateErrorInfo_.startTime >= ERROR_TIME) {
335                 SendWindowStateError(pid, point->GetTargetWindowId());
336             }
337         } else {
338             windowStateErrorInfo_.windowId = point->GetTargetWindowId();
339             windowStateErrorInfo_.startTime = GetSysClockTime();
340             windowStateErrorInfo_.pid = pid;
341         }
342     }
343     DispatchPointerEventInner(point, fd);
344     ResetDisplayXY(point);
345 }
346 
GetClientFd(int32_t pid,std::shared_ptr<PointerEvent> point)347 int32_t EventDispatchHandler::GetClientFd(int32_t pid, std::shared_ptr<PointerEvent> point)
348 {
349     CHKPR(point, INVALID_FD);
350     if (WIN_MGR->AdjustFingerFlag(point)) {
351         return INVALID_FD;
352     }
353     if (point->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL &&
354         point->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_CANCEL &&
355         (point->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
356         point->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) && (pid > 0)) {
357         WIN_MGR->FoldScreenRotation(point);
358         auto udsServer = InputHandler->GetUDSServer();
359         CHKPR(udsServer, INVALID_FD);
360         return udsServer->GetClientFd(pid);
361     }
362     return WIN_MGR->GetClientFd(point);
363 }
364 
UpdateDisplayXY(const std::shared_ptr<PointerEvent> & point)365 void EventDispatchHandler::UpdateDisplayXY(const std::shared_ptr<PointerEvent> &point)
366 {
367 #ifdef OHOS_BUILD_ENABLE_ONE_HAND_MODE
368     CHKPV(point);
369     int32_t pointerId = point->GetPointerId();
370     PointerEvent::PointerItem pointerItem;
371     if (!point->GetPointerItem(pointerId, pointerItem)) {
372         MMI_HILOGE("can't find pointer item, pointer:%{public}d", pointerId);
373         return;
374     }
375     int32_t targetDisplayId = point->GetTargetDisplayId();
376     int32_t targetWindowId = pointerItem.GetTargetWindowId();
377     std::optional<WindowInfo> opt = WIN_MGR->GetWindowAndDisplayInfo(targetWindowId, targetDisplayId);
378     if (opt && point->GetFixedMode() == PointerEvent::FixedMode::AUTO) {
379         WindowInputType windowInputType = opt.value().windowInputType;
380         if (windowInputType != WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE &&
381             windowInputType != WindowInputType::DUALTRIGGER_TOUCH &&
382             windowInputType != WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) {
383             currentXY_.x = pointerItem.GetDisplayXPos();
384             currentXY_.y = pointerItem.GetDisplayYPos();
385             pointerItem.SetDisplayX(pointerItem.GetFixedDisplayX());
386             pointerItem.SetDisplayY(pointerItem.GetFixedDisplayY());
387             pointerItem.SetDisplayXPos(pointerItem.GetFixedDisplayXPos());
388             pointerItem.SetDisplayYPos(pointerItem.GetFixedDisplayYPos());
389             point->UpdatePointerItem(pointerId, pointerItem);
390             currentXY_.fixed = true;
391         } else {
392             MMI_HILOGI("targetDisplayId=%{private}d, targetWindowId=%{private}d, windowInputType=%{private}d, "
393                 "not need to modify DX", targetDisplayId, targetWindowId, static_cast<int32_t>(windowInputType));
394         }
395     }
396 #endif
397 }
398 
ResetDisplayXY(const std::shared_ptr<PointerEvent> & point)399 void EventDispatchHandler::ResetDisplayXY(const std::shared_ptr<PointerEvent> &point)
400 {
401 #ifdef OHOS_BUILD_ENABLE_ONE_HAND_MODE
402     CHKPV(point);
403     int32_t pointerId = point->GetPointerId();
404     PointerEvent::PointerItem pointerItem;
405     if (!point->GetPointerItem(pointerId, pointerItem)) {
406         MMI_HILOGE("can't find pointer item, pointer:%{public}d", pointerId);
407         return;
408     }
409     if (point->GetFixedMode() == PointerEvent::FixedMode::AUTO && currentXY_.fixed) {
410         pointerItem.SetDisplayX(static_cast<int32_t>(currentXY_.x));
411         pointerItem.SetDisplayY(static_cast<int32_t>(currentXY_.y));
412         pointerItem.SetDisplayXPos(currentXY_.x);
413         pointerItem.SetDisplayYPos(currentXY_.y);
414         point->UpdatePointerItem(pointerId, pointerItem);
415         currentXY_.fixed = false;
416     }
417 #endif // OHOS_BUILD_ENABLE_ONE_HAND_MODE
418 }
419 
DispatchPointerEventInner(std::shared_ptr<PointerEvent> point,int32_t fd)420 void EventDispatchHandler::DispatchPointerEventInner(std::shared_ptr<PointerEvent> point, int32_t fd)
421 {
422     currentTime_ = point->GetActionTime();
423     if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
424         eventTime_ = currentTime_;
425         if (point->GetPointerCount() < THREE_FINGERS &&
426             point->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
427             point->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
428             point->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) {
429             MMI_HILOGE("InputTracking id:%{public}d The fd less than 0, fd:%{public}d", point->GetId(), fd);
430         }
431         return;
432     }
433     auto udsServer = InputHandler->GetUDSServer();
434     CHKPV(udsServer);
435     auto sess = udsServer->GetSession(fd);
436     if (sess == nullptr) {
437         return;
438     }
439     auto currentTime = GetSysClockTime();
440     BytraceAdapter::StartBytrace(point, BytraceAdapter::TRACE_STOP);
441     if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, sess)) {
442         bool isTrue = (point->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) ||
443             (point->GetPointerAction() == PointerEvent::POINTER_ACTION_UP) ||
444             (point->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) ||
445             (point->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) ||
446             (point->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN) ||
447             (point->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END);
448         if (isTrue) {
449             MMI_HILOGE("The pointer event does not report normally,app not respon. PointerEvent(deviceid:%{public}d,"
450                 "action:%{public}d)", point->GetDeviceId(), point->GetPointerAction());
451         }
452         MMI_HILOGD("The pointer event does not report normally,app not respon. PointerEvent(deviceid:%{public}d,"
453             "action:%{public}s)", point->GetDeviceId(), point->DumpPointerAction());
454         ANRMgr->HandleAnrState(sess, ANR_DISPATCH, currentTime);
455     }
456     auto pointerEvent = std::make_shared<PointerEvent>(*point);
457     pointerEvent->SetMarkEnabled(AcquireEnableMark(pointerEvent));
458     pointerEvent->SetSensorInputTime(point->GetSensorInputTime());
459     FilterInvalidPointerItem(pointerEvent, fd);
460     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
461     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
462 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
463     InputEventDataTransformation::MarshallingEnhanceData(pointerEvent, pkt);
464 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
465     int32_t pointerAc = pointerEvent->GetPointerAction();
466     NotifyPointerEventToRS(pointerAc, sess->GetProgramName(),
467         static_cast<uint32_t>(sess->GetPid()), pointerEvent->GetPointerCount());
468     if (pointerAc != PointerEvent::POINTER_ACTION_MOVE && pointerAc != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
469         pointerAc != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
470         pointerAc != PointerEvent::POINTER_ACTION_PULL_MOVE) {
471         MMI_HILOG_FREEZEI("SendMsg:%{public}d", sess->GetPid());
472     }
473     WIN_MGR->PrintEnterEventInfo(pointerEvent);
474     if (!udsServer->SendMsg(fd, pkt)) {
475         MMI_HILOGE("Sending structure of EventTouch failed! errCode:%{public}d", MSG_SEND_FAIL);
476         return;
477     }
478     if (sess->GetPid() != AppDebugListener::GetInstance()->GetAppDebugPid() && pointerEvent->IsMarkEnabled()) {
479         MMI_HILOGD("Session pid:%{public}d", sess->GetPid());
480         ANRMgr->AddTimer(ANR_DISPATCH, point->GetId(), currentTime, sess);
481     }
482 }
483 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_POINTER
484 
485 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
DispatchKeyEventPid(UDSServer & udsServer,std::shared_ptr<KeyEvent> key)486 int32_t EventDispatchHandler::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key)
487 {
488     CALL_DEBUG_ENTER;
489     CHKPR(key, PARAM_INPUT_INVALID);
490     int32_t ret = RET_OK;
491     // 1.Determine whether the key event is a focus type event or an operation type event,
492     // 2.Determine whether the current focus window has a safety sub window.
493     auto secSubWindowTargets = WIN_MGR->UpdateTarget(key);
494     for (const auto &item : secSubWindowTargets) {
495         key->ClearFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
496         if (item.second.privacyMode == SecureFlag::PRIVACY_MODE) {
497             key->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
498         }
499         key->SetTargetWindowId(item.second.id);
500         key->SetAgentWindowId(item.second.agentWindowId);
501         ret = DispatchKeyEvent(item.first, udsServer, key);
502     }
503     return ret;
504 }
505 
DispatchKeyEvent(int32_t fd,UDSServer & udsServer,std::shared_ptr<KeyEvent> key)506 int32_t EventDispatchHandler::DispatchKeyEvent(int32_t fd, UDSServer& udsServer, std::shared_ptr<KeyEvent> key)
507 {
508     CALL_DEBUG_ENTER;
509     CHKPR(key, PARAM_INPUT_INVALID);
510     currentTime_ = key->GetActionTime();
511     if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
512         eventTime_ = currentTime_;
513         MMI_HILOGE("Invalid fd, fd:%{public}d", fd);
514         DfxHisysevent::OnUpdateTargetKey(key, fd, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
515         return RET_ERR;
516     }
517     MMI_HILOGD("Event dispatcher of server, KeyEvent:KeyCode:%{private}d, Action:%{public}d, EventType:%{public}d,"
518         "Fd:%{public}d", key->GetKeyCode(), key->GetAction(), key->GetEventType(), fd);
519     auto session = udsServer.GetSession(fd);
520     CHKPR(session, RET_ERR);
521     auto currentTime = GetSysClockTime();
522     if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, session)) {
523         if (!EventLogHelper::IsBetaVersion()) {
524             MMI_HILOGW("The key event does not report normally, application not response."
525                 "KeyEvent(deviceid:%{public}d, key action:%{public}d)",
526                 key->GetDeviceId(), key->GetKeyAction());
527         } else {
528             MMI_HILOGW("The key event does not report normally, application not response."
529                 "KeyEvent(deviceid:%{public}d, keycode:%{private}d, key action:%{public}d)",
530                 key->GetDeviceId(), key->GetKeyCode(), key->GetKeyAction());
531         }
532         ANRMgr->HandleAnrState(session, ANR_DISPATCH, currentTime);
533     }
534     auto keyHandler = InputHandler->GetEventNormalizeHandler();
535     CHKPR(keyHandler, RET_ERR);
536     if (key->GetKeyCode() != keyHandler->GetCurrentHandleKeyCode()) {
537         MMI_HILOGW("Keycode has been changed");
538     }
539     NetPacket pkt(MmiMessageId::ON_KEY_EVENT);
540     InputEventDataTransformation::KeyEventToNetPacket(key, pkt);
541     BytraceAdapter::StartBytrace(key, BytraceAdapter::KEY_DISPATCH_EVENT);
542     pkt << fd;
543 
544 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
545     InputEventDataTransformation::MarshallingEnhanceData(key, pkt);
546 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
547     if (pkt.ChkRWError()) {
548         MMI_HILOGE("Packet write structure of EventKeyboard failed");
549         return RET_ERR;
550     }
551     MMI_HILOGD("InputTracking id:%{public}d, SendMsg to %{public}s:pid:%{public}d",
552         key->GetId(), session->GetProgramName().c_str(), session->GetPid());
553     if (!udsServer.SendMsg(fd, pkt)) {
554         MMI_HILOGE("Sending structure of EventKeyboard failed! errCode:%{public}d", MSG_SEND_FAIL);
555         return MSG_SEND_FAIL;
556     }
557     if (session->GetPid() != AppDebugListener::GetInstance()->GetAppDebugPid()) {
558         MMI_HILOGD("Session pid:%{public}d", session->GetPid());
559         ANRMgr->AddTimer(ANR_DISPATCH, key->GetId(), currentTime, session);
560     }
561     return RET_OK;
562 }
563 #endif // OHOS_BUILD_ENABLE_KEYBOARD
564 
565 #ifdef OHOS_BUILD_ENABLE_POINTER
EnsureMouseEventCycle(std::shared_ptr<PointerEvent> event)566 void EventDispatchHandler::EnsureMouseEventCycle(std::shared_ptr<PointerEvent> event)
567 {
568     WIN_MGR->EnsureMouseEventCycle(event);
569 }
570 
CleanMouseEventCycle(std::shared_ptr<PointerEvent> event)571 void EventDispatchHandler::CleanMouseEventCycle(std::shared_ptr<PointerEvent> event)
572 {
573     WIN_MGR->CleanMouseEventCycle(event);
574 }
575 #endif // OHOS_BUILD_ENABLE_POINTER
576 } // namespace MMI
577 } // namespace OHOS
578