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
21namespace OHOS {
22namespace Rosen {
23namespace {
24constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayPowerController"};
25}
26
27bool 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
35bool 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            {
48                std::lock_guard<std::recursive_mutex> lock(mutex_);
49                displayState_ = state;
50            }
51            DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_ON,
52                EventStatus::BEGIN);
53            break;
54        }
55        case DisplayState::OFF: {
56            {
57                std::lock_guard<std::recursive_mutex> lock(mutex_);
58                displayState_ = state;
59            }
60            DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_OFF,
61                EventStatus::BEGIN);
62            break;
63        }
64        default: {
65            WLOGFW("unknown DisplayState!");
66            return false;
67        }
68    }
69    DisplayManagerAgentController::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID, state);
70    return true;
71}
72
73DisplayState DisplayPowerController::GetDisplayState(DisplayId displayId)
74{
75    return displayState_;
76}
77
78void DisplayPowerController::NotifyDisplayEvent(DisplayEvent event)
79{
80    WLOGFI("DisplayEvent:%{public}u", event);
81    if (event == DisplayEvent::UNLOCK) {
82        std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
83        displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_UNLOCK);
84        DisplayManagerAgentController::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DESKTOP_READY,
85            EventStatus::BEGIN);
86        std::lock_guard<std::recursive_mutex> lock(mutex_);
87        isKeyguardDrawn_ = false;
88        return;
89    }
90    if (event == DisplayEvent::KEYGUARD_DRAWN) {
91        std::lock_guard<std::recursive_mutex> lock(mutex_);
92        isKeyguardDrawn_ = true;
93    }
94}
95}
96}