1 // /*
2 // * Copyright (c) 2023 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 "display_manager_adapter_lite.h"
17
18 #include <iremote_broker.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "display_manager_lite.h"
23 #include "screen_manager_lite.h"
24 #include "dm_common.h"
25 #include "window_manager_hilog.h"
26
27 namespace OHOS::Rosen {
28 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapterLite)
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapterLite)29 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapterLite)
30
31
32 #define INIT_PROXY_CHECK_RETURN(ret) \
33 do { \
34 if (!InitDMSProxy()) { \
35 TLOGE(WmsLogTag::DMS, "InitDMSProxy fail"); \
36 return ret; \
37 } \
38 } while (false)
39
40 DMError BaseAdapterLite::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
41 DisplayManagerAgentType type)
42 {
43 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
44
45 return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type);
46 }
47
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)48 DMError BaseAdapterLite::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
49 DisplayManagerAgentType type)
50 {
51 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
52
53 return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type);
54 }
55
InitDMSProxy()56 bool BaseAdapterLite::InitDMSProxy()
57 {
58 std::lock_guard<std::recursive_mutex> lock(mutex_);
59 if (!isProxyValid_) {
60 sptr<ISystemAbilityManager> systemAbilityManager =
61 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62 if (!systemAbilityManager) {
63 TLOGE(WmsLogTag::DMS, "Failed to get system ability mgr.");
64 return false;
65 }
66
67 sptr<IRemoteObject> remoteObject
68 = systemAbilityManager->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
69 if (!remoteObject) {
70 TLOGE(WmsLogTag::DMS, "Failed to get display manager service.");
71 return false;
72 }
73 displayManagerServiceProxy_ = new(std::nothrow) DisplayManagerLiteProxy(remoteObject);
74 if ((!displayManagerServiceProxy_) || (!displayManagerServiceProxy_->AsObject())) {
75 TLOGW(WmsLogTag::DMS, "Failed to get system display manager services");
76 return false;
77 }
78
79 dmsDeath_ = new(std::nothrow) DMSDeathRecipientLite(*this);
80 if (dmsDeath_ == nullptr) {
81 TLOGE(WmsLogTag::DMS, "Failed to create death Recipient ptr DMSDeathRecipient");
82 return false;
83 }
84 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(dmsDeath_)) {
85 TLOGE(WmsLogTag::DMS, "Failed to add death recipient");
86 return false;
87 }
88 isProxyValid_ = true;
89 }
90 return true;
91 }
92
GetDefaultDisplayInfo()93 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDefaultDisplayInfo()
94 {
95 INIT_PROXY_CHECK_RETURN(nullptr);
96
97 return displayManagerServiceProxy_->GetDefaultDisplayInfo();
98 }
99
IsScreenLessDevice()100 bool DisplayManagerAdapterLite::IsScreenLessDevice()
101 {
102 #ifdef SCREENLESS_ENABLE
103 return true;
104 #else
105 return false;
106 #endif
107 }
108
IsScreenLessDevice()109 bool ScreenManagerAdapterLite::IsScreenLessDevice()
110 {
111 #ifdef SCREENLESS_ENABLE
112 return true;
113 #else
114 return false;
115 #endif
116 }
117
IsFoldable()118 bool DisplayManagerAdapterLite::IsFoldable()
119 {
120 if (IsScreenLessDevice()) {
121 return false;
122 }
123 INIT_PROXY_CHECK_RETURN(false);
124
125 return displayManagerServiceProxy_->IsFoldable();
126 }
127
GetFoldStatus()128 FoldStatus DisplayManagerAdapterLite::GetFoldStatus()
129 {
130 INIT_PROXY_CHECK_RETURN(FoldStatus::UNKNOWN);
131
132 return displayManagerServiceProxy_->GetFoldStatus();
133 }
134
GetFoldDisplayMode()135 FoldDisplayMode DisplayManagerAdapterLite::GetFoldDisplayMode()
136 {
137 INIT_PROXY_CHECK_RETURN(FoldDisplayMode::UNKNOWN);
138
139 return displayManagerServiceProxy_->GetFoldDisplayMode();
140 }
141
SetFoldDisplayMode(const FoldDisplayMode mode)142 void DisplayManagerAdapterLite::SetFoldDisplayMode(const FoldDisplayMode mode)
143 {
144 INIT_PROXY_CHECK_RETURN();
145
146 return displayManagerServiceProxy_->SetFoldDisplayMode(mode);
147 }
148
SetFoldDisplayModeAsync(const FoldDisplayMode mode)149 void DisplayManagerAdapterLite::SetFoldDisplayModeAsync(const FoldDisplayMode mode)
150 {
151 INIT_PROXY_CHECK_RETURN();
152
153 return displayManagerServiceProxy_->SetFoldDisplayModeAsync(mode);
154 }
155
GetDisplayInfo(DisplayId displayId)156 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDisplayInfo(DisplayId displayId)
157 {
158 if (displayId == DISPLAY_ID_INVALID) {
159 TLOGW(WmsLogTag::DMS, "screen id is invalid");
160 return nullptr;
161 }
162 INIT_PROXY_CHECK_RETURN(nullptr);
163
164 return displayManagerServiceProxy_->GetDisplayInfoById(displayId);
165 }
166
GetCutoutInfo(DisplayId displayId)167 sptr<CutoutInfo> DisplayManagerAdapterLite::GetCutoutInfo(DisplayId displayId)
168 {
169 if (displayId == DISPLAY_ID_INVALID) {
170 TLOGE(WmsLogTag::DMS, "screen id is invalid");
171 return nullptr;
172 }
173 INIT_PROXY_CHECK_RETURN(nullptr);
174 return displayManagerServiceProxy_->GetCutoutInfo(displayId);
175 }
176
177 /*
178 * used by powermgr
179 */
WakeUpBegin(PowerStateChangeReason reason)180 bool DisplayManagerAdapterLite::WakeUpBegin(PowerStateChangeReason reason)
181 {
182 if (IsScreenLessDevice()) {
183 return true;
184 }
185 INIT_PROXY_CHECK_RETURN(false);
186
187 return displayManagerServiceProxy_->WakeUpBegin(reason);
188 }
189
WakeUpEnd()190 bool DisplayManagerAdapterLite::WakeUpEnd()
191 {
192 if (IsScreenLessDevice()) {
193 return true;
194 }
195 INIT_PROXY_CHECK_RETURN(false);
196
197 return displayManagerServiceProxy_->WakeUpEnd();
198 }
199
SuspendBegin(PowerStateChangeReason reason)200 bool DisplayManagerAdapterLite::SuspendBegin(PowerStateChangeReason reason)
201 {
202 if (IsScreenLessDevice()) {
203 return true;
204 }
205 INIT_PROXY_CHECK_RETURN(false);
206
207 return displayManagerServiceProxy_->SuspendBegin(reason);
208 }
209
SuspendEnd()210 bool DisplayManagerAdapterLite::SuspendEnd()
211 {
212 if (IsScreenLessDevice()) {
213 return true;
214 }
215 INIT_PROXY_CHECK_RETURN(false);
216
217 return displayManagerServiceProxy_->SuspendEnd();
218 }
219
GetInternalScreenId()220 ScreenId DisplayManagerAdapterLite::GetInternalScreenId()
221 {
222 if (IsScreenLessDevice()) {
223 return 0;
224 }
225 INIT_PROXY_CHECK_RETURN(false);
226
227 return displayManagerServiceProxy_->GetInternalScreenId();
228 }
229
SetScreenPowerById(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)230 bool DisplayManagerAdapterLite::SetScreenPowerById(ScreenId screenId, ScreenPowerState state,
231 PowerStateChangeReason reason)
232 {
233 if (IsScreenLessDevice()) {
234 return true;
235 }
236 INIT_PROXY_CHECK_RETURN(false);
237
238 return displayManagerServiceProxy_->SetScreenPowerById(screenId, state, reason);
239 }
240
SetDisplayState(DisplayState state)241 bool DisplayManagerAdapterLite::SetDisplayState(DisplayState state)
242 {
243 if (IsScreenLessDevice()) {
244 return true;
245 }
246 INIT_PROXY_CHECK_RETURN(false);
247
248 return displayManagerServiceProxy_->SetDisplayState(state);
249 }
250
GetDisplayState(DisplayId displayId)251 DisplayState DisplayManagerAdapterLite::GetDisplayState(DisplayId displayId)
252 {
253 INIT_PROXY_CHECK_RETURN(DisplayState::UNKNOWN);
254
255 return displayManagerServiceProxy_->GetDisplayState(displayId);
256 }
257
TryToCancelScreenOff()258 bool DisplayManagerAdapterLite::TryToCancelScreenOff()
259 {
260 if (IsScreenLessDevice()) {
261 return true;
262 }
263 INIT_PROXY_CHECK_RETURN(false);
264
265 return displayManagerServiceProxy_->TryToCancelScreenOff();
266 }
267
SetScreenBrightness(uint64_t screenId,uint32_t level)268 bool DisplayManagerAdapterLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
269 {
270 if (IsScreenLessDevice()) {
271 return true;
272 }
273 INIT_PROXY_CHECK_RETURN(false);
274
275 return displayManagerServiceProxy_->SetScreenBrightness(screenId, level);
276 }
277
GetScreenBrightness(uint64_t screenId)278 uint32_t DisplayManagerAdapterLite::GetScreenBrightness(uint64_t screenId)
279 {
280 if (IsScreenLessDevice()) {
281 return 0;
282 }
283 INIT_PROXY_CHECK_RETURN(false);
284
285 return displayManagerServiceProxy_->GetScreenBrightness(screenId);
286 }
287
GetAllDisplayIds()288 std::vector<DisplayId> DisplayManagerAdapterLite::GetAllDisplayIds()
289 {
290 TLOGD(WmsLogTag::DMS, "enter");
291 INIT_PROXY_CHECK_RETURN(std::vector<DisplayId>());
292
293 return displayManagerServiceProxy_->GetAllDisplayIds();
294 }
295
GetVirtualScreenFlag(ScreenId screenId)296 VirtualScreenFlag DisplayManagerAdapterLite::GetVirtualScreenFlag(ScreenId screenId)
297 {
298 INIT_PROXY_CHECK_RETURN(VirtualScreenFlag::DEFAULT);
299 if (screenId == SCREEN_ID_INVALID) {
300 TLOGE(WmsLogTag::DMS, "screenId id is invalid");
301 return VirtualScreenFlag::DEFAULT;
302 }
303 return displayManagerServiceProxy_->GetVirtualScreenFlag(screenId);
304 }
305
SetSystemKeyboardStatus(bool isTpKeyboardOn)306 DMError DisplayManagerAdapterLite::SetSystemKeyboardStatus(bool isTpKeyboardOn)
307 {
308 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
309
310 return displayManagerServiceProxy_->SetSystemKeyboardStatus(isTpKeyboardOn);
311 }
312
GetScreenInfo(ScreenId screenId)313 sptr<ScreenInfo> ScreenManagerAdapterLite::GetScreenInfo(ScreenId screenId)
314 {
315 if (screenId == SCREEN_ID_INVALID) {
316 TLOGE(WmsLogTag::DMS, "screen id is invalid");
317 return nullptr;
318 }
319 INIT_PROXY_CHECK_RETURN(nullptr);
320
321 return displayManagerServiceProxy_->GetScreenInfoById(screenId);
322 }
323
SetSpecifiedScreenPower(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)324 bool ScreenManagerAdapterLite::SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state,
325 PowerStateChangeReason reason)
326 {
327 if (IsScreenLessDevice()) {
328 return true;
329 }
330 INIT_PROXY_CHECK_RETURN(false);
331
332 return displayManagerServiceProxy_->SetSpecifiedScreenPower(screenId, state, reason);
333 }
334
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)335 bool ScreenManagerAdapterLite::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
336 {
337 if (IsScreenLessDevice()) {
338 return true;
339 }
340 INIT_PROXY_CHECK_RETURN(false);
341
342 return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
343 }
344
GetScreenPower(ScreenId dmsScreenId)345 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower(ScreenId dmsScreenId)
346 {
347 if (IsScreenLessDevice()) {
348 return ScreenPowerState::POWER_STAND_BY;
349 }
350 INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
351
352 return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
353 }
354
GetScreenPower()355 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower()
356 {
357 if (IsScreenLessDevice()) {
358 return ScreenPowerState::POWER_STAND_BY;
359 }
360 INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
361
362 return displayManagerServiceProxy_->GetScreenPower();
363 }
364
GetAllScreenInfos(std::vector<sptr<ScreenInfo>> & screenInfos)365 DMError ScreenManagerAdapterLite::GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos)
366 {
367 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
368
369 return displayManagerServiceProxy_->GetAllScreenInfos(screenInfos);
370 }
371
GetPhysicalScreenIds(std::vector<ScreenId> & screenIds)372 DMError ScreenManagerAdapterLite::GetPhysicalScreenIds(std::vector<ScreenId>& screenIds)
373 {
374 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
375
376 return displayManagerServiceProxy_->GetPhysicalScreenIds(screenIds);
377 }
378
DMSDeathRecipientLite(BaseAdapterLite & adapter)379 DMSDeathRecipientLite::DMSDeathRecipientLite(BaseAdapterLite& adapter) : adapter_(adapter)
380 {
381 }
382
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)383 void DMSDeathRecipientLite::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
384 {
385 if (wptrDeath == nullptr) {
386 TLOGE(WmsLogTag::DMS, "wptrDeath is nullptr");
387 return;
388 }
389
390 sptr<IRemoteObject> object = wptrDeath.promote();
391 if (!object) {
392 TLOGE(WmsLogTag::DMS, "object is nullptr");
393 return;
394 }
395 TLOGI(WmsLogTag::DMS, "dms OnRemoteDied");
396 adapter_.Clear();
397 if (SingletonContainer::IsDestroyed()) {
398 TLOGE(WmsLogTag::DMS, "SingletonContainer is destroyed");
399 return;
400 }
401 SingletonContainer::Get<DisplayManagerLite>().OnRemoteDied();
402 SingletonContainer::Get<ScreenManagerLite>().OnRemoteDied();
403 return;
404 }
405
~BaseAdapterLite()406 BaseAdapterLite::~BaseAdapterLite()
407 {
408 TLOGI(WmsLogTag::DMS, "destroy");
409 std::lock_guard<std::recursive_mutex> lock(mutex_);
410 Clear();
411 displayManagerServiceProxy_ = nullptr;
412 }
413
Clear()414 void BaseAdapterLite::Clear()
415 {
416 TLOGI(WmsLogTag::DMS, "Clear");
417 std::lock_guard<std::recursive_mutex> lock(mutex_);
418 if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
419 displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
420 }
421 isProxyValid_ = false;
422 }
423
GetKeyboardState()424 bool DisplayManagerAdapterLite::GetKeyboardState()
425 {
426 INIT_PROXY_CHECK_RETURN(false);
427
428 return displayManagerServiceProxy_->GetKeyboardState();
429 }
430
SynchronizePowerStatus(ScreenPowerState state)431 bool ScreenManagerAdapterLite::SynchronizePowerStatus(ScreenPowerState state)
432 {
433 INIT_PROXY_CHECK_RETURN(false);
434
435 return displayManagerServiceProxy_->SynchronizePowerStatus(state);
436 }
437 } // namespace OHOS::Rosen