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
use std::any::Any;
use std::fmt;

use Event;

#[cfg(windows)]
#[path = "windows.rs"]
mod platform;
#[cfg(not(windows))]
#[path = "unix/mod.rs"]
mod platform;

pub(crate) use self::platform::start_threads;
#[cfg(windows)]
pub(crate) use self::platform::ScreenSize;

use Coords;

#[derive(Copy, Clone, Eq, Debug, PartialEq)]
pub enum Key {
    Char([u8; 4], u8),
    Err([u8; 4], u8),
    Esc,
    F1,
    F2,
    F3,
    F4,
    F5,
    F6,
    F7,
    F8,
    F9,
    F10,
    F11,
    F12,
    BS,
    Tab,
    Enter,
    Ins,
    Del,
    Home,
    End,
    PgUp,
    PgDn,
    Up,
    Down,
    Left,
    Right,
}

#[allow(dead_code)]
impl Key {
    fn ascii(byte: u8) -> Key {
        Key::Char([byte, 0, 0, 0], 1)
    }

    fn empty() -> Key {
        Key::Char([0, 0, 0, 0], 0)
    }
}

impl fmt::Display for Key {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::str::from_utf8;
        match *self {
            Key::Char(ref bytes, len) => {
                write!(f, "{}", from_utf8(&bytes[0..len as usize]).unwrap())
            }
            Key::Err(ref bytes, len) => {
                write!(f, "{:?}", &bytes[0..len as usize])
            }
            k => write!(f, "{:?}", k),
        }
    }
}

bitflags! {
    #[derive(Default)]
    pub struct Mods: u8 {
        const NO_MODS = 0;
        const SHIFT = 0b001;
        const ALT = 0b010;
        const CTRL = 0b100;
        const CTRL_ALT = CTRL.bits | ALT.bits;
    }
}


// 0b001 = Shift
// 0b010 = Alt
// 0b100 = Control
impl fmt::Display for Mods {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let val = match self.bits {
            1 => "Shift-",
            2 => "Alt-",
            3 => "Alt-Shift-",
            4 => "Ctrl-",
            5 => "Ctrl-Shift-",
            6 => "Ctrl-Alt-",
            7 => "Ctrl-Alt-Shift-",
            _ => "",
        };
        write!(f, "{}", val)
    }
}

#[allow(dead_code)]
impl Mods {
    #[cfg(windows)]
    fn win32(ckeys: u32) -> Mods {
        let mut mods: u8 = 0;
        if ckeys & 0b10000 != 0 {
            mods += 1;
        }
        if ckeys & 0b11 != 0 {
            mods += 2;
        }
        if ckeys & 0b1100 != 0 {
            mods += 4;
        }
        Mods::from_bits(mods).unwrap()
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MouseButton {
    Unknown,
    Left,
    Middle,
    Right,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ButtonMotion {
    Press,
    Release,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum WheelMotion {
    Up,
    Down,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum InputEvent {
    Repaint,
    Interrupt,
    Break,
    Mouse(ButtonMotion, MouseButton, Mods, Coords),
    MouseWheel(WheelMotion, Mods),
    MouseMove(Mods, Coords),
    Key(Key, Mods),
}

impl Event for InputEvent {
    fn as_any(&self) -> &Any {
        self
    }
}