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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Interprets the terminal events we care about:
//!
//!   - Resize events.
//!   - Keyboard input.
//!
//! Resize events are handled by registering a signal handler for SIGWINCH.
//!
//! Keyboard events are read from stdin. We look for byte strings of key combinations that we care
//! about. E.g. Alt-arrow keys, C-w etc.

extern crate libc;
extern crate nix;

use nix::sys::signal::{sigaction, Signal, SigAction, SigHandler, SigSet};
use nix::sys::signal;

use std::char;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

////////////////////////////////////////////////////////////////////////////////////////////////////
// Public types
////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Key {
    AltArrow(Arrow),
    AltChar(char),
    Arrow(Arrow),
    Backspace,
    Char(char),
    Ctrl(char),
    CtrlArrow(Arrow),
    Del,
    End,
    Esc,
    Home,
    PageDown,
    PageUp,
    ShiftDown,
    ShiftUp,
    Tab,
}

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

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
    /// A single key input.
    Key(Key),

    /// Usually a paste.
    String(String),

    /// SIGWINCH happened.
    Resize,

    FocusGained,
    FocusLost,

    /// An unknown sequence of bytes (probably for a key combination that we don't care about).
    Unknown(Vec<u8>),
}

pub struct Input {
    /// Used when reading from stdin.
    buf: Vec<u8>,
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// SIGWINCH handler
////////////////////////////////////////////////////////////////////////////////////////////////////

static GOT_SIGWINCH : AtomicBool = AtomicBool::new(false);

extern fn sigwinch_handler(_: libc::c_int) {
    GOT_SIGWINCH.store(true, Ordering::Relaxed);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Byte sequences of key pressed we want to capture
// (TODO: We only support xterm for now)
////////////////////////////////////////////////////////////////////////////////////////////////////

static XTERM_ALT_ARROW_DOWN  : [u8; 6] = [27, 91, 49, 59, 51, 66];
static XTERM_ALT_ARROW_LEFT  : [u8; 6] = [27, 91, 49, 59, 51, 68];
static XTERM_ALT_ARROW_RIGHT : [u8; 6] = [27, 91, 49, 59, 51, 67];
static XTERM_ALT_ARROW_UP    : [u8; 6] = [27, 91, 49, 59, 51, 65];
static XTERM_ARROW_DOWN      : [u8; 3] = [27, 91, 66];
static XTERM_ARROW_DOWN_2    : [u8; 3] = [27, 79, 66];
static XTERM_ARROW_LEFT      : [u8; 3] = [27, 91, 68];
static XTERM_ARROW_LEFT_2    : [u8; 3] = [27, 79, 68];
static XTERM_ARROW_RIGHT     : [u8; 3] = [27, 91, 67];
static XTERM_ARROW_RIGHT_2   : [u8; 3] = [27, 79, 67];
static XTERM_ARROW_UP        : [u8; 3] = [27, 91, 65];
static XTERM_ARROW_UP_2      : [u8; 3] = [27, 79, 65];
static XTERM_CTRL_ARROW_DOWN : [u8; 6] = [27, 91, 49, 59, 53, 66];
static XTERM_CTRL_ARROW_LEFT : [u8; 6] = [27, 91, 49, 59, 53, 68];
static XTERM_CTRL_ARROW_RIGHT: [u8; 6] = [27, 91, 49, 59, 53, 67];
static XTERM_CTRL_ARROW_UP   : [u8; 6] = [27, 91, 49, 59, 53, 65];
static XTERM_DEL             : [u8; 4] = [27, 91, 51, 126];
static XTERM_PAGE_DOWN       : [u8; 4] = [27, 91, 54, 126];
static XTERM_PAGE_UP         : [u8; 4] = [27, 91, 53, 126];
static XTERM_SHIFT_UP        : [u8; 6] = [27, 91, 49, 59, 50, 65];
static XTERM_SHIFT_DOWN      : [u8; 6] = [27, 91, 49, 59, 50, 66];
static XTERM_FOCUS_GAINED    : [u8; 3] = [27, 91, 73];
static XTERM_FOCUS_LOST      : [u8; 3] = [27, 91, 79];
// FIXME: For some reason term_input test program gets first two of these bytes while tiny gets the
// latter two. Tried to debug this a little bit by changing termattrs but no luck...
static XTERM_HOME            : [u8; 3] = [27, 91, 72];
static XTERM_END             : [u8; 3] = [27, 91, 70];
static XTERM_HOME_2          : [u8; 3] = [27, 79, 72];
static XTERM_END_2           : [u8; 3] = [27, 79, 70];

static XTERM_KEY_SEQS : [(&'static [u8], Event); 27] =
    [ (&XTERM_ALT_ARROW_DOWN,  Event::Key(Key::AltArrow(Arrow::Down))),
      (&XTERM_ALT_ARROW_LEFT,  Event::Key(Key::AltArrow(Arrow::Left))),
      (&XTERM_ALT_ARROW_RIGHT, Event::Key(Key::AltArrow(Arrow::Right))),
      (&XTERM_ALT_ARROW_UP,    Event::Key(Key::AltArrow(Arrow::Up))),
      (&XTERM_ARROW_DOWN,      Event::Key(Key::Arrow(Arrow::Down))),
      (&XTERM_ARROW_DOWN_2,    Event::Key(Key::Arrow(Arrow::Down))),
      (&XTERM_ARROW_LEFT,      Event::Key(Key::Arrow(Arrow::Left))),
      (&XTERM_ARROW_LEFT_2,    Event::Key(Key::Arrow(Arrow::Left))),
      (&XTERM_ARROW_RIGHT,     Event::Key(Key::Arrow(Arrow::Right))),
      (&XTERM_ARROW_RIGHT_2,   Event::Key(Key::Arrow(Arrow::Right))),
      (&XTERM_ARROW_UP,        Event::Key(Key::Arrow(Arrow::Up))),
      (&XTERM_ARROW_UP_2,      Event::Key(Key::Arrow(Arrow::Up))),
      (&XTERM_CTRL_ARROW_DOWN, Event::Key(Key::CtrlArrow(Arrow::Down))),
      (&XTERM_CTRL_ARROW_LEFT, Event::Key(Key::CtrlArrow(Arrow::Left))),
      (&XTERM_CTRL_ARROW_RIGHT,Event::Key(Key::CtrlArrow(Arrow::Right))),
      (&XTERM_CTRL_ARROW_UP,   Event::Key(Key::CtrlArrow(Arrow::Up))),
      (&XTERM_DEL,             Event::Key(Key::Del)),
      (&XTERM_PAGE_DOWN,       Event::Key(Key::PageDown)),
      (&XTERM_PAGE_UP,         Event::Key(Key::PageUp)),
      (&XTERM_SHIFT_UP,        Event::Key(Key::ShiftUp)),
      (&XTERM_SHIFT_DOWN,      Event::Key(Key::ShiftDown)),
      (&XTERM_HOME,            Event::Key(Key::Home)),
      (&XTERM_END,             Event::Key(Key::End)),
      (&XTERM_HOME_2,          Event::Key(Key::Home)),
      (&XTERM_END_2,           Event::Key(Key::End)),
      (&XTERM_FOCUS_GAINED,    Event::FocusGained),
      (&XTERM_FOCUS_LOST,      Event::FocusLost),
    ];

// Make sure not to use 27 (ESC) because it's used as a prefix in many combinations.
static XTERM_SINGLE_BYTES : [(u8, Event); 13] =
    [ (9,   Event::Key(Key::Tab)),
      (127, Event::Key(Key::Backspace)),
      (1,   Event::Key(Key::Ctrl('a'))),
      (5,   Event::Key(Key::Ctrl('e'))),
      (23,  Event::Key(Key::Ctrl('w'))),
      (11,  Event::Key(Key::Ctrl('k'))),
      (4,   Event::Key(Key::Ctrl('d'))),
      (3,   Event::Key(Key::Ctrl('c'))),
      (17,  Event::Key(Key::Ctrl('q'))),
      (16,  Event::Key(Key::Ctrl('p'))),
      (14,  Event::Key(Key::Ctrl('n'))),
      (21,  Event::Key(Key::Ctrl('u'))),
      (24,  Event::Key(Key::Ctrl('x'))),
    ];

////////////////////////////////////////////////////////////////////////////////////////////////////

impl Input {
    pub fn new() -> Input {
        unsafe {
            // ignore the existing handler
            sigaction(Signal::SIGWINCH, &SigAction::new(SigHandler::Handler(sigwinch_handler),
                                                        signal::SA_RESTART,
                                                        SigSet::empty())).unwrap();
        }

        Input {
            buf: Vec::with_capacity(100),
        }
    }

    /// Fill the event buffer with pending events. Does not block.
    pub fn read_input_events(&mut self, evs: &mut Vec<Event>) {
        evs.clear();

        if GOT_SIGWINCH.swap(false, Ordering::Relaxed) {
            evs.push(Event::Resize);
        }

        if read_stdin(&mut self.buf) {

            let mut buf_slice: &[u8] = &self.buf;

            // read_stdin() returned true, there should be at least one
            // character in the buffer
            debug_assert!(!buf_slice.is_empty());

            while !buf_slice.is_empty() {
                // Special treatment for 127 (backspace) and 13 ('\r')
                let fst = buf_slice[0];
                let read_fn = if (fst < 32 && fst != 13) || fst == 127 {
                    read_key_comb
                } else {
                    read_chars
                };

                match read_fn(buf_slice, evs) {
                    Some(buf_slice_) => {
                        buf_slice = buf_slice_;
                    },
                    None => {
                        evs.push(Event::Unknown(buf_slice.to_owned()));
                        break;
                    }
                }
            }
        }
    }
}

fn read_chars<'a>(mut buf_slice: &'a [u8], evs: &mut Vec<Event>) -> Option<&'a [u8]> {
    debug_assert!(!buf_slice.is_empty());

    // Use a fast path for the common case: Single utf-8 character.
    // (TODO: What about other encodings?)

    utf8_char_len(buf_slice[0]).map(|char_len| {
        if char_len as usize == buf_slice.len() {
            // fast path: single character
            evs.push(Event::Key(Key::Char(get_utf8_char(buf_slice, char_len))));
            &buf_slice[char_len as usize ..]
        } else {
            // probably a paste: allocate a string and collect chars
            let mut string = String::with_capacity(10);
            loop {
                if buf_slice.is_empty() { break; }
                match utf8_char_len(buf_slice[0]) {
                    Some(char_len) => {
                        string.push(get_utf8_char(buf_slice, char_len));
                        buf_slice = &buf_slice[char_len as usize ..];
                    },
                    None => { break; }
                }
            }
            evs.push(Event::String(string));
            return buf_slice;
        }
    })
}

fn read_key_comb<'a>(buf_slice: &'a [u8], evs: &mut Vec<Event>) -> Option<&'a [u8]> {
    debug_assert!(!buf_slice.is_empty());

