implements PLN-0058
This commit is contained in:
@@ -308,12 +308,11 @@ mod tests {
|
||||
other => panic!("expected AppCrashes state, got {:?}", other),
|
||||
}
|
||||
|
||||
let task = firmware.os.task_manager.get(task_id).expect("task should exist");
|
||||
let process =
|
||||
firmware.os.process_manager.get(task.process_id).expect("process should exist");
|
||||
|
||||
assert_eq!(task.state, TaskState::Crashed);
|
||||
assert_eq!(process.state, ProcessState::Crashed);
|
||||
assert_eq!(firmware.os.lifecycle().task_state(task_id), Some(TaskState::Crashed));
|
||||
assert_eq!(
|
||||
firmware.os.lifecycle().process_state_for_task(task_id),
|
||||
Ok(ProcessState::Crashed)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -351,8 +350,8 @@ mod tests {
|
||||
firmware.tick(&signals, &mut hardware);
|
||||
|
||||
assert!(matches!(firmware.state, FirmwareState::SystemRunning(_)));
|
||||
assert!(firmware.os.window_manager.focused.is_some());
|
||||
assert_eq!(firmware.os.window_manager.windows.len(), 1);
|
||||
assert!(firmware.os.window().focused_window().is_some());
|
||||
assert_eq!(firmware.os.window().window_count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -364,11 +363,11 @@ mod tests {
|
||||
firmware.load_cartridge(valid_cartridge(AppMode::Shell));
|
||||
firmware.tick(&signals, &mut hardware);
|
||||
|
||||
let tick_index_before_system_update = firmware.os.vm_runtime.tick_index;
|
||||
let tick_index_before_system_update = firmware.os.vm().tick_index();
|
||||
firmware.tick(&signals, &mut hardware);
|
||||
|
||||
assert!(matches!(firmware.state, FirmwareState::SystemRunning(_)));
|
||||
assert_eq!(firmware.os.vm_runtime.tick_index, tick_index_before_system_update + 1);
|
||||
assert_eq!(firmware.os.vm().tick_index(), tick_index_before_system_update + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -19,7 +19,7 @@ impl GameRunningStep {
|
||||
}
|
||||
|
||||
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
||||
let Some(task_state) = ctx.os.task_manager.get(self.task_id).map(|task| task.state) else {
|
||||
let Some(task_state) = ctx.os.lifecycle().task_state(self.task_id) else {
|
||||
// TODO: is it panic?
|
||||
let report =
|
||||
CrashReport::VmPanic { message: "task is gone, panic!".to_string(), pc: None };
|
||||
@@ -32,18 +32,18 @@ impl GameRunningStep {
|
||||
message: "should be running as foreground, panic!".to_string(),
|
||||
pc: None,
|
||||
};
|
||||
let _ = ctx.os.crash_task(self.task_id, Some(&report));
|
||||
let _ = ctx.os.lifecycle().crash_task(self.task_id, Some(&report));
|
||||
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
|
||||
}
|
||||
|
||||
let result = ctx.os.tick_vm(ctx.vm, ctx.signals, ctx.hw);
|
||||
let result = ctx.os.vm().tick(ctx.vm, ctx.signals, ctx.hw);
|
||||
|
||||
if !ctx.os.vm_runtime.logical_frame_active {
|
||||
if !ctx.os.vm().logical_frame_active() {
|
||||
ctx.hw.gfx_mut().present();
|
||||
}
|
||||
|
||||
if let Some(report) = result {
|
||||
let _ = ctx.os.crash_task(self.task_id, Some(&report));
|
||||
let _ = ctx.os.lifecycle().crash_task(self.task_id, Some(&report));
|
||||
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ impl LoadCartridgeStep {
|
||||
self.cartridge.assets.clone(),
|
||||
);
|
||||
|
||||
self.init_error = ctx.os.initialize_vm(ctx.vm, &self.cartridge).err();
|
||||
self.init_error = ctx.os.vm().initialize(ctx.vm, &self.cartridge).err();
|
||||
}
|
||||
|
||||
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
||||
@@ -43,20 +43,24 @@ impl LoadCartridgeStep {
|
||||
}
|
||||
|
||||
if self.cartridge.app_mode == AppMode::Shell {
|
||||
let id = ctx.os.window_manager.add_window(
|
||||
let id = ctx.os.window().add_window(
|
||||
self.cartridge.title.clone(),
|
||||
Rect { x: 40, y: 20, w: 240, h: 140 },
|
||||
Color::WHITE,
|
||||
);
|
||||
ctx.os.window_manager.set_focus(id);
|
||||
ctx.os.window().set_focus(id);
|
||||
|
||||
let task_id =
|
||||
ctx.os.create_vm_shell_task(self.cartridge.app_id, self.cartridge.title.clone());
|
||||
let task_id = ctx
|
||||
.os
|
||||
.sessions()
|
||||
.create_vm_shell_task(self.cartridge.app_id, self.cartridge.title.clone());
|
||||
return Some(FirmwareState::SystemRunning(SystemRunningStep::new(task_id)));
|
||||
}
|
||||
|
||||
let task_id =
|
||||
ctx.os.create_vm_game_task(self.cartridge.app_id, self.cartridge.title.clone());
|
||||
let task_id = ctx
|
||||
.os
|
||||
.sessions()
|
||||
.create_vm_game_task(self.cartridge.app_id, self.cartridge.title.clone());
|
||||
Some(FirmwareState::GameRunning(GameRunningStep::new(task_id)))
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ pub struct ResetStep;
|
||||
impl ResetStep {
|
||||
pub fn on_enter(&mut self, ctx: &mut PrometeuContext) {
|
||||
ctx.os.log(LogLevel::Info, LogSource::Pos, 0, "Firmware Reset".to_string());
|
||||
ctx.os.vm_runtime.reset(ctx.vm);
|
||||
ctx.os.vm().reset(ctx.vm);
|
||||
}
|
||||
|
||||
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
||||
|
||||
@@ -20,7 +20,7 @@ impl SystemRunningStep {
|
||||
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
||||
let outcome = ctx.hub.update_system_profile(ctx.os, ctx.vm, ctx.signals, ctx.hw);
|
||||
if let Some(report) = outcome.crash {
|
||||
let _ = ctx.os.crash_task(self.task_id, Some(&report));
|
||||
let _ = ctx.os.lifecycle().crash_task(self.task_id, Some(&report));
|
||||
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use crate::fs::{FsBackend, FsState};
|
||||
use crate::os::SystemOS;
|
||||
|
||||
pub struct FsFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> FsFacade<'a> {
|
||||
pub fn needs_mount(&self) -> bool {
|
||||
matches!(self.os.fs_state, FsState::Unmounted | FsState::Error(_))
|
||||
}
|
||||
|
||||
pub fn mount(&mut self, backend: Box<dyn FsBackend>) {
|
||||
self.os.vm_runtime.mount_fs(
|
||||
&mut self.os.log_service,
|
||||
&mut self.os.fs,
|
||||
&mut self.os.fs_state,
|
||||
backend,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn unmount(&mut self) {
|
||||
self.os.fs.unmount();
|
||||
self.os.fs_state = FsState::Unmounted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
use crate::CrashReport;
|
||||
use crate::os::SystemOS;
|
||||
use crate::os::{LifecycleError, LifecycleOperation};
|
||||
use crate::process::{ProcessId, ProcessState};
|
||||
use crate::task::{TaskId, TaskState};
|
||||
|
||||
pub struct LifecycleFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> LifecycleFacade<'a> {
|
||||
pub fn task_state(&self, task_id: TaskId) -> Option<TaskState> {
|
||||
self.os.task_manager.get(task_id).map(|task| task.state)
|
||||
}
|
||||
|
||||
pub fn process_state_for_task(&self, task_id: TaskId) -> Result<ProcessState, LifecycleError> {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
self.os
|
||||
.process_manager
|
||||
.get(process_id)
|
||||
.map(|process| process.state)
|
||||
.ok_or(LifecycleError::ProcessNotFound(process_id))
|
||||
}
|
||||
|
||||
pub fn set_foreground_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
|
||||
self.os.task_manager.set_foreground(task_id);
|
||||
self.os.process_manager.mark_running(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn suspend_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
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(())
|
||||
}
|
||||
|
||||
pub fn resume_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let task =
|
||||
self.os.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
|
||||
|
||||
if task.state != TaskState::Suspended {
|
||||
return Err(LifecycleError::InvalidTransition {
|
||||
task_id,
|
||||
from: task.state,
|
||||
operation: LifecycleOperation::Resume,
|
||||
});
|
||||
}
|
||||
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
|
||||
self.os.task_manager.set_foreground(task_id);
|
||||
self.os.process_manager.mark_running(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn close_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
|
||||
self.os.task_manager.close_task(task_id);
|
||||
self.os.process_manager.mark_stopped(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn crash_task(
|
||||
&mut self,
|
||||
task_id: TaskId,
|
||||
_report: Option<&CrashReport>,
|
||||
) -> Result<(), LifecycleError> {
|
||||
let process_id = process_id_for_task(self.os, task_id)?;
|
||||
|
||||
self.os.task_manager.mark_crashed(task_id);
|
||||
self.os.process_manager.mark_crashed(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn process_id_for_task(os: &SystemOS, task_id: TaskId) -> Result<ProcessId, LifecycleError> {
|
||||
let task = os.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
|
||||
|
||||
if !os.process_manager.contains(task.process_id) {
|
||||
return Err(LifecycleError::ProcessNotFound(task.process_id));
|
||||
}
|
||||
|
||||
Ok(task.process_id)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
mod fs;
|
||||
mod lifecycle;
|
||||
mod sessions;
|
||||
mod vm;
|
||||
mod window;
|
||||
|
||||
pub use fs::FsFacade;
|
||||
pub use lifecycle::LifecycleFacade;
|
||||
pub use sessions::SessionsFacade;
|
||||
pub use vm::VmFacade;
|
||||
pub use window::WindowFacade;
|
||||
@@ -0,0 +1,44 @@
|
||||
use crate::os::SystemOS;
|
||||
use crate::task::TaskId;
|
||||
|
||||
pub struct SessionsFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> SessionsFacade<'a> {
|
||||
pub fn create_vm_game_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.os.process_manager.spawn_vm_game(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_game_task(process_id, app_id, title);
|
||||
self.os
|
||||
.lifecycle()
|
||||
.set_foreground_task(task_id)
|
||||
.expect("newly created game task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
|
||||
pub fn create_vm_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.os.process_manager.spawn_vm_shell(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.os
|
||||
.lifecycle()
|
||||
.set_foreground_task(task_id)
|
||||
.expect("newly created shell task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
|
||||
pub fn create_native_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.os.process_manager.spawn_native_shell(app_id, title.clone());
|
||||
let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.os
|
||||
.lifecycle()
|
||||
.set_foreground_task(task_id)
|
||||
.expect("newly created native shell task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
use crate::CrashReport;
|
||||
use crate::os::SystemOS;
|
||||
use prometeu_hal::cartridge::{AppMode, Cartridge};
|
||||
use prometeu_hal::telemetry::{CertificationConfig, TelemetryFrame};
|
||||
use prometeu_hal::{HardwareBridge, InputSignals};
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
pub struct VmFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> VmFacade<'a> {
|
||||
pub fn initialize(
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
cartridge: &Cartridge,
|
||||
) -> Result<(), CrashReport> {
|
||||
self.os.clear_cartridge_service_state();
|
||||
self.os.vm_runtime.initialize_vm(&mut self.os.log_service, vm, cartridge)
|
||||
}
|
||||
|
||||
pub fn tick(
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
signals: &InputSignals,
|
||||
hw: &mut dyn HardwareBridge,
|
||||
) -> Option<CrashReport> {
|
||||
self.os.vm_runtime.tick(
|
||||
&mut self.os.log_service,
|
||||
&mut self.os.fs,
|
||||
&mut self.os.fs_state,
|
||||
&mut self.os.memcard,
|
||||
&mut self.os.open_files,
|
||||
&mut self.os.next_handle,
|
||||
vm,
|
||||
signals,
|
||||
hw,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn reset(&mut self, vm: &mut VirtualMachine) {
|
||||
self.os.vm_runtime.reset(vm);
|
||||
}
|
||||
|
||||
pub fn debug_step_instruction(
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
hw: &mut dyn HardwareBridge,
|
||||
) -> Option<CrashReport> {
|
||||
self.os.vm_runtime.debug_step_instruction(&mut self.os.log_service, vm, hw)
|
||||
}
|
||||
|
||||
pub fn paused(&self) -> bool {
|
||||
self.os.vm_runtime.paused
|
||||
}
|
||||
|
||||
pub fn set_paused(&mut self, paused: bool) {
|
||||
self.os.vm_runtime.paused = paused;
|
||||
}
|
||||
|
||||
pub fn set_inspection_active(&mut self, active: bool) {
|
||||
self.os.vm_runtime.inspection_active = active;
|
||||
}
|
||||
|
||||
pub fn request_debug_step(&mut self) {
|
||||
self.os.vm_runtime.debug_step_request = true;
|
||||
}
|
||||
|
||||
pub fn debug_step_requested(&self) -> bool {
|
||||
self.os.vm_runtime.debug_step_request
|
||||
}
|
||||
|
||||
pub fn logical_frame_active(&self) -> bool {
|
||||
self.os.vm_runtime.logical_frame_active
|
||||
}
|
||||
|
||||
pub fn tick_index(&self) -> u64 {
|
||||
self.os.vm_runtime.tick_index
|
||||
}
|
||||
|
||||
pub fn logical_frame_index(&self) -> u64 {
|
||||
self.os.vm_runtime.logical_frame_index
|
||||
}
|
||||
|
||||
pub fn last_frame_cpu_time_us(&self) -> u64 {
|
||||
self.os.vm_runtime.last_frame_cpu_time_us
|
||||
}
|
||||
|
||||
pub fn completed_logical_frames(&self) -> u64 {
|
||||
self.os.vm_runtime.atomic_telemetry.completed_logical_frames.load(Ordering::Relaxed).into()
|
||||
}
|
||||
|
||||
pub fn telemetry_snapshot(&self) -> TelemetryFrame {
|
||||
self.os.vm_runtime.atomic_telemetry.snapshot()
|
||||
}
|
||||
|
||||
pub fn cert_config(&self) -> &CertificationConfig {
|
||||
&self.os.vm_runtime.certifier.config
|
||||
}
|
||||
|
||||
pub fn last_crash_report(&self) -> Option<&CrashReport> {
|
||||
self.os.vm_runtime.last_crash_report.as_ref()
|
||||
}
|
||||
|
||||
pub fn current_app_id(&self) -> u32 {
|
||||
self.os.vm_runtime.current_app_id
|
||||
}
|
||||
|
||||
pub fn current_cartridge_title(&self) -> String {
|
||||
self.os.vm_runtime.current_cartridge_title.clone()
|
||||
}
|
||||
|
||||
pub fn current_cartridge_app_version(&self) -> String {
|
||||
self.os.vm_runtime.current_cartridge_app_version.clone()
|
||||
}
|
||||
|
||||
pub fn current_cartridge_app_mode(&self) -> AppMode {
|
||||
self.os.vm_runtime.current_cartridge_app_mode
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
use crate::os::SystemOS;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::window::{Rect, Window, WindowId};
|
||||
|
||||
pub struct WindowFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
}
|
||||
|
||||
impl<'a> WindowFacade<'a> {
|
||||
pub fn add_window(&mut self, title: String, viewport: Rect, color: Color) -> WindowId {
|
||||
self.os.window_manager.add_window(title, viewport, color)
|
||||
}
|
||||
|
||||
pub fn set_focus(&mut self, id: WindowId) {
|
||||
self.os.window_manager.set_focus(id);
|
||||
}
|
||||
|
||||
pub fn focused_window(&self) -> Option<WindowId> {
|
||||
self.os.window_manager.focused
|
||||
}
|
||||
|
||||
pub fn close_window(&mut self, id: WindowId) {
|
||||
self.os.window_manager.remove_window(id);
|
||||
}
|
||||
|
||||
pub fn remove_all_windows(&mut self) {
|
||||
self.os.window_manager.remove_all_windows();
|
||||
}
|
||||
|
||||
pub fn windows(&self) -> &[Window] {
|
||||
&self.os.window_manager.windows
|
||||
}
|
||||
|
||||
pub fn window_count(&self) -> usize {
|
||||
self.os.window_manager.windows.len()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
mod facades;
|
||||
mod lifecycle;
|
||||
mod system_os;
|
||||
|
||||
pub use facades::{FsFacade, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade};
|
||||
pub use lifecycle::{LifecycleError, LifecycleOperation};
|
||||
pub use system_os::SystemOS;
|
||||
|
||||
@@ -1,31 +1,26 @@
|
||||
use crate::CrashReport;
|
||||
use crate::VirtualMachineRuntime;
|
||||
use crate::fs::{FsBackend, FsState, VirtualFS};
|
||||
use crate::os::{LifecycleError, LifecycleOperation};
|
||||
use crate::fs::{FsState, VirtualFS};
|
||||
use crate::os::{FsFacade, LifecycleFacade, SessionsFacade, VmFacade, WindowFacade};
|
||||
use crate::process::ProcessManager;
|
||||
use crate::services::memcard::MemcardService;
|
||||
use crate::services::window_manager::WindowManager;
|
||||
use crate::task::TaskManager;
|
||||
use crate::task::{TaskId, TaskState};
|
||||
use prometeu_hal::cartridge::Cartridge;
|
||||
use prometeu_hal::log::{LogLevel, LogService, LogSource};
|
||||
use prometeu_hal::log::{LogEvent, LogLevel, LogService, LogSource};
|
||||
use prometeu_hal::telemetry::CertificationConfig;
|
||||
use prometeu_hal::{HardwareBridge, InputSignals};
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct SystemOS {
|
||||
pub vm_runtime: VirtualMachineRuntime,
|
||||
pub process_manager: ProcessManager,
|
||||
pub task_manager: TaskManager,
|
||||
pub window_manager: WindowManager,
|
||||
pub log_service: LogService,
|
||||
pub fs: VirtualFS,
|
||||
pub fs_state: FsState,
|
||||
pub memcard: MemcardService,
|
||||
pub open_files: HashMap<u32, String>,
|
||||
pub next_handle: u32,
|
||||
pub(super) vm_runtime: VirtualMachineRuntime,
|
||||
pub(super) process_manager: ProcessManager,
|
||||
pub(super) task_manager: TaskManager,
|
||||
pub(super) window_manager: WindowManager,
|
||||
pub(super) log_service: LogService,
|
||||
pub(super) fs: VirtualFS,
|
||||
pub(super) fs_state: FsState,
|
||||
pub(super) memcard: MemcardService,
|
||||
pub(super) open_files: HashMap<u32, String>,
|
||||
pub(super) next_handle: u32,
|
||||
}
|
||||
|
||||
impl SystemOS {
|
||||
@@ -56,156 +51,47 @@ impl SystemOS {
|
||||
self.vm_runtime.log(&mut self.log_service, level, source, tag, msg);
|
||||
}
|
||||
|
||||
pub fn initialize_vm(
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
cartridge: &Cartridge,
|
||||
) -> Result<(), CrashReport> {
|
||||
self.clear_cartridge_service_state();
|
||||
self.vm_runtime.initialize_vm(&mut self.log_service, vm, cartridge)
|
||||
pub fn recent_logs(&self, n: usize) -> Vec<LogEvent> {
|
||||
self.log_service.get_recent(n)
|
||||
}
|
||||
|
||||
pub fn tick_vm(
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
signals: &InputSignals,
|
||||
hw: &mut dyn HardwareBridge,
|
||||
) -> Option<CrashReport> {
|
||||
self.vm_runtime.tick(
|
||||
&mut self.log_service,
|
||||
&mut self.fs,
|
||||
&mut self.fs_state,
|
||||
&mut self.memcard,
|
||||
&mut self.open_files,
|
||||
&mut self.next_handle,
|
||||
vm,
|
||||
signals,
|
||||
hw,
|
||||
)
|
||||
pub fn logs_after(&self, seq: u64) -> Vec<LogEvent> {
|
||||
self.log_service.get_after(seq)
|
||||
}
|
||||
|
||||
pub fn mount_fs(&mut self, backend: Box<dyn FsBackend>) {
|
||||
self.vm_runtime.mount_fs(&mut self.log_service, &mut self.fs, &mut self.fs_state, backend);
|
||||
pub fn lifecycle(&mut self) -> LifecycleFacade<'_> {
|
||||
LifecycleFacade { os: self }
|
||||
}
|
||||
|
||||
pub fn unmount_fs(&mut self) {
|
||||
self.fs.unmount();
|
||||
self.fs_state = FsState::Unmounted;
|
||||
pub fn sessions(&mut self) -> SessionsFacade<'_> {
|
||||
SessionsFacade { os: self }
|
||||
}
|
||||
|
||||
fn clear_cartridge_service_state(&mut self) {
|
||||
pub fn vm(&mut self) -> VmFacade<'_> {
|
||||
VmFacade { os: self }
|
||||
}
|
||||
|
||||
pub fn fs(&mut self) -> FsFacade<'_> {
|
||||
FsFacade { os: self }
|
||||
}
|
||||
|
||||
pub fn window(&mut self) -> WindowFacade<'_> {
|
||||
WindowFacade { os: self }
|
||||
}
|
||||
|
||||
pub(super) fn clear_cartridge_service_state(&mut self) {
|
||||
self.open_files.clear();
|
||||
self.next_handle = 1;
|
||||
self.memcard.clear_all_staging();
|
||||
}
|
||||
|
||||
pub fn create_vm_game_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.process_manager.spawn_vm_game(app_id, title.clone());
|
||||
let task_id = self.task_manager.create_game_task(process_id, app_id, title);
|
||||
self.set_foreground_task(task_id)
|
||||
.expect("newly created game task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
|
||||
pub fn create_vm_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.process_manager.spawn_vm_shell(app_id, title.clone());
|
||||
let task_id = self.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.set_foreground_task(task_id)
|
||||
.expect("newly created shell task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
|
||||
pub fn create_native_shell_task(&mut self, app_id: u32, title: impl Into<String>) -> TaskId {
|
||||
let title = title.into();
|
||||
let process_id = self.process_manager.spawn_native_shell(app_id, title.clone());
|
||||
let task_id = self.task_manager.create_shell_task(process_id, app_id, title);
|
||||
self.set_foreground_task(task_id)
|
||||
.expect("newly created native shell task should have an associated process");
|
||||
|
||||
task_id
|
||||
}
|
||||
|
||||
pub fn set_foreground_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = self.process_id_for_task(task_id)?;
|
||||
|
||||
self.task_manager.set_foreground(task_id);
|
||||
self.process_manager.mark_running(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn suspend_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = self.process_id_for_task(task_id)?;
|
||||
|
||||
self.task_manager.mark_suspended(task_id);
|
||||
self.process_manager.mark_suspended(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn resume_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let task = self.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
|
||||
|
||||
if task.state != TaskState::Suspended {
|
||||
return Err(LifecycleError::InvalidTransition {
|
||||
task_id,
|
||||
from: task.state,
|
||||
operation: LifecycleOperation::Resume,
|
||||
});
|
||||
}
|
||||
|
||||
let process_id = self.process_id_for_task(task_id)?;
|
||||
|
||||
self.task_manager.set_foreground(task_id);
|
||||
self.process_manager.mark_running(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn close_task(&mut self, task_id: TaskId) -> Result<(), LifecycleError> {
|
||||
let process_id = self.process_id_for_task(task_id)?;
|
||||
|
||||
self.task_manager.close_task(task_id);
|
||||
self.process_manager.mark_stopped(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn crash_task(
|
||||
&mut self,
|
||||
task_id: TaskId,
|
||||
_report: Option<&CrashReport>,
|
||||
) -> Result<(), LifecycleError> {
|
||||
let process_id = self.process_id_for_task(task_id)?;
|
||||
|
||||
self.task_manager.mark_crashed(task_id);
|
||||
self.process_manager.mark_crashed(process_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_id_for_task(
|
||||
&self,
|
||||
task_id: TaskId,
|
||||
) -> Result<crate::process::ProcessId, LifecycleError> {
|
||||
let task = self.task_manager.get(task_id).ok_or(LifecycleError::TaskNotFound(task_id))?;
|
||||
|
||||
if !self.process_manager.contains(task.process_id) {
|
||||
return Err(LifecycleError::ProcessNotFound(task.process_id));
|
||||
}
|
||||
|
||||
Ok(task.process_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::os::{LifecycleError, LifecycleOperation};
|
||||
use crate::process::ProcessState;
|
||||
use crate::task::{TaskId, TaskState};
|
||||
|
||||
fn process_state_for_task(os: &SystemOS, task_id: TaskId) -> ProcessState {
|
||||
let task = os.task_manager.get(task_id).expect("task should exist");
|
||||
@@ -215,10 +101,10 @@ mod tests {
|
||||
#[test]
|
||||
fn set_foreground_task_marks_task_and_process_running() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
os.suspend_task(task_id).expect("suspend should succeed");
|
||||
os.set_foreground_task(task_id).expect("foreground should succeed");
|
||||
os.lifecycle().suspend_task(task_id).expect("suspend should succeed");
|
||||
os.lifecycle().set_foreground_task(task_id).expect("foreground should succeed");
|
||||
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should exist").state,
|
||||
@@ -230,9 +116,9 @@ mod tests {
|
||||
#[test]
|
||||
fn suspend_task_marks_task_and_process_suspended() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
os.suspend_task(task_id).expect("suspend should succeed");
|
||||
os.lifecycle().suspend_task(task_id).expect("suspend should succeed");
|
||||
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should exist").state,
|
||||
@@ -244,10 +130,10 @@ mod tests {
|
||||
#[test]
|
||||
fn resume_task_marks_suspended_task_foreground_and_process_running() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
os.suspend_task(task_id).expect("suspend should succeed");
|
||||
os.resume_task(task_id).expect("resume should succeed");
|
||||
os.lifecycle().suspend_task(task_id).expect("suspend should succeed");
|
||||
os.lifecycle().resume_task(task_id).expect("resume should succeed");
|
||||
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should exist").state,
|
||||
@@ -259,10 +145,10 @@ mod tests {
|
||||
#[test]
|
||||
fn close_task_marks_task_closed_and_process_stopped_without_removing_entities() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
let process_id = os.task_manager.get(task_id).expect("task should exist").process_id;
|
||||
|
||||
os.close_task(task_id).expect("close should succeed");
|
||||
os.lifecycle().close_task(task_id).expect("close should succeed");
|
||||
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should remain").state,
|
||||
@@ -277,9 +163,9 @@ mod tests {
|
||||
#[test]
|
||||
fn crash_task_marks_task_and_process_crashed() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
os.crash_task(task_id, None).expect("crash should succeed");
|
||||
os.lifecycle().crash_task(task_id, None).expect("crash should succeed");
|
||||
|
||||
assert_eq!(
|
||||
os.task_manager.get(task_id).expect("task should exist").state,
|
||||
@@ -292,28 +178,34 @@ mod tests {
|
||||
fn lifecycle_operation_missing_task_returns_typed_error() {
|
||||
let mut os = SystemOS::new(None);
|
||||
|
||||
assert_eq!(os.suspend_task(TaskId(999)), Err(LifecycleError::TaskNotFound(TaskId(999))));
|
||||
assert_eq!(
|
||||
os.lifecycle().suspend_task(TaskId(999)),
|
||||
Err(LifecycleError::TaskNotFound(TaskId(999)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lifecycle_operation_missing_process_returns_typed_error() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
let process_id = os.task_manager.get(task_id).expect("task should exist").process_id;
|
||||
|
||||
os.process_manager.mark_stopped(process_id);
|
||||
os.process_manager.remove_stopped();
|
||||
|
||||
assert_eq!(os.suspend_task(task_id), Err(LifecycleError::ProcessNotFound(process_id)));
|
||||
assert_eq!(
|
||||
os.lifecycle().suspend_task(task_id),
|
||||
Err(LifecycleError::ProcessNotFound(process_id))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resume_task_from_non_suspended_state_returns_invalid_transition() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let task_id = os.create_vm_game_task(42, "Sector Crawl");
|
||||
let task_id = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
|
||||
assert_eq!(
|
||||
os.resume_task(task_id),
|
||||
os.lifecycle().resume_task(task_id),
|
||||
Err(LifecycleError::InvalidTransition {
|
||||
task_id,
|
||||
from: TaskState::Foreground,
|
||||
|
||||
@@ -61,14 +61,15 @@ impl PrometeuHub {
|
||||
}
|
||||
|
||||
if let Some((title, rect, color)) = next_window {
|
||||
os.window_manager.remove_all_windows();
|
||||
let id = os.window_manager.add_window(title, rect, color);
|
||||
os.window_manager.set_focus(id);
|
||||
let mut window = os.window();
|
||||
window.remove_all_windows();
|
||||
let id = window.add_window(title, rect, color);
|
||||
window.set_focus(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, os: &mut SystemOS, hw: &mut dyn HardwareBridge) {
|
||||
for window in &os.window_manager.windows {
|
||||
for window in os.window().windows() {
|
||||
hw.gfx_mut().fill_rect(
|
||||
window.viewport.x,
|
||||
window.viewport.y,
|
||||
@@ -90,17 +91,17 @@ impl PrometeuHub {
|
||||
|
||||
self.gui_update(os, hw);
|
||||
|
||||
if let Some(focused_id) = os.window_manager.focused {
|
||||
if let Some(focused_id) = os.window().focused_window() {
|
||||
if hw.pad().start().down {
|
||||
os.window_manager.remove_window(focused_id);
|
||||
os.window().close_window(focused_id);
|
||||
} else {
|
||||
crash = os.tick_vm(vm, signals, hw);
|
||||
crash = os.vm().tick(vm, signals, hw);
|
||||
}
|
||||
}
|
||||
|
||||
self.render(os, hw);
|
||||
hw.gfx_mut().present();
|
||||
|
||||
SystemProfileUpdate { crash, focused_window_active: os.window_manager.focused.is_some() }
|
||||
SystemProfileUpdate { crash, focused_window_active: os.window().focused_window().is_some() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user