• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "input_windows_manager.h"
16 #include <cstdio>
17 #include <cstdlib>
18 #include "event_dump.h"
19 #include "util.h"
20 #include "util_ex.h"
21 #include "i_pointer_drawing_manager.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "InputWindowsManager"};
27 constexpr uint8_t TOP_LEFT_X  = 0;
28 constexpr uint8_t TOP_LEFT_Y  = 1;
29 constexpr uint8_t TOP_RIGHT_X = 2;
30 constexpr uint8_t TOP_RIGHT_Y = 3;
31 constexpr uint8_t CORNER = 4;
32 }
33 } // namespace MMI
34 } // namespace OHOS
35 
36 using namespace OHOS::MMI;
37 
InputWindowsManager()38 OHOS::MMI::InputWindowsManager::InputWindowsManager() {}
39 
~InputWindowsManager()40 OHOS::MMI::InputWindowsManager::~InputWindowsManager() {}
41 /*
42  * FullName:  Init
43  * Returns:   bool
44  * Qualifier: init windows manager server
45  */
Init(UDSServer & udsServer)46 bool OHOS::MMI::InputWindowsManager::Init(UDSServer& udsServer)
47 {
48     // save server handle
49     udsServer_ = &udsServer;
50     return true;
51 }
52 
53 /*********************************新框架接口添加****************************/
UpdateTarget(std::shared_ptr<InputEvent> inputEvent)54 int32_t OHOS::MMI::InputWindowsManager::UpdateTarget(std::shared_ptr<InputEvent> inputEvent)
55 {
56     CHKPR(inputEvent, ERROR_NULL_POINTER);
57     MMI_LOGD("enter");
58     int32_t pid = GetPidAndUpdateTarget(inputEvent);
59     CHKR(pid > 0, PID_OBTAIN_FAIL, RET_ERR);
60     int32_t fd = udsServer_->GetClientFd(pid);
61     CHKR(fd >= 0, FD_OBTAIN_FAIL, RET_ERR);
62     MMI_LOGD("leave");
63     return fd;
64 }
65 
GetDisplayId(std::shared_ptr<InputEvent> inputEvent)66 int32_t OHOS::MMI::InputWindowsManager::GetDisplayId(std::shared_ptr<InputEvent> inputEvent)
67 {
68     int32_t displayId = inputEvent->GetTargetDisplayId();
69     if (displayId < 0) {
70         MMI_LOGD("target display is -1");
71         if (logicalDisplays_.empty()) {
72             return displayId;
73         }
74         displayId = logicalDisplays_[0].id;
75         inputEvent->SetTargetDisplayId(displayId);
76     }
77     return displayId;
78 }
79 
GetPidAndUpdateTarget(std::shared_ptr<InputEvent> inputEvent)80 int32_t OHOS::MMI::InputWindowsManager::GetPidAndUpdateTarget(std::shared_ptr<InputEvent> inputEvent)
81 {
82     MMI_LOGD("enter");
83     CHKPR(inputEvent, ERROR_NULL_POINTER);
84     const int32_t targetDisplayId = GetDisplayId(inputEvent);
85     if (targetDisplayId < 0) {
86         MMI_LOGE("No display is available.");
87         return RET_ERR;
88     }
89     for (const auto &item : logicalDisplays_) {
90         if (item.id != targetDisplayId) {
91             continue;
92         }
93         MMI_LOGD("target display:%{public}d", targetDisplayId);
94         auto it = windowInfos_.find(item.focusWindowId);
95         if (it == windowInfos_.end()) {
96             MMI_LOGE("can't find window info, focuswindowId:%{public}d", item.focusWindowId);
97             return RET_ERR;
98         }
99         inputEvent->SetTargetWindowId(item.focusWindowId);
100         inputEvent->SetAgentWindowId(it->second.agentWindowId);
101         MMI_LOGD("pid:%{public}d", it->second.pid);
102         return it->second.pid;
103     }
104 
105     MMI_LOGE("leave,can't find logical display,target display:%{public}d", targetDisplayId);
106     return RET_ERR;
107 }
108 
UpdateDisplayInfo(const std::vector<PhysicalDisplayInfo> & physicalDisplays,const std::vector<LogicalDisplayInfo> & logicalDisplays)109 void OHOS::MMI::InputWindowsManager::UpdateDisplayInfo(const std::vector<PhysicalDisplayInfo> &physicalDisplays,
110     const std::vector<LogicalDisplayInfo> &logicalDisplays)
111 {
112     MMI_LOGD("enter");
113     physicalDisplays_.clear();
114     logicalDisplays_.clear();
115     windowInfos_.clear();
116 
117     physicalDisplays_ = physicalDisplays;
118     logicalDisplays_ = logicalDisplays;
119     size_t numLogicalDisplay = logicalDisplays.size();
120     for (size_t i = 0; i < numLogicalDisplay; ++i) {
121         size_t numWindow = logicalDisplays[i].windowsInfo_.size();
122         for (size_t j = 0; j < numWindow; j++) {
123             WindowInfo myWindow = logicalDisplays[i].windowsInfo_[j];
124             windowInfos_.insert(std::pair<int32_t, WindowInfo>(myWindow.id, myWindow));
125         }
126     }
127     if (!logicalDisplays.empty()) {
128         IPointerDrawingManager::GetInstance()->TellDisplayInfo(logicalDisplays[0].id,
129             logicalDisplays[0].width, logicalDisplays_[0].height);
130     }
131     PrintDisplayDebugInfo();
132     MMI_LOGD("leave");
133 }
134 
PrintDisplayDebugInfo()135 void OHOS::MMI::InputWindowsManager::PrintDisplayDebugInfo()
136 {
137     MMI_LOGD("physicalDisplays,num:%{public}zu", physicalDisplays_.size());
138     for (const auto &item : physicalDisplays_) {
139         MMI_LOGD("PhysicalDisplays,id:%{public}d,leftDisplay:%{public}d,upDisplay:%{public}d,"
140             "topLeftX:%{public}d,topLeftY:%{public}d,width:%{public}d,height:%{public}d,name:%{public}s,"
141             "seatId:%{public}s,seatName:%{public}s,logicWidth:%{public}d,logicHeight:%{public}d,"
142             "direction:%{public}d",
143             item.id, item.leftDisplayId, item.upDisplayId,
144             item.topLeftX, item.topLeftY, item.width,
145             item.height, item.name.c_str(), item.seatId.c_str(),
146             item.seatName.c_str(), item.logicWidth, item.logicHeight, item.direction);
147     }
148 
149     MMI_LOGD("logicalDisplays,num:%{public}zu", logicalDisplays_.size());
150     for (const auto &item : logicalDisplays_) {
151         MMI_LOGD("logicalDisplays, id:%{public}d,topLeftX:%{public}d,topLeftY:%{public}d,"
152             "width:%{public}d,height:%{public}d,name:%{public}s,"
153             "seatId:%{public}s,seatName:%{public}s,focusWindowId:%{public}d,window num:%{public}zu",
154             item.id, item.topLeftX, item.topLeftY,
155             item.width, item.height, item.name.c_str(),
156             item.seatId.c_str(), item.seatName.c_str(), item.focusWindowId,
157             item.windowsInfo_.size());
158     }
159 
160     MMI_LOGD("window info,num:%{public}zu", windowInfos_.size());
161     for (const auto &item : windowInfos_) {
162         MMI_LOGD("windowId:%{public}d,id:%{public}d,pid:%{public}d,uid:%{public}d,hotZoneTopLeftX:%{public}d,"
163             "hotZoneTopLeftY:%{public}d,hotZoneWidth:%{public}d,hotZoneHeight:%{public}d,display:%{public}d,"
164             "agentWindowId:%{public}d,winTopLeftX:%{public}d,winTopLeftY:%{public}d,flags:%{public}d",
165             item.first, item.second.id, item.second.pid, item.second.uid, item.second.hotZoneTopLeftX,
166             item.second.hotZoneTopLeftY, item.second.hotZoneWidth, item.second.hotZoneHeight,
167             item.second.displayId, item.second.agentWindowId, item.second.winTopLeftX, item.second.winTopLeftY,
168             item.second.flags);
169     }
170 }
171 
TouchPadPointToDisplayPoint_2(struct libinput_event_touch * touch,int32_t & logicalX,int32_t & logicalY,int32_t & logicalDisplayId)172 bool OHOS::MMI::InputWindowsManager::TouchPadPointToDisplayPoint_2(struct libinput_event_touch* touch,
173     int32_t& logicalX, int32_t& logicalY, int32_t& logicalDisplayId)
174 {
175     CHKPF(touch);
176     if (screensInfo_ != nullptr) {
177         if ((*screensInfo_) != nullptr)
178         logicalDisplayId = (*screensInfo_)->screenId;
179         logicalX = static_cast<int32_t>(libinput_event_touch_get_x_transformed(touch, (*screensInfo_)->width));
180         logicalY = static_cast<int32_t>(libinput_event_touch_get_y_transformed(touch, (*screensInfo_)->height));
181         return true;
182     }
183     MMI_LOGE("ScreensInfo_ is null");
184     return false;
185 }
186 
GetPhysicalDisplay(int32_t id)187 OHOS::MMI::PhysicalDisplayInfo* OHOS::MMI::InputWindowsManager::GetPhysicalDisplay(int32_t id)
188 {
189     for (auto &it : physicalDisplays_) {
190         if (it.id == id) {
191             return &it;
192         }
193     }
194     MMI_LOGE("Failed to obtain physical(%{public}d) display", id);
195     return nullptr;
196 }
197 
FindPhysicalDisplayInfo(const std::string seatId,const std::string seatName)198 OHOS::MMI::PhysicalDisplayInfo* OHOS::MMI::InputWindowsManager::FindPhysicalDisplayInfo(const std::string seatId,
199     const std::string seatName)
200 {
201     for (auto &it : physicalDisplays_) {
202         if (it.seatId == seatId && it.seatName == seatName) {
203             return &it;
204         }
205     }
206     MMI_LOGE("Failed to search for Physical,seat:%{public}s,name:%{public}s", seatId.c_str(), seatName.c_str());
207     return nullptr;
208 }
209 
TurnTouchScreen(PhysicalDisplayInfo * info,Direction direction,int32_t & logicalX,int32_t & logicalY)210 void OHOS::MMI::InputWindowsManager::TurnTouchScreen(PhysicalDisplayInfo* info, Direction direction,
211     int32_t& logicalX, int32_t& logicalY)
212 {
213     CHKPV(info);
214     if (direction == Direction0) {
215         MMI_LOGD("direction is Direction0");
216         return;
217     }
218     if (direction == Direction90) {
219         MMI_LOGD("direction is Direction90");
220         int32_t temp = logicalX;
221         logicalX = info->logicHeight - logicalY;
222         logicalY = temp;
223         MMI_LOGD("logicalX is %{public}d, logicalY is %{public}d", logicalX, logicalY);
224         return;
225     }
226     if (direction == Direction180) {
227         MMI_LOGD("direction is Direction180");
228         logicalX = info->logicWidth - logicalX;
229         logicalY = info->logicHeight - logicalY;
230         return;
231     }
232     if (direction == Direction270) {
233         MMI_LOGD("direction is Direction270");
234         int32_t temp = logicalY;
235         logicalY = info->logicWidth - logicalX;
236         logicalX = temp;
237     }
238 }
239 
TransformOfDisplayPoint(struct libinput_event_touch * touch,Direction & direction,int32_t & globalLogicalX,int32_t & globalLogicalY)240 bool OHOS::MMI::InputWindowsManager::TransformOfDisplayPoint(struct libinput_event_touch* touch, Direction& direction,
241     int32_t &globalLogicalX, int32_t &globalLogicalY)
242 {
243     CHKPF(touch);
244     auto info = FindPhysicalDisplayInfo("seat0", "default0");
245     CHKPF(info);
246 
247     if ((info->width <= 0) || (info->height <= 0) || (info->logicWidth <= 0) || (info->logicHeight <= 0)) {
248         MMI_LOGE("Get DisplayInfo is error");
249         return false;
250     }
251 
252     auto physicalX = libinput_event_touch_get_x_transformed(touch, info->width) + info->topLeftX;
253     auto physicalY = libinput_event_touch_get_y_transformed(touch, info->height) + info->topLeftY;
254     if ((physicalX >= INT32_MAX) || (physicalY >= INT32_MAX)) {
255         MMI_LOGE("Physical display coordinates are out of range");
256         return false;
257     }
258     int32_t localPhysicalX = static_cast<int32_t>(physicalX);
259     int32_t localPhysicalY = static_cast<int32_t>(physicalY);
260 
261     auto logicX = (1L * info->logicWidth * localPhysicalX / info->width);
262     auto logicY = (1L * info->logicHeight * localPhysicalY / info->height);
263     if ((logicX >= INT32_MAX) || (logicY >= INT32_MAX)) {
264         MMI_LOGE("Physical display logical coordinates out of range");
265         return false;
266     }
267     int32_t localLogcialX = static_cast<int32_t>(logicX);
268     int32_t localLogcialY = static_cast<int32_t>(logicY);
269 
270     direction = info->direction;
271     TurnTouchScreen(info, direction, localLogcialX, localLogcialY);
272 
273     globalLogicalX = localLogcialX;
274     globalLogicalY = localLogcialY;
275 
276     for (auto left = GetPhysicalDisplay(info->leftDisplayId); left != nullptr;
277         left = GetPhysicalDisplay(left->leftDisplayId)) {
278         if (direction == Direction0 || direction == Direction180) {
279             globalLogicalX += left->logicWidth;
280         }
281         if (direction == Direction90 || direction == Direction270) {
282             globalLogicalX += left->logicHeight;
283         }
284     }
285 
286     for (auto upper = GetPhysicalDisplay(info->upDisplayId); upper != nullptr;
287         upper = GetPhysicalDisplay(upper->upDisplayId)) {
288         if (direction == Direction0 || direction == Direction180) {
289             globalLogicalY += upper->logicHeight;
290         }
291         if (direction == Direction90 || direction == Direction270) {
292             globalLogicalY += upper->logicWidth;
293         }
294     }
295 
296     return true;
297 }
298 
TouchMotionPointToDisplayPoint(struct libinput_event_touch * touch,Direction & direction,int32_t targetDisplayId,int32_t & displayX,int32_t & displayY)299 bool OHOS::MMI::InputWindowsManager::TouchMotionPointToDisplayPoint(struct libinput_event_touch* touch, Direction& direction,
300     int32_t targetDisplayId, int32_t& displayX, int32_t& displayY)
301 {
302     CHKPF(touch);
303     int32_t globalLogicalX;
304     int32_t globalLogicalY;
305     auto isTransform = TransformOfDisplayPoint(touch, direction, globalLogicalX, globalLogicalY);
306     if (!isTransform) {
307         return isTransform;
308     }
309 
310     for (const auto &display : logicalDisplays_) {
311         if (targetDisplayId == display.id ) {
312             MMI_LOGD("targetDisplay is %{public}d, displayX is %{public}d, displayY is %{public}d ",
313                 targetDisplayId, displayX, displayY);
314             displayX = globalLogicalX - display.topLeftX;
315             displayY = globalLogicalY - display.topLeftY;
316         }
317         return true;
318     }
319 
320     return false;
321 }
322 
TouchDownPointToDisplayPoint(struct libinput_event_touch * touch,Direction & direction,int32_t & logicalX,int32_t & logicalY,int32_t & logicalDisplayId)323 bool OHOS::MMI::InputWindowsManager::TouchDownPointToDisplayPoint(struct libinput_event_touch* touch, Direction& direction,
324     int32_t& logicalX, int32_t& logicalY, int32_t& logicalDisplayId)
325 {
326     CHKPF(touch);
327     int32_t globalLogicalX;
328     int32_t globalLogicalY;
329     auto isTransform = TransformOfDisplayPoint(touch, direction, globalLogicalX, globalLogicalY);
330     if (!isTransform) {
331         return isTransform;
332     }
333 
334     for (const auto &display : logicalDisplays_) {
335         if (globalLogicalX < display.topLeftX || globalLogicalX > display.topLeftX + display.width) {
336             continue;
337         }
338 
339         if (globalLogicalY < display.topLeftY || globalLogicalY > display.topLeftY + display.height) {
340             continue;
341         }
342 
343         logicalDisplayId = display.id;
344         logicalX = globalLogicalX - display.topLeftX;
345         logicalY = globalLogicalY - display.topLeftY;
346         MMI_LOGD("targetDisplay is %{public}d, displayX is %{public}d, displayY is %{public}d ",
347             logicalDisplayId, logicalX, logicalY);
348         return true;
349     }
350 
351     return false;
352 }
353 
GetLogicalDisplayInfo() const354 const std::vector<LogicalDisplayInfo>& OHOS::MMI::InputWindowsManager::GetLogicalDisplayInfo() const
355 {
356     return logicalDisplays_;
357 }
358 
GetWindowInfo() const359 const std::map<int32_t, WindowInfo>& OHOS::MMI::InputWindowsManager::GetWindowInfo() const
360 {
361     return windowInfos_;
362 }
363 
IsInsideWindow(int32_t x,int32_t y,const WindowInfo & info) const364 bool OHOS::MMI::InputWindowsManager::IsInsideWindow(int32_t x, int32_t y, const WindowInfo &info) const
365 {
366     return (x >= info.hotZoneTopLeftX) && (x <= (info.hotZoneTopLeftX + info.hotZoneWidth)) &&
367         (y >= info.hotZoneTopLeftY) && (y <= (info.hotZoneTopLeftY + info.hotZoneHeight));
368 }
369 
AdjustGlobalCoordinate(int32_t & globalX,int32_t & globalY,int32_t width,int32_t height)370 void OHOS::MMI::InputWindowsManager::AdjustGlobalCoordinate(int32_t& globalX, int32_t& globalY,
371     int32_t width, int32_t height)
372 {
373     if (globalX <= 0) {
374         globalX = 0;
375     }
376     if (globalX >= width) {
377         globalX = width;
378     }
379     if (globalY <= 0) {
380         globalY = 0;
381     }
382     if (globalY >= height) {
383         globalY = height;
384     }
385 }
386 
UpdataDisplayId(int32_t & displayId)387 bool OHOS::MMI::InputWindowsManager::UpdataDisplayId(int32_t& displayId)
388 {
389     if (logicalDisplays_.empty()) {
390         MMI_LOGE("logicalDisplays_is empty");
391         return false;
392     }
393     if (displayId < 0) {
394         displayId = logicalDisplays_[0].id;
395         return true;
396     }
397     for (const auto &item : logicalDisplays_) {
398         if (item.id == displayId) {
399             return true;
400         }
401     }
402     return false;
403 }
404 
GetLogicalDisplayId(int32_t displayId)405 LogicalDisplayInfo* OHOS::MMI::InputWindowsManager::GetLogicalDisplayId(int32_t displayId)
406 {
407     for (auto &it : logicalDisplays_) {
408         if (it.id == displayId) {
409             return &it;
410         }
411     }
412     return nullptr;
413 }
414 
AdjustCoordinate(double & coordinateX,double & coordinateY)415 void OHOS::MMI::InputWindowsManager::AdjustCoordinate(double &coordinateX, double &coordinateY)
416 {
417     if (coordinateX < 0) {
418         coordinateX = 0;
419     }
420 
421     if (coordinateY < 0) {
422         coordinateY = 0;
423     }
424 
425     if (logicalDisplays_.empty()) {
426         return;
427     }
428 
429     if (coordinateX > logicalDisplays_[0].width) {
430         coordinateX = logicalDisplays_[0].width;
431     }
432     if (coordinateY > logicalDisplays_[0].height) {
433         coordinateY = logicalDisplays_[0].height;
434     }
435 }
436 
UpdateMouseTargetOld(std::shared_ptr<PointerEvent> pointerEvent)437 int32_t OHOS::MMI::InputWindowsManager::UpdateMouseTargetOld(std::shared_ptr<PointerEvent> pointerEvent)
438 {
439     return RET_ERR;
440 }
441 
SelectWindowInfo(const int32_t & globalX,const int32_t & globalY,const std::shared_ptr<PointerEvent> & pointerEvent,LogicalDisplayInfo * const logicalDisplayInfo,WindowInfo * & touchWindow)442 void InputWindowsManager::SelectWindowInfo(const int32_t& globalX, const int32_t& globalY,
443     const std::shared_ptr<PointerEvent>& pointerEvent, LogicalDisplayInfo* const logicalDisplayInfo,
444     WindowInfo*& touchWindow)
445 {
446     int32_t action = pointerEvent->GetPointerAction();
447     if ((firstBtnDownWindowId_ == -1) ||
448         ((action == PointerEvent::POINTER_ACTION_BUTTON_DOWN) && (pointerEvent->GetPressedButtons().size() == 1)) ||
449         ((action == PointerEvent::POINTER_ACTION_MOVE) && (pointerEvent->GetPressedButtons().empty()))) {
450         int32_t targetWindowId = pointerEvent->GetTargetWindowId();
451         for (const auto& item : logicalDisplayInfo->windowsInfo_) {
452             if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
453                 MMI_LOGD("Skip the untouchable window to continue searching, "
454                     "window:%{public}d, flags:%{public}d", item.id, item.flags);
455                 continue;
456             } else if ((targetWindowId < 0) && (IsInsideWindow(globalX, globalY, item))) {
457                 firstBtnDownWindowId_ = item.id;
458                 MMI_LOGD("find out the dispatch window of this pointerevent when the targetWindowId "
459                     "hasn't been setted up yet, window:%{public}d", firstBtnDownWindowId_);
460                 break;
461             } else if ((targetWindowId >= 0) && (targetWindowId == item.id)) {
462                 firstBtnDownWindowId_ = targetWindowId;
463                 MMI_LOGD("find out the dispatch window of this pointerevent when the targetWindowId "
464                     "has been setted up already, window:%{public}d", firstBtnDownWindowId_);
465                 break;
466             } else {
467                 MMI_LOGD("Continue searching for the dispatch window of this pointerevent");
468             }
469         }
470     }
471     for (auto &item : logicalDisplayInfo->windowsInfo_) {
472         if (item.id == firstBtnDownWindowId_) {
473             touchWindow = const_cast<WindowInfo*>(&item);
474             break;
475         }
476     }
477 }
478 
UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)479 int32_t OHOS::MMI::InputWindowsManager::UpdateMouseTarget(std::shared_ptr<PointerEvent> pointerEvent)
480 {
481     MMI_LOGD("Enter");
482     CHKPR(pointerEvent, ERROR_NULL_POINTER);
483     auto displayId = pointerEvent->GetTargetDisplayId();
484     if (!UpdataDisplayId(displayId)) {
485         MMI_LOGE("This display:%{public}d is not exist", displayId);
486         return RET_ERR;
487     }
488     pointerEvent->SetTargetDisplayId(displayId);
489 
490     int32_t pointerId = pointerEvent->GetPointerId();
491     PointerEvent::PointerItem pointerItem;
492     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
493         MMI_LOGE("Can't find pointer item, pointer:%{public}d", pointerId);
494         return RET_ERR;
495     }
496     LogicalDisplayInfo *logicalDisplayInfo = GetLogicalDisplayId(displayId);
497     CHKPR(logicalDisplayInfo, ERROR_NULL_POINTER);
498     int32_t globalX = pointerItem.GetGlobalX();
499     int32_t globalY = pointerItem.GetGlobalY();
500     IPointerDrawingManager::GetInstance()->DrawPointer(displayId, globalX, globalY);
501     WindowInfo* touchWindow = nullptr;
502     SelectWindowInfo(globalX, globalY, pointerEvent, logicalDisplayInfo, touchWindow);
503     if (touchWindow == nullptr) {
504         MMI_LOGE("touchWindow is nullptr, targetWindow:%{public}d", pointerEvent->GetTargetWindowId());
505         return RET_ERR;
506     }
507     pointerEvent->SetTargetWindowId(touchWindow->id);
508     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
509     int32_t localX = globalX - touchWindow->winTopLeftX;
510     int32_t localY = globalY - touchWindow->winTopLeftY;
511     pointerItem.SetLocalX(localX);
512     pointerItem.SetLocalY(localY);
513     pointerEvent->UpdatePointerItem(pointerId, pointerItem);
514     CHKPR(udsServer_, ERROR_NULL_POINTER);
515     auto fd = udsServer_->GetClientFd(touchWindow->pid);
516 
517     MMI_LOGD("fd:%{public}d,pid:%{public}d,id:%{public}d,agentWindowId:%{public}d,"
518              "globalX:%{public}d,globalY:%{public}d,localX:%{public}d,localY:%{public}d",
519              fd, touchWindow->pid, touchWindow->id, touchWindow->agentWindowId,
520              globalX, globalY, localX, localY);
521     return fd;
522 }
523 
UpdateTouchScreenTargetOld(std::shared_ptr<PointerEvent> pointerEvent)524 int32_t OHOS::MMI::InputWindowsManager::UpdateTouchScreenTargetOld(std::shared_ptr<PointerEvent> pointerEvent)
525 {
526     return RET_ERR;
527 }
528 
UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)529 int32_t OHOS::MMI::InputWindowsManager::UpdateTouchScreenTarget(std::shared_ptr<PointerEvent> pointerEvent)
530 {
531     CHKPR(pointerEvent, ERROR_NULL_POINTER);
532     auto displayId = pointerEvent->GetTargetDisplayId();
533     if (!UpdataDisplayId(displayId)) {
534         MMI_LOGE("This display is not exist");
535         return RET_ERR;
536     }
537     pointerEvent->SetTargetDisplayId(displayId);
538 
539     int32_t pointerId = pointerEvent->GetPointerId();
540     PointerEvent::PointerItem pointerItem;
541     if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
542         MMI_LOGE("Can't find pointer item, pointer:%{public}d", pointerId);
543         return RET_ERR;
544     }
545     MMI_LOGD("UpdateTouchScreenTarget, display:%{public}d", displayId);
546     LogicalDisplayInfo *logicalDisplayInfo = GetLogicalDisplayId(displayId);
547     CHKPR(logicalDisplayInfo, ERROR_NULL_POINTER);
548     int32_t globalX = pointerItem.GetGlobalX();
549     int32_t globalY = pointerItem.GetGlobalY();
550     MMI_LOGD("UpdateTouchScreenTarget, globalX:%{public}d,globalY:%{public}d", globalX, globalY);
551     AdjustGlobalCoordinate(globalX, globalY, logicalDisplayInfo->width, logicalDisplayInfo->height);
552     auto targetWindowId = pointerEvent->GetTargetWindowId();
553     MMI_LOGD("UpdateTouchScreenTarget, targetWindow:%{public}d", targetWindowId);
554     WindowInfo *touchWindow = nullptr;
555     for (auto item : logicalDisplayInfo->windowsInfo_) {
556         if ((item.flags & WindowInfo::FLAG_BIT_UNTOUCHABLE) == WindowInfo::FLAG_BIT_UNTOUCHABLE) {
557             MMI_LOGD("Skip the untouchable window to continue searching, "
558                 "window:%{public}d, flags:%{public}d", item.id, item.flags);
559             continue;
560         }
561         if (targetWindowId < 0) {
562             if (IsInsideWindow(globalX, globalY, item)) {
563                 touchWindow = &item;
564                 break;
565             }
566         } else if (targetWindowId >= 0) {
567             if (targetWindowId == item.id) {
568                 touchWindow = &item;
569                 break;
570             }
571         }
572     }
573     if (touchWindow == nullptr) {
574         MMI_LOGE("touchWindow is nullptr");
575         return RET_ERR;
576     }
577 
578     pointerEvent->SetTargetWindowId(touchWindow->id);
579     pointerEvent->SetAgentWindowId(touchWindow->agentWindowId);
580     int32_t localX = globalX - touchWindow->winTopLeftX;
581     int32_t localY = globalY - touchWindow->winTopLeftY;
582     pointerItem.SetLocalX(localX);
583     pointerItem.SetLocalY(localY);
584     pointerEvent->RemovePointerItem(pointerId);
585     pointerEvent->AddPointerItem(pointerItem);
586     auto fd = udsServer_->GetClientFd(touchWindow->pid);
587     MMI_LOGD("pid:%{public}d,fd:%{public}d,globalX01:%{public}d,"
588              "globalY01:%{public}d,localX:%{public}d,localY:%{public}d,"
589              "TargetWindowId:%{public}d,AgentWindowId:%{public}d",
590              touchWindow->pid, fd, globalX, globalY, localX, localY,
591              pointerEvent->GetTargetWindowId(), pointerEvent->GetAgentWindowId());
592     return fd;
593 }
594 
UpdateTouchPadTargetOld(std::shared_ptr<PointerEvent> pointerEvent)595 int32_t OHOS::MMI::InputWindowsManager::UpdateTouchPadTargetOld(std::shared_ptr<PointerEvent> pointerEvent)
596 {
597     MMI_LOGD("Enter");
598     return RET_ERR;
599 }
600 
UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)601 int32_t OHOS::MMI::InputWindowsManager::UpdateTouchPadTarget(std::shared_ptr<PointerEvent> pointerEvent)
602 {
603     MMI_LOGD("Enter");
604     return RET_ERR;
605 }
606 
UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)607 int32_t OHOS::MMI::InputWindowsManager::UpdateTargetPointer(std::shared_ptr<PointerEvent> pointerEvent)
608 {
609     MMI_LOGD("enter");
610     CHKPR(pointerEvent, ERROR_NULL_POINTER);
611     auto source = pointerEvent->GetSourceType();
612     switch (source) {
613         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
614             return UpdateTouchScreenTarget(pointerEvent);
615         }
616         case PointerEvent::SOURCE_TYPE_MOUSE: {
617             return UpdateMouseTarget(pointerEvent);
618         }
619         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
620             return UpdateTouchPadTarget(pointerEvent);
621         }
622         default: {
623             MMI_LOGW("Source type is unknown, source:%{public}d", source);
624             break;
625         }
626     }
627     MMI_LOGE("Source is not of the correct type, source:%{public}d", source);
628     MMI_LOGD("leave");
629     return RET_ERR;
630 }
631 
UpdateAndAdjustMouseLoction(double & x,double & y)632 void OHOS::MMI::InputWindowsManager::UpdateAndAdjustMouseLoction(double& x, double& y)
633 {
634     int32_t integerX = static_cast<int32_t>(x);
635     int32_t integerY = static_cast<int32_t>(y);
636     const std::vector<LogicalDisplayInfo> logicalDisplayInfo = GetLogicalDisplayInfo();
637     if (logicalDisplayInfo.empty()) {
638         MMI_LOGE("logicalDisplayInfo is empty");
639         return;
640     }
641     for (const auto &item : logicalDisplayInfo) {
642         bool isOutside[CORNER] = { false, false, false, false };
643         if (item.id >= 0) {
644             if (integerX < item.topLeftX) {
645                 mouseLoction_.globalX = item.topLeftX;
646                 x = item.topLeftX;
647                 isOutside[TOP_LEFT_X] = true;
648             } else {
649                 isOutside[TOP_LEFT_X] = false;
650             }
651             if (integerX > (item.topLeftX + item.width)) {
652                 mouseLoction_.globalX = item.topLeftX + item.width;
653                 x = item.topLeftX + item.width;
654                 isOutside[TOP_RIGHT_X] = true;
655             } else {
656                 isOutside[TOP_RIGHT_X] = false;
657             }
658             if (integerY < item.topLeftY) {
659                 mouseLoction_.globalY = item.topLeftY;
660                 y = item.topLeftY;
661                 isOutside[TOP_LEFT_Y] = true;
662             } else {
663                 isOutside[TOP_LEFT_Y] = false;
664             }
665             if (integerY > (item.topLeftY + item.height)) {
666                 mouseLoction_.globalY = item.topLeftY + item.height;
667                 y = item.topLeftY + item.height;
668                 isOutside[TOP_RIGHT_Y] = true;
669             } else {
670                 isOutside[TOP_RIGHT_Y] = false;
671             }
672             if ((isOutside[TOP_LEFT_X] != true) && (isOutside[TOP_LEFT_Y] != true) &&
673                 (isOutside[TOP_RIGHT_X] != true) && (isOutside[TOP_RIGHT_Y] != true)) {
674                 mouseLoction_.globalX = x;
675                 mouseLoction_.globalY = y;
676                 break;
677             }
678         }
679     }
680     MMI_LOGD("Mouse Data: globalX:%{public}d,globalY:%{public}d", mouseLoction_.globalX, mouseLoction_.globalY);
681 }
682 
GetMouseInfo()683 MouseLocation OHOS::MMI::InputWindowsManager::GetMouseInfo()
684 {
685     return mouseLoction_;
686 }