From af06f8da32729a5b9abd9cd1256c5f410b751ca6 Mon Sep 17 00:00:00 2001 From: Fabio Lenherr / DashieTM Date: Wed, 15 Nov 2023 23:24:35 +0100 Subject: [PATCH] fix: Use threading for every single dbus call on audio --- flatpak/cargo-sources.json | 10 +-- src/components/input/outputStreamEntry.rs | 63 +++++++++------- src/components/input/sourceEntry.rs | 63 +++++++++------- src/components/output/inputStreamEntry.rs | 92 +++++++++++++---------- src/components/output/sinkEntry.rs | 86 +++++++++++---------- 5 files changed, 176 insertions(+), 138 deletions(-) diff --git a/flatpak/cargo-sources.json b/flatpak/cargo-sources.json index ef16d58..467c1c0 100644 --- a/flatpak/cargo-sources.json +++ b/flatpak/cargo-sources.json @@ -1042,14 +1042,14 @@ { "type": "archive", "archive-type": "tar-gzip", - "url": "https://static.crates.io/crates/reset_daemon/reset_daemon-0.1.3.crate", - "sha256": "1d7b1c575b773eadd0fc8991de8abb883cfb7bce9c5e8c4f9e10b85cb142efee", - "dest": "cargo/vendor/reset_daemon-0.1.3" + "url": "https://static.crates.io/crates/reset_daemon/reset_daemon-0.1.4.crate", + "sha256": "2978104d24796489f84a2636a8797730bfeac5693a5550751b1fd638dd7bea58", + "dest": "cargo/vendor/reset_daemon-0.1.4" }, { "type": "inline", - "contents": "{\"package\": \"1d7b1c575b773eadd0fc8991de8abb883cfb7bce9c5e8c4f9e10b85cb142efee\", \"files\": {}}", - "dest": "cargo/vendor/reset_daemon-0.1.3", + "contents": "{\"package\": \"2978104d24796489f84a2636a8797730bfeac5693a5550751b1fd638dd7bea58\", \"files\": {}}", + "dest": "cargo/vendor/reset_daemon-0.1.4", "dest-filename": ".cargo-checksum.json" }, { diff --git a/src/components/input/outputStreamEntry.rs b/src/components/input/outputStreamEntry.rs index 0282adc..1f6103e 100644 --- a/src/components/input/outputStreamEntry.rs +++ b/src/components/input/outputStreamEntry.rs @@ -8,7 +8,7 @@ use dbus::blocking::Connection; use dbus::Error; use glib::subclass::types::ObjectSubclassIsExt; use glib::{clone, Cast, Propagation}; -use gtk::StringObject; +use gtk::{gio, StringObject}; use ReSet_Lib::audio::audio::OutputStream; use super::outputStreamEntryImpl; @@ -125,52 +125,63 @@ impl OutputStreamEntry { } fn set_outputstream_volume(value: f64, index: u32, channels: u16) -> bool { + gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.xetibo.ReSet", "/org/xetibo/ReSet", Duration::from_millis(1000), ); - let res: Result<(bool,), Error> = proxy.method_call( + let _: Result<(), Error> = proxy.method_call( "org.xetibo.ReSet", "SetOutputStreamVolume", (index, channels, value as u32), ); - if res.is_err() { - return false; - } - res.unwrap().0 + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } fn toggle_output_stream_mute(index: u32, muted: bool) -> bool { + gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.xetibo.ReSet", "/org/xetibo/ReSet", Duration::from_millis(1000), ); - let res: Result<(bool,), Error> = + let _: Result<(), Error> = proxy.method_call("org.xetibo.ReSet", "SetOutputStreamMute", (index, muted)); - if res.is_err() { - return false; - } - res.unwrap().0 + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } fn set_source_of_output_stream(stream: u32, source: u32) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = proxy.method_call( - "org.xetibo.ReSet", - "SetSourceOfOutputStream", - (stream, source), - ); - if res.is_err() { - return false; - } - res.unwrap().0 + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(bool,), Error> = proxy.method_call( + "org.xetibo.ReSet", + "SetSourceOfOutputStream", + (stream, source), + ); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } + +// TODO propagate error from dbus diff --git a/src/components/input/sourceEntry.rs b/src/components/input/sourceEntry.rs index fe02fc2..749a344 100644 --- a/src/components/input/sourceEntry.rs +++ b/src/components/input/sourceEntry.rs @@ -4,13 +4,13 @@ use std::time::Duration; use adw::glib; use adw::glib::Object; -use adw::prelude::{ButtonExt, RangeExt, CheckButtonExt}; +use adw::prelude::{ButtonExt, CheckButtonExt, RangeExt}; use dbus::blocking::Connection; use dbus::Error; use glib::subclass::types::ObjectSubclassIsExt; use glib::{clone, Propagation}; +use gtk::{gio, CheckButton}; use ReSet_Lib::audio::audio::Source; -use gtk::CheckButton; use super::sourceEntryImpl; @@ -80,51 +80,60 @@ impl SourceEntry { } pub fn set_source_volume(value: f64, index: u32, channels: u16) -> bool { + gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.xetibo.ReSet", "/org/xetibo/ReSet", Duration::from_millis(1000), ); - let res: Result<(bool,), Error> = proxy.method_call( + let _: Result<(), Error> = proxy.method_call( "org.xetibo.ReSet", "SetSourceVolume", (index, channels, value as u32), ); - if res.is_err() { - return false; - } - res.unwrap().0 + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } pub fn toggle_source_mute(index: u32, muted: bool) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = - proxy.method_call("org.xetibo.ReSet", "SetSourceMute", (index, muted)); - if res.is_err() { - return false; - } - res.unwrap().0 -} - -pub fn set_default_source(name: Arc) { - thread::spawn(move || { + gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.xetibo.ReSet", "/org/xetibo/ReSet", Duration::from_millis(1000), ); - let res: Result<(bool,), Error> = + let _: Result<(), Error> = + proxy.method_call("org.xetibo.ReSet", "SetSourceMute", (index, muted)); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true +} + +pub fn set_default_source(name: Arc) -> bool { + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = proxy.method_call("org.xetibo.ReSet", "SetDefaultSink", (name.as_str(),)); - if res.is_err() { - return; - } + // if res.is_err() { + // return; + // } // handle change }); + true } + +// TODO propagate error from dbus diff --git a/src/components/output/inputStreamEntry.rs b/src/components/output/inputStreamEntry.rs index 2d6103d..bfd69ae 100644 --- a/src/components/output/inputStreamEntry.rs +++ b/src/components/output/inputStreamEntry.rs @@ -8,7 +8,7 @@ use dbus::blocking::Connection; use dbus::Error; use glib::subclass::types::ObjectSubclassIsExt; use glib::{clone, Cast, Propagation}; -use gtk::StringObject; +use gtk::{gio, StringObject}; use ReSet_Lib::audio::audio::InputStream; use super::inputStreamEntryImpl; @@ -24,7 +24,6 @@ impl InputStreamEntry { pub fn new(sink_box: Arc, stream: InputStream) -> Self { let obj: Self = Object::builder().build(); // TODO use event callback for progress bar -> this is the "im speaking" indicator - // TODO handle events { let index = stream.sink_index; let box_imp = sink_box.imp(); @@ -145,49 +144,60 @@ impl InputStreamEntry { } fn set_inputstream_volume(value: f64, index: u32, channels: u16) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = proxy.method_call( - "org.xetibo.ReSet", - "SetInputStreamVolume", - (index, channels, value as u32), - ); - if res.is_err() { - return false; - } - res.unwrap().0 + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = proxy.method_call( + "org.xetibo.ReSet", + "SetInputStreamVolume", + (index, channels, value as u32), + ); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } fn toggle_input_stream_mute(index: u32, muted: bool) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = - proxy.method_call("org.xetibo.ReSet", "SetInputStreamMute", (index, muted)); - if res.is_err() { - return false; - } - res.unwrap().0 + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = + proxy.method_call("org.xetibo.ReSet", "SetInputStreamMute", (index, muted)); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } fn set_sink_of_input_stream(stream: u32, sink: u32) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = - proxy.method_call("org.xetibo.ReSet", "SetSinkOfInputStream", (stream, sink)); - if res.is_err() { - return false; - } - res.unwrap().0 + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = + proxy.method_call("org.xetibo.ReSet", "SetSinkOfInputStream", (stream, sink)); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true } + +// TODO propagate error from dbus diff --git a/src/components/output/sinkEntry.rs b/src/components/output/sinkEntry.rs index 4531367..45b42cc 100644 --- a/src/components/output/sinkEntry.rs +++ b/src/components/output/sinkEntry.rs @@ -9,7 +9,7 @@ use dbus::blocking::Connection; use dbus::Error; use glib::subclass::types::ObjectSubclassIsExt; use glib::{clone, Propagation}; -use gtk::CheckButton; +use gtk::{gio, CheckButton}; use ReSet_Lib::audio::audio::Sink; use super::sinkEntryImpl; @@ -80,51 +80,59 @@ impl SinkEntry { } pub fn set_sink_volume(value: f64, index: u32, channels: u16) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = proxy.method_call( - "org.xetibo.ReSet", - "SetSinkVolume", - (index, channels, value as u32), - ); - if res.is_err() { - return false; - } - res.unwrap().0 -} - -pub fn toggle_sink_mute(index: u32, muted: bool) -> bool { - let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - "org.xetibo.ReSet", - "/org/xetibo/ReSet", - Duration::from_millis(1000), - ); - let res: Result<(bool,), Error> = - proxy.method_call("org.xetibo.ReSet", "SetSinkMute", (index, muted)); - if res.is_err() { - return false; - } - res.unwrap().0 -} - -pub fn set_default_sink(name: Arc) { - thread::spawn(move || { + gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.xetibo.ReSet", "/org/xetibo/ReSet", Duration::from_millis(1000), ); - let res: Result<(bool,), Error> = + let _: Result<(), Error> = proxy.method_call( + "org.xetibo.ReSet", + "SetSinkVolume", + (index, channels, value as u32), + ); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true +} + +pub fn toggle_sink_mute(index: u32, muted: bool) -> bool { + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = + proxy.method_call("org.xetibo.ReSet", "SetSinkMute", (index, muted)); + // if res.is_err() { + // return false; + // } + // res.unwrap().0 + }); + true +} + +pub fn set_default_sink(name: Arc) { + gio::spawn_blocking(move || { + let conn = Connection::new_session().unwrap(); + let proxy = conn.with_proxy( + "org.xetibo.ReSet", + "/org/xetibo/ReSet", + Duration::from_millis(1000), + ); + let _: Result<(), Error> = proxy.method_call("org.xetibo.ReSet", "SetDefaultSink", (name.as_str(),)); - if res.is_err() { - return; - } + // if res.is_err() { + // return; + // } // handle change }); } + +// TODO propagate error from dbus