From 60b7f3be692c48deef92b734772edfb14c360ea5 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Wed, 25 Aug 2021 11:42:32 +1200 Subject: [PATCH] Begin cleanup --- .gitignore | 1 + daemon/src/config.rs | 4 ++-- supergfx/src/config.rs | 3 --- supergfx/src/controller.rs | 29 ++++++++++++++++------------- supergfx/src/error.rs | 4 ++-- supergfx/src/lib.rs | 15 +++++++-------- supergfx/src/special.rs | 25 +++++++++++++++++++++++++ supergfx/src/zbus.rs | 2 +- 8 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 supergfx/src/special.rs diff --git a/.gitignore b/.gitignore index 8d235fc8..bec5eaa6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ cargo-config .idea vendor-* vendor_* +.vscode-ctags diff --git a/daemon/src/config.rs b/daemon/src/config.rs index 260e34b8..604c95b8 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -60,8 +60,8 @@ impl Config { if l == 0 { warn!("File is empty {}", CONFIG_PATH); } else { - serde_json::from_str(&buf) - .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)) + *self = serde_json::from_str(&buf) + .unwrap_or_else(|_| panic!("Could not deserialise {}", CONFIG_PATH)); } } } diff --git a/supergfx/src/config.rs b/supergfx/src/config.rs index e05409d2..1967d2db 100644 --- a/supergfx/src/config.rs +++ b/supergfx/src/config.rs @@ -5,9 +5,6 @@ use std::io::{Read, Write}; use crate::gfx_vendors::GfxVendors; -pub static CONFIG_PATH: &str = "/etc/asusd/asusd.conf"; -pub static AURA_CONFIG_PATH: &str = "/etc/asusd/asusd.conf"; - #[derive(Deserialize, Serialize)] pub struct GfxConfig { #[serde(skip)] diff --git a/supergfx/src/controller.rs b/supergfx/src/controller.rs index 9aeae7eb..e6e788b4 100644 --- a/supergfx/src/controller.rs +++ b/supergfx/src/controller.rs @@ -1,9 +1,9 @@ +use ::zbus::Connection; use log::{error, info, warn}; use logind_zbus::{ types::{SessionClass, SessionInfo, SessionState, SessionType}, ManagerProxy, SessionProxy, }; -use ::zbus::Connection; use std::{io::Write, ops::Add, path::Path, time::Instant}; use std::{process::Command, thread::sleep, time::Duration}; use std::{str::FromStr, sync::mpsc}; @@ -11,7 +11,12 @@ use std::{sync::Arc, sync::Mutex}; use sysfs_class::RuntimePM; use sysfs_class::{PciDevice, SysClass}; -use crate::{*, error::GfxError, system::{GraphicsDevice, PciBus}}; +use crate::{ + error::GfxError, + special::{get_asus_gsync_gfx_mode, has_asus_gsync_gfx_mode}, + system::{GraphicsDevice, PciBus}, + *, +}; use super::config::GfxConfig; use super::gfx_vendors::{GfxPower, GfxRequiredUserAction, GfxVendors}; @@ -137,12 +142,8 @@ impl CtrlGraphics { pub(super) fn get_runtime_status() -> Result { let path = Path::new(NVIDIA_RUNTIME_STATUS_PATH); if path.exists() { - let buf = std::fs::read_to_string(path).map_err(|err| { - GfxError::Read( - path.to_string_lossy().to_string(), - err, - ) - })?; + let buf = std::fs::read_to_string(path) + .map_err(|err| GfxError::Read(path.to_string_lossy().to_string(), err))?; Ok(GfxPower::from_str(&buf)?) } else { Ok(GfxPower::Off) @@ -630,11 +631,13 @@ impl CtrlGraphics { /// /// For manually calling (not on boot/startup) via dbus pub fn set_gfx_mode(&mut self, vendor: GfxVendors) -> Result { - // if let Ok(gsync) = CtrlRogBios::get_gfx_mode() { - // if gsync == 1 { - // return Err(GfxError::GsyncModeActive.into()); - // } - // } + if has_asus_gsync_gfx_mode() { + if let Ok(gsync) = get_asus_gsync_gfx_mode() { + if gsync == 1 { + return Err(GfxError::AsusGsyncModeActive); + } + } + } let vfio_enable = if let Ok(config) = self.config.try_lock() { config.gfx_vfio_enable diff --git a/supergfx/src/error.rs b/supergfx/src/error.rs index a43d9445..b5e94612 100644 --- a/supergfx/src/error.rs +++ b/supergfx/src/error.rs @@ -8,7 +8,7 @@ pub enum GfxError { Bus(String, std::io::Error), DisplayManagerAction(String, ExitStatus), DisplayManagerTimeout(String), - GsyncModeActive, + AsusGsyncModeActive, VfioBuiltin, VfioDisabled, MissingModule(String), @@ -34,7 +34,7 @@ impl fmt::Display for GfxError { GfxError::DisplayManagerTimeout(state) => { write!(f, "Timed out waiting for display-manager {} state", state) } - GfxError::GsyncModeActive => write!( + GfxError::AsusGsyncModeActive => write!( f, "Can not switch gfx modes when dedicated/G-Sync mode is active" ), diff --git a/supergfx/src/lib.rs b/supergfx/src/lib.rs index 527667c9..484aece7 100644 --- a/supergfx/src/lib.rs +++ b/supergfx/src/lib.rs @@ -3,6 +3,9 @@ pub mod config; pub mod gfx_vendors; pub mod controller; pub mod system; +/// Special-case functions for check/read/write of key functions on unique laptops +/// such as the G-Sync mode available on some ASUS ROG laptops +pub(crate) mod special; pub mod zbus; const NVIDIA_DRIVERS: [&str; 4] = ["nvidia_drm", "nvidia_modeset", "nvidia_uvm", "nvidia"]; @@ -17,13 +20,9 @@ const VFIO_DRIVERS: [&str; 5] = [ const DISPLAY_MANAGER: &str = "display-manager.service"; -const MODPROBE_PATH: &str = "/etc/modprobe.d/asusd.conf"; +const MODPROBE_PATH: &str = "/etc/modprobe.d/supergfxd.conf"; -static MODPROBE_BASE: &[u8] = br#"# Automatically generated by asusd -# If you have issues with i2c_nvidia_gpu, copy the 2 lines below to a -# new blacklist file and uncomment -#blacklist i2c_nvidia_gpu -#alias i2c_nvidia_gpu off +static MODPROBE_BASE: &[u8] = br#"# Automatically generated by supergfxd blacklist nouveau alias nouveau off options nvidia NVreg_DynamicPowerManagement=0x02 @@ -33,7 +32,7 @@ static MODPROBE_DRM_MODESET: &[u8] = br#" options nvidia-drm modeset=1 "#; -static MODPROBE_INTEGRATED: &[u8] = br#"# Automatically generated by asusd +static MODPROBE_INTEGRATED: &[u8] = br#"# Automatically generated by supergfxd blacklist i2c_nvidia_gpu blacklist nvidia blacklist nvidia-drm @@ -47,7 +46,7 @@ static MODPROBE_VFIO: &[u8] = br#"options vfio-pci ids="#; const XORG_FILE: &str = "90-nvidia-primary.conf"; const XORG_PATH: &str = "/etc/X11/xorg.conf.d/"; -static PRIMARY_GPU_BEGIN: &[u8] = br#"# Automatically generated by asusd +static PRIMARY_GPU_BEGIN: &[u8] = br#"# Automatically generated by supergfxd Section "OutputClass" Identifier "nvidia" MatchDriver "nvidia-drm" diff --git a/supergfx/src/special.rs b/supergfx/src/special.rs new file mode 100644 index 00000000..fdccc37e --- /dev/null +++ b/supergfx/src/special.rs @@ -0,0 +1,25 @@ +use std::{fs::OpenOptions, io::Read, path::Path}; + +use crate::error::GfxError; + +static ASUS_SWITCH_GRAPHIC_MODE: &str = + "/sys/firmware/efi/efivars/AsusSwitchGraphicMode-607005d5-3f75-4b2e-98f0-85ba66797a3e"; + +pub(crate) fn has_asus_gsync_gfx_mode() -> bool { + Path::new(ASUS_SWITCH_GRAPHIC_MODE).exists() +} + +pub(crate) fn get_asus_gsync_gfx_mode() -> Result { + let path = ASUS_SWITCH_GRAPHIC_MODE; + let mut file = OpenOptions::new() + .read(true) + .open(path) + .map_err(|err| GfxError::Path(path.into(), err))?; + + let mut data = Vec::new(); + file.read_to_end(&mut data) + .map_err(|err| GfxError::Read(path.into(), err))?; + + let idx = data.len() - 1; + Ok(data[idx] as i8) +} \ No newline at end of file diff --git a/supergfx/src/zbus.rs b/supergfx/src/zbus.rs index 18551f33..b625912b 100644 --- a/supergfx/src/zbus.rs +++ b/supergfx/src/zbus.rs @@ -2,7 +2,7 @@ use log::{error, info, warn}; use zvariant::ObjectPath; use ::zbus::dbus_interface; -use crate::{gfx_vendors::{GfxPower, GfxRequiredUserAction, GfxVendors}}; +use crate::gfx_vendors::{GfxPower, GfxRequiredUserAction, GfxVendors}; use super::controller::CtrlGraphics;