diff --git a/backend/src/settings/detect/auto_detect.rs b/backend/src/settings/detect/auto_detect.rs index 9a3c550..18a6fd2 100644 --- a/backend/src/settings/detect/auto_detect.rs +++ b/backend/src/settings/detect/auto_detect.rs @@ -7,10 +7,29 @@ use limits_core::json::{Limits, BatteryLimit, CpuLimit, GpuLimit}; use crate::persist::{DriverJson, SettingsJson}; use crate::settings::{TGeneral, TCpus, TGpu, TBattery, Driver, General}; +fn get_limits() -> limits_core::json::Base { + let limits_path = super::utility::limits_path(); + match File::open(&limits_path) { + Ok(f) => { + match serde_json::from_reader(f) { + Ok(lim) => lim, + Err(e) => { + log::warn!("Failed to parse limits file `{}`, cannot use for auto_detect: {}", limits_path.display(), e); + limits_core::json::Base::default() + } + } + }, + Err(e) => { + log::warn!("Failed to open limits file `{}` (trying force refresh...): {}", limits_path.display(), e); + super::limits_worker::get_limits_blocking() + } + } +} + #[inline] pub fn auto_detect_provider() -> DriverJson { let provider = auto_detect0(None, crate::utility::settings_dir().join("autodetect.json")) - .general + .battery .provider(); //log::info!("Detected device automatically, compatible driver: {:?}", provider); provider @@ -27,22 +46,7 @@ pub fn auto_detect0(settings_opt: Option, json_path: std::path::Pa let dmi_info: String = std::process::Command::new("dmidecode").output().map(|out| String::from_utf8_lossy(&out.stdout).into_owned()).unwrap_or_default(); log::debug!("Read dmidecode:\n{}", dmi_info); - let limits_path = super::utility::limits_path(); - let limits = match File::open(&limits_path) { - Ok(f) => { - match serde_json::from_reader(f) { - Ok(lim) => lim, - Err(e) => { - log::warn!("Failed to parse limits file `{}`, cannot use for auto_detect: {}", limits_path.display(), e); - limits_core::json::Base::default() - } - } - }, - Err(e) => { - log::warn!("Failed to open limits file `{}` (trying force refresh...): {}", limits_path.display(), e); - super::limits_worker::get_limits_blocking() - } - }; + let limits = get_limits(); // build driver based on limits conditions for conf in limits.configs { diff --git a/backend/src/settings/detect/limits_worker.rs b/backend/src/settings/detect/limits_worker.rs index 0ee61a3..ef1f0f2 100644 --- a/backend/src/settings/detect/limits_worker.rs +++ b/backend/src/settings/detect/limits_worker.rs @@ -37,15 +37,7 @@ pub fn spawn() -> JoinHandle<()> { let json_res: std::io::Result = response.into_json(); match json_res { Ok(new_base) => { - match std::fs::File::create(&limits_path) { - Ok(f) => { - match serde_json::to_writer_pretty(f, &new_base) { - Ok(_) => log::info!("Successfully updated limits from `{}`, cached at {}", refresh, limits_path.display()), - Err(e) => log::error!("Failed to save limits json to file `{}`: {}", limits_path.display(), e), - } - }, - Err(e) => log::error!("Cannot create {}: {}", limits_path.display(), e) - } + save_base(&new_base, &limits_path); }, Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e), } @@ -98,7 +90,10 @@ pub fn get_limits_blocking() -> Base { Ok(response) => { let json_res: std::io::Result = response.into_json(); match json_res { - Ok(new_base) => return new_base, + Ok(new_base) => { + save_base(&new_base, &limits_path); + return new_base; + }, Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e) } }, @@ -108,3 +103,17 @@ pub fn get_limits_blocking() -> Base { Base::default() } } + +#[cfg(feature = "online")] +fn save_base(new_base: &Base, path: impl AsRef) { + let limits_path = path.as_ref(); + match std::fs::File::create(&limits_path) { + Ok(f) => { + match serde_json::to_writer_pretty(f, &new_base) { + Ok(_) => log::info!("Successfully saved new limits to {}", limits_path.display()), + Err(e) => log::error!("Failed to save limits json to file `{}`: {}", limits_path.display(), e), + } + }, + Err(e) => log::error!("Cannot create {}: {}", limits_path.display(), e) + } +} diff --git a/backend/src/settings/generic/battery.rs b/backend/src/settings/generic/battery.rs index 6eb99c2..bd300b7 100644 --- a/backend/src/settings/generic/battery.rs +++ b/backend/src/settings/generic/battery.rs @@ -122,4 +122,8 @@ impl TBattery for Battery { fn read_current_now(&self) -> Option { None } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Generic + } } diff --git a/backend/src/settings/generic/cpu.rs b/backend/src/settings/generic/cpu.rs index d29153e..df5a6d4 100644 --- a/backend/src/settings/generic/cpu.rs +++ b/backend/src/settings/generic/cpu.rs @@ -187,6 +187,10 @@ impl TCpus for Cpus { fn len(&self) -> usize { self.cpus.len() } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Generic + } } #[derive(Debug, Clone)] diff --git a/backend/src/settings/generic/gpu.rs b/backend/src/settings/generic/gpu.rs index 1782ac5..3344915 100644 --- a/backend/src/settings/generic/gpu.rs +++ b/backend/src/settings/generic/gpu.rs @@ -99,4 +99,8 @@ impl TGpu for Gpu { fn slow_memory(&mut self) -> &mut bool { &mut self.slow_memory } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Generic + } } diff --git a/backend/src/settings/steam_deck/battery.rs b/backend/src/settings/steam_deck/battery.rs index 8cee15e..13fe425 100644 --- a/backend/src/settings/steam_deck/battery.rs +++ b/backend/src/settings/steam_deck/battery.rs @@ -305,4 +305,8 @@ impl TBattery for Battery { } } } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::SteamDeck + } } diff --git a/backend/src/settings/steam_deck/cpu.rs b/backend/src/settings/steam_deck/cpu.rs index 845e3fe..8f02867 100644 --- a/backend/src/settings/steam_deck/cpu.rs +++ b/backend/src/settings/steam_deck/cpu.rs @@ -151,6 +151,10 @@ impl TCpus for Cpus { fn len(&self) -> usize { self.cpus.len() } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::SteamDeck + } } #[derive(Debug, Clone)] diff --git a/backend/src/settings/steam_deck/gpu.rs b/backend/src/settings/steam_deck/gpu.rs index 665a7df..59827fe 100644 --- a/backend/src/settings/steam_deck/gpu.rs +++ b/backend/src/settings/steam_deck/gpu.rs @@ -300,6 +300,10 @@ impl TGpu for Gpu { fn slow_memory(&mut self) -> &mut bool { &mut self.slow_memory } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::SteamDeck + } } #[inline] diff --git a/backend/src/settings/steam_deck_adv/cpu.rs b/backend/src/settings/steam_deck_adv/cpu.rs index 1d04a27..2b13c23 100644 --- a/backend/src/settings/steam_deck_adv/cpu.rs +++ b/backend/src/settings/steam_deck_adv/cpu.rs @@ -151,6 +151,10 @@ impl TCpus for Cpus { fn len(&self) -> usize { self.cpus.len() } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::SteamDeckAdvance + } } #[derive(Debug, Clone)] diff --git a/backend/src/settings/steam_deck_adv/gpu.rs b/backend/src/settings/steam_deck_adv/gpu.rs index fccd598..7ae25b8 100644 --- a/backend/src/settings/steam_deck_adv/gpu.rs +++ b/backend/src/settings/steam_deck_adv/gpu.rs @@ -298,6 +298,10 @@ impl TGpu for Gpu { fn slow_memory(&mut self) -> &mut bool { &mut self.slow_memory } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::SteamDeckAdvance + } } #[inline] diff --git a/backend/src/settings/traits.rs b/backend/src/settings/traits.rs index dd0b433..219cc95 100644 --- a/backend/src/settings/traits.rs +++ b/backend/src/settings/traits.rs @@ -29,6 +29,10 @@ pub trait TGpu: OnResume + OnSet + Debug + Send { fn get_clock_limits(&self) -> Option<&MinMax>; fn slow_memory(&mut self) -> &mut bool; + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::AutoDetect + } } pub trait TCpus: OnResume + OnSet + Debug + Send { @@ -39,6 +43,10 @@ pub trait TCpus: OnResume + OnSet + Debug + Send { fn cpus(&mut self) -> Vec<&mut dyn TCpu>; fn len(&self) -> usize; + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::AutoDetect + } } pub trait TCpu: Debug + Send { @@ -91,4 +99,8 @@ pub trait TBattery: OnResume + OnSet + Debug + Send { fn read_charge_design(&self) -> Option; fn read_current_now(&self) -> Option; + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::AutoDetect + } } diff --git a/backend/src/settings/unknown/battery.rs b/backend/src/settings/unknown/battery.rs index e7edb11..dfc57ce 100644 --- a/backend/src/settings/unknown/battery.rs +++ b/backend/src/settings/unknown/battery.rs @@ -63,4 +63,8 @@ impl TBattery for Battery { fn read_charge_design(&self) -> Option { None } fn read_current_now(&self) -> Option { None } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Unknown + } } diff --git a/backend/src/settings/unknown/cpu.rs b/backend/src/settings/unknown/cpu.rs index 48d4b99..d4c183a 100644 --- a/backend/src/settings/unknown/cpu.rs +++ b/backend/src/settings/unknown/cpu.rs @@ -150,6 +150,10 @@ impl TCpus for Cpus { fn len(&self) -> usize { self.cpus.len() } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Unknown + } } #[derive(Debug, Clone)] diff --git a/backend/src/settings/unknown/gpu.rs b/backend/src/settings/unknown/gpu.rs index 9733cc8..fecc4ed 100644 --- a/backend/src/settings/unknown/gpu.rs +++ b/backend/src/settings/unknown/gpu.rs @@ -86,4 +86,8 @@ impl TGpu for Gpu { fn slow_memory(&mut self) -> &mut bool { &mut self.slow_memory } + + fn provider(&self) -> crate::persist::DriverJson { + crate::persist::DriverJson::Unknown + } }