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