i3status_rs/blocks/battery/
upower.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
use tokio::try_join;
use zbus::fdo::{PropertiesChangedStream, PropertiesProxy};
use zbus::{zvariant, Connection};
use zvariant::ObjectPath;

use super::{BatteryDevice, BatteryInfo, BatteryStatus, DeviceName};
use crate::blocks::prelude::*;
use crate::util::new_system_dbus_connection;

const DISPLAY_DEVICE_PATH: ObjectPath =
    ObjectPath::from_static_str_unchecked("/org/freedesktop/UPower/devices/DisplayDevice");

struct DeviceConnection {
    device_proxy: DeviceProxy<'static>,
    changes: PropertiesChangedStream,
}

impl DeviceConnection {
    async fn new(
        dbus_conn: &Connection,
        device: &DeviceName,
        expected_model: Option<&str>,
    ) -> Result<Option<Self>> {
        let device_proxy =
            if device.exact().is_none_or(|d| d == "DisplayDevice") && expected_model.is_none() {
                DeviceProxy::builder(dbus_conn)
                    .path(DISPLAY_DEVICE_PATH)
                    .unwrap()
                    .build()
                    .await
                    .error("Failed to create DeviceProxy")?
            } else {
                let mut res = None;
                for path in UPowerProxy::new(dbus_conn)
                    .await
                    .error("Failed to create UPowerProxy")?
                    .enumerate_devices()
                    .await
                    .error("Failed to retrieve UPower devices")?
                {
                    let proxy = DeviceProxy::builder(dbus_conn)
                        .path(path)
                        .unwrap()
                        .build()
                        .await
                        .error("Failed to create DeviceProxy")?;

                    // Filter by model if needed
                    if let Some(expected_model) = &expected_model {
                        if let Ok(device_model) = proxy.model().await {
                            if !expected_model.eq(&device_model) {
                                continue;
                            }
                        }
                    }
                    // Verify device type
                    // https://upower.freedesktop.org/docs/Device.html#Device:Type
                    // consider any peripheral, UPS and internal battery
                    let device_type = proxy.type_().await.error("Failed to get device's type")?;
                    if device_type == 1 {
                        continue;
                    }
                    let name = proxy
                        .native_path()
                        .await
                        .error("Failed to get device's native path")?;
                    if device.matches(&name) {
                        res = Some(proxy);
                        break;
                    }
                }
                match res {
                    Some(res) => res,
                    None => return Ok(None),
                }
            };

        let changes = PropertiesProxy::builder(dbus_conn)
            .destination("org.freedesktop.UPower")
            .unwrap()
            .path(device_proxy.inner().path().to_owned())
            .unwrap()
            .build()
            .await
            .error("Failed to create PropertiesProxy")?
            .receive_properties_changed()
            .await
            .error("Failed to create PropertiesChangedStream")?;

        Ok(Some(DeviceConnection {
            device_proxy,
            changes,
        }))
    }
}

pub(super) struct Device {
    dbus_conn: Connection,
    device: DeviceName,
    dev_model: Option<String>,
    device_conn: Option<DeviceConnection>,
    device_added_stream: DeviceAddedStream,
    device_removed_stream: DeviceRemovedStream,
}

impl Device {
    pub(super) async fn new(device: DeviceName, dev_model: Option<String>) -> Result<Self> {
        let dbus_conn = new_system_dbus_connection().await?;

        let device_conn = DeviceConnection::new(&dbus_conn, &device, dev_model.as_deref()).await?;

        let upower_proxy = UPowerProxy::new(&dbus_conn)
            .await
            .error("Could not create UPowerProxy")?;

        let (device_added_stream, device_removed_stream) = try_join! {
            upower_proxy.receive_device_added(),
            upower_proxy.receive_device_removed()
        }
        .error("Could not create signal stream")?;

        Ok(Self {
            dbus_conn,
            device,
            dev_model,
            device_conn,
            device_added_stream,
            device_removed_stream,
        })
    }
}

