1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

feat: icon per connection type

This commit is contained in:
Reinout Meliesie 2024-06-15 20:38:26 +02:00
parent de29b8dee1
commit cd0991e865
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 291 additions and 117 deletions

View file

@ -1,47 +1,80 @@
use std::sync::Arc; use std::ops::Deref;
use std::sync::{Arc, RwLock};
use color_eyre::Result; use color_eyre::Result;
use futures_signals::signal::{Mutable, MutableSignalCloned}; use futures_signals::signal::{Mutable, MutableSignalCloned};
use tracing::error; use tracing::error;
use zbus::blocking::fdo::PropertiesProxy;
use zbus::blocking::Connection; use zbus::blocking::Connection;
use zbus::{ use zbus::{
dbus_proxy, dbus_proxy,
names::InterfaceName,
zvariant::{ObjectPath, Str}, zvariant::{ObjectPath, Str},
}; };
use crate::{register_fallible_client, spawn_blocking}; use crate::{
read_lock, register_fallible_client, spawn_blocking, spawn_blocking_result, write_lock,
};
const DBUS_BUS: &str = "org.freedesktop.NetworkManager"; // States
const DBUS_PATH: &str = "/org/freedesktop/NetworkManager";
const DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager";
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct Client { pub struct State {
client_state: Mutable<ClientState>, pub wired: WiredState,
interface_name: InterfaceName<'static>, pub wifi: WifiState,
dbus_connection: Connection, pub cellular: CellularState,
props_proxy: PropertiesProxy<'static>, pub vpn: VpnState,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum ClientState { pub enum WiredState {
WiredConnected, Connected,
WifiConnected, Disconnected,
CellularConnected, NotPresent,
VpnConnected,
WifiDisconnected,
Offline,
Unknown, Unknown,
} }
#[derive(Clone, Debug)]
pub enum WifiState {
Connected(WifiConnectedState),
Disconnected,
Disabled,
NotPresent,
Unknown,
}
#[derive(Clone, Debug)]
pub struct WifiConnectedState {
pub ssid: String,
}
#[derive(Clone, Debug)]
pub enum CellularState {
Connected,
Disconnected,
Disabled,
NotPresent,
Unknown,
}
#[derive(Clone, Debug)]
pub enum VpnState {
Connected(VpnConnectedState),
Disconnected,
Unknown,
}
#[derive(Clone, Debug)]
pub struct VpnConnectedState {
pub name: String,
}
// D-Bus interfaces
#[dbus_proxy( #[dbus_proxy(
default_service = "org.freedesktop.NetworkManager", default_service = "org.freedesktop.NetworkManager",
interface = "org.freedesktop.NetworkManager", interface = "org.freedesktop.NetworkManager",
default_path = "/org/freedesktop/NetworkManager" default_path = "/org/freedesktop/NetworkManager"
)] )]
trait NetworkManagerDbus { trait Dbus {
#[dbus_proxy(property)] #[dbus_proxy(property)]
fn active_connections(&self) -> Result<Vec<ObjectPath>>; fn active_connections(&self) -> Result<Vec<ObjectPath>>;
@ -61,73 +94,115 @@ trait NetworkManagerDbus {
fn wireless_enabled(&self) -> Result<bool>; fn wireless_enabled(&self) -> Result<bool>;
} }
impl Client { #[dbus_proxy(
fn new() -> Result<Self> { default_service = "org.freedesktop.NetworkManager",
let client_state = Mutable::new(ClientState::Unknown); interface = "org.freedesktop.NetworkManager.Connection.Active"
let dbus_connection = Connection::system()?; )]
let interface_name = InterfaceName::from_static_str(DBUS_INTERFACE)?; trait ActiveConnectionDbus {
let props_proxy = PropertiesProxy::builder(&dbus_connection) #[dbus_proxy(property)]
.destination(DBUS_BUS)? fn connection(&self) -> Result<ObjectPath>;
.path(DBUS_PATH)?
.build()?;
Ok(Self { #[dbus_proxy(property)]
client_state, fn devices(&self) -> Result<Vec<ObjectPath>>;
interface_name,
dbus_connection, #[dbus_proxy(property)]
props_proxy, fn id(&self) -> Result<Str>;
})
#[dbus_proxy(property)]
fn specific_object(&self) -> Result<ObjectPath>;
#[dbus_proxy(property)]
fn type_(&self) -> Result<Str>;
#[dbus_proxy(property)]
fn uuid(&self) -> Result<Str>;
}
// Ironbar client & helpers
#[derive(Debug)]
pub struct Client(Arc<ClientInner<'static>>);
#[derive(Debug)]
struct ClientInner<'a> {
state: Mutable<State>,
dbus_proxy: &'a DbusProxyBlocking<'a>,
primary_connection: RwLock<ObjectPath<'a>>,
primary_connection_type: RwLock<Str<'a>>,
wireless_enabled: RwLock<bool>,
}
impl Client {
fn new() -> Result<Client> {
let state = Mutable::new(State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
});
let dbus_connection = Connection::system()?;
let dbus_proxy = DbusProxyBlocking::new(&dbus_connection)?;
let primary_connection = dbus_proxy.primary_connection()?.to_owned();
let primary_connection_type = dbus_proxy.primary_connection_type()?.to_owned();
let wireless_enabled = dbus_proxy.wireless_enabled()?;
Ok(Client(Arc::new(ClientInner {
state,
dbus_proxy: Box::leak(Box::new(dbus_proxy)),
primary_connection: RwLock::new(primary_connection),
primary_connection_type: RwLock::new(primary_connection_type),
wireless_enabled: RwLock::new(wireless_enabled),
})))
} }
fn run(&self) -> Result<()> { fn run(&self) -> Result<()> {
let proxy = NetworkManagerDbusProxyBlocking::new(&self.dbus_connection)?; macro_rules! spawn_property_watcher {
($client:expr, $property:ident, $property_changes:ident) => {
let client = $client.clone();
spawn_blocking_result!({
while let Some(change) = client.dbus_proxy.$property_changes().next() {
{
let new_value = change.get()?;
let mut value_guard = write_lock!(client.$property);
*value_guard = new_value;
}
client.state.set(determine_state(
read_lock!(client.primary_connection).deref(),
read_lock!(client.primary_connection_type).deref(),
*read_lock!(client.wireless_enabled),
));
}
Ok(())
});
};
}
let mut primary_connection = proxy.primary_connection()?; // Initial state
let mut primary_connection_type = proxy.primary_connection_type()?; self.0.state.set(determine_state(
let mut wireless_enabled = proxy.wireless_enabled()?; &read_lock!(self.0.primary_connection),
&read_lock!(self.0.primary_connection_type),
self.client_state.set(determine_state( *read_lock!(self.0.wireless_enabled),
&primary_connection,
&primary_connection_type,
wireless_enabled,
)); ));
for change in self.props_proxy.receive_properties_changed()? { spawn_property_watcher!(
let args = change.args()?; self.0,
if args.interface_name != self.interface_name { primary_connection,
continue; receive_primary_connection_changed
} );
spawn_property_watcher!(
let changed_props = args.changed_properties; self.0,
let mut relevant_prop_changed = false; primary_connection_type,
receive_primary_connection_type_changed
if changed_props.contains_key("PrimaryConnection") { );
primary_connection = proxy.primary_connection()?; spawn_property_watcher!(self.0, wireless_enabled, receive_wireless_enabled_changed);
relevant_prop_changed = true;
}
if changed_props.contains_key("PrimaryConnectionType") {
primary_connection_type = proxy.primary_connection_type()?;
relevant_prop_changed = true;
}
if changed_props.contains_key("WirelessEnabled") {
wireless_enabled = proxy.wireless_enabled()?;
relevant_prop_changed = true;
}
if relevant_prop_changed {
self.client_state.set(determine_state(
&primary_connection,
&primary_connection_type,
wireless_enabled,
));
}
}
Ok(()) Ok(())
} }
pub fn subscribe(&self) -> MutableSignalCloned<ClientState> { pub fn subscribe(&self) -> MutableSignalCloned<State> {
self.client_state.signal_cloned() self.0.state.signal_cloned()
} }
} }
@ -135,35 +210,87 @@ pub fn create_client() -> Result<Arc<Client>> {
let client = Arc::new(Client::new()?); let client = Arc::new(Client::new()?);
{ {
let client = client.clone(); let client = client.clone();
spawn_blocking(move || { spawn_blocking_result!({
if let Err(error) = client.run() { client.run()?;
error!("{}", error); Ok(())
};
}); });
} }
Ok(client) Ok(client)
} }
fn determine_state( fn determine_state(
primary_connection: &str, primary_connection: &ObjectPath,
primary_connection_type: &str, primary_connection_type: &Str,
wireless_enabled: bool, wireless_enabled: bool,
) -> ClientState { ) -> State {
if primary_connection == "/" { if primary_connection == "/" {
if wireless_enabled { if wireless_enabled {
ClientState::WifiDisconnected State {
wired: WiredState::Unknown,
wifi: WifiState::Disconnected,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
}
} else { } else {
ClientState::Offline State {
wired: WiredState::Unknown,
wifi: WifiState::Disabled,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
}
} }
} else { } else {
match primary_connection_type { match primary_connection_type.as_str() {
"802-3-ethernet" | "adsl" | "pppoe" => ClientState::WiredConnected, "802-3-ethernet" | "adsl" | "pppoe" => State {
"802-11-olpc-mesh" | "802-11-wireless" | "wifi-p2p" => ClientState::WifiConnected, wired: WiredState::Connected,
"cdma" | "gsm" | "wimax" => ClientState::CellularConnected, wifi: WifiState::Unknown,
"vpn" | "wireguard" => ClientState::VpnConnected, cellular: CellularState::Unknown,
_ => ClientState::Unknown, vpn: VpnState::Unknown,
},
"802-11-olpc-mesh" | "802-11-wireless" | "wifi-p2p" => State {
wired: WiredState::Unknown,
wifi: WifiState::Connected(WifiConnectedState {
ssid: String::new(),
}),
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
},
"cdma" | "gsm" | "wimax" => State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Connected,
vpn: VpnState::Unknown,
},
"vpn" | "wireguard" => State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Unknown,
vpn: VpnState::Connected(VpnConnectedState {
name: String::new(),
}),
},
_ => State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
},
} }
} }
} }
// fn instantiate_active_connections<'a>(
// dbus_connection: &Connection,
// active_connection_paths: Vec<ObjectPath>,
// ) -> Result<Vec<ActiveConnectionDbusProxyBlocking<'a>>> {
// let mut active_connections = Vec::new();
// for active_connection_path in active_connection_paths {
// let active_connection_proxy = ActiveConnectionDbusProxyBlocking::builder(dbus_connection)
// .path(active_connection_path)?
// .build()?;
// active_connections.push(active_connection_proxy);
// }
// Ok(active_connections)
// }
register_fallible_client!(Client, networkmanager); register_fallible_client!(Client, networkmanager);

