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