summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: c89544d52b1df28d14f8dfa6e56e16127a43bd28 (plain)
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
extern crate libc;
extern crate x11;

pub mod safe_x11;
pub mod command;
pub mod dotwm;
pub mod event;

use dotwm::DotWM;
use command::exec_cmd;
use event::{Event,next_event};
use std::ops::Deref;
use std::process;

use x11::xlib;
use x11::keysym;

fn exec(_: &DotWM, _: xlib::XEvent, args: &[String]) -> bool {
    if let Some(program) = args.first() {
        let mut prog_args = vec![];
        for arg in args[1..].iter() {
            prog_args.push(arg);
        }
        exec_cmd(program, prog_args.deref()).unwrap();
    }
    true
}

fn move_win(wm: &DotWM, _: xlib::XEvent, args: &[String]) -> bool {
    println!("Parsing x");
    let x = args[0].parse::<i32>().unwrap();
    println!("Parsing y");
    let y = args[1].parse::<i32>().unwrap();
    if let Some(ref win) = wm.current_window() {
        win.move_offset(x, y);
    };
    true
}

fn quit_dotwm(_: &DotWM, _: xlib::XEvent, _: &[String]) -> bool {
    process::exit(0);
}

fn main() {
    println!("Creating dotwm");
    let mut dotwm = DotWM::new();

    dotwm.add_binding(keysym::XK_Return, xlib::Mod4Mask, exec,
                      &["xterm"]);
    dotwm.add_binding(keysym::XK_h, xlib::Mod4Mask, move_win, &["-10", "0"]);
    dotwm.add_binding(keysym::XK_j, xlib::Mod4Mask, move_win, &["0", "10"]);
    dotwm.add_binding(keysym::XK_k, xlib::Mod4Mask, move_win, &["0", "-10"]);
    dotwm.add_binding(keysym::XK_l, xlib::Mod4Mask, move_win, &["10", "0"]);

    dotwm.add_binding(keysym::XK_q, xlib::Mod4Mask | xlib::ShiftMask, quit_dotwm, &[]);

    // Main loop
    loop {
        let event = next_event(dotwm.display);
        match event {
            Event::Key(mut e, true) => {
                let keysym = unsafe { xlib::XLookupKeysym(&mut e, 0) as u32 };
                let mask = e.state;

                dotwm.exec_func(keysym, mask, xlib::XEvent::from(e));
            },
            Event::Create(e) => {
                // XCreateWindowEvent
                let create_event = xlib::XCreateWindowEvent::from(e);
                dotwm.add_window(create_event.window);
            }
            _ => println!("Catched event! {:?}", event),
        }
    }
}