summaryrefslogtreecommitdiff
path: root/src/event.rs
blob: 366ca28b1c5ccd5563417aaf6849c5872f67aa48 (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
use x11::xlib::{
    XEvent,
    XKeyEvent,
    XButtonEvent,
    XMotionEvent,
    XCrossingEvent,
    XCreateWindowEvent,
    XDestroyWindowEvent,
    XUnmapEvent,
    XMapEvent,
    XConfigureEvent,
};

use x11::xlib;
use x11::xlib::Display;
use std::fmt;

use safe_x11::next_xevent;

pub enum Event {
    Key(XKeyEvent, bool),
    Button(XButtonEvent, bool),
    Drag(XMotionEvent),
    Enter(XCrossingEvent),
    Leave(XCrossingEvent),
    Generic(XEvent),
    Create(XCreateWindowEvent),
    Destroy(XDestroyWindowEvent),
    Unmap(XUnmapEvent),
    Map(XMapEvent),
    Configure(XConfigureEvent),
    NoEvent
}

impl fmt::Debug for Event {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            &Event::Key(ev, pressed) => format!("Key({}, {})", XEvent::from(ev).get_type(), pressed),
            &Event::Button(ev, pressed) => format!("Button({}, {})", XEvent::from(ev).get_type(), pressed),
            &Event::Drag(ev) => format!("Drag({})", XEvent::from(ev).get_type()),
            &Event::Generic(ev) => format!("Generic({})", XEvent::from(ev).get_type()),
            &Event::Enter(ev) => format!("Enter({})", XEvent::from(ev).get_type()),
            &Event::Leave(ev) => format!("Enter({})", XEvent::from(ev).get_type()),
            &Event::Create(ev) => format!("Create({})", XEvent::from(ev).get_type()),
            &Event::Destroy(ev) => format!("Destroy({})", XEvent::from(ev).get_type()),
            &Event::Unmap(ev) => format!("Unmap({})", XEvent::from(ev).get_type()),
            &Event::Map(ev) => format!("Map({})", XEvent::from(ev).get_type()),
            &Event::Configure(ev) => format!("Configure({})", XEvent::from(ev).get_type()),
            &Event::NoEvent => format!("NoEvent"),
        };

        write!(f, "{}", s)
    }
}

pub fn next_event(display: *mut Display) -> Event {
    let ev = next_xevent(display);

    match ev.get_type() {
        2 => Event::Key(xlib::XKeyEvent::from(ev), true),
        3 => Event::Key(xlib::XKeyEvent::from(ev), false),
        4 => Event::Button(xlib::XButtonEvent::from(ev), true),
        5 => Event::Button(xlib::XButtonEvent::from(ev), false),
        6 => Event::Drag(XMotionEvent::from(ev)),
        7 => Event::Enter(XCrossingEvent::from(ev)),
        8 => Event::Leave(XCrossingEvent::from(ev)),
        16 => Event::Create(XCreateWindowEvent::from(ev)),
        17 => Event::Destroy(XDestroyWindowEvent::from(ev)),
        18 => Event::Unmap(XUnmapEvent::from(ev)),
        19 => Event::Map(XMapEvent::from(ev)),
        22 => Event::Configure(XConfigureEvent::from(ev)),
        e => {
            println!("Unknown event {}", e);
            Event::NoEvent
        },
    }
}