diff --git a/src/config.rs b/src/config.rs index 3fc7eae..74da8a1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use crate::tile::Block; use crate::tiles; +use dbus::nonblock::SyncConnection; use futures::{stream::BoxStream, Stream}; use serde::{Deserialize, Deserializer}; use smart_default::SmartDefault; @@ -7,6 +8,7 @@ use std::env::var; use std::error::Error; use std::io; use std::path::PathBuf; +use std::sync::Arc; use structopt::StructOpt; use tokio::fs::File; use tokio::prelude::*; @@ -113,11 +115,12 @@ pub async fn read_config() -> Result> { pub fn process_tile( tile: &TileConfig, + connection: &Arc, ) -> BoxStream<'static, Result>> { let five_secs = Duration::from_secs(5); match &tile.config_type { TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))), - TileConfigType::Hostname => wrap(tiles::hostname_stream(), tile.update), + TileConfigType::Hostname => wrap(tiles::hostname_stream(connection.clone()), tile.update), TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))), TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))), TileConfigType::Time(c) => wrap(tiles::time_stream(c.clone()), tile.update), diff --git a/src/main.rs b/src/main.rs index 4b0f33d..32d9b22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod output; mod tile; mod tiles; -use dbus_tokio::connection::new_session_sync; +use dbus_tokio::connection::new_system_sync; use futures::channel::mpsc::{channel, Sender}; use futures::{stream::BoxStream, StreamExt}; use std::fmt::Debug; @@ -16,7 +16,7 @@ async fn main() -> Result<(), Box> { let config = config::read_config().await?; // We can't do much until we have a D-Bus connection so just do it synchronously - let (resource, _conn) = new_session_sync()?; + let (resource, conn) = new_system_sync()?; // Now start listening on our D-Bus connection tokio::spawn(async { @@ -29,7 +29,7 @@ async fn main() -> Result<(), Box> { let tiles: Vec<_> = config .tile .iter() - .map(config::process_tile) + .map(|tile| config::process_tile(tile, &conn)) .enumerate() .map(|(index, stream)| spawn_stream(index, stream, sender.clone())) .collect(); diff --git a/src/tiles/hostname.rs b/src/tiles/hostname.rs index 5b0e9fa..2258c8d 100644 --- a/src/tiles/hostname.rs +++ b/src/tiles/hostname.rs @@ -1,22 +1,28 @@ use crate::tile::Block; -use futures::stream; -use futures::Stream; -use tokio::fs::File; -use tokio::prelude::*; +use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties; +use dbus::nonblock::{Proxy, SyncConnection}; +use futures::{FutureExt, Stream}; +use std::sync::Arc; +use std::time::Duration; pub fn hostname_stream( + connection: Arc, ) -> impl Stream>> { - stream::once(async { - let mut raw = String::new(); - File::open("/proc/sys/kernel/hostname") - .await? - .read_to_string(&mut raw) - .await?; + let proxy = Proxy::new( + "org.freedesktop.hostname1", + "/org/freedesktop/hostname1", + Duration::from_secs(5), + connection, + ); + let reply = proxy.get("org.freedesktop.hostname1", "Hostname"); + async { + let hostname: String = reply.await?; let block = Block { - full_text: raw.trim_end_matches('\n').into(), + full_text: hostname.into(), name: "hostname".into(), ..Default::default() }; Ok(block) - }) + } + .into_stream() }