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 #include <linux/input.h>
18
19 #include "display_manager.h"
20 #include "event_log_helper.h"
21 #include "i_pointer_drawing_manager.h"
22 #include "key_command_handler_util.h"
23 #include "mmi_matrix3.h"
24 #include "scene_board_judgement.h"
25 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
26 #include "touch_drawing_manager.h"
27 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
28 #ifdef OHOS_BUILD_ENABLE_ANCO
29 #endif // OHOS_BUILD_ENABLE_ANCO
30 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
31 #include "magic_pointer_velocity_tracker.h"
32 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
33 #include "hitrace_meter.h"
34
35 #undef MMI_LOG_DOMAIN
36 #define MMI_LOG_DOMAIN MMI_LOG_WINDOW
37 #undef MMI_LOG_TAG
38 #define MMI_LOG_TAG "InputWindowsManager"
39
40 namespace OHOS {
41 namespace MMI {
42 namespace {
43 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
44 constexpr int32_t DEFAULT_POINTER_STYLE { 0 };
45 constexpr int32_t CURSOR_CIRCLE_STYLE { 41 };
46 constexpr int32_t AECH_DEVELOPER_DEFINED_STYLE { 47 };
47 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
48 constexpr int32_t OUTWINDOW_HOT_AREA { 20 };
49 constexpr int32_t SCALE_X { 0 };
50 constexpr int32_t SCALE_Y { 4 };
51 constexpr int32_t ANCHOR_POINT_X { 6 };
52 constexpr int32_t ANCHOR_POINT_Y { 7 };
53 constexpr int32_t TOP_LEFT_AREA { 0 };
54 constexpr int32_t TOP_AREA { 1 };
55 constexpr int32_t TOP_RIGHT_AREA { 2 };
56 constexpr int32_t RIGHT_AREA { 3 };
57 constexpr int32_t BOTTOM_RIGHT_AREA { 4 };
58 constexpr int32_t BOTTOM_AREA { 5 };
59 constexpr int32_t BOTTOM_LEFT_AREA { 6 };
60 constexpr int32_t LEFT_AREA { 7 };
61 [[ maybe_unused ]] constexpr int32_t WAIT_TIME_FOR_REGISTER { 2000 };
62 constexpr int32_t RS_PROCESS_TIMEOUT { 500 * 1000 };
63 constexpr int32_t HICAR_MIN_DISPLAY_ID { 1000 };
64 #ifdef OHOS_BUILD_ENABLE_ANCO
65 constexpr int32_t SHELL_WINDOW_COUNT { 1 };
66 #endif // OHOS_BUILD_ENABLE_ANCO
67 constexpr double HALF_RATIO { 0.5 };
68 constexpr int32_t TWOFOLD { 2 };
69 constexpr int32_t COMMON_PARAMETER_ERROR { 401 };
70 const std::string BIND_CFG_FILE_NAME { "/data/service/el1/public/multimodalinput/display_bind.cfg" };
71 const std::string MOUSE_FILE_NAME { "mouse_settings.xml" };
72 const std::string DEFAULT_ICON_PATH { "/system/etc/multimodalinput/mouse_icon/Default.svg" };
73 const std::string NAVIGATION_SWITCH_NAME { "settings.input.stylus_navigation_hint" };
74 constexpr uint32_t FOLD_STATUS_MASK { 1U << 27U };
75 constexpr int32_t REPEAT_COOLING_TIME { 100 };
76 constexpr int32_t REPEAT_ONCE { 1 };
77 constexpr int32_t DEFAULT_VALUE { -1 };
78 constexpr int32_t ANGLE_90 { 90 };
79 constexpr int32_t ANGLE_360 { 360 };
80 constexpr int32_t POINTER_MOVEFLAG = { 7 };
81 constexpr size_t POINTER_STYLE_WINDOW_NUM = { 10 };
82 constexpr int32_t DEFAULT_DPI { 0 };
83 } // namespace
84
85 enum PointerHotArea : int32_t {
86 TOP = 0,
87 BOTTOM = 1,
88 LEFT = 2,
89 RIGHT = 3,
90 TOP_LEFT = 4,
91 TOP_RIGHT = 5,
92 BOTTOM_LEFT = 6,
93 BOTTOM_RIGHT = 7,
94 };
95
96 enum SourceTool : int32_t {
97 UNKNOWN = 0,
98 FINGER = 1,
99 PEN = 2,
100 RUBBER = 3,
101 BRUSH = 4,
102 PENCIL = 5,
103 AIRBRUSH = 6,
104 MOUSE = 7,
105 LENS = 8,
106 TOUCHPAD = 9,
107 JOYSTICK = 10,
108 };
109
110 std::unordered_map<int32_t, int32_t> InputWindowsManager::convertToolTypeMap_ = {
111 {PointerEvent::TOOL_TYPE_FINGER, SourceTool::FINGER},
112 {PointerEvent::TOOL_TYPE_PEN, SourceTool::PEN},
113 {PointerEvent::TOOL_TYPE_RUBBER, SourceTool::RUBBER},
114 {PointerEvent::TOOL_TYPE_BRUSH, SourceTool::BRUSH},
115 {PointerEvent::TOOL_TYPE_PENCIL, SourceTool::PENCIL},
116 {PointerEvent::TOOL_TYPE_AIRBRUSH, SourceTool::AIRBRUSH},
117 {PointerEvent::TOOL_TYPE_MOUSE, SourceTool::MOUSE},
118 {PointerEvent::TOOL_TYPE_LENS, SourceTool::LENS},
119 {PointerEvent::TOOL_TYPE_TOUCHPAD, SourceTool::TOUCHPAD},
120 };
121
122 std::shared_ptr<IInputWindowsManager> IInputWindowsManager::instance_;
123 std::mutex IInputWindowsManager::mutex_;
124
GetInstance()125 std::shared_ptr<IInputWindowsManager> IInputWindowsManager::GetInstance()
126 {
127 if (instance_ == nullptr) {
128 std::lock_guard<std::mutex> lock(mutex_);
129 if (instance_ == nullptr) {
130 instance_ = std::make_shared<InputWindowsManager>();
131 }
132 }
133 return instance_;
134 }
135
DestroyInstance()136 void IInputWindowsManager::DestroyInstance()
137 {
138 std::lock_guard<std::mutex> lock(mutex_);
139 instance_.reset();
140 }
141
InputWindowsManager()142 InputWindowsManager::InputWindowsManager() : bindInfo_(BIND_CFG_FILE_NAME)
143 {
144 MMI_HILOGI("Bind cfg file name:%{public}s", BIND_CFG_FILE_NAME.c_str());
145 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
146 lastWindowInfo_.id = -1;
147 lastWindowInfo_.pid = -1;
148 lastWindowInfo_.uid = -1;
149 lastWindowInfo_.agentWindowId = -1;
150 lastWindowInfo_.area = { 0, 0, 0, 0 };
151 lastWindowInfo_.flags = -1;
152 lastWindowInfo_.windowType = 0;
153 mouseDownInfo_.id = -1;
154 mouseDownInfo_.pid = -1;
155 mouseDownInfo_.uid = -1;
156 mouseDownInfo_.agentWindowId = -1;
157 mouseDownInfo_.area = { 0, 0, 0, 0 };
158 mouseDownInfo_.flags = -1;
159 mouseDownInfo_.windowType = 0;
160 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
161 #ifdef OHOS_BUILD_ENABLE_TOUCH
162 lastTouchWindowInfo_.id = -1;
163 lastTouchWindowInfo_.pid = -1;
164 lastTouchWindowInfo_.uid = -1;
165 lastTouchWindowInfo_.agentWindowId = -1;
166 lastTouchWindowInfo_.area = { 0, 0, 0, 0 };
167 lastTouchWindowInfo_.flags = -1;
168 lastTouchWindowInfo_.windowType = 0;
169 #endif // OHOS_BUILD_ENABLE_TOUCH
170 std::lock_guard<std::mutex> lock(tmpInfoMutex_);
171 displayGroupInfoTmp_.focusWindowId = -1;
172 displayGroupInfoTmp_.width = 0;
173 displayGroupInfoTmp_.height = 0;
174 displayGroupInfo_.focusWindowId = -1;
175 displayGroupInfo_.width = 0;
176 displayGroupInfo_.height = 0;
177 }
178
~InputWindowsManager()179 InputWindowsManager::~InputWindowsManager()
180 {
181 CALL_INFO_TRACE;
182 }
183
DeviceStatusChanged(int32_t deviceId,const std::string & sysUid,const std::string devStatus)184 void InputWindowsManager::DeviceStatusChanged(int32_t deviceId, const std::string &sysUid, const std::string devStatus)
185 {
186 CALL_INFO_TRACE;
187 if (devStatus == "add") {
188 bindInfo_.AddInputDevice(deviceId, sysUid);
189 } else {
190 bindInfo_.RemoveInputDevice(deviceId);
191 }
192 }
193
Init(UDSServer & udsServer)194 void InputWindowsManager::Init(UDSServer& udsServer)
195 {
196 udsServer_ = &udsServer;
197 CHKPV(udsServer_);
198 bindInfo_.Load();
199 #ifdef OHOS_BUILD_ENABLE_POINTER
200 udsServer_->AddSessionDeletedCallback([this] (SessionPtr session) { return this->OnSessionLost(session); });
201 InitMouseDownInfo();
202 #endif // OHOS_BUILD_ENABLE_POINTER
203 INPUT_DEV_MGR->SetInputStatusChangeCallback(
204 [this] (int32_t deviceId, const std::string &sysUid, const std::string devStatus) {
205 return this->DeviceStatusChanged(deviceId, sysUid, devStatus);
206 }
207 );
208 }
209
IgnoreTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)210 bool InputWindowsManager::IgnoreTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)
211 {
212 CHKPF(pointerEvent);
213 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN ||
214 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL) {
215 return false;
216 }
217 PointerEvent::PointerItem pointer {};
218 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointer)) {
219 MMI_HILOGE("Corrupted pointer event");
220 return false;
221 }
222 /* Fold status is indicated by 27th bit of long axis of touch. */
223 uint32_t longAxis = static_cast<uint32_t>(pointer.GetLongAxis());
224 if (cancelTouchStatus_) {
225 if (longAxis & FOLD_STATUS_MASK) {
226 // Screen in the process of folding, ignore this event
227 return true;
228 } else {
229 // Screen folding is complete
230 cancelTouchStatus_ = false;
231 return false;
232 }
233 } else if (longAxis & FOLD_STATUS_MASK) {
234 // The screen begins to collapse, reissues the cancel event, and ignores this event
235 MMI_HILOGI("Screen begins to collapse, reissue cancel event");
236 cancelTouchStatus_ = true;
237 ReissueCancelTouchEvent(pointerEvent);
238 return true;
239 }
240 return false;
241 }
242
ConvertToolType(int32_t toolType)243 int32_t InputWindowsManager::ConvertToolType(int32_t toolType)
244 {
245 CALL_DEBUG_ENTER;
246 int32_t toolTypeData = -1;
247 auto it = convertToolTypeMap_.find(toolType);
248 if (it != convertToolTypeMap_.end()) {
249 toolTypeData = it->second;
250 }
251 return toolTypeData;
252 }
253
ReissueCancelTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)254 void InputWindowsManager::ReissueCancelTouchEvent(std::shared_ptr<PointerEvent> pointerEvent)
255 {
256 CALL_INFO_TRACE;
257 #ifdef OHOS_BUILD_ENABLE_TOUCH
258 auto items = pointerEvent->GetAllPointerItems();
259 for (const auto &item : items) {
260 if (!item.IsPressed()) {
261 continue;
262 }
263 int32_t pointerId = item.GetPointerId();
264 auto tPointerEvent = std::make_shared<PointerEvent>(*pointerEvent);
265 tPointerEvent->SetPointerId(pointerId);
266 bool isDragging = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
267 (item.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId);
268 if (isDragging) {
269 tPointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_CANCEL);
270 } else {
271 tPointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
272 }
273 tPointerEvent->SetActionTime(GetSysClockTime());
274 tPointerEvent->UpdateId();
275 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
276 CHKPV(inputEventNormalizeHandler);
277 inputEventNormalizeHandler->HandleTouchEvent(tPointerEvent);
278 auto iter = touchItemDownInfos_.find(pointerId);
279 if (iter != touchItemDownInfos_.end()) {
280 iter->second.flag = false;
281 }
282 }
283 #endif // OHOS_BUILD_ENABLE_TOUCH
284 }
285
286 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
InitMouseDownInfo()287 void InputWindowsManager::InitMouseDownInfo()
288 {
289 mouseDownInfo_.id = -1;
290 mouseDownInfo_.pid = -1;
291 mouseDownInfo_.defaultHotAreas.clear();
292 mouseDownInfo_.pointerHotAreas.clear();
293 }
294 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
295
GetWindowGroupInfoByDisplayId(int32_t displayId) const296 const std::vector<WindowInfo> &InputWindowsManager::GetWindowGroupInfoByDisplayId(int32_t displayId) const
297 {
298 CALL_DEBUG_ENTER;
299 if (displayId < 0) {
300 return displayGroupInfo_.windowsInfo;
301 }
302 auto iter = windowsPerDisplay_.find(displayId);
303 if (iter == windowsPerDisplay_.end()) {
304 MMI_HILOGD("GetWindowInfo displayId:%{public}d is null from windowGroupInfo_", displayId);
305 return displayGroupInfo_.windowsInfo;
306 }
307 if (iter->second.windowsInfo.empty()) {
308 MMI_HILOGW("GetWindowInfo displayId:%{public}d is empty", displayId);
309 return displayGroupInfo_.windowsInfo;
310 }
311 return iter->second.windowsInfo;
312 }
313
CheckAppFocused(int32_t pid)314 bool InputWindowsManager::CheckAppFocused(int32_t pid)
315 {
316 int32_t focusWindowId = displayGroupInfo_.focusWindowId;
317 for (auto windowinfo : displayGroupInfo_.windowsInfo) {
318 if (windowinfo.id == focusWindowId) {
319 if (windowinfo.pid != pid) {
320 MMI_HILOGW("CheckAppFocused focusWindowId:%{public}d, pid:%{public}d, windowinfo.pid:%{public}d",
321 focusWindowId, pid, windowinfo.pid);
322 return false;
323 }
324 return true;
325 }
326 }
327 MMI_HILOGW("CheckAppFocused failed:%{public}d", focusWindowId);
328 return false;
329 }
330
GetCancelEventFlag(std::shared_ptr<PointerEvent> pointerEvent)331 bool InputWindowsManager::GetCancelEventFlag(std::shared_ptr<PointerEvent> pointerEvent)
332 {
333 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
334 auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
335 if (iter != touchItemDownInfos_.end()) {
336 return iter->second.flag;
337 }
338 return true;
339 } else if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE ||
340 pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHPAD) {
341 if (mouseDownInfo_.pid != -1) {
342 return false;
343 }
344 return true;
345 }
346 return false;
347 }
348
349 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
AdjustFingerFlag(std::shared_ptr<PointerEvent> pointerEvent)350 bool InputWindowsManager::AdjustFingerFlag(std::shared_ptr<PointerEvent> pointerEvent)
351 {
352 CHKPF(pointerEvent);
353 if (pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
354 return false;
355 }
356 auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
357 return (iter != touchItemDownInfos_.end() && !(iter->second.flag));
358 }
359
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)360 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)
361 {
362 CALL_DEBUG_ENTER;
363 CHKPR(pointerEvent, INVALID_FD);
364 const WindowInfo* windowInfo = nullptr;
365 auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
366 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
367 if (iter != touchItemDownInfos_.end() && !(iter->second.flag)) {
368 MMI_HILOG_DISPATCHD("Drop event");
369 return INVALID_FD;
370 }
371 }
372 std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
373 for (const auto &item : windowsInfo) {
374 bool checkUIExtentionWindow = false;
375 // Determine whether it is a safety sub window
376 for (auto &uiExtentionWindowInfo : item.uiExtentionWindowInfo) {
377 if (uiExtentionWindowInfo.id == pointerEvent->GetTargetWindowId()) {
378 MMI_HILOGD("Find windowInfo by window id %{public}d", uiExtentionWindowInfo.id);
379 windowInfo = &uiExtentionWindowInfo;
380 checkUIExtentionWindow = true;
381 break;
382 }
383 }
384 if (checkUIExtentionWindow) {
385 break;
386 }
387 bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
388 !IsValidZorderWindow(item, pointerEvent);
389 if (checkWindow) {
390 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
391 "window:%{public}d, flags:%{public}d", item.id, item.flags);
392 continue;
393 }
394 if (item.id == pointerEvent->GetTargetWindowId()) {
395 MMI_HILOG_DISPATCHD("find windowinfo by window id %{public}d", item.id);
396 windowInfo = &item;
397 break;
398 }
399 }
400
401 if (windowInfo == nullptr && pointerEvent->GetTargetDisplayId() != firstBtnDownWindowInfo_.second) {
402 windowsInfo.clear();
403 windowsInfo = GetWindowGroupInfoByDisplayId(firstBtnDownWindowInfo_.second);
404 for (const auto &item : windowsInfo) {
405 bool checkUIExtentionWindow = false;
406 // Determine whether it is a safety sub window
407 for (auto &uiExtentionWindowInfo : item.uiExtentionWindowInfo) {
408 if (uiExtentionWindowInfo.id == pointerEvent->GetTargetWindowId()) {
409 MMI_HILOGD("Find windowInfo by window id %{public}d", uiExtentionWindowInfo.id);
410 windowInfo = &uiExtentionWindowInfo;
411 checkUIExtentionWindow = true;
412 break;
413 }
414 }
415 if (checkUIExtentionWindow) {
416 break;
417 }
418 bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
419 !IsValidZorderWindow(item, pointerEvent);
420 if (checkWindow) {
421 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
422 "window:%{public}d, flags:%{public}d", item.id, item.flags);
423 continue;
424 }
425 if (item.id == pointerEvent->GetTargetWindowId()) {
426 MMI_HILOG_DISPATCHD("find windowinfo by window id %{public}d", item.id);
427 windowInfo = &item;
428 break;
429 }
430 }
431 }
432 if (windowInfo == nullptr) {
433 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL) {
434 dragPointerStyle_.id = DEFAULT_POINTER_STYLE;
435 IPointerDrawingManager::GetInstance()->DrawPointerStyle(dragPointerStyle_);
436 }
437 MMI_HILOG_DISPATCHD("window info is null, pointerAction:%{public}d", pointerEvent->GetPointerAction());
438 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
439 windowInfo = &lastWindowInfo_;
440 }
441 }
442 CHKPR(udsServer_, INVALID_FD);
443 if (windowInfo != nullptr) {
444 FoldScreenRotation(pointerEvent);
445 MMI_HILOG_DISPATCHD("get pid:%{public}d from idxPidMap", windowInfo->pid);
446 return udsServer_->GetClientFd(windowInfo->pid);
447 }
448 if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL &&
449 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
450 MMI_HILOG_DISPATCHD("window info is null, so pointerEvent is dropped! return -1");
451 return udsServer_->GetClientFd(-1);
452 }
453 int32_t pid = -1;
454 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
455 if (iter != touchItemDownInfos_.end()) {
456 MMI_HILOG_DISPATCHI("Cant not find pid");
457 pid = iter->second.window.pid;
458 iter->second.flag = false;
459 MMI_HILOG_DISPATCHD("touchscreen occurs, new pid:%{public}d", pid);
460 }
461 }
462 #ifdef OHOS_BUILD_ENABLE_POINTER
463 if ((pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) ||
464 (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_CROWN)) {
465 if (mouseDownInfo_.pid != -1) {
466 pid = GetWindowPid(mouseDownInfo_.agentWindowId);
467 if (pid < 0) {
468 pid = mouseDownInfo_.pid;
469 }
470 MMI_HILOGD("mouseevent occurs, update the pid:%{public}d", pid);
471 InitMouseDownInfo();
472 } else if (axisBeginWindowInfo_ && axisBeginWindowInfo_->pid != -1) {
473 pid = GetWindowPid(axisBeginWindowInfo_->agentWindowId);
474 if (pid < 0) {
475 pid = axisBeginWindowInfo_->pid;
476 }
477 MMI_HILOGD("The axisBeginEvent occurs, update the pid:%{public}d", pid);
478 axisBeginWindowInfo_ = std::nullopt;
479 }
480 }
481 #endif // OHOS_BUILD_ENABLE_POINTER
482 MMI_HILOGD("Get clientFd by %{public}d", pid);
483 return udsServer_->GetClientFd(pid);
484 }
485
FoldScreenRotation(std::shared_ptr<PointerEvent> pointerEvent)486 void InputWindowsManager::FoldScreenRotation(std::shared_ptr<PointerEvent> pointerEvent)
487 {
488 CALL_DEBUG_ENTER;
489 CHKPV(pointerEvent);
490 auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
491 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
492 if (iter == touchItemDownInfos_.end()) {
493 MMI_HILOG_DISPATCHD("Unable to find finger information for touch.pointerId:%{public}d",
494 pointerEvent->GetPointerId());
495 return;
496 }
497 }
498 auto displayId = pointerEvent->GetTargetDisplayId();
499 auto physicDisplayInfo = GetPhysicalDisplay(displayId);
500 CHKPV(physicDisplayInfo);
501 if (lastDirection_.first != displayId || lastDirection_.second == static_cast<Direction>(-1)) {
502 lastDirection_ = std::make_pair(displayId, physicDisplayInfo->direction);
503 return;
504 }
505 if (physicDisplayInfo->direction != lastDirection_.second) {
506 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
507 PointerEvent::PointerItem item;
508 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), item)) {
509 MMI_HILOGE("Get pointer item failed. pointer:%{public}d", pointerEvent->GetPointerId());
510 lastDirection_ = std::make_pair(displayId, physicDisplayInfo->direction);
511 return;
512 }
513 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE && !(item.IsPressed())) {
514 lastDirection_ = std::make_pair(displayId, physicDisplayInfo->direction);
515 return;
516 }
517 }
518 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE ||
519 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_MOVE) {
520 int32_t pointerAction = pointerEvent->GetPointerAction();
521 if (pointerAction == PointerEvent::POINTER_ACTION_HOVER_MOVE ||
522 pointerAction == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
523 pointerAction == PointerEvent::POINTER_ACTION_HOVER_EXIT ||
524 pointerAction == PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
525 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
526 } else {
527 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
528 }
529 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
530 pointerEvent->SetOriginPointerAction(pointerAction);
531 MMI_HILOG_DISPATCHI("touch event send cancel");
532 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
533 iter->second.flag = false;
534 }
535 }
536 }
537 lastDirection_ = std::make_pair(displayId, physicDisplayInfo->direction);
538 }
539 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
540
541 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)542 std::vector<std::pair<int32_t, TargetInfo>> InputWindowsManager::UpdateTarget(std::shared_ptr<KeyEvent> keyEvent)
543 {
544 CALL_DEBUG_ENTER;
545 if (!isParseConfig_) {
546 ParseConfig();
547 isParseConfig_ = true;
548 }
549 std::vector<std::pair<int32_t, TargetInfo>> secSubWindowTargets;
550 if (keyEvent == nullptr) {
551 MMI_HILOG_DISPATCHE("keyEvent is nullptr");
552 return secSubWindowTargets;
553 }
554 auto secSubWindows = GetPidAndUpdateTarget(keyEvent);
555 for (const auto &item : secSubWindows) {
556 int32_t fd = INVALID_FD;
557 int32_t pid = item.first;
558 if (pid <= 0) {
559 MMI_HILOG_DISPATCHE("Invalid pid:%{public}d", pid);
560 continue;
561 }
562 fd = udsServer_->GetClientFd(pid);
563 if (fd < 0) {
564 MMI_HILOG_DISPATCHE("The windowPid:%{public}d matching fd:%{public}d is invalid", pid, fd);
565 continue;
566 }
567 secSubWindowTargets.emplace_back(std::make_pair(fd, item.second));
568 }
569 return secSubWindowTargets;
570 }
571
HandleKeyEventWindowId(std::shared_ptr<KeyEvent> keyEvent)572 void InputWindowsManager::HandleKeyEventWindowId(std::shared_ptr<KeyEvent> keyEvent)
573 {
574 CALL_DEBUG_ENTER;
575 CHKPV(keyEvent);
576 int32_t focusWindowId = displayGroupInfo_.focusWindowId;
577 std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(keyEvent->GetTargetDisplayId());
578 for (auto &item : windowsInfo) {
579 if (item.id == focusWindowId) {
580 keyEvent->SetTargetWindowId(item.id);
581 keyEvent->SetAgentWindowId(item.agentWindowId);
582 if (item.privacyMode == SecureFlag::PRIVACY_MODE) {
583 keyEvent->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
584 }
585 return;
586 }
587 }
588 }
589
ReissueEvent(std::shared_ptr<KeyEvent> keyEvent,int32_t focusWindowId)590 void InputWindowsManager::ReissueEvent(std::shared_ptr<KeyEvent> keyEvent, int32_t focusWindowId)
591 {
592 if (keyEvent->GetKeyAction() != KeyEvent::KEY_ACTION_CANCEL && focusWindowId_ != -1 &&
593 focusWindowId_ != focusWindowId && keyEvent->IsRepeatKey()) {
594 auto keyEventReissue = std::make_shared<KeyEvent>(*keyEvent);
595 auto keyItem = keyEventReissue->GetKeyItems();
596 for (auto item = keyItem.begin(); item != keyItem.end(); ++item) {
597 item->SetPressed(false);
598 }
599 keyEventReissue->SetKeyItem(keyItem);
600 keyEventReissue->UpdateId();
601 keyEventReissue->SetAction(KeyEvent::KEY_ACTION_CANCEL);
602 keyEventReissue->SetKeyAction(KeyEvent::KEY_ACTION_CANCEL);
603 keyEventReissue->SetTargetWindowId(focusWindowId_);
604 keyEventReissue->SetAgentWindowId(focusWindowId_);
605
606 auto eventDispatchHandler = InputHandler->GetEventDispatchHandler();
607 auto udServer = InputHandler->GetUDSServer();
608 CHKPV(udServer);
609 auto fd = udServer->GetClientFd(GetWindowPid(focusWindowId_));
610 MMI_HILOG_DISPATCHI("Out focus window:%{public}d is replaced by window:%{public}d",
611 focusWindowId_, focusWindowId);
612 if (eventDispatchHandler != nullptr && udServer != nullptr) {
613 eventDispatchHandler->DispatchKeyEvent(fd, *udServer, keyEventReissue);
614 }
615 }
616 focusWindowId_ = focusWindowId;
617 }
618 #endif // OHOS_BUILD_ENABLE_KEYBOARD
619
GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const620 int32_t InputWindowsManager::GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const
621 {
622 int32_t displayId = inputEvent->GetTargetDisplayId();
623 if (displayId < 0) {
624 MMI_HILOGD("Target display is -1");
625 if (displayGroupInfo_.displaysInfo.empty()) {
626 return displayId;
627 }
628 displayId = displayGroupInfo_.displaysInfo[0].id;
629 inputEvent->SetTargetDisplayId(displayId);
630 }
631 return displayId;
632 }
633
634 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent,int32_t windowId)635 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent, int32_t windowId)
636 {
637 CALL_DEBUG_ENTER;
638 CHKPR(udsServer_, INVALID_FD);
639 CHKPR(pointerEvent, INVALID_FD);
640 const WindowInfo* windowInfo = nullptr;
641 std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
642 for (const auto &item : windowInfos) {
643 bool checkUIExtentionWindow = false;
644 // Determine whether it is a safety sub window
645 for (const auto &uiExtentionWindowInfo : item.uiExtentionWindowInfo) {
646 if (uiExtentionWindowInfo.id == windowId) {
647 MMI_HILOGD("Find windowInfo by window id %{public}d", uiExtentionWindowInfo.id);
648 windowInfo = &uiExtentionWindowInfo;
649 checkUIExtentionWindow = true;
650 break;
651 }
652 }
653 if (checkUIExtentionWindow) {
654 break;
655 }
656 bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
657 !IsValidZorderWindow(item, pointerEvent);
658 if (checkWindow) {
659 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
660 "window:%{public}d, flags:%{public}d", item.id, item.flags);
661 continue;
662 }
663 if (item.id == windowId) {
664 MMI_HILOGD("Find windowInfo by window id %{public}d", item.id);
665 windowInfo = &item;
666 break;
667 }
668 }
669 if (windowInfo == nullptr) {
670 MMI_HILOGE("WindowInfo is nullptr, pointerAction:%{public}d", pointerEvent->GetPointerAction());
671 return INVALID_FD;
672 }
673 MMI_HILOGD("Get pid:%{public}d from idxPidMap", windowInfo->pid);
674 return udsServer_->GetClientFd(windowInfo->pid);
675 }
676 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
677
678 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
GetPidAndUpdateTarget(std::shared_ptr<KeyEvent> keyEvent)679 std::vector<std::pair<int32_t, TargetInfo>> InputWindowsManager::GetPidAndUpdateTarget(
680 std::shared_ptr<KeyEvent> keyEvent)
681 {
682 CALL_DEBUG_ENTER;
683 std::vector<std::pair<int32_t, TargetInfo>> secSubWindows;
684 if (keyEvent == nullptr) {
685 MMI_HILOG_DISPATCHE("keyEvent is nullptr");
686 return secSubWindows;
687 }
688 const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
689 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
690 UpdateKeyEventDisplayId(keyEvent, focusWindowId);
691 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
692 WindowInfo* windowInfo = nullptr;
693 std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(keyEvent->GetTargetDisplayId());
694 bool isUIExtention = false;
695 auto iter = windowsInfo.begin();
696 for (; iter != windowsInfo.end(); ++iter) {
697 if (iter->id == focusWindowId) {
698 windowInfo = &(*iter);
699 if (!iter->uiExtentionWindowInfo.empty() && !IsOnTheWhitelist(keyEvent)) {
700 isUIExtention = true;
701 }
702 break;
703 }
704 }
705 if (windowInfo == nullptr) {
706 MMI_HILOG_DISPATCHE("windowInfo is nullptr");
707 return secSubWindows;
708 }
709 #ifdef OHOS_BUILD_ENABLE_ANCO
710 if (IsAncoWindowFocus(*windowInfo)) {
711 MMI_HILOG_DISPATCHD("focusWindowId:%{public}d is anco window", focusWindowId);
712 return secSubWindows;
713 }
714 #endif // OHOS_BUILD_ENABLE_ANCO
715 TargetInfo targetInfo = { windowInfo->privacyMode, windowInfo->id, windowInfo->agentWindowId };
716 secSubWindows.emplace_back(std::make_pair(windowInfo->pid, targetInfo));
717 if (isUIExtention) {
718 for (const auto &item : iter->uiExtentionWindowInfo) {
719 if (item.privacyUIFlag) {
720 MMI_HILOG_DISPATCHD("security sub windowId:%{public}d,pid:%{public}d", item.id, item.pid);
721 targetInfo.privacyMode = item.privacyMode;
722 targetInfo.id = item.id;
723 targetInfo.agentWindowId = item.agentWindowId;
724 secSubWindows.emplace_back(std::make_pair(item.pid, targetInfo));
725 }
726 }
727 }
728 return secSubWindows;
729 }
730 #endif // OHOS_BUILD_ENABLE_KEYBOARD
731
GetWindowPid(int32_t windowId) const732 int32_t InputWindowsManager::GetWindowPid(int32_t windowId) const
733 {
734 CALL_DEBUG_ENTER;
735 int32_t windowPid = INVALID_PID;
736 for (const auto &item : displayGroupInfo_.windowsInfo) {
737 MMI_HILOGD("Get windowId:%{public}d", item.id);
738 if (item.id == windowId) {
739 windowPid = item.pid;
740 break;
741 }
742 for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
743 if (uiExtentionWindow.id == windowId) {
744 windowPid = uiExtentionWindow.pid;
745 break;
746 }
747 }
748 }
749 return windowPid;
750 }
751
GetWindowPid(int32_t windowId,const std::vector<WindowInfo> & windowsInfo) const752 int32_t InputWindowsManager::GetWindowPid(int32_t windowId, const std::vector<WindowInfo> &windowsInfo) const
753 {
754 int32_t windowPid = INVALID_PID;
755 for (const auto &item : windowsInfo) {
756 if (item.id == windowId) {
757 windowPid = item.pid;
758 break;
759 }
760 }
761 return windowPid;
762 }
763
CheckFocusWindowChange(const DisplayGroupInfo & displayGroupInfo)764 void InputWindowsManager::CheckFocusWindowChange(const DisplayGroupInfo &displayGroupInfo)
765 {
766 const int32_t oldFocusWindowId = displayGroupInfo_.focusWindowId;
767 const int32_t newFocusWindowId = displayGroupInfo.focusWindowId;
768 if (oldFocusWindowId == newFocusWindowId) {
769 return;
770 }
771 const int32_t oldFocusWindowPid = GetWindowPid(oldFocusWindowId);
772 const int32_t newFocusWindowPid = GetWindowPid(newFocusWindowId, displayGroupInfo.windowsInfo);
773 DfxHisysevent::OnFocusWindowChanged(oldFocusWindowId, newFocusWindowId, oldFocusWindowPid, newFocusWindowPid);
774 }
775
CheckZorderWindowChange(const std::vector<WindowInfo> & oldWindowsInfo,const std::vector<WindowInfo> & newWindowsInfo)776 void InputWindowsManager::CheckZorderWindowChange(const std::vector<WindowInfo> &oldWindowsInfo,
777 const std::vector<WindowInfo> &newWindowsInfo)
778 {
779 int32_t oldZorderFirstWindowId = -1;
780 int32_t newZorderFirstWindowId = -1;
781 if (!oldWindowsInfo.empty()) {
782 oldZorderFirstWindowId = oldWindowsInfo[0].id;
783 }
784 if (!newWindowsInfo.empty()) {
785 newZorderFirstWindowId = newWindowsInfo[0].id;
786 }
787 if (oldZorderFirstWindowId == newZorderFirstWindowId) {
788 return;
789 }
790 const int32_t oldZorderFirstWindowPid = GetWindowPid(oldZorderFirstWindowId);
791 const int32_t newZorderFirstWindowPid = GetWindowPid(newZorderFirstWindowId, newWindowsInfo);
792 DfxHisysevent::OnZorderWindowChanged(oldZorderFirstWindowId, newZorderFirstWindowId,
793 oldZorderFirstWindowPid, newZorderFirstWindowPid);
794 }
795
UpdateDisplayIdAndName()796 void InputWindowsManager::UpdateDisplayIdAndName()
797 {
798 using IdNames = std::set<std::pair<int32_t, std::string>>;
799 IdNames newInfo;
800 for (const auto &item : displayGroupInfo_.displaysInfo) {
801 newInfo.insert(std::make_pair(item.id, item.uniq));
802 }
803 auto oldInfo = bindInfo_.GetDisplayIdNames();
804 if (newInfo == oldInfo) {
805 return;
806 }
807 for (auto it = oldInfo.begin(); it != oldInfo.end();) {
808 if (newInfo.find(*it) == newInfo.end()) {
809 bindInfo_.RemoveDisplay(it->first);
810 oldInfo.erase(it++);
811 } else {
812 ++it;
813 }
814 }
815 for (const auto &item : newInfo) {
816 if (item.first >= HICAR_MIN_DISPLAY_ID) {
817 MMI_HILOGI("Displayinfo id:%{public}d name:%{public}s", item.first, item.second.c_str());
818 continue;
819 }
820 if (!bindInfo_.IsDisplayAdd(item.first, item.second)) {
821 bindInfo_.AddDisplay(item.first, item.second);
822 }
823 }
824 }
825
GetDisplayBindInfo(DisplayBindInfos & infos)826 int32_t InputWindowsManager::GetDisplayBindInfo(DisplayBindInfos &infos)
827 {
828 CALL_DEBUG_ENTER;
829 return bindInfo_.GetDisplayBindInfo(infos);
830 }
831
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)832 int32_t InputWindowsManager::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
833 {
834 CALL_DEBUG_ENTER;
835 return bindInfo_.SetDisplayBind(deviceId, displayId, msg);
836 }
837
UpdateCaptureMode(const DisplayGroupInfo & displayGroupInfo)838 void InputWindowsManager::UpdateCaptureMode(const DisplayGroupInfo &displayGroupInfo)
839 {
840 if (captureModeInfo_.isCaptureMode && (!displayGroupInfo_.windowsInfo.empty()) &&
841 ((displayGroupInfo_.focusWindowId != displayGroupInfo.focusWindowId) ||
842 (displayGroupInfo_.windowsInfo[0].id != displayGroupInfo.windowsInfo[0].id))) {
843 captureModeInfo_.isCaptureMode = false;
844 }
845 }
846
IsFocusedSession(int32_t session) const847 bool InputWindowsManager::IsFocusedSession(int32_t session) const
848 {
849 return ((session >= 0) && (session == GetWindowPid(displayGroupInfo_.focusWindowId)));
850 }
851
UpdateWindowInfo(const WindowGroupInfo & windowGroupInfo)852 void InputWindowsManager::UpdateWindowInfo(const WindowGroupInfo &windowGroupInfo)
853 {
854 CALL_DEBUG_ENTER;
855 PrintWindowGroupInfo(windowGroupInfo);
856 #ifdef OHOS_BUILD_ENABLE_ANCO
857 if (windowGroupInfo.windowsInfo.size() == SHELL_WINDOW_COUNT && IsAncoWindow(windowGroupInfo.windowsInfo[0])) {
858 return UpdateShellWindow(windowGroupInfo.windowsInfo[0]);
859 }
860 #endif // OHOS_BUILD_ENABLE_ANCO
861 DisplayGroupInfo displayGroupInfo;
862 {
863 std::lock_guard<std::mutex> lock(tmpInfoMutex_);
864 displayGroupInfo = displayGroupInfoTmp_;
865 }
866 displayGroupInfo.focusWindowId = windowGroupInfo.focusWindowId;
867 for (const auto &item : windowGroupInfo.windowsInfo) {
868 UpdateDisplayInfoByIncrementalInfo(item, displayGroupInfo);
869 }
870
871 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
872 pointerDrawFlag_ = NeedUpdatePointDrawFlag(windowGroupInfo.windowsInfo);
873 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
874
875 #ifdef OHOS_BUILD_ENABLE_ANCO
876 UpdateWindowInfoExt(windowGroupInfo, displayGroupInfo);
877 #endif // OHOS_BUILD_ENABLE_ANCO
878 UpdateDisplayInfoExtIfNeed(displayGroupInfo, false);
879 }
880
UpdateDisplayInfoExtIfNeed(DisplayGroupInfo & displayGroupInfo,bool needUpdateDisplayExt)881 void InputWindowsManager::UpdateDisplayInfoExtIfNeed(DisplayGroupInfo &displayGroupInfo, bool needUpdateDisplayExt)
882 {
883 UpdateDisplayInfo(displayGroupInfo);
884 #ifdef OHOS_BUILD_ENABLE_ANCO
885 if (needUpdateDisplayExt) {
886 UpdateDisplayInfoExt(displayGroupInfo);
887 }
888 #endif // OHOS_BUILD_ENABLE_ANCO
889 if (displayGroupInfo.displaysInfo.empty()) {
890 MMI_HILOGE("displaysInfo is empty");
891 return;
892 }
893 auto physicDisplayInfo = GetPhysicalDisplay(displayGroupInfo.displaysInfo[0].id);
894 CHKPV(physicDisplayInfo);
895 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
896 TOUCH_DRAWING_MGR->UpdateDisplayInfo(*physicDisplayInfo);
897 TOUCH_DRAWING_MGR->RotationScreen();
898 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
899 }
900
UpdateDisplayInfoByIncrementalInfo(const WindowInfo & window,DisplayGroupInfo & displayGroupInfo)901 void InputWindowsManager::UpdateDisplayInfoByIncrementalInfo(const WindowInfo &window,
902 DisplayGroupInfo &displayGroupInfo)
903 {
904 CALL_DEBUG_ENTER;
905 switch (window.action) {
906 case WINDOW_UPDATE_ACTION::ADD:
907 case WINDOW_UPDATE_ACTION::ADD_END: {
908 auto id = window.id;
909 auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
910 [id](const auto& item) { return item.id == id; });
911 if (pos == std::end(displayGroupInfo.windowsInfo)) {
912 displayGroupInfo.windowsInfo.emplace_back(window);
913 } else {
914 *pos = window;
915 }
916 break;
917 }
918 case WINDOW_UPDATE_ACTION::DEL: {
919 auto oldWindow = displayGroupInfo.windowsInfo.begin();
920 while (oldWindow != displayGroupInfo.windowsInfo.end()) {
921 if (oldWindow->id == window.id) {
922 oldWindow = displayGroupInfo.windowsInfo.erase(oldWindow);
923 } else {
924 oldWindow++;
925 }
926 }
927 break;
928 }
929 case WINDOW_UPDATE_ACTION::CHANGE: {
930 auto id = window.id;
931 auto pos = std::find_if(std::begin(displayGroupInfo.windowsInfo), std::end(displayGroupInfo.windowsInfo),
932 [id](const auto& item) { return item.id == id; });
933 if (pos != std::end(displayGroupInfo.windowsInfo)) {
934 *pos = window;
935 }
936 break;
937 }
938 default: {
939 MMI_HILOGI("WINDOW_UPDATE_ACTION is action:%{public}d", window.action);
940 break;
941 }
942 }
943 }
944
UpdateWindowsInfoPerDisplay(const DisplayGroupInfo & displayGroupInfo)945 void InputWindowsManager::UpdateWindowsInfoPerDisplay(const DisplayGroupInfo &displayGroupInfo)
946 {
947 CALL_DEBUG_ENTER;
948 std::map<int32_t, WindowGroupInfo> windowsPerDisplay;
949 for (const auto &window : displayGroupInfo.windowsInfo) {
950 auto it = windowsPerDisplay.find(window.displayId);
951 if (it == windowsPerDisplay.end()) {
952 windowsPerDisplay[window.displayId] = WindowGroupInfo {-1, window.displayId, {window}};
953 } else {
954 it->second.windowsInfo.emplace_back(window);
955 }
956 if (displayGroupInfo.focusWindowId == window.id) {
957 windowsPerDisplay[window.displayId].focusWindowId = window.id;
958 }
959 }
960 for (auto iter : windowsPerDisplay) {
961 std::sort(iter.second.windowsInfo.begin(), iter.second.windowsInfo.end(),
962 [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
963 return lwindow.zOrder > rwindow.zOrder;
964 });
965 }
966 for (const auto &item : windowsPerDisplay) {
967 int32_t displayId = item.first;
968 if (windowsPerDisplay_.find(displayId) != windowsPerDisplay_.end()) {
969 CheckZorderWindowChange(windowsPerDisplay_[displayId].windowsInfo, item.second.windowsInfo);
970 }
971 }
972
973 windowsPerDisplay_ = windowsPerDisplay;
974
975 #if defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
976 for (const auto &window : displayGroupInfo.windowsInfo) {
977 if (window.windowType == static_cast<int32_t>(Rosen::WindowType::WINDOW_TYPE_TRANSPARENT_VIEW)) {
978 MMI_HILOGI("Transparent window of UNI-CUBIC emerges, redirect touches");
979 if (auto touchGestureMgr = touchGestureMgr_.lock(); touchGestureMgr != nullptr) {
980 touchGestureMgr->HandleGestureWindowEmerged(window.id, lastPointerEventforGesture_);
981 }
982 break;
983 }
984 }
985 #endif // defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
986 }
987
UpdateWindowInfo(DisplayGroupInfo & displayGroupInfo)988 WINDOW_UPDATE_ACTION InputWindowsManager::UpdateWindowInfo(DisplayGroupInfo &displayGroupInfo)
989 {
990 auto action = WINDOW_UPDATE_ACTION::ADD_END;
991 if (!displayGroupInfo.windowsInfo.empty()) {
992 action = displayGroupInfo.windowsInfo.back().action;
993 }
994 MMI_HILOGD("Current action is:%{public}d", action);
995 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
996 pointerDrawFlag_ = NeedUpdatePointDrawFlag(displayGroupInfo.windowsInfo);
997 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
998 std::sort(displayGroupInfo.windowsInfo.begin(), displayGroupInfo.windowsInfo.end(),
999 [](const WindowInfo &lwindow, const WindowInfo &rwindow) -> bool {
1000 return lwindow.zOrder > rwindow.zOrder;
1001 });
1002 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1003 for (auto &windowInfo : displayGroupInfo.windowsInfo) {
1004 if (windowInfo.isDisplayCoord) {
1005 continue;
1006 }
1007 auto displayInfo = GetPhysicalDisplay(windowInfo.displayId, displayGroupInfo);
1008 CHKPR(displayInfo, action);
1009 ChangeWindowArea(displayInfo->x, displayInfo->y, windowInfo);
1010 if (!windowInfo.uiExtentionWindowInfo.empty()) {
1011 for (auto &item : windowInfo.uiExtentionWindowInfo) {
1012 ChangeWindowArea(displayInfo->x, displayInfo->y, item);
1013 }
1014 }
1015 windowInfo.isDisplayCoord = true;
1016 }
1017 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1018 return action;
1019 }
1020
1021 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
ChangeWindowArea(int32_t x,int32_t y,WindowInfo & windowInfo)1022 void InputWindowsManager::ChangeWindowArea(int32_t x, int32_t y, WindowInfo &windowInfo)
1023 {
1024 windowInfo.area.x += x;
1025 windowInfo.area.y += y;
1026 for (auto &area : windowInfo.defaultHotAreas) {
1027 area.x += x;
1028 area.y += y;
1029 }
1030 for (auto &area : windowInfo.pointerHotAreas) {
1031 area.x += x;
1032 area.y += y;
1033 }
1034 }
1035
GetMainScreenDisplayInfo(const DisplayGroupInfo & displayGroupInfo,DisplayInfo & mainScreenDisplayInfo) const1036 int32_t InputWindowsManager::GetMainScreenDisplayInfo(const DisplayGroupInfo &displayGroupInfo,
1037 DisplayInfo &mainScreenDisplayInfo) const
1038 {
1039 CALL_DEBUG_ENTER;
1040 if (displayGroupInfo.displaysInfo.empty()) {
1041 MMI_HILOGE("displayGroupInfo doesn't contain displayInfo");
1042 return RET_ERR;
1043 }
1044 for (const DisplayInfo& display : displayGroupInfo.displaysInfo) {
1045 if (display.screenCombination == OHOS::MMI::ScreenCombination::SCREEN_MAIN) {
1046 mainScreenDisplayInfo = display;
1047 return RET_OK;
1048 }
1049 }
1050 MMI_HILOGD("displayGroupInfo has no main screen, get displayGroupInfo.displaysInfo[0] back");
1051 mainScreenDisplayInfo = displayGroupInfo.displaysInfo[0];
1052 return RET_OK;
1053 }
1054
SendBackCenterPointerEevent(const CursorPosition & cursorPos)1055 void InputWindowsManager::SendBackCenterPointerEevent(const CursorPosition &cursorPos)
1056 {
1057 CALL_DEBUG_ENTER;
1058 auto lastPointerEventCopy = GetlastPointerEvent();
1059 CHKPV(lastPointerEventCopy);
1060 int32_t lastPointerAction = lastPointerEventCopy->GetPointerAction();
1061 std::shared_ptr<PointerEvent> pointerBackCenterEvent = std::make_shared<PointerEvent>(*lastPointerEventCopy);
1062 pointerBackCenterEvent->SetTargetDisplayId(cursorPos.displayId);
1063 auto mainDisplayInfo = GetPhysicalDisplay(cursorPos.displayId);
1064 int32_t logicalX = cursorPos.cursorPos.x + mainDisplayInfo->x;
1065 int32_t logicalY = cursorPos.cursorPos.y + mainDisplayInfo->y;
1066 auto touchWindow = SelectWindowInfo(logicalX, logicalY, pointerBackCenterEvent);
1067 if (touchWindow == std::nullopt) {
1068 MMI_HILOGD("Maybe just down left mouse button, the mouse did not on the window");
1069 return;
1070 }
1071 int32_t pointerId = pointerBackCenterEvent->GetPointerId();
1072 PointerEvent::PointerItem item;
1073 pointerBackCenterEvent->GetPointerItem(pointerId, item);
1074 item.SetDisplayX(cursorPos.cursorPos.x);
1075 item.SetDisplayY(cursorPos.cursorPos.y);
1076 item.SetCanceled(true);
1077 pointerBackCenterEvent->UpdatePointerItem(pointerId, item);
1078 pointerBackCenterEvent->SetTargetWindowId(touchWindow->id);
1079 pointerBackCenterEvent->SetAgentWindowId(touchWindow->id);
1080
1081 if (lastPointerAction == PointerEvent::POINTER_ACTION_MOVE) {
1082 pointerBackCenterEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1083 } else if (lastPointerAction == PointerEvent::POINTER_ACTION_PULL_MOVE) {
1084 pointerBackCenterEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_CANCEL);
1085 }
1086 MMI_HILOGD("pointerBackCenterEvent status: %{public}s", pointerBackCenterEvent->ToString().c_str());
1087 InputHandler->GetFilterHandler()->HandlePointerEvent(pointerBackCenterEvent);
1088 }
1089
ResetPointerPosition(const DisplayGroupInfo & displayGroupInfo)1090 void InputWindowsManager::ResetPointerPosition(const DisplayGroupInfo &displayGroupInfo)
1091 {
1092 CALL_DEBUG_ENTER;
1093 if (displayGroupInfo.displaysInfo.empty()) {
1094 return;
1095 }
1096 CursorPosition oldPtrPos = GetCursorPos();
1097 CursorPosition cursorPos;
1098 for (auto ¤tDisplay : displayGroupInfo.displaysInfo) {
1099 if ((currentDisplay.screenCombination == OHOS::MMI::ScreenCombination::SCREEN_MAIN)) {
1100 MMI_HILOGD("CurDisplayId = %{public}d oldDisplayId = %{public}d", currentDisplay.id, oldPtrPos.displayId);
1101 if ((oldPtrPos.displayId != currentDisplay.id) || (!IsPointerOnCenter(oldPtrPos, currentDisplay))) {
1102 cursorPos = ResetCursorPos();
1103 UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
1104 }
1105 break;
1106 }
1107 }
1108
1109 auto lastPointerEventCopy = GetlastPointerEvent();
1110 if ((lastPointerEventCopy != nullptr) &&
1111 (!lastPointerEventCopy->IsButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT))) {
1112 MMI_HILOGD("Reset pointer position, left mouse button is not pressed");
1113 return;
1114 }
1115 (void)SendBackCenterPointerEevent(cursorPos);
1116 }
1117
IsPointerOnCenter(const CursorPosition & currentPos,const DisplayInfo & currentDisplay)1118 bool InputWindowsManager::IsPointerOnCenter(const CursorPosition ¤tPos, const DisplayInfo ¤tDisplay)
1119 {
1120 auto displayCenterX = currentDisplay.validWidth * HALF_RATIO;
1121 auto displayCenterY = currentDisplay.validHeight * HALF_RATIO;
1122 if ((currentPos.cursorPos.x == displayCenterX) &&
1123 (currentPos.cursorPos.y == displayCenterY)) {
1124 return true;
1125 }
1126 return false;
1127 }
1128 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1129
HandleValidDisplayChange(const DisplayGroupInfo & displayGroupInfo)1130 void InputWindowsManager::HandleValidDisplayChange(const DisplayGroupInfo &displayGroupInfo)
1131 {
1132 ResetPointerPositionIfOutValidDisplay(displayGroupInfo);
1133 CancelTouchScreenEventIfValidDisplayChange(displayGroupInfo);
1134 }
1135
ResetPointerPositionIfOutValidDisplay(const DisplayGroupInfo & displayGroupInfo)1136 void InputWindowsManager::ResetPointerPositionIfOutValidDisplay(const DisplayGroupInfo &displayGroupInfo)
1137 {
1138 if (displayGroupInfo.displaysInfo.empty()) {
1139 MMI_HILOGD("DisplayInfo empty");
1140 return;
1141 }
1142 CursorPosition cursorPos = GetCursorPos();
1143 int32_t cursorDisplayId = cursorPos.displayId;
1144 for (auto ¤tDisplay : displayGroupInfo.displaysInfo) {
1145 if (cursorDisplayId == currentDisplay.id) {
1146 bool isOut = IsPositionOutValidDisplay(cursorPos.cursorPos, currentDisplay);
1147 bool isChange = IsValidDisplayChange(currentDisplay);
1148 MMI_HILOGD("CurDisplayId = %{public}d CurPos = {x:%{private}d, y:%{private}d}, isOut = %{public}d, "
1149 "isChange = %{public}d",
1150 cursorDisplayId,
1151 static_cast<int32_t>(cursorPos.cursorPos.x),
1152 static_cast<int32_t>(cursorPos.cursorPos.y),
1153 static_cast<int32_t>(isOut),
1154 static_cast<int32_t>(isChange));
1155 int32_t validWidth = currentDisplay.validWidth;
1156 int32_t validHeight = currentDisplay.validHeight;
1157 bool pointerActiveRectValid = false;
1158 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
1159 MMI_HILOGD("Start checking vtp cursor active area");
1160 pointerActiveRectValid = IsPointerActiveRectValid(currentDisplay);
1161 if (pointerActiveRectValid) {
1162 validWidth = currentDisplay.pointerActiveWidth;
1163 validHeight = currentDisplay.pointerActiveHeight;
1164 MMI_HILOGD("vtp cursor active area w:%{private}d, h:%{private}d", validWidth, validHeight);
1165 }
1166 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
1167 if (isOut && (isChange || pointerActiveRectValid)) {
1168 double curX = validWidth * HALF_RATIO;
1169 double curY = validHeight * HALF_RATIO;
1170 UpdateAndAdjustMouseLocation(cursorDisplayId, curX, curY);
1171 IPointerDrawingManager::GetInstance()->SetPointerLocation(static_cast<int32_t>(cursorPos_.cursorPos.x),
1172 static_cast<int32_t>(cursorPos_.cursorPos.y),
1173 cursorPos_.displayId);
1174 }
1175 if (isChange) {
1176 CancelMouseEvent();
1177 }
1178 return;
1179 }
1180 }
1181 MMI_HILOGE("Can't find displayInfo by displayId:%{public}d", cursorDisplayId);
1182 }
1183
IsPositionOutValidDisplay(Coordinate2D & position,const DisplayInfo & currentDisplay,bool isPhysicalPos)1184 bool InputWindowsManager::IsPositionOutValidDisplay(
1185 Coordinate2D &position, const DisplayInfo ¤tDisplay, bool isPhysicalPos)
1186 {
1187 int32_t posX = static_cast<int32_t>(position.x);
1188 int32_t posY = static_cast<int32_t>(position.y);
1189 int32_t posWidth = currentDisplay.width;
1190 int32_t posHeight = currentDisplay.height;
1191 int32_t rotateX = posX;
1192 int32_t rotateY = posY;
1193 int32_t validW = currentDisplay.validWidth;
1194 int32_t validH = currentDisplay.validHeight;
1195 int32_t offsetX = 0;
1196 int32_t offsetY = 0;
1197 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
1198 MMI_HILOGD("Start checking vtp cursor active area");
1199 if (IsPointerActiveRectValid(currentDisplay) && !isPhysicalPos) {
1200 validW = currentDisplay.pointerActiveWidth;
1201 validH = currentDisplay.pointerActiveHeight;
1202 MMI_HILOGD("vtp cursor active area w:%{private}d, h:%{private}d", validW, validH);
1203 }
1204 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
1205 if (isPhysicalPos) {
1206 Direction displayDirection = static_cast<Direction>((
1207 ((currentDisplay.direction - currentDisplay.fixedDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
1208 if (displayDirection == DIRECTION90 || displayDirection == DIRECTION270) {
1209 std::swap(validW, validH);
1210 std::swap(posWidth, posHeight);
1211 }
1212 if (currentDisplay.fixedDirection == DIRECTION0) {
1213 rotateX = posX;
1214 rotateY = posY;
1215 } else if (currentDisplay.fixedDirection == DIRECTION90) {
1216 rotateX = posWidth - posY;
1217 rotateY = posX;
1218 } else if (currentDisplay.fixedDirection == DIRECTION180) {
1219 rotateX = posWidth - posX;
1220 rotateY = posHeight - posY;
1221 } else if (currentDisplay.fixedDirection == DIRECTION270) {
1222 rotateX = posY;
1223 rotateY = posHeight - posX;
1224 } else {
1225 MMI_HILOGD("Invalid fixedDirection:%{public}d", currentDisplay.fixedDirection);
1226 }
1227 offsetX = currentDisplay.offsetX;
1228 offsetY = currentDisplay.offsetY;
1229 }
1230 bool isOut = (rotateX < offsetX) || (rotateX > offsetX + validW) ||
1231 (rotateY < offsetY) || (rotateY > offsetY + validH);
1232 PrintDisplayInfo(currentDisplay);
1233 MMI_HILOGD("isOut=%{public}d,isPhysicalPos=%{public}d Position={%{private}d %{private}d}"
1234 "->{%{private}d %{private}d} RealValidWH={w:%{private}d h:%{private}d}",
1235 static_cast<int32_t>(isOut),
1236 static_cast<int32_t>(isPhysicalPos),
1237 posX,
1238 posY,
1239 rotateX,
1240 rotateY,
1241 validW,
1242 validH);
1243
1244 if (!isOut && isPhysicalPos) {
1245 int32_t rotateX1 = rotateX - currentDisplay.offsetX;
1246 int32_t rotateY1 = rotateY - currentDisplay.offsetY;
1247 if (currentDisplay.fixedDirection == DIRECTION0) {
1248 position.x = rotateX1;
1249 position.y = rotateY1;
1250 } else if (currentDisplay.fixedDirection == DIRECTION90) {
1251 position.x = rotateY1;
1252 position.y = posWidth - rotateX1;
1253 } else if (currentDisplay.fixedDirection == DIRECTION180) {
1254 position.x = posWidth - rotateX1;
1255 position.y = posHeight - rotateY1;
1256 } else if (currentDisplay.fixedDirection == DIRECTION270) {
1257 position.x = posHeight - rotateY1;
1258 position.y = rotateX1;
1259 } else {
1260 MMI_HILOGD("Invalid fixedDirection:%{public}d", currentDisplay.fixedDirection);
1261 }
1262 MMI_HILOGD("rerotate={%{private}d %{private}d}->{%{private}f %{private}f} RealValidWH = "
1263 "{w:%{private}d h:%{private}d} RealWH{w:%{private}d h:%{private}d}",
1264 rotateX1,
1265 rotateY1,
1266 position.x,
1267 position.y,
1268 validW,
1269 validH,
1270 posWidth,
1271 posHeight);
1272 }
1273
1274 return isOut;
1275 }
1276
1277 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
IsPointerActiveRectValid(const DisplayInfo & currentDisplay)1278 bool InputWindowsManager::IsPointerActiveRectValid(const DisplayInfo ¤tDisplay)
1279 {
1280 return currentDisplay.pointerActiveWidth > 0 && currentDisplay.pointerActiveHeight > 0;
1281 }
1282 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
1283
CancelTouchScreenEventIfValidDisplayChange(const DisplayGroupInfo & displayGroupInfo)1284 void InputWindowsManager::CancelTouchScreenEventIfValidDisplayChange(const DisplayGroupInfo &displayGroupInfo)
1285 {
1286 if (lastPointerEventforGesture_ == nullptr) {
1287 MMI_HILOGD("lastPointerEventforGesture_ is null");
1288 return;
1289 }
1290 if (lastPointerEventforGesture_->GetSourceType() != PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
1291 MMI_HILOGD("source type:[%{public}d] is not touchscreen", lastPointerEventforGesture_->GetSourceType());
1292 return;
1293 }
1294 int32_t touchDisplayId = lastPointerEventforGesture_->GetTargetDisplayId();
1295 for (auto ¤tDisplay : displayGroupInfo.displaysInfo) {
1296 MMI_HILOGD("touchDisplayId=%{public}d currentDisplay.id=%{public}d", touchDisplayId, currentDisplay.id);
1297 if (touchDisplayId == currentDisplay.id && IsValidDisplayChange(currentDisplay)) {
1298 CancelAllTouches(lastPointerEventforGesture_);
1299 return;
1300 }
1301 }
1302 }
1303
CancelMouseEvent()1304 void InputWindowsManager::CancelMouseEvent()
1305 {
1306 auto lastPointerEventCopy = GetlastPointerEvent();
1307 CHKPV(lastPointerEventCopy);
1308 int32_t action = PointerEvent::POINTER_ACTION_CANCEL;
1309 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1310 action = PointerEvent::POINTER_ACTION_PULL_CANCEL;
1311 }
1312 if (lastPointerEventCopy->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE &&
1313 !lastPointerEventCopy->GetPressedButtons().empty()) {
1314 int32_t pointerId = lastPointerEventCopy->GetPointerId();
1315 int32_t originAction = lastPointerEventCopy->GetPointerAction();
1316 PointerEvent::PointerItem item;
1317 auto isItemExist = lastPointerEventCopy->GetPointerItem(pointerId, item);
1318 if (isItemExist) {
1319 item.SetCanceled(true);
1320 lastPointerEventCopy->UpdatePointerItem(pointerId, item);
1321 }
1322 MMI_HILOGI("Cancel mouse event for valid display change,pointerId:%{public}d action:%{public}d->%{public}d "
1323 "isItemExist=%{public}d",
1324 pointerId,
1325 originAction,
1326 action,
1327 static_cast<uint32_t>(isItemExist));
1328 auto lastPointerEvent = std::make_shared<PointerEvent>(*lastPointerEventCopy);
1329 lastPointerEvent->SetPointerAction(action);
1330 lastPointerEvent->SetOriginPointerAction(originAction);
1331 lastPointerEvent->SetPointerId(pointerId);
1332 auto filter = InputHandler->GetFilterHandler();
1333 CHKPV(filter);
1334 filter->HandlePointerEvent(lastPointerEvent);
1335 }
1336 }
1337
IsValidDisplayChange(const DisplayInfo & displayInfo)1338 bool InputWindowsManager::IsValidDisplayChange(const DisplayInfo &displayInfo)
1339 {
1340 int32_t touchDisplayId = displayInfo.id;
1341 for (auto ¤tDisplay : displayGroupInfo_.displaysInfo) {
1342 if (touchDisplayId == currentDisplay.id) {
1343 auto currentDirection = currentDisplay.direction;
1344 auto currentValidWH =
1345 RotateRect<int32_t>(currentDirection, {currentDisplay.validWidth, currentDisplay.validHeight});
1346 auto newDirection = displayInfo.direction;
1347 auto newValidWH = RotateRect<int32_t>(newDirection, {displayInfo.validWidth, displayInfo.validHeight});
1348 bool isChange =
1349 !(displayInfo.offsetX == currentDisplay.offsetX && displayInfo.offsetY == currentDisplay.offsetY &&
1350 newValidWH.x == currentValidWH.x && newValidWH.y == currentValidWH.y);
1351 MMI_HILOGD("isChange=%{private}d CurDisplayId=%{private}d "
1352 "oldDisplayInfo={{w:%{private}d h:%{private}d} validWH:{%{private}d %{private}d} "
1353 "offsetXY:{%{private}d %{private}d} direction:{%{private}d %{private}d %{private}d}} "
1354 "newDisplayInfo={{w:%{private}d h:%{private}d} validWH:{%{private}d %{private}d}} "
1355 "offsetXY:{%{private}d %{private}d} direction:{%{private}d %{private}d %{private}d}}"
1356 "useDirection:{old:%{private}d new:%{private}d}}",
1357 static_cast<int32_t>(isChange),
1358 touchDisplayId,
1359 currentDisplay.width,
1360 currentDisplay.height,
1361 currentDisplay.validWidth,
1362 currentDisplay.validHeight,
1363 currentDisplay.offsetX,
1364 currentDisplay.offsetY,
1365 currentDisplay.direction,
1366 currentDisplay.displayDirection,
1367 currentDisplay.fixedDirection,
1368 displayInfo.width,
1369 displayInfo.height,
1370 displayInfo.validWidth,
1371 displayInfo.validHeight,
1372 displayInfo.offsetX,
1373 displayInfo.offsetY,
1374 displayInfo.direction,
1375 displayInfo.displayDirection,
1376 displayInfo.fixedDirection,
1377 currentDirection,
1378 newDirection);
1379 return isChange;
1380 }
1381 }
1382 return false;
1383 }
1384
HandleWindowPositionChange()1385 void InputWindowsManager::HandleWindowPositionChange()
1386 {
1387 CALL_DEBUG_ENTER;
1388 PrintWindowNavbar();
1389 for (auto it = touchItemDownInfos_.begin(); it != touchItemDownInfos_.end(); ++it) {
1390 int32_t pointerId = it->first;
1391 int32_t windowId = it->second.window.id;
1392 auto iter = std::find_if(displayGroupInfo_.windowsInfo.begin(), displayGroupInfo_.windowsInfo.end(),
1393 [windowId](const auto& windowInfo) {
1394 return windowId == windowInfo.id && windowInfo.rectChangeBySystem;
1395 });
1396 if (iter != displayGroupInfo_.windowsInfo.end()) {
1397 MMI_HILOGI("Dispatch cancel event pointerId:%{public}d", pointerId);
1398 CHKPV(lastPointerEventforWindowChange_);
1399 PointerEvent::PointerItem pointerItem;
1400 if (!lastPointerEventforWindowChange_->GetPointerItem(pointerId, pointerItem)) {
1401 MMI_HILOGE("Can not find pointer item pointerid:%{public}d", pointerId);
1402 return;
1403 }
1404 auto tmpEvent = std::make_shared<PointerEvent>(*lastPointerEventforWindowChange_);
1405 tmpEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1406 tmpEvent->SetPointerId(pointerId);
1407 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
1408 CHKPV(inputEventNormalizeHandler);
1409 inputEventNormalizeHandler->HandleTouchEvent(tmpEvent);
1410 it->second.flag = true;
1411 iter->rectChangeBySystem = false;
1412 }
1413 }
1414 }
1415
PrintWindowNavbar()1416 void InputWindowsManager::PrintWindowNavbar()
1417 {
1418 for (auto &item : displayGroupInfo_.windowsInfo) {
1419 if (item.windowInputType == WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) {
1420 std::string dump;
1421 dump += StringPrintf("%d|%d|%d|%d|%d|%zu(", item.id, item.area.x, item.area.y, item.area.width,
1422 item.area.height, item.defaultHotAreas.size());
1423 for (const auto &win : item.defaultHotAreas) {
1424 dump += StringPrintf(" %d|%d|%d|%d ", win.x, win.y, win.width, win.height);
1425 }
1426 dump += StringPrintf(")\n");
1427 for (auto it : item.transform) {
1428 dump += StringPrintf("%f,", it);
1429 }
1430 dump += StringPrintf("]\n");
1431 MMI_HILOGI("%{public}s", dump.c_str());
1432 }
1433 }
1434 }
1435
JudgeCaramaInFore()1436 bool InputWindowsManager::JudgeCaramaInFore()
1437 {
1438 int32_t focWid = displayGroupInfo_.focusWindowId;
1439 int32_t focPid = GetPidByWindowId(focWid);
1440 if (udsServer_ == nullptr) {
1441 MMI_HILOGW("The udsServer is nullptr");
1442 return false;
1443 }
1444 SessionPtr sess = udsServer_->GetSessionByPid(focPid);
1445 if (sess == nullptr) {
1446 MMI_HILOGW("The sess is nullptr");
1447 return false;
1448 }
1449 std::string programName = sess->GetProgramName();
1450 return programName.find(".camera") != std::string::npos;
1451 }
1452
UpdateDisplayInfo(DisplayGroupInfo & displayGroupInfo)1453 void InputWindowsManager::UpdateDisplayInfo(DisplayGroupInfo &displayGroupInfo)
1454 {
1455 auto action = UpdateWindowInfo(displayGroupInfo);
1456 CheckFocusWindowChange(displayGroupInfo);
1457 UpdateCaptureMode(displayGroupInfo);
1458 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1459 bool isDisplayChanged = OnDisplayRemovedOrCombiantionChanged(displayGroupInfo);
1460 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1461 {
1462 std::lock_guard<std::mutex> lock(tmpInfoMutex_);
1463 displayGroupInfoTmp_ = displayGroupInfo;
1464 }
1465 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled() || action == WINDOW_UPDATE_ACTION::ADD_END) {
1466 if ((currentUserId_ < 0) || (currentUserId_ == displayGroupInfo.currentUserId)) {
1467 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1468 if (isDisplayChanged) {
1469 ResetPointerPosition(displayGroupInfo);
1470 }
1471 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1472 PrintChangedWindowBySync(displayGroupInfo);
1473 CleanInvalidPiexMap();
1474 HandleValidDisplayChange(displayGroupInfo);
1475 displayGroupInfo_ = displayGroupInfo;
1476 UpdateWindowsInfoPerDisplay(displayGroupInfo);
1477 HandleWindowPositionChange();
1478 }
1479 }
1480 PrintDisplayGroupInfo(displayGroupInfo_);
1481 if (!displayGroupInfo_.displaysInfo.empty()) {
1482 UpdateDisplayIdAndName();
1483 }
1484 UpdateDisplayMode();
1485
1486 #ifdef OHOS_BUILD_ENABLE_POINTER
1487 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1488 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled() &&
1489 (INPUT_DEV_MGR->HasPointerDevice() || INPUT_DEV_MGR->HasVirtualPointerDevice())) {
1490 #else
1491 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1492 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1493 UpdatePointerChangeAreas(displayGroupInfo);
1494 }
1495 InitPointerStyle();
1496 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1497 if (!displayGroupInfo.displaysInfo.empty() && pointerDrawFlag_) {
1498 AdjustDisplayRotation();
1499 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1500 PointerDrawingManagerOnDisplayInfo(displayGroupInfo, isDisplayChanged);
1501 #else
1502 PointerDrawingManagerOnDisplayInfo(displayGroupInfo);
1503 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1504 }
1505 if (displayGroupInfo_.displaysInfo.empty()) {
1506 lastDpi_ = DEFAULT_DPI;
1507 } else {
1508 lastDpi_ = displayGroupInfo_.displaysInfo[0].dpi;
1509 }
1510 if (INPUT_DEV_MGR->HasPointerDevice() && pointerDrawFlag_) {
1511 NotifyPointerToWindow();
1512 }
1513 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1514 #endif // OHOS_BUILD_ENABLE_POINTER
1515 }
1516
1517 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1518 void InputWindowsManager::AdjustDisplayRotation()
1519 {
1520 PhysicalCoordinate coord {
1521 .x = cursorPos_.cursorPos.x,
1522 .y = cursorPos_.cursorPos.y,
1523 };
1524 auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos_.displayId);
1525 CHKPV(displayInfo);
1526 if (cursorPos_.displayDirection != displayInfo->displayDirection) {
1527 MMI_HILOGI("displayId:%{public}d, cursorPosX:%{private}.2f, cursorPosY:%{private}.2f, direction:%{public}d, "
1528 "physicalDisplay id:%{public}d, x:%{private}d, y:%{private}d, width:%{public}d, height:%{public}d, "
1529 "dpi:%{public}d, name:%{public}s, uniq:%{public}s, direction:%{public}d, displayDirection:%{public}d",
1530 cursorPos_.displayId, cursorPos_.cursorPos.x, cursorPos_.cursorPos.y, cursorPos_.direction,
1531 displayInfo->id, displayInfo->x, displayInfo->y, displayInfo->width, displayInfo->height,
1532 displayInfo->dpi, displayInfo->name.c_str(), displayInfo->uniq.c_str(), displayInfo->direction,
1533 displayInfo->displayDirection);
1534 if (cursorPos_.direction == displayInfo->direction) {
1535 ScreenRotateAdjustDisplayXY(*displayInfo, coord);
1536 }
1537 cursorPos_.direction = displayInfo->direction;
1538 cursorPos_.displayDirection = displayInfo->displayDirection;
1539 UpdateAndAdjustMouseLocation(cursorPos_.displayId, coord.x, coord.y);
1540 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*displayInfo);
1541 IPointerDrawingManager::GetInstance()->SetPointerLocation(
1542 static_cast<int32_t>(coord.x), static_cast<int32_t>(coord.y), cursorPos_.displayId);
1543 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1544 if (IsSupported() && extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE)) {
1545 AdjustDragPosition();
1546 }
1547 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
1548 }
1549 }
1550
1551 void InputWindowsManager::AdjustDragPosition()
1552 {
1553 auto lastPointerEvent = GetlastPointerEvent();
1554 CHKPV(lastPointerEvent);
1555 std::shared_ptr<PointerEvent> pointerEvent = std::make_shared<PointerEvent>(*lastPointerEvent);
1556 pointerEvent->SetTargetDisplayId(mouseLocation_.displayId);
1557 auto touchWindow = SelectWindowInfo(mouseLocation_.physicalX, mouseLocation_.physicalY, pointerEvent);
1558 if (touchWindow == std::nullopt) {
1559 MMI_HILOGE("SelectWindowInfo failed");
1560 return;
1561 }
1562 int32_t pointerId = pointerEvent->GetPointerId();
1563 PointerEvent::PointerItem item;
1564 pointerEvent->GetPointerItem(pointerId, item);
1565 item.SetDisplayX(mouseLocation_.physicalX);
1566 item.SetDisplayY(mouseLocation_.physicalY);
1567 pointerEvent->UpdatePointerItem(pointerId, item);
1568 pointerEvent->SetTargetWindowId(touchWindow->id);
1569 pointerEvent->SetAgentWindowId(touchWindow->id);
1570 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_MOVE);
1571 auto now = GetSysClockTime();
1572 pointerEvent->SetActionTime(now);
1573 pointerEvent->UpdateId();
1574 auto filterHandler = InputHandler->GetFilterHandler();
1575 CHKPV(filterHandler);
1576 filterHandler->HandlePointerEvent(pointerEvent);
1577 MMI_HILOGI("pointerEvent: %{public}s", pointerEvent->ToString().c_str());
1578 }
1579 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1580
1581 DisplayMode InputWindowsManager::GetDisplayMode() const
1582 {
1583 return displayMode_;
1584 }
1585
1586 void InputWindowsManager::UpdateDisplayMode()
1587 {
1588 CALL_DEBUG_ENTER;
1589 if (displayGroupInfo_.displaysInfo.empty()) {
1590 MMI_HILOGE("DisplaysInfo is empty");
1591 return;
1592 }
1593 DisplayMode mode = displayGroupInfo_.displaysInfo[0].displayMode;
1594 if (mode == displayMode_) {
1595 MMI_HILOGD("Displaymode not change, mode:%{public}d, diaplayMode_:%{public}d", mode, displayMode_);
1596 return;
1597 }
1598 displayMode_ = mode;
1599 #ifdef OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1600 if (FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_ == nullptr) {
1601 MMI_HILOGD("Send fingersense display mode is nullptr");
1602 return;
1603 }
1604 MMI_HILOGI("Update fingersense display mode, displayMode:%{public}d", displayMode_);
1605 BytraceAdapter::StartUpdateDisplayMode("display mode change");
1606 FINGERSENSE_WRAPPER->sendFingerSenseDisplayMode_(static_cast<int32_t>(displayMode_));
1607 BytraceAdapter::StopUpdateDisplayMode();
1608 #endif // OHOS_BUILD_ENABLE_FINGERSENSE_WRAPPER
1609 }
1610
1611 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1612 void InputWindowsManager::DrawPointer(bool isDisplayRemoved)
1613 {
1614 if (DISPLAY_MONITOR->GetScreenStatus() != EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
1615 if (!isDisplayRemoved) {
1616 IPointerDrawingManager::GetInstance()->DrawPointerStyle(dragPointerStyle_);
1617 } else {
1618 IPointerDrawingManager::GetInstance()->DrawScreenCenterPointer(dragPointerStyle_);
1619 }
1620 }
1621 }
1622
1623 void InputWindowsManager::PointerDrawingManagerOnDisplayInfo(const DisplayGroupInfo &displayGroupInfo,
1624 bool isDisplayRemoved)
1625 {
1626 IPointerDrawingManager::GetInstance()->OnDisplayInfo(displayGroupInfo);
1627 if (displayGroupInfo_.displaysInfo.empty()) {
1628 MMI_HILOGE("displayGroupInfo_ is empty.");
1629 return;
1630 }
1631 if (lastDpi_ != DEFAULT_DPI && lastDpi_ != displayGroupInfo_.displaysInfo[0].dpi) {
1632 auto drawNewDpiRes = IPointerDrawingManager::GetInstance()->DrawNewDpiPointer();
1633 if (drawNewDpiRes != RET_OK) {
1634 MMI_HILOGE("Draw New Dpi pointer failed.");
1635 }
1636 }
1637 auto lastPointerEventCopy = GetlastPointerEvent();
1638 CHKPV(lastPointerEventCopy);
1639 if (INPUT_DEV_MGR->HasPointerDevice() || INPUT_DEV_MGR->HasVirtualPointerDevice()) {
1640 MouseLocation mouseLocation = GetMouseInfo();
1641 int32_t displayId = MouseEventHdr->GetDisplayId();
1642 displayId = displayId < 0 ? displayGroupInfo_.displaysInfo[0].id : displayId;
1643 auto displayInfo = GetPhysicalDisplay(displayId);
1644 CHKPV(displayInfo);
1645 DispatchPointerCancel(displayId);
1646 int32_t logicX = mouseLocation.physicalX + displayInfo->x;
1647 int32_t logicY = mouseLocation.physicalY + displayInfo->y;
1648 lastLogicX_ = logicX;
1649 lastLogicY_ = logicY;
1650 std::optional<WindowInfo> windowInfo;
1651 if (lastPointerEventCopy->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN &&
1652 lastPointerEventCopy->GetPressedButtons().empty()) {
1653 PhysicalCoordinate coord {
1654 .x = logicX,
1655 .y = logicY,
1656 };
1657 if (cursorPos_.direction != displayInfo->direction &&
1658 cursorPos_.displayDirection == displayInfo->displayDirection) {
1659 coord.x = cursorPos_.cursorPos.x;
1660 coord.y = cursorPos_.cursorPos.y;
1661 RotateDisplayScreen(*displayInfo, coord);
1662 }
1663 windowInfo = GetWindowInfo(coord.x, coord.y);
1664 } else {
1665 windowInfo = SelectWindowInfo(logicX, logicY, lastPointerEventCopy);
1666 }
1667 if (windowInfo == std::nullopt && isDisplayRemoved) {
1668 DrawPointer(isDisplayRemoved);
1669 return;
1670 }
1671 CHKFRV(windowInfo, "The windowInfo is nullptr");
1672 int32_t windowPid = GetWindowPid(windowInfo->id);
1673 WinInfo info = { .windowPid = windowPid, .windowId = windowInfo->id };
1674 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1675 PointerStyle pointerStyle;
1676 GetPointerStyle(info.windowPid, info.windowId, pointerStyle);
1677 MMI_HILOGD("Get pointer style, pid:%{public}d, windowid:%{public}d, style:%{public}d",
1678 info.windowPid, info.windowId, pointerStyle.id);
1679 if (!dragFlag_) {
1680 SetMouseFlag(lastPointerEventCopy->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP);
1681 isDragBorder_ = SelectPointerChangeArea(*windowInfo, pointerStyle, logicX, logicY);
1682 dragPointerStyle_ = pointerStyle;
1683 MMI_HILOGD("Not in drag SelectPointerStyle, pointerStyle is:%{public}d", dragPointerStyle_.id);
1684 }
1685 JudgMouseIsDownOrUp(dragFlag_);
1686 if (lastPointerEventCopy->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1687 dragFlag_ = true;
1688 MMI_HILOGD("Is in drag scene");
1689 }
1690 if (lastPointerEventCopy->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
1691 dragFlag_ = false;
1692 isDragBorder_ = false;
1693 }
1694 if (firstBtnDownWindowInfo_.first != displayGroupInfo_.focusWindowId && !extraData_.appended) {
1695 dragPointerStyle_ = pointerStyle;
1696 MMI_HILOGI("Window is changed, pointerStyle is:%{public}d", dragPointerStyle_.id);
1697 }
1698 DrawPointer(isDisplayRemoved);
1699 }
1700 }
1701
1702 void InputWindowsManager::DispatchPointerCancel(int32_t displayId)
1703 {
1704 if (mouseDownInfo_.id < 0 || (extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE))) {
1705 return;
1706 }
1707 auto lastPointerEventCopy = GetlastPointerEvent();
1708 CHKPV(lastPointerEventCopy);
1709 std::optional<WindowInfo> windowInfo;
1710 std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(displayId);
1711 for (const auto &item : windowInfos) {
1712 if (item.id == mouseDownInfo_.id) {
1713 windowInfo = std::make_optional(item);
1714 break;
1715 }
1716 }
1717 if (windowInfo == std::nullopt && displayId != firstBtnDownWindowInfo_.second) {
1718 std::vector<WindowInfo> firstBtnDownWindowsInfo =
1719 GetWindowGroupInfoByDisplayId(firstBtnDownWindowInfo_.second);
1720 for (const auto &item : firstBtnDownWindowsInfo) {
1721 if (item.id == mouseDownInfo_.id) {
1722 windowInfo = std::make_optional(item);
1723 break;
1724 }
1725 }
1726 }
1727 if (windowInfo != std::nullopt) {
1728 return;
1729 }
1730 auto pointerEvent = PointerEvent::Create();
1731 CHKPV(pointerEvent);
1732 pointerEvent->UpdateId();
1733 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), PointerEvent::POINTER_ACTION_CANCEL);
1734 SetPointerEvent(PointerEvent::POINTER_ACTION_CANCEL, pointerEvent);
1735 EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
1736 auto filter = InputHandler->GetFilterHandler();
1737 CHKPV(filter);
1738 filter->HandlePointerEvent(pointerEvent);
1739 }
1740
1741 void InputWindowsManager::UpdatePointerDrawingManagerWindowInfo()
1742 {
1743 auto lastPointerEventCopy = GetlastPointerEvent();
1744 CHKPV(lastPointerEventCopy);
1745 MouseLocation mouseLocation = GetMouseInfo();
1746 int32_t displayId = MouseEventHdr->GetDisplayId();
1747 displayId = displayId < 0 ? displayGroupInfo_.displaysInfo[0].id : displayId;
1748 auto displayInfo = GetPhysicalDisplay(displayId);
1749 CHKPV(displayInfo);
1750 DispatchPointerCancel(displayId);
1751 int32_t logicX = mouseLocation.physicalX + displayInfo->x;
1752 int32_t logicY = mouseLocation.physicalY + displayInfo->y;
1753 lastLogicX_ = logicX;
1754 lastLogicY_ = logicY;
1755 std::optional<WindowInfo> windowInfo;
1756 if (lastPointerEventCopy->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN &&
1757 lastPointerEventCopy->GetPressedButtons().empty()) {
1758 PhysicalCoordinate coord {
1759 .x = logicX,
1760 .y = logicY,
1761 };
1762 if (cursorPos_.direction != displayInfo->direction &&
1763 cursorPos_.displayDirection == displayInfo->displayDirection) {
1764 coord.x = cursorPos_.cursorPos.x;
1765 coord.y = cursorPos_.cursorPos.y;
1766 RotateDisplayScreen(*displayInfo, coord);
1767 }
1768 windowInfo = GetWindowInfo(coord.x, coord.y);
1769 } else {
1770 windowInfo = SelectWindowInfo(logicX, logicY, lastPointerEventCopy);
1771 }
1772 CHKFRV(windowInfo, "The windowInfo is nullptr");
1773 int32_t windowPid = GetWindowPid(windowInfo->id);
1774 WinInfo info = { .windowPid = windowPid, .windowId = windowInfo->id };
1775 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1776 }
1777
1778 void InputWindowsManager::SetPointerEvent(int32_t pointerAction, std::shared_ptr<PointerEvent> pointerEvent)
1779 {
1780 CHKPV(pointerEvent);
1781 PointerEvent::PointerItem lastPointerItem;
1782 auto lastPointerEventCopy = GetlastPointerEvent();
1783 CHKPV(lastPointerEventCopy);
1784 int32_t lastPointerId = lastPointerEventCopy->GetPointerId();
1785 if (!lastPointerEventCopy->GetPointerItem(lastPointerId, lastPointerItem)) {
1786 MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
1787 return;
1788 }
1789 bool checkFlag = lastPointerItem.IsPressed() && lastWindowInfo_.id != mouseDownInfo_.id;
1790 int32_t id = lastWindowInfo_.id;
1791 if (checkFlag) {
1792 id = mouseDownInfo_.id;
1793 }
1794 PointerEvent::PointerItem currentPointerItem;
1795 currentPointerItem.SetWindowX(lastLogicX_- lastWindowInfo_.area.x);
1796 currentPointerItem.SetWindowY(lastLogicY_- lastWindowInfo_.area.y);
1797 currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
1798 currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
1799 currentPointerItem.SetPointerId(0);
1800 pointerEvent->SetTargetDisplayId(lastPointerEventCopy->GetTargetDisplayId());
1801 SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1802 pointerEvent->SetTargetWindowId(id);
1803 pointerEvent->SetAgentWindowId(id);
1804 pointerEvent->SetPointerId(0);
1805 pointerEvent->SetButtonPressed(lastPointerEventCopy->GetButtonId());
1806 pointerEvent->SetButtonId(lastPointerEventCopy->GetButtonId());
1807 pointerEvent->AddPointerItem(currentPointerItem);
1808 pointerEvent->SetPointerAction(pointerAction);
1809 pointerEvent->SetOriginPointerAction(lastPointerEventCopy->GetPointerAction());
1810 pointerEvent->SetSourceType(lastPointerEventCopy->GetSourceType());
1811 int64_t time = GetSysClockTime();
1812 pointerEvent->SetActionTime(time);
1813 pointerEvent->SetActionStartTime(time);
1814 pointerEvent->SetDeviceId(lastPointerEventCopy->GetDeviceId());
1815 }
1816
1817 bool InputWindowsManager::NeedUpdatePointDrawFlag(const std::vector<WindowInfo> &windows)
1818 {
1819 CALL_DEBUG_ENTER;
1820 return !windows.empty() && windows.back().action == WINDOW_UPDATE_ACTION::ADD_END;
1821 }
1822 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1823
1824 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
1825 void InputWindowsManager::SendPointerEvent(int32_t pointerAction)
1826 {
1827 CALL_INFO_TRACE;
1828 CHKPV(udsServer_);
1829 auto pointerEvent = PointerEvent::Create();
1830 CHKPV(pointerEvent);
1831 pointerEvent->UpdateId();
1832 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerAction);
1833 MouseLocation mouseLocation = GetMouseInfo();
1834 auto displayInfo = GetPhysicalDisplay(mouseLocation.displayId);
1835 CHKPV(displayInfo);
1836 lastLogicX_ = mouseLocation.physicalX + displayInfo->x;
1837 lastLogicY_ = mouseLocation.physicalY + displayInfo->y;
1838 if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW ||
1839 Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1840 auto touchWindow = GetWindowInfo(lastLogicX_, lastLogicY_);
1841 if (!touchWindow) {
1842 MMI_HILOGE("TouchWindow is nullptr");
1843 return;
1844 }
1845 lastWindowInfo_ = *touchWindow;
1846 }
1847 PointerEvent::PointerItem pointerItem;
1848 pointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
1849 pointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
1850 pointerItem.SetDisplayX(mouseLocation.physicalX);
1851 pointerItem.SetDisplayY(mouseLocation.physicalY);
1852 pointerItem.SetPointerId(0);
1853
1854 pointerEvent->SetTargetDisplayId(-1);
1855 auto displayId = pointerEvent->GetTargetDisplayId();
1856 if (!UpdateDisplayId(displayId)) {
1857 MMI_HILOGE("This display:%{public}d is not existent", displayId);
1858 return;
1859 }
1860 pointerEvent->SetTargetDisplayId(displayId);
1861 SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1862 pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
1863 pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
1864 pointerEvent->SetPointerId(0);
1865 pointerEvent->AddPointerItem(pointerItem);
1866 pointerEvent->SetPointerAction(pointerAction);
1867 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
1868 int64_t time = GetSysClockTime();
1869 pointerEvent->SetActionTime(time);
1870 pointerEvent->SetActionStartTime(time);
1871 pointerEvent->UpdateId();
1872 LogTracer lt1(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
1873 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1874 pointerEvent->SetBuffer(extraData_.buffer);
1875 pointerEvent->SetPullId(extraData_.pullId);
1876 UpdatePointerAction(pointerEvent);
1877 } else {
1878 pointerEvent->ClearBuffer();
1879 }
1880 auto filter = InputHandler->GetFilterHandler();
1881 CHKPV(filter);
1882 filter->HandlePointerEvent(pointerEvent);
1883 }
1884
1885 void InputWindowsManager::DispatchPointer(int32_t pointerAction, int32_t windowId)
1886 {
1887 CALL_INFO_TRACE;
1888 CHKPV(udsServer_);
1889 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
1890 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState() && !HasMouseHideFlag()) {
1891 MMI_HILOGI("The mouse is hide");
1892 return;
1893 }
1894 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
1895 auto lastPointerEventCopy = GetlastPointerEvent();
1896 if (lastPointerEventCopy == nullptr) {
1897 SendPointerEvent(pointerAction);
1898 return;
1899 }
1900 auto pointerEvent = PointerEvent::Create();
1901 CHKPV(pointerEvent);
1902 pointerEvent->UpdateId();
1903 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerAction);
1904 PointerEvent::PointerItem lastPointerItem;
1905 int32_t lastPointerId = lastPointerEventCopy->GetPointerId();
1906 if (!lastPointerEventCopy->GetPointerItem(lastPointerId, lastPointerItem)) {
1907 MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
1908 return;
1909 }
1910 if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW && windowId <= 0) {
1911 std::optional<WindowInfo> windowInfo;
1912 int32_t eventAction = lastPointerEventCopy->GetPointerAction();
1913 bool checkFlag = (eventAction == PointerEvent::POINTER_ACTION_MOVE &&
1914 lastPointerEventCopy->GetPressedButtons().empty()) ||
1915 (eventAction >= PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
1916 eventAction <= PointerEvent::POINTER_ACTION_AXIS_END);
1917 if (checkFlag) {
1918 windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
1919 } else {
1920 windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEventCopy);
1921 }
1922 if (!windowInfo) {
1923 MMI_HILOGE("windowInfo is nullptr");
1924 return;
1925 }
1926 if (windowInfo->id != lastWindowInfo_.id) {
1927 lastWindowInfo_ = *windowInfo;
1928 }
1929 }
1930 PointerEvent::PointerItem currentPointerItem;
1931 currentPointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
1932 currentPointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
1933 if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW && windowId > 0) {
1934 currentPointerItem.SetDisplayX(mouseLocation_.physicalX);
1935 currentPointerItem.SetDisplayY(mouseLocation_.physicalY);
1936 pointerEvent->SetTargetDisplayId(mouseLocation_.displayId);
1937 } else {
1938 if (IsMouseSimulate()) {
1939 currentPointerItem.SetWindowX(lastPointerItem.GetWindowX());
1940 currentPointerItem.SetWindowY(lastPointerItem.GetWindowY());
1941 }
1942 currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
1943 currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
1944 pointerEvent->SetTargetDisplayId(lastPointerEventCopy->GetTargetDisplayId());
1945 }
1946 currentPointerItem.SetPointerId(0);
1947
1948 SetPrivacyModeFlag(lastWindowInfo_.privacyMode, pointerEvent);
1949 pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
1950 pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
1951 pointerEvent->SetPointerId(0);
1952 pointerEvent->AddPointerItem(currentPointerItem);
1953 pointerEvent->SetPointerAction(pointerAction);
1954 pointerEvent->SetSourceType(lastPointerEventCopy->GetSourceType());
1955 int64_t time = GetSysClockTime();
1956 pointerEvent->SetActionTime(time);
1957 pointerEvent->SetActionStartTime(time);
1958 pointerEvent->SetDeviceId(lastPointerEventCopy->GetDeviceId());
1959 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1960 pointerEvent->SetBuffer(extraData_.buffer);
1961 pointerEvent->SetPullId(extraData_.pullId);
1962 UpdatePointerAction(pointerEvent);
1963 } else {
1964 pointerEvent->ClearBuffer();
1965 }
1966 if (pointerAction == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
1967 pointerEvent->SetAgentWindowId(lastWindowInfo_.id);
1968 }
1969 PrintEnterEventInfo(pointerEvent);
1970 EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
1971 #ifdef OHOS_BUILD_ENABLE_POINTER
1972 auto filter = InputHandler->GetFilterHandler();
1973 filter->HandlePointerEvent(pointerEvent);
1974 #endif // OHOS_BUILD_ENABLE_POINTER
1975 }
1976
1977 void InputWindowsManager::PrintEnterEventInfo(std::shared_ptr<PointerEvent> pointerEvent)
1978 {
1979 int32_t pointerAc = pointerEvent->GetPointerAction();
1980 if (pointerAc == PointerEvent::POINTER_ACTION_LEAVE_WINDOW &&
1981 pointerEvent->GetSourceType() != PointerEvent::SOURCE_TYPE_MOUSE) {
1982 auto device = INPUT_DEV_MGR->GetInputDevice(pointerEvent->GetDeviceId());
1983 CHKPV(device);
1984 MMI_HILOGE("leave-window type:%{public}d,id:%{public}d,pointerid:%{public}d,action:%{public}d by:%{public}s",
1985 pointerEvent->GetSourceType(), pointerEvent->GetId(), pointerEvent->GetPointerId(),
1986 pointerAc, device->GetName().c_str());
1987 }
1988 }
1989 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1990
1991 #ifdef OHOS_BUILD_ENABLE_POINTER
1992 void InputWindowsManager::NotifyPointerToWindow()
1993 {
1994 CALL_DEBUG_ENTER;
1995 std::optional<WindowInfo> windowInfo;
1996 auto lastPointerEventCopy = GetlastPointerEvent();
1997 CHKPV(lastPointerEventCopy);
1998 if ((lastPointerEventCopy->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) &&
1999 (!lastPointerEventCopy->GetPressedButtons().empty())) {
2000 MMI_HILOGD("No need to respond to new interface layouts, btnCounts:%{public}d",
2001 static_cast<int32_t>(lastPointerEventCopy->GetPressedButtons().size()));
2002 return;
2003 }
2004 windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
2005 if (!windowInfo) {
2006 MMI_HILOGE("The windowInfo is nullptr");
2007 return;
2008 }
2009 if (windowInfo->id == lastWindowInfo_.id) {
2010 MMI_HILOGI("The mouse pointer does not leave the window:%{public}d", lastWindowInfo_.id);
2011 lastWindowInfo_ = *windowInfo;
2012 return;
2013 }
2014 bool isFindLastWindow = false;
2015 for (const auto &item : displayGroupInfo_.windowsInfo) {
2016 if (item.id == lastWindowInfo_.id) {
2017 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
2018 isFindLastWindow = true;
2019 break;
2020 }
2021 }
2022 if (!isFindLastWindow) {
2023 if (udsServer_ != nullptr && udsServer_->GetClientFd(lastWindowInfo_.pid) != INVALID_FD) {
2024 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
2025 }
2026 }
2027 lastWindowInfo_ = *windowInfo;
2028 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
2029 }
2030 #endif // OHOS_BUILD_ENABLE_POINTER
2031
2032 void InputWindowsManager::PrintHighZorder(const std::vector<WindowInfo> &windowsInfo, int32_t pointerAction,
2033 int32_t targetWindowId, int32_t logicalX, int32_t logicalY)
2034 {
2035 std::optional<WindowInfo> info = GetWindowInfoById(targetWindowId);
2036 if (!info) {
2037 return;
2038 }
2039 WindowInfo targetWindow = *info;
2040 bool isPrint = false;
2041 std::string windowPrint;
2042 windowPrint += StringPrintf("highZorder");
2043 for (const auto &windowInfo : windowsInfo) {
2044 if (MMI_GNE(windowInfo.zOrder, targetWindow.zOrder) && !windowInfo.flags &&
2045 pointerAction == PointerEvent::POINTER_ACTION_AXIS_BEGIN &&
2046 windowInfo.windowInputType != WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE &&
2047 windowInfo.windowInputType != WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE &&
2048 windowInfo.windowInputType != WindowInputType::TRANSMIT_ALL) {
2049 if (IsInHotArea(logicalX, logicalY, windowInfo.pointerHotAreas, windowInfo)) {
2050 PrintZorderInfo(windowInfo, windowPrint);
2051 isPrint = true;
2052 }
2053 }
2054 }
2055 if (isPrint) {
2056 MMI_HILOGW("%{public}s", windowPrint.c_str());
2057 }
2058 }
2059
2060 void InputWindowsManager::PrintZorderInfo(const WindowInfo &windowInfo, std::string &windowPrint)
2061 {
2062 windowPrint += StringPrintf("|");
2063 windowPrint += StringPrintf("%d", windowInfo.id);
2064 windowPrint += StringPrintf("|");
2065 windowPrint += StringPrintf("%d", windowInfo.pid);
2066 windowPrint += StringPrintf("|");
2067 windowPrint += StringPrintf("%.2f", windowInfo.zOrder);
2068 windowPrint += StringPrintf("|");
2069 for (const auto &win : windowInfo.defaultHotAreas) {
2070 windowPrint += StringPrintf("%d ", win.x);
2071 windowPrint += StringPrintf("%d ", win.y);
2072 windowPrint += StringPrintf("%d ", win.width);
2073 windowPrint += StringPrintf("%d,", win.height);
2074 }
2075 windowPrint += StringPrintf("|");
2076 for (auto it : windowInfo.transform) {
2077 windowPrint += StringPrintf("%.2f,", it);
2078 }
2079 windowPrint += StringPrintf("|");
2080 }
2081
2082 void InputWindowsManager::PrintWindowInfo(const std::vector<WindowInfo> &windowsInfo)
2083 {
2084 std::string window;
2085 window += StringPrintf("windowId:[");
2086 for (const auto &item : windowsInfo) {
2087 MMI_HILOGD("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
2088 "area.x:%d,area.y:%d,area.width:%{public}d,area.height:%{public}d,"
2089 "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
2090 "agentWindowId:%{public}d,flags:%{public}d,action:%{public}d,displayId:%{public}d,"
2091 "zOrder:%{public}f, privacyMode:%{public}d",
2092 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
2093 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
2094 item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder, item.privacyMode);
2095 for (const auto &win : item.defaultHotAreas) {
2096 MMI_HILOGD("defaultHotAreas:x:%d, y:%d, width:%{public}d, height:%{public}d",
2097 win.x, win.y, win.width, win.height);
2098 }
2099 for (const auto &pointer : item.pointerHotAreas) {
2100 MMI_HILOGD("pointerHotAreas:x:%d, y:%d, width:%{public}d, height:%{public}d",
2101 pointer.x, pointer.y, pointer.width, pointer.height);
2102 }
2103
2104 window += StringPrintf("%d,", item.id);
2105 std::string dump;
2106 dump += StringPrintf("pointChangeAreas:[");
2107 for (const auto &it : item.pointerChangeAreas) {
2108 dump += StringPrintf("%d,", it);
2109 }
2110 dump += StringPrintf("]\n");
2111
2112 dump += StringPrintf("transform:[");
2113 for (const auto &it : item.transform) {
2114 dump += StringPrintf("%f,", it);
2115 }
2116 dump += StringPrintf("]\n");
2117 std::istringstream stream(dump);
2118 std::string line;
2119 while (std::getline(stream, line, '\n')) {
2120 MMI_HILOGD("%{public}s", line.c_str());
2121 }
2122 if (!item.uiExtentionWindowInfo.empty()) {
2123 PrintWindowInfo(item.uiExtentionWindowInfo);
2124 }
2125 }
2126 window += StringPrintf("]\n");
2127 MMI_HILOGI("%{public}s", window.c_str());
2128 }
2129
2130 void InputWindowsManager::PrintWindowGroupInfo(const WindowGroupInfo &windowGroupInfo)
2131 {
2132 if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
2133 return;
2134 }
2135 MMI_HILOGD("windowsGroupInfo,focusWindowId:%{public}d,displayId:%{public}d",
2136 windowGroupInfo.focusWindowId, windowGroupInfo.displayId);
2137 PrintWindowInfo(windowGroupInfo.windowsInfo);
2138 }
2139
2140 void InputWindowsManager::PrintDisplayGroupInfo(const DisplayGroupInfo displayGroupInfo)
2141 {
2142 if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
2143 return;
2144 }
2145 MMI_HILOGD("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d,"
2146 "windowsInfosNum:%{public}zu,displayInfosNum:%{public}zu",
2147 displayGroupInfo.width,
2148 displayGroupInfo.height,
2149 displayGroupInfo.focusWindowId,
2150 displayGroupInfo.windowsInfo.size(),
2151 displayGroupInfo.displaysInfo.size());
2152 PrintWindowInfo(displayGroupInfo.windowsInfo);
2153 for (const auto &item : displayGroupInfo.displaysInfo) {
2154 PrintDisplayInfo(item);
2155 }
2156 }
2157
2158 void InputWindowsManager::PrintDisplayInfo(const DisplayInfo displayInfo)
2159 {
2160 if (!HiLogIsLoggable(MMI_LOG_DOMAIN, MMI_LOG_TAG, LOG_DEBUG)) {
2161 return;
2162 }
2163 MMI_HILOGD("displayInfo{id:%{public}d,name:%{public}s,uniq:%{public}s "
2164 "XY:{%{private}d %{private}d} offsetXY:{%{private}d %{private}d} "
2165 "WH:{%{private}d %{private}d} validWH:{%{private}d %{private}d} "
2166 "direction:%{public}d,displayDirection:%{public}d,fixedDirection:%{public}d} "
2167 "oneHandXY:{%{private}d %{private}d},"
2168 "pointerActiveWidth:%{private}d,pointerActiveHeight:%{private}d",
2169 displayInfo.id,
2170 displayInfo.name.c_str(),
2171 displayInfo.uniq.c_str(),
2172 displayInfo.x,
2173 displayInfo.y,
2174 displayInfo.offsetX,
2175 displayInfo.offsetY,
2176 displayInfo.width,
2177 displayInfo.height,
2178 displayInfo.validWidth,
2179 displayInfo.validHeight,
2180 displayInfo.direction,
2181 displayInfo.displayDirection,
2182 displayInfo.fixedDirection,
2183 displayInfo.oneHandX,
2184 displayInfo.oneHandY,
2185 displayInfo.pointerActiveWidth,
2186 displayInfo.pointerActiveHeight);
2187 }
2188
2189 const DisplayInfo* InputWindowsManager::GetPhysicalDisplay(int32_t id) const
2190 {
2191 for (const auto &it : displayGroupInfo_.displaysInfo) {
2192 if (it.id == id) {
2193 return ⁢
2194 }
2195 }
2196 MMI_HILOGW("Failed to obtain physical(%{public}d) display", id);
2197 return nullptr;
2198 }
2199
2200 #ifdef OHOS_BUILD_ENABLE_TOUCH
2201 const DisplayInfo* InputWindowsManager::FindPhysicalDisplayInfo(const std::string& uniq) const
2202 {
2203 for (const auto &it : displayGroupInfo_.displaysInfo) {
2204 if (it.uniq == uniq) {
2205 return ⁢
2206 }
2207 }
2208 MMI_HILOGD("Failed to search for Physical,uniq:%{public}s", uniq.c_str());
2209 if (displayGroupInfo_.displaysInfo.size() > 0) {
2210 return &displayGroupInfo_.displaysInfo[0];
2211 }
2212 return nullptr;
2213 }
2214
2215 const DisplayInfo *InputWindowsManager::GetDefaultDisplayInfo() const
2216 {
2217 return FindPhysicalDisplayInfo("default0");
2218 }
2219 #endif // OHOS_BUILD_ENABLE_TOUCH
2220
2221 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2222 void InputWindowsManager::ScreenRotateAdjustDisplayXY(const DisplayInfo& info, PhysicalCoordinate& coord) const
2223 {
2224 Direction rotation = info.direction;
2225 Direction lastRotation = cursorPos_.direction;
2226 int32_t width = info.validWidth;
2227 int32_t height = info.validHeight;
2228 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled() &&
2229 (rotation == DIRECTION90 || rotation == DIRECTION270)) {
2230 height = info.validWidth;
2231 width = info.validHeight;
2232 }
2233 if ((static_cast<int32_t>(lastRotation) + 1) % 4 == static_cast<int32_t>(rotation)) {
2234 double temp = coord.x;
2235 coord.x = width - coord.y;
2236 coord.y = temp;
2237 } else if ((static_cast<int32_t>(lastRotation) + 2) % 4 == static_cast<int32_t>(rotation)) {
2238 coord.x = width - coord.x;
2239 coord.y = height - coord.y;
2240 } else {
2241 double temp = coord.y;
2242 coord.y = height -coord.x;
2243 coord.x = temp;
2244 }
2245 }
2246
2247 void InputWindowsManager::RotateScreen(const DisplayInfo& info, PhysicalCoordinate& coord) const
2248 {
2249 double oldX = coord.x;
2250 double oldY = coord.y;
2251 const Direction direction = info.direction;
2252 if (direction == DIRECTION0) {
2253 if (cursorPos_.displayDirection != info.displayDirection && cursorPos_.direction != info.direction) {
2254 if (cursorPos_.direction == Direction::DIRECTION90) {
2255 double temp = coord.y;
2256 coord.y = info.validHeight - coord.x;
2257 coord.x = temp;
2258 } else if (cursorPos_.direction == Direction::DIRECTION270) {
2259 double temp = coord.x;
2260 coord.x = info.validWidth - coord.y;
2261 coord.y = temp;
2262 }
2263 }
2264 MMI_HILOGD("DIRECTION0, physicalXY:{%f %f}->{%f %f}", oldX, oldY, coord.x, coord.y);
2265 return;
2266 }
2267 if (direction == DIRECTION90) {
2268 double temp = coord.x;
2269 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2270 coord.x = info.validHeight - coord.y;
2271 } else {
2272 coord.x = info.validWidth - coord.y;
2273 }
2274 coord.y = temp;
2275 MMI_HILOGD("DIRECTION90, physicalXY:{%f %f}->{%f %f}", oldX, oldY, coord.x, coord.y);
2276 return;
2277 }
2278 if (direction == DIRECTION180) {
2279 coord.x = info.validWidth - coord.x;
2280 coord.y = info.validHeight - coord.y;
2281 MMI_HILOGD("DIRECTION180, physicalXY:{%f %f}->{%f %f}", oldX, oldY, coord.x, coord.y);
2282 return;
2283 }
2284 if (direction == DIRECTION270) {
2285 double temp = coord.y;
2286 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2287 coord.y = info.validWidth - coord.x;
2288 } else {
2289 coord.y = info.validHeight - coord.x;
2290 }
2291 coord.x = temp;
2292 MMI_HILOGD("DIRECTION270, physicalXY:{%f %f}->{%f %f}", oldX, oldY, coord.x, coord.y);
2293 }
2294 }
2295
2296 void InputWindowsManager::RotateDisplayScreen(const DisplayInfo& info, PhysicalCoordinate& coord)
2297 {
2298 Direction displayDirection = static_cast<Direction>(
2299 (((info.direction - info.displayDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
2300 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
2301 if (IsSupported()) {
2302 displayDirection = info.direction;
2303 }
2304 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
2305 bool isEnable = Rosen::SceneBoardJudgement::IsSceneBoardEnabled();
2306 double oldX = coord.x;
2307 double oldY = coord.y;
2308 if (displayDirection == DIRECTION0) {
2309 MMI_HILOGD("DIRECTION0, IsSceneBoardEnabled:%d physicalXY:{%f,%f}", isEnable, oldX, oldY);
2310 return;
2311 }
2312 if (displayDirection == DIRECTION90) {
2313 double temp = coord.x;
2314 if (!isEnable) {
2315 coord.x = info.validHeight - coord.y;
2316 } else {
2317 coord.x = info.validWidth - coord.y;
2318 }
2319 coord.y = temp;
2320 MMI_HILOGD(
2321 "DIRECTION90, IsSceneBoardEnabled:%d physicalXY:{%f,%f}->{%f,%f}", isEnable, oldX, oldY, coord.x, coord.y);
2322 return;
2323 }
2324 if (displayDirection == DIRECTION180) {
2325 coord.x = info.validWidth - coord.x;
2326 coord.y = info.validHeight - coord.y;
2327 MMI_HILOGD(
2328 "DIRECTION180, IsSceneBoardEnabled:%d physicalXY:{%f,%f}->{%f,%f}", isEnable, oldX, oldY, coord.x, coord.y);
2329 return;
2330 }
2331 if (displayDirection == DIRECTION270) {
2332 double temp = coord.y;
2333 if (!isEnable) {
2334 coord.y = info.validWidth - coord.x;
2335 } else {
2336 coord.y = info.validHeight - coord.x;
2337 }
2338 coord.x = temp;
2339 MMI_HILOGD(
2340 "DIRECTION270, IsSceneBoardEnabled:%d physicalXY:{%f,%f}->{%f,%f}", isEnable, oldX, oldY, coord.x, coord.y);
2341 }
2342 }
2343 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2344
2345 #ifdef OHOS_BUILD_ENABLE_TOUCH
2346 bool InputWindowsManager::GetPhysicalDisplayCoord(struct libinput_event_touch* touch,
2347 const DisplayInfo& info, EventTouch& touchInfo, bool isNeedClear)
2348 {
2349 PrintDisplayInfo(info);
2350 auto width = info.width;
2351 auto height = info.height;
2352 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2353 if (info.direction == DIRECTION90 || info.direction == DIRECTION270) {
2354 width = info.height;
2355 height = info.width;
2356 }
2357 }
2358 PhysicalCoordinate coord {
2359 .x = libinput_event_touch_get_x_transformed(touch, width),
2360 .y = libinput_event_touch_get_y_transformed(touch, height - info.expandHeight),
2361 };
2362 MMI_HILOGD("width:%{private}d, height:%{private}d, physicalX:%{private}f, physicalY:%{private}f",
2363 width, height, coord.x, coord.y);
2364 Coordinate2D pos = { .x = coord.x, .y = coord.y };
2365 if (IsPositionOutValidDisplay(pos, info, true)) {
2366 MMI_HILOGD("The position is out of the valid display");
2367 if (isNeedClear) {
2368 int32_t seatSlot = libinput_event_touch_get_seat_slot(touch);
2369 TriggerTouchUpOnInvalidAreaEntry(seatSlot);
2370 }
2371 return false;
2372 }
2373 MMI_HILOGD("IsPositionOutValidDisplay physicalXY:{%{private}f %{private}f}->{%{private}f %{private}f}",
2374 coord.x, coord.y, pos.x, pos.y);
2375 coord.x = pos.x;
2376 coord.y = pos.y;
2377 RotateScreen(info, coord);
2378 touchInfo.point.x = static_cast<int32_t>(coord.x);
2379 touchInfo.point.y = static_cast<int32_t>(coord.y);
2380 touchInfo.toolRect.point.x = static_cast<int32_t>(libinput_event_touch_get_tool_x_transformed(touch, width));
2381 touchInfo.toolRect.point.y =
2382 static_cast<int32_t>(libinput_event_touch_get_tool_y_transformed(touch, height - info.expandHeight));
2383 touchInfo.toolRect.width = static_cast<int32_t>(
2384 libinput_event_touch_get_tool_width_transformed(touch, width));
2385 touchInfo.toolRect.height = static_cast<int32_t>(
2386 libinput_event_touch_get_tool_height_transformed(touch, height));
2387 return true;
2388 }
2389
2390 // When the finger moves out of the active area, the touch up event is triggered
2391 void InputWindowsManager::TriggerTouchUpOnInvalidAreaEntry(int32_t pointerId)
2392 {
2393 if (lastPointerEventforGesture_ == nullptr) {
2394 MMI_HILOGE("lastPointerEventforGesture_ is null");
2395 return;
2396 }
2397 PointerEvent::PointerItem item;
2398 if (!(lastPointerEventforGesture_->GetPointerItem(pointerId, item))) {
2399 MMI_HILOGE("Get pointer item failed, pointerId:%{public}d", pointerId);
2400 return;
2401 }
2402 // Make sure to trigger touch up the first time out of the valid area
2403 if ((!item.IsCanceled()) && item.IsPressed()) {
2404 auto pointerEvent = std::make_shared<PointerEvent>(*lastPointerEventforGesture_);
2405 int32_t originAction = pointerEvent->GetPointerAction();
2406 pointerEvent->SetOriginPointerAction(originAction);
2407 int32_t action = PointerEvent::POINTER_ACTION_UP;
2408 bool isDragging = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
2409 (item.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId);
2410 if (isDragging) {
2411 action = PointerEvent::POINTER_ACTION_PULL_UP;
2412 }
2413 pointerEvent->SetPointerAction(action);
2414 pointerEvent->SetPointerId(pointerId);
2415 auto now = GetSysClockTime();
2416 pointerEvent->SetActionTime(now);
2417 pointerEvent->SetTargetWindowId(item.GetTargetWindowId());
2418 auto winOpt = GetWindowAndDisplayInfo(item.GetTargetWindowId(), pointerEvent->GetTargetDisplayId());
2419 if (winOpt) {
2420 pointerEvent->SetAgentWindowId(winOpt->agentWindowId);
2421 }
2422 pointerEvent->UpdateId();
2423 auto eventDispatchHandler = InputHandler->GetEventDispatchHandler();
2424 CHKPV(eventDispatchHandler);
2425 eventDispatchHandler->HandleTouchEvent(pointerEvent);
2426 MMI_HILOGI("Trigger touch up, pointerId:%{public}d, pointerAction:%{public}d", pointerId, action);
2427
2428 // Flag event have been cleaned up
2429 item.SetCanceled(true);
2430 lastPointerEventforGesture_->UpdatePointerItem(pointerId, item);
2431 }
2432 }
2433
2434 void InputWindowsManager::SetAntiMisTake(bool state)
2435 {
2436 antiMistake_.isOpen = state;
2437 }
2438
2439 void InputWindowsManager::SetAntiMisTakeStatus(bool state)
2440 {
2441 isOpenAntiMisTakeObserver_ = state;
2442 }
2443
2444 bool InputWindowsManager::TouchPointToDisplayPoint(int32_t deviceId, struct libinput_event_touch* touch,
2445 EventTouch& touchInfo, int32_t& physicalDisplayId, bool isNeedClear)
2446 {
2447 CHKPF(touch);
2448 std::string screenId = bindInfo_.GetBindDisplayNameByInputDevice(deviceId);
2449 if (screenId.empty()) {
2450 screenId = "default0";
2451 }
2452 auto info = FindPhysicalDisplayInfo(screenId);
2453 CHKPF(info);
2454 physicalDisplayId = info->id;
2455 if ((info->width <= 0) || (info->height <= 0)) {
2456 MMI_HILOGE("Get DisplayInfo is error");
2457 return false;
2458 }
2459 return GetPhysicalDisplayCoord(touch, *info, touchInfo, isNeedClear);
2460 }
2461
2462 bool InputWindowsManager::TransformTipPoint(struct libinput_event_tablet_tool* tip,
2463 PhysicalCoordinate& coord, int32_t& displayId) const
2464 {
2465 CHKPF(tip);
2466 auto displayInfo = FindPhysicalDisplayInfo("default0");
2467 CHKPF(displayInfo);
2468 MMI_HILOGD("PhysicalDisplay.width:%{public}d, PhysicalDisplay.height:%{public}d, "
2469 "PhysicalDisplay.topLeftX:%{public}d, PhysicalDisplay.topLeftY:%{public}d",
2470 displayInfo->width, displayInfo->height, displayInfo->x, displayInfo->y);
2471 displayId = displayInfo->id;
2472 auto width = displayInfo->width;
2473 auto height = displayInfo->height;
2474 if (displayInfo->direction == DIRECTION90 || displayInfo->direction == DIRECTION270) {
2475 width = displayInfo->height;
2476 height = displayInfo->width;
2477 }
2478 PhysicalCoordinate phys {
2479 .x = libinput_event_tablet_tool_get_x_transformed(tip, width),
2480 .y = libinput_event_tablet_tool_get_y_transformed(tip, height),
2481 };
2482 RotateScreen(*displayInfo, phys);
2483 coord.x = phys.x;
2484 coord.y = phys.y;
2485 MMI_HILOGD("physicalX:%{private}f, physicalY:%{private}f, displayId:%{public}d", phys.x, phys.y, displayId);
2486 return true;
2487 }
2488
2489 bool InputWindowsManager::CalculateTipPoint(struct libinput_event_tablet_tool* tip,
2490 int32_t& targetDisplayId, PhysicalCoordinate& coord) const
2491 {
2492 CHKPF(tip);
2493 if (!TransformTipPoint(tip, coord, targetDisplayId)) {
2494 return false;
2495 }
2496 return true;
2497 }
2498 #endif // OHOS_BUILD_ENABLE_TOUCH
2499
2500 #ifdef OHOS_BUILD_ENABLE_POINTER
2501 const DisplayGroupInfo& InputWindowsManager::GetDisplayGroupInfo()
2502 {
2503 return displayGroupInfo_;
2504 }
2505
2506 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
2507 bool InputWindowsManager::IsNeedRefreshLayer(int32_t windowId)
2508 {
2509 CALL_DEBUG_ENTER;
2510 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2511 return true;
2512 }
2513 MouseLocation mouseLocation = GetMouseInfo();
2514 int32_t displayId = MouseEventHdr->GetDisplayId();
2515 if (displayId < 0) {
2516 displayId = displayGroupInfo_.displaysInfo[0].id;
2517 }
2518 auto displayInfo = GetPhysicalDisplay(displayId);
2519 CHKPF(displayInfo);
2520 int32_t logicX = mouseLocation.physicalX + displayInfo->x;
2521 int32_t logicY = mouseLocation.physicalY + displayInfo->y;
2522 std::optional<WindowInfo> touchWindow = GetWindowInfo(logicX, logicY);
2523 if (!touchWindow) {
2524 MMI_HILOGE("TouchWindow is nullptr");
2525 return false;
2526 }
2527 if (touchWindow->id == windowId || windowId == GLOBAL_WINDOW_ID) {
2528 MMI_HILOGD("Need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
2529 touchWindow->id, windowId);
2530 return true;
2531 }
2532 MMI_HILOGD("Not need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
2533 touchWindow->id, windowId);
2534 return false;
2535 }
2536 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
2537
2538 void InputWindowsManager::OnSessionLost(SessionPtr session)
2539 {
2540 CALL_DEBUG_ENTER;
2541 CHKPV(session);
2542 int32_t pid = session->GetPid();
2543 IPointerDrawingManager::GetInstance()->OnSessionLost(pid);
2544 auto it = pointerStyle_.find(pid);
2545 if (it != pointerStyle_.end()) {
2546 pointerStyle_.erase(it);
2547 MMI_HILOGD("Clear the pointer style map, pd:%{public}d", pid);
2548 }
2549 }
2550 #endif // OHOS_BUILD_ENABLE_POINTER
2551
2552 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2553 int32_t InputWindowsManager::UpdatePoinerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
2554 {
2555 CALL_DEBUG_ENTER;
2556 auto it = pointerStyle_.find(pid);
2557 if (it == pointerStyle_.end()) {
2558 MMI_HILOG_CURSORE("The pointer style map is not include param pd:%{public}d", pid);
2559 return COMMON_PARAMETER_ERROR;
2560 }
2561 auto iter = it->second.find(windowId);
2562 if (iter != it->second.end()) {
2563 iter->second = pointerStyle;
2564 return RET_OK;
2565 }
2566
2567 auto [iterator, sign] = it->second.insert(std::make_pair(windowId, pointerStyle));
2568 if (!sign) {
2569 MMI_HILOG_CURSORW("The window type is duplicated");
2570 return COMMON_PARAMETER_ERROR;
2571 }
2572 return RET_OK;
2573 }
2574
2575 int32_t InputWindowsManager::UpdateSceneBoardPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
2576 bool isUiExtension)
2577 {
2578 CALL_DEBUG_ENTER;
2579 auto scenePid = pid;
2580 auto sceneWinId = windowId;
2581 if (isUiExtension) {
2582 auto iter = uiExtensionPointerStyle_.find(scenePid);
2583 if (iter == uiExtensionPointerStyle_.end() || iter->second.find(sceneWinId) == iter->second.end()) {
2584 uiExtensionPointerStyle_[scenePid] = {};
2585 MMI_HILOG_CURSORE("SceneBoardPid %{public}d or windowId:%{public}d does not exist on"
2586 "uiExtensionPointerStyle_", scenePid, sceneWinId);
2587 }
2588 uiExtensionPointerStyle_[scenePid][sceneWinId] = pointerStyle;
2589 MMI_HILOG_CURSORI("set uiextension pointer success. pid:%{public}d, windowid:%{public}d, pointerid:%{public}d",
2590 scenePid, sceneWinId, pointerStyle.id);
2591 return RET_OK;
2592 }
2593 auto sceneIter = pointerStyle_.find(scenePid);
2594 if (sceneIter == pointerStyle_.end() || sceneIter->second.find(sceneWinId) == sceneIter->second.end()) {
2595 if (sceneIter->second.size() > POINTER_STYLE_WINDOW_NUM) {
2596 pointerStyle_[scenePid] = {};
2597 MMI_HILOG_CURSORE("SceneBoardPid %{public}d windowId:%{public}d exceed",
2598 scenePid, sceneWinId);
2599 }
2600 }
2601 pointerStyle_[scenePid][sceneWinId] = pointerStyle;
2602 MMI_HILOG_CURSORD("Sceneboard pid:%{public}d windowId:%{public}d is set to %{public}d",
2603 scenePid, sceneWinId, pointerStyle.id);
2604 auto it = pointerStyle_.find(pid);
2605 if (it == pointerStyle_.end()) {
2606 MMI_HILOG_CURSORE("Pid:%{public}d does not exist in mmi,", pid);
2607 std::map<int32_t, PointerStyle> tmpPointerStyle = {{windowId, pointerStyle}};
2608 auto res = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
2609 if (!res.second) return RET_ERR;
2610 return RET_OK;
2611 }
2612 auto iter = it->second.find(windowId);
2613 if (iter == it->second.end()) {
2614 auto res = it->second.insert(std::make_pair(windowId, pointerStyle));
2615 if (!res.second) return RET_ERR;
2616 return RET_OK;
2617 }
2618 iter->second = pointerStyle;
2619 SetMouseFlag(pointerActionFlag_ == PointerEvent::POINTER_ACTION_BUTTON_DOWN);
2620 UpdateCustomStyle(windowId, pointerStyle);
2621 return RET_OK;
2622 }
2623
2624 void InputWindowsManager::UpdateCustomStyle(int32_t windowId, PointerStyle pointerStyle)
2625 {
2626 if (pointerStyle.id != MOUSE_ICON::DEVELOPER_DEFINED_ICON) {
2627 return;
2628 }
2629 for (auto &item : pointerStyle_) {
2630 for (auto &innerIt : item.second) {
2631 if (innerIt.first != windowId && innerIt.second.id == MOUSE_ICON::DEVELOPER_DEFINED_ICON) {
2632 innerIt.second.id = MOUSE_ICON::DEFAULT;
2633 }
2634 }
2635 }
2636 }
2637
2638 void InputWindowsManager::SetUiExtensionInfo(bool isUiExtension, int32_t uiExtensionPid, int32_t uiExtensionWindoId)
2639 {
2640 MMI_HILOGI("SetUiExtensionInfo. pid:%{public}d, windowid:%{public}d", uiExtensionPid, uiExtensionWindoId);
2641 isUiExtension_ = isUiExtension;
2642 uiExtensionPid_ = uiExtensionPid;
2643 uiExtensionWindowId_ = uiExtensionWindoId;
2644 }
2645
2646 void InputWindowsManager::SetGlobalDefaultPointerStyle()
2647 {
2648 for (auto &iter : pointerStyle_) {
2649 for (auto &item : iter.second) {
2650 if (item.second.id == DEFAULT_POINTER_STYLE) {
2651 item.second.id = globalStyle_.id;
2652 } else if (item.second.id == CURSOR_CIRCLE_STYLE || item.second.id == AECH_DEVELOPER_DEFINED_STYLE) {
2653 item.second.id = globalStyle_.id;
2654 }
2655 item.second.options = globalStyle_.options;
2656 }
2657 }
2658 }
2659
2660 int32_t InputWindowsManager::SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle,
2661 bool isUiExtension)
2662 {
2663 CALL_DEBUG_ENTER;
2664 if (windowId == GLOBAL_WINDOW_ID) {
2665 globalStyle_.id = pointerStyle.id;
2666 globalStyle_.options = pointerStyle.options;
2667 SetGlobalDefaultPointerStyle();
2668 MMI_HILOG_CURSORD("Setting global pointer style");
2669 return RET_OK;
2670 }
2671 MMI_HILOG_CURSORD("Start to get pid by window %{public}d", windowId);
2672 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2673 return UpdatePoinerStyle(pid, windowId, pointerStyle);
2674 }
2675 if (!isUiExtension && uiExtensionPointerStyle_.count(pid) != 0) {
2676 MMI_HILOG_CURSORI("Clear the uiextension mouse style for pid %{public}d", pid);
2677 uiExtensionPointerStyle_.erase(pid);
2678 }
2679 SetUiExtensionInfo(isUiExtension, pid, windowId);
2680 return UpdateSceneBoardPointerStyle(pid, windowId, pointerStyle, isUiExtension);
2681 }
2682
2683 bool InputWindowsManager::IsMouseSimulate()
2684 {
2685 auto lastPointerEventCopy = GetlastPointerEvent();
2686 CHKPF(lastPointerEventCopy);
2687 return lastPointerEventCopy->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE &&
2688 lastPointerEventCopy->HasFlag(InputEvent::EVENT_FLAG_SIMULATE);
2689 }
2690
2691 bool InputWindowsManager::HasMouseHideFlag()
2692 {
2693 auto lastPointerEventCopy = GetlastPointerEvent();
2694 CHKPF(lastPointerEventCopy);
2695 return lastPointerEventCopy->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER);
2696 }
2697 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2698
2699 int32_t InputWindowsManager::ClearWindowPointerStyle(int32_t pid, int32_t windowId)
2700 {
2701 CALL_DEBUG_ENTER;
2702 #ifdef OHOS_BUILD_ENABLE_POINTER
2703 auto it = pointerStyle_.find(pid);
2704 if (it == pointerStyle_.end()) {
2705 MMI_HILOG_CURSORE("Pid:%{public}d does not exist in mmi", pid);
2706 return RET_OK;
2707 }
2708 auto windowIt = it->second.find(windowId);
2709 if (windowIt == it->second.end()) {
2710 MMI_HILOG_CURSORE("windowId %{public}d does not exist in pid%{public}d", windowId, pid);
2711 return RET_OK;
2712 }
2713
2714 it->second.erase(windowIt);
2715 #endif // OHOS_BUILD_ENABLE_POINTER
2716 return RET_OK;
2717 }
2718
2719 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2720 int32_t InputWindowsManager::GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle,
2721 bool isUiExtension) const
2722 {
2723 CALL_DEBUG_ENTER;
2724 if (isUiExtension) {
2725 auto it = uiExtensionPointerStyle_.find(pid);
2726 if (it == uiExtensionPointerStyle_.end()) {
2727 MMI_HILOG_CURSORE("The uiextension pointer style map is not include pid:%{public}d", pid);
2728 pointerStyle.id = globalStyle_.id;
2729 return RET_OK;
2730 }
2731 auto iter = it->second.find(windowId);
2732 if (iter == it->second.end()) {
2733 pointerStyle.id = globalStyle_.id;
2734 return RET_OK;
2735 }
2736 MMI_HILOG_CURSORI("window type:%{public}d, get pointer style:%{public}d success", windowId, iter->second.id);
2737 pointerStyle = iter->second;
2738 return RET_OK;
2739 }
2740 if (windowId == GLOBAL_WINDOW_ID) {
2741 MMI_HILOG_CURSORD("Getting global pointer style");
2742 pointerStyle.id = globalStyle_.id;
2743 pointerStyle.options = globalStyle_.options;
2744 return RET_OK;
2745 }
2746 auto it = pointerStyle_.find(pid);
2747 if (it == pointerStyle_.end()) {
2748 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2749 pointerStyle.id = globalStyle_.id;
2750 return RET_OK;
2751 }
2752 MMI_HILOG_CURSORE("The pointer style map is not include param pd, %{public}d", pid);
2753 return RET_OK;
2754 }
2755 auto iter = it->second.find(windowId);
2756 if (iter == it->second.end()) {
2757 pointerStyle.id = globalStyle_.id;
2758 return RET_OK;
2759 }
2760 if (iter->second.id == DEFAULT_POINTER_STYLE) {
2761 pointerStyle.id = globalStyle_.id;
2762 } else {
2763 pointerStyle = iter->second;
2764 }
2765 MMI_HILOG_CURSORD("Window type:%{public}d get pointer style:%{public}d success", windowId, pointerStyle.id);
2766 return RET_OK;
2767 }
2768 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2769
2770 #ifdef OHOS_BUILD_ENABLE_POINTER
2771 void InputWindowsManager::InitPointerStyle()
2772 {
2773 CALL_DEBUG_ENTER;
2774 PointerStyle pointerStyle;
2775 pointerStyle.id = DEFAULT_POINTER_STYLE;
2776 for (const auto& windowItem : displayGroupInfo_.windowsInfo) {
2777 int32_t pid = windowItem.pid;
2778 auto it = pointerStyle_.find(pid);
2779 if (it == pointerStyle_.end()) {
2780 std::map<int32_t, PointerStyle> tmpPointerStyle = {};
2781 auto iter = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
2782 if (!iter.second) {
2783 MMI_HILOGW("The pd is duplicated");
2784 }
2785 continue;
2786 }
2787 }
2788 MMI_HILOGD("Number of pointer style:%{public}zu", pointerStyle_.size());
2789 }
2790
2791 #endif // OHOS_BUILD_ENABLE_POINTER
2792
2793 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2794 bool InputWindowsManager::IsInHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
2795 const WindowInfo &window) const
2796 {
2797 auto windowXY = TransformWindowXY(window, x, y);
2798 auto windowX = static_cast<int32_t>(windowXY.first);
2799 auto windowY = static_cast<int32_t>(windowXY.second);
2800 for (const auto &item : rects) {
2801 int32_t displayMaxX = 0;
2802 int32_t displayMaxY = 0;
2803 if (!AddInt32(item.x, item.width, displayMaxX)) {
2804 MMI_HILOGE("The addition of displayMaxX overflows");
2805 return false;
2806 }
2807 if (!AddInt32(item.y, item.height, displayMaxY)) {
2808 MMI_HILOGE("The addition of displayMaxY overflows");
2809 return false;
2810 }
2811 if (((windowX >= item.x) && (windowX < displayMaxX)) &&
2812 (windowY >= item.y) && (windowY < displayMaxY)) {
2813 return true;
2814 }
2815 }
2816 return false;
2817 }
2818
2819 bool InputWindowsManager::InWhichHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects,
2820 PointerStyle &pointerStyle) const
2821 {
2822 int32_t areaNum = 0;
2823 bool findFlag = false;
2824 for (const auto &item : rects) {
2825 int32_t displayMaxX = 0;
2826 int32_t displayMaxY = 0;
2827 if (!AddInt32(item.x, item.width, displayMaxX)) {
2828 MMI_HILOGE("The addition of displayMaxX overflows");
2829 return findFlag;
2830 }
2831 if (!AddInt32(item.y, item.height, displayMaxY)) {
2832 MMI_HILOGE("The addition of displayMaxY overflows");
2833 return findFlag;
2834 }
2835 if (((x >= item.x) && (x <= displayMaxX)) && (y >= item.y) && (y <= displayMaxY)) {
2836 findFlag = true;
2837 pointerStyle.id = areaNum;
2838 }
2839 areaNum++;
2840 }
2841 if (!findFlag) {
2842 MMI_HILOGD("pointer not match any area");
2843 return findFlag;
2844 }
2845 switch (pointerStyle.id) {
2846 case PointerHotArea::TOP:
2847 case PointerHotArea::BOTTOM:
2848 pointerStyle.id = MOUSE_ICON::NORTH_SOUTH;
2849 break;
2850 case PointerHotArea::LEFT:
2851 case PointerHotArea::RIGHT:
2852 pointerStyle.id = MOUSE_ICON::WEST_EAST;
2853 break;
2854 case PointerHotArea::TOP_LEFT:
2855 pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
2856 break;
2857 case PointerHotArea::TOP_RIGHT:
2858 pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
2859 break;
2860 case PointerHotArea::BOTTOM_LEFT:
2861 pointerStyle.id = MOUSE_ICON::NORTH_EAST_SOUTH_WEST;
2862 break;
2863 case PointerHotArea::BOTTOM_RIGHT:
2864 pointerStyle.id = MOUSE_ICON::NORTH_WEST_SOUTH_EAST;
2865 break;
2866 default:
2867 MMI_HILOGD("pointerStyle in default is:%{public}d", pointerStyle.id);
2868 break;
2869 }
2870 MMI_HILOGD("pointerStyle after switch ID is :%{public}d", pointerStyle.id);
2871 return findFlag;
2872 }
2873
2874 bool InputWindowsManager::InWhichHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects) const
2875 {
2876 bool findFlag = false;
2877 for (const auto &item : rects) {
2878 int32_t displayMaxX = 0;
2879 int32_t displayMaxY = 0;
2880 if (!AddInt32(item.x, item.width, displayMaxX)) {
2881 MMI_HILOGE("The addition of displayMaxX overflows");
2882 return findFlag;
2883 }
2884 if (!AddInt32(item.y, item.height, displayMaxY)) {
2885 MMI_HILOGE("The addition of displayMaxY overflows");
2886 return findFlag;
2887 }
2888 if (((x > item.x) && (x <= displayMaxX)) && (y > item.y) && (y <= displayMaxY)) {
2889 findFlag = true;
2890 break;
2891 }
2892 }
2893 return findFlag;
2894 }
2895 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
2896
2897 #ifdef OHOS_BUILD_ENABLE_TOUCH
2898 void InputWindowsManager::AdjustDisplayCoordinate(
2899 const DisplayInfo& displayInfo, double& physicalX, double& physicalY) const
2900 {
2901 int32_t width = displayInfo.validWidth;
2902 int32_t height = displayInfo.validHeight;
2903 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
2904 if (displayInfo.direction == DIRECTION90 || displayInfo.direction == DIRECTION270) {
2905 width = displayInfo.height;
2906 height = displayInfo.width;
2907 }
2908 }
2909 if (physicalX <= 0) {
2910 physicalX = 0;
2911 }
2912 if (physicalX >= width && width > 0) {
2913 physicalX = width - 1;
2914 }
2915 if (physicalY <= 0) {
2916 physicalY = 0;
2917 }
2918 if (physicalY >= height && height > 0) {
2919 physicalY = height - 1;
2920 }
2921 }
2922 #endif // OHOS_BUILD_ENABLE_TOUCH
2923
2924 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
2925 bool InputWindowsManager::UpdateDisplayId(int32_t& displayId)
2926 {
2927 if (displayGroupInfo_.displaysInfo.empty()) {
2928 MMI_HILOGE("logicalDisplays_is empty");
2929 return false;
2930 }
2931 if (displayId < 0) {
2932 displayId = displayGroupInfo_.displaysInfo[0].id;
2933 return true;
2934 }
2935 for (const auto &item : displayGroupInfo_.displaysInfo) {
2936 if (item.id == displayId) {
2937 return true;
2938 }
2939 }
2940 return false;
2941 }
2942
2943 std::optional<WindowInfo> InputWindowsManager::SelectWindowInfo(int32_t logicalX, int32_t logicalY,
2944 const std::shared_ptr<PointerEvent>& pointerEvent)
2945 {
2946 CALL_DEBUG_ENTER;
2947 int32_t action = pointerEvent->GetPointerAction();
2948 bool checkFlag = (firstBtnDownWindowInfo_.first == -1) ||
2949 ((action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) && (pointerEvent->GetPressedButtons().size() <= 1)) ||
2950 ((action == PointerEvent::POINTER_ACTION_MOVE) && (pointerEvent->GetPressedButtons().empty())) ||
2951 (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
2952 (action == PointerEvent::POINTER_ACTION_PULL_UP) ||
2953 ((action == PointerEvent::POINTER_ACTION_AXIS_BEGIN || action == PointerEvent::POINTER_ACTION_ROTATE_BEGIN) &&
2954 (pointerEvent->GetPressedButtons().empty()));
2955 std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
2956 if (checkFlag) {
2957 int32_t targetWindowId = pointerEvent->GetTargetWindowId();
2958 static std::unordered_map<int32_t, int32_t> winId2ZorderMap;
2959 bool isHotArea = false;
2960 if (targetWindowId <= 1) {
2961 targetMouseWinIds_.clear();
2962 }
2963 for (const auto &item : windowsInfo) {
2964 if (transparentWins_.find(item.id) != transparentWins_.end()) {
2965 if (IsTransparentWin(transparentWins_[item.id], logicalX - item.area.x, logicalY - item.area.y)) {
2966 winId2ZorderMap.insert({item.id, item.zOrder});
2967 MMI_HILOG_DISPATCHE("It's an abnormal window and pointer find the next window");
2968 continue;
2969 }
2970 }
2971 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
2972 !IsValidZorderWindow(item, pointerEvent)) {
2973 winId2ZorderMap.insert({item.id, item.zOrder});
2974 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching, "
2975 "window:%{public}d, flags:%{public}d, pid:%{public}d", item.id, item.flags, item.pid);
2976 continue;
2977 } else if ((extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) ||
2978 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
2979 if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
2980 if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
2981 winId2ZorderMap.insert({item.id, item.zOrder});
2982 continue;
2983 }
2984 firstBtnDownWindowInfo_.first = item.id;
2985 firstBtnDownWindowInfo_.second = item.displayId;
2986 MMI_HILOG_DISPATCHD("Mouse event select pull window, window:%{public}d, pid:%{public}d",
2987 firstBtnDownWindowInfo_.first, item.pid);
2988 break;
2989 } else {
2990 winId2ZorderMap.insert({item.id, item.zOrder});
2991 continue;
2992 }
2993 } else if ((targetWindowId < 0) && (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item))) {
2994 if ((item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE ||
2995 item.windowInputType == WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) &&
2996 ((pointerEvent->GetPressedButtons().empty()) ||
2997 (action == PointerEvent::POINTER_ACTION_PULL_UP) ||
2998 (action == PointerEvent::POINTER_ACTION_AXIS_BEGIN) ||
2999 (action == PointerEvent::POINTER_ACTION_AXIS_UPDATE) ||
3000 (action == PointerEvent::POINTER_ACTION_AXIS_END)||
3001 (PointerEvent::MOUSE_BUTTON_LEFT != pointerEvent->GetButtonId()))) {
3002 MMI_HILOG_DISPATCHD("Mouse event transparent, action:%{public}d, ButtonId:%{public}d",
3003 action, pointerEvent->GetButtonId());
3004 continue;
3005 }
3006 firstBtnDownWindowInfo_.first = item.id;
3007 firstBtnDownWindowInfo_.second = item.displayId;
3008 if (!item.uiExtentionWindowInfo.empty()) {
3009 // Determine whether the landing point as a safety sub window
3010 CheckUIExtentionWindowPointerHotArea(logicalX, logicalY,
3011 item.uiExtentionWindowInfo, firstBtnDownWindowInfo_.first);
3012 }
3013 MMI_HILOG_DISPATCHD("Find out the dispatch window of this pointer event when the targetWindowId "
3014 "hasn't been set up yet, window:%{public}d, pid:%{public}d",
3015 firstBtnDownWindowInfo_.first, item.pid);
3016 bool isSpecialWindow = HandleWindowInputType(item, pointerEvent);
3017 if (isSpecialWindow) {
3018 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
3019 isHotArea = true;
3020 continue;
3021 } else if (isHotArea) {
3022 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
3023 break;
3024 } else {
3025 break;
3026 }
3027
3028 } else if ((targetWindowId >= 0) && (targetWindowId == item.id)) {
3029 firstBtnDownWindowInfo_.first = targetWindowId;
3030 firstBtnDownWindowInfo_.second = item.displayId;
3031 MMI_HILOG_DISPATCHD("Find out the dispatch window of this pointer event when the targetWindowId "
3032 "has been set up already, window:%{public}d, pid:%{public}d",
3033 firstBtnDownWindowInfo_.first, item.pid);
3034 break;
3035 } else {
3036 winId2ZorderMap.insert({item.id, item.zOrder});
3037 MMI_HILOG_DISPATCHD("Continue searching for the dispatch window of this pointer event");
3038 }
3039 }
3040 if ((firstBtnDownWindowInfo_.first < 0) && (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) &&
3041 (pointerEvent->GetPressedButtons().size() == 1)) {
3042 for (const auto &iter : winId2ZorderMap) {
3043 MMI_HILOG_DISPATCHI("%{public}d, %{public}d", iter.first, iter.second);
3044 }
3045 }
3046 winId2ZorderMap.clear();
3047 }
3048 if (axisBeginWindowInfo_ &&
3049 (action == PointerEvent::POINTER_ACTION_AXIS_UPDATE || action == PointerEvent::POINTER_ACTION_AXIS_END)) {
3050 firstBtnDownWindowInfo_ = { axisBeginWindowInfo_->id, axisBeginWindowInfo_->displayId };
3051 }
3052 MMI_HILOG_DISPATCHD("firstBtnDownWindowInfo_.first:%{public}d", firstBtnDownWindowInfo_.first);
3053 #ifdef OHOS_BUILD_PC_PRIORITY
3054 PrintHighZorder(windowsInfo, pointerEvent->GetPointerAction(), firstBtnDownWindowInfo_.first, logicalX, logicalY);
3055 #endif // OHOS_BUILD_PC_PRIORITY
3056 for (const auto &item : windowsInfo) {
3057 for (const auto &windowInfo : item.uiExtentionWindowInfo) {
3058 if (windowInfo.id == firstBtnDownWindowInfo_.first) {
3059 return std::make_optional(windowInfo);
3060 }
3061 }
3062 if (item.id == firstBtnDownWindowInfo_.first) {
3063 return std::make_optional(item);
3064 }
3065 }
3066 if (pointerEvent->GetTargetDisplayId() != firstBtnDownWindowInfo_.second) {
3067 std::vector<WindowInfo> firstBtnDownWindowsInfo =
3068 GetWindowGroupInfoByDisplayId(firstBtnDownWindowInfo_.second);
3069 for (const auto &item : firstBtnDownWindowsInfo) {
3070 for (const auto &windowInfo : item.uiExtentionWindowInfo) {
3071 if (windowInfo.id == firstBtnDownWindowInfo_.first) {
3072 return std::make_optional(windowInfo);
3073 }
3074 }
3075 if (item.id == firstBtnDownWindowInfo_.first) {
3076 return std::make_optional(item);
3077 }
3078 }
3079 }
3080 return std::nullopt;
3081 }
3082
3083 void InputWindowsManager::CheckUIExtentionWindowPointerHotArea(int32_t logicalX, int32_t logicalY,
3084 const std::vector<WindowInfo>& windowInfos, int32_t& windowId)
3085 {
3086 for (const auto &it : windowInfos) {
3087 if (IsInHotArea(logicalX, logicalY, it.pointerHotAreas, it)) {
3088 windowId = it.id;
3089 break;
3090 }
3091 }
3092 }
3093
3094 std::optional<WindowInfo> InputWindowsManager::GetWindowInfo(int32_t logicalX, int32_t logicalY)
3095 {
3096 CALL_DEBUG_ENTER;
3097 for (const auto& item : displayGroupInfo_.windowsInfo) {
3098 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
3099 MMI_HILOGD("Skip the untouchable window to continue searching, "
3100 "window:%{public}d, flags:%{public}d", item.id, item.flags);
3101 continue;
3102 } else if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas, item)) {
3103 return std::make_optional(item);
3104 } else {
3105 MMI_HILOGD("Continue searching for the dispatch window");
3106 }
3107 }
3108 return std::nullopt;
3109 }
3110
3111 bool InputWindowsManager::SelectPointerChangeArea(const WindowInfo &windowInfo, PointerStyle &pointerStyle,
3112 int32_t logicalX, int32_t logicalY)
3113 {
3114 CALL_DEBUG_ENTER;
3115 int32_t windowId = windowInfo.id;
3116 bool findFlag = false;
3117 if (windowsHotAreas_.find(windowId) != windowsHotAreas_.end()) {
3118 std::vector<Rect> windowHotAreas = windowsHotAreas_[windowId];
3119 MMI_HILOG_CURSORD("windowHotAreas size:%{public}zu, windowId:%{public}d",
3120 windowHotAreas.size(), windowId);
3121 findFlag = InWhichHotArea(logicalX, logicalY, windowHotAreas, pointerStyle);
3122 }
3123 return findFlag;
3124 }
3125
3126 bool InputWindowsManager::SelectPointerChangeArea(int32_t windowId, int32_t logicalX, int32_t logicalY)
3127 {
3128 CALL_DEBUG_ENTER;
3129 bool findFlag = false;
3130 if (windowsHotAreas_.find(windowId) != windowsHotAreas_.end()) {
3131 std::vector<Rect> windowHotAreas = windowsHotAreas_[windowId];
3132 MMI_HILOGE("windowHotAreas size:%{public}zu, windowId:%{public}d",
3133 windowHotAreas.size(), windowId);
3134 findFlag = InWhichHotArea(logicalX, logicalY, windowHotAreas);
3135 }
3136 return findFlag;
3137 }
3138
3139 void InputWindowsManager::UpdatePointerChangeAreas(const DisplayGroupInfo &displayGroupInfo)
3140 {
3141 CALL_DEBUG_ENTER;
3142 for (const auto &windowInfo : displayGroupInfo.windowsInfo) {
3143 std::vector<Rect> windowHotAreas;
3144 int32_t windowId = windowInfo.id;
3145 Rect windowArea = windowInfo.area;
3146 if (windowInfo.transform.size() <= 0) {
3147 continue;
3148 }
3149 windowArea.width = windowInfo.transform[SCALE_X] != 0 ? windowInfo.area.width / windowInfo.transform[SCALE_X]
3150 : windowInfo.area.width;
3151 windowArea.height = windowInfo.transform[SCALE_Y] != 0 ? windowInfo.area.height / windowInfo.transform[SCALE_Y]
3152 : windowInfo.area.height;
3153 std::vector<int32_t> pointerChangeAreas = windowInfo.pointerChangeAreas;
3154 if (!pointerChangeAreas.empty()) {
3155 UpdateTopBottomArea(windowArea, pointerChangeAreas, windowHotAreas);
3156 UpdateLeftRightArea(windowArea, pointerChangeAreas, windowHotAreas);
3157 UpdateInnerAngleArea(windowArea, pointerChangeAreas, windowHotAreas);
3158 }
3159 if (windowsHotAreas_.find(windowId) == windowsHotAreas_.end()) {
3160 windowsHotAreas_.emplace(windowId, windowHotAreas);
3161 } else {
3162 windowsHotAreas_[windowId] = windowHotAreas;
3163 }
3164 }
3165 }
3166
3167 void InputWindowsManager::UpdatePointerChangeAreas()
3168 {
3169 CALL_DEBUG_ENTER;
3170 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
3171 std::lock_guard<std::mutex> lock(tmpInfoMutex_);
3172 UpdatePointerChangeAreas(displayGroupInfoTmp_);
3173 }
3174 }
3175
3176 void InputWindowsManager::UpdateTopBottomArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
3177 std::vector<Rect> &windowHotAreas)
3178 {
3179 CALL_DEBUG_ENTER;
3180 Rect newTopRect;
3181 newTopRect.x = windowArea.x + pointerChangeAreas[TOP_LEFT_AREA];
3182 newTopRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
3183 newTopRect.width = windowArea.width - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[TOP_RIGHT_AREA];
3184 newTopRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_AREA];
3185 Rect newBottomRect;
3186 newBottomRect.x = windowArea.x + pointerChangeAreas[BOTTOM_LEFT_AREA];
3187 newBottomRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_AREA];
3188 newBottomRect.width = windowArea.width - pointerChangeAreas[BOTTOM_LEFT_AREA] -
3189 pointerChangeAreas[BOTTOM_RIGHT_AREA];
3190 newBottomRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_AREA];
3191 if (pointerChangeAreas[TOP_AREA] == 0) {
3192 newTopRect.width = 0;
3193 newTopRect.height = 0;
3194 }
3195 if (pointerChangeAreas[BOTTOM_AREA] == 0) {
3196 newBottomRect.width = 0;
3197 newBottomRect.height = 0;
3198 }
3199 windowHotAreas.push_back(newTopRect);
3200 windowHotAreas.push_back(newBottomRect);
3201 }
3202
3203 void InputWindowsManager::UpdateLeftRightArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
3204 std::vector<Rect> &windowHotAreas)
3205 {
3206 CALL_DEBUG_ENTER;
3207 Rect newLeftRect;
3208 newLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
3209 newLeftRect.y = windowArea.y + pointerChangeAreas[TOP_LEFT_AREA];
3210 newLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[LEFT_AREA];
3211 newLeftRect.height = windowArea.height - pointerChangeAreas[TOP_LEFT_AREA] - pointerChangeAreas[BOTTOM_LEFT_AREA];
3212 Rect newRightRect;
3213 newRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[RIGHT_AREA];
3214 newRightRect.y = windowArea.y + pointerChangeAreas[TOP_RIGHT_AREA];
3215 newRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[RIGHT_AREA];
3216 newRightRect.height = windowArea.height - pointerChangeAreas[TOP_RIGHT_AREA] -
3217 pointerChangeAreas[BOTTOM_RIGHT_AREA];
3218 if (pointerChangeAreas[LEFT_AREA] == 0) {
3219 newLeftRect.width = 0;
3220 newLeftRect.height = 0;
3221 }
3222 if (pointerChangeAreas[RIGHT_AREA] == 0) {
3223 newRightRect.width = 0;
3224 newRightRect.height = 0;
3225 }
3226 windowHotAreas.push_back(newLeftRect);
3227 windowHotAreas.push_back(newRightRect);
3228 }
3229
3230 void InputWindowsManager::UpdateInnerAngleArea(const Rect &windowArea, std::vector<int32_t> &pointerChangeAreas,
3231 std::vector<Rect> &windowHotAreas)
3232 {
3233 CALL_DEBUG_ENTER;
3234 Rect newTopLeftRect;
3235 newTopLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
3236 newTopLeftRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
3237 newTopLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
3238 newTopLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_LEFT_AREA];
3239 Rect newTopRightRect;
3240 newTopRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[TOP_RIGHT_AREA];
3241 newTopRightRect.y = windowArea.y - OUTWINDOW_HOT_AREA;
3242 newTopRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
3243 newTopRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[TOP_RIGHT_AREA];
3244 Rect newBottomLeftRect;
3245 newBottomLeftRect.x = windowArea.x - OUTWINDOW_HOT_AREA;
3246 newBottomLeftRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_LEFT_AREA];
3247 newBottomLeftRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
3248 newBottomLeftRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_LEFT_AREA];
3249 Rect newBottomRightRect;
3250 newBottomRightRect.x = windowArea.x + windowArea.width - pointerChangeAreas[BOTTOM_RIGHT_AREA];
3251 newBottomRightRect.y = windowArea.y + windowArea.height - pointerChangeAreas[BOTTOM_RIGHT_AREA];
3252 newBottomRightRect.width = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
3253 newBottomRightRect.height = OUTWINDOW_HOT_AREA + pointerChangeAreas[BOTTOM_RIGHT_AREA];
3254 if (pointerChangeAreas[TOP_LEFT_AREA] == 0) {
3255 newTopLeftRect.width = 0;
3256 newTopLeftRect.height = 0;
3257 }
3258 if (pointerChangeAreas[TOP_RIGHT_AREA] == 0) {
3259 newTopRightRect.width = 0;
3260 newTopRightRect.height = 0;
3261 }
3262 if (pointerChangeAreas[BOTTOM_LEFT_AREA] == 0) {
3263 newBottomLeftRect.width = 0;
3264 newBottomLeftRect.height = 0;
3265 }
3266 if (pointerChangeAreas[BOTTOM_RIGHT_AREA] == 0) {
3267 newBottomRightRect.width = 0;
3268 newBottomRightRect.height = 0;
3269 }
3270
3271 windowHotAreas.push_back(newTopLeftRect);
3272 windowHotAreas.push_back(newTopRightRect);
3273 windowHotAreas.push_back(newBottomLeftRect);
3274 windowHotAreas.push_back(newBottomRightRect);
3275 }
3276 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3277
3278 #ifdef OHOS_BUILD_ENABLE_POINTER
3279 void InputWindowsManager::UpdatePointerEvent(int32_t logicalX, int32_t logicalY,
3280 const std::shared_ptr<PointerEvent>& pointerEvent, const WindowInfo& touchWindow)
3281 {
3282 CHKPV(pointerEvent);
3283 MMI_HILOG_DISPATCHD("LastWindowInfo:%{public}d, touchWindow:%{public}d", lastWindowInfo_.id, touchWindow.id);
3284 if (lastWindowInfo_.id != touchWindow.id) {
3285 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
3286 lastLogicX_ = logicalX;
3287 lastLogicY_ = logicalY;
3288 {
3289 std::lock_guard<std::mutex> guard(mtx_);
3290 lastPointerEvent_ = pointerEvent;
3291 }
3292 lastWindowInfo_ = touchWindow;
3293 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
3294 return;
3295 }
3296 lastLogicX_ = logicalX;
3297 lastLogicY_ = logicalY;
3298 {
3299 std::lock_guard<std::mutex> guard(mtx_);
3300 lastPointerEvent_ = pointerEvent;
3301 }
3302 lastWindowInfo_ = touchWindow;
3303 }
3304
3305 int32_t InputWindowsManager::SetHoverScrollState(bool state)
3306 {
3307 CALL_DEBUG_ENTER;
3308 MMI_HILOGD("Set mouse hover scroll state:%{public}d", state);
3309 std::string name = "isEnableHoverScroll";
3310 return PREFERENCES_MGR->SetBoolValue(name, MOUSE_FILE_NAME, state);
3311 }
3312
3313 bool InputWindowsManager::GetHoverScrollState() const
3314 {
3315 CALL_DEBUG_ENTER;
3316 std::string name = "isEnableHoverScroll";
3317 bool state = PREFERENCES_MGR->GetBoolValue(name, true);
3318 MMI_HILOGD("Get mouse hover scroll state:%{public}d", state);
3319 return state;
3320 }
3321
3322 int32_t InputWindowsManager::UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)
3323 {
3324 CALL_DEBUG_ENTER;
3325 CHKPR(pointerEvent, ERROR_NULL_POINTER);
3326 auto displayId = pointerEvent->GetTargetDisplayId();
3327 if (!UpdateDisplayId(displayId)) {
3328 MMI_HILOGE("This display:%{public}d is not existent", displayId);
3329 return RET_ERR;
3330 }
3331 pointerEvent->SetTargetDisplayId(displayId);
3332
3333 auto physicalDisplayInfo = GetPhysicalDisplay(displayId);
3334 CHKPR(physicalDisplayInfo, ERROR_NULL_POINTER);
3335 int32_t pointerId = pointerEvent->GetPointerId();
3336 PointerEvent::PointerItem pointerItem;
3337 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3338 MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
3339 return RET_ERR;
3340 }
3341 int32_t logicalX = 0;
3342 int32_t logicalY = 0;
3343 if (!AddInt32(pointerItem.GetDisplayX(), physicalDisplayInfo->x, logicalX)) {
3344 MMI_HILOGE("The addition of logicalX overflows");
3345 return RET_ERR;
3346 }
3347 if (!AddInt32(pointerItem.GetDisplayY(), physicalDisplayInfo->y, logicalY)) {
3348 MMI_HILOGE("The addition of logicalY overflows");
3349 return RET_ERR;
3350 }
3351 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
3352 ClearTargetWindowId(pointerId);
3353 }
3354 int32_t physicalX = pointerItem.GetDisplayX();
3355 int32_t physicalY = pointerItem.GetDisplayY();
3356 auto touchWindow = SelectWindowInfo(logicalX, logicalY, pointerEvent);
3357 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN) {
3358 axisBeginWindowInfo_ = touchWindow;
3359 }
3360 if (!touchWindow) {
3361 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN || (mouseDownInfo_.id == -1 &&
3362 axisBeginWindowInfo_ == std::nullopt)) {
3363 MMI_HILOGE("touchWindow is nullptr, targetWindow:%{public}d", pointerEvent->GetTargetWindowId());
3364 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
3365 IsMouseDrawing(pointerEvent->GetPointerAction()) &&
3366 pointerItem.GetMoveFlag() != POINTER_MOVEFLAG) {
3367 MMI_HILOGD("Turn the mouseDisplay from false to true");
3368 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3369 }
3370 int64_t beginTime = GetSysClockTime();
3371 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
3372 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) &&
3373 pointerItem.GetMoveFlag() == POINTER_MOVEFLAG) {
3374 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
3375 } else {
3376 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3377 }
3378 IPointerDrawingManager::GetInstance()->DrawMovePointer(displayId, physicalX, physicalY);
3379 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
3380 int64_t endTime = GetSysClockTime();
3381 if ((endTime - beginTime) > RS_PROCESS_TIMEOUT) {
3382 MMI_HILOGW("Rs process timeout");
3383 }
3384 return RET_ERR;
3385 }
3386 if (mouseDownInfo_.id != -1) {
3387 touchWindow = std::make_optional(mouseDownInfo_);
3388 } else if (axisBeginWindowInfo_) {
3389 touchWindow = axisBeginWindowInfo_;
3390 }
3391 int32_t pointerAction = pointerEvent->GetPointerAction();
3392 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_MOVE ||
3393 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
3394 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT ||
3395 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
3396 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
3397 } else {
3398 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
3399 }
3400 pointerEvent->SetOriginPointerAction(pointerAction);
3401 MMI_HILOGI("Mouse event send cancel, window:%{public}d, pid:%{public}d", touchWindow->id, touchWindow->pid);
3402 }
3403
3404 bool checkFlag = pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_UPDATE ||
3405 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
3406 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END;
3407 if (checkFlag) {
3408 if ((!GetHoverScrollState()) && (displayGroupInfo_.focusWindowId != touchWindow->id)) {
3409 MMI_HILOGD("disable mouse hover scroll in inactive window, targetWindowId:%{public}d", touchWindow->id);
3410 return RET_OK;
3411 }
3412 }
3413 PointerStyle pointerStyle;
3414 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
3415 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
3416 if (timerId_ != DEFAULT_VALUE) {
3417 TimerMgr->RemoveTimer(timerId_);
3418 timerId_ = DEFAULT_VALUE;
3419 }
3420 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
3421 IsMouseDrawing(pointerEvent->GetPointerAction()) &&
3422 pointerItem.GetMoveFlag() != POINTER_MOVEFLAG) {
3423 MMI_HILOGD("Turn the mouseDisplay from false to true");
3424 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3425 }
3426 pointerStyle = IPointerDrawingManager::GetInstance()->GetLastMouseStyle();
3427 MMI_HILOGD("showing the lastMouseStyle %{public}d, lastPointerStyle %{public}d",
3428 pointerStyle.id, lastPointerStyle_.id);
3429 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
3430 WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
3431 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
3432 } else {
3433 if (timerId_ != DEFAULT_VALUE) {
3434 TimerMgr->RemoveTimer(timerId_);
3435 timerId_ = DEFAULT_VALUE;
3436 }
3437 GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
3438 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
3439 pointerItem.GetMoveFlag() != POINTER_MOVEFLAG) {
3440 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3441 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
3442 }
3443 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
3444 WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
3445 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
3446 }
3447 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
3448 #ifdef OHOS_BUILD_EMULATOR
3449 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN &&
3450 !IPointerDrawingManager::GetInstance()->GetMouseDisplayState() &&
3451 pointerItem.GetMoveFlag() != POINTER_MOVEFLAG) {
3452 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3453 }
3454 #endif
3455 GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
3456 if (isUiExtension_) {
3457 MMI_HILOGD("updatemouse target in uiextension");
3458 GetPointerStyle(uiExtensionPid_, uiExtensionWindowId_, pointerStyle, isUiExtension_);
3459 dragPointerStyle_ = pointerStyle;
3460 } else {
3461 GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
3462 }
3463 if (!isDragBorder_ && !isUiExtension_) {
3464 GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
3465 dragPointerStyle_ = pointerStyle;
3466 }
3467 WindowInfo window = *touchWindow;
3468 if (!dragFlag_) {
3469 isDragBorder_ = SelectPointerChangeArea(window, pointerStyle, logicalX, logicalY);
3470 dragPointerStyle_ = pointerStyle;
3471 MMI_HILOGD("pointerStyle is :%{public}d, windowId is :%{public}d, logicalX is :%{private}d,"
3472 "logicalY is :%{private}d", pointerStyle.id, window.id, logicalX, logicalY);
3473 }
3474 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
3475 SetMouseFlag(true);
3476 dragFlag_ = true;
3477 MMI_HILOGD("Is in drag scene");
3478 }
3479 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP) {
3480 SetMouseFlag(false);
3481 dragFlag_ = false;
3482 isDragBorder_ = false;
3483 }
3484 Direction direction = DIRECTION0;
3485 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
3486 direction = static_cast<Direction>((((physicalDisplayInfo->direction - physicalDisplayInfo->displayDirection) *
3487 ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
3488 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
3489 if (IsSupported()) {
3490 direction = physicalDisplayInfo->direction;
3491 }
3492 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
3493 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
3494 TOUCH_DRAWING_MGR->GetOriginalTouchScreenCoordinates(direction, physicalDisplayInfo->validWidth,
3495 physicalDisplayInfo->validHeight, physicalX, physicalY);
3496 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
3497 }
3498 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
3499 MAGIC_POINTER_VELOCITY_TRACKER->MonitorCursorMovement(pointerEvent);
3500 #endif // OHOS_BUILD_ENABLE_MAGICCURSOR
3501 int64_t beginTime = GetSysClockTime();
3502 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
3503 if (IsMouseDrawing(pointerEvent->GetPointerAction())) {
3504 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_HIDE_POINTER) ||
3505 pointerItem.GetMoveFlag() == POINTER_MOVEFLAG) {
3506 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
3507 } else {
3508 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
3509 }
3510 if (extraData_.drawCursor) {
3511 MMI_HILOGD("Cursor must be default, pointerStyle:%{public}d globalStyle:%{public}d",
3512 dragPointerStyle_.id, globalStyle_.id);
3513 dragPointerStyle_ = globalStyle_;
3514 }
3515 IPointerDrawingManager::GetInstance()->DrawPointer(displayId, physicalX, physicalY,
3516 dragPointerStyle_, direction);
3517 }
3518 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
3519 cursorPos_.direction = physicalDisplayInfo->direction;
3520 cursorPos_.displayDirection = physicalDisplayInfo->displayDirection;
3521 int64_t endTime = GetSysClockTime();
3522 if ((endTime - beginTime) > RS_PROCESS_TIMEOUT) {
3523 MMI_HILOGW("Rs process timeout");
3524 }
3525 if (captureModeInfo_.isCaptureMode && (touchWindow->id != captureModeInfo_.windowId)) {
3526 captureModeInfo_.isCaptureMode = false;
3527 }
3528 SetPrivacyModeFlag(touchWindow->privacyMode, pointerEvent);
3529 pointerEvent->SetTargetWindowId(touchWindow->id);
3530 pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
3531 DispatchUIExtentionPointerEvent(logicalX, logicalY, pointerEvent);
3532 auto windowX = logicalX - touchWindow->area.x;
3533 auto windowY = logicalY - touchWindow->area.y;
3534 if (!(touchWindow->transform.empty())) {
3535 auto windowXY = TransformWindowXY(*touchWindow, logicalX, logicalY);
3536 windowX = windowXY.first;
3537 windowY = windowXY.second;
3538 }
3539 windowX = static_cast<int32_t>(windowX);
3540 windowY = static_cast<int32_t>(windowY);
3541 pointerItem.SetWindowX(windowX);
3542 pointerItem.SetWindowY(windowY);
3543 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3544 if ((extraData_.appended && (extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE)) ||
3545 (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP)) {
3546 pointerEvent->SetBuffer(extraData_.buffer);
3547 pointerEvent->SetPullId(extraData_.pullId);
3548 UpdatePointerAction(pointerEvent);
3549 } else {
3550 pointerEvent->ClearBuffer();
3551 }
3552 CHKPR(udsServer_, ERROR_NULL_POINTER);
3553 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
3554 UpdatePointerEvent(logicalX, logicalY, pointerEvent, *touchWindow);
3555 #elif defined(OHOS_BUILD_EMULATOR)
3556 if (IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
3557 UpdatePointerEvent(logicalX, logicalY, pointerEvent, *touchWindow);
3558 }
3559 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
3560 #ifdef OHOS_BUILD_ENABLE_ANCO
3561 if (touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY)) {
3562 MMI_HILOGD("Process mouse event in Anco window, targetWindowId:%{public}d", touchWindow->id);
3563 pointerEvent->SetAncoDeal(true);
3564 SimulatePointerExt(pointerEvent);
3565 return RET_OK;
3566 }
3567 #endif // OHOS_BUILD_ENABLE_ANCO
3568 int32_t action = pointerEvent->GetPointerAction();
3569 if (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
3570 mouseDownInfo_ = *touchWindow;
3571 mouseDownEventId_ = pointerEvent->GetId();
3572 }
3573 if ((action == PointerEvent::POINTER_ACTION_MOVE && !pointerEvent->GetPressedButtons().empty()) ||
3574 (action == PointerEvent::POINTER_ACTION_BUTTON_UP)) {
3575 if (touchWindow->id != mouseDownInfo_.id) {
3576 MMI_HILOGE("Mouse from:%{public}d move to new window:%{public}d", mouseDownInfo_.id, touchWindow->id);
3577 }
3578 }
3579 if (action == PointerEvent::POINTER_ACTION_BUTTON_UP) {
3580 InitMouseDownInfo();
3581 mouseDownEventId_ = -1;
3582 MMI_HILOGD("Mouse up, clear mouse down info");
3583 }
3584 if (action == PointerEvent::POINTER_ACTION_CANCEL && mouseDownEventId_ > 0) {
3585 mouseDownEventId_ = -1;
3586 }
3587 if (action == PointerEvent::POINTER_ACTION_AXIS_END) {
3588 axisBeginWindowInfo_ = std::nullopt;
3589 MMI_HILOGD("Axis end, clear axis begin info");
3590 }
3591 if (EventLogHelper::IsBetaVersion() && !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
3592 MMI_HILOGD("pid:%{public}d, id:%{public}d, agentWindowId:%{public}d,"
3593 "logicalX:%{public}d, logicalY:%{public}d,"
3594 "displayX:%{public}d, displayY:%{public}d, windowX:%{public}d, windowY:%{public}d",
3595 isUiExtension_ ? uiExtensionPid_ : touchWindow->pid, isUiExtension_ ? uiExtensionWindowId_ :
3596 touchWindow->id, touchWindow->agentWindowId,
3597 logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
3598 } else {
3599 MMI_HILOGD("pid:%{public}d, id:%{public}d, agentWindowId:%{public}d,"
3600 "logicalX:%d, logicalY:%d,displayX:%d, displayY:%d, windowX:%d, windowY:%d",
3601 isUiExtension_ ? uiExtensionPid_ : touchWindow->pid, isUiExtension_ ? uiExtensionWindowId_ :
3602 touchWindow->id, touchWindow->agentWindowId,
3603 logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
3604 }
3605 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
3606 MMI_HILOGD("Clear extra data");
3607 ClearExtraData();
3608 }
3609 if (pointerItem.GetMoveFlag() == POINTER_MOVEFLAG) {
3610 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
3611 }
3612 return ERR_OK;
3613 }
3614 #endif // OHOS_BUILD_ENABLE_POINTER
3615
3616 bool InputWindowsManager::IsMouseDrawing(int32_t currentAction)
3617 {
3618 if (currentAction != PointerEvent::POINTER_ACTION_LEAVE_WINDOW &&
3619 currentAction != PointerEvent::POINTER_ACTION_ENTER_WINDOW &&
3620 currentAction != PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW &&
3621 currentAction != PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
3622 return true;
3623 }
3624 return false;
3625 }
3626
3627 void InputWindowsManager::SetMouseFlag(bool state)
3628 {
3629 mouseFlag_ = state;
3630 }
3631
3632 bool InputWindowsManager::GetMouseFlag()
3633 {
3634 return mouseFlag_;
3635 }
3636
3637 #ifdef OHOS_BUILD_ENABLE_POINTER
3638 void InputWindowsManager::JudgMouseIsDownOrUp(bool dragState)
3639 {
3640 auto lastPointerEventCopy = GetlastPointerEvent();
3641 CHKPV(lastPointerEventCopy);
3642 if (!dragState && (lastPointerEventCopy->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_UP ||
3643 pointerActionFlag_ == PointerEvent::POINTER_ACTION_BUTTON_DOWN)) {
3644 SetMouseFlag(true);
3645 return;
3646 }
3647 if (lastPointerEventCopy->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
3648 SetMouseFlag(true);
3649 }
3650 }
3651 #endif // OHOS_BUILD_ENABLE_POINTER
3652
3653 int32_t InputWindowsManager::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
3654 {
3655 if (windowId < 0) {
3656 MMI_HILOGE("Windowid(%{public}d) is invalid", windowId);
3657 return RET_ERR;
3658 }
3659 if ((captureModeInfo_.isCaptureMode == isCaptureMode) && !isCaptureMode) {
3660 MMI_HILOGE("Windowid:(%{public}d) is not capture mode", windowId);
3661 return RET_OK;
3662 }
3663 captureModeInfo_.windowId = windowId;
3664 captureModeInfo_.isCaptureMode = isCaptureMode;
3665 MMI_HILOGI("Windowid:(%{public}d) is (%{public}d)", windowId, isCaptureMode);
3666 return RET_OK;
3667 }
3668
3669 bool InputWindowsManager::GetMouseIsCaptureMode() const
3670 {
3671 return captureModeInfo_.isCaptureMode;
3672 }
3673
3674 bool InputWindowsManager::IsNeedDrawPointer(PointerEvent::PointerItem &pointerItem) const
3675 {
3676 if (pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN) {
3677 static int32_t lastDeviceId = -1;
3678 static std::shared_ptr<InputDevice> inputDevice = nullptr;
3679 auto nowId = pointerItem.GetDeviceId();
3680 if (lastDeviceId != nowId) {
3681 inputDevice = INPUT_DEV_MGR->GetInputDevice(nowId);
3682 CHKPF(inputDevice);
3683 lastDeviceId = nowId;
3684 }
3685 if (inputDevice != nullptr) {
3686 MMI_HILOGD("name:%{public}s type:%{public}d bus:%{public}d, "
3687 "version:%{public}d product:%{public}d vendor:%{public}d, "
3688 "phys:%{public}s uniq:%{public}s",
3689 inputDevice->GetName().c_str(), inputDevice->GetType(), inputDevice->GetBus(),
3690 inputDevice->GetVersion(), inputDevice->GetProduct(), inputDevice->GetVendor(),
3691 inputDevice->GetPhys().c_str(), inputDevice->GetUniq().c_str());
3692 }
3693 if (inputDevice != nullptr && inputDevice->GetBus() == BUS_USB) {
3694 return true;
3695 }
3696 }
3697 return false;
3698 }
3699
3700 #ifdef OHOS_BUILD_ENABLE_TOUCH
3701 bool InputWindowsManager::SkipAnnotationWindow(uint32_t flag, int32_t toolType)
3702 {
3703 return ((flag & WindowInfo::FLAG_BIT_HANDWRITING) == WindowInfo::FLAG_BIT_HANDWRITING &&
3704 toolType == PointerEvent::TOOL_TYPE_FINGER);
3705 }
3706
3707 bool InputWindowsManager::SkipNavigationWindow(WindowInputType windowType, int32_t toolType)
3708 {
3709 MMI_HILOGD("windowType:%{public}d, toolType:%{public}d", static_cast<int32_t>(windowType), toolType);
3710 if ((windowType != WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE &&
3711 windowType != WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) || toolType != PointerEvent::TOOL_TYPE_PEN) {
3712 return false;
3713 }
3714 if (!isOpenAntiMisTakeObserver_) {
3715 antiMistake_.switchName = NAVIGATION_SWITCH_NAME;
3716 CreateAntiMisTakeObserver(antiMistake_);
3717 isOpenAntiMisTakeObserver_ = true;
3718 MMI_HILOGI("Get anti mistake touch switch start");
3719 SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).GetBoolValue(NAVIGATION_SWITCH_NAME,
3720 antiMistake_.isOpen);
3721 MMI_HILOGI("Get anti mistake touch switch end");
3722 }
3723 if (antiMistake_.isOpen) {
3724 MMI_HILOGI("Anti mistake switch is open");
3725 return true;
3726 }
3727 return false;
3728 }
3729
3730 void InputWindowsManager::GetUIExtentionWindowInfo(std::vector<WindowInfo> &uiExtentionWindowInfo, int32_t windowId,
3731 WindowInfo **touchWindow, bool &isUiExtentionWindow)
3732 {
3733 for (auto &windowinfo : uiExtentionWindowInfo) {
3734 if (windowinfo.id == windowId) {
3735 *touchWindow = &windowinfo;
3736 isUiExtentionWindow = true;
3737 break;
3738 }
3739 }
3740 }
3741 #endif // OHOS_BUILD_ENABLE_TOUCH
3742
3743 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3744 bool InputWindowsManager::IsValidNavigationWindow(const WindowInfo& touchWindow, double physicalX, double physicalY)
3745 {
3746 return (touchWindow.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE ||
3747 touchWindow.windowInputType == WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE) &&
3748 IsInHotArea(static_cast<int32_t>(physicalX), static_cast<int32_t>(physicalY),
3749 touchWindow.defaultHotAreas, touchWindow);
3750 }
3751
3752 bool InputWindowsManager::IsNavigationWindowInjectEvent(std::shared_ptr<PointerEvent> pointerEvent)
3753 {
3754 return (pointerEvent->GetZOrder() > 0 && pointerEvent->GetTargetWindowId() == -1);
3755 }
3756
3757 #ifdef OHOS_BUILD_ENABLE_ONE_HAND_MODE
3758 void InputWindowsManager::UpdateDisplayXYInOneHandMode(double &physicalX, double &physicalY,
3759 const DisplayInfo &displayInfo, float oneHandScale)
3760 {
3761 int32_t oneHandXMax = displayInfo.oneHandX + displayInfo.width * oneHandScale;
3762 if ((physicalY >= displayInfo.oneHandY) && (physicalX >= displayInfo.oneHandX) && (physicalX <= oneHandXMax)) {
3763 double virtualY = physicalY - displayInfo.oneHandY;
3764 double virtualX = physicalX - displayInfo.oneHandX;
3765 physicalX = virtualX / oneHandScale;
3766 physicalY = virtualY / oneHandScale;
3767 }
3768 }
3769
3770 void InputWindowsManager::UpdatePointerItemInOneHandMode(const DisplayInfo &displayInfo,
3771 std::shared_ptr<PointerEvent> &pointerEvent)
3772 {
3773 int32_t pointerId = pointerEvent->GetPointerId();
3774 PointerEvent::PointerItem pointerItem;
3775 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3776 MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
3777 return;
3778 }
3779 double physicalX = pointerItem.GetDisplayXPos();
3780 double physicalY = pointerItem.GetDisplayYPos();
3781 if (displayInfo.height == 0 || displayInfo.height == displayInfo.oneHandY) {
3782 MMI_HILOG_DISPATCHE("displayInfo.height=%{private}d, displayInfo.oneHandY=%{private}d is invalid",
3783 displayInfo.height, displayInfo.oneHandY);
3784 pointerEvent->SetFixedMode(PointerEvent::FixedMode::SCREEN_MODE_UNKNOWN);
3785 pointerItem.SetFixedDisplayX(static_cast<int32_t>(physicalX));
3786 pointerItem.SetFixedDisplayY(static_cast<int32_t>(physicalY));
3787 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3788 return;
3789 }
3790 bool autoToVirtualScreen = pointerEvent->GetAutoToVirtualScreen();
3791 if (displayInfo.scalePercent < 100) {
3792 pointerEvent->SetFixedMode(PointerEvent::FixedMode::ONE_HAND);
3793 MMI_HILOG_DISPATCHI("displayInfo.oneHandX=%{public}d, displayInfo.oneHandY=%{public}d, "
3794 "expandHeight=%{public}d,scalePercent=%{public}d, fixedModeStr=%{public}s",
3795 displayInfo.oneHandX, displayInfo.oneHandY, displayInfo.expandHeight, displayInfo.scalePercent,
3796 pointerEvent->GetFixedModeStr().c_str());
3797 double fixedDisplayX = physicalX;
3798 double fixedDisplayY = physicalY;
3799 float oneHandScale = displayInfo.scalePercent * 1.0 / 100;
3800 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
3801 MMI_HILOG_DISPATCHD("autoToVirtualScreen=%{public}s", autoToVirtualScreen ? "true" : "false");
3802 if (autoToVirtualScreen) {
3803 UpdateDisplayXYInOneHandMode(fixedDisplayX, fixedDisplayY, displayInfo, oneHandScale);
3804 }
3805 } else {
3806 UpdateDisplayXYInOneHandMode(fixedDisplayX, fixedDisplayY, displayInfo, oneHandScale);
3807 }
3808 pointerItem.SetFixedDisplayX(static_cast<int32_t>(fixedDisplayX));
3809 pointerItem.SetFixedDisplayY(static_cast<int32_t>(fixedDisplayY));
3810 } else {
3811 pointerEvent->SetFixedMode(PointerEvent::FixedMode::NORMAL);
3812 pointerItem.SetFixedDisplayX(static_cast<int32_t>(physicalX));
3813 pointerItem.SetFixedDisplayY(static_cast<int32_t>(physicalY));
3814 MMI_HILOG_DISPATCHD("displayInfo.oneHandY=%{private}d, fixedModeStr=%{public}s",
3815 displayInfo.oneHandY, pointerEvent->GetFixedModeStr().c_str());
3816 }
3817 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3818 MMI_HILOG_DISPATCHD("targetDisplayId=%{private}d, DX=%{private}d, DY=%{private}d, FDX=%{private}d, "
3819 "FDY=%{private}d", pointerEvent->GetTargetDisplayId(), pointerItem.GetDisplayX(),
3820 pointerItem.GetDisplayY(), pointerItem.GetFixedDisplayX(), pointerItem.GetFixedDisplayY());
3821 }
3822 #endif // OHOS_BUILD_ENABLE_ONE_HAND_MODE
3823
3824 void InputWindowsManager::UpdateFixedXY(const DisplayInfo& displayInfo, std::shared_ptr<PointerEvent> &pointerEvent)
3825 {
3826 #ifdef OHOS_BUILD_ENABLE_ONE_HAND_MODE
3827 UpdatePointerItemInOneHandMode(displayInfo, pointerEvent);
3828 #else
3829 int32_t pointerId = pointerEvent->GetPointerId();
3830 PointerEvent::PointerItem pointerItem;
3831 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3832 MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
3833 return;
3834 }
3835 pointerItem.SetFixedDisplayX(pointerItem.GetDisplayX());
3836 pointerItem.SetFixedDisplayY(pointerItem.GetDisplayY());
3837 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3838 #endif // OHOS_BUILD_ENABLE_ONE_HAND_MODE
3839 }
3840
3841 void InputWindowsManager::UpdateTransformDisplayXY(std::shared_ptr<PointerEvent> pointerEvent,
3842 const std::vector<WindowInfo>& windowsInfo, const DisplayInfo& displayInfo)
3843 {
3844 CHKPV(pointerEvent);
3845 bool isNavigationWindow = false;
3846 int32_t pointerId = pointerEvent->GetPointerId();
3847 PointerEvent::PointerItem pointerItem;
3848
3849 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3850 MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
3851 return;
3852 }
3853 double physicalX = 0;
3854 double physicalY = 0;
3855 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
3856 physicalX = pointerItem.GetDisplayX();
3857 physicalY = pointerItem.GetDisplayY();
3858 } else {
3859 physicalX = pointerItem.GetDisplayXPos();
3860 physicalY = pointerItem.GetDisplayYPos();
3861 }
3862 for (auto &item : windowsInfo) {
3863 if (IsValidNavigationWindow(item, physicalX, physicalY) &&
3864 !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION) && pointerEvent->GetZOrder() <= 0) {
3865 isNavigationWindow = true;
3866 break;
3867 }
3868 }
3869 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) ||
3870 pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION) ||
3871 IsNavigationWindowInjectEvent(pointerEvent)) {
3872 if (!displayInfo.transform.empty() &&
3873 ((pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_UP) ||
3874 pointerEvent->GetZOrder() > 0) && !isNavigationWindow) {
3875 auto displayXY = TransformDisplayXY(displayInfo, physicalX, physicalY);
3876 physicalX = displayXY.first;
3877 physicalY = displayXY.second;
3878 }
3879 }
3880 if (isNavigationWindow && pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) {
3881 pointerEvent->AddFlag(InputEvent::EVENT_FLAG_SIMULATE_NAVIGATION);
3882 }
3883
3884 pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
3885 pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
3886 pointerItem.SetDisplayXPos(physicalX);
3887 pointerItem.SetDisplayYPos(physicalY);
3888 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3889 UpdateFixedXY(displayInfo, pointerEvent);
3890 }
3891 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3892
3893 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
3894 void InputWindowsManager::SendUIExtentionPointerEvent(int32_t logicalX, int32_t logicalY,
3895 const WindowInfo& windowInfo, std::shared_ptr<PointerEvent> pointerEvent)
3896 {
3897 MMI_HILOG_DISPATCHI("Dispatch uiExtention pointer Event,pid:%{public}d", windowInfo.pid);
3898 int32_t pointerId = pointerEvent->GetPointerId();
3899 PointerEvent::PointerItem pointerItem;
3900 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3901 MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
3902 return;
3903 }
3904 auto windowX = logicalX - windowInfo.area.x;
3905 auto windowY = logicalY - windowInfo.area.y;
3906 if (!(windowInfo.transform.empty())) {
3907 auto windowXY = TransformWindowXY(windowInfo, logicalX, logicalY);
3908 windowX = windowXY.first;
3909 windowY = windowXY.second;
3910 }
3911 auto physicDisplayInfo = GetPhysicalDisplay(pointerEvent->GetTargetDisplayId());
3912 CHKPV(physicDisplayInfo);
3913 double physicalX = logicalX - physicDisplayInfo->x;
3914 double physicalY = logicalY - physicDisplayInfo->y;
3915 pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
3916 pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
3917 pointerItem.SetWindowX(static_cast<int32_t>(windowX));
3918 pointerItem.SetWindowY(static_cast<int32_t>(windowY));
3919 pointerItem.SetTargetWindowId(windowInfo.id);
3920 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
3921 auto fd = udsServer_->GetClientFd(windowInfo.pid);
3922 auto sess = udsServer_->GetSession(fd);
3923 CHKPRV(sess, "The window has disappeared");
3924 NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
3925 InputEventDataTransformation::Marshalling(pointerEvent, pkt);
3926 if (!sess->SendMsg(pkt)) {
3927 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
3928 return;
3929 }
3930 }
3931
3932 void InputWindowsManager::DispatchUIExtentionPointerEvent(int32_t logicalX, int32_t logicalY,
3933 std::shared_ptr<PointerEvent> pointerEvent)
3934 {
3935 auto displayId = pointerEvent->GetTargetDisplayId();
3936 const std::vector<WindowInfo> &windowsInfo = GetWindowGroupInfoByDisplayId(displayId);
3937 auto windowId = pointerEvent->GetTargetWindowId();
3938 for (const auto& item : windowsInfo) {
3939 if (windowId == item.id) {
3940 return;
3941 }
3942 for (const auto& windowInfo : item.uiExtentionWindowInfo) {
3943 if (windowInfo.id == windowId) {
3944 MMI_HILOG_DISPATCHI("Dispatch uiExtention pointer Event,windowId:%{public}d", item.id);
3945 // If the event is sent to the security sub window, then a copy needs to be sent to the host window
3946 pointerEvent->SetAgentWindowId(item.agentWindowId);
3947 pointerEvent->SetTargetWindowId(item.id);
3948 SendUIExtentionPointerEvent(logicalX, logicalY, item, pointerEvent);
3949 pointerEvent->SetAgentWindowId(windowInfo.agentWindowId);
3950 pointerEvent->SetTargetWindowId(windowInfo.id);
3951 return;
3952 }
3953 }
3954 }
3955 }
3956 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
3957
3958 #ifdef OHOS_BUILD_ENABLE_TOUCH
3959 void InputWindowsManager::HandleGestureInjection(bool gestureInject) {
3960 if (!gestureInject) {
3961 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
3962 }
3963 }
3964
3965 int32_t InputWindowsManager::UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)
3966 {
3967 CHKPR(pointerEvent, ERROR_NULL_POINTER);
3968 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_CANCEL) {
3969 MMI_HILOG_DISPATCHD("Abort UpdateTouchScreenTarget due to POINTER_ACTION_CANCEL");
3970 return RET_OK;
3971 }
3972
3973 auto displayId = pointerEvent->GetTargetDisplayId();
3974 if (!UpdateDisplayId(displayId)) {
3975 MMI_HILOG_DISPATCHE("This display is not existent");
3976 return RET_ERR;
3977 }
3978 pointerEvent->SetTargetDisplayId(displayId);
3979
3980 auto physicDisplayInfo = GetPhysicalDisplay(displayId);
3981 CHKPR(physicDisplayInfo, ERROR_NULL_POINTER);
3982 const std::vector<WindowInfo> &windowsInfo = GetWindowGroupInfoByDisplayId(displayId);
3983 UpdateTransformDisplayXY(pointerEvent, windowsInfo, *physicDisplayInfo);
3984 int32_t pointerId = pointerEvent->GetPointerId();
3985 PointerEvent::PointerItem pointerItem;
3986 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
3987 MMI_HILOG_DISPATCHE("Can't find pointer item, pointer:%{public}d", pointerId);
3988 return RET_ERR;
3989 }
3990 double physicalX = 0;
3991 double physicalY = 0;
3992 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
3993 physicalX = pointerItem.GetDisplayX();
3994 physicalY = pointerItem.GetDisplayY();
3995 } else {
3996 physicalX = pointerItem.GetDisplayXPos();
3997 physicalY = pointerItem.GetDisplayYPos();
3998 }
3999 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
4000 AdjustDisplayCoordinate(*physicDisplayInfo, physicalX, physicalY);
4001 }
4002 int32_t logicalX1 = 0;
4003 int32_t logicalY1 = 0;
4004
4005 if (!AddInt32(static_cast<int32_t>(physicalX), physicDisplayInfo->x, logicalX1)) {
4006 MMI_HILOG_DISPATCHE("The addition of logicalX overflows");
4007 return RET_ERR;
4008 }
4009 if (!AddInt32(static_cast<int32_t>(physicalY), physicDisplayInfo->y, logicalY1)) {
4010 MMI_HILOG_DISPATCHE("The addition of logicalY overflows");
4011 return RET_ERR;
4012 }
4013 double logicalX = physicalX + physicDisplayInfo->x;
4014 double logicalY = physicalY + physicDisplayInfo->y;
4015 const WindowInfo *touchWindow = nullptr;
4016 auto targetWindowId = pointerItem.GetTargetWindowId();
4017 bool isHotArea = false;
4018 bool isFirstSpecialWindow = false;
4019 static std::unordered_map<int32_t, WindowInfo> winMap;
4020 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4021 ClearTargetWindowId(pointerId);
4022 }
4023 for (auto &item : windowsInfo) {
4024 bool checkWindow = (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE ||
4025 !IsValidZorderWindow(item, pointerEvent);
4026 if (checkWindow) {
4027 MMI_HILOG_DISPATCHD("Skip the untouchable or invalid zOrder window to continue searching,"
4028 "window:%{public}d, flags:%{public}d", item.id, item.flags);
4029 winMap.insert({item.id, item});
4030 continue;
4031 }
4032 if (transparentWins_.find(item.id) != transparentWins_.end()) {
4033 if (IsTransparentWin(transparentWins_[item.id], logicalX - item.area.x, logicalY - item.area.y)) {
4034 MMI_HILOG_DISPATCHE("It's an abnormal window and touchscreen find the next window");
4035 winMap.insert({item.id, item});
4036 continue;
4037 }
4038 }
4039 if (SkipAnnotationWindow(item.flags, pointerItem.GetToolType())) {
4040 winMap.insert({item.id, item});
4041 continue;
4042 }
4043 if (SkipNavigationWindow(item.windowInputType, pointerItem.GetToolType())) {
4044 winMap.insert({item.id, item});
4045 continue;
4046 }
4047 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) && item.windowType == SCREEN_CONTROL_WINDOW_TYPE) {
4048 continue;
4049 }
4050 MMI_HILOG_DISPATCHD("pointerItem.GetToolType():%{public}d, extraData_.toolType:%{public}d",
4051 pointerItem.GetToolType(), extraData_.toolType);
4052 bool checkToolType = false;
4053 if (extraData_.toolType == SourceTool::UNKNOWN) {
4054 checkToolType = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
4055 ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
4056 pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
4057 } else if (extraData_.toolType == SourceTool::FINGER) {
4058 checkToolType = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
4059 (ConvertToolType(pointerItem.GetToolType()) == extraData_.toolType &&
4060 extraData_.pointerId == pointerId);
4061 } else {
4062 checkToolType = ConvertToolType(pointerItem.GetToolType()) == extraData_.toolType;
4063 }
4064 checkToolType = checkToolType || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
4065 if (checkToolType) {
4066 MMI_HILOG_DISPATCHD("Enter checkToolType");
4067 if (IsInHotArea(static_cast<int32_t>(logicalX), static_cast<int32_t>(logicalY),
4068 item.defaultHotAreas, item)) {
4069 if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
4070 continue;
4071 }
4072 UpdateTargetTouchWinIds(item, pointerItem, pointerEvent, pointerId, displayId);
4073 touchWindow = &item;
4074 break;
4075 } else {
4076 winMap.insert({item.id, item});
4077 continue;
4078 }
4079 }
4080 if (targetWindowId >= 0 && pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN &&
4081 (pointerItem.GetToolType() != PointerEvent::TOOL_TYPE_PEN || pointerItem.GetPressure() > 0)) {
4082 bool isUiExtentionWindow = false;
4083 for (auto &windowinfo : item.uiExtentionWindowInfo) {
4084 if (windowinfo.id == targetWindowId) {
4085 touchWindow = &windowinfo;
4086 isUiExtentionWindow = true;
4087 break;
4088 }
4089 }
4090 if (isUiExtentionWindow) {
4091 break;
4092 }
4093 if (item.id == targetWindowId) {
4094 touchWindow = &item;
4095 break;
4096 }
4097 } else if (IsInHotArea(static_cast<int32_t>(logicalX), static_cast<int32_t>(logicalY),
4098 item.defaultHotAreas, item)) {
4099 touchWindow = &item;
4100 bool isSpecialWindow = HandleWindowInputType(item, pointerEvent);
4101 if (!isFirstSpecialWindow) {
4102 isFirstSpecialWindow = isSpecialWindow;
4103 if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
4104 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_PULL_MOVE &&
4105 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_HOVER_MOVE &&
4106 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
4107 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
4108 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
4109 pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE) {
4110 MMI_HILOG_DISPATCHD("the first special window status:%{public}d", isFirstSpecialWindow);
4111 }
4112 }
4113 std::pair<int32_t, int32_t> logicalXY(std::make_pair(static_cast<int32_t>(logicalX),
4114 static_cast<int32_t>(logicalY)));
4115 // Determine whether the landing point is a safety sub window
4116 CheckUIExtentionWindowDefaultHotArea(logicalXY, isHotArea, pointerEvent, item.uiExtentionWindowInfo,
4117 &touchWindow);
4118 if (isSpecialWindow) {
4119 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), item.id);
4120 isHotArea = true;
4121 continue;
4122 }
4123 break;
4124 } else {
4125 winMap.insert({item.id, item});
4126 }
4127 }
4128 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4129 std::ostringstream oss;
4130 for (auto iter = winMap.begin(); iter != winMap.end(); iter++) {
4131 oss << iter->first << "|" << iter->second.zOrder << "|";
4132 int32_t searchHotAreaCount = 0;
4133 int32_t searchHotAreaMaxCount = 4;
4134 for (auto &hotArea : iter->second.defaultHotAreas) {
4135 searchHotAreaCount++;
4136 oss << hotArea.x << "|" << hotArea.y << "|" << hotArea.width << "|" << hotArea.height << "|";
4137 if (searchHotAreaCount >= searchHotAreaMaxCount) {
4138 break;
4139 }
4140 }
4141 oss << iter->second.pid << " ";
4142 }
4143 if (!oss.str().empty()) {
4144 MMI_HILOG_DISPATCHI("Pre search window %{public}d %{public}s", targetWindowId, oss.str().c_str());
4145 }
4146 }
4147 if (touchWindow == nullptr) {
4148 auto it = touchItemDownInfos_.find(pointerId);
4149 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
4150 if (it == touchItemDownInfos_.end() ||
4151 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4152 int32_t originPointerAction = pointerEvent->GetPointerAction();
4153 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
4154 pointerEvent->SetOriginPointerAction(originPointerAction);
4155 pointerItem.SetCanceled(true);
4156 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
4157 MMI_HILOG_DISPATCHE("The touchWindow is nullptr, logicalX:%{private}f,"
4158 "logicalY:%{private}f, pointerId:%{public}d", logicalX, logicalY, pointerId);
4159 return RET_ERR;
4160 }
4161 }
4162 touchWindow = &it->second.window;
4163 if (it->second.flag) {
4164 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_MOVE ||
4165 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_ENTER ||
4166 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_EXIT ||
4167 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_HOVER_CANCEL) {
4168 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_HOVER_CANCEL);
4169 } else {
4170 int32_t originPointerAction = pointerEvent->GetPointerAction();
4171 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
4172 pointerEvent->SetOriginPointerAction(originPointerAction);
4173 }
4174 MMI_HILOG_DISPATCHI("Not found event down target window, maybe this window was untouchable,"
4175 "need send cancel event, windowId:%{public}d pointerId:%{public}d", touchWindow->id, pointerId);
4176 }
4177 }
4178 winMap.clear();
4179 pointerEvent->SetTargetWindowId(touchWindow->id);
4180 pointerItem.SetTargetWindowId(touchWindow->id);
4181 #ifdef OHOS_BUILD_ENABLE_ANCO
4182 bool isInAnco = touchWindow && IsInAncoWindow(*touchWindow, logicalX, logicalY);
4183 if (isInAnco) {
4184 MMI_HILOG_DISPATCHD("Process touch screen event in Anco window, targetWindowId:%{public}d", touchWindow->id);
4185 std::vector<int32_t> windowIds;
4186 GetTargetWindowIds(pointerId, pointerEvent->GetSourceType(), windowIds);
4187 if (windowIds.size() <= 1) {
4188 pointerEvent->SetAncoDeal(true);
4189 } else {
4190 for (int32_t windowId : windowIds) {
4191 auto windowInfo = GetWindowAndDisplayInfo(windowId, pointerEvent->GetTargetDisplayId());
4192 if (!windowInfo) {
4193 continue;
4194 }
4195 isFirstSpecialWindow = isFirstSpecialWindow || HandleWindowInputType(*windowInfo, pointerEvent);
4196 }
4197 }
4198 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
4199 // Simulate uinput automated injection operations (MMI_GE(pointerEvent->GetZOrder(), 0.0f))
4200 bool isCompensatePointer = (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) &&
4201 !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY)) ||
4202 (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_ACCESSIBILITY) &&
4203 !pointerEvent->HasFlag(InputEvent::EVENT_FLAG_GENERATE_FROM_REAL));
4204 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4205 MMI_HILOG_DISPATCHI("In Anco, WI:%{public}d, SI:%{public}d SW:%{public}d",
4206 touchWindow->id, isCompensatePointer, isFirstSpecialWindow);
4207 }
4208 if (isCompensatePointer || isFirstSpecialWindow) {
4209 SimulatePointerExt(pointerEvent);
4210 isFirstSpecialWindow = false;
4211 } else {
4212 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4213 std::unordered_map<std::string, std::string> mapPayload;
4214 mapPayload["msg"] = "";
4215 constexpr int32_t touchDownBoost = 1006;
4216 auto begin = std::chrono::high_resolution_clock::now();
4217 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
4218 OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchDownBoost, mapPayload);
4219 auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
4220 std::chrono::high_resolution_clock::now() - begin).count();
4221 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
4222 DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::RESOURCE_SCHEDULE_REPORT_DATA,
4223 durationMS);
4224 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
4225 } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_UP) {
4226 constexpr int32_t touchUpBoost = 1007;
4227 std::unordered_map<std::string, std::string> mapPayload;
4228 mapPayload["msg"] = "";
4229 auto begin = std::chrono::high_resolution_clock::now();
4230 OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
4231 OHOS::ResourceSchedule::ResType::RES_TYPE_ANCO_CUST, touchUpBoost, mapPayload);
4232 auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
4233 std::chrono::high_resolution_clock::now() - begin).count();
4234 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
4235 DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::RESOURCE_SCHEDULE_REPORT_DATA,
4236 durationMS);
4237 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
4238 }
4239 }
4240 if (displayGroupInfo_.focusWindowId == touchWindow->id) {
4241 return RET_OK;
4242 }
4243 pointerEvent->SetAncoDeal(false);
4244 }
4245 #endif // OHOS_BUILD_ENABLE_ANCO
4246 if (touchWindow->windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
4247 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
4248 lastTouchEventOnBackGesture_ = std::make_shared<PointerEvent>(*pointerEvent);
4249 }
4250 if (lastTouchEventOnBackGesture_ != nullptr &&
4251 lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL) {
4252 lastTouchEventOnBackGesture_ = std::make_shared<PointerEvent>(*pointerEvent);
4253 }
4254 }
4255 auto windowX = logicalX - touchWindow->area.x;
4256 auto windowY = logicalY - touchWindow->area.y;
4257 if (!(touchWindow->transform.empty())) {
4258 auto windowXY = TransformWindowXY(*touchWindow, physicalX, physicalY);
4259 windowX = windowXY.first;
4260 windowY = windowXY.second;
4261 }
4262 SetPrivacyModeFlag(touchWindow->privacyMode, pointerEvent);
4263 pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
4264 DispatchUIExtentionPointerEvent(logicalX, logicalY, pointerEvent);
4265 pointerItem.SetDisplayX(static_cast<int32_t>(physicalX));
4266 pointerItem.SetDisplayY(static_cast<int32_t>(physicalY));
4267 pointerItem.SetWindowX(static_cast<int32_t>(windowX));
4268 pointerItem.SetWindowY(static_cast<int32_t>(windowY));
4269 pointerItem.SetDisplayXPos(physicalX);
4270 pointerItem.SetDisplayYPos(physicalY);
4271 pointerItem.SetWindowXPos(windowX);
4272 pointerItem.SetWindowYPos(windowY);
4273 pointerItem.SetToolWindowX(pointerItem.GetToolDisplayX() + physicDisplayInfo->x - touchWindow->area.x);
4274 pointerItem.SetToolWindowY(pointerItem.GetToolDisplayY() + physicDisplayInfo->y - touchWindow->area.y);
4275 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
4276 MMI_HILOG_DISPATCHD("pointerItem.GetToolType():%{public}d, extraData_.toolType:%{public}d",
4277 pointerItem.GetToolType(), extraData_.toolType);
4278 bool checkExtraData = false;
4279 if (extraData_.toolType == SourceTool::UNKNOWN) {
4280 checkExtraData = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
4281 ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
4282 pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
4283 } else if (extraData_.toolType == SourceTool::FINGER) {
4284 checkExtraData = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
4285 (ConvertToolType(pointerItem.GetToolType()) == extraData_.toolType && extraData_.pointerId == pointerId);
4286 } else {
4287 checkExtraData = ConvertToolType(pointerItem.GetToolType()) == extraData_.toolType;
4288 }
4289 checkExtraData = checkExtraData || (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP);
4290 int32_t pointerAction = pointerEvent->GetPointerAction();
4291 if ((pointerAction == PointerEvent::POINTER_ACTION_DOWN) && !checkExtraData) {
4292 lastTouchLogicX_ = logicalX;
4293 lastTouchLogicY_ = logicalY;
4294 lastTouchEvent_ = pointerEvent;
4295 lastTouchWindowInfo_ = *touchWindow;
4296 }
4297 if (checkExtraData) {
4298 pointerEvent->SetBuffer(extraData_.buffer);
4299 pointerEvent->SetPullId(extraData_.pullId);
4300 UpdatePointerAction(pointerEvent);
4301 PullEnterLeaveEvent(logicalX, logicalY, pointerEvent, touchWindow);
4302 }
4303 // pointerAction:PA, targetWindowId:TWI, foucsWindowId:FWI, eventId:EID,
4304 // logicalX:LX, logicalY:LY, displayX:DX, displayX:DY, windowX:WX, windowY:WY,
4305 // width:W, height:H, area.x:AX, area.y:AY, displayId:DID, AgentWindowId: AWI
4306 if ((pointerAction != PointerEvent::POINTER_ACTION_MOVE &&
4307 pointerAction != PointerEvent::POINTER_ACTION_PULL_MOVE &&
4308 pointerAction != PointerEvent::POINTER_ACTION_HOVER_MOVE &&
4309 pointerAction != PointerEvent::POINTER_ACTION_AXIS_UPDATE &&
4310 pointerAction != PointerEvent::POINTER_ACTION_SWIPE_UPDATE &&
4311 pointerAction != PointerEvent::POINTER_ACTION_ROTATE_UPDATE &&
4312 pointerAction != PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE)) {
4313 if (!EventLogHelper::IsBetaVersion()) {
4314 MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
4315 "FWI:%{public}d,EID:%{public}d, flags:%{public}d,DID:%{public}d"
4316 "AWI:%{public}d,zOrder:%{public}1f",
4317 pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
4318 displayGroupInfo_.focusWindowId, pointerEvent->GetId(), touchWindow->flags,
4319 displayId, pointerEvent->GetAgentWindowId(), touchWindow->zOrder);
4320 } else {
4321 if (pointerEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
4322 MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
4323 "FWI:%{public}d,EID:%{public}d,"
4324 "W:%{public}d,H:%{public}d,AX:%{public}d,AY:%{public}d,"
4325 "flags:%{public}d,DID:%{public}d"
4326 "AWI:%{public}d,zOrder:%{public}1f",
4327 pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
4328 displayGroupInfo_.focusWindowId, pointerEvent->GetId(),
4329 touchWindow->area.width, touchWindow->area.height, touchWindow->area.x,
4330 touchWindow->area.y, touchWindow->flags, displayId,
4331 pointerEvent->GetAgentWindowId(), touchWindow->zOrder);
4332 } else {
4333 MMI_HILOG_FREEZEI("PA:%{public}s,Pid:%{public}d,TWI:%{public}d,"
4334 "FWI:%{public}d,EID:%{public}d,LX:%{public}1f,LY:%{public}1f,"
4335 "DX:%{public}1f,DY:%{public}1f,WX:%{public}1f,WY:%{public}1f,"
4336 "W:%{public}d,H:%{public}d,AX:%{public}d,AY:%{public}d,"
4337 "flags:%{public}d,DID:%{public}d"
4338 "AWI:%{public}d,zOrder:%{public}1f",
4339 pointerEvent->DumpPointerAction(), touchWindow->pid, touchWindow->id,
4340 displayGroupInfo_.focusWindowId, pointerEvent->GetId(), logicalX, logicalY, physicalX,
4341 physicalY, windowX, windowY, touchWindow->area.width, touchWindow->area.height, touchWindow->area.x,
4342 touchWindow->area.y, touchWindow->flags, displayId,
4343 pointerEvent->GetAgentWindowId(), touchWindow->zOrder);
4344 }
4345 }
4346 }
4347 bool gestureInject = false;
4348 if ((pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) && MMI_GNE(pointerEvent->GetZOrder(), 0.0f)) {
4349 gestureInject = true;
4350 }
4351 #if defined(OHOS_BUILD_ENABLE_POINTER) && (defined(OHOS_BUILD_ENABLE_POINTER_DRAWING) || defined(OHOS_BUILD_EMULATOR))
4352 if (IsNeedDrawPointer(pointerItem)) {
4353 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
4354 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
4355 if (touchWindow->id != lastWindowInfo_.id) {
4356 lastWindowInfo_ = *touchWindow;
4357 }
4358 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW, lastWindowInfo_.id);
4359 }
4360 PointerStyle pointerStyle;
4361 GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
4362 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicDisplayInfo);
4363 WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
4364 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
4365 IPointerDrawingManager::GetInstance()->DrawPointer(displayId,
4366 pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), pointerStyle, physicDisplayInfo->direction);
4367 } else if (IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
4368 if ((!checkExtraData) && (!(extraData_.appended &&
4369 extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE))) {
4370 MMI_HILOG_DISPATCHD("PointerAction is to leave the window");
4371 if (!pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SHOW_CUSOR_WITH_TOUCH) && timerId_ == DEFAULT_VALUE) {
4372 timerId_ = TimerMgr->AddTimer(REPEAT_COOLING_TIME, REPEAT_ONCE, [this, gestureInject]() {
4373 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
4374 HandleGestureInjection(gestureInject);
4375 timerId_ = DEFAULT_VALUE;
4376 });
4377 }
4378 }
4379 }
4380 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
4381 lastPointerEventforWindowChange_ = pointerEvent;
4382 lastPointerEventforGesture_ = pointerEvent;
4383 pointerAction = pointerEvent->GetPointerAction();
4384 if (pointerAction == PointerEvent::POINTER_ACTION_DOWN ||
4385 pointerAction == PointerEvent::POINTER_ACTION_HOVER_ENTER) {
4386 WindowInfoEX windowInfoEX;
4387 windowInfoEX.window = *touchWindow;
4388 windowInfoEX.flag = true;
4389 touchItemDownInfos_[pointerId] = windowInfoEX;
4390 MMI_HILOG_FREEZEI("PointerId:%{public}d, touchWindow:%{public}d", pointerId, touchWindow->id);
4391 } else if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
4392 MMI_HILOG_DISPATCHD("Clear extra data");
4393 pointerEvent->ClearBuffer();
4394 lastTouchEvent_ = nullptr;
4395 lastTouchWindowInfo_.id = -1;
4396 ClearExtraData();
4397 }
4398 return ERR_OK;
4399 }
4400
4401 void InputWindowsManager::UpdateTargetTouchWinIds(const WindowInfo &item, PointerEvent::PointerItem &pointerItem,
4402 std::shared_ptr<PointerEvent> pointerEvent, int32_t pointerId, int32_t displayId) {
4403 if (item.windowInputType != WindowInputType::TRANSMIT_ALL) {
4404 WIN_MGR->GetTargetWindowIds(pointerItem.GetPointerId(), pointerEvent->GetSourceType(),
4405 targetTouchWinIds_[pointerId]);
4406 if (!targetTouchWinIds_[pointerId].empty()) {
4407 ClearMismatchTypeWinIds(pointerId, displayId);
4408 targetTouchWinIds_[pointerId].push_back(item.id);
4409 }
4410 }
4411 }
4412
4413 void InputWindowsManager::ClearMismatchTypeWinIds(int32_t pointerId, int32_t displayId) {
4414 for (int32_t windowId : targetTouchWinIds_[pointerId]) {
4415 auto windowInfo = WIN_MGR->GetWindowAndDisplayInfo(windowId, displayId);
4416 if (windowInfo->windowInputType != WindowInputType::TRANSMIT_ALL) {
4417 auto it = std::find(targetTouchWinIds_[pointerId].begin(), targetTouchWinIds_[pointerId].end(), windowId);
4418 if (it != targetTouchWinIds_[pointerId].end()) {
4419 targetTouchWinIds_[pointerId].erase(it);
4420 }
4421 }
4422 }
4423 }
4424
4425 void InputWindowsManager::CheckUIExtentionWindowDefaultHotArea(std::pair<int32_t, int32_t> logicalXY,
4426 bool isHotArea, const std::shared_ptr<PointerEvent> pointerEvent, const std::vector<WindowInfo>& windowInfos,
4427 const WindowInfo** touchWindow)
4428 {
4429 CHKPV(pointerEvent);
4430 CHKPV(touchWindow);
4431 CHKPV(*touchWindow);
4432 int32_t uiExtentionWindowId = 0;
4433 int32_t windowId = (*touchWindow)->id;
4434 int32_t logicalX = logicalXY.first;
4435 int32_t logicalY = logicalXY.second;
4436 for (const auto& it : windowInfos) {
4437 if (IsInHotArea(logicalX, logicalY, it.defaultHotAreas, it)) {
4438 uiExtentionWindowId = it.id;
4439 break;
4440 }
4441 }
4442 if (uiExtentionWindowId > 0) {
4443 for (auto &windowinfo : windowInfos) {
4444 if (windowinfo.id == uiExtentionWindowId) {
4445 *touchWindow = &windowinfo;
4446 MMI_HILOG_DISPATCHD("uiExtentionWindowid:%{public}d", uiExtentionWindowId);
4447 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), uiExtentionWindowId);
4448 break;
4449 }
4450 }
4451 }
4452 if (isHotArea) {
4453 AddTargetWindowIds(pointerEvent->GetPointerId(), pointerEvent->GetSourceType(), windowId);
4454 }
4455 }
4456
4457 void InputWindowsManager::PullEnterLeaveEvent(int32_t logicalX, int32_t logicalY,
4458 const std::shared_ptr<PointerEvent> pointerEvent, const WindowInfo* touchWindow)
4459 {
4460 CHKPV(pointerEvent);
4461 CHKPV(touchWindow);
4462 MMI_HILOG_DISPATCHD("LastTouchWindowInfo:%{public}d, touchWindow:%{public}d",
4463 lastTouchWindowInfo_.id, touchWindow->id);
4464 if (lastTouchWindowInfo_.id != touchWindow->id) {
4465 if (lastTouchWindowInfo_.id != -1) {
4466 DispatchTouch(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
4467 }
4468 lastTouchLogicX_ = logicalX;
4469 lastTouchLogicY_ = logicalY;
4470 lastTouchEvent_ = pointerEvent;
4471 lastTouchWindowInfo_ = *touchWindow;
4472 DispatchTouch(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
4473 return;
4474 }
4475 lastTouchLogicX_ = logicalX;
4476 lastTouchLogicY_ = logicalY;
4477 lastTouchEvent_ = pointerEvent;
4478 lastTouchWindowInfo_ = *touchWindow;
4479 }
4480
4481 void InputWindowsManager::DispatchTouch(int32_t pointerAction)
4482 {
4483 CALL_INFO_TRACE;
4484 CHKPV(udsServer_);
4485 CHKPV(lastTouchEvent_);
4486 if (pointerAction == PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
4487 WindowInfo touchWindow;
4488 bool isChanged { false };
4489 for (const auto &item : displayGroupInfo_.windowsInfo) {
4490 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
4491 MMI_HILOGD("Skip the untouchable window to continue searching, "
4492 "window:%{public}d, flags:%{public}d", item.id, item.flags);
4493 continue;
4494 }
4495 if (item.windowInputType == WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE) {
4496 continue;
4497 }
4498 if (IsInHotArea(lastTouchLogicX_, lastTouchLogicY_, item.defaultHotAreas, item)) {
4499 touchWindow = item;
4500 isChanged = true;
4501 break;
4502 }
4503 }
4504 if (!isChanged) {
4505 MMI_HILOGE("touchWindow is not init");
4506 return;
4507 }
4508 if (touchWindow.id != lastTouchWindowInfo_.id) {
4509 lastTouchWindowInfo_ = touchWindow;
4510 }
4511 }
4512 auto pointerEvent = PointerEvent::Create();
4513 CHKPV(pointerEvent);
4514 PointerEvent::PointerItem lastPointerItem;
4515 int32_t lastPointerId = lastTouchEvent_->GetPointerId();
4516 if (!lastTouchEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
4517 MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
4518 return;
4519 }
4520 PointerEvent::PointerItem currentPointerItem;
4521 currentPointerItem.SetWindowX(lastTouchLogicX_ - lastTouchWindowInfo_.area.x);
4522 currentPointerItem.SetWindowY(lastTouchLogicY_ - lastTouchWindowInfo_.area.y);
4523 currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
4524 currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
4525 currentPointerItem.SetPointerId(lastPointerId);
4526
4527 pointerEvent->UpdateId();
4528 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
4529 pointerEvent->SetTargetDisplayId(lastTouchEvent_->GetTargetDisplayId());
4530 SetPrivacyModeFlag(lastTouchWindowInfo_.privacyMode, pointerEvent);
4531 pointerEvent->SetTargetWindowId(lastTouchWindowInfo_.id);
4532 pointerEvent->SetAgentWindowId(lastTouchWindowInfo_.agentWindowId);
4533 pointerEvent->SetPointerId(lastPointerId);
4534 pointerEvent->AddPointerItem(currentPointerItem);
4535 pointerEvent->SetPointerAction(pointerAction);
4536 pointerEvent->SetBuffer(extraData_.buffer);
4537 pointerEvent->SetPullId(extraData_.pullId);
4538 pointerEvent->SetSourceType(lastTouchEvent_->GetSourceType());
4539 int64_t time = GetSysClockTime();
4540 pointerEvent->SetActionTime(time);
4541 pointerEvent->SetActionStartTime(time);
4542 pointerEvent->SetDeviceId(lastTouchEvent_->GetDeviceId());
4543 auto fd = udsServer_->GetClientFd(lastTouchWindowInfo_.pid);
4544 auto sess = udsServer_->GetSession(fd);
4545 if (sess == nullptr) {
4546 MMI_HILOGI("The last window has disappeared");
4547 return;
4548 }
4549
4550 EventLogHelper::PrintEventData(pointerEvent, MMI_LOG_FREEZE);
4551 auto filter = InputHandler->GetFilterHandler();
4552 CHKPV(filter);
4553 filter->HandlePointerEvent(pointerEvent);
4554 }
4555 #endif // OHOS_BUILD_ENABLE_TOUCH
4556
4557 #ifdef OHOS_BUILD_ENABLE_POINTER
4558 int32_t InputWindowsManager::UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)
4559 {
4560 CALL_DEBUG_ENTER;
4561 int32_t pointerAction = pointerEvent->GetPointerAction();
4562 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
4563 switch (pointerAction) {
4564 case PointerEvent::POINTER_ACTION_BUTTON_DOWN:
4565 case PointerEvent::POINTER_ACTION_BUTTON_UP:
4566 case PointerEvent::POINTER_ACTION_MOVE: {
4567 return UpdateMouseTarget(pointerEvent);
4568 }
4569 case PointerEvent::POINTER_ACTION_DOWN: {
4570 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
4571 pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
4572 pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
4573 return UpdateMouseTarget(pointerEvent);
4574 }
4575 case PointerEvent::POINTER_ACTION_UP: {
4576 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
4577 pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
4578 pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
4579 return UpdateMouseTarget(pointerEvent);
4580 }
4581 default: {
4582 MMI_HILOG_DISPATCHE("pointer action is unknown, pointerAction:%{public}d", pointerAction);
4583 return RET_ERR;
4584 }
4585 }
4586 return RET_OK;
4587 }
4588 #endif // OHOS_BUILD_ENABLE_POINTER
4589
4590 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
4591 int32_t InputWindowsManager::UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)
4592 {
4593 CALL_DEBUG_ENTER;
4594 CHKPR(pointerEvent, ERROR_NULL_POINTER);
4595 int32_t focusWindowId = displayGroupInfo_.focusWindowId;
4596 const WindowInfo* windowInfo = nullptr;
4597 std::vector<WindowInfo> windowsInfo = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
4598 for (const auto &item : windowsInfo) {
4599 if (item.id == focusWindowId) {
4600 windowInfo = &item;
4601 break;
4602 }
4603 }
4604 CHKPR(windowInfo, ERROR_NULL_POINTER);
4605 SetPrivacyModeFlag(windowInfo->privacyMode, pointerEvent);
4606 pointerEvent->SetTargetDisplayId(windowInfo->displayId);
4607 pointerEvent->SetTargetWindowId(windowInfo->id);
4608 pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
4609 MMI_HILOG_DISPATCHD("focusWindow:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
4610
4611 return RET_OK;
4612 }
4613 #endif // OHOS_BUILD_ENABLE_JOYSTICK
4614
4615 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_CROWN)
4616 int32_t InputWindowsManager::UpdateCrownTarget(std::shared_ptr<PointerEvent> pointerEvent)
4617 {
4618 CALL_DEBUG_ENTER;
4619 CHKPR(pointerEvent, ERROR_NULL_POINTER);
4620 return UpdateMouseTarget(pointerEvent);
4621 }
4622 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_CROWN
4623
4624 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
4625 void InputWindowsManager::DrawTouchGraphic(std::shared_ptr<PointerEvent> pointerEvent)
4626 {
4627 CALL_DEBUG_ENTER;
4628 CHKPV(pointerEvent);
4629 #ifdef OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
4630 if (knuckleDrawMgr_ == nullptr) {
4631 knuckleDrawMgr_ = std::make_shared<KnuckleDrawingManager>();
4632 }
4633 #ifndef OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
4634 if (knuckleDynamicDrawingManager_ == nullptr) {
4635 knuckleDynamicDrawingManager_ = std::make_shared<KnuckleDynamicDrawingManager>();
4636 if (knuckleDrawMgr_ != nullptr) {
4637 knuckleDynamicDrawingManager_->SetKnuckleDrawingManager(knuckleDrawMgr_);
4638 }
4639 }
4640 #endif // OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
4641 #endif // OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
4642 auto displayId = pointerEvent->GetTargetDisplayId();
4643 if (!UpdateDisplayId(displayId)) {
4644 MMI_HILOGE("This display is not exist");
4645 return;
4646 }
4647 auto physicDisplayInfo = GetPhysicalDisplay(displayId);
4648 CHKPV(physicDisplayInfo);
4649 #if defined(OHOS_BUILD_ENABLE_KEYBOARD) && defined(OHOS_BUILD_ENABLE_COMBINATION_KEY) && \
4650 defined(OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER)
4651 std::shared_ptr<OHOS::MMI::InputEventHandler> inputHandler = InputHandler;
4652 CHKPV(InputHandler->GetKeyCommandHandler());
4653 auto knuckleSwitch = InputHandler->GetKeyCommandHandler()->GetKnuckleSwitchValue();
4654 auto isInMethodWindow = InputHandler->GetKeyCommandHandler()->CheckInputMethodArea(pointerEvent);
4655 if (isInMethodWindow) {
4656 int32_t pointerId = pointerEvent->GetPointerId();
4657 PointerEvent::PointerItem item;
4658 if (!pointerEvent->GetPointerItem(pointerId, item)) {
4659 MMI_HILOGE("Invalid pointer:%{public}d", pointerId);
4660 return;
4661 }
4662 if (item.GetToolType() == PointerEvent::TOOL_TYPE_KNUCKLE) {
4663 item.SetToolType(PointerEvent::TOOL_TYPE_FINGER);
4664 pointerEvent->UpdatePointerItem(pointerId, item);
4665 }
4666 }
4667 if (!knuckleSwitch && !isInMethodWindow) {
4668 knuckleDrawMgr_->UpdateDisplayInfo(*physicDisplayInfo);
4669 knuckleDrawMgr_->KnuckleDrawHandler(pointerEvent);
4670 #ifndef OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
4671 knuckleDynamicDrawingManager_->UpdateDisplayInfo(*physicDisplayInfo);
4672 knuckleDynamicDrawingManager_->KnuckleDynamicDrawHandler(pointerEvent);
4673 #endif // OHOS_BUILD_ENABLE_NEW_KNUCKLE_DYNAMIC
4674 }
4675 #endif // OHOS_BUILD_ENABLE_KEYBOARD && OHOS_BUILD_ENABLE_COMBINATION_KEY && OHOS_BUILD_ENABLE_GESTURESENSE_WRAPPER
4676
4677 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
4678 TOUCH_DRAWING_MGR->UpdateDisplayInfo(*physicDisplayInfo);
4679 TOUCH_DRAWING_MGR->TouchDrawHandler(pointerEvent);
4680 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
4681 }
4682
4683 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
4684
4685 template <class T>
4686 void InputWindowsManager::CreateAntiMisTakeObserver(T& item)
4687 {
4688 CALL_INFO_TRACE;
4689 SettingObserver::UpdateFunc updateFunc = [&item](const std::string& key) {
4690 if (SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).GetBoolValue(key, item.isOpen) != RET_OK) {
4691 MMI_HILOGE("Get settingdata failed, key:%{public}s", key.c_str());
4692 }
4693 MMI_HILOGI("Anti mistake observer key:%{public}s, statusValue:%{public}d", key.c_str(), item.isOpen);
4694 };
4695 sptr<SettingObserver> statusObserver = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID)
4696 .CreateObserver(item.switchName, updateFunc);
4697 CHKPV(statusObserver);
4698 ErrCode ret = SettingDataShare::GetInstance(MULTIMODAL_INPUT_SERVICE_ID).RegisterObserver(statusObserver);
4699 if (ret != ERR_OK) {
4700 MMI_HILOGE("Register setting observer failed, ret:%{public}d", ret);
4701 statusObserver = nullptr;
4702 }
4703 }
4704
4705 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
4706 int32_t InputWindowsManager::UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)
4707 {
4708 CALL_DEBUG_ENTER;
4709 CHKPR(pointerEvent, ERROR_NULL_POINTER);
4710 auto source = pointerEvent->GetSourceType();
4711 pointerActionFlag_ = pointerEvent->GetPointerAction();
4712 #ifdef OHOS_BUILD_ENABLE_ANCO
4713 pointerEvent->SetAncoDeal(false);
4714 #endif // OHOS_BUILD_ENABLE_ANCO
4715 if (IsFoldable_ && IgnoreTouchEvent(pointerEvent)) {
4716 MMI_HILOG_DISPATCHD("Ignore touch event, pointerAction:%{public}d", pointerActionFlag_);
4717 return RET_OK;
4718 };
4719 switch (source) {
4720 #ifdef OHOS_BUILD_ENABLE_TOUCH
4721 case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
4722 return UpdateTouchScreenTarget(pointerEvent);
4723 }
4724 #endif // OHOS_BUILD_ENABLE_TOUCH
4725 #ifdef OHOS_BUILD_ENABLE_POINTER
4726 case PointerEvent::SOURCE_TYPE_MOUSE: {
4727 return UpdateMouseTarget(pointerEvent);
4728 }
4729 case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
4730 return UpdateTouchPadTarget(pointerEvent);
4731 }
4732 #endif // OHOS_BUILD_ENABLE_POINTER
4733 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
4734 case PointerEvent::SOURCE_TYPE_JOYSTICK: {
4735 return UpdateJoystickTarget(pointerEvent);
4736 }
4737 #endif // OHOS_BUILD_ENABLE_JOYSTICK
4738 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_CROWN)
4739 case PointerEvent::SOURCE_TYPE_CROWN: {
4740 return UpdateCrownTarget(pointerEvent);
4741 }
4742 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_CROWN
4743 default: {
4744 MMI_HILOG_DISPATCHE("Source type is unknown, source:%{public}d", source);
4745 break;
4746 }
4747 }
4748 return RET_ERR;
4749 }
4750
4751 bool InputWindowsManager::IsInsideDisplay(const DisplayInfo& displayInfo, double physicalX, double physicalY)
4752 {
4753 auto displayDirection = GetDisplayDirection(&displayInfo);
4754 auto physicalRect = RotateRect<int32_t>(displayDirection, { displayInfo.validWidth, displayInfo.validHeight });
4755 bool isInside = (physicalX >= 0 && physicalX < physicalRect.x) && (physicalY >= 0 && physicalY < physicalRect.y);
4756 PrintDisplayInfo(displayInfo);
4757 MMI_HILOGD("isInside:%{public}d physicalXY={%{private}f %{private}f} "
4758 "physicalRect={%{public}d %{public}d} useDirection:%{public}d}",
4759 static_cast<int32_t>(isInside),
4760 physicalX,
4761 physicalY,
4762 physicalRect.x,
4763 physicalRect.y,
4764 displayDirection);
4765 return isInside;
4766 }
4767
4768 bool InputWindowsManager::CalculateLayout(const DisplayInfo &displayInfo, const Vector2D<double> &physical,
4769 Vector2D<double> &layout)
4770 {
4771 Direction direction = GetDisplayDirection(&displayInfo);
4772 Vector2D<double> logical = physical;
4773 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4774 if (IsSupported()) {
4775 auto screenRect = RotateRect<double>(direction, {displayInfo.width, displayInfo.height});
4776 auto transforms = RotateAndFitScreen(direction, screenRect);
4777 logical = MMI::ApplyTransformSteps(transforms, physical);
4778 }
4779 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4780 layout.x = logical.x + displayInfo.x;
4781 layout.y = logical.y + displayInfo.y;
4782
4783 MMI_HILOGD("calculated layout point, id:%{public}d, d:%{public}d, dd:%{public}d, ddd:%{public}d, "
4784 "dx:%{private}d, dy:%{private}d, px:%{private}f, py:%{private}f, "
4785 "lx:%{private}f, ly:%{private}f, lax:%{private}f, lay:%{private}f ",
4786 displayInfo.id, direction, displayInfo.direction, displayInfo.displayDirection,
4787 displayInfo.x, displayInfo.y, physical.x, physical.y,
4788 logical.x, logical.y, layout.x, layout.y);
4789 return true;
4790 }
4791
4792 AcrossDirection InputWindowsManager::CalculateAcrossDirection(const DisplayInfo &displayInfo,
4793 const Vector2D<double> &layout)
4794 {
4795 Vector2D<int32_t> layoutMax;
4796
4797 if (!AddInt32(displayInfo.x, displayInfo.validWidth, layoutMax.x)) {
4798 MMI_HILOGE("The addition of layoutMax.x overflows");
4799 return AcrossDirection::ACROSS_ERROR;
4800 }
4801 if (!AddInt32(displayInfo.y, displayInfo.validHeight, layoutMax.y)) {
4802 MMI_HILOGE("The addition of layoutMax.y overflows");
4803 return AcrossDirection::ACROSS_ERROR;
4804 }
4805
4806 if (layout.x < displayInfo.x) {
4807 return AcrossDirection::LEFTWARDS;
4808 } else if (layout.x >= layoutMax.x) {
4809 return AcrossDirection::RIGHTWARDS;
4810 }
4811 if (layout.y < displayInfo.y) {
4812 return AcrossDirection::UPWARDS;
4813 } else if (layout.y >= layoutMax.y) {
4814 return AcrossDirection::DOWNWARDS;
4815 }
4816
4817 return AcrossDirection::ACROSS_ERROR;
4818 }
4819
4820 bool InputWindowsManager::AcrossDisplay(const DisplayInfo &displayInfoDes, const DisplayInfo &displayInfoOri,
4821 Vector2D<double> &logical, Vector2D<double> &layout, const AcrossDirection &acrossDirection)
4822 {
4823 Vector2D<int32_t> layoutMax;
4824 double layoutX, layoutY;
4825 int32_t pointerWidth = 0, pointerHeight = 0;
4826 bool re = false;
4827 layoutX = layout.x;
4828 layoutY = layout.y;
4829 IPointerDrawingManager::GetInstance()->GetPointerImageSize(pointerWidth, pointerHeight);
4830 if (!AddInt32(displayInfoDes.x, displayInfoDes.validWidth, layoutMax.x)) {
4831 MMI_HILOGE("The addition of layoutMax.x overflows");
4832 return false;
4833 }
4834 if (!AddInt32(displayInfoDes.y, displayInfoDes.validHeight, layoutMax.y)) {
4835 MMI_HILOGE("The addition of layoutMax.y overflows");
4836 return false;
4837 }
4838
4839 re |= (acrossDirection == RIGHTWARDS && displayInfoDes.x == displayInfoOri.x + displayInfoOri.validWidth);
4840 re |= (acrossDirection == LEFTWARDS && displayInfoDes.x + displayInfoDes.validWidth == displayInfoOri.x);
4841 re |= (acrossDirection == DOWNWARDS && displayInfoDes.y == displayInfoOri.y + displayInfoOri.validHeight);
4842 re |= (acrossDirection == UPWARDS && displayInfoDes.y + displayInfoDes.validHeight == displayInfoOri.y);
4843 if (!re) {
4844 MMI_HILOGI("the display is not in across direction.");
4845 return re;
4846 }
4847
4848 if (layout.x < displayInfoDes.x) {
4849 layoutX = displayInfoDes.x;
4850 } else if (layout.x >= layoutMax.x) {
4851 layoutX = layoutMax.x - pointerWidth;
4852 }
4853 if (layout.y < displayInfoDes.y) {
4854 layoutY = displayInfoDes.y;
4855 } else if (layout.y >= layoutMax.y) {
4856 layoutY = layoutMax.y - pointerHeight;
4857 }
4858 logical = { layoutX - displayInfoDes.x, layoutY - displayInfoDes.y };
4859 return re;
4860 }
4861
4862 void InputWindowsManager::FindPhysicalDisplay(const DisplayInfo& displayInfo, double& physicalX,
4863 double& physicalY, int32_t& displayId)
4864 {
4865 CALL_DEBUG_ENTER;
4866 Vector2D<double> physical = { physicalX, physicalY };
4867 Vector2D<double> logical = physical;
4868 Vector2D<double> layout = { 0, 0 };
4869 AcrossDirection acrossDirection;
4870 if (!CalculateLayout(displayInfo, physical, layout)) {
4871 return;
4872 }
4873 for (const auto &item : displayGroupInfo_.displaysInfo) {
4874 if (item.id == displayInfo.id) {
4875 continue;
4876 }
4877 acrossDirection = CalculateAcrossDirection(displayInfo, layout);
4878 MMI_HILOGI("acrossDirection :%{public}d.", acrossDirection);
4879 if (acrossDirection == AcrossDirection::ACROSS_ERROR) {
4880 return;
4881 }
4882 if (!AcrossDisplay(item, displayInfo, logical, layout, acrossDirection)) {
4883 continue;
4884 }
4885 physical = logical;
4886 Direction direction = GetDisplayDirection(&item);
4887 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4888 if (IsSupported()) {
4889 auto screenRect = RotateRect<double>(direction, { item.width, item.height });
4890 auto transforms = RotateAndFitScreen(direction, screenRect);
4891 physical = ResetTransformSteps(transforms, logical);
4892 }
4893 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4894 physicalX = physical.x;
4895 physicalY = physical.y;
4896 displayId = item.id;
4897 MMI_HILOGD("switched into display, id:%{public}d, d:%{public}d, dd:%{public}d, ddd:%{public}d, "
4898 "dx:%{private}d, dy:%{private}d, dw:%{private}d, dh:%{private}d, "
4899 "lx:%{private}f, ly:%{private}f, px:%{private}f, py:%{private}f",
4900 displayId, direction, item.direction, item.displayDirection,
4901 item.x, item.y, item.width, item.height,
4902 logical.x, logical.y, physicalX, physicalY);
4903 break;
4904 }
4905 }
4906 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
4907
4908 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
4909 void InputWindowsManager::CoordinateCorrection(int32_t width, int32_t height, int32_t &integerX, int32_t &integerY)
4910 {
4911 if (integerX < 0) {
4912 integerX = 0;
4913 }
4914 if (integerX >= width) {
4915 integerX = width;
4916 }
4917 if (integerY < 0) {
4918 integerY = 0;
4919 }
4920 if (integerY >= height) {
4921 integerY = height;
4922 }
4923 }
4924
4925 Direction InputWindowsManager::GetDisplayDirection(const DisplayInfo *displayInfo)
4926 {
4927 Direction displayDirection = static_cast<Direction>((
4928 ((displayInfo->direction - displayInfo->displayDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
4929 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4930 if (IsSupported()) {
4931 displayDirection = displayInfo->direction;
4932 }
4933 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
4934 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
4935 displayDirection = displayInfo->direction;
4936 }
4937 return displayDirection;
4938 }
4939
4940 void InputWindowsManager::GetWidthAndHeight(const DisplayInfo* displayInfo, int32_t &width, int32_t &height,
4941 bool isRealData)
4942 {
4943 auto displayDirection = GetDisplayDirection(displayInfo);
4944 if (displayDirection == DIRECTION0 || displayDirection == DIRECTION180) {
4945 width = displayInfo->validWidth;
4946 height = displayInfo->validHeight;
4947 } else {
4948 if (!isRealData) {
4949 width = displayInfo->validWidth;
4950 height = displayInfo->validHeight;
4951 return;
4952 }
4953 height = displayInfo->validWidth;
4954 width = displayInfo->validHeight;
4955 }
4956 }
4957 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
4958
4959 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
4960 void InputWindowsManager::ReverseRotateScreen(const DisplayInfo& info, const double x, const double y,
4961 Coordinate2D& cursorPos) const
4962 {
4963 const Direction direction = info.direction;
4964 MMI_HILOGD("X:%{private}.2f, Y:%{private}.2f, offsetXY={%{private}d %{private}d},"
4965 "info.WH:{%{private}d %{private}d} info.validWH:{%{private}d %{private}d}",
4966 x,
4967 y,
4968 info.offsetX,
4969 info.offsetY,
4970 info.width,
4971 info.height,
4972 info.validWidth,
4973 info.validHeight);
4974 switch (direction) {
4975 case DIRECTION0: {
4976 cursorPos.x = x;
4977 cursorPos.y = y;
4978 MMI_HILOGD("DIRECTION0, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
4979 break;
4980 }
4981 case DIRECTION90: {
4982 cursorPos.y = static_cast<double>(info.validWidth) - x;
4983 cursorPos.x = y;
4984 MMI_HILOGD("DIRECTION90, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
4985 break;
4986 }
4987 case DIRECTION180: {
4988 cursorPos.x = static_cast<double>(info.validWidth) - x;
4989 cursorPos.y = static_cast<double>(info.validHeight) - y;
4990 MMI_HILOGD("DIRECTION180, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
4991 break;
4992 }
4993 case DIRECTION270: {
4994 cursorPos.x = static_cast<double>(info.validHeight) - y;
4995 cursorPos.y = x;
4996 MMI_HILOGD("DIRECTION270, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
4997 break;
4998 }
4999 default: {
5000 MMI_HILOGE("direction is invalid, direction:%{private}d", direction);
5001 break;
5002 }
5003 }
5004 }
5005
5006 void InputWindowsManager::ReverseRotateDisplayScreen(const DisplayInfo& info, const double x, const double y,
5007 Coordinate2D& cursorPos) const
5008 {
5009 Direction displayDirection = static_cast<Direction>((
5010 ((info.direction - info.displayDirection) * ANGLE_90 + ANGLE_360) % ANGLE_360) / ANGLE_90);
5011 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5012 if (WIN_MGR->IsSupported()) {
5013 direction = info.direction;
5014 }
5015 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5016 MMI_HILOGD(
5017 "X:%{private}.2f, Y:%{private}.2f, info.WH:{%{private}d %{private}d}, info.validWH:{%{private}d %{private}d}",
5018 x,
5019 y,
5020 info.width,
5021 info.height,
5022 info.validWidth,
5023 info.validHeight);
5024 switch (displayDirection) {
5025 case DIRECTION0: {
5026 cursorPos.x = x;
5027 cursorPos.y = y;
5028 MMI_HILOGD("DIRECTION0, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
5029 break;
5030 }
5031 case DIRECTION90: {
5032 cursorPos.y = static_cast<double>(info.validWidth) - x;
5033 cursorPos.x = y;
5034 MMI_HILOGD("DIRECTION90, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
5035 break;
5036 }
5037 case DIRECTION180: {
5038 cursorPos.x = static_cast<double>(info.validWidth) - x;
5039 cursorPos.y = static_cast<double>(info.validHeight) - y;
5040 MMI_HILOGD("DIRECTION180, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
5041 break;
5042 }
5043 case DIRECTION270: {
5044 cursorPos.x = static_cast<double>(info.validHeight) - y;
5045 cursorPos.y = x;
5046 MMI_HILOGD("DIRECTION270, physicalX:%{private}.2f, physicalY:%{private}.2f", cursorPos.x, cursorPos.y);
5047 break;
5048 }
5049 default: {
5050 MMI_HILOGE("displayDirection is invalid, displayDirection:%{private}d", displayDirection);
5051 break;
5052 }
5053 }
5054 }
5055 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
5056
5057 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
5058 void InputWindowsManager::UpdateAndAdjustMouseLocation(int32_t& displayId, double& x, double& y, bool isRealData)
5059 {
5060 auto displayInfo = GetPhysicalDisplay(displayId);
5061 CHKPV(displayInfo);
5062 double oldX = x;
5063 double oldY = y;
5064 int32_t lastDisplayId = displayId;
5065 if (!IsInsideDisplay(*displayInfo, x, y)) {
5066 FindPhysicalDisplay(*displayInfo, x, y, displayId);
5067 MMI_HILOGD("not IsInsideDisplay,cursorXY:{%{private}f %{private}f}->{%{private}f %{private}f}",
5068 oldX, oldY, x, y);
5069 }
5070 if (displayId != lastDisplayId) {
5071 displayInfo = GetPhysicalDisplay(displayId);
5072 CHKPV(displayInfo);
5073 }
5074 int32_t width = 0;
5075 int32_t height = 0;
5076 GetWidthAndHeight(displayInfo, width, height, isRealData);
5077 int32_t integerX = static_cast<int32_t>(x);
5078 int32_t integerY = static_cast<int32_t>(y);
5079 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
5080 if (IsPointerActiveRectValid(*displayInfo)) {
5081 width = displayInfo->pointerActiveWidth;
5082 height = displayInfo->pointerActiveHeight;
5083 MMI_HILOGD("vtp cursor active area w:%{private}d, h:%{private}d", width, height);
5084 }
5085 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
5086 CoordinateCorrection(width, height, integerX, integerY);
5087 x = static_cast<double>(integerX) + ((integerX == width) ? 0.0f : (x - floor(x)));
5088 y = static_cast<double>(integerY) + ((integerY == height) ? 0.0f : (y - floor(y)));
5089
5090 mouseLocation_.displayId = displayId;
5091 cursorPos_.displayId = displayId;
5092 if (isRealData) {
5093 PhysicalCoordinate coord {
5094 .x = integerX,
5095 .y = integerY,
5096 };
5097 RotateDisplayScreen(*displayInfo, coord);
5098 mouseLocation_.physicalX = static_cast<int32_t>(coord.x);
5099 mouseLocation_.physicalY = static_cast<int32_t>(coord.y);
5100 cursorPos_.cursorPos.x = x;
5101 cursorPos_.cursorPos.y = y;
5102 } else {
5103 mouseLocation_.physicalX = integerX;
5104 mouseLocation_.physicalY = integerY;
5105 ReverseRotateDisplayScreen(*displayInfo, x, y, cursorPos_.cursorPos);
5106 }
5107 MMI_HILOGD("Mouse Data: isRealData=%{public}d, displayId:%{public}d, mousePhysicalXY={%{public}d, %{public}d}, "
5108 "cursorPosXY: {%{public}.2f, %{public}.2f} -> {%{public}.2f %{public}.2f}",
5109 static_cast<int32_t>(isRealData), displayId, mouseLocation_.physicalX, mouseLocation_.physicalY,
5110 oldX, oldY, cursorPos_.cursorPos.x, cursorPos_.cursorPos.y);
5111 }
5112
5113 MouseLocation InputWindowsManager::GetMouseInfo()
5114 {
5115 if ((mouseLocation_.displayId < 0) && !displayGroupInfo_.displaysInfo.empty()) {
5116 DisplayInfo displayInfo = displayGroupInfo_.displaysInfo[0];
5117 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5118 (void)GetMainScreenDisplayInfo(displayGroupInfo_, displayInfo);
5119 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5120 mouseLocation_.displayId = displayInfo.id;
5121 mouseLocation_.physicalX = displayInfo.validWidth / TWOFOLD;
5122 mouseLocation_.physicalY = displayInfo.validHeight / TWOFOLD;
5123 }
5124 return mouseLocation_;
5125 }
5126
5127 CursorPosition InputWindowsManager::GetCursorPos()
5128 {
5129 if ((cursorPos_.displayId < 0) && !displayGroupInfo_.displaysInfo.empty()) {
5130 DisplayInfo displayInfo = displayGroupInfo_.displaysInfo[0];
5131 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5132 (void)GetMainScreenDisplayInfo(displayGroupInfo_, displayInfo);
5133 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5134 cursorPos_.displayId = displayInfo.id;
5135 cursorPos_.cursorPos.x = displayInfo.validWidth * HALF_RATIO;
5136 cursorPos_.cursorPos.y = displayInfo.validHeight * HALF_RATIO;
5137 }
5138 return cursorPos_;
5139 }
5140
5141 CursorPosition InputWindowsManager::ResetCursorPos()
5142 {
5143 if (!displayGroupInfo_.displaysInfo.empty()) {
5144 DisplayInfo displayInfo = displayGroupInfo_.displaysInfo[0];
5145 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5146 (void)GetMainScreenDisplayInfo(displayGroupInfo_, displayInfo);
5147 if (IsSupported()) {
5148 int32_t x = displayInfo.width * HALF_RATIO;
5149 int32_t y = displayInfo.height * HALF_RATIO;
5150 if (displayInfo.direction == DIRECTION90 || displayInfo.direction == DIRECTION270) {
5151 std::swap(x, y);
5152 }
5153 cursorPos_.cursorPos.x = x;
5154 cursorPos_.cursorPos.y = y;
5155 }
5156 #else
5157 cursorPos_.cursorPos.x = displayInfo.validWidth * HALF_RATIO;
5158 cursorPos_.cursorPos.y = displayInfo.validHeight * HALF_RATIO;
5159 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5160 cursorPos_.displayId = displayInfo.id;
5161 } else {
5162 cursorPos_.displayId = -1;
5163 cursorPos_.cursorPos.x = 0;
5164 cursorPos_.cursorPos.y = 0;
5165 }
5166 return cursorPos_;
5167 }
5168 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
5169
5170 int32_t InputWindowsManager::AppendExtraData(const ExtraData& extraData)
5171 {
5172 CALL_DEBUG_ENTER;
5173 extraData_.appended = extraData.appended;
5174 extraData_.toolType = extraData.toolType;
5175 extraData_.buffer = extraData.buffer;
5176 extraData_.sourceType = extraData.sourceType;
5177 extraData_.pointerId = extraData.pointerId;
5178 extraData_.pullId = extraData.pullId;
5179 extraData_.eventId = extraData.eventId;
5180 extraData_.drawCursor = extraData.drawCursor;
5181 if ((extraData.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) &&
5182 (mouseDownEventId_ < 0 || extraData.eventId < mouseDownEventId_)) {
5183 MMI_HILOGE("Mouse drag failed, PI:%{public}d, EI:%{public}d, DEI:%{public}d",
5184 extraData.pointerId, extraData.eventId, mouseDownEventId_);
5185 ClearExtraData();
5186 return RET_ERR;
5187 }
5188 return RET_OK;
5189 }
5190
5191 void InputWindowsManager::ClearExtraData()
5192 {
5193 CALL_DEBUG_ENTER;
5194 extraData_.appended = false;
5195 extraData_.toolType = 0;
5196 extraData_.buffer.clear();
5197 extraData_.sourceType = -1;
5198 extraData_.pointerId = -1;
5199 extraData_.pullId = -1;
5200 extraData_.eventId = -1;
5201 extraData_.drawCursor = false;
5202 }
5203
5204 ExtraData InputWindowsManager::GetExtraData() const
5205 {
5206 CALL_DEBUG_ENTER;
5207 return extraData_;
5208 }
5209
5210 bool InputWindowsManager::IsWindowVisible(int32_t pid)
5211 {
5212 CALL_DEBUG_ENTER;
5213 if (pid < 0) {
5214 MMI_HILOGE("pid is invalid");
5215 return true;
5216 }
5217 std::vector<sptr<Rosen::WindowVisibilityInfo>> infos;
5218 BytraceAdapter::StartWindowVisible(pid);
5219 Rosen::WindowManagerLite::GetInstance().GetVisibilityWindowInfo(infos);
5220 BytraceAdapter::StopWindowVisible();
5221 for (const auto &it: infos) {
5222 if (pid == it->pid_ &&
5223 it->visibilityState_ < Rosen::WindowVisibilityState::WINDOW_VISIBILITY_STATE_TOTALLY_OCCUSION) {
5224 MMI_HILOGD("pid:%{public}d has visible window", pid);
5225 return true;
5226 }
5227 }
5228 MMI_HILOGD("pid:%{public}d doesn't have visible window", pid);
5229 return false;
5230 }
5231
5232 void InputWindowsManager::UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)
5233 {
5234 CALL_DEBUG_ENTER;
5235 int32_t action = pointerEvent->GetPointerAction();
5236 switch (action) {
5237 case PointerEvent::POINTER_ACTION_MOVE: {
5238 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_MOVE);
5239 break;
5240 }
5241 case PointerEvent::POINTER_ACTION_BUTTON_UP:
5242 case PointerEvent::POINTER_ACTION_UP: {
5243 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_UP);
5244 break;
5245 }
5246 case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
5247 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
5248 break;
5249 }
5250 case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
5251 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
5252 break;
5253 }
5254 default: {
5255 MMI_HILOG_DISPATCHI("Action is:%{public}d, no need change", action);
5256 break;
5257 }
5258 }
5259 MMI_HILOG_DISPATCHD("pointerAction:%{public}s", pointerEvent->DumpPointerAction());
5260 }
5261
5262 void InputWindowsManager::DumpDisplayInfo(int32_t fd)
5263 {
5264 mprintf(fd, "Displays information:\t");
5265 mprintf(fd, "displayInfos,num:%zu", displayGroupInfo_.displaysInfo.size());
5266 for (const auto &item : displayGroupInfo_.displaysInfo) {
5267 mprintf(fd, "\t displayInfos: id:%d | x:%d | y:%d | width:%d | height:%d | name:%s "
5268 "| uniq:%s | direction:%d | displayDirection:%d | displayMode:%u \t",
5269 item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
5270 item.uniq.c_str(), item.direction, item.displayDirection, item.displayMode);
5271 if (item.transform.size() == MATRIX3_SIZE) {
5272 mprintf(fd, "\t transform: scaleX:%f | scaleY:%f | anchorPointX:%f | anchorPointY:%f \t",
5273 item.transform[SCALE_X], item.transform[SCALE_Y], item.transform[ANCHOR_POINT_X],
5274 item.transform[ANCHOR_POINT_Y]);
5275 }
5276 }
5277 }
5278
5279 void InputWindowsManager::Dump(int32_t fd, const std::vector<std::string> &args)
5280 {
5281 CALL_DEBUG_ENTER;
5282 mprintf(fd, "Windows information:\t");
5283 mprintf(fd, "windowsInfos,num:%zu", displayGroupInfo_.windowsInfo.size());
5284 for (const auto &item : displayGroupInfo_.windowsInfo) {
5285 mprintf(fd, " windowsInfos: id:%d | pid:%d | uid:%d | area.x:%d | area.y:%d "
5286 "| area.width:%d | area.height:%d | defaultHotAreas.size:%zu "
5287 "| pointerHotAreas.size:%zu | agentWindowId:%d | flags:%u "
5288 "| action:%d | displayId:%d | zOrder:%f \t",
5289 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
5290 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
5291 item.agentWindowId, item.flags, item.action, item.displayId, item.zOrder);
5292 for (const auto &win : item.defaultHotAreas) {
5293 mprintf(fd, "\t defaultHotAreas: x:%d | y:%d | width:%d | height:%d \t",
5294 win.x, win.y, win.width, win.height);
5295 }
5296 for (const auto &pointer : item.pointerHotAreas) {
5297 mprintf(fd, "\t pointerHotAreas: x:%d | y:%d | width:%d | height:%d \t",
5298 pointer.x, pointer.y, pointer.width, pointer.height);
5299 }
5300
5301 std::string dump;
5302 dump += StringPrintf("\t pointerChangeAreas: ");
5303 for (const auto &it : item.pointerChangeAreas) {
5304 dump += StringPrintf("%d | ", it);
5305 }
5306 dump += StringPrintf("\n\t transform: ");
5307 for (const auto &it : item.transform) {
5308 dump += StringPrintf("%f | ", it);
5309 }
5310 std::istringstream stream(dump);
5311 std::string line;
5312 while (std::getline(stream, line, '\n')) {
5313 mprintf(fd, "%s", line.c_str());
5314 }
5315 }
5316 DumpDisplayInfo(fd);
5317 mprintf(fd, "Input device and display bind info:\n%s", bindInfo_.Dumps().c_str());
5318 #ifdef OHOS_BUILD_ENABLE_ANCO
5319 std::string ancoWindows;
5320 DumpAncoWindows(ancoWindows);
5321 mprintf(fd, "%s\n", ancoWindows.c_str());
5322 #endif // OHOS_BUILD_ENABLE_ANCO
5323 }
5324
5325 std::pair<double, double> InputWindowsManager::TransformWindowXY(const WindowInfo &window,
5326 double logicX, double logicY) const
5327 {
5328 Matrix3f transform(window.transform);
5329 if (window.transform.size() != MATRIX3_SIZE || transform.IsIdentity()) {
5330 return {logicX, logicY};
5331 }
5332 Vector3f logicXY(logicX, logicY, 1.0);
5333 Vector3f windowXY = transform * logicXY;
5334 return {round(windowXY[0]), round(windowXY[1])};
5335 }
5336
5337 std::pair<double, double> InputWindowsManager::TransformDisplayXY(const DisplayInfo &info,
5338 double logicX, double logicY) const
5339 {
5340 Matrix3f transform(info.transform);
5341 if (info.transform.size() != MATRIX3_SIZE || transform.IsIdentity()) {
5342 return {logicX, logicY};
5343 }
5344 Vector3f logicXY(logicX, logicY, 1.0);
5345 Vector3f displayXY = transform * logicXY;
5346 return {round(displayXY[0]), round(displayXY[1])};
5347 }
5348
5349 bool InputWindowsManager::IsValidZorderWindow(const WindowInfo &window,
5350 const std::shared_ptr<PointerEvent>& pointerEvent)
5351 {
5352 CHKPR(pointerEvent, false);
5353 if (!(pointerEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) || MMI_LE(pointerEvent->GetZOrder(), 0.0f)) {
5354 return true;
5355 }
5356 if (MMI_GE(window.zOrder, pointerEvent->GetZOrder())) {
5357 MMI_HILOGE("Current window zorder:%{public}f greater than the simulate target zOrder:%{public}f, "
5358 "ignore this window::%{public}d", window.zOrder, pointerEvent->GetZOrder(), window.id);
5359 return false;
5360 }
5361 return true;
5362 }
5363
5364 bool InputWindowsManager::HandleWindowInputType(const WindowInfo &window, std::shared_ptr<PointerEvent> pointerEvent)
5365 {
5366 CALL_DEBUG_ENTER;
5367 int32_t pointerId = pointerEvent->GetPointerId();
5368 PointerEvent::PointerItem item;
5369 if (!pointerEvent->GetPointerItem(pointerId, item)) {
5370 MMI_HILOG_WINDOWE("Invalid pointer:%{public}d", pointerId);
5371 return false;
5372 }
5373 [[ maybe_unused ]] int32_t toolType = item.GetToolType();
5374 [[ maybe_unused ]] int32_t sourceType = pointerEvent->GetSourceType();
5375 switch (window.windowInputType)
5376 {
5377 case WindowInputType::NORMAL:
5378 return false;
5379 case WindowInputType::TRANSMIT_ALL:
5380 return true;
5381 case WindowInputType::TRANSMIT_EXCEPT_MOVE: {
5382 auto pointerAction = pointerEvent->GetPointerAction();
5383 return (pointerAction == PointerEvent::POINTER_ACTION_MOVE ||
5384 pointerAction == PointerEvent::POINTER_ACTION_PULL_MOVE);
5385 }
5386 case WindowInputType::ANTI_MISTAKE_TOUCH:
5387 return false;
5388 case WindowInputType::TRANSMIT_AXIS_MOVE:
5389 return false;
5390 case WindowInputType::TRANSMIT_MOUSE_MOVE:
5391 return false;
5392 case WindowInputType::TRANSMIT_LEFT_RIGHT:
5393 return false;
5394 case WindowInputType::TRANSMIT_BUTTOM:
5395 return false;
5396 case WindowInputType::MIX_LEFT_RIGHT_ANTI_AXIS_MOVE:
5397 return false;
5398 case WindowInputType::MIX_BUTTOM_ANTI_AXIS_MOVE:
5399 return false;
5400 default:
5401 return false;
5402 }
5403 }
5404
5405 std::optional<WindowInfo> InputWindowsManager::GetWindowAndDisplayInfo(int32_t windowId, int32_t displayId)
5406 {
5407 CALL_DEBUG_ENTER;
5408 std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(displayId);
5409 for (const auto &item : windowInfos) {
5410 if (windowId == item.id) {
5411 return std::make_optional(item);
5412 }
5413 for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
5414 if (windowId == uiExtentionWindow.id) {
5415 return std::make_optional(uiExtentionWindow);
5416 }
5417 }
5418 }
5419 return std::nullopt;
5420 }
5421
5422 void InputWindowsManager::GetTargetWindowIds(int32_t pointerItemId, int32_t sourceType,
5423 std::vector<int32_t> &windowIds)
5424 {
5425 CALL_DEBUG_ENTER;
5426 if (sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
5427 if (targetMouseWinIds_.find(pointerItemId) != targetMouseWinIds_.end()) {
5428 windowIds = targetMouseWinIds_[pointerItemId];
5429 }
5430 return;
5431 } else if (sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
5432 if (targetTouchWinIds_.find(pointerItemId) != targetTouchWinIds_.end()) {
5433 windowIds = targetTouchWinIds_[pointerItemId];
5434 }
5435 }
5436 }
5437
5438 void InputWindowsManager::AddTargetWindowIds(int32_t pointerItemId, int32_t sourceType, int32_t windowId)
5439 {
5440 CALL_DEBUG_ENTER;
5441 if (sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
5442 if (targetMouseWinIds_.find(pointerItemId) != targetMouseWinIds_.end()) {
5443 targetMouseWinIds_[pointerItemId].push_back(windowId);
5444 } else {
5445 std::vector<int32_t> windowIds;
5446 windowIds.push_back(windowId);
5447 targetMouseWinIds_.emplace(pointerItemId, windowIds);
5448 }
5449 return;
5450 } else if (sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
5451 if (targetTouchWinIds_.find(pointerItemId) != targetTouchWinIds_.end()) {
5452 targetTouchWinIds_[pointerItemId].push_back(windowId);
5453 } else {
5454 std::vector<int32_t> windowIds;
5455 windowIds.push_back(windowId);
5456 targetTouchWinIds_.emplace(pointerItemId, windowIds);
5457 }
5458 }
5459 }
5460
5461 void InputWindowsManager::ClearTargetWindowId(int32_t pointerId)
5462 {
5463 CALL_DEBUG_ENTER;
5464 if (targetTouchWinIds_.find(pointerId) == targetTouchWinIds_.end()) {
5465 MMI_HILOGD("Clear target windowId fail, pointerId:%{public}d", pointerId);
5466 return;
5467 }
5468 targetTouchWinIds_.erase(pointerId);
5469 }
5470
5471 void InputWindowsManager::SetPrivacyModeFlag(SecureFlag privacyMode, std::shared_ptr<InputEvent> event)
5472 {
5473 if (privacyMode == SecureFlag::PRIVACY_MODE) {
5474 MMI_HILOGD("Window security mode is privacy");
5475 event->AddFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
5476 }
5477 }
5478
5479 int32_t InputWindowsManager::CheckWindowIdPermissionByPid(int32_t windowId, int32_t pid)
5480 {
5481 CALL_DEBUG_ENTER;
5482 int32_t checkingPid = GetWindowPid(windowId);
5483 if (checkingPid != pid) {
5484 MMI_HILOGE("check windowId failed, windowId is %{public}d, pid is %{public}d", windowId, pid);
5485 return RET_ERR;
5486 }
5487 return RET_OK;
5488 }
5489
5490 #ifdef OHOS_BUILD_ENABLE_TOUCH
5491 void InputWindowsManager::ReverseXY(int32_t &x, int32_t &y)
5492 {
5493 if (displayGroupInfo_.displaysInfo.empty()) {
5494 MMI_HILOGE("displayGroupInfo_.displaysInfo is empty");
5495 return;
5496 }
5497 const Direction direction = displayGroupInfo_.displaysInfo.front().direction;
5498 if (direction < Direction::DIRECTION0 || direction > Direction::DIRECTION270) {
5499 MMI_HILOGE("direction is invalid, direction:%{public}d", direction);
5500 return;
5501 }
5502 Coordinate2D matrix { 0.0, 0.0 };
5503 ReverseRotateScreen(displayGroupInfo_.displaysInfo.front(), x, y, matrix);
5504 x = static_cast<int32_t>(matrix.x);
5505 y = static_cast<int32_t>(matrix.y);
5506 }
5507
5508 void InputWindowsManager::SendCancelEventWhenLock()
5509 {
5510 CALL_INFO_TRACE;
5511 CHKPV(lastTouchEventOnBackGesture_);
5512 if (lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_MOVE &&
5513 lastTouchEventOnBackGesture_->GetPointerAction() != PointerEvent::POINTER_ACTION_DOWN) {
5514 return;
5515 }
5516 lastTouchEventOnBackGesture_->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
5517 lastTouchEventOnBackGesture_->SetActionTime(GetSysClockTime());
5518 lastTouchEventOnBackGesture_->UpdateId();
5519 lastTouchEventOnBackGesture_->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT | InputEvent::EVENT_FLAG_NO_MONITOR);
5520 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
5521 CHKPV(inputEventNormalizeHandler);
5522 MMI_HILOGI("Screen locked, Send cancel event");
5523 inputEventNormalizeHandler->HandleTouchEvent(lastTouchEventOnBackGesture_);
5524 auto iter = touchItemDownInfos_.find(lastTouchEventOnBackGesture_->GetPointerId());
5525 if (iter != touchItemDownInfos_.end()) {
5526 iter->second.flag = false;
5527 }
5528 }
5529 #endif // OHOS_BUILD_ENABLE_TOUCH
5530
5531 bool InputWindowsManager::IsTransparentWin(
5532 std::unique_ptr<Media::PixelMap> &pixelMap, int32_t logicalX, int32_t logicalY)
5533 __attribute__((no_sanitize("cfi")))
5534 {
5535 CALL_DEBUG_ENTER;
5536 if (pixelMap == nullptr) {
5537 return false;
5538 }
5539
5540 uint32_t dst = 0;
5541 OHOS::Media::Position pos { logicalY, logicalX };
5542 uint32_t result = pixelMap->ReadPixel(pos, dst);
5543 if (result != RET_OK) {
5544 MMI_HILOGE("Failed to read pixelmap");
5545 return false;
5546 }
5547 MMI_HILOGD("dst:%{public}d, byteCount:%{public}d, width:%{public}d, height:%{public}d",
5548 dst, pixelMap->GetByteCount(), pixelMap->GetWidth(), pixelMap->GetHeight());
5549 return dst == RET_OK;
5550 }
5551
5552 int32_t InputWindowsManager::SetCurrentUser(int32_t userId)
5553 {
5554 CALL_DEBUG_ENTER;
5555 currentUserId_ = userId;
5556 return RET_OK;
5557 }
5558
5559 void InputWindowsManager::PrintChangedWindowByEvent(int32_t eventType, const WindowInfo &newWindowInfo)
5560 {
5561 auto iter = lastMatchedWindow_.find(eventType);
5562 if (iter != lastMatchedWindow_.end() && iter->second.id != newWindowInfo.id) {
5563 MMI_HILOGI("Target window changed %{public}d %{public}d %{public}d %{public}f "
5564 "%{public}d %{public}d %{public}f", eventType, iter->second.id, iter->second.pid,
5565 iter->second.zOrder, newWindowInfo.id, newWindowInfo.pid, newWindowInfo.zOrder);
5566 }
5567 lastMatchedWindow_[eventType] = newWindowInfo;
5568 }
5569
5570 void InputWindowsManager::PrintChangedWindowBySync(const DisplayGroupInfo &newDisplayInfo)
5571 {
5572 auto &oldWindows = displayGroupInfo_.windowsInfo;
5573 auto &newWindows = newDisplayInfo.windowsInfo;
5574 if (!oldWindows.empty() && !newWindows.empty()) {
5575 if (oldWindows[0].id != newWindows[0].id) {
5576 MMI_HILOGI("Window sync changed %{public}d %{public}d %{public}f %{public}d %{public}d %{public}f",
5577 oldWindows[0].id, oldWindows[0].pid, oldWindows[0].zOrder, newWindows[0].id,
5578 newWindows[0].pid, newWindows[0].zOrder);
5579 }
5580 }
5581 if (newDisplayInfo.displaysInfo.empty() || displayGroupInfo_.displaysInfo.empty()) {
5582 MMI_HILOGE("displayGroupInfo.displaysInfo is empty");
5583 return;
5584 }
5585 for (const auto &item : newDisplayInfo.displaysInfo) {
5586 int32_t displayId = item.id;
5587 auto iter = std::find_if(displayGroupInfo_.displaysInfo.begin(), displayGroupInfo_.displaysInfo.end(),
5588 [displayId](const auto& displayInfo) {
5589 return displayId == displayInfo.id;
5590 });
5591 if (iter == displayGroupInfo_.displaysInfo.end()) {
5592 continue;
5593 }
5594 if (item.direction != iter->direction) {
5595 MMI_HILOGI("displayInfos,id:%{public}d,x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d,"
5596 "name:%{public}s,uniq:%{public}s,direction:%{public}d,displayDirection:%{public}d,"
5597 "oldDirection:%{public}d,oldDisplayDirection:%{public}d", item.id, item.x, item.y, item.width,
5598 item.height, item.name.c_str(), item.uniq.c_str(), item.direction, item.displayDirection,
5599 iter->direction, iter->displayDirection);
5600 }
5601 }
5602 }
5603
5604 bool InputWindowsManager::ParseConfig()
5605 {
5606 std::string defaultConfig = "/system/etc/multimodalinput/white_list_config.json";
5607 return ParseJson(defaultConfig);
5608 }
5609
5610 bool InputWindowsManager::ParseJson(const std::string &configFile)
5611 {
5612 CALL_DEBUG_ENTER;
5613 std::string jsonStr = ReadJsonFile(configFile);
5614 if (jsonStr.empty()) {
5615 MMI_HILOGE("Read configFile failed");
5616 return false;
5617 }
5618 JsonParser jsonData;
5619 jsonData.json_ = cJSON_Parse(jsonStr.c_str());
5620 if (!cJSON_IsObject(jsonData.json_)) {
5621 MMI_HILOGE("The json data is not object");
5622 return false;
5623 }
5624 cJSON* whiteList = cJSON_GetObjectItemCaseSensitive(jsonData.json_, "whiteList");
5625 if (!cJSON_IsArray(whiteList)) {
5626 MMI_HILOGE("White list number must be array");
5627 return false;
5628 }
5629 int32_t whiteListSize = cJSON_GetArraySize(whiteList);
5630 for (int32_t i = 0; i < whiteListSize; ++i) {
5631 cJSON *whiteListJson = cJSON_GetArrayItem(whiteList, i);
5632 if (!cJSON_IsObject(whiteListJson)) {
5633 MMI_HILOGE("White list json is not object");
5634 continue;
5635 }
5636 SwitchFocusKey switchFocusKey;
5637 cJSON *keyCodeJson = cJSON_GetObjectItemCaseSensitive(whiteListJson, "keyCode");
5638 if (!cJSON_IsNumber(keyCodeJson)) {
5639 MMI_HILOGE("Key code json is not number");
5640 continue;
5641 }
5642 switchFocusKey.keyCode = keyCodeJson->valueint;
5643 cJSON *pressedKeyJson = cJSON_GetObjectItemCaseSensitive(whiteListJson, "pressedKey");
5644 if (!cJSON_IsNumber(pressedKeyJson)) {
5645 MMI_HILOGE("Pressed key json is not number");
5646 continue;
5647 }
5648 switchFocusKey.pressedKey = pressedKeyJson->valueint;
5649 vecWhiteList_.push_back(switchFocusKey);
5650 }
5651 return true;
5652 }
5653
5654 void InputWindowsManager::SetWindowStateNotifyPid(int32_t pid)
5655 {
5656 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
5657 windowStateNotifyPid_ = pid;
5658 }
5659 }
5660
5661 int32_t InputWindowsManager::GetWindowStateNotifyPid()
5662 {
5663 return windowStateNotifyPid_;
5664 }
5665
5666 int32_t InputWindowsManager::GetPidByWindowId(int32_t id)
5667 {
5668 for (auto &item : displayGroupInfo_.windowsInfo) {
5669 if (item.id == id) {
5670 return item.pid;
5671 }
5672 for (const auto &uiExtentionWindow : item.uiExtentionWindowInfo) {
5673 if (uiExtentionWindow.id == id) {
5674 return uiExtentionWindow.pid;
5675 }
5676 }
5677 }
5678 return RET_ERR;
5679 }
5680 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
5681 bool InputWindowsManager::IsKeyPressed(int32_t pressedKey, std::vector<KeyEvent::KeyItem> &keyItems)
5682 {
5683 CALL_DEBUG_ENTER;
5684 for (const auto &item : keyItems) {
5685 if (item.GetKeyCode() == pressedKey && item.IsPressed()) {
5686 return true;
5687 }
5688 }
5689 return false;
5690 }
5691
5692 bool InputWindowsManager::IsOnTheWhitelist(std::shared_ptr<KeyEvent> keyEvent)
5693 {
5694 CALL_DEBUG_ENTER;
5695 CHKPR(keyEvent, false);
5696 for (const auto &item : vecWhiteList_) {
5697 if (item.keyCode == keyEvent->GetKeyCode()) {
5698 auto keyItems = keyEvent->GetKeyItems();
5699 if (item.pressedKey == -1 && keyItems.size() == 1) {
5700 return true;
5701 }
5702 bool flag = ((item.pressedKey != -1) && (keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_DOWN) &&
5703 (keyItems.size() == 2) && IsKeyPressed(item.pressedKey, keyItems));
5704 if (flag) {
5705 return true;
5706 }
5707 }
5708 }
5709 return false;
5710 }
5711 #endif // OHOS_BUILD_ENABLE_KEYBOARD
5712
5713 int32_t InputWindowsManager::SetPixelMapData(int32_t infoId, void *pixelMap)
5714 __attribute__((no_sanitize("cfi")))
5715 {
5716 CALL_DEBUG_ENTER;
5717 if (infoId < 0 || pixelMap == nullptr) {
5718 MMI_HILOGE("The infoId is invalid or pixelMap is nullptr");
5719 return ERR_INVALID_VALUE;
5720 }
5721 std::unique_ptr<OHOS::Media::PixelMap> pixelMapSource(
5722 static_cast<OHOS::Media::PixelMap*>(pixelMap));
5723 Media::InitializationOptions opts;
5724 auto pixelMapPtr = OHOS::Media::PixelMap::Create(*pixelMapSource, opts);
5725 CHKPR(pixelMapPtr, RET_ERR);
5726 MMI_HILOGD("The byteCount:%{public}d, width:%{public}d, height:%{public}d",
5727 pixelMapPtr->GetByteCount(), pixelMapPtr->GetWidth(), pixelMapPtr->GetHeight());
5728 transparentWins_.insert_or_assign(infoId, std::move(pixelMapPtr));
5729 return RET_OK;
5730 }
5731
5732 void InputWindowsManager::CleanInvalidPiexMap()
5733 {
5734 for (auto it = transparentWins_.begin(); it != transparentWins_.end();) {
5735 int32_t windowId = it->first;
5736 auto iter = std::find_if(displayGroupInfo_.windowsInfo.begin(), displayGroupInfo_.windowsInfo.end(),
5737 [windowId](const auto &window) {
5738 return window.id == windowId;
5739 });
5740 if (iter == displayGroupInfo_.windowsInfo.end()) {
5741 it = transparentWins_.erase(it);
5742 } else {
5743 ++it;
5744 }
5745 }
5746 }
5747
5748 #ifdef OHOS_BUILD_ENABLE_ANCO
5749 bool InputWindowsManager::IsKnuckleOnAncoWindow(std::shared_ptr<PointerEvent> pointerEvent)
5750 {
5751 CALL_DEBUG_ENTER;
5752 CHKPR(pointerEvent, false);
5753 PointerEvent::PointerItem pointerItem {};
5754 int32_t pointerId = pointerEvent->GetPointerId();
5755 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
5756 MMI_HILOGE("Get pointer item failed, pointer:%{public}d", pointerId);
5757 return false;
5758 }
5759
5760 if (pointerItem.GetToolType() != PointerEvent::TOOL_TYPE_KNUCKLE) {
5761 return false;
5762 }
5763
5764 const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
5765 WindowInfo *windowInfo = nullptr;
5766 std::vector<WindowInfo> windowInfos = GetWindowGroupInfoByDisplayId(pointerEvent->GetTargetDisplayId());
5767 auto iter = find_if(windowInfos.begin(), windowInfos.end(),
5768 [&](const auto &item) { return item.id == focusWindowId; });
5769 if (iter != windowInfos.end()) {
5770 windowInfo = &(*iter);
5771 }
5772
5773 if (windowInfo == nullptr) {
5774 MMI_HILOGE("windowInfo is nullptr");
5775 return false;
5776 }
5777
5778 return IsAncoWindowFocus(*windowInfo);
5779 }
5780 #endif // OHOS_BUILD_ENABLE_ANCO
5781
5782 #ifdef OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5783 void InputWindowsManager::UpdateKeyEventDisplayId(std::shared_ptr<KeyEvent> keyEvent, int32_t focusWindowId)
5784 {
5785 CHKPV(keyEvent);
5786 bool hasFound = false;
5787 for (const auto &item : windowsPerDisplay_) {
5788 if (item.second.focusWindowId == focusWindowId) {
5789 keyEvent->SetTargetDisplayId(item.second.displayId);
5790 hasFound = true;
5791 }
5792 }
5793 if (!hasFound && !displayGroupInfo_.displaysInfo.empty()) {
5794 keyEvent->SetTargetDisplayId(displayGroupInfo_.displaysInfo[0].id);
5795 }
5796 }
5797
5798 bool InputWindowsManager::OnDisplayRemovedOrCombiantionChanged(const DisplayGroupInfo &displayGroupInfo)
5799 {
5800 if (displayGroupInfo.displaysInfo.empty() || displayGroupInfo_.displaysInfo.empty()) {
5801 return false;
5802 }
5803 if (displayGroupInfo.displaysInfo.size() < displayGroupInfo_.displaysInfo.size()) {
5804 MMI_HILOGD("display has been removed");
5805 return true;
5806 }
5807 DisplayInfo newMainDisplayInfo;
5808 DisplayInfo oldMainDisplayInfo;
5809 (void)GetMainScreenDisplayInfo(displayGroupInfo, newMainDisplayInfo);
5810 (void)GetMainScreenDisplayInfo(displayGroupInfo_, oldMainDisplayInfo);
5811 if (displayGroupInfo.displaysInfo.size() == displayGroupInfo_.displaysInfo.size() &&
5812 newMainDisplayInfo.id != oldMainDisplayInfo.id) {
5813 MMI_HILOGD("current mainScreenDisplayId changed");
5814 return true;
5815 }
5816 return false;
5817 }
5818
5819 bool InputWindowsManager::IsSupported()
5820 {
5821 return IPointerDrawingManager::GetInstance()->IsSupported();
5822 }
5823 #endif // OHOS_BUILD_ENABLE_HARDWARE_CURSOR
5824
5825 int32_t InputWindowsManager::GetCurrentUserId()
5826 {
5827 return currentUserId_;
5828 }
5829
5830 void InputWindowsManager::SetFoldState()
5831 {
5832 BytraceAdapter::StartFoldState(Rosen::DisplayManager::GetInstance().IsFoldable());
5833 auto begin = std::chrono::high_resolution_clock::now();
5834 IsFoldable_ = Rosen::DisplayManager::GetInstance().IsFoldable();
5835 auto durationMS = std::chrono::duration_cast<std::chrono::milliseconds>(
5836 std::chrono::high_resolution_clock::now() - begin).count();
5837 #ifdef OHOS_BUILD_ENABLE_DFX_RADAR
5838 DfxHisysevent::ReportApiCallTimes(ApiDurationStatistics::Api::IS_FOLDABLE, durationMS);
5839 #endif // OHOS_BUILD_ENABLE_DFX_RADAR
5840 BytraceAdapter::StopFoldState();
5841 }
5842
5843 const DisplayInfo* InputWindowsManager::GetPhysicalDisplay(int32_t id, const DisplayGroupInfo &displayGroupInfo) const
5844 {
5845 for (const auto &it : displayGroupInfo.displaysInfo) {
5846 if (it.id == id) {
5847 return ⁢
5848 }
5849 }
5850 MMI_HILOGW("Failed to obtain physical(%{public}d) display", id);
5851 return nullptr;
5852 }
5853
5854 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
5855 std::optional<WindowInfo> InputWindowsManager::GetWindowInfoById(int32_t windowId) const
5856 {
5857 for (auto iter = windowsPerDisplay_.begin(); iter != windowsPerDisplay_.end(); ++iter) {
5858 int32_t displayId = iter->first;
5859 const std::vector<WindowInfo> &windowsInfo = iter->second.windowsInfo;
5860 if (displayId < 0) {
5861 MMI_HILOGE("windowsPerDisplay_ contain invalid displayId:%{public}d", displayId);
5862 continue;
5863 }
5864 for (const auto& item : windowsInfo) {
5865 if (item.id == windowId &&
5866 (item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) != WindowInfo::FLAG_BIT_UNTOUCHABLE &&
5867 transparentWins_.find(item.id) == transparentWins_.end()) {
5868 return std::make_optional(item);
5869 }
5870 }
5871 }
5872 return std::nullopt;
5873 }
5874
5875 int32_t InputWindowsManager::ShiftAppMousePointerEvent(const ShiftWindowInfo &shiftWindowInfo, bool autoGenDown)
5876 {
5877 auto lastPointerEventCopy = GetlastPointerEvent();
5878 if (!lastPointerEventCopy || !lastPointerEventCopy->IsButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT)) {
5879 MMI_HILOGE("Failed shift pointerEvent, left mouse button is not pressed");
5880 return RET_ERR;
5881 }
5882 const WindowInfo &sourceWindowInfo = shiftWindowInfo.sourceWindowInfo;
5883 const WindowInfo &targetWindowInfo = shiftWindowInfo.targetWindowInfo;
5884 std::shared_ptr<PointerEvent> pointerEvent = std::make_shared<PointerEvent>(*lastPointerEventCopy);
5885 pointerEvent->ClearButtonPressed();
5886
5887 int32_t pointerId = pointerEvent->GetPointerId();
5888 PointerEvent::PointerItem item;
5889 pointerEvent->GetPointerItem(pointerId, item);
5890 item.SetWindowX(lastLogicX_ - sourceWindowInfo.area.x);
5891 item.SetWindowY(lastLogicY_ - sourceWindowInfo.area.y);
5892 item.SetPressed(false);
5893 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
5894 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
5895 pointerEvent->SetButtonId(PointerEvent::MOUSE_BUTTON_LEFT);
5896 pointerEvent->SetTargetDisplayId(sourceWindowInfo.displayId);
5897 pointerEvent->SetTargetWindowId(sourceWindowInfo.id);
5898 pointerEvent->SetAgentWindowId(sourceWindowInfo.agentWindowId);
5899 ClearTargetWindowId(pointerId);
5900 pointerEvent->UpdatePointerItem(pointerId, item);
5901 InputHandler->GetFilterHandler()->HandlePointerEvent(pointerEvent);
5902 if (autoGenDown) {
5903 item.SetWindowX(shiftWindowInfo.x);
5904 item.SetWindowY(shiftWindowInfo.y);
5905 if (shiftWindowInfo.x == -1 && shiftWindowInfo.y == -1) {
5906 item.SetWindowX(lastLogicX_ - targetWindowInfo.area.x);
5907 item.SetWindowY(lastLogicY_ - targetWindowInfo.area.y);
5908 }
5909 item.SetPressed(true);
5910 pointerEvent->ClearButtonPressed();
5911 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
5912 pointerEvent->SetButtonPressed(PointerEvent::MOUSE_BUTTON_LEFT);
5913 pointerEvent->UpdatePointerItem(pointerId, item);
5914 pointerEvent->SetTargetDisplayId(targetWindowInfo.displayId);
5915 pointerEvent->SetTargetWindowId(targetWindowInfo.id);
5916 pointerEvent->SetAgentWindowId(targetWindowInfo.agentWindowId);
5917 HITRACE_METER_NAME(HITRACE_TAG_MULTIMODALINPUT, "shift pointer event dispatch down event");
5918 InputHandler->GetFilterHandler()->HandlePointerEvent(pointerEvent);
5919 }
5920 firstBtnDownWindowInfo_.first = targetWindowInfo.id;
5921 MMI_HILOGI("shift pointer event success for mouse");
5922 return RET_OK;
5923 }
5924
5925 int32_t InputWindowsManager::ShiftAppPointerEvent(const ShiftWindowParam ¶m, bool autoGenDown)
5926 {
5927 MMI_HILOGI("Start shift pointer event, sourceWindowId:%{public}d, targetWindowId:%{public}d,"
5928 "x:%{private}d, y:%{private}d, autoGenDown:%{public}d",
5929 param.sourceWindowId, param.targetWindowId, param.x, param.y, static_cast<int32_t>(autoGenDown));
5930 std::optional<WindowInfo> sourceWindowInfo = GetWindowInfoById(param.sourceWindowId);
5931 std::optional<WindowInfo> targetWindowInfo = GetWindowInfoById(param.targetWindowId);
5932 if (!sourceWindowInfo || !targetWindowInfo) {
5933 MMI_HILOGE("Failed shift pointerEvent, get null sourceWindowInfo, source:%{public}d, target:%{public}d",
5934 static_cast<int32_t>(!!sourceWindowInfo), static_cast<int32_t>(!!targetWindowInfo));
5935 return RET_ERR;
5936 }
5937 ShiftWindowInfo shiftWindowInfo;
5938 shiftWindowInfo.sourceWindowInfo = *sourceWindowInfo;
5939 shiftWindowInfo.targetWindowInfo = *targetWindowInfo;
5940 shiftWindowInfo.x = param.x;
5941 shiftWindowInfo.y = param.y;
5942 return ShiftAppMousePointerEvent(shiftWindowInfo, autoGenDown);
5943 }
5944 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
5945
5946 #if defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
5947 bool InputWindowsManager::CancelTouch(int32_t touch)
5948 {
5949 auto iter = touchItemDownInfos_.find(touch);
5950 if ((iter != touchItemDownInfos_.end()) && iter->second.flag) {
5951 iter->second.flag = false;
5952 return true;
5953 }
5954 return false;
5955 }
5956
5957 void InputWindowsManager::AttachTouchGestureMgr(std::shared_ptr<TouchGestureManager> touchGestureMgr)
5958 {
5959 touchGestureMgr_ = touchGestureMgr;
5960 }
5961
5962 void InputWindowsManager::CancelAllTouches(std::shared_ptr<PointerEvent> event)
5963 {
5964 CHKPV(event);
5965 auto pointerEvent = std::make_shared<PointerEvent>(*event);
5966 int32_t originAction = pointerEvent->GetPointerAction();
5967 pointerEvent->SetOriginPointerAction(originAction);
5968 auto items = event->GetAllPointerItems();
5969 for (const auto &item : items) {
5970 if (!item.IsPressed()) {
5971 continue;
5972 }
5973 int32_t pointerId = item.GetPointerId();
5974 int32_t action = PointerEvent::POINTER_ACTION_CANCEL;
5975 bool isDragging = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
5976 (item.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId);
5977 if (isDragging) {
5978 action = PointerEvent::POINTER_ACTION_PULL_CANCEL;
5979 }
5980 pointerEvent->SetPointerAction(action);
5981 pointerEvent->AddFlag(InputEvent::EVENT_FLAG_NO_INTERCEPT);
5982 pointerEvent->SetPointerId(pointerId);
5983
5984 if (AdjustFingerFlag(pointerEvent)) {
5985 continue;
5986 }
5987 MMI_HILOGI("Cancel touch, pointerId:%{public}d, action:%{public}d", pointerId, action);
5988 auto now = GetSysClockTime();
5989 pointerEvent->SetActionTime(now);
5990 pointerEvent->SetTargetWindowId(item.GetTargetWindowId());
5991 auto winOpt = GetWindowAndDisplayInfo(item.GetTargetWindowId(), pointerEvent->GetTargetDisplayId());
5992 if (winOpt) {
5993 pointerEvent->SetAgentWindowId(winOpt->agentWindowId);
5994 }
5995 pointerEvent->UpdateId();
5996 auto filter = InputHandler->GetFilterHandler();
5997 CHKPV(filter);
5998 filter->HandleTouchEvent(pointerEvent);
5999 }
6000 }
6001 #endif // defined(OHOS_BUILD_ENABLE_TOUCH) && defined(OHOS_BUILD_ENABLE_MONITOR)
6002
6003 std::shared_ptr<PointerEvent> InputWindowsManager::GetlastPointerEvent()
6004 {
6005 std::lock_guard<std::mutex> guard(mtx_);
6006 return lastPointerEvent_;
6007 }
6008 } // namespace MMI
6009 } // namespace OHOS
6010