fix: Load audio listener properly

This commit is contained in:
Fabio Lenherr / DashieTM 2023-11-19 02:31:41 +01:00
parent 9108ab0d74
commit b28b736697
14 changed files with 440 additions and 143 deletions

View file

@ -1,7 +1,7 @@
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::time::{Duration, SystemTime};
use adw::glib;
use adw::glib::Object;
@ -49,45 +49,47 @@ impl BluetoothBox {
}
pub fn scanForDevices(&self) {
let selfImp = self.imp();
let mut wifiEntries = selfImp.availableDevices.borrow_mut();
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Mouse,
"ina mouse",
)));
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Keyboard,
"inaboard",
)));
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Controller,
"ina controller",
)));
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Controller,
"ina best waifu",
)));
for wifiEntry in wifiEntries.iter() {
selfImp.resetBluetoothAvailableDevices.append(wifiEntry);
}
// let selfImp = self.imp();
// let mut wifiEntries = selfImp.availableDevices.borrow_mut();
// wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Mouse,
// "ina mouse",
// )));
// wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Keyboard,
// "inaboard",
// )));
// wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Controller,
// "ina controller",
// )));
// wifiEntries.push(
// ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Controller,
// "ina best waifu",
// ))
// );
//
// for wifiEntry in wifiEntries.iter() {
// selfImp.resetBluetoothAvailableDevices.append(wifiEntry);
// }
}
pub fn addConnectedDevices(&self) {
let selfImp = self.imp();
let mut wifiEntries = selfImp.connectedDevices.borrow_mut();
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Mouse,
"why are we still here?",
)));
wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
DeviceTypes::Keyboard,
"just to suffer?",
)));
for wifiEntry in wifiEntries.iter() {
selfImp.resetBluetoothConnectedDevices.append(wifiEntry);
}
// let selfImp = self.imp();
// let mut wifiEntries = selfImp.connectedDevices.borrow_mut();
// wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Mouse,
// "why are we still here?",
// )));
// wifiEntries.push(ListEntry::new(&BluetoothEntry::new(
// DeviceTypes::Keyboard,
// "just to suffer?",
// )));
//
// for wifiEntry in wifiEntries.iter() {
// selfImp.resetBluetoothConnectedDevices.append(wifiEntry);
// }
}
}
@ -104,7 +106,7 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
Duration::from_millis(1000),
);
let _: Result<(), Error> =
proxy.method_call("org.xetibo.ReSet", "StartBluetoothSearch", (5000,));
proxy.method_call("org.xetibo.ReSet", "StartBluetoothSearch", (10000,));
let device_added = BluetoothDeviceAdded::match_rule(
Some(&"org.xetibo.ReSet".into()),
Some(&Path::from("/org/xetibo/ReSet")),
@ -117,14 +119,21 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
.static_clone();
let device_added_box = bluetooth_box.clone();
let device_removed_box = bluetooth_box.clone();
let loop_box = bluetooth_box.clone();
let res = conn.add_match(device_added, move |ir: BluetoothDeviceAdded, _, _| {
let bluetooth_box = device_added_box.clone();
println!("added");
glib::spawn_future(async move {
glib::idle_add_once(move || {
//
//
let imp = bluetooth_box.imp();
let path = ir.bluetooth_device.path.clone();
let bluetooth_entry = Arc::new(BluetoothEntry::new(ir.bluetooth_device));
let entry = Arc::new(ListEntry::new(&*bluetooth_entry));
imp.availableDevices
.borrow_mut()
.insert(path, (bluetooth_entry.clone(), entry.clone()));
imp.resetBluetoothAvailableDevices.append(&*entry);
});
});
true
@ -139,8 +148,13 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
println!("removed");
glib::spawn_future(async move {
glib::idle_add_once(move || {
//
//
let imp = bluetooth_box.imp();
let map = imp.availableDevices.borrow_mut();
let list_entry = map.get(&ir.bluetooth_device);
if list_entry.is_some() {
imp.resetBluetoothAvailableDevices
.remove(&*list_entry.unwrap().0);
}
});
});
true
@ -151,10 +165,19 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
}
listeners.bluetooth_listener.store(true, Ordering::SeqCst);
let time = SystemTime::now();
loop {
let _ = conn.process(Duration::from_millis(1000));
if !listeners.bluetooth_listener.load(Ordering::SeqCst) {
if !listeners.bluetooth_listener.load(Ordering::SeqCst)
// || time.elapsed().unwrap() > Duration::from_millis(5000)
{
glib::spawn_future(async move {
glib::idle_add_once(move || {
let imp = loop_box.imp();
imp.resetBluetoothConnectedDevices.remove_all();
});
});
println!("stopping bluetooth listener");
break;
}
@ -162,4 +185,3 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
}
});
}

