• 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 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerLite"};
30 }
31 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerLite)
32 
33 class DisplayManagerLite::Impl : public RefBase {
34 public:
Impl(std::recursive_mutex & mutex)35     Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
36     ~Impl();
37     static inline SingletonDelegator<DisplayManagerLite> delegator;
38     sptr<DisplayLite> GetDefaultDisplay();
39     FoldStatus GetFoldStatus();
40     FoldDisplayMode GetFoldDisplayMode();
41     FoldDisplayMode GetFoldDisplayModeForExternal();
42     void SetFoldDisplayMode(const FoldDisplayMode);
43     bool IsFoldable();
44 
45     DMError RegisterDisplayListener(sptr<IDisplayListener> listener);
46     DMError UnregisterDisplayListener(sptr<IDisplayListener> listener);
47     DMError RegisterFoldStatusListener(sptr<IFoldStatusListener> listener);
48     DMError UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener);
49     DMError RegisterDisplayModeListener(sptr<IDisplayModeListener> listener);
50     DMError UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener);
51     DMError RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener);
52     DMError UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener);
53     void OnRemoteDied();
54     sptr<DisplayLite> GetDisplayById(DisplayId displayId);
55     /*
56      * used by powermgr
57      */
58     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
59 private:
60     void NotifyDisplayCreate(sptr<DisplayInfo> info);
61     void NotifyDisplayDestroy(DisplayId);
62     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
63     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
64     void NotifyFoldStatusChanged(FoldStatus foldStatus);
65     void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
66     /*
67      * used by powermgr
68      */
69     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
70     void ClearDisplayStateCallback();
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             WLOGFE("onDisplayCreate: displayInfo is nullptr");
106             return;
107         }
108         if (pImpl_ == nullptr) {
109             WLOGFE("onDisplayCreate: 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             WLOGFE("onDisplayDestroy: displayId is invalid");
127             return;
128         }
129         if (pImpl_ == nullptr) {
130             WLOGFE("onDisplayDestroy: 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             WLOGFE("onDisplayChange: displayInfo is nullptr");
148             return;
149         }
150         if (pImpl_ == nullptr) {
151             WLOGFE("onDisplayChange: pImpl_ is nullptr");
152             return;
153         }
154         WLOGD("onDisplayChange: 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         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed");
239     }
240     ClearDisplayStateCallback();
241 }
242 
~Impl()243 DisplayManagerLite::Impl::~Impl()
244 {
245     Clear();
246 }
247 
DisplayManagerLite()248 DisplayManagerLite::DisplayManagerLite() : pImpl_(new Impl(mutex_))
249 {
250 }
251 
~DisplayManagerLite()252 DisplayManagerLite::~DisplayManagerLite()
253 {
254     std::lock_guard<std::recursive_mutex> lock(mutex_);
255     destroyed_ = true;
256 }
257 
RegisterDisplayListener(sptr<IDisplayListener> listener)258 DMError DisplayManagerLite::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
259 {
260     std::lock_guard<std::recursive_mutex> lock(mutex_);
261     DMError ret = DMError::DM_OK;
262     if (displayManagerListener_ == nullptr) {
263         displayManagerListener_ = new DisplayManagerListener(this);
264         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
265             displayManagerListener_,
266             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
267     }
268     if (ret != DMError::DM_OK) {
269         WLOGFW("RegisterDisplayManagerAgent failed");
270         displayManagerListener_ = nullptr;
271     } else {
272         displayListeners_.insert(listener);
273     }
274     return ret;
275 }
276 
RegisterDisplayListener(sptr<IDisplayListener> listener)277 DMError DisplayManagerLite::RegisterDisplayListener(sptr<IDisplayListener> listener)
278 {
279     if (listener == nullptr) {
280         WLOGFE("RegisterDisplayListener listener is nullptr");
281         return DMError::DM_ERROR_NULLPTR;
282     }
283     return pImpl_->RegisterDisplayListener(listener);
284 }
285 
UnregisterDisplayListener(sptr<IDisplayListener> listener)286 DMError DisplayManagerLite::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
287 {
288     std::lock_guard<std::recursive_mutex> lock(mutex_);
289     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
290     if (iter == displayListeners_.end()) {
291         WLOGFE("could not find this listener");
292         return DMError::DM_ERROR_NULLPTR;
293     }
294     displayListeners_.erase(iter);
295     DMError ret = DMError::DM_OK;
296     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
297         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
298             displayManagerListener_,
299             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
300         displayManagerListener_ = nullptr;
301     }
302     return ret;
303 }
304 
UnregisterDisplayListener(sptr<IDisplayListener> listener)305 DMError DisplayManagerLite::UnregisterDisplayListener(sptr<IDisplayListener> listener)
306 {
307     if (listener == nullptr) {
308         WLOGFE("UnregisterDisplayListener listener is nullptr");
309         return DMError::DM_ERROR_NULLPTR;
310     }
311     return pImpl_->UnregisterDisplayListener(listener);
312 }
313 
NotifyDisplayCreate(sptr<DisplayInfo> info)314 void DisplayManagerLite::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
315 {
316     std::lock_guard<std::recursive_mutex> lock(mutex_);
317     static_cast<void>(UpdateDisplayInfoLocked(info));
318 }
319 
NotifyDisplayDestroy(DisplayId displayId)320 void DisplayManagerLite::Impl::NotifyDisplayDestroy(DisplayId displayId)
321 {
322     WLOGFD("displayId:%{public}" PRIu64".", displayId);
323     std::lock_guard<std::recursive_mutex> lock(mutex_);
324     displayMap_.erase(displayId);
325 }
326 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)327 void DisplayManagerLite::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
328 {
329     std::lock_guard<std::recursive_mutex> lock(mutex_);
330     static_cast<void>(UpdateDisplayInfoLocked(displayInfo));
331 }
332 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)333 bool DisplayManagerLite::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
334 {
335     if (displayInfo == nullptr) {
336         WLOGFE("displayInfo is null");
337         return false;
338     }
339     DisplayId displayId = displayInfo->GetDisplayId();
340     WLOGFD("displayId:%{public}" PRIu64".", displayId);
341     if (displayId == DISPLAY_ID_INVALID) {
342         WLOGFE("displayId is invalid");
343         return false;
344     }
345     auto iter = displayMap_.find(displayId);
346     if (iter != displayMap_.end() && iter->second != nullptr) {
347         WLOGFD("get screen in screen map");
348         iter->second->UpdateDisplayInfo(displayInfo);
349         return true;
350     }
351     sptr<DisplayLite> display = new DisplayLite("", displayInfo);
352     displayMap_[displayId] = display;
353     return true;
354 }
355 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)356 DMError DisplayManagerLite::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
357 {
358     if (listener == nullptr) {
359         WLOGFE("IFoldStatusListener listener is nullptr.");
360         return DMError::DM_ERROR_NULLPTR;
361     }
362     return pImpl_->RegisterFoldStatusListener(listener);
363 }
364 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)365 DMError DisplayManagerLite::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
366 {
367     std::lock_guard<std::recursive_mutex> lock(mutex_);
368     DMError ret = DMError::DM_OK;
369     if (foldStatusListenerAgent_ == nullptr) {
370         foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
371         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
372             foldStatusListenerAgent_,
373             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
374     }
375     if (ret != DMError::DM_OK) {
376         WLOGFW("RegisterFoldStatusListener failed !");
377         foldStatusListenerAgent_ = nullptr;
378     } else {
379         WLOGI("IFoldStatusListener register success");
380         foldStatusListeners_.insert(listener);
381     }
382     return ret;
383 }
384 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)385 DMError DisplayManagerLite::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
386 {
387     if (listener == nullptr) {
388         WLOGFE("UnregisterFoldStatusListener listener is nullptr.");
389         return DMError::DM_ERROR_NULLPTR;
390     }
391     return pImpl_->UnregisterFoldStatusListener(listener);
392 }
393 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)394 DMError DisplayManagerLite::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
395 {
396     std::lock_guard<std::recursive_mutex> lock(mutex_);
397     auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
398     if (iter == foldStatusListeners_.end()) {
399         WLOGFE("could not find this listener");
400         return DMError::DM_ERROR_NULLPTR;
401     }
402     foldStatusListeners_.erase(iter);
403     DMError ret = DMError::DM_OK;
404     if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
405         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
406             foldStatusListenerAgent_,
407             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
408         foldStatusListenerAgent_ = nullptr;
409     }
410     return ret;
411 }
412 
NotifyFoldStatusChanged(FoldStatus foldStatus)413 void DisplayManagerLite::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
414 {
415     std::set<sptr<IFoldStatusListener>> foldStatusListeners;
416     {
417         std::lock_guard<std::recursive_mutex> lock(mutex_);
418         foldStatusListeners = foldStatusListeners_;
419     }
420     for (auto& listener : foldStatusListeners) {
421         listener->OnFoldStatusChanged(foldStatus);
422     }
423 }
424 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)425 DMError DisplayManagerLite::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
426 {
427     if (listener == nullptr) {
428         WLOGFE("IDisplayModeListener listener is nullptr.");
429         return DMError::DM_ERROR_NULLPTR;
430     }
431     return pImpl_->RegisterDisplayModeListener(listener);
432 }
433 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)434 DMError DisplayManagerLite::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
435 {
436     std::lock_guard<std::recursive_mutex> lock(mutex_);
437     DMError ret = DMError::DM_OK;
438     if (displayModeListenerAgent_ == nullptr) {
439         displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
440         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
441             displayModeListenerAgent_,
442             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
443     }
444     if (ret != DMError::DM_OK) {
445         WLOGFW("RegisterDisplayModeListener failed !");
446         displayModeListenerAgent_ = nullptr;
447     } else {
448         WLOGI("IDisplayModeListener register success");
449         displayModeListeners_.insert(listener);
450     }
451     return ret;
452 }
453 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)454 DMError DisplayManagerLite::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
455 {
456     if (listener == nullptr) {
457         WLOGFE("UnregisterDisplayModeListener listener is nullptr.");
458         return DMError::DM_ERROR_NULLPTR;
459     }
460     return pImpl_->UnregisterDisplayModeListener(listener);
461 }
462 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)463 DMError DisplayManagerLite::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
464 {
465     std::lock_guard<std::recursive_mutex> lock(mutex_);
466     auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
467     if (iter == displayModeListeners_.end()) {
468         WLOGFE("could not find this listener");
469         return DMError::DM_ERROR_NULLPTR;
470     }
471     displayModeListeners_.erase(iter);
472     DMError ret = DMError::DM_OK;
473     if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
474         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
475             displayModeListenerAgent_,
476             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
477         displayModeListenerAgent_ = nullptr;
478     }
479     return ret;
480 }
481 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)482 void DisplayManagerLite::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
483 {
484     std::set<sptr<IDisplayModeListener>> displayModeListeners;
485     {
486         std::lock_guard<std::recursive_mutex> lock(mutex_);
487         displayModeListeners = displayModeListeners_;
488     }
489     for (auto& listener : displayModeListeners) {
490         listener->OnDisplayModeChanged(displayMode);
491     }
492 }
493 
GetFoldStatus()494 FoldStatus DisplayManagerLite::GetFoldStatus()
495 {
496     return pImpl_->GetFoldStatus();
497 }
498 
GetFoldStatus()499 FoldStatus DisplayManagerLite::Impl::GetFoldStatus()
500 {
501     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldStatus();
502 }
503 
GetDefaultDisplay()504 sptr<DisplayLite> DisplayManagerLite::GetDefaultDisplay()
505 {
506     return pImpl_->GetDefaultDisplay();
507 }
508 
GetDefaultDisplay()509 sptr<DisplayLite> DisplayManagerLite::Impl::GetDefaultDisplay()
510 {
511     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
512     if (displayInfo == nullptr) {
513         return nullptr;
514     }
515     auto displayId = displayInfo->GetDisplayId();
516     std::lock_guard<std::recursive_mutex> lock(mutex_);
517     if (!UpdateDisplayInfoLocked(displayInfo)) {
518         displayMap_.erase(displayId);
519         return nullptr;
520     }
521     return displayMap_[displayId];
522 }
523 
IsFoldable()524 bool DisplayManagerLite::IsFoldable()
525 {
526     return pImpl_->IsFoldable();
527 }
528 
IsFoldable()529 bool DisplayManagerLite::Impl::IsFoldable()
530 {
531     return SingletonContainer::Get<DisplayManagerAdapterLite>().IsFoldable();
532 }
533 
GetFoldDisplayMode()534 FoldDisplayMode DisplayManagerLite::GetFoldDisplayMode()
535 {
536     return pImpl_->GetFoldDisplayMode();
537 }
538 
GetFoldDisplayModeForExternal()539 FoldDisplayMode DisplayManagerLite::GetFoldDisplayModeForExternal()
540 {
541     return pImpl_->GetFoldDisplayModeForExternal();
542 }
543 
GetFoldDisplayMode()544 FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayMode()
545 {
546     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
547 }
548 
GetFoldDisplayModeForExternal()549 FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayModeForExternal()
550 {
551     FoldDisplayMode displayMode = SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
552     if (displayMode == FoldDisplayMode::GLOBAL_FULL) {
553         return FoldDisplayMode::FULL;
554     }
555     return displayMode;
556 }
557 
SetFoldDisplayMode(const FoldDisplayMode mode)558 void DisplayManagerLite::SetFoldDisplayMode(const FoldDisplayMode mode)
559 {
560     return pImpl_->SetFoldDisplayMode(mode);
561 }
562 
SetFoldDisplayMode(const FoldDisplayMode mode)563 void DisplayManagerLite::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
564 {
565     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetFoldDisplayMode(mode);
566 }
567 
OnRemoteDied()568 void DisplayManagerLite::Impl::OnRemoteDied()
569 {
570     WLOGFI("dms is died");
571     std::lock_guard<std::recursive_mutex> lock(mutex_);
572     displayManagerListener_ = nullptr;
573 }
574 
OnRemoteDied()575 void DisplayManagerLite::OnRemoteDied()
576 {
577     pImpl_->OnRemoteDied();
578 }
579 
GetDisplayById(DisplayId displayId)580 sptr<DisplayLite> DisplayManagerLite::Impl::GetDisplayById(DisplayId displayId)
581 {
582     WLOGFD("GetDisplayById start, displayId: %{public}" PRIu64" ", displayId);
583     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayInfo(displayId);
584     std::lock_guard<std::recursive_mutex> lock(mutex_);
585     if (!UpdateDisplayInfoLocked(displayInfo)) {
586         displayMap_.erase(displayId);
587         return nullptr;
588     }
589     return displayMap_[displayId];
590 }
591 
GetDisplayById(DisplayId displayId)592 sptr<DisplayLite> DisplayManagerLite::GetDisplayById(DisplayId displayId)
593 {
594     if (destroyed_) {
595         return nullptr;
596     }
597     std::lock_guard<std::recursive_mutex> lock(mutex_);
598     return pImpl_->GetDisplayById(displayId);
599 }
600 
601 /*
602  * used by powermgr
603  */
WakeUpBegin(PowerStateChangeReason reason)604 bool DisplayManagerLite::WakeUpBegin(PowerStateChangeReason reason)
605 {
606     WLOGFD("[UL_POWER]WakeUpBegin start, reason:%{public}u", reason);
607     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpBegin(reason);
608 }
609 
WakeUpEnd()610 bool DisplayManagerLite::WakeUpEnd()
611 {
612     WLOGFD("[UL_POWER]WakeUpEnd start");
613     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpEnd();
614 }
615 
SuspendBegin(PowerStateChangeReason reason)616 bool DisplayManagerLite::SuspendBegin(PowerStateChangeReason reason)
617 {
618     // dms->wms notify other windows to hide
619     WLOGFD("[UL_POWER]SuspendBegin start, reason:%{public}u", reason);
620     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendBegin(reason);
621 }
622 
SuspendEnd()623 bool DisplayManagerLite::SuspendEnd()
624 {
625     WLOGFD("[UL_POWER]SuspendEnd start");
626     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendEnd();
627 }
628 
GetInternalScreenId()629 ScreenId DisplayManagerLite::GetInternalScreenId()
630 {
631     WLOGFD("[UL_POWER]GetInternalScreenId start");
632     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetInternalScreenId();
633 }
634 
SetScreenPowerById(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)635 bool DisplayManagerLite::SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)
636 {
637     WLOGFD("[UL_POWER]SetScreenPowerById start");
638     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenPowerById(screenId, state, reason);
639 }
640 
SetDisplayState(DisplayState state,DisplayStateCallback callback)641 bool DisplayManagerLite::SetDisplayState(DisplayState state, DisplayStateCallback callback)
642 {
643     return pImpl_->SetDisplayState(state, callback);
644 }
645 
GetDisplayState(DisplayId displayId)646 DisplayState DisplayManagerLite::GetDisplayState(DisplayId displayId)
647 {
648     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayState(displayId);
649 }
650 
SetDisplayState(DisplayState state,DisplayStateCallback callback)651 bool DisplayManagerLite::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
652 {
653     WLOGFD("[UL_POWER]state:%{public}u", state);
654     bool ret = true;
655     {
656         std::lock_guard<std::recursive_mutex> lock(mutex_);
657         if (displayStateCallback_ != nullptr || callback == nullptr) {
658             if (displayStateCallback_ != nullptr) {
659                 WLOGFI("[UL_POWER]previous callback not called, the displayStateCallback_ is not null");
660             }
661             if (callback == nullptr) {
662                 WLOGFI("[UL_POWER]Invalid callback received");
663             }
664             return false;
665         }
666         displayStateCallback_ = callback;
667 
668         if (displayStateAgent_ == nullptr) {
669             displayStateAgent_ = new DisplayManagerAgent(this);
670             ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
671                 displayStateAgent_,
672                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
673         }
674     }
675     ret = ret && SingletonContainer::Get<DisplayManagerAdapterLite>().SetDisplayState(state);
676     if (!ret) {
677         ClearDisplayStateCallback();
678     }
679     return ret;
680 }
681 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)682 void DisplayManagerLite::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
683 {
684     WLOGFD("state:%{public}u", state);
685     DisplayStateCallback displayStateCallback = nullptr;
686     {
687         std::lock_guard<std::recursive_mutex> lock(mutex_);
688         displayStateCallback = displayStateCallback_;
689     }
690     if (displayStateCallback) {
691         displayStateCallback(state);
692         ClearDisplayStateCallback();
693         return;
694     }
695     WLOGFW("callback_ target is not set!");
696 }
697 
ClearDisplayStateCallback()698 void DisplayManagerLite::Impl::ClearDisplayStateCallback()
699 {
700     std::lock_guard<std::recursive_mutex> lock(mutex_);
701     WLOGFD("[UL_POWER]Clear displaystatecallback enter");
702     displayStateCallback_ = nullptr;
703     if (displayStateAgent_ != nullptr) {
704         WLOGFI("[UL_POWER]UnregisterDisplayManagerAgent enter and displayStateAgent_ is cleared");
705         SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(displayStateAgent_,
706             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
707         displayStateAgent_ = nullptr;
708     }
709 }
710 
RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)711 DMError DisplayManagerLite::RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
712 {
713     if (listener == nullptr) {
714         WLOGFE("IScreenMagneticStateListener listener is nullptr.");
715         return DMError::DM_ERROR_NULLPTR;
716     }
717     return pImpl_->RegisterScreenMagneticStateListener(listener);
718 }
719 
RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)720 DMError DisplayManagerLite::Impl::RegisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
721 {
722     std::lock_guard<std::recursive_mutex> lock(mutex_);
723     DMError ret = DMError::DM_OK;
724     if (screenMagneticStateListenerAgent_ == nullptr) {
725         screenMagneticStateListenerAgent_ = new DisplayManagerScreenMagneticStateAgent(this);
726         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
727             screenMagneticStateListenerAgent_,
728             DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER);
729     }
730     if (ret != DMError::DM_OK) {
731         WLOGFW("RegisterScreenMagneticStateListener failed !");
732         screenMagneticStateListenerAgent_ = nullptr;
733     } else {
734         WLOGD("IScreenMagneticStateListener register success");
735         screenMagneticStateListeners_.insert(listener);
736     }
737     return ret;
738 }
739 
UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)740 DMError DisplayManagerLite::UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
741 {
742     if (listener == nullptr) {
743         WLOGFE("UnregisterScreenMagneticStateListener listener is nullptr.");
744         return DMError::DM_ERROR_NULLPTR;
745     }
746     return pImpl_->UnregisterScreenMagneticStateListener(listener);
747 }
748 
UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)749 DMError DisplayManagerLite::Impl::UnregisterScreenMagneticStateListener(sptr<IScreenMagneticStateListener> listener)
750 {
751     std::lock_guard<std::recursive_mutex> lock(mutex_);
752     auto iter = std::find(screenMagneticStateListeners_.begin(), screenMagneticStateListeners_.end(), listener);
753     if (iter == screenMagneticStateListeners_.end()) {
754         WLOGFE("could not find this listener");
755         return DMError::DM_ERROR_NULLPTR;
756     }
757     screenMagneticStateListeners_.erase(iter);
758     DMError ret = DMError::DM_OK;
759     if (screenMagneticStateListeners_.empty() && screenMagneticStateListenerAgent_ != nullptr) {
760         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
761             screenMagneticStateListenerAgent_,
762             DisplayManagerAgentType::SCREEN_MAGNETIC_STATE_CHANGED_LISTENER);
763         screenMagneticStateListenerAgent_ = nullptr;
764     }
765     return ret;
766 }
767 
NotifyScreenMagneticStateChanged(bool isMagneticState)768 void DisplayManagerLite::Impl::NotifyScreenMagneticStateChanged(bool isMagneticState)
769 {
770     std::set<sptr<IScreenMagneticStateListener>> screenMagneticStateListeners;
771     {
772         std::lock_guard<std::recursive_mutex> lock(mutex_);
773         screenMagneticStateListeners = screenMagneticStateListeners_;
774     }
775     for (auto& listener : screenMagneticStateListeners) {
776         listener->OnScreenMagneticStateChanged(isMagneticState);
777     }
778 }
779 
TryToCancelScreenOff()780 bool DisplayManagerLite::TryToCancelScreenOff()
781 {
782     WLOGFD("[UL_POWER]TryToCancelScreenOff start");
783     return SingletonContainer::Get<DisplayManagerAdapterLite>().TryToCancelScreenOff();
784 }
785 
SetScreenBrightness(uint64_t screenId,uint32_t level)786 bool DisplayManagerLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
787 {
788     WLOGFD("[UL_POWER]SetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
789     SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenBrightness(screenId, level);
790     return true;
791 }
792 
GetScreenBrightness(uint64_t screenId) const793 uint32_t DisplayManagerLite::GetScreenBrightness(uint64_t screenId) const
794 {
795     uint32_t level = SingletonContainer::Get<DisplayManagerAdapterLite>().GetScreenBrightness(screenId);
796     WLOGFD("[UL_POWER]GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
797     return level;
798 }
799 
GetDefaultDisplayId()800 DisplayId DisplayManagerLite::GetDefaultDisplayId()
801 {
802     auto info = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
803     if (info == nullptr) {
804         return DISPLAY_ID_INVALID;
805     }
806     return info->GetDisplayId();
807 }
808 
GetAllDisplayIds()809 std::vector<DisplayId> DisplayManagerLite::GetAllDisplayIds()
810 {
811     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetAllDisplayIds();
812 }
813 
GetVirtualScreenFlag(ScreenId screenId)814 VirtualScreenFlag DisplayManagerLite::GetVirtualScreenFlag(ScreenId screenId)
815 {
816     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetVirtualScreenFlag(screenId);
817 }
818 
SetSystemKeyboardStatus(bool isOn)819 DMError DisplayManagerLite::SetSystemKeyboardStatus(bool isOn)
820 {
821     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetSystemKeyboardStatus(isOn);
822 }
823 } // namespace OHOS::Rosen