diff --git a/Cargo.lock b/Cargo.lock index e9de6fa..ff50eb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,16 +194,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3" -[[package]] -name = "dbus" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b1334c0161ddfccd239ac81b188d62015b049c986c5cd0b7f9447cf2c54f4a3" -dependencies = [ - "libc", - "libdbus-sys", -] - [[package]] name = "event-listener" version = "2.5.1" @@ -270,15 +260,6 @@ version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" -[[package]] -name = "libdbus-sys" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc12a3bc971424edbbf7edaf6e5740483444db63aa8e23d3751ff12a30f306f0" -dependencies = [ - "pkg-config", -] - [[package]] name = "linked-hash-map" version = "0.5.3" @@ -416,12 +397,11 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68" [[package]] name = "rsblocks" -version = "0.1.9" +version = "0.1.8" dependencies = [ "alsa", "breadx", "chrono", - "dbus", "minreq", "mpd", "nix 0.19.1", diff --git a/Cargo.toml b/Cargo.toml index 9e38b4b..d80267d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsblocks" -version = "0.1.9" +version = "0.1.8" authors = ["mustafa salih "] edition = "2018" readme = "README.md" @@ -17,7 +17,6 @@ minreq = "2.2.1" alsa = "0.4.3" nix = "0.19.1" mpd = "0.0.12" -dbus = "0.9.1" [dependencies.breadx] version = "0.1.11" diff --git a/README.md b/README.md index 2b5350e..66e532c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀 * Cpu Temperature * Uptime * Mpd Current Song File +* Spotify Current Song * Easy to configure with `rsblocks.yml` file @@ -26,6 +27,16 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀 * This tool is still in development stage. * currently supports only linux. + +## Build Requirements +* [Libdbus](https://dbus.freedesktop.org/releases/dbus/) 1.6 or higher as a spotify requirement + +On Ubuntu, you can do : +```sh +sudo apt install libdbus-1-dev pkg-config +``` + + ## Cargo Installation You can install the binary crate directly ```sh diff --git a/rsblocks.yml b/rsblocks.yml index 65a5569..059016a 100644 --- a/rsblocks.yml +++ b/rsblocks.yml @@ -5,8 +5,7 @@ general: seperator: '┃' - -# Time always running, no enable option for this + time: icon: '' format: '%d %b, %I:%M:%S %p' @@ -14,21 +13,21 @@ time: memory: - icon: '▦' enable: true + icon: '▦' delay: 2.0 disk: - icon: '' enable: true + icon: '' delay: 120.0 battery: + source: 'BAT0' icon: '' enable: false - source: 'BAT0' delay: 120.0 @@ -46,19 +45,15 @@ mpd: delay: 5.0 +# reads from alsa, alsa-utils package should +# be installed on the system to make it work. volume: - icon: '' enable: true + icon: '' delay: 0.18 card: 'PULSE' -spotify: - icon: '' - enable: false - delay: 0.5 - - # weather format options is available on # https://github.com/chubin/wttr.in # NOTES: diff --git a/src/lib.rs b/src/lib.rs index c03c954..d417164 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,32 +1,6 @@ -//! `rsblocks` A multi threaded status bar for dwm window manager. - -/* - - Licence: MIT - - - Author: Mustafa Salih - [MustafaSalih](https://github.com/MustafaSalih1993) - - - Contributers: - - - AdaShoelace - for her/his contributions. - [AdaShoelace](https://github.com/AdaShoelace) - - - Thanks to: - - wttr.in - for using their weather API. - [wttr.in](https://github.com/chubin/wttr.in) - - -*/ - use alsa::mixer::{Mixer, SelemChannelId, SelemId}; use breadx::{display::*, window::Window}; use chrono::prelude::*; -use dbus::blocking::stdintf::org_freedesktop_dbus::Properties; -use dbus::{arg, blocking::Connection}; use mpd::{Client, Song}; use std::fs::File; use std::io::Error; @@ -47,7 +21,6 @@ pub enum ThreadsData { Battery(String), CpuTemp(String), Uptime(String), - Spotify(String), } #[derive(Clone)] @@ -62,7 +35,6 @@ pub struct Config { pub cpu_temperature: CpuTemp, pub uptime: Uptime, pub mpd: Mpd, - pub spotify: Spotify, } #[derive(Clone)] @@ -132,33 +104,6 @@ pub struct Mpd { pub enabled: bool, pub delay: f64, } - -#[derive(Clone)] -pub struct Spotify { - pub icon: String, - pub enabled: bool, - pub delay: f64, -} - -pub struct Blocks { - disp: Display, - root: Window, -} - -impl Blocks { - pub fn new() -> Self { - let disp = Display::create(None, None).expect("Failed to create x11 connection"); - let root = disp.default_screen().root; - Self { disp, root } - } -} - -impl Default for Blocks { - fn default() -> Self { - Self::new() - } -} - /* TODOS TODO 1: Error handling required if rsblocks.yml file is empty. @@ -172,12 +117,11 @@ impl Default for Blocks { TODO 5: Fix repeated code for threads in `run` function. - TODO 6: Getting song metadata by a format provided by the user. + TODO 6: Getting song metadata from mpd by a format provided by the user. */ -/* -this function is responsible to check if the rsblocks.yml file +/*this function is responsible to check if the rsblocks.yml file exists to call parse_config to read it OTHERWISE it will call load_defaults to load a hardcoded default configuration @@ -256,80 +200,62 @@ fn load_defaults() -> Config { enabled: false, delay: 15.0, }, - spotify: Spotify { - icon: String::from(""), - enabled: false, - delay: 15.0, - }, } } /* it will read and parse the rsblocks.yml file content and return a valid configuration IF some content is missing in the rsblocks.yml file, it will set -a default values to that. - -NOTE: (get_or_set) functions job below getting the values from the configuration doc IF - a value is not exist in the config it will SET a value givin in the last argument. +a default values to that */ fn parse_config(doc: &yaml::Yaml) -> Config { + // parsing icons and set default if not exist in the config file let seperator = get_or_set_string(doc, "general", "seperator", "|"); - - // time values let time_icon = get_or_set_string(doc, "time", "icon", ""); - let time_format = get_or_set_string(doc, "time", "format", "%T"); - let time_delay = get_or_set_f64(doc, "time", "delay", 1.0); - - // memory values let mem_icon = get_or_set_string(doc, "memory", "icon", ""); - let memory_enabled = get_or_set_bool(doc, "memory", "enable"); - let memory_delay = get_or_set_f64(doc, "memory", "delay", 2.0); - - //disk values let disk_icon = get_or_set_string(doc, "disk", "icon", ""); - let disk_enabled = get_or_set_bool(doc, "disk", "enable"); - let disk_delay = get_or_set_f64(doc, "disk", "delay", 120.0); - - // volume values let volume_icon = get_or_set_string(doc, "volume", "icon", ""); - let volume_enabled = get_or_set_bool(doc, "volume", "enable"); - let volume_delay = get_or_set_f64(doc, "volume", "delay", 0.17); - let volume_card = get_or_set_string(doc, "volume", "card", "ALSA"); - - // weather values let weather_icon = get_or_set_string(doc, "weather", "icon", ""); + let battery_icon = get_or_set_string(doc, "battery", "icon", ""); + let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", ""); + let uptime_icon = get_or_set_string(doc, "uptime", "icon", ""); + let mpd_icon = get_or_set_string(doc, "mpd", "icon", ""); + + //parsing formats and city weather + let time_format = get_or_set_string(doc, "time", "format", "%T"); let weather_format = get_or_set_string(doc, "weather", "format", "%l:+%t"); let weather_city = get_or_set_string(doc, "weather", "city", ""); + + // parsing enabled state (everything false by default) + let disk_enabled = get_or_set_bool(doc, "disk", "enable"); + let memory_enabled = get_or_set_bool(doc, "memory", "enable"); + let volume_enabled = get_or_set_bool(doc, "volume", "enable"); let weather_enabled = get_or_set_bool(doc, "weather", "enable"); - let weather_delay = get_or_set_f64(doc, "weather", "delay", 7200.0); - - // battery values - let battery_icon = get_or_set_string(doc, "battery", "icon", ""); let battery_enabled = get_or_set_bool(doc, "battery", "enable"); - let battery_source = get_or_set_string(doc, "battery", "source", "BAT0"); - let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.0); - - // cpu values - let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", ""); let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable"); - let cpu_temperature_delay = get_or_set_f64(doc, "cpu_temperature", "delay", 120.0); - - // uptime values - let uptime_icon = get_or_set_string(doc, "uptime", "icon", ""); let uptime_enabled = get_or_set_bool(doc, "uptime", "enable"); - let uptime_delay = get_or_set_f64(doc, "uptime", "delay", 60.0); - - // mpd values - let mpd_icon = get_or_set_string(doc, "mpd", "icon", ""); - let mpd_host = get_or_set_string(doc, "mpd", "host", "127.0.0.1"); - let mpd_port = get_or_set_string(doc, "mpd", "port", "6600"); let mpd_enabled = get_or_set_bool(doc, "mpd", "enable"); + + // parsing update_delay state (should be all seconds in f64 type) + let time_delay = get_or_set_f64(doc, "time", "delay", 1.0); + let disk_delay = get_or_set_f64(doc, "disk", "delay", 120.0); + let memory_delay = get_or_set_f64(doc, "memory", "delay", 2.0); + let volume_delay = get_or_set_f64(doc, "volume", "delay", 0.17); + let weather_delay = get_or_set_f64(doc, "weather", "delay", 7200.0); + let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.0); + let cpu_temperature_delay = get_or_set_f64(doc, "cpu_temperature", "delay", 120.0); + let uptime_delay = get_or_set_f64(doc, "uptime", "delay", 60.0); let mpd_delay = get_or_set_f64(doc, "mpd", "delay", 15.0); - //spotify values - let spotify_icon = get_or_set_string(doc, "spotify", "icon", ""); - let spotify_enabled = get_or_set_bool(doc, "spotify", "enable"); - let spotify_delay = get_or_set_f64(doc, "spotify", "delay", 10.0); + // parsing card for volume, ALSA or PULSE + let volume_card = get_or_set_string(doc, "volume", "card", "ALSA"); + + // parsing battery source name + let battery_source = get_or_set_string(doc, "battery", "source", "BAT0"); + + //parsing mpd host and port + let mpd_host = get_or_set_string(doc, "mpd", "host", "127.0.0.1"); + let mpd_port = get_or_set_string(doc, "mpd", "port", "6600"); Config { seperator, @@ -384,15 +310,10 @@ fn parse_config(doc: &yaml::Yaml) -> Config { enabled: mpd_enabled, delay: mpd_delay, }, - spotify: Spotify { - icon: spotify_icon, - enabled: spotify_enabled, - delay: spotify_delay, - }, } } -// getting a f64 value from rsblocks.yml file or set default (last argument) +// getting a f32 value from rsblocks.yml file or set default (last argument) fn get_or_set_f64(doc: &yaml::Yaml, parent: &str, child: &str, default: f64) -> f64 { let val: f64 = if doc[parent][child].is_badvalue() { default @@ -431,21 +352,28 @@ fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: & // Running the program: +pub struct Blocks { + disp: Display, + root: Window, +} + +impl Blocks { + pub fn new() -> Self { + let disp = Display::create(None, None).expect("Failed to create x11 connection"); + let root = disp.default_screen().root; + Self { disp, root } + } +} + +impl Default for Blocks { + fn default() -> Self { + Self::new() + } +} + pub fn run(config: Config, mut blocks: Blocks) { let (tx, rx) = mpsc::channel(); - // spotify thread - if config.spotify.enabled { - let spotify_tx = tx.clone(); - let configcp = config.clone(); - let mut spotify_data = ThreadsData::Spotify(get_spotify(&configcp)); - thread::spawn(move || loop { - spotify_tx.send(spotify_data).unwrap(); - spotify_data = ThreadsData::Spotify(get_spotify(&configcp)); - thread::sleep(Duration::from_secs_f64(configcp.spotify.delay)) - }); - } - // mpd thread if config.mpd.enabled { let mpd_tx = tx.clone(); @@ -546,7 +474,6 @@ pub fn run(config: Config, mut blocks: Blocks) { thread::sleep(Duration::from_secs_f64(configcp.uptime.delay)) }); } - // Time thread { let time_tx = tx; @@ -562,20 +489,19 @@ pub fn run(config: Config, mut blocks: Blocks) { //Main { // NOTE: order matters to the final format - let mut bar: Vec = vec!["".to_string(); 10]; + let mut bar: Vec = vec!["".to_string(); 9]; //iterating the values recieved from the threads for data in rx { match data { - ThreadsData::Spotify(x) => bar[0] = x, - ThreadsData::Mpd(x) => bar[1] = x, - ThreadsData::Sound(x) => bar[2] = x, - ThreadsData::Weather(x) => bar[3] = x, - ThreadsData::Disk(x) => bar[4] = x, - ThreadsData::Memory(x) => bar[5] = x, - ThreadsData::CpuTemp(x) => bar[6] = x, - ThreadsData::Battery(x) => bar[7] = x, - ThreadsData::Uptime(x) => bar[8] = x, - ThreadsData::Time(x) => bar[9] = x, + ThreadsData::Mpd(x) => bar[0] = x, + ThreadsData::Sound(x) => bar[1] = x, + ThreadsData::Weather(x) => bar[2] = x, + ThreadsData::Disk(x) => bar[3] = x, + ThreadsData::Memory(x) => bar[4] = x, + ThreadsData::CpuTemp(x) => bar[5] = x, + ThreadsData::Battery(x) => bar[6] = x, + ThreadsData::Uptime(x) => bar[7] = x, + ThreadsData::Time(x) => bar[8] = x, } // match ends here @@ -585,6 +511,7 @@ pub fn run(config: Config, mut blocks: Blocks) { } pub fn update(bar: &[String], blocks: &mut Blocks) { + // TODO: FIX ME, this solution sucks let mut x = String::new(); for i in bar.iter() { x.push_str(i.as_str()); @@ -617,7 +544,10 @@ pub fn get_time(config: &Config) -> String { ) } -// will make a GET request from wttr.in +/* +CREDIT: thanks for wttr.in to use their API +will make a GET request from wttr.in +*/ fn get_weather(config: &Config) -> String { let format = if config.weather.format.is_empty() { String::from("%l:+%t") @@ -655,7 +585,6 @@ pub fn get_disk(config: &Config) -> String { ) } -// getting volume percentage pub fn get_volume(config: &Config) -> String { let card = if config.volume.card == "PULSE" { "pulse" @@ -834,21 +763,19 @@ pub fn get_uptime(config: &Config) -> Result { Ok(result) } -// yes, error handling looks fucking sucks! // getting mpd song file pub fn get_mpd_current(config: &Config) -> String { let stream_path = format!("{}:{}", config.mpd.host, config.mpd.port); - let empty_string = String::from(""); let mut conn = match Client::connect(&stream_path) { Ok(connection) => connection, - _ => return empty_string, + _ => return String::from(""), }; let current: Song = match conn.currentsong() { Ok(opt) => match opt { Some(song) => song, - _ => return empty_string, + _ => return String::from(""), }, - _ => return empty_string, + _ => return String::from(""), }; let result = format!( @@ -858,44 +785,3 @@ pub fn get_mpd_current(config: &Config) -> String { result } - -// getting spotify current artist and title. -// FIXME: I know im lazy asshole, this error handling looks ugly, i dont like it too, need to fix soon. -fn get_spotify(config: &Config) -> String { - let conn = match Connection::new_session() { - Ok(conn) => conn, - _ => return String::from(""), - }; - - let p = conn.with_proxy( - "org.mpris.MediaPlayer2.spotify", - "/org/mpris/MediaPlayer2", - Duration::from_millis(5000), - ); - - let metadata: arg::PropMap = match p.get("org.mpris.MediaPlayer2.Player", "Metadata") { - Ok(data) => data, - _ => return String::from(""), - }; - - let title: Option<&String> = arg::prop_cast(&metadata, "xesam:title"); - let artist: Option<&Vec> = arg::prop_cast(&metadata, "xesam:artist"); - - let title = match title { - Some(title) => title, - _ => "", - }; - - let artist = match artist { - Some(artist_vec) => match artist_vec.first() { - Some(name) => name, - _ => "", - }, - None => "", - }; - - format!( - " {} {} - {} {}", - config.spotify.icon, artist, title, config.seperator - ) -}