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 "oh_window.h"
17
18 #include <cstdint>
19 #include <functional>
20 #include <mutex>
21
22 #include "image/pixelmap_native.h"
23 #include "pixelmap_native_impl.h"
24 #include "ui_content.h"
25
26 #include <event_handler.h>
27 #include <event_runner.h>
28
29 #include "oh_window_comm.h"
30 #include "singleton_container.h"
31 #include "window.h"
32 #include "window_manager.h"
33 #include "window_manager_hilog.h"
34
35 using namespace OHOS::Rosen;
36
37 namespace OHOS {
38 namespace Rosen {
39 namespace {
40 constexpr uint32_t NORMAL_STATE_CHANGE = 0;
41 constexpr bool SHOW_WITH_NO_ANIMATION = false;
42 constexpr bool SHOW_WITH_FOCUS = true;
43 std::shared_ptr<OHOS::AppExecFwk::EventHandler> g_eventHandler;
44 std::once_flag g_onceFlagForInitEventHandler;
45
IsMainWindow(WindowType type)46 inline bool IsMainWindow(WindowType type)
47 {
48 return (type >= WindowType::APP_MAIN_WINDOW_BASE && type < WindowType::APP_MAIN_WINDOW_END);
49 }
50
IsMainWindowAndNotShown(WindowType type,WindowState state)51 inline bool IsMainWindowAndNotShown(WindowType type, WindowState state)
52 {
53 return (IsMainWindow(type) && state != WindowState::STATE_SHOWN);
54 }
55 }
56
GetMainEventHandler()57 std::shared_ptr<OHOS::AppExecFwk::EventHandler> GetMainEventHandler()
58 {
59 std::call_once(g_onceFlagForInitEventHandler, [] {
60 g_eventHandler =
61 std::make_shared<OHOS::AppExecFwk::EventHandler>(OHOS::AppExecFwk::EventRunner::GetMainEventRunner());
62 });
63 return g_eventHandler;
64 }
65
ShowWindowInner(int32_t windowId)66 WindowManager_ErrorCode ShowWindowInner(int32_t windowId)
67 {
68 auto eventHandler = GetMainEventHandler();
69 WindowManager_ErrorCode ret = WindowManager_ErrorCode::OK;
70 eventHandler->PostSyncTask([windowId, &ret] {
71 auto window = Window::GetWindowWithId(windowId);
72 if (window == nullptr) {
73 TLOGNE(WmsLogTag::WMS_LIFE, "window is null, windowId:%{public}d", windowId);
74 ret = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
75 return;
76 }
77 if (IsMainWindowAndNotShown(window->GetType(), window->GetWindowState())) {
78 TLOGNW(WmsLogTag::WMS_LIFE,
79 "window Type %{public}u and window state %{public}u is not supported, [%{public}u, %{public}s]",
80 static_cast<uint32_t>(window->GetType()), static_cast<uint32_t>(window->GetWindowState()),
81 window->GetWindowId(), window->GetWindowName().c_str());
82 ret = WindowManager_ErrorCode::OK;
83 return;
84 }
85 if (window->Show(NORMAL_STATE_CHANGE, SHOW_WITH_NO_ANIMATION, SHOW_WITH_FOCUS) == WMError::WM_OK) {
86 ret = WindowManager_ErrorCode::OK;
87 } else {
88 ret = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
89 }
90 TLOGNI(WmsLogTag::WMS_LIFE, "Window [%{public}u, %{public}s] show with ret=%{public}d",
91 window->GetWindowId(), window->GetWindowName().c_str(), ret);
92 }, __func__);
93 return ret;
94 }
95
IsWindowShownInner(int32_t windowId,bool * isShow)96 WindowManager_ErrorCode IsWindowShownInner(int32_t windowId, bool* isShow)
97 {
98 if (isShow == nullptr) {
99 TLOGE(WmsLogTag::WMS_LIFE, "isShow is null");
100 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
101 }
102 auto eventHandler = GetMainEventHandler();
103 WindowManager_ErrorCode ret = WindowManager_ErrorCode::OK;
104 eventHandler->PostSyncTask([windowId, isShow, &ret] {
105 auto window = Window::GetWindowWithId(windowId);
106 if (window == nullptr) {
107 TLOGNE(WmsLogTag::WMS_LIFE, "window is null, windowId:%{public}d", windowId);
108 ret = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
109 return;
110 }
111 *isShow = window->GetWindowState() == WindowState::STATE_SHOWN;
112 }, __func__);
113 return ret;
114 }
115 } // namespace Rosen
116 } // namespace OHOS
117
OH_WindowManager_ShowWindow(int32_t windowId)118 int32_t OH_WindowManager_ShowWindow(int32_t windowId)
119 {
120 return static_cast<int32_t>(OHOS::Rosen::ShowWindowInner(windowId));
121 }
122
OH_WindowManager_IsWindowShown(int32_t windowId,bool * isShow)123 int32_t OH_WindowManager_IsWindowShown(int32_t windowId, bool* isShow)
124 {
125 return static_cast<int32_t>(OHOS::Rosen::IsWindowShownInner(windowId, isShow));
126 }
127
128 namespace {
129 #define WINDOW_MANAGER_FREE_MEMORY(ptr) \
130 do { \
131 if ((ptr)) { \
132 free((ptr)); \
133 (ptr) = NULL; \
134 } \
135 } while (0)
136
137 /*
138 * Used to map from WMError to WindowManager_ErrorCode.
139 */
140 const std::unordered_map<WMError, WindowManager_ErrorCode> OH_WINDOW_TO_ERROR_CODE_MAP {
141 { WMError::WM_OK, WindowManager_ErrorCode::OK },
142 { WMError::WM_ERROR_INVALID_PARAM, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM },
143 { WMError::WM_ERROR_DEVICE_NOT_SUPPORT, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_DEVICE_NOT_SUPPORTED },
144 { WMError::WM_ERROR_INVALID_WINDOW, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL },
145 { WMError::WM_ERROR_INVALID_CALLING, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL },
146 { WMError::WM_ERROR_NULLPTR, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL },
147 { WMError::WM_ERROR_SYSTEM_ABNORMALLY, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL },
148 { WMError::WM_ERROR_INVALID_PERMISSION, WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_NO_PERMISSION },
149 };
150
151 /*
152 * Used to map from WindowType to WindowManager_WindowType.
153 */
154 const std::unordered_map<WindowType, WindowManager_WindowType> OH_WINDOW_TO_WINDOW_TYPE_MAP {
155 { WindowType::WINDOW_TYPE_APP_SUB_WINDOW, WindowManager_WindowType::WINDOW_MANAGER_WINDOW_TYPE_APP },
156 { WindowType::WINDOW_TYPE_DIALOG, WindowManager_WindowType::WINDOW_MANAGER_WINDOW_TYPE_DIALOG },
157 { WindowType::WINDOW_TYPE_APP_MAIN_WINDOW, WindowManager_WindowType::WINDOW_MANAGER_WINDOW_TYPE_MAIN },
158 { WindowType::WINDOW_TYPE_FLOAT, WindowManager_WindowType::WINDOW_MANAGER_WINDOW_TYPE_FLOAT },
159 };
160
TransformedToWindowManagerRect(const Rect & rect,WindowManager_Rect & wmRect)161 void TransformedToWindowManagerRect(const Rect& rect, WindowManager_Rect& wmRect)
162 {
163 wmRect.posX = rect.posX_;
164 wmRect.posY = rect.posY_;
165 wmRect.width = rect.width_;
166 wmRect.height = rect.height_;
167 }
168
TransformedToWindowManagerAvoidArea(const AvoidArea & allAvoidArea,WindowManager_AvoidArea * avoidArea)169 void TransformedToWindowManagerAvoidArea(const AvoidArea& allAvoidArea, WindowManager_AvoidArea* avoidArea)
170 {
171 if (avoidArea == nullptr) {
172 TLOGE(WmsLogTag::WMS_IMMS, "avoidArea is nullptr");
173 return;
174 }
175 TransformedToWindowManagerRect(allAvoidArea.topRect_, avoidArea->topRect);
176 TransformedToWindowManagerRect(allAvoidArea.leftRect_, avoidArea->leftRect);
177 TransformedToWindowManagerRect(allAvoidArea.rightRect_, avoidArea->rightRect);
178 TransformedToWindowManagerRect(allAvoidArea.bottomRect_, avoidArea->bottomRect);
179 }
180 } // namespace
181
OH_WindowManager_GetWindowAvoidArea(int32_t windowId,WindowManager_AvoidAreaType type,WindowManager_AvoidArea * avoidArea)182 int32_t OH_WindowManager_GetWindowAvoidArea(
183 int32_t windowId, WindowManager_AvoidAreaType type, WindowManager_AvoidArea* avoidArea)
184 {
185 if (avoidArea == nullptr) {
186 TLOGE(WmsLogTag::WMS_IMMS, "avoidArea is null, windowId:%{public}d", windowId);
187 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
188 }
189 auto eventHandler = GetMainEventHandler();
190 if (eventHandler == nullptr) {
191 TLOGE(WmsLogTag::WMS_IMMS, "eventHandler null, windowId:%{public}d", windowId);
192 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
193 }
194 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
195 eventHandler->PostSyncTask([windowId, type, avoidArea, &errCode, where = __func__] {
196 auto window = Window::GetWindowWithId(windowId);
197 if (window == nullptr) {
198 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is null, windowId:%{public}d", where, windowId);
199 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
200 return;
201 }
202 AvoidArea allAvoidArea;
203 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(
204 window->GetAvoidAreaByType(static_cast<AvoidAreaType>(type), allAvoidArea));
205 TransformedToWindowManagerAvoidArea(allAvoidArea, avoidArea);
206 }, __func__);
207 return errCode;
208 }
209
OH_WindowManager_SetWindowStatusBarEnabled(int32_t windowId,bool enabled,bool enableAnimation)210 int32_t OH_WindowManager_SetWindowStatusBarEnabled(int32_t windowId, bool enabled, bool enableAnimation)
211 {
212 auto eventHandler = GetMainEventHandler();
213 if (eventHandler == nullptr) {
214 TLOGE(WmsLogTag::WMS_IMMS, "eventHandler null, windowId:%{public}d", windowId);
215 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
216 }
217 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
218 eventHandler->PostSyncTask([windowId, enabled, enableAnimation, &errCode, where = __func__] {
219 auto window = Window::GetWindowWithId(windowId);
220 if (window == nullptr) {
221 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is null, windowId:%{public}d", where, windowId);
222 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
223 return;
224 }
225 if (window->IsPcWindow()) {
226 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s device is not support, windowId:%{public}d", where, windowId);
227 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_DEVICE_NOT_SUPPORTED;
228 return;
229 }
230 auto property = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
231 property.enable_ = enabled;
232 property.settingFlag_ = static_cast<SystemBarSettingFlag>(
233 static_cast<uint32_t>(property.settingFlag_) | static_cast<uint32_t>(SystemBarSettingFlag::ENABLE_SETTING));
234 property.enableAnimation_ = enableAnimation;
235 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(
236 window->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, property));
237 }, __func__);
238 return errCode;
239 }
240
OH_WindowManager_SetWindowStatusBarColor(int32_t windowId,int32_t color)241 int32_t OH_WindowManager_SetWindowStatusBarColor(int32_t windowId, int32_t color)
242 {
243 auto eventHandler = GetMainEventHandler();
244 if (eventHandler == nullptr) {
245 TLOGE(WmsLogTag::WMS_IMMS, "eventHandler null, windowId:%{public}d", windowId);
246 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
247 }
248 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
249 eventHandler->PostSyncTask([windowId, color, &errCode, where = __func__] {
250 auto window = Window::GetWindowWithId(windowId);
251 if (window == nullptr) {
252 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is null, windowId:%{public}d", where, windowId);
253 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
254 return;
255 }
256 if (window->IsPcWindow()) {
257 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s device is not support, windowId:%{public}d", where, windowId);
258 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_DEVICE_NOT_SUPPORTED;
259 return;
260 }
261 auto property = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
262 property.contentColor_ = color;
263 property.settingFlag_ = static_cast<SystemBarSettingFlag>(
264 static_cast<uint32_t>(property.settingFlag_) | static_cast<uint32_t>(SystemBarSettingFlag::COLOR_SETTING));
265 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(
266 window->SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, property));
267 }, __func__);
268 return errCode;
269 }
270
OH_WindowManager_SetWindowNavigationBarEnabled(int32_t windowId,bool enabled,bool enableAnimation)271 int32_t OH_WindowManager_SetWindowNavigationBarEnabled(int32_t windowId, bool enabled, bool enableAnimation)
272 {
273 auto eventHandler = GetMainEventHandler();
274 if (eventHandler == nullptr) {
275 TLOGE(WmsLogTag::WMS_IMMS, "eventHandler null, windowId:%{public}d", windowId);
276 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
277 }
278 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
279 eventHandler->PostSyncTask([windowId, enabled, enableAnimation, &errCode, where = __func__] {
280 auto window = Window::GetWindowWithId(windowId);
281 if (window == nullptr) {
282 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is null, windowId:%{public}d", where, windowId);
283 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
284 return;
285 }
286 if (window->IsPcWindow()) {
287 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s device is not support, windowId:%{public}d", where, windowId);
288 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_DEVICE_NOT_SUPPORTED;
289 return;
290 }
291 auto property = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
292 property.enable_ = enabled;
293 property.settingFlag_ = static_cast<SystemBarSettingFlag>(
294 static_cast<uint32_t>(property.settingFlag_) | static_cast<uint32_t>(SystemBarSettingFlag::ENABLE_SETTING));
295 property.enableAnimation_ = enableAnimation;
296 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(
297 window->SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR, property));
298 }, __func__);
299 return errCode;
300 }
301
OH_WindowManager_Snapshot(int32_t windowId,OH_PixelmapNative * pixelMap)302 int32_t OH_WindowManager_Snapshot(int32_t windowId, OH_PixelmapNative* pixelMap)
303 {
304 if (pixelMap == nullptr) {
305 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "pixelMap is null, windowId:%{public}d", windowId);
306 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
307 }
308 auto eventHandler = GetMainEventHandler();
309 if (eventHandler == nullptr) {
310 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
311 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
312 }
313 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
314 eventHandler->PostSyncTask([windowId, pixelMap, &errCode, where = __func__]() mutable {
315 auto window = Window::GetWindowWithId(windowId);
316 if (window == nullptr) {
317 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
318 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
319 return;
320 }
321 *pixelMap = OH_PixelmapNative(window->Snapshot());
322 }, __func__);
323 return pixelMap != nullptr ? WindowManager_ErrorCode::OK : errCode;
324 }
325
OH_WindowManager_SetWindowBackgroundColor(int32_t windowId,const char * color)326 int32_t OH_WindowManager_SetWindowBackgroundColor(int32_t windowId, const char* color)
327 {
328 if (color == nullptr) {
329 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "color is null, windowId:%{public}d", windowId);
330 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
331 }
332 auto eventHandler = GetMainEventHandler();
333 if (eventHandler == nullptr) {
334 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
335 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
336 }
337 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
338 eventHandler->PostSyncTask([windowId, color, &errCode, where = __func__] {
339 auto window = Window::GetWindowWithId(windowId);
340 if (window == nullptr) {
341 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
342 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
343 return;
344 }
345 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetBackgroundColor(std::string(color)));
346 }, __func__);
347 return errCode;
348 }
349
OH_WindowManager_SetWindowBrightness(int32_t windowId,float brightness)350 int32_t OH_WindowManager_SetWindowBrightness(int32_t windowId, float brightness)
351 {
352 auto eventHandler = GetMainEventHandler();
353 if (eventHandler == nullptr) {
354 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
355 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
356 }
357 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
358 eventHandler->PostSyncTask([windowId, brightness, &errCode, where = __func__] {
359 auto window = Window::GetWindowWithId(windowId);
360 if (window == nullptr) {
361 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
362 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
363 return;
364 }
365 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetBrightness(brightness));
366 }, __func__);
367 return errCode;
368 }
369
OH_WindowManager_SetWindowKeepScreenOn(int32_t windowId,bool isKeepScreenOn)370 int32_t OH_WindowManager_SetWindowKeepScreenOn(int32_t windowId, bool isKeepScreenOn)
371 {
372 auto eventHandler = GetMainEventHandler();
373 if (eventHandler == nullptr) {
374 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
375 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
376 }
377 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
378 eventHandler->PostSyncTask([windowId, isKeepScreenOn, &errCode, where = __func__] {
379 auto window = Window::GetWindowWithId(windowId);
380 if (window == nullptr) {
381 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
382 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
383 return;
384 }
385 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetKeepScreenOn(isKeepScreenOn));
386 }, __func__);
387 return errCode;
388 }
389
OH_WindowManager_SetWindowPrivacyMode(int32_t windowId,bool isPrivacy)390 int32_t OH_WindowManager_SetWindowPrivacyMode(int32_t windowId, bool isPrivacy)
391 {
392 auto eventHandler = GetMainEventHandler();
393 if (eventHandler == nullptr) {
394 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
395 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
396 }
397 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
398 eventHandler->PostSyncTask([windowId, isPrivacy, &errCode, where = __func__] {
399 auto window = Window::GetWindowWithId(windowId);
400 if (window == nullptr) {
401 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
402 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
403 return;
404 }
405 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetPrivacyMode(isPrivacy));
406 }, __func__);
407 return errCode;
408 }
409
OH_WindowManager_GetWindowProperties(int32_t windowId,WindowManager_WindowProperties * windowProperties)410 int32_t OH_WindowManager_GetWindowProperties(
411 int32_t windowId, WindowManager_WindowProperties* windowProperties)
412 {
413 if (windowProperties == nullptr) {
414 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "windowProperties is null, windowId:%{public}d", windowId);
415 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
416 }
417 auto eventHandler = GetMainEventHandler();
418 if (eventHandler == nullptr) {
419 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler null, windowId:%{public}d", windowId);
420 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
421 }
422 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::OK;
423 eventHandler->PostSyncTask([windowId, windowProperties, &errCode, where = __func__] {
424 auto window = Window::GetWindowWithId(windowId);
425 if (window == nullptr) {
426 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s window is null, windowId:%{public}d", where, windowId);
427 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
428 return;
429 }
430 if (OH_WINDOW_TO_WINDOW_TYPE_MAP.count(window->GetType()) != 0) {
431 windowProperties->type = OH_WINDOW_TO_WINDOW_TYPE_MAP.at(window->GetType());
432 } else {
433 windowProperties->type = static_cast<WindowManager_WindowType>(window->GetType());
434 }
435 TransformedToWindowManagerRect(window->GetRect(), windowProperties->windowRect);
436 windowProperties->isLayoutFullScreen = window->IsLayoutFullScreen();
437 windowProperties->isFullScreen = window->IsFullScreen();
438 windowProperties->touchable = window->GetTouchable();
439 windowProperties->focusable = window->GetFocusable();
440 windowProperties->isPrivacyMode = window->IsPrivacyMode();
441 windowProperties->isKeepScreenOn = window->IsKeepScreenOn();
442 windowProperties->brightness = window->GetBrightness();
443 windowProperties->isTransparent = window->IsTransparent();
444 windowProperties->id = window->GetWindowId();
445 windowProperties->displayId = window->GetDisplayId();
446 Rect drawableRect = { 0, 0, 0, 0 };
447 auto uicontent = window->GetUIContent();
448 if (uicontent == nullptr) {
449 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s uicontent is null, windowId:%{public}d", where, windowId);
450 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
451 return;
452 }
453 uicontent->GetAppPaintSize(drawableRect);
454 TransformedToWindowManagerRect(drawableRect, windowProperties->drawableRect);
455 }, __func__);
456 return errCode;
457 }
458
OH_WindowManager_SetWindowTouchable(int32_t windowId,bool touchable)459 int32_t OH_WindowManager_SetWindowTouchable(int32_t windowId, bool touchable)
460 {
461 auto eventHandler = GetMainEventHandler();
462 if (eventHandler == nullptr) {
463 TLOGE(WmsLogTag::WMS_EVENT, "eventHandler null, windowId:%{public}d", windowId);
464 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
465 }
466 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
467 eventHandler->PostSyncTask([windowId, touchable, &errCode, where = __func__] {
468 auto window = Window::GetWindowWithId(windowId);
469 if (window == nullptr) {
470 TLOGNE(WmsLogTag::WMS_EVENT, "%{public}s window is null, windowId:%{public}d", where, windowId);
471 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
472 return;
473 }
474 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetTouchable(touchable));
475 }, __func__);
476 return errCode;
477 }
478
OH_WindowManager_GetAllWindowLayoutInfoList(int64_t displayId,WindowManager_Rect ** windowLayoutInfoList,size_t * windowLayoutInfoSize)479 int32_t OH_WindowManager_GetAllWindowLayoutInfoList(
480 int64_t displayId, WindowManager_Rect** windowLayoutInfoList, size_t* windowLayoutInfoSize)
481 {
482 if (displayId < 0) {
483 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "displayId is invalid, displayId:%{public}" PRIu64, displayId);
484 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
485 }
486 if (windowLayoutInfoList == nullptr || windowLayoutInfoSize == nullptr) {
487 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "param is nullptr, displayId:%{public}" PRIu64, displayId);
488 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_INVALID_PARAM;
489 }
490 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::OK;
491 auto eventHandler = GetMainEventHandler();
492 if (eventHandler == nullptr) {
493 TLOGE(WmsLogTag::WMS_ATTRIBUTE, "eventHandler is null, displayId:%{public}" PRIu64, displayId);
494 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
495 }
496 eventHandler->PostSyncTask([displayId, windowLayoutInfoList, windowLayoutInfoSize, &errCode, where = __func__] {
497 std::vector<OHOS::sptr<WindowLayoutInfo>> infos;
498 auto ret =
499 SingletonContainer::Get<WindowManager>().GetAllWindowLayoutInfo(static_cast<uint64_t>(displayId), infos);
500 if (OH_WINDOW_TO_ERROR_CODE_MAP.find(ret) == OH_WINDOW_TO_ERROR_CODE_MAP.end()) {
501 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
502 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s get failed, errCode: %{public}d", where, errCode);
503 return;
504 } else if (OH_WINDOW_TO_ERROR_CODE_MAP.at(ret) != WindowManager_ErrorCode::OK) {
505 errCode = (ret == WMError::WM_ERROR_DEVICE_NOT_SUPPORT) ? OH_WINDOW_TO_ERROR_CODE_MAP.at(ret) :
506 WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
507 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s get failed, errCode: %{public}d", where, errCode);
508 return;
509 }
510 WindowManager_Rect* infosInner = (WindowManager_Rect*)malloc(sizeof(WindowManager_Rect) * infos.size());
511 if (infosInner == nullptr) {
512 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
513 TLOGNE(WmsLogTag::WMS_ATTRIBUTE, "%{public}s infosInner is nullptr", where);
514 return;
515 }
516 for (size_t i = 0; i < infos.size(); i++) {
517 TransformedToWindowManagerRect(infos[i]->rect, infosInner[i]);
518 TLOGND(WmsLogTag::WMS_ATTRIBUTE, "%{public}s rect: %{public}d %{public}d %{public}d %{public}d",
519 where, infosInner[i].posX, infosInner[i].posY, infosInner[i].width, infosInner[i].height);
520 }
521 *windowLayoutInfoList = infosInner;
522 *windowLayoutInfoSize = infos.size();
523 }, __func__);
524 return errCode;
525 }
526
OH_WindowManager_ReleaseAllWindowLayoutInfoList(WindowManager_Rect * windowLayoutInfoList)527 void OH_WindowManager_ReleaseAllWindowLayoutInfoList(WindowManager_Rect* windowLayoutInfoList)
528 {
529 WINDOW_MANAGER_FREE_MEMORY(windowLayoutInfoList);
530 }
531
OH_WindowManager_SetWindowFocusable(int32_t windowId,bool isFocusable)532 int32_t OH_WindowManager_SetWindowFocusable(int32_t windowId, bool isFocusable)
533 {
534 auto eventHandler = GetMainEventHandler();
535 if (eventHandler == nullptr) {
536 TLOGE(WmsLogTag::WMS_FOCUS, "eventHandler is null, windowId:%{public}d", windowId);
537 return WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
538 }
539 WindowManager_ErrorCode errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_SYSTEM_ABNORMAL;
540 eventHandler->PostSyncTask([windowId, isFocusable, &errCode, where = __func__] {
541 auto window = Window::GetWindowWithId(windowId);
542 if (window == nullptr) {
543 TLOGNE(WmsLogTag::WMS_FOCUS, "%{public}s window is null, windowId:%{public}d", where, windowId);
544 errCode = WindowManager_ErrorCode::WINDOW_MANAGER_ERRORCODE_STATE_ABNORMAL;
545 return;
546 }
547 errCode = OH_WINDOW_TO_ERROR_CODE_MAP.at(window->SetFocusable(isFocusable));
548 }, __func__);
549 return errCode;
550 }