Added cpu temperature and battery

This commit is contained in:
mustafa salih 2021-01-09 03:40:14 +03:00
parent 3fa52e145a
commit 14405523bc
7 changed files with 185 additions and 40 deletions

2
Cargo.lock generated
View File

@ -380,7 +380,7 @@ checksum = "4e1b7878800220a76a08f32c057829511440f65528b63b940f2f2bc145d7ac68"
[[package]]
name = "rsblocks"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"alsa",
"breadx",

View File

@ -1,6 +1,6 @@
[package]
name = "rsblocks"
version = "0.1.4"
version = "0.1.5"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018"
readme = "README.md"

View File

@ -5,7 +5,7 @@
A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
<p>
<img align="center" src="./screenshots/1.png"/>
<img align="center" src="./screenshots/2.png"/>
</p><br/>
## Features
@ -14,7 +14,9 @@ A multi threaded fast status bar for dwm window manager written in **Rust** 🦀
* Memory Usage
* Disk Usage
* Weather Temperature
* Sound volume
* Sound Volume
* Battery Percentage
* Cpu Temperature
* Easy to configure with `rsblocks.yml` file

View File

@ -46,3 +46,15 @@ weather:
format: '%l:+%t'
delay: 7200.0 # 7200 seconds = 2 hours
battery:
source: 'BAT0'
icon: ''
enable: false
delay: 120.0
cpu_temperature:
icon: ''
enable: true
delay: 120.0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

BIN
screenshots/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

