diff --git a/apps/app-loader/src/api.rs b/apps/app-loader/src/api.rs new file mode 100644 index 000000000..083a9834c --- /dev/null +++ b/apps/app-loader/src/api.rs @@ -0,0 +1,101 @@ +use gam::{Gam, MenuItem, UxRegistration, menu_matic, APP_NAME_APP_LOADER, APP_MENU_0_APP_LOADER, MenuMatic}; +use xous_ipc::Buffer; +use num_traits::ToPrimitive; +use crate::SERVER_NAME_APP_LOADER; + +pub(crate) struct AppLoader { + gam: Gam, + auth: [u32; 4], + ticktimer: ticktimer_server::Ticktimer, + menu: MenuMatic, + conn: xous::CID, + apps: Vec>, +} + +impl AppLoader { + pub(crate) fn new(xns: &xous_names::XousNames, sid: &xous::SID) -> AppLoader { + // gam + let gam = Gam::new(xns).expect("can't connect to GAM"); + + // the gam token + let auth = gam.register_ux(UxRegistration { + app_name: xous_ipc::String::from_str(APP_NAME_APP_LOADER), + ux_type: gam::UxType::Modal, + predictor: None, + listener: sid.to_array(), + redraw_id: Opcode::Redraw.to_u32().unwrap(), + gotinput_id: None, + audioframe_id: None, + focuschange_id: None, + rawkeys_id: None + }).expect("Couldn't register").expect("Didn't get an auth token"); + + // a connection to the server + let conn = xns.request_connection_blocking(SERVER_NAME_APP_LOADER).expect("Couldn't connect to server"); + + // the ticktimer + let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); + + // the menu + let menu = menu_matic(Vec::new(), APP_MENU_0_APP_LOADER, Some(xous::create_server().unwrap())).expect("Couldn't create menu"); + + AppLoader{ gam, auth, conn, ticktimer, menu, apps: Vec::new() } + } + + pub(crate) fn add_app(&mut self, name: &str) { + let buf = Buffer::into_buf(LoadAppRequest { name: xous_ipc::String::from_str(name) }) + .expect("Couldn't create buffer"); + buf.send(self.conn, + Opcode::LoadApp.to_u32().unwrap()).expect("Couldn't send message"); + + // add its name to GAM + self.gam.register_name(name, self.auth).expect("Couldn't register name"); + + // add it to the menu + self.menu.add_item(MenuItem { name: xous_ipc::String::from_str(name), + action_conn: Some(self.conn), + action_opcode: Opcode::DispatchApp.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([self.apps.len().try_into().unwrap(), 0, 0, 0]), + close_on_select: true }); + self.apps.push(xous_ipc::String::from_str(name)); + } + + pub(crate) fn dispatch_app(&self, index: usize) { + if index < self.apps.len() { + let name: xous_ipc::String<64> = self.apps[index]; + log::info!("Switching to app `{}'", name); + self.gam.switch_to_app(name.as_ref(), self.auth).expect(&format!("Could not dispatch app `{}'", name)); + } else { + panic!("Unrecognized app index"); + } + } + + pub(crate) fn redraw(&self) { + // Properly close the app menu + // for anyone who needs this I found this in Menu::key_event + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + + self.ticktimer.sleep_ms(100).ok(); // yield for a moment to allow the previous menu to close + + // open the submenu + self.gam.raise_menu(APP_MENU_0_APP_LOADER).expect("Couldn't raise menu"); + } +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum Opcode { + /// to load an app into ram + LoadApp, + + /// dispatch the app + DispatchApp, + + /// Redraw the UI + Redraw +} + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub(crate) struct LoadAppRequest { + pub name: xous_ipc::String<64> +} diff --git a/apps/app-loader/src/main.rs b/apps/app-loader/src/main.rs index 7fdb38853..47c09aac8 100644 --- a/apps/app-loader/src/main.rs +++ b/apps/app-loader/src/main.rs @@ -1,26 +1,10 @@ -use gam::{Gam, MenuItem, UxRegistration, menu_matic, APP_NAME_APP_LOADER, APP_MENU_0_APP_LOADER}; +mod api; +use api::*; +use num_traits::FromPrimitive; use xous_ipc::Buffer; -use num_traits::{ToPrimitive, FromPrimitive}; const SERVER_NAME_APP_LOADER: &str = "_App Loader_"; -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -enum Opcode { - /// to load an app into ram - LoadApp, - - /// dispatch the app - DispatchApp, - - /// Redraw the UI - Redraw -} - -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -struct LoadAppRequest { - pub name: xous_ipc::String<64> -} - fn main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -28,32 +12,10 @@ fn main() -> ! { let xns = xous_names::XousNames::new().unwrap(); let sid = xns.register_name(SERVER_NAME_APP_LOADER, None).expect("Couldn't create server"); - let self_conn = xns.request_connection_blocking(SERVER_NAME_APP_LOADER).expect("Couldn't connect to server"); - - // the ticktimer - let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); - - let gam = Gam::new(&xns).expect("can't connect to GAM"); - let auth = gam.register_ux(UxRegistration { - app_name: xous_ipc::String::from_str(APP_NAME_APP_LOADER), - ux_type: gam::UxType::Modal, - predictor: None, - listener: sid.to_array(), - redraw_id: Opcode::Redraw.to_u32().unwrap(), - gotinput_id: None, - audioframe_id: None, - focuschange_id: None, - rawkeys_id: None - }).expect("Couldn't register").expect("Didn't get an auth token"); - - let menu = menu_matic(Vec::new(), APP_MENU_0_APP_LOADER, Some(xous::create_server().unwrap())).expect("Couldn't create menu"); - - let mut apps = Vec::new(); // start off by adding hello world - let buf = Buffer::into_buf(LoadAppRequest { name: xous_ipc::String::from_str("Hello") }).expect("Couldn't create buffer"); - buf.send(self_conn, - Opcode::LoadApp.to_u32().unwrap()).expect("Couldn't send message"); + let mut app_loader = AppLoader::new(&xns, &sid); + app_loader.add_app("Hello"); loop { let msg = xous::receive_message(sid).unwrap(); @@ -74,10 +36,7 @@ fn main() -> ! { xous::Message::new_blocking_scalar(2, 1, 2, 3, 4), ) .unwrap(); - log::info!("Result of ping: {:?}", result); - - // add its name to GAM - gam.register_name(load_app_req.name.to_str(), auth).expect("Couldn't register name"); + assert_eq!(xous::Result::Scalar1(2), result); // load the app from the binary file let bin = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/hello"); @@ -86,34 +45,14 @@ fn main() -> ! { let buf = unsafe { xous::MemoryRange::new(bin_loc & !0xFFF, bin_len + if bin_len & 0xFFF == 0 { 0 } else {0x1000 - (bin_len & 0xFFF)}).expect("Couldn't create a buffer from the segment") }; xous::send_message(spawn.cid, xous::Message::new_lend(1, buf, None, None)).expect("Couldn't send a message to spawn"); - menu.add_item(MenuItem { name: load_app_req.name, - action_conn: Some(self_conn), - action_opcode: Opcode::DispatchApp.to_u32().unwrap(), - action_payload: gam::MenuPayload::Scalar([apps.len().try_into().unwrap(), 0, 0, 0]), - close_on_select: true }); - apps.push(load_app_req.name); - log::info!("Added app `{}'!", load_app_req.name); + log::info!("Loaded app `{}'!", load_app_req.name); }, Opcode::DispatchApp => { let index = msg.body.scalar_message().expect("Not a scalar").arg1; - if index < apps.len() { - let name: xous_ipc::String<64> = apps[index]; - log::info!("Switching to app `{}'", name); - gam.switch_to_app(name.as_ref(), auth).expect(&format!("Could not dispatch app `{}'", name)); - } else { - panic!("Unrecognized app index"); - } + app_loader.dispatch_app(index); }, Opcode::Redraw => { - // Properly close the app menu - // for anyone who needs this I found this in Menu::key_event - gam.relinquish_focus().unwrap(); - xous::yield_slice(); - - ticktimer.sleep_ms(100).ok(); // yield for a moment to allow the previous menu to close - - // open the submenu - gam.raise_menu(APP_MENU_0_APP_LOADER).expect("Couldn't raise menu"); + app_loader.redraw(); }, } }