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
//! Privacy Monitor
//!
//! # Configuration
//!
//! Key        | Values | Default|
//! -----------|--------|--------|
//! `driver` | The configuration of a driver (see below). | **Required**
//! `format`   | Format string. | <code>\"{ $icon_audio \|}{ $icon_audio_sink \|}{ $icon_video \|}{ $icon_webcam \|}{ $icon_unknown \|}\"</code> |
//! `format_alt`   | Format string. | <code>\"{ $icon_audio $info_audio \|}{ $icon_audio_sink $info_audio_sink \|}{ $icon_video $info_video \|}{ $icon_webcam $info_webcam \|}{ $icon_unknown $info_unknown \|}\"</code> |
//!
//! # pipewire Options (requires the pipewire feature to be enabled)
//!
//! Key | Values | Required | Default
//! ----|--------|----------|--------
//! `name` | `pipewire` | Yes | None
//! `exclude_output` | An output node to ignore, example: `["HD Pro Webcam C920"]` | No | `[]`
//! `exclude_input` | An input node to ignore, example: `["openrgb"]` | No | `[]`
//! `display`   | Which node field should be used as a display name, options: `name`, `description`, `nickname` | No | `name`
//!
//! # vl4 Options
//!
//! Key | Values | Required | Default
//! ----|--------|----------|--------
//! `name` | `vl4` | Yes | None
//! `exclude_device` | A device to ignore, example: `["/dev/video5"]` | No | `[]`
//! `exclude_consumer` | Processes to ignore | No | `["pipewire", "wireplumber"]`
//!
//! # Available Format Keys
//!
//! Placeholder                                      | Value                                          | Type     | Unit
//! -------------------------------------------------|------------------------------------------------|----------|-----
//! `icon_{audio,audio_sink,video,webcam,unknown}`   | A static icon                                  | Icon     | -
//! `info_{audio,audio_sink,video,webcam,unknown}`   | The mapping of which source are being consumed | Text     | -
//!
//! You can use the suffixes noted above to get the following:
//!
//! Suffix       | Description
//! -------------|------------
//! `audio`      | Captured audio (ex. Mic)
//! `audio_sink` | Audio captured from a sink (ex. openrgb)
//! `video`      | Video capture (ex. screen capture)
//! `webcam`     | Webcam capture
//! `unknown`    | Anything else
//!
//! # Available Actions
//!
//! Action          | Description                               | Default button
//! ----------------|-------------------------------------------|---------------
//! `toggle_format` | Toggles between `format` and `format_alt` | Left
//!
//! # Example
//!
//! ```toml
//! [[block]]
//! block = "privacy"
//! [[block.driver]]
//! name = "v4l"
//! [[block.driver]]
//! name = "pipewire"
//! exclude_input = ["openrgb"]
//! display = "nickname"
//! ```
//!
//! # Icons Used
//! - `microphone`
//! - `volume`
//! - `xrandr`
//! - `webcam`
//! - `unknown`

use futures::future::{select_all, try_join_all};

use super::prelude::*;

make_log_macro!(debug, "privacy");

#[cfg(feature = "pipewire")]
mod pipewire;
mod v4l;

#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Config {
    #[serde(default)]
    pub format: FormatConfig,
    #[serde(default)]
    pub format_alt: FormatConfig,
    pub driver: Vec<PrivacyDriver>,
}

#[cfg(feature = "pipewire")]
#[derive(Deserialize, Debug)]
#[serde(tag = "name", rename_all = "snake_case")]
pub enum PrivacyDriver {
    Pipewire(pipewire::Config),
    V4l(v4l::Config),
}

#[cfg(not(feature = "pipewire"))]
#[derive(Deserialize, Debug)]
#[serde(tag = "name", rename_all = "snake_case")]
pub enum PrivacyDriver {
    V4l(v4l::Config),
}

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
enum Type {
    Audio,
    AudioSink,
    Video,
    Webcam,
    Unknown,
}

// {type: {source: {destination: count}}
type PrivacyInfo = HashMap<Type, PrivacyInfoInner>;

type PrivacyInfoInnerType = HashMap<String, HashMap<String, usize>>;
#[derive(Default, Debug)]
struct PrivacyInfoInner(PrivacyInfoInnerType);

