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