Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to windows-sys #718

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,28 @@ all-features = true
#
[features]
default = ["bracketed-paste", "windows", "events"]
windows = ["dep:winapi", "dep:crossterm_winapi"] # Disables winapi dependencies from being included into the binary (SHOULD NOT be disabled on windows).
bracketed-paste = [] # Enables triggering a `Event::Paste` when pasting text into the terminal.
windows = [
"dep:windows-sys",
"dep:crossterm_winapi",
] # Disables winapi dependencies from being included into the binary (SHOULD NOT be disabled on windows).
bracketed-paste = [
] # Enables triggering a `Event::Paste` when pasting text into the terminal.
event-stream = ["dep:futures-core", "events"] # Enables async events
use-dev-tty = ["filedescriptor"] # Enables raw file descriptor polling / selecting instead of mio.
events = ["dep:mio", "dep:signal-hook", "dep:signal-hook-mio"] # Enables reading input/events from the system.
use-dev-tty = [
"filedescriptor",
] # Enables raw file descriptor polling / selecting instead of mio.
events = [
"dep:mio",
"dep:signal-hook",
"dep:signal-hook-mio",
] # Enables reading input/events from the system.
serde = ["dep:serde", "bitflags/serde"] # Enables 'serde' for various types.

#
# Shared dependencies
#
[dependencies]
bitflags = {version = "2.3" }
bitflags = { version = "2.3" }
parking_lot = "0.12"

# optional deps only added when requested
Expand All @@ -49,10 +59,18 @@ serde = { version = "1.0", features = ["derive"], optional = true }
#
# Windows dependencies
#
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.9"
features = ["winuser", "winerror"]
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52.0"
optional = true
features = [
"Win32_Foundation",
"Win32_System_Console",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_UI_WindowsAndMessaging",
"Win32_UI_TextServices",
"Win32_UI_Input_KeyboardAndMouse",
]

[target.'cfg(windows)'.dependencies]
crossterm_winapi = { version = "0.9.1", optional = true }
Expand All @@ -65,7 +83,9 @@ libc = "0.2"
signal-hook = { version = "0.3.17", optional = true }
filedescriptor = { version = "0.8", optional = true }
mio = { version = "0.8", features = ["os-poll"], optional = true }
signal-hook-mio = { version = "0.2.3", features = ["support-v0_8"], optional = true }
signal-hook-mio = { version = "0.2.3", features = [
"support-v0_8",
], optional = true }

#
# Dev dependencies (examples, ...)
Expand Down
2 changes: 1 addition & 1 deletion src/ansi_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering};

use crossterm_winapi::{ConsoleMode, Handle};
use parking_lot::Once;
use winapi::um::wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING;
use windows_sys::Win32::System::Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING;

/// Enable virtual terminal processing.
///
Expand Down
13 changes: 8 additions & 5 deletions src/cursor/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ use std::io;
use std::sync::atomic::{AtomicU64, Ordering};

