diff --git a/Cargo.toml b/Cargo.toml index 4ea9590..a3be09a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "GPL-3.0-only" [dependencies] reset_daemon = "0.5.6" -re_set-lib = "0.8.2" +re_set-lib = "0.8.4" adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] } dbus = "0.9.7" gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] } diff --git a/src/components/base/utils.rs b/src/components/base/utils.rs index a7eaff0..8a08ab8 100644 --- a/src/components/base/utils.rs +++ b/src/components/base/utils.rs @@ -7,20 +7,13 @@ use std::{ time::Duration, }; -use dbus::{ - arg::{self, Append}, - blocking::Connection, - Error, -}; +use dbus::{blocking::Connection, Error}; use gtk::gio; -use re_set_lib::{ - audio::audio_structures::{InputStream, OutputStream, Sink, Source}, - signals::GetVal, -}; use crate::components::{ input::source_box::{start_input_box_listener, SourceBox}, - output::sink_box::{start_output_box_listener, SinkBox}, utils::{BASE, DBUS_PATH, AUDIO, WIRELESS}, + output::sink_box::{start_output_box_listener, SinkBox}, + utils::{AUDIO, BASE, DBUS_PATH, WIRELESS}, }; #[derive(Default, PartialEq, Eq)] @@ -52,13 +45,8 @@ impl Listeners { self.wifi_listener.store(false, Ordering::SeqCst); thread::spawn(|| { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let _: Result<(bool,), Error> = - proxy.method_call(WIRELESS, "StopNetworkListener", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let _: Result<(bool,), Error> = proxy.method_call(WIRELESS, "StopNetworkListener", ()); }); } @@ -71,330 +59,6 @@ impl Listeners { } } -#[derive(Debug)] -pub struct SinkAdded { - pub sink: Sink, -} - -impl arg::AppendAll for SinkAdded { - fn append(&self, i: &mut arg::IterAppend) { - self.sink.append_by_ref(i); - } -} - -impl arg::ReadAll for SinkAdded { - fn read(i: &mut arg::Iter) -> Result { - Ok(SinkAdded { sink: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SinkAdded { - const NAME: &'static str = "SinkAdded"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(Sink,)> for SinkAdded { - fn get_value(&self) -> (Sink,) { - (self.sink.clone(),) - } -} - -#[derive(Debug)] -pub struct SinkChanged { - pub sink: Sink, -} - -impl arg::AppendAll for SinkChanged { - fn append(&self, i: &mut arg::IterAppend) { - self.sink.append_by_ref(i); - } -} - -impl arg::ReadAll for SinkChanged { - fn read(i: &mut arg::Iter) -> Result { - Ok(SinkChanged { sink: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SinkChanged { - const NAME: &'static str = "SinkChanged"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(Sink,)> for SinkChanged { - fn get_value(&self) -> (Sink,) { - (self.sink.clone(),) - } -} - -#[derive(Debug)] -pub struct SinkRemoved { - pub index: u32, -} - -impl arg::AppendAll for SinkRemoved { - fn append(&self, i: &mut arg::IterAppend) { - self.index.append_by_ref(i); - } -} - -impl arg::ReadAll for SinkRemoved { - fn read(i: &mut arg::Iter) -> Result { - Ok(SinkRemoved { index: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SinkRemoved { - const NAME: &'static str = "SinkRemoved"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(u32,)> for SinkRemoved { - fn get_value(&self) -> (u32,) { - (self.index,) - } -} - -#[derive(Debug)] -pub struct InputStreamAdded { - pub stream: InputStream, -} - -impl arg::AppendAll for InputStreamAdded { - fn append(&self, i: &mut arg::IterAppend) { - self.stream.append_by_ref(i); - } -} - -impl arg::ReadAll for InputStreamAdded { - fn read(i: &mut arg::Iter) -> Result { - Ok(InputStreamAdded { stream: i.read()? }) - } -} - -impl dbus::message::SignalArgs for InputStreamAdded { - const NAME: &'static str = "InputStreamAdded"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(InputStream,)> for InputStreamAdded { - fn get_value(&self) -> (InputStream,) { - (self.stream.clone(),) - } -} - -#[derive(Debug)] -pub struct InputStreamChanged { - pub stream: InputStream, -} - -impl arg::AppendAll for InputStreamChanged { - fn append(&self, i: &mut arg::IterAppend) { - self.stream.append_by_ref(i); - } -} - -impl arg::ReadAll for InputStreamChanged { - fn read(i: &mut arg::Iter) -> Result { - Ok(InputStreamChanged { stream: i.read()? }) - } -} - -impl dbus::message::SignalArgs for InputStreamChanged { - const NAME: &'static str = "InputStreamChanged"; - const INTERFACE: &'static str = AUDIO; -} - -#[derive(Debug)] -pub struct InputStreamRemoved { - pub index: u32, -} - -impl arg::AppendAll for InputStreamRemoved { - fn append(&self, i: &mut arg::IterAppend) { - self.index.append_by_ref(i); - } -} - -impl arg::ReadAll for InputStreamRemoved { - fn read(i: &mut arg::Iter) -> Result { - Ok(InputStreamRemoved { index: i.read()? }) - } -} - -impl dbus::message::SignalArgs for InputStreamRemoved { - const NAME: &'static str = "InputStreamRemoved"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(u32,)> for InputStreamRemoved { - fn get_value(&self) -> (u32,) { - (self.index,) - } -} - -#[derive(Debug)] -pub struct SourceAdded { - pub source: Source, -} - -impl arg::AppendAll for SourceAdded { - fn append(&self, i: &mut arg::IterAppend) { - self.source.append_by_ref(i); - } -} - -impl arg::ReadAll for SourceAdded { - fn read(i: &mut arg::Iter) -> Result { - Ok(SourceAdded { source: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SourceAdded { - const NAME: &'static str = "SourceAdded"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(Source,)> for SourceAdded { - fn get_value(&self) -> (Source,) { - (self.source.clone(),) - } -} - -#[derive(Debug)] -pub struct SourceChanged { - pub source: Source, -} - -impl arg::AppendAll for SourceChanged { - fn append(&self, i: &mut arg::IterAppend) { - self.source.append_by_ref(i); - } -} - -impl arg::ReadAll for SourceChanged { - fn read(i: &mut arg::Iter) -> Result { - Ok(SourceChanged { source: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SourceChanged { - const NAME: &'static str = "SourceChanged"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(Source,)> for SourceChanged { - fn get_value(&self) -> (Source,) { - (self.source.clone(),) - } -} - -#[derive(Debug)] -pub struct SourceRemoved { - pub index: u32, -} - -impl arg::AppendAll for SourceRemoved { - fn append(&self, i: &mut arg::IterAppend) { - self.index.append_by_ref(i); - } -} - -impl arg::ReadAll for SourceRemoved { - fn read(i: &mut arg::Iter) -> Result { - Ok(SourceRemoved { index: i.read()? }) - } -} - -impl dbus::message::SignalArgs for SourceRemoved { - const NAME: &'static str = "SourceRemoved"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(u32,)> for SourceRemoved { - fn get_value(&self) -> (u32,) { - (self.index,) - } -} - -#[derive(Debug)] -pub struct OutputStreamAdded { - pub stream: OutputStream, -} - -impl arg::AppendAll for OutputStreamAdded { - fn append(&self, i: &mut arg::IterAppend) { - self.stream.append_by_ref(i); - } -} - -impl arg::ReadAll for OutputStreamAdded { - fn read(i: &mut arg::Iter) -> Result { - Ok(OutputStreamAdded { stream: i.read()? }) - } -} - -impl dbus::message::SignalArgs for OutputStreamAdded { - const NAME: &'static str = "OutputStreamAdded"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(OutputStream,)> for OutputStreamAdded { - fn get_value(&self) -> (OutputStream,) { - (self.stream.clone(),) - } -} - -#[derive(Debug)] -pub struct OutputStreamChanged { - pub stream: OutputStream, -} - -impl arg::AppendAll for OutputStreamChanged { - fn append(&self, i: &mut arg::IterAppend) { - self.stream.append_by_ref(i); - } -} - -impl arg::ReadAll for OutputStreamChanged { - fn read(i: &mut arg::Iter) -> Result { - Ok(OutputStreamChanged { stream: i.read()? }) - } -} - -impl dbus::message::SignalArgs for OutputStreamChanged { - const NAME: &'static str = "OutputStreamChanged"; - const INTERFACE: &'static str = AUDIO; -} - -#[derive(Debug)] -pub struct OutputStreamRemoved { - pub index: u32, -} - -impl arg::AppendAll for OutputStreamRemoved { - fn append(&self, i: &mut arg::IterAppend) { - self.index.append_by_ref(i); - } -} - -impl arg::ReadAll for OutputStreamRemoved { - fn read(i: &mut arg::Iter) -> Result { - Ok(OutputStreamRemoved { index: i.read()? }) - } -} - -impl dbus::message::SignalArgs for OutputStreamRemoved { - const NAME: &'static str = "OutputStreamRemoved"; - const INTERFACE: &'static str = AUDIO; -} - -impl GetVal<(u32,)> for OutputStreamRemoved { - fn get_value(&self) -> (u32,) { - (self.index,) - } -} - pub fn start_audio_listener( listeners: Arc, sink_box: Option>, @@ -430,20 +94,12 @@ pub fn start_audio_listener( } fn start_dbus_audio_listener(conn: Connection) -> Connection { - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); let _: Result<(), Error> = proxy.method_call(AUDIO, "StartAudioListener", ()); conn } fn stop_dbus_audio_listener(conn: Connection) { - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); let _: Result<(), Error> = proxy.method_call(AUDIO, "StopAudioListener", ()); } diff --git a/src/components/bluetooth/bluetooth_box.rs b/src/components/bluetooth/bluetooth_box.rs index 1f7c19e..b3e1f9c 100644 --- a/src/components/bluetooth/bluetooth_box.rs +++ b/src/components/bluetooth/bluetooth_box.rs @@ -5,7 +5,7 @@ use std::time::{Duration, SystemTime}; use adw::glib; use adw::glib::Object; -use adw::prelude::{ComboRowExt, ListModelExtManual, PreferencesGroupExt}; +use adw::prelude::{ComboRowExt, PreferencesGroupExt}; use adw::subclass::prelude::ObjectSubclassIsExt; use dbus::blocking::Connection; use dbus::message::SignalArgs; @@ -13,14 +13,14 @@ use dbus::{Error, Path}; use glib::{clone, Cast}; use gtk::glib::Variant; use gtk::prelude::{ActionableExt, ButtonExt, ListBoxRowExt, WidgetExt}; -use gtk::{gio, StringObject, Widget}; +use gtk::{gio, StringObject}; use re_set_lib::bluetooth::bluetooth_structures::{BluetoothAdapter, BluetoothDevice}; use re_set_lib::signals::{BluetoothDeviceAdded, BluetoothDeviceChanged, BluetoothDeviceRemoved}; use crate::components::base::utils::Listeners; use crate::components::bluetooth::bluetooth_box_impl; use crate::components::bluetooth::bluetooth_entry::BluetoothEntry; -use crate::components::utils::{DBUS_PATH, BASE, BLUETOOTH}; +use crate::components::utils::{BASE, BLUETOOTH, DBUS_PATH}; glib::wrapper! { pub struct BluetoothBox(ObjectSubclass) @@ -82,17 +82,24 @@ fn setup_callbacks( .connect_state_set(move |_, state| { if !state { let imp = bluetooth_box_ref.imp(); - for x in imp - .reset_bluetooth_connected_devices - .observe_children() - .iter::() - .flatten() - { - // todo test this - if let Some(item) = x.downcast_ref::() { - imp.reset_bluetooth_available_devices.remove(item); - } + let mut available_devices = imp.available_devices.borrow_mut(); + for entry in available_devices.iter() { + imp.reset_bluetooth_available_devices.remove(&**entry.1); } + available_devices.clear(); + + let mut connected_devices = imp.connected_devices.borrow_mut(); + for entry in connected_devices.iter() { + imp.reset_bluetooth_connected_devices.remove(&**entry.1); + } + connected_devices.clear(); + + imp.reset_bluetooth_pairable_switch.set_active(false); + imp.reset_bluetooth_pairable_switch.set_sensitive(false); + imp.reset_bluetooth_discoverable_switch.set_active(false); + imp.reset_bluetooth_discoverable_switch.set_sensitive(false); + imp.reset_bluetooth_refresh_button.set_sensitive(false); + listeners_ref .bluetooth_listener .store(false, Ordering::SeqCst); @@ -101,12 +108,26 @@ fn setup_callbacks( false, ); } else { - let imp = bluetooth_box_ref.imp(); - set_adapter_enabled( - imp.reset_current_bluetooth_adapter.borrow().path.clone(), - true, - ); - start_bluetooth_listener(listeners_ref.clone(), bluetooth_box_ref.clone()); + let restart_ref = bluetooth_box_ref.clone(); + let restart_listener_ref = listeners_ref.clone(); + { + let imp = bluetooth_box_ref.imp(); + imp.reset_bluetooth_discoverable_switch.set_sensitive(true); + imp.reset_bluetooth_pairable_switch.set_sensitive(true); + } + gio::spawn_blocking(move || { + if set_adapter_enabled( + restart_ref + .imp() + .reset_current_bluetooth_adapter + .borrow() + .path + .clone(), + true, + ) { + start_bluetooth_listener(restart_listener_ref.clone(), restart_ref.clone()); + } + }); } glib::Propagation::Proceed }); @@ -151,14 +172,8 @@ pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc) { let current_adapter = imp.reset_current_bluetooth_adapter.borrow(); imp.reset_bluetooth_switch .set_state(current_adapter.powered); - imp.reset_bluetooth_switch - .set_active(current_adapter.powered); imp.reset_bluetooth_discoverable_switch .set_active(current_adapter.discoverable); - imp.reset_bluetooth_discoverable_switch - .set_active(current_adapter.discoverable); - imp.reset_bluetooth_pairable_switch - .set_active(current_adapter.pairable); imp.reset_bluetooth_pairable_switch .set_active(current_adapter.pairable); } @@ -207,28 +222,17 @@ pub fn start_bluetooth_listener(listeners: Arc, bluetooth_box: Arc = - proxy.method_call(BLUETOOTH, "StartBluetoothListener", ()); - let device_added = BluetoothDeviceAdded::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let device_removed = BluetoothDeviceRemoved::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let device_changed = BluetoothDeviceChanged::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let _: Result<(), Error> = proxy.method_call(BLUETOOTH, "StartBluetoothListener", ()); + let device_added = + BluetoothDeviceAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); + let device_removed = + BluetoothDeviceRemoved::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); + let device_changed = + BluetoothDeviceChanged::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); let device_added_box = bluetooth_box.clone(); let device_removed_box = bluetooth_box.clone(); let device_changed_box = bluetooth_box.clone(); @@ -347,8 +351,7 @@ pub fn start_bluetooth_listener(listeners: Arc, bluetooth_box: Arc = - proxy.method_call(BLUETOOTH, "StopBluetoothScan", ()); + let _: Result<(), Error> = proxy.method_call(BLUETOOTH, "StopBluetoothScan", ()); } if !listener_active && listeners.bluetooth_scan_requested.load(Ordering::SeqCst) { listeners @@ -366,16 +369,9 @@ pub fn start_bluetooth_listener(listeners: Arc, bluetooth_box: Arc Vec { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(Vec,), Error> = proxy.method_call( - BLUETOOTH, - "GetConnectedBluetoothDevices", - (), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(Vec,), Error> = + proxy.method_call(BLUETOOTH, "GetConnectedBluetoothDevices", ()); if res.is_err() { return Vec::new(); } @@ -384,11 +380,7 @@ fn get_connected_devices() -> Vec { fn get_bluetooth_adapters() -> Vec { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); let res: Result<(Vec,), Error> = proxy.method_call(BLUETOOTH, "GetBluetoothAdapters", ()); if res.is_err() { @@ -399,22 +391,14 @@ fn get_bluetooth_adapters() -> Vec { fn set_bluetooth_adapter(path: Path<'static>) { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let _: Result<(Vec,), Error> = + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let _: Result<(Path<'static>,), Error> = proxy.method_call(BLUETOOTH, "SetBluetoothAdapter", (path,)); } fn set_bluetooth_adapter_visibility(path: Path<'static>, visible: bool) { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); let _: Result<(bool,), Error> = proxy.method_call( BLUETOOTH, "SetBluetoothAdapterDiscoverability", @@ -424,28 +408,18 @@ fn set_bluetooth_adapter_visibility(path: Path<'static>, visible: bool) { fn set_bluetooth_adapter_pairability(path: Path<'static>, visible: bool) { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let _: Result<(bool,), Error> = proxy.method_call( - BLUETOOTH, - "SetBluetoothAdapterPairability", - (path, visible), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let _: Result<(bool,), Error> = + proxy.method_call(BLUETOOTH, "SetBluetoothAdapterPairability", (path, visible)); } -fn set_adapter_enabled(path: Path<'static>, enabled: bool) { +fn set_adapter_enabled(path: Path<'static>, enabled: bool) -> bool { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let _: Result<(Vec,), Error> = proxy.method_call( - BLUETOOTH, - "SetBluetoothAdapterEnabled", - (path, enabled), - ); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let result: Result<(bool,), Error> = + proxy.method_call(BLUETOOTH, "SetBluetoothAdapterEnabled", (path, enabled)); + if result.is_err() { + return false; + } + result.unwrap().0 } diff --git a/src/components/input/source_box.rs b/src/components/input/source_box.rs index 2fff091..11f6023 100644 --- a/src/components/input/source_box.rs +++ b/src/components/input/source_box.rs @@ -1,5 +1,6 @@ use adw::prelude::PreferencesRowExt; use re_set_lib::audio::audio_structures::{Card, OutputStream, Source}; +use re_set_lib::signals::{OutputStreamChanged, OutputStreamRemoved, OutputStreamAdded, SourceChanged, SourceRemoved, SourceAdded}; use std::sync::Arc; use std::time::{Duration, SystemTime}; @@ -18,10 +19,6 @@ use gtk::{gio, StringObject}; use crate::components::base::card_entry::CardEntry; use crate::components::base::list_entry::ListEntry; -use crate::components::base::utils::{ - OutputStreamAdded, OutputStreamChanged, OutputStreamRemoved, SourceAdded, SourceChanged, - SourceRemoved, -}; use crate::components::input::source_box_impl; use crate::components::input::source_entry::set_source_volume; use crate::components::utils::{create_dropdown_label_factory, set_combo_row_ellipsis, BASE, DBUS_PATH, AUDIO}; diff --git a/src/components/output/sink_box.rs b/src/components/output/sink_box.rs index 40a1ec3..5ccfa4b 100644 --- a/src/components/output/sink_box.rs +++ b/src/components/output/sink_box.rs @@ -3,6 +3,12 @@ use adw::prelude::PreferencesRowExt; use re_set_lib::audio::audio_structures::Card; use re_set_lib::audio::audio_structures::InputStream; use re_set_lib::audio::audio_structures::Sink; +use re_set_lib::signals::InputStreamAdded; +use re_set_lib::signals::InputStreamChanged; +use re_set_lib::signals::InputStreamRemoved; +use re_set_lib::signals::SinkAdded; +use re_set_lib::signals::SinkChanged; +use re_set_lib::signals::SinkRemoved; use std::sync::Arc; use std::time::{Duration, SystemTime}; @@ -19,9 +25,6 @@ use gtk::{gio, StringObject}; use crate::components::base::card_entry::CardEntry; use crate::components::base::list_entry::ListEntry; -use crate::components::base::utils::{ - InputStreamAdded, InputStreamChanged, InputStreamRemoved, SinkAdded, SinkChanged, SinkRemoved, -}; use crate::components::output::sink_entry::set_sink_volume; use crate::components::utils::AUDIO; use crate::components::utils::BASE; @@ -312,13 +315,8 @@ pub fn populate_cards(output_box: Arc) { fn get_input_streams() -> Vec { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(Vec,), Error> = - proxy.method_call(AUDIO, "ListInputStreams", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(Vec,), Error> = proxy.method_call(AUDIO, "ListInputStreams", ()); if res.is_err() { return Vec::new(); } @@ -327,13 +325,8 @@ fn get_input_streams() -> Vec { fn get_sinks() -> Vec { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(Vec,), Error> = - proxy.method_call(AUDIO, "ListSinks", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(Vec,), Error> = proxy.method_call(AUDIO, "ListSinks", ()); if res.is_err() { return Vec::new(); } @@ -342,13 +335,8 @@ fn get_sinks() -> Vec { fn get_cards() -> Vec { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(Vec,), Error> = - proxy.method_call(AUDIO, "ListCards", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(Vec,), Error> = proxy.method_call(AUDIO, "ListCards", ()); if res.is_err() { return Vec::new(); } @@ -357,13 +345,8 @@ fn get_cards() -> Vec { fn get_default_sink_name() -> String { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(String,), Error> = - proxy.method_call(AUDIO, "GetDefaultSinkName", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(String,), Error> = proxy.method_call(AUDIO, "GetDefaultSinkName", ()); if res.is_err() { return String::from(""); } @@ -372,13 +355,8 @@ fn get_default_sink_name() -> String { fn get_default_sink() -> Sink { let conn = Connection::new_session().unwrap(); - let proxy = conn.with_proxy( - BASE, - DBUS_PATH, - Duration::from_millis(1000), - ); - let res: Result<(Sink,), Error> = - proxy.method_call(AUDIO, "GetDefaultSink", ()); + let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000)); + let res: Result<(Sink,), Error> = proxy.method_call(AUDIO, "GetDefaultSink", ()); if res.is_err() { return Sink::default(); } @@ -386,36 +364,21 @@ fn get_default_sink() -> Sink { } pub fn start_output_box_listener(conn: Connection, sink_box: Arc) -> Connection { - let sink_added = SinkAdded::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let sink_removed = SinkRemoved::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let sink_changed = SinkChanged::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let input_stream_added = InputStreamAdded::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let input_stream_removed = InputStreamRemoved::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); - let input_stream_changed = InputStreamChanged::match_rule( - Some(&BASE.into()), - Some(&Path::from(DBUS_PATH)), - ) - .static_clone(); + let sink_added = + SinkAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))).static_clone(); + let sink_removed = + SinkRemoved::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))).static_clone(); + let sink_changed = + SinkChanged::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))).static_clone(); + let input_stream_added = + InputStreamAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); + let input_stream_removed = + InputStreamRemoved::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); + let input_stream_changed = + InputStreamChanged::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); let sink_added_box = sink_box.clone(); let sink_removed_box = sink_box.clone(); diff --git a/src/components/wifi/wifi_box.rs b/src/components/wifi/wifi_box.rs index f167b78..137a021 100644 --- a/src/components/wifi/wifi_box.rs +++ b/src/components/wifi/wifi_box.rs @@ -19,7 +19,7 @@ use gtk::glib::Variant; use gtk::prelude::{ActionableExt, WidgetExt}; use gtk::{gio, StringList, StringObject}; use re_set_lib::network::network_structures::{AccessPoint, WifiDevice, WifiStrength}; -use re_set_lib::signals::{AccessPointAdded, WifiDeviceChanged}; +use re_set_lib::signals::{AccessPointAdded, WifiDeviceChanged, WifiDeviceReset}; use re_set_lib::signals::{AccessPointChanged, AccessPointRemoved}; use crate::components::wifi::wifi_box_impl; @@ -265,6 +265,7 @@ pub fn start_event_listener(listeners: Arc, wifi_box: Arc) { let removed_ref = wifi_box.clone(); let changed_ref = wifi_box.clone(); let wifi_changed_ref = wifi_box.clone(); + let wifi_reset_ref = wifi_box.clone(); let access_point_added = AccessPointAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) .static_clone(); @@ -277,6 +278,9 @@ pub fn start_event_listener(listeners: Arc, wifi_box: Arc) { let device_changed = WifiDeviceChanged::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) .static_clone(); + let devices_reset = + WifiDeviceReset::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH))) + .static_clone(); let res = conn.add_match(access_point_added, move |ir: AccessPointAdded, _, _| { let wifi_box = added_ref.clone(); glib::spawn_future(async move { @@ -414,6 +418,44 @@ pub fn start_event_listener(listeners: Arc, wifi_box: Arc) { println!("fail on wifi device change event"); return; } + let res = conn.add_match(devices_reset, move |ir: WifiDeviceReset, _, _| { + if ir.devices.is_empty() { + return true; + } + { + let imp = wifi_reset_ref.imp(); + let list = imp.reset_model_list.write().unwrap(); + let mut model_index = imp.reset_model_index.write().unwrap(); + let mut map = imp.reset_wifi_devices.write().unwrap(); + imp.reset_current_wifi_device + .replace(ir.devices.last().unwrap().clone()); + for (index, device) in ir.devices.into_iter().enumerate() { + list.append(&device.name); + map.insert(device.name.clone(), (device, index as u32)); + *model_index += 1; + } + } + let wifi_box = wifi_reset_ref.clone(); + glib::spawn_future(async move { + glib::idle_add_once(move || { + let imp = wifi_box.imp(); + let list = imp.reset_model_list.read().unwrap(); + imp.reset_wifi_device.set_model(Some(&*list)); + let map = imp.reset_wifi_devices.read().unwrap(); + { + let device = imp.reset_current_wifi_device.borrow(); + if let Some(index) = map.get(&device.name) { + imp.reset_wifi_device.set_selected(index.1); + } + } + }); + }); + true + }); + if res.is_err() { + println!("fail on wifi device change event"); + return; + } loop { let _ = conn.process(Duration::from_millis(1000)); diff --git a/src/components/wifi/wifi_box_impl.rs b/src/components/wifi/wifi_box_impl.rs index c2657e8..9e0f93a 100644 --- a/src/components/wifi/wifi_box_impl.rs +++ b/src/components/wifi/wifi_box_impl.rs @@ -33,7 +33,6 @@ pub struct WifiBox { pub reset_available_networks: TemplateChild, pub wifi_entries: Arc, Arc>>>, pub wifi_entries_path: Arc, Arc>>>, - pub saved_wifi_entries: Arc>>, pub reset_wifi_devices: Arc>>, pub reset_current_wifi_device: Arc>, pub reset_model_list: Arc>,