tvis/input/
mod.rs

1use std::any::Any;
2use std::fmt;
3
4#[cfg(windows)]
5#[path = "windows.rs"]
6mod platform;
7#[cfg(not(windows))]
8#[path = "unix/mod.rs"]
9mod platform;
10
11pub(crate) use self::platform::start_threads;
12#[cfg(windows)]
13pub(crate) use self::platform::Resizer;
14
15use Coords;
16
17pub trait Event: fmt::Debug + Send {
18    fn as_any(&self) -> &Any;
19}
20
21#[derive(Copy, Clone, Eq, Debug, PartialEq)]
22pub enum Key {
23    Char(char, [u8; 4], usize),
24    Err([u8; 4], usize),
25    Esc,
26    F1,
27    F2,
28    F3,
29    F4,
30    F5,
31    F6,
32    F7,
33    F8,
34    F9,
35    F10,
36    F11,
37    F12,
38    BS,
39    Tab,
40    Enter,
41    Ins,
42    Del,
43    Home,
44    End,
45    PgUp,
46    PgDn,
47    Up,
48    Down,
49    Left,
50    Right,
51}
52
53#[allow(dead_code)]
54impl Key {
55    fn ascii(byte: u8) -> Key {
56        Key::Char(
57            unsafe { ::std::char::from_u32_unchecked(byte as u32) },
58            [byte, 0, 0, 0],
59            1,
60        )
61    }
62
63    fn empty() -> Key {
64        Key::Char('\x00', [0, 0, 0, 0], 0)
65    }
66}
67
68impl fmt::Display for Key {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        match *self {
71            Key::Char(c, _, _) => write!(f, "{}", c),
72            Key::Err(ref bytes, len) => {
73                write!(f, "{:?}", &bytes[0..len as usize])
74            }
75            k => write!(f, "{:?}", k),
76        }
77    }
78}
79
80bitflags! {
81    #[derive(Default)]
82    pub struct Mods: u8 {
83        const SHIFT = 0b001;
84        const ALT = 0b010;
85        const CTRL = 0b100;
86        const CTRL_ALT = Self::CTRL.bits | Self::ALT.bits;
87    }
88}
89
90// 0b001 = Shift
91// 0b010 = Alt
92// 0b100 = Control
93impl fmt::Display for Mods {
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        let val = match self.bits {
96            1 => "Shift-",
97            2 => "Alt-",
98            3 => "Alt-Shift-",
99            4 => "Ctrl-",
100            5 => "Ctrl-Shift-",
101            6 => "Ctrl-Alt-",
102            7 => "Ctrl-Alt-Shift-",
103            _ => "",
104        };
105        write!(f, "{}", val)
106    }
107}
108
109#[allow(dead_code)]
110impl Mods {
111    #[cfg(windows)]
112    fn win32(ckeys: u32) -> Mods {
113        let mut mods: u8 = 0;
114        if ckeys & 0b1_0000 != 0 {
115            mods += 1;
116        }
117        if ckeys & 0b11 != 0 {
118            mods += 2;
119        }
120        if ckeys & 0b1100 != 0 {
121            mods += 4;
122        }
123        Mods::from_bits(mods).unwrap()
124    }
125}
126
127#[derive(Copy, Clone, Debug, PartialEq, Eq)]
128pub enum MouseButton {
129    Unknown,
130    Left,
131    Middle,
132    Right,
133}
134
135#[derive(Copy, Clone, Debug, PartialEq, Eq)]
136pub enum ButtonMotion {
137    Press,
138    Release,
139}
140
141#[derive(Copy, Clone, Debug, PartialEq, Eq)]
142pub enum WheelMotion {
143    Up,
144    Down,
145}
146
147#[derive(Copy, Clone, Debug, PartialEq, Eq)]
148pub enum InputEvent {
149    Repaint,
150    Interrupt,
151    Break,
152    Mouse(ButtonMotion, MouseButton, Mods, Coords),
153    MouseWheel(WheelMotion, Mods, Coords),
154    MouseMove(Mods, Coords),
155    Key(Key, Mods),
156}
157
158impl Event for InputEvent {
159    fn as_any(&self) -> &Any {
160        self
161    }
162}