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