Skip to content

Commit

Permalink
Expand types of privacy monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Jan 12, 2024
1 parent 17df860 commit fcfb08e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 20 deletions.
86 changes: 71 additions & 15 deletions src/blocks/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! Key | Values | Default|
//! -----------|--------|--------|
//! `driver` | The configuration of a driver (see below). | **Required**
//! `format` | Format string. | <code>"{ $icon \|}"</code> |
//! `format_alt` | Format string. | <code>"{ $icon $info \|}"</code> |
//! `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> |
//!
//! # vl4 Options
//!
Expand All @@ -18,10 +18,20 @@
//!
//! # Available Format Keys
//!
//! Placeholder | Value | Type | Unit
//! --------------|------------------------------------------------|----------|-----
//! `icon` | A static icon | Icon | -
//! `info` | The mapping of which source are being consumed | Text | -
//! 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
//!
Expand All @@ -39,7 +49,13 @@
//! ```
//!
//! # Icons Used
//! - `microphone`
//! - `volume`
//! - `xrandr`
//! - `webcam`
//! - `unknown`
use std::collections::HashSet;

use super::prelude::*;

Expand All @@ -63,22 +79,32 @@ pub enum PrivacyDriver {
V4l(v4l::Config),
}

// name -> [reader]
type PrivacyInfo = HashMap<String, Vec<String>>;
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
enum Type {
Audio,
AudioSink,
Video,
Webcam,
Unknown,
}

// {type: {name: {reader}}
type PrivacyInfo = HashMap<Type, HashMap<String, HashSet<String>>>;

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

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 |}")?;
let mut format_alt = config.format_alt.with_default("{ $icon $info |}")?;
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 device: Box<dyn PrivacyMonitor + Send + Sync> = match &config.driver {
PrivacyDriver::V4l(driver_config) => {
Expand All @@ -90,14 +116,44 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new().with_format(format.clone());

let info = device.get_info().await?;

if !info.is_empty() {
widget.state = State::Warning;
widget.set_values(map! {
"icon" => Value::icon(device.get_icon()),
"info" => Value::text(format!("{:?}", info)),
}

let mut values = Values::new();

if let Some(info_by_type) = info.get(&Type::Audio) {
values.extend(map! {
"icon_audio" => Value::icon("microphone"),
"info_audio" => Value::text(format!("{:?}", info_by_type))
});
}
if let Some(info_by_type) = info.get(&Type::AudioSink) {
values.extend(map! {
"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) {
values.extend(map! {
"icon_video" => Value::icon("xrandr"),
"info_video" => Value::text(format!("{:?}", info_by_type))
});
}
if let Some(info_by_type) = info.get(&Type::Webcam) {
values.extend(map! {
"icon_webcam" => Value::icon("webcam"),
"info_webcam" => Value::text(format!("{:?}", info_by_type))
});
}
if let Some(info_by_type) = info.get(&Type::Unknown) {
values.extend(map! {
"icon_unknown" => Value::icon("unknown"),
"info_unknown" => Value::text(format!("{:?}", info_by_type))
});
}

widget.set_values(values);

api.set_widget(widget)?;

Expand Down
8 changes: 3 additions & 5 deletions src/blocks/privacy/v4l.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ impl<'a> PrivacyMonitor for Monitor<'a> {
}
debug!("{} {:?}", reader, link_path);
mapping
.entry(Type::Webcam)
.or_default()
.entry(link_path.to_string_lossy().to_string())
.or_default()
.push(reader);
.insert(reader);
debug!("{:?}", mapping);
}
}
Expand All @@ -151,8 +153,4 @@ impl<'a> PrivacyMonitor for Monitor<'a> {
}
Ok(())
}

fn get_icon(&self) -> &'static str {
"webcam"
}
}

0 comments on commit fcfb08e

Please sign in to comment.