View file

@ -1,11 +1,14 @@
use std::cell::RefCell;
use gtk::{CompositeTemplate, glib, ListBox, Switch};
use dbus::Path;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate, ListBox, Switch};
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;
use crate::components::base::listEntry::ListEntry;
use crate::components::bluetooth::bluetoothBox;
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
use crate::components::base::listEntry::ListEntry;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
@ -21,8 +24,8 @@ pub struct BluetoothBox {
pub resetVisibility: TemplateChild<ListEntry>,
#[template_child]
pub resetBluetoothMainTab: TemplateChild<ListEntry>,
pub availableDevices: RefCell<Vec<ListEntry>>,
pub connectedDevices: RefCell<Vec<ListEntry>>,
pub availableDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
pub connectedDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
}
#[glib::object_subclass]

View file

@ -1,8 +1,16 @@
use std::time::Duration;
use crate::components::bluetooth::bluetoothEntryImpl;
use crate::components::bluetooth::bluetoothEntryImpl::DeviceTypes;
use adw::glib;
use adw::glib::Object;
use adw::subclass::prelude::ObjectSubclassIsExt;
use crate::components::bluetooth::bluetoothEntryImpl;
use crate::components::bluetooth::bluetoothEntryImpl::DeviceTypes;
use dbus::blocking::Connection;
use dbus::{Error, Path};
use glib::clone;
use gtk::prelude::WidgetExt;
use gtk::{gio, GestureClick};
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
glib::wrapper! {
pub struct BluetoothEntry(ObjectSubclass<bluetoothEntryImpl::BluetoothEntry>)
@ -11,21 +19,75 @@ glib::wrapper! {
}
impl BluetoothEntry {
pub fn new(deviceType: DeviceTypes, name: &str) -> Self {
pub fn new(device: BluetoothDevice) -> Self {
let entry: BluetoothEntry = Object::builder().build();
let entryImp = entry.imp();
entryImp.resetBluetoothLabel.get().set_text(name);
entryImp.resetBluetoothDeviceType.get().set_from_icon_name(match deviceType {
DeviceTypes::Mouse => Some("input-mouse-symbolic"),
DeviceTypes::Keyboard => Some("input-keyboard-symbolic"),
DeviceTypes::Headset => Some("output-headset-symbolic"),
DeviceTypes::Controller => Some("input-gaming-symbolic"),
DeviceTypes::None => Some("text-x-generic-symbolic") // no generic bluetooth device icon found
});
{
let mut wifiName = entryImp.deviceName.borrow_mut();
*wifiName = String::from(name);
}
entryImp.resetBluetoothLabel.get().set_text(&device.name);
entryImp.resetBluetoothAddress.get().set_text(&device.address);
// entryImp
// .resetBluetoothDeviceType
// .get()
// .set_from_icon_name(match deviceType {
// DeviceTypes::Mouse => Some("input-mouse-symbolic"),
// DeviceTypes::Keyboard => Some("input-keyboard-symbolic"),
// DeviceTypes::Headset => Some("output-headset-symbolic"),
// DeviceTypes::Controller => Some("input-gaming-symbolic"),
// DeviceTypes::None => Some("text-x-generic-symbolic"), // no generic bluetooth device icon found
// });
let gesture = GestureClick::new();
let connected = false;
// TODO implement this connected
entryImp.device.replace(device);
gesture.connect_released(clone!(@weak entryImp => move |_, _, _, _| {
let device = entryImp.device.borrow_mut();
if connected {
disconnect_from_device(device.path.clone());
} else if device.paired {
connect_to_device(device.path.clone());
} else {
pair_with_device(device.path.clone());
}
}));
entry.add_controller(gesture);
entry
}
}
}
fn connect_to_device(path: Path<'static>) {
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", "ConnectToBluetoothDevice", (path,));
});
}
fn pair_with_device(path: Path<'static>) {
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", "PairWithBluetoothDevice", (path,));
});
}
fn disconnect_from_device(path: Path<'static>) {
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", "DisconnectFromBluetoothDevice", (path,));
});
}

View file

@ -1,4 +1,5 @@
use std::cell::RefCell;
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
use gtk::{Button, CompositeTemplate, glib, Image, Label};
use gtk::subclass::prelude::*;
use crate::components::bluetooth::bluetoothEntry;
@ -21,8 +22,11 @@ pub struct BluetoothEntry {
#[template_child]
pub resetBluetoothLabel: TemplateChild<Label>,
#[template_child]
pub resetBluetoothAddress: TemplateChild<Label>,
#[template_child]
pub resetBluetoothButton: TemplateChild<Button>,
pub deviceName: RefCell<String>,
pub device: RefCell<BluetoothDevice>
}
#[glib::object_subclass]