mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 18:51:04 +02:00
fix: state not being set properly in initialisation
This commit is contained in:
parent
4182038643
commit
39eb3cf917
3 changed files with 111 additions and 77 deletions
|
@ -14,17 +14,17 @@ trait Dbus {
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn devices(&self) -> Result<Vec<ObjectPath>>;
|
fn devices(&self) -> Result<Vec<ObjectPath>>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn networking_enabled(&self) -> Result<bool>;
|
// fn networking_enabled(&self) -> Result<bool>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn primary_connection(&self) -> Result<ObjectPath>;
|
// fn primary_connection(&self) -> Result<ObjectPath>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn primary_connection_type(&self) -> Result<Str>;
|
// fn primary_connection_type(&self) -> Result<Str>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn wireless_enabled(&self) -> Result<bool>;
|
// fn wireless_enabled(&self) -> Result<bool>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[dbus_proxy(
|
#[dbus_proxy(
|
||||||
|
@ -32,26 +32,26 @@ trait Dbus {
|
||||||
interface = "org.freedesktop.NetworkManager.Connection.Active"
|
interface = "org.freedesktop.NetworkManager.Connection.Active"
|
||||||
)]
|
)]
|
||||||
trait ActiveConnectionDbus {
|
trait ActiveConnectionDbus {
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn connection(&self) -> Result<ObjectPath>;
|
// fn connection(&self) -> Result<ObjectPath>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn default(&self) -> Result<bool>;
|
// fn default(&self) -> Result<bool>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn default6(&self) -> Result<bool>;
|
// fn default6(&self) -> Result<bool>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn devices(&self) -> Result<Vec<ObjectPath>>;
|
fn devices(&self) -> Result<Vec<ObjectPath>>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn id(&self) -> Result<Str>;
|
// fn id(&self) -> Result<Str>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn type_(&self) -> Result<Str>;
|
fn type_(&self) -> Result<Str>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn uuid(&self) -> Result<Str>;
|
// fn uuid(&self) -> Result<Str>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[dbus_proxy(
|
#[dbus_proxy(
|
||||||
|
@ -59,8 +59,8 @@ trait ActiveConnectionDbus {
|
||||||
interface = "org.freedesktop.NetworkManager.Device"
|
interface = "org.freedesktop.NetworkManager.Device"
|
||||||
)]
|
)]
|
||||||
trait DeviceDbus {
|
trait DeviceDbus {
|
||||||
#[dbus_proxy(property)]
|
// #[dbus_proxy(property)]
|
||||||
fn active_connection(&self) -> Result<ObjectPath>;
|
// fn active_connection(&self) -> Result<ObjectPath>;
|
||||||
|
|
||||||
#[dbus_proxy(property)]
|
#[dbus_proxy(property)]
|
||||||
fn device_type(&self) -> Result<DeviceType>;
|
fn device_type(&self) -> Result<DeviceType>;
|
||||||
|
@ -125,9 +125,9 @@ pub(super) enum DeviceState {
|
||||||
|
|
||||||
impl DeviceState {
|
impl DeviceState {
|
||||||
pub(super) fn is_enabled(&self) -> bool {
|
pub(super) fn is_enabled(&self) -> bool {
|
||||||
match self {
|
!matches!(
|
||||||
DeviceState::Unknown | DeviceState::Unmanaged | DeviceState::Unavailable => false,
|
self,
|
||||||
_ => true,
|
DeviceState::Unknown | DeviceState::Unmanaged | DeviceState::Unavailable,
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
mod dbus;
|
|
||||||
pub mod state;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
@ -21,17 +18,20 @@ use crate::{
|
||||||
read_lock, register_fallible_client, spawn_blocking, spawn_blocking_result, write_lock,
|
read_lock, register_fallible_client, spawn_blocking, spawn_blocking_result, write_lock,
|
||||||
};
|
};
|
||||||
|
|
||||||
type PathMap<'a, T> = HashMap<ObjectPath<'a>, T>;
|
mod dbus;
|
||||||
|
pub mod state;
|
||||||
|
|
||||||
|
type PathMap<'l, ValueType> = HashMap<ObjectPath<'l>, ValueType>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Client(Arc<ClientInner<'static>>);
|
pub struct Client(Arc<ClientInner<'static>>);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct ClientInner<'a> {
|
struct ClientInner<'l> {
|
||||||
state: Mutable<State>,
|
state: Mutable<State>,
|
||||||
root_object: &'a DbusProxyBlocking<'a>,
|
root_object: &'l DbusProxyBlocking<'l>,
|
||||||
active_connections: RwLock<PathMap<'a, ActiveConnectionDbusProxyBlocking<'a>>>,
|
active_connections: RwLock<PathMap<'l, ActiveConnectionDbusProxyBlocking<'l>>>,
|
||||||
devices: RwLock<PathMap<'a, DeviceDbusProxyBlocking<'a>>>,
|
devices: RwLock<PathMap<'l, DeviceDbusProxyBlocking<'l>>>,
|
||||||
dbus_connection: Connection,
|
dbus_connection: Connection,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,11 +44,15 @@ impl Client {
|
||||||
vpn: VpnState::Unknown,
|
vpn: VpnState::Unknown,
|
||||||
});
|
});
|
||||||
let dbus_connection = Connection::system()?;
|
let dbus_connection = Connection::system()?;
|
||||||
let dbus_proxy = DbusProxyBlocking::new(&dbus_connection)?;
|
let root_object = {
|
||||||
|
let root_object = DbusProxyBlocking::new(&dbus_connection)?;
|
||||||
|
// Workaround for the fact that zbus (unnecessarily) requires a static lifetime here
|
||||||
|
Box::leak(Box::new(root_object))
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Client(Arc::new(ClientInner {
|
Ok(Client(Arc::new(ClientInner {
|
||||||
state,
|
state,
|
||||||
root_object: Box::leak(Box::new(dbus_proxy)), // TODO: Check if boxing is still necessary
|
root_object,
|
||||||
active_connections: RwLock::new(HashMap::new()),
|
active_connections: RwLock::new(HashMap::new()),
|
||||||
devices: RwLock::new(HashMap::new()),
|
devices: RwLock::new(HashMap::new()),
|
||||||
dbus_connection,
|
dbus_connection,
|
||||||
|
@ -56,41 +60,74 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self) -> Result<()> {
|
fn run(&self) -> Result<()> {
|
||||||
macro_rules! spawn_property_watcher {
|
// Initialisation
|
||||||
($client:expr, $property_changes:ident) => {
|
{
|
||||||
let client = $client.clone();
|
let client = &self.0;
|
||||||
spawn_blocking_result!({
|
|
||||||
while let Some(_) = client.root_object.$property_changes().next() {
|
// Initial active connections path list
|
||||||
client.state.set(State {
|
{
|
||||||
wired: determine_wired_state(&read_lock!(client.devices))?,
|
let new_paths = client.root_object.active_connections()?;
|
||||||
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
let mut pathmap = write_lock!(client.active_connections);
|
||||||
cellular: determine_cellular_state(&read_lock!(client.devices))?,
|
for new_path in new_paths {
|
||||||
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
|
let new_proxy =
|
||||||
|
ActiveConnectionDbusProxyBlocking::builder(&client.dbus_connection)
|
||||||
|
.path(new_path.clone())?
|
||||||
|
.build()?;
|
||||||
|
pathmap.insert(new_path, new_proxy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial devices path list
|
||||||
|
{
|
||||||
|
let new_paths = client.root_object.devices()?;
|
||||||
|
let mut pathmap = write_lock!(client.devices);
|
||||||
|
for new_path in new_paths {
|
||||||
|
let new_proxy = DeviceDbusProxyBlocking::builder(&client.dbus_connection)
|
||||||
|
.path(new_path.clone())?
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
// Specific device state watcher
|
||||||
|
{
|
||||||
|
let client = client.clone();
|
||||||
|
let new_path = new_path.clone();
|
||||||
|
spawn_blocking_result!({
|
||||||
|
let changes = read_lock!(client.devices)
|
||||||
|
.get(&new_path)
|
||||||
|
.unwrap()
|
||||||
|
.receive_state_changed();
|
||||||
|
for _ in changes {
|
||||||
|
// TODO: Check if our device still exists in client.devices
|
||||||
|
client.state.set(State {
|
||||||
|
wired: determine_wired_state(&read_lock!(client.devices))?,
|
||||||
|
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
||||||
|
cellular: determine_cellular_state(&read_lock!(
|
||||||
|
client.devices
|
||||||
|
))?,
|
||||||
|
vpn: client.state.get_cloned().vpn,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
});
|
pathmap.insert(new_path, new_proxy);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
client.state.set(State {
|
||||||
|
wired: determine_wired_state(&read_lock!(client.devices))?,
|
||||||
|
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
||||||
|
cellular: determine_cellular_state(&read_lock!(client.devices))?,
|
||||||
|
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! spawn_list_watcher {
|
// Watcher for active connections path list
|
||||||
() => {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initial state
|
|
||||||
self.0.state.set(State {
|
|
||||||
wired: determine_wired_state(&read_lock!(self.0.devices))?,
|
|
||||||
wifi: determine_wifi_state(&read_lock!(self.0.devices))?,
|
|
||||||
cellular: determine_cellular_state(&read_lock!(self.0.devices))?,
|
|
||||||
vpn: determine_vpn_state(&read_lock!(self.0.active_connections))?,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Active connections paths list watcher
|
|
||||||
{
|
{
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
spawn_blocking_result!({
|
spawn_blocking_result!({
|
||||||
let mut changes = client.root_object.receive_active_connections_changed();
|
let changes = client.root_object.receive_active_connections_changed();
|
||||||
while let Some(_) = changes.next() {
|
for _ in changes {
|
||||||
let mut new_pathmap = HashMap::new();
|
let mut new_pathmap = HashMap::new();
|
||||||
{
|
{
|
||||||
let new_paths = client.root_object.active_connections()?;
|
let new_paths = client.root_object.active_connections()?;
|
||||||
|
@ -115,9 +152,9 @@ impl Client {
|
||||||
}
|
}
|
||||||
*write_lock!(client.active_connections) = new_pathmap;
|
*write_lock!(client.active_connections) = new_pathmap;
|
||||||
client.state.set(State {
|
client.state.set(State {
|
||||||
wired: determine_wired_state(&read_lock!(client.devices))?,
|
wired: client.state.get_cloned().wired,
|
||||||
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
wifi: client.state.get_cloned().wifi,
|
||||||
cellular: determine_cellular_state(&read_lock!(client.devices))?,
|
cellular: client.state.get_cloned().cellular,
|
||||||
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
|
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -125,12 +162,12 @@ impl Client {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Devices paths list watcher
|
// Watcher for devices path list
|
||||||
{
|
{
|
||||||
let client = self.0.clone();
|
let client = self.0.clone();
|
||||||
spawn_blocking_result!({
|
spawn_blocking_result!({
|
||||||
let mut changes = client.root_object.receive_devices_changed();
|
let changes = client.root_object.receive_devices_changed();
|
||||||
while let Some(_) = changes.next() {
|
for _ in changes {
|
||||||
let mut new_pathmap = HashMap::new();
|
let mut new_pathmap = HashMap::new();
|
||||||
{
|
{
|
||||||
let new_paths = client.root_object.devices()?;
|
let new_paths = client.root_object.devices()?;
|
||||||
|
@ -152,11 +189,12 @@ impl Client {
|
||||||
let client = client.clone();
|
let client = client.clone();
|
||||||
let new_path = new_path.clone();
|
let new_path = new_path.clone();
|
||||||
spawn_blocking_result!({
|
spawn_blocking_result!({
|
||||||
let mut changes = read_lock!(client.devices)
|
let changes = read_lock!(client.devices)
|
||||||
.get(&new_path)
|
.get(&new_path)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.receive_state_changed();
|
.receive_state_changed();
|
||||||
while let Some(_) = changes.next() {
|
for _ in changes {
|
||||||
|
// TODO: Check if our device still exists in client.devices
|
||||||
client.state.set(State {
|
client.state.set(State {
|
||||||
wired: determine_wired_state(&read_lock!(
|
wired: determine_wired_state(&read_lock!(
|
||||||
client.devices
|
client.devices
|
||||||
|
@ -167,9 +205,7 @@ impl Client {
|
||||||
cellular: determine_cellular_state(&read_lock!(
|
cellular: determine_cellular_state(&read_lock!(
|
||||||
client.devices
|
client.devices
|
||||||
))?,
|
))?,
|
||||||
vpn: determine_vpn_state(&read_lock!(
|
vpn: client.state.get_cloned().vpn,
|
||||||
client.active_connections
|
|
||||||
))?,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -187,13 +223,12 @@ impl Client {
|
||||||
wired: determine_wired_state(&read_lock!(client.devices))?,
|
wired: determine_wired_state(&read_lock!(client.devices))?,
|
||||||
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
wifi: determine_wifi_state(&read_lock!(client.devices))?,
|
||||||
cellular: determine_cellular_state(&read_lock!(client.devices))?,
|
cellular: determine_cellular_state(&read_lock!(client.devices))?,
|
||||||
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
|
vpn: client.state.get_cloned().vpn,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -124,8 +124,7 @@ impl Module<GtkBox> for NetworkManagerModule {
|
||||||
});
|
});
|
||||||
update_icon!(vpn_icon, vpn, {
|
update_icon!(vpn_icon, vpn, {
|
||||||
VpnState::Connected(_) => "icon:network-vpn-symbolic",
|
VpnState::Connected(_) => "icon:network-vpn-symbolic",
|
||||||
VpnState::Disconnected => "icon:network-vpn-disabled-symbolic",
|
VpnState::Disconnected | VpnState::Unknown => "",
|
||||||
VpnState::Unknown => "",
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue