forge
Explore
Log in

Don't you dare go Hollow

Inhibit power key when systemd service

Browse files at this commit

2187661ff82fdf377a767ee6a10f93b00fdbc286Ivan MolodetskikhParent: 38d85e6
This way we can suspend on power key rather than poweroff.
src/niri.rsmodified+29 -0
@@ -99,6 +99,7 @@ pub struct Niri {
99 pub dnd_icon: Option<WlSurface>,99 pub dnd_icon: Option<WlSurface>,
100 100
101 pub zbus_conn: Option<zbus::blocking::Connection>,101 pub zbus_conn: Option<zbus::blocking::Connection>,
102+ pub inhibit_power_key_fd: Option<zbus::zvariant::OwnedFd>,
102 }103 }
103 104
104 pub struct OutputState {105 pub struct OutputState {
@@ -167,6 +168,7 @@ impl Niri {
167 );168 );
168 169
169 let mut zbus_conn = None;170 let mut zbus_conn = None;
171+ let mut inhibit_power_key_fd = None;
170 if std::env::var_os("NOTIFY_SOCKET").is_some() {172 if std::env::var_os("NOTIFY_SOCKET").is_some() {
171 // We're starting as a systemd service. Export our variables and tell systemd we're173 // We're starting as a systemd service. Export our variables and tell systemd we're
172 // ready.174 // ready.
@@ -214,6 +216,32 @@ impl Niri {
214 if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) {216 if let Err(err) = sd_notify::notify(true, &[NotifyState::Ready]) {
215 warn!("error notifying systemd: {err:?}");217 warn!("error notifying systemd: {err:?}");
216 };218 };
219+
220+ // Inhibit power key handling so we can suspend on it.
221+ let zbus_system_conn = zbus::blocking::ConnectionBuilder::system()
222+ .unwrap()
223+ .build()
224+ .unwrap();
225+
226+ // logind-zbus has a wrong signature for this method, so do it manually.
227+ // https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5
228+ let message = zbus_system_conn
229+ .call_method(
230+ Some("org.freedesktop.login1"),
231+ "/org/freedesktop/login1",
232+ Some("org.freedesktop.login1.Manager"),
233+ "Inhibit",
234+ &("handle-power-key", "niri", "Power key handling", "block"),
235+ )
236+ .unwrap();
237+ match message.body() {
238+ Ok(fd) => {
239+ inhibit_power_key_fd = Some(fd);
240+ }
241+ Err(err) => {
242+ warn!("error inhibiting power key: {err:?}");
243+ }
244+ }
217 }245 }
218 246
219 let display_source = Generic::new(247 let display_source = Generic::new(
@@ -257,6 +285,7 @@ impl Niri {
257 dnd_icon: None,285 dnd_icon: None,
258 286
259 zbus_conn,287 zbus_conn,
288+ inhibit_power_key_fd,
260 }289 }
261 }290 }
262 291