niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 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 | } |