terminal-input 0.1.0

Cross-terminal precise decoding of modified keys and other input events
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use std::io::Write;
use std::ffi::CStr;
use const_cstr::ConstCStr;

use crate::Event::*;
use crate::{Event, Modifiers, KeyInput};

mod ext;

fn write_now(data: &[u8]) -> Result<(), std::io::Error> {
    let stdout = std::io::stdout();
    let mut lock = stdout.lock();
    lock.write_all(data)?;
    lock.flush()?;
    Ok(())
}

struct BracketedPaste {
    _priv: ()
}

impl Drop for BracketedPaste {
    fn drop(&mut self) {
        let _ = write_now(b"\x1b[?2004l");
    }
}

impl BracketedPaste {
    fn start() -> Option<BracketedPaste> {
        write_now(b"\x1b[?2004h").ok()?;
        Some(BracketedPaste { _priv: () })
    }
}

struct XTermModifyOtherKeys {
    _priv: ()
}

impl Drop for XTermModifyOtherKeys {
    fn drop(&mut self) {
        let _ = write_now(b"\x1b[>4n");
    }
}

impl XTermModifyOtherKeys {
    fn start() -> Option<XTermModifyOtherKeys> {
        write_now(b"\x1b[>4;2m").ok()?;
        Some(XTermModifyOtherKeys { _priv: () })
    }
}

enum XTermModifyKeyState {
    Off,
    ParsingMode(u32),
    ParsingChar(u32, u32)
}

struct KittyFullMode {
    _priv: ()
}

impl Drop for KittyFullMode {
    fn drop(&mut self) {
        let _ = write_now(b"\x1b[?2017l");
    }
}

impl KittyFullMode {
    fn start() -> Option<KittyFullMode> {
        write_now(b"\x1b[?2017h").ok()?;
        Some(KittyFullMode { _priv: () })
    }
}

#[derive(Copy, Clone, Debug)]
enum KeyType {
    Press,
    Release,
    Repeat
}

enum KittyFullModeState {
    Off,
    ParsingType,
    ParsingModifiers(KeyType),
    ParsingKey(KeyType, u8, u32)
}

pub struct InputStream {
    _bracketed_paste: Option<BracketedPaste>,
    _xterm_modify_keys: Option<XTermModifyOtherKeys>,
    _kitty_full_mode: Option<KittyFullMode>,

    extra_bound_keys: Vec<(i32, Event)>,
    
    in_progress_codepoint: u32,
    utf8_bytes_left: usize,
    xterm_modify_key_state: XTermModifyKeyState,
    kitty_full_mode_state: KittyFullModeState

}

unsafe fn define_if_necessary(def: &std::ffi::CStr, code: std::os::raw::c_int) -> Result<(), ()> {
    if ext::key_code_for(def) == Err(ext::KeyError::NotDefined) {
        ext::define_key_code(def, code)
    } else {
        Ok(())
    }
}

