diff options
| author | Andreas Grois <andi@grois.info> | 2024-02-11 15:15:44 +0100 |
|---|---|---|
| committer | Andreas Grois <andi@grois.info> | 2024-02-11 15:15:44 +0100 |
| commit | e5ebcc0e4f7598ec26f1a67305d7f9a42bf35a59 (patch) | |
| tree | 5dc39989670e14451db1ac9e83a79fbd665c8074 /alsa | |
| parent | 41d1e47d29898e6ef0ec8ae5b9c6cab719fdf5f1 (diff) | |
Fix config for ALSA
Diffstat (limited to 'alsa')
| -rw-r--r-- | alsa/src/config.rs | 10 | ||||
| -rw-r--r-- | alsa/src/runnable.rs | 17 |
2 files changed, 18 insertions, 9 deletions
diff --git a/alsa/src/config.rs b/alsa/src/config.rs index 93bcab9..51942c2 100644 --- a/alsa/src/config.rs +++ b/alsa/src/config.rs @@ -1,5 +1,3 @@ -use std::ffi::CString; - use formatable_float::{FormatableFloatValue, KeyBackingTypeMetadata, FormattingError}; use serde::{Serialize, Deserialize}; use swaystatus_plugin::*; @@ -8,8 +6,8 @@ use crate::{runnable::AlsaVolumeRunnable, communication::{SenderForMain, make_se #[derive(Serialize, Deserialize)] pub struct AlsaVolumeConfig{ - pub(crate) device : CString, - pub(crate) element : CString, + pub(crate) device : String, + pub(crate) element : String, pub(crate) abstraction : SElemAbstraction, sorting: FieldSorting, mute: FormatableMute, @@ -93,8 +91,8 @@ impl SwayStatusModuleInstance for AlsaVolumeConfig { impl Default for AlsaVolumeConfig { fn default() -> Self { Self { - device: CString::new("default").unwrap(), - element: CString::new("Master").unwrap(), + device: "default".into(), + element: "Master".into(), abstraction : SElemAbstraction::None, volume: FormatableFloatValue::Numeric { label: " ".into(), digits: 0 }, mute: FormatableMute::Symbol { label : String::new(), mute_symbol : String::from("🔇"), unmute_symbol : String::from("🔊") }, diff --git a/alsa/src/runnable.rs b/alsa/src/runnable.rs index 4835436..5ff2e62 100644 --- a/alsa/src/runnable.rs +++ b/alsa/src/runnable.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, fmt::Display, error::Error, ffi::CStr}; +use std::{cell::RefCell, fmt::Display, error::Error, ffi::{CStr, CString}}; use formatable_float::FormattingError; use libc::{c_int, c_char, c_uint, c_void, c_long, c_ushort, c_short}; @@ -26,13 +26,22 @@ impl<'r> AlsaVolumeRunnable<'r> { //Using C callbacks in Rust is a minefield. //However, we can take the easy way out here, namely we only care about a single element, so we can just make a single data field ;-) //It still needs to be in a Cell though. + let elem_name = match CString::new(&*self.config.element){ + Ok(s) => s, + Err(_) => return Err(AlsaVolumeError::ConfigError), + }; + let device = match CString::new(&*self.config.device){ + Ok(s) => s, + Err(_) => return Err(AlsaVolumeError::ConfigError), + }; + let elem_scratch_space = RefCell::new(None); let mixer_scratch_space = MixerScratchSpace{ - elem_name: &self.config.element, + elem_name: &elem_name, elem_scratch: &elem_scratch_space, }; let mixer = open_mixer(0)?; - register_selem(mixer.handle, &self.config.device, self.config.abstraction)?; + register_selem(mixer.handle, &device, self.config.abstraction)?; unsafe { snd_mixer_set_callback(mixer.handle, Some(Self::mixer_callback)) }; unsafe { snd_mixer_set_callback_private(mixer.handle, &mixer_scratch_space as *const MixerScratchSpace as *const c_void)}; load_mixer(mixer.handle)?; @@ -201,6 +210,7 @@ enum AlsaVolumeError{ MainHungUpWithoutQuit, DeviceRemoved, EventHandlingError, + ConfigError, } impl Display for AlsaVolumeError{ @@ -214,6 +224,7 @@ impl Display for AlsaVolumeError{ AlsaVolumeError::MainHungUpWithoutQuit => write!(f, "Main thread ended communication without saying goodbye. Debug."), AlsaVolumeError::DeviceRemoved => write!(f, "Device removed. Unsupported for now."), AlsaVolumeError::EventHandlingError => write!(f, "Failure while handling mixer events. Debug."), + AlsaVolumeError::ConfigError => write!(f, "Configuration contains non-ASCI values for device or element."), } } } |
