aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--alsa/src/config.rs10
-rw-r--r--alsa/src/runnable.rs17
-rw-r--r--testconfig4
3 files changed, 20 insertions, 11 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."),
}
}
}
diff --git a/testconfig b/testconfig
index 66530d9..4c32c95 100644
--- a/testconfig
+++ b/testconfig
@@ -52,8 +52,8 @@ AfterText = ""
Plugin = "AlsaVolume"
[Element.Config]
-device = [100, 101, 102, 97, 117, 108, 116]
-element = [77, 97, 115, 116, 101, 114]
+device = "default"
+element = "Master"
abstraction = "None"
sorting = "MuteVolume"