• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "input_windows_manager.h"
17 
18 #include <algorithm>
19 #include <cstdlib>
20 #include <cstdio>
21 #include <linux/input.h>
22 
23 #include "dfx_hisysevent.h"
24 #include "fingersense_wrapper.h"
25 #include "input_device_manager.h"
26 #include "i_pointer_drawing_manager.h"
27 #include "mouse_event_normalize.h"
28 #include "pointer_drawing_manager.h"
29 #include "preferences.h"
30 #include "preferences_impl.h"
31 #include "preferences_errno.h"
32 #include "preferences_helper.h"
33 #include "preferences_xml_utils.h"
34 #include "util.h"
35 #include "mmi_matrix3.h"
36 #include "util_ex.h"
37 #include "util_napi_error.h"
38 #include "input_device_manager.h"
39 #include "scene_board_judgement.h"
40 #include "multimodal_input_preferences_manager.h"
41 #include "setting_datashare.h"
42 #include "system_ability_definition.h"
43 #include "touch_drawing_manager.h"
44 #ifdef OHOS_BUILD_ENABLE_ANCO
45 #include "res_sched_client.h"
46 #include "res_type.h"
47 #endif // OHOS_BUILD_ENABLE_ANCO
48 
49 namespace OHOS {
50 namespace MMI {
51 namespace {
52 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputWindowsManager" };
53 #ifdef OHOS_BUILD_ENABLE_POINTER
54 constexpr int32_t DEFAULT_POINTER_STYLE = 0;
55 #endif // OHOS_BUILD_ENABLE_POINTER
56 constexpr int32_t OUTWINDOW_HOT_AREA = 20;
57 constexpr int32_t SCALE_X = 0;
58 constexpr int32_t SCALE_Y = 4;
59 constexpr int32_t TOP_LEFT_AREA = 0;
60 constexpr int32_t TOP_AREA = 1;
61 constexpr int32_t TOP_RIGHT_AREA = 2;
62 constexpr int32_t RIGHT_AREA = 3;
63 constexpr int32_t BOTTOM_RIGHT_AREA = 4;
64 constexpr int32_t BOTTOM_AREA = 5;
65 constexpr int32_t BOTTOM_LEFT_AREA = 6;
66 constexpr int32_t LEFT_AREA = 7;
67 #ifdef OHOS_BUILD_ENABLE_ANCO
68 constexpr int32_t SHELL_WINDOW_COUNT = 1;
69 #endif // OHOS_BUILD_ENABLE_ANCO
70 const std::string bindCfgFileName = "/data/service/el1/public/multimodalinput/display_bind.cfg";
71 const std::string mouseFileName = "mouse_settings.xml";
72 const std::string defaultIconPath = "/system/etc/multimodalinput/mouse_icon/Default.svg";
73 const std::string showCursorSwitchName = "settings.input.show_touch_hint";
74 } // namespace
75 
76 enum PointerHotArea : int32_t {
77     TOP = 0,
78     BOTTOM = 1,
79     LEFT = 2,
80     RIGHT = 3,
81     TOP_LEFT = 4,
82     TOP_RIGHT = 5,
83     BOTTOM_LEFT = 6,
84     BOTTOM_RIGHT = 7,
85 };
86 
InputWindowsManager()87 InputWindowsManager::InputWindowsManager() : bindInfo_(bindCfgFileName)
88 {
89     MMI_HILOGI("Bind cfg file name:%{public}s", bindCfgFileName.c_str());
90 }
91 
~InputWindowsManager()92 InputWindowsManager::~InputWindowsManager() {}
93 
DeviceStatusChanged(int32_t deviceId,const std::string & sysUid,const std::string devStatus)94 void InputWindowsManager::DeviceStatusChanged(int32_t deviceId, const std::string &sysUid, const std::string devStatus)
95 {
96     CALL_INFO_TRACE;
97     if (devStatus == "add") {
98         bindInfo_.AddInputDevice(deviceId, sysUid);
99     } else {
100         bindInfo_.RemoveInputDevice(deviceId);
101     }
102 }
103 
Init(UDSServer & udsServer)104 void InputWindowsManager::Init(UDSServer& udsServer)
105 {
106     udsServer_ = &udsServer;
107     CHKPV(udsServer_);
108     bindInfo_.Load();
109 #ifdef OHOS_BUILD_ENABLE_POINTER
110     udsServer_->AddSessionDeletedCallback(std::bind(&InputWindowsManager::OnSessionLost, this, std::placeholders::_1));
111     InitMouseDownInfo();
112 #endif // OHOS_BUILD_ENABLE_POINTER
113     InputDevMgr->SetInputStatusChangeCallback(std::bind(&InputWindowsManager::DeviceStatusChanged, this,
114         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
115 }
116 
117 #ifdef OHOS_BUILD_ENABLE_POINTER
InitMouseDownInfo()118 void InputWindowsManager::InitMouseDownInfo()
119 {
120     mouseDownInfo_.id = -1;
121     mouseDownInfo_.pid = -1;
122     mouseDownInfo_.defaultHotAreas.clear();
123     mouseDownInfo_.pointerHotAreas.clear();
124 }
125 #endif // OHOS_BUILD_ENABLE_POINTER
126 
GetWindowGroupInfoByDisplayId(int32_t displayId) const127 const std::vector<WindowInfo> &InputWindowsManager::GetWindowGroupInfoByDisplayId(int32_t displayId) const
128 {
129     CALL_DEBUG_ENTER;
130     auto iter = windowsPerDisplay_.find(displayId);
131     if (displayId == -1 || iter == windowsPerDisplay_.end()) {
132         MMI_HILOGW("GetWindowInfo displayId: %{public}d is null from windowGroupInfo_", displayId);
133         return displayGroupInfo_.windowsInfo;
134     }
135     if (iter->second.windowsInfo.empty()) {
136         MMI_HILOGW("GetWindowInfo displayId: %{public}d is empty", displayId);
137         return displayGroupInfo_.windowsInfo;
138     }
139     return iter->second.windowsInfo;
140 }
141 
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)142 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)
143 {
144     CALL_DEBUG_ENTER;
145     CHKPR(pointerEvent, INVALID_FD);
146     const WindowInfo* windowInfo = nullptr;
147     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
148     for (const auto &item : windowsInfo) {
149         if (item.id == pointerEvent->GetTargetWindowId()) {
150             MMI_HILOGD("find windowinfo by window id %{public}d", item.id);
151             windowInfo = &item;
152             break;
153         }
154     }
155     CHKPR(udsServer_, INVALID_FD);
156     if (windowInfo == nullptr) {
157         MMI_HILOGD("window info is null, pointerAction:%{public}d", pointerEvent->GetPointerAction());
158         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
159             windowInfo = &lastWindowInfo_;
160         }
161     }
162     if (windowInfo != nullptr) {
163         MMI_HILOGD("get pid:%{public}d from idxPidMap", windowInfo->pid);
164         return udsServer_->GetClientFd(windowInfo->pid);
165     }
166     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL) {
167         MMI_HILOGD("window info is null, so pointerEvent is dropped! return -1");
168         return udsServer_->GetClientFd(-1);
169     }
170     int32_t pid = -1;
171     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
172         auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
173         if (iter != touchItemDownInfos_.end()) {
174             pid = GetWindowPid(iter->second.agentWindowId);
175             MMI_HILOGD("touchscreen occurs, new pid:%{public}d", pid);
176             touchItemDownInfos_.erase(iter);
177         }
178     }
179 #ifdef OHOS_BUILD_ENABLE_POINTER
180     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
181         if (mouseDownInfo_.pid != -1) {
182             pid = GetWindowPid(mouseDownInfo_.agentWindowId);
183             MMI_HILOGD("mouseevent occurs, update the pid:%{public}d", pid);
184             InitMouseDownInfo();
185         }
186     }
187 #endif // OHOS_BUILD_ENABLE_POINTER
188     MMI_HILOGD("get clientFd by %{public}d", pid);
189     return udsServer_->GetClientFd(pid);
190 }
191 
192 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)193 int32_t InputWindowsManager::UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)
194 {
195     CHKPR(keyEvent, INVALID_FD);
196     CALL_DEBUG_ENTER;
197     int32_t pid = GetPidAndUpdateTarget(keyEvent);
198     if (pid <= 0) {
199         MMI_HILOGE("Invalid pid");
200         return INVALID_FD;
201     }
202     int32_t fd = udsServer_->GetClientFd(pid);
203     if (fd < 0) {
204         MMI_HILOGE("Invalid fd");
205         return INVALID_FD;
206     }
207     return fd;
208 }
209 #endif // OHOS_BUILD_ENABLE_KEYBOARD
210 
GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const211 int32_t InputWindowsManager::GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const
212 {
213     int32_t displayId = inputEvent->GetTargetDisplayId();
214     if (displayId < 0) {
215         MMI_HILOGD("Target display is -1");
216         if (displayGroupInfo_.displaysInfo.empty()) {
217             return displayId;
218         }
219         displayId = displayGroupInfo_.displaysInfo[0].id;
220         inputEvent->SetTargetDisplayId(displayId);
221     }
222     return displayId;
223 }
224 
225 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
GetPidAndUpdateTarget(std::shared_ptr<KeyEvent> keyEvent)226 int32_t InputWindowsManager::GetPidAndUpdateTarget(std::shared_ptr<KeyEvent> keyEvent)
227 {
228     CALL_DEBUG_ENTER;
229     CHKPR(keyEvent, INVALID_PID);
230     const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
231     WindowInfo* windowInfo = nullptr;
232     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(keyEvent->GetTargetDisplayId());
233     for (auto &item : windowsInfo) {
234         if (item.id == focusWindowId) {
235             windowInfo = &item;
236             break;
237         }
238     }
239     CHKPR(windowInfo, INVALID_PID);
240 #ifdef OHOS_BUILD_ENABLE_ANCO
241     if (IsAncoWindowFocus(*windowInfo)) {
242         MMI_HILOGD("focusWindowId:%{public}d is anco window.", focusWindowId);
243         if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
244             SimulateKeyExt(keyEvent);
245         }
246         return INVALID_PID;
247     }
248 #endif // OHOS_BUILD_ENABLE_ANCO
249     keyEvent->SetTargetWindowId(windowInfo->id);
250     keyEvent->SetAgentWindowId(windowInfo->agentWindowId);
251     MMI_HILOGD("focusWindowId:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
252     return windowInfo->pid;
253 }
254 #endif // OHOS_BUILD_ENABLE_KEYBOARD
255 
GetWindowPid(int32_t windowId) const256 int32_t InputWindowsManager::GetWindowPid(int32_t windowId) const
257 {
258     CALL_DEBUG_ENTER;
259     int32_t windowPid = -1;
260     for (const auto &item : displayGroupInfo_.windowsInfo) {
261         MMI_HILOGD("get windowID %{public}d", item.id);
262         if (item.id == windowId) {
263             windowPid = item.pid;
264             break;
265         }
266     }
267     return windowPid;
268 }
269 
GetWindowPid(int32_t windowId,const std::vector<WindowInfo> & windowsInfo) const270 int32_t InputWindowsManager::GetWindowPid(int32_t windowId, const std::vector<WindowInfo> &windowsInfo) const
271 {
272     int32_t windowPid = -1;
273     for (const auto &item : windowsInfo) {
274         if (item.id == windowId) {
275             windowPid = item.pid;
276             break;
277         }
278     }
279     return windowPid;
280 }
281 
CheckFocusWindowChange(const DisplayGroupInfo & displayGroupInfo)282 void InputWindowsManager::CheckFocusWindowChange(const DisplayGroupInfo &displayGroupInfo)
283 {
284     const int32_t oldFocusWindowId = displayGroupInfo_.focusWindowId;
285     const int32_t newFocusWindowId = displayGroupInfo.focusWindowId;
286     if (oldFocusWindowId == newFocusWindowId) {
287         return;
288     }
289     const int32_t oldFocusWindowPid = GetWindowPid(oldFocusWindowId);
290     const int32_t newFocusWindowPid = GetWindowPid(newFocusWindowId, displayGroupInfo.windowsInfo);
291     DfxHisysevent::OnFocusWindowChanged(oldFocusWindowId, newFocusWindowId, oldFocusWindowPid, newFocusWindowPid);
292 }
293 
CheckZorderWindowChange(const std::vector<WindowInfo> & oldWindowsInfo,const std::vector<WindowInfo> & newWindowsInfo)294 void InputWindowsManager::CheckZorderWindowChange(const std::vector<WindowInfo> &oldWindowsInfo,
295     const std::vector<WindowInfo> &newWindowsInfo)
296 {
297     int32_t oldZorderFirstWindowId = -1;
298     int32_t newZorderFirstWindowId = -1;
299     if (!oldWindowsInfo.empty()) {
300         oldZorderFirstWindowId = oldWindowsInfo[0].id;
301     }
302     if (!newWindowsInfo.empty()) {
303         newZorderFirstWindowId = newWindowsInfo[0].id;
304     }
305     if (oldZorderFirstWindowId == newZorderFirstWindowId) {
306         return;
307     }
308     const int32_t oldZorderFirstWindowPid = GetWindowPid(oldZorderFirstWindowId);
309     const int32_t newZorderFirstWindowPid = GetWindowPid(newZorderFirstWindowId, newWindowsInfo);
310     DfxHisysevent::OnZorderWindowChanged(oldZorderFirstWindowId, newZorderFirstWindowId,
311         oldZorderFirstWindowPid, newZorderFirstWindowPid);
312 }
313 
UpdateDisplayIdAndName()314 void InputWindowsManager::UpdateDisplayIdAndName()
315 {
316     using IdNames = std::set<std::pair<int32_t, std::string>>;
317     IdNames newInfo;
318     for (const auto &item : displayGroupInfo_.displaysInfo) {
319         newInfo.insert(std::make_pair(item.id, item.uniq));
320     }
321     auto oldInfo = bindInfo_.GetDisplayIdNames();
322     if (newInfo == oldInfo) {
323         return;
324     }
325     for (auto it = oldInfo.begin(); it != oldInfo.end();) {
326         if (newInfo.find(*it) == newInfo.end()) {
327             bindInfo_.RemoveDisplay(it->first);
328             oldInfo.erase(it++);
329         } else {
330             ++it;
331         }
332     }
333     for (const auto &item : newInfo) {
334         if (!bindInfo_.IsDisplayAdd(item.first, item.second)) {
335             bindInfo_.AddDisplay(item.first, item.second);
336         }
337     }
338 }
339 
GetDisplayBindInfo(DisplayBindInfos & infos)340 int32_t InputWindowsManager::GetDisplayBindInfo(DisplayBindInfos &infos)
341 {
342     CALL_DEBUG_ENTER;
343     return bindInfo_.GetDisplayBindInfo(infos);
344 }
345 
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)346 int32_t InputWindowsManager::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
347 {
348     CALL_DEBUG_ENTER;
349     return bindInfo_.SetDisplayBind(deviceId, displayId, msg);
350 }
351 
UpdateCaptureMode(const DisplayGroupInfo & displayGroupInfo)352 void InputWindowsManager::UpdateCaptureMode(const DisplayGroupInfo &displayGroupInfo)
353 {
354     if (captureModeInfo_.isCaptureMode &&
355         ((displayGroupInfo_.focusWindowId != displayGroupInfo.focusWindowId) ||
356         (displayGroupInfo_.windowsInfo[0].id != displayGroupInfo.windowsInfo[0].id))) {
357         captureModeInfo_.isCaptureMode = false;
358     }
359 }
360 
UpdateWindowInfo(const WindowGroupInfo & windowGroupInfo)361 void InputWindowsManager::UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo)
362 {
363     CALL_DEBUG_ENTER;
364     PrintWindowGroupInfo(windowGroupInfo);
365 #ifdef OHOS_BUILD_ENABLE_ANCO
366     if (windowGroupInfo.windowsInfo.size() == SHELL_WINDOW_COUNT && IsAncoWindow(windowGroupInfo.windowsInfo[0])) {
367         return UpdateShellWindow(windowGroupInfo.windowsInfo[0]);
368     }
369 #endif // OHOS_BUILD_ENABLE_ANCO
370     DisplayGroupInfo displayGroupInfo = displayGroupInfo_;
371     displayGroupInfo.focusWindowId = windowGroupInfo.focusWindowId;
372     for (const auto &item : windowGroupInfo.windowsInfo) {
373         UpdateDisplayInfoByIncrementalInfo(item, displayGroupInfo);
374     }
375 
376 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
377     pointerDrawFlag_ = NeedUpdatePointDrawFlag(windowGroupInfo.windowsInfo);
378 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
379 
380 #ifdef OHOS_BUILD_ENABLE_ANCO
381     UpdateWindowInfoExt(windowGroupInfo, displayGroupInfo);
382 #endif // OHOS_BUILD_ENABLE_ANCO
383     UpdateDisplayInfoExtIfNeed(displayGroupInfo, false);
384 }
385 
UpdateDisplayInfoExtIfNeed(DisplayGroupInfo & displayGroupInfo,bool needUpdateDisplayExt)386 void InputWindowsManager::UpdateDisplayInfoExtIfNeed(DisplayGroupInfo &displayGroupInfo, bool needUpdateDisplayExt)
387 {
388     UpdateDisplayInfo(displayGroupInfo);
389 #ifdef OHOS_BUILD_ENABLE_ANCO
390     if (needUpdateDisplayExt) {
391         UpdateDisplayInfoExt(displayGroupInfo);
392     }
393 #endif // OHOS_BUILD_ENABLE_ANCO
394 }
395 
UpdateDisplayInfoByIncrementalInfo(const WindowInfo & window,DisplayGroupInfo & displayGroupInfo)396 void InputWindowsManager::UpdateDisplayInfoByIncrementalInfo(const WindowInfo &window,
397     DisplayGroupInfo &displayGroupInfo)
398 {
399     CALL_DEBUG_ENTER;
400     switch (window.action) {
401         case WINDOW_UPDATE_ACTION::ADD:
402         case WINDOW_UPDATE_ACTION::ADD_END: {
403             auto id = window.id;
404             auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
405                 [id](const auto& item) { return item.id == id; });
406             if (pos == std::end(displayGroupInfo.windowsInfo)) {
407                 displayGroupInfo.windowsInfo.emplace_back(window);
408             } else {
409                 *pos = window;
410             }
411             break;
412         }
413         case WINDOW_UPDATE_ACTION::DEL: {
414             auto oldWindow = displayGroupInfo.windowsInfo.begin();
415             while (oldWindow != displayGroupInfo.windowsInfo.end()) {
416                 if (oldWindow->id == window.id) {
417                     oldWindow = displayGroupInfo.windowsInfo.erase(oldWindow);
418                 } else {
419                     oldWindow++;
420                 }
421             }
422             break;
423         }
424         case WINDOW_UPDATE_ACTION::CHANGE: {
425             auto id = window.id;
426             auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
427                 [id](const auto& item) { return item.id == id; });
428             if (pos != std::end(displayGroupInfo.windowsInfo)) {
429                 *pos = window;
430             }
431             break;
432         }
433         default: {
434             MMI_HILOGI("WINDOW_UPDATE_ACTION is action:%{public}d", window.action);
435             break;
436         }
437     }
438 }
439 
UpdateWindowsInfoPerDisplay(const DisplayGroupInfo & displayGroupInfo)440 void InputWindowsManager::UpdateWindowsInfoPerDisplay(const DisplayGroupInfo &displayGroupInfo)
441 {
442     CALL_DEBUG_ENTER;
443     std::map<int32_t, WindowGroupInfo> windowsPerDisplay;
444     for (const auto &window : displayGroupInfo.windowsInfo) {
445         auto it = windowsPerDisplay.find(window.displayId);
446         if (it == windowsPerDisplay.end()) {
447             windowsPerDisplay[window.displayId] = WindowGroupInfo {-1, window.displayId, {window}};
448         } else {
449             it->second.windowsInfo.emplace_back(window);
450         }
451         if (displayGroupInfo.focusWindowId == window.id) {
452             windowsPerDisplay[window.displayId].focusWindowId = window.id;
453         }
454     }
455     for (auto iter : windowsPerDisplay) {
456         std::sort(iter.second.windowsInfo.begin(), iter.second.windowsInfo.end(),
457             [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
458             return lwindow.zOrder > rwindow.zOrder;
459         });
460     }
461     for (const auto &item : windowsPerDisplay) {
462         int32_t displayId = item.first;
463         if (windowsPerDisplay_.find(displayId) != windowsPerDisplay_.end()) {
464             CheckZorderWindowChange(windowsPerDisplay_[displayId].windowsInfo, item.second.windowsInfo);
465         }
466     }
467 
468     windowsPerDisplay_ = windowsPerDisplay;
469 }
470 
471 
UpdateDisplayInfo(DisplayGroupInfo & displayGroupInfo)472 void InputWindowsManager::UpdateDisplayInfo(DisplayGroupInfo &displayGroupInfo)
473 {
474     CALL_DEBUG_ENTER;
475 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
476     pointerDrawFlag_ = NeedUpdatePointDrawFlag(displayGroupInfo.windowsInfo);
477 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
478     std::sort(displayGroupInfo.windowsInfo.begin(), displayGroupInfo.windowsInfo.end(),
479         [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
480         return lwindow.zOrder > rwindow.zOrder;
481     });
482     UpdateWindowsInfoPerDisplay(displayGroupInfo);
483     CheckFocusWindowChange(displayGroupInfo);
484     UpdateCaptureMode(displayGroupInfo);
485     displayGroupInfo_ = displayGroupInfo;
486     PrintDisplayInfo();
487     UpdateDisplayIdAndName();
488     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
489         UpdatePointerChangeAreas(displayGroupInfo);
490     }
491 #ifdef OHOS_BUILD_ENABLE_POINTER
492     InitPointerStyle();
493 #endif // OHOS_BUILD_ENABLE_POINTER
494 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
495     if (!displayGroupInfo.displaysInfo.empty() && pointerDrawFlag_) {
496         PointerDrawingManagerOnDisplayInfo(displayGroupInfo);
497     }
498 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
499     UpdateDisplayMode();
500 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
501     if (InputDevMgr->HasPointerDevice()) {
502         NotifyPointerToWindow();
503     }
504 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
505 }
506 
507 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
UpdateDisplayMode()508 void InputWindowsManager::UpdateDisplayMode()
509 {
510     CALL_DEBUG_ENTER;
511     if (displayGroupInfo_.displaysInfo.empty()) {
512         MMI_HILOGE("displaysInfo is empty");
513         return;
514     }
515     DisplayMode mode = displayGroupInfo_.displaysInfo[0].displayMode;
516     if (mode == displayMode_) {
517         MMI_HILOGD("displaymode not change, mode: %{public}d, diaplayMode_: %{public}d", mode, displayMode_);
518         return;
519     }
520     displayMode_ = mode;
521     if (FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_ == nullptr) {
522         MMI_HILOGD("send fingersense display mode is nullptr");
523         return;
524     }
525     MMI_HILOGI("update fingersense display mode, displayMode: %{public}d", displayMode_);
526     FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_(static_cast<int32_t>(displayMode_));
527 }
528 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
529 
530 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
PointerDrawingManagerOnDisplayInfo(const DisplayGroupInfo & displayGroupInfo)531 void InputWindowsManager::PointerDrawingManagerOnDisplayInfo(const DisplayGroupInfo &displayGroupInfo)
532 {
533     IPointerDrawingManager::GetInstance()->OnDisplayInfo(displayGroupInfo);
534     if (InputDevMgr->HasPointerDevice()) {
535         MouseLocation mouseLocation = GetMouseInfo();
536         int32_t displayId = MouseEventHdr->GetDisplayId();
537         if (displayId < 0) {
538             displayId = displayGroupInfo_.displaysInfo[0].id;
539         }
540         auto displayInfo = GetPhysicalDisplay(displayId);
541         CHKPV(displayInfo);
542         int32_t logicX = mouseLocation.physicalX + displayInfo->x;
543         int32_t logicY = mouseLocation.physicalY + displayInfo->y;
544         std::optional<WindowInfo> windowInfo;
545         CHKPV(lastPointerEvent_);
546         if ((lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE ||
547             lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) &&
548             lastPointerEvent_->GetPressedButtons().empty()) {
549             windowInfo = GetWindowInfo(logicX, logicY);
550         } else {
551             windowInfo = SelectWindowInfo(logicX, logicY, lastPointerEvent_);
552         }
553         CHKFRV(windowInfo, "The windowInfo is nullptr");
554         int32_t windowPid = GetWindowPid(windowInfo->id);
555         WinInfo info = { .windowPid = windowPid, .windowId = windowInfo->id };
556         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
557         PointerStyle pointerStyle;
558         int32_t ret = WinMgr->GetPointerStyle(info.windowPid, info.windowId, pointerStyle);
559         MMI_HILOGD("get pointer style, pid: %{public}d, windowid: %{public}d, style: %{public}d",
560             info.windowPid, info.windowId, pointerStyle.id);
561         CHKNOKRV(ret, "Draw pointer style failed, pointerStyleInfo is nullptr");
562         WindowInfo window = *windowInfo;
563         if (!dragFlag_) {
564             SelectPointerChangeArea(window, pointerStyle, logicX, logicY);
565             dragPointerStyle_ = pointerStyle;
566             MMI_HILOGD("not in drag SelectPointerStyle, pointerStyle is:%{public}d", dragPointerStyle_.id);
567         }
568         if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
569             dragFlag_ = true;
570             MMI_HILOGD("Is in drag scene");
571         }
572         if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
573             dragFlag_ = false;
574         }
575         IPointerDrawingManager::GetInstance()->DrawPointerStyle(dragPointerStyle_);
576     }
577 }
578 
NeedUpdatePointDrawFlag(const std::vector<WindowInfo> & windows)579 bool InputWindowsManager::NeedUpdatePointDrawFlag(const std::vector<WindowInfo> &windows)
580 {
581     CALL_DEBUG_ENTER;
582     return !windows.empty() && windows.back().action == WINDOW_UPDATE_ACTION::ADD_END;
583 }
584 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
585 
GetPointerStyleByArea(WindowArea area,int32_t pid,int32_t winId,PointerStyle & pointerStyle)586 void InputWindowsManager::GetPointerStyleByArea(WindowArea area, int32_t pid, int32_t winId, PointerStyle& pointerStyle)
587 {
588     CALL_DEBUG_ENTER;
589     switch (area) {
590         case WindowArea::ENTER:
591         case WindowArea::EXIT:
592             MMI_HILOGD("SetPointerStyle for Enter or exit! No need to deal with it now");
593             break;
594         case WindowArea::FOCUS_ON_TOP_LEFT:
595         case WindowArea::FOCUS_ON_BOTTOM_RIGHT:
596             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
597             break;
598         case WindowArea::FOCUS_ON_TOP_RIGHT:
599         case WindowArea::FOCUS_ON_BOTTOM_LEFT:
600             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
601             break;
602         case WindowArea::FOCUS_ON_TOP:
603         case WindowArea::FOCUS_ON_BOTTOM:
604             pointerStyle.id = MOUSE_ICON::NORTH_SOUTH;
605             break;
606         case WindowArea::FOCUS_ON_LEFT:
607         case WindowArea::FOCUS_ON_RIGHT:
608             pointerStyle.id = MOUSE_ICON::WEST_EAST;
609             break;
610         case WindowArea::TOP_LEFT_LIMIT:
611             pointerStyle.id = MOUSE_ICON::SOUTH_EAST;
612             break;
613         case WindowArea::TOP_RIGHT_LIMIT:
614             pointerStyle.id = MOUSE_ICON::SOUTH_WEST;
615             break;
616         case WindowArea::TOP_LIMIT:
617             pointerStyle.id = MOUSE_ICON::SOUTH;
618             break;
619         case WindowArea::LEFT_LIMIT:
620             pointerStyle.id = MOUSE_ICON::EAST;
621             break;
622         case WindowArea::RIGHT_LIMIT:
623             pointerStyle.id = MOUSE_ICON::WEST;
624             break;
625         case WindowArea::BOTTOM_LEFT_LIMIT:
626             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
627             break;
628         case WindowArea::BOTTOM_LIMIT:
629             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
630             break;
631         case WindowArea::BOTTOM_RIGHT_LIMIT:
632             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
633             break;
634         case WindowArea::FOCUS_ON_INNER:
635             int32_t ret = GetPointerStyle(pid, winId, pointerStyle);
636             CHKNOKRV(ret, "Get pointer style failed, pointerStyleInfo is nullptr");
637             break;
638     }
639 }
640 
SetWindowPointerStyle(WindowArea area,int32_t pid,int32_t windowId)641 void InputWindowsManager::SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId)
642 {
643     CALL_DEBUG_ENTER;
644     PointerStyle pointerStyle;
645     GetPointerStyleByArea(area, pid, windowId, pointerStyle);
646     UpdateWindowPointerVisible(pid);
647     if (lastPointerStyle_.id == pointerStyle.id) {
648         MMI_HILOGE("Tha lastPointerStyle is  totally equal with this, no need to change it");
649         return;
650     }
651     lastPointerStyle_.id = pointerStyle.id;
652     std::map<MOUSE_ICON, IconStyle> mouseIcons = IPointerDrawingManager::GetInstance()->GetMouseIconPath();
653     if (windowId != GLOBAL_WINDOW_ID && (pointerStyle.id == MOUSE_ICON::DEFAULT &&
654         mouseIcons[MOUSE_ICON(pointerStyle.id)].iconPath != defaultIconPath)) {
655         PointerStyle style;
656         int32_t ret = WinMgr->GetPointerStyle(pid, GLOBAL_WINDOW_ID, style);
657         if (ret != RET_OK) {
658             MMI_HILOGE("Get global pointer style failed!");
659             return;
660         }
661         lastPointerStyle_ = style;
662     }
663     MMI_HILOGI("Window id:%{public}d set pointer style:%{public}d success", windowId, lastPointerStyle_.id);
664     IPointerDrawingManager::GetInstance()->DrawPointerStyle(lastPointerStyle_);
665 }
666 
UpdateWindowPointerVisible(int32_t pid)667 void InputWindowsManager::UpdateWindowPointerVisible(int32_t pid)
668 {
669     bool visible = IPointerDrawingManager::GetInstance()->GetPointerVisible(pid);
670     IPointerDrawingManager::GetInstance()->SetPointerVisible(pid, visible);
671 }
672 
673 #ifdef OHOS_BUILD_ENABLE_POINTER
SendPointerEvent(int32_t pointerAction)674 void InputWindowsManager::SendPointerEvent(int32_t pointerAction)
675 {
676     CALL_INFO_TRACE;
677     CHKPV(udsServer_);
678     auto pointerEvent = PointerEvent::Create();
679     CHKPV(pointerEvent);
680     pointerEvent->UpdateId();
681     MouseLocation mouseLocation = GetMouseInfo();
682     lastLogicX_ = mouseLocation.physicalX;
683     lastLogicY_ = mouseLocation.physicalY;
684     if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW ||
685         Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
686         auto touchWindow = GetWindowInfo(lastLogicX_, lastLogicY_);
687         if (!touchWindow) {
688             MMI_HILOGE("TouchWindow is nullptr");
689             return;
690         }
691         lastWindowInfo_ = *touchWindow;
692     }
693     PointerEvent::PointerItem pointerItem;
694     pointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
695     pointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
696     pointerItem.SetDisplayX(lastLogicX_);
697     pointerItem.SetDisplayY(lastLogicY_);
698     pointerItem.SetPointerId(0);
699 
700     pointerEvent->SetTargetDisplayId(-1);
701     auto displayId = pointerEvent->GetTargetDisplayId();
702     if (!UpdateDisplayId(displayId)) {
703         MMI_HILOGE("This display:%{public}d is not existent", displayId);
704         return;
705     }
706     pointerEvent->SetTargetDisplayId(displayId);
707     pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
708     pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
709     pointerEvent->SetPointerId(0);
710     pointerEvent->AddPointerItem(pointerItem);
711     pointerEvent->SetPointerAction(pointerAction);
712     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
713     int64_t time = GetSysClockTime();
714     pointerEvent->SetActionTime(time);
715     pointerEvent->SetActionStartTime(time);
716     pointerEvent->UpdateId();
717     if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
718         pointerEvent->SetBuffer(extraData_.buffer);
719         UpdatePointerAction(pointerEvent);
720     } else {
721         pointerEvent->ClearBuffer();
722     }
723     auto fd = udsServer_->GetClientFd(lastWindowInfo_.pid);
724     auto sess = udsServer_->GetSession(fd);
725     CHKPRV(sess, "The last window has disappeared");
726     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
727     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
728     if (!sess->SendMsg(pkt)) {
729         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
730         return;
731     }
732 }
733 
DispatchPointer(int32_t pointerAction)734 void InputWindowsManager::DispatchPointer(int32_t pointerAction)
735 {
736     CALL_INFO_TRACE;
737     CHKPV(udsServer_);
738     if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
739         MMI_HILOGI("the mouse is hide");
740         return;
741     }
742     if (lastPointerEvent_ == nullptr) {
743         SendPointerEvent(pointerAction);
744         return;
745     }
746     auto pointerEvent = PointerEvent::Create();
747     CHKPV(pointerEvent);
748     pointerEvent->UpdateId();
749 
750     PointerEvent::PointerItem lastPointerItem;
751     int32_t lastPointerId = lastPointerEvent_->GetPointerId();
752     if (!lastPointerEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
753         MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
754         return;
755     }
756     if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW) {
757         std::optional<WindowInfo> windowInfo;
758         if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE &&
759             lastPointerEvent_->GetPressedButtons().empty()) {
760             windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
761         } else {
762             windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEvent_);
763         }
764         if (!windowInfo) {
765             MMI_HILOGE("windowInfo is nullptr");
766             return;
767         }
768         if (windowInfo->id != lastWindowInfo_.id) {
769             lastWindowInfo_ = *windowInfo;
770         }
771     }
772     PointerEvent::PointerItem currentPointerItem;
773     currentPointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
774     currentPointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
775     currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
776     currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
777     currentPointerItem.SetPointerId(0);
778 
779     pointerEvent->SetTargetDisplayId(lastPointerEvent_->GetTargetDisplayId());
780     pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
781     pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
782     pointerEvent->SetPointerId(0);
783     pointerEvent->AddPointerItem(currentPointerItem);
784     pointerEvent->SetPointerAction(pointerAction);
785     pointerEvent->SetSourceType(lastPointerEvent_->GetSourceType());
786     int64_t time = GetSysClockTime();
787     pointerEvent->SetActionTime(time);
788     pointerEvent->SetActionStartTime(time);
789     pointerEvent->SetDeviceId(lastPointerEvent_->GetDeviceId());
790     if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
791         pointerEvent->SetBuffer(extraData_.buffer);
792         UpdatePointerAction(pointerEvent);
793     } else {
794         pointerEvent->ClearBuffer();
795     }
796     int pid = lastWindowInfo_.pid;
797     if (pointerAction == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
798         pointerEvent->SetAgentWindowId(lastWindowInfo_.id);
799     }
800     auto fd = udsServer_->GetClientFd(pid);
801     if (fd == RET_ERR) {
802         auto windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
803         if (!windowInfo) {
804             MMI_HILOGE("The windowInfo is nullptr");
805             return;
806         }
807         fd = udsServer_->GetClientFd(windowInfo->pid);
808     }
809     auto sess = udsServer_->GetSession(fd);
810     if (sess == nullptr) {
811         MMI_HILOGI("The last window has disappeared");
812         return;
813     }
814 
815     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
816     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
817     if (!sess->SendMsg(pkt)) {
818         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
819         return;
820     }
821 }
822 
NotifyPointerToWindow()823 void InputWindowsManager::NotifyPointerToWindow()
824 {
825     CALL_INFO_TRACE;
826     std::optional<WindowInfo> windowInfo;
827     CHKPV(lastPointerEvent_);
828     if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE &&
829         lastPointerEvent_->GetPressedButtons().empty()) {
830         windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
831     } else {
832         windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEvent_);
833     }
834     if (!windowInfo) {
835         MMI_HILOGE("The windowInfo is nullptr");
836         return;
837     }
838     if (windowInfo->id == lastWindowInfo_.id) {
839         MMI_HILOGI("The mouse pointer does not leave the window:%{public}d", lastWindowInfo_.id);
840         lastWindowInfo_ = *windowInfo;
841         return;
842     }
843     bool isFindLastWindow = false;
844     for (const auto &item : displayGroupInfo_.windowsInfo) {
845         if (item.id == lastWindowInfo_.id) {
846             DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
847             isFindLastWindow = true;
848             break;
849         }
850     }
851     if (!isFindLastWindow) {
852         if (udsServer_ != nullptr && udsServer_->GetClientFd(lastWindowInfo_.pid) != INVALID_FD) {
853             DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
854         }
855     }
856     lastWindowInfo_ = *windowInfo;
857     DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
858 }
859 #endif // OHOS_BUILD_ENABLE_POINTER
860 
PrintWindowInfo(const std::vector<WindowInfo> & windowsInfo)861 void InputWindowsManager::PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo)
862 {
863     std::string window;
864     window += StringPrintf("windowId:[");
865     for (const auto &item : windowsInfo) {
866         MMI_HILOGD("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
867             "area.x:%{public}d,area.y:%{public}d,area.width:%{public}d,area.height:%{public}d,"
868             "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
869             "agentWindowId:%{public}d,flags:%{public}d,action:%{public}d,displayId:%{public}d,"
870             "zOrder:%{public}f",
871             item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
872             item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
873             item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder);
874         for (const auto &win : item.defaultHotAreas) {
875             MMI_HILOGD("defaultHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
876                 win.x, win.y, win.width, win.height);
877         }
878         for (const auto &pointer : item.pointerHotAreas) {
879             MMI_HILOGD("pointerHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
880                 pointer.x, pointer.y, pointer.width, pointer.height);
881         }
882 
883         window += StringPrintf("%d,", item.id);
884         std::string dump;
885         dump += StringPrintf("pointChangeAreas:[");
886         for (auto it : item.pointerChangeAreas) {
887             dump += StringPrintf("%d,", it);
888         }
889         dump += StringPrintf("]\n");
890 
891         dump += StringPrintf("transform:[");
892         for (auto it : item.transform) {
893             dump += StringPrintf("%f,", it);
894         }
895         dump += StringPrintf("]\n");
896         std::istringstream stream(dump);
897         std::string line;
898         while (std::getline(stream, line, '\n')) {
899             MMI_HILOGD("%{public}s", line.c_str());
900         }
901     }
902     window += StringPrintf("]\n");
903     MMI_HILOGI("%{public}s", window.c_str());
904 }
905 
PrintWindowGroupInfo(const WindowGroupInfo & windowGroupInfo)906 void InputWindowsManager::PrintWindowGroupInfo(const WindowGroupInfo &windowGroupInfo)
907 {
908     MMI_HILOGD("windowsGroupInfo,focusWindowId:%{public}d,displayId:%{public}d",
909         windowGroupInfo.focusWindowId, windowGroupInfo.displayId);
910     PrintWindowInfo(windowGroupInfo.windowsInfo);
911 }
912 
PrintDisplayInfo()913 void InputWindowsManager::PrintDisplayInfo()
914 {
915     MMI_HILOGD("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d",
916         displayGroupInfo_.width, displayGroupInfo_.height, displayGroupInfo_.focusWindowId);
917     MMI_HILOGD("windowsInfos,num:%{public}zu", displayGroupInfo_.windowsInfo.size());
918     PrintWindowInfo(displayGroupInfo_.windowsInfo);
919 
920     MMI_HILOGD("displayInfos,num:%{public}zu", displayGroupInfo_.displaysInfo.size());
921     for (const auto &item : displayGroupInfo_.displaysInfo) {
922         MMI_HILOGD("displayInfos,id:%{public}d,x:%{public}d,y:%{public}d,"
923             "width:%{public}d,height:%{public}d,name:%{public}s,"
924             "uniq:%{public}s,direction:%{public}d,displayDirection:%{public}d",
925             item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
926             item.uniq.c_str(), item.direction, item.displayDirection);
927     }
928 }
929 
930 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
GetPhysicalDisplay(int32_t id) const931 const DisplayInfo* InputWindowsManager::GetPhysicalDisplay(int32_t id) const
932 {
933     for (auto &it : displayGroupInfo_.displaysInfo) {
934         if (it.id == id) {
935             return &it;
936         }
937     }
938     MMI_HILOGW("Failed to obtain physical(%{public}d) display", id);
939     return nullptr;
940 }
941 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
942 
943 #ifdef OHOS_BUILD_ENABLE_TOUCH
FindPhysicalDisplayInfo(const std::string & uniq) const944 const DisplayInfo* InputWindowsManager::FindPhysicalDisplayInfo(const std::string& uniq) const
945 {
946     for (auto &it : displayGroupInfo_.displaysInfo) {
947         if (it.uniq == uniq) {
948             return &it;
949         }
950     }
951     MMI_HILOGE("Failed to search for Physical,uniq:%{public}s", uniq.c_str());
952     return nullptr;
953 }
954 
RotateScreen(const DisplayInfo & info,LogicalCoordinate & coord) const955 void InputWindowsManager::RotateScreen(const DisplayInfo& info, LogicalCoordinate& coord) const
956 {
957     const Direction direction = info.direction;
958     if (direction == DIRECTION0) {
959         MMI_HILOGD("direction is DIRECTION0");
960         return;
961     }
962     if (direction == DIRECTION90) {
963         MMI_HILOGD("direction is DIRECTION90");
964         int32_t temp = coord.x;
965         coord.x = info.width - coord.y;
966         coord.y = temp;
967         MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
968         return;
969     }
970     if (direction == DIRECTION180) {
971         MMI_HILOGD("direction is DIRECTION180");
972         coord.x = info.width - coord.x;
973         coord.y = info.height - coord.y;
974         MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
975         return;
976     }
977     if (direction == DIRECTION270) {
978         MMI_HILOGD("direction is DIRECTION270");
979         int32_t temp = coord.y;
980         coord.y = info.height - coord.x;
981         coord.x = temp;
982         MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
983     }
984 }
985 
GetPhysicalDisplayCoord(struct libinput_event_touch * touch,const DisplayInfo & info,EventTouch & touchInfo)986 void InputWindowsManager::GetPhysicalDisplayCoord(struct libinput_event_touch* touch,
987     const DisplayInfo& info, EventTouch& touchInfo)
988 {
989     auto width = info.width;
990     auto height = info.height;
991     if (info.direction == DIRECTION90 || info.direction == DIRECTION270) {
992         width = info.height;
993         height = info.width;
994     }
995     LogicalCoordinate coord {
996         .x = static_cast<int32_t>(libinput_event_touch_get_x_transformed(touch, width)),
997         .y = static_cast<int32_t>(libinput_event_touch_get_y_transformed(touch, height)),
998     };
999     RotateScreen(info, coord);
1000     touchInfo.point.x = coord.x;
1001     touchInfo.point.y = coord.y;
1002     touchInfo.toolRect.point.x = static_cast<int32_t>(libinput_event_touch_get_tool_x_transformed(touch, width));
1003     touchInfo.toolRect.point.y = static_cast<int32_t>(libinput_event_touch_get_tool_y_transformed(touch, height));
1004     touchInfo.toolRect.width = static_cast<int32_t>(
1005         libinput_event_touch_get_tool_width_transformed(touch, width));
1006     touchInfo.toolRect.height = static_cast<int32_t>(
1007         libinput_event_touch_get_tool_height_transformed(touch, height));
1008 }
1009 
TouchPointToDisplayPoint(int32_t deviceId,struct libinput_event_touch * touch,EventTouch & touchInfo,int32_t & physicalDisplayId)1010 bool InputWindowsManager::TouchPointToDisplayPoint(int32_t deviceId, struct libinput_event_touch* touch,
1011     EventTouch& touchInfo, int32_t& physicalDisplayId)
1012 {
1013     CHKPF(touch);
1014     std::string screenId = bindInfo_.GetBindDisplayNameByInputDevice(deviceId);
1015     if (screenId.empty()) {
1016         screenId = "default0";
1017     }
1018     auto info = FindPhysicalDisplayInfo(screenId);
1019     CHKPF(info);
1020     physicalDisplayId = info->id;
1021     if ((info->width <= 0) || (info->height <= 0)) {
1022         MMI_HILOGE("Get DisplayInfo is error");
1023         return false;
1024     }
1025     GetPhysicalDisplayCoord(touch, *info, touchInfo);
1026     return true;
1027 }
1028 
TransformTipPoint(struct libinput_event_tablet_tool * tip,LogicalCoordinate & coord,int32_t & displayId) const1029 bool InputWindowsManager::TransformTipPoint(struct libinput_event_tablet_tool* tip,
1030     LogicalCoordinate& coord, int32_t& displayId) const
1031 {
1032     CHKPF(tip);
1033     auto displayInfo = FindPhysicalDisplayInfo("default0");
1034     CHKPF(displayInfo);
1035     MMI_HILOGD("PhysicalDisplay.width:%{public}d, PhysicalDisplay.height:%{public}d, "
1036                "PhysicalDisplay.topLeftX:%{public}d, PhysicalDisplay.topLeftY:%{public}d",
1037                displayInfo->width, displayInfo->height, displayInfo->x, displayInfo->y);
1038     displayId = displayInfo->id;
1039     PhysicalCoordinate phys {
1040         .x = libinput_event_tablet_tool_get_x_transformed(tip, displayInfo->width),
1041         .y = libinput_event_tablet_tool_get_y_transformed(tip, displayInfo->height)
1042     };
1043 
1044     coord.x = static_cast<int32_t>(phys.x);
1045     coord.y = static_cast<int32_t>(phys.y);
1046     MMI_HILOGD("physicalX:%{public}f, physicalY:%{public}f, displayId:%{public}d", phys.x, phys.y, displayId);
1047     return true;
1048 }
1049 
CalculateTipPoint(struct libinput_event_tablet_tool * tip,int32_t & targetDisplayId,LogicalCoordinate & coord) const1050 bool InputWindowsManager::CalculateTipPoint(struct libinput_event_tablet_tool* tip,
1051     int32_t& targetDisplayId, LogicalCoordinate& coord) const
1052 {
1053     CHKPF(tip);
1054     if (!TransformTipPoint(tip, coord, targetDisplayId)) {
1055         return false;
1056     }
1057     return true;
1058 }
1059 #endif // OHOS_BUILD_ENABLE_TOUCH
1060 
1061 #ifdef OHOS_BUILD_ENABLE_POINTER
GetDisplayGroupInfo()1062 const DisplayGroupInfo& InputWindowsManager::GetDisplayGroupInfo()
1063 {
1064     return displayGroupInfo_;
1065 }
1066 
1067 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
IsNeedRefreshLayer(int32_t windowId)1068 bool InputWindowsManager::IsNeedRefreshLayer(int32_t windowId)
1069 {
1070     CALL_DEBUG_ENTER;
1071     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1072         return true;
1073     }
1074     MouseLocation mouseLocation = GetMouseInfo();
1075     int32_t displayId = MouseEventHdr->GetDisplayId();
1076     if (displayId < 0) {
1077         displayId = displayGroupInfo_.displaysInfo[0].id;
1078     }
1079     auto displayInfo = GetPhysicalDisplay(displayId);
1080     CHKPF(displayInfo);
1081     int32_t logicX = mouseLocation.physicalX + displayInfo->x;
1082     int32_t logicY = mouseLocation.physicalY + displayInfo->y;
1083     std::optional<WindowInfo> touchWindow = GetWindowInfo(logicX, logicY);
1084     if (!touchWindow) {
1085         MMI_HILOGE("TouchWindow is nullptr");
1086         return false;
1087     }
1088     if (touchWindow->id == windowId || windowId == GLOBAL_WINDOW_ID) {
1089         MMI_HILOGD("Need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
1090             touchWindow->id, windowId);
1091         return true;
1092     }
1093     MMI_HILOGD("Not need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
1094         touchWindow->id, windowId);
1095     return false;
1096 }
1097 #endif
1098 
OnSessionLost(SessionPtr session)1099 void InputWindowsManager::OnSessionLost(SessionPtr session)
1100 {
1101     CALL_DEBUG_ENTER;
1102     CHKPV(session);
1103     int32_t pid = session->GetPid();
1104 
1105     auto it = pointerStyle_.find(pid);
1106     if (it != pointerStyle_.end()) {
1107         pointerStyle_.erase(it);
1108         MMI_HILOGD("Clear the pointer style map, pd:%{public}d", pid);
1109     }
1110 }
1111 
UpdatePoinerStyle(int32_t pid,int32_t windowId,PointerStyle pointerStyle)1112 int32_t InputWindowsManager::UpdatePoinerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
1113 {
1114     CALL_DEBUG_ENTER;
1115     auto it = pointerStyle_.find(pid);
1116     if (it == pointerStyle_.end()) {
1117         MMI_HILOGE("The pointer style map is not include param pd:%{public}d", pid);
1118         return COMMON_PARAMETER_ERROR;
1119     }
1120     auto iter = it->second.find(windowId);
1121     if (iter != it->second.end()) {
1122         iter->second = pointerStyle;
1123         return RET_OK;
1124     }
1125 
1126     for (const auto& windowInfo : displayGroupInfo_.windowsInfo) {
1127         if (windowId == windowInfo.id && pid == windowInfo.pid) {
1128             auto iterator = it->second.insert(std::make_pair(windowId, pointerStyle));
1129             if (!iterator.second) {
1130                 MMI_HILOGW("The window type is duplicated");
1131             }
1132             return RET_OK;
1133         }
1134     }
1135     MMI_HILOGE("The window id is invalid");
1136     return COMMON_PARAMETER_ERROR;
1137 }
1138 
UpdateSceneBoardPointerStyle(int32_t pid,int32_t windowId,PointerStyle pointerStyle)1139 int32_t InputWindowsManager::UpdateSceneBoardPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
1140 {
1141     CALL_DEBUG_ENTER;
1142     auto scenePid = pid;
1143     auto sceneWinId = windowId;
1144     auto sceneIter = pointerStyle_.find(scenePid);
1145     if (sceneIter == pointerStyle_.end() || sceneIter->second.find(sceneWinId) == sceneIter->second.end()) {
1146         pointerStyle_[scenePid] = {};
1147         MMI_HILOGE("SceneBoardPid %{public}d or windowId:%{public}d  does not exist on pointerStyle_",
1148             scenePid, sceneWinId);
1149     }
1150     pointerStyle_[scenePid][sceneWinId] = pointerStyle;
1151     MMI_HILOGD("Sceneboard pid:%{public}d windowId:%{public}d is set to %{public}d",
1152         scenePid, sceneWinId, pointerStyle.id);
1153     auto it = pointerStyle_.find(pid);
1154     if (it == pointerStyle_.end()) {
1155         MMI_HILOGE("Pid:%{public}d does not exist in mmi,", pid);
1156         std::map<int32_t, PointerStyle> tmpPointerStyle = {{windowId, pointerStyle}};
1157         auto res = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
1158         if (!res.second) return RET_ERR;
1159         return RET_OK;
1160     }
1161     auto iter = it->second.find(windowId);
1162     if (iter == it->second.end()) {
1163         auto res = it->second.insert(std::make_pair(windowId, pointerStyle));
1164         if (!res.second) return RET_ERR;
1165         return RET_OK;
1166     }
1167     iter->second = pointerStyle;
1168     return RET_OK;
1169 }
1170 
SetPointerStyle(int32_t pid,int32_t windowId,PointerStyle pointerStyle)1171 int32_t InputWindowsManager::SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
1172 {
1173     CALL_DEBUG_ENTER;
1174     if (windowId == GLOBAL_WINDOW_ID) {
1175         globalStyle_.id = pointerStyle.id;
1176         MMI_HILOGD("Setting global pointer style");
1177         return RET_OK;
1178     }
1179     MMI_HILOGD("start to get pid by window %{public}d", windowId);
1180     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1181         return UpdatePoinerStyle(pid, windowId, pointerStyle);
1182     }
1183     return UpdateSceneBoardPointerStyle(pid, windowId, pointerStyle);
1184 }
1185 
ClearWindowPointerStyle(int32_t pid,int32_t windowId)1186 int32_t InputWindowsManager::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
1187 {
1188     CALL_DEBUG_ENTER;
1189     auto it = pointerStyle_.find(pid);
1190     if (it == pointerStyle_.end()) {
1191         MMI_HILOGE("Pid: %{public}d does not exist in mmi", pid);
1192         return RET_OK;
1193     }
1194     auto windowIt = it->second.find(windowId);
1195     if (windowIt == it->second.end()) {
1196         MMI_HILOGE("windowId %{public}d does not exist in pid%{public}d", windowId, pid);
1197         return RET_OK;
1198     }
1199 
1200     it->second.erase(windowIt);
1201     return RET_OK;
1202 }
1203 
GetPointerStyle(int32_t pid,int32_t windowId,PointerStyle & pointerStyle) const1204 int32_t InputWindowsManager::GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle) const
1205 {
1206     CALL_DEBUG_ENTER;
1207     if (windowId == GLOBAL_WINDOW_ID) {
1208         MMI_HILOGD("Getting global pointer style");
1209         pointerStyle.id = globalStyle_.id;
1210         return RET_OK;
1211     }
1212     auto it = pointerStyle_.find(pid);
1213     if (it == pointerStyle_.end()) {
1214         if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1215             pointerStyle.id = globalStyle_.id;
1216             return RET_OK;
1217         }
1218         MMI_HILOGE("The pointer style map is not include param pd, %{public}d", pid);
1219         return RET_OK;
1220     }
1221     auto iter = it->second.find(windowId);
1222     if (iter == it->second.end()) {
1223         pointerStyle.id = globalStyle_.id;
1224         return RET_OK;
1225     }
1226 
1227     MMI_HILOGD("Window type:%{public}d get pointer style:%{public}d success", windowId, iter->second.id);
1228     pointerStyle = iter->second;
1229     return RET_OK;
1230 }
1231 
InitPointerStyle()1232 void InputWindowsManager::InitPointerStyle()
1233 {
1234     CALL_DEBUG_ENTER;
1235     PointerStyle pointerStyle;
1236     pointerStyle.id = DEFAULT_POINTER_STYLE;
1237     for (const auto& windowItem : displayGroupInfo_.windowsInfo) {
1238         int32_t pid = windowItem.pid;
1239         auto it = pointerStyle_.find(pid);
1240         if (it == pointerStyle_.end()) {
1241             std::map<int32_t, PointerStyle> tmpPointerStyle = {};
1242             auto iter = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
1243             if (!iter.second) {
1244                 MMI_HILOGW("The pd is duplicated");
1245             }
1246             continue;
1247         }
1248     }
1249     MMI_HILOGD("Number of pointer style:%{public}zu", pointerStyle_.size());
1250 }
1251 
1252 #endif // OHOS_BUILD_ENABLE_POINTER
1253 
1254 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
IsInHotArea(int32_t x,int32_t y,const std::vector<Rect> & rects,const WindowInfo & window) const1255 bool InputWindowsManager::IsInHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
1256     const WindowInfo &window) const
1257 {
1258     auto windowXY = TransformWindowXY(window, x, y);
1259     auto windowX = windowXY.first;
1260     auto windowY = windowXY.second;
1261     for (const auto &item : rects) {
1262         int32_t displayMaxX = 0;
1263         int32_t displayMaxY = 0;
1264         if (!AddInt32(item.x, item.width, displayMaxX)) {
1265             MMI_HILOGE("The addition of displayMaxX overflows");
1266             return false;
1267         }
1268         if (!AddInt32(item.y, item.height, displayMaxY)) {
1269             MMI_HILOGE("The addition of displayMaxY overflows");
1270             return false;
1271         }
1272         if (((windowX >= item.x) && (windowX < displayMaxX)) &&
1273             (windowY >= item.y) && (windowY < displayMaxY)) {
1274             return true;
1275         }
1276     }
1277     return false;
1278 }
1279 
InWhichHotArea(int32_t x,int32_t y,const std::vector<Rect> & rects,PointerStyle & pointerStyle) const1280 void InputWindowsManager::InWhichHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
1281     PointerStyle &pointerStyle) const
1282 {
1283     CALL_DEBUG_ENTER;
1284     int32_t areaNum = 0;
1285     bool findFlag = false;
1286     for (const auto &item : rects) {
1287         int32_t displayMaxX = 0;
1288         int32_t displayMaxY = 0;
1289         if (!AddInt32(item.x, item.width, displayMaxX)) {
1290             MMI_HILOGE("The addition of displayMaxX overflows");
1291             return;
1292         }
1293         if (!AddInt32(item.y, item.height, displayMaxY)) {
1294             MMI_HILOGE("The addition of displayMaxY overflows");
1295             return;
1296         }
1297         if (((x >= item.x) && (x < displayMaxX)) &&
1298             (y >= item.y) && (y < displayMaxY)) {
1299             findFlag = true;
1300             pointerStyle.id = areaNum;
1301         }
1302         areaNum++;
1303     }
1304     if (!findFlag) {
1305         MMI_HILOGW("pointer not match any area");
1306         return;
1307     }
1308     switch (pointerStyle.id) {
1309         case PointerHotArea::TOP:
1310         case PointerHotArea::BOTTOM:
1311             pointerStyle.id = MOUSE_ICON::NORTH_SOUTH;
1312             break;
1313         case PointerHotArea::LEFT:
1314         case PointerHotArea::RIGHT:
1315             pointerStyle.id = MOUSE_ICON::WEST_EAST;
1316             break;
1317         case PointerHotArea::TOP_LEFT:
1318             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
1319             break;
1320         case PointerHotArea::TOP_RIGHT:
1321             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
1322             break;
1323         case PointerHotArea::BOTTOM_LEFT:
1324             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
1325             break;
1326         case PointerHotArea::BOTTOM_RIGHT:
1327             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
1328             break;
1329         default:
1330             MMI_HILOGD("pointerStyle in default is: %{public}d", pointerStyle.id);
1331             break;
1332     }
1333     MMI_HILOGD("pointerStyle after switch ID is :%{public}d", pointerStyle.id);
1334     return;
1335 }
1336 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1337 
1338 #ifdef OHOS_BUILD_ENABLE_TOUCH
AdjustDisplayCoordinate(const DisplayInfo & displayInfo,int32_t & physicalX,int32_t & physicalY) const1339 void InputWindowsManager::AdjustDisplayCoordinate(
1340     const DisplayInfo& displayInfo, int32_t& physicalX, int32_t& physicalY) const
1341 {
1342     int32_t width = displayInfo.width;
1343     int32_t height = displayInfo.height;
1344     if (physicalX <= 0) {
1345         physicalX = 0;
1346     }
1347     if (physicalX >= width && width > 0) {
1348         physicalX = width - 1;
1349     }
1350     if (physicalY <= 0) {
1351         physicalY = 0;
1352     }
1353     if (physicalY >= height && height > 0) {
1354         physicalY = height - 1;
1355     }
1356 }
1357 #endif // OHOS_BUILD_ENABLE_TOUCH
1358 
1359 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdateDisplayId(int32_t & displayId)1360 bool InputWindowsManager::UpdateDisplayId(int32_t& displayId)
1361 {
1362     if (displayGroupInfo_.displaysInfo.empty()) {
1363         MMI_HILOGE("logicalDisplays_is empty");
1364         return false;
1365     }
1366     if (displayId < 0) {
1367         displayId = displayGroupInfo_.displaysInfo[0].id;
1368         return true;
1369     }
1370     for (const auto &item : displayGroupInfo_.displaysInfo) {
1371         if (item.id == displayId) {
1372             return true;
1373         }
1374     }
1375     return false;
1376 }
1377 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1378 
1379 #ifdef OHOS_BUILD_ENABLE_POINTER
SelectWindowInfo(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> & pointerEvent)1380 std::optional<WindowInfo> InputWindowsManager::SelectWindowInfo(int32_t logicalX, int32_t logicalY,
1381     const std::shared_ptr<PointerEvent>& pointerEvent)
1382 {
1383     CALL_DEBUG_ENTER;
1384     int32_t action = pointerEvent->GetPointerAction();
1385     bool checkFlag = (firstBtnDownWindowId_ == -1) ||
1386         ((action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) && (pointerEvent->GetPressedButtons().size() == 1)) ||
1387         ((action == PointerEvent::POINTER_ACTION_MOVE) && (pointerEvent->GetPressedButtons().empty())) ||
1388         (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
1389         (action == PointerEvent::POINTER_ACTION_PULL_UP) ||
1390         (action == PointerEvent::POINTER_ACTION_AXIS_BEGIN);
1391     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
1392     if (checkFlag) {
1393         int32_t targetWindowId = pointerEvent->GetTargetWindowId();
1394         for (const auto &item : windowsInfo) {
1395             if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
1396                 !IsValidZorderWindow(item, pointerEvent)) {
1397                 MMI_HILOGD("Skip the untouchable or invalid zOrder window to continue searching, "
1398                     "window:%{public}d, flags:%{public}d, pid:%{public}d", item.id, item.flags, item.pid);
1399                 continue;
1400             } else if ((extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
1401                 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
1402                 if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
1403                     firstBtnDownWindowId_ = item.id;
1404                     MMI_HILOGD("Mouse event select pull window, window:%{public}d, pid:%{public}d",
1405                         firstBtnDownWindowId_, item.pid);
1406                     break;
1407                 } else {
1408                     continue;
1409                 }
1410             } else if ((targetWindowId < 0) && (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item))) {
1411                 firstBtnDownWindowId_ = item.id;
1412                 MMI_HILOGD("Find out the dispatch window of this pointer event when the targetWindowId "
1413                     "hasn't been set up yet, window:%{public}d, pid:%{public}d", firstBtnDownWindowId_, item.pid);
1414                 break;
1415             } else if ((targetWindowId >= 0) && (targetWindowId == item.id)) {
1416                 firstBtnDownWindowId_ = targetWindowId;
1417                 MMI_HILOGD("Find out the dispatch window of this pointer event when the targetWindowId "
1418                     "has been set up already, window:%{public}d, pid:%{public}d", firstBtnDownWindowId_, item.pid);
1419                 break;
1420             } else {
1421                 MMI_HILOGW("Continue searching for the dispatch window of this pointer event");
1422             }
1423         }
1424     }
1425     MMI_HILOGD("firstBtnDownWindowId_:%{public}d", firstBtnDownWindowId_);
1426     for (const auto &item : windowsInfo) {
1427         if (item.id == firstBtnDownWindowId_) {
1428             return std::make_optional(item);
1429         }
1430     }
1431     return std::nullopt;
1432 }
1433 
GetWindowInfo(int32_t logicalX,int32_t logicalY)1434 std::optional<WindowInfo> InputWindowsManager::GetWindowInfo(int32_t logicalX, int32_t logicalY)
1435 {
1436     CALL_DEBUG_ENTER;
1437     for (const auto& item : displayGroupInfo_.windowsInfo) {
1438         if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
1439             MMI_HILOGD("Skip the untouchable window to continue searching, "
1440                        "window:%{public}d, flags:%{public}d", item.id, item.flags);
1441             continue;
1442         } else if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
1443             return std::make_optional(item);
1444         } else {
1445             MMI_HILOGW("Continue searching for the dispatch window");
1446         }
1447     }
1448     return std::nullopt;
1449 }
1450 
SelectPointerChangeArea(const WindowInfo & windowInfo,PointerStyle & pointerStyle,int32_t logicalX,int32_t logicalY)1451 void InputWindowsManager::SelectPointerChangeArea(const WindowInfo &windowInfo, PointerStyle &pointerStyle,
1452     int32_t logicalX, int32_t logicalY)
1453 {
1454     CALL_DEBUG_ENTER;
1455     int32_t windowId = windowInfo.id;
1456     if (windowsHotAreas_.find(windowId) != windowsHotAreas_.end()) {
1457         std::vector<Rect> windowHotAreas = windowsHotAreas_[windowId];
1458         MMI_HILOGD("windowHotAreas size is:%{public}zu, windowId is :%{public}d", windowHotAreas.size(), windowId);
1459         InWhichHotArea(logicalX, logicalY, windowHotAreas, pointerStyle);
1460     }
1461 }
1462 
UpdatePointerChangeAreas(const DisplayGroupInfo & displayGroupInfo)1463 void InputWindowsManager::UpdatePointerChangeAreas(const DisplayGroupInfo &displayGroupInfo)
1464 {
1465     CALL_DEBUG_ENTER;
1466     for (const auto &windowInfo : displayGroupInfo.windowsInfo) {
1467         std::vector<Rect> windowHotAreas;
1468         int32_t windowId = windowInfo.id;
1469         Rect windowArea = windowInfo.area;
1470         windowArea.width = windowInfo.transform[SCALE_X] != 0 ? windowInfo.area.width / windowInfo.transform[SCALE_X]
1471             : windowInfo.area.width;
1472         windowArea.height = windowInfo.transform[SCALE_Y] != 0 ?  windowInfo.area.height / windowInfo.transform[SCALE_Y]
1473             : windowInfo.area.height;
1474         std::vector<int32_t> pointerChangeAreas = windowInfo.pointerChangeAreas;
1475         UpdateTopBottomArea(windowArea, pointerChangeAreas, windowHotAreas);
1476         UpdateLeftRightArea(windowArea, pointerChangeAreas, windowHotAreas);
1477         UpdateInnerAngleArea(windowArea, pointerChangeAreas, windowHotAreas);
1478         if (windowsHotAreas_.find(windowId) == windowsHotAreas_.end()) {
1479             windowsHotAreas_.emplace(windowId, windowHotAreas);
1480         } else {
1481             windowsHotAreas_[windowId] = windowHotAreas;
1482         }
1483     }
1484 }
1485 
UpdateTopBottomArea(const Rect & windowArea,std::vector<int32_t> & pointerChangeAreas,std::vector<Rect> & windowHotAreas)1486 void InputWindowsManager::UpdateTopBottomArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
1487     std::vector<Rect> &windowHotAreas)
1488 {
1489     CALL_DEBUG_ENTER;
1490     Rect newTopRect;
1491     newTopRect.x = windowArea.x + pointerChangeAreas[TOP_LEFT_AREA];
1492     newTopRect.y = windowArea.y - pointerChangeAreas[TOP_LEFT_AREA];
1493     newTopRect.width = windowArea.width - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[TOP_RIGHT_AREA];
1494     newTopRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_AREA];
1495     Rect newBottomRect;
1496     newBottomRect.x = windowArea.x + pointerChangeAreas[BOTTOM_LEFT_AREA];
1497     newBottomRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_AREA];
1498     newBottomRect.width = windowArea.width - pointerChangeAreas[BOTTOM_LEFT_AREA] -
1499         pointerChangeAreas[BOTTOM_RIGHT_AREA];
1500     newBottomRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_AREA];
1501     if (pointerChangeAreas[TOP_AREA] == 0) {
1502         newTopRect.width = 0;
1503         newTopRect.height = 0;
1504     }
1505     if (pointerChangeAreas[BOTTOM_AREA] == 0) {
1506         newBottomRect.width = 0;
1507         newBottomRect.height = 0;
1508     }
1509     windowHotAreas.push_back(newTopRect);
1510     windowHotAreas.push_back(newBottomRect);
1511 }
1512 
UpdateLeftRightArea(const Rect & windowArea,std::vector<int32_t> & pointerChangeAreas,std::vector<Rect> & windowHotAreas)1513 void InputWindowsManager::UpdateLeftRightArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
1514     std::vector<Rect> &windowHotAreas)
1515 {
1516     CALL_DEBUG_ENTER;
1517     Rect newLeftRect;
1518     newLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
1519     newLeftRect.y = windowArea.y + pointerChangeAreas[TOP_LEFT_AREA];
1520     newLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[LEFT_AREA];
1521     newLeftRect.height = windowArea.height - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[BOTTOM_LEFT_AREA];
1522     Rect newRightRect;
1523     newRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[RIGHT_AREA];
1524     newRightRect.y = windowArea.y + pointerChangeAreas[TOP_RIGHT_AREA];
1525     newRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[RIGHT_AREA];
1526     newRightRect.height = windowArea.height - pointerChangeAreas[TOP_RIGHT_AREA] -
1527         pointerChangeAreas[BOTTOM_RIGHT_AREA];
1528     if (pointerChangeAreas[LEFT_AREA] == 0) {
1529         newLeftRect.width = 0;
1530         newLeftRect.height = 0;
1531     }
1532     if (pointerChangeAreas[RIGHT_AREA] == 0) {
1533         newRightRect.width = 0;
1534         newRightRect.height = 0;
1535     }
1536     windowHotAreas.push_back(newLeftRect);
1537     windowHotAreas.push_back(newRightRect);
1538 }
1539 
UpdateInnerAngleArea(const Rect & windowArea,std::vector<int32_t> & pointerChangeAreas,std::vector<Rect> & windowHotAreas)1540 void InputWindowsManager::UpdateInnerAngleArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
1541     std::vector<Rect> &windowHotAreas)
1542 {
1543     CALL_DEBUG_ENTER;
1544     Rect newTopLeftRect;
1545     newTopLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
1546     newTopLeftRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
1547     newTopLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
1548     newTopLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
1549     Rect newTopRightRect;
1550     newTopRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[TOP_RIGHT_AREA];
1551     newTopRightRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
1552     newTopRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
1553     newTopRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
1554     Rect newBottomLeftRect;
1555     newBottomLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
1556     newBottomLeftRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_LEFT_AREA];
1557     newBottomLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
1558     newBottomLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
1559     Rect newBottomRightRect;
1560     newBottomRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[BOTTOM_RIGHT_AREA];
1561     newBottomRightRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_RIGHT_AREA];
1562     newBottomRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
1563     newBottomRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
1564     if (pointerChangeAreas[TOP_LEFT_AREA] == 0) {
1565         newTopLeftRect.width = 0;
1566         newTopLeftRect.height = 0;
1567     }
1568     if (pointerChangeAreas[TOP_RIGHT_AREA] == 0) {
1569         newTopRightRect.width = 0;
1570         newTopRightRect.height = 0;
1571     }
1572     if (pointerChangeAreas[BOTTOM_LEFT_AREA] == 0) {
1573         newBottomLeftRect.width = 0;
1574         newBottomLeftRect.height = 0;
1575     }
1576     if (pointerChangeAreas[BOTTOM_RIGHT_AREA] == 0) {
1577         newBottomRightRect.width = 0;
1578         newBottomRightRect.height = 0;
1579     }
1580 
1581     windowHotAreas.push_back(newTopLeftRect);
1582     windowHotAreas.push_back(newTopRightRect);
1583     windowHotAreas.push_back(newBottomLeftRect);
1584     windowHotAreas.push_back(newBottomRightRect);
1585 }
1586 
UpdatePointerEvent(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> & pointerEvent,const WindowInfo & touchWindow)1587 void InputWindowsManager::UpdatePointerEvent(int32_t logicalX, int32_t logicalY,
1588     const std::shared_ptr<PointerEvent>& pointerEvent, const WindowInfo& touchWindow)
1589 {
1590     CHKPV(pointerEvent);
1591     MMI_HILOGD("LastWindowInfo:%{public}d, touchWindow:%{public}d", lastWindowInfo_.id, touchWindow.id);
1592     if (lastWindowInfo_.id != touchWindow.id) {
1593         DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
1594         lastLogicX_ = logicalX;
1595         lastLogicY_ = logicalY;
1596         lastPointerEvent_ = pointerEvent;
1597         lastWindowInfo_ = touchWindow;
1598         DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
1599         return;
1600     }
1601     lastLogicX_ = logicalX;
1602     lastLogicY_ = logicalY;
1603     lastPointerEvent_ = pointerEvent;
1604     lastWindowInfo_ = touchWindow;
1605 }
1606 
SetHoverScrollState(bool state)1607 int32_t InputWindowsManager::SetHoverScrollState(bool state)
1608 {
1609     CALL_DEBUG_ENTER;
1610     MMI_HILOGD("Set mouse hover scroll state:%{public}d", state);
1611     std::string name = "isEnableHoverScroll";
1612     return PreferencesMgr->SetBoolValue(name, mouseFileName, state);
1613 }
1614 
GetHoverScrollState() const1615 bool InputWindowsManager::GetHoverScrollState() const
1616 {
1617     CALL_DEBUG_ENTER;
1618     std::string name = "isEnableHoverScroll";
1619     bool state = PreferencesMgr->GetBoolValue(name, true);
1620     MMI_HILOGD("Get mouse hover scroll state:%{public}d", state);
1621     return state;
1622 }
1623 
UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)1624 int32_t InputWindowsManager::UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)
1625 {
1626     CALL_DEBUG_ENTER;
1627     CHKPR(pointerEvent, ERROR_NULL_POINTER);
1628     auto displayId = pointerEvent->GetTargetDisplayId();
1629     if (!UpdateDisplayId(displayId)) {
1630         MMI_HILOGE("This display:%{public}d is not existent", displayId);
1631         return RET_ERR;
1632     }
1633     pointerEvent->SetTargetDisplayId(displayId);
1634 
1635     int32_t pointerId = pointerEvent->GetPointerId();
1636     PointerEvent::PointerItem pointerItem;
1637     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
1638         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
1639         return RET_ERR;
1640     }
1641     auto physicalDisplayInfo = GetPhysicalDisplay(displayId);
1642     CHKPR(physicalDisplayInfo, ERROR_NULL_POINTER);
1643     int32_t logicalX = 0;
1644     int32_t logicalY = 0;
1645     if (!AddInt32(pointerItem.GetDisplayX(), physicalDisplayInfo->x, logicalX)) {
1646         MMI_HILOGE("The addition of logicalX overflows");
1647         return RET_ERR;
1648     }
1649     if (!AddInt32(pointerItem.GetDisplayY(), physicalDisplayInfo->y, logicalY)) {
1650         MMI_HILOGE("The addition of logicalY overflows");
1651         return RET_ERR;
1652     }
1653     auto touchWindow = SelectWindowInfo(logicalX, logicalY, pointerEvent);
1654     if (!touchWindow) {
1655         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN || mouseDownInfo_.id == -1) {
1656             MMI_HILOGE("touchWindow is nullptr, targetWindow:%{public}d", pointerEvent->GetTargetWindowId());
1657             return RET_ERR;
1658         }
1659         touchWindow = std::make_optional(mouseDownInfo_);
1660         pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1661         MMI_HILOGI("mouse event send cancel, window:%{public}d, pid:%{public}d", touchWindow->id, touchWindow->pid);
1662     }
1663 
1664     bool checkFlag = pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_UPDATE ||
1665         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
1666         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END;
1667     if (checkFlag) {
1668         if ((!GetHoverScrollState()) && (displayGroupInfo_.focusWindowId != touchWindow->id)) {
1669             MMI_HILOGD("disable mouse hover scroll in inactive window, targetWindowId:%{public}d", touchWindow->id);
1670             return RET_OK;
1671         }
1672     }
1673 
1674     PointerStyle pointerStyle;
1675     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1676         if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1677             MMI_HILOGD("turn the mouseDisplay from false to true");
1678             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
1679         }
1680         pointerStyle = IPointerDrawingManager::GetInstance()->GetLastMouseStyle();
1681         MMI_HILOGD("showing the lastMouseStyle %{public}d, lastPointerStyle %{public}d",
1682             pointerStyle.id, lastPointerStyle_.id);
1683     } else {
1684         int32_t ret = GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
1685         if (ret != RET_OK) {
1686             MMI_HILOGE("Get pointer style failed, pointerStyleInfo is nullptr");
1687             return ret;
1688         }
1689         if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1690             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
1691             DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
1692         }
1693         IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
1694         WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
1695         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1696     }
1697     GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
1698     dragPointerStyle_ = pointerStyle;
1699     if (!touchWindow) {
1700         MMI_HILOGE("TouchWindow is nullptr");
1701         return RET_ERR;
1702     }
1703     WindowInfo window = *touchWindow;
1704     if (!dragFlag_) {
1705         SelectPointerChangeArea(window, pointerStyle, logicalX, logicalY);
1706         dragPointerStyle_ = pointerStyle;
1707         MMI_HILOGD("pointerStyle is :%{public}d, windowId is :%{public}d, logicalX is :%{public}d,"
1708             "logicalY is :%{public}d", pointerStyle.id, window.id, logicalX, logicalY);
1709     }
1710     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1711         dragFlag_ = true;
1712         MMI_HILOGD("Is in drag scene");
1713     }
1714     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
1715         dragFlag_ = false;
1716     }
1717     Direction direction = DIRECTION0;
1718     if (physicalDisplayInfo->displayDirection == DIRECTION0) {
1719         direction = physicalDisplayInfo->direction;
1720     }
1721     IPointerDrawingManager::GetInstance()->DrawPointer(displayId, static_cast<int32_t>(absolutionX_),
1722         static_cast<int32_t>(absolutionY_), dragPointerStyle_, direction);
1723 
1724     if (captureModeInfo_.isCaptureMode && (touchWindow->id != captureModeInfo_.windowId)) {
1725         captureModeInfo_.isCaptureMode = false;
1726     }
1727     pointerEvent->SetTargetWindowId(touchWindow->id);
1728     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
1729     auto windowX = logicalX - touchWindow->area.x;
1730     auto windowY = logicalY - touchWindow->area.y;
1731     if (!(touchWindow->transform.empty())) {
1732         auto windowXY = TransformWindowXY(*touchWindow, logicalX, logicalY);
1733         windowX = windowXY.first;
1734         windowY = windowXY.second;
1735     }
1736     pointerItem.SetWindowX(windowX);
1737     pointerItem.SetWindowY(windowY);
1738     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
1739     if ((extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE)) ||
1740         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
1741         pointerEvent->SetBuffer(extraData_.buffer);
1742         UpdatePointerAction(pointerEvent);
1743     } else {
1744         pointerEvent->ClearBuffer();
1745     }
1746     CHKPR(udsServer_, ERROR_NULL_POINTER);
1747 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1748     UpdatePointerEvent(logicalX, logicalY, pointerEvent, *touchWindow);
1749 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1750 #ifdef OHOS_BUILD_ENABLE_ANCO
1751     if (touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY)) {
1752         MMI_HILOGD("Process mouse event in Anco window, targetWindowId:%{public}d", touchWindow->id);
1753         SimulatePointerExt(pointerEvent);
1754         return RET_OK;
1755     }
1756 #endif // OHOS_BUILD_ENABLE_ANCO
1757     int32_t action = pointerEvent->GetPointerAction();
1758     if (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1759         mouseDownInfo_ = *touchWindow;
1760     }
1761     if (action == PointerEvent::POINTER_ACTION_BUTTON_UP) {
1762         InitMouseDownInfo();
1763         MMI_HILOGD("Mouse up, clear mouse down info");
1764     }
1765     MMI_HILOGD("pid:%{public}d,id:%{public}d,agentWindowId:%{public}d,"
1766                "logicalX:%{public}d,logicalY:%{public}d,"
1767                "displayX:%{public}d,displayY:%{public}d,windowX:%{public}d,windowY:%{public}d",
1768                touchWindow->pid, touchWindow->id, touchWindow->agentWindowId,
1769                logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
1770     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
1771         MMI_HILOGD("Clear extra data");
1772         ClearExtraData();
1773     }
1774     return ERR_OK;
1775 }
1776 #endif // OHOS_BUILD_ENABLE_POINTER
1777 
SetMouseCaptureMode(int32_t windowId,bool isCaptureMode)1778 int32_t InputWindowsManager::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
1779 {
1780     if (windowId < 0) {
1781         MMI_HILOGE("Windowid(%{public}d) is invalid", windowId);
1782         return RET_ERR;
1783     }
1784     if ((captureModeInfo_.isCaptureMode == isCaptureMode) && !isCaptureMode) {
1785         MMI_HILOGE("Windowid:(%{public}d) is not capture mode", windowId);
1786         return RET_OK;
1787     }
1788     captureModeInfo_.windowId = windowId;
1789     captureModeInfo_.isCaptureMode = isCaptureMode;
1790     MMI_HILOGI("Windowid:(%{public}d) is (%{public}d)", windowId, isCaptureMode);
1791     return RET_OK;
1792 }
1793 
GetMouseIsCaptureMode() const1794 bool InputWindowsManager::GetMouseIsCaptureMode() const
1795 {
1796     return captureModeInfo_.isCaptureMode;
1797 }
1798 
IsNeedDrawPointer(PointerEvent::PointerItem & pointerItem) const1799 bool InputWindowsManager::IsNeedDrawPointer(PointerEvent::PointerItem &pointerItem) const
1800 {
1801     if (pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN) {
1802         std::shared_ptr<InputDevice> inputDevice = InputDevMgr->GetInputDevice(pointerItem.GetDeviceId());
1803         if (inputDevice != nullptr) {
1804             MMI_HILOGD("name:%{public}s type:%{public}d bus:%{public}d, "
1805                        "version:%{public}d product:%{public}d vendor:%{public}d, "
1806                        "phys:%{public}s uniq:%{public}s",
1807                        inputDevice->GetName().c_str(), inputDevice->GetType(), inputDevice->GetBus(),
1808                        inputDevice->GetVersion(), inputDevice->GetProduct(), inputDevice->GetVendor(),
1809                        inputDevice->GetPhys().c_str(), inputDevice->GetUniq().c_str());
1810         }
1811         if (inputDevice != nullptr && inputDevice->GetBus() == BUS_USB) {
1812             return true;
1813         }
1814     }
1815     return false;
1816 }
1817 
1818 #ifdef OHOS_BUILD_ENABLE_TOUCH
UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)1819 int32_t InputWindowsManager::UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)
1820 {
1821     CALL_DEBUG_ENTER;
1822     CHKPR(pointerEvent, ERROR_NULL_POINTER);
1823     auto displayId = pointerEvent->GetTargetDisplayId();
1824     if (!UpdateDisplayId(displayId)) {
1825         MMI_HILOGE("This display is not existent");
1826         return RET_ERR;
1827     }
1828     pointerEvent->SetTargetDisplayId(displayId);
1829 
1830     int32_t pointerId = pointerEvent->GetPointerId();
1831     PointerEvent::PointerItem pointerItem;
1832     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
1833         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
1834         return RET_ERR;
1835     }
1836     MMI_HILOGD("display:%{public}d", displayId);
1837     auto physicDisplayInfo = GetPhysicalDisplay(displayId);
1838     CHKPR(physicDisplayInfo, ERROR_NULL_POINTER);
1839     int32_t physicalX = pointerItem.GetDisplayX();
1840     int32_t physicalY = pointerItem.GetDisplayY();
1841     AdjustDisplayCoordinate(*physicDisplayInfo, physicalX, physicalY);
1842     int32_t logicalX = 0;
1843     int32_t logicalY = 0;
1844     if (!AddInt32(physicalX, physicDisplayInfo->x, logicalX)) {
1845         MMI_HILOGE("The addition of logicalX overflows");
1846         return RET_ERR;
1847     }
1848     if (!AddInt32(physicalY, physicDisplayInfo->y, logicalY)) {
1849         MMI_HILOGE("The addition of logicalY overflows");
1850         return RET_ERR;
1851     }
1852     WindowInfo *touchWindow = nullptr;
1853     auto targetWindowId = pointerItem.GetTargetWindowId();
1854     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
1855     for (auto &item : windowsInfo) {
1856         for (const auto &win : item.defaultHotAreas) {
1857             MMI_HILOGE("defaultHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
1858                 win.x, win.y, win.width, win.height);
1859         }
1860         bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
1861             !IsValidZorderWindow(item, pointerEvent);
1862         if (checkWindow) {
1863             MMI_HILOGD("Skip the untouchable or invalid zOrder window to continue searching, "
1864                        "window:%{public}d, flags:%{public}d", item.id, item.flags);
1865             continue;
1866         }
1867 
1868         bool checkToolType = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
1869             ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
1870             pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
1871         checkToolType = checkToolType || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
1872         if (checkToolType) {
1873             if (IsInHotArea(logicalX, logicalY, item.defaultHotAreas, item)) {
1874                 touchWindow = &item;
1875                 break;
1876             } else {
1877                 continue;
1878             }
1879         }
1880         if (targetWindowId >= 0) {
1881             if (item.id == targetWindowId) {
1882                 touchWindow = &item;
1883                 break;
1884             }
1885         } else if (IsInHotArea(logicalX, logicalY, item.defaultHotAreas, item)) {
1886             touchWindow = &item;
1887             break;
1888         }
1889     }
1890     if (touchWindow == nullptr) {
1891         auto it = touchItemDownInfos_.find(pointerId);
1892         if (it == touchItemDownInfos_.end() ||
1893             pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
1894             MMI_HILOGE("The touchWindow is nullptr, logicalX:%{public}d, logicalY:%{public}d",
1895                 logicalX, logicalY);
1896             return RET_ERR;
1897         }
1898         touchWindow = &it->second;
1899         pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1900         MMI_HILOGD("touch event send cancel, window:%{public}d", touchWindow->id);
1901     }
1902 #ifdef OHOS_BUILD_ENABLE_ANCO
1903     bool isInAnco =  touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY);
1904     if (isInAnco) {
1905         MMI_HILOGD("Process touch screen event in Anco window, targetWindowId:%{public}d", touchWindow->id);
1906         bool isCompensatePointer = pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) &&
1907             MMI_GE(pointerEvent->GetZOrder(), 0.0f);
1908         if (isCompensatePointer) {
1909             SimulatePointerExt(pointerEvent);
1910         } else {
1911             if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
1912                 std::unordered_map<std::string, std::string> mapPayload;
1913                 mapPayload["msg"] = "";
1914                 constexpr int32_t touchDownBoost = 1006;
1915                 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
1916                     OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchDownBoost, mapPayload);
1917             } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP) {
1918                 constexpr int32_t touchUpBoost = 1007;
1919                 std::unordered_map<std::string, std::string> mapPayload;
1920                 mapPayload["msg"] = "";
1921                 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
1922                     OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchUpBoost, mapPayload);
1923             }
1924         }
1925         return RET_OK;
1926     }
1927 #endif // OHOS_BUILD_ENABLE_ANCO
1928     auto windowX = logicalX - touchWindow->area.x;
1929     auto windowY = logicalY - touchWindow->area.y;
1930     if (!(touchWindow->transform.empty())) {
1931         auto windowXY = TransformWindowXY(*touchWindow, logicalX, logicalY);
1932         windowX = windowXY.first;
1933         windowY = windowXY.second;
1934     }
1935     MMI_HILOGE("touch event send to window:%{public}d", touchWindow->id);
1936     pointerEvent->SetTargetWindowId(touchWindow->id);
1937     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
1938     pointerItem.SetDisplayX(physicalX);
1939     pointerItem.SetDisplayY(physicalY);
1940     pointerItem.SetWindowX(windowX);
1941     pointerItem.SetWindowY(windowY);
1942     pointerItem.SetToolWindowX(pointerItem.GetToolDisplayX() + physicDisplayInfo->x - touchWindow->area.x);
1943     pointerItem.SetToolWindowY(pointerItem.GetToolDisplayY() + physicDisplayInfo->y - touchWindow->area.y);
1944     pointerItem.SetTargetWindowId(touchWindow->id);
1945     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
1946     bool checkExtraData = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
1947         ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
1948         pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
1949     checkExtraData = checkExtraData || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
1950     if (checkExtraData) {
1951         pointerEvent->SetBuffer(extraData_.buffer);
1952         UpdatePointerAction(pointerEvent);
1953         PullEnterLeaveEvent(logicalX, logicalY, pointerEvent, touchWindow);
1954     } else {
1955         pointerEvent->ClearBuffer();
1956         lastTouchEvent_ = nullptr;
1957         lastTouchWindowInfo_.id = -1;
1958     }
1959     MMI_HILOGI("pid:%{public}d, targetWindowId:%{public}d, foucsWindowId:%{public}d, eventId:%{public}d,"
1960                " displayX:%{public}d, displayY:%{public}d, windowX:%{public}d, windowY:%{public}d, width:%{public}d,"
1961                " height:%{public}d,", touchWindow->pid, touchWindow->id, displayGroupInfo_.focusWindowId,
1962                pointerEvent->GetId(), physicalX, physicalY, windowX, windowY, touchWindow->area.width,
1963                touchWindow->area.height);
1964     MMI_HILOGD("logicalX:%{public}d,logicalY:%{public}d,"
1965                "physicalX:%{public}d,physicalY:%{public}d,windowX:%{public}d,windowY:%{public}d,"
1966                "displayId:%{public}d,TargetWindowId:%{public}d,AgentWindowId:%{public}d",
1967                logicalX, logicalY, physicalX, physicalY, windowX, windowY, displayId,
1968                pointerEvent->GetTargetWindowId(), pointerEvent->GetAgentWindowId());
1969     if (IsNeedDrawPointer(pointerItem)) {
1970         if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1971             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
1972             DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
1973         }
1974         PointerStyle pointerStyle;
1975         int32_t ret = GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
1976         if (ret != RET_OK) {
1977             MMI_HILOGE("Get pointer style failed, pointerStyleInfo is nullptr");
1978             return ret;
1979         }
1980         IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicDisplayInfo);
1981         WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
1982         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1983         IPointerDrawingManager::GetInstance()->DrawPointer(displayId,
1984             pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), pointerStyle, physicDisplayInfo->direction);
1985     } else {
1986         if (IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1987             if (!checkExtraData) {
1988                 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
1989                 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
1990             }
1991         }
1992     }
1993 
1994     int32_t pointerAction = pointerEvent->GetPointerAction();
1995     if (pointerAction == PointerEvent::POINTER_ACTION_DOWN) {
1996         touchItemDownInfos_.insert(std::make_pair(pointerId, *touchWindow));
1997     }
1998     if (pointerAction == PointerEvent::POINTER_ACTION_UP) {
1999         auto iter = touchItemDownInfos_.find(pointerId);
2000         if (iter != touchItemDownInfos_.end()) {
2001             touchItemDownInfos_.erase(iter);
2002             MMI_HILOGD("Clear the touch info, action is up, pointerid:%{public}d", pointerId);
2003         }
2004     }
2005     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
2006         MMI_HILOGD("Clear extra data");
2007         ClearExtraData();
2008     }
2009     return ERR_OK;
2010 }
2011 
PullEnterLeaveEvent(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> pointerEvent,const WindowInfo * touchWindow)2012 void InputWindowsManager::PullEnterLeaveEvent(int32_t logicalX, int32_t logicalY,
2013     const std::shared_ptr<PointerEvent> pointerEvent, const WindowInfo* touchWindow)
2014 {
2015     CHKPV(pointerEvent);
2016     CHKPV(touchWindow);
2017     MMI_HILOGD("LastTouchWindowInfo:%{public}d, touchWindow:%{public}d", lastTouchWindowInfo_.id, touchWindow->id);
2018     if (lastTouchWindowInfo_.id != -1 && lastTouchWindowInfo_.id != touchWindow->id) {
2019         DispatchTouch(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
2020         lastTouchLogicX_ = logicalX;
2021         lastTouchLogicY_ = logicalY;
2022         lastTouchEvent_ = pointerEvent;
2023         lastTouchWindowInfo_ = *touchWindow;
2024         DispatchTouch(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
2025         return;
2026     }
2027     lastTouchLogicX_ = logicalX;
2028     lastTouchLogicY_ = logicalY;
2029     lastTouchEvent_ = pointerEvent;
2030     lastTouchWindowInfo_ = *touchWindow;
2031 }
2032 
DispatchTouch(int32_t pointerAction)2033 void InputWindowsManager::DispatchTouch(int32_t pointerAction)
2034 {
2035     CALL_INFO_TRACE;
2036     CHKPV(udsServer_);
2037     CHKPV(lastTouchEvent_);
2038     if (pointerAction == PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
2039         WindowInfo *touchWindow = nullptr;
2040         for (auto item : displayGroupInfo_.windowsInfo) {
2041             if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
2042                 MMI_HILOGD("Skip the untouchable window to continue searching, "
2043                     "window:%{public}d, flags:%{public}d", item.id, item.flags);
2044                 continue;
2045             }
2046             if (IsInHotArea(lastTouchLogicX_, lastTouchLogicY_, item.defaultHotAreas, item)) {
2047                 touchWindow = &item;
2048                 break;
2049             }
2050         }
2051         if (touchWindow == nullptr) {
2052             MMI_HILOGE("touchWindow is nullptr");
2053             return;
2054         }
2055         if (touchWindow->id != lastTouchWindowInfo_.id) {
2056             lastTouchWindowInfo_ = *touchWindow;
2057         }
2058     }
2059     auto pointerEvent = PointerEvent::Create();
2060     CHKPV(pointerEvent);
2061     PointerEvent::PointerItem lastPointerItem;
2062     int32_t lastPointerId = lastTouchEvent_->GetPointerId();
2063     if (!lastTouchEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
2064         MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
2065         return;
2066     }
2067     PointerEvent::PointerItem currentPointerItem;
2068     currentPointerItem.SetWindowX(lastTouchLogicX_ - lastTouchWindowInfo_.area.x);
2069     currentPointerItem.SetWindowY(lastTouchLogicY_ - lastTouchWindowInfo_.area.y);
2070     currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
2071     currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
2072     currentPointerItem.SetPointerId(lastPointerId);
2073 
2074     pointerEvent->UpdateId();
2075     pointerEvent->SetTargetDisplayId(lastTouchEvent_->GetTargetDisplayId());
2076     pointerEvent->SetTargetWindowId(lastTouchWindowInfo_.id);
2077     pointerEvent->SetAgentWindowId(lastTouchWindowInfo_.agentWindowId);
2078     pointerEvent->SetPointerId(lastPointerId);
2079     pointerEvent->AddPointerItem(currentPointerItem);
2080     pointerEvent->SetPointerAction(pointerAction);
2081     pointerEvent->SetBuffer(extraData_.buffer);
2082     pointerEvent->SetSourceType(lastTouchEvent_->GetSourceType());
2083     int64_t time = GetSysClockTime();
2084     pointerEvent->SetActionTime(time);
2085     pointerEvent->SetActionStartTime(time);
2086     pointerEvent->SetDeviceId(lastTouchEvent_->GetDeviceId());
2087     auto fd = udsServer_->GetClientFd(lastTouchWindowInfo_.pid);
2088     auto sess = udsServer_->GetSession(fd);
2089     if (sess == nullptr) {
2090         MMI_HILOGI("The last window has disappeared");
2091         return;
2092     }
2093 
2094     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
2095     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
2096     if (!sess->SendMsg(pkt)) {
2097         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
2098         return;
2099     }
2100 }
2101 #endif // OHOS_BUILD_ENABLE_TOUCH
2102 
2103 #ifdef OHOS_BUILD_ENABLE_POINTER
UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)2104 int32_t InputWindowsManager::UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)
2105 {
2106     CALL_DEBUG_ENTER;
2107     int32_t pointerAction =  pointerEvent->GetPointerAction();
2108     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
2109     switch (pointerAction) {
2110         case PointerEvent::POINTER_ACTION_BUTTON_DOWN:
2111         case PointerEvent::POINTER_ACTION_BUTTON_UP:
2112         case PointerEvent::POINTER_ACTION_MOVE: {
2113             return UpdateMouseTarget(pointerEvent);
2114         }
2115         case PointerEvent::POINTER_ACTION_DOWN: {
2116             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
2117             pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
2118             pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
2119             return UpdateMouseTarget(pointerEvent);
2120         }
2121         case PointerEvent::POINTER_ACTION_UP: {
2122             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
2123             pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
2124             pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
2125             return UpdateMouseTarget(pointerEvent);
2126         }
2127         default: {
2128             MMI_HILOGE("pointer action is unknown, pointerAction:%{public}d", pointerAction);
2129             return RET_ERR;
2130         }
2131     }
2132     return RET_OK;
2133 }
2134 #endif // OHOS_BUILD_ENABLE_POINTER
2135 
2136 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)2137 int32_t InputWindowsManager::UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)
2138 {
2139     CALL_DEBUG_ENTER;
2140     CHKPR(pointerEvent, ERROR_NULL_POINTER);
2141     int32_t focusWindowId = displayGroupInfo_.focusWindowId;
2142     const WindowInfo* windowInfo = nullptr;
2143     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
2144     for (const auto &item : windowsInfo) {
2145         if (item.id == focusWindowId) {
2146             windowInfo = &item;
2147             break;
2148         }
2149     }
2150     CHKPR(windowInfo, ERROR_NULL_POINTER);
2151     pointerEvent->SetTargetWindowId(windowInfo->id);
2152     pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
2153     MMI_HILOGD("focusWindow:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
2154 
2155     return RET_OK;
2156 }
2157 #endif // OHOS_BUILD_ENABLE_JOYSTICK
2158 
2159 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
DrawTouchGraphic(std::shared_ptr<PointerEvent> pointerEvent)2160 void InputWindowsManager::DrawTouchGraphic(std::shared_ptr<PointerEvent> pointerEvent)
2161 {
2162     CALL_DEBUG_ENTER;
2163     if (!haveSetObserver_) {
2164         showCursor_.SwitchName = showCursorSwitchName;
2165         CreateStatusConfigObserver(showCursor_);
2166         haveSetObserver_ = true;
2167     }
2168     if (!showCursor_.isShow) {
2169         MMI_HILOGD("The touchCursor does not need to be displayed.");
2170         return;
2171     }
2172     CHKPV(pointerEvent);
2173     auto displayId = pointerEvent->GetTargetDisplayId();
2174     if (!UpdateDisplayId(displayId)) {
2175         MMI_HILOGE("This display is nonexistent");
2176         return;
2177     }
2178     auto physicDisplayInfo = GetPhysicalDisplay(displayId);
2179     CHKPV(physicDisplayInfo);
2180     TOUCH_DRAWING_MANAGER->UpdateDisplayInfo(*physicDisplayInfo);
2181     TOUCH_DRAWING_MANAGER->TouchDrawHandler(pointerEvent);
2182 }
2183 
2184 template <class T>
CreateStatusConfigObserver(T & item)2185 void InputWindowsManager::CreateStatusConfigObserver(T& item)
2186 {
2187     CALL_DEBUG_ENTER;
2188     SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
2189         bool statusValue = false;
2190         auto ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
2191             .GetBoolValue(key, statusValue);
2192         if (ret != RET_OK) {
2193             MMI_HILOGE("Get value from setting date fail");
2194             return;
2195         }
2196         item.isShow = statusValue;
2197         MMI_HILOGI("key: %{public}s, statusValue: %{public}d", key.c_str(), statusValue);
2198     };
2199     sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
2200         .CreateObserver(item.SwitchName, updateFunc);
2201     ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
2202     if (ret != ERR_OK) {
2203         MMI_HILOGE("register setting observer failed, ret=%{public}d", ret);
2204         statusObserver = nullptr;
2205     }
2206 }
2207 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2208 
2209 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)2210 int32_t InputWindowsManager::UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)
2211 {
2212     CALL_DEBUG_ENTER;
2213     CHKPR(pointerEvent, ERROR_NULL_POINTER);
2214     auto source = pointerEvent->GetSourceType();
2215     switch (source) {
2216 #ifdef OHOS_BUILD_ENABLE_TOUCH
2217         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
2218             return UpdateTouchScreenTarget(pointerEvent);
2219         }
2220 #endif // OHOS_BUILD_ENABLE_TOUCH
2221 #ifdef OHOS_BUILD_ENABLE_POINTER
2222         case PointerEvent::SOURCE_TYPE_MOUSE: {
2223             return UpdateMouseTarget(pointerEvent);
2224         }
2225         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
2226             return UpdateTouchPadTarget(pointerEvent);
2227         }
2228 #endif // OHOS_BUILD_ENABLE_POINTER
2229 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
2230         case PointerEvent::SOURCE_TYPE_JOYSTICK: {
2231             return UpdateJoystickTarget(pointerEvent);
2232         }
2233 #endif // OHOS_BUILD_ENABLE_JOYSTICK
2234         default: {
2235             MMI_HILOGE("Source type is unknown, source:%{public}d", source);
2236             break;
2237         }
2238     }
2239     return RET_ERR;
2240 }
2241 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2242 
2243 #ifdef OHOS_BUILD_ENABLE_POINTER
IsInsideDisplay(const DisplayInfo & displayInfo,int32_t physicalX,int32_t physicalY)2244 bool InputWindowsManager::IsInsideDisplay(const DisplayInfo& displayInfo, int32_t physicalX, int32_t physicalY)
2245 {
2246     return (physicalX >= 0 && physicalX < displayInfo.width) && (physicalY >= 0 && physicalY < displayInfo.height);
2247 }
2248 
FindPhysicalDisplay(const DisplayInfo & displayInfo,int32_t & physicalX,int32_t & physicalY,int32_t & displayId)2249 void InputWindowsManager::FindPhysicalDisplay(const DisplayInfo& displayInfo, int32_t& physicalX,
2250     int32_t& physicalY, int32_t& displayId)
2251 {
2252     CALL_DEBUG_ENTER;
2253     int32_t logicalX = 0;
2254     int32_t logicalY = 0;
2255     if (!AddInt32(physicalX, displayInfo.x, logicalX)) {
2256         MMI_HILOGE("The addition of logicalX overflows");
2257         return;
2258     }
2259     if (!AddInt32(physicalY, displayInfo.y, logicalY)) {
2260         MMI_HILOGE("The addition of logicalY overflows");
2261         return;
2262     }
2263     for (const auto &item : displayGroupInfo_.displaysInfo) {
2264         int32_t displayMaxX = 0;
2265         int32_t displayMaxY = 0;
2266         if (!AddInt32(item.x, item.width, displayMaxX)) {
2267             MMI_HILOGE("The addition of displayMaxX overflows");
2268             return;
2269         }
2270         if (!AddInt32(item.y, item.height, displayMaxY)) {
2271             MMI_HILOGE("The addition of displayMaxY overflows");
2272             return;
2273         }
2274         if ((logicalX >= item.x && logicalX < displayMaxX) &&
2275             (logicalY >= item.y && logicalY < displayMaxY)) {
2276             physicalX = logicalX - item.x;
2277             physicalY = logicalY - item.y;
2278             displayId = item.id;
2279             break;
2280         }
2281     }
2282 }
2283 
CoordinateCorrection(int32_t width,int32_t height,int32_t & integerX,int32_t & integerY)2284 void InputWindowsManager::CoordinateCorrection(int32_t width, int32_t height, int32_t &integerX, int32_t &integerY)
2285 {
2286     if (integerX < 0) {
2287         integerX = 0;
2288     }
2289     if (integerX >= width) {
2290         integerX = width - 1;
2291     }
2292     if (integerY < 0) {
2293         integerY = 0;
2294     }
2295     if (integerY >= height) {
2296         integerY = height - 1;
2297     }
2298 }
2299 
GetWidthAndHeight(const DisplayInfo * displayInfo,int32_t & width,int32_t & height)2300 void InputWindowsManager::GetWidthAndHeight(const DisplayInfo* displayInfo, int32_t &width, int32_t &height)
2301 {
2302     if (displayInfo->displayDirection == DIRECTION0) {
2303         if (displayInfo->direction == DIRECTION0 || displayInfo->direction == DIRECTION180) {
2304             width = displayInfo->width;
2305             height = displayInfo->height;
2306         } else {
2307             height = displayInfo->width;
2308             width = displayInfo->height;
2309         }
2310     } else {
2311         width = displayInfo->width;
2312         height = displayInfo->height;
2313     }
2314 }
2315 
UpdateAndAdjustMouseLocation(int32_t & displayId,double & x,double & y)2316 void InputWindowsManager::UpdateAndAdjustMouseLocation(int32_t& displayId, double& x, double& y)
2317 {
2318     auto displayInfo = GetPhysicalDisplay(displayId);
2319     CHKPV(displayInfo);
2320     int32_t integerX = static_cast<int32_t>(x);
2321     int32_t integerY = static_cast<int32_t>(y);
2322     int32_t lastDisplayId = displayId;
2323     if (!IsInsideDisplay(*displayInfo, integerX, integerY)) {
2324         FindPhysicalDisplay(*displayInfo, integerX, integerY, displayId);
2325     }
2326     if (displayId != lastDisplayId) {
2327         displayInfo = GetPhysicalDisplay(displayId);
2328         CHKPV(displayInfo);
2329     }
2330     int32_t width = 0;
2331     int32_t height = 0;
2332     GetWidthAndHeight(displayInfo, width, height);
2333     CoordinateCorrection(width, height, integerX, integerY);
2334     x = static_cast<double>(integerX) + (x - floor(x));
2335     y = static_cast<double>(integerY) + (y - floor(y));
2336     absolutionX_ = x;
2337     absolutionY_ = y;
2338     if (displayInfo->displayDirection == DIRECTION0) {
2339         LogicalCoordinate coord {
2340             .x = integerX,
2341             .y = integerY,
2342         };
2343         RotateScreen(*displayInfo, coord);
2344         mouseLocation_.physicalX = coord.x;
2345         mouseLocation_.physicalY = coord.y;
2346     } else {
2347         mouseLocation_.physicalX = integerX;
2348         mouseLocation_.physicalY = integerY;
2349     }
2350     MMI_HILOGD("Mouse Data: physicalX:%{public}d,physicalY:%{public}d, displayId:%{public}d",
2351         mouseLocation_.physicalX, mouseLocation_.physicalY, displayId);
2352 }
2353 
GetMouseInfo()2354 MouseLocation InputWindowsManager::GetMouseInfo()
2355 {
2356     if (mouseLocation_.physicalX == -1 || mouseLocation_.physicalY == -1) {
2357         if (!displayGroupInfo_.displaysInfo.empty()) {
2358             mouseLocation_.physicalX = displayGroupInfo_.displaysInfo[0].width / 2;
2359             mouseLocation_.physicalY = displayGroupInfo_.displaysInfo[0].height / 2;
2360         }
2361     }
2362     return mouseLocation_;
2363 }
2364 #endif // OHOS_BUILD_ENABLE_POINTER
2365 
AppendExtraData(const ExtraData & extraData)2366 int32_t InputWindowsManager::AppendExtraData(const ExtraData& extraData)
2367 {
2368     CALL_DEBUG_ENTER;
2369     extraData_.appended = extraData.appended;
2370     extraData_.buffer = extraData.buffer;
2371     extraData_.sourceType = extraData.sourceType;
2372     extraData_.pointerId = extraData.pointerId;
2373     return RET_OK;
2374 }
2375 
ClearExtraData()2376 void InputWindowsManager::ClearExtraData()
2377 {
2378     CALL_DEBUG_ENTER;
2379     extraData_.appended = false;
2380     extraData_.buffer.clear();
2381     extraData_.sourceType = -1;
2382     extraData_.pointerId = -1;
2383 }
2384 
IsWindowVisible(int32_t pid)2385 bool InputWindowsManager::IsWindowVisible(int32_t pid)
2386 {
2387     CALL_DEBUG_ENTER;
2388     if (pid < 0) {
2389         MMI_HILOGE("pid is invalid");
2390         return true;
2391     }
2392     std::vector<sptr<Rosen::WindowVisibilityInfo>> infos;
2393     Rosen::WindowManager::GetInstance().GetVisibilityWindowInfo(infos);
2394     for (const auto &it: infos) {
2395         if (pid == it->pid_ &&
2396             it->visibilityState_ < Rosen::WindowVisibilityState::WINDOW_VISIBILITY_STATE_TOTALLY_OCCUSION) {
2397             MMI_HILOGD("pid:%{public}d has visible window", pid);
2398             return true;
2399         }
2400     }
2401     MMI_HILOGD("pid:%{public}d doesn't have visible window", pid);
2402     return false;
2403 }
2404 
UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)2405 void InputWindowsManager::UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)
2406 {
2407     CALL_DEBUG_ENTER;
2408     int32_t action = pointerEvent->GetPointerAction();
2409     switch (action) {
2410         case PointerEvent::POINTER_ACTION_MOVE: {
2411             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_MOVE);
2412             break;
2413         }
2414         case PointerEvent::POINTER_ACTION_BUTTON_UP:
2415         case PointerEvent::POINTER_ACTION_UP: {
2416             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_UP);
2417             break;
2418         }
2419         case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
2420             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
2421             break;
2422         }
2423         case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
2424             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
2425             break;
2426         }
2427         default: {
2428             MMI_HILOGI("Action is:%{public}d, no need change", action);
2429             break;
2430         }
2431     }
2432 }
2433 
Dump(int32_t fd,const std::vector<std::string> & args)2434 void InputWindowsManager::Dump(int32_t fd, const std::vector<std::string> &args)
2435 {
2436     CALL_DEBUG_ENTER;
2437     mprintf(fd, "Windows information:\t");
2438     mprintf(fd, "windowsInfos,num:%zu", displayGroupInfo_.windowsInfo.size());
2439     for (const auto &item : displayGroupInfo_.windowsInfo) {
2440         mprintf(fd, "  windowsInfos: id:%d | pid:%d | uid:%d | area.x:%d | area.y:%d "
2441                 "| area.width:%d | area.height:%d | defaultHotAreas.size:%zu "
2442                 "| pointerHotAreas.size:%zu | agentWindowId:%d | flags:%d "
2443                 "| action:%d | displayId:%d | zOrder:%f \t",
2444                 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
2445                 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
2446                 item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder);
2447         for (const auto &win : item.defaultHotAreas) {
2448             mprintf(fd, "\t defaultHotAreas: x:%d | y:%d | width:%d | height:%d \t",
2449                     win.x, win.y, win.width, win.height);
2450         }
2451         for (const auto &pointer : item.pointerHotAreas) {
2452             mprintf(fd, "\t pointerHotAreas: x:%d | y:%d | width:%d | height:%d \t",
2453                     pointer.x, pointer.y, pointer.width, pointer.height);
2454         }
2455 
2456         std::string dump;
2457         dump += StringPrintf("\t pointerChangeAreas: ");
2458         for (const auto &it : item.pointerChangeAreas) {
2459             dump += StringPrintf("%d | ", it);
2460         }
2461         dump += StringPrintf("\n\t transform: ");
2462         for (const auto &it : item.transform) {
2463             dump += StringPrintf("%f | ", it);
2464         }
2465         std::istringstream stream(dump);
2466         std::string line;
2467         while (std::getline(stream, line, '\n')) {
2468             mprintf(fd, "%s", line.c_str());
2469         }
2470     }
2471     mprintf(fd, "Displays information:\t");
2472     mprintf(fd, "displayInfos,num:%zu", displayGroupInfo_.displaysInfo.size());
2473     for (const auto &item : displayGroupInfo_.displaysInfo) {
2474         mprintf(fd, "\t displayInfos: id:%d | x:%d | y:%d | width:%d | height:%d | name:%s "
2475                 "| uniq:%s | direction:%d | displayDirection:%d | displayMode:%d \t",
2476                 item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
2477                 item.uniq.c_str(), item.direction, item.displayDirection, item.displayMode);
2478     }
2479     mprintf(fd, "Input device and display bind info:\n%s", bindInfo_.Dumps().c_str());
2480 #ifdef OHOS_BUILD_ENABLE_ANCO
2481     std::string ancoWindows;
2482     DumpAncoWindows(ancoWindows);
2483     mprintf(fd, "%s\n", ancoWindows.c_str());
2484 #endif // OHOS_BUILD_ENABLE_ANCO
2485 }
2486 
TransformWindowXY(const WindowInfo & window,int32_t logicX,int32_t logicY) const2487 std::pair<int32_t, int32_t> InputWindowsManager::TransformWindowXY(const WindowInfo &window,
2488     int32_t logicX, int32_t logicY) const
2489 {
2490     Matrix3f transform(window.transform);
2491     if (window.transform.size() != MATRIX3_SIZE || transform.IsIdentity()) {
2492         return {logicX, logicY};
2493     }
2494     Vector3f logicXY(logicX, logicY, 1.0);
2495     Vector3f windowXY = transform * logicXY;
2496     return {(int)(round(windowXY[0])), (int)(round(windowXY[1]))};
2497 }
2498 
IsValidZorderWindow(const WindowInfo & window,const std::shared_ptr<PointerEvent> & pointerEvent)2499 bool InputWindowsManager::IsValidZorderWindow(const WindowInfo &window,
2500     const std::shared_ptr<PointerEvent>& pointerEvent)
2501 {
2502     CHKPR(pointerEvent, false);
2503     if (!(pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) || MMI_LE(pointerEvent->GetZOrder(), 0.0f)) {
2504         return true;
2505     }
2506     if (MMI_GE(window.zOrder, pointerEvent->GetZOrder())) {
2507         MMI_HILOGE("current window zorder:%{public}f greater than the simulate target zOrder:%{public}f, "
2508             "ignore this window::%{public}d", window.zOrder, pointerEvent->GetZOrder(), window.id);
2509         return false;
2510     }
2511     return true;
2512 }
2513 } // namespace MMI
2514 } // namespace OHOS
2515