mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-07-01 15:57:46 +02:00
fix: Update default sink and source after setting it
This commit is contained in:
parent
d3bc2edf88
commit
049a846c36
15 changed files with 425 additions and 258 deletions
0
src/components/output/audio_box.rs
Normal file
0
src/components/output/audio_box.rs
Normal file
|
@ -4,3 +4,4 @@ pub mod sink_box;
|
|||
pub mod sink_box_impl;
|
||||
pub mod sink_entry;
|
||||
pub mod sink_entry_impl;
|
||||
pub mod audio_box;
|
||||
|
|
|
@ -65,7 +65,6 @@ impl SinkBox {
|
|||
self_imp
|
||||
.reset_cards_row
|
||||
.set_action_target_value(Some(&Variant::from("profileConfiguration")));
|
||||
self_imp.reset_cards_row.connect_action_name_notify(|_| {});
|
||||
|
||||
self_imp.reset_input_stream_button.set_activatable(true);
|
||||
self_imp
|
||||
|
@ -92,12 +91,11 @@ impl Default for SinkBox {
|
|||
|
||||
pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
||||
gio::spawn_blocking(move || {
|
||||
let output_box_ref = output_box.clone();
|
||||
let sinks = get_sinks();
|
||||
{
|
||||
let output_box_imp = output_box.imp();
|
||||
let mut map = output_box_imp.reset_sink_map.write().unwrap();
|
||||
let list = output_box_imp.reset_model_list.write().unwrap();
|
||||
let mut map = output_box_imp.reset_sink_map.write().unwrap();
|
||||
let mut model_index = output_box_imp.reset_model_index.write().unwrap();
|
||||
output_box_imp
|
||||
.reset_default_sink
|
||||
|
@ -112,8 +110,10 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
|||
populate_cards(output_box.clone());
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let output_box_ref_select = output_box.clone();
|
||||
let output_box_ref_slider = output_box.clone();
|
||||
let output_box_ref_mute = output_box.clone();
|
||||
let output_box_ref = output_box.clone();
|
||||
{
|
||||
let output_box_imp = output_box_ref.imp();
|
||||
let default_sink = output_box_imp.reset_default_sink.clone();
|
||||
|
@ -136,6 +136,7 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
|||
is_default,
|
||||
output_box_imp.reset_default_check_button.clone(),
|
||||
sink,
|
||||
output_box.clone(),
|
||||
));
|
||||
let sink_clone = sink_entry.clone();
|
||||
let entry = Arc::new(ListEntry::new(&*sink_entry));
|
||||
|
@ -147,13 +148,12 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
|||
output_box_imp.reset_sink_dropdown.set_model(Some(&*list));
|
||||
let map = output_box_imp.reset_sink_map.read().unwrap();
|
||||
let name = output_box_imp.reset_default_sink.borrow();
|
||||
let name = &name.alias;
|
||||
let index = map.get(name);
|
||||
if let Some(index) = index {
|
||||
if let Some(index) = map.get(&name.alias) {
|
||||
output_box_imp.reset_sink_dropdown.set_selected(index.1);
|
||||
}
|
||||
output_box_imp.reset_sink_dropdown.connect_selected_notify(
|
||||
clone!(@weak output_box_imp => move |dropdown| {
|
||||
let output_box_ref = output_box_ref_select.clone();
|
||||
let selected = dropdown.selected_item();
|
||||
if selected.is_none() {
|
||||
return;
|
||||
|
@ -167,8 +167,15 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
|||
if sink.is_none() {
|
||||
return;
|
||||
}
|
||||
let sink = Arc::new(sink.unwrap().2.clone());
|
||||
set_default_sink(sink);
|
||||
let new_sink_name = Arc::new(sink.unwrap().2.clone());
|
||||
gio::spawn_blocking(move || {
|
||||
let result = set_default_sink(new_sink_name);
|
||||
if result.is_none() {
|
||||
return;
|
||||
}
|
||||
let new_sink = result.unwrap();
|
||||
refresh_default_sink(new_sink, output_box_ref, false);
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -216,6 +223,43 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn refresh_default_sink(new_sink: Sink, output_box: Arc<SinkBox>, entry: bool) {
|
||||
let volume = *new_sink.volume.first().unwrap_or(&0_u32);
|
||||
let fraction = (volume as f64 / 655.36).round();
|
||||
let percentage = (fraction).to_string() + "%";
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let imp = output_box.imp();
|
||||
if !entry {
|
||||
let list = imp.reset_sink_list.read().unwrap();
|
||||
let entry = list.get(&new_sink.index);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
let entry_imp = entry.unwrap().1.imp();
|
||||
entry_imp.reset_selected_sink.set_active(true);
|
||||
} else {
|
||||
let map = imp.reset_sink_map.read().unwrap();
|
||||
let entry = map.get(&new_sink.alias);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
imp.reset_sink_dropdown.set_selected(entry.unwrap().1);
|
||||
}
|
||||
imp.reset_volume_percentage.set_text(&percentage);
|
||||
imp.reset_volume_slider.set_value(volume as f64);
|
||||
if new_sink.muted {
|
||||
imp.reset_sink_mute
|
||||
.set_icon_name("audio-volume-muted-symbolic");
|
||||
} else {
|
||||
imp.reset_sink_mute
|
||||
.set_icon_name("audio-volume-high-symbolic");
|
||||
}
|
||||
imp.reset_default_sink.replace(new_sink);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn populate_inputstreams(output_box: Arc<SinkBox>) {
|
||||
let output_box_ref = output_box.clone();
|
||||
|
||||
|
@ -283,6 +327,21 @@ fn get_sinks() -> Vec<Sink> {
|
|||
res.unwrap().0
|
||||
}
|
||||
|
||||
fn get_cards() -> Vec<Card> {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
"org.Xetibo.ReSetDaemon",
|
||||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let res: Result<(Vec<Card>,), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetAudio", "ListCards", ());
|
||||
if res.is_err() {
|
||||
return Vec::new();
|
||||
}
|
||||
res.unwrap().0
|
||||
}
|
||||
|
||||
fn get_default_sink_name() -> String {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
|
@ -313,21 +372,6 @@ fn get_default_sink() -> Sink {
|
|||
res.unwrap().0
|
||||
}
|
||||
|
||||
fn get_cards() -> Vec<Card> {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
"org.Xetibo.ReSetDaemon",
|
||||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let res: Result<(Vec<Card>,), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetAudio", "ListCards", ());
|
||||
if res.is_err() {
|
||||
return Vec::new();
|
||||
}
|
||||
res.unwrap().0
|
||||
}
|
||||
|
||||
pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Connection {
|
||||
let sink_added = SinkAdded::match_rule(
|
||||
Some(&"org.Xetibo.ReSetDaemon".into()),
|
||||
|
@ -385,6 +429,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
|
|||
is_default,
|
||||
output_box_imp.reset_default_check_button.clone(),
|
||||
ir.sink,
|
||||
output_box.clone(),
|
||||
));
|
||||
let sink_clone = sink_entry.clone();
|
||||
let entry = Arc::new(ListEntry::new(&*sink_entry));
|
||||
|
@ -416,17 +461,15 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
|
|||
let output_box = sink_box.clone();
|
||||
let output_box_imp = output_box.imp();
|
||||
let mut list = output_box_imp.reset_sink_list.write().unwrap();
|
||||
let entry = list.get(&ir.index);
|
||||
let entry = list.remove(&ir.index);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
output_box_imp.reset_sinks.remove(&*entry.unwrap().0);
|
||||
let alias = list.remove(&ir.index);
|
||||
if alias.is_none() {
|
||||
return;
|
||||
}
|
||||
output_box_imp
|
||||
.reset_sinks
|
||||
.remove(&*entry.clone().unwrap().0);
|
||||
let mut map = output_box_imp.reset_sink_map.write().unwrap();
|
||||
let entry_index = map.remove(&alias.unwrap().2);
|
||||
let entry_index = map.remove(&entry.unwrap().2);
|
||||
if let Some(entry_index) = entry_index {
|
||||
output_box_imp
|
||||
.reset_model_list
|
||||
|
@ -529,8 +572,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
|
|||
let alias: String;
|
||||
{
|
||||
let sink_list = imp.reset_sink_list.read().unwrap();
|
||||
let alias_opt = sink_list.get(&ir.stream.sink_index);
|
||||
if let Some(alias_opt) = alias_opt {
|
||||
if let Some(alias_opt) = sink_list.get(&ir.stream.sink_index) {
|
||||
alias = alias_opt.2.clone();
|
||||
} else {
|
||||
alias = String::from("");
|
||||
|
@ -566,8 +608,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
|
|||
imp.reset_volume_percentage.set_text(&percentage);
|
||||
imp.reset_volume_slider.set_value(*volume as f64);
|
||||
let map = output_box_imp.reset_sink_map.read().unwrap();
|
||||
let index = map.get(&alias);
|
||||
if let Some(index) = index {
|
||||
if let Some(index) = map.get(&alias) {
|
||||
imp.reset_sink_selection.set_selected(index.1);
|
||||
}
|
||||
});
|
||||
|
@ -586,14 +627,13 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
|
|||
let output_box = sink_box.clone();
|
||||
let output_box_imp = output_box.imp();
|
||||
let mut list = output_box_imp.reset_input_stream_list.write().unwrap();
|
||||
let entry = list.get(&ir.index);
|
||||
let entry = list.remove(&ir.index);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
output_box_imp
|
||||
.reset_input_streams
|
||||
.remove(&*entry.unwrap().0);
|
||||
list.remove(&ir.index);
|
||||
});
|
||||
});
|
||||
true
|
||||
|
|
|
@ -16,6 +16,8 @@ use super::sink_entry::SinkEntry;
|
|||
|
||||
type SinkEntryMap = Arc<RwLock<HashMap<u32, (Arc<ListEntry>, Arc<SinkEntry>, String)>>>;
|
||||
type InputStreamEntryMap = Arc<RwLock<HashMap<u32, (Arc<ListEntry>, Arc<InputStreamEntry>)>>>;
|
||||
// key is model name -> alias, first u32 is the index of the sink, the second the index in the model list and the third is
|
||||
// the detailed name
|
||||
type SinkMap = Arc<RwLock<HashMap<String, (u32, u32, String)>>>;
|
||||
|
||||
#[derive(Default, CompositeTemplate)]
|
||||
|
@ -51,8 +53,6 @@ pub struct SinkBox {
|
|||
pub reset_input_stream_list: InputStreamEntryMap,
|
||||
pub reset_model_list: Arc<RwLock<StringList>>,
|
||||
pub reset_model_index: Arc<RwLock<u32>>,
|
||||
// first u32 is the index of the sink, the second the index in the model list and the third is
|
||||
// the full name
|
||||
pub reset_sink_map: SinkMap,
|
||||
pub volume_time_stamp: RefCell<Option<SystemTime>>,
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ use glib::{clone, Propagation};
|
|||
use gtk::{gio, CheckButton};
|
||||
use re_set_lib::audio::audio_structures::Sink;
|
||||
|
||||
use super::sink_box::{refresh_default_sink, SinkBox};
|
||||
use super::sink_entry_impl;
|
||||
|
||||
glib::wrapper! {
|
||||
|
@ -23,7 +24,12 @@ unsafe impl Send for SinkEntry {}
|
|||
unsafe impl Sync for SinkEntry {}
|
||||
|
||||
impl SinkEntry {
|
||||
pub fn new(is_default: bool, check_group: Arc<CheckButton>, stream: Sink) -> Self {
|
||||
pub fn new(
|
||||
is_default: bool,
|
||||
check_group: Arc<CheckButton>,
|
||||
stream: Sink,
|
||||
output_box: Arc<SinkBox>,
|
||||
) -> Self {
|
||||
let obj: Self = Object::builder().build();
|
||||
// TODO use event callback for progress bar -> this is the "im speaking" indicator
|
||||
{
|
||||
|
@ -62,8 +68,16 @@ impl SinkEntry {
|
|||
imp.reset_selected_sink.set_active(false);
|
||||
}
|
||||
imp.reset_selected_sink.connect_toggled(move |button| {
|
||||
let output_box_ref = output_box.clone();
|
||||
if button.is_active() {
|
||||
set_default_sink(name.clone());
|
||||
let name = name.clone();
|
||||
gio::spawn_blocking(move || {
|
||||
let result = set_default_sink(name);
|
||||
if result.is_none() {
|
||||
return;
|
||||
}
|
||||
refresh_default_sink(result.unwrap(), output_box_ref, true);
|
||||
});
|
||||
}
|
||||
});
|
||||
imp.reset_sink_mute
|
||||
|
@ -124,21 +138,17 @@ pub fn toggle_sink_mute(index: u32, muted: bool) -> bool {
|
|||
true
|
||||
}
|
||||
|
||||
pub fn set_default_sink(name: Arc<String>) {
|
||||
gio::spawn_blocking(move || {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
"org.Xetibo.ReSetDaemon",
|
||||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let _: Result<(), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetAudio", "SetDefaultSink", (name.as_str(),));
|
||||
// if res.is_err() {
|
||||
// return;
|
||||
// }
|
||||
// handle change
|
||||
});
|
||||
pub fn set_default_sink(name: Arc<String>) -> Option<Sink> {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
"org.Xetibo.ReSetDaemon",
|
||||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let res: Result<(Sink,), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetAudio", "SetDefaultSink", (name.as_str(),));
|
||||
if res.is_err() {
|
||||
return None;
|
||||
}
|
||||
Some(res.unwrap().0)
|
||||
}
|
||||
|
||||
// TODO propagate error from dbus
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue