mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-04-08 22:52:01 +02:00
feat: Add bluetooth changed event
This commit is contained in:
parent
ba49da2317
commit
f8c27fd498
|
@ -6,7 +6,7 @@ description = "A wip universal Linux settings application."
|
|||
|
||||
[dependencies]
|
||||
reset_daemon = "0.3.3"
|
||||
ReSet-Lib = "0.5.4"
|
||||
ReSet-Lib = "0.5.5"
|
||||
adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] }
|
||||
dbus = "0.9.7"
|
||||
gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] }
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use std::collections::HashMap;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
@ -12,15 +11,14 @@ use dbus::message::SignalArgs;
|
|||
use dbus::{Error, Path};
|
||||
use gtk::gio;
|
||||
use gtk::glib::Variant;
|
||||
use gtk::prelude::{ActionableExt, ListBoxRowExt};
|
||||
use gtk::prelude::{ActionableExt, ListBoxRowExt, WidgetExt};
|
||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||
use ReSet_Lib::signals::{BluetoothDeviceAdded, BluetoothDeviceRemoved};
|
||||
use ReSet_Lib::signals::{BluetoothDeviceAdded, BluetoothDeviceChanged, BluetoothDeviceRemoved};
|
||||
|
||||
use crate::components::base::listEntry::ListEntry;
|
||||
use crate::components::base::utils::Listeners;
|
||||
use crate::components::bluetooth::bluetoothBoxImpl;
|
||||
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
||||
// use crate::components::bluetooth::bluetoothEntryImpl::DeviceTypes;
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct BluetoothBox(ObjectSubclass<bluetoothBoxImpl::BluetoothBox>)
|
||||
|
@ -52,6 +50,12 @@ impl BluetoothBox {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for BluetoothBox {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc<BluetoothBox>) {
|
||||
gio::spawn_blocking(move || {
|
||||
let ref_box = bluetooth_box.clone();
|
||||
|
@ -63,11 +67,11 @@ pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc<BluetoothBox>) {
|
|||
for device in devices {
|
||||
let path = device.path.clone();
|
||||
let connected = device.connected;
|
||||
let bluetooth_entry = Arc::new(BluetoothEntry::new(device));
|
||||
let bluetooth_entry = Arc::new(BluetoothEntry::new(&device));
|
||||
let entry = Arc::new(ListEntry::new(&*bluetooth_entry));
|
||||
imp.availableDevices
|
||||
.borrow_mut()
|
||||
.insert(path, (bluetooth_entry.clone(), entry.clone()));
|
||||
.insert(path, (bluetooth_entry.clone(), entry.clone(), device));
|
||||
if connected {
|
||||
imp.resetBluetoothConnectedDevices.append(&*entry);
|
||||
} else {
|
||||
|
@ -108,8 +112,14 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
|||
Some(&Path::from("/org/Xetibo/ReSetDaemon")),
|
||||
)
|
||||
.static_clone();
|
||||
let device_changed = BluetoothDeviceChanged::match_rule(
|
||||
Some(&"org.Xetibo.ReSetDaemon".into()),
|
||||
Some(&Path::from("/org/Xetibo/ReSetDaemon")),
|
||||
)
|
||||
.static_clone();
|
||||
let device_added_box = bluetooth_box.clone();
|
||||
let device_removed_box = bluetooth_box.clone();
|
||||
let device_changed_box = bluetooth_box.clone();
|
||||
let loop_box = bluetooth_box.clone();
|
||||
|
||||
// TODO handle device changed
|
||||
|
@ -118,14 +128,16 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
|||
println!("added");
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
println!("{}",ir.bluetooth_device.icon);
|
||||
let imp = bluetooth_box.imp();
|
||||
let path = ir.bluetooth_device.path.clone();
|
||||
let connected = ir.bluetooth_device.connected;
|
||||
let bluetooth_entry = Arc::new(BluetoothEntry::new(ir.bluetooth_device));
|
||||
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.availableDevices.borrow_mut().insert(
|
||||
path,
|
||||
(bluetooth_entry.clone(), entry.clone(), ir.bluetooth_device),
|
||||
);
|
||||
if connected {
|
||||
imp.resetBluetoothConnectedDevices.append(&*entry);
|
||||
} else {
|
||||
|
@ -147,12 +159,43 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
|||
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().1);
|
||||
imp.resetBluetoothConnectedDevices
|
||||
.remove(&*list_entry.unwrap().1);
|
||||
if let Some(list_entry) = map.get(&ir.bluetooth_device) {
|
||||
imp.resetBluetoothAvailableDevices.remove(&*list_entry.1);
|
||||
imp.resetBluetoothConnectedDevices.remove(&*list_entry.1);
|
||||
}
|
||||
});
|
||||
});
|
||||
true
|
||||
});
|
||||
if res.is_err() {
|
||||
println!("fail on bluetooth device remove");
|
||||
return;
|
||||
}
|
||||
|
||||
let res = conn.add_match(device_changed, move |ir: BluetoothDeviceChanged, _, _| {
|
||||
let bluetooth_box = device_changed_box.clone();
|
||||
println!("removed");
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let imp = bluetooth_box.imp();
|
||||
let map = imp.availableDevices.borrow_mut();
|
||||
if let Some(list_entry) = map.get(&ir.bluetooth_device.path) {
|
||||
if list_entry.2.connected != ir.bluetooth_device.connected {
|
||||
if ir.bluetooth_device.connected {
|
||||
imp.resetBluetoothConnectedDevices.append(&*list_entry.1);
|
||||
imp.resetBluetoothAvailableDevices.remove(&*list_entry.1);
|
||||
} else {
|
||||
imp.resetBluetoothAvailableDevices.append(&*list_entry.1);
|
||||
imp.resetBluetoothConnectedDevices.remove(&*list_entry.1);
|
||||
}
|
||||
}
|
||||
if list_entry.2.paired != ir.bluetooth_device.paired {
|
||||
if ir.bluetooth_device.paired {
|
||||
list_entry.0.imp().resetBluetoothButton.set_sensitive(true);
|
||||
} else {
|
||||
list_entry.0.imp().resetBluetoothButton.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use adw::ActionRow;
|
||||
use dbus::Path;
|
||||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
|
@ -5,12 +6,15 @@ use gtk::{glib, CompositeTemplate, ListBox, Switch};
|
|||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use adw::ActionRow;
|
||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||
|
||||
use crate::components::base::listEntry::ListEntry;
|
||||
use crate::components::bluetooth::bluetoothBox;
|
||||
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
||||
|
||||
type BluetoothMap =
|
||||
RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>, BluetoothDevice)>>;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Default, CompositeTemplate)]
|
||||
#[template(resource = "/org/Xetibo/ReSet/resetBluetooth.ui")]
|
||||
|
@ -25,8 +29,8 @@ pub struct BluetoothBox {
|
|||
pub resetVisibility: TemplateChild<ActionRow>,
|
||||
#[template_child]
|
||||
pub resetBluetoothMainTab: TemplateChild<ListEntry>,
|
||||
pub availableDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
|
||||
pub connectedDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
|
||||
pub availableDevices: BluetoothMap,
|
||||
pub connectedDevices: BluetoothMap,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
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 dbus::blocking::Connection;
|
||||
use dbus::{Error, Path};
|
||||
use glib::clone;
|
||||
use gtk::prelude::{ButtonExt, WidgetExt};
|
||||
use gtk::{gio, GestureClick};
|
||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||
|
@ -21,21 +18,20 @@ glib::wrapper! {
|
|||
}
|
||||
|
||||
impl BluetoothEntry {
|
||||
pub fn new(device: BluetoothDevice) -> Self {
|
||||
pub fn new(device: &BluetoothDevice) -> Self {
|
||||
let entry: BluetoothEntry = Object::builder().build();
|
||||
let entryImp = entry.imp();
|
||||
entryImp.resetBluetoothLabel.get().set_text(&device.name);
|
||||
entryImp.resetBluetoothAddress.get().set_text(&device.alias);
|
||||
// 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
|
||||
// });
|
||||
entryImp.resetBluetoothLabel.get().set_text(&device.alias);
|
||||
entryImp.resetBluetoothAddress.get().set_text(&device.address);
|
||||
if device.icon == "" {
|
||||
entryImp
|
||||
.resetBluetoothDeviceType
|
||||
.set_icon_name(Some("dialog-question-symbolic"));
|
||||
} else {
|
||||
entryImp
|
||||
.resetBluetoothDeviceType
|
||||
.set_icon_name(Some(&device.icon));
|
||||
}
|
||||
if device.paired {
|
||||
entryImp.resetBluetoothButton.set_sensitive(true);
|
||||
} else {
|
||||
|
@ -47,17 +43,17 @@ impl BluetoothEntry {
|
|||
});
|
||||
let gesture = GestureClick::new();
|
||||
let connected = device.connected;
|
||||
entryImp.device.replace(device);
|
||||
gesture.connect_released(clone!(@weak entryImp => move |_, _, _, _| {
|
||||
let device = entryImp.device.borrow_mut();
|
||||
let paired = device.paired;
|
||||
let path = device.path.clone();
|
||||
gesture.connect_released(move |_, _, _, _| {
|
||||
if connected {
|
||||
disconnect_from_device(device.path.clone());
|
||||
} else if device.paired {
|
||||
connect_to_device(device.path.clone());
|
||||
disconnect_from_device(path.clone());
|
||||
} else if paired {
|
||||
connect_to_device(path.clone());
|
||||
} else {
|
||||
pair_with_device(device.path.clone());
|
||||
pair_with_device(path.clone());
|
||||
}
|
||||
}));
|
||||
});
|
||||
entry.add_controller(gesture);
|
||||
entry
|
||||
}
|
||||
|
|
|
@ -2,17 +2,7 @@ use crate::components::bluetooth::bluetoothEntry;
|
|||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, Button, CompositeTemplate, Image, Label};
|
||||
use std::cell::RefCell;
|
||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||
|
||||
#[derive(Default, Copy, Clone)]
|
||||
pub enum DeviceTypes {
|
||||
Mouse,
|
||||
Keyboard,
|
||||
Headset,
|
||||
Controller,
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Default, CompositeTemplate)]
|
||||
#[template(resource = "/org/Xetibo/ReSet/resetBluetoothEntry.ui")]
|
||||
|
@ -26,7 +16,6 @@ pub struct BluetoothEntry {
|
|||
#[template_child]
|
||||
pub resetBluetoothButton: TemplateChild<Button>,
|
||||
pub deviceName: RefCell<String>,
|
||||
pub device: RefCell<BluetoothDevice>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
|
|
@ -63,7 +63,8 @@ fn shutdown(_: &Application) {
|
|||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(100),
|
||||
);
|
||||
let res: Result<(), Error> = proxy.method_call("org.Xetibo.ReSetDaemon", "UnregisterClient", ("ReSet",));
|
||||
let res: Result<(), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetDaemon", "UnregisterClient", ("ReSet",));
|
||||
res
|
||||
});
|
||||
}
|
||||
|
@ -76,7 +77,8 @@ async fn daemon_check() {
|
|||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(100),
|
||||
);
|
||||
let res: Result<(), Error> = proxy.method_call("org.Xetibo.ReSetDaemon", "RegisterClient", ("ReSet",));
|
||||
let res: Result<(), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetDaemon", "RegisterClient", ("ReSet",));
|
||||
res
|
||||
});
|
||||
let res = handle.join();
|
||||
|
|
Loading…
Reference in a new issue