1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

system: is_ac

This commit is contained in:
RGBCube 2025-05-22 20:12:25 +03:00
parent 004b879672
commit a343e38d95
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
2 changed files with 26 additions and 29 deletions

View file

@ -246,20 +246,6 @@ pub fn get_cpu_global_info(cpu_cores: &[CpuCoreInfo]) -> CpuGlobalInfo {
}
}
pub fn get_battery_info(config: &AppConfig) -> anyhow::Result<Vec<BatteryInfo>> {
// No AC adapter detected but we're on a desktop system
// Default to AC power for desktops
if !overall_ac_connected {
overall_ac_connected = is_likely_desktop_system();
}
// If we found no batteries but have power supplies, we're likely on a desktop
if batteries.is_empty() && overall_ac_connected {
log::debug!("No laptop batteries found, likely a desktop system");
}
Ok(batteries)
}
pub fn get_cpu_model() -> anyhow::Result<String> {
let path = Path::new("/proc/cpuinfo");
let content = fs::read_to_string(path).map_err(|_| {

View file

@ -1,19 +1,25 @@
use anyhow::{Context, bail};
use crate::fs;
use crate::{cpu, fs, power_supply};
pub struct System {
pub is_desktop: bool,
pub is_ac: bool,
pub load_average_1min: f64,
pub load_average_5min: f64,
pub load_average_15min: f64,
pub cpus: Vec<cpu::Cpu>,
pub power_supplies: Vec<power_supply::PowerSupply>,
}
impl System {
pub fn new() -> anyhow::Result<Self> {
let mut system = Self {
is_desktop: false,
is_ac: false,
cpus: Vec::new(),
power_supplies: Vec::new(),
load_average_1min: 0.0,
load_average_5min: 0.0,
@ -26,13 +32,23 @@ impl System {
}
pub fn rescan(&mut self) -> anyhow::Result<()> {
self.rescan_is_desktop()?;
self.cpus = cpu::Cpu::all().context("failed to scan CPUs")?;
self.power_supplies =
power_supply::PowerSupply::all().context("failed to scan power supplies")?;
self.is_ac = self
.power_supplies
.iter()
.any(|power_supply| power_supply.is_ac())
|| self.is_desktop()?;
self.rescan_load_average()?;
Ok(())
}
fn rescan_is_desktop(&mut self) -> anyhow::Result<()> {
fn is_desktop(&mut self) -> anyhow::Result<bool> {
if let Some(chassis_type) =
fs::read("/sys/class/dmi/id/chassis_type").context("failed to read chassis type")?
{
@ -42,13 +58,11 @@ impl System {
match chassis_type.trim() {
// Desktop form factors.
"3" | "4" | "5" | "6" | "7" | "15" | "16" | "17" => {
self.is_desktop = true;
return Ok(());
return Ok(true);
}
// Laptop form factors.
"9" | "10" | "14" => {
self.is_desktop = false;
return Ok(());
return Ok(false);
}
// Unknown, continue with other checks
@ -61,8 +75,7 @@ impl System {
|| fs::exists("/sys/devices/system/cpu/cpufreq/conservative");
if !power_saving_exists {
self.is_desktop = true;
return Ok(()); // Likely a desktop.
return Ok(true); // Likely a desktop.
}
// Check battery-specific ACPI paths that laptops typically have
@ -74,14 +87,12 @@ impl System {
for path in laptop_acpi_paths {
if fs::exists(path) {
self.is_desktop = false; // Likely a laptop.
return Ok(());
return Ok(false); // Likely a laptop.
}
}
// Default to assuming desktop if we can't determine.
self.is_desktop = true;
Ok(())
Ok(true)
}
fn rescan_load_average(&mut self) -> anyhow::Result<()> {