mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-02 03:01:04 +02:00
refactor: introduce networkmanager client
This commit is contained in:
parent
e1094ca586
commit
1044da251e
5 changed files with 269 additions and 103 deletions
|
@ -7,6 +7,8 @@ pub mod clipboard;
|
|||
pub mod compositor;
|
||||
#[cfg(feature = "music")]
|
||||
pub mod music;
|
||||
#[cfg(feature = "networkmanager")]
|
||||
pub mod networkmanager;
|
||||
#[cfg(feature = "notifications")]
|
||||
pub mod swaync;
|
||||
#[cfg(feature = "tray")]
|
||||
|
@ -28,6 +30,8 @@ pub struct Clients {
|
|||
clipboard: Option<Arc<clipboard::Client>>,
|
||||
#[cfg(feature = "music")]
|
||||
music: std::collections::HashMap<music::ClientType, Arc<dyn music::MusicClient>>,
|
||||
#[cfg(feature = "networkmanager")]
|
||||
networkmanager: Option<Arc<networkmanager::Client>>,
|
||||
#[cfg(feature = "notifications")]
|
||||
notifications: Option<Arc<swaync::Client>>,
|
||||
#[cfg(feature = "tray")]
|
||||
|
@ -76,6 +80,13 @@ impl Clients {
|
|||
.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")]
|
||||
pub fn notifications(&mut self) -> Arc<swaync::Client> {
|
||||
self.notifications
|
||||
|
|
187
src/clients/networkmanager.rs
Normal file
187
src/clients/networkmanager.rs
Normal 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);
|
|
@ -1,13 +1,12 @@
|
|||
use color_eyre::Result;
|
||||
use futures_lite::StreamExt;
|
||||
use gtk::prelude::*;
|
||||
use futures_signals::signal::SignalExt;
|
||||
use futures_util::StreamExt;
|
||||
use gtk::prelude::ContainerExt;
|
||||
use gtk::{Image, Orientation};
|
||||
use serde::Deserialize;
|
||||
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::gtk_helpers::IronbarGtkExt;
|
||||
use crate::image::ImageProvider;
|
||||
|
@ -27,19 +26,8 @@ const fn default_icon_size() -> i32 {
|
|||
24
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum NetworkManagerState {
|
||||
Cellular,
|
||||
Offline,
|
||||
Unknown,
|
||||
Vpn,
|
||||
Wired,
|
||||
Wireless,
|
||||
WirelessDisconnected,
|
||||
}
|
||||
|
||||
impl Module<gtk::Box> for NetworkManagerModule {
|
||||
type SendMessage = NetworkManagerState;
|
||||
type SendMessage = ClientState;
|
||||
type ReceiveMessage = ();
|
||||
|
||||
fn name() -> &'static str {
|
||||
|
@ -49,42 +37,18 @@ impl Module<gtk::Box> for NetworkManagerModule {
|
|||
fn spawn_controller(
|
||||
&self,
|
||||
_: &ModuleInfo,
|
||||
context: &WidgetContext<NetworkManagerState, ()>,
|
||||
context: &WidgetContext<ClientState, ()>,
|
||||
_: Receiver<()>,
|
||||
) -> 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 {
|
||||
/* TODO: This should be moved into a client à la the upower module, however that
|
||||
requires additional refactoring as both would request a PropertyProxy but on
|
||||
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));
|
||||
while let Some(state) = client_signal_stream.next().await {
|
||||
send_async!(widget_transmitter, ModuleUpdateEvent::Update(state));
|
||||
}
|
||||
|
||||
Result::<()>::Ok(())
|
||||
});
|
||||
|
||||
Ok(())
|
||||
|
@ -92,7 +56,7 @@ impl Module<gtk::Box> for NetworkManagerModule {
|
|||
|
||||
fn into_widget(
|
||||
self,
|
||||
context: WidgetContext<NetworkManagerState, ()>,
|
||||
context: WidgetContext<ClientState, ()>,
|
||||
info: &ModuleInfo,
|
||||
) -> Result<ModuleParts<gtk::Box>> {
|
||||
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)
|
||||
.map(|provider| provider.load_into_image(icon.clone()));
|
||||
|
||||
let rx = context.subscribe();
|
||||
glib_recv!(rx, state => {
|
||||
let widget_receiver = context.subscribe();
|
||||
glib_recv!(widget_receiver, state => {
|
||||
let icon_name = match state {
|
||||
NetworkManagerState::Cellular => "network-cellular-symbolic",
|
||||
NetworkManagerState::Offline => "network-wireless-disabled-symbolic",
|
||||
NetworkManagerState::Unknown => "dialog-question-symbolic",
|
||||
NetworkManagerState::Vpn => "network-vpn-symbolic",
|
||||
NetworkManagerState::Wired => "network-wired-symbolic",
|
||||
NetworkManagerState::Wireless => "network-wireless-symbolic",
|
||||
NetworkManagerState::WirelessDisconnected => "network-wireless-acquiring-symbolic",
|
||||
ClientState::Unknown => "dialog-question-symbolic",
|
||||
ClientState::WiredConnected => "network-wired-symbolic",
|
||||
ClientState::WifiConnected => "network-wireless-symbolic",
|
||||
ClientState::CellularConnected => "network-cellular-symbolic",
|
||||
ClientState::VpnConnected => "network-vpn-symbolic",
|
||||
ClientState::WifiDisconnected => "network-wireless-acquiring-symbolic",
|
||||
ClientState::Offline => "network-wireless-disabled-symbolic",
|
||||
};
|
||||
ImageProvider::parse(icon_name, &icon_theme, false, self.icon_size)
|
||||
.map(|provider| provider.load_into_image(icon.clone()));
|
||||
|
@ -124,46 +88,3 @@ impl Module<gtk::Box> for NetworkManagerModule {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue