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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! This is a WINDOWS specific implementation for input related action.

use std::{
    char, io,
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc::{self, Receiver, Sender},
        Arc, Mutex,
    },
    thread,
    time::Duration,
};

use crossterm_utils::Result;
use winapi::um::{
    wincon::{
        LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED, SHIFT_PRESSED,
    },
    winnt::INT,
    winuser::{
        VK_BACK, VK_CONTROL, VK_DELETE, VK_DOWN, VK_END, VK_ESCAPE, VK_F1, VK_F10, VK_F11, VK_F12,
        VK_F2, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_HOME, VK_INSERT, VK_LEFT,
        VK_MENU, VK_NEXT, VK_PRIOR, VK_RETURN, VK_RIGHT, VK_SHIFT, VK_UP,
    },
};

use crossterm_winapi::{
    ButtonState, Console, ConsoleMode, EventFlags, Handle, InputEventType, KeyEventRecord,
    MouseEvent, ScreenBuffer,
};
use lazy_static::lazy_static;

use crate::{input::Input, InputEvent, KeyEvent, MouseButton};

const ENABLE_MOUSE_MODE: u32 = 0x0010 | 0x0080 | 0x0008;

lazy_static! {
    static ref ORIGINAL_CONSOLE_MODE: Mutex<Option<u32>> = Mutex::new(None);
}

/// Initializes the default console color. It will will be skipped if it has already been initialized.
fn init_original_console_mode(original_mode: u32) {
    let mut lock = ORIGINAL_CONSOLE_MODE.lock().unwrap();

    if lock.is_none() {
        *lock = Some(original_mode);
    }
}

/// Returns the original console color, make sure to call `init_console_color` before calling this function. Otherwise this function will panic.
fn original_console_mode() -> u32 {
    // safe unwrap, initial console color was set with `init_console_color` in `WinApiColor::new()`
    ORIGINAL_CONSOLE_MODE
        .lock()
        .unwrap()
        .expect("Original console mode not set")
}

pub(crate) struct WindowsInput;

impl WindowsInput {
    pub fn new() -> WindowsInput {
        WindowsInput
    }
}

impl Input for WindowsInput {
    fn read_char(&self) -> Result<char> {
        // _getwch is without echo and _getwche is with echo
        let pressed_char = unsafe { _getwche() };

        // we could return error but maybe option to keep listening until valid character is inputted.
        if pressed_char == 0 || pressed_char == 0xe0 {
            Err(io::Error::new(
                io::ErrorKind::Other,
                "Given input char is not a valid char, mostly occurs when pressing special keys",
            ))?;
        }

        let ch = char::from_u32(pressed_char as u32).ok_or_else(|| {
            io::Error::new(io::ErrorKind::Other, "Could not parse given input to char")
        })?;

        Ok(ch)
    }

    fn read_async(&self) -> AsyncReader {
        AsyncReader::new(Box::new(move |event_tx, cancellation_token| loop {
            for i in read_input_events().unwrap().1 {
                if event_tx.send(i).is_err() {
                    return;
                }
            }

            if cancellation_token.load(Ordering::SeqCst) {
                return;
            }

            thread::sleep(Duration::from_millis(1));
        }))
    }

    fn read_until_async(&self, delimiter: u8) -> AsyncReader {
        AsyncReader::new(Box::new(move |event_tx, cancellation_token| loop {
            for event in read_input_events().unwrap().1 {
                if let InputEvent::Keyboard(KeyEvent::Char(key)) = event {
                    if (key as u8) == delimiter {
                        return;
                    }
                }

                if cancellation_token.load(Ordering::SeqCst) {
                    return;
                } else {
                    if event_tx.send(event).is_err() {
                        return;
                    }
                }

                thread::sleep(Duration::from_millis(1));
            }
        }))
    }

    fn read_sync(&self) -> SyncReader {
        SyncReader
    }

    fn enable_mouse_mode(&self) -> Result<()> {
        let mode = ConsoleMode::from(Handle::current_in_handle()?);

        init_original_console_mode(mode.mode()?);
        mode.set_mode(ENABLE_MOUSE_MODE)?;

        Ok(())
    }

