wip: Refactor Dbus functions

This commit is contained in:
Fabio Lenherr / DashieTM 2024-04-01 19:09:19 +02:00
parent 39fd353c7e
commit 7593f2edc1
12 changed files with 115 additions and 50 deletions

View file

@ -17,23 +17,23 @@ use crate::components::{
utils::{AUDIO, BASE, DBUS_PATH},
};
use super::generic_entry::{Audio, AudioBox, AudioBoxImpl, AudioImpl};
use super::generic_entry::{Audio, AudioBox, AudioBoxImpl, AudioImpl, DBusFunction};
pub fn set_volume<T: ReSetErrorImpl + 'static>(
value: f64,
index: u32,
channels: u16,
reset_box: Arc<T>,
function: (&'static str, &'static str),
function: &'static DBusFunction,
) -> bool {
gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(), Error> =
proxy.method_call(AUDIO, function.0, (index, channels, value as u32));
proxy.method_call(AUDIO, function.function, (index, channels, value as u32));
if res.is_err() {
// TODO: also log this with LOG/ERROR
show_error::<T>(reset_box.clone(), function.1);
show_error::<T>(reset_box.clone(), function.error);
}
});
true
@ -43,15 +43,15 @@ pub fn toggle_audio_object_mute<T: ReSetErrorImpl + 'static>(
index: u32,
muted: bool,
input_box: Arc<T>,
function: (&'static str, &'static str),
function: &'static DBusFunction,
) -> bool {
gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(), Error> = proxy.method_call(AUDIO, function.0, (index, muted));
let res: Result<(), Error> = proxy.method_call(AUDIO, function.function, (index, muted));
if res.is_err() {
// TODO: also log this with LOG/ERROR
show_error::<T>(input_box.clone(), function.1);
show_error::<T>(input_box.clone(), function.error);
}
});
true
@ -60,7 +60,7 @@ pub fn toggle_audio_object_mute<T: ReSetErrorImpl + 'static>(
pub fn set_default_audio_object<T, R>(
name: Arc<String>,
input_box: Arc<T>,
function: (&'static str, &'static str),
function: &'static DBusFunction,
) -> Option<R>
where
T: ReSetErrorImpl + 'static,
@ -68,9 +68,9 @@ where
{
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(R,), Error> = proxy.method_call(AUDIO, function.0, (name.as_str(),));
let res: Result<(R,), Error> = proxy.method_call(AUDIO, function.function, (name.as_str(),));
if res.is_err() {
show_error::<T>(input_box.clone(), function.1);
show_error::<T>(input_box.clone(), function.error);
return None;
}
Some(res.unwrap().0)
@ -116,12 +116,11 @@ pub fn refresh_default_audio_object<
}
imp.volume_percentage().set_text(&percentage);
imp.volume_slider().set_value(volume as f64);
let icons = imp.icons();
if new_audio_object.muted() {
imp.audio_object_mute()
.set_icon_name("audio-volume-muted-symbolic");
imp.audio_object_mute().set_icon_name(icons.muted);
} else {
imp.audio_object_mute()
.set_icon_name("audio-volume-high-symbolic");
imp.audio_object_mute().set_icon_name(icons.active);
}
imp.default_audio_object().replace(new_audio_object);
});

View file

