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