use crossterm_winapi::{result, Coord, Handle, HandleType, ScreenBuffer};
use winapi::{
shared::minwindef::{FALSE, TRUE},
um::wincon::{SetConsoleCursorInfo, SetConsoleCursorPosition, CONSOLE_CURSOR_INFO, COORD},
use windows_sys::Win32::Foundation::BOOL;
use windows_sys::Win32::System::Console::{
SetConsoleCursorInfo, SetConsoleCursorPosition, CONSOLE_CURSOR_INFO, COORD,
};

const FALSE: BOOL = 0;
const TRUE: BOOL = 1;

/// The position of the cursor, written when you save the cursor's position.
///
/// This is `u64::MAX` initially. Otherwise, it stores the cursor's x position bit-shifted left 16
Expand Down Expand Up @@ -147,7 +150,7 @@ impl ScreenBufferCursor {

unsafe {
if result(SetConsoleCursorPosition(
**self.screen_buffer.handle(),
**self.screen_buffer.handle() as isize,
position,
))
.is_err()
Expand All @@ -166,7 +169,7 @@ impl ScreenBufferCursor {

unsafe {
if result(SetConsoleCursorInfo(
**self.screen_buffer.handle(),
**self.screen_buffer.handle() as isize,
&cursor_info,
))
.is_err()
Expand Down
18 changes: 10 additions & 8 deletions src/event/sys/windows/parse.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crossterm_winapi::{ControlKeyState, EventFlags, KeyEventRecord, ScreenBuffer};
use winapi::um::{
wincon::{
use windows_sys::Win32::{
System::Console::{
CAPSLOCK_ON, LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED,
SHIFT_PRESSED,
},
winuser::{
GetForegroundWindow, GetKeyboardLayout, GetWindowThreadProcessId, ToUnicodeEx, VK_BACK,
VK_CONTROL, VK_DELETE, VK_DOWN, VK_END, VK_ESCAPE, VK_F1, VK_F24, VK_HOME, VK_INSERT,
VK_LEFT, VK_MENU, VK_NEXT, VK_NUMPAD0, VK_NUMPAD9, VK_PRIOR, VK_RETURN, VK_RIGHT, VK_SHIFT,
VK_TAB, VK_UP,
UI::{
Input::KeyboardAndMouse::{
GetKeyboardLayout, ToUnicodeEx, VK_BACK, VK_CONTROL, VK_DELETE, VK_DOWN, VK_END,
VK_ESCAPE, VK_F1, VK_F24, VK_HOME, VK_INSERT, VK_LEFT, VK_MENU, VK_NEXT, VK_NUMPAD0,
VK_NUMPAD9, VK_PRIOR, VK_RETURN, VK_RIGHT, VK_SHIFT, VK_TAB, VK_UP,
},
WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId},
},
};

Expand Down Expand Up @@ -203,7 +205,7 @@ fn get_char_for_key(key_event: &KeyEventRecord) -> Option<char> {

fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent> {
let modifiers = KeyModifiers::from(&key_event.control_key_state);
let virtual_key_code = key_event.virtual_key_code as i32;
let virtual_key_code = key_event.virtual_key_code;

// We normally ignore all key release events, but we will make an exception for an Alt key
// release if it carries a u_char value, as this indicates an Alt code.
Expand Down
14 changes: 6 additions & 8 deletions src/event/sys/windows/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ use std::io;
use std::time::Duration;

use crossterm_winapi::Handle;
use winapi::{
shared::winerror::WAIT_TIMEOUT,
um::{
synchapi::WaitForMultipleObjects,
winbase::{INFINITE, WAIT_ABANDONED_0, WAIT_FAILED, WAIT_OBJECT_0},
},
use windows_sys::Win32::{
Foundation::{WAIT_ABANDONED_0, WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT},
System::Threading::{WaitForMultipleObjects, INFINITE},
};

#[cfg(feature = "event-stream")]
Expand Down Expand Up @@ -50,8 +47,9 @@ impl WinApiPoll {
#[cfg(not(feature = "event-stream"))]
let handles = &[*console_handle];

let output =
unsafe { WaitForMultipleObjects(handles.len() as u32, handles.as_ptr(), 0, dw_millis) };
let output = unsafe {
WaitForMultipleObjects(handles.len() as u32, handles.as_ptr() as _, 0, dw_millis)
};

match output {
output if output == WAIT_OBJECT_0 => {
Expand Down
2 changes: 1 addition & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl Command for SetUnderlineColor {
fn execute_winapi(&self) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"SetUnderlineColor not supported by winapi.",
"SetUnderlineColor not supported by windows-sys.",
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/style/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryFrom;
use std::sync::atomic::{AtomicU32, Ordering};

use crossterm_winapi::{Console, Handle, HandleType, ScreenBuffer};
use winapi::um::wincon;
use windows_sys::Win32::System::Console as wincon;

use super::super::{Color, Colored};

Expand Down
2 changes: 1 addition & 1 deletion src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use crossterm_winapi::{ConsoleMode, Handle, ScreenBuffer};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(windows)]
use winapi::um::wincon::ENABLE_WRAP_AT_EOL_OUTPUT;
use windows_sys::Win32::System::Console::ENABLE_WRAP_AT_EOL_OUTPUT;

#[doc(no_inline)]
use crate::Command;
Expand Down
10 changes: 5 additions & 5 deletions src/terminal/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::fmt::{self, Write};
use std::io::{self};

use crossterm_winapi::{Console, ConsoleMode, Coord, Handle, ScreenBuffer, Size};
use winapi::{
shared::minwindef::DWORD,
um::wincon::{SetConsoleTitleW, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT},
use windows_sys::Win32::System::Console::{
SetConsoleTitleW, CONSOLE_MODE, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
};

use crate::{
Expand All @@ -15,7 +14,8 @@ use crate::{
};

/// bits which can't be set in raw mode
const NOT_RAW_MODE_MASK: DWORD = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT;
const NOT_RAW_MODE_MASK: CONSOLE_MODE =
ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT;

pub(crate) fn is_raw_mode_enabled() -> std::io::Result<bool> {
let console_mode = ConsoleMode::from(Handle::current_in_handle()?);
Expand Down Expand Up @@ -366,7 +366,7 @@ mod tests {

use crossterm_winapi::ScreenBuffer;
use serial_test::serial;
use winapi::um::wincon::GetConsoleTitleW;
use windows_sys::Win32::System::Console::GetConsoleTitleW;

use super::{scroll_down, scroll_up, set_size, set_window_title, size, temp_screen_buffer};

Expand Down
8 changes: 4 additions & 4 deletions src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::os::unix::io::AsRawFd;
use std::os::windows::io::AsRawHandle;

#[cfg(windows)]
use winapi::um::consoleapi::GetConsoleMode;
use windows_sys::Win32::System::Console::{GetConsoleMode, CONSOLE_MODE};

/// Adds the `is_tty` method to types that might represent a terminal
///
Expand Down Expand Up @@ -39,8 +39,8 @@ impl<S: AsRawFd> IsTty for S {
#[cfg(windows)]
impl<S: AsRawHandle> IsTty for S {
fn is_tty(&self) -> bool {
let mut mode = 0;
let ok = unsafe { GetConsoleMode(self.as_raw_handle() as *mut _, &mut mode) };
ok == 1
let mut mode = CONSOLE_MODE::default();
let ok = unsafe { GetConsoleMode(self.as_raw_handle() as isize, &mut mode) };
ok != 0
}
}