logind_zbus/session/
types.rs

1use serde::{Deserialize, Serialize};
2use std::str::FromStr;
3use zbus::fdo;
4use zbus::zvariant::{OwnedFd, OwnedObjectPath, OwnedValue, Structure, Type};
5
6use crate::{enum_impl_serde_str, enum_impl_str_conv, impl_try_from_owned_as_str};
7
8#[derive(Debug, PartialEq, Eq, Clone, Type, Serialize, Deserialize)]
9pub struct User {
10    uid: u32,
11    /// Name of session user
12    path: OwnedObjectPath,
13}
14
15impl User {
16    pub fn uid(&self) -> u32 {
17        self.uid
18    }
19
20    pub fn path(&self) -> &OwnedObjectPath {
21        &self.path
22    }
23}
24
25impl TryFrom<OwnedValue> for User {
26    type Error = zbus::Error;
27
28    fn try_from(value: OwnedValue) -> Result<Self, Self::Error> {
29        let value = <Structure<'_>>::try_from(value)?;
30        Ok(Self {
31            uid: <u32>::try_from(value.fields()[0].try_clone()?)?,
32            path: <OwnedObjectPath>::try_from(value.fields()[1].try_clone()?)?,
33        })
34    }
35}
36
37/// The type of Session
38#[derive(Debug, PartialEq, Eq, Clone, Copy, Type)]
39#[zvariant(signature = "s")]
40pub enum SessionType {
41    X11,
42    Wayland,
43    MIR,
44    TTY,
45    Unspecified,
46}
47enum_impl_serde_str!(SessionType);
48impl_try_from_owned_as_str!(SessionType);
49enum_impl_str_conv!(SessionType, {
50    "wayland": Wayland,
51    "x11": X11,
52    "mir": MIR,
53    "tty": TTY,
54    "unspecified": Unspecified,
55});
56
57#[derive(Debug, PartialEq, Eq, Type, Serialize, Deserialize)]
58pub struct Device {
59    file_descriptor: OwnedFd,
60    inactive: bool,
61}
62
63impl Device {
64    pub fn file_descriptor(&self) -> std::os::unix::io::RawFd {
65        use std::os::unix::io::AsRawFd;
66        self.file_descriptor.as_raw_fd()
67    }
68
69    pub fn inactive(&self) -> bool {
70        self.inactive
71    }
72}
73
74/// Class of Session
75#[derive(Debug, PartialEq, Eq, Clone, Copy, Type)]
76#[zvariant(signature = "s")]
77pub enum SessionClass {
78    User,
79    Greeter,
80    LockScreen,
81    Manager,
82}
83enum_impl_serde_str!(SessionClass);
84impl_try_from_owned_as_str!(SessionClass);
85enum_impl_str_conv!(SessionClass, {
86    "user": User,
87    "greeter": Greeter,
88    "lock-screen": LockScreen,
89    "manager": Manager,
90});
91
92/// State of a session
93#[derive(Debug, PartialEq, Eq, Clone, Copy, Type)]
94#[zvariant(signature = "s")]
95pub enum SessionState {
96    Online,
97    Active,
98    Closing,
99}
100enum_impl_serde_str!(SessionState);
101impl_try_from_owned_as_str!(SessionState);
102enum_impl_str_conv!(SessionState, {
103    "online": Online,
104    "active": Active,
105    "closing": Closing,
106});