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