    fn disable_mouse_mode(&self) -> Result<()> {
        let mode = ConsoleMode::from(Handle::current_in_handle()?);
        mode.set_mode(original_console_mode())?;
        Ok(())
    }
}

/// A synchronous input reader (blocking).
///
/// `SyncReader` implements the [`Iterator`](https://doc.rust-lang.org/std/iter/index.html#iterator)
/// trait. Documentation says:
///
/// > An iterator has a method, `next`, which when called, returns an `Option<Item>`. `next` will return
/// > `Some(Item)` as long as there are elements, and once they've all been exhausted, will return `None`
/// > to indicate that iteration is finished. Individual iterators may choose to resume iteration, and
/// > so calling `next` again may or may not eventually start returning `Some(Item)` again at some point.
///
/// `SyncReader` is an individual iterator and it doesn't use `None` to indicate that the iteration is
/// finished. You can expect additional `Some(InputEvent)` after calling `next` even if you have already
/// received `None`. Unfortunately, `None` means that an error occurred, but you're free to call `next`
/// again. This behavior will be changed in the future to avoid errors consumption.  
///
/// # Notes
///
/// * It requires enabled raw mode (see the
///   [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation to learn more).
/// * See the [`AsyncReader`](struct.AsyncReader.html) if you want a non blocking reader.
///
/// # Examples
///
/// ```no_run
/// use std::{thread, time::Duration};
///
/// use crossterm_input::{input, InputEvent, KeyEvent, RawScreen};
///
/// fn main() {
///     println!("Press 'ESC' to quit.");
///
///     // Enable raw mode and keep the `_raw` around otherwise the raw mode will be disabled
///     let _raw = RawScreen::into_raw_mode();
///
///     // Create an input from our screen
///     let input = input();
///
///     // Create a sync reader
///     let mut reader = input.read_sync();
///
///     loop {
///         if let Some(event) = reader.next() { // Blocking call
///             match event {
///                 InputEvent::Keyboard(KeyEvent::Esc) => {
///                     println!("Program closing ...");
///                     break;
///                  }
///                  InputEvent::Mouse(event) => { /* Mouse event */ }
///                  _ => { /* Other events */ }
///             }
///         }
///         thread::sleep(Duration::from_millis(50));
///     }
/// } // `_raw` dropped <- raw mode disabled
/// ```
pub struct SyncReader;

impl Iterator for SyncReader {
    type Item = InputEvent;

    /// Tries to read the next input event (blocking).
    ///
    /// `None` doesn't mean that the iteration is finished. See the
    /// [`SyncReader`](struct.SyncReader.html) documentation for more information.
    fn next(&mut self) -> Option<Self::Item> {
        // This synces the behaviour with the unix::SyncReader (& documentation) where
        // None is returned in case of error.
        read_single_event().unwrap_or(None)
    }
}

/// An asynchronous input reader (not blocking).
///
/// `AsyncReader` implements the [`Iterator`](https://doc.rust-lang.org/std/iter/index.html#iterator)
/// trait. Documentation says:
///
/// > An iterator has a method, `next`, which when called, returns an `Option<Item>`. `next` will return
/// > `Some(Item)` as long as there are elements, and once they've all been exhausted, will return `None`
/// > to indicate that iteration is finished. Individual iterators may choose to resume iteration, and
/// > so calling `next` again may or may not eventually start returning `Some(Item)` again at some point.
///
/// `AsyncReader` is an individual iterator and it doesn't use `None` to indicate that the iteration is
/// finished. You can expect additional `Some(InputEvent)` after calling `next` even if you have already
/// received `None`.
///
/// # Notes
///
/// * It requires enabled raw mode (see the
///   [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation to learn more).
/// * A thread is spawned to read the input.
/// * The reading thread is cleaned up when you drop the `AsyncReader`.
/// * See the [`SyncReader`](struct.SyncReader.html) if you want a blocking,
///   or a less resource hungry reader.
///
/// # Examples
///
/// ```no_run
/// use std::{thread, time::Duration};
///
/// use crossterm_input::{input, InputEvent, KeyEvent, RawScreen};
///
/// fn main() {
///     println!("Press 'ESC' to quit.");
///
///     // Enable raw mode and keep the `_raw` around otherwise the raw mode will be disabled
///     let _raw = RawScreen::into_raw_mode();
///
///     // Create an input from our screen
///     let input = input();
///
///     // Create an async reader
///     let mut reader = input.read_async();
///
///     loop {
///         if let Some(event) = reader.next() { // Not a blocking call
///             match event {
///                 InputEvent::Keyboard(KeyEvent::Esc) => {
///                     println!("Program closing ...");
///                     break;
///                  }
///                  InputEvent::Mouse(event) => { /* Mouse event */ }
///                  _ => { /* Other events */ }
///             }
///         }
///         thread::sleep(Duration::from_millis(50));
///     }
/// } // `reader` dropped <- thread cleaned up, `_raw` dropped <- raw mode disabled
/// ```
pub struct AsyncReader {
    event_rx: Receiver<InputEvent>,
    shutdown: Arc<AtomicBool>,
}

