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