implements PLN-0140
This commit is contained in:
@@ -4,7 +4,10 @@ mod programs;
|
||||
mod services;
|
||||
|
||||
pub use crash_report::CrashReport;
|
||||
pub use os::{LifecycleError, LifecycleOperation, SystemOS};
|
||||
pub use os::{
|
||||
DEFAULT_GAME_PAUSE_BUDGET_TICKS, GameLifecycleEvent, GameLifecycleEventKind, LifecycleError,
|
||||
LifecycleOperation, SystemOS,
|
||||
};
|
||||
pub use programs::{NativeShellApp, PrometeuHub, SystemProfileAction, SystemProfileUpdate};
|
||||
pub use prometeu_hal::{RenderWorkerBackend, RenderWorkerFrameSink};
|
||||
pub use services::async_work::{
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use crate::CrashReport;
|
||||
use crate::os::SystemOS;
|
||||
use crate::os::{LifecycleError, LifecycleOperation};
|
||||
use crate::os::{
|
||||
DEFAULT_GAME_PAUSE_BUDGET_TICKS, GameLifecycleEvent, GameLifecycleEventKind, LifecycleError,
|
||||
LifecycleOperation,
|
||||
};
|
||||
use crate::process::{ProcessId, ProcessState};
|
||||
use crate::services::foreground::{ForegroundOwner, ForegroundStackError, ResidentGame};
|
||||
use crate::services::foreground::{
|
||||
ForegroundOwner, ForegroundStackError, ResidentGame, ResidentGameState,
|
||||
};
|
||||
use crate::task::{TaskId, TaskKind, TaskState};
|
||||
|
||||
pub struct LifecycleFacade<'a> {
|
||||
@@ -79,15 +84,47 @@ impl<'a> LifecycleFacade<'a> {
|
||||
}
|
||||
|
||||
pub fn request_home_from_game(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
let _process_id = process_id_for_task(self.os, task_id)?;
|
||||
let previous = self.os.foreground_stack.resident_game();
|
||||
|
||||
self.os.foreground_stack.request_home_from_game(task_id).map_err(map_foreground_error)?;
|
||||
self.os.task_manager.mark_suspended(task_id);
|
||||
self.os.process_manager.mark_suspended(process_id);
|
||||
self.os
|
||||
.foreground_stack
|
||||
.request_home_from_game(task_id, DEFAULT_GAME_PAUSE_BUDGET_TICKS)
|
||||
.map_err(map_foreground_error)?;
|
||||
|
||||
if !matches!(
|
||||
previous.map(|game| game.state),
|
||||
Some(ResidentGameState::PauseRequested { .. } | ResidentGameState::PausedSuspended)
|
||||
) {
|
||||
self.os
|
||||
.game_lifecycle_events
|
||||
.push(GameLifecycleEvent { task_id, kind: GameLifecycleEventKind::Pause });
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn advance_game_pause_budget(&mut self, task_id: TaskId) -> Result<bool, LifecycleError> {
|
||||
let expired =
|
||||
self.os.foreground_stack.advance_pause_budget(task_id).map_err(map_foreground_error)?;
|
||||
|
||||
if expired {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
self.os.task_manager.mark_suspended(task_id);
|
||||
self.os.process_manager.mark_suspended(process_id);
|
||||
}
|
||||
|
||||
Ok(expired)
|
||||
}
|
||||
|
||||
pub fn pending_game_lifecycle_events(&self) -> &[GameLifecycleEvent] {
|
||||
&self.os.game_lifecycle_events
|
||||
}
|
||||
|
||||
pub fn take_game_lifecycle_events(&mut self) -> Vec<GameLifecycleEvent> {
|
||||
std::mem::take(&mut self.os.game_lifecycle_events)
|
||||
}
|
||||
|
||||
pub fn return_to_hub(&mut self) {
|
||||
self.os.foreground_stack.return_to_hub();
|
||||
self.os.task_manager.clear_foreground();
|
||||
@@ -102,7 +139,11 @@ impl<'a> LifecycleFacade<'a> {
|
||||
ForegroundOwner::Game(owner) if owner == task_id => {
|
||||
self.os
|
||||
.foreground_stack
|
||||
.request_home_from_game(task_id)
|
||||
.request_home_from_game(task_id, DEFAULT_GAME_PAUSE_BUDGET_TICKS)
|
||||
.map_err(map_foreground_error)?;
|
||||
self.os
|
||||
.foreground_stack
|
||||
.advance_pause_budget(task_id)
|
||||
.map_err(map_foreground_error)?;
|
||||
}
|
||||
ForegroundOwner::Shell(owner) if owner == task_id => {
|
||||
@@ -132,10 +173,18 @@ impl<'a> LifecycleFacade<'a> {
|
||||
TaskKind::Game => {
|
||||
self.os
|
||||
.foreground_stack
|
||||
.set_game_foreground(task_id)
|
||||
.request_resume_game(task_id)
|
||||
.map_err(map_foreground_error)?;
|
||||
self.os.task_manager.set_foreground(task_id);
|
||||
self.os.process_manager.mark_running(process_id);
|
||||
self.os.game_lifecycle_events.push(GameLifecycleEvent {
|
||||
task_id,
|
||||
kind: GameLifecycleEventKind::ResumeForeground,
|
||||
});
|
||||
self.os
|
||||
.foreground_stack
|
||||
.complete_resume_game(task_id)
|
||||
.map_err(map_foreground_error)?;
|
||||
}
|
||||
TaskKind::Shell => {
|
||||
self.os
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
use crate::process::ProcessId;
|
||||
use crate::task::{TaskId, TaskState};
|
||||
|
||||
pub const DEFAULT_GAME_PAUSE_BUDGET_TICKS: u8 = 1;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum GameLifecycleEventKind {
|
||||
Pause,
|
||||
ResumeForeground,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct GameLifecycleEvent {
|
||||
pub task_id: TaskId,
|
||||
pub kind: GameLifecycleEventKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LifecycleOperation {
|
||||
SetForeground,
|
||||
|
||||
@@ -3,5 +3,8 @@ mod lifecycle;
|
||||
mod system_os;
|
||||
|
||||
pub use facades::{FsFacade, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade};
|
||||
pub use lifecycle::{LifecycleError, LifecycleOperation};
|
||||
pub use lifecycle::{
|
||||
DEFAULT_GAME_PAUSE_BUDGET_TICKS, GameLifecycleEvent, GameLifecycleEventKind, LifecycleError,
|
||||
LifecycleOperation,
|
||||
};
|
||||
pub use system_os::SystemOS;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::VirtualMachineRuntime;
|
||||
use crate::fs::{FsState, VirtualFS};
|
||||
use crate::os::{FsFacade, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade};
|
||||
use crate::os::{
|
||||
FsFacade, GameLifecycleEvent, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade,
|
||||
};
|
||||
use crate::process::ProcessManager;
|
||||
use crate::services::foreground::ForegroundStack;
|
||||
use crate::services::memcard::MemcardService;
|
||||
@@ -16,6 +18,7 @@ pub struct SystemOS {
|
||||
pub(super) process_manager: ProcessManager,
|
||||
pub(super) task_manager: TaskManager,
|
||||
pub(super) foreground_stack: ForegroundStack,
|
||||
pub(super) game_lifecycle_events: Vec<GameLifecycleEvent>,
|
||||
pub(super) window_manager: WindowManager,
|
||||
pub(super) log_service: LogService,
|
||||
pub(super) fs: VirtualFS,
|
||||
@@ -36,6 +39,7 @@ impl SystemOS {
|
||||
process_manager: ProcessManager::new(),
|
||||
task_manager: TaskManager::new(),
|
||||
foreground_stack: ForegroundStack::new(),
|
||||
game_lifecycle_events: Vec::new(),
|
||||
window_manager: WindowManager::new(),
|
||||
log_service,
|
||||
fs: VirtualFS::new(),
|
||||
@@ -136,7 +140,7 @@ impl SystemOS {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::os::{LifecycleError, LifecycleOperation};
|
||||
use crate::os::{GameLifecycleEventKind, LifecycleError, LifecycleOperation};
|
||||
use crate::process::ProcessState;
|
||||
use crate::services::foreground::{ForegroundOwner, ResidentGame, ResidentGameState};
|
||||
use crate::task::{TaskId, TaskState};
|
||||
@@ -195,12 +199,30 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn home_request_moves_game_to_resident_suspended_and_hub_foreground() {
|
||||
fn home_request_notifies_game_before_budget_suspends_it() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
os.lifecycle().request_home_from_game(task_id).expect("home request should succeed");
|
||||
|
||||
assert_eq!(os.lifecycle().foreground_owner(), ForegroundOwner::Game(task_id));
|
||||
assert_eq!(
|
||||
os.lifecycle().resident_game(),
|
||||
Some(ResidentGame {
|
||||
task_id,
|
||||
state: ResidentGameState::PauseRequested { remaining_ticks: 1 },
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
os.lifecycle().pending_game_lifecycle_events()[0].kind,
|
||||
GameLifecycleEventKind::Pause
|
||||
);
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should exist").state,
|
||||
TaskState::Foreground
|
||||
);
|
||||
|
||||
assert!(os.lifecycle().advance_game_pause_budget(task_id).expect("budget should advance"));
|
||||
assert_eq!(os.lifecycle().foreground_owner(), ForegroundOwner::Hub);
|
||||
assert_eq!(
|
||||
os.lifecycle().resident_game(),
|
||||
@@ -218,6 +240,7 @@ mod tests {
|
||||
let mut os = SystemOS::new(None);
|
||||
let game = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
os.lifecycle().request_home_from_game(game).expect("home request should succeed");
|
||||
os.lifecycle().advance_game_pause_budget(game).expect("budget should expire");
|
||||
let shell = os.sessions().create_vm_shell_task(7, "Settings");
|
||||
|
||||
assert_eq!(os.lifecycle().foreground_owner(), ForegroundOwner::Shell(shell));
|
||||
@@ -284,6 +307,10 @@ mod tests {
|
||||
TaskState::Foreground
|
||||
);
|
||||
assert_eq!(process_state_for_task(&os, task_id), ProcessState::Running);
|
||||
assert_eq!(
|
||||
os.lifecycle().pending_game_lifecycle_events().last().map(|event| event.kind),
|
||||
Some(GameLifecycleEventKind::ResumeForeground)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -10,6 +10,8 @@ pub enum ForegroundOwner {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ResidentGameState {
|
||||
Foreground,
|
||||
PauseRequested { remaining_ticks: u8 },
|
||||
ResumeRequested,
|
||||
PausedSuspended,
|
||||
}
|
||||
|
||||
@@ -70,12 +72,81 @@ impl ForegroundStack {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn request_home_from_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
|
||||
pub fn request_home_from_game(
|
||||
&mut self,
|
||||
task_id: TaskId,
|
||||
pause_budget_ticks: u8,
|
||||
) -> Result<(), ForegroundStackError> {
|
||||
match self.resident_game {
|
||||
Some(game) if game.task_id == task_id => {
|
||||
if !matches!(
|
||||
game.state,
|
||||
ResidentGameState::PauseRequested { .. } | ResidentGameState::PausedSuspended
|
||||
) {
|
||||
self.resident_game = Some(ResidentGame {
|
||||
task_id,
|
||||
state: ResidentGameState::PauseRequested {
|
||||
remaining_ticks: pause_budget_ticks.max(1),
|
||||
},
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn advance_pause_budget(&mut self, task_id: TaskId) -> Result<bool, ForegroundStackError> {
|
||||
match self.resident_game {
|
||||
Some(game)
|
||||
if game.task_id == task_id
|
||||
&& matches!(
|
||||
game.state,
|
||||
ResidentGameState::PauseRequested { remaining_ticks: 1 }
|
||||
) =>
|
||||
{
|
||||
self.resident_game =
|
||||
Some(ResidentGame { task_id, state: ResidentGameState::PausedSuspended });
|
||||
self.owner = ForegroundOwner::Hub;
|
||||
Ok(true)
|
||||
}
|
||||
Some(game)
|
||||
if game.task_id == task_id
|
||||
&& matches!(game.state, ResidentGameState::PauseRequested { .. }) =>
|
||||
{
|
||||
let ResidentGameState::PauseRequested { remaining_ticks } = game.state else {
|
||||
unreachable!();
|
||||
};
|
||||
self.resident_game = Some(ResidentGame {
|
||||
task_id,
|
||||
state: ResidentGameState::PauseRequested {
|
||||
remaining_ticks: remaining_ticks.saturating_sub(1).max(1),
|
||||
},
|
||||
});
|
||||
Ok(false)
|
||||
}
|
||||
Some(game) if game.task_id == task_id => Ok(false),
|
||||
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_resume_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
|
||||
match self.resident_game {
|
||||
Some(game) if game.task_id == task_id => {
|
||||
self.resident_game =
|
||||
Some(ResidentGame { task_id, state: ResidentGameState::ResumeRequested });
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn complete_resume_game(&mut self, task_id: TaskId) -> Result<(), ForegroundStackError> {
|
||||
match self.resident_game {
|
||||
Some(game) if game.task_id == task_id => {
|
||||
self.resident_game =
|
||||
Some(ResidentGame { task_id, state: ResidentGameState::Foreground });
|
||||
self.owner = ForegroundOwner::Game(task_id);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ForegroundStackError::TaskIsNotResidentGame(task_id)),
|
||||
@@ -159,13 +230,32 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn home_request_moves_resident_game_to_paused_suspended() {
|
||||
fn home_request_moves_resident_game_to_pause_requested() {
|
||||
let mut stack = ForegroundStack::new();
|
||||
let game = TaskId(3);
|
||||
|
||||
stack.set_game_foreground(game).expect("game should be foreground");
|
||||
stack.request_home_from_game(game).expect("home request should suspend game");
|
||||
stack.request_home_from_game(game, 2).expect("home request should pause game");
|
||||
|
||||
assert_eq!(stack.owner(), ForegroundOwner::Game(game));
|
||||
assert_eq!(
|
||||
stack.resident_game(),
|
||||
Some(ResidentGame {
|
||||
task_id: game,
|
||||
state: ResidentGameState::PauseRequested { remaining_ticks: 2 },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pause_budget_expiry_suspends_game_and_returns_to_hub() {
|
||||
let mut stack = ForegroundStack::new();
|
||||
let game = TaskId(3);
|
||||
|
||||
stack.set_game_foreground(game).expect("game should be foreground");
|
||||
stack.request_home_from_game(game, 1).expect("home request should pause game");
|
||||
|
||||
assert!(stack.advance_pause_budget(game).expect("budget should advance"));
|
||||
assert_eq!(stack.owner(), ForegroundOwner::Hub);
|
||||
assert_eq!(
|
||||
stack.resident_game(),
|
||||
@@ -173,6 +263,30 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resume_request_precedes_game_foreground_restore() {
|
||||
let mut stack = ForegroundStack::new();
|
||||
let game = TaskId(3);
|
||||
|
||||
stack.set_game_foreground(game).expect("game should be foreground");
|
||||
stack.request_home_from_game(game, 1).expect("home request should pause game");
|
||||
stack.advance_pause_budget(game).expect("budget should expire");
|
||||
|
||||
stack.request_resume_game(game).expect("resume should be requested");
|
||||
assert_eq!(stack.owner(), ForegroundOwner::Hub);
|
||||
assert_eq!(
|
||||
stack.resident_game(),
|
||||
Some(ResidentGame { task_id: game, state: ResidentGameState::ResumeRequested })
|
||||
);
|
||||
|
||||
stack.complete_resume_game(game).expect("resume should complete");
|
||||
assert_eq!(stack.owner(), ForegroundOwner::Game(game));
|
||||
assert_eq!(
|
||||
stack.resident_game(),
|
||||
Some(ResidentGame { task_id: game, state: ResidentGameState::Foreground })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shell_foreground_keeps_resident_game() {
|
||||
let mut stack = ForegroundStack::new();
|
||||
@@ -180,7 +294,8 @@ mod tests {
|
||||
let shell = TaskId(4);
|
||||
|
||||
stack.set_game_foreground(game).expect("game should be foreground");
|
||||
stack.request_home_from_game(game).expect("home request should suspend game");
|
||||
stack.request_home_from_game(game, 1).expect("home request should suspend game");
|
||||
stack.advance_pause_budget(game).expect("budget should expire");
|
||||
stack.set_shell_foreground(shell).expect("shell should become foreground");
|
||||
|
||||
assert_eq!(stack.owner(), ForegroundOwner::Shell(shell));
|
||||
|
||||
Reference in New Issue
Block a user