@ -58,8 +58,10 @@ pub trait AudioBoxImpl<OBJ, ENTRY, STREAMENTRY> {
fn model_index(&self) -> Arc<RwLock<u32>>;
fn source_map(&self) -> &AudioMap;
fn volume_time_stamp(&self) -> &RefCell<Option<SystemTime>>;
fn icons(&self) -> &AudioIcons;
}
#[allow(dead_code)]
pub trait AudioImpl<T: AudioObject> {
fn name(&self) -> &TemplateChild<ActionRow>;
fn selected_audio_object(&self) -> &TemplateChild<CheckButton>;
@ -68,9 +70,10 @@ pub trait AudioImpl<T: AudioObject> {
fn volume_percentage(&self) -> &TemplateChild<Label>;
fn audio_object(&self) -> Arc<RefCell<T>>;
fn volume_time_stamp(&self) -> &RefCell<Option<SystemTime>>;
fn set_volume_fn(&self) -> (&'static str, &'static str);
fn set_audio_object_fn(&self) -> (&'static str, &'static str);
fn set_mute_fn(&self) -> (&'static str, &'static str);
fn set_volume_fn(&self) -> &'static DBusFunction;
fn set_audio_object_fn(&self) -> &'static DBusFunction;
fn set_mute_fn(&self) -> &'static DBusFunction;
fn icons(&self) -> &AudioIcons;
}
// pub trait AudioObject {
@ -83,6 +86,17 @@ pub trait AudioImpl<T: AudioObject> {
// fn toggle_muted(&mut self);
// fn active() -> i32;
// }
//
pub struct AudioIcons {
pub muted: &'static str,
pub active: &'static str,
}
pub struct DBusFunction {
pub function: &'static str,
pub error: &'static str,
}
pub fn new_entry<
AObject: AudioObject + Arg + for<'z> Get<'z> + Send + Sync + 'static,

View file

@ -4,5 +4,6 @@ pub mod source_box;
mod source_box_handlers;
pub mod source_box_impl;
mod source_box_utils;
mod source_const;
pub mod source_entry;
pub mod source_entry_impl;

View file

@ -16,13 +16,14 @@ use re_set_lib::signals::{
SourceRemoved,
};
use crate::components::base::list_entry::ListEntry;
use crate::components::{audio::generic_audio_functions::set_volume, base::list_entry::ListEntry};
use super::{
output_stream_entry::OutputStreamEntry,
source_box::SourceBox,
source_box_utils::{get_default_source_name, refresh_default_source},
source_entry::{set_default_source, set_source_volume, toggle_source_mute, SourceEntry},
source_const::SETVOLUME,
source_entry::{set_default_source, toggle_source_mute, SourceEntry},
};
pub fn source_added_handler(source_box: Arc<SourceBox>, ir: SourceAdded) -> bool {
@ -290,7 +291,7 @@ pub fn volume_slider_handler(source_box: Arc<SourceBox>, value: f64) -> Propagat
}
*time = Some(SystemTime::now());
}
set_source_volume(value, index, channels, source_box.clone());
set_volume::<SourceBox>(value, index, channels, source_box.clone(), &SETVOLUME);
Propagation::Proceed
}

View file

@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::SystemTime;
use crate::components::audio::generic_entry::AudioBoxImpl;
use crate::components::audio::generic_entry::{AudioBoxImpl, AudioIcons};
use crate::components::audio::input::source_box;
use crate::components::base::error::ReSetError;
use crate::components::base::list_entry::ListEntry;
@ -14,6 +14,7 @@ use gtk::{prelude::*, Button, Label, Scale};
use gtk::{CheckButton, CompositeTemplate, StringList};
use super::output_stream_entry::OutputStreamEntry;
use super::source_const::ICONS;
use super::source_entry::SourceEntry;
type SourceEntryMap = Arc<RwLock<HashMap<u32, (Arc<ListEntry>, Arc<SourceEntry>, String)>>>;
@ -178,4 +179,8 @@ impl AudioBoxImpl<Source, SourceEntry, super::source_entry_impl::SourceEntry> fo
fn volume_time_stamp(&self) -> &RefCell<Option<SystemTime>> {
&self.volume_time_stamp
}
fn icons(&self) -> &AudioIcons {
&ICONS
}
}

View file

@ -0,0 +1,21 @@
use crate::components::audio::generic_entry::{AudioIcons, DBusFunction};
pub const ICONS: AudioIcons = AudioIcons {
muted: "audio-input-microphone-symbolic",
active: "microphone-disabled-symbolic",
};
pub const SETVOLUME: DBusFunction = DBusFunction {
function: "SetSourceVolume",
error: "Failed to set source volume",
};
pub const SETMUTE: DBusFunction = DBusFunction {
function: "SetSourceMute",
error: "Failed to mute source",
};
pub const SETDEFAULT: DBusFunction = DBusFunction {
function: "SetDefaultSource",
error: "Failed to set default source",
};

View file

