summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f00523de7bdc00d892a0368bf36374ca5c3496eb (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
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
// See LICENSE file for copyright and license details.

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,collect_zombies};
use event::{Event,next_event};

use safe_x11::grab_key;

use std::ops::Deref;
use std::process;
use std::collections::HashMap;

use x11::xlib;
use x11::xlib::GrabModeAsync;
use x11::xlib::XEvent;
use x11::keysym;

/// Defines a callback to call no certains events.
pub type ExecFn = fn(&mut DotWM, XEvent, &[String]) -> bool;

/// Map for keys => functions
pub type BindingHash = HashMap<(u32, u32), (ExecFn, Vec<String>)>;

fn exec_func(wm: &mut DotWM, bindings: &mut BindingHash, key: u32, modifiers: u32, ev: xlib::XEvent) {
    if let Some(&(func, ref args)) = bindings.get(&(key, modifiers)) {
        let v = args.clone();
        func(wm, ev, &v);
    }
}

fn exec(_: &mut 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: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool {
    let x = args[0].parse::<i32>().unwrap();
    let y = args[1].parse::<i32>().unwrap();
    if let Some(ref win) = wm.current_window() {
        win.move_offset(x, y);
    };
    true
}

fn resize_win(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool {
    let w = args[0].parse::<i32>().unwrap();
    let h = args[1].parse::<i32>().unwrap();

    if let Some(ref win) = wm.current_window() {
        win.resize(w, h);
    };

    true
}

fn focus_next(wm: &mut DotWM, _: xlib::XEvent, _: &[String]) -> bool {
    wm.focus_next();
    true
}

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

/// Add a binding to the WM.
///
/// # Example
///
/// ```
/// fn quit_dotwm(_: &mut DotWM, _: xlib::XEvent, _: &[String]) -> bool {
///     process::exit(0);
/// }
///
/// // ...
///
/// add_binding(keysym::XK_Return, xlib::Mod4Mask, exec,
///             &["xterm"]);
/// ```
fn add_binding(wm: &mut DotWM, bindings: &mut BindingHash,
               key: u32, modifiers: u32, func: ExecFn, args: &[&str]) {
        grab_key(wm.display, key, modifiers, true, GrabModeAsync, GrabModeAsync);        
        let mut v = vec![];
        for arg in args {
            v.push(arg.to_string());
        }
        bindings.insert((key, modifiers), (func, v));
}

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

    add_binding(&mut dotwm,&mut  bindings, keysym::XK_Return, xlib::Mod4Mask, exec,
                &["xterm"]);
    add_binding(&mut dotwm,&mut  bindings, keysym::XK_p, xlib::Mod4Mask, exec,
                &["dmenu_run"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_Tab, xlib::Mod4Mask, focus_next, &[]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_h, xlib::Mod4Mask, move_win, &["-10", "0"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_j, xlib::Mod4Mask, move_win, &["0", "10"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_k, xlib::Mod4Mask, move_win, &["0", "-10"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_l, xlib::Mod4Mask, move_win, &["10", "0"]);

    // Resize
    add_binding(&mut dotwm,&mut bindings, keysym::XK_h, xlib::Mod4Mask | xlib::ControlMask,
                resize_win, &["-10", "0"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_j, xlib::Mod4Mask | xlib::ControlMask,
                resize_win, &["0", "10"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_k, xlib::Mod4Mask | xlib::ControlMask,
                resize_win, &["0", "-10"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_l, xlib::Mod4Mask | xlib::ControlMask,
                resize_win, &["10", "0"]);

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

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

                exec_func(&mut dotwm, &mut bindings, keysym, mask, xlib::XEvent::from(e));
            },
            Event::Map(e) => {
                // If the window has override_redirect setted to true, this should not
                // be handled by the wm.
                let map_event = xlib::XMapEvent::from(e);
                if map_event.override_redirect == 0 {
                    println!("Adding window");
                    dotwm.add_window(map_event.window);
                } else {
                    println!("Map event 0 :(");
                }
            }
            Event::Unmap(e) => {
                dotwm.remove_window(e.window);
            },
            Event::Enter(e) => {
                if let Some(idx) = dotwm.find_window(e.window) {
                    dotwm.change_focus_of(idx);
                }
            },
            _ => (),
        }
        collect_zombies();
    }
}