-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinput.rs
More file actions
432 lines (395 loc) · 14 KB
/
input.rs
File metadata and controls
432 lines (395 loc) · 14 KB
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
/*
* Copyright (c) 2014-2021 Mathias Hällman
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
use std::thread;
use futures::channel::mpsc;
use futures::channel::oneshot;
use futures::{FutureExt, StreamExt};
use crate::keymap::{Key, KeyMod, KeySym};
#[cfg(not(test))]
const STDIN_FILENO: libc::c_int = 0;
/*
* TermInput is returned when starting listening for key events on a file
* descriptor. It controls the life time of the terminal input loop. When the
* TermInput is dropped, the terminal input loop will finish.
*/
pub struct TermInput {
kill_tx: Option<oneshot::Sender<()>>,
died_rx: Option<oneshot::Receiver<()>>,
}
impl Drop for TermInput {
fn drop(&mut self) {
self.kill_tx
.take()
.expect("TermInput already killed.")
.send(())
.expect("Input thread died prematurely.");
tokio::runtime::Builder::new_current_thread()
.build()
.expect("Runtime started.")
.block_on(self.died_rx.take().expect("TermInput already killed."))
.expect("Input thread died prematurely.");
}
}
// start listening for terminal input on stdin
#[cfg(not(test))]
pub fn start(key_tx: mpsc::UnboundedSender<Key>) -> TermInput {
start_on_fd(STDIN_FILENO, key_tx)
}
// start listening for terminal input on the specified file descriptor
pub fn start_on_fd(fd: libc::c_int, key_tx: mpsc::UnboundedSender<Key>) -> TermInput {
let (kill_tx, kill_rx) = oneshot::channel();
let (died_tx, died_rx) = oneshot::channel();
thread::spawn(move || input_loop(kill_rx, died_tx, key_tx, fd));
TermInput {
kill_tx: Some(kill_tx),
died_rx: Some(died_rx),
}
}
/*
* Helper module for detecting available bytes on the file descriptor being
* listened to.
*/
mod libc_poll {
use libc::{c_int, c_long, c_short};
#[repr(C)]
struct Pollfd {
fd: c_int,
events: c_short,
revents: c_short,
}
extern "C" {
fn poll(fds: *mut Pollfd, nfds: c_long, timeout: c_int) -> c_int;
}
#[derive(PartialEq)]
pub enum PollResult {
Ready,
Timeout,
}
pub fn poll_fd(fd: c_int, timeout_ms: u16) -> PollResult {
const POLLIN: c_short = 1;
let mut pollfd = Pollfd {
fd,
events: POLLIN,
revents: 0,
};
let num_fds = 1;
match (unsafe { poll(&mut pollfd, num_fds, timeout_ms as c_int) }).cmp(&0) {
std::cmp::Ordering::Less => panic!("Unable to poll stdin."),
std::cmp::Ordering::Greater => PollResult::Ready,
std::cmp::Ordering::Equal => PollResult::Timeout,
}
}
}
/*
* Events to the input loop.
*/
#[derive(Clone)]
enum Event {
Continue,
Break,
}
fn input_loop(
kill_rx: oneshot::Receiver<()>,
died_tx: oneshot::Sender<()>,
key_tx: mpsc::UnboundedSender<Key>,
fd: libc::c_int,
) {
let mut tk = termkey::TermKey::new(fd, termkey::c::Flag::CTRLC);
let inf = futures::stream::repeat::<Event>(Event::Continue);
let killer = kill_rx.into_stream().map(|_| Event::Break);
let mut event_stream = futures::stream::select(inf, killer);
let mut input_loop = || {
// check for available input
let poll_timeout_ms = 10;
if libc_poll::poll_fd(fd, poll_timeout_ms) == libc_poll::PollResult::Ready {
// tell termkey that there's input available
tk.advisereadable();
}
// empty the fd, translating and sending key events
loop {
if let Some(key) = match tk.getkey() {
termkey::Result::None_ => break, // got nothing, done here
termkey::Result::Eof => panic!("stdin closed, you're on your own."),
termkey::Result::Error { err } => {
panic!("termkey::geykey failed with error code {}", err)
}
termkey::Result::Key(key) => translate_key(key),
termkey::Result::Again =>
// There's input available, but not enough to make a key event. Likely
// escape has been pushed, which we want to force out and interpret as
// a key on its own, otherwise oops.
{
match tk.getkey_force() {
termkey::Result::Key(key) => translate_key(key),
termkey::Result::Eof => panic!("stdin closed, you're on your own."),
termkey::Result::Error { err } => {
panic!("termkey::geykey_force failed with error code {}", err)
}
_ => unreachable!(),
}
}
} {
key_tx.unbounded_send(key).expect("Key channel died.");
}
}
};
tokio::runtime::Builder::new_current_thread()
.build()
.expect("Runtime started.")
.block_on(async {
while let Some(event) = event_stream.next().await {
match event {
Event::Continue => input_loop(),
Event::Break => break,
}
}
});
died_tx.send(()).expect("Main thread died prematurely.");
}
fn translate_key(key: termkey::Event) -> Option<Key> {
match key {
termkey::Event::Function { num, mods } => Some(Key::Fn {
num,
mods: translate_mods(mods),
}),
termkey::Event::KeySym { sym, mods } => Some(Key::Sym {
sym: translate_sym(sym),
mods: translate_mods(mods),
}),
termkey::Event::Unicode {
codepoint, mods, ..
} => Some(Key::Unicode {
codepoint,
mods: translate_mods(mods),
}),
_ => None,
}
}
fn translate_sym(sym: termkey::c::Sym) -> KeySym {
match sym {
termkey::c::Sym::UNKNOWN => KeySym::Unknown,
termkey::c::Sym::NONE => KeySym::None,
termkey::c::Sym::BACKSPACE => KeySym::Backspace,
termkey::c::Sym::TAB => KeySym::Tab,
termkey::c::Sym::ENTER => KeySym::Enter,
termkey::c::Sym::ESCAPE => KeySym::Escape,
termkey::c::Sym::SPACE => KeySym::Space,
termkey::c::Sym::DEL => KeySym::Del,
termkey::c::Sym::UP => KeySym::Up,
termkey::c::Sym::DOWN => KeySym::Down,
termkey::c::Sym::LEFT => KeySym::Left,
termkey::c::Sym::RIGHT => KeySym::Right,
termkey::c::Sym::BEGIN => KeySym::Begin,
termkey::c::Sym::FIND => KeySym::Find,
termkey::c::Sym::INSERT => KeySym::Insert,
termkey::c::Sym::DELETE => KeySym::Delete,
termkey::c::Sym::SELECT => KeySym::Select,
termkey::c::Sym::PAGEUP => KeySym::Pageup,
termkey::c::Sym::PAGEDOWN => KeySym::Pagedown,
termkey::c::Sym::HOME => KeySym::Home,
termkey::c::Sym::END => KeySym::End,
termkey::c::Sym::CANCEL => KeySym::Cancel,
termkey::c::Sym::CLEAR => KeySym::Clear,
termkey::c::Sym::CLOSE => KeySym::Close,
termkey::c::Sym::COMMAND => KeySym::Command,
termkey::c::Sym::COPY => KeySym::Copy,
termkey::c::Sym::EXIT => KeySym::Exit,
termkey::c::Sym::HELP => KeySym::Help,
termkey::c::Sym::MARK => KeySym::Mark,
termkey::c::Sym::MESSAGE => KeySym::Message,
termkey::c::Sym::MOVE => KeySym::Move,
termkey::c::Sym::OPEN => KeySym::Open,
termkey::c::Sym::OPTIONS => KeySym::Options,
termkey::c::Sym::PRINT => KeySym::Print,
termkey::c::Sym::REDO => KeySym::Redo,
termkey::c::Sym::REFERENCE => KeySym::Reference,
termkey::c::Sym::REFRESH => KeySym::Refresh,
termkey::c::Sym::REPLACE => KeySym::Replace,
termkey::c::Sym::RESTART => KeySym::Restart,
termkey::c::Sym::RESUME => KeySym::Resume,
termkey::c::Sym::SAVE => KeySym::Save,
termkey::c::Sym::SUSPEND => KeySym::Suspend,
termkey::c::Sym::UNDO => KeySym::Undo,
termkey::c::Sym::KP0 => KeySym::KP0,
termkey::c::Sym::KP1 => KeySym::KP1,
termkey::c::Sym::KP2 => KeySym::KP2,
termkey::c::Sym::KP3 => KeySym::KP3,
termkey::c::Sym::KP4 => KeySym::KP4,
termkey::c::Sym::KP5 => KeySym::KP5,
termkey::c::Sym::KP6 => KeySym::KP6,
termkey::c::Sym::KP7 => KeySym::KP7,
termkey::c::Sym::KP8 => KeySym::KP8,
termkey::c::Sym::KP9 => KeySym::KP9,
termkey::c::Sym::KPENTER => KeySym::KPEnter,
termkey::c::Sym::KPPLUS => KeySym::KPPlus,
termkey::c::Sym::KPMINUS => KeySym::KPMinus,
termkey::c::Sym::KPMULT => KeySym::KPMult,
termkey::c::Sym::KPDIV => KeySym::KPDiv,
termkey::c::Sym::KPCOMMA => KeySym::KPComma,
termkey::c::Sym::KPPERIOD => KeySym::KPPeriod,
termkey::c::Sym::KPEQUALS => KeySym::KPEquals,
termkey::c::Sym::N_SYMS => KeySym::NSyms,
}
}
fn translate_mods(mods: termkey::c::KeyMod) -> KeyMod {
let mut ret = KeyMod::MOD_NONE;
if mods.contains(termkey::c::KeyMod::SHIFT) {
ret.insert(KeyMod::MOD_SHIFT);
}
if mods.contains(termkey::c::KeyMod::ALT) {
ret.insert(KeyMod::MOD_ALT);
}
if mods.contains(termkey::c::KeyMod::CTRL) {
ret.insert(KeyMod::MOD_CTRL);
}
ret
}
#[cfg(test)]
mod test {
use std::mem;
use std::thread;
use std::time::Duration;
use futures::channel::{mpsc, oneshot};
use futures::StreamExt;
use crate::keymap::{Key, KeySym};
use super::*;
// Simulates stdin by writing bytes to a pipe, then listens for the key
// outputs and matches with expectations.
#[test]
fn test_input() {
// pairs of input bytes on "stdin" and corresponding expected key output
let input_output_pairs = vec![
(
vec![0x61],
Key::Unicode {
codepoint: 'a',
mods: KeyMod::MOD_NONE,
},
),
(
vec![0x1B, 0x61],
Key::Unicode {
codepoint: 'a',
mods: KeyMod::MOD_ALT,
},
),
(
vec![0x1B],
Key::Sym {
sym: KeySym::Escape,
mods: KeyMod::MOD_NONE,
},
),
(
vec![0x61],
Key::Unicode {
codepoint: 'a',
mods: KeyMod::MOD_NONE,
},
),
(
vec![0x03],
Key::Unicode {
codepoint: 'c',
mods: KeyMod::MOD_CTRL,
},
),
(
vec![0x1B, 0x5B, 0x41],
Key::Sym {
sym: KeySym::Up,
mods: KeyMod::MOD_NONE,
},
),
(
vec![0x1B, 0x4F, 0x53],
Key::Fn {
num: 4,
mods: KeyMod::MOD_NONE,
},
),
(
vec![0xE3, 0x81, 0x82],
Key::Unicode {
codepoint: 'あ',
mods: KeyMod::MOD_NONE,
},
),
];
let inputs: Vec<Vec<u8>> = input_output_pairs
.iter()
.map(|&(ref input, _)| input.clone())
.collect();
let outputs: Vec<Key> = input_output_pairs
.iter()
.map(|&(_, output)| output)
.collect();
// set up communication channels
let (key_tx, key_rx) = mpsc::unbounded();
let (reader_fd, writer_fd) = unsafe {
let mut fds = [0; 2];
if libc::pipe(fds.as_mut_ptr()) != 0 {
panic!("Failed to create pipe");
}
(fds[0], fds[1])
};
// make sure we quit in an orderly fashion even at failure
let (close_writer_tx, close_writer_rx) = oneshot::channel();
struct PipeKiller {
close_writer_tx: Option<oneshot::Sender<()>>,
reader_fd: libc::c_int,
}
impl Drop for PipeKiller {
fn drop(&mut self) {
unsafe {
libc::close(self.reader_fd);
}
self.close_writer_tx.take().unwrap().send(()).unwrap();
}
}
let _pipe_killer = PipeKiller {
close_writer_tx: Some(close_writer_tx),
reader_fd,
};
// start input listener
let _term_input = start_on_fd(reader_fd, key_tx);
// simulate keyboard input
thread::spawn(move || {
for input in inputs.iter() {
unsafe {
let buffer = mem::transmute(&input[0]);
let count = input.len() as libc::size_t;
libc::write(writer_fd, buffer, count);
}
// give termkey a chance to parse escape as a standalone key
thread::sleep(Duration::from_millis(1));
}
// keep the pipe alive until we're finished with it
tokio::runtime::Builder::new_current_thread()
.build()
.expect("Runtime started.")
.block_on(close_writer_rx)
.unwrap();
unsafe {
libc::close(writer_fd);
}
});
// match up received keys with the expected output
let expected_output = futures::stream::iter(outputs.iter());
let check = key_rx.zip(expected_output).for_each(|(key, output)| {
assert_eq!(key, *output);
futures::future::ready(())
});
tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.expect("Runtime started.")
.block_on(async { tokio::time::timeout(Duration::from_millis(10), check).await })
.expect("Timeout waiting for key event.");
}
}