forked from Smithay/smithay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
396 lines (345 loc) · 12.9 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! This interface allows clients to monitor user idle status.
//!
//! ```
//! # extern crate wayland_server;
//! # #[macro_use] extern crate smithay;
//! use smithay::delegate_idle_notify;
//! use smithay::wayland::idle_notify::{IdleNotifierState, IdleNotifierHandler};
//! # use smithay::input::{Seat, SeatHandler, SeatState, pointer::CursorImageStatus};
//! # use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
//!
//! struct State { idle_notifier: IdleNotifierState<Self> }
//! # let mut event_loop = smithay::reexports::calloop::EventLoop::<State>::try_new().unwrap();
//! # let mut display = wayland_server::Display::<State>::new().unwrap();
//! // Create the idle_notifier state
//! let idle_notifier = IdleNotifierState::<State>::new(
//! &display.handle(),
//! event_loop.handle(),
//! );
//!
//! let state = State { idle_notifier };
//!
//! // Implement the necessary trait
//! # impl SeatHandler for State {
//! # type KeyboardFocus = WlSurface;
//! # type PointerFocus = WlSurface;
//! # type TouchFocus = WlSurface;
//! # fn seat_state(&mut self) -> &mut SeatState<Self> { unimplemented!() }
//! # fn focus_changed(&mut self, seat: &Seat<Self>, focused: Option<&WlSurface>) { unimplemented!() }
//! # fn cursor_image(&mut self, seat: &Seat<Self>, image: CursorImageStatus) { unimplemented!() }
//! # }
//! impl IdleNotifierHandler for State {
//! fn idle_notifier_state(&mut self) -> &mut IdleNotifierState<Self> {
//! &mut self.idle_notifier
//! }
//! }
//! delegate_idle_notify!(State);
//!
//! // On input you should notify the idle_notifier
//! // state.idle_notifier.notify_activity(&seat);
//! ```
use std::{
collections::HashMap,
sync::{
atomic::{self, AtomicBool},
Mutex,
},
time::Duration,
};
use calloop::{timer::TimeoutAction, LoopHandle, RegistrationToken};
use wayland_protocols::ext::idle_notify::v1::server::{
ext_idle_notification_v1::{self, ExtIdleNotificationV1},
ext_idle_notifier_v1::{self, ExtIdleNotifierV1},
};
use wayland_server::{
backend::{ClientId, GlobalId},
protocol::wl_seat::WlSeat,
Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource,
};
use crate::input::{Seat, SeatHandler};
/// Handler trait for ext-idle-notify
pub trait IdleNotifierHandler: Sized {
/// [`IdleNotifierState`] getter
fn idle_notifier_state(&mut self) -> &mut IdleNotifierState<Self>;
}
/// User data of the [`ExtIdleNotificationV1`] resource
#[derive(Debug)]
pub struct IdleNotificationUserData {
seat: WlSeat,
is_idle: AtomicBool,
timeout: Duration,
timer_token: Mutex<Option<RegistrationToken>>,
/// If listener was created with `get_input_idle_notification`
ignore_inhibitor: bool,
}
impl IdleNotificationUserData {
fn take_timer_token(&self) -> Option<RegistrationToken> {
self.timer_token.lock().unwrap().take()
}
fn set_timer_token(&self, idle: Option<RegistrationToken>) {
*self.timer_token.lock().unwrap() = idle;
}
fn set_idle(&self, idle: bool) {
self.is_idle.store(idle, atomic::Ordering::Release);
}
fn is_idle(&self) -> bool {
self.is_idle.load(atomic::Ordering::Acquire)
}
}
/// State of ext-idle-notify module
#[derive(Debug)]
pub struct IdleNotifierState<D> {
global: GlobalId,
notifications: HashMap<WlSeat, Vec<ExtIdleNotificationV1>>,
loop_handle: LoopHandle<'static, D>,
is_inhibited: bool,
}
impl<D: IdleNotifierHandler> IdleNotifierState<D> {
/// Create new [`ExtIdleNotifierV1`] global.
pub fn new(display: &DisplayHandle, loop_handle: LoopHandle<'static, D>) -> Self
where
D: GlobalDispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotificationV1, IdleNotificationUserData>,
D: IdleNotifierHandler,
D: 'static,
{
let global = display.create_global::<D, ExtIdleNotifierV1, _>(1, ());
Self {
global,
notifications: HashMap::new(),
loop_handle,
is_inhibited: false,
}
}
/// Inhibit entering idle state, eg. by the idle-inhibit protocol
pub fn set_is_inhibited(&mut self, is_inhibited: bool) {
if self.is_inhibited == is_inhibited {
return;
}
self.is_inhibited = is_inhibited;
for notification in self.notifications() {
let data = notification.data::<IdleNotificationUserData>().unwrap();
if data.ignore_inhibitor {
continue;
}
if is_inhibited {
if data.is_idle() {
notification.resumed();
data.set_idle(false);
}
if let Some(token) = data.take_timer_token() {
self.loop_handle.remove(token);
}
} else {
self.reinsert_timer(notification);
}
}
}
/// Is idle state inhibited, eg. by the idle-inhibit protocol
pub fn is_inhibited(&mut self) -> bool {
self.is_inhibited
}
/// Should be called whenever activity occurs on a seat, eg. mouse/keyboard input.
///
/// You may want to use [`Self::notify_activity`] instead which accepts a [`Seat`].
pub fn notify_activity_for_wl_seat(&mut self, seat: &WlSeat) {
let Some(notifications) = self.notifications.get(seat) else {
return;
};
for notification in notifications {
let data = notification.data::<IdleNotificationUserData>().unwrap();
if data.is_idle() {
notification.resumed();
data.set_idle(false);
}
self.reinsert_timer(notification);
}
}
/// Returns the [`ExtIdleNotifierV1`] global.
pub fn global(&self) -> GlobalId {
self.global.clone()
}
fn notifications(&self) -> impl Iterator<Item = &ExtIdleNotificationV1> {
self.notifications.values().flatten()
}
fn reinsert_timer(&self, notification: &ExtIdleNotificationV1) {
let data = notification.data::<IdleNotificationUserData>().unwrap();
if let Some(token) = data.take_timer_token() {
self.loop_handle.remove(token);
}
if !data.ignore_inhibitor && self.is_inhibited {
return;
}
let token = self
.loop_handle
.insert_source(calloop::timer::Timer::from_duration(data.timeout), {
let idle_notification = notification.clone();
move |_, _, state| {
let data = idle_notification.data::<IdleNotificationUserData>().unwrap();
let is_inhibited = !data.ignore_inhibitor && state.idle_notifier_state().is_inhibited;
let is_idle_already = data.is_idle();
if !is_inhibited && !is_idle_already {
idle_notification.idled();
data.set_idle(true);
}
data.set_timer_token(None);
TimeoutAction::Drop
}
});
data.set_timer_token(token.ok());
}
}
impl<D: IdleNotifierHandler + SeatHandler> IdleNotifierState<D> {
/// Should be called whenever activity occurs on a seat, eg. mouse/keyboard input.
pub fn notify_activity(&mut self, seat: &Seat<D>) {
for seat in &seat.arc.inner.lock().unwrap().known_seats {
if let Ok(seat) = seat.upgrade() {
self.notify_activity_for_wl_seat(&seat);
}
}
}
}
impl<D> GlobalDispatch<ExtIdleNotifierV1, (), D> for IdleNotifierState<D>
where
D: GlobalDispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotificationV1, IdleNotificationUserData>,
D: IdleNotifierHandler,
D: 'static,
{
fn bind(
_state: &mut D,
_handle: &DisplayHandle,
_client: &Client,
resource: New<ExtIdleNotifierV1>,
_global_data: &(),
data_init: &mut wayland_server::DataInit<'_, D>,
) {
data_init.init(resource, ());
}
}
impl<D> Dispatch<ExtIdleNotifierV1, (), D> for IdleNotifierState<D>
where
D: GlobalDispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotifierV1, ()>,
D: Dispatch<ExtIdleNotificationV1, IdleNotificationUserData>,
D: IdleNotifierHandler,
D: 'static,
{
fn request(
state: &mut D,
_client: &Client,
_resource: &ExtIdleNotifierV1,
request: ext_idle_notifier_v1::Request,
_data: &(),
_dhandle: &DisplayHandle,
data_init: &mut DataInit<'_, D>,
) {
match request {
ext_idle_notifier_v1::Request::GetIdleNotification { id, timeout, seat } => {
let timeout = Duration::from_millis(timeout as u64);
let idle_notifier_state = state.idle_notifier_state();
let idle_notification = data_init.init(
id,
IdleNotificationUserData {
seat: seat.clone(),
is_idle: AtomicBool::new(false),
timeout,
timer_token: Mutex::new(None),
ignore_inhibitor: false,
},
);
idle_notifier_state.reinsert_timer(&idle_notification);
state
.idle_notifier_state()
.notifications
.entry(seat)
.or_default()
.push(idle_notification);
}
ext_idle_notifier_v1::Request::GetInputIdleNotification { id, timeout, seat } => {
let timeout = Duration::from_millis(timeout as u64);
let idle_notifier_state = state.idle_notifier_state();
let idle_notification = data_init.init(
id,
IdleNotificationUserData {
seat: seat.clone(),
is_idle: AtomicBool::new(false),
timeout,
timer_token: Mutex::new(None),
ignore_inhibitor: true,
},
);
idle_notifier_state.reinsert_timer(&idle_notification);
state
.idle_notifier_state()
.notifications
.entry(seat)
.or_default()
.push(idle_notification);
}
ext_idle_notifier_v1::Request::Destroy => {}
_ => unimplemented!(),
}
}
}
impl<D> Dispatch<ExtIdleNotificationV1, IdleNotificationUserData, D> for IdleNotifierState<D>
where
D: Dispatch<ExtIdleNotificationV1, IdleNotificationUserData>,
D: IdleNotifierHandler,
{
fn request(
_state: &mut D,
_client: &Client,
_resource: &ExtIdleNotificationV1,
request: ext_idle_notification_v1::Request,
_data: &IdleNotificationUserData,
_dhandle: &DisplayHandle,
_data_init: &mut DataInit<'_, D>,
) {
match request {
ext_idle_notification_v1::Request::Destroy => {}
_ => unimplemented!(),
}
}
fn destroyed(
state: &mut D,
_client: ClientId,
notification: &ExtIdleNotificationV1,
data: &IdleNotificationUserData,
) {
let state = state.idle_notifier_state();
if let Some(notifications) = state.notifications.get_mut(&data.seat) {
notifications.retain(|x| x != notification);
}
state
.notifications
.retain(|seat, notifications| !notifications.is_empty() && seat.is_alive());
}
}
/// Macro to delegate implementation of the ext idle notify protocol
#[macro_export]
macro_rules! delegate_idle_notify {
($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
type __ExtIdleNotifierV1 =
$crate::reexports::wayland_protocols::ext::idle_notify::v1::server::ext_idle_notifier_v1::ExtIdleNotifierV1;
type __ExtIdleNotificationV1 =
$crate::reexports::wayland_protocols::ext::idle_notify::v1::server::ext_idle_notification_v1::ExtIdleNotificationV1;
$crate::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
[
__ExtIdleNotifierV1: ()
] => $crate::wayland::idle_notify::IdleNotifierState<$ty>
);
$crate::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
[
__ExtIdleNotifierV1: ()
] => $crate::wayland::idle_notify::IdleNotifierState<$ty>
);
$crate::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty:
[
__ExtIdleNotificationV1: $crate::wayland::idle_notify::IdleNotificationUserData
] => $crate::wayland::idle_notify::IdleNotifierState<$ty>
);
};
}