impl AsyncReader {
    // TODO Should the new() really be public?
    /// Creates a new `AsyncReader`.
    ///
    /// # Notes
    ///
    /// * A thread is spawned to read the input.
    /// * The reading thread is cleaned up when you drop the `AsyncReader`.
    pub fn new(function: Box<dyn Fn(&Sender<InputEvent>, &Arc<AtomicBool>) + Send>) -> AsyncReader {
        let shutdown_handle = Arc::new(AtomicBool::new(false));

        let (event_tx, event_rx) = mpsc::channel();
        let thread_shutdown = shutdown_handle.clone();

        thread::spawn(move || loop {
            function(&event_tx, &thread_shutdown);
        });

        AsyncReader {
            event_rx,
            shutdown: shutdown_handle,
        }
    }

    // TODO If we we keep the Drop semantics, do we really need this in the public API? It's useless as
    //      there's no `start`, etc.
    /// Stops the input reader.
    ///
    /// # Notes
    ///
    /// * The reading thread is cleaned up.
    /// * You don't need to call this method, because it will be automatically called when the
    ///   `AsyncReader` is dropped.
    pub fn stop(&mut self) {
        self.shutdown.store(true, Ordering::SeqCst);
    }
}

impl Drop for AsyncReader {
    fn drop(&mut self) {
        self.stop();
    }
}

impl Iterator for AsyncReader {
    type Item = InputEvent;

    /// Tries to read the next input event (not blocking).
    ///
    /// `None` doesn't mean that the iteration is finished. See the
    /// [`AsyncReader`](struct.AsyncReader.html) documentation for more information.
    fn next(&mut self) -> Option<Self::Item> {
        let mut iterator = self.event_rx.try_iter();
        iterator.next()
    }
}

extern "C" {
    fn _getwche() -> INT;
}

fn read_single_event() -> Result<Option<InputEvent>> {
    let console = Console::from(Handle::current_in_handle()?);

    let input = console.read_single_input_event()?;

    match input.event_type {
        InputEventType::KeyEvent => {
            handle_key_event(unsafe { KeyEventRecord::from(*input.event.KeyEvent()) })
        }
        InputEventType::MouseEvent => {
            handle_mouse_event(unsafe { MouseEvent::from(*input.event.MouseEvent()) })
        }
        // NOTE (@imdaveho): ignore below
        InputEventType::WindowBufferSizeEvent => return Ok(None), // TODO implement terminal resize event
        InputEventType::FocusEvent => Ok(None),
        InputEventType::MenuEvent => Ok(None),
    }
}

/// partially inspired by: https://github.com/retep998/wio-rs/blob/master/src/console.rs#L130
fn read_input_events() -> Result<(u32, Vec<InputEvent>)> {
    let console = Console::from(Handle::current_in_handle()?);

    let result = console.read_console_input()?;

    let mut input_events = Vec::with_capacity(result.0 as usize);

    for input in result.1 {
        match input.event_type {
            InputEventType::KeyEvent => {
                if let Ok(Some(event)) =
                    handle_key_event(unsafe { KeyEventRecord::from(*input.event.KeyEvent()) })
                {
                    input_events.push(event)
                }
            }
            InputEventType::MouseEvent => {
                if let Ok(Some(event)) =
                    handle_mouse_event(unsafe { MouseEvent::from(*input.event.MouseEvent()) })
                {
                    input_events.push(event)
                }
            }
            // NOTE (@imdaveho): ignore below
            InputEventType::WindowBufferSizeEvent => (), // TODO implement terminal resize event
            InputEventType::FocusEvent => (),
            InputEventType::MenuEvent => (),
        }
    }

    return Ok((result.0, input_events));
}

