mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-04-08 22:52:01 +02:00
feat: Improve Wifi event handling
This commit is contained in:
parent
dd2856261b
commit
eed10e8b63
|
@ -6,7 +6,7 @@ description = "A wip universal Linux settings application."
|
|||
|
||||
[dependencies]
|
||||
reset_daemon = "0.2.3"
|
||||
ReSet-Lib = "0.4.0"
|
||||
ReSet-Lib = "0.4.6"
|
||||
adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] }
|
||||
dbus = "0.9.7"
|
||||
gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] }
|
||||
|
|
|
@ -402,6 +402,7 @@ pub fn start_audio_listener(
|
|||
}
|
||||
|
||||
listeners.pulse_listener.store(true, Ordering::SeqCst);
|
||||
|
||||
println!("starting audio listener");
|
||||
loop {
|
||||
let _ = conn.process(Duration::from_millis(1000));
|
||||
|
|
|
@ -14,15 +14,16 @@ use adw::prelude::{ListBoxRowExt, PreferencesGroupExt};
|
|||
use adw::subclass::prelude::ObjectSubclassIsExt;
|
||||
use dbus::arg::{AppendAll, ReadAll, RefArg};
|
||||
use dbus::blocking::Connection;
|
||||
use dbus::message::SignalArgs;
|
||||
use dbus::Error;
|
||||
use dbus::Path;
|
||||
use glib::ObjectExt;
|
||||
use glib::{ObjectExt, PropertySet};
|
||||
use gtk::gio;
|
||||
use gtk::glib::Variant;
|
||||
use gtk::prelude::ActionableExt;
|
||||
use ReSet_Lib::network::network::AccessPoint;
|
||||
use ReSet_Lib::signals::AccessPointRemoved;
|
||||
use gtk::prelude::{ActionableExt, WidgetExt};
|
||||
use ReSet_Lib::network::network::{AccessPoint, WifiStrength};
|
||||
use ReSet_Lib::signals::{AccessPointAdded, GetVal};
|
||||
use ReSet_Lib::signals::{AccessPointChanged, AccessPointRemoved};
|
||||
use ReSet_Lib::utils::Events;
|
||||
|
||||
use crate::components::wifi::wifiBoxImpl;
|
||||
|
@ -66,89 +67,28 @@ pub fn scanForWifi(listeners: Arc<Listeners>, wifiBox: Arc<WifiBox>) {
|
|||
let wifibox_ref = wifiBox.clone();
|
||||
let wifibox_ref_listener = wifiBox.clone();
|
||||
let wifiEntries = wifiBox.imp().wifiEntries.clone();
|
||||
let wifiEntriesPath = wifiBox.imp().wifiEntriesPath.clone();
|
||||
|
||||
gio::spawn_blocking(move || {
|
||||
let accessPoints = get_access_points();
|
||||
let wifiEntriesListener = wifiEntries.clone();
|
||||
let wifiEntries = wifiEntries.clone();
|
||||
let wifiEntriesPath = wifiEntriesPath.clone();
|
||||
dbus_start_network_events();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let mut wifiEntries = wifiEntries.lock().unwrap();
|
||||
let mut wifiEntriesPath = wifiEntriesPath.lock().unwrap();
|
||||
let selfImp = wifibox_ref.imp();
|
||||
for accessPoint in accessPoints {
|
||||
let ssid = accessPoint.ssid.clone();
|
||||
let path = accessPoint.dbus_path.clone();
|
||||
let entry = WifiEntry::new(accessPoint, selfImp);
|
||||
wifiEntries.insert(ssid, entry.clone());
|
||||
wifiEntriesPath.insert(path, entry.clone());
|
||||
selfImp.resetWifiList.add(&*entry);
|
||||
}
|
||||
});
|
||||
});
|
||||
if listeners.network_listener.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
listeners.network_listener.store(true, Ordering::SeqCst);
|
||||
dbus_start_network_events();
|
||||
let (sender, receiver): (
|
||||
Sender<Events<(AccessPoint,), (AccessPoint,)>>,
|
||||
Receiver<Events<(AccessPoint,), (AccessPoint,)>>,
|
||||
) = channel();
|
||||
let sender_ref = Arc::new(sender);
|
||||
let res = start_event_listener::<
|
||||
(AccessPoint,),
|
||||
(AccessPoint,),
|
||||
AccessPointAdded,
|
||||
AccessPointRemoved,
|
||||
>(listeners.clone(), sender_ref);
|
||||
if res.is_err() {
|
||||
println!("Could not connect listener");
|
||||
}
|
||||
loop {
|
||||
let wifiEntriesListener = wifiEntriesListener.clone();
|
||||
if listeners
|
||||
.network_listener
|
||||
.load(std::sync::atomic::Ordering::SeqCst)
|
||||
== false
|
||||
{
|
||||
break;
|
||||
}
|
||||
let res = receiver.recv();
|
||||
if res.is_ok() {
|
||||
let access_point = res.unwrap();
|
||||
match access_point {
|
||||
Events::AddedEvent(access_point) => {
|
||||
let wifiEntriesListener = wifiEntriesListener.clone();
|
||||
let wifiBoxImpl = wifibox_ref_listener.clone();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let mut wifiEntries = wifiEntriesListener.lock().unwrap();
|
||||
let ssid = access_point.0.ssid.clone();
|
||||
if wifiEntries.get(&ssid).is_some() {
|
||||
return;
|
||||
}
|
||||
let entry = WifiEntry::new(access_point.0, wifiBoxImpl.imp());
|
||||
wifiEntries.insert(ssid, entry.clone());
|
||||
wifiBoxImpl.imp().resetWifiList.add(&*entry);
|
||||
});
|
||||
});
|
||||
}
|
||||
Events::RemovedEvent(access_point) => {
|
||||
let wifiBoxImpl = wifibox_ref_listener.clone();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let mut wifiEntries = wifiEntriesListener.lock().unwrap();
|
||||
let entry = wifiEntries.remove(&access_point.0.ssid);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
wifiBoxImpl.imp().resetWifiList.remove(&*entry.unwrap());
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
} else {
|
||||
println!("no message there :)");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -236,75 +176,146 @@ pub fn getConnectionSettings(path: Path<'static>) -> ResetConnection {
|
|||
res.unwrap()
|
||||
}
|
||||
|
||||
// temporary, testing this with lib is pain
|
||||
//
|
||||
pub fn start_event_listener(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) {
|
||||
gio::spawn_blocking(move || {
|
||||
if listeners.network_listener.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
listeners.network_listener.store(true, Ordering::SeqCst);
|
||||
|
||||
pub fn start_event_listener<
|
||||
AddedType: ReadAll + AppendAll + Send + Sync + 'static,
|
||||
RemovedType: ReadAll + AppendAll + Send + Sync + 'static,
|
||||
AddedEvent: ReadAll + AppendAll + dbus::message::SignalArgs + GetVal<AddedType>,
|
||||
RemovedEvent: ReadAll + AppendAll + dbus::message::SignalArgs + GetVal<RemovedType>,
|
||||
>(
|
||||
listeners: Arc<Listeners>,
|
||||
sender: Arc<Sender<Events<AddedType, RemovedType>>>,
|
||||
) -> Result<(), dbus::Error> {
|
||||
thread::spawn(move || {
|
||||
let added_sender = sender.clone();
|
||||
let removed_sender = sender.clone();
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let mr = AddedEvent::match_rule(
|
||||
let added_ref = wifi_box.clone();
|
||||
let removed_ref = wifi_box.clone();
|
||||
let changed_ref = wifi_box.clone(); // TODO implement changed
|
||||
let access_point_added = AccessPointAdded::match_rule(
|
||||
Some(&"org.xetibo.ReSet".into()),
|
||||
Some(&Path::from("/org/xetibo/ReSet")),
|
||||
)
|
||||
.static_clone();
|
||||
let mrb = RemovedEvent::match_rule(
|
||||
let access_point_removed = AccessPointRemoved::match_rule(
|
||||
Some(&"org.xetibo.ReSet".into()),
|
||||
Some(&Path::from("/org/xetibo/ReSet")),
|
||||
)
|
||||
.static_clone();
|
||||
let res = conn.add_match(mr, move |ir: AddedEvent, _, _| {
|
||||
let access_point_changed = AccessPointChanged::match_rule(
|
||||
Some(&"org.xetibo.ReSet".into()),
|
||||
Some(&Path::from("/org/xetibo/ReSet")),
|
||||
)
|
||||
.static_clone();
|
||||
let res = conn.add_match(access_point_added, move |ir: AccessPointAdded, _, _| {
|
||||
println!("received added event");
|
||||
let res = added_sender.send(Events::AddedEvent(ir.get_value()));
|
||||
if res.is_err() {
|
||||
println!("fail on sending added");
|
||||
return false;
|
||||
}
|
||||
// TODO handle add
|
||||
let wifi_box = added_ref.clone();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let imp = wifi_box.imp();
|
||||
let mut wifiEntries = imp.wifiEntries.lock().unwrap();
|
||||
let mut wifiEntriesPath = imp.wifiEntriesPath.lock().unwrap();
|
||||
let ssid = ir.access_point.ssid.clone();
|
||||
let path = ir.access_point.dbus_path.clone();
|
||||
if wifiEntries.get(&ssid).is_some() {
|
||||
return;
|
||||
}
|
||||
let entry = WifiEntry::new(ir.access_point, imp);
|
||||
wifiEntries.insert(ssid, entry.clone());
|
||||
wifiEntriesPath.insert(path, entry.clone());
|
||||
imp.resetWifiList.add(&*entry);
|
||||
});
|
||||
});
|
||||
true
|
||||
});
|
||||
if res.is_err() {
|
||||
println!("fail on add");
|
||||
return Err(dbus::Error::new_custom(
|
||||
"SignalMatchFailed",
|
||||
"Failed to match signal on ReSet.",
|
||||
));
|
||||
return;
|
||||
}
|
||||
let res = conn.add_match(mrb, move |ir: RemovedEvent, _, _| {
|
||||
let res = conn.add_match(access_point_removed, move |ir: AccessPointRemoved, _, _| {
|
||||
println!("received removed event");
|
||||
let res = removed_sender.send(Events::RemovedEvent(ir.get_value()));
|
||||
if res.is_err() {
|
||||
println!("fail on sending removed");
|
||||
return false;
|
||||
}
|
||||
// TODO handle remove
|
||||
let wifi_box = removed_ref.clone();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let imp = wifi_box.imp();
|
||||
let mut wifiEntries = imp.wifiEntries.lock().unwrap();
|
||||
let mut wifiEntriesPath = imp.wifiEntriesPath.lock().unwrap();
|
||||
let entry = wifiEntriesPath.remove(&ir.access_point);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
let ssid = entry.imp().accessPoint.borrow().ssid.clone();
|
||||
wifiEntries.remove(&ssid);
|
||||
imp.resetWifiList.remove(&*entry);
|
||||
});
|
||||
});
|
||||
true
|
||||
});
|
||||
if res.is_err() {
|
||||
println!("fail on remove");
|
||||
return Err(dbus::Error::new_custom(
|
||||
"SignalMatchFailed",
|
||||
"Failed to match signal on ReSet.",
|
||||
));
|
||||
return;
|
||||
}
|
||||
let res = conn.add_match(access_point_changed, move |ir: AccessPointChanged, _, _| {
|
||||
println!("received changed event");
|
||||
dbg!(ir.access_point.clone());
|
||||
let wifi_box = changed_ref.clone();
|
||||
glib::spawn_future(async move {
|
||||
glib::idle_add_local_once(move || {
|
||||
let imp = wifi_box.imp();
|
||||
let wifiEntries = imp.wifiEntries.lock().unwrap();
|
||||
let entry = wifiEntries.get(&ir.access_point.ssid);
|
||||
if entry.is_none() {
|
||||
return;
|
||||
}
|
||||
let entry = entry.unwrap();
|
||||
let entryImp = entry.imp();
|
||||
let strength = WifiStrength::from_u8(ir.access_point.strength);
|
||||
let ssid = ir.access_point.ssid.clone();
|
||||
let name_opt = String::from_utf8(ssid).unwrap_or_else(|_| String::from(""));
|
||||
let name = name_opt.as_str();
|
||||
entryImp.wifiStrength.set(strength);
|
||||
entryImp.resetWifiLabel.get().set_text(name);
|
||||
entryImp.resetWifiEncrypted.set_visible(false);
|
||||
// TODO handle encryption thing
|
||||
entryImp
|
||||
.resetWifiStrength
|
||||
.get()
|
||||
.set_from_icon_name(match strength {
|
||||
WifiStrength::Excellent => {
|
||||
Some("network-wireless-signal-excellent-symbolic")
|
||||
}
|
||||
WifiStrength::Ok => Some("network-wireless-signal-ok-symbolic"),
|
||||
WifiStrength::Weak => Some("network-wireless-signal-weak-symbolic"),
|
||||
WifiStrength::None => Some("network-wireless-signal-none-symbolic"),
|
||||
});
|
||||
if !ir.access_point.stored {
|
||||
entryImp.resetWifiEditButton.set_sensitive(false);
|
||||
}
|
||||
if ir.access_point.connected {
|
||||
entryImp
|
||||
.resetWifiConnected
|
||||
.get()
|
||||
.set_from_icon_name(Some("network-wireless-connected-symbolic"));
|
||||
} else {
|
||||
entryImp.resetWifiConnected.get().set_from_icon_name(None);
|
||||
}
|
||||
{
|
||||
let mut wifiName = entryImp.wifiName.borrow_mut();
|
||||
*wifiName = String::from(name);
|
||||
}
|
||||
});
|
||||
});
|
||||
true
|
||||
});
|
||||
if res.is_err() {
|
||||
println!("fail on change");
|
||||
return;
|
||||
}
|
||||
listeners.network_listener.store(true, Ordering::SeqCst);
|
||||
println!("starting thread listener");
|
||||
loop {
|
||||
let _ = conn.process(Duration::from_millis(1000))?;
|
||||
let _ = conn.process(Duration::from_millis(1000));
|
||||
if !listeners.network_listener.load(Ordering::SeqCst) {
|
||||
println!("stopping thread listener");
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use crate::components::wifi::wifiBox;
|
||||
use adw::{ActionRow, ComboRow, NavigationView, PreferencesGroup};
|
||||
use dbus::Path;
|
||||
use gtk::prelude::*;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, CompositeTemplate, ListBox, Switch};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use adw::{ActionRow, ComboRow, NavigationView, PreferencesGroup};
|
||||
|
||||
use crate::components::base::listEntry::ListEntry;
|
||||
use crate::components::wifi::wifiEntry::WifiEntry;
|
||||
|
@ -30,6 +31,7 @@ pub struct WifiBox {
|
|||
#[template_child]
|
||||
pub resetAvailableNetworks: TemplateChild<ListEntry>,
|
||||
pub wifiEntries: Arc<Mutex<HashMap<Vec<u8>, Arc<WifiEntry>>>>,
|
||||
pub wifiEntriesPath: Arc<Mutex<HashMap<Path<'static>, Arc<WifiEntry>>>>,
|
||||
pub savedWifiEntries: Arc<Mutex<Vec<ListEntry>>>,
|
||||
}
|
||||
|
||||
|
|
|
@ -13,9 +13,9 @@ use gtk::prelude::{ListBoxRowExt, WidgetExt};
|
|||
use gtk::{gio, AlertDialog, GestureClick};
|
||||
use ReSet_Lib::network::network::{AccessPoint, WifiStrength};
|
||||
|
||||
use crate::components::wifi::{wifiEntryImpl};
|
||||
use crate::components::wifi::wifiBox::getConnectionSettings;
|
||||
use crate::components::wifi::wifiBoxImpl::WifiBox;
|
||||
use crate::components::wifi::wifiEntryImpl;
|
||||
use crate::components::wifi::wifiOptions::WifiOptions;
|
||||
|
||||
use super::savedWifiEntry::SavedWifiEntry;
|
||||
|
@ -71,7 +71,7 @@ impl WifiEntry {
|
|||
entry.connect_activated(clone!(@weak entryImp => move |_| {
|
||||
let access_point = entryImp.accessPoint.borrow();
|
||||
if access_point.connected {
|
||||
click_disconnect();
|
||||
click_disconnect(stored_entry.clone());
|
||||
} else if access_point.stored {
|
||||
click_stored_network(stored_entry.clone());
|
||||
} else {
|
||||
|
@ -91,8 +91,9 @@ impl WifiEntry {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn click_disconnect() {
|
||||
pub fn click_disconnect(entry: Arc<WifiEntry>) {
|
||||
println!("called disconnect");
|
||||
let entry_ref = entry.clone();
|
||||
gio::spawn_blocking(move || {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
|
@ -106,7 +107,10 @@ pub fn click_disconnect() {
|
|||
println!("res of disconnect was error bro");
|
||||
return;
|
||||
}
|
||||
println!("disconnect worked");
|
||||
let imp = entry_ref.imp();
|
||||
imp.resetWifiConnected.get().set_from_icon_name(None);
|
||||
imp.accessPoint.borrow_mut().connected = false;
|
||||
println!("disconnect worked");
|
||||
});
|
||||
}
|
||||
pub fn click_stored_network(entry: Arc<WifiEntry>) {
|
||||
|
@ -114,11 +118,6 @@ pub fn click_stored_network(entry: Arc<WifiEntry>) {
|
|||
let entryImp = entry.imp();
|
||||
let access_point = entryImp.accessPoint.borrow().clone();
|
||||
let entry_ref = entry.clone();
|
||||
let popup = entry.imp().resetWifiPopup.imp();
|
||||
popup.resetPopupLabel.set_text("Connecting...");
|
||||
popup.resetPopupLabel.set_visible(true);
|
||||
popup.resetPopupEntry.set_sensitive(false);
|
||||
popup.resetPopupButton.set_sensitive(false);
|
||||
|
||||
gio::spawn_blocking(move || {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
|
@ -135,20 +134,22 @@ pub fn click_stored_network(entry: Arc<WifiEntry>) {
|
|||
glib::spawn_future(async move {
|
||||
glib::idle_add_once(move || {
|
||||
let imp = entry_ref.imp();
|
||||
let popup = imp.resetWifiPopup.imp();
|
||||
if res.is_err() {
|
||||
popup.resetPopupLabel.set_text("Could not connect to dbus.");
|
||||
println!("wat bro?");
|
||||
result.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
return;
|
||||
}
|
||||
if res.unwrap() == (false,) {
|
||||
popup
|
||||
.resetPopupLabel
|
||||
.set_text("Could not connect to access point.");
|
||||
println!("error bro?");
|
||||
result.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||
return;
|
||||
}
|
||||
entry_ref.imp().resetWifiPopup.popdown();
|
||||
let imp = entry_ref.imp();
|
||||
println!("wateroni");
|
||||
imp.resetWifiConnected
|
||||
.get()
|
||||
.set_from_icon_name(Some("network-wireless-connected-symbolic"));
|
||||
imp.accessPoint.borrow_mut().connected = true;
|
||||
result.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
});
|
||||
});
|
||||
|
@ -205,7 +206,12 @@ pub fn click_new_network(entry: Arc<WifiEntry>) {
|
|||
return;
|
||||
}
|
||||
println!("worked?");
|
||||
entry_ref.imp().resetWifiPopup.popdown();
|
||||
let imp = entry_ref.imp();
|
||||
imp.resetWifiPopup.popdown();
|
||||
imp.resetWifiEditButton.set_sensitive(true);
|
||||
imp.resetWifiConnected
|
||||
.get()
|
||||
.set_from_icon_name(Some("network-wireless-connected-symbolic"));
|
||||
result.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
use gtk::prelude::FrameExt;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::components::base::settingBox::SettingBox;
|
||||
use crate::components::base::utils::{start_audio_listener, Listeners};
|
||||
use crate::components::bluetooth::bluetoothBox::{start_bluetooth_listener, BluetoothBox};
|
||||
use crate::components::input::sourceBox::{populate_sources, SourceBox};
|
||||
use crate::components::output::sinkBox::{populate_sinks, SinkBox};
|
||||
use crate::components::wifi::wifiBox::{scanForWifi, show_stored_connections, WifiBox};
|
||||
use crate::components::wifi::wifiBox::{
|
||||
scanForWifi, show_stored_connections, start_event_listener, WifiBox,
|
||||
};
|
||||
use gtk::prelude::WidgetExt;
|
||||
use gtk::{FlowBox, Frame, Label};
|
||||
|
||||
|
@ -15,6 +17,7 @@ pub const HANDLE_CONNECTIVITY_CLICK: fn(Arc<Listeners>, FlowBox) =
|
|||
|listeners: Arc<Listeners>, resetMain: FlowBox| {
|
||||
listeners.stop_audio_listener();
|
||||
let wifiBox = Arc::new(WifiBox::new());
|
||||
start_event_listener(listeners.clone(), wifiBox.clone());
|
||||
show_stored_connections(wifiBox.clone());
|
||||
scanForWifi(listeners.clone(), wifiBox.clone());
|
||||
let wifiFrame = wrapInFrame(SettingBox::new(&*wifiBox));
|
||||
|
@ -32,6 +35,7 @@ pub const HANDLE_WIFI_CLICK: fn(Arc<Listeners>, FlowBox) =
|
|||
listeners.stop_audio_listener();
|
||||
listeners.stop_bluetooth_listener();
|
||||
let wifiBox = Arc::new(WifiBox::new());
|
||||
start_event_listener(listeners.clone(), wifiBox.clone());
|
||||
show_stored_connections(wifiBox.clone());
|
||||
scanForWifi(listeners.clone(), wifiBox.clone());
|
||||
let wifiFrame = wrapInFrame(SettingBox::new(&*wifiBox));
|
||||
|
|
Loading…
Reference in a new issue