forge
Explore
Log in

Don't you dare go Hollow

Add login1 LidClosed monitoring

Browse files at this commit

8c8447918f4fd7bc6c86a8622b1db52417fbbbbdIvan MolodetskikhParent: a4f5c90
src/dbus/freedesktop_login1.rsadded+105 -0
@@ -0,0 +1,105 @@
1+use futures_util::StreamExt;
2+use zbus::fdo;
3+use zbus::names::InterfaceName;
4+
5+pub enum Login1ToNiri {
6+ LidClosedChanged(bool),
7+}
8+
9+pub fn start(
10+ to_niri: calloop::channel::Sender<Login1ToNiri>,
11+) -> anyhow::Result<zbus::blocking::Connection> {
12+ let conn = zbus::blocking::Connection::system()?;
13+
14+ let async_conn = conn.inner().clone();
15+ let future = async move {
16+ let proxy = fdo::PropertiesProxy::new(
17+ &async_conn,
18+ "org.freedesktop.login1",
19+ "/org/freedesktop/login1",
20+ )
21+ .await;
22+ let proxy = match proxy {
23+ Ok(x) => x,
24+ Err(err) => {
25+ warn!("error creating PropertiesProxy: {err:?}");
26+ return;
27+ }
28+ };
29+
30+ let mut props_changed = match proxy.receive_properties_changed().await {
31+ Ok(x) => x,
32+ Err(err) => {
33+ warn!("error subscribing to PropertiesChanged: {err:?}");
34+ return;
35+ }
36+ };
37+
38+ let props = proxy
39+ .get_all(InterfaceName::try_from("org.freedesktop.login1.Manager").unwrap())
40+ .await;
41+ let mut props = match props {
42+ Ok(x) => x,
43+ Err(err) => {
44+ warn!("error receiving initial properties: {err:?}");
45+ return;
46+ }
47+ };
48+
49+ trace!("initial properties: {props:?}");
50+
51+ let mut lid_closed = props
52+ .remove("LidClosed")
53+ .and_then(|value| bool::try_from(value).ok())
54+ .unwrap_or_default();
55+
56+ if let Err(err) = to_niri.send(Login1ToNiri::LidClosedChanged(lid_closed)) {
57+ warn!("error sending initial lid state to niri: {err:?}");
58+ return;
59+ };
60+
61+ while let Some(signal) = props_changed.next().await {
62+ let args = match signal.args() {
63+ Ok(args) => args,
64+ Err(err) => {
65+ warn!("error parsing PropertiesChanged args: {err:?}");
66+ return;
67+ }
68+ };
69+
70+ let mut new_lid_closed = lid_closed;
71+ let mut changed = false;
72+ for (name, value) in args.changed_properties() {
73+ trace!("changed property: {name} => {value:?}");
74+ if *name != "LidClosed" {
75+ continue;
76+ }
77+
78+ new_lid_closed = bool::try_from(value).unwrap_or(new_lid_closed);
79+ changed = true;
80+ }
81+
82+ if !changed {
83+ continue;
84+ }
85+
86+ if new_lid_closed == lid_closed {
87+ continue;
88+ }
89+
90+ lid_closed = new_lid_closed;
91+ if let Err(err) = to_niri.send(Login1ToNiri::LidClosedChanged(lid_closed)) {
92+ warn!("error sending message to niri: {err:?}");
93+ return;
94+ };
95+ }
96+ };
97+
98+ let task = conn
99+ .inner()
100+ .executor()
101+ .spawn(future, "monitor login1 property changes");
102+ task.detach();
103+
104+ Ok(conn)
105+}
src/dbus/mod.rsmodified+18 -0
@@ -5,6 +5,7 @@ use crate::niri::State;
5 5
6 pub mod freedesktop_a11y;6 pub mod freedesktop_a11y;
7 pub mod freedesktop_locale1;7 pub mod freedesktop_locale1;
8+pub mod freedesktop_login1;
8 pub mod freedesktop_screensaver;9 pub mod freedesktop_screensaver;
9 pub mod gnome_shell_introspect;10 pub mod gnome_shell_introspect;
10 pub mod gnome_shell_screenshot;11 pub mod gnome_shell_screenshot;
@@ -35,6 +36,7 @@ pub struct DBusServers {
35 pub conn_introspect: Option<Connection>,36 pub conn_introspect: Option<Connection>,
36 #[cfg(feature = "xdp-gnome-screencast")]37 #[cfg(feature = "xdp-gnome-screencast")]
37 pub conn_screen_cast: Option<Connection>,38 pub conn_screen_cast: Option<Connection>,
39+ pub conn_login1: Option<Connection>,
38 pub conn_locale1: Option<Connection>,40 pub conn_locale1: Option<Connection>,
39 pub conn_keyboard_monitor: Option<Connection>,41 pub conn_keyboard_monitor: Option<Connection>,
40 }42 }
@@ -136,6 +138,22 @@ impl DBusServers {
136 }138 }
137 }139 }
138 140
141+ let (to_niri, from_login1) = calloop::channel::channel();
142+ niri.event_loop
143+ .insert_source(from_login1, move |event, _, state| match event {
144+ calloop::channel::Event::Msg(msg) => state.on_login1_msg(msg),
145+ calloop::channel::Event::Closed => (),
146+ })
147+ .unwrap();
148+ match freedesktop_login1::start(to_niri) {
149+ Ok(conn) => {
150+ dbus.conn_login1 = Some(conn);
151+ }
152+ Err(err) => {
153+ warn!("error starting login1 watcher: {err:?}");
154+ }
155+ }
156+
139 let (to_niri, from_locale1) = calloop::channel::channel();157 let (to_niri, from_locale1) = calloop::channel::channel();
140 niri.event_loop158 niri.event_loop
141 .insert_source(from_locale1, move |event, _, state| match event {159 .insert_source(from_locale1, move |event, _, state| match event {
src/niri.rsmodified+12 -0
@@ -120,6 +120,8 @@ use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor};
120 #[cfg(feature = "dbus")]120 #[cfg(feature = "dbus")]
121 use crate::dbus::freedesktop_locale1::Locale1ToNiri;121 use crate::dbus::freedesktop_locale1::Locale1ToNiri;
122 #[cfg(feature = "dbus")]122 #[cfg(feature = "dbus")]
123+use crate::dbus::freedesktop_login1::Login1ToNiri;
124+#[cfg(feature = "dbus")]
123 use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect};125 use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect};
124 #[cfg(feature = "dbus")]126 #[cfg(feature = "dbus")]
125 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};127 use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri};
@@ -726,6 +728,8 @@ impl State {
726 self.niri.notified_activity_this_iteration = false;728 self.niri.notified_activity_this_iteration = false;
727 }729 }
728 730
731+ // We monitor both libinput and logind: libinput is always there (including without DBus), but
732+ // it misses some switch events (e.g. after unsuspend) on some systems.
729 pub fn set_lid_closed(&mut self, is_closed: bool) {733 pub fn set_lid_closed(&mut self, is_closed: bool) {
730 if self.niri.is_lid_closed == is_closed {734 if self.niri.is_lid_closed == is_closed {
731 return;735 return;
@@ -2298,6 +2302,14 @@ impl State {
2298 }2302 }
2299 }2303 }
2300 2304
2305+ #[cfg(feature = "dbus")]
2306+ pub fn on_login1_msg(&mut self, msg: Login1ToNiri) {
2307+ let Login1ToNiri::LidClosedChanged(is_closed) = msg;
2308+
2309+ trace!("login1 lid {}", if is_closed { "closed" } else { "opened" });
2310+ self.set_lid_closed(is_closed);
2311+ }
2312+
2301 #[cfg(feature = "dbus")]2313 #[cfg(feature = "dbus")]
2302 pub fn on_locale1_msg(&mut self, msg: Locale1ToNiri) {2314 pub fn on_locale1_msg(&mut self, msg: Locale1ToNiri) {
2303 let Locale1ToNiri::XkbChanged(xkb) = msg;2315 let Locale1ToNiri::XkbChanged(xkb) = msg;