Skip to content

Commit 9f398f9

Browse files
author
Deren Vural
committed
Updated gschema xml
- Added (string list) GPU UUID list Updated Provider class - Switched from enum (ProviderType) to i32 values (will probably switch back at some point) - Updated constructor to include ProviderType (i32) and set as property - Updated default impl to include ProviderType (i32) - Refactored to create "get_gpu_uuids" as new function, to be called MainWindow class Updated Processor class - Modified "process" and "parse" functions to return Option<Vec<String>> and Vec<String> respectively - Modified parse to actually return a Vec instead of just one line Updated MainWindow class & UI - Updated UI to include a panel with a listbox of GPU rows - Refactored to create "create_provider" as new function, called by "refresh_cards" in imp.rs and "open_nvidia_settings" action in mod.rs - Updated class to include new UI template objects and their callbacks - Callbacks for most menu items disabled (will be removing most of them) - CustomButton at bottom of panel now used for refreshing GPU rows - Updated to have "setup_widgets" function for updating initial GPU list and adding callbacks - Updated UI to have some clear comments Updated main.rs - Added CustomButton module import to allow import in MainWindow class Signed-off-by: Deren Vural <[email protected]>
1 parent ecdf62f commit 9f398f9

File tree

7 files changed

+620
-66
lines changed

7 files changed

+620
-66
lines changed

src/com.gtk_d.NvidiaMonitorRust.gschema.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ SPDX-License-Identifier: GPL-3.0-or-later
4242
</description>
4343
</key>
4444

45+
<!-- List of GPUs -->
46+
<key name="gpus" type="as">
47+
<default>[]</default>
48+
<summary>GPU UUIDs</summary>
49+
<description>
50+
List of UUIDs of scanned GPUs
51+
</description>
52+
</key>
53+
4554
<!-- Not entirely sure what these are for..-->
4655
<key name="settingsconfig" type="as">
4756
<default>[]</default>

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod provider;
2727
mod subprocess;
2828
use mainwindow::MainWindow;
2929
mod settingswindow;
30+
mod custom_button;
3031

3132
// Imports
3233
use adwaita::prelude::*;

src/mainwindow/imp.rs

Lines changed: 203 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@
1919
*/
2020

2121
// Imports
22-
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
22+
use adwaita::{gio, glib, prelude::*, subclass::prelude::*, ActionRow};
2323
use gio::Settings;
2424
use glib::{once_cell::sync::OnceCell, signal::Inhibit, subclass::InitializingObject};
25-
use gtk::{subclass::prelude::*, CompositeTemplate}; //, Entry, ListBox, TemplateChild};
25+
use gtk::{subclass::prelude::*, CompositeTemplate, TemplateChild, ListBox};
2626
use std::{cell::Cell, cell::RefCell, rc::Rc};
2727

28+
use crate::formatter::Formatter;
29+
use crate::processor::Processor;
30+
use crate::property::Property;
31+
use crate::provider::Provider;
2832
// Modules
2933
//use crate::utils::data_path;
3034
use crate::settingswindow::SettingsWindow;
35+
use crate::custom_button::CustomButton;
3136