View file

@ -222,3 +222,14 @@ macro_rules! rc_mut {
std::rc::Rc::new(std::cell::RefCell::new($val)) std::rc::Rc::new(std::cell::RefCell::new($val))
}; };
} }
#[macro_export]
macro_rules! spawn_blocking_result {
($body:block) => {
spawn_blocking(move || {
if let Err(error) = (|| -> Result<()> { $body })() {
error!("Error in fallible spawned closure: {}", error);
}
});
};
}

View file

@ -1,12 +1,12 @@
use color_eyre::Result; use color_eyre::Result;
use futures_lite::StreamExt; use futures_lite::StreamExt;
use futures_signals::signal::SignalExt; use futures_signals::signal::SignalExt;
use gtk::prelude::ContainerExt; use gtk::prelude::{ContainerExt, WidgetExt};
use gtk::{Box as GtkBox, Image, Orientation}; use gtk::{Box as GtkBox, Image, Orientation};
use serde::Deserialize; use serde::Deserialize;
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use crate::clients::networkmanager::{Client, ClientState}; use crate::clients::networkmanager::{CellularState, Client, State, VpnState, WifiState, WiredState};
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;
@ -28,13 +28,13 @@ const fn default_icon_size() -> i32 {
} }
impl Module<GtkBox> for NetworkManagerModule { impl Module<GtkBox> for NetworkManagerModule {
type SendMessage = ClientState; type SendMessage = State;
type ReceiveMessage = (); type ReceiveMessage = ();
fn spawn_controller( fn spawn_controller(
&self, &self,
_: &ModuleInfo, _: &ModuleInfo,
context: &WidgetContext<ClientState, ()>, context: &WidgetContext<State, ()>,
_: Receiver<()>, _: Receiver<()>,
) -> Result<()> { ) -> Result<()> {
let client = context.try_client::<Client>()?; let client = context.try_client::<Client>()?;
@ -52,33 +52,69 @@ impl Module<GtkBox> for NetworkManagerModule {
fn into_widget( fn into_widget(
self, self,
context: WidgetContext<ClientState, ()>, context: WidgetContext<State, ()>,
info: &ModuleInfo, info: &ModuleInfo,
) -> Result<ModuleParts<GtkBox>> { ) -> Result<ModuleParts<GtkBox>> {
let container = GtkBox::new(Orientation::Horizontal, 0); let container = GtkBox::new(Orientation::Horizontal, 0);
let icon = Image::new();
icon.add_class("icon"); // Wired icon
container.add(&icon); let wired_icon = Image::new();
wired_icon.add_class("wired-icon");
container.add(&wired_icon);
// Wifi icon
let wifi_icon = Image::new();
wifi_icon.add_class("wifi-icon");
container.add(&wifi_icon);
// Cellular icon
let cellular_icon = Image::new();
cellular_icon.add_class("cellular-icon");
container.add(&cellular_icon);
// VPN icon
let vpn_icon = Image::new();
vpn_icon.add_class("vpn-icon");
container.add(&vpn_icon);
let icon_theme = info.icon_theme.clone(); let icon_theme = info.icon_theme.clone();
glib_recv!(context.subscribe(), state => {
macro_rules! update_icon {
($icon_var:expr, $state_type:ident, {$($state:pat => $icon_name:expr,)+}) => {
let icon_name = match state.$state_type {
$($state => $icon_name,)+
};
if icon_name.is_empty() {
$icon_var.hide();
} else {
ImageProvider::parse(icon_name, &icon_theme, false, self.icon_size)
.map(|provider| provider.load_into_image($icon_var.clone()));
$icon_var.show();
}
};
}
let initial_icon_name = "content-loading-symbolic"; update_icon!(wired_icon, wired, {
ImageProvider::parse(initial_icon_name, &icon_theme, false, self.icon_size) WiredState::Connected => "icon:network-wired-symbolic",
.map(|provider| provider.load_into_image(icon.clone())); WiredState::Disconnected => "icon:network-wired-disconnected-symbolic",
WiredState::NotPresent | WiredState::Unknown => "",
let widget_receiver = context.subscribe(); });
glib_recv!(widget_receiver, state => { update_icon!(wifi_icon, wifi, {
let icon_name = match state { WifiState::Connected(_) => "icon:network-wireless-connected-symbolic",
ClientState::WiredConnected => "network-wired-symbolic", WifiState::Disconnected => "icon:network-wireless-offline-symbolic",
ClientState::WifiConnected => "network-wireless-symbolic", WifiState::Disabled => "icon:network-wireless-hardware-disabled-symbolic",
ClientState::CellularConnected => "network-cellular-symbolic", WifiState::NotPresent | WifiState::Unknown => "",
ClientState::VpnConnected => "network-vpn-symbolic", });
ClientState::WifiDisconnected => "network-wireless-acquiring-symbolic", update_icon!(cellular_icon, cellular, {
ClientState::Offline => "network-wireless-disabled-symbolic", CellularState::Connected => "icon:network-cellular-connected-symbolic",
ClientState::Unknown => "dialog-question-symbolic", CellularState::Disconnected => "icon:network-cellular-offline-symbolic",
}; CellularState::Disabled => "icon:network-cellular-hardware-disabled-symbolic",
ImageProvider::parse(icon_name, &icon_theme, false, self.icon_size) CellularState::NotPresent | CellularState::Unknown => "",
.map(|provider| provider.load_into_image(icon.clone())); });
update_icon!(vpn_icon, vpn, {
VpnState::Connected(_) => "icon:network-vpn-symbolic",
VpnState::Disconnected | VpnState::Unknown => "",
});
}); });
Ok(ModuleParts::new(container, None)) Ok(ModuleParts::new(container, None))