Skip to content

Commit

Permalink
Fixed ControlState and assigning speeds under Hyprland
Browse files Browse the repository at this point in the history
  • Loading branch information
WombatFromHell committed Jan 1, 2025
1 parent 12e5599 commit 36607ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
28 changes: 13 additions & 15 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ use std::process::{Command, Stdio};

pub fn get_gpu_temp() -> u64 {
let output = Command::new("nvidia-smi")
.args(&["--query-gpu=temperature.gpu", "--format=csv,noheader"])
.args(["--query-gpu=temperature.gpu", "--format=csv,noheader"])
.output()
.expect("Failed to execute nvidia-smi");

let temp_str = String::from_utf8_lossy(&output.stdout);

temp_str
.trim()
.parse::<u64>()
.unwrap_or(0 as u64)
.clamp(0, 200)
temp_str.trim().parse::<u64>().unwrap_or(0).clamp(0, 200)
}

pub fn get_fan_speed() -> u64 {
let output = Command::new("nvidia-smi")
.args(&["--query-gpu=fan.speed", "--format=csv,noheader"])
.args(["--query-gpu=fan.speed", "--format=csv,noheader"])
.output()
.expect("Failed to execute nvidia-smi");

let _speed_str = String::from_utf8_lossy(&output.stdout);
let speed_str = _speed_str.trim().replace(" %", "");

speed_str.parse::<u64>().unwrap_or(0 as u64).clamp(0, 100)
speed_str.parse::<u64>().unwrap_or(0).clamp(0, 100)
}

pub fn set_fan_control(mode: u8) -> Result<(), Box<dyn std::error::Error>> {
let output = Command::new("sudo")
.args(&[
.args([
"nvidia-settings",
"-c",
"0",
"-a",
format!("*:1[gpu:0]/GPUFanControlState={}", mode).as_str(),
format!("GPUFanControlState={}", mode).as_str(),
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand All @@ -52,14 +50,14 @@ pub fn set_fan_control(mode: u8) -> Result<(), Box<dyn std::error::Error>> {

pub fn set_fan_speed(speed: u64) -> Result<(), Box<dyn std::error::Error>> {
let output = Command::new("sudo")
.args(&[
.args([
"nvidia-settings",
"-c",
"0",
"-a",
"*:1[gpu:0]/GPUFanControlState=1",
"GPUFanControlState=1",
"-a",
&format!("*:1[fan-0]/GPUTargetFanSpeed={}", speed),
"-a",
&format!("*:1[fan-1]/GPUTargetFanSpeed={}", speed),
&format!("GPUTargetFanSpeed={}", speed),
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Config {
let home_dir = env::var("HOME").map_err(|_| ConfigError::MissingHomeDir)?;
let xdg_config_path = format!("{}/.config/veridian-controller.toml", home_dir);

let file_path = custom_path.or_else(|| Some(xdg_config_path));
let file_path = custom_path.or(Some(xdg_config_path));

let file_path = file_path.ok_or(ConfigError::MissingConfigFile)?;

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
});

let _ = thermal_thread.join().unwrap_or_else(|err| {
thermal_thread.join().unwrap_or_else(|err| {
eprintln!("Error in thread: {:?}", err);
});

Expand Down
6 changes: 3 additions & 3 deletions src/thermalmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl<'a> ThermalManager<'a> {
last_adjustment_time: None,
last_temp_time: None,
current_fan_speed: 0,
target_fan_speed: config.fan_speed_floor as u64,
target_fan_speed: config.fan_speed_floor,
smooth_mode: if config.smooth_mode { "~" } else { "" },
}
}

pub fn update_temperature(&mut self) {
self.current_temp = commands::get_gpu_temp();
self.last_temp_time = Some(Instant::now());
self.current_fan_speed = commands::get_fan_speed() as u64;
self.current_fan_speed = commands::get_fan_speed();
self.samples.push_back(self.current_temp);
if self.samples.len() > self.config.sampling_window_size {
self.samples.pop_front();
Expand All @@ -57,7 +57,7 @@ impl<'a> ThermalManager<'a> {
// rearrange into descending order
_temps
.into_iter()
.zip(_speeds.into_iter())
.zip(_speeds)
.rev()
.collect::<Vec<(u64, u64)>>()
}
Expand Down

0 comments on commit 36607ac

Please sign in to comment.