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 "server_msg_handler.h"
17
18 #include <cinttypes>
19
20 #include "anr_manager.h"
21 #include "event_dump.h"
22 #include "event_interceptor_handler.h"
23 #include "event_monitor_handler.h"
24 #include "hos_key_event.h"
25 #include "input_device_manager.h"
26 #include "input_event_data_transformation.h"
27 #include "input_event_handler.h"
28 #include "input_event.h"
29 #include "input_windows_manager.h"
30 #include "key_event_normalize.h"
31 #include "i_pointer_drawing_manager.h"
32 #include "key_subscriber_handler.h"
33 #include "libinput_adapter.h"
34 #include "mmi_func_callback.h"
35 #include "mouse_event_normalize.h"
36 #include "time_cost_chk.h"
37
38 namespace OHOS {
39 namespace MMI {
40 namespace {
41 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ServerMsgHandler" };
42 } // namespace
43
ServerMsgHandler()44 ServerMsgHandler::ServerMsgHandler() {}
45
~ServerMsgHandler()46 ServerMsgHandler::~ServerMsgHandler() {}
47
Init(UDSServer & udsServer)48 void ServerMsgHandler::Init(UDSServer& udsServer)
49 {
50 udsServer_ = &udsServer;
51 MsgCallback funs[] = {
52 {MmiMessageId::MARK_PROCESS, MsgCallbackBind2(&ServerMsgHandler::MarkProcessed, this)},
53 {MmiMessageId::DISPLAY_INFO, MsgCallbackBind2(&ServerMsgHandler::OnDisplayInfo, this)},
54 };
55 for (auto &it : funs) {
56 if (!RegistrationEvent(it)) {
57 MMI_HILOGW("Failed to register event errCode:%{public}d", EVENT_REG_FAIL);
58 continue;
59 }
60 }
61 }
62
OnMsgHandler(SessionPtr sess,NetPacket & pkt)63 void ServerMsgHandler::OnMsgHandler(SessionPtr sess, NetPacket& pkt)
64 {
65 CHKPV(sess);
66 auto id = pkt.GetMsgId();
67 TimeCostChk chk("ServerMsgHandler::OnMsgHandler", "overtime 300(us)", MAX_OVER_TIME, id);
68 auto callback = GetMsgCallback(id);
69 if (callback == nullptr) {
70 MMI_HILOGE("Unknown msg id:%{public}d,errCode:%{public}d", id, UNKNOWN_MSG_ID);
71 return;
72 }
73 auto ret = (*callback)(sess, pkt);
74 if (ret < 0) {
75 MMI_HILOGE("Msg handling failed. id:%{public}d,errCode:%{public}d", id, ret);
76 }
77 }
78
MarkProcessed(SessionPtr sess,NetPacket & pkt)79 int32_t ServerMsgHandler::MarkProcessed(SessionPtr sess, NetPacket& pkt)
80 {
81 CALL_DEBUG_ENTER;
82 CHKPR(sess, ERROR_NULL_POINTER);
83 int32_t eventId = 0;
84 int32_t eventType = 0;
85 pkt >> eventId >> eventType;
86 MMI_HILOGD("Event type:%{public}d, id:%{public}d", eventType, eventId);
87 if (pkt.ChkRWError()) {
88 MMI_HILOGE("Packet read data failed");
89 return PACKET_READ_FAIL;
90 }
91 ANRMgr->MarkProcessed(eventType, eventId, sess);
92 return RET_OK;
93 }
94
95 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)96 int32_t ServerMsgHandler::OnInjectKeyEvent(const std::shared_ptr<KeyEvent> keyEvent)
97 {
98 CALL_INFO_TRACE;
99 CHKPR(keyEvent, ERROR_NULL_POINTER);
100 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
101 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
102 inputEventNormalizeHandler->HandleKeyEvent(keyEvent);
103 MMI_HILOGD("Inject keyCode:%{public}d, action:%{public}d", keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
104 return RET_OK;
105 }
106
OnGetFunctionKeyState(int32_t funcKey,bool & state)107 int32_t ServerMsgHandler::OnGetFunctionKeyState(int32_t funcKey, bool &state)
108 {
109 CALL_INFO_TRACE;
110 const auto &keyEvent = KeyEventHdr->GetKeyEvent();
111 CHKPR(keyEvent, ERROR_NULL_POINTER);
112 state = keyEvent->GetFunctionKey(funcKey);
113 MMI_HILOGD("Get the function key:%{public}d status as %{public}s", funcKey, state ? "open" : "close");
114 return RET_OK;
115 }
116
OnSetFunctionKeyState(int32_t funcKey,bool enable)117 int32_t ServerMsgHandler::OnSetFunctionKeyState(int32_t funcKey, bool enable)
118 {
119 CALL_INFO_TRACE;
120 auto device = InputDevMgr->GetKeyboardDevice();
121 CHKPR(device, ERROR_NULL_POINTER);
122 if (LibinputAdapter::DeviceLedUpdate(device, funcKey, enable) != RET_OK) {
123 MMI_HILOGE("Failed to set the keyboard led");
124 return RET_ERR;
125 }
126 int32_t state = libinput_get_funckey_state(device, funcKey);
127
128 auto keyEvent = KeyEventHdr->GetKeyEvent();
129 CHKPR(keyEvent, ERROR_NULL_POINTER);
130 int32_t ret = keyEvent->SetFunctionKey(funcKey, state);
131 if (ret != funcKey) {
132 MMI_HILOGE("Failed to enable the function key");
133 return RET_ERR;
134 }
135 MMI_HILOGD("Update function key:%{public}d succeed", funcKey);
136 return RET_OK;
137 }
138 #endif // OHOS_BUILD_ENABLE_KEYBOARD
139
140 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)141 int32_t ServerMsgHandler::OnInjectPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent)
142 {
143 CALL_INFO_TRACE;
144 CHKPR(pointerEvent, ERROR_NULL_POINTER);
145 pointerEvent->UpdateId();
146 int32_t action = pointerEvent->GetPointerAction();
147 auto source = pointerEvent->GetSourceType();
148 switch (source) {
149 case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
150 #ifdef OHOS_BUILD_ENABLE_TOUCH
151 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
152 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
153 if (!FixTargetWindowId(pointerEvent, action)) {
154 return RET_ERR;
155 }
156 inputEventNormalizeHandler->HandleTouchEvent(pointerEvent);
157 #endif // OHOS_BUILD_ENABLE_TOUCH
158 break;
159 }
160 case PointerEvent::SOURCE_TYPE_MOUSE:
161 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
162 case PointerEvent::SOURCE_TYPE_JOYSTICK:
163 #endif // OHOS_BUILD_ENABLE_JOYSTICK
164 case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
165 #ifdef OHOS_BUILD_ENABLE_POINTER
166 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
167 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
168 if (!IPointerDrawingManager::GetInstance()->IsPointerVisible()) {
169 IPointerDrawingManager::GetInstance()->SetPointerVisible(getpid(), true);
170 }
171 inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
172 #endif // OHOS_BUILD_ENABLE_POINTER
173 break;
174 }
175 default: {
176 MMI_HILOGW("Source type is unknown, source:%{public}d", source);
177 break;
178 }
179 }
180 if (source == PointerEvent::SOURCE_TYPE_TOUCHSCREEN && action == PointerEvent::POINTER_ACTION_DOWN) {
181 targetWindowId_ = pointerEvent->GetTargetWindowId();
182 }
183 return RET_OK;
184 }
185 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
186
187 #ifdef OHOS_BUILD_ENABLE_TOUCH
FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent,int32_t action)188 bool ServerMsgHandler::FixTargetWindowId(std::shared_ptr<PointerEvent> pointerEvent, int32_t action)
189 {
190 if (action == PointerEvent::POINTER_ACTION_DOWN || targetWindowId_ < 0) {
191 MMI_HILOGD("Down event or targetWindowId_ less 0 is not need fix window id");
192 return true;
193 }
194 pointerEvent->SetTargetWindowId(targetWindowId_);
195 PointerEvent::PointerItem pointerItem;
196 auto pointerIds = pointerEvent->GetPointerIds();
197 if (pointerIds.empty()) {
198 MMI_HILOGE("GetPointerIds is empty");
199 return false;
200 }
201 auto id = pointerIds.front();
202 if (!pointerEvent->GetPointerItem(id, pointerItem)) {
203 MMI_HILOGE("Can't find pointer item");
204 return false;
205 }
206 pointerItem.SetTargetWindowId(targetWindowId_);
207 pointerEvent->UpdatePointerItem(id, pointerItem);
208 return true;
209 }
210 #endif // OHOS_BUILD_ENABLE_TOUCH
211
OnDisplayInfo(SessionPtr sess,NetPacket & pkt)212 int32_t ServerMsgHandler::OnDisplayInfo(SessionPtr sess, NetPacket &pkt)
213 {
214 CALL_DEBUG_ENTER;
215 CHKPR(sess, ERROR_NULL_POINTER);
216 DisplayGroupInfo displayGroupInfo;
217 pkt >> displayGroupInfo.width >> displayGroupInfo.height >> displayGroupInfo.focusWindowId;
218 uint32_t num = 0;
219 pkt >> num;
220 if (pkt.ChkRWError()) {
221 MMI_HILOGE("Packet read display info failed");
222 return RET_ERR;
223 }
224 for (uint32_t i = 0; i < num; i++) {
225 WindowInfo info;
226 pkt >> info.id >> info.pid >> info.uid >> info.area >> info.defaultHotAreas
227 >> info.pointerHotAreas >> info.agentWindowId >> info.flags;
228 displayGroupInfo.windowsInfo.push_back(info);
229 if (pkt.ChkRWError()) {
230 MMI_HILOGE("Packet read display info failed");
231 return RET_ERR;
232 }
233 }
234 pkt >> num;
235 for (uint32_t i = 0; i < num; i++) {
236 DisplayInfo info;
237 pkt >> info.id >> info.x >> info.y >> info.width >> info.height
238 >> info.name >> info.uniq >> info.direction;
239 displayGroupInfo.displaysInfo.push_back(info);
240 if (pkt.ChkRWError()) {
241 MMI_HILOGE("Packet read display info failed");
242 return RET_ERR;
243 }
244 }
245 if (pkt.ChkRWError()) {
246 MMI_HILOGE("Packet read display info failed");
247 return RET_ERR;
248 }
249 WinMgr->UpdateDisplayInfo(displayGroupInfo);
250 return RET_OK;
251 }
252
253 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnAddInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType)254 int32_t ServerMsgHandler::OnAddInputHandler(SessionPtr sess, InputHandlerType handlerType,
255 HandleEventType eventType)
256 {
257 CHKPR(sess, ERROR_NULL_POINTER);
258 MMI_HILOGD("handlerType:%{public}d", handlerType);
259 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
260 if (handlerType == InputHandlerType::INTERCEPTOR) {
261 auto interceptorHandler = InputHandler->GetInterceptorHandler();
262 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
263 return interceptorHandler->AddInputHandler(handlerType, eventType, sess);
264 }
265 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
266 #ifdef OHOS_BUILD_ENABLE_MONITOR
267 if (handlerType == InputHandlerType::MONITOR) {
268 auto monitorHandler = InputHandler->GetMonitorHandler();
269 CHKPR(monitorHandler, ERROR_NULL_POINTER);
270 return monitorHandler->AddInputHandler(handlerType, eventType, sess);
271 }
272 #endif // OHOS_BUILD_ENABLE_MONITOR
273 return RET_OK;
274 }
275
OnRemoveInputHandler(SessionPtr sess,InputHandlerType handlerType,HandleEventType eventType)276 int32_t ServerMsgHandler::OnRemoveInputHandler(SessionPtr sess, InputHandlerType handlerType,
277 HandleEventType eventType)
278 {
279 CHKPR(sess, ERROR_NULL_POINTER);
280 MMI_HILOGD("OnRemoveInputHandler handlerType:%{public}d eventType:%{public}u", handlerType, eventType);
281 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
282 if (handlerType == InputHandlerType::INTERCEPTOR) {
283 auto interceptorHandler = InputHandler->GetInterceptorHandler();
284 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
285 interceptorHandler->RemoveInputHandler(handlerType, eventType, sess);
286 }
287 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
288 #ifdef OHOS_BUILD_ENABLE_MONITOR
289 if (handlerType == InputHandlerType::MONITOR) {
290 auto monitorHandler = InputHandler->GetMonitorHandler();
291 CHKPR(monitorHandler, ERROR_NULL_POINTER);
292 monitorHandler->RemoveInputHandler(handlerType, eventType, sess);
293 }
294 #endif // OHOS_BUILD_ENABLE_MONITOR
295 return RET_OK;
296 }
297 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
298
299 #ifdef OHOS_BUILD_ENABLE_MONITOR
OnMarkConsumed(SessionPtr sess,int32_t eventId)300 int32_t ServerMsgHandler::OnMarkConsumed(SessionPtr sess, int32_t eventId)
301 {
302 CHKPR(sess, ERROR_NULL_POINTER);
303 auto monitorHandler = InputHandler->GetMonitorHandler();
304 CHKPR(monitorHandler, ERROR_NULL_POINTER);
305 monitorHandler->MarkConsumed(eventId, sess);
306 return RET_OK;
307 }
308 #endif // OHOS_BUILD_ENABLE_MONITOR
309
310 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
OnMoveMouse(int32_t offsetX,int32_t offsetY)311 int32_t ServerMsgHandler::OnMoveMouse(int32_t offsetX, int32_t offsetY)
312 {
313 CALL_DEBUG_ENTER;
314 if (MouseEventHdr->NormalizeMoveMouse(offsetX, offsetY)) {
315 auto pointerEvent = MouseEventHdr->GetPointerEvent();
316 CHKPR(pointerEvent, ERROR_NULL_POINTER);
317 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
318 CHKPR(inputEventNormalizeHandler, ERROR_NULL_POINTER);
319 inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
320 MMI_HILOGD("Mouse movement message processed successfully");
321 }
322 return RET_OK;
323 }
324 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
325
326 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
OnSubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId,const std::shared_ptr<KeyOption> option)327 int32_t ServerMsgHandler::OnSubscribeKeyEvent(IUdsServer *server, int32_t pid,
328 int32_t subscribeId, const std::shared_ptr<KeyOption> option)
329 {
330 CALL_DEBUG_ENTER;
331 CHKPR(server, ERROR_NULL_POINTER);
332 auto sess = server->GetSessionByPid(pid);
333 CHKPR(sess, ERROR_NULL_POINTER);
334 auto subscriberHandler = InputHandler->GetSubscriberHandler();
335 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
336 return subscriberHandler->SubscribeKeyEvent(sess, subscribeId, option);
337 }
338
OnUnsubscribeKeyEvent(IUdsServer * server,int32_t pid,int32_t subscribeId)339 int32_t ServerMsgHandler::OnUnsubscribeKeyEvent(IUdsServer *server, int32_t pid, int32_t subscribeId)
340 {
341 CALL_DEBUG_ENTER;
342 CHKPR(server, ERROR_NULL_POINTER);
343 auto sess = server->GetSessionByPid(pid);
344 CHKPR(sess, ERROR_NULL_POINTER);
345 auto subscriberHandler = InputHandler->GetSubscriberHandler();
346 CHKPR(subscriberHandler, ERROR_NULL_POINTER);
347 return subscriberHandler->UnsubscribeKeyEvent(sess, subscribeId);
348 }
349 #endif // OHOS_BUILD_ENABLE_KEYBOARD
350
351 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
AddInputEventFilter(sptr<IEventFilter> filter)352 int32_t ServerMsgHandler::AddInputEventFilter(sptr<IEventFilter> filter)
353 {
354 auto filterHandler = InputHandler->GetFilterHandler();
355 CHKPR(filterHandler, ERROR_NULL_POINTER);
356 filterHandler->AddInputEventFilter(filter);
357 return RET_OK;
358 }
359 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
360 } // namespace MMI
361 } // namespace OHOS
362