More towards handling click events

main
Tassilo Horn 3 years ago
parent f92586630d
commit 4d4937d292
  1. 25
      src/bar.rs
  2. 2
      src/bar/module.rs
  3. 30
      src/bar/module/battery.rs
  4. 42
      src/bar/module/sysinfo.rs

@ -18,7 +18,9 @@
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use env_logger::Env; use env_logger::Env;
use serde_json; use serde_json;
use std::thread; use serde_json::Deserializer;
use std::{sync::Arc, thread};
use swaybar_types as sbt;
pub mod config; pub mod config;
pub mod module; pub mod module;
@ -29,8 +31,7 @@ pub fn start() {
let config = config::Config::default(); let config = config::Config::default();
thread::spawn(handle_input); let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(vec![
let mods: Vec<Box<dyn BarModuleFn>> = vec![
module::window::BarModuleWindow::create( module::window::BarModuleWindow::create(
module::window::BarModuleWindow::default_config("0".to_owned()), module::window::BarModuleWindow::default_config("0".to_owned()),
), ),
@ -43,16 +44,28 @@ pub fn start() {
module::date::BarModuleDate::create( module::date::BarModuleDate::create(
module::date::BarModuleDate::default_config("0".to_owned()), module::date::BarModuleDate::default_config("0".to_owned()),
), ),
]; ]);
let mods_for_input = mods.clone();
thread::spawn(move || handle_input(mods_for_input));
generate_status(&mods, config.refresh_interval); generate_status(&mods, config.refresh_interval);
} }
pub fn handle_input() { pub fn handle_input(mods: Arc<Vec<Box<dyn BarModuleFn>>>) {
// TODO: Read stdin and react to click events. // TODO: Read stdin and react to click events.
// let stream =
// Deserializer::from_reader(std::io::stdin()).into_iter::<sbt::Click>();
// for click in stream {
// log::debug!("Click received: {:?}", click);
// }
// let lines = std::io::stdin().lock();
// for l in lines {
// log::debug!("{}", l);
// }
} }
pub fn generate_status(mods: &[Box<dyn BarModuleFn>], refresh_interval: u64) { pub fn generate_status(mods: &[Box<dyn BarModuleFn>], refresh_interval: u64) {
println!("{{\"version\": 1}}"); println!("{{\"version\": 1, \"click_events\": true}}");
// status_command should output an infinite array meaning we emit an // status_command should output an infinite array meaning we emit an
// opening [ and never the closing bracket. // opening [ and never the closing bracket.
println!("["); println!("[");

@ -21,7 +21,7 @@ pub mod date;
pub mod sysinfo; pub mod sysinfo;
pub mod window; pub mod window;
pub trait BarModuleFn { pub trait BarModuleFn: Sync + Send {
fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn>
where where
Self: Sized; Self: Sized;

@ -19,7 +19,6 @@ use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace; use crate::fmt_replace::fmt_replace;
use battery as bat; use battery as bat;
use std::cell::RefCell;
use std::collections::HashSet; use std::collections::HashSet;
use swaybar_types as s; use swaybar_types as s;
@ -27,18 +26,15 @@ const NAME: &str = "battery";
pub struct BarModuleBattery { pub struct BarModuleBattery {
config: config::ModuleConfig, config: config::ModuleConfig,
manager: RefCell<bat::Manager>,
} }
fn get_refreshed_batteries( fn get_refreshed_batteries(
manager: &RefCell<bat::Manager>, manager: &bat::Manager,
) -> Result<Vec<bat::Battery>, bat::Error> { ) -> Result<Vec<bat::Battery>, bat::Error> {
let m = manager.borrow();
let mut bats = vec![]; let mut bats = vec![];
for bat in m.batteries()? { for bat in manager.batteries()? {
let mut bat = bat?; let mut bat = bat?;
if m.refresh(&mut bat).is_ok() { if manager.refresh(&mut bat).is_ok() {
bats.push(bat); bats.push(bat);
} }
} }
@ -46,11 +42,12 @@ fn get_refreshed_batteries(
Ok(bats) Ok(bats)
} }
fn get_text( fn get_text(cfg: &config::ModuleConfig) -> String {
manager: &RefCell<bat::Manager>, // FIXME: Creating the Manager on every refresh is bad but internally
cfg: &config::ModuleConfig, // it uses an Rc so if I keep it as a field of BarModuleBattery, that
) -> String { // cannot be Sync.
match get_refreshed_batteries(manager) { let manager = battery::Manager::new().unwrap();
match get_refreshed_batteries(&manager) {
Ok(bats) => { Ok(bats) => {
if bats.is_empty() { if bats.is_empty() {
return String::new(); return String::new();
@ -94,12 +91,7 @@ fn get_text(
impl BarModuleFn for BarModuleBattery { impl BarModuleFn for BarModuleBattery {
fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> { fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleBattery { Box::new(BarModuleBattery { config })
config,
manager: RefCell::new(
bat::Manager::new().expect("Could not create Manager"),
),
})
} }
fn default_config(instance: String) -> config::ModuleConfig { fn default_config(instance: String) -> config::ModuleConfig {
@ -120,7 +112,7 @@ impl BarModuleFn for BarModuleBattery {
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {
let text = get_text(&self.manager, &self.config); let text = get_text(&self.config);
s::Block { s::Block {
name: Some(Self::name().to_owned()), name: Some(Self::name().to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),

@ -18,7 +18,7 @@
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace; use crate::fmt_replace::fmt_replace;
use std::cell::RefCell; use std::sync::Mutex;
use std::sync::Once; use std::sync::Once;
use swaybar_types as s; use swaybar_types as s;
use sysinfo as si; use sysinfo as si;
@ -29,7 +29,7 @@ const NAME: &str = "sysinfo";
pub struct BarModuleSysInfo { pub struct BarModuleSysInfo {
config: config::ModuleConfig, config: config::ModuleConfig,
system: RefCell<si::System>, system: Mutex<si::System>,
} }
struct OnceRefresher { struct OnceRefresher {
@ -45,23 +45,22 @@ impl OnceRefresher {
} }
} }
fn refresh_cpu(&self, sys: &RefCell<si::System>) { fn refresh_cpu(&self, sys: &mut si::System) {
self.cpu.call_once(|| sys.borrow_mut().refresh_cpu()); self.cpu.call_once(|| sys.refresh_cpu());
} }
fn refresh_memory(&self, sys: &RefCell<si::System>) { fn refresh_memory(&self, sys: &mut si::System) {
self.memory.call_once(|| sys.borrow_mut().refresh_memory()); self.memory.call_once(|| sys.refresh_memory());
} }
} }
fn get_cpu_usage(sys: &RefCell<si::System>, upd: &OnceRefresher) -> f32 { fn get_cpu_usage(sys: &mut si::System, upd: &OnceRefresher) -> f32 {
upd.refresh_cpu(sys); upd.refresh_cpu(sys);
sys.borrow().global_processor_info().cpu_usage() sys.global_processor_info().cpu_usage()
} }
fn get_memory_usage(sys: &RefCell<si::System>, upd: &OnceRefresher) -> f64 { fn get_memory_usage(sys: &mut si::System, upd: &OnceRefresher) -> f64 {
upd.refresh_memory(sys); upd.refresh_memory(sys);
let sys = sys.borrow();
sys.used_memory() as f64 * 100_f64 / sys.total_memory() as f64 sys.used_memory() as f64 * 100_f64 / sys.total_memory() as f64
} }
@ -73,12 +72,12 @@ enum LoadAvg {
} }
fn get_load_average( fn get_load_average(
sys: &RefCell<si::System>, sys: &mut si::System,
avg: LoadAvg, avg: LoadAvg,
upd: &OnceRefresher, upd: &OnceRefresher,
) -> f64 { ) -> f64 {
upd.refresh_cpu(sys); upd.refresh_cpu(sys);
let load_avg = sys.borrow().load_average(); let load_avg = sys.load_average();
match avg { match avg {
LoadAvg::One => load_avg.one, LoadAvg::One => load_avg.one,
LoadAvg::Five => load_avg.five, LoadAvg::Five => load_avg.five,
@ -90,7 +89,7 @@ impl BarModuleFn for BarModuleSysInfo {
fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> { fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleSysInfo { Box::new(BarModuleSysInfo {
config, config,
system: RefCell::new(si::System::new_all()), system: Mutex::new(si::System::new_all()),
}) })
} }
@ -107,16 +106,19 @@ impl BarModuleFn for BarModuleSysInfo {
s::Block { s::Block {
name: Some(Self::name().to_owned()), name: Some(Self::name().to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),
full_text: fmt_replace!(&self.config.format, self.config.html_escape, { full_text: {
"cpu_usage" => get_cpu_usage(&self.system, &updater), let mut sys = self.system.lock().unwrap();
"mem_usage" => get_memory_usage(&self.system, &updater), fmt_replace!(&self.config.format, self.config.html_escape, {
"load_avg_1" => get_load_average(&self.system, "cpu_usage" => get_cpu_usage(&mut sys, &updater),
"mem_usage" => get_memory_usage(&mut sys, &updater),
"load_avg_1" => get_load_average(&mut sys,
LoadAvg::One, &updater), LoadAvg::One, &updater),
"load_avg_5" => get_load_average(&self.system, "load_avg_5" => get_load_average(&mut sys,
LoadAvg::Five, &updater), LoadAvg::Five, &updater),
"load_avg_15" => get_load_average(&self.system, "load_avg_15" => get_load_average(&mut sys,
LoadAvg::Fifteen, &updater), LoadAvg::Fifteen, &updater),
}), })
},
align: Some(s::Align::Left), align: Some(s::Align::Left),
markup: Some(s::Markup::Pango), markup: Some(s::Markup::Pango),
short_text: None, short_text: None,

Loading…
Cancel
Save