@ -1,3 +1,4 @@
use alsa::mixer::{Mixer, SelemChannelId, SelemId};
use breadx::{display::*, window::Window};
use chrono::prelude::*;
use minreq;
@ -7,7 +8,6 @@ use std::io::Read;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use alsa::mixer::{Mixer, SelemChannelId, SelemId};
use yaml_rust::{yaml, YamlLoader};
#[derive(Debug)]
@ -17,6 +17,8 @@ pub enum ThreadsData {
Memory(String),
Time(String),
Weather(String),
Battery(String),
CpuTemp(String),
}
#[derive(Clone)]
@ -27,6 +29,8 @@ pub struct Config {
pub disk: Disk,
pub volume: Volume,
pub weather: Weather,
pub battery: Battery,
pub cpu_temperature: CpuTemp,
}
#[derive(Clone)]
@ -54,7 +58,7 @@ pub struct Volume {
pub icon: String,
pub enabled: bool,
pub delay: f64,
pub card: String
pub card: String,
}
#[derive(Clone)]
@ -66,10 +70,37 @@ pub struct Weather {
pub delay: f64,
}
/*
// TODO: error handling required if rsblocks.yml file is empty
#[derive(Clone)]
pub struct Battery {
pub source: String,
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
this function is responsible to check if the rsblocks.yml file
#[derive(Clone)]
pub struct CpuTemp {
pub icon: String,
pub enabled: bool,
pub delay: f64,
}
/* TODOS
TODO 1: Error handling required if rsblocks.yml file is empty.
TODO 2: This lib file growing and soon it will be annoying to move
arround, need fix soon.
TODO 3: Need a better comments in code, or no one will understand what happens.
TODO 4: Need a documentation.
TODO 5: Fix repeated code for threads in `run` function.
*/
/*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
@ -116,7 +147,7 @@ fn load_defaults() -> Config {
icon: String::from(""),
enabled: false,
delay: 0.17,
card: String::from("ALSA")
card: String::from("ALSA"),
},
weather: Weather {
city: String::from(""),
@ -125,6 +156,17 @@ fn load_defaults() -> Config {
enabled: false,
delay: 7200.0, //7200 seconds = 2 hours
},
battery: Battery {
source: String::from("BAT0"),
icon: String::from(""),
enabled: false,
delay: 120.0,
},
cpu_temperature: CpuTemp {
icon: String::from(""),
enabled: false,
delay: 120.0,
},
}
}
@ -141,6 +183,8 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
let disk_icon = get_or_set_string(doc, "disk", "icon", "");
let volume_icon = get_or_set_string(doc, "volume", "icon", "");
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", "");
//parsing formats and city weather
let time_format = get_or_set_string(doc, "time", "format", "%T");
@ -152,17 +196,24 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
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 battery_enabled = get_or_set_bool(doc, "battery", "enable");
let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable");
// parsing update_delay state (should be all seconds in f64 type)
let time_delay = get_or_set_f32(doc, "time", "delay", 1.0);
let disk_delay = get_or_set_f32(doc, "disk", "delay", 120.0);
let memory_delay = get_or_set_f32(doc, "memory", "delay", 2.0);
let volume_delay = get_or_set_f32(doc, "volume", "delay", 0.17);
let weather_delay = get_or_set_f32(doc, "weather", "delay", 7200.0);
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);
// 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");
Config {
seperator,
time: Time {
@ -184,7 +235,7 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
icon: volume_icon,
enabled: volume_enabled,
delay: volume_delay,
card: volume_card
card: volume_card,
},
weather: Weather {
city: weather_city,
@ -193,11 +244,22 @@ fn parse_config(doc: &yaml::Yaml) -> Config {
enabled: weather_enabled,
delay: weather_delay,
},
battery: Battery {
source: battery_source,
icon: battery_icon,
enabled: battery_enabled,
delay: battery_delay,
},
cpu_temperature: CpuTemp {
icon: cpu_temperature_icon,
enabled: cpu_temperature_enabled,
delay: cpu_temperature_delay,
},
}
}
// getting a f32 value from rsblocks.yml file or set default (last argument)
fn get_or_set_f32(doc: &yaml::Yaml, parent: &str, child: &str, default: f64) -> f64 {
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
} else {
@ -233,9 +295,7 @@ fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &
*/
/* Running the program:
TODO: this is sucks, repeated code in threads below, fix me you fucking asshole
*/
// Running the program:
pub struct Blocks {
disp: Display<name::NameConnection>,
@ -246,10 +306,7 @@ 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,
}
Self { disp, root }
}
}
@ -259,15 +316,11 @@ pub fn run(config: Config, mut blocks: Blocks) {
if config.volume.enabled {
let volume_tx = tx.clone();
let configcp = config.clone();
thread::spawn(move || {
let mut vol_data =
ThreadsData::Sound(get_volume(&configcp));
loop {
let _ = volume_tx.send(vol_data);
vol_data =
ThreadsData::Sound(get_volume(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.volume.delay))
}
let mut vol_data = ThreadsData::Sound(get_volume(&configcp));
thread::spawn(move || loop {
volume_tx.send(vol_data).unwrap();
vol_data = ThreadsData::Sound(get_volume(&configcp));
thread::sleep(Duration::from_secs_f64(configcp.volume.delay))
});
}
@ -309,6 +362,32 @@ pub fn run(config: Config, mut blocks: Blocks) {
});
}
// Battery thread
if config.battery.enabled {
let battery_tx = tx.clone();
let configcp = config.clone();
let battery_data = get_battery(&configcp).unwrap();
let mut battery_data = ThreadsData::Battery(battery_data);
thread::spawn(move || loop {
battery_tx.send(battery_data).unwrap();
battery_data = ThreadsData::Battery(get_battery(&configcp).unwrap());
thread::sleep(Duration::from_secs_f64(configcp.battery.delay))
});
}
// Cpu temperature thread
if config.cpu_temperature.enabled {
let cpu_temp_tx = tx.clone();
let configcp = config.clone();
let cpu_temp_data = get_cpu_temp(&configcp).unwrap();
let mut cpu_temp_data = ThreadsData::CpuTemp(cpu_temp_data);
thread::spawn(move || loop {
cpu_temp_tx.send(cpu_temp_data).unwrap();
cpu_temp_data = ThreadsData::CpuTemp(get_cpu_temp(&configcp).unwrap());
thread::sleep(Duration::from_secs_f64(configcp.cpu_temperature.delay))
});
}
// Time thread
{
let time_tx = tx;
@ -324,7 +403,7 @@ pub fn run(config: Config, mut blocks: Blocks) {
//Main
{
// NOTE: order matters to the final format
let mut bar: Vec<String> = vec!["".to_string(); 5];
let mut bar: Vec<String> = vec!["".to_string(); 7];
//iterating the values recieved from the threads
for data in rx {
match data {
@ -332,7 +411,9 @@ pub fn run(config: Config, mut blocks: Blocks) {
ThreadsData::Weather(x) => bar[1] = x,
ThreadsData::Disk(x) => bar[2] = x,
ThreadsData::Memory(x) => bar[3] = x,
ThreadsData::Time(x) => bar[4] = x,
ThreadsData::CpuTemp(x) => bar[4] = x,
ThreadsData::Battery(x) => bar[5] = x,
ThreadsData::Time(x) => bar[6] = x,
}
// match ends here
@ -399,6 +480,7 @@ fn get_weather(config: &Config) -> String {
format!(" {} {} {}", config.weather.icon, res, config.seperator)
}
// getting disk usage
pub fn get_disk(config: &Config) -> String {
const GB: u64 = (1024 * 1024) * 1024;
let statvfs = nix::sys::statvfs::statvfs("/").unwrap();
@ -408,7 +490,7 @@ pub fn get_disk(config: &Config) -> String {
let available = (statvfs.blocks_free() * statvfs.fragment_size()) / GB;
let used = total - available;
disk_used.push_str(&format!("{}GB", used));
disk_used.push_str(&format!("{}G", used));
format!(
" {} {} {}",
config.disk.icon, disk_used, config.seperator
@ -426,15 +508,15 @@ pub fn get_volume(config: &Config) -> String {
let selem_id = SelemId::new("Master", 0);
let selem = mixer.find_selem(&selem_id).expect("Couldn't find selem");
let selem_chan_id = SelemChannelId::FrontLeft;
let (min, max) = selem.get_playback_volume_range();
let mut raw_volume = selem
.get_playback_volume(selem_chan_id)
.expect("Failed to get raw_volume");
let range = max - min;
let vol = if range == 0 {
0
0
} else {
raw_volume -= min;
((raw_volume as f64 / range as f64) * 100.) as u64
@ -524,3 +606,52 @@ fn assign_val(line: &str, assignable: &mut u32) {
.unwrap();
*assignable = parsed;
}
// getting battery percentage
pub fn get_battery(config: &Config) -> Result<String, Error> {
let battery_full_cap_file = format!(
"/sys/class/power_supply/{}/charge_full_design",
config.battery.source
);
let battery_charge_now_file = format!(
"/sys/class/power_supply/{}/charge_now",
config.battery.source
);
let mut buf = String::new();
// FIXME: ugly error handling AGAIN fixing later, im lazy
match File::open(&battery_full_cap_file) {
Ok(mut file) => file.read_to_string(&mut buf)?,
Err(_) => return Ok(String::from("check your battery source name")),
};
let full_design = buf.trim().parse::<u32>().unwrap();
buf.clear();
// NOTE: no need to error check if passed the above match
File::open(&battery_charge_now_file)?.read_to_string(&mut buf)?;
let charge_now = buf.trim().parse::<u32>().unwrap();
let battery_percentage = (charge_now as f32 / full_design as f32) * 100.0;
let result = format!(
" {} {:.0}% {}",
config.battery.icon, battery_percentage, config.seperator
);
Ok(result)
}
// getting cpu temperature
pub fn get_cpu_temp(config: &Config) -> Result<String, std::io::Error> {
let mut buf = String::new();
File::open("/sys/class/thermal/thermal_zone0/temp")?.read_to_string(&mut buf)?;
let value = buf.trim().parse::<f32>().unwrap();
let result = format!(
" {} {}° {}",
config.cpu_temperature.icon,
value / 1000.0,
config.seperator
);
Ok(result)
}