From 2187661ff82fdf377a767ee6a10f93b00fdbc286 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sun, 3 Sep 2023 10:28:00 +0400 Subject: Inhibit power key when systemd service This way we can suspend on power key rather than poweroff. --- src/niri.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/niri.rs b/src/niri.rs index 97195f8c..1a00915a 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -99,6 +99,7 @@ pub struct Niri { pub dnd_icon: Option, pub zbus_conn: Option, + pub inhibit_power_key_fd: Option, } pub struct OutputState { @@ -167,6 +168,7 @@ impl Niri { ); let mut zbus_conn = None; + let mut inhibit_power_key_fd = None; if std::env::var_os("NOTIFY_SOCKET").is_some() { // We're starting as a systemd service. Export our variables and tell systemd we're // ready. @@ -214,6 +216,32 @@ impl Niri { if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) { warn!("error notifying systemd: {err:?}"); }; + + // Inhibit power key handling so we can suspend on it. + let zbus_system_conn = zbus::blocking::ConnectionBuilder::system() + .unwrap() + .build() + .unwrap(); + + // logind-zbus has a wrong signature for this method, so do it manually. + // https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5 + let message = zbus_system_conn + .call_method( + Some("org.freedesktop.login1"), + "/org/freedesktop/login1", + Some("org.freedesktop.login1.Manager"), + "Inhibit", + &("handle-power-key", "niri", "Power key handling", "block"), + ) + .unwrap(); + match message.body() { + Ok(fd) => { + inhibit_power_key_fd = Some(fd); + } + Err(err) => { + warn!("error inhibiting power key: {err:?}"); + } + } } let display_source = Generic::new( @@ -257,6 +285,7 @@ impl Niri { dnd_icon: None, zbus_conn, + inhibit_power_key_fd, } } -- cgit