• 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 <cinttypes>
19 
20 #include "dfx_hisysevent.h"
21 #include "hitrace_meter.h"
22 
23 #include "anr_manager.h"
24 #include "app_debug_listener.h"
25 #include "bytrace_adapter.h"
26 #include "error_multimodal.h"
27 #include "input_event_data_transformation.h"
28 #include "input_event_handler.h"
29 #include "input_windows_manager.h"
30 #include "input-event-codes.h"
31 #include "mouse_device_state.h"
32 #include "napi_constants.h"
33 #include "proto.h"
34 #include "util.h"
35 
36 namespace OHOS {
37 namespace MMI {
38 namespace {
39 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) || defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
40 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventDispatchHandler" };
41 #endif // OHOS_BUILD_ENABLE_KEYBOARD ||  OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
42 constexpr int32_t INTERVAL_TIME = 3000; // log time interval is 3 seconds.
43 } // namespace
44 
45 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)46 void EventDispatchHandler::HandleKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
47 {
48     CHKPV(keyEvent);
49     auto udsServer = InputHandler->GetUDSServer();
50     CHKPV(udsServer);
51     DispatchKeyEventPid(*udsServer, keyEvent);
52 }
53 #endif // OHOS_BUILD_ENABLE_KEYBOARD
54 
55 #ifdef OHOS_BUILD_ENABLE_POINTER
HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)56 void EventDispatchHandler::HandlePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
57 {
58     CHKPV(pointerEvent);
59     HandlePointerEventInner(pointerEvent);
60 }
61 #endif // OHOS_BUILD_ENABLE_POINTER
62 
63 #ifdef OHOS_BUILD_ENABLE_TOUCH
HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)64 void EventDispatchHandler::HandleTouchEvent(const std::shared_ptr<PointerEvent> pointerEvent)
65 {
66     CHKPV(pointerEvent);
67     HandlePointerEventInner(pointerEvent);
68 }
69 #endif // OHOS_BUILD_ENABLE_TOUCH
70 
71 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointerEvent,int32_t fd)72 void EventDispatchHandler::FilterInvalidPointerItem(const std::shared_ptr<PointerEvent> pointerEvent, int32_t fd)
73 {
74     CHKPV(pointerEvent);
75     auto udsServer = InputHandler->GetUDSServer();
76     CHKPV(udsServer);
77     auto pointerIdList = pointerEvent->GetPointerIds();
78     if (pointerIdList.size() > 1) {
79         for (const auto& id : pointerIdList) {
80             PointerEvent::PointerItem pointeritem;
81             if (!pointerEvent->GetPointerItem(id, pointeritem)) {
82                 MMI_HILOGW("Can't find this pointerItem");
83                 continue;
84             }
85             auto itemPid = WinMgr->GetWindowPid(pointeritem.GetTargetWindowId());
86             if ((itemPid >= 0) && (itemPid != udsServer->GetClientPid(fd))) {
87                 pointerEvent->RemovePointerItem(id);
88                 MMI_HILOGD("pointerIdList size:%{public}zu", pointerEvent->GetPointerIds().size());
89             }
90         }
91     }
92 }
93 
HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)94 void EventDispatchHandler::HandlePointerEventInner(const std::shared_ptr<PointerEvent> point)
95 {
96     CALL_DEBUG_ENTER;
97     CHKPV(point);
98     auto fd = WinMgr->GetClientFd(point);
99     currentTime_ = point->GetActionTime();
100     if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
101         eventTime_ = currentTime_;
102         MMI_HILOGE("InputTracking id:%{public}d The fd less than 0, fd:%{public}d", point->GetId(), fd);
103         DfxHisysevent::OnUpdateTargetPointer(point, fd, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
104         return;
105     }
106     auto udsServer = InputHandler->GetUDSServer();
107     CHKPV(udsServer);
108     auto session = udsServer->GetSession(fd);
109     CHKPV(session);
110     auto currentTime = GetSysClockTime();
111     if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, session)) {
112         MMI_HILOGW("InputTracking id:%{public}d, The pointer event does not report normally,"
113             "application not response", point->GetId());
114         return;
115     }
116     auto pointerEvent = std::make_shared<PointerEvent>(*point);
117     pointerEvent->SetSensorInputTime(point->GetSensorInputTime());
118     FilterInvalidPointerItem(pointerEvent, fd);
119     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
120     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
121 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
122     InputEventDataTransformation::MarshallingEnhanceData(pointerEvent, pkt);
123 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
124     BytraceAdapter::StartBytrace(point, BytraceAdapter::TRACE_STOP);
125     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE) {
126         MMI_HILOGI("InputTracking id:%{public}d, SendMsg to %{public}s:pid:%{public}d",
127             pointerEvent->GetId(), session->GetProgramName().c_str(), session->GetPid());
128     }
129     if (!udsServer->SendMsg(fd, pkt)) {
130         MMI_HILOGE("Sending structure of EventTouch failed! errCode:%{public}d", MSG_SEND_FAIL);
131         return;
132     }
133     if (session->GetPid() != AppDebugListener::GetInstance()->GetAppDebugPid()) {
134         MMI_HILOGD("session pid : %{public}d", session->GetPid());
135         ANRMgr->AddTimer(ANR_DISPATCH, point->GetId(), currentTime, session);
136     }
137 }
138 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_POINTER
139 
140 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
DispatchKeyEventPid(UDSServer & udsServer,std::shared_ptr<KeyEvent> key)141 int32_t EventDispatchHandler::DispatchKeyEventPid(UDSServer& udsServer, std::shared_ptr<KeyEvent> key)
142 {
143     CALL_DEBUG_ENTER;
144     CHKPR(key, PARAM_INPUT_INVALID);
145     auto fd = WinMgr->UpdateTarget(key);
146     currentTime_ = key->GetActionTime();
147     if (fd < 0 && currentTime_ - eventTime_ > INTERVAL_TIME) {
148         eventTime_ = currentTime_;
149         MMI_HILOGE("Invalid fd, fd:%{public}d", fd);
150         DfxHisysevent::OnUpdateTargetKey(key, fd, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
151         return RET_ERR;
152     }
153     MMI_HILOGD("Event dispatcher of server:KeyEvent:KeyCode:%{public}d,Action:%{public}d,EventType:%{public}d,"
154         "Fd:%{public}d", key->GetKeyCode(), key->GetAction(), key->GetEventType(), fd);
155     auto session = udsServer.GetSession(fd);
156     CHKPR(session, RET_ERR);
157     auto currentTime = GetSysClockTime();
158     if (ANRMgr->TriggerANR(ANR_DISPATCH, currentTime, session)) {
159         MMI_HILOGW("The key event does not report normally, application not response");
160         return RET_OK;
161     }
162 
163     NetPacket pkt(MmiMessageId::ON_KEY_EVENT);
164     InputEventDataTransformation::KeyEventToNetPacket(key, pkt);
165     BytraceAdapter::StartBytrace(key, BytraceAdapter::KEY_DISPATCH_EVENT);
166     pkt << fd;
167 
168 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
169     InputEventDataTransformation::MarshallingEnhanceData(key, pkt);
170 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
171     if (pkt.ChkRWError()) {
172         MMI_HILOGE("Packet write structure of EventKeyboard failed");
173         return RET_ERR;
174     }
175     MMI_HILOGI("InputTracking id:%{public}d, SendMsg to %{public}s:pid:%{public}d",
176         key->GetId(), session->GetProgramName().c_str(), session->GetPid());
177     if (!udsServer.SendMsg(fd, pkt)) {
178         MMI_HILOGE("Sending structure of EventKeyboard failed! errCode:%{public}d", MSG_SEND_FAIL);
179         return MSG_SEND_FAIL;
180     }
181     if (session->GetPid() != AppDebugListener::GetInstance()->GetAppDebugPid()) {
182         MMI_HILOGD("session pid : %{public}d", session->GetPid());
183         ANRMgr->AddTimer(ANR_DISPATCH, key->GetId(), currentTime, session);
184     }
185     return RET_OK;
186 }
187 #endif // OHOS_BUILD_ENABLE_KEYBOARD
188 } // namespace MMI
189 } // namespace OHOS
190