blob: cac5edadfeab802a9d6b9804160ea255a84f26ac (
plain)
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
|
use crate::error::{ Error, Result };
use crate::facility::{ FacilityMap, FacilityType };
use crate::facility::envsys::EnvSys;
use crate::module::{ CreatableModule, Module };
use std::time::Duration;
pub struct BatteryModule {
is_visible: bool,
}
impl CreatableModule for BatteryModule {
const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS;
fn new(facilities: &mut FacilityMap) -> Result<BatteryModule> {
let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom(
"Battery module can't access envsys facility."))?;
let mut is_visible = false;
for name in envsys.device_iter() {
if Self::is_battery_device(name) {
is_visible = true;
}
}
Ok(BatteryModule { is_visible })
}
}
impl Module for BatteryModule {
fn format(&mut self, facilities: &mut FacilityMap) -> Result<String> {
let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom(
"Battery module can't access envsys facility."))?;
/* When there's more than one battery, we want to present them in an
* order which is both stable and predictable. Their device names are also
* user-visible via envstat(8), so lexicographic ordering will be fine for
* both needs. Anyway, to make that happen, we collect and sort the ones
* we're interested in in advance.
*/
let mut devices = Vec::new();
for name in envsys.device_iter() {
if Self::is_battery_device(name) {
devices.push(name);
}
}
devices.sort();
self.is_visible = !devices.is_empty();
let mut text = String::new();
for device in &devices {
if !text.is_empty() {
text.push_str(" ");
}
text.push_str(device);
}
Ok(text)
}
fn sleep_duration(&self) -> Option<Duration> {
None
}
fn is_visible(&self) -> bool {
self.is_visible
}
}
impl BatteryModule {
fn is_battery_device(name: &str) -> bool {
name.starts_with("acpibat")
}
}
|