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 #include <unordered_map>
23 
24 #include "cJSON.h"
25 #include "display_manager.h"
26 #include "dfx_hisysevent.h"
27 #include "event_log_helper.h"
28 #include "fingersense_wrapper.h"
29 #include "input_device_manager.h"
30 #include "input_event_handler.h"
31 #include "i_pointer_drawing_manager.h"
32 #include "key_command_handler.h"
33 #include "mouse_event_normalize.h"
34 #include "pointer_drawing_manager.h"
35 #include "preferences.h"
36 #include "preferences_errno.h"
37 #include "preferences_helper.h"
38 #include "util.h"
39 #include "key_command_handler_util.h"
40 #include "mmi_matrix3.h"
41 #include "uds_session.h"
42 #include "util_ex.h"
43 #include "scene_board_judgement.h"
44 #include "i_preference_manager.h"
45 #include "parameters.h"
46 #include "setting_datashare.h"
47 #include "system_ability_definition.h"
48 #include "timer_manager.h"
49 #include "touch_drawing_manager.h"
50 #ifdef OHOS_RSS_CLIENT
51 #include "res_sched_client.h"
52 #include "res_type.h"
53 #endif // OHOS_RSS_CLIENT
54 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
55 #include "magic_pointer_velocity_tracker.h"
56 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
57 
58 #undef MMI_LOG_DOMAIN
59 #define MMI_LOG_DOMAIN MMI_LOG_WINDOW
60 #undef MMI_LOG_TAG
61 #define MMI_LOG_TAG "InputWindowsManager"
62 
63 namespace OHOS {
64 namespace MMI {
65 namespace {
66 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
67 constexpr int32_t DEFAULT_POINTER_STYLE { 0 };
68 constexpr int32_t CURSOR_CIRCLE_STYLE { 41 };
69 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
70 constexpr int32_t OUTWINDOW_HOT_AREA { 20 };
71 constexpr int32_t SCALE_X { 0 };
72 constexpr int32_t SCALE_Y { 4 };
73 constexpr int32_t TOP_LEFT_AREA { 0 };
74 constexpr int32_t TOP_AREA { 1 };
75 constexpr int32_t TOP_RIGHT_AREA { 2 };
76 constexpr int32_t RIGHT_AREA { 3 };
77 constexpr int32_t BOTTOM_RIGHT_AREA { 4 };
78 constexpr int32_t BOTTOM_AREA { 5 };
79 constexpr int32_t BOTTOM_LEFT_AREA { 6 };
80 constexpr int32_t LEFT_AREA { 7 };
81 constexpr int32_t WAIT_TIME_FOR_REGISTER { 2000 };
82 constexpr int32_t RS_PROCESS_TIMEOUT { 100 * 1000 };
83 constexpr int32_t HICAR_MIN_DISPLAY_ID { 1000 };
84 #ifdef OHOS_BUILD_ENABLE_ANCO
85 constexpr int32_t SHELL_WINDOW_COUNT { 1 };
86 #endif // OHOS_BUILD_ENABLE_ANCO
87 constexpr double HALF_RATIO { 0.5 };
88 constexpr int32_t TWOFOLD { 2 };
89 constexpr int32_t COMMON_PARAMETER_ERROR { 401 };
90 const std::string BIND_CFG_FILE_NAME { "/data/service/el1/public/multimodalinput/display_bind.cfg" };
91 const std::string MOUSE_FILE_NAME { "mouse_settings.xml" };
92 const std::string DEFAULT_ICON_PATH { "/system/etc/multimodalinput/mouse_icon/Default.svg" };
93 const std::string NAVIGATION_SWITCH_NAME { "settings.input.stylus_navigation_hint" };
94 const int32_t ROTATE_POLICY = system::GetIntParameter("const.window.device.rotate_policy", 0);
95 constexpr int32_t WINDOW_ROTATE { 0 };
96 constexpr int32_t FOLDABLE_DEVICE { 2 };
97 constexpr uint32_t FOLD_STATUS_MASK { 1U << 27U };
98 int32_t sourceTemp_ { -1 };
99 } // namespace
100 
101 enum PointerHotArea : int32_t {
102     TOP = 0,
103     BOTTOM = 1,
104     LEFT = 2,
105     RIGHT = 3,
106     TOP_LEFT = 4,
107     TOP_RIGHT = 5,
108     BOTTOM_LEFT = 6,
109     BOTTOM_RIGHT = 7,
110 };
111 
112 std::shared_ptr<IInputWindowsManager> IInputWindowsManager::instance_;
113 std::mutex IInputWindowsManager::mutex_;
114 
GetInstance()115 std::shared_ptr<IInputWindowsManager> IInputWindowsManager::GetInstance()
116 {
117     if (instance_ == nullptr) {
118         std::lock_guard<std::mutex> lock(mutex_);
119         if (instance_ == nullptr) {
120             instance_ = std::make_shared<InputWindowsManager>();
121         }
122     }
123     return instance_;
124 }
125 
DestroyInstance()126 void IInputWindowsManager::DestroyInstance()
127 {
128     std::lock_guard<std::mutex> lock(mutex_);
129     instance_.reset();
130 }
131 
InputWindowsManager()132 InputWindowsManager::InputWindowsManager() : bindInfo_(BIND_CFG_FILE_NAME)
133 {
134     MMI_HILOGI("Bind cfg file name:%{public}s", BIND_CFG_FILE_NAME.c_str());
135 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
136     lastWindowInfo_.id = -1;
137     lastWindowInfo_.pid = -1;
138     lastWindowInfo_.uid = -1;
139     lastWindowInfo_.agentWindowId = -1;
140     lastWindowInfo_.area = { 0, 0, 0, 0 };
141     lastWindowInfo_.flags = -1;
142     lastWindowInfo_.windowType = 0;
143     mouseDownInfo_.id = -1;
144     mouseDownInfo_.pid = -1;
145     mouseDownInfo_.uid = -1;
146     mouseDownInfo_.agentWindowId = -1;
147     mouseDownInfo_.area = { 0, 0, 0, 0 };
148     mouseDownInfo_.flags = -1;
149     mouseDownInfo_.windowType = 0;
150 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
151 #ifdef OHOS_BUILD_ENABLE_TOUCH
152     lastTouchWindowInfo_.id = -1;
153     lastTouchWindowInfo_.pid = -1;
154     lastTouchWindowInfo_.uid = -1;
155     lastTouchWindowInfo_.agentWindowId = -1;
156     lastTouchWindowInfo_.area = { 0, 0, 0, 0 };
157     lastTouchWindowInfo_.flags = -1;
158     lastTouchWindowInfo_.windowType = 0;
159 #endif // OHOS_BUILD_ENABLE_TOUCH
160     displayGroupInfoTmp_.focusWindowId = -1;
161     displayGroupInfoTmp_.width = 0;
162     displayGroupInfoTmp_.height = 0;
163     displayGroupInfo_.focusWindowId = -1;
164     displayGroupInfo_.width = 0;
165     displayGroupInfo_.height = 0;
166 }
167 
~InputWindowsManager()168 InputWindowsManager::~InputWindowsManager()
169 {
170     CALL_INFO_TRACE;
171 }
172 
DeviceStatusChanged(int32_t deviceId,const std::string & sysUid,const std::string devStatus)173 void InputWindowsManager::DeviceStatusChanged(int32_t deviceId, const std::string &sysUid, const std::string devStatus)
174 {
175     CALL_INFO_TRACE;
176     if (devStatus == "add") {
177         bindInfo_.AddInputDevice(deviceId, sysUid);
178     } else {
179         bindInfo_.RemoveInputDevice(deviceId);
180     }
181 }
182 
Init(UDSServer & udsServer)183 void InputWindowsManager::Init(UDSServer& udsServer)
184 {
185     udsServer_ = &udsServer;
186     CHKPV(udsServer_);
187     bindInfo_.Load();
188 #ifdef OHOS_BUILD_ENABLE_POINTER
189     udsServer_->AddSessionDeletedCallback([this] (SessionPtr session) { return this->OnSessionLost(session); });
190     InitMouseDownInfo();
191 #endif // OHOS_BUILD_ENABLE_POINTER
192     INPUT_DEV_MGR->SetInputStatusChangeCallback(
193         [this] (int32_t deviceId, const std::string &sysUid, const std::string devStatus) {
194             return this->DeviceStatusChanged(deviceId, sysUid, devStatus);
195         }
196         );
197 }
198 
IgnoreTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)199 bool InputWindowsManager::IgnoreTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)
200 {
201     CHKPF(pointerEvent);
202     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
203         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL) {
204         return false;
205     }
206     PointerEvent::PointerItem pointer {};
207     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointer)) {
208         MMI_HILOGE("Corrupted pointer event");
209         return false;
210     }
211     /* Fold status is indicated by 27th bit of long axis of touch. */
212     uint32_t longAxis = static_cast<uint32_t>(pointer.GetLongAxis());
213     if (cancelTouchStatus_) {
214         if (longAxis & FOLD_STATUS_MASK) {
215             // Screen in the process of folding, ignore this event
216             return true;
217         } else {
218             // Screen folding is complete
219             cancelTouchStatus_ = false;
220             return false;
221         }
222     } else if (longAxis & FOLD_STATUS_MASK) {
223         // The screen begins to collapse, reissues the cancel event, and ignores this event
224         MMI_HILOGI("Screen begins to collapse, reissue cancel event");
225         cancelTouchStatus_ = true;
226         ReissueCancelTouchEvent(pointerEvent);
227         return true;
228     }
229     return false;
230 }
231 
ReissueCancelTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)232 void InputWindowsManager::ReissueCancelTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)
233 {
234     CALL_INFO_TRACE;
235 #ifdef OHOS_BUILD_ENABLE_TOUCH
236     auto items = pointerEvent->GetAllPointerItems();
237     for (const auto &item : items) {
238         if (!item.IsPressed()) {
239             continue;
240         }
241         int32_t pointerId = item.GetPointerId();
242         auto tPointerEvent = std::make_shared<PointerEvent>(*pointerEvent);
243         tPointerEvent->SetPointerId(pointerId);
244         bool isDragging = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
245             (item.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId);
246         if (isDragging) {
247             tPointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_CANCEL);
248         } else {
249             tPointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
250         }
251         tPointerEvent->SetActionTime(GetSysClockTime());
252         tPointerEvent->UpdateId();
253         tPointerEvent->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT | InputEvent::EVENT_FLAG_NO_MONITOR);
254         auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
255         CHKPV(inputEventNormalizeHandler);
256         inputEventNormalizeHandler->HandleTouchEvent(tPointerEvent);
257         auto iter = touchItemDownInfos_.find(pointerId);
258         if (iter != touchItemDownInfos_.end()) {
259             iter->second.flag = false;
260         }
261     }
262 #endif // OHOS_BUILD_ENABLE_TOUCH
263 }
264 
265 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
InitMouseDownInfo()266 void InputWindowsManager::InitMouseDownInfo()
267 {
268     mouseDownInfo_.id = -1;
269     mouseDownInfo_.pid = -1;
270     mouseDownInfo_.defaultHotAreas.clear();
271     mouseDownInfo_.pointerHotAreas.clear();
272 }
273 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
274 
GetWindowGroupInfoByDisplayId(int32_t displayId) const275 const std::vector<WindowInfo> &InputWindowsManager::GetWindowGroupInfoByDisplayId(int32_t displayId) const
276 {
277     CALL_DEBUG_ENTER;
278     if (displayId < 0) {
279         return displayGroupInfo_.windowsInfo;
280     }
281     auto iter = windowsPerDisplay_.find(displayId);
282     if (iter == windowsPerDisplay_.end()) {
283         MMI_HILOGD("GetWindowInfo displayId:%{public}d is null from windowGroupInfo_", displayId);
284         return displayGroupInfo_.windowsInfo;
285     }
286     if (iter->second.windowsInfo.empty()) {
287         MMI_HILOGW("GetWindowInfo displayId:%{public}d is empty", displayId);
288         return displayGroupInfo_.windowsInfo;
289     }
290     return iter->second.windowsInfo;
291 }
292 
GetCancelEventFlag(std::shared_ptr<PointerEvent> pointerEvent)293 bool InputWindowsManager::GetCancelEventFlag(std::shared_ptr<PointerEvent> pointerEvent)
294 {
295     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
296         auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
297         if (iter != touchItemDownInfos_.end()) {
298             return iter->second.flag;
299         }
300         return true;
301     } else if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE ||
302         pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
303         if (mouseDownInfo_.pid != -1) {
304             return false;
305         }
306         return true;
307     }
308     return false;
309 }
310 
311 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
AdjustFingerFlag(std::shared_ptr<PointerEvent> pointerEvent)312 bool InputWindowsManager::AdjustFingerFlag(std::shared_ptr<PointerEvent> pointerEvent)
313 {
314     CHKPF(pointerEvent);
315     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
316         return false;
317     }
318     std::map<int32_t, WindowInfoEX> tmpWindowInfo;
319     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
320         tmpWindowInfo = shellTouchItemDownInfos_;
321     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
322         tmpWindowInfo = accessTouchItemDownInfos_;
323     } else {
324         tmpWindowInfo = touchItemDownInfos_;
325     }
326     auto iter = tmpWindowInfo.find(pointerEvent->GetPointerId());
327     return (iter != tmpWindowInfo.end() && !(iter->second.flag));
328 }
329 
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)330 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)
331 {
332     CALL_DEBUG_ENTER;
333     CHKPR(pointerEvent, INVALID_FD);
334     const WindowInfo* windowInfo = nullptr;
335     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
336         if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
337             auto iter = shellTouchItemDownInfos_.find(pointerEvent->GetPointerId());
338             if (iter != shellTouchItemDownInfos_.end() && !(iter->second.flag)) {
339                 MMI_HILOG_DISPATCHD("Drop event");
340                 return INVALID_FD;
341             }
342         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
343             auto iter = accessTouchItemDownInfos_.find(pointerEvent->GetPointerId());
344             if (iter != accessTouchItemDownInfos_.end() && !(iter->second.flag)) {
345                 MMI_HILOG_DISPATCHD("Drop event");
346                 return INVALID_FD;
347             }
348         } else {
349             auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
350             if (iter != touchItemDownInfos_.end() && !(iter->second.flag)) {
351                 MMI_HILOG_DISPATCHD("Drop event");
352                 return INVALID_FD;
353             }
354         }
355     }
356     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
357     for (const auto &item : windowsInfo) {
358         bool checkUIExtentionWindow = false;
359         // Determine whether it is a safety sub window
360         for (auto &uiExtentionWindowInfo : item.uiExtentionWindowInfo) {
361             if (uiExtentionWindowInfo.id == pointerEvent->GetTargetWindowId()) {
362                 MMI_HILOGD("Find windowInfo by window id %{public}d", uiExtentionWindowInfo.id);
363                 windowInfo = &uiExtentionWindowInfo;
364                 checkUIExtentionWindow = true;
365                 break;
366             }
367         }
368         if (checkUIExtentionWindow) {
369             break;
370         }
371         bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
372             !IsValidZorderWindow(item, pointerEvent);
373         if (checkWindow) {
374             MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
375                 "window:%{public}d, flags:%{public}d", item.id, item.flags);
376             continue;
377         }
378         if (item.id == pointerEvent->GetTargetWindowId()) {
379             MMI_HILOG_DISPATCHD("find windowinfo by window id %{public}d", item.id);
380             windowInfo = &item;
381             break;
382         }
383     }
384     CHKPR(udsServer_, INVALID_FD);
385     if (windowInfo == nullptr) {
386         MMI_HILOG_DISPATCHD("window info is null, pointerAction:%{public}d", pointerEvent->GetPointerAction());
387         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
388             windowInfo = &lastWindowInfo_;
389         }
390     }
391     if (windowInfo != nullptr) {
392         if (ROTATE_POLICY == FOLDABLE_DEVICE) {
393             FoldScreenRotation(pointerEvent);
394         }
395         MMI_HILOG_DISPATCHD("get pid:%{public}d from idxPidMap", windowInfo->pid);
396         return udsServer_->GetClientFd(windowInfo->pid);
397     }
398     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL &&
399         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
400         MMI_HILOG_DISPATCHD("window info is null, so pointerEvent is dropped! return -1");
401         return udsServer_->GetClientFd(-1);
402     }
403     int32_t pid = -1;
404     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
405         if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
406             auto iter = shellTouchItemDownInfos_.find(pointerEvent->GetPointerId());
407             if (iter != shellTouchItemDownInfos_.end()) {
408                 MMI_HILOG_DISPATCHI("Cant not find pid");
409                 pid = iter->second.window.pid;
410                 iter->second.flag = false;
411                 MMI_HILOG_DISPATCHD("touchscreen occurs, new pid:%{public}d", pid);
412             }
413         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
414             auto iter = accessTouchItemDownInfos_.find(pointerEvent->GetPointerId());
415             if (iter != accessTouchItemDownInfos_.end()) {
416                 MMI_HILOG_DISPATCHI("Cant not find pid");
417                 pid = iter->second.window.pid;
418                 iter->second.flag = false;
419                 MMI_HILOG_DISPATCHD("touchscreen occurs, new pid:%{public}d", pid);
420             }
421         } else {
422             auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
423             if (iter != touchItemDownInfos_.end()) {
424                 MMI_HILOG_DISPATCHI("Cant not find pid");
425                 pid = iter->second.window.pid;
426                 iter->second.flag = false;
427                 MMI_HILOG_DISPATCHD("touchscreen occurs, new pid:%{public}d", pid);
428             }
429         }
430     }
431 #ifdef OHOS_BUILD_ENABLE_POINTER
432     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
433         if (mouseDownInfo_.pid != -1) {
434             pid = GetWindowPid(mouseDownInfo_.agentWindowId);
435             if (pid < 0) {
436                 pid = mouseDownInfo_.pid;
437             }
438             MMI_HILOGD("mouseevent occurs, update the pid:%{public}d", pid);
439             InitMouseDownInfo();
440         }
441     }
442 #endif // OHOS_BUILD_ENABLE_POINTER
443     MMI_HILOGD("get clientFd by %{public}d", pid);
444     return udsServer_->GetClientFd(pid);
445 }
446 
FoldScreenRotation(std::shared_ptr<PointerEvent> pointerEvent)447 void InputWindowsManager::FoldScreenRotation(std::shared_ptr<PointerEvent> pointerEvent)
448 {
449     CALL_DEBUG_ENTER;
450     CHKPV(pointerEvent);
451     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
452         if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
453             auto iter = shellTouchItemDownInfos_.find(pointerEvent->GetPointerId());
454             if (iter == shellTouchItemDownInfos_.end()) {
455                 MMI_HILOG_DISPATCHD("Unable to find finger information for touch.pointerId:%{public}d",
456                     pointerEvent->GetPointerId());
457                 return;
458             }
459         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
460             auto iter = accessTouchItemDownInfos_.find(pointerEvent->GetPointerId());
461             if (iter == accessTouchItemDownInfos_.end()) {
462                 MMI_HILOG_DISPATCHD("Unable to find finger information for touch.pointerId:%{public}d",
463                     pointerEvent->GetPointerId());
464                 return;
465             }
466         } else {
467             auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
468             if (iter == touchItemDownInfos_.end()) {
469                 MMI_HILOG_DISPATCHD("Unable to find finger information for touch.pointerId:%{public}d",
470                     pointerEvent->GetPointerId());
471                 return;
472             }
473         }
474     }
475     auto displayId = pointerEvent->GetTargetDisplayId();
476     auto physicDisplayInfo = GetPhysicalDisplay(displayId);
477     CHKPV(physicDisplayInfo);
478     if (TOUCH_DRAWING_MGR->IsWindowRotation()) {
479         MMI_HILOG_DISPATCHD("Not in the unfolded state of the folding screen");
480         return;
481     }
482     if (lastDirection_ == static_cast<Direction>(-1)) {
483         lastDirection_ = physicDisplayInfo->direction;
484         return;
485     }
486     if (physicDisplayInfo->direction == lastDirection_) {
487         lastDirection_ = physicDisplayInfo->direction;
488         return;
489     }
490     if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
491         PointerEvent::PointerItem item;
492         if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), item)) {
493             MMI_HILOGE("Get pointer item failed. pointer:%{public}d", pointerEvent->GetPointerId());
494             lastDirection_ = physicDisplayInfo->direction;
495             return;
496         }
497         if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE && !(item.IsPressed())) {
498             lastDirection_ = physicDisplayInfo->direction;
499             return;
500         }
501     }
502     if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
503         pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_MOVE) {
504         lastDirection_ = physicDisplayInfo->direction;
505         return;
506     }
507     int32_t pointerAction = pointerEvent->GetPointerAction();
508     switch (pointerAction) {
509         case PointerEvent::POINTER_ACTION_HOVER_MOVE:
510         case PointerEvent::POINTER_ACTION_HOVER_ENTER:
511         case PointerEvent::POINTER_ACTION_HOVER_EXIT:
512         case PointerEvent::POINTER_ACTION_HOVER_CANCEL: {
513             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
514             break;
515         }
516         default: {
517             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
518             break;
519         }
520     }
521     pointerEvent->SetOriginPointerAction(pointerAction);
522     MMI_HILOG_DISPATCHI("touch event send cancel");
523     if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
524         lastDirection_ = physicDisplayInfo->direction;
525         return;
526     }
527     auto fun = [] (std::map<int32_t, WindowInfoEX> &tmpWindowInfo, int32_t pointerId) {
528         auto iter = tmpWindowInfo.find(pointerId);
529         if (iter != tmpWindowInfo.end()) {
530             iter->second.flag = false;
531         }
532     };
533     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
534         fun(shellTouchItemDownInfos_, pointerEvent->GetPointerId());
535     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
536         fun(accessTouchItemDownInfos_, pointerEvent->GetPointerId());
537     } else {
538         fun(touchItemDownInfos_, pointerEvent->GetPointerId());
539     }
540     lastDirection_ = physicDisplayInfo->direction;
541 }
542 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
543 
544 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)545 std::vector<std::pair<int32_t, TargetInfo>> InputWindowsManager::UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)
546 {
547     CALL_DEBUG_ENTER;
548     if (!isParseConfig_) {
549         ParseConfig();
550         isParseConfig_ = true;
551     }
552     std::vector<std::pair<int32_t, TargetInfo>> secSubWindowTargets;
553     if (keyEvent == nullptr) {
554         MMI_HILOG_DISPATCHE("keyEvent is nullptr");
555         return secSubWindowTargets;
556     }
557     auto secSubWindows = GetPidAndUpdateTarget(keyEvent);
558     for (const auto &item : secSubWindows) {
559         int32_t fd = INVALID_FD;
560         int32_t pid = item.first;
561         if (pid <= 0) {
562             MMI_HILOG_DISPATCHE("Invalid pid:%{public}d", pid);
563             continue;
564         }
565         fd = udsServer_->GetClientFd(pid);
566         if (fd < 0) {
567             MMI_HILOG_DISPATCHE("The windowPid:%{public}d matching fd:%{public}d is invalid", pid, fd);
568             continue;
569         }
570         secSubWindowTargets.emplace_back(std::make_pair(fd, item.second));
571     }
572     return secSubWindowTargets;
573 }
574 
HandleKeyEventWindowId(std::shared_ptr<KeyEvent> keyEvent)575 void InputWindowsManager::HandleKeyEventWindowId(std::shared_ptr<KeyEvent> keyEvent)
576 {
577     CALL_DEBUG_ENTER;
578     CHKPV(keyEvent);
579     int32_t focusWindowId = displayGroupInfo_.focusWindowId;
580     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(keyEvent->GetTargetDisplayId());
581     for (const auto &item : windowsInfo) {
582         if (item.id == focusWindowId) {
583             keyEvent->SetTargetWindowId(item.id);
584             keyEvent->SetAgentWindowId(item.agentWindowId);
585             if (item.privacyMode == SecureFlag::PRIVACY_MODE) {
586                 keyEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
587             }
588             return;
589         }
590     }
591 }
592 #endif // OHOS_BUILD_ENABLE_KEYBOARD
593 
GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const594 int32_t InputWindowsManager::GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const
595 {
596     int32_t displayId = inputEvent->GetTargetDisplayId();
597     if (displayId < 0) {
598         MMI_HILOGD("Target display is -1");
599         if (displayGroupInfo_.displaysInfo.empty()) {
600             return displayId;
601         }
602         displayId = displayGroupInfo_.displaysInfo[0].id;
603         inputEvent->SetTargetDisplayId(displayId);
604     }
605     return displayId;
606 }
607 
608 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent,int32_t windowId)609 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent, int32_t windowId)
610 {
611     CALL_DEBUG_ENTER;
612     CHKPR(udsServer_, INVALID_FD);
613     CHKPR(pointerEvent, INVALID_FD);
614     const WindowInfo* windowInfo = nullptr;
615     std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
616     for (const auto &item : windowInfos) {
617         bool checkUIExtentionWindow = false;
618         // Determine whether it is a safety sub window
619         for (auto &uiExtentionWindowInfo : item.uiExtentionWindowInfo) {
620             if (uiExtentionWindowInfo.id == windowId) {
621                 MMI_HILOGD("Find windowInfo by window id %{public}d", uiExtentionWindowInfo.id);
622                 windowInfo = &uiExtentionWindowInfo;
623                 checkUIExtentionWindow = true;
624                 break;
625             }
626         }
627         if (checkUIExtentionWindow) {
628             break;
629         }
630         bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
631             !IsValidZorderWindow(item, pointerEvent);
632         if (checkWindow) {
633             MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
634                 "window:%{public}d, flags:%{public}d", item.id, item.flags);
635             continue;
636         }
637         if (item.id == windowId) {
638             MMI_HILOGD("Find windowInfo by window id %{public}d", item.id);
639             windowInfo = &item;
640             break;
641         }
642     }
643     if (windowInfo == nullptr) {
644         MMI_HILOGE("WindowInfo is nullptr, pointerAction:%{public}d", pointerEvent->GetPointerAction());
645         return INVALID_FD;
646     }
647     MMI_HILOGD("Get pid:%{public}d from idxPidMap", windowInfo->pid);
648     return udsServer_->GetClientFd(windowInfo->pid);
649 }
650 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
651 
652 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
GetPidAndUpdateTarget(std::shared_ptr<KeyEvent> keyEvent)653 std::vector<std::pair<int32_t, TargetInfo>> InputWindowsManager::GetPidAndUpdateTarget(
654     std::shared_ptr<KeyEvent> keyEvent)
655 {
656     CALL_DEBUG_ENTER;
657     std::vector<std::pair<int32_t, TargetInfo>> secSubWindows;
658     if (keyEvent == nullptr) {
659         MMI_HILOG_DISPATCHE("keyEvent is nullptr");
660         return secSubWindows;
661     }
662     const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
663     WindowInfo* windowInfo = nullptr;
664     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(keyEvent->GetTargetDisplayId());
665     bool isUIExtention = false;
666     auto iter = windowsInfo.begin();
667     for (; iter != windowsInfo.end(); ++iter) {
668         if (iter->id == focusWindowId) {
669             windowInfo = &(*iter);
670             if (!iter->uiExtentionWindowInfo.empty() && !IsOnTheWhitelist(keyEvent)) {
671                 isUIExtention = true;
672             }
673             break;
674         }
675     }
676     if (windowInfo == nullptr) {
677         MMI_HILOG_DISPATCHE("windowInfo is nullptr");
678         return secSubWindows;
679     }
680 #ifdef OHOS_BUILD_ENABLE_ANCO
681     if (IsAncoWindowFocus(*windowInfo)) {
682         MMI_HILOG_DISPATCHD("focusWindowId:%{public}d is anco window", focusWindowId);
683         if (keyEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
684             SimulateKeyExt(keyEvent);
685         }
686         return secSubWindows;
687     }
688 #endif // OHOS_BUILD_ENABLE_ANCO
689     TargetInfo targetInfo = { windowInfo->privacyMode, windowInfo->id, windowInfo->agentWindowId };
690     secSubWindows.emplace_back(std::make_pair(windowInfo->pid, targetInfo));
691     if (isUIExtention) {
692         for (auto &item : iter->uiExtentionWindowInfo) {
693             if (item.privacyUIFlag) {
694                 MMI_HILOG_DISPATCHD("security sub windowId:%{public}d,pid:%{public}d", item.id, item.pid);
695                 targetInfo.privacyMode = item.privacyMode;
696                 targetInfo.id = item.id;
697                 targetInfo.agentWindowId = item.agentWindowId;
698                 secSubWindows.emplace_back(std::make_pair(item.pid, targetInfo));
699             }
700         }
701     }
702     return secSubWindows;
703 }
704 #endif // OHOS_BUILD_ENABLE_KEYBOARD
705 
GetWindowPid(int32_t windowId) const706 int32_t InputWindowsManager::GetWindowPid(int32_t windowId) const
707 {
708     CALL_DEBUG_ENTER;
709     int32_t windowPid = INVALID_PID;
710     for (const auto &item : displayGroupInfo_.windowsInfo) {
711         MMI_HILOGD("get windowId:%{public}d", item.id);
712         if (item.id == windowId) {
713             windowPid = item.pid;
714             break;
715         }
716         for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
717             if (uiExtentionWindow.id == windowId) {
718                 windowPid = uiExtentionWindow.pid;
719                 break;
720             }
721         }
722     }
723     return windowPid;
724 }
725 
GetWindowPid(int32_t windowId,const std::vector<WindowInfo> & windowsInfo) const726 int32_t InputWindowsManager::GetWindowPid(int32_t windowId, const std::vector<WindowInfo> &windowsInfo) const
727 {
728     int32_t windowPid = INVALID_PID;
729     for (const auto &item : windowsInfo) {
730         if (item.id == windowId) {
731             windowPid = item.pid;
732             break;
733         }
734     }
735     return windowPid;
736 }
737 
CheckFocusWindowChange(const DisplayGroupInfo & displayGroupInfo)738 void InputWindowsManager::CheckFocusWindowChange(const DisplayGroupInfo &displayGroupInfo)
739 {
740     const int32_t oldFocusWindowId = displayGroupInfo_.focusWindowId;
741     const int32_t newFocusWindowId = displayGroupInfo.focusWindowId;
742     if (oldFocusWindowId == newFocusWindowId) {
743         return;
744     }
745     const int32_t oldFocusWindowPid = GetWindowPid(oldFocusWindowId);
746     const int32_t newFocusWindowPid = GetWindowPid(newFocusWindowId, displayGroupInfo.windowsInfo);
747     DfxHisysevent::OnFocusWindowChanged(oldFocusWindowId, newFocusWindowId, oldFocusWindowPid, newFocusWindowPid);
748 }
749 
CheckZorderWindowChange(const std::vector<WindowInfo> & oldWindowsInfo,const std::vector<WindowInfo> & newWindowsInfo)750 void InputWindowsManager::CheckZorderWindowChange(const std::vector<WindowInfo> &oldWindowsInfo,
751     const std::vector<WindowInfo> &newWindowsInfo)
752 {
753     int32_t oldZorderFirstWindowId = -1;
754     int32_t newZorderFirstWindowId = -1;
755     if (!oldWindowsInfo.empty()) {
756         oldZorderFirstWindowId = oldWindowsInfo[0].id;
757     }
758     if (!newWindowsInfo.empty()) {
759         newZorderFirstWindowId = newWindowsInfo[0].id;
760     }
761     if (oldZorderFirstWindowId == newZorderFirstWindowId) {
762         return;
763     }
764     const int32_t oldZorderFirstWindowPid = GetWindowPid(oldZorderFirstWindowId);
765     const int32_t newZorderFirstWindowPid = GetWindowPid(newZorderFirstWindowId, newWindowsInfo);
766     DfxHisysevent::OnZorderWindowChanged(oldZorderFirstWindowId, newZorderFirstWindowId,
767         oldZorderFirstWindowPid, newZorderFirstWindowPid);
768 }
769 
UpdateDisplayIdAndName()770 void InputWindowsManager::UpdateDisplayIdAndName()
771 {
772     using IdNames = std::set<std::pair<int32_t, std::string>>;
773     IdNames newInfo;
774     for (const auto &item : displayGroupInfo_.displaysInfo) {
775         newInfo.insert(std::make_pair(item.id, item.uniq));
776     }
777     auto oldInfo = bindInfo_.GetDisplayIdNames();
778     if (newInfo == oldInfo) {
779         return;
780     }
781     for (auto it = oldInfo.begin(); it != oldInfo.end();) {
782         if (newInfo.find(*it) == newInfo.end()) {
783             bindInfo_.RemoveDisplay(it->first);
784             oldInfo.erase(it++);
785         } else {
786             ++it;
787         }
788     }
789     for (const auto &item : newInfo) {
790         if (item.first >= HICAR_MIN_DISPLAY_ID) {
791             MMI_HILOGI("Displayinfo id:%{public}d name:%{public}s", item.first, item.second.c_str());
792             continue;
793         }
794         if (!bindInfo_.IsDisplayAdd(item.first, item.second)) {
795             bindInfo_.AddDisplay(item.first, item.second);
796         }
797     }
798 }
799 
GetDisplayBindInfo(DisplayBindInfos & infos)800 int32_t InputWindowsManager::GetDisplayBindInfo(DisplayBindInfos &infos)
801 {
802     CALL_DEBUG_ENTER;
803     return bindInfo_.GetDisplayBindInfo(infos);
804 }
805 
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)806 int32_t InputWindowsManager::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
807 {
808     CALL_DEBUG_ENTER;
809     return bindInfo_.SetDisplayBind(deviceId, displayId, msg);
810 }
811 
UpdateCaptureMode(const DisplayGroupInfo & displayGroupInfo)812 void InputWindowsManager::UpdateCaptureMode(const DisplayGroupInfo &displayGroupInfo)
813 {
814     if (captureModeInfo_.isCaptureMode && (!displayGroupInfo_.windowsInfo.empty()) &&
815         ((displayGroupInfo_.focusWindowId != displayGroupInfo.focusWindowId) ||
816         (displayGroupInfo_.windowsInfo[0].id != displayGroupInfo.windowsInfo[0].id))) {
817         captureModeInfo_.isCaptureMode = false;
818     }
819 }
820 
UpdateWindowInfo(const WindowGroupInfo & windowGroupInfo)821 void InputWindowsManager::UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo)
822 {
823     CALL_DEBUG_ENTER;
824     PrintWindowGroupInfo(windowGroupInfo);
825 #ifdef OHOS_BUILD_ENABLE_ANCO
826     if (windowGroupInfo.windowsInfo.size() == SHELL_WINDOW_COUNT && IsAncoWindow(windowGroupInfo.windowsInfo[0])) {
827         return UpdateShellWindow(windowGroupInfo.windowsInfo[0]);
828     }
829 #endif // OHOS_BUILD_ENABLE_ANCO
830     DisplayGroupInfo displayGroupInfo = displayGroupInfoTmp_;
831     displayGroupInfo.focusWindowId = windowGroupInfo.focusWindowId;
832     for (const auto &item : windowGroupInfo.windowsInfo) {
833         UpdateDisplayInfoByIncrementalInfo(item, displayGroupInfo);
834     }
835 
836 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
837     pointerDrawFlag_ = NeedUpdatePointDrawFlag(windowGroupInfo.windowsInfo);
838 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
839 
840 #ifdef OHOS_BUILD_ENABLE_ANCO
841     UpdateWindowInfoExt(windowGroupInfo, displayGroupInfo);
842 #endif // OHOS_BUILD_ENABLE_ANCO
843     UpdateDisplayInfoExtIfNeed(displayGroupInfo, false);
844 }
845 
UpdateDisplayInfoExtIfNeed(DisplayGroupInfo & displayGroupInfo,bool needUpdateDisplayExt)846 void InputWindowsManager::UpdateDisplayInfoExtIfNeed(DisplayGroupInfo &displayGroupInfo, bool needUpdateDisplayExt)
847 {
848     UpdateDisplayInfo(displayGroupInfo);
849 #ifdef OHOS_BUILD_ENABLE_ANCO
850     if (needUpdateDisplayExt) {
851         UpdateDisplayInfoExt(displayGroupInfo);
852     }
853 #endif // OHOS_BUILD_ENABLE_ANCO
854     if (displayGroupInfo.displaysInfo.empty()) {
855         MMI_HILOGE("displaysInfo is empty");
856         return;
857     }
858     auto physicDisplayInfo = GetPhysicalDisplay(displayGroupInfo.displaysInfo[0].id);
859     CHKPV(physicDisplayInfo);
860     TOUCH_DRAWING_MGR->UpdateDisplayInfo(*physicDisplayInfo);
861     TOUCH_DRAWING_MGR->RotationScreen();
862 }
863 
UpdateDisplayInfoByIncrementalInfo(const WindowInfo & window,DisplayGroupInfo & displayGroupInfo)864 void InputWindowsManager::UpdateDisplayInfoByIncrementalInfo(const WindowInfo &window,
865     DisplayGroupInfo &displayGroupInfo)
866 {
867     CALL_DEBUG_ENTER;
868     switch (window.action) {
869         case WINDOW_UPDATE_ACTION::ADD:
870         case WINDOW_UPDATE_ACTION::ADD_END: {
871             auto id = window.id;
872             auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
873                 [id](const auto& item) { return item.id == id; });
874             if (pos == std::end(displayGroupInfo.windowsInfo)) {
875                 displayGroupInfo.windowsInfo.emplace_back(window);
876             } else {
877                 *pos = window;
878             }
879             break;
880         }
881         case WINDOW_UPDATE_ACTION::DEL: {
882             auto oldWindow = displayGroupInfo.windowsInfo.begin();
883             while (oldWindow != displayGroupInfo.windowsInfo.end()) {
884                 if (oldWindow->id == window.id) {
885                     oldWindow = displayGroupInfo.windowsInfo.erase(oldWindow);
886                 } else {
887                     oldWindow++;
888                 }
889             }
890             break;
891         }
892         case WINDOW_UPDATE_ACTION::CHANGE: {
893             auto id = window.id;
894             auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
895                 [id](const auto& item) { return item.id == id; });
896             if (pos != std::end(displayGroupInfo.windowsInfo)) {
897                 *pos = window;
898             }
899             break;
900         }
901         default: {
902             MMI_HILOGI("WINDOW_UPDATE_ACTION is action:%{public}d", window.action);
903             break;
904         }
905     }
906 }
907 
UpdateWindowsInfoPerDisplay(const DisplayGroupInfo & displayGroupInfo)908 void InputWindowsManager::UpdateWindowsInfoPerDisplay(const DisplayGroupInfo &displayGroupInfo)
909 {
910     CALL_DEBUG_ENTER;
911     std::map<int32_t, WindowGroupInfo> windowsPerDisplay;
912     for (const auto &window : displayGroupInfo.windowsInfo) {
913         auto it = windowsPerDisplay.find(window.displayId);
914         if (it == windowsPerDisplay.end()) {
915             windowsPerDisplay[window.displayId] = WindowGroupInfo {-1, window.displayId, {window}};
916         } else {
917             it->second.windowsInfo.emplace_back(window);
918         }
919         if (displayGroupInfo.focusWindowId == window.id) {
920             windowsPerDisplay[window.displayId].focusWindowId = window.id;
921         }
922     }
923     for (auto iter : windowsPerDisplay) {
924         std::sort(iter.second.windowsInfo.begin(), iter.second.windowsInfo.end(),
925             [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
926             return lwindow.zOrder > rwindow.zOrder;
927         });
928     }
929     for (const auto &item : windowsPerDisplay) {
930         int32_t displayId = item.first;
931         if (windowsPerDisplay_.find(displayId) != windowsPerDisplay_.end()) {
932             CheckZorderWindowChange(windowsPerDisplay_[displayId].windowsInfo, item.second.windowsInfo);
933         }
934     }
935 
936     windowsPerDisplay_ = windowsPerDisplay;
937 }
938 
939 
HandleWindowPositionChange()940 void InputWindowsManager::HandleWindowPositionChange()
941 {
942     CALL_INFO_TRACE;
943     CHKPV(lastPointerEventforWindowChange_);
944     if (lastPointerEventforWindowChange_->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
945         for (auto it = shellTouchItemDownInfos_.begin(); it != shellTouchItemDownInfos_.end(); ++it) {
946             int32_t pointerId = it->first;
947             int32_t windowId = it->second.window.id;
948             auto iter = std::find_if(displayGroupInfo_.windowsInfo.begin(), displayGroupInfo_.windowsInfo.end(),
949                 [windowId](const auto& windowInfo) {
950                 return windowId == windowInfo.id;
951             });
952             if (iter == displayGroupInfo_.windowsInfo.end() || (!iter->rectChangeBySystem)) {
953                 continue;
954             }
955             iter->rectChangeBySystem = false;
956             it->second.flag = true;
957             SendCancelEventWhenWindowChange(pointerId);
958         }
959     } else {
960         for (auto it = touchItemDownInfos_.begin(); it != touchItemDownInfos_.end(); ++it) {
961             int32_t pointerId = it->first;
962             int32_t windowId = it->second.window.id;
963             auto iter = std::find_if(displayGroupInfo_.windowsInfo.begin(), displayGroupInfo_.windowsInfo.end(),
964                 [windowId](const auto& windowInfo) {
965                 return windowId == windowInfo.id;
966             });
967             if (iter == displayGroupInfo_.windowsInfo.end() || (!iter->rectChangeBySystem)) {
968                 continue;
969             }
970             iter->rectChangeBySystem = false;
971             it->second.flag = true;
972             SendCancelEventWhenWindowChange(pointerId);
973         }
974     }
975 }
976 
SendCancelEventWhenWindowChange(int32_t pointerId)977 void InputWindowsManager::SendCancelEventWhenWindowChange(int32_t pointerId)
978 {
979     MMI_HILOGI("Dispatch cancel event pointerId:%{public}d", pointerId);
980     CHKPV(lastPointerEventforWindowChange_);
981     PointerEvent::PointerItem pointerItem;
982     if (!lastPointerEventforWindowChange_->GetPointerItem(pointerId, pointerItem)) {
983         MMI_HILOGE("Can not find pointer item pointerid:%{public}d", pointerId);
984         return;
985     }
986     auto tmpEvent = std::make_shared<PointerEvent>(*lastPointerEventforWindowChange_);
987     tmpEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
988     tmpEvent->SetPointerId(pointerId);
989     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
990     CHKPV(inputEventNormalizeHandler);
991     inputEventNormalizeHandler->HandleTouchEvent(tmpEvent);
992 }
993 
JudgeCaramaInFore()994 bool InputWindowsManager::JudgeCaramaInFore()
995 {
996     int32_t focWid = displayGroupInfo_.focusWindowId;
997     int32_t focPid = GetPidByWindowId(focWid);
998     if (udsServer_ == nullptr) {
999         MMI_HILOGW("The udsServer is nullptr");
1000         return false;
1001     }
1002     SessionPtr sess = udsServer_->GetSessionByPid(focPid);
1003     if (sess == nullptr) {
1004         MMI_HILOGW("The sess is nullptr");
1005         return false;
1006     }
1007     std::string programName = sess->GetProgramName();
1008     return programName.find(".camera") != std::string::npos;
1009 }
1010 
UpdateDisplayInfo(DisplayGroupInfo & displayGroupInfo)1011 void InputWindowsManager::UpdateDisplayInfo(DisplayGroupInfo &displayGroupInfo)
1012 {
1013     auto action = WINDOW_UPDATE_ACTION::ADD_END;
1014     if (!displayGroupInfo.windowsInfo.empty()) {
1015         action = displayGroupInfo.windowsInfo.back().action;
1016     }
1017     MMI_HILOGD("Current action is:%{public}d", action);
1018 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1019     pointerDrawFlag_ = NeedUpdatePointDrawFlag(displayGroupInfo.windowsInfo);
1020 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1021     std::sort(displayGroupInfo.windowsInfo.begin(), displayGroupInfo.windowsInfo.end(),
1022         [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
1023         return lwindow.zOrder > rwindow.zOrder;
1024     });
1025     CheckFocusWindowChange(displayGroupInfo);
1026     UpdateCaptureMode(displayGroupInfo);
1027     displayGroupInfoTmp_ = displayGroupInfo;
1028     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled() ||
1029         action == WINDOW_UPDATE_ACTION::ADD_END) {
1030         if ((currentUserId_ < 0) || (currentUserId_ == displayGroupInfoTmp_.currentUserId)) {
1031             PrintChangedWindowBySync(displayGroupInfoTmp_);
1032             CleanInvalidPiexMap();
1033             displayGroupInfo_ = displayGroupInfoTmp_;
1034             UpdateWindowsInfoPerDisplay(displayGroupInfo);
1035             HandleWindowPositionChange();
1036         }
1037     }
1038     PrintDisplayInfo();
1039     if (!displayGroupInfo_.displaysInfo.empty()) {
1040         UpdateDisplayIdAndName();
1041     }
1042     UpdateDisplayMode();
1043 
1044 #ifdef OHOS_BUILD_ENABLE_POINTER
1045 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1046     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled() && INPUT_DEV_MGR->HasPointerDevice()) {
1047 #else
1048     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1049 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1050         UpdatePointerChangeAreas(displayGroupInfo);
1051     }
1052 
1053     InitPointerStyle();
1054 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1055     if (!displayGroupInfo.displaysInfo.empty() && pointerDrawFlag_) {
1056         AdjustDisplayRotation();
1057         PointerDrawingManagerOnDisplayInfo(displayGroupInfo);
1058     }
1059     if (INPUT_DEV_MGR->HasPointerDevice() && pointerDrawFlag_) {
1060         NotifyPointerToWindow();
1061     }
1062 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1063 #endif // OHOS_BUILD_ENABLE_POINTER
1064 }
1065 
1066 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1067 void InputWindowsManager::AdjustDisplayRotation()
1068 {
1069     if (!TOUCH_DRAWING_MGR->IsWindowRotation()) {
1070         PhysicalCoordinate coord {
1071             .x = cursorPos_.cursorPos.x,
1072             .y = cursorPos_.cursorPos.y,
1073         };
1074         auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos_.displayId);
1075         CHKPV(displayInfo);
1076         if (cursorPos_.direction != displayInfo->direction) {
1077             RotateScreen(*displayInfo, coord);
1078             cursorPos_.direction = displayInfo->direction;
1079             UpdateAndAdjustMouseLocation(cursorPos_.displayId, coord.x, coord.y);
1080             IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*displayInfo);
1081             IPointerDrawingManager::GetInstance()->SetPointerLocation(
1082                 static_cast<int32_t>(coord.x), static_cast<int32_t>(coord.y));
1083         }
1084     }
1085 }
1086 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1087 
1088 DisplayMode InputWindowsManager::GetDisplayMode() const
1089 {
1090     return displayMode_;
1091 }
1092 
1093 void InputWindowsManager::UpdateDisplayMode()
1094 {
1095     CALL_DEBUG_ENTER;
1096     if (displayGroupInfo_.displaysInfo.empty()) {
1097         MMI_HILOGE("displaysInfo is empty");
1098         return;
1099     }
1100     DisplayMode mode = displayGroupInfo_.displaysInfo[0].displayMode;
1101     if (mode == displayMode_) {
1102         MMI_HILOGD("displaymode not change, mode:%{public}d, diaplayMode_:%{public}d", mode, displayMode_);
1103         return;
1104     }
1105     displayMode_ = mode;
1106 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1107     if (FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_ == nullptr) {
1108         MMI_HILOGD("send fingersense display mode is nullptr");
1109         return;
1110     }
1111     MMI_HILOGI("update fingersense display mode, displayMode:%{public}d", displayMode_);
1112     FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_(static_cast<int32_t>(displayMode_));
1113 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1114 }
1115 
1116 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1117 void InputWindowsManager::PointerDrawingManagerOnDisplayInfo(const DisplayGroupInfo &displayGroupInfo)
1118 {
1119     IPointerDrawingManager::GetInstance()->OnDisplayInfo(displayGroupInfo);
1120     if (lastPointerEvent_ == nullptr) {
1121         MMI_HILOGD("lastPointerEvent_ is nullptr");
1122         return;
1123     }
1124     if (INPUT_DEV_MGR->HasPointerDevice() || IsMouseSimulate()) {
1125         MouseLocation mouseLocation = GetMouseInfo();
1126         int32_t displayId = MouseEventHdr->GetDisplayId();
1127         displayId = displayId < 0 ? displayGroupInfo_.displaysInfo[0].id : displayId;
1128         auto displayInfo = GetPhysicalDisplay(displayId);
1129         CHKPV(displayInfo);
1130         DispatchPointerCancel(displayId);
1131         int32_t logicX = mouseLocation.physicalX + displayInfo->x;
1132         int32_t logicY = mouseLocation.physicalY + displayInfo->y;
1133         std::optional<WindowInfo> windowInfo;
1134         if (lastPointerEvent_->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN &&
1135             lastPointerEvent_->GetPressedButtons().empty()) {
1136             windowInfo = GetWindowInfo(logicX, logicY);
1137         } else {
1138             windowInfo = SelectWindowInfo(logicX, logicY, lastPointerEvent_);
1139         }
1140         CHKFRV(windowInfo, "The windowInfo is nullptr");
1141         int32_t windowPid = GetWindowPid(windowInfo->id);
1142         WinInfo info = { .windowPid = windowPid, .windowId = windowInfo->id };
1143         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1144         PointerStyle pointerStyle;
1145         GetPointerStyle(info.windowPid, info.windowId, pointerStyle);
1146         MMI_HILOGD("get pointer style, pid:%{public}d, windowid:%{public}d, style:%{public}d",
1147             info.windowPid, info.windowId, pointerStyle.id);
1148         if (!dragFlag_) {
1149             isDragBorder_ = SelectPointerChangeArea(*windowInfo, pointerStyle, logicX, logicY);
1150             dragPointerStyle_ = pointerStyle;
1151             MMI_HILOGD("not in drag SelectPointerStyle, pointerStyle is:%{public}d", dragPointerStyle_.id);
1152         }
1153         JudgMouseIsDownOrUp(dragFlag_);
1154         if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1155             dragFlag_ = true;
1156             MMI_HILOGD("Is in drag scene");
1157         }
1158         if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
1159             dragFlag_ = false;
1160             isDragBorder_ = false;
1161         }
1162         IPointerDrawingManager::GetInstance()->DrawPointerStyle(dragPointerStyle_);
1163     }
1164 }
1165 
1166 void InputWindowsManager::DispatchPointerCancel(int32_t displayId)
1167 {
1168     if (mouseDownInfo_.id < 0 || (extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE))) {
1169         return;
1170     }
1171     if (lastPointerEvent_ == nullptr) {
1172         MMI_HILOGD("lastPointerEvent is null");
1173         return;
1174     }
1175     std::optional<WindowInfo> windowInfo;
1176     std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(displayId);
1177     for (const auto &item : windowInfos) {
1178         if (item.id == mouseDownInfo_.id) {
1179             windowInfo = std::make_optional(item);
1180             break;
1181         }
1182     }
1183     if (windowInfo != std::nullopt) {
1184         return;
1185     }
1186     auto pointerEvent = PointerEvent::Create();
1187     CHKPV(pointerEvent);
1188     pointerEvent->UpdateId();
1189     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), PointerEvent::POINTER_ACTION_CANCEL);
1190     SetPointerEvent(PointerEvent::POINTER_ACTION_CANCEL, pointerEvent);
1191     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
1192     auto filter = InputHandler->GetFilterHandler();
1193     CHKPV(filter);
1194     filter->HandlePointerEvent(pointerEvent);
1195 }
1196 
1197 void InputWindowsManager::SetPointerEvent(int32_t pointerAction, std::shared_ptr<PointerEvent> pointerEvent)
1198 {
1199     CHKPV(pointerEvent);
1200     PointerEvent::PointerItem lastPointerItem;
1201     int32_t lastPointerId = lastPointerEvent_->GetPointerId();
1202     if (!lastPointerEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
1203         MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
1204         return;
1205     }
1206     bool checkFlag = lastPointerItem.IsPressed() && lastWindowInfo_.id != mouseDownInfo_.id;
1207     int32_t id = lastWindowInfo_.id;
1208     if (checkFlag) {
1209         id = mouseDownInfo_.id;
1210     }
1211     PointerEvent::PointerItem currentPointerItem;
1212     currentPointerItem.SetWindowX(lastLogicX_- lastWindowInfo_.area.x);
1213     currentPointerItem.SetWindowY(lastLogicY_- lastWindowInfo_.area.y);
1214     currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
1215     currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
1216     currentPointerItem.SetPointerId(0);
1217     pointerEvent->SetTargetDisplayId(lastPointerEvent_->GetTargetDisplayId());
1218     SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1219     pointerEvent->SetTargetWindowId(id);
1220     pointerEvent->SetAgentWindowId(id);
1221     pointerEvent->SetPointerId(0);
1222     pointerEvent->SetButtonPressed(lastPointerEvent_->GetButtonId());
1223     pointerEvent->SetButtonId(lastPointerEvent_->GetButtonId());
1224     pointerEvent->AddPointerItem(currentPointerItem);
1225     pointerEvent->SetPointerAction(pointerAction);
1226     pointerEvent->SetOriginPointerAction(lastPointerEvent_->GetPointerAction());
1227     pointerEvent->SetSourceType(lastPointerEvent_->GetSourceType());
1228     int64_t time = GetSysClockTime();
1229     pointerEvent->SetActionTime(time);
1230     pointerEvent->SetActionStartTime(time);
1231     pointerEvent->SetDeviceId(lastPointerEvent_->GetDeviceId());
1232 }
1233 
1234 bool InputWindowsManager::NeedUpdatePointDrawFlag(const std::vector<WindowInfo> &windows)
1235 {
1236     CALL_DEBUG_ENTER;
1237     return !windows.empty() && windows.back().action == WINDOW_UPDATE_ACTION::ADD_END;
1238 }
1239 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1240 
1241 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1242 void InputWindowsManager::GetPointerStyleByArea(WindowArea area, int32_t pid, int32_t winId, PointerStyle& pointerStyle)
1243 {
1244     CALL_DEBUG_ENTER;
1245     switch (area) {
1246         case WindowArea::ENTER:
1247         case WindowArea::EXIT:
1248             MMI_HILOG_CURSORD("SetPointerStyle for Enter or exit! No need to deal with it now");
1249             break;
1250         case WindowArea::FOCUS_ON_TOP_LEFT:
1251         case WindowArea::FOCUS_ON_BOTTOM_RIGHT:
1252             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
1253             break;
1254         case WindowArea::FOCUS_ON_TOP_RIGHT:
1255         case WindowArea::FOCUS_ON_BOTTOM_LEFT:
1256             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
1257             break;
1258         case WindowArea::FOCUS_ON_TOP:
1259         case WindowArea::FOCUS_ON_BOTTOM:
1260             pointerStyle.id = MOUSE_ICON::NORTH_SOUTH;
1261             break;
1262         case WindowArea::FOCUS_ON_LEFT:
1263         case WindowArea::FOCUS_ON_RIGHT:
1264             pointerStyle.id = MOUSE_ICON::WEST_EAST;
1265             break;
1266         case WindowArea::TOP_LEFT_LIMIT:
1267             pointerStyle.id = MOUSE_ICON::SOUTH_EAST;
1268             break;
1269         case WindowArea::TOP_RIGHT_LIMIT:
1270             pointerStyle.id = MOUSE_ICON::SOUTH_WEST;
1271             break;
1272         case WindowArea::TOP_LIMIT:
1273             pointerStyle.id = MOUSE_ICON::SOUTH;
1274             break;
1275         case WindowArea::LEFT_LIMIT:
1276             pointerStyle.id = MOUSE_ICON::EAST;
1277             break;
1278         case WindowArea::RIGHT_LIMIT:
1279             pointerStyle.id = MOUSE_ICON::WEST;
1280             break;
1281         case WindowArea::BOTTOM_LEFT_LIMIT:
1282             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
1283             break;
1284         case WindowArea::BOTTOM_LIMIT:
1285             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
1286             break;
1287         case WindowArea::BOTTOM_RIGHT_LIMIT:
1288             pointerStyle.id = MOUSE_ICON::NORTH_WEST;
1289             break;
1290         case WindowArea::FOCUS_ON_INNER:
1291             GetPointerStyle(pid, winId, pointerStyle);
1292             break;
1293     }
1294 }
1295 
1296 void InputWindowsManager::SetWindowPointerStyle(WindowArea area, int32_t pid, int32_t windowId)
1297 {
1298     CALL_DEBUG_ENTER;
1299     PointerStyle pointerStyle;
1300     GetPointerStyleByArea(area, pid, windowId, pointerStyle);
1301     UpdateWindowPointerVisible(pid);
1302     if (lastPointerStyle_.id == pointerStyle.id) {
1303         MMI_HILOG_CURSORE("Tha lastPointerStyle is totally equal with this, no need to change it");
1304         return;
1305     }
1306     lastPointerStyle_.id = pointerStyle.id;
1307     std::map<MOUSE_ICON, IconStyle> mouseIcons = IPointerDrawingManager::GetInstance()->GetMouseIconPath();
1308     if (windowId != GLOBAL_WINDOW_ID && (pointerStyle.id == MOUSE_ICON::DEFAULT &&
1309         mouseIcons[MOUSE_ICON(pointerStyle.id)].iconPath != DEFAULT_ICON_PATH)) {
1310         PointerStyle style;
1311         GetPointerStyle(pid, GLOBAL_WINDOW_ID, style);
1312         lastPointerStyle_ = style;
1313     }
1314     MMI_HILOG_CURSORI("Window id:%{public}d set pointer style:%{public}d success", windowId, lastPointerStyle_.id);
1315     IPointerDrawingManager::GetInstance()->DrawPointerStyle(lastPointerStyle_);
1316 }
1317 
1318 void InputWindowsManager::UpdateWindowPointerVisible(int32_t pid)
1319 {
1320     bool visible = IPointerDrawingManager::GetInstance()->GetPointerVisible(pid);
1321     IPointerDrawingManager::GetInstance()->SetPointerVisible(pid, visible, 0, false);
1322 }
1323 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1324 
1325 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1326 void InputWindowsManager::SendPointerEvent(int32_t pointerAction)
1327 {
1328     CALL_INFO_TRACE;
1329     CHKPV(udsServer_);
1330     auto pointerEvent = PointerEvent::Create();
1331     CHKPV(pointerEvent);
1332     pointerEvent->UpdateId();
1333     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerAction);
1334     MouseLocation mouseLocation = GetMouseInfo();
1335     lastLogicX_ = mouseLocation.physicalX;
1336     lastLogicY_ = mouseLocation.physicalY;
1337     if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW ||
1338         Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1339         auto touchWindow = GetWindowInfo(lastLogicX_, lastLogicY_);
1340         CHKFRV(touchWindow, "TouchWindow is nullptr");
1341         lastWindowInfo_ = *touchWindow;
1342     }
1343     PointerEvent::PointerItem pointerItem;
1344     pointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
1345     pointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
1346     pointerItem.SetDisplayX(lastLogicX_);
1347     pointerItem.SetDisplayY(lastLogicY_);
1348     pointerItem.SetPointerId(0);
1349 
1350     pointerEvent->SetTargetDisplayId(-1);
1351     auto displayId = pointerEvent->GetTargetDisplayId();
1352     if (!UpdateDisplayId(displayId)) {
1353         MMI_HILOGE("This display:%{public}d is not existent", displayId);
1354         return;
1355     }
1356     pointerEvent->SetTargetDisplayId(displayId);
1357     SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1358     pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
1359     pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
1360     pointerEvent->SetPointerId(0);
1361     pointerEvent->AddPointerItem(pointerItem);
1362     pointerEvent->SetPointerAction(pointerAction);
1363     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
1364     int64_t time = GetSysClockTime();
1365     pointerEvent->SetActionTime(time);
1366     pointerEvent->SetActionStartTime(time);
1367     pointerEvent->UpdateId();
1368     LogTracer lt1(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
1369     if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1370         pointerEvent->SetBuffer(extraData_.buffer);
1371         pointerEvent->SetPullId(extraData_.pullId);
1372         UpdatePointerAction(pointerEvent);
1373     } else {
1374         pointerEvent->ClearBuffer();
1375     }
1376     auto fd = udsServer_->GetClientFd(lastWindowInfo_.pid);
1377     auto sess = udsServer_->GetSession(fd);
1378     CHKPRV(sess, "The last window has disappeared");
1379     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
1380     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
1381     if (!sess->SendMsg(pkt)) {
1382         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1383         return;
1384     }
1385 }
1386 
1387 void InputWindowsManager::DispatchPointer(int32_t pointerAction, int32_t windowId)
1388 {
1389     CALL_INFO_TRACE;
1390     CHKPV(udsServer_);
1391 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1392     if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1393         MMI_HILOGI("the mouse is hide");
1394         return;
1395     }
1396 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1397     if (lastPointerEvent_ == nullptr) {
1398         SendPointerEvent(pointerAction);
1399         return;
1400     }
1401     auto pointerEvent = PointerEvent::Create();
1402     CHKPV(pointerEvent);
1403     pointerEvent->UpdateId();
1404     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerAction);
1405     PointerEvent::PointerItem lastPointerItem;
1406     int32_t lastPointerId = lastPointerEvent_->GetPointerId();
1407     if (!lastPointerEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
1408         MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
1409         return;
1410     }
1411     if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW && windowId <= 0) {
1412         std::optional<WindowInfo> windowInfo;
1413         int32_t eventAction = lastPointerEvent_->GetPointerAction();
1414         bool checkFlag = (eventAction == PointerEvent::POINTER_ACTION_MOVE &&
1415             lastPointerEvent_->GetPressedButtons().empty()) ||
1416             (eventAction >= PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
1417             eventAction <= PointerEvent::POINTER_ACTION_AXIS_END);
1418         if (checkFlag) {
1419             windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
1420         } else {
1421             windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEvent_);
1422         }
1423         if (!windowInfo) {
1424             MMI_HILOGE("windowInfo is nullptr");
1425             return;
1426         }
1427         if (windowInfo->id != lastWindowInfo_.id) {
1428             lastWindowInfo_ = *windowInfo;
1429         }
1430     }
1431     PointerEvent::PointerItem currentPointerItem;
1432     currentPointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
1433     currentPointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
1434     currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
1435     currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
1436     currentPointerItem.SetPointerId(0);
1437 
1438     pointerEvent->SetTargetDisplayId(lastPointerEvent_->GetTargetDisplayId());
1439     SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1440     pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
1441     pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
1442     pointerEvent->SetPointerId(0);
1443     pointerEvent->AddPointerItem(currentPointerItem);
1444     pointerEvent->SetPointerAction(pointerAction);
1445     pointerEvent->SetSourceType(lastPointerEvent_->GetSourceType());
1446     int64_t time = GetSysClockTime();
1447     pointerEvent->SetActionTime(time);
1448     pointerEvent->SetActionStartTime(time);
1449     pointerEvent->SetDeviceId(lastPointerEvent_->GetDeviceId());
1450     if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1451         pointerEvent->SetBuffer(extraData_.buffer);
1452         pointerEvent->SetPullId(extraData_.pullId);
1453         UpdatePointerAction(pointerEvent);
1454     } else {
1455         pointerEvent->ClearBuffer();
1456     }
1457     if (pointerAction == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
1458         pointerEvent->SetAgentWindowId(lastWindowInfo_.id);
1459     }
1460     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
1461 #ifdef OHOS_BUILD_ENABLE_POINTER
1462     auto filter = InputHandler->GetFilterHandler();
1463     CHKPV(filter);
1464     filter->HandlePointerEvent(pointerEvent);
1465 #endif // OHOS_BUILD_ENABLE_POINTER
1466 }
1467 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1468 
1469 #ifdef OHOS_BUILD_ENABLE_POINTER
1470 void InputWindowsManager::NotifyPointerToWindow()
1471 {
1472     CALL_DEBUG_ENTER;
1473     std::optional<WindowInfo> windowInfo;
1474     if (lastPointerEvent_ == nullptr) {
1475         MMI_HILOGD("lastPointerEvent_ is nullptr");
1476         return;
1477     }
1478     if ((lastPointerEvent_->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) &&
1479         (lastPointerEvent_->GetButtonId() >= 0)) {
1480         MMI_HILOGD("No need to respond to new interface layouts");
1481         return;
1482     }
1483     windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
1484     if (!windowInfo) {
1485         MMI_HILOGE("The windowInfo is nullptr");
1486         return;
1487     }
1488     if (windowInfo->id == lastWindowInfo_.id) {
1489         MMI_HILOGI("The mouse pointer does not leave the window:%{public}d", lastWindowInfo_.id);
1490         lastWindowInfo_ = *windowInfo;
1491         return;
1492     }
1493     bool isFindLastWindow = false;
1494     for (const auto &item : displayGroupInfo_.windowsInfo) {
1495         if (item.id == lastWindowInfo_.id) {
1496             DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
1497             isFindLastWindow = true;
1498             break;
1499         }
1500     }
1501     if (!isFindLastWindow) {
1502         if (udsServer_ != nullptr && udsServer_->GetClientFd(lastWindowInfo_.pid) != INVALID_FD) {
1503             DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
1504         }
1505     }
1506     lastWindowInfo_ = *windowInfo;
1507     DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
1508 }
1509 #endif // OHOS_BUILD_ENABLE_POINTER
1510 
1511 void InputWindowsManager::PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo)
1512 {
1513     std::string window;
1514     window += StringPrintf("windowId:[");
1515     for (const auto &item : windowsInfo) {
1516         MMI_HILOGD("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
1517             "area.x:%d,area.y:%d,area.width:%{public}d,area.height:%{public}d,"
1518             "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
1519             "agentWindowId:%{public}d,flags:%{public}d,action:%{public}d,displayId:%{public}d,"
1520             "zOrder:%{public}f, privacyMode:%{public}d",
1521             item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
1522             item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
1523             item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder, item.privacyMode);
1524         for (const auto &win : item.defaultHotAreas) {
1525             MMI_HILOGD("defaultHotAreas:x:%d, y:%d, width:%{public}d, height:%{public}d",
1526                 win.x, win.y, win.width, win.height);
1527         }
1528         for (const auto &pointer : item.pointerHotAreas) {
1529             MMI_HILOGD("pointerHotAreas:x:%d, y:%d, width:%{public}d, height:%{public}d",
1530                 pointer.x, pointer.y, pointer.width, pointer.height);
1531         }
1532 
1533         window += StringPrintf("%d,", item.id);
1534         std::string dump;
1535         dump += StringPrintf("pointChangeAreas:[");
1536         for (auto it : item.pointerChangeAreas) {
1537             dump += StringPrintf("%d,", it);
1538         }
1539         dump += StringPrintf("]\n");
1540 
1541         dump += StringPrintf("transform:[");
1542         for (auto it : item.transform) {
1543             dump += StringPrintf("%f,", it);
1544         }
1545         dump += StringPrintf("]\n");
1546         std::istringstream stream(dump);
1547         std::string line;
1548         while (std::getline(stream, line, '\n')) {
1549             MMI_HILOGD("%{public}s", line.c_str());
1550         }
1551         if (!item.uiExtentionWindowInfo.empty()) {
1552             PrintWindowInfo(item.uiExtentionWindowInfo);
1553         }
1554     }
1555     window += StringPrintf("]\n");
1556     MMI_HILOGI("%{public}s", window.c_str());
1557 }
1558 
1559 void InputWindowsManager::PrintWindowGroupInfo(const WindowGroupInfo &windowGroupInfo)
1560 {
1561     if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
1562         return;
1563     }
1564     MMI_HILOGD("windowsGroupInfo,focusWindowId:%{public}d,displayId:%{public}d",
1565         windowGroupInfo.focusWindowId, windowGroupInfo.displayId);
1566     PrintWindowInfo(windowGroupInfo.windowsInfo);
1567 }
1568 
1569 void InputWindowsManager::PrintDisplayInfo()
1570 {
1571     if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
1572         return;
1573     }
1574     MMI_HILOGD("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d",
1575         displayGroupInfo_.width, displayGroupInfo_.height, displayGroupInfo_.focusWindowId);
1576     MMI_HILOGD("windowsInfos,num:%{public}zu", displayGroupInfo_.windowsInfo.size());
1577     PrintWindowInfo(displayGroupInfo_.windowsInfo);
1578 
1579     MMI_HILOGD("displayInfos,num:%{public}zu", displayGroupInfo_.displaysInfo.size());
1580     for (const auto &item : displayGroupInfo_.displaysInfo) {
1581         MMI_HILOGD("displayInfos,id:%{public}d,x:%d,y:%d,"
1582             "width:%{public}d,height:%{public}d,name:%{public}s,"
1583             "uniq:%{public}s,direction:%{public}d,displayDirection:%{public}d",
1584             item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
1585             item.uniq.c_str(), item.direction, item.displayDirection);
1586     }
1587 }
1588 
1589 const DisplayInfo* InputWindowsManager::GetPhysicalDisplay(int32_t id) const
1590 {
1591     for (auto &it : displayGroupInfo_.displaysInfo) {
1592         if (it.id == id) {
1593             return &it;
1594         }
1595     }
1596     MMI_HILOGW("Failed to obtain physical(%{public}d) display", id);
1597     return nullptr;
1598 }
1599 
1600 #ifdef OHOS_BUILD_ENABLE_TOUCH
1601 const DisplayInfo* InputWindowsManager::FindPhysicalDisplayInfo(const std::string& uniq) const
1602 {
1603     for (auto &it : displayGroupInfo_.displaysInfo) {
1604         if (it.uniq == uniq) {
1605             return &it;
1606         }
1607     }
1608     MMI_HILOGD("Failed to search for Physical,uniq:%{public}s", uniq.c_str());
1609     if (displayGroupInfo_.displaysInfo.size() > 0) {
1610         return &displayGroupInfo_.displaysInfo[0];
1611     }
1612     return nullptr;
1613 }
1614 
1615 const DisplayInfo *InputWindowsManager::GetDefaultDisplayInfo() const
1616 {
1617     return FindPhysicalDisplayInfo("default0");
1618 }
1619 #endif // OHOS_BUILD_ENABLE_TOUCH
1620 
1621 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1622 void InputWindowsManager::RotateScreen(const DisplayInfo& info, PhysicalCoordinate& coord) const
1623 {
1624     const Direction direction = info.direction;
1625     if (direction == DIRECTION0) {
1626         if (!TOUCH_DRAWING_MGR->IsWindowRotation() && cursorPos_.direction != info.direction &&
1627             sourceTemp_ == PointerEvent::SOURCE_TYPE_MOUSE) {
1628             if (cursorPos_.direction == Direction::DIRECTION90) {
1629                 double temp = coord.y;
1630                 coord.y = info.height - coord.x;
1631                 coord.x = temp;
1632             } else if (cursorPos_.direction == Direction::DIRECTION270) {
1633                 double temp = coord.x;
1634                 coord.x = info.width - coord.y;
1635                 coord.y = temp;
1636             }
1637         }
1638         MMI_HILOGD("direction is DIRECTION0");
1639         return;
1640     }
1641     if (direction == DIRECTION90) {
1642         MMI_HILOGD("direction is DIRECTION90");
1643         double temp = coord.x;
1644         if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1645             coord.x = info.height - coord.y;
1646         } else {
1647             coord.x = info.width - coord.y;
1648         }
1649         coord.y = temp;
1650         MMI_HILOGD("physicalX:%f, physicalY:%f", coord.x, coord.y);
1651         return;
1652     }
1653     if (direction == DIRECTION180) {
1654         MMI_HILOGD("direction is DIRECTION180");
1655         coord.x = info.width - coord.x;
1656         coord.y = info.height - coord.y;
1657         MMI_HILOGD("physicalX:%f, physicalY:%f", coord.x, coord.y);
1658         return;
1659     }
1660     if (direction == DIRECTION270) {
1661         MMI_HILOGD("direction is DIRECTION270");
1662         double temp = coord.y;
1663         if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1664             coord.y = info.width - coord.x;
1665         } else {
1666             coord.y = info.height - coord.x;
1667         }
1668         coord.x = temp;
1669         MMI_HILOGD("physicalX:%f, physicalY:%f", coord.x, coord.y);
1670     }
1671 }
1672 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1673 
1674 #ifdef OHOS_BUILD_ENABLE_TOUCH
1675 void InputWindowsManager::GetPhysicalDisplayCoord(struct libinput_event_touch* touch,
1676     const DisplayInfo& info, EventTouch& touchInfo)
1677 {
1678     auto width = info.width;
1679     auto height = info.height;
1680     if (info.direction == DIRECTION90 || info.direction == DIRECTION270) {
1681         width = info.height;
1682         height = info.width;
1683     }
1684     PhysicalCoordinate coord {
1685         .x = libinput_event_touch_get_x_transformed(touch, width),
1686         .y = libinput_event_touch_get_y_transformed(touch, height),
1687     };
1688     MMI_HILOGD("width:%{private}d, height:%{private}d, physicalX:%{private}f, physicalY:%{private}f",
1689         width, height, coord.x, coord.y);
1690     RotateScreen(info, coord);
1691     touchInfo.point.x = static_cast<int32_t>(coord.x);
1692     touchInfo.point.y = static_cast<int32_t>(coord.y);
1693     touchInfo.toolRect.point.x = static_cast<int32_t>(libinput_event_touch_get_tool_x_transformed(touch, width));
1694     touchInfo.toolRect.point.y = static_cast<int32_t>(libinput_event_touch_get_tool_y_transformed(touch, height));
1695     touchInfo.toolRect.width = static_cast<int32_t>(
1696         libinput_event_touch_get_tool_width_transformed(touch, width));
1697     touchInfo.toolRect.height = static_cast<int32_t>(
1698         libinput_event_touch_get_tool_height_transformed(touch, height));
1699 }
1700 
1701 void InputWindowsManager::SetAntiMisTake(bool state)
1702 {
1703     antiMistake_.isOpen = state;
1704 }
1705 
1706 void InputWindowsManager::SetAntiMisTakeStatus(bool state)
1707 {
1708     isOpenAntiMisTakeObserver_ = state;
1709 }
1710 
1711 bool InputWindowsManager::TouchPointToDisplayPoint(int32_t deviceId, struct libinput_event_touch* touch,
1712     EventTouch& touchInfo, int32_t& physicalDisplayId)
1713 {
1714     CHKPF(touch);
1715     std::string screenId = bindInfo_.GetBindDisplayNameByInputDevice(deviceId);
1716     if (screenId.empty()) {
1717         screenId = "default0";
1718     }
1719     auto info = FindPhysicalDisplayInfo(screenId);
1720     CHKPF(info);
1721     physicalDisplayId = info->id;
1722     if ((info->width <= 0) || (info->height <= 0)) {
1723         MMI_HILOGE("Get DisplayInfo is error");
1724         return false;
1725     }
1726     GetPhysicalDisplayCoord(touch, *info, touchInfo);
1727     return true;
1728 }
1729 
1730 bool InputWindowsManager::TransformTipPoint(struct libinput_event_tablet_tool* tip,
1731     PhysicalCoordinate& coord, int32_t& displayId) const
1732 {
1733     CHKPF(tip);
1734     auto displayInfo = FindPhysicalDisplayInfo("default0");
1735     CHKPF(displayInfo);
1736     MMI_HILOGD("PhysicalDisplay.width:%{public}d, PhysicalDisplay.height:%{public}d, "
1737                "PhysicalDisplay.topLeftX:%{public}d, PhysicalDisplay.topLeftY:%{public}d",
1738                displayInfo->width, displayInfo->height, displayInfo->x, displayInfo->y);
1739     displayId = displayInfo->id;
1740     auto width = displayInfo->width;
1741     auto height = displayInfo->height;
1742     if (displayInfo->direction == DIRECTION90 || displayInfo->direction == DIRECTION270) {
1743         width = displayInfo->height;
1744         height = displayInfo->width;
1745     }
1746     PhysicalCoordinate phys {
1747         .x = libinput_event_tablet_tool_get_x_transformed(tip, width),
1748         .y = libinput_event_tablet_tool_get_y_transformed(tip, height),
1749     };
1750     RotateScreen(*displayInfo, phys);
1751     coord.x = phys.x;
1752     coord.y = phys.y;
1753     MMI_HILOGD("physicalX:%{private}f, physicalY:%{private}f, displayId:%{public}d", phys.x, phys.y, displayId);
1754     return true;
1755 }
1756 
1757 bool InputWindowsManager::CalculateTipPoint(struct libinput_event_tablet_tool* tip,
1758     int32_t& targetDisplayId, PhysicalCoordinate& coord) const
1759 {
1760     CHKPF(tip);
1761     if (!TransformTipPoint(tip, coord, targetDisplayId)) {
1762         return false;
1763     }
1764     return true;
1765 }
1766 #endif // OHOS_BUILD_ENABLE_TOUCH
1767 
1768 #ifdef OHOS_BUILD_ENABLE_POINTER
1769 const DisplayGroupInfo& InputWindowsManager::GetDisplayGroupInfo()
1770 {
1771     return displayGroupInfo_;
1772 }
1773 
1774 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1775 bool InputWindowsManager::IsNeedRefreshLayer(int32_t windowId)
1776 {
1777     CALL_DEBUG_ENTER;
1778     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1779         return true;
1780     }
1781     MouseLocation mouseLocation = GetMouseInfo();
1782     int32_t displayId = MouseEventHdr->GetDisplayId();
1783     if (displayId < 0) {
1784         displayId = displayGroupInfo_.displaysInfo[0].id;
1785     }
1786     auto displayInfo = GetPhysicalDisplay(displayId);
1787     CHKPF(displayInfo);
1788     int32_t logicX = mouseLocation.physicalX + displayInfo->x;
1789     int32_t logicY = mouseLocation.physicalY + displayInfo->y;
1790     std::optional<WindowInfo> touchWindow = GetWindowInfo(logicX, logicY);
1791     if (!touchWindow) {
1792         MMI_HILOGE("TouchWindow is nullptr");
1793         return false;
1794     }
1795     if (touchWindow->id == windowId || windowId == GLOBAL_WINDOW_ID) {
1796         MMI_HILOGD("Need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
1797             touchWindow->id, windowId);
1798         return true;
1799     }
1800     MMI_HILOGD("Not need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
1801         touchWindow->id, windowId);
1802     return false;
1803 }
1804 #endif
1805 
1806 void InputWindowsManager::OnSessionLost(SessionPtr session)
1807 {
1808     CALL_DEBUG_ENTER;
1809     CHKPV(session);
1810     int32_t pid = session->GetPid();
1811     IPointerDrawingManager::GetInstance()->OnSessionLost(pid);
1812     auto it = pointerStyle_.find(pid);
1813     if (it != pointerStyle_.end()) {
1814         pointerStyle_.erase(it);
1815         MMI_HILOGD("Clear the pointer style map, pd:%{public}d", pid);
1816     }
1817 }
1818 #endif // OHOS_BUILD_ENABLE_POINTER
1819 
1820 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1821 int32_t InputWindowsManager::UpdatePoinerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
1822 {
1823     CALL_DEBUG_ENTER;
1824     auto it = pointerStyle_.find(pid);
1825     if (it == pointerStyle_.end()) {
1826         MMI_HILOG_CURSORE("The pointer style map is not include param pd:%{public}d", pid);
1827         return COMMON_PARAMETER_ERROR;
1828     }
1829     auto iter = it->second.find(windowId);
1830     if (iter != it->second.end()) {
1831         iter->second = pointerStyle;
1832         return RET_OK;
1833     }
1834 
1835     auto [iterator, sign] = it->second.insert(std::make_pair(windowId, pointerStyle));
1836     if (!sign) {
1837         MMI_HILOG_CURSORW("The window type is duplicated");
1838         return COMMON_PARAMETER_ERROR;
1839     }
1840     return RET_OK;
1841 }
1842 
1843 int32_t InputWindowsManager::UpdateSceneBoardPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
1844     bool isUiExtension)
1845 {
1846     CALL_DEBUG_ENTER;
1847     // update the pointerStyle for sceneboard
1848     auto scenePid = pid;
1849     auto sceneWinId = windowId;
1850     if (isUiExtension) {
1851         auto iter = uiExtensionPointerStyle_.find(scenePid);
1852         if (iter == uiExtensionPointerStyle_.end() || iter->second.find(sceneWinId) == iter->second.end()) {
1853             uiExtensionPointerStyle_[scenePid] = {};
1854             MMI_HILOG_CURSORE("SceneBoardPid %{public}d or windowId:%{public}d does not exist on"
1855                 "uiExtensionPointerStyle_", scenePid, sceneWinId);
1856         }
1857         uiExtensionPointerStyle_[scenePid][sceneWinId] = pointerStyle;
1858         MMI_HILOG_CURSORI("set uiextension pointer success. pid:%{public}d, windowid:%{public}d, pointerid:%{public}d",
1859             scenePid, sceneWinId, pointerStyle.id);
1860         return RET_OK;
1861     }
1862     auto sceneIter = pointerStyle_.find(scenePid);
1863     if (sceneIter == pointerStyle_.end() || sceneIter->second.find(sceneWinId) == sceneIter->second.end()) {
1864         pointerStyle_[scenePid] = {};
1865         MMI_HILOG_CURSORE("SceneBoardPid %{public}d or windowId:%{public}d does not exist on pointerStyle_",
1866             scenePid, sceneWinId);
1867     }
1868     pointerStyle_[scenePid][sceneWinId] = pointerStyle;
1869     MMI_HILOG_CURSORD("Sceneboard pid:%{public}d windowId:%{public}d is set to %{public}d",
1870         scenePid, sceneWinId, pointerStyle.id);
1871     auto it = pointerStyle_.find(pid);
1872     if (it == pointerStyle_.end()) {
1873         MMI_HILOG_CURSORE("Pid:%{public}d does not exist in mmi,", pid);
1874         std::map<int32_t, PointerStyle> tmpPointerStyle = {{windowId, pointerStyle}};
1875         auto res = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
1876         if (!res.second) return RET_ERR;
1877         return RET_OK;
1878     }
1879     auto iter = it->second.find(windowId);
1880     if (iter == it->second.end()) {
1881         auto res = it->second.insert(std::make_pair(windowId, pointerStyle));
1882         if (!res.second) return RET_ERR;
1883         return RET_OK;
1884     }
1885     iter->second = pointerStyle;
1886     SetMouseFlag(pointerActionFlag_ == PointerEvent::POINTER_ACTION_BUTTON_DOWN);
1887     return RET_OK;
1888 }
1889 
1890 void InputWindowsManager::SetUiExtensionInfo(bool isUiExtension, int32_t uiExtensionPid, int32_t uiExtensionWindoId)
1891 {
1892     MMI_HILOGI("SetUiExtensionInfo. pid:%{public}d, windowid:%{public}d", uiExtensionPid, uiExtensionWindoId);
1893     isUiExtension_ = isUiExtension;
1894     uiExtensionPid_ = uiExtensionPid;
1895     uiExtensionWindowId_ = uiExtensionWindoId;
1896 }
1897 
1898 void InputWindowsManager::SetGlobalDefaultPointerStyle()
1899 {
1900     for (auto &iter : pointerStyle_) {
1901         for (auto &item : iter.second) {
1902             if (item.second.id == DEFAULT_POINTER_STYLE) {
1903                 item.second.id = globalStyle_.id;
1904             } else if (item.second.id == CURSOR_CIRCLE_STYLE) {
1905                 item.second.id = globalStyle_.id;
1906             }
1907             item.second.options = globalStyle_.options;
1908         }
1909     }
1910 }
1911 
1912 int32_t InputWindowsManager::SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
1913     bool isUiExtension)
1914 {
1915     CALL_DEBUG_ENTER;
1916     if (windowId == GLOBAL_WINDOW_ID) {
1917         globalStyle_.id = pointerStyle.id;
1918         globalStyle_.options = pointerStyle.options;
1919         SetGlobalDefaultPointerStyle();
1920         MMI_HILOG_CURSORD("Setting global pointer style");
1921         return RET_OK;
1922     }
1923     MMI_HILOG_CURSORD("start to get pid by window %{public}d", windowId);
1924     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1925         return UpdatePoinerStyle(pid, windowId, pointerStyle);
1926     }
1927     if (!isUiExtension && uiExtensionPointerStyle_.count(pid) != 0) {
1928         MMI_HILOG_CURSORI("clear the uiextension mouse style for pid %{public}d", pid);
1929         uiExtensionPointerStyle_.erase(pid);
1930     }
1931     SetUiExtensionInfo(isUiExtension, pid, windowId);
1932     return UpdateSceneBoardPointerStyle(pid, windowId, pointerStyle, isUiExtension);
1933 }
1934 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1935 
1936 bool InputWindowsManager::IsMouseSimulate() const
1937 {
1938     if (lastPointerEvent_ == nullptr) {
1939         MMI_HILOG_CURSORD("lastPointerEvent is nullptr");
1940         return false;
1941     }
1942     return lastPointerEvent_->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE &&
1943         lastPointerEvent_->HasFlag(InputEvent::EVENT_FLAG_SIMULATE);
1944 }
1945 
1946 bool InputWindowsManager::HasMouseHideFlag() const
1947 {
1948     if (lastPointerEvent_ == nullptr) {
1949         MMI_HILOG_CURSORD("The lastPointerEvent is nullptr");
1950         return false;
1951     }
1952     return lastPointerEvent_->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER);
1953 }
1954 
1955 int32_t InputWindowsManager::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
1956 {
1957     CALL_DEBUG_ENTER;
1958 #ifdef OHOS_BUILD_ENABLE_POINTER
1959     auto it = pointerStyle_.find(pid);
1960     if (it == pointerStyle_.end()) {
1961         MMI_HILOG_CURSORE("Pid:%{public}d does not exist in mmi", pid);
1962         return RET_OK;
1963     }
1964     auto windowIt = it->second.find(windowId);
1965     if (windowIt == it->second.end()) {
1966         MMI_HILOG_CURSORE("windowId %{public}d does not exist in pid%{public}d", windowId, pid);
1967         return RET_OK;
1968     }
1969 
1970     it->second.erase(windowIt);
1971 #endif // OHOS_BUILD_ENABLE_POINTER
1972     return RET_OK;
1973 }
1974 
1975 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1976 int32_t InputWindowsManager::GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle,
1977     bool isUiExtension) const
1978 {
1979     CALL_DEBUG_ENTER;
1980     if (isUiExtension) {
1981         auto it = uiExtensionPointerStyle_.find(pid);
1982         if (it == uiExtensionPointerStyle_.end()) {
1983             MMI_HILOG_CURSORE("The uiextension pointer style map is not include pid:%{public}d", pid);
1984             pointerStyle.id = globalStyle_.id;
1985             return RET_OK;
1986         }
1987         auto iter = it->second.find(windowId);
1988         if (iter == it->second.end()) {
1989             pointerStyle.id = globalStyle_.id;
1990             return RET_OK;
1991         }
1992         MMI_HILOG_CURSORI("window type:%{public}d, get pointer style:%{public}d success", windowId, iter->second.id);
1993         pointerStyle = iter->second;
1994         return RET_OK;
1995     }
1996     if (windowId == GLOBAL_WINDOW_ID) {
1997         MMI_HILOG_CURSORD("Getting global pointer style");
1998         pointerStyle.id = globalStyle_.id;
1999         pointerStyle.options = globalStyle_.options;
2000         return RET_OK;
2001     }
2002     auto it = pointerStyle_.find(pid);
2003     if (it == pointerStyle_.end()) {
2004         if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2005             pointerStyle.id = globalStyle_.id;
2006             return RET_OK;
2007         }
2008         MMI_HILOG_CURSORE("The pointer style map is not include param pd, %{public}d", pid);
2009         return RET_OK;
2010     }
2011     auto iter = it->second.find(windowId);
2012     if (iter == it->second.end()) {
2013         pointerStyle.id = globalStyle_.id;
2014         return RET_OK;
2015     }
2016 
2017     MMI_HILOG_CURSORD("Window type:%{public}d get pointer style:%{public}d success", windowId, iter->second.id);
2018     pointerStyle = iter->second;
2019     return RET_OK;
2020 }
2021 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2022 
2023 #ifdef OHOS_BUILD_ENABLE_POINTER
2024 void InputWindowsManager::InitPointerStyle()
2025 {
2026     CALL_DEBUG_ENTER;
2027     PointerStyle pointerStyle;
2028     pointerStyle.id = DEFAULT_POINTER_STYLE;
2029     for (const auto& windowItem : displayGroupInfo_.windowsInfo) {
2030         int32_t pid = windowItem.pid;
2031         auto it = pointerStyle_.find(pid);
2032         if (it == pointerStyle_.end()) {
2033             std::map<int32_t, PointerStyle> tmpPointerStyle = {};
2034             auto iter = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
2035             if (!iter.second) {
2036                 MMI_HILOGW("The pd is duplicated");
2037             }
2038             continue;
2039         }
2040     }
2041     MMI_HILOGD("Number of pointer style:%{public}zu", pointerStyle_.size());
2042 }
2043 #endif // OHOS_BUILD_ENABLE_POINTER
2044 
2045 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2046 bool InputWindowsManager::IsInHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
2047     const WindowInfo &window) const
2048 {
2049     auto windowXY = TransformWindowXY(window, x, y);
2050     auto windowX = static_cast<int32_t>(windowXY.first);
2051     auto windowY = static_cast<int32_t>(windowXY.second);
2052     for (const auto &item : rects) {
2053         int32_t displayMaxX = 0;
2054         int32_t displayMaxY = 0;
2055         if (!AddInt32(item.x, item.width, displayMaxX)) {
2056             MMI_HILOGE("The addition of displayMaxX overflows");
2057             return false;
2058         }
2059         if (!AddInt32(item.y, item.height, displayMaxY)) {
2060             MMI_HILOGE("The addition of displayMaxY overflows");
2061             return false;
2062         }
2063         if (((windowX >= item.x) && (windowX < displayMaxX)) &&
2064             (windowY >= item.y) && (windowY < displayMaxY)) {
2065             return true;
2066         }
2067     }
2068     return false;
2069 }
2070 
2071 bool InputWindowsManager::InWhichHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
2072     PointerStyle &pointerStyle) const
2073 {
2074     int32_t areaNum = 0;
2075     bool findFlag = false;
2076     for (const auto &item : rects) {
2077         int32_t displayMaxX = 0;
2078         int32_t displayMaxY = 0;
2079         if (!AddInt32(item.x, item.width, displayMaxX)) {
2080             MMI_HILOGE("The addition of displayMaxX overflows");
2081             return findFlag;
2082         }
2083         if (!AddInt32(item.y, item.height, displayMaxY)) {
2084             MMI_HILOGE("The addition of displayMaxY overflows");
2085             return findFlag;
2086         }
2087         if (((x > item.x) && (x <= displayMaxX)) && (y > item.y) && (y <= displayMaxY)) {
2088             findFlag = true;
2089             pointerStyle.id = areaNum;
2090         }
2091         areaNum++;
2092     }
2093     if (!findFlag) {
2094         MMI_HILOGD("pointer not match any area");
2095         return findFlag;
2096     }
2097     switch (pointerStyle.id) {
2098         case PointerHotArea::TOP:
2099         case PointerHotArea::BOTTOM:
2100             pointerStyle.id = MOUSE_ICON::NORTH_SOUTH;
2101             break;
2102         case PointerHotArea::LEFT:
2103         case PointerHotArea::RIGHT:
2104             pointerStyle.id = MOUSE_ICON::WEST_EAST;
2105             break;
2106         case PointerHotArea::TOP_LEFT:
2107             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
2108             break;
2109         case PointerHotArea::TOP_RIGHT:
2110             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
2111             break;
2112         case PointerHotArea::BOTTOM_LEFT:
2113             pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
2114             break;
2115         case PointerHotArea::BOTTOM_RIGHT:
2116             pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
2117             break;
2118         default:
2119             MMI_HILOGD("pointerStyle in default is:%{public}d", pointerStyle.id);
2120             break;
2121     }
2122     MMI_HILOGD("pointerStyle after switch ID is :%{public}d", pointerStyle.id);
2123     return findFlag;
2124 }
2125 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2126 
2127 #ifdef OHOS_BUILD_ENABLE_TOUCH
2128 void InputWindowsManager::AdjustDisplayCoordinate(
2129     const DisplayInfo& displayInfo, double& physicalX, double& physicalY) const
2130 {
2131     int32_t width = displayInfo.width;
2132     int32_t height = displayInfo.height;
2133     if (physicalX <= 0) {
2134         physicalX = 0;
2135     }
2136     if (physicalX >= width && width > 0) {
2137         physicalX = width - 1;
2138     }
2139     if (physicalY <= 0) {
2140         physicalY = 0;
2141     }
2142     if (physicalY >= height && height > 0) {
2143         physicalY = height - 1;
2144     }
2145 }
2146 #endif // OHOS_BUILD_ENABLE_TOUCH
2147 
2148 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2149 bool InputWindowsManager::UpdateDisplayId(int32_t& displayId)
2150 {
2151     if (displayGroupInfo_.displaysInfo.empty()) {
2152         MMI_HILOGE("logicalDisplays_is empty");
2153         return false;
2154     }
2155     if (displayId < 0) {
2156         displayId = displayGroupInfo_.displaysInfo[0].id;
2157         return true;
2158     }
2159     for (const auto &item : displayGroupInfo_.displaysInfo) {
2160         if (item.id == displayId) {
2161             return true;
2162         }
2163     }
2164     return false;
2165 }
2166 
2167 std::optional<WindowInfo> InputWindowsManager::SelectWindowInfo(int32_t logicalX, int32_t logicalY,
2168     const std::shared_ptr<PointerEvent>& pointerEvent)
2169 {
2170     CALL_DEBUG_ENTER;
2171     int32_t action = pointerEvent->GetPointerAction();
2172     bool checkFlag = (firstBtnDownWindowId_ == -1) ||
2173         ((action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) && (pointerEvent->GetPressedButtons().size() == 1)) ||
2174         ((action == PointerEvent::POINTER_ACTION_MOVE) && (pointerEvent->GetPressedButtons().empty())) ||
2175         (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
2176         (action == PointerEvent::POINTER_ACTION_PULL_UP) ||
2177         ((action == PointerEvent::POINTER_ACTION_AXIS_BEGIN || action == PointerEvent::POINTER_ACTION_AXIS_END ||
2178         action == PointerEvent::POINTER_ACTION_ROTATE_BEGIN || action == PointerEvent::POINTER_ACTION_ROTATE_END) &&
2179         (pointerEvent->GetPressedButtons().empty()));
2180     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
2181     if (checkFlag) {
2182         int32_t targetWindowId = pointerEvent->GetTargetWindowId();
2183         static std::unordered_map<int32_t, int32_t> winId2ZorderMap;
2184         bool isHotArea = false;
2185         if (targetWindowId <= 1) {
2186             targetMouseWinIds_.clear();
2187         }
2188         for (const auto &item : windowsInfo) {
2189             if (transparentWins_.find(item.id) != transparentWins_.end()) {
2190                 if (IsTransparentWin(transparentWins_[item.id], logicalX - item.area.x, logicalY - item.area.y)) {
2191                     winId2ZorderMap.insert({item.id, item.zOrder});
2192                     MMI_HILOG_DISPATCHE("It's an abnormal window and pointer find the next window");
2193                     continue;
2194                 }
2195             }
2196             if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
2197                 !IsValidZorderWindow(item, pointerEvent)) {
2198                 winId2ZorderMap.insert({item.id, item.zOrder});
2199                 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching, "
2200                     "window:%{public}d, flags:%{public}d, pid:%{public}d", item.id, item.flags, item.pid);
2201                 continue;
2202             } else if ((extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
2203                 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
2204                 if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
2205                     if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
2206                         winId2ZorderMap.insert({item.id, item.zOrder});
2207                         continue;
2208                     }
2209                     firstBtnDownWindowId_ = item.id;
2210                     MMI_HILOG_DISPATCHD("Mouse event select pull window, window:%{public}d, pid:%{public}d",
2211                         firstBtnDownWindowId_, item.pid);
2212                     break;
2213                 } else {
2214                     winId2ZorderMap.insert({item.id, item.zOrder});
2215                     continue;
2216                 }
2217             } else if ((targetWindowId < 0) && (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item))) {
2218                 if ((item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) &&
2219                     ((pointerEvent->GetPressedButtons().empty()) ||
2220                     (action == PointerEvent::POINTER_ACTION_PULL_UP) ||
2221                     (action == PointerEvent::POINTER_ACTION_AXIS_BEGIN) ||
2222                     (action == PointerEvent::POINTER_ACTION_AXIS_UPDATE) ||
2223                     (action == PointerEvent::POINTER_ACTION_AXIS_END))) {
2224                     continue;
2225                 }
2226                 firstBtnDownWindowId_ = item.id;
2227                 if (!item.uiExtentionWindowInfo.empty()) {
2228                     // Determine whether the landing point as a safety sub window
2229                     CheckUIExtentionWindowPointerHotArea(logicalX, logicalY,
2230                         item.uiExtentionWindowInfo, firstBtnDownWindowId_);
2231                 }
2232                 MMI_HILOG_DISPATCHD("Find out the dispatch window of this pointer event when the targetWindowId "
2233                     "hasn't been set up yet, window:%{public}d, pid:%{public}d", firstBtnDownWindowId_, item.pid);
2234                 bool isSpecialWindow = HandleWindowInputType(item, pointerEvent);
2235                 if (isSpecialWindow) {
2236                     AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
2237                     isHotArea = true;
2238                     continue;
2239                 } else if (isHotArea) {
2240                     AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
2241                     break;
2242                 } else {
2243                     break;
2244                 }
2245 
2246             } else if ((targetWindowId >= 0) && (targetWindowId == item.id)) {
2247                 firstBtnDownWindowId_ = targetWindowId;
2248                 MMI_HILOG_DISPATCHD("Find out the dispatch window of this pointer event when the targetWindowId "
2249                     "has been set up already, window:%{public}d, pid:%{public}d", firstBtnDownWindowId_, item.pid);
2250                 break;
2251             } else {
2252                 winId2ZorderMap.insert({item.id, item.zOrder});
2253                 MMI_HILOG_DISPATCHD("Continue searching for the dispatch window of this pointer event");
2254             }
2255         }
2256         if ((firstBtnDownWindowId_ < 0) && (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) &&
2257             (pointerEvent->GetPressedButtons().size() == 1)) {
2258             for (auto iter = winId2ZorderMap.begin(); iter != winId2ZorderMap.end(); iter++) {
2259                 MMI_HILOG_DISPATCHI("%{public}d, %{public}d", iter->first, iter->second);
2260             }
2261         }
2262         winId2ZorderMap.clear();
2263     }
2264     MMI_HILOG_DISPATCHD("firstBtnDownWindowId_:%{public}d", firstBtnDownWindowId_);
2265     for (const auto &item : windowsInfo) {
2266         for (const auto &windowInfo : item.uiExtentionWindowInfo) {
2267             if (windowInfo.id == firstBtnDownWindowId_) {
2268                 return std::make_optional(windowInfo);
2269             }
2270         }
2271         if (item.id == firstBtnDownWindowId_) {
2272             return std::make_optional(item);
2273         }
2274     }
2275     return std::nullopt;
2276 }
2277 
2278 void InputWindowsManager::CheckUIExtentionWindowPointerHotArea(int32_t logicalX, int32_t logicalY,
2279     const std::vector<WindowInfo>& windowInfos, int32_t& windowId)
2280 {
2281     for (auto it = windowInfos.rbegin(); it != windowInfos.rend(); ++it) {
2282         if (IsInHotArea(logicalX, logicalY, it->pointerHotAreas, *it)) {
2283             windowId = it->id;
2284             break;
2285         }
2286     }
2287 }
2288 
2289 std::optional<WindowInfo> InputWindowsManager::GetWindowInfo(int32_t logicalX, int32_t logicalY)
2290 {
2291     CALL_DEBUG_ENTER;
2292     for (const auto& item : displayGroupInfo_.windowsInfo) {
2293         if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
2294             MMI_HILOGD("Skip the untouchable window to continue searching, "
2295                        "window:%{public}d, flags:%{public}d", item.id, item.flags);
2296             continue;
2297         } else if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
2298             return std::make_optional(item);
2299         } else {
2300             MMI_HILOGD("Continue searching for the dispatch window");
2301         }
2302     }
2303     return std::nullopt;
2304 }
2305 
2306 bool InputWindowsManager::SelectPointerChangeArea(const WindowInfo &windowInfo, PointerStyle &pointerStyle,
2307     int32_t logicalX, int32_t logicalY)
2308 {
2309     CALL_DEBUG_ENTER;
2310     int32_t windowId = windowInfo.id;
2311     bool findFlag = false;
2312     if (windowsHotAreas_.find(windowId) != windowsHotAreas_.end()) {
2313         std::vector<Rect> windowHotAreas = windowsHotAreas_[windowId];
2314         MMI_HILOG_CURSORD("windowHotAreas size:%{public}zu, windowId:%{public}d",
2315             windowHotAreas.size(), windowId);
2316         findFlag = InWhichHotArea(logicalX, logicalY, windowHotAreas, pointerStyle);
2317     }
2318     return findFlag;
2319 }
2320 
2321 void InputWindowsManager::UpdatePointerChangeAreas(const DisplayGroupInfo &displayGroupInfo)
2322 {
2323     CALL_DEBUG_ENTER;
2324     for (const auto &windowInfo : displayGroupInfo.windowsInfo) {
2325         std::vector<Rect> windowHotAreas;
2326         int32_t windowId = windowInfo.id;
2327         Rect windowArea = windowInfo.area;
2328         windowArea.width = windowInfo.transform[SCALE_X] != 0 ? windowInfo.area.width / windowInfo.transform[SCALE_X]
2329             : windowInfo.area.width;
2330         windowArea.height = windowInfo.transform[SCALE_Y] != 0 ?  windowInfo.area.height / windowInfo.transform[SCALE_Y]
2331             : windowInfo.area.height;
2332         std::vector<int32_t> pointerChangeAreas = windowInfo.pointerChangeAreas;
2333         UpdateTopBottomArea(windowArea, pointerChangeAreas, windowHotAreas);
2334         UpdateLeftRightArea(windowArea, pointerChangeAreas, windowHotAreas);
2335         UpdateInnerAngleArea(windowArea, pointerChangeAreas, windowHotAreas);
2336         if (windowsHotAreas_.find(windowId) == windowsHotAreas_.end()) {
2337             windowsHotAreas_.emplace(windowId, windowHotAreas);
2338         } else {
2339             windowsHotAreas_[windowId] = windowHotAreas;
2340         }
2341     }
2342 }
2343 
2344 void InputWindowsManager::UpdatePointerChangeAreas()
2345 {
2346     CALL_DEBUG_ENTER;
2347     UpdatePointerChangeAreas(displayGroupInfoTmp_);
2348 }
2349 
2350 void InputWindowsManager::UpdateTopBottomArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
2351     std::vector<Rect> &windowHotAreas)
2352 {
2353     CALL_DEBUG_ENTER;
2354     Rect newTopRect;
2355     newTopRect.x = windowArea.x + pointerChangeAreas[TOP_LEFT_AREA];
2356     newTopRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
2357     newTopRect.width = windowArea.width - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[TOP_RIGHT_AREA];
2358     newTopRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_AREA];
2359     Rect newBottomRect;
2360     newBottomRect.x = windowArea.x + pointerChangeAreas[BOTTOM_LEFT_AREA];
2361     newBottomRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_AREA];
2362     newBottomRect.width = windowArea.width - pointerChangeAreas[BOTTOM_LEFT_AREA] -
2363         pointerChangeAreas[BOTTOM_RIGHT_AREA];
2364     newBottomRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_AREA];
2365     if (pointerChangeAreas[TOP_AREA] == 0) {
2366         newTopRect.width = 0;
2367         newTopRect.height = 0;
2368     }
2369     if (pointerChangeAreas[BOTTOM_AREA] == 0) {
2370         newBottomRect.width = 0;
2371         newBottomRect.height = 0;
2372     }
2373     windowHotAreas.push_back(newTopRect);
2374     windowHotAreas.push_back(newBottomRect);
2375 }
2376 
2377 void InputWindowsManager::UpdateLeftRightArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
2378     std::vector<Rect> &windowHotAreas)
2379 {
2380     CALL_DEBUG_ENTER;
2381     Rect newLeftRect;
2382     newLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
2383     newLeftRect.y = windowArea.y + pointerChangeAreas[TOP_LEFT_AREA];
2384     newLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[LEFT_AREA];
2385     newLeftRect.height = windowArea.height - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[BOTTOM_LEFT_AREA];
2386     Rect newRightRect;
2387     newRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[RIGHT_AREA];
2388     newRightRect.y = windowArea.y + pointerChangeAreas[TOP_RIGHT_AREA];
2389     newRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[RIGHT_AREA];
2390     newRightRect.height = windowArea.height - pointerChangeAreas[TOP_RIGHT_AREA] -
2391         pointerChangeAreas[BOTTOM_RIGHT_AREA];
2392     if (pointerChangeAreas[LEFT_AREA] == 0) {
2393         newLeftRect.width = 0;
2394         newLeftRect.height = 0;
2395     }
2396     if (pointerChangeAreas[RIGHT_AREA] == 0) {
2397         newRightRect.width = 0;
2398         newRightRect.height = 0;
2399     }
2400     windowHotAreas.push_back(newLeftRect);
2401     windowHotAreas.push_back(newRightRect);
2402 }
2403 
2404 void InputWindowsManager::UpdateInnerAngleArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
2405     std::vector<Rect> &windowHotAreas)
2406 {
2407     CALL_DEBUG_ENTER;
2408     Rect newTopLeftRect;
2409     newTopLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
2410     newTopLeftRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
2411     newTopLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
2412     newTopLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
2413     Rect newTopRightRect;
2414     newTopRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[TOP_RIGHT_AREA];
2415     newTopRightRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
2416     newTopRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
2417     newTopRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
2418     Rect newBottomLeftRect;
2419     newBottomLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
2420     newBottomLeftRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_LEFT_AREA];
2421     newBottomLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
2422     newBottomLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
2423     Rect newBottomRightRect;
2424     newBottomRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[BOTTOM_RIGHT_AREA];
2425     newBottomRightRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_RIGHT_AREA];
2426     newBottomRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
2427     newBottomRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
2428     if (pointerChangeAreas[TOP_LEFT_AREA] == 0) {
2429         newTopLeftRect.width = 0;
2430         newTopLeftRect.height = 0;
2431     }
2432     if (pointerChangeAreas[TOP_RIGHT_AREA] == 0) {
2433         newTopRightRect.width = 0;
2434         newTopRightRect.height = 0;
2435     }
2436     if (pointerChangeAreas[BOTTOM_LEFT_AREA] == 0) {
2437         newBottomLeftRect.width = 0;
2438         newBottomLeftRect.height = 0;
2439     }
2440     if (pointerChangeAreas[BOTTOM_RIGHT_AREA] == 0) {
2441         newBottomRightRect.width = 0;
2442         newBottomRightRect.height = 0;
2443     }
2444 
2445     windowHotAreas.push_back(newTopLeftRect);
2446     windowHotAreas.push_back(newTopRightRect);
2447     windowHotAreas.push_back(newBottomLeftRect);
2448     windowHotAreas.push_back(newBottomRightRect);
2449 }
2450 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2451 
2452 #ifdef OHOS_BUILD_ENABLE_POINTER
2453 void InputWindowsManager::UpdatePointerEvent(int32_t logicalX, int32_t logicalY,
2454     const std::shared_ptr<PointerEvent>& pointerEvent, const WindowInfo& touchWindow)
2455 {
2456     CHKPV(pointerEvent);
2457     MMI_HILOG_DISPATCHD("LastWindowInfo:%{public}d, touchWindow:%{public}d", lastWindowInfo_.id, touchWindow.id);
2458     if (lastWindowInfo_.id != touchWindow.id) {
2459         DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
2460         lastLogicX_ = logicalX;
2461         lastLogicY_ = logicalY;
2462         lastPointerEvent_ = pointerEvent;
2463         lastWindowInfo_ = touchWindow;
2464         DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
2465         return;
2466     }
2467     lastLogicX_ = logicalX;
2468     lastLogicY_ = logicalY;
2469     lastPointerEvent_ = pointerEvent;
2470     lastWindowInfo_ = touchWindow;
2471 }
2472 
2473 int32_t InputWindowsManager::SetHoverScrollState(bool state)
2474 {
2475     CALL_DEBUG_ENTER;
2476     MMI_HILOGD("Set mouse hover scroll state:%{public}d", state);
2477     std::string name = "isEnableHoverScroll";
2478     return PREFERENCES_MGR->SetBoolValue(name, MOUSE_FILE_NAME, state);
2479 }
2480 
2481 bool InputWindowsManager::GetHoverScrollState() const
2482 {
2483     CALL_DEBUG_ENTER;
2484     std::string name = "isEnableHoverScroll";
2485     bool state = PREFERENCES_MGR->GetBoolValue(name, true);
2486     MMI_HILOGD("Get mouse hover scroll state:%{public}d", state);
2487     return state;
2488 }
2489 
2490 int32_t InputWindowsManager::UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)
2491 {
2492     CALL_DEBUG_ENTER;
2493     CHKPR(pointerEvent, ERROR_NULL_POINTER);
2494     auto displayId = pointerEvent->GetTargetDisplayId();
2495     if (!UpdateDisplayId(displayId)) {
2496         MMI_HILOGE("This display:%{public}d is not existent", displayId);
2497         return RET_ERR;
2498     }
2499     pointerEvent->SetTargetDisplayId(displayId);
2500 
2501     auto physicalDisplayInfo = GetPhysicalDisplay(displayId);
2502     CHKPR(physicalDisplayInfo, ERROR_NULL_POINTER);
2503     int32_t pointerId = pointerEvent->GetPointerId();
2504     PointerEvent::PointerItem pointerItem;
2505     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
2506         MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
2507         return RET_ERR;
2508     }
2509     int32_t logicalX = 0;
2510     int32_t logicalY = 0;
2511     if (!AddInt32(pointerItem.GetDisplayX(), physicalDisplayInfo->x, logicalX)) {
2512         MMI_HILOGE("The addition of logicalX overflows");
2513         return RET_ERR;
2514     }
2515     if (!AddInt32(pointerItem.GetDisplayY(), physicalDisplayInfo->y, logicalY)) {
2516         MMI_HILOGE("The addition of logicalY overflows");
2517         return RET_ERR;
2518     }
2519     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
2520         ClearTargetWindowId(pointerId);
2521     }
2522     int32_t physicalX = pointerItem.GetDisplayX();
2523     int32_t physicalY = pointerItem.GetDisplayY();
2524     auto touchWindow = SelectWindowInfo(logicalX, logicalY, pointerEvent);
2525     if (!touchWindow) {
2526         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN || mouseDownInfo_.id == -1) {
2527             MMI_HILOGE("touchWindow is nullptr, targetWindow:%{public}d", pointerEvent->GetTargetWindowId());
2528             if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) &&
2529                 !IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
2530                 IsMouseDrawing(pointerEvent->GetPointerAction())) {
2531                     MMI_HILOGD("Turn the mouseDisplay from false to true");
2532                     IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
2533             }
2534             int64_t beginTime = GetSysClockTime();
2535 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
2536             IPointerDrawingManager::GetInstance()->DrawMovePointer(displayId, physicalX, physicalY);
2537 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
2538             int64_t endTime = GetSysClockTime();
2539             if ((endTime - beginTime) > RS_PROCESS_TIMEOUT) {
2540                 MMI_HILOGW("Rs process timeout");
2541             }
2542             return RET_ERR;
2543         }
2544         touchWindow = std::make_optional(mouseDownInfo_);
2545         int32_t pointerAction = pointerEvent->GetPointerAction();
2546         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_MOVE ||
2547             pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
2548             pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT ||
2549             pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
2550             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
2551         } else {
2552             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
2553         }
2554         pointerEvent->SetOriginPointerAction(pointerAction);
2555         MMI_HILOGI("mouse event send cancel, window:%{public}d, pid:%{public}d", touchWindow->id, touchWindow->pid);
2556     }
2557 
2558     bool checkFlag = pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_UPDATE ||
2559         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
2560         pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END;
2561     if (checkFlag) {
2562         if ((!GetHoverScrollState()) && (displayGroupInfo_.focusWindowId != touchWindow->id)) {
2563             MMI_HILOGD("disable mouse hover scroll in inactive window, targetWindowId:%{public}d", touchWindow->id);
2564             return RET_OK;
2565         }
2566     }
2567     PointerStyle pointerStyle;
2568 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
2569     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2570         if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) &&
2571             !IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
2572             IsMouseDrawing(pointerEvent->GetPointerAction())) {
2573             MMI_HILOGD("Turn the mouseDisplay from false to true");
2574             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
2575         }
2576         pointerStyle = IPointerDrawingManager::GetInstance()->GetLastMouseStyle();
2577         MMI_HILOGD("showing the lastMouseStyle %{public}d, lastPointerStyle %{public}d",
2578             pointerStyle.id, lastPointerStyle_.id);
2579         IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
2580         WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
2581         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
2582     } else {
2583         GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
2584         if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) &&
2585             !IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
2586             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
2587             DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
2588         }
2589         IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
2590         WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
2591         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
2592     }
2593 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
2594     GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
2595     if (isUiExtension_) {
2596         MMI_HILOGD("updatemouse target in uiextension");
2597         GetPointerStyle(uiExtensionPid_, uiExtensionWindowId_, pointerStyle, isUiExtension_);
2598         dragPointerStyle_ = pointerStyle;
2599     } else {
2600         GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
2601     }
2602     if (!isDragBorder_ && !isUiExtension_) {
2603         GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
2604         dragPointerStyle_ = pointerStyle;
2605     }
2606     WindowInfo window = *touchWindow;
2607     if (!dragFlag_) {
2608         isDragBorder_ = SelectPointerChangeArea(window, pointerStyle, logicalX, logicalY);
2609         dragPointerStyle_ = pointerStyle;
2610         MMI_HILOGD("pointerStyle is :%{public}d, windowId is :%{public}d, logicalX is :%{private}d,"
2611             "logicalY is :%{private}d", pointerStyle.id, window.id, logicalX, logicalY);
2612     }
2613     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
2614         SetMouseFlag(true);
2615         dragFlag_ = true;
2616         MMI_HILOGD("Is in drag scene");
2617     }
2618     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
2619         SetMouseFlag(false);
2620         dragFlag_ = false;
2621         isDragBorder_ = false;
2622     }
2623     Direction direction = DIRECTION0;
2624     if (TOUCH_DRAWING_MGR->IsWindowRotation() && Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2625         direction = physicalDisplayInfo->direction;
2626         TOUCH_DRAWING_MGR->GetOriginalTouchScreenCoordinates(direction, physicalDisplayInfo->width,
2627             physicalDisplayInfo->height, physicalX, physicalY);
2628     }
2629 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
2630     MAGIC_POINTER_VELOCITY_TRACKER->MonitorCursorMovement(pointerEvent);
2631 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
2632     int64_t beginTime = GetSysClockTime();
2633 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
2634     if (IsMouseDrawing(pointerEvent->GetPointerAction())) {
2635         if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER)) {
2636             MMI_HILOGE("SetMouseDisplayState false");
2637             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
2638         } else {
2639             MMI_HILOGE("SetMouseDisplayState true");
2640             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
2641         }
2642         IPointerDrawingManager::GetInstance()->DrawPointer(displayId, physicalX, physicalY,
2643             dragPointerStyle_, direction);
2644     }
2645 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
2646     cursorPos_.direction = physicalDisplayInfo->direction;
2647     int64_t endTime = GetSysClockTime();
2648     if ((endTime - beginTime) > RS_PROCESS_TIMEOUT) {
2649         MMI_HILOGW("Rs process timeout");
2650     }
2651     if (captureModeInfo_.isCaptureMode && (touchWindow->id != captureModeInfo_.windowId)) {
2652         captureModeInfo_.isCaptureMode = false;
2653     }
2654     SetPrivacyModeFlag(touchWindow->privacyMode, pointerEvent);
2655     pointerEvent->SetTargetWindowId(touchWindow->id);
2656     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
2657     DispatchUIExtentionPointerEvent(logicalX, logicalY, pointerEvent);
2658     auto windowX = logicalX - touchWindow->area.x;
2659     auto windowY = logicalY - touchWindow->area.y;
2660     if (!(touchWindow->transform.empty())) {
2661         auto windowXY = TransformWindowXY(*touchWindow, logicalX, logicalY);
2662         windowX = windowXY.first;
2663         windowY = windowXY.second;
2664     }
2665     windowX = static_cast<int32_t>(windowX);
2666     windowY = static_cast<int32_t>(windowY);
2667     pointerItem.SetWindowX(windowX);
2668     pointerItem.SetWindowY(windowY);
2669     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
2670     if ((extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE)) ||
2671         (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
2672         pointerEvent->SetBuffer(extraData_.buffer);
2673         pointerEvent->SetPullId(extraData_.pullId);
2674         UpdatePointerAction(pointerEvent);
2675     } else {
2676         pointerEvent->ClearBuffer();
2677     }
2678     CHKPR(udsServer_, ERROR_NULL_POINTER);
2679 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
2680     UpdatePointerEvent(logicalX, logicalY, pointerEvent, *touchWindow);
2681 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
2682 #ifdef OHOS_BUILD_ENABLE_ANCO
2683     if (touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY)) {
2684         MMI_HILOGD("Process mouse event in Anco window, targetWindowId:%{public}d", touchWindow->id);
2685         pointerEvent->SetAncoDeal(true);
2686         SimulatePointerExt(pointerEvent);
2687         return RET_OK;
2688     }
2689 #endif // OHOS_BUILD_ENABLE_ANCO
2690     int32_t action = pointerEvent->GetPointerAction();
2691     if (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
2692         mouseDownInfo_ = *touchWindow;
2693     }
2694     if (action == PointerEvent::POINTER_ACTION_BUTTON_UP) {
2695         InitMouseDownInfo();
2696         MMI_HILOGD("Mouse up, clear mouse down info");
2697     }
2698     if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
2699         MMI_HILOGD("pid:%{public}d, id:%{public}d, agentWindowId:%{public}d,"
2700             "logicalX:%d, logicalY:%d,displayX:%d, displayY:%d, windowX:%d, windowY:%d",
2701             isUiExtension_ ? uiExtensionPid_ : touchWindow->pid, isUiExtension_ ? uiExtensionWindowId_ :
2702             touchWindow->id, touchWindow->agentWindowId,
2703             logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
2704     } else {
2705         MMI_HILOGD("pid:%{public}d, id:%{public}d, agentWindowId:%{public}d,"
2706             "logicalX:%d, logicalY:%d,displayX:%d, displayY:%d, windowX:%d, windowY:%d",
2707             isUiExtension_ ? uiExtensionPid_ : touchWindow->pid, isUiExtension_ ? uiExtensionWindowId_ :
2708             touchWindow->id, touchWindow->agentWindowId,
2709             logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
2710     }
2711     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
2712         MMI_HILOGD("Clear extra data");
2713         ClearExtraData();
2714     }
2715     return ERR_OK;
2716 }
2717 #endif // OHOS_BUILD_ENABLE_POINTER
2718 
2719 bool InputWindowsManager::IsMouseDrawing(int32_t currentAction)
2720 {
2721     if (currentAction != PointerEvent::POINTER_ACTION_LEAVE_WINDOW &&
2722         currentAction != PointerEvent::POINTER_ACTION_ENTER_WINDOW &&
2723         currentAction != PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW &&
2724         currentAction != PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
2725         return true;
2726     }
2727     return false;
2728 }
2729 
2730 void InputWindowsManager::SetMouseFlag(bool state)
2731 {
2732     mouseFlag_ = state;
2733 }
2734 
2735 bool InputWindowsManager::GetMouseFlag()
2736 {
2737     return mouseFlag_;
2738 }
2739 
2740 #ifdef OHOS_BUILD_ENABLE_POINTER
2741 void InputWindowsManager::JudgMouseIsDownOrUp(bool dragState)
2742 {
2743     if (!dragState && (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP ||
2744         pointerActionFlag_ == PointerEvent::POINTER_ACTION_BUTTON_DOWN)) {
2745         SetMouseFlag(true);
2746         return;
2747     }
2748     if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
2749         SetMouseFlag(true);
2750     }
2751 }
2752 #endif // OHOS_BUILD_ENABLE_POINTER
2753 
2754 int32_t InputWindowsManager::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
2755 {
2756     if (windowId < 0) {
2757         MMI_HILOGE("Windowid(%{public}d) is invalid", windowId);
2758         return RET_ERR;
2759     }
2760     if ((captureModeInfo_.isCaptureMode == isCaptureMode) && !isCaptureMode) {
2761         MMI_HILOGE("Windowid:(%{public}d) is not capture mode", windowId);
2762         return RET_OK;
2763     }
2764     captureModeInfo_.windowId = windowId;
2765     captureModeInfo_.isCaptureMode = isCaptureMode;
2766     MMI_HILOGI("Windowid:(%{public}d) is (%{public}d)", windowId, isCaptureMode);
2767     return RET_OK;
2768 }
2769 
2770 bool InputWindowsManager::GetMouseIsCaptureMode() const
2771 {
2772     return captureModeInfo_.isCaptureMode;
2773 }
2774 
2775 bool InputWindowsManager::IsNeedDrawPointer(PointerEvent::PointerItem &pointerItem) const
2776 {
2777     if (pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN) {
2778         static int32_t lastDeviceId = -1;
2779         static std::shared_ptr<InputDevice> inputDevice = nullptr;
2780         auto nowId = pointerItem.GetDeviceId();
2781         if (lastDeviceId != nowId) {
2782             inputDevice = INPUT_DEV_MGR->GetInputDevice(nowId);
2783             CHKPF(inputDevice);
2784             lastDeviceId = nowId;
2785         }
2786         if (inputDevice != nullptr) {
2787             MMI_HILOGD("name:%{public}s type:%{public}d bus:%{public}d, "
2788                        "version:%{public}d product:%{public}d vendor:%{public}d, "
2789                        "phys:%{public}s uniq:%{public}s",
2790                        inputDevice->GetName().c_str(), inputDevice->GetType(), inputDevice->GetBus(),
2791                        inputDevice->GetVersion(), inputDevice->GetProduct(), inputDevice->GetVendor(),
2792                        inputDevice->GetPhys().c_str(), inputDevice->GetUniq().c_str());
2793         }
2794         if (inputDevice != nullptr && inputDevice->GetBus() == BUS_USB) {
2795             return true;
2796         }
2797     }
2798     return false;
2799 }
2800 
2801 #ifdef OHOS_BUILD_ENABLE_TOUCH
2802 bool InputWindowsManager::SkipAnnotationWindow(uint32_t flag, int32_t toolType)
2803 {
2804     return ((flag & WindowInfo::FLAG_BIT_HANDWRITING) == WindowInfo::FLAG_BIT_HANDWRITING &&
2805             toolType == PointerEvent::TOOL_TYPE_FINGER);
2806 }
2807 
2808 bool InputWindowsManager::SkipNavigationWindow(WindowInputType windowType, int32_t toolType)
2809 {
2810     MMI_HILOGD("windowType: %{public}d, toolType: %{public}d", static_cast<int32_t>(windowType), toolType);
2811     if ((windowType != WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE &&
2812         windowType != WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) || toolType != PointerEvent::TOOL_TYPE_PEN) {
2813         return false;
2814     }
2815     if (!isOpenAntiMisTakeObserver_) {
2816         antiMistake_.switchName = NAVIGATION_SWITCH_NAME;
2817         CreateAntiMisTakeObserver(antiMistake_);
2818         isOpenAntiMisTakeObserver_ = true;
2819         MMI_HILOGI("Get anti mistake touch switch start");
2820         SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).GetBoolValue(NAVIGATION_SWITCH_NAME,
2821             antiMistake_.isOpen);
2822         MMI_HILOGI("Get anti mistake touch switch end");
2823     }
2824     if (antiMistake_.isOpen) {
2825         MMI_HILOGD("Anti mistake switch is open");
2826         return true;
2827     }
2828     return false;
2829 }
2830 
2831 void InputWindowsManager::GetUIExtentionWindowInfo(std::vector<WindowInfo> &uiExtentionWindowInfo, int32_t windowId,
2832     WindowInfo **touchWindow, bool &isUiExtentionWindow)
2833 {
2834     for (auto &windowinfo : uiExtentionWindowInfo) {
2835         if (windowinfo.id == windowId) {
2836             *touchWindow = &windowinfo;
2837             isUiExtentionWindow = true;
2838             break;
2839         }
2840     }
2841 }
2842 #endif // OHOS_BUILD_ENABLE_TOUCH
2843 
2844 bool InputWindowsManager::CheckPidInSession(int32_t pid)
2845 {
2846     return pointerStyle_.find(pid) != pointerStyle_.end();
2847 }
2848 
2849 #ifdef OHOS_BUILD_ENABLE_TOUCH
2850 bool InputWindowsManager::IsValidNavigationWindow(const WindowInfo& touchWindow, double physicalX, double physicalY)
2851 {
2852     return (touchWindow.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE ||
2853             touchWindow.windowInputType == WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) &&
2854             IsInHotArea(static_cast<int32_t>(physicalX), static_cast<int32_t>(physicalY),
2855             touchWindow.defaultHotAreas, touchWindow);
2856 }
2857 
2858 bool InputWindowsManager::IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)
2859 {
2860     return (pointerEvent->GetZOrder() > 0 && pointerEvent->GetTargetWindowId() == -1);
2861 }
2862 
2863 void InputWindowsManager::UpdateTransformDisplayXY(std::shared_ptr<PointerEvent> pointerEvent,
2864     std::vector<WindowInfo>& windowsInfo, const DisplayInfo& displayInfo)
2865 {
2866     CHKPV(pointerEvent);
2867     bool isNavigationWindow = false;
2868     int32_t pointerId = pointerEvent->GetPointerId();
2869     PointerEvent::PointerItem pointerItem;
2870     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
2871         MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
2872         return;
2873     }
2874     double physicalX = 0;
2875     double physicalY = 0;
2876     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
2877         physicalX = pointerItem.GetDisplayX();
2878         physicalY = pointerItem.GetDisplayY();
2879     } else {
2880         physicalX = pointerItem.GetDisplayXPos();
2881         physicalY = pointerItem.GetDisplayYPos();
2882     }
2883     for (auto &item : windowsInfo) {
2884         if (IsValidNavigationWindow(item, physicalX, physicalY) &&
2885             !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION) && pointerEvent->GetZOrder() <= 0) {
2886             isNavigationWindow = true;
2887             break;
2888         }
2889     }
2890     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) ||
2891         pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION) ||
2892         IsNavigationWindowInjectEvent(pointerEvent)) {
2893         if (!displayInfo.transform.empty() &&
2894             ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_UP) ||
2895             pointerEvent->GetZOrder() > 0) && !isNavigationWindow) {
2896             auto displayXY = TransformDisplayXY(displayInfo, physicalX, physicalY);
2897             physicalX = displayXY.first;
2898             physicalY = displayXY.second;
2899         }
2900     }
2901     if (isNavigationWindow && pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
2902         pointerEvent->AddFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION);
2903     }
2904     pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
2905     pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
2906     pointerItem.SetDisplayXPos(physicalX);
2907     pointerItem.SetDisplayYPos(physicalY);
2908     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
2909 }
2910 #endif // OHOS_BUILD_ENABLE_TOUCH
2911 
2912 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2913 void InputWindowsManager::SendUIExtentionPointerEvent(int32_t logicalX, int32_t logicalY,
2914     const WindowInfo& windowInfo, std::shared_ptr<PointerEvent> pointerEvent)
2915 {
2916     MMI_HILOG_DISPATCHI("Dispatch uiExtention pointer Event,pid:%{public}d", windowInfo.pid);
2917     int32_t pointerId = pointerEvent->GetPointerId();
2918     PointerEvent::PointerItem pointerItem;
2919     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
2920         MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
2921         return;
2922     }
2923     auto windowX = logicalX - windowInfo.area.x;
2924     auto windowY = logicalY - windowInfo.area.y;
2925     if (!(windowInfo.transform.empty())) {
2926         auto windowXY = TransformWindowXY(windowInfo, logicalX, logicalY);
2927         windowX = windowXY.first;
2928         windowY = windowXY.second;
2929     }
2930     auto physicDisplayInfo = GetPhysicalDisplay(pointerEvent->GetTargetDisplayId());
2931     CHKPV(physicDisplayInfo);
2932     double physicalX = logicalX - physicDisplayInfo->x;
2933     double physicalY = logicalY - physicDisplayInfo->y;
2934     pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
2935     pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
2936     pointerItem.SetWindowX(static_cast<int32_t>(windowX));
2937     pointerItem.SetWindowY(static_cast<int32_t>(windowY));
2938     pointerItem.SetTargetWindowId(windowInfo.id);
2939     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
2940     auto fd = udsServer_->GetClientFd(windowInfo.pid);
2941     auto sess = udsServer_->GetSession(fd);
2942     CHKPRV(sess, "The window has disappeared");
2943     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
2944     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
2945     if (!sess->SendMsg(pkt)) {
2946         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
2947         return;
2948     }
2949 }
2950 
2951 void InputWindowsManager::DispatchUIExtentionPointerEvent(int32_t logicalX, int32_t logicalY,
2952     std::shared_ptr<PointerEvent> pointerEvent)
2953 {
2954     auto displayId = pointerEvent->GetTargetDisplayId();
2955     const std::vector<WindowInfo> &windowsInfo = GetWindowGroupInfoByDisplayId(displayId);
2956     auto windowId = pointerEvent->GetTargetWindowId();
2957     for (const auto& item : windowsInfo) {
2958         if (windowId == item.id) {
2959             return;
2960         }
2961         for (const auto& windowInfo : item.uiExtentionWindowInfo) {
2962             if (windowInfo.id == windowId) {
2963                 MMI_HILOG_DISPATCHI("Dispatch uiExtention pointer Event,windowId:%{public}d", item.id);
2964                 // If the event is sent to the security sub window, then a copy needs to be sent to the host window
2965                 pointerEvent->SetAgentWindowId(item.agentWindowId);
2966                 pointerEvent->SetTargetWindowId(item.id);
2967                 SendUIExtentionPointerEvent(logicalX, logicalY, item, pointerEvent);
2968                 pointerEvent->SetAgentWindowId(windowInfo.agentWindowId);
2969                 pointerEvent->SetTargetWindowId(windowInfo.id);
2970                 return;
2971             }
2972         }
2973     }
2974 }
2975 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2976 
2977 #ifdef OHOS_BUILD_ENABLE_TOUCH
2978 int32_t InputWindowsManager::UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)
2979 {
2980     CHKPR(pointerEvent, ERROR_NULL_POINTER);
2981     auto displayId = pointerEvent->GetTargetDisplayId();
2982     if (!UpdateDisplayId(displayId)) {
2983         MMI_HILOG_DISPATCHE("This display is not existent");
2984         return RET_ERR;
2985     }
2986     pointerEvent->SetTargetDisplayId(displayId);
2987 
2988     auto physicDisplayInfo = GetPhysicalDisplay(displayId);
2989     CHKPR(physicDisplayInfo, ERROR_NULL_POINTER);
2990     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(displayId);
2991     UpdateTransformDisplayXY(pointerEvent, windowsInfo, *physicDisplayInfo);
2992     int32_t pointerId = pointerEvent->GetPointerId();
2993     PointerEvent::PointerItem pointerItem;
2994     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
2995         MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
2996         return RET_ERR;
2997     }
2998     double physicalX = 0;
2999     double physicalY = 0;
3000     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
3001         physicalX = pointerItem.GetDisplayX();
3002         physicalY = pointerItem.GetDisplayY();
3003     } else {
3004         physicalX = pointerItem.GetDisplayXPos();
3005         physicalY = pointerItem.GetDisplayYPos();
3006     }
3007     if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
3008         AdjustDisplayCoordinate(*physicDisplayInfo, physicalX, physicalY);
3009     }
3010     int32_t logicalX1 = 0;
3011     int32_t logicalY1 = 0;
3012 
3013     if (!AddInt32(static_cast<int32_t>(physicalX), physicDisplayInfo->x, logicalX1)) {
3014         MMI_HILOG_DISPATCHE("The addition of logicalX overflows");
3015         return RET_ERR;
3016     }
3017     if (!AddInt32(static_cast<int32_t>(physicalY), physicDisplayInfo->y, logicalY1)) {
3018         MMI_HILOG_DISPATCHE("The addition of logicalY overflows");
3019         return RET_ERR;
3020     }
3021     double logicalX = physicalX + physicDisplayInfo->x;
3022     double logicalY = physicalY + physicDisplayInfo->y;
3023     const WindowInfo *touchWindow = nullptr;
3024     auto targetWindowId = pointerItem.GetTargetWindowId();
3025     bool isHotArea = false;
3026     bool isFirstSpecialWindow = false;
3027     static std::unordered_map<int32_t, int32_t> winMap;
3028     if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN ||
3029         (pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN && MMI_LE(pointerItem.GetPressure(), 0.0f))) {
3030         ClearTargetWindowId(pointerId);
3031     }
3032     for (auto &item : windowsInfo) {
3033         bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
3034             !IsValidZorderWindow(item, pointerEvent);
3035         if (checkWindow) {
3036             MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
3037                 "window:%{public}d, flags:%{public}d", item.id, item.flags);
3038             winMap.insert({item.id, item.zOrder});
3039             continue;
3040         }
3041         if (transparentWins_.find(item.id) != transparentWins_.end()) {
3042             if (IsTransparentWin(transparentWins_[item.id], logicalX - item.area.x, logicalY - item.area.y)) {
3043                 MMI_HILOG_DISPATCHE("It's an abnormal window and touchscreen find the next window");
3044                 winMap.insert({item.id, item.zOrder});
3045                 continue;
3046             }
3047         }
3048         if (SkipAnnotationWindow(item.flags, pointerItem.GetToolType())) {
3049             winMap.insert({item.id, item.zOrder});
3050             continue;
3051         }
3052         if (SkipNavigationWindow(item.windowInputType, pointerItem.GetToolType())) {
3053             winMap.insert({item.id, item.zOrder});
3054             continue;
3055         }
3056 
3057         bool checkToolType = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
3058             ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
3059             pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
3060         checkToolType = checkToolType || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
3061         if (checkToolType) {
3062             if (IsInHotArea(static_cast<int32_t>(logicalX), static_cast<int32_t>(logicalY),
3063                 item.defaultHotAreas, item)) {
3064                 if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
3065                     continue;
3066                 }
3067                 touchWindow = &item;
3068                 break;
3069             } else {
3070                 winMap.insert({item.id, item.zOrder});
3071                 continue;
3072             }
3073         }
3074         if (targetWindowId >= 0 && pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN &&
3075             (pointerItem.GetToolType() != PointerEvent::TOOL_TYPE_PEN || pointerItem.GetPressure() > 0)) {
3076             bool isUiExtentionWindow = false;
3077             for (auto &windowinfo : item.uiExtentionWindowInfo) {
3078                 if (windowinfo.id == targetWindowId) {
3079                     touchWindow = &windowinfo;
3080                     isUiExtentionWindow = true;
3081                     break;
3082                 }
3083             }
3084             if (isUiExtentionWindow) {
3085                 break;
3086             }
3087             if (item.id == targetWindowId) {
3088                 touchWindow = &item;
3089                 break;
3090             }
3091         } else if (IsInHotArea(static_cast<int32_t>(logicalX), static_cast<int32_t>(logicalY),
3092             item.defaultHotAreas, item)) {
3093             touchWindow = &item;
3094             bool isSpecialWindow = HandleWindowInputType(item, pointerEvent);
3095             if (!isFirstSpecialWindow) {
3096                 isFirstSpecialWindow = isSpecialWindow;
3097                 if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
3098                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE &&
3099                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_MOVE &&
3100                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
3101                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
3102                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
3103                     pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE) {
3104                     MMI_HILOG_DISPATCHD("the first special window status:%{public}d", isFirstSpecialWindow);
3105                 }
3106             }
3107             std::pair<int32_t, int32_t> logicalXY(std::make_pair(static_cast<int32_t>(logicalX),
3108                 static_cast<int32_t>(logicalY)));
3109             // Determine whether the landing point is a safety sub window
3110             CheckUIExtentionWindowDefaultHotArea(logicalXY, isHotArea, pointerEvent, item.uiExtentionWindowInfo,
3111                 &touchWindow);
3112             if (isSpecialWindow) {
3113                 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
3114                 isHotArea = true;
3115                 continue;
3116             }
3117             break;
3118         } else {
3119             winMap.insert({item.id, item.zOrder});
3120         }
3121     }
3122     std::map<int32_t, WindowInfoEX> tmpWindowInfo;
3123     if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
3124         tmpWindowInfo = shellTouchItemDownInfos_;
3125     } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
3126         tmpWindowInfo = accessTouchItemDownInfos_;
3127     } else {
3128         tmpWindowInfo = touchItemDownInfos_;
3129     }
3130     if (touchWindow == nullptr) {
3131         auto it = tmpWindowInfo.find(pointerId);
3132         if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
3133             if (it == tmpWindowInfo.end() ||
3134                 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
3135                 MMI_HILOG_DISPATCHE("The touchWindow is nullptr, logicalX:%{private}f,"
3136                     "logicalY:%{private}f, pointerId:%{public}d", logicalX, logicalY, pointerId);
3137                 return RET_ERR;
3138             }
3139         }
3140         touchWindow = &it->second.window;
3141         if (it->second.flag) {
3142             if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_MOVE ||
3143                 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
3144                 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT ||
3145                 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
3146                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
3147             } else {
3148                 int32_t originPointerAction = pointerEvent->GetPointerAction();
3149                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
3150                 pointerEvent->SetOriginPointerAction(originPointerAction);
3151             }
3152             MMI_HILOG_DISPATCHI("Not found event down target window, maybe this window was untouchable,"
3153                 "need send cancel event, windowId:%{public}d pointerId:%{public}d", touchWindow->id, pointerId);
3154         }
3155     }
3156     winMap.clear();
3157     pointerEvent->SetTargetWindowId(touchWindow->id);
3158     pointerItem.SetTargetWindowId(touchWindow->id);
3159 #ifdef OHOS_BUILD_ENABLE_ANCO
3160     bool isInAnco = touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY);
3161     if (isInAnco) {
3162         MMI_HILOG_DISPATCHD("Process touch screen event in Anco window, targetWindowId:%{public}d", touchWindow->id);
3163         std::vector<int32_t> windowIds;
3164         GetTargetWindowIds(pointerId, pointerEvent->GetSourceType(), windowIds);
3165         if (windowIds.size() <= 1) {
3166             pointerEvent->SetAncoDeal(true);
3167         } else {
3168             for (int32_t windowId : windowIds) {
3169                 auto windowInfo = GetWindowAndDisplayInfo(windowId, pointerEvent->GetTargetDisplayId());
3170                 if (!windowInfo) {
3171                     continue;
3172                 }
3173                 isFirstSpecialWindow = isFirstSpecialWindow || HandleWindowInputType(*windowInfo, pointerEvent);
3174             }
3175         }
3176         pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3177         // Simulate uinput automated injection operations (MMI_GE(pointerEvent->GetZOrder(), 0.0f))
3178         bool isCompensatePointer = (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) &&
3179             !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) ||
3180             (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) &&
3181             !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_GENERATE_FROM_REAL));
3182         if (isCompensatePointer || isFirstSpecialWindow) {
3183             SimulatePointerExt(pointerEvent);
3184             isFirstSpecialWindow = false;
3185         } else {
3186 #ifdef OHOS_RSS_CLIENT
3187             if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
3188                 std::unordered_map<std::string, std::string> mapPayload;
3189                 mapPayload["msg"] = "";
3190                 constexpr int32_t touchDownBoost = 1006;
3191                 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
3192                     OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchDownBoost, mapPayload);
3193             } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP) {
3194                 constexpr int32_t touchUpBoost = 1007;
3195                 std::unordered_map<std::string, std::string> mapPayload;
3196                 mapPayload["msg"] = "";
3197                 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
3198                     OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchUpBoost, mapPayload);
3199             }
3200 #endif // OHOS_RSS_CLIENT
3201         }
3202         if (displayGroupInfo_.focusWindowId == touchWindow->id) {
3203             return RET_OK;
3204         }
3205         pointerEvent->SetAncoDeal(false);
3206     }
3207 #endif // OHOS_BUILD_ENABLE_ANCO
3208     if (touchWindow->windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
3209         if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
3210             lastTouchEventOnBackGesture_ = std::make_shared<PointerEvent>(*pointerEvent);
3211         }
3212         if (lastTouchEventOnBackGesture_ != nullptr &&
3213             lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL) {
3214             lastTouchEventOnBackGesture_ = std::make_shared<PointerEvent>(*pointerEvent);
3215         }
3216     }
3217     auto windowX = logicalX - touchWindow->area.x;
3218     auto windowY = logicalY - touchWindow->area.y;
3219     if (!(touchWindow->transform.empty())) {
3220         auto windowXY = TransformWindowXY(*touchWindow, logicalX, logicalY);
3221         windowX = windowXY.first;
3222         windowY = windowXY.second;
3223     }
3224     SetPrivacyModeFlag(touchWindow->privacyMode, pointerEvent);
3225     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
3226     DispatchUIExtentionPointerEvent(logicalX, logicalY, pointerEvent);
3227     pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
3228     pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
3229     pointerItem.SetWindowX(static_cast<int32_t>(windowX));
3230     pointerItem.SetWindowY(static_cast<int32_t>(windowY));
3231     pointerItem.SetDisplayXPos(physicalX);
3232     pointerItem.SetDisplayYPos(physicalY);
3233     pointerItem.SetWindowXPos(windowX);
3234     pointerItem.SetWindowYPos(windowY);
3235     pointerItem.SetToolWindowX(pointerItem.GetToolDisplayX() + physicDisplayInfo->x - touchWindow->area.x);
3236     pointerItem.SetToolWindowY(pointerItem.GetToolDisplayY() + physicDisplayInfo->y - touchWindow->area.y);
3237     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3238     bool checkExtraData = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
3239         ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
3240         pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
3241     checkExtraData = checkExtraData || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
3242     int32_t pointerAction = pointerEvent->GetPointerAction();
3243     if ((pointerAction == PointerEvent::POINTER_ACTION_DOWN) && !checkExtraData) {
3244         lastTouchLogicX_ = logicalX;
3245         lastTouchLogicY_ = logicalY;
3246         lastTouchEvent_ = pointerEvent;
3247         lastTouchWindowInfo_ = *touchWindow;
3248     }
3249     if (checkExtraData) {
3250         pointerEvent->SetBuffer(extraData_.buffer);
3251         pointerEvent->SetPullId(extraData_.pullId);
3252         UpdatePointerAction(pointerEvent);
3253         PullEnterLeaveEvent(logicalX, logicalY, pointerEvent, touchWindow);
3254     }
3255     // pointerAction:PA, targetWindowId:TWI, foucsWindowId:FWI, eventId:EID,
3256     // logicalX:LX, logicalY:LY, displayX:DX, displayX:DY, windowX:WX, windowY:WY,
3257     // width:W, height:H, area.x:AX, area.y:AY, displayId:DID, AgentWindowId: AWI
3258     if ((pointerAction != PointerEvent::POINTER_ACTION_MOVE &&
3259         pointerAction != PointerEvent::POINTER_ACTION_PULL_MOVE &&
3260         pointerAction != PointerEvent::POINTER_ACTION_HOVER_MOVE &&
3261         pointerAction != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
3262         pointerAction != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
3263         pointerAction != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
3264         pointerAction != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE)) {
3265         if (!EventLogHelper::IsBetaVersion()) {
3266             MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
3267                 "FWI:%{public}d,EID:%{public}d,"
3268                 "flags:%{public}d,DID:%{public}d"
3269                 "AWI:%{public}d,zOrder:%{public}d",
3270                 pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
3271                 displayGroupInfo_.focusWindowId, pointerEvent->GetId(),
3272                 touchWindow->flags, displayId,
3273                 pointerEvent->GetAgentWindowId(), static_cast<int32_t>(touchWindow->zOrder));
3274         } else {
3275             if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
3276                 MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
3277                     "FWI:%{public}d,EID:%{public}d,"
3278                     "W:%{public}d,H:%{public}d,AX:%{public}d,AY:%{public}d,"
3279                     "flags:%{public}d,DID:%{public}d"
3280                     "AWI:%{public}d,zOrder:%{public}d",
3281                     pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
3282                     displayGroupInfo_.focusWindowId, pointerEvent->GetId(),
3283                     touchWindow->area.width, touchWindow->area.height, touchWindow->area.x,
3284                     touchWindow->area.y, touchWindow->flags, displayId,
3285                     pointerEvent->GetAgentWindowId(), static_cast<int32_t>(touchWindow->zOrder));
3286             } else {
3287                 MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
3288                     "FWI:%{public}d,EID:%{public}d,"
3289                     "W:%{public}d,H:%{public}d,AX:%{public}d,AY:%{public}d,"
3290                     "flags:%{public}d,DID:%{public}d"
3291                     "AWI:%{public}d,zOrder:%{public}d",
3292                     pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
3293                     displayGroupInfo_.focusWindowId, pointerEvent->GetId(),
3294                     touchWindow->area.width, touchWindow->area.height, touchWindow->area.x,
3295                     touchWindow->area.y, touchWindow->flags, displayId,
3296                     pointerEvent->GetAgentWindowId(), static_cast<int32_t>(touchWindow->zOrder));
3297             }
3298         }
3299     }
3300 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
3301     bool gestureInject = false;
3302     if ((pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) && MMI_GNE(pointerEvent->GetZOrder(), 0.0f)) {
3303         gestureInject = true;
3304     }
3305     if (IsNeedDrawPointer(pointerItem)) {
3306         if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) &&
3307             !IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
3308             IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3309             if (touchWindow->id != lastWindowInfo_.id) {
3310                 lastWindowInfo_ = *touchWindow;
3311             }
3312             DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
3313         }
3314         PointerStyle pointerStyle;
3315         GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
3316         IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicDisplayInfo);
3317         WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
3318         IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
3319         IPointerDrawingManager::GetInstance()->DrawPointer(displayId,
3320             pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), pointerStyle, physicDisplayInfo->direction);
3321     } else if (IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
3322         if ((!checkExtraData) && (!(extraData_.appended &&
3323             extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE))) {
3324             MMI_HILOG_DISPATCHD("PointerAction is to leave the window");
3325             DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
3326             if (!gestureInject) {
3327                 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
3328             }
3329         }
3330     }
3331 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
3332     lastPointerEventforWindowChange_ = pointerEvent;
3333     pointerAction = pointerEvent->GetPointerAction();
3334     if (pointerAction == PointerEvent::POINTER_ACTION_DOWN ||
3335         pointerAction == PointerEvent::POINTER_ACTION_HOVER_ENTER) {
3336         WindowInfoEX windowInfoEX;
3337         windowInfoEX.window = *touchWindow;
3338         windowInfoEX.flag = true;
3339         if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
3340             shellTouchItemDownInfos_[pointerId] = windowInfoEX;
3341         } else if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
3342             accessTouchItemDownInfos_[pointerId] = windowInfoEX;
3343         } else {
3344             touchItemDownInfos_[pointerId] = windowInfoEX;
3345         }
3346         MMI_HILOG_FREEZEI("PointerId:%{public}d, touchWindow:%{public}d", pointerId, touchWindow->id);
3347     } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
3348         MMI_HILOG_DISPATCHD("Clear extra data");
3349         pointerEvent->ClearBuffer();
3350         lastTouchEvent_ = nullptr;
3351         lastTouchWindowInfo_.id = -1;
3352         ClearExtraData();
3353     }
3354     return ERR_OK;
3355 }
3356 
3357 void InputWindowsManager::CheckUIExtentionWindowDefaultHotArea(std::pair<int32_t, int32_t> logicalXY,
3358     bool isHotArea, const std::shared_ptr<PointerEvent> pointerEvent,
3359     const std::vector<WindowInfo>& windowInfos, const WindowInfo** touchWindow)
3360 {
3361     CHKPV(pointerEvent);
3362     CHKPV(touchWindow);
3363     CHKPV(*touchWindow);
3364     int32_t uiExtentionWindowId = 0;
3365     int32_t windowId = (*touchWindow)->id;
3366     int32_t logicalX = logicalXY.first;
3367     int32_t logicalY = logicalXY.second;
3368     for (auto it = windowInfos.rbegin(); it != windowInfos.rend(); ++it) {
3369         if (IsInHotArea(logicalX, logicalY, it->defaultHotAreas, *it)) {
3370             uiExtentionWindowId = it->id;
3371             break;
3372         }
3373     }
3374     if (uiExtentionWindowId > 0) {
3375         for (auto &windowinfo : windowInfos) {
3376             if (windowinfo.id == uiExtentionWindowId) {
3377                 *touchWindow = &windowinfo;
3378                 MMI_HILOG_DISPATCHD("uiExtentionWindowid:%{public}d", uiExtentionWindowId);
3379                 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), uiExtentionWindowId);
3380                 break;
3381             }
3382         }
3383     }
3384     if (isHotArea) {
3385         AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), windowId);
3386     }
3387 }
3388 
3389 void InputWindowsManager::PullEnterLeaveEvent(int32_t logicalX, int32_t logicalY,
3390     const std::shared_ptr<PointerEvent> pointerEvent, const WindowInfo* touchWindow)
3391 {
3392     CHKPV(pointerEvent);
3393     CHKPV(touchWindow);
3394     MMI_HILOG_DISPATCHD("LastTouchWindowInfo:%{public}d, touchWindow:%{public}d",
3395         lastTouchWindowInfo_.id, touchWindow->id);
3396     if (lastTouchWindowInfo_.id != touchWindow->id) {
3397         if (lastTouchWindowInfo_.id != -1) {
3398             DispatchTouch(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
3399         }
3400         lastTouchLogicX_ = logicalX;
3401         lastTouchLogicY_ = logicalY;
3402         lastTouchEvent_ = pointerEvent;
3403         lastTouchWindowInfo_ = *touchWindow;
3404         DispatchTouch(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
3405         return;
3406     }
3407     lastTouchLogicX_ = logicalX;
3408     lastTouchLogicY_ = logicalY;
3409     lastTouchEvent_ = pointerEvent;
3410     lastTouchWindowInfo_ = *touchWindow;
3411 }
3412 
3413 void InputWindowsManager::DispatchTouch(int32_t pointerAction)
3414 {
3415     CALL_INFO_TRACE;
3416     CHKPV(udsServer_);
3417     CHKPV(lastTouchEvent_);
3418     if (pointerAction == PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
3419         WindowInfo touchWindow;
3420         bool isChanged { false };
3421         for (const auto &item : displayGroupInfo_.windowsInfo) {
3422             if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
3423                 MMI_HILOGD("Skip the untouchable window to continue searching, "
3424                     "window:%{public}d, flags:%{public}d", item.id, item.flags);
3425                 continue;
3426             }
3427             if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
3428                 continue;
3429             }
3430             if (IsInHotArea(lastTouchLogicX_, lastTouchLogicY_, item.defaultHotAreas, item)) {
3431                 touchWindow = item;
3432                 isChanged = true;
3433                 break;
3434             }
3435         }
3436         if (!isChanged) {
3437             MMI_HILOGE("touchWindow is not init");
3438             return;
3439         }
3440         if (touchWindow.id != lastTouchWindowInfo_.id) {
3441             lastTouchWindowInfo_ = touchWindow;
3442         }
3443     }
3444     auto pointerEvent = PointerEvent::Create();
3445     CHKPV(pointerEvent);
3446     PointerEvent::PointerItem lastPointerItem;
3447     int32_t lastPointerId = lastTouchEvent_->GetPointerId();
3448     if (!lastTouchEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
3449         MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
3450         return;
3451     }
3452     PointerEvent::PointerItem currentPointerItem;
3453     currentPointerItem.SetWindowX(lastTouchLogicX_ - lastTouchWindowInfo_.area.x);
3454     currentPointerItem.SetWindowY(lastTouchLogicY_ - lastTouchWindowInfo_.area.y);
3455     currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
3456     currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
3457     currentPointerItem.SetPointerId(lastPointerId);
3458 
3459     pointerEvent->UpdateId();
3460     LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
3461     pointerEvent->SetTargetDisplayId(lastTouchEvent_->GetTargetDisplayId());
3462     SetPrivacyModeFlag(lastTouchWindowInfo_.privacyMode, pointerEvent);
3463     pointerEvent->SetTargetWindowId(lastTouchWindowInfo_.id);
3464     pointerEvent->SetAgentWindowId(lastTouchWindowInfo_.agentWindowId);
3465     pointerEvent->SetPointerId(lastPointerId);
3466     pointerEvent->AddPointerItem(currentPointerItem);
3467     pointerEvent->SetPointerAction(pointerAction);
3468     pointerEvent->SetBuffer(extraData_.buffer);
3469     pointerEvent->SetPullId(extraData_.pullId);
3470     pointerEvent->SetSourceType(lastTouchEvent_->GetSourceType());
3471     int64_t time = GetSysClockTime();
3472     pointerEvent->SetActionTime(time);
3473     pointerEvent->SetActionStartTime(time);
3474     pointerEvent->SetDeviceId(lastTouchEvent_->GetDeviceId());
3475     auto fd = udsServer_->GetClientFd(lastTouchWindowInfo_.pid);
3476     auto sess = udsServer_->GetSession(fd);
3477     if (sess == nullptr) {
3478         MMI_HILOGI("The last window has disappeared");
3479         return;
3480     }
3481 
3482     EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
3483     NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
3484     InputEventDataTransformation::Marshalling(pointerEvent, pkt);
3485     if (!sess->SendMsg(pkt)) {
3486         MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
3487         return;
3488     }
3489 }
3490 #endif // OHOS_BUILD_ENABLE_TOUCH
3491 
3492 #ifdef OHOS_BUILD_ENABLE_POINTER
3493 int32_t InputWindowsManager::UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)
3494 {
3495     CALL_DEBUG_ENTER;
3496     int32_t pointerAction = pointerEvent->GetPointerAction();
3497     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
3498     switch (pointerAction) {
3499         case PointerEvent::POINTER_ACTION_BUTTON_DOWN:
3500         case PointerEvent::POINTER_ACTION_BUTTON_UP:
3501         case PointerEvent::POINTER_ACTION_MOVE: {
3502             return UpdateMouseTarget(pointerEvent);
3503         }
3504         case PointerEvent::POINTER_ACTION_DOWN: {
3505             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
3506             pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
3507             pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
3508             return UpdateMouseTarget(pointerEvent);
3509         }
3510         case PointerEvent::POINTER_ACTION_UP: {
3511             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
3512             pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
3513             pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
3514             return UpdateMouseTarget(pointerEvent);
3515         }
3516         default: {
3517             MMI_HILOG_DISPATCHE("pointer action is unknown, pointerAction:%{public}d", pointerAction);
3518             return RET_ERR;
3519         }
3520     }
3521     return RET_OK;
3522 }
3523 #endif // OHOS_BUILD_ENABLE_POINTER
3524 
3525 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
3526 int32_t InputWindowsManager::UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)
3527 {
3528     CALL_DEBUG_ENTER;
3529     CHKPR(pointerEvent, ERROR_NULL_POINTER);
3530     int32_t focusWindowId = displayGroupInfo_.focusWindowId;
3531     const WindowInfo* windowInfo = nullptr;
3532     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
3533     for (const auto &item : windowsInfo) {
3534         if (item.id == focusWindowId) {
3535             windowInfo = &item;
3536             break;
3537         }
3538     }
3539     CHKPR(windowInfo, ERROR_NULL_POINTER);
3540     SetPrivacyModeFlag(windowInfo->privacyMode, pointerEvent);
3541     pointerEvent->SetTargetWindowId(windowInfo->id);
3542     pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
3543     MMI_HILOG_DISPATCHD("focusWindow:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
3544 
3545     return RET_OK;
3546 }
3547 #endif // OHOS_BUILD_ENABLE_JOYSTICK
3548 
3549 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_CROWN)
3550 int32_t InputWindowsManager::UpdateCrownTarget(std::shared_ptr<PointerEvent> pointerEvent)
3551 {
3552     CALL_DEBUG_ENTER;
3553     CHKPR(pointerEvent, ERROR_NULL_POINTER);
3554     int32_t focusWindowId = displayGroupInfo_.focusWindowId;
3555     const WindowInfo* windowInfo = nullptr;
3556     std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
3557     for (const auto &item : windowsInfo) {
3558         if (item.id == focusWindowId) {
3559             windowInfo = &item;
3560             break;
3561         }
3562     }
3563     CHKPR(windowInfo, ERROR_NULL_POINTER);
3564     SetPrivacyModeFlag(windowInfo->privacyMode, pointerEvent);
3565     pointerEvent->SetTargetWindowId(windowInfo->id);
3566     pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
3567     MMI_HILOG_DISPATCHD("focusWindow:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
3568 
3569     return RET_OK;
3570 }
3571 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_CROWN
3572 
3573 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3574 void InputWindowsManager::DrawTouchGraphic(std::shared_ptr<PointerEvent> pointerEvent)
3575 {
3576     CALL_DEBUG_ENTER;
3577     CHKPV(pointerEvent);
3578 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
3579     if (knuckleDrawMgr_ == nullptr) {
3580         knuckleDrawMgr_ = std::make_shared<KnuckleDrawingManager>();
3581     }
3582 #ifndef OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
3583     if (knuckleDynamicDrawingManager_ == nullptr) {
3584         knuckleDynamicDrawingManager_ = std::make_shared<KnuckleDynamicDrawingManager>();
3585         if (knuckleDrawMgr_ != nullptr) {
3586             knuckleDynamicDrawingManager_->SetKnuckleDrawingManager(knuckleDrawMgr_);
3587         }
3588     }
3589 #endif // OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
3590 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
3591     auto displayId = pointerEvent->GetTargetDisplayId();
3592     if (!UpdateDisplayId(displayId)) {
3593         MMI_HILOGE("This display is not exist");
3594         return;
3595     }
3596     auto physicDisplayInfo = GetPhysicalDisplay(displayId);
3597     CHKPV(physicDisplayInfo);
3598 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY) && \
3599     defined(OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER)
3600     std::shared_ptr<OHOS::MMI::InputEventHandler> inputHandler = InputHandler;
3601     CHKPV(InputHandler->GetKeyCommandHandler());
3602     auto knuckleSwitch = InputHandler->GetKeyCommandHandler()->GetKnuckleSwitchValue();
3603     auto isInMethodWindow = InputHandler->GetKeyCommandHandler()->CheckInputMethodArea(pointerEvent);
3604     if (isInMethodWindow) {
3605         int32_t pointerId = pointerEvent->GetPointerId();
3606         PointerEvent::PointerItem item;
3607         if (!pointerEvent->GetPointerItem(pointerId, item)) {
3608             MMI_HILOGE("Invalid pointer:%{public}d", pointerId);
3609             return;
3610         }
3611         if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) {
3612             item.SetToolType(PointerEvent::TOOL_TYPE_FINGER);
3613             pointerEvent->UpdatePointerItem(pointerId, item);
3614         }
3615     }
3616     if (!knuckleSwitch && !isInMethodWindow) {
3617         knuckleDrawMgr_->UpdateDisplayInfo(*physicDisplayInfo);
3618         knuckleDrawMgr_->KnuckleDrawHandler(pointerEvent);
3619 #ifndef OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
3620         knuckleDynamicDrawingManager_->UpdateDisplayInfo(*physicDisplayInfo);
3621         knuckleDynamicDrawingManager_->KnuckleDynamicDrawHandler(pointerEvent);
3622 #endif // OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
3623     }
3624 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY && OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
3625 
3626     TOUCH_DRAWING_MGR->UpdateDisplayInfo(*physicDisplayInfo);
3627     TOUCH_DRAWING_MGR->TouchDrawHandler(pointerEvent);
3628 }
3629 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3630 
3631 template <class T>
3632 void InputWindowsManager::CreateAntiMisTakeObserver(T& item)
3633 {
3634     CALL_INFO_TRACE;
3635     SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
3636         if (SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).GetBoolValue(key, item.isOpen) != RET_OK) {
3637             MMI_HILOGE("Get settingdata failed, key: %{public}s", key.c_str());
3638         }
3639         MMI_HILOGI("Anti mistake observer key: %{public}s, statusValue: %{public}d", key.c_str(), item.isOpen);
3640     };
3641     sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
3642         .CreateObserver(item.switchName, updateFunc);
3643     if (statusObserver == nullptr) {
3644         MMI_HILOGE("Create observer failed");
3645         return;
3646     }
3647     ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
3648     if (ret != ERR_OK) {
3649         MMI_HILOGE("Register setting observer failed, ret: %{public}d", ret);
3650         statusObserver = nullptr;
3651     }
3652 }
3653 
3654 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3655 int32_t InputWindowsManager::UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)
3656 {
3657     CALL_DEBUG_ENTER;
3658     CHKPR(pointerEvent, ERROR_NULL_POINTER);
3659     auto source = pointerEvent->GetSourceType();
3660     sourceTemp_ = source;
3661     pointerActionFlag_ = pointerEvent->GetPointerAction();
3662 #ifdef OHOS_BUILD_ENABLE_ANCO
3663     pointerEvent->SetAncoDeal(false);
3664 #endif // OHOS_BUILD_ENABLE_ANCO
3665     if (IsFoldable_ && IgnoreTouchEvent(pointerEvent)) {
3666         MMI_HILOG_DISPATCHD("Ignore touch event, pointerAction:%{public}d", pointerActionFlag_);
3667         return RET_OK;
3668     };
3669     switch (source) {
3670 #ifdef OHOS_BUILD_ENABLE_TOUCH
3671         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
3672             return UpdateTouchScreenTarget(pointerEvent);
3673         }
3674 #endif // OHOS_BUILD_ENABLE_TOUCH
3675 #ifdef OHOS_BUILD_ENABLE_POINTER
3676         case PointerEvent::SOURCE_TYPE_MOUSE: {
3677             return UpdateMouseTarget(pointerEvent);
3678         }
3679         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
3680             return UpdateTouchPadTarget(pointerEvent);
3681         }
3682 #endif // OHOS_BUILD_ENABLE_POINTER
3683 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
3684         case PointerEvent::SOURCE_TYPE_JOYSTICK: {
3685             return UpdateJoystickTarget(pointerEvent);
3686         }
3687 #endif // OHOS_BUILD_ENABLE_JOYSTICK
3688 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_CROWN)
3689         case PointerEvent::SOURCE_TYPE_CROWN: {
3690             return UpdateCrownTarget(pointerEvent);
3691         }
3692 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_CROWN
3693         default: {
3694             MMI_HILOG_DISPATCHE("Source type is unknown, source:%{public}d", source);
3695             break;
3696         }
3697     }
3698     return RET_ERR;
3699 }
3700 
3701 bool InputWindowsManager::IsInsideDisplay(const DisplayInfo& displayInfo, int32_t physicalX, int32_t physicalY)
3702 {
3703     return (physicalX >= 0 && physicalX < displayInfo.width) && (physicalY >= 0 && physicalY < displayInfo.height);
3704 }
3705 
3706 void InputWindowsManager::FindPhysicalDisplay(const DisplayInfo& displayInfo, int32_t& physicalX,
3707     int32_t& physicalY, int32_t& displayId)
3708 {
3709     CALL_DEBUG_ENTER;
3710     int32_t logicalX = 0;
3711     int32_t logicalY = 0;
3712     if (!AddInt32(physicalX, displayInfo.x, logicalX)) {
3713         MMI_HILOGE("The addition of logicalX overflows");
3714         return;
3715     }
3716     if (!AddInt32(physicalY, displayInfo.y, logicalY)) {
3717         MMI_HILOGE("The addition of logicalY overflows");
3718         return;
3719     }
3720     for (const auto &item : displayGroupInfo_.displaysInfo) {
3721         int32_t displayMaxX = 0;
3722         int32_t displayMaxY = 0;
3723         if (!AddInt32(item.x, item.width, displayMaxX)) {
3724             MMI_HILOGE("The addition of displayMaxX overflows");
3725             return;
3726         }
3727         if (!AddInt32(item.y, item.height, displayMaxY)) {
3728             MMI_HILOGE("The addition of displayMaxY overflows");
3729             return;
3730         }
3731         if ((logicalX >= item.x && logicalX < displayMaxX) &&
3732             (logicalY >= item.y && logicalY < displayMaxY)) {
3733             physicalX = logicalX - item.x;
3734             physicalY = logicalY - item.y;
3735             displayId = item.id;
3736             break;
3737         }
3738     }
3739 }
3740 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3741 
3742 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3743 void InputWindowsManager::CoordinateCorrection(int32_t width, int32_t height, int32_t &integerX, int32_t &integerY)
3744 {
3745     if (integerX < 0) {
3746         integerX = 0;
3747     }
3748     if (integerX >= width) {
3749         integerX = width - 1;
3750     }
3751     if (integerY < 0) {
3752         integerY = 0;
3753     }
3754     if (integerY >= height) {
3755         integerY = height - 1;
3756     }
3757 }
3758 
3759 void InputWindowsManager::GetWidthAndHeight(const DisplayInfo* displayInfo, int32_t &width, int32_t &height,
3760     bool isRealData)
3761 {
3762     if (TOUCH_DRAWING_MGR->IsWindowRotation()) {
3763         if (displayInfo->direction == DIRECTION0 || displayInfo->direction == DIRECTION180) {
3764             width = displayInfo->width;
3765             height = displayInfo->height;
3766         } else {
3767             if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled() || !isRealData) {
3768                 width = displayInfo->width;
3769                 height = displayInfo->height;
3770                 return;
3771             }
3772             height = displayInfo->width;
3773             width = displayInfo->height;
3774         }
3775     } else {
3776         width = displayInfo->width;
3777         height = displayInfo->height;
3778     }
3779 }
3780 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3781 
3782 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3783 void InputWindowsManager::ReverseRotateScreen(const DisplayInfo& info, const double x, const double y,
3784     Coordinate2D& cursorPos) const
3785 {
3786     const Direction direction = info.direction;
3787     MMI_HILOGD("X:%{private}.2f, Y:%{private}.2f, info.width:%{private}d, info.height:%{private}d",
3788         x, y, info.width, info.height);
3789     switch (direction) {
3790         case DIRECTION0: {
3791             MMI_HILOGD("direction is DIRECTION0");
3792             cursorPos.x = x;
3793             cursorPos.y = y;
3794             MMI_HILOGD("physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
3795             break;
3796         }
3797         case DIRECTION90: {
3798             MMI_HILOGD("direction is DIRECTION90");
3799             cursorPos.y = static_cast<double>(info.width) - x;
3800             cursorPos.x = y;
3801             MMI_HILOGD("physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
3802             break;
3803         }
3804         case DIRECTION180: {
3805             MMI_HILOGD("direction is DIRECTION180");
3806             cursorPos.x = static_cast<double>(info.width) - x;
3807             cursorPos.y = static_cast<double>(info.height) - y;
3808             MMI_HILOGD("physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
3809             break;
3810         }
3811         case DIRECTION270: {
3812             MMI_HILOGD("direction is DIRECTION270");
3813             cursorPos.x = static_cast<double>(info.height) - y;
3814             cursorPos.y = x;
3815             MMI_HILOGD("physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
3816             break;
3817         }
3818         default: {
3819             MMI_HILOGE("direction is invalid, direction:%{private}d", direction);
3820             break;
3821         }
3822     }
3823     if (EventLogHelper::IsBetaVersion()) {
3824         MMI_HILOGD("physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
3825     }
3826 }
3827 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3828 
3829 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3830 void InputWindowsManager::UpdateAndAdjustMouseLocation(int32_t& displayId, double& x, double& y, bool isRealData)
3831 {
3832     auto displayInfo = GetPhysicalDisplay(displayId);
3833     CHKPV(displayInfo);
3834     int32_t integerX = static_cast<int32_t>(x);
3835     int32_t integerY = static_cast<int32_t>(y);
3836     int32_t lastDisplayId = displayId;
3837     if (!IsInsideDisplay(*displayInfo, integerX, integerY)) {
3838         FindPhysicalDisplay(*displayInfo, integerX, integerY, displayId);
3839     }
3840     if (displayId != lastDisplayId) {
3841         displayInfo = GetPhysicalDisplay(displayId);
3842         CHKPV(displayInfo);
3843     }
3844     int32_t width = 0;
3845     int32_t height = 0;
3846     GetWidthAndHeight(displayInfo, width, height, isRealData);
3847     CoordinateCorrection(width, height, integerX, integerY);
3848     x = static_cast<double>(integerX) + (x - floor(x));
3849     y = static_cast<double>(integerY) + (y - floor(y));
3850 
3851     if (TOUCH_DRAWING_MGR->IsWindowRotation() && isRealData) {
3852         PhysicalCoordinate coord {
3853             .x = integerX,
3854             .y = integerY,
3855         };
3856         RotateScreen(*displayInfo, coord);
3857         mouseLocation_.physicalX = static_cast<int32_t>(coord.x);
3858         mouseLocation_.physicalY = static_cast<int32_t>(coord.y);
3859     } else {
3860         mouseLocation_.physicalX = integerX;
3861         mouseLocation_.physicalY = integerY;
3862     }
3863     mouseLocation_.displayId = displayId;
3864     MMI_HILOGD("Mouse Data: physicalX:%{private}d,physicalY:%{private}d, displayId:%{public}d",
3865         mouseLocation_.physicalX, mouseLocation_.physicalY, displayId);
3866     cursorPos_.displayId = displayId;
3867     if (TOUCH_DRAWING_MGR->IsWindowRotation() && !isRealData) {
3868         ReverseRotateScreen(*displayInfo, x, y, cursorPos_.cursorPos);
3869         return;
3870     }
3871     cursorPos_.cursorPos.x = x;
3872     cursorPos_.cursorPos.y = y;
3873 }
3874 
3875 MouseLocation InputWindowsManager::GetMouseInfo()
3876 {
3877     if ((mouseLocation_.displayId < 0) && !displayGroupInfo_.displaysInfo.empty()) {
3878         const DisplayInfo &displayInfo = displayGroupInfo_.displaysInfo[0];
3879         mouseLocation_.displayId = displayInfo.id;
3880         mouseLocation_.physicalX = displayInfo.width / TWOFOLD;
3881         mouseLocation_.physicalY = displayInfo.height / TWOFOLD;
3882     }
3883     return mouseLocation_;
3884 }
3885 
3886 CursorPosition InputWindowsManager::GetCursorPos()
3887 {
3888     if ((cursorPos_.displayId < 0) && !displayGroupInfo_.displaysInfo.empty()) {
3889         const DisplayInfo &displayInfo = displayGroupInfo_.displaysInfo[0];
3890         cursorPos_.displayId = displayInfo.id;
3891         cursorPos_.cursorPos.x = displayInfo.width * HALF_RATIO;
3892         cursorPos_.cursorPos.y = displayInfo.height * HALF_RATIO;
3893     }
3894     return cursorPos_;
3895 }
3896 
3897 CursorPosition InputWindowsManager::ResetCursorPos()
3898 {
3899     if (!displayGroupInfo_.displaysInfo.empty()) {
3900         const DisplayInfo &displayInfo = displayGroupInfo_.displaysInfo[0];
3901         cursorPos_.displayId = displayInfo.id;
3902         cursorPos_.cursorPos.x = displayInfo.width * HALF_RATIO;
3903         cursorPos_.cursorPos.y = displayInfo.height * HALF_RATIO;
3904     } else {
3905         cursorPos_.displayId = -1;
3906         cursorPos_.cursorPos.x = 0;
3907         cursorPos_.cursorPos.y = 0;
3908     }
3909     return cursorPos_;
3910 }
3911 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3912 
3913 int32_t InputWindowsManager::AppendExtraData(const ExtraData& extraData)
3914 {
3915     CALL_DEBUG_ENTER;
3916     extraData_.appended = extraData.appended;
3917     extraData_.buffer = extraData.buffer;
3918     extraData_.sourceType = extraData.sourceType;
3919     extraData_.pointerId = extraData.pointerId;
3920     extraData_.pullId = extraData.pullId;
3921     return RET_OK;
3922 }
3923 
3924 void InputWindowsManager::ClearExtraData()
3925 {
3926     CALL_DEBUG_ENTER;
3927     extraData_.appended = false;
3928     extraData_.buffer.clear();
3929     extraData_.sourceType = -1;
3930     extraData_.pointerId = -1;
3931     extraData_.pullId = -1;
3932 }
3933 
3934 ExtraData InputWindowsManager::GetExtraData() const
3935 {
3936     CALL_DEBUG_ENTER;
3937     return extraData_;
3938 }
3939 
3940 bool InputWindowsManager::IsWindowVisible(int32_t pid)
3941 {
3942     CALL_DEBUG_ENTER;
3943     if (pid < 0) {
3944         MMI_HILOGE("pid is invalid");
3945         return true;
3946     }
3947     std::vector<sptr<Rosen::WindowVisibilityInfo>> infos;
3948     Rosen::WindowManagerLite::GetInstance().GetVisibilityWindowInfo(infos);
3949     for (const auto &it: infos) {
3950         if (pid == it->pid_ &&
3951             it->visibilityState_ < Rosen::WindowVisibilityState::WINDOW_VISIBILITY_STATE_TOTALLY_OCCUSION) {
3952             MMI_HILOGD("pid:%{public}d has visible window", pid);
3953             return true;
3954         }
3955     }
3956     MMI_HILOGD("pid:%{public}d doesn't have visible window", pid);
3957     return false;
3958 }
3959 
3960 void InputWindowsManager::UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)
3961 {
3962     CALL_DEBUG_ENTER;
3963     int32_t action = pointerEvent->GetPointerAction();
3964     switch (action) {
3965         case PointerEvent::POINTER_ACTION_MOVE: {
3966             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_MOVE);
3967             break;
3968         }
3969         case PointerEvent::POINTER_ACTION_BUTTON_UP:
3970         case PointerEvent::POINTER_ACTION_UP: {
3971             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_UP);
3972             break;
3973         }
3974         case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
3975             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
3976             break;
3977         }
3978         case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
3979             pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
3980             break;
3981         }
3982         default: {
3983             MMI_HILOG_DISPATCHI("Action is:%{public}d, no need change", action);
3984             break;
3985         }
3986     }
3987     MMI_HILOGD("pointerAction:%{public}s", pointerEvent->DumpPointerAction());
3988 }
3989 
3990 void InputWindowsManager::Dump(int32_t fd, const std::vector<std::string> &args)
3991 {
3992     CALL_DEBUG_ENTER;
3993     mprintf(fd, "Windows information:\t");
3994     mprintf(fd, "windowsInfos,num:%zu", displayGroupInfo_.windowsInfo.size());
3995     for (const auto &item : displayGroupInfo_.windowsInfo) {
3996         mprintf(fd, "  windowsInfos: id:%d | pid:%d | uid:%d | area.x:%d | area.y:%d "
3997                 "| area.width:%d | area.height:%d | defaultHotAreas.size:%zu "
3998                 "| pointerHotAreas.size:%zu | agentWindowId:%d | flags:%u "
3999                 "| action:%d | displayId:%d | zOrder:%f \t",
4000                 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
4001                 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
4002                 item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder);
4003         for (const auto &win : item.defaultHotAreas) {
4004             mprintf(fd, "\t defaultHotAreas: x:%d | y:%d | width:%d | height:%d \t",
4005                     win.x, win.y, win.width, win.height);
4006         }
4007         for (const auto &pointer : item.pointerHotAreas) {
4008             mprintf(fd, "\t pointerHotAreas: x:%d | y:%d | width:%d | height:%d \t",
4009                     pointer.x, pointer.y, pointer.width, pointer.height);
4010         }
4011 
4012         std::string dump;
4013         dump += StringPrintf("\t pointerChangeAreas: ");
4014         for (const auto &it : item.pointerChangeAreas) {
4015             dump += StringPrintf("%d | ", it);
4016         }
4017         dump += StringPrintf("\n\t transform: ");
4018         for (const auto &it : item.transform) {
4019             dump += StringPrintf("%f | ", it);
4020         }
4021         std::istringstream stream(dump);
4022         std::string line;
4023         while (std::getline(stream, line, '\n')) {
4024             mprintf(fd, "%s", line.c_str());
4025         }
4026     }
4027     mprintf(fd, "Displays information:\t");
4028     mprintf(fd, "displayInfos,num:%zu", displayGroupInfo_.displaysInfo.size());
4029     for (const auto &item : displayGroupInfo_.displaysInfo) {
4030         mprintf(fd, "\t displayInfos: id:%d | x:%d | y:%d | width:%d | height:%d | name:%s "
4031                 "| uniq:%s | direction:%d | displayDirection:%d | displayMode:%u \t",
4032                 item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
4033                 item.uniq.c_str(), item.direction, item.displayDirection, item.displayMode);
4034     }
4035     mprintf(fd, "Input device and display bind info:\n%s", bindInfo_.Dumps().c_str());
4036 #ifdef OHOS_BUILD_ENABLE_ANCO
4037     std::string ancoWindows;
4038     DumpAncoWindows(ancoWindows);
4039     mprintf(fd, "%s\n", ancoWindows.c_str());
4040 #endif // OHOS_BUILD_ENABLE_ANCO
4041 }
4042 
4043 std::pair<double, double> InputWindowsManager::TransformWindowXY(const WindowInfo &window,
4044     double logicX, double logicY) const
4045 {
4046     Matrix3f transform(window.transform);
4047     if (window.transform.size() != MATRIX3_SIZE || transform.IsIdentity()) {
4048         return {logicX, logicY};
4049     }
4050     Vector3f logicXY(logicX, logicY, 1.0);
4051     Vector3f windowXY = transform * logicXY;
4052     return {round(windowXY[0]), round(windowXY[1])};
4053 }
4054 
4055 std::pair<double, double> InputWindowsManager::TransformDisplayXY(const DisplayInfo &info,
4056     double logicX, double logicY) const
4057 {
4058     Matrix3f transform(info.transform);
4059     if (info.transform.size() != MATRIX3_SIZE || transform.IsIdentity()) {
4060         return {logicX, logicY};
4061     }
4062     Vector3f logicXY(logicX, logicY, 1.0);
4063     Vector3f displayXY = transform * logicXY;
4064     return {round(displayXY[0]), round(displayXY[1])};
4065 }
4066 
4067 bool InputWindowsManager::IsValidZorderWindow(const WindowInfo &window,
4068     const std::shared_ptr<PointerEvent>& pointerEvent)
4069 {
4070     CHKPR(pointerEvent, false);
4071     if (!(pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) || MMI_LE(pointerEvent->GetZOrder(), 0.0f)) {
4072         return true;
4073     }
4074     if (MMI_GE(window.zOrder, pointerEvent->GetZOrder())) {
4075         MMI_HILOGE("current window zorder:%{public}f greater than the simulate target zOrder:%{public}f, "
4076             "ignore this window::%{public}d", window.zOrder, pointerEvent->GetZOrder(), window.id);
4077         return false;
4078     }
4079     return true;
4080 }
4081 
4082 bool InputWindowsManager::HandleWindowInputType(const WindowInfo &window, std::shared_ptr<PointerEvent> pointerEvent)
4083 {
4084     CALL_DEBUG_ENTER;
4085     int32_t pointerId = pointerEvent->GetPointerId();
4086     PointerEvent::PointerItem item;
4087     if (!pointerEvent->GetPointerItem(pointerId, item)) {
4088         MMI_HILOG_WINDOWE("Invalid pointer:%{public}d", pointerId);
4089         return false;
4090     }
4091     int32_t toolType = item.GetToolType();
4092     int32_t sourceType = pointerEvent->GetSourceType();
4093     switch (window.windowInputType)
4094     {
4095         case WindowInputType::NORMAL:
4096             return false;
4097         case WindowInputType::TRANSMIT_ALL:
4098             return true;
4099         case WindowInputType::TRANSMIT_EXCEPT_MOVE: {
4100             auto pointerAction = pointerEvent->GetPointerAction();
4101             return (pointerAction == PointerEvent::POINTER_ACTION_MOVE ||
4102                 pointerAction == PointerEvent::POINTER_ACTION_PULL_MOVE);
4103         }
4104         case WindowInputType::ANTI_MISTAKE_TOUCH:
4105             return false;
4106         case WindowInputType::TRANSMIT_AXIS_MOVE:
4107             return false;
4108         case WindowInputType::TRANSMIT_MOUSE_MOVE:
4109             return false;
4110         case WindowInputType::TRANSMIT_LEFT_RIGHT:
4111             return false;
4112         case WindowInputType::TRANSMIT_BUTTOM:
4113             return false;
4114         case WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE:
4115             return false;
4116         case WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE:
4117             return false;
4118         default:
4119             return false;
4120     }
4121 }
4122 
4123 std::optional<WindowInfo> InputWindowsManager::GetWindowAndDisplayInfo(int32_t windowId, int32_t displayId)
4124 {
4125     CALL_DEBUG_ENTER;
4126     std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(displayId);
4127     for (const auto &item : windowInfos) {
4128         if (windowId == item.id) {
4129             return std::make_optional(item);
4130         }
4131         for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
4132             if (windowId == uiExtentionWindow.id) {
4133                 return std::make_optional(uiExtentionWindow);
4134             }
4135         }
4136     }
4137     return std::nullopt;
4138 }
4139 
4140 void InputWindowsManager::GetTargetWindowIds(int32_t pointerItemId, int32_t sourceType,
4141     std::vector<int32_t> &windowIds)
4142 {
4143     CALL_DEBUG_ENTER;
4144     if (sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
4145         if (targetMouseWinIds_.find(pointerItemId) != targetMouseWinIds_.end()) {
4146             windowIds = targetMouseWinIds_[pointerItemId];
4147         }
4148         return;
4149     } else if (sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
4150         if (targetTouchWinIds_.find(pointerItemId) != targetTouchWinIds_.end()) {
4151             windowIds = targetTouchWinIds_[pointerItemId];
4152         }
4153     }
4154 }
4155 
4156 void InputWindowsManager::AddTargetWindowIds(int32_t pointerItemId, int32_t sourceType, int32_t windowId)
4157 {
4158     CALL_DEBUG_ENTER;
4159     if (sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
4160         if (targetMouseWinIds_.find(pointerItemId) != targetMouseWinIds_.end()) {
4161             targetMouseWinIds_[pointerItemId].push_back(windowId);
4162         } else {
4163             std::vector<int32_t> windowIds;
4164             windowIds.push_back(windowId);
4165             targetMouseWinIds_.emplace(pointerItemId, windowIds);
4166         }
4167         return;
4168     } else if (sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
4169         if (targetTouchWinIds_.find(pointerItemId) != targetTouchWinIds_.end()) {
4170             targetTouchWinIds_[pointerItemId].push_back(windowId);
4171         } else {
4172             std::vector<int32_t> windowIds;
4173             windowIds.push_back(windowId);
4174             targetTouchWinIds_.emplace(pointerItemId, windowIds);
4175         }
4176     }
4177 }
4178 
4179 void InputWindowsManager::ClearTargetWindowId(int32_t pointerId)
4180 {
4181     CALL_DEBUG_ENTER;
4182     if (targetTouchWinIds_.find(pointerId) == targetTouchWinIds_.end()) {
4183         MMI_HILOGD("Clear target windowId fail, pointerId:%{public}d", pointerId);
4184         return;
4185     }
4186     targetTouchWinIds_.erase(pointerId);
4187 }
4188 
4189 void InputWindowsManager::SetPrivacyModeFlag(SecureFlag privacyMode, std::shared_ptr<InputEvent> event)
4190 {
4191     if (privacyMode == SecureFlag::PRIVACY_MODE) {
4192         MMI_HILOGD("Window security mode is privacy");
4193         event->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
4194     }
4195 }
4196 
4197 int32_t InputWindowsManager::CheckWindowIdPermissionByPid(int32_t windowId, int32_t pid)
4198 {
4199     CALL_DEBUG_ENTER;
4200     int32_t checkingPid = GetWindowPid(windowId);
4201     if (checkingPid != pid) {
4202         MMI_HILOGE("check windowId failed, windowId is %{public}d, pid is %{public}d", windowId, pid);
4203         return RET_ERR;
4204     }
4205     return RET_OK;
4206 }
4207 
4208 #ifdef OHOS_BUILD_ENABLE_TOUCH
4209 void InputWindowsManager::ReverseXY(int32_t &x, int32_t &y)
4210 {
4211     if (displayGroupInfo_.displaysInfo.empty()) {
4212         MMI_HILOGE("displayGroupInfo_.displaysInfo is empty");
4213         return;
4214     }
4215     const Direction direction = displayGroupInfo_.displaysInfo.front().direction;
4216     if (direction < Direction::DIRECTION0 || direction > Direction::DIRECTION270) {
4217         MMI_HILOGE("direction is invalid, direction:%{public}d", direction);
4218         return;
4219     }
4220     Coordinate2D matrix { 0.0, 0.0 };
4221     ReverseRotateScreen(displayGroupInfo_.displaysInfo.front(), x, y, matrix);
4222     x = static_cast<int32_t>(matrix.x);
4223     y = static_cast<int32_t>(matrix.y);
4224 }
4225 
4226 void InputWindowsManager::SendCancelEventWhenLock()
4227 {
4228     CALL_INFO_TRACE;
4229     CHKPV(lastTouchEventOnBackGesture_);
4230     if (lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
4231         lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN) {
4232         return;
4233     }
4234     lastTouchEventOnBackGesture_->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
4235     lastTouchEventOnBackGesture_->SetActionTime(GetSysClockTime());
4236     lastTouchEventOnBackGesture_->UpdateId();
4237     lastTouchEventOnBackGesture_->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT | InputEvent::EVENT_FLAG_NO_MONITOR);
4238     auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
4239     CHKPV(inputEventNormalizeHandler);
4240     inputEventNormalizeHandler->HandleTouchEvent(lastTouchEventOnBackGesture_);
4241     if (lastTouchEventOnBackGesture_->HasFlag(InputEvent::EVENT_FLAG_SHELL)) {
4242         auto iter = shellTouchItemDownInfos_.find(lastTouchEventOnBackGesture_->GetPointerId());
4243         if (iter != shellTouchItemDownInfos_.end()) {
4244             iter->second.flag = false;
4245             MMI_HILOG_DISPATCHI("Screen locked, send cancel event from shell");
4246         }
4247     } else if (lastTouchEventOnBackGesture_->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
4248         auto iter = accessTouchItemDownInfos_.find(lastTouchEventOnBackGesture_->GetPointerId());
4249         if (iter != accessTouchItemDownInfos_.end()) {
4250             iter->second.flag = false;
4251             MMI_HILOG_DISPATCHI("Screen locked, send cancel event from shell");
4252         }
4253     } else {
4254         auto iter = touchItemDownInfos_.find(lastTouchEventOnBackGesture_->GetPointerId());
4255         if (iter != touchItemDownInfos_.end()) {
4256             iter->second.flag = false;
4257             MMI_HILOG_DISPATCHI("Screen locked, send cancel event from native");
4258         }
4259     }
4260 }
4261 #endif // OHOS_BUILD_ENABLE_TOUCH
4262 
4263 bool InputWindowsManager::IsTransparentWin(
4264     std::unique_ptr<Media::PixelMap> &pixelMap, int32_t logicalX, int32_t logicalY)
4265     __attribute__((no_sanitize("cfi")))
4266 {
4267     CALL_DEBUG_ENTER;
4268     if (pixelMap == nullptr) {
4269         return false;
4270     }
4271 
4272     uint32_t dst = 0;
4273     OHOS::Media::Position pos { logicalY, logicalX };
4274     uint32_t result = pixelMap->ReadPixel(pos, dst);
4275     if (result != RET_OK) {
4276         MMI_HILOGE("Failed to read pixelmap");
4277         return false;
4278     }
4279     return dst == RET_OK;
4280 }
4281 
4282 int32_t InputWindowsManager::SetCurrentUser(int32_t userId)
4283 {
4284     CALL_DEBUG_ENTER;
4285     currentUserId_ = userId;
4286     return RET_OK;
4287 }
4288 
4289 void InputWindowsManager::PrintChangedWindowByEvent(int32_t eventType, const WindowInfo &newWindowInfo)
4290 {
4291     auto iter = lastMatchedWindow_.find(eventType);
4292     if (iter != lastMatchedWindow_.end() && iter->second.id != newWindowInfo.id) {
4293         MMI_HILOGI("Target window changed %{public}d %{public}d %{public}d %{public}f "
4294             "%{public}d %{public}d %{public}f", eventType, iter->second.id, iter->second.pid,
4295             iter->second.zOrder, newWindowInfo.id, newWindowInfo.pid, newWindowInfo.zOrder);
4296     }
4297     lastMatchedWindow_[eventType] = newWindowInfo;
4298 }
4299 
4300 void InputWindowsManager::PrintChangedWindowBySync(const DisplayGroupInfo &newDisplayInfo)
4301 {
4302     auto &oldWindows = displayGroupInfo_.windowsInfo;
4303     auto &newWindows = newDisplayInfo.windowsInfo;
4304     if (!oldWindows.empty() && !newWindows.empty()) {
4305         if (oldWindows[0].id != newWindows[0].id) {
4306             MMI_HILOGI("Window sync changed %{public}d %{public}d %{public}f %{public}d %{public}d %{public}f",
4307                 oldWindows[0].id, oldWindows[0].pid, oldWindows[0].zOrder, newWindows[0].id,
4308                 newWindows[0].pid, newWindows[0].zOrder);
4309         }
4310     }
4311 }
4312 
4313 bool InputWindowsManager::ParseConfig()
4314 {
4315     std::string defaultConfig = "/system/etc/multimodalinput/white_list_config.json";
4316     return ParseJson(defaultConfig);
4317 }
4318 
4319 bool InputWindowsManager::ParseJson(const std::string &configFile)
4320 {
4321     CALL_DEBUG_ENTER;
4322     std::string jsonStr = ReadJsonFile(configFile);
4323     if (jsonStr.empty()) {
4324         MMI_HILOGE("Read configFile failed");
4325         return false;
4326     }
4327     JsonParser jsonData;
4328     jsonData.json_ = cJSON_Parse(jsonStr.c_str());
4329     if (!cJSON_IsObject(jsonData.json_)) {
4330         MMI_HILOGE("jsonData.json_ is not object");
4331         return false;
4332     }
4333     cJSON* whiteList = cJSON_GetObjectItemCaseSensitive(jsonData.json_, "whiteList");
4334     if (!cJSON_IsArray(whiteList)) {
4335         MMI_HILOGE("whiteList number must be array");
4336         return false;
4337     }
4338     int32_t whiteListSize = cJSON_GetArraySize(whiteList);
4339     for (int32_t i = 0; i < whiteListSize; ++i) {
4340         cJSON *whiteListJson = cJSON_GetArrayItem(whiteList, i);
4341         if (!cJSON_IsObject(whiteListJson)) {
4342             MMI_HILOGE("whiteListJson is not object");
4343             continue;
4344         }
4345         SwitchFocusKey switchFocusKey;
4346         cJSON *keyCodeJson = cJSON_GetObjectItemCaseSensitive(whiteListJson, "keyCode");
4347         if (!cJSON_IsNumber(keyCodeJson)) {
4348             MMI_HILOGE("keyCodeJson is not number");
4349             continue;
4350         }
4351         switchFocusKey.keyCode = keyCodeJson->valueint;
4352         cJSON *pressedKeyJson = cJSON_GetObjectItemCaseSensitive(whiteListJson, "pressedKey");
4353         if (!cJSON_IsNumber(pressedKeyJson)) {
4354             MMI_HILOGE("pressedKeyJson is not number");
4355             continue;
4356         }
4357         switchFocusKey.pressedKey = pressedKeyJson->valueint;
4358         vecWhiteList_.push_back(switchFocusKey);
4359     }
4360     return true;
4361 }
4362 
4363 void InputWindowsManager::SetWindowStateNotifyPid(int32_t pid)
4364 {
4365     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
4366         windowStateNotifyPid_ = pid;
4367     }
4368 }
4369 
4370 int32_t InputWindowsManager::GetWindowStateNotifyPid()
4371 {
4372     return windowStateNotifyPid_;
4373 }
4374 
4375 int32_t InputWindowsManager::GetPidByWindowId(int32_t id)
4376 {
4377     for (auto &item : displayGroupInfo_.windowsInfo) {
4378         if (item.id== id) {
4379             return item.pid;
4380         }
4381         for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
4382             if (uiExtentionWindow.id == id) {
4383                 return uiExtentionWindow.pid;
4384             }
4385         }
4386     }
4387     return RET_ERR;
4388 }
4389 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
4390 bool InputWindowsManager::IsKeyPressed(int32_t pressedKey, std::vector<KeyEvent::KeyItem> &keyItems)
4391 {
4392     CALL_DEBUG_ENTER;
4393     for (const auto &item : keyItems) {
4394         if (item.GetKeyCode() == pressedKey && item.IsPressed()) {
4395             return true;
4396         }
4397     }
4398     return false;
4399 }
4400 
4401 bool InputWindowsManager::IsOnTheWhitelist(std::shared_ptr<KeyEvent> keyEvent)
4402 {
4403     CALL_DEBUG_ENTER;
4404     CHKPR(keyEvent, false);
4405     for (const auto &item : vecWhiteList_) {
4406         if (item.keyCode == keyEvent->GetKeyCode()) {
4407             auto keyItems = keyEvent->GetKeyItems();
4408             if (item.pressedKey == -1 && keyItems.size() == 1) {
4409                 return true;
4410             }
4411             bool flag = ((item.pressedKey != -1) && (keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_DOWN) &&
4412                 (keyItems.size() == 2) && IsKeyPressed(item.pressedKey, keyItems));
4413             if (flag) {
4414                 return true;
4415             }
4416         }
4417     }
4418     return false;
4419 }
4420 #endif // OHOS_BUILD_ENABLE_KEYBOARD
4421 
4422 int32_t InputWindowsManager::SetPixelMapData(int32_t infoId, void *pixelMap)
4423     __attribute__((no_sanitize("cfi")))
4424 {
4425     CALL_DEBUG_ENTER;
4426     if (infoId < 0 || pixelMap == nullptr) {
4427         MMI_HILOGE("The infoId is invalid or pixelMap is nullptr");
4428         return ERR_INVALID_VALUE;
4429     }
4430     auto pixelMapSource = static_cast<OHOS::Media::PixelMap*>(pixelMap);
4431     Media::InitializationOptions opts;
4432     auto pixelMapPtr = OHOS::Media::PixelMap::Create(*pixelMapSource, opts);
4433     CHKPR(pixelMapPtr, RET_ERR);
4434     MMI_HILOGD("byteCount:%{public}d, width:%{public}d, height:%{public}d",
4435         pixelMapPtr->GetByteCount(), pixelMapPtr->GetWidth(), pixelMapPtr->GetHeight());
4436     transparentWins_.insert_or_assign(infoId, std::move(pixelMapPtr));
4437     return RET_OK;
4438 }
4439 
4440 void InputWindowsManager::CleanInvalidPiexMap() {
4441     for (auto it = transparentWins_.begin(); it != transparentWins_.end();) {
4442         int32_t windowId = it->first;
4443         auto iter = std::find_if(displayGroupInfo_.windowsInfo.begin(), displayGroupInfo_.windowsInfo.end(),
4444             [windowId](const auto &window) {
4445                     return window.id == windowId;
4446                 });
4447         if (iter == displayGroupInfo_.windowsInfo.end()) {
4448             it = transparentWins_.erase(it);
4449         } else {
4450             ++it;
4451         }
4452     }
4453 }
4454 
4455 #ifdef OHOS_BUILD_ENABLE_ANCO
4456 bool InputWindowsManager::IsKnuckleOnAncoWindow(std::shared_ptr<PointerEvent> pointerEvent)
4457 {
4458     CALL_DEBUG_ENTER;
4459     CHKPR(pointerEvent, false);
4460     PointerEvent::PointerItem pointerItem {};
4461     int32_t pointerId = pointerEvent->GetPointerId();
4462     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
4463         MMI_HILOGE("Get pointer item failed, pointer:%{public}d", pointerId);
4464         return false;
4465     }
4466 
4467     if (pointerItem.GetToolType() != PointerEvent::TOOL_TYPE_KNUCKLE) {
4468         return false;
4469     }
4470 
4471     const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
4472     WindowInfo *windowInfo = nullptr;
4473     std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
4474     auto iter = find_if(windowInfos.begin(), windowInfos.end(),
4475         [&](const auto &item) { return item.id == focusWindowId; });
4476     if (iter != windowInfos.end()) {
4477         windowInfo = &(*iter);
4478     }
4479 
4480     if (windowInfo == nullptr) {
4481         MMI_HILOGE("windowInfo is nullptr");
4482         return false;
4483     }
4484 
4485     return IsAncoWindowFocus(*windowInfo);
4486 }
4487 #endif // OHOS_BUILD_ENABLE_ANCO
4488 
4489 int32_t InputWindowsManager::GetCurrentUserId()
4490 {
4491     return currentUserId_;
4492 }
4493 
4494 void InputWindowsManager::SetFoldState()
4495 {
4496     IsFoldable_ = Rosen::DisplayManager::GetInstance().IsFoldable();
4497 }
4498 } // namespace MMI
4499 } // namespace OHOS
4500