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
//! # Event

//!

//! The `event` module provides the functionality to read keyboard, mouse and terminal resize events.

//!

//! * The [`read`](fn.read.html) function returns an [`Event`](enum.Event.html) immediately

//! (if available) or blocks until an [`Event`](enum.Event.html) is available.

//!

//! * The [`poll`](fn.poll.html) function allows you to check if there is or isn't an [`Event`](enum.Event.html) available

//! within the given period of time. In other words - if subsequent call to the [`read`](fn.read.html)

//! function will block or not.

//!

//! It's **not allowed** to call these functions from different threads or combine them with the

//! [`EventStream`](struct.EventStream.html). You're allowed to either:

//!

//! * use the [`read`](fn.read.html) & [`poll`](fn.poll.html) functions on any, but same, thread

//! * or the [`EventStream`](struct.EventStream.html).

//!

//! ## Mouse Events

//!

//! Mouse events are not enabled by default. You have to enable them with the

//! [`EnableMouseCapture`](struct.EnableMouseCapture.html) command. See [Command API](../index.html#command-api)

//! for more information.

//!

//! ## Examples

//!

//! Blocking read:

//!

//! ```no_run

//! use crossterm::event::{read, Event};

//!

//! fn print_events() -> crossterm::Result<()> {

//!     loop {

//!         // `read()` blocks until an `Event` is available

//!         match read()? {

//!             Event::Key(event) => println!("{:?}", event),

//!             Event::Mouse(event) => println!("{:?}", event),

//!             Event::Resize(width, height) => println!("New size {}x{}", width, height),

//!         }

//!     }

//!     Ok(())

//! }

//! ```

//!

//! Non-blocking read:

//!

//! ```no_run

//! use std::time::Duration;

//!

//! use crossterm::event::{poll, read, Event};

//!

//! fn print_events() -> crossterm::Result<()> {

//!     loop {

//!         // `poll()` waits for an `Event` for a given time period

//!         if poll(Duration::from_millis(500))? {

//!             // It's guaranteed that the `read()` won't block when the `poll()`

//!             // function returns `true`

//!             match read()? {

//!                 Event::Key(event) => println!("{:?}", event),

//!                 Event::Mouse(event) => println!("{:?}", event),

//!                 Event::Resize(width, height) => println!("New size {}x{}", width, height),

//!             }

//!         } else {

//!             // Timeout expired and no `Event` is available

//!         }

//!     }

//!     Ok(())

//! }

//! ```

//!

//! Check the [examples](https://github.com/crossterm-rs/crossterm/tree/master/examples) folder for more of

//! them (`event-*`).


use std::time::Duration;

use parking_lot::RwLock;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use bitflags::bitflags;
use filter::{EventFilter, Filter};
use lazy_static::lazy_static;
#[cfg(feature = "event-stream")]
pub use stream::EventStream;
use timeout::PollTimeout;

use crate::{Command, Result};

mod ansi;
pub(crate) mod filter;
mod read;
mod source;
#[cfg(feature = "event-stream")]
mod stream;
pub(crate) mod sys;
mod timeout;

lazy_static! {
    /// Static instance of `InternalEventReader`.

    /// This needs to be static because there can be one event reader.

    static ref INTERNAL_EVENT_READER: RwLock<read::InternalEventReader> = RwLock::new(read::InternalEventReader::default());
}

/// Checks if there is an [`Event`](enum.Event.html) available.

///

/// Returns `Ok(true)` if an [`Event`](enum.Event.html) is available otherwise it returns `Ok(false)`.

///

/// `Ok(true)` guarantees that subsequent call to the [`read`](fn.read.html) function

/// wont block.

///

/// # Arguments

///

/// * `timeout` - maximum waiting time for event availability

///

/// # Examples

///

/// Return immediately:

///

/// ```no_run

/// use std::time::Duration;

///

/// use crossterm::{event::poll, Result};

///

/// fn is_event_available() -> Result<bool> {