impl std::ops::Deref for PrivacyInfoInner {
    type Target = PrivacyInfoInnerType;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::ops::DerefMut for PrivacyInfoInner {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl std::fmt::Display for PrivacyInfoInner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{{ {} }}",
            itertools::join(
                self.iter().map(|(source, destinations)| {
                    format!(
                        "{} => [ {} ]",
                        source,
                        itertools::join(
                            destinations
                                .iter()
                                .map(|(destination, count)| if count == &1 {
                                    destination.into()
                                } else {
                                    format!("{} (x{})", destination, count)
                                }),
                            ", "
                        )
                    )
                }),
                ", ",
            )
        )
    }
}

#[async_trait]
trait PrivacyMonitor {
    async fn get_info(&mut self) -> Result<PrivacyInfo>;
    async fn wait_for_change(&mut self) -> Result<()>;
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
    let mut actions = api.get_actions()?;
    api.set_default_actions(&[(MouseButton::Left, None, "toggle_format")])?;

    let mut format = config.format.with_default(
        "{ $icon_audio |}{ $icon_audio_sink |}{ $icon_video |}{ $icon_webcam |}{ $icon_unknown |}",
    )?;
    let mut format_alt = config.format_alt.with_default("{ $icon_audio $info_audio |}{ $icon_audio_sink $info_audio_sink |}{ $icon_video $info_video |}{ $icon_webcam $info_webcam |}{ $icon_unknown $info_unknown |}")?;

    let mut drivers: Vec<Box<dyn PrivacyMonitor + Send + Sync>> = Vec::new();

    for driver in &config.driver {
        drivers.push(match driver {
            #[cfg(feature = "pipewire")]
            PrivacyDriver::Pipewire(driver_config) => {
                Box::new(pipewire::Monitor::new(driver_config).await?)
            }
            PrivacyDriver::V4l(driver_config) => {
                Box::new(v4l::Monitor::new(driver_config, api.error_interval).await?)
            }
        });
    }

    loop {
        let mut widget = Widget::new().with_format(format.clone());

        let mut info = PrivacyInfo::default();
        //Merge driver info
        for driver_info in try_join_all(drivers.iter_mut().map(|driver| driver.get_info())).await? {
            for (type_, mapping) in driver_info {
                let existing_mapping = info.entry(type_).or_default();
                for (source, dest) in mapping.0 {
                    existing_mapping.entry(source).or_default().extend(dest);
                }
            }
        }
        if !info.is_empty() {
            widget.state = State::Warning;
        }

        let mut values = Values::new();

        if let Some(info_by_type) = info.get(&Type::Audio) {
            map! { @extend values
                "icon_audio" => Value::icon("microphone"),
                "info_audio" => Value::text(format!("{}", info_by_type))
            }
        }
        if let Some(info_by_type) = info.get(&Type::AudioSink) {
            map! { @extend values
                "icon_audio_sink" => Value::icon("volume"),
                "info_audio_sink" => Value::text(format!("{}", info_by_type))
            }
        }
        if let Some(info_by_type) = info.get(&Type::Video) {
            map! { @extend values
                "icon_video" => Value::icon("xrandr"),
                "info_video" => Value::text(format!("{}", info_by_type))
            }
        }
        if let Some(info_by_type) = info.get(&Type::Webcam) {
            map! { @extend values
                "icon_webcam" => Value::icon("webcam"),
                "info_webcam" => Value::text(format!("{}", info_by_type))
            }
        }
        if let Some(info_by_type) = info.get(&Type::Unknown) {
            map! { @extend values
                "icon_unknown" => Value::icon("unknown"),
                "info_unknown" => Value::text(format!("{}", info_by_type))
            }
        }

        widget.set_values(values);

        api.set_widget(widget)?;

        select! {
            _ = api.wait_for_update_request() => (),
            _ = select_all(drivers.iter_mut().map(|driver| driver.wait_for_change())) =>(),
            Some(action) = actions.recv() => match action.as_ref() {
                "toggle_format" => {
                    std::mem::swap(&mut format_alt, &mut format);
                }
                _ => (),
            }
        }
    }
}