Debian Codegraph @

debian/rust-vendor/crossterm/src/event/sys/windows.rs

raw
1 //! This is a WINDOWS specific implementation for input related action.
2
3 use std::convert::TryFrom;
4 use std::io;
5 use std::sync::atomic::{AtomicU64, Ordering};
6
7 use crossterm_winapi::{ConsoleMode, Handle};
8
9 pub(crate) mod parse;
10 pub(crate) mod poll;
11 #[cfg(feature = "event-stream")]
12 pub(crate) mod waker;
13
14 const ENABLE_MOUSE_MODE: u32 = 0x0010 | 0x0080 | 0x0008;
15
16 /// This is a either `u64::MAX` if it's uninitialized or a valid `u32` that stores the original
17 /// console mode if it's initialized.
18 static ORIGINAL_CONSOLE_MODE: AtomicU64 = AtomicU64::new(u64::MAX);
19
20 /// Initializes the default console color. It will will be skipped if it has already been initialized.
21 fn init_original_console_mode(original_mode: u32) {
22 let _ = ORIGINAL_CONSOLE_MODE.compare_exchange(
23 u64::MAX,
24 u64::from(original_mode),
25 Ordering::Relaxed,
26 Ordering::Relaxed,
27 );
28 }
29
30 /// Returns the original console color, make sure to call `init_console_color` before calling this function. Otherwise this function will panic.
31 fn original_console_mode() -> std::io::Result<u32> {
32 u32::try_from(ORIGINAL_CONSOLE_MODE.load(Ordering::Relaxed))
33 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Initial console modes not set"))
34 }
35
36 pub(crate) fn enable_mouse_capture() -> std::io::Result<()> {
37 let mode = ConsoleMode::from(Handle::current_in_handle()?);
38 init_original_console_mode(mode.mode()?);
39 mode.set_mode(ENABLE_MOUSE_MODE)?;
40
41 Ok(())
42 }
43
44 pub(crate) fn disable_mouse_capture() -> std::io::Result<()> {
45 let mode = ConsoleMode::from(Handle::current_in_handle()?);
46 mode.set_mode(original_console_mode()?)?;
47 Ok(())
48 }