i3status_rs/blocks/privacy/
v4l.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
use debounced::{debounced, Debounced};
use inotify::{EventStream, Inotify, WatchDescriptor, WatchMask, Watches};
use tokio::fs::{read_dir, File};
use tokio::time::{interval, Interval};

use std::path::PathBuf;

use super::*;

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(rename_all = "lowercase", deny_unknown_fields, default)]
pub struct Config {
    exclude_device: Vec<PathBuf>,
    #[default(vec!["pipewire".into(), "wireplumber".into()])]
    exclude_consumer: Vec<String>,
}

pub(super) struct Monitor<'a> {
    config: &'a Config,
    devices: HashMap<PathBuf, WatchDescriptor>,
    interval: Interval,
    watches: Watches,
    updates: Debounced<EventStream<[u8; 1024]>>,
}

impl<'a> Monitor<'a> {
    pub(super) async fn new(config: &'a Config, duration: Duration) -> Result<Self> {
        let notify = Inotify::init().error("Failed to start inotify")?;
        let watches = notify.watches();

        let updates = debounced(
            notify
                .into_event_stream([0; 1024])
                .error("Failed to create event stream")?,
            Duration::from_millis(100),
        );

        let mut s = Self {
            config,
            devices: HashMap::new(),
            interval: interval(duration),
            watches,
            updates,
        };
        s.update_devices().await?;

        Ok(s)
    }

    async fn update_devices(&mut self) -> Result<bool> {
        let mut changes = false;
        let mut devices_to_remove: HashMap<PathBuf, WatchDescriptor> = self.devices.clone();
        let mut sysfs_paths = read_dir("/dev").await.error("Unable to read /dev")?;
        while let Some(entry) = sysfs_paths
            .next_entry()
            .await
            .error("Unable to get next device in /dev")?
        {
            if let Some(file_name) = entry.file_name().to_str() {
                if !file_name.starts_with("video") {
                    continue;
                }
            }

            let sysfs_path = entry.path();

            if self.config.exclude_device.contains(&sysfs_path) {
                debug!("ignoring {:?}", sysfs_path);
                continue;
            }

            if self.devices.contains_key(&sysfs_path) {
                devices_to_remove.remove(&sysfs_path);
            } else {
                debug!("adding watch {:?}", sysfs_path);
                self.devices.insert(
                    sysfs_path.clone(),
                    self.watches
                        .add(&sysfs_path, WatchMask::OPEN | WatchMask::CLOSE)
                        .error("Failed to watch data location")?,
                );
                changes = true;
            }
        }
        for (sysfs_path, wd) in devices_to_remove {
            debug!("removing watch {:?}", sysfs_path);
            self.devices.remove(&sysfs_path);
            self.watches
                .remove(wd)
                .error("Failed to unwatch data location")?;
            changes = true;
        }

        Ok(changes)
    }
}

#[async_trait]
impl PrivacyMonitor for Monitor<'_> {
    async fn get_info(&mut self) -> Result<PrivacyInfo> {
        let mut mapping: PrivacyInfo = PrivacyInfo::new();

        let mut proc_paths = read_dir("/proc").await.error("Unable to read /proc")?;
        while let Some(proc_path) = proc_paths
            .next_entry()
            .await
            .error("Unable to get next device in /proc")?
        {
            let proc_path = proc_path.path();
            let fd_path = proc_path.join("fd");
            let Ok(mut fd_paths) = read_dir(fd_path).await else {
                continue;
            };
            while let Ok(Some(fd_path)) = fd_paths.next_entry().await {
                let Ok(link_path) = fd_path.path().read_link() else {
                    continue;
                };
                if self.devices.contains_key(&link_path) {
                    let Ok(mut file) = File::open(proc_path.join("comm")).await else {
                        continue;
                    };
                    let mut contents = String::new();
                    if file.read_to_string(&mut contents).await.is_ok() {
                        let reader = contents.trim_end().to_string();
                        if self.config.exclude_consumer.contains(&reader) {
                            continue;
                        }
                        debug!("{} {:?}", reader, link_path);
                        *mapping
                            .entry(Type::Webcam)
                            .or_default()
                            .entry(link_path.to_string_lossy().to_string())
                            .or_default()
                            .entry(reader)
                            .or_default() += 1;
                        debug!("{:?}", mapping);
                    }
                }
            }
        }
        Ok(mapping)
    }

    async fn wait_for_change(&mut self) -> Result<()> {
        loop {
            select! {
                _ = self.interval.tick() => {
                    if self.update_devices().await? {
                        break;
                    }
                },
                _ = self.updates.next() => break,
            }
        }
        Ok(())
    }
}