fn handle_mouse_event(mouse_event: MouseEvent) -> Result<Option<InputEvent>> {
    if let Ok(Some(event)) = parse_mouse_event_record(&mouse_event) {
        return Ok(Some(InputEvent::Mouse(event)));
    }
    Ok(None)
}

fn handle_key_event(key_event: KeyEventRecord) -> Result<Option<InputEvent>> {
    if key_event.key_down {
        if let Some(event) = parse_key_event_record(&key_event) {
            return Ok(Some(InputEvent::Keyboard(event)));
        }
    }

    return Ok(None);
}

fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<KeyEvent> {
    let key_code = key_event.virtual_key_code as i32;
    match key_code {
        VK_SHIFT | VK_CONTROL | VK_MENU => None,
        VK_BACK => Some(KeyEvent::Backspace),
        VK_ESCAPE => Some(KeyEvent::Esc),
        VK_RETURN => Some(KeyEvent::Enter),
        VK_F1 | VK_F2 | VK_F3 | VK_F4 | VK_F5 | VK_F6 | VK_F7 | VK_F8 | VK_F9 | VK_F10 | VK_F11
        | VK_F12 => Some(KeyEvent::F((key_event.virtual_key_code - 111) as u8)),
        VK_LEFT | VK_UP | VK_RIGHT | VK_DOWN => {
            // Modifier Keys (Ctrl, Shift) Support
            let key_state = &key_event.control_key_state;
            let ctrl_pressed = key_state.has_state(RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED);
            let shift_pressed = key_state.has_state(SHIFT_PRESSED);

            let event = match key_code {
                VK_LEFT => {
                    if ctrl_pressed {
                        Some(KeyEvent::CtrlLeft)
                    } else if shift_pressed {
                        Some(KeyEvent::ShiftLeft)
                    } else {
                        Some(KeyEvent::Left)
                    }
                }
                VK_UP => {
                    if ctrl_pressed {
                        Some(KeyEvent::CtrlUp)
                    } else if shift_pressed {
                        Some(KeyEvent::ShiftUp)
                    } else {
                        Some(KeyEvent::Up)
                    }
                }
                VK_RIGHT => {
                    if ctrl_pressed {
                        Some(KeyEvent::CtrlRight)
                    } else if shift_pressed {
                        Some(KeyEvent::ShiftRight)
                    } else {
                        Some(KeyEvent::Right)
                    }
                }
                VK_DOWN => {
                    if ctrl_pressed {
                        Some(KeyEvent::CtrlDown)
                    } else if shift_pressed {
                        Some(KeyEvent::ShiftDown)
                    } else {
                        Some(KeyEvent::Down)
                    }
                }
                _ => None,
            };

            event
        }
        VK_PRIOR | VK_NEXT => {
            if key_code == VK_PRIOR {
                Some(KeyEvent::PageUp)
            } else if key_code == VK_NEXT {
                Some(KeyEvent::PageDown)
            } else {
                None
            }
        }
        VK_END | VK_HOME => {
            if key_code == VK_HOME {
                Some(KeyEvent::Home)
            } else if key_code == VK_END {
                Some(KeyEvent::End)
            } else {
                None
            }
        }
        VK_DELETE => Some(KeyEvent::Delete),
        VK_INSERT => Some(KeyEvent::Insert),
        _ => {
            // Modifier Keys (Ctrl, Alt, Shift) Support
            let character_raw = { (unsafe { *key_event.u_char.UnicodeChar() } as u16) };

            if character_raw < 255 {
                let character = character_raw as u8 as char;

                let key_state = &key_event.control_key_state;

                if key_state.has_state(LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) {
                    // If the ALT key is held down, pressing the A key produces ALT+A, which the system does not treat as a character at all, but rather as a system command.
                    // The pressed command is stored in `virtual_key_code`.
                    let command = key_event.virtual_key_code as u8 as char;

                    if (command).is_alphabetic() {
                        Some(KeyEvent::Alt(command))
                    } else {
                        None
                    }
                } else if key_state.has_state(LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) {
                    match character_raw as u8 {
                        c @ b'\x01'..=b'\x1A' => {
                            Some(KeyEvent::Ctrl((c as u8 - 0x1 + b'a') as char))
                        }
                        c @ b'\x1C'..=b'\x1F' => {
                            Some(KeyEvent::Ctrl((c as u8 - 0x1C + b'4') as char))
                        }
                        _ => None,
                    }
                } else if key_state.has_state(SHIFT_PRESSED) && character == '\t' {
                    Some(KeyEvent::BackTab)
                } else {
                    if character == '\t' {
                        Some(KeyEvent::Tab)
                    } else {
                        // Shift + key press, essentially the same as single key press
                        // Separating to be explicit about the Shift press.
                        Some(KeyEvent::Char(character))
                    }
                }
            } else {
                None
            }
        }
    }
}

