i3status_rs/blocks/sound.rs
1//! Volume level
2//!
3//! This block displays the volume level (according to PulseAudio or ALSA). Right click to toggle mute, scroll to adjust volume.
4//!
5//! Requires a PulseAudio installation or `alsa-utils` for ALSA.
6//!
7//! Note that if you are using PulseAudio commands (such as `pactl`) to control your volume, you should select the `"pulseaudio"` (or `"auto"`) driver to see volume changes that exceed 100%.
8//!
9//! # Configuration
10//!
11//! Key | Values | Default
12//! ----|--------|--------
13//! `driver` | `"auto"`, `"pulseaudio"`, `"alsa"`. | `"auto"` (Pulseaudio with ALSA fallback)
14//! `format` | A string to customise the output of this block. See below for available placeholders. | <code>\" $icon {$volume.eng(w:2) \|}\"</code>
15//! `format_alt` | If set, block will switch between `format` and `format_alt` on every click. | `None`
16//! `name` | PulseAudio device name, or the ALSA control name as found in the output of `amixer -D yourdevice scontrols`. | PulseAudio: `@DEFAULT_SINK@` / ALSA: `Master`
17//! `device` | ALSA device name, usually in the form "hw:X" or "hw:X,Y" where `X` is the card number and `Y` is the device number as found in the output of `aplay -l`. | `default`
18//! `device_kind` | PulseAudio device kind: `source` or `sink`. | `"sink"`
19//! `natural_mapping` | When using the ALSA driver, display the "mapped volume" as given by `alsamixer`/`amixer -M`, which represents the volume level more naturally with respect for the human ear. | `false`
20//! `step_width` | The percent volume level is increased/decreased for the selected audio device when scrolling. Capped automatically at 50. | `5`
21//! `max_vol` | Max volume in percent that can be set via scrolling. Note it can still be set above this value if changed by another application. | `None`
22//! `show_volume_when_muted` | Show the volume even if it is currently muted. | `false`
23//! `headphones_indicator` | Change icon when headphones are plugged in (pulseaudio only) | `false`
24//! `mappings` | Map `output_name` to a custom name. | `None`
25//! `mappings_use_regex` | Let `mappings` match using regex instead of string equality. The replacement will be regex aware and can contain capture groups. | `true`
26//! `active_port_mappings` | Map `active_port` to a custom name. The replacement will be regex aware and can contain capture groups. | `None`
27//!
28//! Placeholder | Value | Type | Unit
29//! ---------------------|-----------------------------------|--------|---------------
30//! `icon` | Icon based on volume | Icon | -
31//! `volume` | Current volume. Missing if muted. | Number | %
32//! `output_name` | PulseAudio or ALSA device name | Text | -
33//! `output_description` | PulseAudio device description, will fallback to `output_name` if no description is available and will be overwritten by mappings (mappings will still use `output_name`) | Text | -
34//! `active_port` | Active port (same as information in Ports section of `pactl list cards`). Will be absent if not supported by `driver` or if mapped to `""` in `active_port_mappings`. | Text | -
35//!
36//! Action | Default button
37//! ----------------|---------------
38//! `toggle_format` | Left
39//! `toggle_mute` | Right
40//! `volume_down` | Wheel Down
41//! `volume_up` | Wheel Up
42//!
43//! # Examples
44//!
45//! Change the default scrolling step width to 3 percent:
46//!
47//! ```toml
48//! [[block]]
49//! block = "sound"
50//! step_width = 3
51//! ```
52//!
53//! Change the output name shown:
54//!
55//! ```toml
56//! [[block]]
57//! block = "sound"
58//! format = " $icon $output_name{ $volume|} "
59//! [block.mappings]
60//! "alsa_output.usb-Harman_Multimedia_JBL_Pebbles_1.0.0-00.analog-stereo" = "Speakers"
61//! "alsa_output.pci-0000_00_1b.0.analog-stereo" = "Headset"
62//! ```
63//!
64//! Since the default value for the `device_kind` key is `sink`,
65//! to display ***microphone*** block you have to use the `source` value:
66//!
67//! ```toml
68//! [[block]]
69//! block = "sound"
70//! driver = "pulseaudio"
71//! device_kind = "source"
72//! ```
73//!
74//! Display warning in block if microphone if using the wrong port:
75//!
76//! ```toml
77//! [[block]]
78//! block = "sound"
79//! driver = "pulseaudio"
80//! device_kind = "source"
81//! format = " $icon { $volume|} {$active_port |}"
82//! [block.active_port_mappings]
83//! "analog-input-rear-mic" = "" # Mapping to an empty string makes `$active_port` absent
84//! "analog-input-front-mic" = "ERR!"
85//! ```
86//!
87//! # Icons Used
88//!
89//! - `microphone_muted` (as a progression)
90//! - `microphone` (as a progression)
91//! - `volume_muted` (as a progression)
92//! - `volume` (as a progression)
93//! - `headphones`
94
95mod alsa;
96#[cfg(feature = "pulseaudio")]
97mod pulseaudio;
98
99use super::prelude::*;
100use crate::wrappers::SerdeRegex;
101use indexmap::IndexMap;
102use regex::Regex;
103
104make_log_macro!(debug, "sound");
105
106#[derive(Deserialize, Debug, SmartDefault)]
107#[serde(deny_unknown_fields, default)]
108pub struct Config {
109 pub driver: SoundDriver,
110 pub name: Option<String>,
111 pub device: Option<String>,
112 pub device_kind: DeviceKind,
113 pub natural_mapping: bool,
114 #[default(5)]
115 pub step_width: u32,
116 pub format: FormatConfig,
117 pub format_alt: Option<FormatConfig>,
118 pub headphones_indicator: bool,
119 pub show_volume_when_muted: bool,
120 pub mappings: Option<IndexMap<String, String>>,
121 #[default(true)]
122 pub mappings_use_regex: bool,
123 pub max_vol: Option<u32>,
124 pub active_port_mappings: IndexMap<SerdeRegex, String>,
125}
126
127enum Mappings<'a> {
128 Exact(&'a IndexMap<String, String>),
129 Regex(Vec<(Regex, &'a str)>),
130}
131
132pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
133 let mut actions = api.get_actions()?;
134 api.set_default_actions(&[
135 (MouseButton::Left, None, "toggle_format"),
136 (MouseButton::Right, None, "toggle_mute"),
137 (MouseButton::WheelUp, None, "volume_up"),
138 (MouseButton::WheelDown, None, "volume_down"),
139 ])?;
140
141 let mut format = config.format.with_default(" $icon {$volume.eng(w:2)|} ")?;
142 let mut format_alt = match &config.format_alt {
143 Some(f) => Some(f.with_default("")?),
144 None => None,
145 };
146
147 let device_kind = config.device_kind;
148 let step_width = config.step_width.clamp(0, 50) as i32;
149
150 let icon = |muted: bool, device: &dyn SoundDevice| -> &'static str {
151 if config.headphones_indicator && device_kind == DeviceKind::Sink {
152 let form_factor = device.form_factor();
153 let active_port = device.active_port();
154 debug!("form_factor = {form_factor:?} active_port = {active_port:?}");
155 let headphones = match form_factor {
156 // form_factor's possible values are listed at:
157 // https://docs.rs/libpulse-binding/2.25.0/libpulse_binding/proplist/properties/constant.DEVICE_FORM_FACTOR.html
158 Some("headset") | Some("headphone") | Some("hands-free") | Some("portable") => true,
159 // Per discussion at
160 // https://github.com/greshake/i3status-rust/pull/1363#issuecomment-1046095869,
161 // some sinks may not have the form_factor property, so we should fall back to the
162 // active_port if that property is not present.
163 None => active_port.is_some_and(|p| p.to_lowercase().contains("headphones")),
164 // form_factor is present and is some non-headphone value
165 _ => false,
166 };
167 if headphones {
168 return "headphones";
169 }
170 }
171 if muted {
172 match device_kind {
173 DeviceKind::Source => "microphone_muted",
174 DeviceKind::Sink => "volume_muted",
175 }
176 } else {
177 match device_kind {
178 DeviceKind::Source => "microphone",
179 DeviceKind::Sink => "volume",
180 }
181 }
182 };
183
184 type DeviceType = Box<dyn SoundDevice>;
185 let mut device: DeviceType = match config.driver {
186 SoundDriver::Alsa => Box::new(alsa::Device::new(
187 config.name.clone().unwrap_or_else(|| "Master".into()),
188 config.device.clone().unwrap_or_else(|| "default".into()),
189 config.natural_mapping,
190 )?),
191 #[cfg(feature = "pulseaudio")]
192 SoundDriver::PulseAudio => Box::new(pulseaudio::Device::new(
193 config.device_kind,
194 config.name.clone(),
195 )?),
196 #[cfg(feature = "pulseaudio")]
197 SoundDriver::Auto => {
198 if let Ok(pulse) = pulseaudio::Device::new(config.device_kind, config.name.clone()) {
199 Box::new(pulse)
200 } else {
201 Box::new(alsa::Device::new(
202 config.name.clone().unwrap_or_else(|| "Master".into()),
203 config.device.clone().unwrap_or_else(|| "default".into()),
204 config.natural_mapping,
205 )?)
206 }
207 }
208 #[cfg(not(feature = "pulseaudio"))]
209 SoundDriver::Auto => Box::new(alsa::Device::new(
210 config.name.clone().unwrap_or_else(|| "Master".into()),
211 config.device.clone().unwrap_or_else(|| "default".into()),
212 config.natural_mapping,
213 )?),
214 };
215
216 let mappings = match &config.mappings {
217 Some(m) => {
218 if config.mappings_use_regex {
219 Some(Mappings::Regex(
220 m.iter()
221 .map(|(key, val)| {
222 Ok((
223 Regex::new(key)
224 .error("Failed to parse `{key}` in mappings as regex")?,
225 val.as_str(),
226 ))
227 })
228 .collect::<Result<_>>()?,
229 ))
230 } else {
231 Some(Mappings::Exact(m))
232 }
233 }
234 None => None,
235 };
236
237 loop {
238 device.get_info().await?;
239 let volume = device.volume();
240 let muted = device.muted();
241 let mut output_name = device.output_name();
242 let mut active_port = device.active_port();
243 match &mappings {
244 Some(Mappings::Regex(m)) => {
245 if let Some((regex, mapped)) =
246 m.iter().find(|(regex, _)| regex.is_match(&output_name))
247 {
248 output_name = regex.replace(&output_name, *mapped).into_owned();
249 }
250 }
251 Some(Mappings::Exact(m)) => {
252 if let Some(mapped) = m.get(&output_name) {
253 output_name.clone_from(mapped);
254 }
255 }
256 None => (),
257 }
258 if let Some(ap) = &active_port {
259 if let Some((regex, mapped)) = config
260 .active_port_mappings
261 .iter()
262 .find(|(regex, _)| regex.0.is_match(ap))
263 {
264 let mapped = regex.0.replace(ap, mapped);
265 if mapped.is_empty() {
266 active_port = None;
267 } else {
268 active_port = Some(mapped.into_owned());
269 }
270 }
271 }
272
273 let output_description = device
274 .output_description()
275 .unwrap_or_else(|| output_name.clone());
276
277 let mut values = map! {
278 "icon" => Value::icon_progression(icon(muted, &*device), volume as f64 / 100.0),
279 "volume" => Value::percents(volume),
280 "output_name" => Value::text(output_name),
281 "output_description" => Value::text(output_description),
282 [if let Some(ap) = active_port] "active_port" => Value::text(ap),
283 };
284
285 let mut widget = Widget::new().with_format(format.clone());
286
287 if muted {
288 widget.state = State::Warning;
289 if !config.show_volume_when_muted {
290 values.remove("volume");
291 }
292 }
293
294 widget.set_values(values);
295 api.set_widget(widget)?;
296
297 loop {
298 select! {
299 val = device.wait_for_update() => {
300 val?;
301 break;
302 }
303 _ = api.wait_for_update_request() => break,
304 Some(action) = actions.recv() => match action.as_ref() {
305 "toggle_format" => {
306 if let Some(format_alt) = &mut format_alt {
307 std::mem::swap(format_alt, &mut format);
308 break;
309 }
310 }
311 "toggle_mute" => {
312 device.toggle().await?;
313 }
314 "volume_up" => {
315 device.set_volume(step_width, config.max_vol).await?;
316 }
317 "volume_down" => {
318 device.set_volume(-step_width, config.max_vol).await?;
319 }
320 _ => (),
321 }
322 }
323 }
324 }
325}
326
327#[derive(Deserialize, Debug, SmartDefault, Clone, Copy)]
328#[serde(rename_all = "lowercase")]
329pub enum SoundDriver {
330 #[default]
331 Auto,
332 Alsa,
333 #[cfg(feature = "pulseaudio")]
334 PulseAudio,
335}
336
337#[derive(Deserialize, Debug, SmartDefault, Clone, Copy, PartialEq, Eq, Hash)]
338#[serde(rename_all = "lowercase")]
339pub enum DeviceKind {
340 #[default]
341 Sink,
342 Source,
343}
344
345#[async_trait::async_trait]
346trait SoundDevice {
347 fn volume(&self) -> u32;
348 fn muted(&self) -> bool;
349 fn output_name(&self) -> String;
350 fn output_description(&self) -> Option<String>;
351 fn active_port(&self) -> Option<String>;
352 fn form_factor(&self) -> Option<&str>;
353
354 async fn get_info(&mut self) -> Result<()>;
355 async fn set_volume(&mut self, step: i32, max_vol: Option<u32>) -> Result<()>;
356 async fn toggle(&mut self) -> Result<()>;
357 async fn wait_for_update(&mut self) -> Result<()>;
358}