From c0f832bceff241ff615f3481a5b8d4ead8fcc7ba Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Thu, 14 Dec 2023 23:29:46 +0000 Subject: [PATCH] WIP NetworkManager tile --- build.rs | 7 +++++- flake.nix | 2 +- src/config.rs | 12 ++++++++++ src/generated.rs | 1 + src/tiles/mod.rs | 2 ++ src/tiles/network_manager.rs | 46 ++++++++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/tiles/network_manager.rs diff --git a/build.rs b/build.rs index 8fbd7e7..1ab082a 100644 --- a/build.rs +++ b/build.rs @@ -12,7 +12,12 @@ fn main() { println!("cargo:rerun-if-env-changed=DBUS_XML_PATH"); println!("cargo:rerun-if-changed=build.rs"); - let required_files = HashSet::from(["org.freedesktop.hostname1.xml"]); + let required_files = HashSet::from([ + "org.freedesktop.NetworkManager.AccessPoint.xml", + "org.freedesktop.NetworkManager.Connection.Active.xml", + "org.freedesktop.NetworkManager.xml", + "org.freedesktop.hostname1.xml", + ]); let required_interfaces = required_files .iter() .map(|name| name.strip_suffix(".xml").unwrap().to_string()) diff --git a/flake.nix b/flake.nix index 6e7957d..cc93507 100644 --- a/flake.nix +++ b/flake.nix @@ -13,7 +13,7 @@ pkgs = import nixpkgs { inherit system; }; inherit (pkgs) lib; dbusPaths = lib.makeSearchPathOutput "out" "share/dbus-1/interfaces" - (with pkgs; [ systemd ]); + (with pkgs; [ systemd networkmanager ]); in rec { packages.rustybar = with pkgs; rustPlatform.buildRustPackage { diff --git a/src/config.rs b/src/config.rs index cd24912..eeade2c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -60,6 +60,7 @@ pub enum TileConfig { LocalTime(LocalTimeConfig), InternetTime(InternetTimeConfig), Iwd(IwdConfig), + NetworkManager(NetworkManagerConfig), } #[derive(SmartDefault, Deserialize, Clone, Debug)] @@ -151,6 +152,14 @@ pub struct InternetTimeConfig { // No update field, update on the, uuh, wtf is metric time } +#[derive(SmartDefault, Deserialize, Clone, Debug)] +#[serde(default)] +pub struct NetworkManagerConfig { + #[default(true)] + pub use_symbols: bool, + // No update field, we listen for events +} + #[derive(Debug, StructOpt)] #[structopt( name = "rustybar", @@ -209,6 +218,9 @@ pub fn launch_tile( TileConfig::SystemTime(c) => tiles::system_time(c, output_chan).await, TileConfig::LocalTime(c) => tiles::local_time(c, output_chan).await, TileConfig::InternetTime(c) => tiles::internet_time(c, output_chan).await, + TileConfig::NetworkManager(c) => { + tiles::network_manager(c, output_chan, dbus_conn).await + } }; match result { Ok(_) => warn!("Tile {} exited without error", tile_id), diff --git a/src/generated.rs b/src/generated.rs index dd0ae94..64caa2c 100644 --- a/src/generated.rs +++ b/src/generated.rs @@ -1,3 +1,4 @@ #![allow(clippy::needless_borrow)] +#![allow(non_snake_case)] include!(concat!(env!("OUT_DIR"), "/generated.rs")); diff --git a/src/tiles/mod.rs b/src/tiles/mod.rs index 07345db..5740a1e 100644 --- a/src/tiles/mod.rs +++ b/src/tiles/mod.rs @@ -5,6 +5,7 @@ pub mod iwd; pub mod load; pub mod local_time; pub mod memory; +pub mod network_manager; pub mod system_time; pub mod utc_time; pub use battery::battery; @@ -14,5 +15,6 @@ pub use iwd::iwd; pub use load::load; pub use local_time::local_time; pub use memory::memory; +pub use network_manager::network_manager; pub use system_time::system_time; pub use utc_time::utc_time; diff --git a/src/tiles/network_manager.rs b/src/tiles/network_manager.rs new file mode 100644 index 0000000..8c9789d --- /dev/null +++ b/src/tiles/network_manager.rs @@ -0,0 +1,46 @@ +use crate::config::NetworkManagerConfig; +use crate::generated::OrgFreedesktopNetworkManager; +use crate::generated::OrgFreedesktopNetworkManagerAccessPoint; +use crate::generated::OrgFreedesktopNetworkManagerConnectionActive; +use crate::output::OutputChannel; +use crate::tile::Block; +use dbus::nonblock::{Proxy, SyncConnection}; +use std::sync::Arc; +use std::time::Duration; + +pub async fn network_manager( + config: NetworkManagerConfig, + output: OutputChannel, + dbus_conn: Arc, +) -> eyre::Result<()> { + let root_proxy = Proxy::new( + "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + Duration::from_secs(5), + dbus_conn.clone(), + ); + + let primary_connection = root_proxy.primary_connection().await?; + let primary_connection_proxy = Proxy::new( + "org.freedesktop.NetworkManager", + primary_connection, + Duration::from_secs(5), + dbus_conn.clone(), + ); + + let specific_object = primary_connection_proxy.specific_object().await?; + let specific_object_proxy = Proxy::new( + "org.freedesktop.NetworkManager", + specific_object, + Duration::from_secs(5), + dbus_conn.clone(), + ); + + let ssid = specific_object_proxy.ssid().await?; + + log::warn!("got ssid: {}", String::from_utf8_lossy(&ssid)); + + loop { + tokio::time::sleep(Duration::from_secs(3600)).await; + } +}