1 /*
2 * Copyright (c) 2021-2022 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 <cinttypes>
19
20 #include "dfx_hisysevent.h"
21 #include "hitrace_meter.h"
22
23 #include "anr_manager.h"
24 #include "bytrace_adapter.h"
25 #include "error_multimodal.h"
26 #include "input_event_data_transformation.h"
27 #include "input_event_handler.h"
28 #include "input_windows_manager.h"
29 #include "input-event-codes.h"
30 #include "mouse_device_state.h"
31 #include "napi_constants.h"
32 #include "proto.h"
33 #include "util.h"
34
35 namespace OHOS {
36 namespace MMI {
37 namespace {
38 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) || defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
39 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventDispatchHandler" };
40 #endif // OHOS_BUILD_ENABLE_KEYBOARD || OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
41 constexpr int32_t INTERVAL_TIME = 3000; // log time interval is 3 seconds.
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 = WinMgr->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
HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)93 void EventDispatchHandler::HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)
94 {
95 CALL_DEBUG_ENTER;
96 CHKPV(point);
97 auto fd = WinMgr->GetClientFd(point);
98 currentTime_ = point->GetActionTime();
99 if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
100 eventTime_ = currentTime_;
101 MMI_HILOGE("The fd less than 0, fd:%{public}d", fd);
102 DfxHisysevent::OnUpdateTargetPointer(point, fd, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
103 return;
104 }
105 auto udsServer = InputHandler->GetUDSServer();
106 CHKPV(udsServer);
107 auto session = udsServer->GetSession(fd);
108 CHKPV(session);
109 auto currentTime = GetSysClockTime();
110 if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, session)) {
111 MMI_HILOGD("The pointer event does not report normally, application not response");
112 return;
113 }
114 auto pointerEvent = std::make_shared<PointerEvent>(*point);
115 pointerEvent->SetSensorInputTime(point->GetSensorInputTime());
116 FilterInvalidPointerItem(pointerEvent, fd);
117 NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
118 InputEventDataTransformation::Marshalling(pointerEvent, pkt);
119 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
120 InputEventDataTransformation::MarshallingEnhanceData(pointerEvent, pkt);
121 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
122 BytraceAdapter::StartBytrace(point, BytraceAdapter::TRACE_STOP);
123 if (!udsServer->SendMsg(fd, pkt)) {
124 MMI_HILOGE("Sending structure of EventTouch failed! errCode:%{public}d", MSG_SEND_FAIL);
125 return;
126 }
127 ANRMgr->AddTimer(ANR_DISPATCH, point->GetId(), currentTime, session);
128 }
129 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_POINTER
130
131 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
DispatchKeyEventPid(UDSServer & udsServer,std::shared_ptr<KeyEvent> key)132 int32_t EventDispatchHandler::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key)
133 {
134 CALL_DEBUG_ENTER;
135 CHKPR(key, PARAM_INPUT_INVALID);
136 auto fd = WinMgr->UpdateTarget(key);
137 currentTime_ = key->GetActionTime();
138 if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
139 eventTime_ = currentTime_;
140 MMI_HILOGE("Invalid fd, fd:%{public}d", fd);
141 DfxHisysevent::OnUpdateTargetKey(key, fd, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
142 return RET_ERR;
143 }
144 MMI_HILOGD("Event dispatcher of server:KeyEvent:KeyCode:%{public}d,Action:%{public}d,EventType:%{public}d,"
145 "Fd:%{public}d", key->GetKeyCode(), key->GetAction(), key->GetEventType(), fd);
146 auto session = udsServer.GetSession(fd);
147 CHKPR(session, RET_ERR);
148 auto currentTime = GetSysClockTime();
149 if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, session)) {
150 MMI_HILOGW("The key event does not report normally, application not response");
151 return RET_OK;
152 }
153
154 NetPacket pkt(MmiMessageId::ON_KEY_EVENT);
155 InputEventDataTransformation::KeyEventToNetPacket(key, pkt);
156 BytraceAdapter::StartBytrace(key, BytraceAdapter::KEY_DISPATCH_EVENT);
157 pkt << fd;
158 if (pkt.ChkRWError()) {
159 MMI_HILOGE("Packet write structure of EventKeyboard failed");
160 return RET_ERR;
161 }
162 if (!udsServer.SendMsg(fd, pkt)) {
163 MMI_HILOGE("Sending structure of EventKeyboard failed! errCode:%{public}d", MSG_SEND_FAIL);
164 return MSG_SEND_FAIL;
165 }
166 ANRMgr->AddTimer(ANR_DISPATCH, key->GetId(), currentTime, session);
167 return RET_OK;
168 }
169 #endif // OHOS_BUILD_ENABLE_KEYBOARD
170 } // namespace MMI
171 } // namespace OHOS
172