rsblocks now support mpd

This commit is contained in:
mustafa salih 2021-01-11 05:37:56 +03:00
parent 21d03ad9d0
commit 5e993cbd82
7 changed files with 126 additions and 25 deletions

26
Cargo.lock generated
View File

@ -130,6 +130,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "bufstream"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40e38929add23cdf8a366df9b0e088953150724bcbe5fc330b0d8eb3b328eec8"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.4.1" version = "1.4.1"
@ -281,6 +287,17 @@ version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2466d0a7e6bfcd54f69e4a17d4a4318985aaaf7fe3df4cd3b6f11ff551129ca3" checksum = "2466d0a7e6bfcd54f69e4a17d4a4318985aaaf7fe3df4cd3b6f11ff551129ca3"
[[package]]
name = "mpd"
version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a20784da57fa01bf7910a5da686d9f39ff37feaa774856b71f050e4331bf82"
dependencies = [
"bufstream",
"rustc-serialize",
"time",
]
[[package]] [[package]]
name = "nb-connect" name = "nb-connect"
version = "1.0.2" version = "1.0.2"
@ -380,16 +397,23 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68"
[[package]] [[package]]
name = "rsblocks" name = "rsblocks"
version = "0.1.5" version = "0.1.6"
dependencies = [ dependencies = [
"alsa", "alsa",
"breadx", "breadx",
"chrono", "chrono",
"minreq", "minreq",
"mpd",
"nix 0.19.1", "nix 0.19.1",
"yaml-rust", "yaml-rust",
] ]
[[package]]
name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rsblocks" name = "rsblocks"
version = "0.1.5" version = "0.1.6"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"] authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -16,6 +16,7 @@ yaml-rust = "0.4"
minreq = "2.2.1" minreq = "2.2.1"
alsa = "0.4.3" alsa = "0.4.3"
nix = "0.19.1" nix = "0.19.1"
mpd = "0.0.12"
[dependencies.breadx] [dependencies.breadx]
version = "0.1.11" version = "0.1.11"

View File

@ -5,7 +5,7 @@
A multi threaded fast status bar for dwm window manager written in **Rust** 🦀 A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
<p> <p>
<img align="center" src="./screenshots/2.png"/> <img align="center" src="./screenshots/1.png"/>
</p><br/> </p><br/>
## Features ## Features
@ -18,6 +18,7 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
* Battery Percentage * Battery Percentage
* Cpu Temperature * Cpu Temperature
* Uptime * Uptime
* Mpd Current Song File
* Easy to configure with `rsblocks.yml` file * Easy to configure with `rsblocks.yml` file

View File

@ -24,6 +24,27 @@ disk:
delay: 120.0 delay: 120.0
battery:
source: 'BAT0'
icon: ''
enable: false
delay: 120.0
cpu_temperature:
icon: ''
enable: true
delay: 120.0
mpd:
icon: ''
host: '127.0.0.1'
port: '6600'
enable: false
delay: 5.0
# reads from alsa, alsa-utils package should # reads from alsa, alsa-utils package should
# be installed on the system to make it work. # be installed on the system to make it work.
volume: volume:
@ -45,16 +66,3 @@ weather:
city: '' city: ''
format: '%l:+%t' format: '%l:+%t'
delay: 7200.0 # 7200 seconds = 2 hours delay: 7200.0 # 7200 seconds = 2 hours
battery:
source: 'BAT0'
icon: ''
enable: false
delay: 120.0
cpu_temperature:
icon: ''
enable: true
delay: 120.0

BIN
screenshots/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.6 KiB

View File

