mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-04-17 10:18:32 +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]
|
[dependencies]
|
||||||
reset_daemon = "0.3.3"
|
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"] }
|
adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] }
|
||||||
dbus = "0.9.7"
|
dbus = "0.9.7"
|
||||||
gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] }
|
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::atomic::Ordering;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
@ -12,15 +11,14 @@ use dbus::message::SignalArgs;
|
||||||
use dbus::{Error, Path};
|
use dbus::{Error, Path};
|
||||||
use gtk::gio;
|
use gtk::gio;
|
||||||
use gtk::glib::Variant;
|
use gtk::glib::Variant;
|
||||||
use gtk::prelude::{ActionableExt, ListBoxRowExt};
|
use gtk::prelude::{ActionableExt, ListBoxRowExt, WidgetExt};
|
||||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
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::listEntry::ListEntry;
|
||||||
use crate::components::base::utils::Listeners;
|
use crate::components::base::utils::Listeners;
|
||||||
use crate::components::bluetooth::bluetoothBoxImpl;
|
use crate::components::bluetooth::bluetoothBoxImpl;
|
||||||
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
||||||
// use crate::components::bluetooth::bluetoothEntryImpl::DeviceTypes;
|
|
||||||
|
|
||||||
glib::wrapper! {
|
glib::wrapper! {
|
||||||
pub struct BluetoothBox(ObjectSubclass<bluetoothBoxImpl::BluetoothBox>)
|
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>) {
|
pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc<BluetoothBox>) {
|
||||||
gio::spawn_blocking(move || {
|
gio::spawn_blocking(move || {
|
||||||
let ref_box = bluetooth_box.clone();
|
let ref_box = bluetooth_box.clone();
|
||||||
|
@ -63,11 +67,11 @@ pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc<BluetoothBox>) {
|
||||||
for device in devices {
|
for device in devices {
|
||||||
let path = device.path.clone();
|
let path = device.path.clone();
|
||||||
let connected = device.connected;
|
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));
|
let entry = Arc::new(ListEntry::new(&*bluetooth_entry));
|
||||||
imp.availableDevices
|
imp.availableDevices
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.insert(path, (bluetooth_entry.clone(), entry.clone()));
|
.insert(path, (bluetooth_entry.clone(), entry.clone(), device));
|
||||||
if connected {
|
if connected {
|
||||||
imp.resetBluetoothConnectedDevices.append(&*entry);
|
imp.resetBluetoothConnectedDevices.append(&*entry);
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,8 +112,14 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
||||||
Some(&Path::from("/org/Xetibo/ReSetDaemon")),
|
Some(&Path::from("/org/Xetibo/ReSetDaemon")),
|
||||||
)
|
)
|
||||||
.static_clone();
|
.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_added_box = bluetooth_box.clone();
|
||||||
let device_removed_box = bluetooth_box.clone();
|
let device_removed_box = bluetooth_box.clone();
|
||||||
|
let device_changed_box = bluetooth_box.clone();
|
||||||
let loop_box = bluetooth_box.clone();
|
let loop_box = bluetooth_box.clone();
|
||||||
|
|
||||||
// TODO handle device changed
|
// TODO handle device changed
|
||||||
|
@ -118,14 +128,16 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
||||||
println!("added");
|
println!("added");
|
||||||
glib::spawn_future(async move {
|
glib::spawn_future(async move {
|
||||||
glib::idle_add_once(move || {
|
glib::idle_add_once(move || {
|
||||||
|
println!("{}",ir.bluetooth_device.icon);
|
||||||
let imp = bluetooth_box.imp();
|
let imp = bluetooth_box.imp();
|
||||||
let path = ir.bluetooth_device.path.clone();
|
let path = ir.bluetooth_device.path.clone();
|
||||||
let connected = ir.bluetooth_device.connected;
|
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));
|
let entry = Arc::new(ListEntry::new(&*bluetooth_entry));
|
||||||
imp.availableDevices
|
imp.availableDevices.borrow_mut().insert(
|
||||||
.borrow_mut()
|
path,
|
||||||
.insert(path, (bluetooth_entry.clone(), entry.clone()));
|
(bluetooth_entry.clone(), entry.clone(), ir.bluetooth_device),
|
||||||
|
);
|
||||||
if connected {
|
if connected {
|
||||||
imp.resetBluetoothConnectedDevices.append(&*entry);
|
imp.resetBluetoothConnectedDevices.append(&*entry);
|
||||||
} else {
|
} else {
|
||||||
|
@ -147,12 +159,43 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
|
||||||
glib::idle_add_once(move || {
|
glib::idle_add_once(move || {
|
||||||
let imp = bluetooth_box.imp();
|
let imp = bluetooth_box.imp();
|
||||||
let map = imp.availableDevices.borrow_mut();
|
let map = imp.availableDevices.borrow_mut();
|
||||||
let list_entry = map.get(&ir.bluetooth_device);
|
if let Some(list_entry) = map.get(&ir.bluetooth_device) {
|
||||||
if list_entry.is_some() {
|
imp.resetBluetoothAvailableDevices.remove(&*list_entry.1);
|
||||||
imp.resetBluetoothAvailableDevices
|
imp.resetBluetoothConnectedDevices.remove(&*list_entry.1);
|
||||||
.remove(&*list_entry.unwrap().1);
|
}
|
||||||
imp.resetBluetoothConnectedDevices
|
});
|
||||||
.remove(&*list_entry.unwrap().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 dbus::Path;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::subclass::prelude::*;
|
use gtk::subclass::prelude::*;
|
||||||
|
@ -5,12 +6,15 @@ use gtk::{glib, CompositeTemplate, ListBox, Switch};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use adw::ActionRow;
|
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||||
|
|
||||||
use crate::components::base::listEntry::ListEntry;
|
use crate::components::base::listEntry::ListEntry;
|
||||||
use crate::components::bluetooth::bluetoothBox;
|
use crate::components::bluetooth::bluetoothBox;
|
||||||
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
use crate::components::bluetooth::bluetoothEntry::BluetoothEntry;
|
||||||
|
|
||||||
|
type BluetoothMap =
|
||||||
|
RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>, BluetoothDevice)>>;
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[derive(Default, CompositeTemplate)]
|
#[derive(Default, CompositeTemplate)]
|
||||||
#[template(resource = "/org/Xetibo/ReSet/resetBluetooth.ui")]
|
#[template(resource = "/org/Xetibo/ReSet/resetBluetooth.ui")]
|
||||||
|
@ -25,8 +29,8 @@ pub struct BluetoothBox {
|
||||||
pub resetVisibility: TemplateChild<ActionRow>,
|
pub resetVisibility: TemplateChild<ActionRow>,
|
||||||
#[template_child]
|
#[template_child]
|
||||||
pub resetBluetoothMainTab: TemplateChild<ListEntry>,
|
pub resetBluetoothMainTab: TemplateChild<ListEntry>,
|
||||||
pub availableDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
|
pub availableDevices: BluetoothMap,
|
||||||
pub connectedDevices: RefCell<HashMap<Path<'static>, (Arc<BluetoothEntry>, Arc<ListEntry>)>>,
|
pub connectedDevices: BluetoothMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
use std::rc::Rc;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::components::bluetooth::bluetoothEntryImpl;
|
use crate::components::bluetooth::bluetoothEntryImpl;
|
||||||
// use crate::components::bluetooth::bluetoothEntryImpl::DeviceTypes;
|
|
||||||
use adw::glib;
|
use adw::glib;
|
||||||
use adw::glib::Object;
|
use adw::glib::Object;
|
||||||
use adw::subclass::prelude::ObjectSubclassIsExt;
|
use adw::subclass::prelude::ObjectSubclassIsExt;
|
||||||
use dbus::blocking::Connection;
|
use dbus::blocking::Connection;
|
||||||
use dbus::{Error, Path};
|
use dbus::{Error, Path};
|
||||||
use glib::clone;
|
|
||||||
use gtk::prelude::{ButtonExt, WidgetExt};
|
use gtk::prelude::{ButtonExt, WidgetExt};
|
||||||
use gtk::{gio, GestureClick};
|
use gtk::{gio, GestureClick};
|
||||||
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
use ReSet_Lib::bluetooth::bluetooth::BluetoothDevice;
|
||||||
|
@ -21,21 +18,20 @@ glib::wrapper! {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BluetoothEntry {
|
impl BluetoothEntry {
|
||||||
pub fn new(device: BluetoothDevice) -> Self {
|
pub fn new(device: &BluetoothDevice) -> Self {
|
||||||
let entry: BluetoothEntry = Object::builder().build();
|
let entry: BluetoothEntry = Object::builder().build();
|
||||||
let entryImp = entry.imp();
|
let entryImp = entry.imp();
|
||||||
entryImp.resetBluetoothLabel.get().set_text(&device.name);
|
entryImp.resetBluetoothLabel.get().set_text(&device.alias);
|
||||||
entryImp.resetBluetoothAddress.get().set_text(&device.alias);
|
entryImp.resetBluetoothAddress.get().set_text(&device.address);
|
||||||
// entryImp
|
if device.icon == "" {
|
||||||
// .resetBluetoothDeviceType
|
entryImp
|
||||||
// .get()
|
.resetBluetoothDeviceType
|
||||||
// .set_from_icon_name(match deviceType {
|
.set_icon_name(Some("dialog-question-symbolic"));
|
||||||
// DeviceTypes::Mouse => Some("input-mouse-symbolic"),
|
} else {
|
||||||
// DeviceTypes::Keyboard => Some("input-keyboard-symbolic"),
|
entryImp
|
||||||
// DeviceTypes::Headset => Some("output-headset-symbolic"),
|
.resetBluetoothDeviceType
|
||||||
// DeviceTypes::Controller => Some("input-gaming-symbolic"),
|
.set_icon_name(Some(&device.icon));
|
||||||
// DeviceTypes::None => Some("text-x-generic-symbolic"), // no generic bluetooth device icon found
|
}
|
||||||
// });
|
|
||||||
if device.paired {
|
if device.paired {
|
||||||
entryImp.resetBluetoothButton.set_sensitive(true);
|
entryImp.resetBluetoothButton.set_sensitive(true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,17 +43,17 @@ impl BluetoothEntry {
|
||||||
});
|
});
|
||||||
let gesture = GestureClick::new();
|
let gesture = GestureClick::new();
|
||||||
let connected = device.connected;
|
let connected = device.connected;
|
||||||
entryImp.device.replace(device);
|
let paired = device.paired;
|
||||||
gesture.connect_released(clone!(@weak entryImp => move |_, _, _, _| {
|
let path = device.path.clone();
|
||||||
let device = entryImp.device.borrow_mut();
|
gesture.connect_released(move |_, _, _, _| {
|
||||||
if connected {
|
if connected {
|
||||||
disconnect_from_device(device.path.clone());
|
disconnect_from_device(path.clone());
|
||||||
} else if device.paired {
|
} else if paired {
|
||||||
connect_to_device(device.path.clone());
|
connect_to_device(path.clone());
|
||||||
} else {
|
} else {
|
||||||
pair_with_device(device.path.clone());
|
pair_with_device(path.clone());
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
entry.add_controller(gesture);
|
entry.add_controller(gesture);
|
||||||
entry
|
entry
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,7 @@ use crate::components::bluetooth::bluetoothEntry;
|
||||||
use gtk::subclass::prelude::*;
|
use gtk::subclass::prelude::*;
|
||||||
use gtk::{glib, Button, CompositeTemplate, Image, Label};
|
use gtk::{glib, Button, CompositeTemplate, Image, Label};
|
||||||
use std::cell::RefCell;
|
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)]
|
#[allow(non_snake_case)]
|
||||||
#[derive(Default, CompositeTemplate)]
|
#[derive(Default, CompositeTemplate)]
|
||||||
#[template(resource = "/org/Xetibo/ReSet/resetBluetoothEntry.ui")]
|
#[template(resource = "/org/Xetibo/ReSet/resetBluetoothEntry.ui")]
|
||||||
|
@ -26,7 +16,6 @@ pub struct BluetoothEntry {
|
||||||
#[template_child]
|
#[template_child]
|
||||||
pub resetBluetoothButton: TemplateChild<Button>,
|
pub resetBluetoothButton: TemplateChild<Button>,
|
||||||
pub deviceName: RefCell<String>,
|
pub deviceName: RefCell<String>,
|
||||||
pub device: RefCell<BluetoothDevice>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
|
|
@ -63,7 +63,8 @@ fn shutdown(_: &Application) {
|
||||||
"/org/Xetibo/ReSetDaemon",
|
"/org/Xetibo/ReSetDaemon",
|
||||||
Duration::from_millis(100),
|
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
|
res
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -76,7 +77,8 @@ async fn daemon_check() {
|
||||||
"/org/Xetibo/ReSetDaemon",
|
"/org/Xetibo/ReSetDaemon",
|
||||||
Duration::from_millis(100),
|
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
|
res
|
||||||
});
|
});
|
||||||
let res = handle.join();
|
let res = handle.join();
|
||||||
|
|
Loading…
Reference in a new issue