///     // Zero duration says that the `poll` function must return immediately

///     // with an `Event` availability information

///     poll(Duration::from_secs(0))

/// }

/// ```

///

/// Wait up to 100ms:

///

/// ```no_run

/// use std::time::Duration;

///

/// use crossterm::{event::poll, Result};

///

/// fn is_event_available() -> Result<bool> {

///     // Wait for an `Event` availability for 100ms. It returns immediately

///     // if an `Event` is/becomes available.

///     poll(Duration::from_millis(100))

/// }

/// ```

pub fn poll(timeout: Duration) -> Result<bool> {
    poll_internal(Some(timeout), &EventFilter)
}

/// Reads a single [`Event`](enum.Event.html).

///

/// This function blocks until an [`Event`](enum.Event.html) is available. Combine it with the

/// [`poll`](fn.poll.html) function to get non-blocking reads.

///

/// # Examples

///

/// Blocking read:

///

/// ```no_run

/// use crossterm::{event::read, Result};

///

/// fn print_events() -> Result<bool> {

///     loop {

///         // Blocks until an `Event` is available

///         println!("{:?}", read()?);

///     }

/// }

/// ```

///

/// Non-blocking read:

///

/// ```no_run

/// use std::time::Duration;

///

/// use crossterm::{event::{read, poll}, Result};

///

/// fn print_events() -> Result<bool> {

///     loop {

///         if poll(Duration::from_millis(100))? {

///             // It's guaranteed that `read` wont block, because `poll` returned

///             // `Ok(true)`.

///             println!("{:?}", read()?);

///         } else {

///             // Timeout expired, no `Event` is available

///         }

///     }

/// }

/// ```

pub fn read() -> Result<Event> {
    match read_internal(&EventFilter)? {
        InternalEvent::Event(event) => Ok(event),
        #[cfg(unix)]
        _ => unreachable!(),
    }
}

/// Polls to check if there are any `InternalEvent`s that can be read within the given duration.

pub(crate) fn poll_internal<F>(timeout: Option<Duration>, filter: &F) -> Result<bool>
where
    F: Filter,
{
    let (mut reader, timeout) = if let Some(timeout) = timeout {
        let poll_timeout = PollTimeout::new(Some(timeout));
        if let Some(reader) = INTERNAL_EVENT_READER.try_write_for(timeout) {
            (reader, poll_timeout.leftover())
        } else {
            return Ok(false);
        }
    } else {
        (INTERNAL_EVENT_READER.write(), None)
    };
    reader.poll(timeout, filter)
}

/// Reads a single `InternalEvent`.

pub(crate) fn read_internal<F>(filter: &F) -> Result<InternalEvent>
where
    F: Filter,
{
    let mut reader = INTERNAL_EVENT_READER.write();
    reader.read(filter)
}

/// A command that enables mouse event capturing.

///

/// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html).

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableMouseCapture;

impl Command for EnableMouseCapture {
    type AnsiType = &'static str;

    fn ansi_code(&self) -> Self::AnsiType {
        ansi::ENABLE_MOUSE_MODE_CSI_SEQUENCE
    }

    #[cfg(windows)]
    fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
        sys::windows::enable_mouse_capture()
    }

    #[cfg(windows)]
    fn is_ansi_code_supported(&self) -> bool {
        false
    }
}

/// A command that disables mouse event capturing.

///

/// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html).

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableMouseCapture;

impl Command for DisableMouseCapture {
    type AnsiType = &'static str;

    fn ansi_code(&self) -> Self::AnsiType {
        ansi::DISABLE_MOUSE_MODE_CSI_SEQUENCE
    }

    #[cfg(windows)]
    fn execute_winapi(&self, _writer: impl FnMut() -> Result<()>) -> Result<()> {
        sys::windows::disable_mouse_capture()
    }

    #[cfg(windows)]
    fn is_ansi_code_supported(&self) -> bool {
        false
    }
}

