1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#[macro_use]
extern crate anyhow;
mod bar;
mod blocks_cache;
mod button_manager;
mod color;
mod config;
mod event_loop;
mod i3bar_protocol;
mod icon;
mod output;
mod pointer_btn;
mod protocol;
mod shared_state;
mod state;
mod status_cmd;
mod text;
mod tray;
mod utils;
mod wm_info_provider;
use std::env;
use std::io::{self, ErrorKind, Read};
use std::os::fd::{AsRawFd, RawFd};
use std::path::PathBuf;
use anyhow::Context;
use clap::Parser;
use daemonize::Daemonize;
use signal_hook::consts::*;
use wayrs_client::{Connection, IoMode};
use config::{Config, ConfigWatcher};
use event_loop::{Action, Callback, EventLoop};
use state::State;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(short, long, value_name = "FILE", help = "The path to a config file")]
config: Option<PathBuf>,
#[arg(long, help = "Keep the bar attached to the current terminal")]
foreground: bool,
}
fn status_callback(fd: RawFd) -> Callback {
Box::new(move |ctx| {
if ctx.state.status_cmd_fd() != Some(fd) {
return Ok(Action::Unregister);
}
match ctx
.state
.shared_state
.status_cmd
.as_mut()
.unwrap()
.receive_blocks()
{
Ok(None) => Ok(Action::Keep),
Ok(Some(blocks)) => {
ctx.state.set_blocks(ctx.conn, blocks);
Ok(Action::Keep)
}
Err(e) => {
let _ = ctx
.state
.shared_state
.status_cmd
.take()
.unwrap()
.child
.kill();
ctx.state.set_error(ctx.conn, "status", e);
Ok(Action::Unregister)
}
}
})
}
fn main() -> anyhow::Result<()> {
let args = Cli::parse();
let config_path = Config::path(args.config.as_deref());
if !args.foreground {
let working_directory = env::current_dir().context("failed to read working directory")?;
Daemonize::new()
.working_directory(working_directory)
.start()
.context("failed to start in the background")?;
}
let (mut sig_read, sig_write) = io::pipe()?;
signal_hook::low_level::pipe::register(SIGUSR1, sig_write)?;
let mut conn = Connection::connect()?;
conn.blocking_roundtrip()?;
let mut el = EventLoop::new();
let mut state = State::new(&mut conn, &mut el, config_path.as_deref());
conn.flush(IoMode::Blocking)?;
el.add_on_idle(|ctx| {
ctx.conn.flush(IoMode::Blocking)?;
Ok(event_loop::Action::Keep)
});
el.register_with_fd(sig_read.as_raw_fd(), move |ctx| {
sig_read.read_exact(&mut [0u8]).unwrap();
ctx.state.toggle_visibility(ctx.conn);
Ok(event_loop::Action::Keep)
});
el.register_with_fd(conn.as_raw_fd(), |ctx| {
match ctx.conn.recv_events(IoMode::NonBlocking) {
Ok(()) => ctx.conn.dispatch_events(ctx.state),
Err(e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!(e),
}
Ok(event_loop::Action::Keep)
});
if let Some(fd) = state.status_cmd_fd() {
el.register_with_fd(fd, status_callback(fd));
}
if let Some(config_path) = config_path {
let mut watcher = ConfigWatcher::new(&config_path)
.with_context(|| format!("failed to watch {}", config_path.display()))?;
let watcher_fd = watcher.fd();
el.register_with_fd(watcher_fd, move |ctx| {
if !watcher.changed()? {
return Ok(Action::Keep);
}
match ctx.state.reload_config(ctx.conn, &config_path) {
Ok(Some(fd)) => Ok(Action::KeepAndRegister(fd, status_callback(fd))),
Ok(None) => Ok(Action::Keep),
Err(error) => {
ctx.state.set_error(ctx.conn, "config", error);
Ok(Action::Keep)
}
}
});
}
el.run(&mut conn, &mut state)?;
unreachable!();
}
|