fn parse_mouse_event_record(event: &MouseEvent) -> Result<Option<crate::MouseEvent>> {
    // NOTE (@imdaveho): xterm emulation takes the digits of the coords and passes them
    // individually as bytes into a buffer; the below cxbs and cybs replicates that and
    // mimicks the behavior; additionally, in xterm, mouse move is only handled when a
    // mouse button is held down (ie. mouse drag)

    let window_size = ScreenBuffer::current()?.info()?.terminal_window();

    let xpos = event.mouse_position.x;
    let mut ypos = event.mouse_position.y;

    // The 'y' position of a mouse event is not relative to the window but absolute to screen buffer.
    // This means that when the mouse cursor is at the top left it will be x: 0, y: 2295 (e.g. y = number of cells counting from the absolute buffer height) instead of relative x: 0, y: 0 to the window.

    ypos = ypos - window_size.top;

    Ok(match event.event_flags {
        EventFlags::PressOrRelease => {
            // Single click
            match event.button_state {
                ButtonState::Release => Some(crate::MouseEvent::Release(xpos as u16, ypos as u16)),
                ButtonState::FromLeft1stButtonPressed => {
                    // left click
                    Some(crate::MouseEvent::Press(
                        MouseButton::Left,
                        xpos as u16,
                        ypos as u16,
                    ))
                }
                ButtonState::RightmostButtonPressed => {
                    // right click
                    Some(crate::MouseEvent::Press(
                        MouseButton::Right,
                        xpos as u16,
                        ypos as u16,
                    ))
                }
                ButtonState::FromLeft2ndButtonPressed => {
                    // middle click
                    Some(crate::MouseEvent::Press(
                        MouseButton::Middle,
                        xpos as u16,
                        ypos as u16,
                    ))
                }
                _ => None,
            }
        }
        EventFlags::MouseMoved => {
            // Click + Move
            // NOTE (@imdaveho) only register when mouse is not released
            if event.button_state != ButtonState::Release {
                Some(crate::MouseEvent::Hold(xpos as u16, ypos as u16))
            } else {
                None
            }
        }
        EventFlags::MouseWheeled => {
            // Vertical scroll
            // NOTE (@imdaveho) from https://docs.microsoft.com/en-us/windows/console/mouse-event-record-str
            // if `button_state` is negative then the wheel was rotated backward, toward the user.
            if event.button_state != ButtonState::Negative {
                Some(crate::MouseEvent::Press(
                    MouseButton::WheelUp,
                    xpos as u16,
                    ypos as u16,
                ))
            } else {
                Some(crate::MouseEvent::Press(
                    MouseButton::WheelDown,
                    xpos as u16,
                    ypos as u16,
                ))
            }
        }
        EventFlags::DoubleClick => None, // NOTE (@imdaveho): double click not supported by unix terminals
        EventFlags::MouseHwheeled => None, // NOTE (@imdaveho): horizontal scroll not supported by unix terminals
                                           // TODO: Handle Ctrl + Mouse, Alt + Mouse, etc.
    })
}