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