Refresh window mod on window/workspace events; release swayrbar-0.2.0

main
Tassilo Horn 3 years ago
parent 039743a29a
commit 8f06f4c196
  1. 2
      Cargo.lock
  2. 9
      README.md
  3. 2
      swayrbar/Cargo.toml
  4. 15
      swayrbar/NEWS.md
  5. 91
      swayrbar/src/bar.rs

2
Cargo.lock generated

@ -637,7 +637,7 @@ dependencies = [
[[package]]
name = "swayrbar"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"battery",
"chrono",

@ -18,6 +18,7 @@
* [Screenshots](#swayrbar-screenshots)
* [Installation](#swayrbar-installation)
* [Configuration](#swayrbar-configuration)
* [Version changes](#swayrbar-version-changes)
* [Questions and patches](#questions-and-patches)
* [Bugs](#bugs)
* [Build status](#build-status)
@ -673,6 +674,14 @@ The `date` module shows the date and time by defining the `format` using
[chrono's strftime
format](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers).
### <a id="swayr-version-changes">Version changes</a>
Version changes are summarized in the [NEWS](swayrbar/NEWS.md) file. If
something doesn't seem to work as expected after an update, please consult this
file to check if there has been some (possibly incompatible) change requiring
an update of your config.
## <a id="questions-and-patches">Questions & Patches</a>
For asking questions, sending feedback, or patches, refer to [my public inbox

@ -1,6 +1,6 @@
[package]
name = "swayrbar"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
homepage = "https://sr.ht/~tsdh/swayr/#swayrbar"
repository = "https://git.sr.ht/~tsdh/swayr"

@ -0,0 +1,15 @@
swayrbar 0.2.0
==============
- If a window module is used, subscribe to sway events in order to immediately
refresh it on window/workspace changes.
swayrbar 0.1.1
==============
- Only refresh the module which received the click event.
swayrbar 0.1.0
==============
- Add pactl module.

@ -27,6 +27,7 @@ use std::sync::Mutex;
use std::time::Duration;
use std::{sync::Arc, thread};
use swaybar_types as sbt;
use swayipc as si;
pub fn start() {
env_logger::Builder::from_env(Env::default().default_filter_or("warn"))
@ -38,8 +39,24 @@ pub fn start() {
let mods_for_input = mods.clone();
let trigger =
Arc::new((Mutex::new((String::new(), String::new())), Condvar::new()));
let trigger_for_input = trigger.clone();
thread::spawn(move || handle_input(mods_for_input, trigger_for_input));
let window_mods: Vec<NameAndInstance> = mods
.iter()
.filter(|m| m.get_config().name == "window")
.map(|m| (m.get_config().name.clone(), m.get_config().instance.clone()))
.collect();
if !window_mods.is_empty() {
// There's at least a window module, so subscribe to focus events for
// immediate refreshes.
let trigger_for_events = trigger.clone();
thread::spawn(move || {
handle_sway_events(window_mods, trigger_for_events)
});
}
generate_status(&mods, trigger, refresh_interval);
}
@ -62,7 +79,7 @@ fn create_modules(config: config::Config) -> Vec<Box<dyn BarModuleFn>> {
mods
}
pub fn handle_input(
fn handle_input(
mods: Arc<Vec<Box<dyn BarModuleFn>>>,
trigger: Arc<(Mutex<NameAndInstance>, Condvar)>,
) {
@ -152,7 +169,77 @@ fn execute_command(cmd: &[String]) {
}
}
pub fn generate_status(
fn sway_subscribe() -> si::Fallible<si::EventStream> {
si::Connection::new()?.subscribe(&[
si::EventType::Window,
si::EventType::Shutdown,
si::EventType::Workspace,
])
}
fn handle_sway_events(
window_mods: Vec<NameAndInstance>,
trigger: Arc<(Mutex<NameAndInstance>, Condvar)>,
) {
let mut resets = 0;
let max_resets = 10;
'reset: loop {
if resets >= max_resets {
break;
}
resets += 1;
log::debug!("Connecting to sway for subscribing to events...");
match sway_subscribe() {
Err(err) => {
log::warn!("Could not connect and subscribe: {}", err);
std::thread::sleep(std::time::Duration::from_secs(3));
}
Ok(iter) => {
for ev_result in iter {
resets = 0;
match ev_result {
Ok(ev) => match ev {
si::Event::Window(_) | si::Event::Workspace(_) => {
log::trace!(
"Window or Workspace event: {:?}",
ev
);
for m in &window_mods {
let (mtx, cvar) = &*trigger;
let mut name_and_instance =
mtx.lock().unwrap();
name_and_instance.0 = m.0.to_owned();
name_and_instance.1 = m.1.to_owned();
cvar.notify_one();
}
}
si::Event::Shutdown(sd_ev) => {
log::debug!(
"Sway shuts down with reason '{:?}'.",
sd_ev.change
);
break 'reset;
}
_ => (),
},
Err(e) => {
log::warn!("Error while receiving events: {}", e);
std::thread::sleep(std::time::Duration::from_secs(
3,
));
log::warn!("Resetting!");
}
}
}
}
}
}
}
fn generate_status(
mods: &[Box<dyn BarModuleFn>],
trigger: Arc<(Mutex<NameAndInstance>, Condvar)>,
refresh_interval: u64,

Loading…
Cancel
Save