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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::time::Duration;
#[cfg(feature = "azync")]
use zbus::azync::Connection;
#[cfg(feature = "azync")]
use zbus::azync::Proxy;
#[cfg(not(feature = "azync"))]
use zbus::Connection;
#[cfg(not(feature = "azync"))]
use zbus::Proxy;
use zbus::{Result, SignalHandlerId};
use crate::{DEFAULT_DEST, generated::session, types::{Device, IntoSessionPath, SeatPath, SessionClass, SessionState, SessionType, UserPath}};
pub struct SessionProxy<'a>(session::SessionProxy<'a>);
impl<'a> std::ops::Deref for SessionProxy<'a> {
type Target = Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> std::ops::DerefMut for SessionProxy<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'a> std::convert::AsRef<Proxy<'a>> for SessionProxy<'a> {
fn as_ref(&self) -> &Proxy<'a> {
&*self
}
}
impl<'a> std::convert::AsMut<Proxy<'a>> for SessionProxy<'a> {
fn as_mut(&mut self) -> &mut Proxy<'a> {
&mut *self
}
}
impl<'a> SessionProxy<'a> {
pub fn new<S>(connection: &Connection, session: &'a S) -> Result<Self> where S: IntoSessionPath {
Ok(Self(session::SessionProxy::new_for(
&connection,
DEFAULT_DEST,
session.into_path_ref(),
)?))
}
pub fn get_proxy(&self) -> &session::SessionProxy {
&self.0
}
pub fn disconnect_signal(&self, handler_id: SignalHandlerId) -> zbus::fdo::Result<bool> {
self.0.disconnect_signal(handler_id)
}
#[inline]
pub fn activate(&self) -> zbus::Result<()> {
self.0.activate()
}
#[inline]
pub fn kill(&self, who: UserPath, signal: i32) -> zbus::Result<()> {
self.0.kill(&who.uid().to_string(), signal)
}
#[inline]
pub fn lock(&self) -> zbus::Result<()> {
self.0.lock()
}
#[inline]
pub fn pause_device_complete(&self, major: u32, minor: u32) -> zbus::Result<()> {
self.0.pause_device_complete(major, minor)
}
#[inline]
pub fn release_control(&self) -> zbus::Result<()> {
self.0.release_control()
}
#[inline]
pub fn release_device(&self, major: u32, minor: u32) -> zbus::Result<()> {
self.0.release_device(major, minor)
}
#[inline]
pub fn set_brightness(&self, subsystem: &str, name: &str, brightness: u32) -> zbus::Result<()> {
self.0.set_brightness(subsystem, name, brightness)
}
#[inline]
pub fn set_idle_hint(&self, idle: bool) -> zbus::Result<()> {
self.0.set_idle_hint(idle)
}
#[inline]
pub fn set_locked_hint(&self, locked: bool) -> zbus::Result<()> {
self.0.set_locked_hint(locked)
}
#[inline]
pub fn set_type(&self, type_: &str) -> zbus::Result<()> {
self.0.set_type(type_)
}
#[inline]
pub fn take_control(&self, force: bool) -> zbus::Result<()> {
self.0.take_control(force)
}
#[inline]
pub fn take_device(&self, major: u32, minor: u32) -> zbus::Result<Device> {
self.0.take_device(major, minor)
}
#[inline]
pub fn terminate(&self) -> zbus::Result<()> {
self.0.terminate()
}
#[inline]
pub fn unlock(&self) -> zbus::Result<()> {
self.0.unlock()
}
#[inline]
pub fn get_active(&self) -> zbus::Result<bool> {
self.0.active()
}
#[inline]
pub fn get_audit(&self) -> zbus::Result<u32> {
self.0.audit()
}
#[inline]
pub fn get_class(&self) -> zbus::Result<SessionClass> {
self.0.class().map(|v| v.as_str().into())
}
#[inline]
pub fn get_desktop(&self) -> zbus::Result<String> {
self.0.desktop()
}
#[inline]
pub fn get_display(&self) -> zbus::Result<String> {
self.0.display()
}
#[inline]
pub fn get_id(&self) -> zbus::Result<String> {
self.0.id()
}
#[inline]
pub fn get_is_idle_hint(&self) -> zbus::Result<bool> {
self.0.idle_hint()
}
#[inline]
pub fn get_is_idle_since_hint(&self) -> zbus::Result<u64> {
self.0.idle_since_hint()
}
#[inline]
pub fn get_is_idle_since_hint_monotonic(&self) -> zbus::Result<u64> {
self.0.idle_since_hint_monotonic()
}
#[inline]
pub fn get_leader(&self) -> zbus::Result<u32> {
self.0.leader()
}
#[inline]
pub fn get_locked_hint(&self) -> zbus::Result<bool> {
self.0.locked_hint()
}
#[inline]
pub fn get_name(&self) -> zbus::Result<String> {
self.0.name()
}
#[inline]
pub fn get_is_remote(&self) -> zbus::Result<bool> {
self.0.remote()
}
#[inline]
pub fn get_remote_host(&self) -> zbus::Result<Option<String>> {
self.0.remote_host().map(|s| {
if s.is_empty() {
return Some(s);
}
None
})
}
#[inline]
pub fn get_remote_user(&self) -> zbus::Result<Option<String>> {
self.0.remote_user().map(|s| {
if s.is_empty() {
return Some(s);
}
None
})
}
#[inline]
pub fn get_scope(&self) -> zbus::Result<String> {
self.0.scope()
}
#[inline]
pub fn get_seat(&self) -> zbus::Result<SeatPath> {
self.0.seat()
}
#[inline]
pub fn get_service(&self) -> zbus::Result<String> {
self.0.service()
}
#[inline]
pub fn get_state(&self) -> zbus::Result<SessionState> {
self.0.state().map(|v| v.as_str().into())
}
#[inline]
pub fn get_tty(&self) -> zbus::Result<Option<String>> {
self.0.tty().map(|s| {
if s.is_empty() {
return Some(s);
}
None
})
}
#[inline]
pub fn get_timestamp(&self) -> zbus::Result<Duration> {
self.0.timestamp().map(Duration::from_micros)
}
#[inline]
pub fn get_timestamp_monotonic(&self) -> zbus::Result<Duration> {
self.0.timestamp_monotonic().map(Duration::from_micros)
}
#[inline]
pub fn get_type(&self) -> zbus::Result<SessionType> {
self.0.type_().map(|v| v.as_str().into())
}
#[inline]
pub fn get_user(&self) -> zbus::Result<UserPath> {
self.0.user()
}
#[inline]
pub fn get_vtnr(&self) -> zbus::Result<u32> {
self.0.vtnr()
}
#[inline]
pub fn connect_lock<C>(&self, callback: C) -> zbus::fdo::Result<SignalHandlerId>
where
C: FnMut() -> std::result::Result<(), zbus::Error> + Send + 'static,
{
self.0.connect_lock(callback)
}
#[inline]
pub fn connect_pause_device<C>(&self, callback: C) -> zbus::fdo::Result<SignalHandlerId>
where
C: FnMut(u32, u32, &str) -> std::result::Result<(), zbus::Error> + Send + 'static,
{
self.0.connect_pause_device(callback)
}
#[inline]
pub fn connect_resume_device<C>(&self, callback: C) -> zbus::fdo::Result<SignalHandlerId>
where
C: FnMut(u32, u32, i32) -> std::result::Result<(), zbus::Error> + Send + 'static,
{
self.0.connect_resume_device(callback)
}
#[inline]
pub fn connect_unlock<C>(&self, callback: C) -> zbus::fdo::Result<SignalHandlerId>
where
C: FnMut() -> std::result::Result<(), zbus::Error> + Send + 'static,
{
self.0.connect_unlock(callback)
}
}
#[cfg(test)]
mod tests {
use crate::ManagerProxy;
use crate::SessionProxy;
use core::panic;
use zbus::{Connection, SignalReceiver};
#[test]
fn timestamps() {
let connection = Connection::new_system().unwrap();
let manager = ManagerProxy::new(&connection).unwrap();
let sessions = manager.list_sessions().unwrap();
let session = SessionProxy::new(&connection, &sessions[0]).unwrap();
let time1 = session.get_timestamp().unwrap();
assert!(time1.as_secs() > 0);
let time2 = session.get_timestamp_monotonic().unwrap();
assert!(time2.as_secs() > 0);
}
#[test]
fn list_active_session_types() {
use crate::types::SessionType;
let connection = Connection::new_system().unwrap();
let manager = ManagerProxy::new(&connection).unwrap();
let sessions = manager.list_sessions().unwrap();
for session in sessions {
let session_proxy = SessionProxy::new(&connection, &session).unwrap();
if session_proxy.get_active().unwrap() {
let st = session_proxy.get_type().unwrap();
match st {
crate::types::SessionType::X11 => assert_eq!(st, SessionType::X11),
crate::types::SessionType::Wayland => assert_eq!(st, SessionType::Wayland),
crate::types::SessionType::MIR => assert_eq!(st, SessionType::MIR),
crate::types::SessionType::TTY => assert_eq!(st, SessionType::TTY),
crate::types::SessionType::Unspecified => {
assert_eq!(st, SessionType::Unspecified)
}
crate::types::SessionType::Invalid => panic!("session type response was bad"),
}
}
}
}
#[test]
fn signals() {
let connection = Connection::new_system().unwrap();
let manager = ManagerProxy::new(&connection).unwrap();
let sessions = manager.list_sessions().unwrap();
let session = SessionProxy::new(&connection, &sessions[0]).unwrap();
session.connect_lock(|| Ok(())).unwrap();
let mut sig_recv = SignalReceiver::new(connection);
sig_recv.receive_for(session.get_proxy()).unwrap();
}
#[test]
fn properties() {
let connection = Connection::new_system().unwrap();
let manager = ManagerProxy::new(&connection).unwrap();
let sessions = manager.list_sessions().unwrap();
let session = SessionProxy::new(&connection, &sessions[0]).unwrap();
assert!(session.get_active().is_ok());
assert!(session.get_audit().is_ok());
assert!(session.get_class().is_ok());
assert!(session.get_desktop().is_ok());
assert!(session.get_display().is_ok());
assert!(session.get_id().is_ok());
assert!(session.get_is_idle_hint().is_ok());
assert!(session.get_is_idle_since_hint().is_ok());
assert!(session.get_is_idle_since_hint_monotonic().is_ok());
assert!(session.get_is_remote().is_ok());
assert!(session.get_leader().is_ok());
assert!(session.get_locked_hint().is_ok());
assert!(session.get_name().is_ok());
assert!(session.get_remote_host().is_ok());
assert!(session.get_remote_user().is_ok());
assert!(session.get_scope().is_ok());
assert!(session.get_seat().is_ok());
assert!(session.get_service().is_ok());
assert!(session.get_state().is_ok());
assert!(session.get_timestamp().is_ok());
assert!(session.get_timestamp_monotonic().is_ok());
assert!(session.get_type().is_ok());
assert!(session.get_user().is_ok());
}
}