• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_lite.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 
21 #include "display_manager_adapter_lite.h"
22 #include "display_manager_agent_default.h"
23 #include "dm_common.h"
24 #include "singleton_delegator.h"
25 #include "window_manager_hilog.h"
26 
27 namespace OHOS::Rosen {
28 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerLite)
29 
30 class DisplayManagerLite::Impl : public RefBase {
31 public:
Impl(std::recursive_mutex & mutex)32     Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
33     ~Impl();
34     static inline SingletonDelegator<DisplayManagerLite> delegator;
35     sptr<DisplayLite> GetDefaultDisplay();
36     FoldStatus GetFoldStatus();
37     FoldDisplayMode GetFoldDisplayMode();
38     FoldDisplayMode GetFoldDisplayModeForExternal();
39     void SetFoldDisplayMode(const FoldDisplayMode);
40     void SetFoldDisplayModeAsync(const FoldDisplayMode);
41     bool IsFoldable();
42 
43     DMError RegisterDisplayListener(sptr<IDisplayListener> listener);
44     DMError UnregisterDisplayListener(sptr<IDisplayListener> listener);
45     DMError RegisterFoldStatusListener(sptr<IFoldStatusListener> listener);
46     DMError UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener);
47     DMError RegisterDisplayModeListener(sptr<IDisplayModeListener> listener);
48     DMError UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener);
49     DMError RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener);
50     DMError UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener);
51     void OnRemoteDied();
52     sptr<DisplayLite> GetDisplayById(DisplayId displayId);
53     /*
54      * used by powermgr
55      */
56     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
57 private:
58     void NotifyDisplayCreate(sptr<DisplayInfo> info);
59     void NotifyDisplayDestroy(DisplayId);
60     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
61     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
62     void NotifyFoldStatusChanged(FoldStatus foldStatus);
63     void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
64     /*
65      * used by powermgr
66      */
67     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
68     void ClearDisplayStateCallback();
69     void ClearFoldStatusCallback();
70     void ClearDisplayModeCallback();
71     void Clear();
72 
73     std::map<DisplayId, sptr<DisplayLite>> displayMap_;
74     DisplayStateCallback displayStateCallback_;
75     std::recursive_mutex& mutex_;
76     std::set<sptr<IDisplayListener>> displayListeners_;
77     std::set<sptr<IFoldStatusListener>> foldStatusListeners_;
78     std::set<sptr<IDisplayModeListener>> displayModeListeners_;
79     class DisplayManagerListener;
80     sptr<DisplayManagerListener> displayManagerListener_;
81     class DisplayManagerFoldStatusAgent;
82     sptr<DisplayManagerFoldStatusAgent> foldStatusListenerAgent_;
83     class DisplayManagerDisplayModeAgent;
84     sptr<DisplayManagerDisplayModeAgent> displayModeListenerAgent_;
85     /*
86      * used by powermgr
87      */
88     class DisplayManagerAgent;
89     sptr<DisplayManagerAgent> displayStateAgent_;
90     void NotifyScreenMagneticStateChanged(bool isMagneticState);
91     std::set<sptr<IScreenMagneticStateListener>> screenMagneticStateListeners_;
92     class DisplayManagerScreenMagneticStateAgent;
93     sptr<DisplayManagerScreenMagneticStateAgent> screenMagneticStateListenerAgent_;
94 };
95 
96 class DisplayManagerLite::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
97 public:
DisplayManagerListener(sptr<Impl> impl)98     explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
99     {
100     }
101 
OnDisplayCreate(sptr<DisplayInfo> displayInfo)102     void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
103     {
104         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
105             TLOGE(WmsLogTag::DMS, "displayInfo is nullptr");
106             return;
107         }
108         if (pImpl_ == nullptr) {
109             TLOGE(WmsLogTag::DMS, "pImpl_ is nullptr");
110             return;
111         }
112         pImpl_->NotifyDisplayCreate(displayInfo);
113         std::set<sptr<IDisplayListener>> displayListeners;
114         {
115             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
116             displayListeners = pImpl_->displayListeners_;
117         }
118         for (auto listener : displayListeners) {
119             listener->OnCreate(displayInfo->GetDisplayId());
120         }
121     };
122 
OnDisplayDestroy(DisplayId displayId)123     void OnDisplayDestroy(DisplayId displayId) override
124     {
125         if (displayId == DISPLAY_ID_INVALID) {
126             TLOGE(WmsLogTag::DMS, "displayId is invalid");
127             return;
128         }
129         if (pImpl_ == nullptr) {
130             TLOGE(WmsLogTag::DMS, "impl is nullptr");
131             return;
132         }
133         pImpl_->NotifyDisplayDestroy(displayId);
134         std::set<sptr<IDisplayListener>> displayListeners;
135         {
136             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
137             displayListeners = pImpl_->displayListeners_;
138         }
139         for (auto listener : displayListeners) {
140             listener->OnDestroy(displayId);
141         }
142     };
143 
OnDisplayChange(sptr<DisplayInfo> displayInfo,DisplayChangeEvent event)144     void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
145     {
146         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
147             TLOGE(WmsLogTag::DMS, "displayInfo is nullptr");
148             return;
149         }
150         if (pImpl_ == nullptr) {
151             TLOGE(WmsLogTag::DMS, "pImpl_ is nullptr");
152             return;
153         }
154         TLOGD(WmsLogTag::DMS, "display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
155         pImpl_->NotifyDisplayChange(displayInfo);
156         std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
157         for (auto listener : pImpl_->displayListeners_) {
158             listener->OnChange(displayInfo->GetDisplayId());
159         }
160     };
161 private:
162     sptr<Impl> pImpl_;
163 };
164 
165 class DisplayManagerLite::Impl::DisplayManagerFoldStatusAgent : public DisplayManagerAgentDefault {
166 public:
DisplayManagerFoldStatusAgent(sptr<Impl> impl)167     explicit DisplayManagerFoldStatusAgent(sptr<Impl> impl) : pImpl_(impl)
168     {
169     }
170     ~DisplayManagerFoldStatusAgent() = default;
171 
NotifyFoldStatusChanged(FoldStatus foldStatus)172     virtual void NotifyFoldStatusChanged(FoldStatus foldStatus) override
173     {
174         pImpl_->NotifyFoldStatusChanged(foldStatus);
175     }
176 private:
177     sptr<Impl> pImpl_;
178 };
179 
180 class DisplayManagerLite::Impl::DisplayManagerDisplayModeAgent : public DisplayManagerAgentDefault {
181 public:
DisplayManagerDisplayModeAgent(sptr<Impl> impl)182     explicit DisplayManagerDisplayModeAgent(sptr<Impl> impl) : pImpl_(impl)
183     {
184     }
185     ~DisplayManagerDisplayModeAgent() = default;
186 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)187     virtual void NotifyDisplayModeChanged(FoldDisplayMode displayMode) override
188     {
189         pImpl_->NotifyDisplayModeChanged(displayMode);
190     }
191 private:
192     sptr<Impl> pImpl_;
193 };
194 
195 class DisplayManagerLite::Impl::DisplayManagerScreenMagneticStateAgent : public DisplayManagerAgentDefault {
196 public:
DisplayManagerScreenMagneticStateAgent(sptr<Impl> impl)197     explicit DisplayManagerScreenMagneticStateAgent(sptr<Impl> impl) : pImpl_(impl)
198     {
199     }
200     ~DisplayManagerScreenMagneticStateAgent() = default;
201 
NotifyScreenMagneticStateChanged(bool isMagneticState)202     virtual void NotifyScreenMagneticStateChanged(bool isMagneticState) override
203     {
204         pImpl_->NotifyScreenMagneticStateChanged(isMagneticState);
205     }
206 private:
207     sptr<Impl> pImpl_;
208 };
209 
210 /*
211  * used by powermgr
212  */
213 class DisplayManagerLite::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
214 public:
DisplayManagerAgent(sptr<Impl> impl)215     explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
216     {
217     }
218     ~DisplayManagerAgent() = default;
219 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)220     virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
221     {
222         pImpl_->NotifyDisplayStateChanged(id, state);
223     }
224 private:
225     sptr<Impl> pImpl_;
226 };
227 
Clear()228 void DisplayManagerLite::Impl::Clear()
229 {
230     std::lock_guard<std::recursive_mutex> lock(mutex_);
231     DMError res = DMError::DM_OK;
232     if (displayManagerListener_ != nullptr) {
233         res = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
234             displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
235     }
236     displayManagerListener_ = nullptr;
237     if (res != DMError::DM_OK) {
238         TLOGW(WmsLogTag::DMS, "UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed");
239     }
240     ClearDisplayStateCallback();
241     ClearFoldStatusCallback();
242     ClearDisplayModeCallback();
243 }
244 
~Impl()245 DisplayManagerLite::Impl::~Impl()
246 {
247     Clear();
248 }
249 
DisplayManagerLite()250 DisplayManagerLite::DisplayManagerLite() : pImpl_(new Impl(mutex_))
251 {
252 }
253 
~DisplayManagerLite()254 DisplayManagerLite::~DisplayManagerLite()
255 {
256     std::lock_guard<std::recursive_mutex> lock(mutex_);
257     destroyed_ = true;
258 }
259 
RegisterDisplayListener(sptr<IDisplayListener> listener)260 DMError DisplayManagerLite::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
261 {
262     std::lock_guard<std::recursive_mutex> lock(mutex_);
263     DMError ret = DMError::DM_OK;
264     if (displayManagerListener_ == nullptr) {
265         displayManagerListener_ = new DisplayManagerListener(this);
266         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
267             displayManagerListener_,
268             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
269     }
270     if (ret != DMError::DM_OK) {
271         TLOGW(WmsLogTag::DMS, "RegisterDisplayManagerAgent failed");
272         displayManagerListener_ = nullptr;
273     } else {
274         displayListeners_.insert(listener);
275     }
276     return ret;
277 }
278 
RegisterDisplayListener(sptr<IDisplayListener> listener)279 DMError DisplayManagerLite::RegisterDisplayListener(sptr<IDisplayListener> listener)
280 {
281     if (listener == nullptr) {
282         TLOGE(WmsLogTag::DMS, "listener is nullptr");
283         return DMError::DM_ERROR_NULLPTR;
284     }
285     return pImpl_->RegisterDisplayListener(listener);
286 }
287 
UnregisterDisplayListener(sptr<IDisplayListener> listener)288 DMError DisplayManagerLite::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
289 {
290     std::lock_guard<std::recursive_mutex> lock(mutex_);
291     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
292     if (iter == displayListeners_.end()) {
293         TLOGE(WmsLogTag::DMS, "could not find this listener");
294         return DMError::DM_ERROR_NULLPTR;
295     }
296     displayListeners_.erase(iter);
297     DMError ret = DMError::DM_OK;
298     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
299         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
300             displayManagerListener_,
301             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
302         displayManagerListener_ = nullptr;
303     }
304     return ret;
305 }
306 
UnregisterDisplayListener(sptr<IDisplayListener> listener)307 DMError DisplayManagerLite::UnregisterDisplayListener(sptr<IDisplayListener> listener)
308 {
309     if (listener == nullptr) {
310         TLOGE(WmsLogTag::DMS, "listener is nullptr");
311         return DMError::DM_ERROR_NULLPTR;
312     }
313     return pImpl_->UnregisterDisplayListener(listener);
314 }
315 
NotifyDisplayCreate(sptr<DisplayInfo> info)316 void DisplayManagerLite::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
317 {
318     std::lock_guard<std::recursive_mutex> lock(mutex_);
319     static_cast<void>(UpdateDisplayInfoLocked(info));
320 }
321 
NotifyDisplayDestroy(DisplayId displayId)322 void DisplayManagerLite::Impl::NotifyDisplayDestroy(DisplayId displayId)
323 {
324     TLOGD(WmsLogTag::DMS, "displayId:%{public}" PRIu64".", displayId);
325     std::lock_guard<std::recursive_mutex> lock(mutex_);
326     displayMap_.erase(displayId);
327 }
328 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)329 void DisplayManagerLite::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
330 {
331     std::lock_guard<std::recursive_mutex> lock(mutex_);
332     static_cast<void>(UpdateDisplayInfoLocked(displayInfo));
333 }
334 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)335 bool DisplayManagerLite::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
336 {
337     if (displayInfo == nullptr) {
338         TLOGE(WmsLogTag::DMS, "displayInfo is null");
339         return false;
340     }
341     DisplayId displayId = displayInfo->GetDisplayId();
342     TLOGD(WmsLogTag::DMS, "displayId:%{public}" PRIu64".", displayId);
343     if (displayId == DISPLAY_ID_INVALID) {
344         TLOGE(WmsLogTag::DMS, "displayId is invalid");
345         return false;
346     }
347     auto iter = displayMap_.find(displayId);
348     if (iter != displayMap_.end() && iter->second != nullptr) {
349         TLOGD(WmsLogTag::DMS, "get screen in screen map");
350         iter->second->UpdateDisplayInfo(displayInfo);
351         return true;
352     }
353     sptr<DisplayLite> display = new DisplayLite("", displayInfo);
354     displayMap_[displayId] = display;
355     return true;
356 }
357 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)358 DMError DisplayManagerLite::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
359 {
360     if (listener == nullptr) {
361         TLOGE(WmsLogTag::DMS, "IFoldStatusListener listener is nullptr.");
362         return DMError::DM_ERROR_NULLPTR;
363     }
364     return pImpl_->RegisterFoldStatusListener(listener);
365 }
366 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)367 DMError DisplayManagerLite::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
368 {
369     std::lock_guard<std::recursive_mutex> lock(mutex_);
370     DMError ret = DMError::DM_OK;
371     if (foldStatusListenerAgent_ == nullptr) {
372         foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
373         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
374             foldStatusListenerAgent_,
375             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
376     }
377     if (ret != DMError::DM_OK) {
378         TLOGW(WmsLogTag::DMS, "failed !");
379         foldStatusListenerAgent_ = nullptr;
380     } else {
381         TLOGI(WmsLogTag::DMS, "IFoldStatusListener register success");
382         foldStatusListeners_.insert(listener);
383     }
384     return ret;
385 }
386 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)387 DMError DisplayManagerLite::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
388 {
389     if (listener == nullptr) {
390         TLOGE(WmsLogTag::DMS, "listener is nullptr.");
391         return DMError::DM_ERROR_NULLPTR;
392     }
393     return pImpl_->UnregisterFoldStatusListener(listener);
394 }
395 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)396 DMError DisplayManagerLite::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
397 {
398     std::lock_guard<std::recursive_mutex> lock(mutex_);
399     auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
400     if (iter == foldStatusListeners_.end()) {
401         TLOGE(WmsLogTag::DMS, "could not find this listener");
402         return DMError::DM_ERROR_NULLPTR;
403     }
404     foldStatusListeners_.erase(iter);
405     DMError ret = DMError::DM_OK;
406     if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
407         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
408             foldStatusListenerAgent_,
409             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
410         foldStatusListenerAgent_ = nullptr;
411     }
412     return ret;
413 }
414 
NotifyFoldStatusChanged(FoldStatus foldStatus)415 void DisplayManagerLite::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
416 {
417     std::set<sptr<IFoldStatusListener>> foldStatusListeners;
418     {
419         std::lock_guard<std::recursive_mutex> lock(mutex_);
420         foldStatusListeners = foldStatusListeners_;
421     }
422     for (auto& listener : foldStatusListeners) {
423         listener->OnFoldStatusChanged(foldStatus);
424     }
425 }
426 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)427 DMError DisplayManagerLite::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
428 {
429     if (listener == nullptr) {
430         TLOGE(WmsLogTag::DMS, "IDisplayModeListener listener is nullptr.");
431         return DMError::DM_ERROR_NULLPTR;
432     }
433     return pImpl_->RegisterDisplayModeListener(listener);
434 }
435 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)436 DMError DisplayManagerLite::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
437 {
438     std::lock_guard<std::recursive_mutex> lock(mutex_);
439     DMError ret = DMError::DM_OK;
440     if (displayModeListenerAgent_ == nullptr) {
441         displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
442         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
443             displayModeListenerAgent_,
444             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
445     }
446     if (ret != DMError::DM_OK) {
447         TLOGW(WmsLogTag::DMS, "RegisterDisplayModeListener failed !");
448         displayModeListenerAgent_ = nullptr;
449     } else {
450         TLOGI(WmsLogTag::DMS, "IDisplayModeListener register success");
451         displayModeListeners_.insert(listener);
452     }
453     return ret;
454 }
455 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)456 DMError DisplayManagerLite::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
457 {
458     if (listener == nullptr) {
459         TLOGE(WmsLogTag::DMS, "listener is nullptr.");
460         return DMError::DM_ERROR_NULLPTR;
461     }
462     return pImpl_->UnregisterDisplayModeListener(listener);
463 }
464 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)465 DMError DisplayManagerLite::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
466 {
467     std::lock_guard<std::recursive_mutex> lock(mutex_);
468     auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
469     if (iter == displayModeListeners_.end()) {
470         TLOGE(WmsLogTag::DMS, "could not find this listener");
471         return DMError::DM_ERROR_NULLPTR;
472     }
473     displayModeListeners_.erase(iter);
474     DMError ret = DMError::DM_OK;
475     if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
476         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
477             displayModeListenerAgent_,
478             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
479         displayModeListenerAgent_ = nullptr;
480     }
481     return ret;
482 }
483 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)484 void DisplayManagerLite::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
485 {
486     std::set<sptr<IDisplayModeListener>> displayModeListeners;
487     {
488         std::lock_guard<std::recursive_mutex> lock(mutex_);
489         displayModeListeners = displayModeListeners_;
490     }
491     for (auto& listener : displayModeListeners) {
492         listener->OnDisplayModeChanged(displayMode);
493     }
494 }
495 
GetFoldStatus()496 FoldStatus DisplayManagerLite::GetFoldStatus()
497 {
498     return pImpl_->GetFoldStatus();
499 }
500 
GetFoldStatus()501 FoldStatus DisplayManagerLite::Impl::GetFoldStatus()
502 {
503     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldStatus();
504 }
505 
GetDefaultDisplay()506 sptr<DisplayLite> DisplayManagerLite::GetDefaultDisplay()
507 {
508     return pImpl_->GetDefaultDisplay();
509 }
510 
GetDefaultDisplay()511 sptr<DisplayLite> DisplayManagerLite::Impl::GetDefaultDisplay()
512 {
513     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
514     if (displayInfo == nullptr) {
515         return nullptr;
516     }
517     auto displayId = displayInfo->GetDisplayId();
518     std::lock_guard<std::recursive_mutex> lock(mutex_);
519     if (!UpdateDisplayInfoLocked(displayInfo)) {
520         displayMap_.erase(displayId);
521         return nullptr;
522     }
523     return displayMap_[displayId];
524 }
525 
IsFoldable()526 bool DisplayManagerLite::IsFoldable()
527 {
528     return pImpl_->IsFoldable();
529 }
530 
IsFoldable()531 bool DisplayManagerLite::Impl::IsFoldable()
532 {
533     return SingletonContainer::Get<DisplayManagerAdapterLite>().IsFoldable();
534 }
535 
GetFoldDisplayMode()536 FoldDisplayMode DisplayManagerLite::GetFoldDisplayMode()
537 {
538     return pImpl_->GetFoldDisplayMode();
539 }
540 
GetFoldDisplayModeForExternal()541 FoldDisplayMode DisplayManagerLite::GetFoldDisplayModeForExternal()
542 {
543     return pImpl_->GetFoldDisplayModeForExternal();
544 }
545 
GetFoldDisplayMode()546 FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayMode()
547 {
548     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
549 }
550 
GetFoldDisplayModeForExternal()551 FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayModeForExternal()
552 {
553     FoldDisplayMode displayMode = SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
554     if (displayMode == FoldDisplayMode::GLOBAL_FULL) {
555         return FoldDisplayMode::FULL;
556     }
557     return displayMode;
558 }
559 
SetFoldDisplayMode(const FoldDisplayMode mode)560 void DisplayManagerLite::SetFoldDisplayMode(const FoldDisplayMode mode)
561 {
562     return pImpl_->SetFoldDisplayMode(mode);
563 }
564 
SetFoldDisplayModeAsync(const FoldDisplayMode mode)565 void DisplayManagerLite::SetFoldDisplayModeAsync(const FoldDisplayMode mode)
566 {
567     return pImpl_->SetFoldDisplayModeAsync(mode);
568 }
569 
SetFoldDisplayMode(const FoldDisplayMode mode)570 void DisplayManagerLite::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
571 {
572     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetFoldDisplayMode(mode);
573 }
574 
SetFoldDisplayModeAsync(const FoldDisplayMode mode)575 void DisplayManagerLite::Impl::SetFoldDisplayModeAsync(const FoldDisplayMode mode)
576 {
577     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetFoldDisplayModeAsync(mode);
578 }
579 
OnRemoteDied()580 void DisplayManagerLite::Impl::OnRemoteDied()
581 {
582     TLOGI(WmsLogTag::DMS, "dms is died");
583     std::lock_guard<std::recursive_mutex> lock(mutex_);
584     displayManagerListener_ = nullptr;
585     foldStatusListenerAgent_ = nullptr;
586     displayModeListenerAgent_ = nullptr;
587 }
588 
OnRemoteDied()589 void DisplayManagerLite::OnRemoteDied()
590 {
591     pImpl_->OnRemoteDied();
592 }
593 
GetDisplayById(DisplayId displayId)594 sptr<DisplayLite> DisplayManagerLite::Impl::GetDisplayById(DisplayId displayId)
595 {
596     TLOGD(WmsLogTag::DMS, "GetDisplayById start, displayId: %{public}" PRIu64" ", displayId);
597     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayInfo(displayId);
598     std::lock_guard<std::recursive_mutex> lock(mutex_);
599     if (!UpdateDisplayInfoLocked(displayInfo)) {
600         displayMap_.erase(displayId);
601         return nullptr;
602     }
603     return displayMap_[displayId];
604 }
605 
GetDisplayById(DisplayId displayId)606 sptr<DisplayLite> DisplayManagerLite::GetDisplayById(DisplayId displayId)
607 {
608     if (destroyed_) {
609         return nullptr;
610     }
611     std::lock_guard<std::recursive_mutex> lock(mutex_);
612     return pImpl_->GetDisplayById(displayId);
613 }
614 
615 /*
616  * used by powermgr
617  */
WakeUpBegin(PowerStateChangeReason reason)618 bool DisplayManagerLite::WakeUpBegin(PowerStateChangeReason reason)
619 {
620     TLOGD(WmsLogTag::DMS, "[UL_POWER]WakeUpBegin start, reason:%{public}u", reason);
621     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpBegin(reason);
622 }
623 
WakeUpEnd()624 bool DisplayManagerLite::WakeUpEnd()
625 {
626     TLOGD(WmsLogTag::DMS, "[UL_POWER]WakeUpEnd start");
627     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpEnd();
628 }
629 
SuspendBegin(PowerStateChangeReason reason)630 bool DisplayManagerLite::SuspendBegin(PowerStateChangeReason reason)
631 {
632     // dms->wms notify other windows to hide
633     TLOGD(WmsLogTag::DMS, "[UL_POWER]SuspendBegin start, reason:%{public}u", reason);
634     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendBegin(reason);
635 }
636 
SuspendEnd()637 bool DisplayManagerLite::SuspendEnd()
638 {
639     TLOGD(WmsLogTag::DMS, "[UL_POWER]SuspendEnd start");
640     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendEnd();
641 }
642 
GetInternalScreenId()643 ScreenId DisplayManagerLite::GetInternalScreenId()
644 {
645     TLOGD(WmsLogTag::DMS, "[UL_POWER]GetInternalScreenId start");
646     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetInternalScreenId();
647 }
648 
SetScreenPowerById(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)649 bool DisplayManagerLite::SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)
650 {
651     TLOGD(WmsLogTag::DMS, "[UL_POWER]SetScreenPowerById start");
652     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenPowerById(screenId, state, reason);
653 }
654 
SetDisplayState(DisplayState state,DisplayStateCallback callback)655 bool DisplayManagerLite::SetDisplayState(DisplayState state, DisplayStateCallback callback)
656 {
657     return pImpl_->SetDisplayState(state, callback);
658 }
659 
GetDisplayState(DisplayId displayId)660 DisplayState DisplayManagerLite::GetDisplayState(DisplayId displayId)
661 {
662     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayState(displayId);
663 }
664 
SetDisplayState(DisplayState state,DisplayStateCallback callback)665 bool DisplayManagerLite::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
666 {
667     TLOGD(WmsLogTag::DMS, "[UL_POWER]state:%{public}u", state);
668     bool ret = true;
669     {
670         std::lock_guard<std::recursive_mutex> lock(mutex_);
671         if (displayStateCallback_ != nullptr || callback == nullptr) {
672             if (displayStateCallback_ != nullptr) {
673                 TLOGI(WmsLogTag::DMS, "[UL_POWER]previous callback not called, the displayStateCallback_ is not null");
674             }
675             if (callback == nullptr) {
676                 TLOGI(WmsLogTag::DMS, "[UL_POWER]Invalid callback received");
677             }
678             return false;
679         }
680         displayStateCallback_ = callback;
681 
682         if (displayStateAgent_ == nullptr) {
683             displayStateAgent_ = new DisplayManagerAgent(this);
684             ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
685                 displayStateAgent_,
686                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
687         }
688     }
689     ret = ret && SingletonContainer::Get<DisplayManagerAdapterLite>().SetDisplayState(state);
690     if (!ret) {
691         ClearDisplayStateCallback();
692     }
693     return ret;
694 }
695 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)696 void DisplayManagerLite::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
697 {
698     TLOGD(WmsLogTag::DMS, "state:%{public}u", state);
699     DisplayStateCallback displayStateCallback = nullptr;
700     {
701         std::lock_guard<std::recursive_mutex> lock(mutex_);
702         displayStateCallback = displayStateCallback_;
703     }
704     if (displayStateCallback) {
705         displayStateCallback(state);
706         ClearDisplayStateCallback();
707         return;
708     }
709     TLOGW(WmsLogTag::DMS, "callback_ target is not set!");
710 }
711 
ClearDisplayStateCallback()712 void DisplayManagerLite::Impl::ClearDisplayStateCallback()
713 {
714     std::lock_guard<std::recursive_mutex> lock(mutex_);
715     TLOGD(WmsLogTag::DMS, "[UL_POWER]Clear displaystatecallback enter");
716     displayStateCallback_ = nullptr;
717     if (displayStateAgent_ != nullptr) {
718         TLOGI(WmsLogTag::DMS, "[UL_POWER]UnregisterDisplayManagerAgent enter and displayStateAgent_ is cleared");
719         SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(displayStateAgent_,
720             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
721         displayStateAgent_ = nullptr;
722     }
723 }
724 
ClearFoldStatusCallback()725 void DisplayManagerLite::Impl::ClearFoldStatusCallback()
726 {
727     DMError ret = DMError::DM_OK;
728     std::lock_guard<std::recursive_mutex> lock(mutex_);
729     if (foldStatusListenerAgent_ != nullptr) {
730         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
731             foldStatusListenerAgent_,
732             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
733         foldStatusListenerAgent_ = nullptr;
734         TLOGI(WmsLogTag::DMS, "foldStatusListenerAgent_ is nullptr !");
735     }
736     if (ret != DMError::DM_OK) {
737         TLOGW(WmsLogTag::DMS, "Unregister failed! Error code: %{public}d", ret);
738     }
739 }
740 
ClearDisplayModeCallback()741 void DisplayManagerLite::Impl::ClearDisplayModeCallback()
742 {
743     DMError ret = DMError::DM_OK;
744     std::lock_guard<std::recursive_mutex> lock(mutex_);
745     if (displayModeListenerAgent_ != nullptr) {
746         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
747             displayModeListenerAgent_,
748             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
749         displayModeListenerAgent_ = nullptr;
750         TLOGI(WmsLogTag::DMS, "displayModeListenerAgent_ is nullptr !");
751     }
752     if (ret != DMError::DM_OK) {
753         TLOGW(WmsLogTag::DMS, "Unregister failed! Error code: %{public}d", ret);
754     }
755 }
756 
RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)757 DMError DisplayManagerLite::RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
758 {
759     if (listener == nullptr) {
760         TLOGE(WmsLogTag::DMS, "IScreenMagneticStateListener listener is nullptr.");
761         return DMError::DM_ERROR_NULLPTR;
762     }
763     return pImpl_->RegisterScreenMagneticStateListener(listener);
764 }
765 
RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)766 DMError DisplayManagerLite::Impl::RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
767 {
768     std::lock_guard<std::recursive_mutex> lock(mutex_);
769     DMError ret = DMError::DM_OK;
770     if (screenMagneticStateListenerAgent_ == nullptr) {
771         screenMagneticStateListenerAgent_ = new DisplayManagerScreenMagneticStateAgent(this);
772         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
773             screenMagneticStateListenerAgent_,
774             DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER);
775     }
776     if (ret != DMError::DM_OK) {
777         TLOGW(WmsLogTag::DMS, "RegisterScreenMagneticStateListener failed !");
778         screenMagneticStateListenerAgent_ = nullptr;
779     } else if (listener != nullptr) {
780         TLOGD(WmsLogTag::DMS, "IScreenMagneticStateListener register success");
781         bool isKeyboardOn = SingletonContainer::Get<DisplayManagerAdapterLite>().GetKeyboardState();
782         TLOGI(WmsLogTag::DMS, "RegisterScreenMagneticStateListener isKeyboardOn : %{public}d", isKeyboardOn);
783         listener->OnScreenMagneticStateChanged(isKeyboardOn);
784         screenMagneticStateListeners_.insert(listener);
785     }
786     return ret;
787 }
788 
UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)789 DMError DisplayManagerLite::UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
790 {
791     if (listener == nullptr) {
792         TLOGE(WmsLogTag::DMS, "UnregisterScreenMagneticStateListener listener is nullptr.");
793         return DMError::DM_ERROR_NULLPTR;
794     }
795     return pImpl_->UnregisterScreenMagneticStateListener(listener);
796 }
797 
UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)798 DMError DisplayManagerLite::Impl::UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
799 {
800     std::lock_guard<std::recursive_mutex> lock(mutex_);
801     auto iter = std::find(screenMagneticStateListeners_.begin(), screenMagneticStateListeners_.end(), listener);
802     if (iter == screenMagneticStateListeners_.end()) {
803         TLOGE(WmsLogTag::DMS, "could not find this listener");
804         return DMError::DM_ERROR_NULLPTR;
805     }
806     screenMagneticStateListeners_.erase(iter);
807     DMError ret = DMError::DM_OK;
808     if (screenMagneticStateListeners_.empty() && screenMagneticStateListenerAgent_ != nullptr) {
809         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
810             screenMagneticStateListenerAgent_,
811             DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER);
812         screenMagneticStateListenerAgent_ = nullptr;
813     }
814     return ret;
815 }
816 
NotifyScreenMagneticStateChanged(bool isMagneticState)817 void DisplayManagerLite::Impl::NotifyScreenMagneticStateChanged(bool isMagneticState)
818 {
819     std::set<sptr<IScreenMagneticStateListener>> screenMagneticStateListeners;
820     {
821         std::lock_guard<std::recursive_mutex> lock(mutex_);
822         screenMagneticStateListeners = screenMagneticStateListeners_;
823     }
824     for (auto& listener : screenMagneticStateListeners) {
825         listener->OnScreenMagneticStateChanged(isMagneticState);
826     }
827 }
828 
TryToCancelScreenOff()829 bool DisplayManagerLite::TryToCancelScreenOff()
830 {
831     TLOGD(WmsLogTag::DMS, "[UL_POWER]TryToCancelScreenOff start");
832     return SingletonContainer::Get<DisplayManagerAdapterLite>().TryToCancelScreenOff();
833 }
834 
SetScreenBrightness(uint64_t screenId,uint32_t level)835 bool DisplayManagerLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
836 {
837     TLOGD(WmsLogTag::DMS, "[UL_POWER]SetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId,
838         level);
839     SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenBrightness(screenId, level);
840     return true;
841 }
842 
GetScreenBrightness(uint64_t screenId) const843 uint32_t DisplayManagerLite::GetScreenBrightness(uint64_t screenId) const
844 {
845     uint32_t level = SingletonContainer::Get<DisplayManagerAdapterLite>().GetScreenBrightness(screenId);
846     TLOGD(WmsLogTag::DMS, "[UL_POWER]GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId,
847         level);
848     return level;
849 }
850 
GetDefaultDisplayId()851 DisplayId DisplayManagerLite::GetDefaultDisplayId()
852 {
853     auto info = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
854     if (info == nullptr) {
855         return DISPLAY_ID_INVALID;
856     }
857     return info->GetDisplayId();
858 }
859 
GetAllDisplayIds()860 std::vector<DisplayId> DisplayManagerLite::GetAllDisplayIds()
861 {
862     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetAllDisplayIds();
863 }
864 
GetVirtualScreenFlag(ScreenId screenId)865 VirtualScreenFlag DisplayManagerLite::GetVirtualScreenFlag(ScreenId screenId)
866 {
867     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetVirtualScreenFlag(screenId);
868 }
869 
SetSystemKeyboardStatus(bool isTpKeyboardOn)870 DMError DisplayManagerLite::SetSystemKeyboardStatus(bool isTpKeyboardOn)
871 {
872     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetSystemKeyboardStatus(isTpKeyboardOn);
873 }
874 } // namespace OHOS::Rosen