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 "display_power_controller.h"
17 #include "display_manager_service.h"
18 #include "display_manager_agent_controller.h"
19 #include "window_manager_hilog.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayPowerController"};
25 }
26
SuspendBegin(PowerStateChangeReason reason)27 bool DisplayPowerController::SuspendBegin(PowerStateChangeReason reason)
28 {
29 WLOGFI("reason:%{public}u", reason);
30 std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
31 displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_SUSPEND);
32 return true;
33 }
34
SetDisplayState(DisplayState state)35 bool DisplayPowerController::SetDisplayState(DisplayState state)
36 {
37 WLOGFI("state:%{public}u", state);
38 {
39 std::lock_guard<std::recursive_mutex> lock(mutex_);
40 if (displayState_ == state) {
41 WLOGFE("state is already set");
42 return false;
43 }
44 }
45 switch (state) {
46 case DisplayState::ON: {
47 bool isKeyguardDrawn;
48 {
49 std::lock_guard<std::recursive_mutex> lock(mutex_);
50 displayState_ = state;
51 isKeyguardDrawn = isKeyguardDrawn_;
52 }
53 if (!isKeyguardDrawn) {
54 std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
55 displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr,
56 emptyMap, DisplayStateChangeType::BEFORE_UNLOCK);
57 }
58 DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_ON,
59 EventStatus::BEGIN);
60 break;
61 }
62 case DisplayState::OFF: {
63 {
64 std::lock_guard<std::recursive_mutex> lock(mutex_);
65 displayState_ = state;
66 }
67 DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_OFF,
68 EventStatus::BEGIN);
69 break;
70 }
71 default: {
72 WLOGFW("unknown DisplayState!");
73 return false;
74 }
75 }
76 DisplayManagerAgentController::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID, state);
77 return true;
78 }
79
GetDisplayState(DisplayId displayId)80 DisplayState DisplayPowerController::GetDisplayState(DisplayId displayId)
81 {
82 return displayState_;
83 }
84
NotifyDisplayEvent(DisplayEvent event)85 void DisplayPowerController::NotifyDisplayEvent(DisplayEvent event)
86 {
87 WLOGFI("DisplayEvent:%{public}u", event);
88 if (event == DisplayEvent::UNLOCK) {
89 std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
90 displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_UNLOCK);
91 DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DESKTOP_READY,
92 EventStatus::BEGIN);
93 std::lock_guard<std::recursive_mutex> lock(mutex_);
94 isKeyguardDrawn_ = false;
95 return;
96 }
97 if (event == DisplayEvent::KEYGUARD_DRAWN) {
98 std::lock_guard<std::recursive_mutex> lock(mutex_);
99 isKeyguardDrawn_ = true;
100 }
101 }
102 }
103 }