    // TODO: This is not working, see https://github.com/rust-lang/rust/issues/36401
    for &(byte, ref ev) in XTERM_SINGLE_BYTES.iter() {
        if byte == buf_slice[0] {
            evs.push(ev.clone());
            return Some(&buf_slice[1 ..]);
        }
    }

    for &(byte_seq, ref ev) in XTERM_KEY_SEQS.iter() {
        if buf_slice.starts_with(byte_seq) {
            evs.push(ev.clone());
            return Some(&buf_slice[byte_seq.len() ..]);
        }
    }

    if buf_slice[0] == 27 {
        // 27 not followed by anything is an actual ESC
        if buf_slice.len() == 1 {
            evs.push(Event::Key(Key::Esc));
            return Some(&buf_slice[1 ..]);
        }
        // otherwise it's probably alt + key
        else {
            debug_assert!(buf_slice.len() >= 2);
            return utf8_char_len(buf_slice[1]).map(|char_len| {
                evs.push(Event::Key(Key::AltChar(get_utf8_char(&buf_slice[1..], char_len))));
                &buf_slice[char_len as usize + 1 ..]
            });
        }
    }

    None
}

fn utf8_char_len(byte: u8) -> Option<u8> {
    if      byte >> 7 == 0b0     { Some(1) }
    else if byte >> 5 == 0b110   { Some(2) }
    else if byte >> 4 == 0b1110  { Some(3) }
    else if byte >> 3 == 0b11110 { Some(4) }
    else                         { None    }
}