3237
// Structure for storing SettingsWindow and info
3338
pub struct SettingsWindowContainer {
3439
pub window: Option<SettingsWindow>,
3540
pub open: bool,
36-
//... other values associated to that window
3741
}
3842
impl Default for SettingsWindowContainer {
3943
fn default() -> Self {
@@ -51,6 +55,13 @@ pub struct MainWindow {
5155
pub settings: OnceCell<Settings>,
5256
pub app_id: Cell<String>,
5357
pub settings_window: Rc<RefCell<SettingsWindowContainer>>,
58+
pub provider: Cell<Option<Provider>>,
59+
60+
#[template_child]
61+
pub cards_list: TemplateChild<ListBox>,
62+
63+
#[template_child]
64+
pub refresh_button: TemplateChild<CustomButton>,
5465
}
5566

5667
// The central trait for subclassing a GObject
@@ -63,13 +74,201 @@ impl ObjectSubclass for MainWindow {
6374

6475
fn class_init(klass: &mut Self::Class) {
6576
klass.bind_template();
77+
klass.bind_template_callbacks();
6678
}
6779

6880
fn instance_init(obj: &InitializingObject<Self>) {
6981
obj.init_template();
7082
}
7183
}
7284

85+
/*
86+
* Name:
87+
* MainWindow
88+
*
89+
* Description:
90+
* Trait shared by all MainWindow objects
91+
*
92+
* Made:
93+
* 13/10/2022
94+
*
95+
* Made by:
96+
* Deren Vural
97+
*
98+
* Notes:
99+
*
100+
*/
101+
#[gtk::template_callbacks]
102+
impl MainWindow {
103+
#[template_callback]
104+
fn card_selected(&self, row: &ActionRow) {
105+
println!("CARD CHOSEN: {}", row.title());
106+
}
107+
108+
pub fn create_provider(provider_type: i32) -> Provider {
109+
match provider_type {
110+
0 => {
111+
// Nvidia Settings and Nvidia SMI
112+
// Create new provider
113+
Provider::new(|| {
114+
vec![
115+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "utilization.gpu", "", &Formatter::new(), &1),
116+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "temperature.gpu", "", &Formatter::new(), &1),
117+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "memory.used,memory.total", "", &Formatter::new(), &1),
118+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "fan.speed", "", &Formatter::new(), &1),
119+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
120+
]
121+
}, 0)
122+
}
123+
1 => {
124+
// Nvidia Settings
125+
// Create new provider
126+
Provider::new(|| {
127+
vec![
128+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "utilization.gpu", "", &Formatter::new(), &1),
129+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "temperature.gpu", "", &Formatter::new(), &1),
130+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "memory.used,memory.total", "", &Formatter::new(), &1),
131+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "fan.speed", "", &Formatter::new(), &1),
132+
]
133+
}, 1)
134+
}
135+
2 => {
136+
// Nvidia SMI
137+
// Create new provider
138+
Provider::new(|| {
139+
vec![
140+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "utilization.gpu", "", &Formatter::new(), &1),
141+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "temperature.gpu", "", &Formatter::new(), &1),
142+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "memory.used,memory.total", "", &Formatter::new(), &1),
143+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "fan.speed", "", &Formatter::new(), &1),
144+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
145+
]
146+
}, 2)
147+
}
148+
3 => {
149+
// Nvidia Optimus
150+
// Create new provider
151+
Provider::new(|| {
152+
vec![
153+
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "utilization.gpu", "", &Formatter::new(), &1),
154+
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "temperature.gpu", "", &Formatter::new(), &1),
155+
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "memory.used,memory.total", "", &Formatter::new(), &1),
156+
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "fan.speed", "", &Formatter::new(), &1),
157+
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
158+
]
159+
}, 3)
160+
},
161+
_ => {
162+
// Assume Default (Nvidia Settings and Nvidia SMI)
163+
// Create new provider
164+
Provider::new(|| {
165+
vec![
166+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "utilization.gpu", "", &Formatter::new(), &1),
167+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "temperature.gpu", "", &Formatter::new(), &1),
168+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "memory.used,memory.total", "", &Formatter::new(), &1),
169+
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "fan.speed", "", &Formatter::new(), &1),
170+
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
171+
]
172+
}, 0)
173+
}
174+
}
175+
}
176+
177+
#[template_callback]
178+
fn refresh_cards(&self, button: &CustomButton) {
179+
//TEST: Grab button label
180+
let label_val = button.label().expect("cannot grab label of refresh button");
181+
println!("Button Pressed: {}", label_val);
182+
183+
// Clear current ActionRow objects from GtkListBox
184+
let mut done: bool = false;
185+
while done == false {
186+
// Try to grab a child
187+
let current_child: Option<gtk::Widget> = self.cards_list.get().first_child();
188+
189+
// Check if there are any children left
190+
match current_child {
191+
Some(valid_child) => {
192+
// Remove child
193+
self.cards_list.get().remove(&valid_child);
194+
}
195+
None => {
196+
// End loop
197+
done = true;
198+
}
199+
}
200+
}
201+
202+
// Grab settings
203+
let settings: &Settings = self.settings.get().expect("..Cannot retrieve settings");
204+
205+
// Grab provider
206+
let provider: Option<Provider> = self.provider.take();
207+
208+
// If provider does not exist
209+
match provider {
210+
Some(existing_provider) => {
211+
// Update GPU list
212+
//let gpu_uuids = existing_provider.gpus;
213+
let gpu_uuids = ["",""];
214+
215+
// Store GPU list in settings
216+
settings
217+
.set_strv("gpus", &gpu_uuids)
218+
.expect("..Cannot store list of GPU UUID in `gpus` setting");
219+
220+
// Re-Store provider
221+
self.provider.set(Some(existing_provider));
222+
}
223+
None => {
224+
// Check provider type
225+
let provider_type: i32 = self.settings.get().expect("..cannot fetch settings").int("provider");
226+
227+
let new_provider: Provider = MainWindow::create_provider(provider_type);
228+
229+
// Update GPU list
230+
//let gpu_uuids = new_provider.gpus;
231+
let gpu_uuids = ["",""];
232+
233+
// Store new provider
234+
self.provider.set(Some(new_provider));
235+
236+
// Store GPU list in settings
237+
settings
238+
.set_strv("gpus", &gpu_uuids)
239+
.expect("..Cannot store list of GPU UUID in `gpus` setting");
240+
}
241+
}
242+
243+
// Grab provider
244+
let provider: Option<Provider> = self.provider.take();
245+
246+
// Fetch updated GPU list
247+
//let gpus: Vec<GString> = settings.strv("gpus");
248+
249+
// For each scanned GPU
250+
let gpus = vec![("title 1","subtitle 1"), ("title 2","subtitle 2"), ("title 3","subtitle 3")];//TEST
251+
for i in 0..gpus.len() {
252+
// Get GPU data
253+
let title: &str = gpus[i].0;//TEST
254+
let subtitle: &str = gpus[i].1;//TEST
255+
//let gpu_name = provider.get_gpu_data(&gpus[i], "name");
256+
//let gpu_uuid = provider.get_gpu_data(&gpus[i], "UUID");
257+
258+
// Create new ActionRow object
259+
let current_row: ActionRow = ActionRow::builder()
260+
.title(title)
261+
.subtitle(subtitle)
262+
.build();
263+
264+
// Append new ActionRow object to GtkListBox
265+
self.cards_list.append(&current_row);
266+
}
267+
268+
// Put provider back
269+
self.provider.set(provider);
270+
}
271+
}
73272
/*
74273
* Trait Name:
75274
* ObjectImpl
@@ -93,7 +292,7 @@ impl ObjectImpl for MainWindow {
93292

94293
// Setup
95294
obj.setup_settings();
96-
//obj.setup_tasks();
295+
obj.setup_widgets();
97296
obj.restore_data();
98297
obj.setup_callbacks();
99298
obj.setup_actions();

0 commit comments

Comments
 (0)