#[async_trait]
impl BatteryDevice for Device {
    async fn get_info(&mut self) -> Result<Option<BatteryInfo>> {
        match &self.device_conn {
            None => Ok(None),
            Some(device_conn) => {
                match try_join! {
                    device_conn.device_proxy.percentage(),
                    device_conn.device_proxy.energy_rate(),
                    device_conn.device_proxy.state(),
                    device_conn.device_proxy.time_to_full(),
                    device_conn.device_proxy.time_to_empty(),
                } {
                    Err(_) => Ok(None),
                    Ok((capacity, power, state, time_to_full, time_to_empty)) => {
                        let status = match state {
                            1 => BatteryStatus::Charging,
                            2 | 6 => BatteryStatus::Discharging,
                            3 => BatteryStatus::Empty,
                            4 => BatteryStatus::Full,
                            5 => BatteryStatus::NotCharging,
                            _ => BatteryStatus::Unknown,
                        };

                        let time_remaining = match status {
                            BatteryStatus::Charging => Some(time_to_full as f64),
                            BatteryStatus::Discharging => Some(time_to_empty as f64),
                            _ => None,
                        };

                        Ok(Some(BatteryInfo {
                            status,
                            capacity,
                            power: Some(power),
                            time_remaining,
                        }))
                    }
                }
            }
        }
    }

    async fn wait_for_change(&mut self) -> Result<()> {
        match &mut self.device_conn {
            Some(device_conn) => loop {
                select! {
                    _ = self.device_added_stream.next() => {},
                    _ = device_conn.changes.next() => {
                        break;
                    },
                    Some(msg) = self.device_removed_stream.next() => {
                        let args = msg.args().unwrap();
                        if args.device().as_ref() == device_conn.device_proxy.inner().path().as_ref() {
                            self.device_conn = None;
                            break;
                        }
                    },
                }
            },
            None => loop {
                select! {
                    _ = self.device_removed_stream.next() => {},
                    _ = self.device_added_stream.next() => {
                        if let Some(device_conn) =
                        DeviceConnection::new(&self.dbus_conn, &self.device, self.dev_model.as_deref()).await?
                        {
                            self.device_conn = Some(device_conn);
                            break;
                        }
                    },
                }
            },
        }

        Ok(())
    }
}

#[zbus::proxy(
    interface = "org.freedesktop.UPower.Device",
    default_service = "org.freedesktop.UPower"
)]
trait Device {
    #[zbus(property)]
    fn energy_rate(&self) -> zbus::Result<f64>;

    #[zbus(property)]
    fn is_present(&self) -> zbus::Result<bool>;

    #[zbus(property)]
    fn native_path(&self) -> zbus::Result<String>;

    #[zbus(property)]
    fn model(&self) -> zbus::Result<String>;

    #[zbus(property)]
    fn online(&self) -> zbus::Result<bool>;

    #[zbus(property)]
    fn percentage(&self) -> zbus::Result<f64>;

    #[zbus(property)]
    fn state(&self) -> zbus::Result<u32>;

    #[zbus(property)]
    fn time_to_empty(&self) -> zbus::Result<i64>;

    #[zbus(property)]
    fn time_to_full(&self) -> zbus::Result<i64>;

    #[zbus(property, name = "Type")]
    fn type_(&self) -> zbus::Result<u32>;
}

#[zbus::proxy(
    interface = "org.freedesktop.UPower",
    default_service = "org.freedesktop.UPower",
    default_path = "/org/freedesktop/UPower"
)]
trait UPower {
    fn enumerate_devices(&self) -> zbus::Result<Vec<zvariant::OwnedObjectPath>>;

    fn get_display_device(&self) -> zbus::Result<zvariant::OwnedObjectPath>;

    #[zbus(signal)]
    fn device_added(&self, device: zvariant::OwnedObjectPath) -> zbus::Result<()>;

    #[zbus(signal)]
    fn device_removed(&self, device: zvariant::OwnedObjectPath) -> zbus::Result<()>;

    #[zbus(property)]
    fn on_battery(&self) -> zbus::Result<bool>;
}