|  | @@ -8,12 +8,87 @@ extern crate fontconfig_sys;
 | 
	
		
			
				|  |  |  mod sensors;
 | 
	
		
			
				|  |  |  mod config;
 | 
	
		
			
				|  |  |  mod ui;
 | 
	
		
			
				|  |  | +mod widgets;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use simple_signal::Signal;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  use std::env;
 | 
	
		
			
				|  |  |  use std::process;
 | 
	
		
			
				|  |  | +use std::sync::mpsc;
 | 
	
		
			
				|  |  | +use std::thread;
 | 
	
		
			
				|  |  | +use std::time;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +use ui::x11;
 | 
	
		
			
				|  |  | +use ui::widget;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  fn main() {
 | 
	
		
			
				|  |  |      let config_path = env::args().nth(1).unwrap_or("./panel.toml".to_string());
 | 
	
		
			
				|  |  |      let cfg = config::load(&config_path);
 | 
	
		
			
				|  |  | -    process::exit(ui::ui_main(&cfg));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if let Some(conn) = x11::Connection::new() {
 | 
	
		
			
				|  |  | +        let (tx, rx) = mpsc::channel();
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            let tx = tx.clone();
 | 
	
		
			
				|  |  | +            simple_signal::set_handler(&[Signal::Int, Signal::Term], move |_signals| {
 | 
	
		
			
				|  |  | +                tx.send(widget::Message::Quit).expect("Failed to send quit");
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            let tx = tx.clone();
 | 
	
		
			
				|  |  | +            let conn = conn.clone_connection();
 | 
	
		
			
				|  |  | +            thread::spawn(move || {
 | 
	
		
			
				|  |  | +                loop {
 | 
	
		
			
				|  |  | +                    match conn.wait_for_event() {
 | 
	
		
			
				|  |  | +                        Some(event) => {
 | 
	
		
			
				|  |  | +                            let message = widget::Message::XcbEvent(event);
 | 
	
		
			
				|  |  | +                            tx.send(message).expect("Failed to send xcb event");
 | 
	
		
			
				|  |  | +                        },
 | 
	
		
			
				|  |  | +                        None => { break; }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let mut panel = ui::panel::Panel::new(conn, &cfg);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let bspwm = widgets::bspwm::Bspwm::new(tx.clone(), panel.make_draw_context());
 | 
	
		
			
				|  |  | +        panel.add_left_widget(Box::new(bspwm));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let spacer = widgets::spacer::create(panel.make_draw_context(), 4, 0x6666, 0x6666, 0x6666, 0xFFFF);
 | 
	
		
			
				|  |  | +        panel.add_left_widget(Box::new(spacer));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let title = widgets::title::Title::new(panel.conn.clone(), panel.make_draw_context());
 | 
	
		
			
				|  |  | +        panel.add_left_widget(Box::new(title));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let tray = widgets::tray::Tray::new(tx.clone(), panel.conn.clone(), panel.window);
 | 
	
		
			
				|  |  | +        panel.add_right_widget(Box::new(tray));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let sensors = widgets::sensors::Sensors::new(panel.make_draw_context(), &cfg);
 | 
	
		
			
				|  |  | +        panel.add_right_widget(Box::new(sensors));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        let music = widgets::music::mpd(tx.clone(), panel.make_draw_context());
 | 
	
		
			
				|  |  | +        panel.add_right_widget(Box::new(music));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        panel.create();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            let tx = tx.clone();
 | 
	
		
			
				|  |  | +            thread::spawn(move || {
 | 
	
		
			
				|  |  | +                loop {
 | 
	
		
			
				|  |  | +                    tx.send(widget::Message::Update).expect("Failed to send update");
 | 
	
		
			
				|  |  | +                    thread::sleep(time::Duration::from_secs(1));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        loop {
 | 
	
		
			
				|  |  | +            let event = rx.recv();
 | 
	
		
			
				|  |  | +            if panel.handle_event(event.unwrap()) {
 | 
	
		
			
				|  |  | +                println!("Exiting");
 | 
	
		
			
				|  |  | +                break;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    process::exit(0);
 | 
	
		
			
				|  |  |  }
 |