1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 18:51:04 +02:00

refactor: introduce networkmanager client

This commit is contained in:
Reinout Meliesie 2024-04-18 01:22:55 +02:00
parent e1094ca586
commit 1044da251e
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
5 changed files with 269 additions and 103 deletions

44
Cargo.lock generated
View file

@ -784,6 +784,12 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "discard"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]] [[package]]
name = "dlib" name = "dlib"
version = "0.5.2" version = "0.5.2"
@ -1059,6 +1065,22 @@ dependencies = [
"syn 2.0.48", "syn 2.0.48",
] ]
[[package]]
name = "futures-signals"
version = "0.3.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b175f2f6600dd81d92d20cf10872b03ea9df6b2513ca7f672341260dacb1ab2"
dependencies = [
"discard",
"futures-channel",
"futures-core",
"futures-util",
"gensym",
"log",
"pin-project",
"serde",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.30" version = "0.3.30"
@ -1157,6 +1179,18 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "gensym"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "913dce4c5f06c2ea40fc178c06f777ac89fc6b1383e90c254fafb1abe4ba3c82"
dependencies = [
"proc-macro2",
"quote 1.0.35",
"syn 2.0.48",
"uuid",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.9" version = "0.2.9"
@ -1669,6 +1703,7 @@ dependencies = [
"ctrlc", "ctrlc",
"dirs", "dirs",
"futures-lite 2.3.0", "futures-lite 2.3.0",
"futures-signals",
"futures-util", "futures-util",
"glib", "glib",
"gtk", "gtk",
@ -3545,6 +3580,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"

View file

@ -59,7 +59,7 @@ music = ["regex"]
"music+mpris" = ["music", "mpris"] "music+mpris" = ["music", "mpris"]
"music+mpd" = ["music", "mpd-utils"] "music+mpd" = ["music", "mpd-utils"]
networkmanager = ["futures-lite", "zbus"] networkmanager = ["futures-lite", "futures-signals", "zbus"]
notifications = ["zbus"] notifications = ["zbus"]
@ -130,6 +130,9 @@ chrono = { version = "0.4.35", optional = true, features = ["unstable-locales"]
mpd-utils = { version = "0.2.0", optional = true } mpd-utils = { version = "0.2.0", optional = true }
mpris = { version = "2.0.1", optional = true } mpris = { version = "2.0.1", optional = true }
# networkmanager
futures-signals = { version = "0.3.33", optional = true }
# sys_info # sys_info
sysinfo = { version = "0.29.11", optional = true } sysinfo = { version = "0.29.11", optional = true }
@ -137,6 +140,7 @@ sysinfo = { version = "0.29.11", optional = true }
system-tray = { version = "0.2.0", optional = true } system-tray = { version = "0.2.0", optional = true }
# upower # upower
futures-lite = { version = "2.3.0", optional = true }
upower_dbus = { version = "0.3.2", optional = true } upower_dbus = { version = "0.3.2", optional = true }
# volume # volume
@ -151,5 +155,4 @@ futures-util = { version = "0.3.30", optional = true }
regex = { version = "1.10.4", default-features = false, features = [ regex = { version = "1.10.4", default-features = false, features = [
"std", "std",
], optional = true } # music, sys_info ], optional = true } # music, sys_info
futures-lite = { version = "2.3.0", optional = true } # networkmanager, upower
zbus = { version = "3.15.2", optional = true } # networkmanager, notifications, upower zbus = { version = "3.15.2", optional = true } # networkmanager, notifications, upower

View file

@ -7,6 +7,8 @@ pub mod clipboard;
pub mod compositor; pub mod compositor;
#[cfg(feature = "music")] #[cfg(feature = "music")]
pub mod music; pub mod music;
#[cfg(feature = "networkmanager")]
pub mod networkmanager;
#[cfg(feature = "notifications")] #[cfg(feature = "notifications")]
pub mod swaync; pub mod swaync;
#[cfg(feature = "tray")] #[cfg(feature = "tray")]
@ -28,6 +30,8 @@ pub struct Clients {
clipboard: Option<Arc<clipboard::Client>>, clipboard: Option<Arc<clipboard::Client>>,
#[cfg(feature = "music")] #[cfg(feature = "music")]
music: std::collections::HashMap<music::ClientType, Arc<dyn music::MusicClient>>, music: std::collections::HashMap<music::ClientType, Arc<dyn music::MusicClient>>,
#[cfg(feature = "networkmanager")]
networkmanager: Option<Arc<networkmanager::Client>>,
#[cfg(feature = "notifications")] #[cfg(feature = "notifications")]
notifications: Option<Arc<swaync::Client>>, notifications: Option<Arc<swaync::Client>>,
#[cfg(feature = "tray")] #[cfg(feature = "tray")]
@ -76,6 +80,13 @@ impl Clients {
.clone() .clone()
} }
#[cfg(feature = "networkmanager")]
pub fn networkmanager(&mut self) -> Arc<networkmanager::Client> {
self.networkmanager
.get_or_insert_with(networkmanager::create_client)
.clone()
}
#[cfg(feature = "notifications")] #[cfg(feature = "notifications")]
pub fn notifications(&mut self) -> Arc<swaync::Client> { pub fn notifications(&mut self) -> Arc<swaync::Client> {
self.notifications self.notifications

View file

@ -0,0 +1,187 @@
use std::sync::Arc;
use futures_signals::signal::{Mutable, MutableSignalCloned};
use tracing::error;
use zbus::{
blocking::{fdo::PropertiesProxy, Connection},
names::InterfaceName,
zvariant::{ObjectPath, Str},
};
use crate::{register_client, spawn_blocking};
static DBUS_BUS: &str = "org.freedesktop.NetworkManager";
static DBUS_PATH: &str = "/org/freedesktop/NetworkManager";
static DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager";
#[derive(Debug)]
pub struct Client {
client_state: Mutable<ClientState>,
}
#[derive(Clone, Debug)]
pub enum ClientState {
Unknown,
WiredConnected,
WifiConnected,
CellularConnected,
VpnConnected,
WifiDisconnected,
Offline,
}
impl Client {
fn new() -> Self {
let client_state = Mutable::new(ClientState::Unknown);
Self { client_state }
}
fn run(&self) {
let Ok(dbus_connection) = Connection::system() else {
error!("Failed to create D-Bus system connection");
return;
};
let builder = PropertiesProxy::builder(&dbus_connection);
let Ok(builder) = builder.destination(DBUS_BUS) else {
error!("Failed to connect to NetworkManager D-Bus bus");
return;
};
let Ok(builder) = builder.path(DBUS_PATH) else {
error!("Failed to set to NetworkManager D-Bus path");
return;
};
let Ok(props_proxy) = builder.build() else {
error!("Failed to create NetworkManager D-Bus properties proxy");
return;
};
let Ok(interface_name) = InterfaceName::from_static_str(DBUS_INTERFACE) else {
error!("Failed to create NetworkManager D-Bus interface name");
return;
};
let Ok(changed_props_stream) = props_proxy.receive_properties_changed() else {
error!("Failed to create NetworkManager D-Bus changed properties stream");
return;
};
let Ok(props) = props_proxy.get_all(interface_name.clone()) else {
error!("Failed to get NetworkManager D-Bus properties");
return;
};
let mut primary_connection = {
let Some(primary_connection) = props["PrimaryConnection"].downcast_ref::<ObjectPath>()
else {
error!("PrimaryConnection D-Bus property is not a path");
return;
};
primary_connection.to_string()
};
let mut primary_connection_type = {
let Some(primary_connection_type) =
props["PrimaryConnectionType"].downcast_ref::<Str>()
else {
error!("PrimaryConnectionType D-Bus property is not a string");
return;
};
primary_connection_type.to_string()
};
let mut wireless_enabled = {
let Some(wireless_enabled) = props["WirelessEnabled"].downcast_ref::<bool>() else {
error!("WirelessEnabled D-Bus property is not a boolean");
return;
};
*wireless_enabled
};
self.client_state.set(determine_state(
&primary_connection,
&primary_connection_type,
wireless_enabled,
));
for signal in changed_props_stream {
let Ok(args) = signal.args() else {
error!("Failed to obtain NetworkManager D-Bus changed properties signal arguments");
return;
};
if args.interface_name != interface_name {
continue;
}
let changed_props = args.changed_properties;
if let Some(new_primary_connection) = changed_props.get("PrimaryConnection") {
let Some(new_primary_connection) =
new_primary_connection.downcast_ref::<ObjectPath>()
else {
error!("PrimaryConnection D-Bus property is not a path");
return;
};
primary_connection = new_primary_connection.to_string();
}
if let Some(new_primary_connection_type) = changed_props.get("PrimaryConnectionType") {
let Some(new_primary_connection_type) =
new_primary_connection_type.downcast_ref::<Str>()
else {
error!("PrimaryConnectionType D-Bus property is not a string");
return;
};
primary_connection_type = new_primary_connection_type.to_string();
}
if let Some(new_wireless_enabled) = changed_props.get("WirelessEnabled") {
let Some(new_wireless_enabled) = new_wireless_enabled.downcast_ref::<bool>() else {
error!("WirelessEnabled D-Bus property is not a string");
return;
};
wireless_enabled = *new_wireless_enabled;
}
self.client_state.set(determine_state(
&primary_connection,
&primary_connection_type,
wireless_enabled,
));
}
}
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> {
self.client_state.signal_cloned()
}
}
pub fn create_client() -> Arc<Client> {
let client = Arc::new(Client::new());
{
let client = client.clone();
spawn_blocking(move || {
client.run();
});
}
client
}
fn determine_state(
primary_connection: &str,
primary_connection_type: &str,
wireless_enabled: bool,
) -> ClientState {
if primary_connection == "/" {
if wireless_enabled {
ClientState::WifiDisconnected
} else {
ClientState::Offline
}
} else {
match primary_connection_type {
"802-11-olpc-mesh" => ClientState::WifiConnected,
"802-11-wireless" => ClientState::WifiConnected,
"802-3-ethernet" => ClientState::WiredConnected,
"adsl" => ClientState::WiredConnected,
"cdma" => ClientState::CellularConnected,
"gsm" => ClientState::CellularConnected,
"pppoe" => ClientState::WiredConnected,
"vpn" => ClientState::VpnConnected,
"wifi-p2p" => ClientState::WifiConnected,
"wimax" => ClientState::CellularConnected,
"wireguard" => ClientState::VpnConnected,
_ => ClientState::Unknown,
}
}
}
register_client!(Client, networkmanager);

View file

@ -1,13 +1,12 @@
use color_eyre::Result; use color_eyre::Result;
use futures_lite::StreamExt; use futures_signals::signal::SignalExt;
use gtk::prelude::*; use futures_util::StreamExt;
use gtk::prelude::ContainerExt;
use gtk::{Image, Orientation}; use gtk::{Image, Orientation};
use serde::Deserialize; use serde::Deserialize;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use zbus::fdo::PropertiesProxy;
use zbus::names::InterfaceName;
use zbus::zvariant::ObjectPath;
use crate::clients::networkmanager::{Client, ClientState};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarGtkExt; use crate::gtk_helpers::IronbarGtkExt;
use crate::image::ImageProvider; use crate::image::ImageProvider;
@ -27,19 +26,8 @@ const fn default_icon_size() -> i32 {
24 24
} }
#[derive(Clone, Debug)]
pub enum NetworkManagerState {
Cellular,
Offline,
Unknown,
Vpn,
Wired,
Wireless,
WirelessDisconnected,
}
impl Module<gtk::Box> for NetworkManagerModule { impl Module<gtk::Box> for NetworkManagerModule {
type SendMessage = NetworkManagerState; type SendMessage = ClientState;
type ReceiveMessage = (); type ReceiveMessage = ();
fn name() -> &'static str { fn name() -> &'static str {
@ -49,42 +37,18 @@ impl Module<gtk::Box> for NetworkManagerModule {
fn spawn_controller( fn spawn_controller(
&self, &self,
_: &ModuleInfo, _: &ModuleInfo,
context: &WidgetContext<NetworkManagerState, ()>, context: &WidgetContext<ClientState, ()>,
_: Receiver<()>, _: Receiver<()>,
) -> Result<()> { ) -> Result<()> {
let tx = context.tx.clone(); let client = context.client::<Client>();
let client_signal = client.subscribe();
let mut client_signal_stream = client_signal.to_stream();
let widget_transmitter = context.tx.clone();
spawn(async move { spawn(async move {
/* TODO: This should be moved into a client à la the upower module, however that while let Some(state) = client_signal_stream.next().await {
requires additional refactoring as both would request a PropertyProxy but on send_async!(widget_transmitter, ModuleUpdateEvent::Update(state));
different buses. The proper solution will be to rewrite both to use trait-derived
proxies. */
let nm_proxy = {
let dbus = zbus::Connection::system().await?;
PropertiesProxy::builder(&dbus)
.destination("org.freedesktop.NetworkManager")?
.path("/org/freedesktop/NetworkManager")?
.build()
.await?
};
let device_interface_name =
InterfaceName::from_static_str("org.freedesktop.NetworkManager")?;
let state = get_network_state(&nm_proxy, &device_interface_name).await?;
send_async!(tx, ModuleUpdateEvent::Update(state));
let mut prop_changed_stream = nm_proxy.receive_properties_changed().await?;
while let Some(signal) = prop_changed_stream.next().await {
let args = signal.args()?;
if args.interface_name != device_interface_name {
continue;
}
let state = get_network_state(&nm_proxy, &device_interface_name).await?;
send_async!(tx, ModuleUpdateEvent::Update(state));
} }
Result::<()>::Ok(())
}); });
Ok(()) Ok(())
@ -92,7 +56,7 @@ impl Module<gtk::Box> for NetworkManagerModule {
fn into_widget( fn into_widget(
self, self,
context: WidgetContext<NetworkManagerState, ()>, context: WidgetContext<ClientState, ()>,
info: &ModuleInfo, info: &ModuleInfo,
) -> Result<ModuleParts<gtk::Box>> { ) -> Result<ModuleParts<gtk::Box>> {
let container = gtk::Box::new(Orientation::Horizontal, 0); let container = gtk::Box::new(Orientation::Horizontal, 0);
@ -106,16 +70,16 @@ impl Module<gtk::Box> for NetworkManagerModule {
ImageProvider::parse(initial_icon_name, &icon_theme, false, self.icon_size) ImageProvider::parse(initial_icon_name, &icon_theme, false, self.icon_size)
.map(|provider| provider.load_into_image(icon.clone())); .map(|provider| provider.load_into_image(icon.clone()));
let rx = context.subscribe(); let widget_receiver = context.subscribe();
glib_recv!(rx, state => { glib_recv!(widget_receiver, state => {
let icon_name = match state { let icon_name = match state {
NetworkManagerState::Cellular => "network-cellular-symbolic", ClientState::Unknown => "dialog-question-symbolic",
NetworkManagerState::Offline => "network-wireless-disabled-symbolic", ClientState::WiredConnected => "network-wired-symbolic",
NetworkManagerState::Unknown => "dialog-question-symbolic", ClientState::WifiConnected => "network-wireless-symbolic",
NetworkManagerState::Vpn => "network-vpn-symbolic", ClientState::CellularConnected => "network-cellular-symbolic",
NetworkManagerState::Wired => "network-wired-symbolic", ClientState::VpnConnected => "network-vpn-symbolic",
NetworkManagerState::Wireless => "network-wireless-symbolic", ClientState::WifiDisconnected => "network-wireless-acquiring-symbolic",
NetworkManagerState::WirelessDisconnected => "network-wireless-acquiring-symbolic", ClientState::Offline => "network-wireless-disabled-symbolic",
}; };
ImageProvider::parse(icon_name, &icon_theme, false, self.icon_size) ImageProvider::parse(icon_name, &icon_theme, false, self.icon_size)
.map(|provider| provider.load_into_image(icon.clone())); .map(|provider| provider.load_into_image(icon.clone()));
@ -124,46 +88,3 @@ impl Module<gtk::Box> for NetworkManagerModule {
Ok(ModuleParts::new(container, None)) Ok(ModuleParts::new(container, None))
} }
} }
async fn get_network_state(
nm_proxy: &PropertiesProxy<'_>,
device_interface_name: &InterfaceName<'_>,
) -> Result<NetworkManagerState> {
let properties = nm_proxy.get_all(device_interface_name.clone()).await?;
let primary_connection_path = properties["PrimaryConnection"]
.downcast_ref::<ObjectPath>()
.expect("PrimaryConnection was not an object path, violation of NetworkManager D-Bus interface");
if primary_connection_path != "/" {
let primary_connection_type = properties["PrimaryConnectionType"]
.downcast_ref::<str>()
.expect("PrimaryConnectionType was not a string, violation of NetworkManager D-Bus interface")
.to_string();
match primary_connection_type.as_str() {
"802-11-olpc-mesh" => Ok(NetworkManagerState::Wireless),
"802-11-wireless" => Ok(NetworkManagerState::Wireless),
"802-3-ethernet" => Ok(NetworkManagerState::Wired),
"adsl" => Ok(NetworkManagerState::Wired),
"cdma" => Ok(NetworkManagerState::Cellular),
"gsm" => Ok(NetworkManagerState::Cellular),
"pppoe" => Ok(NetworkManagerState::Wired),
"vpn" => Ok(NetworkManagerState::Vpn),
"wifi-p2p" => Ok(NetworkManagerState::Wireless),
"wimax" => Ok(NetworkManagerState::Cellular),
"wireguard" => Ok(NetworkManagerState::Vpn),
"wpan" => Ok(NetworkManagerState::Wireless),
_ => Ok(NetworkManagerState::Unknown),
}
} else {
let wireless_enabled = *properties["WirelessEnabled"]
.downcast_ref::<bool>()
.expect("WirelessEnabled was not a boolean, violation of NetworkManager D-Bus interface");
if wireless_enabled {
Ok(NetworkManagerState::WirelessDisconnected)
} else {
Ok(NetworkManagerState::Offline)
}
}
}