You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.5 KiB
105 lines
3.5 KiB
3 years ago
|
// Copyright (C) 2022 Tassilo Horn <tsdh@gnu.org>
|
||
|
//
|
||
|
// This program is free software: you can redistribute it and/or modify it
|
||
|
// under the terms of the GNU General Public License as published by the Free
|
||
|
// Software Foundation, either version 3 of the License, or (at your option)
|
||
|
// any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||
|
// more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License along with
|
||
|
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
//! `swayrbar` lib.
|
||
|
|
||
|
use crate::bar::module::BarModuleFn;
|
||
|
use env_logger::Env;
|
||
|
use serde_json;
|
||
3 years ago
|
use std::io;
|
||
3 years ago
|
use std::{sync::Arc, thread};
|
||
|
use swaybar_types as sbt;
|
||
3 years ago
|
|
||
3 years ago
|
pub mod config;
|
||
3 years ago
|
pub mod module;
|
||
|
|
||
|
pub fn start() {
|
||
|
env_logger::Builder::from_env(Env::default().default_filter_or("warn"))
|
||
|
.init();
|
||
|
|
||
3 years ago
|
let config = config::Config::default();
|
||
|
|
||
3 years ago
|
let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(vec![
|
||
3 years ago
|
module::window::BarModuleWindow::create(
|
||
|
module::window::BarModuleWindow::default_config("0".to_owned()),
|
||
|
),
|
||
|
module::sysinfo::BarModuleSysInfo::create(
|
||
|
module::sysinfo::BarModuleSysInfo::default_config("0".to_owned()),
|
||
|
),
|
||
|
module::battery::BarModuleBattery::create(
|
||
|
module::battery::BarModuleBattery::default_config("0".to_owned()),
|
||
|
),
|
||
|
module::date::BarModuleDate::create(
|
||
|
module::date::BarModuleDate::default_config("0".to_owned()),
|
||
|
),
|
||
3 years ago
|
]);
|
||
|
let mods_for_input = mods.clone();
|
||
|
thread::spawn(move || handle_input(mods_for_input));
|
||
3 years ago
|
generate_status(&mods, config.refresh_interval);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
pub fn handle_input(mods: Arc<Vec<Box<dyn BarModuleFn>>>) {
|
||
3 years ago
|
let mut sb = String::new();
|
||
|
io::stdin()
|
||
|
.read_line(&mut sb)
|
||
|
.expect("Could not read from stdin");
|
||
3 years ago
|
|
||
3 years ago
|
if "[\n" != sb {
|
||
|
log::error!("Expected [\\n but got {}", sb);
|
||
|
log::error!("Sorry, input events won't work is this session.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
loop {
|
||
|
let mut buf = String::new();
|
||
|
if let Err(err) = io::stdin().read_line(&mut buf) {
|
||
|
log::error!("Error while reading from stdin: {}", err);
|
||
|
log::error!("Skipping this input line...");
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
let click = match serde_json::from_str::<sbt::Click>(
|
||
|
buf.strip_prefix(",").unwrap_or(&buf),
|
||
|
) {
|
||
|
Ok(click) => click,
|
||
|
Err(err) => {
|
||
|
log::error!("Error while parsing str to Click: {}", err);
|
||
|
log::error!("The string was {}", buf);
|
||
|
log::error!("Skipping this input line...");
|
||
|
continue;
|
||
|
}
|
||
|
};
|
||
|
log::debug!("Received click: {:?}", click);
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
pub fn generate_status(mods: &[Box<dyn BarModuleFn>], refresh_interval: u64) {
|
||
3 years ago
|
println!("{{\"version\": 1, \"click_events\": true}}");
|
||
3 years ago
|
// status_command should output an infinite array meaning we emit an
|
||
|
// opening [ and never the closing bracket.
|
||
|
println!("[");
|
||
|
|
||
|
loop {
|
||
|
let mut blocks = vec![];
|
||
|
for m in mods {
|
||
|
blocks.push(m.build());
|
||
|
}
|
||
|
let json = serde_json::to_string_pretty(&blocks)
|
||
|
.unwrap_or_else(|_| "".to_string());
|
||
|
println!("{},", json);
|
||
3 years ago
|
thread::sleep(std::time::Duration::from_millis(refresh_interval));
|
||
3 years ago
|
}
|
||
|
}
|