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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/assert.h"
#include "core/hle/service/am/lifecycle_manager.h"
namespace Service::AM {
LifecycleManager::LifecycleManager(Core::System& system, KernelHelpers::ServiceContext& context,
bool is_application)
: m_system_event(context), m_operation_mode_changed_system_event(context),
m_is_application(is_application) {}
LifecycleManager::~LifecycleManager() = default;
Event& LifecycleManager::GetSystemEvent() {
return m_system_event;
}
Event& LifecycleManager::GetOperationModeChangedSystemEvent() {
return m_operation_mode_changed_system_event;
}
void LifecycleManager::PushUnorderedMessage(AppletMessage message) {
m_unordered_messages.push_back(message);
this->SignalSystemEventIfNeeded();
}
AppletMessage LifecycleManager::PopMessageInOrderOfPriority() {
if (m_has_resume) {
m_has_resume = false;
return AppletMessage::Resume;
}
if (m_has_acknowledged_exit != m_has_requested_exit) {
m_has_acknowledged_exit = m_has_requested_exit;
return AppletMessage::Exit;
}
if (m_focus_state_changed_notification_enabled) {
if (!m_is_application) {
if (m_requested_focus_state != m_acknowledged_focus_state) {
m_acknowledged_focus_state = m_requested_focus_state;
switch (m_requested_focus_state) {
case FocusState::InFocus:
return AppletMessage::ChangeIntoForeground;
case FocusState::NotInFocus:
return AppletMessage::ChangeIntoBackground;
default:
ASSERT(false);
}
}
} else if (m_has_focus_state_changed) {
m_has_focus_state_changed = false;
return AppletMessage::FocusStateChanged;
}
}
if (m_has_requested_request_to_prepare_sleep != m_has_acknowledged_request_to_prepare_sleep) {
m_has_acknowledged_request_to_prepare_sleep = true;
return AppletMessage::RequestToPrepareSleep;
}
if (m_requested_request_to_display_state != m_acknowledged_request_to_display_state) {
m_acknowledged_request_to_display_state = m_requested_request_to_display_state;
return AppletMessage::RequestToDisplay;
}
if (m_has_operation_mode_changed) {
m_has_operation_mode_changed = false;
return AppletMessage::OperationModeChanged;
}
if (m_has_performance_mode_changed) {
m_has_performance_mode_changed = false;
return AppletMessage::PerformanceModeChanged;
}
if (m_has_sd_card_removed) {
m_has_sd_card_removed = false;
return AppletMessage::SdCardRemoved;
}
if (m_has_sleep_required_by_high_temperature) {
m_has_sleep_required_by_high_temperature = false;
return AppletMessage::SleepRequiredByHighTemperature;
}
if (m_has_sleep_required_by_low_battery) {
m_has_sleep_required_by_low_battery = false;
return AppletMessage::SleepRequiredByLowBattery;
}
if (m_has_auto_power_down) {
m_has_auto_power_down = false;
return AppletMessage::AutoPowerDown;
}
if (m_has_album_screen_shot_taken) {
m_has_album_screen_shot_taken = false;
return AppletMessage::AlbumScreenShotTaken;
}
if (m_has_album_recording_saved) {
m_has_album_recording_saved = false;
return AppletMessage::AlbumRecordingSaved;
}
if (!m_unordered_messages.empty()) {
const auto message = m_unordered_messages.front();
m_unordered_messages.pop_front();
return message;
}
return AppletMessage::None;
}
bool LifecycleManager::ShouldSignalSystemEvent() {
if (m_focus_state_changed_notification_enabled) {
if (!m_is_application) {
if (m_requested_focus_state != m_acknowledged_focus_state) {
return true;
}
} else if (m_has_focus_state_changed) {
return true;
}
}
return !m_unordered_messages.empty() || m_has_resume ||
(m_has_requested_exit != m_has_acknowledged_exit) ||
(m_has_requested_request_to_prepare_sleep !=
m_has_acknowledged_request_to_prepare_sleep) ||
m_has_operation_mode_changed || m_has_performance_mode_changed ||
m_has_sd_card_removed || m_has_sleep_required_by_high_temperature ||
m_has_sleep_required_by_low_battery || m_has_auto_power_down ||
(m_requested_request_to_display_state != m_acknowledged_request_to_display_state) ||
m_has_album_screen_shot_taken || m_has_album_recording_saved;
}
void LifecycleManager::OnOperationAndPerformanceModeChanged() {
if (m_operation_mode_changed_notification_enabled) {
m_has_operation_mode_changed = true;
}
if (m_performance_mode_changed_notification_enabled) {
m_has_performance_mode_changed = true;
}
m_operation_mode_changed_system_event.Signal();
this->SignalSystemEventIfNeeded();
}
void LifecycleManager::SignalSystemEventIfNeeded() {
// Check our cached value for the system event.
const bool applet_message_available = m_applet_message_available;
// If it's not current, we need to do an update, either clearing or signaling.
if (applet_message_available != this->ShouldSignalSystemEvent()) {
if (!applet_message_available) {
m_system_event.Signal();
m_applet_message_available = true;
} else {
m_system_event.Clear();
m_applet_message_available = false;
}
}
}
bool LifecycleManager::PopMessage(AppletMessage* out_message) {
const auto message = this->PopMessageInOrderOfPriority();
this->SignalSystemEventIfNeeded();
*out_message = message;
return message != AppletMessage::None;
}
void LifecycleManager::SetFocusHandlingMode(bool suspend) {
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
case FocusHandlingMode::SuspendHomeSleep:
if (!suspend) {
// Disallow suspension.
m_focus_handling_mode = FocusHandlingMode::NoSuspend;
}
break;
case FocusHandlingMode::NoSuspend:
if (suspend) {
// Allow suspension temporally.
m_focus_handling_mode = FocusHandlingMode::SuspendHomeSleep;
}
break;
}
}
void LifecycleManager::SetOutOfFocusSuspendingEnabled(bool enabled) {
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
if (!enabled) {
// Allow suspension temporally.
m_focus_handling_mode = FocusHandlingMode::SuspendHomeSleep;
}
break;
case FocusHandlingMode::SuspendHomeSleep:
case FocusHandlingMode::NoSuspend:
if (enabled) {
// Allow suspension.
m_focus_handling_mode = FocusHandlingMode::AlwaysSuspend;
}
break;
}
}
void LifecycleManager::RemoveForceResumeIfPossible() {
// If resume is not forced, we have nothing to do.
if (m_suspend_mode != SuspendMode::ForceResume) {
return;
}
// Check activity state.
// If we are already resumed, we can remove the forced state.
switch (m_activity_state) {
case ActivityState::ForegroundVisible:
case ActivityState::ForegroundObscured:
m_suspend_mode = SuspendMode::NoOverride;
return;
default:
break;
}
// Check focus handling mode.
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
case FocusHandlingMode::SuspendHomeSleep:
// If the applet allows suspension, we can remove the forced state.
m_suspend_mode = SuspendMode::NoOverride;
break;
case FocusHandlingMode::NoSuspend:
// If the applet is not an application, we can remove the forced state.
// Only applications can be forced to resume.
if (!m_is_application) {
m_suspend_mode = SuspendMode::NoOverride;
}
}
}
bool LifecycleManager::IsRunnable() const {
// If suspend is forced, return that.
if (m_forced_suspend) {
return false;
}
// Check suspend mode override.
switch (m_suspend_mode) {
case SuspendMode::NoOverride:
// Continue processing.
break;
case SuspendMode::ForceResume:
// The applet is runnable during forced resumption when its exit is requested.
return m_has_requested_exit;
case SuspendMode::ForceSuspend:
// The applet is never runnable during forced suspension.
return false;
}
// Always run if exit is requested.
if (m_has_requested_exit) {
return true;
}
if (m_activity_state == ActivityState::ForegroundVisible) {
// The applet is runnable now.
return true;
}
if (m_activity_state == ActivityState::ForegroundObscured) {
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
// The applet is not runnable while running the applet.
return false;
case FocusHandlingMode::SuspendHomeSleep:
// The applet is runnable while running the applet.
return true;
case FocusHandlingMode::NoSuspend:
// The applet is always runnable.
return true;
}
}
// The activity is a suspended one.
// The applet should be suspended unless it has disabled suspension.
return m_focus_handling_mode == FocusHandlingMode::NoSuspend;
}
FocusState LifecycleManager::GetFocusStateWhileForegroundObscured() const {
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
// The applet never learns it has lost focus.
return FocusState::InFocus;
case FocusHandlingMode::SuspendHomeSleep:
// The applet learns it has lost focus when launching a child applet.
return FocusState::NotInFocus;
case FocusHandlingMode::NoSuspend:
// The applet always learns it has lost focus.
return FocusState::NotInFocus;
default:
UNREACHABLE();
}
}
FocusState LifecycleManager::GetFocusStateWhileBackground(bool is_obscured) const {
switch (m_focus_handling_mode) {
case FocusHandlingMode::AlwaysSuspend:
// The applet never learns it has lost focus.
return FocusState::InFocus;
case FocusHandlingMode::SuspendHomeSleep:
// The applet learns it has lost focus when launching a child applet.
return is_obscured ? FocusState::NotInFocus : FocusState::InFocus;
case FocusHandlingMode::NoSuspend:
// The applet always learns it has lost focus.
return m_is_application ? FocusState::Background : FocusState::NotInFocus;
default:
UNREACHABLE();
}
}
bool LifecycleManager::UpdateRequestedFocusState() {
FocusState new_state{};
if (m_suspend_mode == SuspendMode::NoOverride) {
// With no forced suspend or resume, we take the focus state designated
// by the combination of the activity flag and the focus handling mode.
switch (m_activity_state) {
case ActivityState::ForegroundVisible:
new_state = FocusState::InFocus;
break;
case ActivityState::ForegroundObscured:
new_state = this->GetFocusStateWhileForegroundObscured();
break;
case ActivityState::BackgroundVisible:
new_state = this->GetFocusStateWhileBackground(false);
break;
case ActivityState::BackgroundObscured:
new_state = this->GetFocusStateWhileBackground(true);
break;
default:
UNREACHABLE();
}
} else {
// With forced suspend or resume, the applet is guaranteed to be background.
new_state = this->GetFocusStateWhileBackground(false);
}
if (new_state != m_requested_focus_state) {
// Mark the focus state as ready for update.
m_requested_focus_state = new_state;
// We changed the focus state.
return true;
}
// We didn't change the focus state.
return false;
}
} // namespace Service::AM
|