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 "screen_action.h"
17
18 #include <hisysevent.h>
19
20 #include <ipc_skeleton.h>
21 #include "dm_common.h"
22 #include "display_manager.h"
23 #include "display_log.h"
24 #include "screen_manager.h"
25
26 namespace OHOS {
27 namespace DisplayPowerMgr {
ScreenAction(uint32_t displayId)28 ScreenAction::ScreenAction(uint32_t displayId) : displayId_(displayId)
29 {}
30
GetDefaultDisplayId()31 uint32_t ScreenAction::GetDefaultDisplayId()
32 {
33 std::string identity = IPCSkeleton::ResetCallingIdentity();
34 uint64_t defaultId = Rosen::DisplayManager::GetInstance().GetDefaultDisplayId();
35 IPCSkeleton::SetCallingIdentity(identity);
36 return static_cast<uint32_t>(defaultId);
37 }
38
GetAllDisplayId()39 std::vector<uint32_t> ScreenAction::GetAllDisplayId()
40 {
41 std::string identity = IPCSkeleton::ResetCallingIdentity();
42 std::vector<uint64_t> allIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds();
43 IPCSkeleton::SetCallingIdentity(identity);
44 std::vector<uint32_t> displayIds;
45 if (allIds.empty()) {
46 displayIds.push_back(DEFAULT_DISPLAY_ID);
47 return displayIds;
48 }
49 std::transform(allIds.begin(), allIds.end(), back_inserter(displayIds), [](uint64_t id) {
50 return static_cast<uint32_t>(id);
51 });
52 return displayIds;
53 }
54
GetDisplayId()55 uint32_t ScreenAction::GetDisplayId()
56 {
57 return displayId_;
58 }
59
GetDisplayState()60 DisplayState ScreenAction::GetDisplayState()
61 {
62 DisplayState state = DisplayState::DISPLAY_UNKNOWN;
63 Rosen::ScreenPowerState powerState = Rosen::ScreenManager::GetInstance().GetScreenPower(displayId_);
64 DISPLAY_HILOGI(FEAT_STATE, "ScreenPowerState=%{public}d", static_cast<uint32_t>(powerState));
65 switch (powerState) {
66 case Rosen::ScreenPowerState::POWER_ON:
67 state = DisplayState::DISPLAY_ON;
68 break;
69 case Rosen::ScreenPowerState::POWER_STAND_BY:
70 state = DisplayState::DISPLAY_DIM;
71 break;
72 case Rosen::ScreenPowerState::POWER_SUSPEND:
73 state = DisplayState::DISPLAY_SUSPEND;
74 break;
75 case Rosen::ScreenPowerState::POWER_OFF:
76 state = DisplayState::DISPLAY_OFF;
77 break;
78 default:
79 break;
80 }
81 DISPLAY_HILOGI(FEAT_STATE, "state=%{public}u displayId=%{public}u", static_cast<uint32_t>(state), displayId_);
82 return state;
83 }
84
SetDisplayState(DisplayState state,const std::function<void (DisplayState)> & callback)85 bool ScreenAction::SetDisplayState(DisplayState state, const std::function<void(DisplayState)>& callback)
86 {
87 DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState: displayId=%{public}u, state=%{public}u",
88 displayId_, static_cast<uint32_t>(state));
89
90 Rosen::DisplayState rds = Rosen::DisplayState::UNKNOWN;
91 switch (state) {
92 case DisplayState::DISPLAY_ON:
93 rds = Rosen::DisplayState::ON;
94 break;
95 case DisplayState::DISPLAY_OFF:
96 rds = Rosen::DisplayState::OFF;
97 break;
98 default:
99 break;
100 }
101 std::string identity = IPCSkeleton::ResetCallingIdentity();
102 bool ret = Rosen::DisplayManager::GetInstance().SetDisplayState(rds,
103 [callback](Rosen::DisplayState rosenState) {
104 DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState Callback:%{public}d",
105 static_cast<uint32_t>(rosenState));
106 DisplayState state;
107 switch (rosenState) {
108 case Rosen::DisplayState::ON:
109 state = DisplayState::DISPLAY_ON;
110 break;
111 case Rosen::DisplayState::OFF:
112 state = DisplayState::DISPLAY_OFF;
113 break;
114 default:
115 return;
116 }
117 callback(state);
118 });
119 IPCSkeleton::SetCallingIdentity(identity);
120 // Notify screen state change event to battery statistics
121 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "SCREEN_STATE",
122 HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", static_cast<int32_t>(state));
123 DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState:%{public}d", ret);
124 return ret;
125 }
126
SetDisplayPower(DisplayState state,uint32_t reason)127 bool ScreenAction::SetDisplayPower(DisplayState state, uint32_t reason)
128 {
129 DISPLAY_HILOGI(FEAT_STATE, "SetDisplayPower: displayId=%{public}u, state=%{public}u, reason=%{public}u",
130 displayId_, static_cast<uint32_t>(state), reason);
131 Rosen::ScreenPowerState status = Rosen::ScreenPowerState::INVALID_STATE;
132 switch (state) {
133 case DisplayState::DISPLAY_ON:
134 status = Rosen::ScreenPowerState::POWER_ON;
135 break;
136 case DisplayState::DISPLAY_DIM:
137 status = Rosen::ScreenPowerState::POWER_STAND_BY;
138 break;
139 case DisplayState::DISPLAY_SUSPEND:
140 status = Rosen::ScreenPowerState::POWER_SUSPEND;
141 break;
142 case DisplayState::DISPLAY_OFF:
143 status = Rosen::ScreenPowerState::POWER_OFF;
144 break;
145 default:
146 break;
147 }
148 bool ret = Rosen::ScreenManager::GetInstance().SetScreenPowerForAll(status,
149 Rosen::PowerStateChangeReason::POWER_BUTTON);
150 DISPLAY_HILOGI(FEAT_STATE, "SetScreenPowerForAll:%{public}d", ret);
151 return true;
152 }
153
GetBrightness()154 uint32_t ScreenAction::GetBrightness()
155 {
156 std::lock_guard lock(mutexBrightness_);
157 std::string identity = IPCSkeleton::ResetCallingIdentity();
158 brightness_ = Rosen::DisplayManager::GetInstance().GetScreenBrightness(displayId_);
159 IPCSkeleton::SetCallingIdentity(identity);
160 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, brightness_);
161 return brightness_;
162 }
163
SetBrightness(uint32_t value)164 bool ScreenAction::SetBrightness(uint32_t value)
165 {
166 DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, value);
167 // Notify screen brightness change event to battery statistics
168 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "BRIGHTNESS_NIT",
169 HiviewDFX::HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", value);
170 std::string identity = IPCSkeleton::ResetCallingIdentity();
171 bool isSucc = Rosen::DisplayManager::GetInstance().SetScreenBrightness(displayId_, value);
172 IPCSkeleton::SetCallingIdentity(identity);
173 std::lock_guard lock(mutexBrightness_);
174 brightness_ = isSucc ? value : brightness_;
175 return isSucc;
176 }
177 } // namespace DisplayPowerMgr
178 } // namespace OHOS
179