diff --git a/src/checkerr.rs b/src/checkerr.rs new file mode 100644 index 0000000..4f39bf7 --- /dev/null +++ b/src/checkerr.rs @@ -0,0 +1,8 @@ +#[macro_export] +macro_rules! checkerr { + ($ex:expr) => { + if $ex.is_err() { + return Err(()); + } + }; +} diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs index 977c237..d5d7bcd 100644 --- a/src/file_builders/active_runtime_json.rs +++ b/src/file_builders/active_runtime_json.rs @@ -1,4 +1,5 @@ use crate::{ + checkerr, file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly}, paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir, SYSTEM_PREFIX}, profile::{Profile, XRServiceType}, @@ -69,13 +70,18 @@ pub fn get_current_active_runtime() -> Option { get_active_runtime_from_path(&get_active_runtime_json_path()) } -fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) { +fn dump_active_runtime_to_path( + active_runtime: &ActiveRuntime, + path_s: &String, +) -> Result<(), serde_json::Error> { let writer = get_writer(path_s); - serde_json::to_writer_pretty(writer, active_runtime).expect("Unable to save active runtime"); + serde_json::to_writer_pretty(writer, active_runtime) } -pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) { - dump_active_runtime_to_path(active_runtime, &get_active_runtime_json_path()); +pub fn dump_current_active_runtime( + active_runtime: &ActiveRuntime, +) -> Result<(), serde_json::Error> { + dump_active_runtime_to_path(active_runtime, &get_active_runtime_json_path()) } fn build_steam_active_runtime() -> ActiveRuntime { @@ -96,9 +102,10 @@ fn build_steam_active_runtime() -> ActiveRuntime { } } -pub fn set_current_active_runtime_to_steam() { - set_file_readonly(&get_active_runtime_json_path(), false); - dump_current_active_runtime(&build_steam_active_runtime()) +pub fn set_current_active_runtime_to_steam() -> Result<(), ()> { + checkerr!(set_file_readonly(&get_active_runtime_json_path(), false)); + checkerr!(dump_current_active_runtime(&build_steam_active_runtime())); + Ok(()) } pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime { @@ -119,9 +126,9 @@ pub fn build_profile_active_runtime(profile: &Profile) -> ActiveRuntime { } } -pub fn set_current_active_runtime_to_profile(profile: &Profile) { +pub fn set_current_active_runtime_to_profile(profile: &Profile) -> Result<(), ()> { let dest = get_active_runtime_json_path(); - set_file_readonly(&dest, false); + checkerr!(set_file_readonly(&dest, false)); backup_steam_active_runtime(); let pfx = profile.clone().prefix; let mut ar = build_profile_active_runtime(profile); @@ -139,8 +146,9 @@ pub fn set_current_active_runtime_to_profile(profile: &Profile) { rels = rel_chain.join("/") ); } - dump_current_active_runtime(&ar); - set_file_readonly(&dest, true); + checkerr!(dump_current_active_runtime(&ar)); + checkerr!(set_file_readonly(&dest, true)); + Ok(()) } #[cfg(test)] @@ -175,6 +183,7 @@ mod tests { name: Some("SteamVR".into()), }, }; - dump_active_runtime_to_path(&ar, &"./target/testout/active_runtime.json.steamvr".into()); + dump_active_runtime_to_path(&ar, &"./target/testout/active_runtime.json.steamvr".into()) + .expect("could not dump active runtime to path"); } } diff --git a/src/file_builders/openvrpaths_vrpath.rs b/src/file_builders/openvrpaths_vrpath.rs index fadf8a1..da5533a 100644 --- a/src/file_builders/openvrpaths_vrpath.rs +++ b/src/file_builders/openvrpaths_vrpath.rs @@ -1,4 +1,5 @@ use crate::{ + checkerr, file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly}, paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir}, profile::Profile, @@ -68,12 +69,15 @@ pub fn get_current_openvrpaths() -> Option { get_openvrpaths_from_path(&get_openvrpaths_vrpath_path()) } -fn dump_openvrpaths_to_path(ovr_paths: &OpenVrPaths, path_s: &String) { +fn dump_openvrpaths_to_path( + ovr_paths: &OpenVrPaths, + path_s: &String, +) -> Result<(), serde_json::Error> { let writer = get_writer(path_s); - serde_json::to_writer_pretty(writer, ovr_paths).expect("Unable to save openvrpaths"); + serde_json::to_writer_pretty(writer, ovr_paths) } -pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) { +pub fn dump_current_openvrpaths(ovr_paths: &OpenVrPaths) -> Result<(), serde_json::Error> { dump_openvrpaths_to_path(ovr_paths, &get_openvrpaths_vrpath_path()) } @@ -96,9 +100,10 @@ fn build_steam_openvrpaths() -> OpenVrPaths { } } -pub fn set_current_openvrpaths_to_steam() { - set_file_readonly(&get_openvrpaths_vrpath_path(), false); - dump_current_openvrpaths(&build_steam_openvrpaths()) +pub fn set_current_openvrpaths_to_steam() -> Result<(), ()> { + checkerr!(set_file_readonly(&get_openvrpaths_vrpath_path(), false)); + checkerr!(dump_current_openvrpaths(&build_steam_openvrpaths())); + Ok(()) } pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths { @@ -116,12 +121,13 @@ pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths { } } -pub fn set_current_openvrpaths_to_profile(profile: &Profile) { +pub fn set_current_openvrpaths_to_profile(profile: &Profile) -> Result<(), ()> { let dest = get_openvrpaths_vrpath_path(); - set_file_readonly(&dest, false); + checkerr!(set_file_readonly(&dest, false)); backup_steam_openvrpaths(); - dump_current_openvrpaths(&build_profile_openvrpaths(profile)); - set_file_readonly(&dest, true); + checkerr!(dump_current_openvrpaths(&build_profile_openvrpaths(profile))); + checkerr!(set_file_readonly(&dest, true)); + Ok(()) } #[cfg(test)] @@ -152,5 +158,6 @@ mod tests { version: 1, }; dump_openvrpaths_to_path(&ovrp, &"./target/testout/openvrpaths.vrpath".into()) + .expect("could not dump openvrpaths to path"); } } diff --git a/src/file_utils.rs b/src/file_utils.rs index b4614ad..2cc6fc6 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -52,17 +52,17 @@ pub fn deserialize_file(path_s: &String) -> Opti } } -pub fn set_file_readonly(path_s: &String, readonly: bool) { +pub fn set_file_readonly(path_s: &String, readonly: bool) -> Result<(), std::io::Error> { let path = Path::new(&path_s); if !path.is_file() { println!("WARN: trying to set readonly on a file that does not exist"); - return; + return Ok(()); } let mut perms = fs::metadata(path) .expect("Could not get metadata for file") .permissions(); perms.set_readonly(readonly); - fs::set_permissions(path, perms).expect("Could not set permissions for file") + fs::set_permissions(path, perms) } pub fn setcap_cap_sys_nice_eip(file: String) { diff --git a/src/main.rs b/src/main.rs index ac97c69..b29cf47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ pub mod runner_pipeline; pub mod ui; pub mod xr_devices; pub mod build_tools; +pub mod checkerr; fn restore_steam_xr_files() { let active_runtime = get_current_active_runtime(); diff --git a/src/ui/app.rs b/src/ui/app.rs index 3615d75..3392697 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -126,10 +126,23 @@ impl App { } pub fn start_xrservice(&mut self) { - self.set_inhibit_session(true); let prof = self.get_selected_profile(); - set_current_active_runtime_to_profile(&prof); - set_current_openvrpaths_to_profile(&prof); + if set_current_active_runtime_to_profile(&prof).is_err() { + alert( + "Failed to start XR Service", + Some("Error setting current active runtime to profile"), + Some(&self.app_win.clone().upcast::()), + ); + return; + } + if set_current_openvrpaths_to_profile(&prof).is_err() { + alert( + "Failed to start XR Service", + Some("Error setting current openvrpaths file to profile"), + Some(&self.app_win.clone().upcast::()), + ); + return; + }; self.devices_processed = match prof.xrservice_type { XRServiceType::Monado => false, XRServiceType::Wivrn => true, // no device from log in wivrn @@ -145,6 +158,7 @@ impl App { true, Some(self.get_selected_profile()), )); + self.set_inhibit_session(true); } Err(_) => { alert( @@ -159,15 +173,31 @@ impl App { }; } + pub fn restore_openxr_openvr_files(&self) { + if set_current_active_runtime_to_steam().is_err() { + alert( + "Could not restore Steam active runtime", + None, + Some(&self.app_win.clone().upcast::()), + ); + } + if set_current_openvrpaths_to_steam().is_err() { + alert( + "Could not restore Steam openvrpaths", + None, + Some(&self.app_win.clone().upcast::()), + ); + }; + } + pub fn shutdown_xrservice(&mut self) { + self.set_inhibit_session(false); if self.xrservice_runner.is_some() { if self.xrservice_runner.as_mut().unwrap().status() == RunnerStatus::Running { self.xrservice_runner.as_mut().unwrap().terminate(); } } - set_current_active_runtime_to_steam(); - set_current_openvrpaths_to_steam(); - self.set_inhibit_session(false); + self.restore_openxr_openvr_files(); self.main_view .sender() .emit(MainViewMsg::XRServiceActiveChanged(false, None)); @@ -231,8 +261,7 @@ impl SimpleComponent for App { self.xrservice_runner.as_mut().unwrap().terminate(); } } - set_current_active_runtime_to_steam(); - set_current_openvrpaths_to_steam(); + self.restore_openxr_openvr_files(); } fn update(&mut self, message: Self::Input, sender: ComponentSender) {