diff --git a/CHANGELOG.md b/CHANGELOG.md index bf322104..05dc6056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 new crate `rog-aura` - Correctly enable compute mode for nvidia plus no-reboot or logout if switching from vfio/integrated/compute. +- Add asusd config option to not save compute/vfio mode switch. +- Enable basic multiple user anime configs (asusd-user must still be restarted) # [3.4.1] - 2021-04-11 ### Changed diff --git a/daemon-user/src/ctrl_anime.rs b/daemon-user/src/ctrl_anime.rs index 4f8c4010..40c710e5 100644 --- a/daemon-user/src/ctrl_anime.rs +++ b/daemon-user/src/ctrl_anime.rs @@ -15,7 +15,7 @@ use zbus::dbus_interface; use zvariant::ObjectPath; use zvariant_derive::Type; -use crate::{error::Error, user_config::UserConfig}; +use crate::{error::Error, user_config::{UserAnimeConfig, UserConfig}}; #[derive(Debug, Clone, Deserialize, Serialize, Type)] pub enum TimeType { @@ -111,7 +111,7 @@ impl<'a> CtrlAnimeInner<'static> { } pub struct CtrlAnime<'a> { - config: Arc>, + config: Arc>, client: AuraDbusClient<'a>, inner: Arc>>, /// Must be the same Atomic as in CtrlAnimeInner @@ -120,7 +120,7 @@ pub struct CtrlAnime<'a> { impl<'a> CtrlAnime<'static> { pub fn new( - config: Arc>, + config: Arc>, inner: Arc>>, client: AuraDbusClient<'static>, inner_early_return: &'static AtomicBool, diff --git a/daemon-user/src/daemon.rs b/daemon-user/src/daemon.rs index c76b7868..a5b8dc6f 100644 --- a/daemon-user/src/daemon.rs +++ b/daemon-user/src/daemon.rs @@ -24,9 +24,11 @@ fn main() -> Result<(), Box> { let mut config = UserConfig::new(); config.load_config()?; - let anime = config.create_anime()?; - let config = Arc::new(Mutex::new(config)); + let anime_config = UserAnimeConfig::load_config(config.active_anime)?; + let anime = anime_config.create_anime()?; + + let anime_config = Arc::new(Mutex::new(anime_config)); // Create server let connection = Connection::new_session()?; @@ -45,7 +47,7 @@ fn main() -> Result<(), Box> { // Need new client object for dbus control part let (client, _) = AuraDbusClient::new().unwrap(); let anime_control = - CtrlAnime::new(config, inner.clone(), client, &ANIME_INNER_EARLY_RETURN)?; + CtrlAnime::new(anime_config, inner.clone(), client, &ANIME_INNER_EARLY_RETURN)?; anime_control.add_to_server(&mut server); // Thread using inner let _anime_thread = thread::Builder::new() diff --git a/daemon-user/src/user_config.rs b/daemon-user/src/user_config.rs index 9a3bd31e..44ce6949 100644 --- a/daemon-user/src/user_config.rs +++ b/daemon-user/src/user_config.rs @@ -1,22 +1,92 @@ -use std::{ - fs::{create_dir, OpenOptions}, - io::{Read, Write}, - time::Duration, -}; +use std::{fs::{create_dir, OpenOptions}, io::{Read, Write}, time::Duration}; use rog_anime::{AnimTime, AnimeAction, Sequences, Vec2}; use serde_derive::{Deserialize, Serialize}; use crate::error::Error; -#[derive(Debug, Default, Deserialize, Serialize)] -pub struct UserConfig { +#[derive(Debug, Deserialize, Serialize)] +pub struct UserAnimeConfig { + pub name: String, pub anime: Vec, } -impl UserConfig { - pub fn new() -> Self { +impl UserAnimeConfig { + pub fn create_anime(&self) -> Result { + let mut seq = Sequences::new(); + + for (idx, action) in self.anime.iter().enumerate() { + seq.insert(idx, action)?; + } + + Ok(seq) + } + + pub fn write(&self) -> Result<(), Error> { + let mut path = if let Some(dir) = dirs::config_dir() { + dir + } else { + return Err(Error::XdgVars); + }; + + path.push("rog"); + if !path.exists() { + create_dir(path.clone())?; + } + let name = self.name.clone(); + path.push(name + ".cfg"); + + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&path)?; + + let json = serde_json::to_string_pretty(&self).unwrap(); + file.write_all(json.as_bytes())?; + Ok(()) + } + + pub fn load_config(name: String) -> Result { + let mut path = if let Some(dir) = dirs::config_dir() { + dir + } else { + return Err(Error::XdgVars); + }; + + path.push("rog"); + if !path.exists() { + create_dir(path.clone())?; + } + + path.push(name + ".cfg"); + + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(&path)?; + + let mut buf = String::new(); + + if let Ok(read_len) = file.read_to_string(&mut buf) { + if read_len == 0 { + let default = UserAnimeConfig::default(); + let json = serde_json::to_string_pretty(&default).unwrap(); + file.write_all(json.as_bytes())?; + return Ok(default); + } else if let Ok(data) = serde_json::from_str::(&buf) { + return Ok(data); + } + } + Err(Error::ConfigLoadFail) + } +} + +impl Default for UserAnimeConfig { + fn default() -> Self { let x = Self { + name: "default".to_string(), anime: vec![ AnimeAction::AsusAnimation { file: "/usr/share/asusd/anime/asus/rog/Sunset.gif".into(), @@ -52,6 +122,20 @@ impl UserConfig { println!("{}", serde_json::to_string_pretty(&x).unwrap()); x } +} + +#[derive(Debug, Default, Deserialize, Serialize)] +pub struct UserConfig { + /// Name of active anime config file in the user config directory + pub active_anime: String, +} + +impl UserConfig { + pub fn new() -> Self { + Self { + active_anime: "anime-default".to_string() + } + } pub fn load_config(&mut self) -> Result<(), Error> { let mut path = if let Some(dir) = dirs::config_dir() { @@ -80,7 +164,7 @@ impl UserConfig { let json = serde_json::to_string_pretty(&self).unwrap(); file.write_all(json.as_bytes())?; } else if let Ok(data) = serde_json::from_str::(&buf) { - self.anime = data.anime; + self.active_anime = data.active_anime; return Ok(()); } } @@ -108,18 +192,7 @@ impl UserConfig { .open(&path)?; let json = serde_json::to_string_pretty(&self).unwrap(); - dbg!(&json); file.write_all(json.as_bytes())?; Ok(()) } - - pub fn create_anime(&self) -> Result { - let mut seq = Sequences::new(); - - for (idx, action) in self.anime.iter().enumerate() { - seq.insert(idx, action)?; - } - - Ok(seq) - } } diff --git a/daemon/src/config.rs b/daemon/src/config.rs index 4e804316..55b39aa8 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -17,6 +17,7 @@ pub struct Config { pub gfx_last_mode: GfxVendors, pub gfx_managed: bool, pub gfx_vfio_enable: bool, + pub gfx_save_compute_vfio: bool, pub active_profile: String, pub toggle_profiles: Vec, #[serde(skip)] @@ -37,6 +38,7 @@ impl Default for Config { gfx_last_mode: GfxVendors::Hybrid, gfx_managed: true, gfx_vfio_enable: false, + gfx_save_compute_vfio: true, active_profile: "normal".into(), toggle_profiles: vec!["normal".into(), "boost".into(), "silent".into()], curr_fan_mode: 0, diff --git a/daemon/src/config_old.rs b/daemon/src/config_old.rs index 2b580b46..2b8ac136 100644 --- a/daemon/src/config_old.rs +++ b/daemon/src/config_old.rs @@ -28,6 +28,7 @@ impl ConfigV317 { gfx_last_mode: GfxVendors::Hybrid, gfx_managed: self.gfx_managed, gfx_vfio_enable: false, + gfx_save_compute_vfio: false, active_profile: self.active_profile, toggle_profiles: self.toggle_profiles, curr_fan_mode: self.curr_fan_mode, @@ -56,6 +57,7 @@ impl ConfigV324 { gfx_last_mode: GfxVendors::Hybrid, gfx_managed: self.gfx_managed, gfx_vfio_enable: false, + gfx_save_compute_vfio: false, active_profile: self.active_profile, toggle_profiles: self.toggle_profiles, curr_fan_mode: self.curr_fan_mode, @@ -85,6 +87,7 @@ impl ConfigV341 { gfx_last_mode: GfxVendors::Hybrid, gfx_managed: self.gfx_managed, gfx_vfio_enable: false, + gfx_save_compute_vfio: false, active_profile: self.active_profile, toggle_profiles: self.toggle_profiles, curr_fan_mode: self.curr_fan_mode, diff --git a/daemon/src/ctrl_gfx/gfx.rs b/daemon/src/ctrl_gfx/gfx.rs index a98b5d59..bf365e40 100644 --- a/daemon/src/ctrl_gfx/gfx.rs +++ b/daemon/src/ctrl_gfx/gfx.rs @@ -286,7 +286,7 @@ impl CtrlGraphics { let mut base = MODPROBE_BASE.to_vec(); base.append(&mut MODPROBE_DRM_MODESET.to_vec()); base - }, + } GfxVendors::Vfio => Self::get_vfio_conf(devices), GfxVendors::Integrated => MODPROBE_INTEGRATED.to_vec(), GfxVendors::Compute => MODPROBE_BASE.to_vec(), @@ -423,9 +423,13 @@ impl CtrlGraphics { fn logout_required(&self, vendor: GfxVendors) -> GfxRequiredUserAction { if let Ok(config) = self.config.lock() { let current = config.gfx_mode; - if matches!(current, GfxVendors::Integrated | GfxVendors::Vfio | GfxVendors::Compute) - && matches!(vendor, GfxVendors::Integrated | GfxVendors::Vfio | GfxVendors::Compute) - { + if matches!( + current, + GfxVendors::Integrated | GfxVendors::Vfio | GfxVendors::Compute + ) && matches!( + vendor, + GfxVendors::Integrated | GfxVendors::Vfio | GfxVendors::Compute + ) { return GfxRequiredUserAction::None; } } @@ -660,6 +664,13 @@ impl CtrlGraphics { let bus = self.bus.clone(); Self::do_vendor_tasks(vendor, vfio_enable, &devices, &bus)?; info!("GFX: Graphics mode changed to {}", <&str>::from(vendor)); + if let Ok(config) = self.config.lock() { + if matches!(vendor, GfxVendors::Compute | GfxVendors::Vfio) + && config.gfx_save_compute_vfio + { + Self::save_gfx_mode(vendor, self.config.clone()); + } + } } // TODO: undo if failed? Save last mode, catch errors... Ok(action_required)