mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
format app-loader
This commit is contained in:
parent
3a1a1b53d0
commit
faafa9078b
2 changed files with 47 additions and 112 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::SERVER_NAME_APP_LOADER;
|
use std::io::Read;
|
||||||
|
|
||||||
use gam::{
|
use gam::{
|
||||||
menu_matic, Gam, MenuItem, MenuMatic, TextEntryPayload, UxRegistration, APP_MENU_0_APP_LOADER,
|
menu_matic, Gam, MenuItem, MenuMatic, TextEntryPayload, UxRegistration, APP_MENU_0_APP_LOADER,
|
||||||
APP_MENU_1_APP_LOADER, APP_NAME_APP_LOADER,
|
APP_MENU_1_APP_LOADER, APP_NAME_APP_LOADER,
|
||||||
|
|
@ -6,7 +7,8 @@ use gam::{
|
||||||
use locales::t;
|
use locales::t;
|
||||||
use modals::Modals;
|
use modals::Modals;
|
||||||
use num_traits::ToPrimitive;
|
use num_traits::ToPrimitive;
|
||||||
use std::io::Read;
|
|
||||||
|
use crate::SERVER_NAME_APP_LOADER;
|
||||||
|
|
||||||
pub(crate) struct AppLoader {
|
pub(crate) struct AppLoader {
|
||||||
gam: Gam,
|
gam: Gam,
|
||||||
|
|
@ -46,9 +48,8 @@ impl AppLoader {
|
||||||
let modals = Modals::new(xns).expect("Couldn't get modals");
|
let modals = Modals::new(xns).expect("Couldn't get modals");
|
||||||
|
|
||||||
// a connection to the server
|
// a connection to the server
|
||||||
let conn = xns
|
let conn =
|
||||||
.request_connection_blocking(SERVER_NAME_APP_LOADER)
|
xns.request_connection_blocking(SERVER_NAME_APP_LOADER).expect("Couldn't connect to server");
|
||||||
.expect("Couldn't connect to server");
|
|
||||||
|
|
||||||
// the ticktimer
|
// the ticktimer
|
||||||
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
|
let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer");
|
||||||
|
|
@ -74,12 +75,9 @@ impl AppLoader {
|
||||||
Some(xous::create_server().unwrap()),
|
Some(xous::create_server().unwrap()),
|
||||||
)
|
)
|
||||||
.expect("Couldn't create menu");
|
.expect("Couldn't create menu");
|
||||||
let load_menu = menu_matic(
|
let load_menu =
|
||||||
vec![close_item.clone()],
|
menu_matic(vec![close_item.clone()], APP_MENU_1_APP_LOADER, Some(xous::create_server().unwrap()))
|
||||||
APP_MENU_1_APP_LOADER,
|
.expect("Couldn't create menu");
|
||||||
Some(xous::create_server().unwrap()),
|
|
||||||
)
|
|
||||||
.expect("Couldn't create menu");
|
|
||||||
|
|
||||||
AppLoader {
|
AppLoader {
|
||||||
gam,
|
gam,
|
||||||
|
|
@ -108,9 +106,7 @@ impl AppLoader {
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
let response = match ureq::get(&format!(
|
let response = match ureq::get(&format!(
|
||||||
"{}/{}",
|
"{}/{}",
|
||||||
self.server
|
self.server.as_ref().expect("AddApp was somehow called without a server!"),
|
||||||
.as_ref()
|
|
||||||
.expect("AddApp was somehow called without a server!"),
|
|
||||||
name
|
name
|
||||||
))
|
))
|
||||||
.call()
|
.call()
|
||||||
|
|
@ -119,11 +115,7 @@ impl AppLoader {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.modals
|
self.modals
|
||||||
.show_notification(
|
.show_notification(
|
||||||
&format!(
|
&format!("{}{}", t!("apploader.addapp.server_error", locales::LANG), e),
|
||||||
"{}{}",
|
|
||||||
t!("apploader.addapp.server_error", locales::LANG),
|
|
||||||
e
|
|
||||||
),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("Couldn't show modal");
|
.expect("Couldn't show modal");
|
||||||
|
|
@ -132,15 +124,10 @@ impl AppLoader {
|
||||||
};
|
};
|
||||||
|
|
||||||
let len = match response.header("Content-Length") {
|
let len = match response.header("Content-Length") {
|
||||||
Some(len) => len
|
Some(len) => len.parse::<usize>().expect("Couldn't parse Content-Length header"),
|
||||||
.parse::<usize>()
|
|
||||||
.expect("Couldn't parse Content-Length header"),
|
|
||||||
None => {
|
None => {
|
||||||
self.modals
|
self.modals
|
||||||
.show_notification(
|
.show_notification(t!("apploader.addapp.content_length_error", locales::LANG), None)
|
||||||
t!("apploader.addapp.content_length_error", locales::LANG),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.expect("Couldn't show modal");
|
.expect("Couldn't show modal");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -149,11 +136,7 @@ impl AppLoader {
|
||||||
let mut memory = xous::map_memory(
|
let mut memory = xous::map_memory(
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
len + if len & 0xFFF == 0 {
|
len + if len & 0xFFF == 0 { 0 } else { 0x1000 - (len & 0xFFF) },
|
||||||
0
|
|
||||||
} else {
|
|
||||||
0x1000 - (len & 0xFFF)
|
|
||||||
},
|
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||||
)
|
)
|
||||||
.expect("Couldn't map memory");
|
.expect("Couldn't map memory");
|
||||||
|
|
@ -162,9 +145,7 @@ impl AppLoader {
|
||||||
.read_exact(&mut unsafe { memory.as_slice_mut() }[..len])
|
.read_exact(&mut unsafe { memory.as_slice_mut() }[..len])
|
||||||
.expect("Couldn't read");
|
.expect("Couldn't read");
|
||||||
|
|
||||||
self.modals
|
self.modals.update_progress(1).expect("Couldn't update progress");
|
||||||
.update_progress(1)
|
|
||||||
.expect("Couldn't update progress");
|
|
||||||
//////////////////////
|
//////////////////////
|
||||||
// The loading part //
|
// The loading part //
|
||||||
//////////////////////
|
//////////////////////
|
||||||
|
|
@ -181,31 +162,22 @@ impl AppLoader {
|
||||||
|
|
||||||
// perform a ping to make sure that spawn is running
|
// perform a ping to make sure that spawn is running
|
||||||
let result =
|
let result =
|
||||||
xous::send_message(spawn.cid, xous::Message::new_blocking_scalar(2, 1, 2, 3, 4))
|
xous::send_message(spawn.cid, xous::Message::new_blocking_scalar(2, 1, 2, 3, 4)).unwrap();
|
||||||
.unwrap();
|
|
||||||
assert_eq!(xous::Result::Scalar1(2), result);
|
assert_eq!(xous::Result::Scalar1(2), result);
|
||||||
|
|
||||||
self.modals
|
self.modals.update_progress(2).expect("Couldn't update progress");
|
||||||
.update_progress(2)
|
|
||||||
.expect("Couldn't update progress");
|
|
||||||
|
|
||||||
// load the app from the binary file
|
// load the app from the binary file
|
||||||
let res = xous::send_message(
|
let res = xous::send_message(spawn.cid, xous::Message::new_lend_mut(1, memory, None, None))
|
||||||
spawn.cid,
|
.expect("Couldn't send a message to spawn");
|
||||||
xous::Message::new_lend_mut(1, memory, None, None),
|
// we are just going to do some very basic error handling: if the "offset" is None, we are good,
|
||||||
)
|
// otherwise there was a problem TODO: make this better. Perhaps Buffer::from_raw_parts?
|
||||||
.expect("Couldn't send a message to spawn");
|
|
||||||
// we are just going to do some very basic error handling: if the "offset" is None, we are good, otherwise there was a problem
|
|
||||||
// TODO: make this better. Perhaps Buffer::from_raw_parts?
|
|
||||||
match res {
|
match res {
|
||||||
xous::Result::MemoryReturned(None, _) => self
|
xous::Result::MemoryReturned(None, _) => {
|
||||||
.modals
|
self.modals.update_progress(3).expect("Couldn't update progress")
|
||||||
.update_progress(3)
|
}
|
||||||
.expect("Couldn't update progress"),
|
|
||||||
_ => {
|
_ => {
|
||||||
self.modals
|
self.modals.finish_progress().expect("Couldn't close progressbar");
|
||||||
.finish_progress()
|
|
||||||
.expect("Couldn't close progressbar");
|
|
||||||
self.modals
|
self.modals
|
||||||
.show_notification(t!("apploader.addapp.error", locales::LANG), None)
|
.show_notification(t!("apploader.addapp.error", locales::LANG), None)
|
||||||
.expect("Couldn't show modal");
|
.expect("Couldn't show modal");
|
||||||
|
|
@ -218,9 +190,7 @@ impl AppLoader {
|
||||||
//////////////////////
|
//////////////////////
|
||||||
|
|
||||||
// add its name to GAM
|
// add its name to GAM
|
||||||
self.gam
|
self.gam.register_name(name.to_str(), self.auth).expect("Couldn't register name");
|
||||||
.register_name(name.to_str(), self.auth)
|
|
||||||
.expect("Couldn't register name");
|
|
||||||
for menu in 0..menus {
|
for menu in 0..menus {
|
||||||
self.gam
|
self.gam
|
||||||
.register_name(&format!("{} Submenu {}", name, menu), self.auth)
|
.register_name(&format!("{} Submenu {}", name, menu), self.auth)
|
||||||
|
|
@ -233,12 +203,7 @@ impl AppLoader {
|
||||||
name,
|
name,
|
||||||
action_conn: Some(self.conn),
|
action_conn: Some(self.conn),
|
||||||
action_opcode: Opcode::DispatchApp.to_u32().unwrap(),
|
action_opcode: Opcode::DispatchApp.to_u32().unwrap(),
|
||||||
action_payload: gam::MenuPayload::Scalar([
|
action_payload: gam::MenuPayload::Scalar([self.apps.len().try_into().unwrap(), 0, 0, 0]),
|
||||||
self.apps.len().try_into().unwrap(),
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
]),
|
|
||||||
close_on_select: true,
|
close_on_select: true,
|
||||||
},
|
},
|
||||||
0,
|
0,
|
||||||
|
|
@ -247,9 +212,7 @@ impl AppLoader {
|
||||||
|
|
||||||
log::info!("Added app `{}'!", name);
|
log::info!("Added app `{}'!", name);
|
||||||
|
|
||||||
self.modals
|
self.modals.finish_progress().expect("Couldn't close progressbar");
|
||||||
.finish_progress()
|
|
||||||
.expect("Couldn't close progressbar");
|
|
||||||
let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu
|
let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,16 +222,14 @@ impl AppLoader {
|
||||||
.alert_builder("Server Address")
|
.alert_builder("Server Address")
|
||||||
.field(
|
.field(
|
||||||
Some("e.g. http://ip:port".to_string()),
|
Some("e.g. http://ip:port".to_string()),
|
||||||
Some(
|
Some(|payload: TextEntryPayload| match url::Url::parse(payload.as_str()) {
|
||||||
|payload: TextEntryPayload| match url::Url::parse(payload.as_str()) {
|
Ok(_) => None,
|
||||||
Ok(_) => None,
|
Err(e) => Some(xous_ipc::String::from_str(&format!(
|
||||||
Err(e) => Some(xous_ipc::String::from_str(&format!(
|
"{}{}",
|
||||||
"{}{}",
|
t!("apploader.setserver.error", locales::LANG),
|
||||||
t!("apploader.setserver.error", locales::LANG),
|
e
|
||||||
e
|
))),
|
||||||
))),
|
}),
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
.ok();
|
.ok();
|
||||||
|
|
@ -276,10 +237,7 @@ impl AppLoader {
|
||||||
if self.server.is_none() && payload.is_some() {
|
if self.server.is_none() && payload.is_some() {
|
||||||
self.menu.insert_item(
|
self.menu.insert_item(
|
||||||
MenuItem {
|
MenuItem {
|
||||||
name: xous_ipc::String::from_str(t!(
|
name: xous_ipc::String::from_str(t!("apploader.menu.reloadapplist", locales::LANG)),
|
||||||
"apploader.menu.reloadapplist",
|
|
||||||
locales::LANG
|
|
||||||
)),
|
|
||||||
action_conn: Some(self.conn),
|
action_conn: Some(self.conn),
|
||||||
action_opcode: Opcode::ReloadAppList.to_u32().unwrap(),
|
action_opcode: Opcode::ReloadAppList.to_u32().unwrap(),
|
||||||
action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]),
|
action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]),
|
||||||
|
|
@ -311,39 +269,26 @@ impl AppLoader {
|
||||||
pub(crate) fn reload_app_list(&mut self) {
|
pub(crate) fn reload_app_list(&mut self) {
|
||||||
// without a path, the server responds with a JSON list of strings representing the list of app names
|
// without a path, the server responds with a JSON list of strings representing the list of app names
|
||||||
self.modals
|
self.modals
|
||||||
.start_progress(
|
.start_progress(t!("apploader.reloadapplist.loading", locales::LANG), 0, 3, 0)
|
||||||
t!("apploader.reloadapplist.loading", locales::LANG),
|
|
||||||
0,
|
|
||||||
3,
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
.expect("Couldn't start progressbar");
|
.expect("Couldn't start progressbar");
|
||||||
|
|
||||||
let old = self.possible_apps.clone();
|
let old = self.possible_apps.clone();
|
||||||
|
|
||||||
// this... disgusting error handling is so that if there is an error on the server side there isn't a panic
|
// this... disgusting error handling is so that if there is an error on the server side there isn't a
|
||||||
|
// panic
|
||||||
self.possible_apps = match match ureq::get(
|
self.possible_apps = match match ureq::get(
|
||||||
&self
|
&self.server.as_ref().expect("ReloadAppList was somehow called without a server!"),
|
||||||
.server
|
|
||||||
.as_ref()
|
|
||||||
.expect("ReloadAppList was somehow called without a server!"),
|
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
{
|
{
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
self.modals
|
self.modals.update_progress(1).expect("Couldn't update progress");
|
||||||
.update_progress(1)
|
|
||||||
.expect("Couldn't update progress");
|
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.modals
|
self.modals
|
||||||
.show_notification(
|
.show_notification(
|
||||||
&format!(
|
&format!("{}{}", t!("apploader.reloadapplist.connection_error", locales::LANG), e),
|
||||||
"{}{}",
|
|
||||||
t!("apploader.reloadapplist.connection_error", locales::LANG),
|
|
||||||
e
|
|
||||||
),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("Couldn't show modal");
|
.expect("Couldn't show modal");
|
||||||
|
|
@ -353,19 +298,13 @@ impl AppLoader {
|
||||||
.into_json::<Vec<(String, usize)>>()
|
.into_json::<Vec<(String, usize)>>()
|
||||||
{
|
{
|
||||||
Ok(json) => {
|
Ok(json) => {
|
||||||
self.modals
|
self.modals.update_progress(2).expect("Couldn't update progress");
|
||||||
.update_progress(2)
|
|
||||||
.expect("Couldn't update progress");
|
|
||||||
json
|
json
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.modals
|
self.modals
|
||||||
.show_notification(
|
.show_notification(
|
||||||
&format!(
|
&format!("{}{}", t!("apploader.reloadapplist.json_error", locales::LANG), e),
|
||||||
"{}{}",
|
|
||||||
t!("apploader.reloadapplist.json_error", locales::LANG),
|
|
||||||
e
|
|
||||||
),
|
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
.expect("Couldn't show modal");
|
.expect("Couldn't show modal");
|
||||||
|
|
@ -392,9 +331,7 @@ impl AppLoader {
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.modals
|
self.modals.finish_progress().expect("Couldn't close progress bar");
|
||||||
.finish_progress()
|
|
||||||
.expect("Couldn't close progress bar");
|
|
||||||
|
|
||||||
let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu
|
let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,7 @@ fn main() -> ! {
|
||||||
log::info!("my PID is {}", xous::process::id());
|
log::info!("my PID is {}", xous::process::id());
|
||||||
|
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
let sid = xns
|
let sid = xns.register_name(SERVER_NAME_APP_LOADER, None).expect("Couldn't create server");
|
||||||
.register_name(SERVER_NAME_APP_LOADER, None)
|
|
||||||
.expect("Couldn't create server");
|
|
||||||
|
|
||||||
// start off by adding hello world
|
// start off by adding hello world
|
||||||
let mut app_loader = AppLoader::new(&xns, &sid);
|
let mut app_loader = AppLoader::new(&xns, &sid);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue