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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#[macro_use]
mod de;
#[macro_use]
mod util;
pub mod blocks;
mod config;
mod errors;
mod formatting;
mod http;
mod icons;
mod input;
mod scheduler;
mod signals;
mod subprocess;
mod themes;
mod widgets;

#[cfg(feature = "profiling")]
use cpuprofiler::PROFILER;
#[cfg(feature = "profiling")]
use std::ops::DerefMut;

#[cfg(feature = "pulseaudio")]
use libpulse_binding as pulse;

use std::time::Duration;

use clap::{crate_authors, crate_description, App, Arg, ArgMatches};
use crossbeam_channel::{select, Receiver, Sender};

use crate::blocks::create_block;
use crate::blocks::Block;
use crate::config::Config;
use crate::config::SharedConfig;
use crate::errors::*;
use crate::input::{process_events, I3BarEvent};
use crate::scheduler::{Task, UpdateScheduler};
use crate::signals::process_signals;
use crate::util::deserialize_file;
use crate::widgets::text::TextWidget;
use crate::widgets::{I3BarWidget, State};

fn main() {
    let ver = if env!("GIT_COMMIT_HASH").is_empty() || env!("GIT_COMMIT_DATE").is_empty() {
        env!("CARGO_PKG_VERSION").to_string()
    } else {
        format!(
            "{} (commit {} {})",
            env!("CARGO_PKG_VERSION"),
            env!("GIT_COMMIT_HASH"),
            env!("GIT_COMMIT_DATE")
        )
    };

    let mut builder = App::new("i3status-rs");
    builder = builder
        .version(&*ver)
        .author(crate_authors!())
        .about(crate_description!())
        .arg(
            Arg::with_name("config")
                .value_name("CONFIG_FILE")
                .help("Sets a toml config file")
                .required(false)
                .index(1),
        )
        .arg(
            Arg::with_name("exit-on-error")
                .help("Exit rather than printing errors to i3bar and continuing")
                .long("exit-on-error")
                .takes_value(false),
        )
        .arg(
            Arg::with_name("never-pause")
                .help("Ignore any attempts by i3 to pause the bar when hidden/fullscreen")
                .long("never-pause")
                .takes_value(false),
        )
        .arg(
            Arg::with_name("one-shot")
                .help("Print blocks once and exit")
                .long("one-shot")
                .takes_value(false)
                .hidden(true),
        );

    #[cfg(feature = "profiling")]
    {
        builder = builder
            .arg(
                Arg::with_name("profile")
                    .long("profile")
                    .takes_value(true)
                    .help("A block to be profiled. Creates a `block.profile` file that can be analyzed with `pprof`"),
            )
            .arg(
                Arg::with_name("profile-runs")
                    .long("profile-runs")
                    .takes_value(true)
                    .default_value("10000")
                    .help("Number of times to execute update when profiling"),
            );
    }

    let matches = builder.get_matches();
    let exit_on_error = matches.is_present("exit-on-error");

    // Run and match for potential error
    if let Err(error) = run(&matches) {
        if exit_on_error {
            eprintln!("{:?}", error);
            ::std::process::exit(1);
        }

        let error_widget = TextWidget::new(0, 0, Default::default())
            .with_state(State::Critical)
            .with_text(&format!("{:?}", error));
        let error_rendered = error_widget.get_data();
        println!("[{}]", error_rendered.render());

        eprintln!("\n\n{:?}", error);
        // Do nothing, so the error message keeps displayed
        loop {
            ::std::thread::sleep(Duration::from_secs(::std::u64::MAX));
        }
    }
}

fn run(matches: &ArgMatches) -> Result<()> {
    // Now we can start to run the i3bar protocol
    let initialise = if matches.is_present("never-pause") {
        "\"version\": 1, \"click_events\": true, \"stop_signal\": 0"
    } else {
        "\"version\": 1, \"click_events\": true"
    };
    print!("{{{}}}\n[", initialise);

    // Read & parse the config file
    let config_path = match matches.value_of("config") {
        Some(config_path) => std::path::PathBuf::from(config_path),
        None => util::xdg_config_home().join("i3status-rust/config.toml"),
    };
    let config: Config = deserialize_file(&config_path)?;

    // Update request channel
    let (tx_update_requests, rx_update_requests): (Sender<Task>, Receiver<Task>) =
        crossbeam_channel::unbounded();

    // In dev build, we might diverge into profiling blocks here
    #[cfg(feature = "profiling")]
    {
        if let Some(name) = matches.value_of("profile") {
            return profile_config(
                name,
                matches.value_of("profile-runs").unwrap(),
                &config,
                tx_update_requests,
            );
        }
    }

    let shared_config = SharedConfig::new(&config);

    // Initialize the blocks
    let mut blocks: Vec<Box<dyn Block>> = Vec::new();
    for &(ref block_name, ref block_config) in &config.blocks {
        blocks.push(create_block(
            blocks.len(),
            block_name,
            block_config.clone(),
            shared_config.clone(),
            tx_update_requests.clone(),
        )?);
    }

    let mut scheduler = UpdateScheduler::new(&blocks);

    // We wait for click events in a separate thread, to avoid blocking to wait for stdin
    let (tx_clicks, rx_clicks): (Sender<I3BarEvent>, Receiver<I3BarEvent>) =
        crossbeam_channel::unbounded();
    process_events(tx_clicks);

    // We wait for signals in a separate thread
    let (tx_signals, rx_signals): (Sender<i32>, Receiver<i32>) = crossbeam_channel::unbounded();
    process_signals(tx_signals);

    // Time to next update channel.
    // Fires immediately for first updates
    let mut ttnu = crossbeam_channel::after(Duration::from_millis(0));

    let one_shot = matches.is_present("one-shot");
    loop {
        // We use the message passing concept of channel selection
        // to avoid busy wait
        select! {
            // Receive click events
            recv(rx_clicks) -> res => if let Ok(event) = res {
                if let Some(id) = event.id {
                        blocks.get_mut(id)
                    .internal_error("click handler", "could not get required block")?
                            .click(&event)?;
                    util::print_blocks(&blocks, &shared_config)?;
                }
            },
            // Receive async update requests
            recv(rx_update_requests) -> request => if let Ok(req) = request {
                // Process immediately and forget
                blocks.get_mut(req.id)
                    .internal_error("scheduler", "could not get required block")?
                    .update()?;
                util::print_blocks(&blocks, &shared_config)?;
            },
            // Receive update timer events
            recv(ttnu) -> _ => {
                scheduler.do_scheduled_updates(&mut blocks)?;
                // redraw the blocks, state changed
                util::print_blocks(&blocks, &shared_config)?;
            },
            // Receive signal events
            recv(rx_signals) -> res => if let Ok(sig) = res {
                match sig {
                    signal_hook::consts::SIGUSR1 => {
                        //USR1 signal that updates every block in the bar
                        for block in blocks.iter_mut() {
                            block.update()?;
                        }
                        util::print_blocks(&blocks, &shared_config)?;
                    },
                    signal_hook::consts::SIGUSR2 => {
                        //USR2 signal that should reload the config
                        //TODO not implemented
                        //unimplemented!("SIGUSR2 is meant to be used to reload the config toml, but this feature is yet not implemented");
                    },
                    _ => {
                        //Real time signal that updates only the blocks listening
                        //for that signal
                        for block in blocks.iter_mut() {
                            block.signal(sig)?;
                        }
                    },
                };
            }
        }

        // Set the time-to-next-update timer
        if let Some(time) = scheduler.time_to_next_update() {
            ttnu = crossbeam_channel::after(time)
        }
        if one_shot {
            break Ok(());
        }
    }
}

#[cfg(feature = "profiling")]
fn profile(iterations: i32, name: &str, block: &mut dyn Block) {
    let mut bar = progress::Bar::new();
    println!(
        "Now profiling the {0} block by executing {1} updates.\n \
         Use pprof to analyze {0}.profile later.",
        name, iterations
    );

    PROFILER
        .lock()
        .unwrap()
        .start(format!("./{}.profile", name))
        .unwrap();

    bar.set_job_title("Profiling...");

    for i in 0..iterations {
        block.update().expect("block update failed");
        bar.reach_percent(((i as f64 / iterations as f64) * 100.).round() as i32);
    }

    PROFILER.lock().unwrap().stop().unwrap();
}

#[cfg(feature = "profiling")]
fn profile_config(name: &str, runs: &str, config: &Config, update: Sender<Task>) -> Result<()> {
    let profile_runs = runs
        .parse::<i32>()
        .configuration_error("failed to parse --profile-runs as an integer")?;
    let shared_config = SharedConfig::new(&config);
    for &(ref block_name, ref block_config) in &config.blocks {
        if block_name == name {
            let mut block =
                create_block(0, &block_name, block_config.clone(), shared_config, update)?;
            profile(profile_runs, &block_name, block.deref_mut());
            break;
        }
    }
    Ok(())
}