Gui code restructure

This commit is contained in:
Ilya Zlobintsev 2020-11-16 09:27:22 +02:00
parent d9dda8ed25
commit cd9774d292

View File

@ -2,7 +2,7 @@ extern crate gdk;
extern crate gio; extern crate gio;
extern crate gtk; extern crate gtk;
use daemon::{Daemon, daemon_connection::DaemonConnection, gpu_controller::GpuInfo}; use daemon::{Daemon, daemon_connection::DaemonConnection};
use gio::prelude::*; use gio::prelude::*;
use gtk::{Adjustment, Button, ButtonsType, ComboBoxText, DialogFlags, Frame, Label, LevelBar, MessageType, Switch, prelude::*}; use gtk::{Adjustment, Button, ButtonsType, ComboBoxText, DialogFlags, Frame, Label, LevelBar, MessageType, Switch, prelude::*};
@ -42,15 +42,16 @@ fn build_ui(application: &gtk::Application) {
let power_cap_label: Label = builder.get_object("power_cap_label").unwrap(); let power_cap_label: Label = builder.get_object("power_cap_label").unwrap();
let apply_button: Button = builder.get_object("apply_button").unwrap();
let automatic_fan_control_switch: Switch = let automatic_fan_control_switch: Switch =
builder.get_object("automatic_fan_control_switch").unwrap(); builder.get_object("automatic_fan_control_switch").unwrap();
let apply_button: Button = builder.get_object("apply_button").unwrap();
let fan_curve_frame: Frame = builder.get_object("fan_curve_frame").unwrap(); let fan_curve_frame: Frame = builder.get_object("fan_curve_frame").unwrap();
let gpu_power_adjustment: Adjustment = builder.get_object("gpu_power_adjustment").unwrap(); let gpu_power_adjustment: Adjustment = builder.get_object("gpu_power_adjustment").unwrap();
let mut unpriviliged: bool = false; let mut unpriviliged: bool = false;
let d = match DaemonConnection::new() { let d = match DaemonConnection::new() {
@ -92,13 +93,8 @@ fn build_ui(application: &gtk::Application) {
let current_gpu_id = Arc::new(RwLock::new(0u32)); let current_gpu_id = Arc::new(RwLock::new(0u32));
let cur_id = current_gpu_id.clone();
let build = builder.clone(); let build = builder.clone();
let fan_curv_frm = fan_curve_frame.clone();
let auto_fan_ctrl_swtch = automatic_fan_control_switch.clone();
let b = apply_button.clone(); let b = apply_button.clone();
gpu_power_adjustment.connect_value_changed(move |adjustment| { gpu_power_adjustment.connect_value_changed(move |adjustment| {
println!("changed adjustment value to {}/{}", adjustment.get_value(), adjustment.get_upper()); println!("changed adjustment value to {}/{}", adjustment.get_value(), adjustment.get_upper());
@ -106,36 +102,16 @@ fn build_ui(application: &gtk::Application) {
power_cap_label.set_text(&format!("{}/{}", adjustment.get_value().floor(), adjustment.get_upper())); power_cap_label.set_text(&format!("{}/{}", adjustment.get_value().floor(), adjustment.get_upper()));
}); });
let cur_id = current_gpu_id.clone();
let b = apply_button.clone(); let b = apply_button.clone();
gpu_select_comboboxtext.connect_changed(move |combobox| { gpu_select_comboboxtext.connect_changed(move |combobox| {
let mut current_gpu_id = cur_id.write().unwrap(); let mut current_gpu_id = cur_id.write().unwrap();
*current_gpu_id = combobox.get_active_id().unwrap().parse::<u32>().expect("invalid id"); *current_gpu_id = combobox.get_active_id().unwrap().parse::<u32>().expect("invalid id");
println!("Set current gpu id to {}", current_gpu_id); println!("Set current gpu id to {}", current_gpu_id);
let gpu_info = d.get_gpu_info(*current_gpu_id).unwrap(); set_info(&build, d, current_gpu_id.clone());
set_info(&build, &gpu_info);
let fan_control = d.get_fan_control(*current_gpu_id);
match fan_control {
Ok(ref fan_control) => {
if fan_control.enabled {
println!("Automatic fan control disabled!");
auto_fan_ctrl_swtch.set_active(false);
fan_curv_frm.set_visible(true);
} else {
println!("Automatic fan control enabled");
auto_fan_ctrl_swtch.set_active(true);
fan_curv_frm.set_visible(false);
}
},
Err(_) => {
auto_fan_ctrl_swtch.set_sensitive(false);
auto_fan_ctrl_swtch.set_tooltip_text(Some("Unavailable"));
fan_curv_frm.set_visible(false);
}
}
b.set_sensitive(false); b.set_sensitive(false);
@ -144,8 +120,6 @@ fn build_ui(application: &gtk::Application) {
//gpu_select_comboboxtext.set_active_id(Some(&current_gpu_id.to_string())); //gpu_select_comboboxtext.set_active_id(Some(&current_gpu_id.to_string()));
gpu_select_comboboxtext.set_active(Some(0)); gpu_select_comboboxtext.set_active(Some(0));
if unpriviliged { if unpriviliged {
automatic_fan_control_switch.set_sensitive(false); automatic_fan_control_switch.set_sensitive(false);
fan_curve_frame.set_visible(false); fan_curve_frame.set_visible(false);
@ -189,32 +163,6 @@ fn build_ui(application: &gtk::Application) {
glib::Continue(true) glib::Continue(true)
}); });
let fan_control = d.get_fan_control(*current_gpu_id.read().unwrap());
match fan_control {
Ok(ref fan_control) => {
if fan_control.enabled {
println!("Automatic fan control disabled!");
automatic_fan_control_switch.set_active(false);
fan_curve_frame.set_visible(true);
} else {
println!("Automatic fan control enabled");
fan_curve_frame.set_visible(false);
}
},
Err(_) => {
automatic_fan_control_switch.set_sensitive(false);
automatic_fan_control_switch.set_tooltip_text(Some("Unavailable"));
fan_curve_frame.set_visible(false);
}
}
let (power_cap, power_cap_max) = d.get_power_cap(*current_gpu_id.read().unwrap()).unwrap();
gpu_power_adjustment.set_upper(power_cap_max as f64);
gpu_power_adjustment.set_value(power_cap as f64);
let b = apply_button.clone(); let b = apply_button.clone();
let switch = automatic_fan_control_switch.clone(); let switch = automatic_fan_control_switch.clone();
@ -231,66 +179,6 @@ fn build_ui(application: &gtk::Application) {
b.set_sensitive(true); b.set_sensitive(true);
}); });
match fan_control {
Ok(fan_control) => {
let curve: Arc<RwLock<BTreeMap<i32, f64>>> = Arc::new(RwLock::new(fan_control.curve));
for i in 1..6 {
let curve_temperature_adjustment: Adjustment = builder
.get_object(&format!("curve_temperature_adjustment_{}", i))
.unwrap();
let value = *curve
.read()
.unwrap()
.get(&(i * 20))
.expect("Could not get by index");
println!("Setting value {} on adjustment {}", value, i);
curve_temperature_adjustment.set_value(value);
let c = curve.clone();
let b = apply_button.clone();
curve_temperature_adjustment.connect_value_changed(move |adj| {
c.write().unwrap().insert(20 * i, adj.get_value());
b.set_sensitive(true);
});
}
apply_button.connect_clicked(move |b| {
let current_gpu_id = *current_gpu_id.read().unwrap();
let curve = curve.read().unwrap().clone();
println!("setting curve to {:?}", curve);
d.set_fan_curve(current_gpu_id, curve).unwrap();
b.set_sensitive(false);
match automatic_fan_control_switch.get_active() {
true => {
d.stop_fan_control(current_gpu_id).unwrap();
let diag = MessageDialog::new(
None::<&Window>,
DialogFlags::empty(),
MessageType::Error,
ButtonsType::Ok,
"WARNING: Due to a driver bug, the GPU fan may misbehave after switching to automatic control. You may need to reboot your system to avoid issues.",
);
diag.run();
diag.hide();
}
false => {
d.start_fan_control(current_gpu_id).unwrap();
}
}
let power_cap = gpu_power_adjustment.get_value().floor() as i32;
d.set_power_cap(current_gpu_id, power_cap).unwrap();
});
},
Err(_) => (),
}
main_window.set_application(Some(application)); main_window.set_application(Some(application));
@ -298,15 +186,11 @@ fn build_ui(application: &gtk::Application) {
main_window.show(); main_window.show();
} }
fn set_info(builder: &Builder, gpu_info: &GpuInfo) { fn set_info(builder: &Builder, d: DaemonConnection, gpu_id: u32) {
let gpu_model_text_buffer: TextBuffer = builder let gpu_model_text_buffer: TextBuffer = builder
.get_object("gpu_model_text_buffer") .get_object("gpu_model_text_buffer")
.expect("Couldn't get textbuffer"); .expect("Couldn't get textbuffer");
let vbios_version_text_buffer: TextBuffer = builder .get_object("vbios_version_text_buffer") .expect("Couldn't get textbuffer");
let vbios_version_text_buffer: TextBuffer = builder
.get_object("vbios_version_text_buffer")
.expect("Couldn't get textbuffer");
let driver_text_buffer: TextBuffer = builder let driver_text_buffer: TextBuffer = builder
.get_object("driver_text_buffer") .get_object("driver_text_buffer")
.expect("Couldn't get textbuffer"); .expect("Couldn't get textbuffer");
@ -335,6 +219,17 @@ fn set_info(builder: &Builder, gpu_info: &GpuInfo) {
.get_object("vulkan_features_text_buffer") .get_object("vulkan_features_text_buffer")
.expect("Couldn't get textbuffer"); .expect("Couldn't get textbuffer");
let automatic_fan_control_switch: Switch =
builder.get_object("automatic_fan_control_switch").unwrap();
let fan_curve_frame: Frame = builder.get_object("fan_curve_frame").unwrap();
let gpu_power_adjustment: Adjustment = builder.get_object("gpu_power_adjustment").unwrap();
let apply_button: Button = builder.get_object("apply_button").unwrap();
let gpu_info = d.get_gpu_info(gpu_id).unwrap();
gpu_model_text_buffer.set_text(&gpu_info.card_model); gpu_model_text_buffer.set_text(&gpu_info.card_model);
manufacturer_text_buffer.set_text(&gpu_info.card_vendor); manufacturer_text_buffer.set_text(&gpu_info.card_vendor);
vbios_version_text_buffer.set_text(&gpu_info.vbios_version); vbios_version_text_buffer.set_text(&gpu_info.vbios_version);
@ -350,7 +245,95 @@ fn set_info(builder: &Builder, gpu_info: &GpuInfo) {
vulkan_device_name_text_buffer.set_text(&gpu_info.vulkan_info.device_name); vulkan_device_name_text_buffer.set_text(&gpu_info.vulkan_info.device_name);
vulkan_version_text_buffer.set_text(&gpu_info.vulkan_info.api_version); vulkan_version_text_buffer.set_text(&gpu_info.vulkan_info.api_version);
vulkan_features_text_buffer.set_text(&vulkan_features); vulkan_features_text_buffer.set_text(&vulkan_features);
let (power_cap, power_cap_max) = d.get_power_cap(gpu_id).unwrap();
gpu_power_adjustment.set_upper(power_cap_max as f64);
gpu_power_adjustment.set_value(power_cap as f64);
let fan_control = d.get_fan_control(gpu_id);
match fan_control {
Ok(ref fan_control) => {
if fan_control.enabled {
println!("Automatic fan control disabled!");
automatic_fan_control_switch.set_active(false);
fan_curve_frame.set_visible(true);
} else {
println!("Automatic fan control enabled");
automatic_fan_control_switch.set_active(true);
fan_curve_frame.set_visible(false);
}
},
Err(_) => {
automatic_fan_control_switch.set_sensitive(false);
automatic_fan_control_switch.set_tooltip_text(Some("Unavailable"));
fan_curve_frame.set_visible(false);
}
}
match fan_control {
Ok(fan_control) => {
let curve: Arc<RwLock<BTreeMap<i32, f64>>> = Arc::new(RwLock::new(fan_control.curve));
for i in 1..6 {
let curve_temperature_adjustment: Adjustment = builder
.get_object(&format!("curve_temperature_adjustment_{}", i))
.unwrap();
let value = *curve
.read()
.unwrap()
.get(&(i * 20))
.expect("Could not get by index");
println!("Setting value {} on adjustment {}", value, i);
curve_temperature_adjustment.set_value(value);
let c = curve.clone();
let b = apply_button.clone();
curve_temperature_adjustment.connect_value_changed(move |adj| {
c.write().unwrap().insert(20 * i, adj.get_value());
b.set_sensitive(true);
});
}
apply_button.connect_clicked(move |b| {
//let current_gpu_id = *current_gpu_id.read().unwrap();
let curve = curve.read().unwrap().clone();
println!("setting curve to {:?}", curve);
d.set_fan_curve(gpu_id, curve).unwrap();
b.set_sensitive(false);
match automatic_fan_control_switch.get_active() {
true => {
d.stop_fan_control(gpu_id).unwrap();
let diag = MessageDialog::new(
None::<&Window>,
DialogFlags::empty(),
MessageType::Error,
ButtonsType::Ok,
"WARNING: Due to a driver bug, the GPU fan may misbehave after switching to automatic control. You may need to reboot your system to avoid issues.",
);
diag.run();
diag.hide();
}
false => {
d.start_fan_control(gpu_id).unwrap();
}
}
let power_cap = gpu_power_adjustment.get_value().floor() as i32;
d.set_power_cap(gpu_id, power_cap).unwrap();
});
},
Err(_) => (),
}
} }
fn main() { fn main() {