• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <key_event.h>
19 #include <system_ability_definition.h>
20 #include <rs_window_animation_target.h>
21 #include "window_manager.h"
22 #include "window_manager_proxy.h"
23 #include "window_manager_hilog.h"
24 #include "wm_common.h"
25 #include "scene_board_judgement.h"
26 #include "session_manager.h"
27 #include "focus_change_info.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace {
32     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAdapter"};
33 }
WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter)34 WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapter)
35 
36 #define INIT_PROXY_CHECK_RETURN(ret) \
37     do { \
38         if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) { \
39             if (!InitSSMProxy()) { \
40                 WLOGFE("InitSSMProxy failed!"); \
41                 return ret; \
42             } \
43         } else { \
44             if (!InitWMSProxy()) { \
45                 WLOGFE("InitWMSProxy failed!"); \
46                 return ret; \
47             } \
48         } \
49     } while (false)
50 
51 WMError WindowAdapter::CreateWindow(sptr<IWindow>& window, sptr<WindowProperty>& windowProperty,
52     std::shared_ptr<RSSurfaceNode> surfaceNode, uint32_t& windowId, const sptr<IRemoteObject>& token)
53 {
54     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
55     return windowManagerServiceProxy_->CreateWindow(window, windowProperty, surfaceNode, windowId, token);
56 }
57 
AddWindow(sptr<WindowProperty> & windowProperty)58 WMError WindowAdapter::AddWindow(sptr<WindowProperty>& windowProperty)
59 {
60     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
61     return windowManagerServiceProxy_->AddWindow(windowProperty);
62 }
63 
RemoveWindow(uint32_t windowId,bool isFromInnerkits)64 WMError WindowAdapter::RemoveWindow(uint32_t windowId, bool isFromInnerkits)
65 {
66     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
67     return windowManagerServiceProxy_->RemoveWindow(windowId, isFromInnerkits);
68 }
69 
DestroyWindow(uint32_t windowId)70 WMError WindowAdapter::DestroyWindow(uint32_t windowId)
71 {
72     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
73     return windowManagerServiceProxy_->DestroyWindow(windowId);
74 }
75 
RequestFocus(uint32_t windowId)76 WMError WindowAdapter::RequestFocus(uint32_t windowId)
77 {
78     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
79     return windowManagerServiceProxy_->RequestFocus(windowId);
80 }
81 
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)82 WMError WindowAdapter::RegisterWindowManagerAgent(WindowManagerAgentType type,
83     const sptr<IWindowManagerAgent>& windowManagerAgent)
84 {
85     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
86     return windowManagerServiceProxy_->RegisterWindowManagerAgent(type, windowManagerAgent);
87 }
88 
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)89 WMError WindowAdapter::UnregisterWindowManagerAgent(WindowManagerAgentType type,
90     const sptr<IWindowManagerAgent>& windowManagerAgent)
91 {
92     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
93     return windowManagerServiceProxy_->UnregisterWindowManagerAgent(type, windowManagerAgent);
94 }
95 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos)96 WMError WindowAdapter::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos)
97 {
98     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
99     return windowManagerServiceProxy_->GetAccessibilityWindowInfo(infos);
100 }
101 
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos)102 WMError WindowAdapter::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos)
103 {
104     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
105     return windowManagerServiceProxy_->GetVisibilityWindowInfo(infos);
106 }
107 
SetWindowAnimationController(const sptr<RSIWindowAnimationController> & controller)108 WMError WindowAdapter::SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller)
109 {
110     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
111     return windowManagerServiceProxy_->SetWindowAnimationController(controller);
112 }
113 
GetAvoidAreaByType(uint32_t windowId,AvoidAreaType type,AvoidArea & avoidArea)114 WMError WindowAdapter::GetAvoidAreaByType(uint32_t windowId, AvoidAreaType type, AvoidArea& avoidArea)
115 {
116     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
117     avoidArea = windowManagerServiceProxy_->GetAvoidAreaByType(windowId, type);
118     return WMError::WM_OK;
119 }
120 
NotifyServerReadyToMoveOrDrag(uint32_t windowId,sptr<WindowProperty> & windowProperty,sptr<MoveDragProperty> & moveDragProperty)121 void WindowAdapter::NotifyServerReadyToMoveOrDrag(uint32_t windowId, sptr<WindowProperty>& windowProperty,
122     sptr<MoveDragProperty>& moveDragProperty)
123 {
124     INIT_PROXY_CHECK_RETURN();
125     return windowManagerServiceProxy_->NotifyServerReadyToMoveOrDrag(windowId, windowProperty, moveDragProperty);
126 }
127 
ProcessPointDown(uint32_t windowId,bool isPointDown)128 void WindowAdapter::ProcessPointDown(uint32_t windowId, bool isPointDown)
129 {
130     INIT_PROXY_CHECK_RETURN();
131     return windowManagerServiceProxy_->ProcessPointDown(windowId, isPointDown);
132 }
133 
ProcessPointUp(uint32_t windowId)134 void WindowAdapter::ProcessPointUp(uint32_t windowId)
135 {
136     INIT_PROXY_CHECK_RETURN();
137     return windowManagerServiceProxy_->ProcessPointUp(windowId);
138 }
139 
MinimizeAllAppWindows(DisplayId displayId)140 WMError WindowAdapter::MinimizeAllAppWindows(DisplayId displayId)
141 {
142     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
143     return windowManagerServiceProxy_->MinimizeAllAppWindows(displayId);
144 }
145 
ToggleShownStateForAllAppWindows()146 WMError WindowAdapter::ToggleShownStateForAllAppWindows()
147 {
148     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
149     return windowManagerServiceProxy_->ToggleShownStateForAllAppWindows();
150 }
151 
GetSystemConfig(SystemConfig & systemConfig)152 WMError WindowAdapter::GetSystemConfig(SystemConfig& systemConfig)
153 {
154     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
155     return windowManagerServiceProxy_->GetSystemConfig(systemConfig);
156 }
157 
GetModeChangeHotZones(DisplayId displayId,ModeChangeHotZones & hotZones)158 WMError WindowAdapter::GetModeChangeHotZones(DisplayId displayId, ModeChangeHotZones& hotZones)
159 {
160     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
161     return windowManagerServiceProxy_->GetModeChangeHotZones(displayId, hotZones);
162 }
163 
InitWMSProxy()164 bool WindowAdapter::InitWMSProxy()
165 {
166     std::lock_guard<std::recursive_mutex> lock(mutex_);
167     if (!isProxyValid_) {
168         sptr<ISystemAbilityManager> systemAbilityManager =
169                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
170         if (!systemAbilityManager) {
171             WLOGFE("Failed to get system ability mgr.");
172             return false;
173         }
174 
175         sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
176         if (!remoteObject) {
177             WLOGFE("Failed to get window manager service.");
178             return false;
179         }
180 
181         windowManagerServiceProxy_ = iface_cast<IWindowManager>(remoteObject);
182         if ((!windowManagerServiceProxy_) || (!windowManagerServiceProxy_->AsObject())) {
183             WLOGFE("Failed to get system window manager services");
184             return false;
185         }
186 
187         wmsDeath_ = new WMSDeathRecipient();
188         if (!wmsDeath_) {
189             WLOGFE("Failed to create death Recipient ptr WMSDeathRecipient");
190             return false;
191         }
192         if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) {
193             WLOGFE("Failed to add death recipient");
194             return false;
195         }
196         isProxyValid_ = true;
197     }
198     return true;
199 }
200 
InitSSMProxy()201 bool WindowAdapter::InitSSMProxy()
202 {
203     std::lock_guard<std::recursive_mutex> lock(mutex_);
204     if (!isProxyValid_) {
205         windowManagerServiceProxy_ = SessionManager::GetInstance().GetSceneSessionManagerProxy();
206         if ((!windowManagerServiceProxy_) || (!windowManagerServiceProxy_->AsObject())) {
207             WLOGFE("Failed to get system scene session manager services");
208             return false;
209         }
210 
211         wmsDeath_ = new (std::nothrow) WMSDeathRecipient();
212         if (!wmsDeath_) {
213             WLOGFE("Failed to create death Recipient ptr WMSDeathRecipient");
214             return false;
215         }
216         sptr<IRemoteObject> remoteObject = windowManagerServiceProxy_->AsObject();
217         if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) {
218             WLOGFE("Failed to add death recipient");
219             return false;
220         }
221         isProxyValid_ = true;
222     }
223     return true;
224 }
225 
ClearWindowAdapter()226 void WindowAdapter::ClearWindowAdapter()
227 {
228     if ((windowManagerServiceProxy_ != nullptr) && (windowManagerServiceProxy_->AsObject() != nullptr)) {
229         windowManagerServiceProxy_->AsObject()->RemoveDeathRecipient(wmsDeath_);
230     }
231     std::lock_guard<std::recursive_mutex> lock(mutex_);
232     isProxyValid_ = false;
233 }
234 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)235 void WMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
236 {
237     if (wptrDeath == nullptr) {
238         WLOGFE("wptrDeath is null");
239         return;
240     }
241 
242     sptr<IRemoteObject> object = wptrDeath.promote();
243     if (!object) {
244         WLOGFE("object is null");
245         return;
246     }
247     WLOGI("wms OnRemoteDied");
248     SingletonContainer::Get<WindowAdapter>().ClearWindowAdapter();
249     SingletonContainer::Get<WindowManager>().OnRemoteDied();
250     SingletonContainer::Get<SessionManager>().ClearSessionManagerProxy();
251 }
252 
GetTopWindowId(uint32_t mainWinId,uint32_t & topWinId)253 WMError WindowAdapter::GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId)
254 {
255     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
256     return windowManagerServiceProxy_->GetTopWindowId(mainWinId, topWinId);
257 }
258 
SetWindowLayoutMode(WindowLayoutMode mode)259 WMError WindowAdapter::SetWindowLayoutMode(WindowLayoutMode mode)
260 {
261     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
262     return windowManagerServiceProxy_->SetWindowLayoutMode(mode);
263 }
264 
UpdateProperty(sptr<WindowProperty> & windowProperty,PropertyChangeAction action)265 WMError WindowAdapter::UpdateProperty(sptr<WindowProperty>& windowProperty, PropertyChangeAction action)
266 {
267     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
268     return windowManagerServiceProxy_->UpdateProperty(windowProperty, action);
269 }
270 
SetWindowGravity(uint32_t windowId,WindowGravity gravity,uint32_t percent)271 WMError WindowAdapter::SetWindowGravity(uint32_t windowId, WindowGravity gravity, uint32_t percent)
272 {
273     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
274     return windowManagerServiceProxy_->SetWindowGravity(windowId, gravity, percent);
275 }
276 
NotifyWindowTransition(sptr<WindowTransitionInfo> from,sptr<WindowTransitionInfo> to)277 WMError WindowAdapter::NotifyWindowTransition(sptr<WindowTransitionInfo> from, sptr<WindowTransitionInfo> to)
278 {
279     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
280     return windowManagerServiceProxy_->NotifyWindowTransition(from, to, true);
281 }
282 
MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds,bool isAnimated,sptr<RSIWindowAnimationFinishedCallback> & finishCallback)283 void WindowAdapter::MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds, bool isAnimated,
284     sptr<RSIWindowAnimationFinishedCallback>& finishCallback)
285 {
286     INIT_PROXY_CHECK_RETURN();
287     windowManagerServiceProxy_->MinimizeWindowsByLauncher(windowIds, isAnimated, finishCallback);
288 }
289 
UpdateAvoidAreaListener(uint32_t windowId,bool haveListener)290 WMError WindowAdapter::UpdateAvoidAreaListener(uint32_t windowId, bool haveListener)
291 {
292     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
293     return windowManagerServiceProxy_->UpdateAvoidAreaListener(windowId, haveListener);
294 }
295 
UpdateRsTree(uint32_t windowId,bool isAdd)296 WMError WindowAdapter::UpdateRsTree(uint32_t windowId, bool isAdd)
297 {
298     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
299     return windowManagerServiceProxy_->UpdateRsTree(windowId, isAdd);
300 }
301 
BindDialogTarget(uint32_t & windowId,sptr<IRemoteObject> targetToken)302 WMError WindowAdapter::BindDialogTarget(uint32_t& windowId, sptr<IRemoteObject> targetToken)
303 {
304     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
305     return windowManagerServiceProxy_->BindDialogTarget(windowId, targetToken);
306 }
307 
SetAnchorAndScale(int32_t x,int32_t y,float scale)308 void WindowAdapter::SetAnchorAndScale(int32_t x, int32_t y, float scale)
309 {
310     INIT_PROXY_CHECK_RETURN();
311     windowManagerServiceProxy_->SetAnchorAndScale(x, y, scale);
312 }
313 
SetAnchorOffset(int32_t deltaX,int32_t deltaY)314 void WindowAdapter::SetAnchorOffset(int32_t deltaX, int32_t deltaY)
315 {
316     INIT_PROXY_CHECK_RETURN();
317     windowManagerServiceProxy_->SetAnchorOffset(deltaX, deltaY);
318 }
319 
OffWindowZoom()320 void WindowAdapter::OffWindowZoom()
321 {
322     INIT_PROXY_CHECK_RETURN();
323     windowManagerServiceProxy_->OffWindowZoom();
324 }
325 
RaiseToAppTop(uint32_t windowId)326 WmErrorCode WindowAdapter::RaiseToAppTop(uint32_t windowId)
327 {
328     INIT_PROXY_CHECK_RETURN(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
329     return windowManagerServiceProxy_->RaiseToAppTop(windowId);
330 }
331 
GetSnapshot(int32_t windowId)332 std::shared_ptr<Media::PixelMap> WindowAdapter::GetSnapshot(int32_t windowId)
333 {
334     INIT_PROXY_CHECK_RETURN(nullptr);
335     return windowManagerServiceProxy_->GetSnapshot(windowId);
336 }
337 
SetGestureNavigaionEnabled(bool enable)338 WMError WindowAdapter::SetGestureNavigaionEnabled(bool enable)
339 {
340     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
341     return windowManagerServiceProxy_->SetGestureNavigaionEnabled(enable);
342 }
343 
DispatchKeyEvent(uint32_t windowId,std::shared_ptr<MMI::KeyEvent> event)344 void WindowAdapter::DispatchKeyEvent(uint32_t windowId, std::shared_ptr<MMI::KeyEvent> event)
345 {
346     INIT_PROXY_CHECK_RETURN();
347     windowManagerServiceProxy_->DispatchKeyEvent(windowId, event);
348 }
349 
NotifyDumpInfoResult(const std::vector<std::string> & info)350 void WindowAdapter::NotifyDumpInfoResult(const std::vector<std::string>& info)
351 {
352     INIT_PROXY_CHECK_RETURN();
353     windowManagerServiceProxy_->NotifyDumpInfoResult(info);
354 }
355 
GetWindowAnimationTargets(std::vector<uint32_t> missionIds,std::vector<sptr<RSWindowAnimationTarget>> & targets)356 WMError WindowAdapter::GetWindowAnimationTargets(std::vector<uint32_t> missionIds,
357     std::vector<sptr<RSWindowAnimationTarget>>& targets)
358 {
359     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
360     return windowManagerServiceProxy_->GetWindowAnimationTargets(missionIds, targets);
361 }
SetMaximizeMode(MaximizeMode maximizeMode)362 void WindowAdapter::SetMaximizeMode(MaximizeMode maximizeMode)
363 {
364     INIT_PROXY_CHECK_RETURN();
365     windowManagerServiceProxy_->SetMaximizeMode(maximizeMode);
366 }
367 
GetMaximizeMode()368 MaximizeMode WindowAdapter::GetMaximizeMode()
369 {
370     INIT_PROXY_CHECK_RETURN(MaximizeMode::MODE_FULL_FILL);
371     return windowManagerServiceProxy_->GetMaximizeMode();
372 }
373 
GetFocusWindowInfo(FocusChangeInfo & focusInfo)374 void WindowAdapter::GetFocusWindowInfo(FocusChangeInfo& focusInfo)
375 {
376     INIT_PROXY_CHECK_RETURN();
377     return windowManagerServiceProxy_->GetFocusWindowInfo(focusInfo);
378 }
379 
UpdateSessionAvoidAreaListener(int32_t & persistentId,bool haveListener)380 WMError WindowAdapter::UpdateSessionAvoidAreaListener(int32_t& persistentId, bool haveListener)
381 {
382     INIT_PROXY_CHECK_RETURN(WMError::WM_DO_NOTHING);
383     return static_cast<WMError>(windowManagerServiceProxy_->UpdateSessionAvoidAreaListener(persistentId, haveListener));
384 }
385 } // namespace Rosen
386 } // namespace OHOS
387