dbus: Add power-daemon hotplug handling

This commit is contained in:
Victoria Brekenfeld 2024-02-07 13:58:44 +01:00 committed by Victoria Brekenfeld
parent 83ec68d383
commit d8c1087ebe
5 changed files with 158 additions and 2 deletions

View file

@ -716,7 +716,7 @@ impl State {
Ok(())
}
fn device_changed(&mut self, dev: dev_t) -> Result<()> {
pub(crate) fn device_changed(&mut self, dev: dev_t) -> Result<()> {
if !self.backend.kms().session.is_active() {
return Ok(());
}

66
src/dbus/mod.rs Normal file
View file

@ -0,0 +1,66 @@
use crate::state::{BackendData, State};
use anyhow::{Context, Result};
use calloop::{InsertError, LoopHandle, RegistrationToken};
mod power;
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> {
let mut tokens = Vec::new();
match power::init() {
Ok(power_daemon) => {
let (tx, rx) = calloop::channel::channel();
let token = evlh
.insert_source(rx, |event, _, state| match event {
calloop::channel::Event::Msg(_) => {
let nodes = match &mut state.backend {
BackendData::Kms(kms) => {
kms.devices.keys().cloned().collect::<Vec<_>>()
}
_ => Vec::new(),
};
for node in nodes {
if let Err(err) = state.device_changed(node.dev_id()) {
tracing::error!(?err, "Failed to update drm device {}.", node);
}
}
()
}
calloop::channel::Event::Closed => (),
})
.map_err(|InsertError { error, .. }| error)
.with_context(|| "Failed to add channel to event_loop")?;
// start helper thread
let result = std::thread::Builder::new()
.name("system76-power-hotplug".to_string())
.spawn(move || {
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect() {
while let Some(msg) = msg_iter.next() {
if tx.send(msg).is_err() {
break;
}
}
}
})
.with_context(|| "Failed to start helper thread");
match result {
Ok(_handle) => {
tokens.push(token);
// detach thread
}
Err(err) => {
evlh.remove(token);
return Err(err);
}
}
}
Err(err) => {
tracing::info!(?err, "Failed to connect to com.system76.PowerDaemon");
}
};
Ok(tokens)
}

86
src/dbus/power.rs Normal file
View file

@ -0,0 +1,86 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! # DBus interface proxy for: `com.system76.PowerDaemon`
//!
//! This code was generated by `zbus-xmlgen` `3.0.0` from DBus introspection data.
//! Source: `Interface '/com/system76/PowerDaemon' from service 'com.system76.PowerDaemon' on system bus`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the
//! [Writing a client proxy](https://dbus.pages.freedesktop.org/zbus/client.html)
//! section of the zbus documentation.
//!
//! This DBus object implements
//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html),
//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used:
//!
//! * [`zbus::fdo::IntrospectableProxy`]
//!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
use zbus::{blocking::Connection, dbus_proxy};
#[dbus_proxy(
interface = "com.system76.PowerDaemon",
default_path = "/com/system76/PowerDaemon"
)]
trait PowerDaemon {
/// Balanced method
fn balanced(&self) -> zbus::Result<()>;
/// Battery method
fn battery(&self) -> zbus::Result<()>;
/// GetChargeProfiles method
fn get_charge_profiles(
&self,
) -> zbus::Result<Vec<std::collections::HashMap<String, zbus::zvariant::OwnedValue>>>;
/// GetChargeThresholds method
fn get_charge_thresholds(&self) -> zbus::Result<(u8, u8)>;
/// GetDefaultGraphics method
fn get_default_graphics(&self) -> zbus::Result<String>;
/// GetExternalDisplaysRequireDGPU method
fn get_external_displays_require_dgpu(&self) -> zbus::Result<bool>;
/// GetGraphics method
fn get_graphics(&self) -> zbus::Result<String>;
/// GetGraphicsPower method
fn get_graphics_power(&self) -> zbus::Result<bool>;
/// GetProfile method
fn get_profile(&self) -> zbus::Result<String>;
/// GetSwitchable method
fn get_switchable(&self) -> zbus::Result<bool>;
/// Performance method
fn performance(&self) -> zbus::Result<()>;
/// SetChargeThresholds method
fn set_charge_thresholds(&self, thresholds: &(u8, u8)) -> zbus::Result<()>;
/// SetGraphics method
fn set_graphics(&self, vendor: &str) -> zbus::Result<()>;
/// SetGraphicsPower method
fn set_graphics_power(&self, power: bool) -> zbus::Result<()>;
/// HotPlugDetect signal
#[dbus_proxy(signal)]
fn hot_plug_detect(&self, port: u64) -> zbus::Result<()>;
/// PowerProfileSwitch signal
#[dbus_proxy(signal)]
fn power_profile_switch(&self, profile: &str) -> zbus::Result<()>;
}
pub fn init() -> anyhow::Result<PowerDaemonProxyBlocking<'static>> {
let conn = Connection::system()?;
let proxy = PowerDaemonProxyBlocking::new(&conn)?;
proxy.introspect()?;
Ok(proxy)
}

View file

@ -19,6 +19,7 @@ use crate::{
pub mod backend;
pub mod config;
pub mod dbus;
#[cfg(feature = "debug")]
pub mod debug;
pub mod input;

View file

@ -150,7 +150,6 @@ pub struct Common {
//pub output_conf: ConfigurationManager,
pub shell: Shell,
seats: Vec<Seat<State>>,
last_active_seat: Option<Seat<State>>,
@ -392,6 +391,10 @@ impl State {
let shell = Shell::new(&config, dh);
if let Err(err) = crate::dbus::init(&handle) {
tracing::warn!(?err, "Failed to initialize dbus handlers");
}
State {
common: Common {
config,