aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/hle/service/am/lifecycle_manager.h
blob: 7c70434a18dc6d9e6a3eedb339e7ca40213ba481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <list>

#include "core/hle/service/am/am_types.h"
#include "core/hle/service/os/event.h"

namespace Core {
class System;
}

namespace Service::AM {

enum class ActivityState : u32 {
    ForegroundVisible = 0,
    ForegroundObscured = 1,
    BackgroundVisible = 2,
    BackgroundObscured = 3,
};

enum class FocusHandlingMode : u32 {
    AlwaysSuspend = 0,
    SuspendHomeSleep = 1,
    NoSuspend = 2,
};

enum class SuspendMode : u32 {
    NoOverride = 0,
    ForceResume = 1,
    ForceSuspend = 2,
};

class LifecycleManager {
public:
    explicit LifecycleManager(Core::System& system, KernelHelpers::ServiceContext& context,
                              bool is_application);
    ~LifecycleManager();

public:
    Event& GetSystemEvent();
    Event& GetOperationModeChangedSystemEvent();

public:
    bool IsApplication() {
        return m_is_application;
    }

    bool GetForcedSuspend() {
        return m_forced_suspend;
    }

    bool GetExitRequested() {
        return m_has_requested_exit;
    }

    ActivityState GetActivityState() {
        return m_activity_state;
    }

    FocusState GetAndClearFocusState() {
        m_acknowledged_focus_state = m_requested_focus_state;
        return m_acknowledged_focus_state;
    }

    void SetFocusState(FocusState state) {
        if (m_requested_focus_state != state) {
            m_has_focus_state_changed = true;
        }
        m_requested_focus_state = state;
        this->SignalSystemEventIfNeeded();
    }

    void RequestExit() {
        m_has_requested_exit = true;
        this->SignalSystemEventIfNeeded();
    }

    void RequestResumeNotification() {
        // NOTE: this appears to be a bug in am.
        // If an applet makes a concurrent request to receive resume notifications
        // while it is being suspended, the first resume notification will be lost.
        // This is not the case with other notification types.
        if (m_resume_notification_enabled) {
            m_has_resume = true;
        }
    }

    void OnOperationAndPerformanceModeChanged();

public:
    void SetFocusStateChangedNotificationEnabled(bool enabled) {
        m_focus_state_changed_notification_enabled = enabled;
        this->SignalSystemEventIfNeeded();
    }

    void SetOperationModeChangedNotificationEnabled(bool enabled) {
        m_operation_mode_changed_notification_enabled = enabled;
        this->SignalSystemEventIfNeeded();
    }

    void SetPerformanceModeChangedNotificationEnabled(bool enabled) {
        m_performance_mode_changed_notification_enabled = enabled;
        this->SignalSystemEventIfNeeded();
    }

    void SetResumeNotificationEnabled(bool enabled) {
        m_resume_notification_enabled = enabled;
    }

    void SetActivityState(ActivityState state) {
        m_activity_state = state;
    }

    void SetSuspendMode(SuspendMode mode) {
        m_suspend_mode = mode;
    }

    void SetForcedSuspend(bool enabled) {
        m_forced_suspend = enabled;
    }

public:
    void SetFocusHandlingMode(bool suspend);
    void SetOutOfFocusSuspendingEnabled(bool enabled);
    void RemoveForceResumeIfPossible();
    bool IsRunnable() const;
    bool UpdateRequestedFocusState();
    void SignalSystemEventIfNeeded();

public:
    void PushUnorderedMessage(AppletMessage message);
    bool PopMessage(AppletMessage* out_message);

private:
    FocusState GetFocusStateWhileForegroundObscured() const;
    FocusState GetFocusStateWhileBackground(bool is_obscured) const;

private:
    AppletMessage PopMessageInOrderOfPriority();
    bool ShouldSignalSystemEvent();

private:
    Event m_system_event;
    Event m_operation_mode_changed_system_event;

    std::list<AppletMessage> m_unordered_messages{};

    bool m_is_application{};
    bool m_focus_state_changed_notification_enabled{true};
    bool m_operation_mode_changed_notification_enabled{true};
    bool m_performance_mode_changed_notification_enabled{true};
    bool m_resume_notification_enabled{};

    bool m_requested_request_to_display_state{};
    bool m_acknowledged_request_to_display_state{};
    bool m_has_resume{};
    bool m_has_focus_state_changed{true};
    bool m_has_album_recording_saved{};
    bool m_has_album_screen_shot_taken{};
    bool m_has_auto_power_down{};
    bool m_has_sleep_required_by_low_battery{};
    bool m_has_sleep_required_by_high_temperature{};
    bool m_has_sd_card_removed{};
    bool m_has_performance_mode_changed{};
    bool m_has_operation_mode_changed{};
    bool m_has_requested_request_to_prepare_sleep{};
    bool m_has_acknowledged_request_to_prepare_sleep{};
    bool m_has_requested_exit{};
    bool m_has_acknowledged_exit{};
    bool m_applet_message_available{};

    bool m_forced_suspend{};
    FocusHandlingMode m_focus_handling_mode{FocusHandlingMode::SuspendHomeSleep};
    ActivityState m_activity_state{ActivityState::ForegroundVisible};
    SuspendMode m_suspend_mode{SuspendMode::NoOverride};
    FocusState m_requested_focus_state{};
    FocusState m_acknowledged_focus_state{};
};

} // namespace Service::AM