1 /*
2  * Copyright (c) 2023-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 "brightness_action.h"
17 
18 #include <hisysevent.h>
19 #include <ipc_skeleton.h>
20 
21 #include "display_log.h"
22 #include "display_manager_lite.h"
23 #include "dm_common.h"
24 #include "screen_manager_lite.h"
25 
26 namespace OHOS {
27 namespace DisplayPowerMgr {
BrightnessAction(uint32_t displayId)28 BrightnessAction::BrightnessAction(uint32_t displayId) : mDisplayId(displayId)
29 {}
30 
GetDisplayId()31 uint32_t BrightnessAction::GetDisplayId()
32 {
33     return mDisplayId;
34 }
35 
SetDisplayId(uint32_t displayId)36 void BrightnessAction::SetDisplayId(uint32_t displayId)
37 {
38     mDisplayId = displayId;
39 }
40 
GetDisplayState()41 DisplayState BrightnessAction::GetDisplayState()
42 {
43     DisplayState state = DisplayState::DISPLAY_UNKNOWN;
44     Rosen::ScreenPowerState powerState = Rosen::ScreenManagerLite::GetInstance().GetScreenPower(mDisplayId);
45     DISPLAY_HILOGI(FEAT_STATE, "ScreenPowerState=%{public}d", static_cast<uint32_t>(powerState));
46     switch (powerState) {
47         case Rosen::ScreenPowerState::POWER_ON:
48             state = DisplayState::DISPLAY_ON;
49             break;
50         case Rosen::ScreenPowerState::POWER_STAND_BY:
51             state = DisplayState::DISPLAY_DIM;
52             break;
53         case Rosen::ScreenPowerState::POWER_SUSPEND:
54             state = DisplayState::DISPLAY_SUSPEND;
55             break;
56         case Rosen::ScreenPowerState::POWER_OFF:
57             state = DisplayState::DISPLAY_OFF;
58             break;
59         default:
60             break;
61     }
62     DISPLAY_HILOGI(FEAT_STATE, "state=%{public}u displayId=%{public}u", static_cast<uint32_t>(state), mDisplayId);
63     return state;
64 }
65 
GetBrightness()66 uint32_t BrightnessAction::GetBrightness()
67 {
68     std::lock_guard lock(mMutexBrightness);
69     std::string identity = IPCSkeleton::ResetCallingIdentity();
70     mBrightness = Rosen::DisplayManagerLite::GetInstance().GetScreenBrightness(mDisplayId);
71     IPCSkeleton::SetCallingIdentity(identity);
72     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", mDisplayId, mBrightness);
73     return mBrightness;
74 }
75 
SetBrightness(uint32_t value)76 bool BrightnessAction::SetBrightness(uint32_t value)
77 {
78     return SetBrightness(mDisplayId, value);
79 }
80 
SetBrightness(uint32_t displayId,uint32_t value)81 bool BrightnessAction::SetBrightness(uint32_t displayId, uint32_t value)
82 {
83     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetBrightness mDisplayId=%{public}u, displayId=%{public}u ,brightness=%{public}u",
84         mDisplayId, displayId, value);
85     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId, value);
86     std::string identity = IPCSkeleton::ResetCallingIdentity();
87     bool isSucc = Rosen::DisplayManagerLite::GetInstance().SetScreenBrightness(displayId, value);
88     IPCSkeleton::SetCallingIdentity(identity);
89     std::lock_guard lock(mMutexBrightness);
90     mBrightness = isSucc ? value : mBrightness;
91     return isSucc;
92 }
93 } // namespace DisplayPowerMgr
94 } // namespace OHOS
95