// TODO: instead parse any keys with numbers after them according to this pattern
const KNOWN_EXTRA_TERM_CAPABILITIES: &'static [(ConstCStr, Event)] = &[
    (const_cstr!("kDC3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),
    (const_cstr!("kDC4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),
    (const_cstr!("kDC5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),
    (const_cstr!("kDC6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),
    (const_cstr!("kDC7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),
    (const_cstr!("kDC8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_DC), is_repeat: false }),

    (const_cstr!("kLFT3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),
    (const_cstr!("kLFT4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),
    (const_cstr!("kLFT5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),
    (const_cstr!("kLFT6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),
    (const_cstr!("kLFT7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),
    (const_cstr!("kLFT8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_LEFT), is_repeat: false }),

    (const_cstr!("kRIT3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),
    (const_cstr!("kRIT4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),
    (const_cstr!("kRIT5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),
    (const_cstr!("kRIT6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),
    (const_cstr!("kRIT7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),
    (const_cstr!("kRIT8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_RIGHT), is_repeat: false }),

    (const_cstr!("kUP3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),
    (const_cstr!("kUP4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),
    (const_cstr!("kUP5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),
    (const_cstr!("kUP6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),
    (const_cstr!("kUP7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),
    (const_cstr!("kUP8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_UP), is_repeat: false }),

    (const_cstr!("kDN3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),
    (const_cstr!("kDN4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),
    (const_cstr!("kDN5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),
    (const_cstr!("kDN6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),
    (const_cstr!("kDN7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),
    (const_cstr!("kDN8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_DOWN), is_repeat: false }),

    (const_cstr!("kHOM3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),
    (const_cstr!("kHOM4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),
    (const_cstr!("kHOM5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),
    (const_cstr!("kHOM6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),
    (const_cstr!("kHOM7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),
    (const_cstr!("kHOM8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_HOME), is_repeat: false }),

    (const_cstr!("kEND3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),
    (const_cstr!("kEND4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),
    (const_cstr!("kEND5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),
    (const_cstr!("kEND6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),
    (const_cstr!("kEND7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),
    (const_cstr!("kEND8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_END), is_repeat: false }),

    (const_cstr!("kPRV3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),
    (const_cstr!("kPRV4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),
    (const_cstr!("kPRV5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),
    (const_cstr!("kPRV6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),
    (const_cstr!("kPRV7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),
    (const_cstr!("kPRV8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_PPAGE), is_repeat: false }),

    (const_cstr!("kNXT3"), KeyPress { modifiers: Modifiers(3 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
    (const_cstr!("kNXT4"), KeyPress { modifiers: Modifiers(4 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
    (const_cstr!("kNXT5"), KeyPress { modifiers: Modifiers(5 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
    (const_cstr!("kNXT6"), KeyPress { modifiers: Modifiers(6 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
    (const_cstr!("kNXT7"), KeyPress { modifiers: Modifiers(7 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
    (const_cstr!("kNXT8"), KeyPress { modifiers: Modifiers(8 - 1), key: KeyInput::Special(ncurses::KEY_NPAGE), is_repeat: false }),
];

impl InputStream {
    pub unsafe fn init(window: ncurses::WINDOW) -> Self {
        // TODO: error handling?
        ncurses::ll::keypad(window, true as ncurses::ll::c_bool);
        ncurses::ll::raw();
        ncurses::ll::noecho();

        // TODO: Make this configurable to allow for mouse movements and for clicks
        ncurses::ll::mousemask(ncurses::ALL_MOUSE_EVENTS as ncurses::mmask_t, core::ptr::null_mut());
        ncurses::ll::mouseinterval(0); // We care about up/down, not clicks

        // Start bracketed paste mode, but only if we can successfully handle the brackets
        // TODO: Should we query support first?
        let bracketed_paste_guard = if ext::define_key_code(const_cstr!("\x1b[200~").as_cstr(), 2000).is_ok() &&
                                       ext::define_key_code(const_cstr!("\x1b[201~").as_cstr(), 2001).is_ok() {
            BracketedPaste::start()
        } else {
            None
        };

        let xterm_modify_other_keys_guard = if ext::define_key_code(const_cstr!("\x1b[27;").as_cstr(), 2100).is_ok() {
            XTermModifyOtherKeys::start()
        } else {
            None
        };

        // TODO: Should we query support first?
        let kitty_full_mode_guard = if ext::define_key_code(const_cstr!("\x1b_K").as_cstr(), 2200).is_ok() &&
                                       ext::define_key_code(const_cstr!("\x1b\\").as_cstr(), 2201).is_ok() {
            KittyFullMode::start()
        } else {
            None
        };

        // We use Esc heavily and modern computers are quite fast, so unless the user has overridden it directly,
        // set ESCDELAY to a small 25ms. The normal default of 1 second is too high.
        // TODO: If one of the other protocols causes the Esc key to be sent unambiguously, increase this value significantly
        if std::env::var_os("ESCDELAY").is_none() {
            ncurses::ll::set_escdelay(25);
        }

        let mut extra_bound_keys = Vec::new();
        for &(name, inp) in KNOWN_EXTRA_TERM_CAPABILITIES {
            if let Some(description) = ext::get_terminfo_string(name.as_cstr()) {
                if let Ok(code) = ext::key_code_for(description) {
                    extra_bound_keys.push((code, inp));
                }
            }
        }

        // Hackily detect if our terminal is using rxvt-style codes and add the rest if necessary. Note that this
        // should never override an existing binding, so it shouldn't cause problems even if it happens to be enabled
        // on a terminal that uses different bindings.
        if ext::key_code_for(const_cstr!("\x1b[A").as_cstr()) == Ok(ncurses::KEY_UP) &&
           ext::key_code_for(const_cstr!("\x1b[B").as_cstr()) == Ok(ncurses::KEY_DOWN) &&
           ext::key_code_for(const_cstr!("\x1b[C").as_cstr()) == Ok(ncurses::KEY_RIGHT) &&
           ext::key_code_for(const_cstr!("\x1b[D").as_cstr()) == Ok(ncurses::KEY_LEFT) &&
           ext::key_code_for(const_cstr!("\x1b[c").as_cstr()) == Ok(ncurses::KEY_SRIGHT) &&
           ext::key_code_for(const_cstr!("\x1b[d").as_cstr()) == Ok(ncurses::KEY_SLEFT) {

            let _ = define_if_necessary(const_cstr!("\x1bOa").as_cstr(), 2340);
            let _ = define_if_necessary(const_cstr!("\x1bOb").as_cstr(), 2341);
            let _ = define_if_necessary(const_cstr!("\x1bOc").as_cstr(), 2342);
            let _ = define_if_necessary(const_cstr!("\x1bOd").as_cstr(), 2343);
            // And AltSendsEscape versions as well (TODO: fold into a general AltSendsEscape mechanism)
            let _ = define_if_necessary(const_cstr!("\x1b\x1bOa").as_cstr(), 2360);
            let _ = define_if_necessary(const_cstr!("\x1b\x1bOb").as_cstr(), 2361);
            let _ = define_if_necessary(const_cstr!("\x1b\x1bOc").as_cstr(), 2362);
            let _ = define_if_necessary(const_cstr!("\x1b\x1bOd").as_cstr(), 2363);

            let _ = define_if_necessary(const_cstr!("\x1b\x1b[A").as_cstr(), 2320);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[B").as_cstr(), 2321);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[C").as_cstr(), 2322);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[D").as_cstr(), 2323);

            let _ = define_if_necessary(const_cstr!("\x1b\x1b[a").as_cstr(), 2330);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[b").as_cstr(), 2331);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[c").as_cstr(), 2332);
            let _ = define_if_necessary(const_cstr!("\x1b\x1b[d").as_cstr(), 2333);

            if ext::key_code_for(const_cstr!("\x1b[3~").as_cstr()) == Ok(ncurses::KEY_DC) {
                let _ = define_if_necessary(const_cstr!("\x1b[3^").as_cstr(), 2348);
                let _ = define_if_necessary(const_cstr!("\x1b\x1b[3^").as_cstr(), 2368);
            }
        }

        // Hackily detect if our terminal is using XTerm-style codes and add the rest if necessary
        if ext::key_code_for(const_cstr!("\x1bOA").as_cstr()) == Ok(ncurses::KEY_UP) &&
           ext::key_code_for(const_cstr!("\x1bOB").as_cstr()) == Ok(ncurses::KEY_DOWN) &&
           ext::key_code_for(const_cstr!("\x1bOC").as_cstr()) == Ok(ncurses::KEY_RIGHT) &&
           ext::key_code_for(const_cstr!("\x1bOD").as_cstr()) == Ok(ncurses::KEY_LEFT) &&
           ext::key_code_for(const_cstr!("\x1b[1;2C").as_cstr()) == Ok(ncurses::KEY_SRIGHT) &&
           ext::key_code_for(const_cstr!("\x1b[1;2D").as_cstr()) == Ok(ncurses::KEY_SLEFT) {

            for mode in 2..=7 {
                for &(indicator, key) in &[(b'A', 0), (b'B', 1), (b'C', 2), (b'D', 3), (b'H', 4), (b'F', 5)] {
                    let _ = define_if_necessary(
                        CStr::from_bytes_with_nul(&[0x1b, b'[', b'1', b';', b'1' + mode as u8, indicator, 0]).unwrap(),
                        2300 + mode * 10 + key
                    );
                }
            }

            if ext::key_code_for(const_cstr!("\x1b[3~").as_cstr()) == Ok(ncurses::KEY_DC) &&
               ext::key_code_for(const_cstr!("\x1b[5~").as_cstr()) == Ok(ncurses::KEY_PPAGE) &&
               ext::key_code_for(const_cstr!("\x1b[6~").as_cstr()) == Ok(ncurses::KEY_NPAGE) {

                for mode in 2..=7 {
                    for &(indicator, key) in &[(b'3', 8), (b'5', 6), (b'6', 7)] {
                        let _ = define_if_necessary(
                            CStr::from_bytes_with_nul(&[0x1b, b'[', indicator, b';', b'1' + mode as u8, b'~', 0]).unwrap(),
                            2300 + mode * 10 + key
                        );
                    }
                }
            }
        }

        // TODO: What about in front of, e.g., arrow keys? Generalize this.
        // Brute-force handle the most common cases for AltSendsEscape
        for byte in (1..=26).chain(48..=57).chain(65..=90).chain(97..=122) {
            let _ = define_if_necessary(CStr::from_bytes_with_nul(&[0x1b, byte as u8, 0]).unwrap(), 3000 + byte);
        }

        ncurses::ll::ungetch(ncurses::KEY_RESIZE);

        InputStream {
            _bracketed_paste: bracketed_paste_guard,
            _xterm_modify_keys: xterm_modify_other_keys_guard,
            _kitty_full_mode: kitty_full_mode_guard,

            extra_bound_keys: extra_bound_keys,

            in_progress_codepoint: 0,
            utf8_bytes_left: 0,
            xterm_modify_key_state: XTermModifyKeyState::Off,
            kitty_full_mode_state: KittyFullModeState::Off
        }
    }

    pub fn next_event(&mut self, window: ncurses::WINDOW) -> Result<Event, ()> {
        use crate::KeyInput::*;
        const NONE: Modifiers = crate::Modifiers::NONE;
        const CTRL: Modifiers = crate::Modifiers::CTRL;
        const ALT: Modifiers = crate::Modifiers::ALT;
        const SHIFT: Modifiers = crate::Modifiers::SHIFT;

        loop {
            let curses_input = unsafe { ncurses::ll::wgetch(window) };
            if curses_input == ncurses::ERR {
                return Err(());
            }

            let input;
            // We need to parse utf8.
            if curses_input < 256 {
                let byte = curses_input as u8;
                if self.utf8_bytes_left == 0 {
                    // New character
                    if byte >> 7 == 0b0 {
                        self.utf8_bytes_left = 0;
                        self.in_progress_codepoint = (byte & 0x7f) as u32;
                    } else if byte >> 5 == 0b110 {
                        self.utf8_bytes_left = 1;
                        self.in_progress_codepoint = (byte & 0x1f) as u32;
                    } else if byte >> 4 == 0b1110 {
                        self.utf8_bytes_left = 2;
                        self.in_progress_codepoint = (byte & 0x0f) as u32;
                    } else if byte >> 3 == 0b11110 {
                        self.utf8_bytes_left = 3;
                        self.in_progress_codepoint = (byte & 0x07) as u32;
                    } else {
                        return Ok(KeyPress { modifiers: NONE, key: Byte(byte), is_repeat: false });
                    }
                } else if byte >> 6 == 0b10 {
                    // Continuation bytes
                    self.utf8_bytes_left -= 1;
                    self.in_progress_codepoint = (self.in_progress_codepoint << 6) | ((byte & 0x3f) as u32);
                } else {
                    return Ok(KeyPress { modifiers: NONE, key: Byte(byte), is_repeat: false });
                }
                if self.utf8_bytes_left == 0 {
                    // FIXME: This should not crash
                    input = Codepoint(std::char::from_u32(self.in_progress_codepoint).expect("BUG: Bad char cast"));
                } else {
                    continue;
                }
            } else {
                input = Special(curses_input);
            }

            // Translate keys bound to non-standard terminfo entries
            if let Special(code) = input {
                for &(possible_code, possible_inp) in &self.extra_bound_keys {
                    if possible_code == code {
                        return Ok(possible_inp);
                    }
                }
            }

            // Translate various known special keys to a decomposed form
            match input {
                // Non-key inputs
                Special(ncurses::KEY_RESIZE) => {
                    let mut height = 0;
                    let mut width = 0;
                    ncurses::getmaxyx(window, &mut height, &mut width);
                    return Ok(Resize {
                        width: width as u32,
                        height: height as u32
                    });
                },
                Special(ncurses::KEY_MOUSE) => {
                    let mut event = ncurses::ll::MEVENT {
                        id: 0,
                        x: 0,
                        y: 0,
                        z: 0,
                        bstate: 0
                    };
                    if ncurses::getmouse(&mut event) != ncurses::OK {
                        return Err(());
                    }
                    return Ok(Mouse {
                        device_id: event.id as u16,
                        x: event.x as u32,
                        y: event.y as u32,
                        buttons: event.bstate & !((ncurses::BUTTON_CTRL | ncurses::BUTTON_ALT | ncurses::BUTTON_SHIFT) as u32),
                        modifiers: if event.bstate & (ncurses::BUTTON_CTRL as u32)  != 0 { CTRL  } else { NONE }
                                 | if event.bstate & (ncurses::BUTTON_ALT as u32)   != 0 { ALT   } else { NONE }
                                 | if event.bstate & (ncurses::BUTTON_SHIFT as u32) != 0 { SHIFT } else { NONE }
                    });
                }
                Special(2000) => return Ok(PasteBegin),
                Special(2001) => return Ok(PasteEnd),
                // Shifted standard keys
                Special(ncurses::KEY_SLEFT)  => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_LEFT), is_repeat: false }),
                Special(ncurses::KEY_SRIGHT) => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_RIGHT), is_repeat: false }),
                Special(ncurses::KEY_SR)     => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_UP), is_repeat: false }),
                Special(ncurses::KEY_SF)     => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_DOWN), is_repeat: false }),
                Special(ncurses::KEY_SHOME)  => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_HOME), is_repeat: false }),
                Special(ncurses::KEY_SEND)   => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_END), is_repeat: false }),
                Special(ncurses::KEY_SDC)    => return Ok(KeyPress { modifiers: SHIFT, key: Special(ncurses::KEY_DC), is_repeat: false }),
                Special(ncurses::KEY_BTAB)   => return Ok(KeyPress { modifiers: SHIFT, key: Codepoint('\t'), is_repeat: false }),
                // Ctrl+Z triggers a suspend
                Special(ncurses::KEY_SUSPEND) => return Ok(KeyPress { modifiers: CTRL, key: Codepoint('z'), is_repeat: false }),
                // The DEL and BACKSPACE have different meanings, but since they are inconsistently assigned, we unify them into one code
                Codepoint('\u{7f}') => return Ok(KeyPress { modifiers: NONE, key: Special(ncurses::KEY_BACKSPACE), is_repeat: false }),
                // Assume that control characters aren't from actual typing and are instead generated by Ctrl + a printable character
                Codepoint(chr) if (chr as u32) < 27 && chr != '\t' && chr != '\n' && chr != '\u{8}'
                    => return Ok(KeyPress { modifiers: CTRL, key: Codepoint(std::char::from_u32(chr as u32 + 96).unwrap()), is_repeat: false }),
                Codepoint(chr) if (chr as u32) > 128 && (chr as u32) < 155 // TODO: Consider whitelist? Cancel is sometimes used for Backspace
                    => return Ok(KeyPress { modifiers: CTRL | ALT, key: Codepoint(std::char::from_u32(chr as u32 - 32).unwrap()), is_repeat: false }),
                // AltSendsEscape + either a control character (assumed to be from Ctrl) or a printable character
                Special(code @ 3001..=3026) => return Ok(KeyPress { modifiers: CTRL | ALT, key: Codepoint(std::char::from_u32(code as u32 - 3000 + 96).unwrap()), is_repeat: false}),
                Special(code @ 3000..=3255) => return Ok(KeyPress { modifiers: ALT, key: Codepoint(std::char::from_u32(code as u32 - 3000).unwrap()), is_repeat: false }),
                // XTerm-style modified keys that weren't in the Terminfo
                Special(code @ 2300..=2399) => {
                    let base_code = code - 2300;
                    let modifiers = Modifiers((base_code / 10) as u8);
                    match base_code % 10 {
                        0 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_UP), is_repeat: false }),
                        1 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_DOWN), is_repeat: false }),
                        2 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_RIGHT), is_repeat: false }),
                        3 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_LEFT), is_repeat: false }),
                        4 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_HOME), is_repeat: false }),
                        5 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_END), is_repeat: false }),
                        6 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_PPAGE), is_repeat: false }),
                        7 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_NPAGE), is_repeat: false }),
                        8 => return Ok(KeyPress { modifiers: modifiers, key: Special(ncurses::KEY_DC), is_repeat: false }),
                        _ => { }
                    }
                },
                _ => { }
            }

            // Handle XTerm's modifyOtherKeys extension, parsing manually
            if let Special(2100) = input {
                self.xterm_modify_key_state = XTermModifyKeyState::ParsingMode(0);
                continue;
            }
            match self.xterm_modify_key_state {
                XTermModifyKeyState::Off => { },
                XTermModifyKeyState::ParsingMode(mode_so_far) => {
                    if let Codepoint(chr) = input {
                        if let Some(digit) = chr.to_digit(10) {
                            self.xterm_modify_key_state = XTermModifyKeyState::ParsingMode(mode_so_far * 10 + digit);
                            continue;
                        } else if chr == ';' {
                            self.xterm_modify_key_state = XTermModifyKeyState::ParsingChar(mode_so_far, 0);
                            continue;
                        }
                    }
                },
                XTermModifyKeyState::ParsingChar(mode, char_so_far) => {
                    if let Codepoint(chr) = input {
                        if let Some(digit) = chr.to_digit(10) {
                            self.xterm_modify_key_state = XTermModifyKeyState::ParsingChar(mode, char_so_far * 10 + digit);
                            continue;
                        } else if chr == '~' {
                            self.xterm_modify_key_state = XTermModifyKeyState::Off;
                            if 1 <= mode {
                                // FIXME: This should not crash
                                return Ok(KeyPress { modifiers: Modifiers((mode as u8) - 1), key: Codepoint(std::char::from_u32(char_so_far).unwrap()), is_repeat: false });
                            } else {
                                eprintln!("0 mode?");
                                continue;
                            }
                        }
                    }
                }
            }

            // Handle Kitty's full mode extension, parsing manually
            if let Special(2200) = input {
                self.kitty_full_mode_state = KittyFullModeState::ParsingType;
                continue;
            }
            match self.kitty_full_mode_state {
                KittyFullModeState::Off => { },
                KittyFullModeState::ParsingType => match input {
                    Codepoint('p') => {
                        self.kitty_full_mode_state = KittyFullModeState::ParsingModifiers(KeyType::Press);
                        continue;
                    },
                    Codepoint('r') => {
                        self.kitty_full_mode_state = KittyFullModeState::ParsingModifiers(KeyType::Release);
                        continue;
                    },
                    Codepoint('t') => {
                        self.kitty_full_mode_state = KittyFullModeState::ParsingModifiers(KeyType::Repeat);
                        continue;
                    },
                    _ => { }
                },
                KittyFullModeState::ParsingModifiers(key_type) => {
                    if let Codepoint(chr) = input {
                        // Decode base 64
                        let decoded = if 'A' <= chr && chr <= 'Z' {
                            Some(chr as u32 - 'A' as u32)
                        } else if 'a' <= chr && chr <= 'z' {
                            Some(chr as u32 - 'a' as u32 + 26)
                        } else if '0' <= chr && chr <= '9' {
                            Some(chr as u32 - '0' as u32 + 52)
                        } else if chr == '+' {
                            Some(62)
                        } else if chr == '/' {
                            Some(63)
                        } else {
                            None
                        };
                        if let Some(mode) = decoded {
                            self.kitty_full_mode_state = KittyFullModeState::ParsingKey(key_type, mode as u8, 0);
                            continue;
                        }
                    }
                },
                KittyFullModeState::ParsingKey(key_type, mode, key_so_far) => {
                    if let Codepoint(chr) = input {
                        let decoded = if 'A' <= chr && chr <= 'Z' {
                            Some(chr as u32 - 'A' as u32)
                        } else if 'a' <= chr && chr <= 'z' {
                            Some(chr as u32 - 'a' as u32 + 26)
                        } else if '0' <= chr && chr <= '9' {
                            Some(chr as u32 - '0' as u32 + 52)
                        } else {
                            ".-:+=^!/*?&<>()[]{}@%$#".chars().position(|c| c == chr).map(|i| i as u32 + 62)
                        };
                        if let Some(value) = decoded {
                            self.kitty_full_mode_state = KittyFullModeState::ParsingKey(key_type, mode, key_so_far * 85 + value);
                            continue;
                        }
                    } else if let Special(2201) = input {
                        self.kitty_full_mode_state = KittyFullModeState::Off;
                        let modifiers = Modifiers(mode);
                        // FIXME: more complete translation, unify different input types
                        let translated = match key_so_far {
                            6..=15 => if modifiers & SHIFT != NONE {
                                // FIXME capitalize numbers?
                                Special(key_so_far as i32 + 600)
                            } else {
                                Codepoint(std::char::from_u32('0' as u32 + key_so_far as u32 - 6).unwrap())
                            },
                            18..=43 => if modifiers & SHIFT != NONE { // If shift, capitalize the letter
                                Codepoint(std::char::from_u32('A' as u32 + key_so_far as u32 - 18).unwrap())
                            } else {
                                Codepoint(std::char::from_u32('a' as u32 + key_so_far as u32 - 18).unwrap())
                            },
                            50 => Codepoint('\u{1b}'), // Escape
                            52 => Codepoint('\t'),
                            53 => Special(ncurses::KEY_BACKSPACE),
                            55 => Special(ncurses::KEY_DC),
                            56 => Special(ncurses::KEY_RIGHT),
                            57 => Special(ncurses::KEY_LEFT),
                            58 => Special(ncurses::KEY_DOWN),
                            59 => Special(ncurses::KEY_UP),
                            60 => Special(ncurses::KEY_PPAGE),
                            61 => Special(ncurses::KEY_NPAGE),
                            62 => Special(ncurses::KEY_HOME),
                            63 => Special(ncurses::KEY_END),
                            69 => Special(ncurses::KEY_F1),
                            _ => Special(key_so_far as i32 + 600)
                        };
                        return Ok(match key_type {
                            KeyType::Press   => KeyPress { modifiers: modifiers, key: translated, is_repeat: false },
                            KeyType::Repeat  => KeyPress { modifiers: modifiers, key: translated, is_repeat: true },
                            KeyType::Release => KeyRelease { modifiers: modifiers, key: translated },
                        });
                    }
                }
            }

            return Ok(KeyPress { modifiers: NONE, key: input, is_repeat: false })
        }
    }
}