summaryrefslogtreecommitdiff
path: root/src/event.rs
blob: e15ee188bd49e9a0d03b0cacf870528bf1b64ade (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
use x11::xlib::{
    XEvent,
    XKeyEvent,
    XButtonEvent,
    XMotionEvent,
    XCrossingEvent,
    XCreateWindowEvent,
    XDestroyWindowEvent,
    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),
    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::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)),
        22 => Event::Configure(XConfigureEvent::from(ev)),
        e => {
            println!("Unknown event {}", e);
            Event::NoEvent
        },
    }
}