/// Represents an event.

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Event {
    /// A single key event with additional pressed modifiers.

    Key(KeyEvent),
    /// A single mouse event with additional pressed modifiers.

    Mouse(MouseEvent),
    /// An resize event with new dimensions after resize (columns, rows).

    Resize(u16, u16),
}

/// Represents a mouse event.

///

/// # Platform-specific Notes

///

/// ## Mouse Buttons

///

/// Some platforms/terminals do not report mouse button for the

/// `MouseEvent::Up` and `MouseEvent::Drag` events. `MouseButton::Left`

/// is returned if we don't know which button was used.

///

/// ## Key Modifiers

///

/// Some platforms/terminals does not report all key modifiers

/// combinations for all mouse event types. For example - macOS reports

/// `Ctrl` + left mouse button click as a right mouse button click.

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum MouseEvent {
    /// Pressed mouse button.

    ///

    /// Contains mouse button, pressed pointer location (column, row), and additional key modifiers.

    Down(MouseButton, u16, u16, KeyModifiers),
    /// Released mouse button.

    ///

    /// Contains mouse button, released pointer location (column, row), and additional key modifiers.

    Up(MouseButton, u16, u16, KeyModifiers),
    /// Moved mouse pointer while pressing a mouse button.

    ///

    /// Contains the pressed mouse button, released pointer location (column, row), and additional key modifiers.

    Drag(MouseButton, u16, u16, KeyModifiers),
    /// Scrolled mouse wheel downwards (towards the user).

    ///

    /// Contains the scroll location (column, row), and additional key modifiers.

    ScrollDown(u16, u16, KeyModifiers),
    /// Scrolled mouse wheel upwards (away from the user).

    ///

    /// Contains the scroll location (column, row), and additional key modifiers.

    ScrollUp(u16, u16, KeyModifiers),
}

/// Represents a mouse button.

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum MouseButton {
    /// Left mouse button.

    Left,
    /// Right mouse button.

    Right,
    /// Middle mouse button.

    Middle,
}

bitflags! {
    /// Represents key modifiers (shift, control, alt).

    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    pub struct KeyModifiers: u8 {
        const SHIFT = 0b0000_0001;
        const CONTROL = 0b0000_0010;
        const ALT = 0b0000_0100;
        const NONE = 0b0000_0000;
    }
}

/// Represents a key event.

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub struct KeyEvent {
    /// The key itself.

    pub code: KeyCode,
    /// Additional key modifiers.

    pub modifiers: KeyModifiers,
}

impl KeyEvent {
    pub fn new(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
        KeyEvent { code, modifiers }
    }
}

impl From<KeyCode> for KeyEvent {
    fn from(code: KeyCode) -> Self {
        KeyEvent {
            code,
            modifiers: KeyModifiers::empty(),
        }
    }
}

/// Represents a key.

#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyCode {
    /// Backspace key.

    Backspace,
    /// Enter key.

    Enter,
    /// Left arrow key.

    Left,
    /// Right arrow key.

    Right,
    /// Up arrow key.

    Up,
    /// Down arrow key.

    Down,
    /// Home key.

    Home,
    /// End key.

    End,
    /// Page up key.

    PageUp,
    /// Page dow key.

    PageDown,
    /// Tab key.

    Tab,
    /// Shift + Tab key.

    BackTab,
    /// Delete key.

    Delete,
    /// Insert key.

    Insert,
    /// F key.

    ///

    /// `KeyEvent::F(1)` represents F1 key, etc.

    F(u8),
    /// A character.

    ///

    /// `KeyEvent::Char('c')` represents `c` character, etc.

    Char(char),
    /// Null.

    Null,
    /// Escape key.

    Esc,
}

/// An internal event.

///

/// Encapsulates publicly available `Event` with additional internal

/// events that shouldn't be publicly available to the crate users.

#[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Eq)]
pub(crate) enum InternalEvent {
    /// An event.

    Event(Event),
    /// A cursor position (`col`, `row`).

    #[cfg(unix)]
    CursorPosition(u16, u16),
}