fix(presence): count Wayland input outside the window as activity (#106)
@@ -338,6 +338,8 @@ dependencies = [
338
338
"tempfile",
339
339
"tokio",
340
340
"tray-icon",
341
"wayland-client",
342
"wayland-protocols",
341
343
"windows 0.62.2",
342
344
"x11rb",
343
345
"zbus 5.19.0",
@@ -338,6 +338,8 @@ dependencies = [338
"tempfile",338
"tempfile",339
"tokio",339
"tokio",340
"tray-icon",340
"tray-icon",341
"wayland-client",342
"wayland-protocols",341
"windows 0.62.2",343
"windows 0.62.2",342
"x11rb",344
"x11rb",343
"zbus 5.19.0",345
"zbus 5.19.0",@@ -41,6 +41,8 @@ core-foundation = "0.10"
41
41
core-foundation-sys = "0.8"
42
42
43
43
[target.'cfg(target_os = "linux")'.dependencies]
44
wayland-client = "0.31"
45
wayland-protocols = { version = "0.32", features = ["client", "staging"] }
44
46
x11rb = { version = "0.13", features = ["shape"] }
45
47
ksni = { version = "0.3", features = ["blocking"] }
46
48
zbus = { version = "5", default-features = false, features = ["tokio"] }
@@ -41,6 +41,8 @@ core-foundation = "0.10"41
core-foundation-sys = "0.8"41
core-foundation-sys = "0.8"42
42
43
[target.'cfg(target_os = "linux")'.dependencies]43
[target.'cfg(target_os = "linux")'.dependencies]44
wayland-client = "0.31"45
wayland-protocols = { version = "0.32", features = ["client", "staging"] }44
x11rb = { version = "0.13", features = ["shape"] }46
x11rb = { version = "0.13", features = ["shape"] }45
ksni = { version = "0.3", features = ["blocking"] }47
ksni = { version = "0.3", features = ["blocking"] }46
zbus = { version = "5", default-features = false, features = ["tokio"] }48
zbus = { version = "5", default-features = false, features = ["tokio"] }@@ -26,6 +26,8 @@ pub mod inline_image;
26
26
pub mod linux_desktop;
27
27
#[cfg(target_os = "linux")]
28
28
pub mod linux_fonts;
29
#[cfg(target_os = "linux")]
30
mod linux_idle;
29
31
pub mod native_dumps;
30
32
pub mod notify;
31
33
pub mod orgs;
@@ -26,6 +26,8 @@ pub mod inline_image;26
pub mod linux_desktop;26
pub mod linux_desktop;27
#[cfg(target_os = "linux")]27
#[cfg(target_os = "linux")]28
pub mod linux_fonts;28
pub mod linux_fonts;29
#[cfg(target_os = "linux")]30
mod linux_idle;29
pub mod native_dumps;31
pub mod native_dumps;30
pub mod notify;32
pub mod notify;31
pub mod orgs;33
pub mod orgs;@@ -0,0 +1,152 @@
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
}
@@ -0,0 +1,152 @@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 because5
//! it does not run X11 sessions. Without `WAYLAND_DISPLAY`, or when the6
//! compositor does not offer the protocol, this returns `None` and presence7
//! 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
None35
}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
state47
});48
let state = state.lock().ok()?;49
state50
.available51
.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 also70
// 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
}@@ -127,7 +127,12 @@ fn system_idle() -> Option<Duration> {
127
127
})
128
128
}
129
129
130
#[cfg(not(windows))]
130
#[cfg(target_os = "linux")]
131
fn system_idle() -> Option<Duration> {
132
crate::linux_idle::system_idle()
133
}
134
135
#[cfg(not(any(windows, target_os = "linux")))]
131
136
fn system_idle() -> Option<Duration> {
132
137
None
133
138
}
@@ -127,7 +127,12 @@ fn system_idle() -> Option<Duration> {127
})127
})128
}128
}129
129
130
#[cfg(not(windows))]130
#[cfg(target_os = "linux")]131
fn system_idle() -> Option<Duration> {132
crate::linux_idle::system_idle()133
}134
135
#[cfg(not(any(windows, target_os = "linux")))]131
fn system_idle() -> Option<Duration> {136
fn system_idle() -> Option<Duration> {132
None137
None133
}138
}