1 /*
2 * Copyright (c) 2021-2022 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 <cstdlib>
19 #include <cstdio>
20 #include <linux/input.h>
21
22 #include "dfx_hisysevent.h"
23 #include "input_device_manager.h"
24 #include "i_pointer_drawing_manager.h"
25 #include "mouse_event_normalize.h"
26 #include "pointer_drawing_manager.h"
27 #include "preferences.h"
28 #include "preferences_impl.h"
29 #include "preferences_errno.h"
30 #include "preferences_helper.h"
31 #include "preferences_xml_utils.h"
32 #include "util.h"
33 #include "util_ex.h"
34 #include "util_napi_error.h"
35 #include "input_device_manager.h"
36
37 namespace OHOS {
38 namespace MMI {
39 namespace {
40 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputWindowsManager" };
41 #ifdef OHOS_BUILD_ENABLE_POINTER
42 constexpr int32_t DEFAULT_POINTER_STYLE = 0;
43 #endif // OHOS_BUILD_ENABLE_POINTER
44 const std::string bindCfgFileName = "/data/service/el1/public/multimodalinput/display_bind.cfg";
45 const std::string mouseFileName = "/data/service/el1/public/multimodalinput/mouse_settings.xml";
46 } // namespace
47
InputWindowsManager()48 InputWindowsManager::InputWindowsManager() : bindInfo_(bindCfgFileName)
49 {
50 MMI_HILOGI("Bind cfg file name:%{public}s", bindCfgFileName.c_str());
51 }
52
~InputWindowsManager()53 InputWindowsManager::~InputWindowsManager() {}
54
DeviceStatusChanged(int32_t deviceId,const std::string & sysUid,const std::string devStatus)55 void InputWindowsManager::DeviceStatusChanged(int32_t deviceId, const std::string &sysUid, const std::string devStatus)
56 {
57 CALL_INFO_TRACE;
58 if (devStatus == "add") {
59 bindInfo_.AddInputDevice(deviceId, sysUid);
60 } else {
61 bindInfo_.RemoveInputDevice(deviceId);
62 }
63 }
64
Init(UDSServer & udsServer)65 void InputWindowsManager::Init(UDSServer& udsServer)
66 {
67 udsServer_ = &udsServer;
68 CHKPV(udsServer_);
69 bindInfo_.Load();
70 #ifdef OHOS_BUILD_ENABLE_POINTER
71 udsServer_->AddSessionDeletedCallback(std::bind(&InputWindowsManager::OnSessionLost, this, std::placeholders::_1));
72 InitMouseDownInfo();
73 #endif // OHOS_BUILD_ENABLE_POINTER
74 InputDevMgr->SetInputStatusChangeCallback(std::bind(&InputWindowsManager::DeviceStatusChanged, this,
75 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
76 }
77
78 #ifdef OHOS_BUILD_ENABLE_POINTER
InitMouseDownInfo()79 void InputWindowsManager::InitMouseDownInfo()
80 {
81 mouseDownInfo_.id = -1;
82 mouseDownInfo_.pid = -1;
83 mouseDownInfo_.defaultHotAreas.clear();
84 mouseDownInfo_.pointerHotAreas.clear();
85 }
86 #endif // OHOS_BUILD_ENABLE_POINTER
87
GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)88 int32_t InputWindowsManager::GetClientFd(std::shared_ptr<PointerEvent> pointerEvent)
89 {
90 CALL_DEBUG_ENTER;
91 CHKPR(pointerEvent, INVALID_FD);
92 const WindowInfo* windowInfo = nullptr;
93 for (const auto &item : displayGroupInfo_.windowsInfo) {
94 if (item.id == pointerEvent->GetTargetWindowId()) {
95 MMI_HILOGD("find windowinfo by window id %{public}d", item.id);
96 windowInfo = &item;
97 break;
98 }
99 }
100
101 CHKPR(udsServer_, INVALID_FD);
102 if (windowInfo != nullptr) {
103 MMI_HILOGD("get pid:%{public}d from idxPidMap", windowInfo->pid);
104 return udsServer_->GetClientFd(windowInfo->pid);
105 }
106 if (pointerEvent->GetPointerAction() != PointerEvent::POINTER_ACTION_CANCEL) {
107 MMI_HILOGD("window info is null, so pointerEvent is dropped! return -1");
108 return udsServer_->GetClientFd(-1);
109 }
110 int32_t pid = -1;
111 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
112 auto iter = touchItemDownInfos_.find(pointerEvent->GetPointerId());
113 if (iter != touchItemDownInfos_.end()) {
114 pid = GetWindowPid(iter->second.agentWindowId);
115 MMI_HILOGD("touchscreen occurs, new pid:%{public}d", pid);
116 touchItemDownInfos_.erase(iter);
117 }
118 }
119 #ifdef OHOS_BUILD_ENABLE_POINTER
120 if (pointerEvent->GetSourceType() == PointerEvent::SOURCE_TYPE_MOUSE) {
121 if (mouseDownInfo_.pid != -1) {
122 pid = GetWindowPid(mouseDownInfo_.agentWindowId);
123 MMI_HILOGD("mouseevent occurs, update the pid:%{public}d", pid);
124 InitMouseDownInfo();
125 }
126 }
127 #endif // OHOS_BUILD_ENABLE_POINTER
128 MMI_HILOGD("get clientFd by %{public}d", pid);
129 return udsServer_->GetClientFd(pid);
130 }
131
132 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
UpdateTarget(std::shared_ptr<InputEvent> inputEvent)133 int32_t InputWindowsManager::UpdateTarget(std::shared_ptr<InputEvent> inputEvent)
134 {
135 CHKPR(inputEvent, INVALID_FD);
136 CALL_DEBUG_ENTER;
137 int32_t pid = GetPidAndUpdateTarget(inputEvent);
138 if (pid <= 0) {
139 MMI_HILOGE("Invalid pid");
140 return INVALID_FD;
141 }
142 int32_t fd = udsServer_->GetClientFd(pid);
143 if (fd < 0) {
144 MMI_HILOGE("Invalid fd");
145 return INVALID_FD;
146 }
147 return fd;
148 }
149 #endif // OHOS_BUILD_ENABLE_KEYBOARD
150
GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const151 int32_t InputWindowsManager::GetDisplayId(std::shared_ptr<InputEvent> inputEvent) const
152 {
153 int32_t displayId = inputEvent->GetTargetDisplayId();
154 if (displayId < 0) {
155 MMI_HILOGD("Target display is -1");
156 if (displayGroupInfo_.displaysInfo.empty()) {
157 return displayId;
158 }
159 displayId = displayGroupInfo_.displaysInfo[0].id;
160 inputEvent->SetTargetDisplayId(displayId);
161 }
162 return displayId;
163 }
164
165 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
GetPidAndUpdateTarget(std::shared_ptr<InputEvent> inputEvent)166 int32_t InputWindowsManager::GetPidAndUpdateTarget(std::shared_ptr<InputEvent> inputEvent)
167 {
168 CALL_DEBUG_ENTER;
169 CHKPR(inputEvent, INVALID_PID);
170 const int32_t focusWindowId = displayGroupInfo_.focusWindowId;
171 WindowInfo* windowInfo = nullptr;
172 for (auto &item : displayGroupInfo_.windowsInfo) {
173 if (item.id == focusWindowId) {
174 windowInfo = &item;
175 break;
176 }
177 }
178 CHKPR(windowInfo, INVALID_PID);
179 inputEvent->SetTargetWindowId(windowInfo->id);
180 inputEvent->SetAgentWindowId(windowInfo->agentWindowId);
181 MMI_HILOGD("focusWindowId:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
182 return windowInfo->pid;
183 }
184 #endif // OHOS_BUILD_ENABLE_KEYBOARD
185
GetWindowPid(int32_t windowId) const186 int32_t InputWindowsManager::GetWindowPid(int32_t windowId) const
187 {
188 CALL_DEBUG_ENTER;
189 int32_t windowPid = -1;
190 for (const auto &item : displayGroupInfo_.windowsInfo) {
191 MMI_HILOGD("get windowID %{public}d", item.id);
192 if (item.id == windowId) {
193 windowPid = item.pid;
194 break;
195 }
196 }
197 return windowPid;
198 }
199
GetWindowPid(int32_t windowId,const DisplayGroupInfo & displayGroupInfo) const200 int32_t InputWindowsManager::GetWindowPid(int32_t windowId, const DisplayGroupInfo& displayGroupInfo) const
201 {
202 int32_t windowPid = -1;
203 for (const auto &item : displayGroupInfo.windowsInfo) {
204 if (item.id == windowId) {
205 windowPid = item.pid;
206 break;
207 }
208 }
209 return windowPid;
210 }
211
CheckFocusWindowChange(const DisplayGroupInfo & displayGroupInfo)212 void InputWindowsManager::CheckFocusWindowChange(const DisplayGroupInfo &displayGroupInfo)
213 {
214 const int32_t oldFocusWindowId = displayGroupInfo_.focusWindowId;
215 const int32_t newFocusWindowId = displayGroupInfo.focusWindowId;
216 if (oldFocusWindowId == newFocusWindowId) {
217 return;
218 }
219 const int32_t oldFocusWindowPid = GetWindowPid(oldFocusWindowId);
220 const int32_t newFocusWindowPid = GetWindowPid(newFocusWindowId, displayGroupInfo);
221 DfxHisysevent::OnFocusWindowChanged(oldFocusWindowId, newFocusWindowId, oldFocusWindowPid, newFocusWindowPid);
222 }
223
CheckZorderWindowChange(const DisplayGroupInfo & displayGroupInfo)224 void InputWindowsManager::CheckZorderWindowChange(const DisplayGroupInfo &displayGroupInfo)
225 {
226 int32_t oldZorderFirstWindowId = -1;
227 int32_t newZorderFirstWindowId = -1;
228 if (!displayGroupInfo_.windowsInfo.empty()) {
229 oldZorderFirstWindowId = displayGroupInfo_.windowsInfo[0].id;
230 }
231 if (!displayGroupInfo.windowsInfo.empty()) {
232 newZorderFirstWindowId = displayGroupInfo.windowsInfo[0].id;
233 }
234 if (oldZorderFirstWindowId == newZorderFirstWindowId) {
235 return;
236 }
237 const int32_t oldZorderFirstWindowPid = GetWindowPid(oldZorderFirstWindowId);
238 const int32_t newZorderFirstWindowPid = GetWindowPid(newZorderFirstWindowId, displayGroupInfo);
239 DfxHisysevent::OnZorderWindowChanged(oldZorderFirstWindowId, newZorderFirstWindowId,
240 oldZorderFirstWindowPid, newZorderFirstWindowPid);
241 }
242
UpdateDisplayIdAndName()243 void InputWindowsManager::UpdateDisplayIdAndName()
244 {
245 using IdNames = std::set<std::pair<int32_t, std::string>>;
246 IdNames newInfo;
247 for (const auto &item : displayGroupInfo_.displaysInfo) {
248 newInfo.insert(std::make_pair(item.id, item.uniq));
249 }
250 auto oldInfo = bindInfo_.GetDisplayIdNames();
251 if (newInfo == oldInfo) {
252 return;
253 }
254 for (auto it = oldInfo.begin(); it != oldInfo.end();) {
255 if (newInfo.find(*it) == newInfo.end()) {
256 bindInfo_.RemoveDisplay(it->first);
257 oldInfo.erase(it++);
258 } else {
259 ++it;
260 }
261 }
262 for (const auto &item : newInfo) {
263 if (!bindInfo_.IsDisplayAdd(item.first, item.second)) {
264 bindInfo_.AddDisplay(item.first, item.second);
265 }
266 }
267 }
268
GetDisplayBindInfo(DisplayBindInfos & infos)269 int32_t InputWindowsManager::GetDisplayBindInfo(DisplayBindInfos &infos)
270 {
271 CALL_DEBUG_ENTER;
272 return bindInfo_.GetDisplayBindInfo(infos);
273 }
274
SetDisplayBind(int32_t deviceId,int32_t displayId,std::string & msg)275 int32_t InputWindowsManager::SetDisplayBind(int32_t deviceId, int32_t displayId, std::string &msg)
276 {
277 CALL_DEBUG_ENTER;
278 return bindInfo_.SetDisplayBind(deviceId, displayId, msg);
279 }
280
UpdateDisplayInfo(const DisplayGroupInfo & displayGroupInfo)281 void InputWindowsManager::UpdateDisplayInfo(const DisplayGroupInfo &displayGroupInfo)
282 {
283 CALL_DEBUG_ENTER;
284 CheckFocusWindowChange(displayGroupInfo);
285 CheckZorderWindowChange(displayGroupInfo);
286 if (captureModeInfo_.isCaptureMode &&
287 ((displayGroupInfo_.focusWindowId != displayGroupInfo.focusWindowId) ||
288 (displayGroupInfo_.windowsInfo[0].id != displayGroupInfo.windowsInfo[0].id))) {
289 captureModeInfo_.isCaptureMode = false;
290 }
291 displayGroupInfo_ = displayGroupInfo;
292 PrintDisplayInfo();
293 UpdateDisplayIdAndName();
294 #ifdef OHOS_BUILD_ENABLE_POINTER
295 UpdatePointerStyle();
296 #endif // OHOS_BUILD_ENABLE_POINTER
297
298 if (!displayGroupInfo.displaysInfo.empty()) {
299 #if defined(OHOS_BUILD_ENABLE_POINTER) && defined(OHOS_BUILD_ENABLE_POINTER_DRAWING)
300 IPointerDrawingManager::GetInstance()->OnDisplayInfo(displayGroupInfo);
301 if (InputDevMgr->HasPointerDevice()) {
302 MouseLocation mouseLocation = GetMouseInfo();
303 int32_t displayId = MouseEventHdr->GetDisplayId();
304 if (displayId < 0) {
305 displayId = displayGroupInfo_.displaysInfo[0].id;
306 }
307 auto displayInfo = GetPhysicalDisplay(displayId);
308 CHKPV(displayInfo);
309 int32_t logicX = mouseLocation.physicalX + displayInfo->x;
310 int32_t logicY = mouseLocation.physicalY + displayInfo->y;
311 std::optional<WindowInfo> windowInfo;
312 CHKPV(lastPointerEvent_);
313 if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE &&
314 lastPointerEvent_->GetPressedButtons().empty()) {
315 windowInfo = GetWindowInfo(logicX, logicY);
316 } else {
317 windowInfo = SelectWindowInfo(logicX, logicY, lastPointerEvent_);
318 }
319 if (!windowInfo) {
320 MMI_HILOGE("The windowInfo is nullptr");
321 return;
322 }
323 int32_t windowPid = GetWindowPid(windowInfo->id);
324 WinInfo info = { .windowPid = windowPid, .windowId = windowInfo->id };
325 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
326 IPointerDrawingManager::GetInstance()->DrawPointerStyle();
327 }
328 #endif // OHOS_BUILD_ENABLE_POINTER && OHOS_BUILD_ENABLE_POINTER_DRAWING
329 }
330 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
331 if (InputDevMgr->HasPointerDevice()) {
332 #ifdef OHOS_BUILD_ENABLE_POINTER
333 NotifyPointerToWindow();
334 #endif // OHOS_BUILD_ENABLE_POINTER
335 }
336 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
337 }
338
339 #ifdef OHOS_BUILD_ENABLE_POINTER
SendPointerEvent(int32_t pointerAction)340 void InputWindowsManager::SendPointerEvent(int32_t pointerAction)
341 {
342 CALL_INFO_TRACE;
343 CHKPV(udsServer_);
344 auto pointerEvent = PointerEvent::Create();
345 CHKPV(pointerEvent);
346 pointerEvent->UpdateId();
347 MouseLocation mouseLocation = GetMouseInfo();
348 lastLogicX_ = mouseLocation.physicalX;
349 lastLogicY_ = mouseLocation.physicalY;
350 if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW) {
351 auto touchWindow = GetWindowInfo(lastLogicX_, lastLogicY_);
352 if (!touchWindow) {
353 MMI_HILOGE("TouchWindow is nullptr");
354 return;
355 }
356 lastWindowInfo_ = *touchWindow;
357 }
358 PointerEvent::PointerItem pointerItem;
359 pointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
360 pointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
361 pointerItem.SetDisplayX(lastLogicX_);
362 pointerItem.SetDisplayY(lastLogicY_);
363 pointerItem.SetPointerId(0);
364
365 pointerEvent->SetTargetDisplayId(-1);
366 auto displayId = pointerEvent->GetTargetDisplayId();
367 if (!UpdateDisplayId(displayId)) {
368 MMI_HILOGE("This display:%{public}d is not existent", displayId);
369 return;
370 }
371 pointerEvent->SetTargetDisplayId(displayId);
372 pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
373 pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
374 pointerEvent->SetPointerId(0);
375 pointerEvent->AddPointerItem(pointerItem);
376 pointerEvent->SetPointerAction(pointerAction);
377 pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
378 int64_t time = GetSysClockTime();
379 pointerEvent->SetActionTime(time);
380 pointerEvent->SetActionStartTime(time);
381 pointerEvent->UpdateId();
382 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
383 pointerEvent->SetBuffer(extraData_.buffer);
384 UpdatePointerAction(pointerEvent);
385 } else {
386 pointerEvent->ClearBuffer();
387 }
388
389 auto fd = udsServer_->GetClientFd(lastWindowInfo_.pid);
390 auto sess = udsServer_->GetSession(fd);
391 if (sess == nullptr) {
392 MMI_HILOGI("The last window has disappeared");
393 return;
394 }
395 NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
396 InputEventDataTransformation::Marshalling(pointerEvent, pkt);
397 if (!sess->SendMsg(pkt)) {
398 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
399 return;
400 }
401 }
402
DispatchPointer(int32_t pointerAction)403 void InputWindowsManager::DispatchPointer(int32_t pointerAction)
404 {
405 CALL_INFO_TRACE;
406 CHKPV(udsServer_);
407 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
408 MMI_HILOGI("the mouse is hide");
409 return;
410 }
411 if (lastPointerEvent_ == nullptr) {
412 SendPointerEvent(pointerAction);
413 return;
414 }
415 auto pointerEvent = PointerEvent::Create();
416 CHKPV(pointerEvent);
417 pointerEvent->UpdateId();
418
419 PointerEvent::PointerItem lastPointerItem;
420 int32_t lastPointerId = lastPointerEvent_->GetPointerId();
421 if (!lastPointerEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
422 MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
423 return;
424 }
425 if (pointerAction == PointerEvent::POINTER_ACTION_ENTER_WINDOW) {
426 std::optional<WindowInfo> windowInfo;
427 if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE &&
428 lastPointerEvent_->GetPressedButtons().empty()) {
429 windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
430 } else {
431 windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEvent_);
432 }
433 if (!windowInfo) {
434 MMI_HILOGE("windowInfo is nullptr");
435 return;
436 }
437 if (windowInfo->id != lastWindowInfo_.id) {
438 lastWindowInfo_ = *windowInfo;
439 }
440 }
441 PointerEvent::PointerItem currentPointerItem;
442 currentPointerItem.SetWindowX(lastLogicX_ - lastWindowInfo_.area.x);
443 currentPointerItem.SetWindowY(lastLogicY_ - lastWindowInfo_.area.y);
444 currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
445 currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
446 currentPointerItem.SetPointerId(0);
447
448 pointerEvent->SetTargetDisplayId(lastPointerEvent_->GetTargetDisplayId());
449 pointerEvent->SetTargetWindowId(lastWindowInfo_.id);
450 pointerEvent->SetAgentWindowId(lastWindowInfo_.agentWindowId);
451 pointerEvent->SetPointerId(0);
452 pointerEvent->AddPointerItem(currentPointerItem);
453 pointerEvent->SetPointerAction(pointerAction);
454 pointerEvent->SetSourceType(lastPointerEvent_->GetSourceType());
455 int64_t time = GetSysClockTime();
456 pointerEvent->SetActionTime(time);
457 pointerEvent->SetActionStartTime(time);
458 pointerEvent->SetDeviceId(lastPointerEvent_->GetDeviceId());
459 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
460 pointerEvent->SetBuffer(extraData_.buffer);
461 UpdatePointerAction(pointerEvent);
462 } else {
463 pointerEvent->ClearBuffer();
464 }
465 int pid = lastWindowInfo_.pid;
466 if (pointerAction == PointerEvent::POINTER_ACTION_LEAVE_WINDOW) {
467 pointerEvent->SetAgentWindowId(lastWindowInfo_.id);
468 }
469 auto fd = udsServer_->GetClientFd(pid);
470 if (fd == RET_ERR) {
471 auto windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
472 if (!windowInfo) {
473 MMI_HILOGE("The windowInfo is nullptr");
474 return;
475 }
476 fd = udsServer_->GetClientFd(windowInfo->pid);
477 }
478 auto sess = udsServer_->GetSession(fd);
479 if (sess == nullptr) {
480 MMI_HILOGI("The last window has disappeared");
481 return;
482 }
483
484 NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
485 InputEventDataTransformation::Marshalling(pointerEvent, pkt);
486 if (!sess->SendMsg(pkt)) {
487 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
488 return;
489 }
490 }
491
NotifyPointerToWindow()492 void InputWindowsManager::NotifyPointerToWindow()
493 {
494 CALL_INFO_TRACE;
495 std::optional<WindowInfo> windowInfo;
496 CHKPV(lastPointerEvent_);
497 if (lastPointerEvent_->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE &&
498 lastPointerEvent_->GetPressedButtons().empty()) {
499 windowInfo = GetWindowInfo(lastLogicX_, lastLogicY_);
500 } else {
501 windowInfo = SelectWindowInfo(lastLogicX_, lastLogicY_, lastPointerEvent_);
502 }
503 if (!windowInfo) {
504 MMI_HILOGE("The windowInfo is nullptr");
505 return;
506 }
507 if (windowInfo->id == lastWindowInfo_.id) {
508 MMI_HILOGI("The mouse pointer does not leave the window:%{public}d", lastWindowInfo_.id);
509 lastWindowInfo_ = *windowInfo;
510 return;
511 }
512 bool isFindLastWindow = false;
513 for (const auto &item : displayGroupInfo_.windowsInfo) {
514 if (item.id == lastWindowInfo_.id) {
515 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
516 isFindLastWindow = true;
517 break;
518 }
519 }
520 if (!isFindLastWindow) {
521 if (udsServer_ != nullptr && udsServer_->GetClientFd(lastWindowInfo_.pid) != INVALID_FD) {
522 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
523 }
524 }
525 lastWindowInfo_ = *windowInfo;
526 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
527 }
528 #endif // OHOS_BUILD_ENABLE_POINTER
529
PrintDisplayInfo()530 void InputWindowsManager::PrintDisplayInfo()
531 {
532 MMI_HILOGD("logicalInfo,width:%{public}d,height:%{public}d,focusWindowId:%{public}d",
533 displayGroupInfo_.width, displayGroupInfo_.height, displayGroupInfo_.focusWindowId);
534 MMI_HILOGD("windowsInfos,num:%{public}zu", displayGroupInfo_.windowsInfo.size());
535 for (const auto &item : displayGroupInfo_.windowsInfo) {
536 MMI_HILOGD("windowsInfos,id:%{public}d,pid:%{public}d,uid:%{public}d,"
537 "area.x:%{public}d,area.y:%{public}d,area.width:%{public}d,area.height:%{public}d,"
538 "defaultHotAreas.size:%{public}zu,pointerHotAreas.size:%{public}zu,"
539 "agentWindowId:%{public}d,flags:%{public}d",
540 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
541 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
542 item.agentWindowId, item.flags);
543 for (const auto &win : item.defaultHotAreas) {
544 MMI_HILOGD("defaultHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
545 win.x, win.y, win.width, win.height);
546 }
547 for (const auto &pointer : item.pointerHotAreas) {
548 MMI_HILOGD("pointerHotAreas:x:%{public}d,y:%{public}d,width:%{public}d,height:%{public}d",
549 pointer.x, pointer.y, pointer.width, pointer.height);
550 }
551 }
552
553 MMI_HILOGD("displayInfos,num:%{public}zu", displayGroupInfo_.displaysInfo.size());
554 for (const auto &item : displayGroupInfo_.displaysInfo) {
555 MMI_HILOGD("displayInfos,id:%{public}d,x:%{public}d,y:%{public}d,"
556 "width:%{public}d,height:%{public}d,name:%{public}s,"
557 "uniq:%{public}s,direction:%{public}d",
558 item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
559 item.uniq.c_str(), item.direction);
560 }
561 }
562
563 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
GetPhysicalDisplay(int32_t id) const564 const DisplayInfo* InputWindowsManager::GetPhysicalDisplay(int32_t id) const
565 {
566 for (auto &it : displayGroupInfo_.displaysInfo) {
567 if (it.id == id) {
568 return ⁢
569 }
570 }
571 MMI_HILOGW("Failed to obtain physical(%{public}d) display", id);
572 return nullptr;
573 }
574 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
575
576 #ifdef OHOS_BUILD_ENABLE_TOUCH
FindPhysicalDisplayInfo(const std::string & uniq) const577 const DisplayInfo* InputWindowsManager::FindPhysicalDisplayInfo(const std::string& uniq) const
578 {
579 for (auto &it : displayGroupInfo_.displaysInfo) {
580 if (it.uniq == uniq) {
581 return ⁢
582 }
583 }
584 MMI_HILOGE("Failed to search for Physical,uniq:%{public}s", uniq.c_str());
585 return nullptr;
586 }
587
RotateTouchScreen(DisplayInfo info,LogicalCoordinate & coord) const588 void InputWindowsManager::RotateTouchScreen(DisplayInfo info, LogicalCoordinate& coord) const
589 {
590 const Direction direction = info.direction;
591
592 if (direction == DIRECTION0) {
593 MMI_HILOGD("direction is DIRECTION0");
594 return;
595 }
596 if (direction == DIRECTION90) {
597 MMI_HILOGD("direction is DIRECTION90");
598 int32_t temp = coord.x;
599 coord.x = info.height - coord.y;
600 coord.y = temp;
601 MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
602 return;
603 }
604 if (direction == DIRECTION180) {
605 MMI_HILOGD("direction is DIRECTION180");
606 coord.x = info.width - coord.x;
607 coord.y = info.height - coord.y;
608 MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
609 return;
610 }
611 if (direction == DIRECTION270) {
612 MMI_HILOGD("direction is DIRECTION270");
613 int32_t temp = coord.y;
614 coord.y = info.width - coord.x;
615 coord.x = temp;
616 MMI_HILOGD("physicalX:%{public}d, physicalY:%{public}d", coord.x, coord.y);
617 }
618 }
619
GetPhysicalDisplayCoord(struct libinput_event_touch * touch,const DisplayInfo & info,EventTouch & touchInfo)620 void InputWindowsManager::GetPhysicalDisplayCoord(struct libinput_event_touch* touch,
621 const DisplayInfo& info, EventTouch& touchInfo)
622 {
623 LogicalCoordinate coord {
624 .x = static_cast<int32_t>(libinput_event_touch_get_x_transformed(touch, info.width)),
625 .y = static_cast<int32_t>(libinput_event_touch_get_y_transformed(touch, info.height)),
626 };
627 RotateTouchScreen(info, coord);
628 touchInfo.point.x = coord.x;
629 touchInfo.point.y = coord.y;
630 touchInfo.toolRect.point.x = static_cast<int32_t>(libinput_event_touch_get_tool_x_transformed(touch, info.width));
631 touchInfo.toolRect.point.y = static_cast<int32_t>(libinput_event_touch_get_tool_y_transformed(touch, info.height));
632 touchInfo.toolRect.width = static_cast<int32_t>(
633 libinput_event_touch_get_tool_width_transformed(touch, info.width));
634 touchInfo.toolRect.height = static_cast<int32_t>(
635 libinput_event_touch_get_tool_height_transformed(touch, info.height));
636 }
637
TouchPointToDisplayPoint(int32_t deviceId,struct libinput_event_touch * touch,EventTouch & touchInfo,int32_t & physicalDisplayId)638 bool InputWindowsManager::TouchPointToDisplayPoint(int32_t deviceId, struct libinput_event_touch* touch,
639 EventTouch& touchInfo, int32_t& physicalDisplayId)
640 {
641 CHKPF(touch);
642 std::string screenId = bindInfo_.GetBindDisplayNameByInputDevice(deviceId);
643 if (screenId.empty()) {
644 screenId = "default0";
645 }
646 auto info = FindPhysicalDisplayInfo(screenId);
647 CHKPF(info);
648 physicalDisplayId = info->id;
649 if ((info->width <= 0) || (info->height <= 0)) {
650 MMI_HILOGE("Get DisplayInfo is error");
651 return false;
652 }
653 GetPhysicalDisplayCoord(touch, *info, touchInfo);
654 return true;
655 }
656
TransformTipPoint(struct libinput_event_tablet_tool * tip,LogicalCoordinate & coord,int32_t & displayId) const657 bool InputWindowsManager::TransformTipPoint(struct libinput_event_tablet_tool* tip,
658 LogicalCoordinate& coord, int32_t& displayId) const
659 {
660 CHKPF(tip);
661 auto displayInfo = FindPhysicalDisplayInfo("default0");
662 CHKPF(displayInfo);
663 MMI_HILOGD("PhysicalDisplay.width:%{public}d, PhysicalDisplay.height:%{public}d, "
664 "PhysicalDisplay.topLeftX:%{public}d, PhysicalDisplay.topLeftY:%{public}d",
665 displayInfo->width, displayInfo->height, displayInfo->x, displayInfo->y);
666 displayId = displayInfo->id;
667 PhysicalCoordinate phys {
668 .x = libinput_event_tablet_tool_get_x_transformed(tip, displayInfo->width),
669 .y = libinput_event_tablet_tool_get_y_transformed(tip, displayInfo->height)
670 };
671
672 coord.x = static_cast<int32_t>(phys.x);
673 coord.y = static_cast<int32_t>(phys.y);
674 MMI_HILOGD("physicalX:%{public}f, physicalY:%{public}f, displayId:%{public}d", phys.x, phys.y, displayId);
675 return true;
676 }
677
CalculateTipPoint(struct libinput_event_tablet_tool * tip,int32_t & targetDisplayId,LogicalCoordinate & coord) const678 bool InputWindowsManager::CalculateTipPoint(struct libinput_event_tablet_tool* tip,
679 int32_t& targetDisplayId, LogicalCoordinate& coord) const
680 {
681 CHKPF(tip);
682 if (!TransformTipPoint(tip, coord, targetDisplayId)) {
683 return false;
684 }
685 return true;
686 }
687 #endif // OHOS_BUILD_ENABLE_TOUCH
688
689 #ifdef OHOS_BUILD_ENABLE_POINTER
GetDisplayGroupInfo()690 const DisplayGroupInfo& InputWindowsManager::GetDisplayGroupInfo()
691 {
692 return displayGroupInfo_;
693 }
694
695 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
IsNeedRefreshLayer(int32_t windowId)696 bool InputWindowsManager::IsNeedRefreshLayer(int32_t windowId)
697 {
698 CALL_DEBUG_ENTER;
699 MouseLocation mouseLocation = GetMouseInfo();
700 int32_t displayId = MouseEventHdr->GetDisplayId();
701 if (displayId < 0) {
702 displayId = displayGroupInfo_.displaysInfo[0].id;
703 }
704 auto displayInfo = GetPhysicalDisplay(displayId);
705 CHKPF(displayInfo);
706 int32_t logicX = mouseLocation.physicalX + displayInfo->x;
707 int32_t logicY = mouseLocation.physicalY + displayInfo->y;
708 std::optional<WindowInfo> touchWindow = GetWindowInfo(logicX, logicY);
709 if (!touchWindow) {
710 MMI_HILOGE("TouchWindow is nullptr");
711 return false;
712 }
713 if (touchWindow->id == windowId || windowId == GLOBAL_WINDOW_ID) {
714 MMI_HILOGD("Need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
715 touchWindow->id, windowId);
716 return true;
717 }
718
719 MMI_HILOGD("Not need refresh pointer style, focusWindow type:%{public}d, window type:%{public}d",
720 touchWindow->id, windowId);
721 return false;
722 }
723 #endif
724
OnSessionLost(SessionPtr session)725 void InputWindowsManager::OnSessionLost(SessionPtr session)
726 {
727 CALL_DEBUG_ENTER;
728 CHKPV(session);
729 int32_t pid = session->GetPid();
730
731 auto it = pointerStyle_.find(pid);
732 if (it != pointerStyle_.end()) {
733 pointerStyle_.erase(it);
734 MMI_HILOGD("Clear the pointer style map, pd:%{public}d", pid);
735 }
736 }
737
SetPointerStyle(int32_t pid,int32_t windowId,PointerStyle pointerStyle)738 int32_t InputWindowsManager::SetPointerStyle(int32_t pid, int32_t windowId, PointerStyle pointerStyle)
739 {
740 CALL_DEBUG_ENTER;
741 if (windowId == GLOBAL_WINDOW_ID) {
742 globalStyle_.id = pointerStyle.id;
743 MMI_HILOGD("Setting global pointer style");
744 return RET_OK;
745 }
746 MMI_HILOGD("start to get pid by window %{public}d", windowId);
747 // call to change window id
748 if (pid == -1) {
749 pid = GetWindowPid(windowId);
750 MMI_HILOGD("changing pid form -1 to %{public}d", pid);
751 }
752 auto it = pointerStyle_.find(pid);
753 if (it == pointerStyle_.end()) {
754 MMI_HILOGE("The pointer style map is not include param pd:%{public}d", pid);
755 return COMMON_PARAMETER_ERROR;
756 }
757 auto iter = it->second.find(windowId);
758 if (iter != it->second.end()) {
759 iter->second = pointerStyle;
760 return RET_OK;
761 }
762
763 for (const auto& windowInfo : displayGroupInfo_.windowsInfo) {
764 if (windowId == windowInfo.id && pid == windowInfo.pid) {
765 auto iterator = it->second.insert(std::make_pair(windowId, pointerStyle));
766 if (!iterator.second) {
767 MMI_HILOGW("The window type is duplicated");
768 }
769 return RET_OK;
770 }
771 }
772 MMI_HILOGE("The window id is invalid");
773 return COMMON_PARAMETER_ERROR;
774 }
775
GetPointerStyle(int32_t pid,int32_t windowId,PointerStyle & pointerStyle) const776 int32_t InputWindowsManager::GetPointerStyle(int32_t pid, int32_t windowId, PointerStyle &pointerStyle) const
777 {
778 CALL_DEBUG_ENTER;
779 if (windowId == GLOBAL_WINDOW_ID) {
780 MMI_HILOGD("Getting global pointer style");
781 pointerStyle.id = globalStyle_.id;
782 return RET_OK;
783 }
784 auto it = pointerStyle_.find(pid);
785 if (it == pointerStyle_.end()) {
786 MMI_HILOGE("The pointer style map is not include param pd, %{public}d", pid);
787 return RET_ERR;
788 }
789 auto iter = it->second.find(windowId);
790 if (iter == it->second.end()) {
791 pointerStyle.id = globalStyle_.id;
792 return RET_OK;
793 }
794
795 MMI_HILOGD("Window type:%{public}d get pointer style:%{public}d success", windowId, iter->second.id);
796 pointerStyle = iter->second;
797 return RET_OK;
798 }
799
UpdatePointerStyle()800 void InputWindowsManager::UpdatePointerStyle()
801 {
802 CALL_DEBUG_ENTER;
803 PointerStyle pointerStyle;
804 pointerStyle.id = DEFAULT_POINTER_STYLE;
805 for (const auto& windowItem : displayGroupInfo_.windowsInfo) {
806 int32_t pid = windowItem.pid;
807 auto it = pointerStyle_.find(pid);
808 if (it == pointerStyle_.end()) {
809 std::map<int32_t, PointerStyle> tmpPointerStyle = {};
810 auto iter = pointerStyle_.insert(std::make_pair(pid, tmpPointerStyle));
811 if (!iter.second) {
812 MMI_HILOGW("The pd is duplicated");
813 }
814 continue;
815 }
816 }
817 MMI_HILOGD("Number of pointer style:%{public}zu", pointerStyle_.size());
818 }
819
820 #endif // OHOS_BUILD_ENABLE_POINTER
821
822 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
IsInHotArea(int32_t x,int32_t y,const std::vector<Rect> & rects) const823 bool InputWindowsManager::IsInHotArea(int32_t x, int32_t y, const std::vector<Rect> &rects) const
824 {
825 for (const auto &item : rects) {
826 int32_t displayMaxX = 0;
827 int32_t displayMaxY = 0;
828 if (!AddInt32(item.x, item.width, displayMaxX)) {
829 MMI_HILOGE("The addition of displayMaxX overflows");
830 return false;
831 }
832 if (!AddInt32(item.y, item.height, displayMaxY)) {
833 MMI_HILOGE("The addition of displayMaxY overflows");
834 return false;
835 }
836 if (((x >= item.x) && (x < displayMaxX)) &&
837 (y >= item.y) && (y < displayMaxY)) {
838 return true;
839 }
840 }
841 return false;
842 }
843 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
844
845 #ifdef OHOS_BUILD_ENABLE_TOUCH
AdjustDisplayCoordinate(const DisplayInfo & displayInfo,int32_t & physicalX,int32_t & physicalY) const846 void InputWindowsManager::AdjustDisplayCoordinate(
847 const DisplayInfo& displayInfo, int32_t& physicalX, int32_t& physicalY) const
848 {
849 int32_t width = 0;
850 int32_t height = 0;
851 if (displayInfo.direction == DIRECTION0 || displayInfo.direction == DIRECTION180) {
852 width = displayInfo.width;
853 height = displayInfo.height;
854 } else {
855 height = displayInfo.width;
856 width = displayInfo.height;
857 }
858 if (physicalX <= 0) {
859 physicalX = 0;
860 }
861 if (physicalX >= width && width > 0) {
862 physicalX = width - 1;
863 }
864 if (physicalY <= 0) {
865 physicalY = 0;
866 }
867 if (physicalY >= height && height > 0) {
868 physicalY = height - 1;
869 }
870 }
871 #endif // OHOS_BUILD_ENABLE_TOUCH
872
873 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdateDisplayId(int32_t & displayId)874 bool InputWindowsManager::UpdateDisplayId(int32_t& displayId)
875 {
876 if (displayGroupInfo_.displaysInfo.empty()) {
877 MMI_HILOGE("logicalDisplays_is empty");
878 return false;
879 }
880 if (displayId < 0) {
881 displayId = displayGroupInfo_.displaysInfo[0].id;
882 return true;
883 }
884 for (const auto &item : displayGroupInfo_.displaysInfo) {
885 if (item.id == displayId) {
886 return true;
887 }
888 }
889 return false;
890 }
891 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
892
893 #ifdef OHOS_BUILD_ENABLE_POINTER
SelectWindowInfo(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> & pointerEvent)894 std::optional<WindowInfo> InputWindowsManager::SelectWindowInfo(int32_t logicalX, int32_t logicalY,
895 const std::shared_ptr<PointerEvent>& pointerEvent)
896 {
897 CALL_DEBUG_ENTER;
898 int32_t action = pointerEvent->GetPointerAction();
899 bool checkFlag = (firstBtnDownWindowId_ == -1) ||
900 ((action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) && (pointerEvent->GetPressedButtons().size() == 1)) ||
901 ((action == PointerEvent::POINTER_ACTION_MOVE) && (pointerEvent->GetPressedButtons().empty())) ||
902 (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE);
903 if (checkFlag) {
904 int32_t targetWindowId = pointerEvent->GetTargetWindowId();
905 for (const auto &item : displayGroupInfo_.windowsInfo) {
906 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
907 MMI_HILOGD("Skip the untouchable window to continue searching, "
908 "window:%{public}d, flags:%{public}d, pid:%{public}d", item.id, item.flags, item.pid);
909 continue;
910 } else if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
911 if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas)) {
912 firstBtnDownWindowId_ = item.id;
913 MMI_HILOGD("Mouse event select pull window, window:%{public}d, pid:%{public}d",
914 firstBtnDownWindowId_, item.pid);
915 break;
916 } else {
917 continue;
918 }
919 } else if ((targetWindowId < 0) && (IsInHotArea(logicalX, logicalY, item.pointerHotAreas))) {
920 firstBtnDownWindowId_ = item.id;
921 MMI_HILOGD("Find out the dispatch window of this pointer event when the targetWindowId "
922 "hasn't been set up yet, window:%{public}d, pid:%{public}d",
923 firstBtnDownWindowId_, item.pid);
924 break;
925 } else if ((targetWindowId >= 0) && (targetWindowId == item.id)) {
926 firstBtnDownWindowId_ = targetWindowId;
927 MMI_HILOGD("Find out the dispatch window of this pointer event when the targetWindowId "
928 "has been set up already, window:%{public}d, pid:%{public}d",
929 firstBtnDownWindowId_, item.pid);
930 break;
931 } else {
932 MMI_HILOGW("Continue searching for the dispatch window of this pointer event");
933 }
934 }
935 }
936 MMI_HILOGD("firstBtnDownWindowId_:%{public}d", firstBtnDownWindowId_);
937 for (const auto &item : displayGroupInfo_.windowsInfo) {
938 if (item.id == firstBtnDownWindowId_) {
939 return std::make_optional(item);
940 }
941 }
942 return std::nullopt;
943 }
944
GetWindowInfo(int32_t logicalX,int32_t logicalY)945 std::optional<WindowInfo> InputWindowsManager::GetWindowInfo(int32_t logicalX, int32_t logicalY)
946 {
947 CALL_DEBUG_ENTER;
948 for (const auto& item : displayGroupInfo_.windowsInfo) {
949 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
950 MMI_HILOGD("Skip the untouchable window to continue searching, "
951 "window:%{public}d, flags:%{public}d", item.id, item.flags);
952 continue;
953 } else if (IsInHotArea(logicalX, logicalY, item.pointerHotAreas)) {
954 return std::make_optional(item);
955 } else {
956 MMI_HILOGW("Continue searching for the dispatch window");
957 }
958 }
959 return std::nullopt;
960 }
961
UpdatePointerEvent(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> & pointerEvent,const WindowInfo & touchWindow)962 void InputWindowsManager::UpdatePointerEvent(int32_t logicalX, int32_t logicalY,
963 const std::shared_ptr<PointerEvent>& pointerEvent, const WindowInfo& touchWindow)
964 {
965 CHKPV(pointerEvent);
966 MMI_HILOGD("LastWindowInfo:%{public}d, touchWindow:%{public}d", lastWindowInfo_.id, touchWindow.id);
967 if (lastWindowInfo_.id != touchWindow.id) {
968 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
969 lastLogicX_ = logicalX;
970 lastLogicY_ = logicalY;
971 lastPointerEvent_ = pointerEvent;
972 lastWindowInfo_ = touchWindow;
973 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
974 return;
975 }
976 lastLogicX_ = logicalX;
977 lastLogicY_ = logicalY;
978 lastPointerEvent_ = pointerEvent;
979 lastWindowInfo_ = touchWindow;
980 }
981
SetHoverScrollState(bool state)982 int32_t InputWindowsManager::SetHoverScrollState(bool state)
983 {
984 CALL_DEBUG_ENTER;
985 MMI_HILOGD("Set mouse hover scroll state:%{public}d", state);
986 int32_t errCode = RET_OK;
987 std::shared_ptr<NativePreferences::Preferences> pref =
988 NativePreferences::PreferencesHelper::GetPreferences(mouseFileName, errCode);
989 if (pref == nullptr) {
990 MMI_HILOGE("pref is nullptr, errCode: %{public}d", errCode);
991 return RET_ERR;
992 }
993 std::string name = "isEnableHoverScroll";
994 pref->PutBool(name, state);
995 int ret = pref->FlushSync();
996 if (ret != RET_OK) {
997 MMI_HILOGE("flush sync is failed, ret:%{public}d", ret);
998 return RET_ERR;
999 }
1000 NativePreferences::PreferencesHelper::RemovePreferencesFromCache(mouseFileName);
1001 return RET_OK;
1002 }
1003
GetHoverScrollState() const1004 bool InputWindowsManager::GetHoverScrollState() const
1005 {
1006 CALL_DEBUG_ENTER;
1007 int32_t errCode = RET_OK;
1008 std::shared_ptr<NativePreferences::Preferences> pref =
1009 NativePreferences::PreferencesHelper::GetPreferences(mouseFileName, errCode);
1010 if (pref == nullptr) {
1011 MMI_HILOGE("pref is nullptr, errCode: %{public}d", errCode);
1012 return RET_ERR;
1013 }
1014 std::string name = "isEnableHoverScroll";
1015 bool state = pref->GetBool(name, true);
1016 NativePreferences::PreferencesHelper::RemovePreferencesFromCache(mouseFileName);
1017 MMI_HILOGD("Get mouse hover scroll state:%{public}d", state);
1018 return state;
1019 }
1020
UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)1021 int32_t InputWindowsManager::UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)
1022 {
1023 CALL_DEBUG_ENTER;
1024 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1025 auto displayId = pointerEvent->GetTargetDisplayId();
1026 if (!UpdateDisplayId(displayId)) {
1027 MMI_HILOGE("This display:%{public}d is not existent", displayId);
1028 return RET_ERR;
1029 }
1030 pointerEvent->SetTargetDisplayId(displayId);
1031
1032 int32_t pointerId = pointerEvent->GetPointerId();
1033 PointerEvent::PointerItem pointerItem;
1034 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
1035 MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
1036 return RET_ERR;
1037 }
1038 auto physicalDisplayInfo = GetPhysicalDisplay(displayId);
1039 CHKPR(physicalDisplayInfo, ERROR_NULL_POINTER);
1040 int32_t logicalX = 0;
1041 int32_t logicalY = 0;
1042 if (!AddInt32(pointerItem.GetDisplayX(), physicalDisplayInfo->x, logicalX)) {
1043 MMI_HILOGE("The addition of logicalX overflows");
1044 return RET_ERR;
1045 }
1046 if (!AddInt32(pointerItem.GetDisplayY(), physicalDisplayInfo->y, logicalY)) {
1047 MMI_HILOGE("The addition of logicalY overflows");
1048 return RET_ERR;
1049 }
1050 auto touchWindow = SelectWindowInfo(logicalX, logicalY, pointerEvent);
1051 if (!touchWindow) {
1052 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_BUTTON_DOWN || mouseDownInfo_.id == -1) {
1053 MMI_HILOGE("touchWindow is nullptr, targetWindow:%{public}d", pointerEvent->GetTargetWindowId());
1054 return RET_ERR;
1055 }
1056 touchWindow = std::make_optional(mouseDownInfo_);
1057 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1058 MMI_HILOGI("mouse event send cancel, window:%{public}d, pid:%{public}d", touchWindow->id, touchWindow->pid);
1059 }
1060
1061 bool checkFlag = pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_UPDATE ||
1062 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_BEGIN ||
1063 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_AXIS_END;
1064 if (checkFlag) {
1065 if ((!GetHoverScrollState()) && (displayGroupInfo_.focusWindowId != touchWindow->id)) {
1066 MMI_HILOGD("disable mouse hover scroll in inactive window, targetWindowId:%{public}d", touchWindow->id);
1067 return RET_OK;
1068 }
1069 }
1070
1071 PointerStyle pointerStyle;
1072 int32_t ret = GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
1073 if (ret != RET_OK) {
1074 MMI_HILOGE("Get pointer style failed, pointerStyleInfo is nullptr");
1075 return ret;
1076 }
1077 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1078 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
1079 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
1080 }
1081 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicalDisplayInfo);
1082 WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
1083 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1084 IPointerDrawingManager::GetInstance()->DrawPointer(displayId, pointerItem.GetDisplayX(),
1085 pointerItem.GetDisplayY(), MOUSE_ICON(pointerStyle.id));
1086 if (captureModeInfo_.isCaptureMode && (touchWindow->id != captureModeInfo_.windowId)) {
1087 captureModeInfo_.isCaptureMode = false;
1088 }
1089 pointerEvent->SetTargetWindowId(touchWindow->id);
1090 pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
1091 int32_t windowX = logicalX - touchWindow->area.x;
1092 int32_t windowY = logicalY - touchWindow->area.y;
1093 pointerItem.SetWindowX(windowX);
1094 pointerItem.SetWindowY(windowY);
1095 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
1096 #ifdef OHOS_BUILD_EMULATOR
1097 extraData_.sourceType = PointerEvent::SOURCE_TYPE_MOUSE;
1098 #endif
1099 if (extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_MOUSE) {
1100 pointerEvent->SetBuffer(extraData_.buffer);
1101 UpdatePointerAction(pointerEvent);
1102 } else {
1103 pointerEvent->ClearBuffer();
1104 }
1105 CHKPR(udsServer_, ERROR_NULL_POINTER);
1106 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
1107 UpdatePointerEvent(logicalX, logicalY, pointerEvent, *touchWindow);
1108 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
1109 int32_t action = pointerEvent->GetPointerAction();
1110 if (action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) {
1111 mouseDownInfo_ = *touchWindow;
1112 }
1113 if (action == PointerEvent::POINTER_ACTION_BUTTON_UP) {
1114 InitMouseDownInfo();
1115 MMI_HILOGD("Mouse up, clear mouse down info");
1116 }
1117 MMI_HILOGD("pid:%{public}d,id:%{public}d,agentWindowId:%{public}d,"
1118 "logicalX:%{public}d,logicalY:%{public}d,"
1119 "displayX:%{public}d,displayY:%{public}d,windowX:%{public}d,windowY:%{public}d",
1120 touchWindow->pid, touchWindow->id, touchWindow->agentWindowId,
1121 logicalX, logicalY, pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), windowX, windowY);
1122 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
1123 MMI_HILOGD("Clear extra data");
1124 ClearExtraData();
1125 }
1126 return ERR_OK;
1127 }
1128 #endif // OHOS_BUILD_ENABLE_POINTER
1129
SetMouseCaptureMode(int32_t windowId,bool isCaptureMode)1130 int32_t InputWindowsManager::SetMouseCaptureMode(int32_t windowId, bool isCaptureMode)
1131 {
1132 if (windowId < 0) {
1133 MMI_HILOGE("Windowid(%{public}d) is invalid", windowId);
1134 return RET_ERR;
1135 }
1136 if ((captureModeInfo_.isCaptureMode == isCaptureMode) && !isCaptureMode) {
1137 MMI_HILOGE("Windowid:(%{public}d) is not capture mode", windowId);
1138 return RET_OK;
1139 }
1140 captureModeInfo_.windowId = windowId;
1141 captureModeInfo_.isCaptureMode = isCaptureMode;
1142 MMI_HILOGI("Windowid:(%{public}d) is (%{public}d)", windowId, isCaptureMode);
1143 return RET_OK;
1144 }
1145
GetMouseIsCaptureMode() const1146 bool InputWindowsManager::GetMouseIsCaptureMode() const
1147 {
1148 return captureModeInfo_.isCaptureMode;
1149 }
1150
IsNeedDrawPointer(PointerEvent::PointerItem & pointerItem) const1151 bool InputWindowsManager::IsNeedDrawPointer(PointerEvent::PointerItem &pointerItem) const
1152 {
1153 if (pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN) {
1154 std::shared_ptr<InputDevice> inputDevice = InputDevMgr->GetInputDevice(pointerItem.GetDeviceId());
1155 if (inputDevice != nullptr) {
1156 MMI_HILOGD("name:%{public}s type:%{public}d bus:%{public}d, "
1157 "version:%{public}d product:%{public}d vendor:%{public}d, "
1158 "phys:%{public}s uniq:%{public}s",
1159 inputDevice->GetName().c_str(), inputDevice->GetType(), inputDevice->GetBus(),
1160 inputDevice->GetVersion(), inputDevice->GetProduct(), inputDevice->GetVendor(),
1161 inputDevice->GetPhys().c_str(), inputDevice->GetUniq().c_str());
1162 }
1163 if (inputDevice != nullptr && inputDevice->GetBus() == BUS_USB) {
1164 return true;
1165 }
1166 }
1167 return false;
1168 }
1169
1170 #ifdef OHOS_BUILD_ENABLE_TOUCH
UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)1171 int32_t InputWindowsManager::UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)
1172 {
1173 CALL_DEBUG_ENTER;
1174 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1175 auto displayId = pointerEvent->GetTargetDisplayId();
1176 if (!UpdateDisplayId(displayId)) {
1177 MMI_HILOGE("This display is not existent");
1178 return RET_ERR;
1179 }
1180 pointerEvent->SetTargetDisplayId(displayId);
1181
1182 int32_t pointerId = pointerEvent->GetPointerId();
1183 PointerEvent::PointerItem pointerItem;
1184 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
1185 MMI_HILOGE("Can't find pointer item, pointer:%{public}d", pointerId);
1186 return RET_ERR;
1187 }
1188 MMI_HILOGD("display:%{public}d", displayId);
1189 auto physicDisplayInfo = GetPhysicalDisplay(displayId);
1190 CHKPR(physicDisplayInfo, ERROR_NULL_POINTER);
1191 int32_t physicalX = pointerItem.GetDisplayX();
1192 int32_t physicalY = pointerItem.GetDisplayY();
1193 AdjustDisplayCoordinate(*physicDisplayInfo, physicalX, physicalY);
1194 int32_t logicalX = 0;
1195 int32_t logicalY = 0;
1196 if (!AddInt32(physicalX, physicDisplayInfo->x, logicalX)) {
1197 MMI_HILOGE("The addition of logicalX overflows");
1198 return RET_ERR;
1199 }
1200 if (!AddInt32(physicalY, physicDisplayInfo->y, logicalY)) {
1201 MMI_HILOGE("The addition of logicalY overflows");
1202 return RET_ERR;
1203 }
1204 WindowInfo *touchWindow = nullptr;
1205 auto targetWindowId = pointerItem.GetTargetWindowId();
1206 for (auto &item : displayGroupInfo_.windowsInfo) {
1207 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
1208 MMI_HILOGD("Skip the untouchable window to continue searching, "
1209 "window:%{public}d, flags:%{public}d", item.id, item.flags);
1210 continue;
1211 }
1212 bool checkToolType = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
1213 ((pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_FINGER && extraData_.pointerId == pointerId) ||
1214 pointerItem.GetToolType() == PointerEvent::TOOL_TYPE_PEN);
1215 if (checkToolType) {
1216 if (IsInHotArea(logicalX, logicalY, item.defaultHotAreas)) {
1217 touchWindow = &item;
1218 break;
1219 } else {
1220 continue;
1221 }
1222 }
1223 if (targetWindowId >= 0) {
1224 if (item.id == targetWindowId) {
1225 touchWindow = &item;
1226 break;
1227 }
1228 } else if (IsInHotArea(logicalX, logicalY, item.defaultHotAreas)) {
1229 touchWindow = &item;
1230 break;
1231 }
1232 }
1233 if (touchWindow == nullptr) {
1234 auto it = touchItemDownInfos_.find(pointerId);
1235 if (it == touchItemDownInfos_.end() ||
1236 pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_DOWN) {
1237 MMI_HILOGE("The touchWindow is nullptr, logicalX:%{public}d, logicalY:%{public}d",
1238 logicalX, logicalY);
1239 return RET_ERR;
1240 }
1241 touchWindow = &it->second;
1242 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
1243 MMI_HILOGD("touch event send cancel, window:%{public}d", touchWindow->id);
1244 }
1245 auto windowX = logicalX - touchWindow->area.x;
1246 auto windowY = logicalY - touchWindow->area.y;
1247 pointerEvent->SetTargetWindowId(touchWindow->id);
1248 pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
1249 pointerItem.SetDisplayX(physicalX);
1250 pointerItem.SetDisplayY(physicalY);
1251 pointerItem.SetWindowX(windowX);
1252 pointerItem.SetWindowY(windowY);
1253 pointerItem.SetToolWindowX(pointerItem.GetToolDisplayX() + physicDisplayInfo->x - touchWindow->area.x);
1254 pointerItem.SetToolWindowY(pointerItem.GetToolDisplayY() + physicDisplayInfo->y - touchWindow->area.y);
1255 pointerItem.SetTargetWindowId(touchWindow->id);
1256 pointerEvent->UpdatePointerItem(pointerId, pointerItem);
1257 bool checkExtraData = extraData_.appended && extraData_.sourceType == PointerEvent::SOURCE_TYPE_TOUCHSCREEN &&
1258 extraData_.pointerId == pointerId;
1259 if (checkExtraData) {
1260 pointerEvent->SetBuffer(extraData_.buffer);
1261 UpdatePointerAction(pointerEvent);
1262 PullEnterLeaveEvent(logicalX, logicalY, pointerEvent, touchWindow);
1263 } else {
1264 pointerEvent->ClearBuffer();
1265 lastTouchEvent_ = nullptr;
1266 lastTouchWindowInfo_.id = -1;
1267 }
1268 MMI_HILOGI("pid:%{public}d,eventId:%{public}d,", touchWindow->pid, pointerEvent->GetId());
1269 MMI_HILOGD("logicalX:%{public}d,logicalY:%{public}d,"
1270 "physicalX:%{public}d,physicalY:%{public}d,windowX:%{public}d,windowY:%{public}d,"
1271 "displayId:%{public}d,TargetWindowId:%{public}d,AgentWindowId:%{public}d",
1272 logicalX, logicalY, physicalX, physicalY, windowX, windowY, displayId,
1273 pointerEvent->GetTargetWindowId(), pointerEvent->GetAgentWindowId());
1274 if (IsNeedDrawPointer(pointerItem)) {
1275 if (!IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1276 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(true);
1277 DispatchPointer(PointerEvent::POINTER_ACTION_ENTER_WINDOW);
1278 }
1279 PointerStyle pointerStyle;
1280 int32_t ret = GetPointerStyle(touchWindow->pid, touchWindow->id, pointerStyle);
1281 if (ret != RET_OK) {
1282 MMI_HILOGE("Get pointer style failed, pointerStyleInfo is nullptr");
1283 return ret;
1284 }
1285 IPointerDrawingManager::GetInstance()->UpdateDisplayInfo(*physicDisplayInfo);
1286 WinInfo info = { .windowPid = touchWindow->pid, .windowId = touchWindow->id };
1287 IPointerDrawingManager::GetInstance()->OnWindowInfo(info);
1288 IPointerDrawingManager::GetInstance()->DrawPointer(displayId,
1289 pointerItem.GetDisplayX(), pointerItem.GetDisplayY(), MOUSE_ICON(pointerStyle.id));
1290 } else {
1291 if (IPointerDrawingManager::GetInstance()->GetMouseDisplayState()) {
1292 DispatchPointer(PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
1293 IPointerDrawingManager::GetInstance()->SetMouseDisplayState(false);
1294 }
1295 }
1296
1297 int32_t pointerAction = pointerEvent->GetPointerAction();
1298 if (pointerAction == PointerEvent::POINTER_ACTION_DOWN) {
1299 touchItemDownInfos_.insert(std::make_pair(pointerId, *touchWindow));
1300 }
1301 if (pointerAction == PointerEvent::POINTER_ACTION_UP) {
1302 auto iter = touchItemDownInfos_.find(pointerId);
1303 if (iter != touchItemDownInfos_.end()) {
1304 touchItemDownInfos_.erase(iter);
1305 MMI_HILOGD("Clear the touch info, action is up, pointerid:%{public}d", pointerId);
1306 }
1307 }
1308 if (pointerEvent->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_UP) {
1309 MMI_HILOGD("Clear extra data");
1310 ClearExtraData();
1311 }
1312 return ERR_OK;
1313 }
1314
PullEnterLeaveEvent(int32_t logicalX,int32_t logicalY,const std::shared_ptr<PointerEvent> pointerEvent,const WindowInfo * touchWindow)1315 void InputWindowsManager::PullEnterLeaveEvent(int32_t logicalX, int32_t logicalY,
1316 const std::shared_ptr<PointerEvent> pointerEvent, const WindowInfo* touchWindow)
1317 {
1318 CHKPV(pointerEvent);
1319 CHKPV(touchWindow);
1320 MMI_HILOGD("LastTouchWindowInfo:%{public}d, touchWindow:%{public}d", lastTouchWindowInfo_.id, touchWindow->id);
1321 if (lastTouchWindowInfo_.id != -1 && lastTouchWindowInfo_.id != touchWindow->id) {
1322 DispatchTouch(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
1323 lastTouchLogicX_ = logicalX;
1324 lastTouchLogicY_ = logicalY;
1325 lastTouchEvent_ = pointerEvent;
1326 lastTouchWindowInfo_ = *touchWindow;
1327 DispatchTouch(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
1328 return;
1329 }
1330 lastTouchLogicX_ = logicalX;
1331 lastTouchLogicY_ = logicalY;
1332 lastTouchEvent_ = pointerEvent;
1333 lastTouchWindowInfo_ = *touchWindow;
1334 }
1335
DispatchTouch(int32_t pointerAction)1336 void InputWindowsManager::DispatchTouch(int32_t pointerAction)
1337 {
1338 CALL_INFO_TRACE;
1339 CHKPV(udsServer_);
1340 CHKPV(lastTouchEvent_);
1341 if (pointerAction == PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) {
1342 WindowInfo *touchWindow = nullptr;
1343 for (auto item : displayGroupInfo_.windowsInfo) {
1344 if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
1345 MMI_HILOGD("Skip the untouchable window to continue searching, "
1346 "window:%{public}d, flags:%{public}d", item.id, item.flags);
1347 continue;
1348 }
1349 if (IsInHotArea(lastTouchLogicX_, lastTouchLogicY_, item.defaultHotAreas)) {
1350 touchWindow = &item;
1351 break;
1352 }
1353 }
1354 if (touchWindow == nullptr) {
1355 MMI_HILOGE("touchWindow is nullptr");
1356 return;
1357 }
1358 if (touchWindow->id != lastTouchWindowInfo_.id) {
1359 lastTouchWindowInfo_ = *touchWindow;
1360 }
1361 }
1362 auto pointerEvent = PointerEvent::Create();
1363 CHKPV(pointerEvent);
1364 PointerEvent::PointerItem lastPointerItem;
1365 int32_t lastPointerId = lastTouchEvent_->GetPointerId();
1366 if (!lastTouchEvent_->GetPointerItem(lastPointerId, lastPointerItem)) {
1367 MMI_HILOGE("GetPointerItem:%{public}d fail", lastPointerId);
1368 return;
1369 }
1370 PointerEvent::PointerItem currentPointerItem;
1371 currentPointerItem.SetWindowX(lastTouchLogicX_ - lastTouchWindowInfo_.area.x);
1372 currentPointerItem.SetWindowY(lastTouchLogicY_ - lastTouchWindowInfo_.area.y);
1373 currentPointerItem.SetDisplayX(lastPointerItem.GetDisplayX());
1374 currentPointerItem.SetDisplayY(lastPointerItem.GetDisplayY());
1375 currentPointerItem.SetPointerId(lastPointerId);
1376
1377 pointerEvent->UpdateId();
1378 pointerEvent->SetTargetDisplayId(lastTouchEvent_->GetTargetDisplayId());
1379 pointerEvent->SetTargetWindowId(lastTouchWindowInfo_.id);
1380 pointerEvent->SetAgentWindowId(lastTouchWindowInfo_.agentWindowId);
1381 pointerEvent->SetPointerId(lastPointerId);
1382 pointerEvent->AddPointerItem(currentPointerItem);
1383 pointerEvent->SetPointerAction(pointerAction);
1384 pointerEvent->SetBuffer(extraData_.buffer);
1385 pointerEvent->SetSourceType(lastTouchEvent_->GetSourceType());
1386 int64_t time = GetSysClockTime();
1387 pointerEvent->SetActionTime(time);
1388 pointerEvent->SetActionStartTime(time);
1389 pointerEvent->SetDeviceId(lastTouchEvent_->GetDeviceId());
1390 auto fd = udsServer_->GetClientFd(lastTouchWindowInfo_.pid);
1391 auto sess = udsServer_->GetSession(fd);
1392 if (sess == nullptr) {
1393 MMI_HILOGI("The last window has disappeared");
1394 return;
1395 }
1396
1397 NetPacket pkt(MmiMessageId::ON_POINTER_EVENT);
1398 InputEventDataTransformation::Marshalling(pointerEvent, pkt);
1399 if (!sess->SendMsg(pkt)) {
1400 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
1401 return;
1402 }
1403 }
1404 #endif // OHOS_BUILD_ENABLE_TOUCH
1405
1406 #ifdef OHOS_BUILD_ENABLE_POINTER
UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)1407 int32_t InputWindowsManager::UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)
1408 {
1409 CALL_DEBUG_ENTER;
1410 return RET_ERR;
1411 }
1412 #endif // OHOS_BUILD_ENABLE_POINTER
1413
1414 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)1415 int32_t InputWindowsManager::UpdateJoystickTarget(std::shared_ptr<PointerEvent> pointerEvent)
1416 {
1417 CALL_DEBUG_ENTER;
1418 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1419 int32_t focusWindowId = displayGroupInfo_.focusWindowId;
1420 const WindowInfo* windowInfo = nullptr;
1421 for (const auto &item : displayGroupInfo_.windowsInfo) {
1422 if (item.id == focusWindowId) {
1423 windowInfo = &item;
1424 break;
1425 }
1426 }
1427 CHKPR(windowInfo, ERROR_NULL_POINTER);
1428 pointerEvent->SetTargetWindowId(windowInfo->id);
1429 pointerEvent->SetAgentWindowId(windowInfo->agentWindowId);
1430 MMI_HILOGD("focusWindow:%{public}d, pid:%{public}d", focusWindowId, windowInfo->pid);
1431
1432 return RET_OK;
1433 }
1434 #endif // OHOS_BUILD_ENABLE_JOYSTICK
1435
1436 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)1437 int32_t InputWindowsManager::UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)
1438 {
1439 CALL_DEBUG_ENTER;
1440 CHKPR(pointerEvent, ERROR_NULL_POINTER);
1441 auto source = pointerEvent->GetSourceType();
1442 switch (source) {
1443 #ifdef OHOS_BUILD_ENABLE_TOUCH
1444 case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
1445 return UpdateTouchScreenTarget(pointerEvent);
1446 }
1447 #endif // OHOS_BUILD_ENABLE_TOUCH
1448 #ifdef OHOS_BUILD_ENABLE_POINTER
1449 case PointerEvent::SOURCE_TYPE_MOUSE: {
1450 return UpdateMouseTarget(pointerEvent);
1451 }
1452 case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
1453 return UpdateTouchPadTarget(pointerEvent);
1454 }
1455 #endif // OHOS_BUILD_ENABLE_POINTER
1456 #ifdef OHOS_BUILD_ENABLE_JOYSTICK
1457 case PointerEvent::SOURCE_TYPE_JOYSTICK: {
1458 return UpdateJoystickTarget(pointerEvent);
1459 }
1460 #endif // OHOS_BUILD_ENABLE_JOYSTICK
1461 default: {
1462 MMI_HILOGE("Source type is unknown, source:%{public}d", source);
1463 break;
1464 }
1465 }
1466 return RET_ERR;
1467 }
1468 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
1469
1470 #ifdef OHOS_BUILD_ENABLE_POINTER
IsInsideDisplay(const DisplayInfo & displayInfo,int32_t physicalX,int32_t physicalY)1471 bool InputWindowsManager::IsInsideDisplay(const DisplayInfo& displayInfo, int32_t physicalX, int32_t physicalY)
1472 {
1473 return (physicalX >= 0 && physicalX < displayInfo.width) && (physicalY >= 0 && physicalY < displayInfo.height);
1474 }
1475
FindPhysicalDisplay(const DisplayInfo & displayInfo,int32_t & physicalX,int32_t & physicalY,int32_t & displayId)1476 void InputWindowsManager::FindPhysicalDisplay(const DisplayInfo& displayInfo, int32_t& physicalX,
1477 int32_t& physicalY, int32_t& displayId)
1478 {
1479 CALL_DEBUG_ENTER;
1480 int32_t logicalX = 0;
1481 int32_t logicalY = 0;
1482 if (!AddInt32(physicalX, displayInfo.x, logicalX)) {
1483 MMI_HILOGE("The addition of logicalX overflows");
1484 return;
1485 }
1486 if (!AddInt32(physicalY, displayInfo.y, logicalY)) {
1487 MMI_HILOGE("The addition of logicalY overflows");
1488 return;
1489 }
1490 for (const auto &item : displayGroupInfo_.displaysInfo) {
1491 int32_t displayMaxX = 0;
1492 int32_t displayMaxY = 0;
1493 if (!AddInt32(item.x, item.width, displayMaxX)) {
1494 MMI_HILOGE("The addition of displayMaxX overflows");
1495 return;
1496 }
1497 if (!AddInt32(item.y, item.height, displayMaxY)) {
1498 MMI_HILOGE("The addition of displayMaxY overflows");
1499 return;
1500 }
1501 if ((logicalX >= item.x && logicalX < displayMaxX) &&
1502 (logicalY >= item.y && logicalY < displayMaxY)) {
1503 physicalX = logicalX - item.x;
1504 physicalY = logicalY - item.y;
1505 displayId = item.id;
1506 break;
1507 }
1508 }
1509 }
UpdateAndAdjustMouseLocation(int32_t & displayId,double & x,double & y)1510 void InputWindowsManager::UpdateAndAdjustMouseLocation(int32_t& displayId, double& x, double& y)
1511 {
1512 auto displayInfo = GetPhysicalDisplay(displayId);
1513 CHKPV(displayInfo);
1514 int32_t integerX = static_cast<int32_t>(x);
1515 int32_t integerY = static_cast<int32_t>(y);
1516 int32_t lastDisplayId = displayId;
1517 if (!IsInsideDisplay(*displayInfo, integerX, integerY)) {
1518 FindPhysicalDisplay(*displayInfo, integerX, integerY, displayId);
1519 }
1520 if (displayId != lastDisplayId) {
1521 displayInfo = GetPhysicalDisplay(displayId);
1522 CHKPV(displayInfo);
1523 }
1524 int32_t width = 0;
1525 int32_t height = 0;
1526 if (displayInfo->direction == DIRECTION0 || displayInfo->direction == DIRECTION180) {
1527 width = displayInfo->width;
1528 height = displayInfo->height;
1529 } else {
1530 height = displayInfo->width;
1531 width = displayInfo->height;
1532 }
1533 if (integerX < 0) {
1534 integerX = 0;
1535 }
1536 if (integerX >= width) {
1537 integerX = width - 1;
1538 }
1539 if (integerY < 0) {
1540 integerY = 0;
1541 }
1542 if (integerY >= height) {
1543 integerY = height - 1;
1544 }
1545 x = static_cast<double>(integerX) + (x - floor(x));
1546 y = static_cast<double>(integerY) + (y - floor(y));
1547 mouseLocation_.physicalX = integerX;
1548 mouseLocation_.physicalY = integerY;
1549 MMI_HILOGD("Mouse Data: physicalX:%{public}d,physicalY:%{public}d, displayId:%{public}d",
1550 mouseLocation_.physicalX, mouseLocation_.physicalY, displayId);
1551 }
1552
GetMouseInfo()1553 MouseLocation InputWindowsManager::GetMouseInfo()
1554 {
1555 if (mouseLocation_.physicalX == -1 || mouseLocation_.physicalY == -1) {
1556 if (!displayGroupInfo_.displaysInfo.empty()) {
1557 mouseLocation_.physicalX = displayGroupInfo_.displaysInfo[0].width / 2;
1558 mouseLocation_.physicalY = displayGroupInfo_.displaysInfo[0].height / 2;
1559 }
1560 }
1561 return mouseLocation_;
1562 }
1563 #endif // OHOS_BUILD_ENABLE_POINTER
1564
AppendExtraData(const ExtraData & extraData)1565 int32_t InputWindowsManager::AppendExtraData(const ExtraData& extraData)
1566 {
1567 CALL_DEBUG_ENTER;
1568 extraData_.appended = extraData.appended;
1569 extraData_.buffer = extraData.buffer;
1570 extraData_.sourceType = extraData.sourceType;
1571 extraData_.pointerId = extraData.pointerId;
1572 return RET_OK;
1573 }
1574
ClearExtraData()1575 void InputWindowsManager::ClearExtraData()
1576 {
1577 CALL_DEBUG_ENTER;
1578 extraData_.appended = false;
1579 extraData_.buffer.clear();
1580 extraData_.sourceType = -1;
1581 extraData_.pointerId = -1;
1582 }
1583
IsWindowVisible(int32_t pid)1584 bool InputWindowsManager::IsWindowVisible(int32_t pid)
1585 {
1586 CALL_DEBUG_ENTER;
1587 if (pid < 0) {
1588 MMI_HILOGE("pid is invalid");
1589 return true;
1590 }
1591 std::vector<sptr<Rosen::WindowVisibilityInfo>> infos;
1592 Rosen::WindowManager::GetInstance().GetVisibilityWindowInfo(infos);
1593 for (const auto &it: infos) {
1594 if (pid == it->pid_ && it->isVisible_) {
1595 MMI_HILOGD("pid:%{public}d has visible window", pid);
1596 return true;
1597 }
1598 }
1599 MMI_HILOGD("pid:%{public}d doesn't have visible window", pid);
1600 return false;
1601 }
1602
UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)1603 void InputWindowsManager::UpdatePointerAction(std::shared_ptr<PointerEvent> pointerEvent)
1604 {
1605 CALL_DEBUG_ENTER;
1606 int32_t action = pointerEvent->GetPointerAction();
1607 switch (action) {
1608 case PointerEvent::POINTER_ACTION_MOVE: {
1609 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_MOVE);
1610 break;
1611 }
1612 case PointerEvent::POINTER_ACTION_BUTTON_UP:
1613 case PointerEvent::POINTER_ACTION_UP: {
1614 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_UP);
1615 break;
1616 }
1617 case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
1618 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_IN_WINDOW);
1619 break;
1620 }
1621 case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
1622 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW);
1623 break;
1624 }
1625 default: {
1626 MMI_HILOGI("Action is:%{public}d, no need change", action);
1627 break;
1628 }
1629 }
1630 }
1631
Dump(int32_t fd,const std::vector<std::string> & args)1632 void InputWindowsManager::Dump(int32_t fd, const std::vector<std::string> &args)
1633 {
1634 CALL_DEBUG_ENTER;
1635 mprintf(fd, "Windows information:\t");
1636 mprintf(fd, "windowsInfos,num:%zu", displayGroupInfo_.windowsInfo.size());
1637 for (const auto &item : displayGroupInfo_.windowsInfo) {
1638 mprintf(fd,
1639 "\t windowsInfos: id:%d | pid:%d | uid:%d | area.x:%d | area.y:%d "
1640 "| area.width:%d | area.height:%d | defaultHotAreas.size:%zu "
1641 "| pointerHotAreas.size:%zu | agentWindowId:%d | flags:%d \t",
1642 item.id, item.pid, item.uid, item.area.x, item.area.y, item.area.width,
1643 item.area.height, item.defaultHotAreas.size(), item.pointerHotAreas.size(),
1644 item.agentWindowId, item.flags);
1645 for (const auto &win : item.defaultHotAreas) {
1646 mprintf(fd,
1647 "\t defaultHotAreas: x:%d | y:%d | width:%d | height:%d \t",
1648 win.x, win.y, win.width, win.height);
1649 }
1650 for (const auto &pointer : item.pointerHotAreas) {
1651 mprintf(fd,
1652 "\t pointerHotAreas: x:%d | y:%d | width:%d | height:%d \t",
1653 pointer.x, pointer.y, pointer.width, pointer.height);
1654 }
1655 }
1656 mprintf(fd, "Displays information:\t");
1657 mprintf(fd, "displayInfos,num:%zu", displayGroupInfo_.displaysInfo.size());
1658 for (const auto &item : displayGroupInfo_.displaysInfo) {
1659 mprintf(fd,
1660 "\t displayInfos: id:%d | x:%d | y:%d | width:%d | height:%d | name:%s "
1661 "| uniq:%s | direction:%d \t",
1662 item.id, item.x, item.y, item.width, item.height, item.name.c_str(),
1663 item.uniq.c_str(), item.direction);
1664 }
1665 mprintf(fd, "Input device and display bind info:\n%s\n", bindInfo_.Dumps().c_str());
1666 }
1667 } // namespace MMI
1668 } // namespace OHOS
1669