1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "window_adapter.h"
17 #include <iservice_registry.h>
18 #include <system_ability_definition.h>
19 #include "window_manager.h"
20 #include "window_manager_hilog.h"
21 #include "wm_common.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAdapter"};
27 }
WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter)28 WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter)
29
30 #define INIT_PROXY_CHECK_RETURN(ret) \
31 do { \
32 if (!InitWMSProxy()) { \
33 WLOGFE("InitWMSProxy failed!"); \
34 return ret; \
35 } \
36 } while (false)
37
38 WMError WindowAdapter::CreateWindow(sptr<IWindow>& window, sptr<WindowProperty>& windowProperty,
39 std::shared_ptr<RSSurfaceNode> surfaceNode, uint32_t& windowId, const sptr<IRemoteObject>& token)
40 {
41 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
42
43 return windowManagerServiceProxy_->CreateWindow(window, windowProperty, surfaceNode, windowId, token);
44 }
45
AddWindow(sptr<WindowProperty> & windowProperty)46 WMError WindowAdapter::AddWindow(sptr<WindowProperty>& windowProperty)
47 {
48 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
49
50 return windowManagerServiceProxy_->AddWindow(windowProperty);
51 }
52
RemoveWindow(uint32_t windowId)53 WMError WindowAdapter::RemoveWindow(uint32_t windowId)
54 {
55 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
56
57 return windowManagerServiceProxy_->RemoveWindow(windowId);
58 }
59
DestroyWindow(uint32_t windowId)60 WMError WindowAdapter::DestroyWindow(uint32_t windowId)
61 {
62 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
63
64 return windowManagerServiceProxy_->DestroyWindow(windowId);
65 }
66
RequestFocus(uint32_t windowId)67 WMError WindowAdapter::RequestFocus(uint32_t windowId)
68 {
69 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
70
71 return windowManagerServiceProxy_->RequestFocus(windowId);
72 }
73
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)74 bool WindowAdapter::RegisterWindowManagerAgent(WindowManagerAgentType type,
75 const sptr<IWindowManagerAgent>& windowManagerAgent)
76 {
77 INIT_PROXY_CHECK_RETURN(false);
78
79 return windowManagerServiceProxy_->RegisterWindowManagerAgent(type, windowManagerAgent);
80 }
81
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)82 bool WindowAdapter::UnregisterWindowManagerAgent(WindowManagerAgentType type,
83 const sptr<IWindowManagerAgent>& windowManagerAgent)
84 {
85 INIT_PROXY_CHECK_RETURN(false);
86
87 return windowManagerServiceProxy_->UnregisterWindowManagerAgent(type, windowManagerAgent);
88 }
89
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos)90 WMError WindowAdapter::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos)
91 {
92 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
93
94 return windowManagerServiceProxy_->GetAccessibilityWindowInfo(infos);
95 }
96
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos)97 WMError WindowAdapter::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos)
98 {
99 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
100
101 return windowManagerServiceProxy_->GetVisibilityWindowInfo(infos);
102 }
103
SetWindowAnimationController(const sptr<RSIWindowAnimationController> & controller)104 WMError WindowAdapter::SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller)
105 {
106 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
107 return windowManagerServiceProxy_->SetWindowAnimationController(controller);
108 }
109
GetAvoidAreaByType(uint32_t windowId,AvoidAreaType type,AvoidArea & avoidArea)110 WMError WindowAdapter::GetAvoidAreaByType(uint32_t windowId, AvoidAreaType type, AvoidArea& avoidArea)
111 {
112 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
113 avoidArea = windowManagerServiceProxy_->GetAvoidAreaByType(windowId, type);
114 return WMError::WM_OK;
115 }
116
NotifyServerReadyToMoveOrDrag(uint32_t windowId,sptr<WindowProperty> & windowProperty,sptr<MoveDragProperty> & moveDragProperty)117 void WindowAdapter::NotifyServerReadyToMoveOrDrag(uint32_t windowId, sptr<WindowProperty>& windowProperty,
118 sptr<MoveDragProperty>& moveDragProperty)
119 {
120 INIT_PROXY_CHECK_RETURN();
121
122 return windowManagerServiceProxy_->NotifyServerReadyToMoveOrDrag(windowId, windowProperty, moveDragProperty);
123 }
124
ProcessPointDown(uint32_t windowId,bool isPointDown)125 void WindowAdapter::ProcessPointDown(uint32_t windowId, bool isPointDown)
126 {
127 INIT_PROXY_CHECK_RETURN();
128
129 return windowManagerServiceProxy_->ProcessPointDown(windowId, isPointDown);
130 }
131
ProcessPointUp(uint32_t windowId)132 void WindowAdapter::ProcessPointUp(uint32_t windowId)
133 {
134 INIT_PROXY_CHECK_RETURN();
135
136 return windowManagerServiceProxy_->ProcessPointUp(windowId);
137 }
138
MinimizeAllAppWindows(DisplayId displayId)139 void WindowAdapter::MinimizeAllAppWindows(DisplayId displayId)
140 {
141 INIT_PROXY_CHECK_RETURN();
142
143 windowManagerServiceProxy_->MinimizeAllAppWindows(displayId);
144 }
145
ToggleShownStateForAllAppWindows()146 WMError WindowAdapter::ToggleShownStateForAllAppWindows()
147 {
148 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
149
150 return windowManagerServiceProxy_->ToggleShownStateForAllAppWindows();
151 }
152
GetSystemConfig(SystemConfig & systemConfig)153 WMError WindowAdapter::GetSystemConfig(SystemConfig& systemConfig)
154 {
155 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
156
157 return windowManagerServiceProxy_->GetSystemConfig(systemConfig);
158 }
159
GetModeChangeHotZones(DisplayId displayId,ModeChangeHotZones & hotZones)160 WMError WindowAdapter::GetModeChangeHotZones(DisplayId displayId, ModeChangeHotZones& hotZones)
161 {
162 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
163
164 return windowManagerServiceProxy_->GetModeChangeHotZones(displayId, hotZones);
165 }
166
InitWMSProxy()167 bool WindowAdapter::InitWMSProxy()
168 {
169 std::lock_guard<std::recursive_mutex> lock(mutex_);
170 if (!isProxyValid_) {
171 sptr<ISystemAbilityManager> systemAbilityManager =
172 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
173 if (!systemAbilityManager) {
174 WLOGFE("Failed to get system ability mgr.");
175 return false;
176 }
177
178 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
179 if (!remoteObject) {
180 WLOGFE("Failed to get window manager service.");
181 return false;
182 }
183
184 windowManagerServiceProxy_ = iface_cast<IWindowManager>(remoteObject);
185 if ((!windowManagerServiceProxy_) || (!windowManagerServiceProxy_->AsObject())) {
186 WLOGFE("Failed to get system window manager services");
187 return false;
188 }
189
190 wmsDeath_ = new WMSDeathRecipient();
191 if (!wmsDeath_) {
192 WLOGFE("Failed to create death Recipient ptr WMSDeathRecipient");
193 return false;
194 }
195 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) {
196 WLOGFE("Failed to add death recipient");
197 return false;
198 }
199 isProxyValid_ = true;
200 }
201 return true;
202 }
203
ClearWindowAdapter()204 void WindowAdapter::ClearWindowAdapter()
205 {
206 if ((windowManagerServiceProxy_ != nullptr) && (windowManagerServiceProxy_->AsObject() != nullptr)) {
207 windowManagerServiceProxy_->AsObject()->RemoveDeathRecipient(wmsDeath_);
208 }
209 std::lock_guard<std::recursive_mutex> lock(mutex_);
210 isProxyValid_ = false;
211 }
212
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)213 void WMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
214 {
215 if (wptrDeath == nullptr) {
216 WLOGFE("wptrDeath is null");
217 return;
218 }
219
220 sptr<IRemoteObject> object = wptrDeath.promote();
221 if (!object) {
222 WLOGFE("object is null");
223 return;
224 }
225 WLOGFI("wms OnRemoteDied");
226 SingletonContainer::Get<WindowAdapter>().ClearWindowAdapter();
227 SingletonContainer::Get<WindowManager>().OnRemoteDied();
228 }
229
GetTopWindowId(uint32_t mainWinId,uint32_t & topWinId)230 WMError WindowAdapter::GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId)
231 {
232 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
233
234 return windowManagerServiceProxy_->GetTopWindowId(mainWinId, topWinId);
235 }
236
SetWindowLayoutMode(WindowLayoutMode mode)237 WMError WindowAdapter::SetWindowLayoutMode(WindowLayoutMode mode)
238 {
239 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
240
241 return windowManagerServiceProxy_->SetWindowLayoutMode(mode);
242 }
243
UpdateProperty(sptr<WindowProperty> & windowProperty,PropertyChangeAction action)244 WMError WindowAdapter::UpdateProperty(sptr<WindowProperty>& windowProperty, PropertyChangeAction action)
245 {
246 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
247 return windowManagerServiceProxy_->UpdateProperty(windowProperty, action);
248 }
249
NotifyWindowTransition(sptr<WindowTransitionInfo> from,sptr<WindowTransitionInfo> to)250 WMError WindowAdapter::NotifyWindowTransition(sptr<WindowTransitionInfo> from, sptr<WindowTransitionInfo> to)
251 {
252 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
253
254 return windowManagerServiceProxy_->NotifyWindowTransition(from, to, true);
255 }
256
MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds,bool isAnimated,sptr<RSIWindowAnimationFinishedCallback> & finishCallback)257 void WindowAdapter::MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds, bool isAnimated,
258 sptr<RSIWindowAnimationFinishedCallback>& finishCallback)
259 {
260 INIT_PROXY_CHECK_RETURN();
261 windowManagerServiceProxy_->MinimizeWindowsByLauncher(windowIds, isAnimated, finishCallback);
262 return;
263 }
264
UpdateAvoidAreaListener(uint32_t windowId,bool haveListener)265 WMError WindowAdapter::UpdateAvoidAreaListener(uint32_t windowId, bool haveListener)
266 {
267 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
268 return windowManagerServiceProxy_->UpdateAvoidAreaListener(windowId, haveListener);
269 }
270
UpdateRsTree(uint32_t windowId,bool isAdd)271 WMError WindowAdapter::UpdateRsTree(uint32_t windowId, bool isAdd)
272 {
273 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
274 return windowManagerServiceProxy_->UpdateRsTree(windowId, isAdd);
275 }
276
BindDialogTarget(uint32_t & windowId,sptr<IRemoteObject> targetToken)277 WMError WindowAdapter::BindDialogTarget(uint32_t& windowId, sptr<IRemoteObject> targetToken)
278 {
279 INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
280 return windowManagerServiceProxy_->BindDialogTarget(windowId, targetToken);
281 }
282
SetAnchorAndScale(int32_t x,int32_t y,float scale)283 void WindowAdapter::SetAnchorAndScale(int32_t x, int32_t y, float scale)
284 {
285 INIT_PROXY_CHECK_RETURN();
286 windowManagerServiceProxy_->SetAnchorAndScale(x, y, scale);
287 }
288
SetAnchorOffset(int32_t deltaX,int32_t deltaY)289 void WindowAdapter::SetAnchorOffset(int32_t deltaX, int32_t deltaY)
290 {
291 INIT_PROXY_CHECK_RETURN();
292 windowManagerServiceProxy_->SetAnchorOffset(deltaX, deltaY);
293 }
294
OffWindowZoom()295 void WindowAdapter::OffWindowZoom()
296 {
297 INIT_PROXY_CHECK_RETURN();
298 windowManagerServiceProxy_->OffWindowZoom();
299 }
300
GetSnapshot(int32_t windowId)301 std::shared_ptr<Media::PixelMap> WindowAdapter::GetSnapshot(int32_t windowId)
302 {
303 INIT_PROXY_CHECK_RETURN(nullptr);
304 return windowManagerServiceProxy_->GetSnapshot(windowId);
305 }
306
NotifyDumpInfoResult(const std::vector<std::string> & info)307 void WindowAdapter::NotifyDumpInfoResult(const std::vector<std::string>& info)
308 {
309 INIT_PROXY_CHECK_RETURN();
310 windowManagerServiceProxy_->NotifyDumpInfoResult(info);
311 }
312 } // namespace Rosen
313 } // namespace OHOS