129 lines
5.1 KiB
Rust
129 lines
5.1 KiB
Rust
use crate::firmware::firmware_state::{
|
|
AppCrashesStep, FirmwareState, LoadCartridgeStep, ShellRunningStep,
|
|
};
|
|
use crate::firmware::prometeu_context::PrometeuContext;
|
|
use crate::firmware::resident_game_termination;
|
|
use prometeu_hal::app_mode::AppMode;
|
|
use prometeu_hal::cartridge_loader::CartridgeLoader;
|
|
use prometeu_hal::log::{LogLevel, LogSource};
|
|
use prometeu_hal::primitives::Rect;
|
|
use prometeu_system::SystemProfileAction;
|
|
use prometeu_system::windows::WindowOwner;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct HubHomeStep;
|
|
|
|
impl HubHomeStep {
|
|
pub fn on_enter(&mut self, ctx: &mut PrometeuContext) {
|
|
ctx.os.log(LogLevel::Info, LogSource::Hub, 0, "Entering HubHome".to_string());
|
|
ctx.os.windows().remove_all_windows();
|
|
ctx.hub.init();
|
|
}
|
|
|
|
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
|
let outcome = ctx.hub.update_shell_profile(ctx.os, ctx.platform);
|
|
if let Some(report) = outcome.crash {
|
|
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
|
|
}
|
|
|
|
match outcome.action {
|
|
Some(SystemProfileAction::LaunchNativeShell(app)) => {
|
|
let task_id = match ctx
|
|
.os
|
|
.sessions()
|
|
.try_create_native_shell_task(app.app_id(), app.title())
|
|
{
|
|
Ok(task_id) => task_id,
|
|
Err(error) => {
|
|
ctx.os.log(
|
|
LogLevel::Warn,
|
|
LogSource::Hub,
|
|
0,
|
|
format!("Failed to launch native shell {}: {:?}", app.title(), error),
|
|
);
|
|
return None;
|
|
}
|
|
};
|
|
let id = ctx.os.windows().add_window(
|
|
app.title().to_string(),
|
|
WindowOwner::Task(task_id),
|
|
Rect { x: 40, y: 20, w: 400, h: 220 },
|
|
app.color(),
|
|
);
|
|
ctx.os.windows().set_focus(id);
|
|
return Some(FirmwareState::ShellRunning(ShellRunningStep::new(task_id)));
|
|
}
|
|
Some(SystemProfileAction::LaunchGame { target }) => {
|
|
match CartridgeLoader::load(&target.path) {
|
|
Ok(cartridge) => {
|
|
if cartridge.app_mode == AppMode::Game
|
|
&& let Some(resident_task_id) = ctx.os.lifecycle().resident_game_task()
|
|
{
|
|
let resident_app_id = ctx
|
|
.os
|
|
.sessions()
|
|
.vm_session_for_task(resident_task_id)
|
|
.map(|session| session.app_id)
|
|
.unwrap_or(0);
|
|
|
|
if resident_app_id == cartridge.app_id {
|
|
return Some(FirmwareState::ResumeResidentGame);
|
|
}
|
|
|
|
if let Err(error) = resident_game_termination::terminate_resident_game(
|
|
ctx.os,
|
|
ctx.platform,
|
|
false,
|
|
) {
|
|
ctx.os.log(
|
|
LogLevel::Error,
|
|
LogSource::Hub,
|
|
0,
|
|
format!(
|
|
"Failed to terminate resident game app_id {} before launching {}: {:?}",
|
|
resident_app_id, cartridge.title, error
|
|
),
|
|
);
|
|
return None;
|
|
}
|
|
}
|
|
|
|
return Some(FirmwareState::LoadCartridge(
|
|
LoadCartridgeStep::new_game_switch(cartridge),
|
|
));
|
|
}
|
|
Err(error) => {
|
|
ctx.os.log(
|
|
LogLevel::Error,
|
|
LogSource::Hub,
|
|
0,
|
|
format!(
|
|
"Failed to launch Home game {}: {:?}",
|
|
target.path.display(),
|
|
error
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
Some(SystemProfileAction::KillResidentGame) => {
|
|
if let Err(error) =
|
|
resident_game_termination::terminate_resident_game(ctx.os, ctx.platform, true)
|
|
{
|
|
ctx.os.log(
|
|
LogLevel::Error,
|
|
LogSource::Hub,
|
|
0,
|
|
format!("Failed to kill resident game from Hub: {error:?}"),
|
|
);
|
|
}
|
|
}
|
|
Some(SystemProfileAction::CloseShell) | None => {}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
pub fn on_exit(&mut self, _ctx: &mut PrometeuContext) {}
|
|
}
|