1 /*
2 * Copyright (C) 2025 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 "window_adapter.h"
17
18 #include <cinttypes>
19
20 #include "global.h"
21 #include "window.h"
22 #include "wm_common.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 using namespace OHOS::Rosen;
27 using WMError = OHOS::Rosen::WMError;
28 // LCOV_EXCL_START
~WindowAdapter()29 WindowAdapter::~WindowAdapter()
30 {
31 }
32
GetInstance()33 WindowAdapter &WindowAdapter::GetInstance()
34 {
35 static WindowAdapter windowAdapter;
36 return windowAdapter;
37 }
38 // LCOV_EXCL_STOP
GetFocusInfo(OHOS::Rosen::FocusChangeInfo & focusInfo,uint64_t displayId)39 void WindowAdapter::GetFocusInfo(OHOS::Rosen::FocusChangeInfo &focusInfo, uint64_t displayId)
40 {
41 #ifdef SCENE_BOARD_ENABLE
42 WindowManagerLite::GetInstance().GetFocusWindowInfo(focusInfo, displayId);
43 #else
44 WindowManager::GetInstance().GetFocusWindowInfo(focusInfo, displayId);
45 #endif
46 }
47
GetCallingWindowInfo(const uint32_t windId,const int32_t userId,CallingWindowInfo & callingWindowInfo)48 bool WindowAdapter::GetCallingWindowInfo(
49 const uint32_t windId, const int32_t userId, CallingWindowInfo &callingWindowInfo)
50 {
51 #ifdef SCENE_BOARD_ENABLE
52 IMSA_HILOGD("[%{public}d,%{public}d] run in.", userId, windId);
53 callingWindowInfo.windowId_ = static_cast<int32_t>(windId);
54 callingWindowInfo.userId_ = userId;
55 auto wmErr = WindowManagerLite::GetInstance().GetCallingWindowInfo(callingWindowInfo);
56 if (wmErr != WMError::WM_OK) {
57 IMSA_HILOGE("[%{public}d,%{public}d,%{public}d] failed to get calling window info.", userId, windId, wmErr);
58 return false;
59 }
60 IMSA_HILOGD("callingWindowInfo:%{public}s",
61 WindowDisplayChangeListener::CallingWindowInfoToString(callingWindowInfo).c_str());
62 return true;
63 #else
64 IMSA_HILOGE("capability not supported");
65 return false;
66 #endif
67 }
68 // LCOV_EXCL_START
RegisterCallingWindowInfoChangedListener(const WindowDisplayChangeHandler & handle)69 void WindowAdapter::RegisterCallingWindowInfoChangedListener(const WindowDisplayChangeHandler &handle)
70 {
71 #ifdef SCENE_BOARD_ENABLE
72 sptr<WindowDisplayChangeListener> listener = new (std::nothrow) WindowDisplayChangeListener(handle);
73 if (listener == nullptr) {
74 IMSA_HILOGE("failed to create listener");
75 return;
76 }
77 auto wmErr = WindowManagerLite::GetInstance().RegisterCallingWindowDisplayChangedListener(listener);
78 IMSA_HILOGI("register focus changed listener ret: %{public}d", wmErr);
79 #endif
80 }
81 // LCOV_EXCL_STOP
ListWindowInfo(std::vector<sptr<OHOS::Rosen::WindowInfo>> & windowInfos)82 bool WindowAdapter::ListWindowInfo(std::vector<sptr<OHOS::Rosen::WindowInfo>> &windowInfos)
83 {
84 #ifdef SCENE_BOARD_ENABLE
85 WindowInfoOption option;
86 WMError ret = WindowManagerLite::GetInstance().ListWindowInfo(option, windowInfos);
87 if (ret != WMError::WM_OK) {
88 IMSA_HILOGE("ListWindowInfo failed, ret: %{public}d", ret);
89 return false;
90 }
91 return true;
92 #else
93 IMSA_HILOGE("capability not supported");
94 return false;
95 #endif
96 }
97
GetDisplayIdByWindowId(int32_t callingWindowId)98 uint64_t WindowAdapter::GetDisplayIdByWindowId(int32_t callingWindowId)
99 {
100 #ifdef SCENE_BOARD_ENABLE
101 if (callingWindowId == DEFAULT_DISPLAY_ID) {
102 FocusChangeInfo info;
103 WindowManagerLite::GetInstance().GetFocusWindowInfo(info);
104 callingWindowId = info.windowId_;
105 }
106 std::vector<sptr<WindowInfo>> windowInfos;
107 if (!ListWindowInfo(windowInfos)) {
108 return DEFAULT_DISPLAY_ID;
109 }
110 auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingWindowId](const auto &windowInfo) {
111 if (windowInfo == nullptr) {
112 return false;
113 }
114 return windowInfo->windowMetaInfo.windowId == callingWindowId;
115 });
116 if (iter == windowInfos.end()) {
117 IMSA_HILOGE("not found window info with windowId: %{public}d", callingWindowId);
118 return DEFAULT_DISPLAY_ID;
119 }
120 auto callingDisplayId = (*iter)->windowDisplayInfo.displayId;
121 IMSA_HILOGD("window windowId: %{public}d, displayId: %{public}" PRIu64 "", callingWindowId, callingDisplayId);
122 return callingDisplayId;
123 #else
124 IMSA_HILOGI("capability not supported");
125 return DEFAULT_DISPLAY_ID;
126 #endif
127 }
128
GetDisplayIdByPid(int64_t callingPid)129 uint64_t WindowAdapter::GetDisplayIdByPid(int64_t callingPid)
130 {
131 #ifdef SCENE_BOARD_ENABLE
132 std::vector<sptr<WindowInfo>> windowInfos;
133 if (!ListWindowInfo(windowInfos)) {
134 return DEFAULT_DISPLAY_ID;
135 }
136 auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingPid](const auto &windowInfo) {
137 if (windowInfo == nullptr) {
138 return false;
139 }
140 return windowInfo->windowMetaInfo.pid == callingPid;
141 });
142 if (iter == windowInfos.end()) {
143 IMSA_HILOGE("not found window info with pid: %{public}" PRId64 "", callingPid);
144 return DEFAULT_DISPLAY_ID;
145 }
146 auto callingDisplayId = (*iter)->windowDisplayInfo.displayId;
147 IMSA_HILOGD("window pid: %{public}" PRId64 ", displayId: %{public}" PRIu64 "", callingPid, callingDisplayId);
148 return callingDisplayId;
149 #else
150 IMSA_HILOGI("capability not supported");
151 return DEFAULT_DISPLAY_ID;
152 #endif
153 }
154
GetDisplayId(int64_t callingPid,uint64_t & displayId)155 bool WindowAdapter::GetDisplayId(int64_t callingPid, uint64_t &displayId)
156 {
157 displayId = DEFAULT_DISPLAY_ID;
158 #ifdef SCENE_BOARD_ENABLE
159 std::vector<sptr<WindowInfo>> windowInfos;
160 if (!ListWindowInfo(windowInfos)) {
161 return false;
162 }
163 auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingPid](const auto &windowInfo) {
164 if (windowInfo == nullptr) {
165 return false;
166 }
167 return windowInfo->windowMetaInfo.pid == callingPid;
168 });
169 if (iter == windowInfos.end()) {
170 IMSA_HILOGE("not found window info with pid: %{public}" PRId64 "", callingPid);
171 return false;
172 }
173 auto callingDisplayId = (*iter)->windowDisplayInfo.displayId;
174 IMSA_HILOGD("window pid: %{public}" PRId64 ", displayId: %{public}" PRIu64 "", callingPid, callingDisplayId);
175 displayId = callingDisplayId;
176 return true;
177 #else
178 IMSA_HILOGI("capability not supported");
179 return true;
180 #endif
181 }
182 } // namespace MiscServices
183 } // namespace OHOS
184