@ -46,20 +46,6 @@ impl SourceEntry {
}
}
pub fn set_source_volume(value: f64, index: u32, channels: u16, input_box: Arc<SourceBox>) -> bool {
gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(), Error> =
proxy.method_call(AUDIO, "SetSourceVolume", (index, channels, value as u32));
if res.is_err() {
// TODO: also log this with LOG/ERROR
show_error::<SourceBox>(input_box.clone(), "Failed to set source volume");
}
});
true
}
pub fn toggle_source_mute(index: u32, muted: bool, input_box: Arc<SourceBox>) -> bool {
gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();

View file

@ -8,8 +8,9 @@ use std::time::SystemTime;
use gtk::subclass::prelude::*;
use gtk::{Button, CheckButton, CompositeTemplate, Label, Scale};
use crate::components::audio::generic_entry::AudioImpl;
use crate::components::audio::generic_entry::{AudioIcons, AudioImpl, DBusFunction};
use super::source_const::{ICONS, SETDEFAULT, SETMUTE, SETVOLUME};
use super::source_entry;
#[derive(Default, CompositeTemplate)]
@ -80,15 +81,19 @@ impl AudioImpl<Source> for SourceEntry {
&self.volume_time_stamp
}
fn set_volume_fn(&self) -> (&'static str, &'static str) {
("SetSourceVolume", "Failed to set set source volume")
fn set_volume_fn(&self) -> &'static DBusFunction {
&SETVOLUME
}
fn set_audio_object_fn(&self) -> (&'static str, &'static str) {
("SetDefaultSource", "Faield to set default source")
fn set_audio_object_fn(&self) -> &'static DBusFunction {
&SETDEFAULT
}
fn set_mute_fn(&self) -> (&'static str, &'static str) {
("SetSourceMute", "Failed to mute source")
fn set_mute_fn(&self) -> &'static DBusFunction {
&SETMUTE
}
fn icons(&self) -> &AudioIcons {
&ICONS
}
}

View file

@ -4,5 +4,6 @@ pub mod sink_box;
mod sink_box_handlers;
pub mod sink_box_impl;
mod sink_box_utils;
mod sink_const;
pub mod sink_entry;
pub mod sink_entry_impl;

View file

@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::SystemTime;
use crate::components::audio::generic_entry::AudioBoxImpl;
use crate::components::audio::generic_entry::{AudioBoxImpl, AudioIcons};
use crate::components::audio::output::input_stream_entry::InputStreamEntry;
use crate::components::base::error::ReSetError;
use crate::components::base::list_entry::ListEntry;
@ -14,6 +14,7 @@ use gtk::{prelude::*, Scale};
use gtk::{Box, Button, CheckButton, CompositeTemplate, Label, StringList};
use super::sink_box;
use super::sink_const::ICONS;
use super::sink_entry::SinkEntry;
type SinkEntryMap = Arc<RwLock<HashMap<u32, (Arc<ListEntry>, Arc<SinkEntry>, String)>>>;
@ -179,4 +180,8 @@ impl AudioBoxImpl<Sink, SinkEntry, super::sink_entry_impl::SinkEntry> for SinkBo
fn volume_time_stamp(&self) -> &RefCell<Option<SystemTime>> {
&self.volume_time_stamp
}
fn icons(&self) -> &AudioIcons {
&ICONS
}
}

View file

@ -0,0 +1,21 @@
use crate::components::audio::generic_entry::{AudioIcons, DBusFunction};
pub const ICONS: AudioIcons = AudioIcons {
muted: "audio-volume-high-symbolic",
active: "audio-volume-muted-symbolic",
};
pub const SETVOLUME: DBusFunction = DBusFunction {
function: "SetSinkVolume",
error: "Failed to set sink volume",
};
pub const SETMUTE: DBusFunction = DBusFunction {
function: "SetSinkMute",
error: "Failed to mute sink",
};
pub const SETDEFAULT: DBusFunction = DBusFunction {
function: "SetDefaultSink",
error: "Failed to set default sink",
};

View file

@ -5,11 +5,13 @@ use std::cell::RefCell;
use std::sync::Arc;
use std::time::SystemTime;
use crate::components::audio::generic_entry::AudioImpl;
use crate::components::audio::generic_entry::{AudioIcons, AudioImpl, DBusFunction};
use crate::components::audio::output::sink_entry;
use gtk::subclass::prelude::*;
use gtk::{Button, CheckButton, CompositeTemplate, Label, Scale};
use super::sink_const::{ICONS, SETDEFAULT, SETMUTE, SETVOLUME};
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/Xetibo/ReSet/resetSinkEntry.ui")]
pub struct SinkEntry {
@ -78,15 +80,19 @@ impl AudioImpl<Sink> for SinkEntry {
&self.volume_time_stamp
}
fn set_volume_fn(&self) -> (&'static str, &'static str) {
("SetSinkVolume", "Failed to set set sink volume")
fn set_volume_fn(&self) -> &'static DBusFunction {
&SETVOLUME
}
fn set_audio_object_fn(&self) -> (&'static str, &'static str) {
("SetDefaultSink", "Faield to set default sink")
fn set_audio_object_fn(&self) -> &'static DBusFunction {
&SETDEFAULT
}
fn set_mute_fn(&self) -> (&'static str, &'static str) {
("SetSinkMute", "Failed to mute sink")
fn set_mute_fn(&self) -> &'static DBusFunction {
&SETMUTE
}
fn icons(&self) -> &AudioIcons {
&ICONS
}
}