| 1 | //! System-wide idle time on a Wayland session via `ext-idle-notify-v1`. |
| 2 | //! |
| 3 | //! Wayland idle follows <https://github.com/danieldaeschle/rusty-teams/pull/96> |
| 4 | //! by Lukas Grundtner. This product line ships only that Wayland slice because |
| 5 | //! it does not run X11 sessions. Without `WAYLAND_DISPLAY`, or when the |
| 6 | //! compositor does not offer the protocol, this returns `None` and presence |
| 7 | //! stays based on input inside the app window. |
| 8 | |
| 9 | use std::sync::{Arc, Mutex, OnceLock}; |
| 10 | use std::time::{Duration, Instant}; |
| 11 | |
| 12 | use wayland_client::globals::{GlobalListContents, registry_queue_init}; |
| 13 | use wayland_client::protocol::{wl_registry, wl_seat}; |
| 14 | use wayland_client::{Connection, Dispatch, Proxy, QueueHandle}; |
| 15 | use wayland_protocols::ext::idle_notify::v1::client::ext_idle_notification_v1::{ |
| 16 | self, ExtIdleNotificationV1, |
| 17 | }; |
| 18 | use wayland_protocols::ext::idle_notify::v1::client::ext_idle_notifier_v1::ExtIdleNotifierV1; |
| 19 | |
| 20 | /// The compositor reports idle only after this much quiet, so it is added back on. |
| 21 | const NOTIFY_AFTER: Duration = Duration::from_secs(5); |
| 22 | const INPUT_IDLE_VERSION: u32 = 2; |
| 23 | |
| 24 | #[derive(Default)] |
| 25 | struct WaylandIdle { |
| 26 | available: bool, |
| 27 | idle_since: Option<Instant>, |
| 28 | } |
| 29 | |
| 30 | pub fn system_idle() -> Option<Duration> { |
| 31 | if std::env::var_os("WAYLAND_DISPLAY").is_some() { |
| 32 | wayland_idle() |
| 33 | } else { |
| 34 | None |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn wayland_idle() -> Option<Duration> { |
| 39 | static STATE: OnceLock<Arc<Mutex<WaylandIdle>>> = OnceLock::new(); |
| 40 | let state = STATE.get_or_init(|| { |
| 41 | let state = Arc::new(Mutex::new(WaylandIdle::default())); |
| 42 | let watched = state.clone(); |
| 43 | let _ = std::thread::Builder::new() |
| 44 | .name("wayland-idle".into()) |
| 45 | .spawn(move || watch_wayland(watched)); |
| 46 | state |
| 47 | }); |
| 48 | let state = state.lock().ok()?; |
| 49 | state |
| 50 | .available |
| 51 | .then(|| idle_duration(state.idle_since, Instant::now())) |
| 52 | } |
| 53 | |
| 54 | pub fn idle_duration(idle_since: Option<Instant>, now: Instant) -> Duration { |
| 55 | idle_since.map_or(Duration::ZERO, |since| now.saturating_duration_since(since)) |
| 56 | } |
| 57 | |
| 58 | struct Watcher { |
| 59 | state: Arc<Mutex<WaylandIdle>>, |
| 60 | } |
| 61 | |
| 62 | fn watch_wayland(state: Arc<Mutex<WaylandIdle>>) -> Option<()> { |
| 63 | let connection = Connection::connect_to_env().ok()?; |
| 64 | let (globals, mut queue) = registry_queue_init::<Watcher>(&connection).ok()?; |
| 65 | let handle = queue.handle(); |
| 66 | let seat: wl_seat::WlSeat = globals.bind(&handle, 1..=1, ()).ok()?; |
| 67 | let notifier: ExtIdleNotifierV1 = globals.bind(&handle, 1..=INPUT_IDLE_VERSION, ()).ok()?; |
| 68 | let timeout = u32::try_from(NOTIFY_AFTER.as_millis()).ok()?; |
| 69 | // Version 2 counts input only, like Windows' last-input time. Version 1 also |
| 70 | // honours idle inhibitors such as a playing video. |
| 71 | let _notification = if notifier.version() >= INPUT_IDLE_VERSION { |
| 72 | notifier.get_input_idle_notification(timeout, &seat, &handle, ()) |
| 73 | } else { |
| 74 | notifier.get_idle_notification(timeout, &seat, &handle, ()) |
| 75 | }; |
| 76 | state.lock().ok()?.available = true; |
| 77 | let mut watcher = Watcher { state }; |
| 78 | loop { |
| 79 | queue.blocking_dispatch(&mut watcher).ok()?; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Dispatch<ExtIdleNotificationV1, ()> for Watcher { |
| 84 | fn event( |
| 85 | watcher: &mut Self, |
| 86 | _: &ExtIdleNotificationV1, |
| 87 | event: ext_idle_notification_v1::Event, |
| 88 | _: &(), |
| 89 | _: &Connection, |
| 90 | _: &QueueHandle<Self>, |
| 91 | ) { |
| 92 | let Ok(mut state) = watcher.state.lock() else { |
| 93 | return; |
| 94 | }; |
| 95 | match event { |
| 96 | ext_idle_notification_v1::Event::Idled => { |
| 97 | state.idle_since = Instant::now().checked_sub(NOTIFY_AFTER); |
| 98 | } |
| 99 | ext_idle_notification_v1::Event::Resumed => state.idle_since = None, |
| 100 | _ => {} |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for Watcher { |
| 106 | fn event( |
| 107 | _: &mut Self, |
| 108 | _: &wl_registry::WlRegistry, |
| 109 | _: wl_registry::Event, |
| 110 | _: &GlobalListContents, |
| 111 | _: &Connection, |
| 112 | _: &QueueHandle<Self>, |
| 113 | ) { |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl Dispatch<wl_seat::WlSeat, ()> for Watcher { |
| 118 | fn event( |
| 119 | _: &mut Self, |
| 120 | _: &wl_seat::WlSeat, |
| 121 | _: wl_seat::Event, |
| 122 | _: &(), |
| 123 | _: &Connection, |
| 124 | _: &QueueHandle<Self>, |
| 125 | ) { |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl Dispatch<ExtIdleNotifierV1, ()> for Watcher { |
| 130 | fn event( |
| 131 | _: &mut Self, |
| 132 | _: &ExtIdleNotifierV1, |
| 133 | _: <ExtIdleNotifierV1 as Proxy>::Event, |
| 134 | _: &(), |
| 135 | _: &Connection, |
| 136 | _: &QueueHandle<Self>, |
| 137 | ) { |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use super::*; |
| 144 | |
| 145 | #[test] |
| 146 | fn idle_counts_from_the_reported_start_and_is_zero_while_active() { |
| 147 | let now = Instant::now(); |
| 148 | assert_eq!(idle_duration(None, now), Duration::ZERO); |
| 149 | let since = now.checked_sub(Duration::from_secs(400)).unwrap(); |
| 150 | assert_eq!(idle_duration(Some(since), now), Duration::from_secs(400)); |
| 151 | } |
| 152 | } |