i3status_rs/blocks/privacy/
pipewire.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
266
use std::cell::Cell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, Mutex, Weak};
use std::thread;

use ::pipewire::{
    context::Context, keys, main_loop::MainLoop, properties::properties, spa::utils::dict::DictRef,
    types::ObjectType,
};
use itertools::Itertools;
use tokio::sync::Notify;

use super::*;

static CLIENT: LazyLock<Result<Client>> = LazyLock::new(Client::new);

#[derive(Debug)]
struct Node {
    name: String,
    nick: Option<String>,
    media_class: Option<String>,
    media_role: Option<String>,
    description: Option<String>,
}

impl Node {
    fn new(global_id: u32, global_props: &DictRef) -> Self {
        Self {
            name: global_props
                .get(&keys::NODE_NAME)
                .map_or_else(|| format!("node_{}", global_id), |s| s.to_string()),
            nick: global_props.get(&keys::NODE_NICK).map(|s| s.to_string()),
            media_class: global_props.get(&keys::MEDIA_CLASS).map(|s| s.to_string()),
            media_role: global_props.get(&keys::MEDIA_ROLE).map(|s| s.to_string()),
            description: global_props
                .get(&keys::NODE_DESCRIPTION)
                .map(|s| s.to_string()),
        }
    }
}

#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
struct Link {
    link_output_node: u32,
    link_input_node: u32,
}

impl Link {
    fn new(global_props: &DictRef) -> Option<Self> {
        if let (Some(link_output_node), Some(link_input_node)) = (
            global_props
                .get(&keys::LINK_OUTPUT_NODE)
                .and_then(|s| s.parse().ok()),
            global_props
                .get(&keys::LINK_INPUT_NODE)
                .and_then(|s| s.parse().ok()),
        ) {
            Some(Self {
                link_output_node,
                link_input_node,
            })
        } else {
            None
        }
    }
}

#[derive(Default)]
struct Data {
    nodes: HashMap<u32, Node>,
    links: HashMap<u32, Link>,
}

#[derive(Default)]
struct Client {
    event_listeners: Mutex<Vec<Weak<Notify>>>,
    data: Mutex<Data>,
}

impl Client {
    fn new() -> Result<Client> {
        thread::Builder::new()
            .name("privacy_pipewire".to_string())
            .spawn(Client::main_loop_thread)
            .error("failed to spawn a thread")?;

        Ok(Client::default())
    }

    fn main_loop_thread() {
        let client = CLIENT.as_ref().error("Could not get client").unwrap();

        let proplist = properties! {*keys::APP_NAME => env!("CARGO_PKG_NAME")};

        let main_loop = MainLoop::new(None).expect("Failed to create main loop");

        let context =
            Context::with_properties(&main_loop, proplist).expect("Failed to create context");
        let core = context.connect(None).expect("Failed to connect");
        let registry = core.get_registry().expect("Failed to get registry");

        let updated = Rc::new(Cell::new(false));
        let updated_copy = updated.clone();
        let updated_copy2 = updated.clone();

        // Register a callback to the `global` event on the registry, which notifies of any new global objects
        // appearing on the remote.
        // The callback will only get called as long as we keep the returned listener alive.
        let _registry_listener = registry
            .add_listener_local()
            .global(move |global| {
                let Some(global_props) = global.props else {
                    return;
                };
                match &global.type_ {
                    ObjectType::Node => {
                        client
                            .data
                            .lock()
                            .unwrap()
                            .nodes
                            .insert(global.id, Node::new(global.id, global_props));
                        updated_copy.set(true);
                    }
                    ObjectType::Link => {
                        let Some(link) = Link::new(global_props) else {
                            return;
                        };
                        client.data.lock().unwrap().links.insert(global.id, link);
                        updated_copy.set(true);
                    }
                    _ => (),
                }
            })
            .global_remove(move |uid| {
                let mut data = client.data.lock().unwrap();
                if data.nodes.remove(&uid).is_some() || data.links.remove(&uid).is_some() {
                    updated_copy2.set(true);
                }
            })
            .register();

        loop {
            main_loop.loop_().iterate(Duration::from_secs(60 * 60 * 24));
            if updated.get() {
                updated.set(false);
                client
                    .event_listeners
                    .lock()
                    .unwrap()
                    .retain(|notify| notify.upgrade().inspect(|x| x.notify_one()).is_some());
            }
        }
    }

    fn add_event_listener(&self, notify: &Arc<Notify>) {
        self.event_listeners
            .lock()
            .unwrap()
            .push(Arc::downgrade(notify));
    }
}

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(rename_all = "lowercase", deny_unknown_fields, default)]
pub struct Config {
    exclude_output: Vec<String>,
    exclude_input: Vec<String>,
    display: NodeDisplay,
}

#[derive(Deserialize, Debug, SmartDefault)]
#[serde(rename_all = "snake_case")]
enum NodeDisplay {
    #[default]
    Name,
    Description,
    Nickname,
}

impl NodeDisplay {
    fn map_node(&self, node: &Node) -> String {
        match self {
            NodeDisplay::Name => node.name.clone(),
            NodeDisplay::Description => node.description.clone().unwrap_or(node.name.clone()),
            NodeDisplay::Nickname => node.nick.clone().unwrap_or(node.name.clone()),
        }
    }
}

pub(super) struct Monitor<'a> {
    config: &'a Config,
    notify: Arc<Notify>,
}

impl<'a> Monitor<'a> {
    pub(super) async fn new(config: &'a Config) -> Result<Self> {
        let client = CLIENT.as_ref().error("Could not get client")?;
        let notify = Arc::new(Notify::new());
        client.add_event_listener(&notify);
        Ok(Self { config, notify })
    }
}

#[async_trait]
impl PrivacyMonitor for Monitor<'_> {
    async fn get_info(&mut self) -> Result<PrivacyInfo> {
        let client = CLIENT.as_ref().error("Could not get client")?;
        let data = client.data.lock().unwrap();
        let mut mapping: PrivacyInfo = PrivacyInfo::new();

        for node in data.nodes.values() {
            debug! {"{:?}", node};
        }

        // The links must be sorted and then dedup'ed since you can multiple links between any given pair of nodes
        for Link {
            link_output_node,
            link_input_node,
            ..
        } in data.links.values().sorted().dedup()
        {
            let (Some(output_node), Some(input_node)) = (
                data.nodes.get(link_output_node),
                data.nodes.get(link_input_node),
            ) else {
                continue;
            };
            if input_node.media_class != Some("Audio/Sink".into())
                && !self.config.exclude_output.contains(&output_node.name)
                && !self.config.exclude_input.contains(&input_node.name)
            {
                let type_ = if input_node.media_class == Some("Stream/Input/Video".into()) {
                    if output_node.media_role == Some("Camera".into()) {
                        Type::Webcam
                    } else {
                        Type::Video
                    }
                } else if input_node.media_class == Some("Stream/Input/Audio".into()) {
                    if output_node.media_class == Some("Audio/Sink".into()) {
                        Type::AudioSink
                    } else {
                        Type::Audio
                    }
                } else {
                    Type::Unknown
                };
                *mapping
                    .entry(type_)
                    .or_default()
                    .entry(self.config.display.map_node(output_node))
                    .or_default()
                    .entry(self.config.display.map_node(input_node))
                    .or_default() += 1;
            }
        }

        Ok(mapping)
    }

    async fn wait_for_change(&mut self) -> Result<()> {
        self.notify.notified().await;
        Ok(())
    }
}