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

extern crate libc;
extern crate x11;
extern crate unix_socket;

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

use dotwm::DotWM;
use command::*;
use event::{Event,select_event};
use socket::parser;

use std::collections::HashMap;

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

use std::os::unix::io::AsRawFd;
use std::io::{Read,Write};
use unix_socket::UnixListener;

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

    // 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_Return, xlib::Mod4Mask, exec, &["xterm"]);
    add_binding(&mut dotwm,&mut bindings, keysym::XK_Tab, xlib::Mod4Mask, focus_next, &[]);
    add_binding(&mut dotwm, &mut bindings, keysym::XK_q, xlib::Mod4Mask | xlib::ShiftMask, quit_dotwm, &[]);

    let listener = UnixListener::bind("./dotwm.sock").unwrap();
    let socket_fd = listener.as_raw_fd();

    exec_cmd("./autostart", &[]).unwrap();

    // Main loop
    loop {
        let event = unsafe { select_event(dotwm.display, x11_fd, socket_fd, &listener) };
        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 {
                    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);
                }
            },
            Event::Socket(stream) => {
                let mut s = stream.try_clone().unwrap();
                let mut buf = String::new();
                s.read_to_string(&mut buf).unwrap();

                for line in buf.lines() {
                    let result = parser::parse(&mut dotwm, &mut bindings, &line);
                    let _ = write!(s, "{}", result);
                }
            },
            _ => println!("Unknown event"),
        }
        collect_zombies();
    }
}