fn get_utf8_char(buf: &[u8], len: u8) -> char {
    let codepoint : u32 = {
        if len == 1 {
            (buf[0] & 0b0111_1111) as u32
        } else if len == 2 {
            (((buf[0] & 0b0001_1111) as u32) << 6) +
             ((buf[1] & 0b0011_1111) as u32)
        } else if len == 3 {
            (((buf[0] & 0b0000_1111) as u32) << 12) +
            (((buf[1] & 0b0011_1111) as u32) <<  6) +
             ((buf[2] & 0b0011_1111) as u32)
        } else {
            debug_assert!(len == 4);
            (((buf[0] & 0b0000_0111) as u32) << 18) +
            (((buf[1] & 0b0011_1111) as u32) << 12) +
            (((buf[2] & 0b0011_1111) as u32) <<  6) +
             ((buf[3] & 0b0011_1111) as u32)
        }
    };

    char::from_u32(codepoint).unwrap()
}

/// Read stdin contents if it's ready for reading. Returns true when it was able to read. Buffer is
/// not modified when return value is 0.
pub fn read_stdin(buf : &mut Vec<u8>) -> bool {
    let mut bytes_available : i32 = 0; // this really needs to be a 32-bit value
    let ioctl_ret = unsafe { libc::ioctl(libc::STDIN_FILENO, libc::FIONREAD, &mut bytes_available) };
    // println!("ioctl_ret: {}", ioctl_ret);
    // println!("bytes_available: {}", bytes_available);
    if ioctl_ret < 0 || bytes_available == 0 {
        false
    } else {
        buf.clear();
        buf.reserve(bytes_available as usize);

        let buf_ptr : *mut libc::c_void = buf.as_ptr() as *mut libc::c_void;
        let bytes_read = unsafe { libc::read(libc::STDIN_FILENO, buf_ptr, bytes_available as usize) };
        debug_assert!(bytes_read == bytes_available as isize);

        unsafe { buf.set_len(bytes_read as usize); }
        true
    }
}