Skip to content

Commit fec1ab4

Browse files
committed
battery: Trigger dgpu refresh on toggle
1 parent 5f208bd commit fec1ab4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

cosmic-applet-battery/src/app.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ struct CosmicBatteryApplet {
8181
battery_percent: f64,
8282
on_battery: bool,
8383
gpus: HashMap<PathBuf, GPUData>,
84+
update_trigger: Option<UnboundedSender<()>>,
8485
time_remaining: Duration,
8586
max_kbd_brightness: Option<i32>,
8687
kbd_brightness: Option<i32>,
@@ -180,6 +181,7 @@ enum Message {
180181
SetChargingLimit(chain::Toggler, bool),
181182
KeyboardBacklight(KeyboardBacklightUpdate),
182183
UpowerDevice(DeviceDbusEvent),
184+
GpuInit(UnboundedSender<()>),
183185
GpuOn(PathBuf, String, Option<Vec<Entry>>),
184186
GpuOff(PathBuf),
185187
ToggleGpuApps(PathBuf),
@@ -338,6 +340,9 @@ impl cosmic::Application for CosmicBatteryApplet {
338340
if let Some(tx) = self.power_profile_sender.as_ref() {
339341
let _ = tx.send(PowerProfileRequest::Get);
340342
}
343+
if let Some(tx) = self.update_trigger.as_ref() {
344+
let _ = tx.send(());
345+
}
341346
let mut tasks = vec![get_popup(popup_settings)];
342347
// Try again every time a popup is opened
343348
if self.charging_limit.is_none() {
@@ -424,6 +429,9 @@ impl cosmic::Application for CosmicBatteryApplet {
424429
tokio::spawn(cosmic::process::spawn(cmd));
425430
}
426431
},
432+
Message::GpuInit(tx) => {
433+
self.update_trigger = Some(tx);
434+
}
427435
Message::GpuOn(path, name, app_list) => {
428436
let toggled = self
429437
.gpus
@@ -845,6 +853,7 @@ impl cosmic::Application for CosmicBatteryApplet {
845853
PowerProfileUpdate::Error(e) => Message::Errored(e), // TODO: handle error
846854
}),
847855
dgpu_subscription(0).map(|event| match event {
856+
GpuUpdate::Init(tx) => Message::GpuInit(tx),
848857
GpuUpdate::On(path, name, list) => Message::GpuOn(path, name, list),
849858
GpuUpdate::Off(path) => Message::GpuOff(path),
850859
}),

cosmic-applet-battery/src/dgpu.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use drm::control::Device as ControlDevice;
1919
use futures::{FutureExt, SinkExt};
2020
use tokio::{
2121
io::unix::AsyncFd,
22+
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
2223
task::spawn_blocking,
2324
time::{self, Interval},
2425
};
@@ -396,12 +397,13 @@ pub fn dgpu_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
396397
#[derive(Debug)]
397398
pub enum State {
398399
Ready,
399-
Waiting(GpuMonitor),
400+
Waiting(GpuMonitor, UnboundedReceiver<()>),
400401
Finished,
401402
}
402403

403404
#[derive(Debug)]
404405
pub enum GpuUpdate {
406+
Init(UnboundedSender<()>),
405407
Off(PathBuf),
406408
On(PathBuf, String, Option<Vec<Entry>>),
407409
}
@@ -412,10 +414,17 @@ async fn start_listening(
412414
) -> State {
413415
match state {
414416
State::Ready => match GpuMonitor::new().await {
415-
Some(monitor) => State::Waiting(monitor),
417+
Some(monitor) => {
418+
let (tx, rx) = mpsc::unbounded_channel();
419+
if output.send(GpuUpdate::Init(tx)).await.is_err() {
420+
State::Finished
421+
} else {
422+
State::Waiting(monitor, rx)
423+
}
424+
}
416425
None => State::Finished,
417426
},
418-
State::Waiting(mut monitor) => {
427+
State::Waiting(mut monitor, mut trigger) => {
419428
let select_all = futures::future::select_all(
420429
monitor
421430
.gpus
@@ -503,7 +512,7 @@ async fn start_listening(
503512
i = select_all => {
504513
let gpu = &mut monitor.gpus[i];
505514
if gpu.path == monitor.primary_gpu {
506-
return State::Waiting(monitor);
515+
return State::Waiting(monitor, trigger);
507516
}
508517

509518
trace!("Polling gpu {}", gpu.path.display());
@@ -529,9 +538,14 @@ async fn start_listening(
529538
return State::Finished;
530539
}
531540
}
541+
_ = trigger.recv() => {
542+
for gpu in &mut monitor.gpus {
543+
gpu.interval.reset_immediately();
544+
}
545+
}
532546
};
533547

534-
State::Waiting(monitor)
548+
State::Waiting(monitor, trigger)
535549
}
536550
State::Finished => iced::futures::future::pending().await,
537551
}

0 commit comments

Comments
 (0)