@ -1,6 +1,7 @@
use alsa::mixer::{Mixer, SelemChannelId, SelemId}; use alsa::mixer::{Mixer, SelemChannelId, SelemId};
use breadx::{display::*, window::Window}; use breadx::{display::*, window::Window};
use chrono::prelude::*; use chrono::prelude::*;
use mpd::{Client, Song};
use std::fs::File; use std::fs::File;
use std::io::Error; use std::io::Error;
use std::io::Read; use std::io::Read;
@ -11,6 +12,7 @@ use yaml_rust::{yaml, YamlLoader};
#[derive(Debug)] #[derive(Debug)]
pub enum ThreadsData { pub enum ThreadsData {
Mpd(String),
Sound(String), Sound(String),
Disk(String), Disk(String),
Memory(String), Memory(String),
@ -32,6 +34,7 @@ pub struct Config {
pub battery: Battery, pub battery: Battery,
pub cpu_temperature: CpuTemp, pub cpu_temperature: CpuTemp,
pub uptime: Uptime, pub uptime: Uptime,
pub mpd: Mpd,
} }
#[derive(Clone)] #[derive(Clone)]
@ -92,6 +95,15 @@ pub struct Uptime {
pub enabled: bool, pub enabled: bool,
pub delay: f64, pub delay: f64,
} }
#[derive(Clone)]
pub struct Mpd {
pub icon: String,
pub host: String,
pub port: String,
pub enabled: bool,
pub delay: f64,
}
/* TODOS /* TODOS
TODO 1: Error handling required if rsblocks.yml file is empty. TODO 1: Error handling required if rsblocks.yml file is empty.
@ -105,6 +117,8 @@ pub struct Uptime {
TODO 5: Fix repeated code for threads in `run` function. TODO 5: Fix repeated code for threads in `run` function.
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
@ -179,6 +193,13 @@ fn load_defaults() -> Config {
enabled: false, enabled: false,
delay: 60.0, delay: 60.0,
}, },
mpd: Mpd {
icon: String::from(""),
host: String::from("127.0.0.1"),
port: String::from("6600"),
enabled: false,
delay: 15.0,
},
} }
} }
@ -198,6 +219,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let battery_icon = get_or_set_string(doc, "battery", "icon", ""); let battery_icon = get_or_set_string(doc, "battery", "icon", "");
let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", ""); let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", "");
let uptime_icon = get_or_set_string(doc, "uptime", "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 //parsing formats and city weather
let time_format = get_or_set_string(doc, "time", "format", "%T"); let time_format = get_or_set_string(doc, "time", "format", "%T");
@ -212,6 +234,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let battery_enabled = get_or_set_bool(doc, "battery", "enable"); let battery_enabled = get_or_set_bool(doc, "battery", "enable");
let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable"); let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable");
let uptime_enabled = get_or_set_bool(doc, "uptime", "enable"); let uptime_enabled = get_or_set_bool(doc, "uptime", "enable");
let mpd_enabled = get_or_set_bool(doc, "mpd", "enable");
// parsing update_delay state (should be all seconds in f64 type) // parsing update_delay state (should be all seconds in f64 type)
let time_delay = get_or_set_f64(doc, "time", "delay", 1.0); let time_delay = get_or_set_f64(doc, "time", "delay", 1.0);
@ -222,6 +245,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.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 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 uptime_delay = get_or_set_f64(doc, "uptime", "delay", 60.0);
let mpd_delay = get_or_set_f64(doc, "mpd", "delay", 15.0);
// parsing card for volume, ALSA or PULSE // parsing card for volume, ALSA or PULSE
let volume_card = get_or_set_string(doc, "volume", "card", "ALSA"); let volume_card = get_or_set_string(doc, "volume", "card", "ALSA");
@ -229,6 +253,10 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
// parsing battery source name // parsing battery source name
let battery_source = get_or_set_string(doc, "battery", "source", "BAT0"); 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 { Config {
seperator, seperator,
time: Time { time: Time {
@ -275,6 +303,13 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
enabled: uptime_enabled, enabled: uptime_enabled,
delay: uptime_delay, delay: uptime_delay,
}, },
mpd: Mpd {
icon: mpd_icon,
host: mpd_host,
port: mpd_port,
enabled: mpd_enabled,
delay: mpd_delay,
},
} }
} }
@ -338,6 +373,19 @@ impl Default for Blocks {
pub fn run(config: Config, mut blocks: Blocks) { pub fn run(config: Config, mut blocks: Blocks) {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
// mpd thread
if config.mpd.enabled {
let mpd_tx = tx.clone();
let configcp = config.clone();
let mut mpd_data = ThreadsData::Mpd(get_mpd_current(&configcp));
thread::spawn(move || loop {
mpd_tx.send(mpd_data).unwrap();
mpd_data = ThreadsData::Mpd(get_mpd_current(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.mpd.delay))
});
}
// volume thread // volume thread
if config.volume.enabled { if config.volume.enabled {
let volume_tx = tx.clone(); let volume_tx = tx.clone();
@ -441,18 +489,19 @@ pub fn run(config: Config, mut blocks: Blocks) {
//Main //Main
{ {
// NOTE: order matters to the final format // NOTE: order matters to the final format
let mut bar: Vec<String> = vec!["".to_string(); 8]; let mut bar: Vec<String> = vec!["".to_string(); 9];
//iterating the values recieved from the threads //iterating the values recieved from the threads
for data in rx { for data in rx {
match data { match data {
ThreadsData::Sound(x) => bar[0] = x, ThreadsData::Mpd(x) => bar[0] = x,
ThreadsData::Weather(x) => bar[1] = x, ThreadsData::Sound(x) => bar[1] = x,
ThreadsData::Disk(x) => bar[2] = x, ThreadsData::Weather(x) => bar[2] = x,
ThreadsData::Memory(x) => bar[3] = x, ThreadsData::Disk(x) => bar[3] = x,
ThreadsData::CpuTemp(x) => bar[4] = x, ThreadsData::Memory(x) => bar[4] = x,
ThreadsData::Battery(x) => bar[5] = x, ThreadsData::CpuTemp(x) => bar[5] = x,
ThreadsData::Uptime(x) => bar[6] = x, ThreadsData::Battery(x) => bar[6] = x,
ThreadsData::Time(x) => bar[7] = x, ThreadsData::Uptime(x) => bar[7] = x,
ThreadsData::Time(x) => bar[8] = x,
} }
// match ends here // match ends here
@ -713,3 +762,21 @@ pub fn get_uptime(config: &Config) -> Result<String, std::io::Error> {
let result = format!(" {} {} {}", config.uptime.icon, uptime, config.seperator); let result = format!(" {} {} {}", config.uptime.icon, uptime, config.seperator);
Ok(result) Ok(result)
} }
// getting mpd song file
pub fn get_mpd_current(config: &Config) -> String {
let stream_path = format!("{}:{}", config.mpd.host, config.mpd.port);
let mut conn = match Client::connect(&stream_path) {
Ok(connection) => connection,
_ => return String::from(""),
};
conn.volume(100).unwrap();
let current: Song = conn.currentsong().unwrap().unwrap();
let result = format!(
" {} {} {}",
config.mpd.icon, current.file, config.seperator
);
result
}