Skip to content

Commit 16ce47f

Browse files
author
Vasily Galkin
committed
Port from winapi to windows-sys
This simplifies windows-based targets linking, allowing use of raw-dylib based windows-link crate instead of dealing with prepared .lib files Changes are minimal required for porting for readability, use groups from same crate are note combined
1 parent 929f7be commit 16ce47f

7 files changed

Lines changed: 97 additions & 91 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ nix = { version = "0.26", default-features = false, features = ["poll", "signal"
2727
terminfo = "0.8"
2828

2929
[target.'cfg(windows)'.dependencies]
30-
winapi = { version = "0.3", features = [
31-
"consoleapi", "handleapi", "minwindef", "ntdef", "processenv", "synchapi",
32-
"winbase", "wincon", "winerror", "winnt", "winuser" ] }
30+
windows-sys = { version = ">=0.59.0, <=0.61.2", features = [
31+
"Win32_UI_Input_KeyboardAndMouse", "Win32_Security",
32+
"Win32_Storage_FileSystem", "Win32_System_Console", "Win32_System_Threading" ] }
3333

3434
[dev-dependencies]
3535
rand = "0.8"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern crate unicode_width;
3939
#[cfg(unix)] extern crate nix;
4040
#[cfg(unix)] extern crate terminfo;
4141

42-
#[cfg(windows)] extern crate winapi;
42+
#[cfg(windows)] extern crate windows_sys;
4343

4444
pub use crate::screen::{Screen, ScreenReadGuard, ScreenWriteGuard};
4545
pub use crate::sequence::{FindResult, SequenceMap};

src/screen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl crate::windows::TerminalExt for Screen {
602602
self.0.read_raw(buf, timeout)
603603
}
604604

605-
fn read_raw_event(&mut self, events: &mut [::winapi::um::wincon::INPUT_RECORD],
605+
fn read_raw_event(&mut self, events: &mut [::windows_sys::Win32::System::Console::INPUT_RECORD],
606606
timeout: Option<Duration>) -> io::Result<Option<Event>> {
607607
self.0.read_raw_event(events, timeout)
608608
}
@@ -614,7 +614,7 @@ impl<'a> crate::windows::TerminalExt for ScreenReadGuard<'a> {
614614
self.0.read_raw(buf, timeout)
615615
}
616616

617-
fn read_raw_event(&mut self, events: &mut [::winapi::um::wincon::INPUT_RECORD],
617+
fn read_raw_event(&mut self, events: &mut [::windows_sys::Win32::System::Console::INPUT_RECORD],
618618
timeout: Option<Duration>) -> io::Result<Option<Event>> {
619619
self.0.read_raw_event(events, timeout)
620620
}

src/terminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ impl crate::windows::TerminalExt for Terminal {
11321132
self.0.read_raw(buf, timeout)
11331133
}
11341134

1135-
fn read_raw_event(&mut self, events: &mut [::winapi::um::wincon::INPUT_RECORD],
1135+
fn read_raw_event(&mut self, events: &mut [::windows_sys::Win32::System::Console::INPUT_RECORD],
11361136
timeout: Option<Duration>) -> io::Result<Option<Event>> {
11371137
self.0.read_raw_event(events, timeout)
11381138
}
@@ -1144,7 +1144,7 @@ impl<'a> crate::windows::TerminalExt for TerminalReadGuard<'a> {
11441144
self.0.read_raw(buf, timeout)
11451145
}
11461146

1147-
fn read_raw_event(&mut self, events: &mut [::winapi::um::wincon::INPUT_RECORD],
1147+
fn read_raw_event(&mut self, events: &mut [::windows_sys::Win32::System::Console::INPUT_RECORD],
11481148
timeout: Option<Duration>) -> io::Result<Option<Event>> {
11491149
self.0.read_raw_event(events, timeout)
11501150
}

src/windows/ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::io;
44
use std::time::Duration;
55

6-
use winapi::um::wincon::INPUT_RECORD;
6+
use windows_sys::Win32::System::Console::INPUT_RECORD;
77

88
use crate::priv_util::Private;
99
use crate::terminal::Event;

src/windows/screen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::io;
22
use std::sync::{LockResult, Mutex, MutexGuard, TryLockResult};
33
use std::time::Duration;
44

5-
use winapi::shared::ntdef::HANDLE;
6-
use winapi::um::wincon::INPUT_RECORD;
5+
use windows_sys::Win32::Foundation::HANDLE;
6+
use windows_sys::Win32::System::Console::INPUT_RECORD;
77

88
use crate::buffer::ScreenBuffer;
99
use crate::priv_util::{

src/windows/terminal.rs

Lines changed: 86 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,46 @@ use std::sync::atomic::{AtomicUsize, Ordering};
88
use std::sync::{LockResult, Mutex, MutexGuard, TryLockResult};
99
use std::time::Duration;
1010

11-
use winapi::ctypes::c_int;
12-
use winapi::shared::winerror::{
11+
use windows_sys::Win32::Foundation::{
1312
WAIT_TIMEOUT,
1413
};
15-
use winapi::shared::minwindef::{
16-
FALSE, TRUE,
17-
BOOL, DWORD, WORD,
14+
// windows_sys::Win32::Foundation does not provide type aliases defined below, those aliases are effectively "deprecated" since not present in official win32-metadata
15+
type BOOL = i32;
16+
type DWORD = u32;
17+
type WORD = u16;
18+
type CHAR = i8;
19+
type WCHAR = u16;
20+
type SHORT = i16;
21+
type VOID = core::ffi::c_void;
22+
23+
use windows_sys::Win32::Foundation::{
24+
FALSE, TRUE, HANDLE,
1825
};
19-
use winapi::shared::ntdef::{
20-
CHAR, SHORT, VOID, WCHAR, HANDLE,
21-
};
22-
use winapi::um::consoleapi::{
26+
use windows_sys::Win32::System::Console::{
2327
SetConsoleCtrlHandler,
2428
GetConsoleMode,
2529
ReadConsoleW,
2630
ReadConsoleInputW,
2731
WriteConsoleW,
2832
SetConsoleMode,
2933
};
30-
use winapi::um::handleapi::{
34+
use windows_sys::Win32::Foundation::{
3135
CloseHandle,
3236
};
33-
use winapi::um::processenv::{
37+
use windows_sys::Win32::System::Console::{
3438
GetStdHandle,
3539
};
36-
use winapi::um::synchapi::{
40+
use windows_sys::Win32::System::Threading::{
3741
WaitForSingleObject,
38-
};
39-
use winapi::um::winbase::{
4042
INFINITE,
43+
};
44+
use windows_sys::Win32::System::Console::{
4145
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE,
46+
};
47+
use windows_sys::Win32::Foundation::{
4248
WAIT_FAILED, WAIT_OBJECT_0,
4349
};
44-
use winapi::um::wincon::{
50+
use windows_sys::Win32::System::Console::{
4551
self,
4652
CreateConsoleScreenBuffer,
4753
WriteConsoleInputW,
@@ -55,7 +61,7 @@ use winapi::um::wincon::{
5561
GetConsoleScreenBufferInfo,
5662
SetConsoleTextAttribute,
5763
SetConsoleWindowInfo,
58-
CHAR_INFO, CHAR_INFO_Char, CONSOLE_CURSOR_INFO, CONSOLE_SCREEN_BUFFER_INFO,
64+
CHAR_INFO, CHAR_INFO_0, CONSOLE_CURSOR_INFO, CONSOLE_SCREEN_BUFFER_INFO,
5965
COORD, SMALL_RECT,
6066
CONSOLE_TEXTMODE_BUFFER,
6167
INPUT_RECORD,
@@ -68,9 +74,11 @@ use winapi::um::wincon::{
6874
ENABLE_PROCESSED_OUTPUT, ENABLE_WRAP_AT_EOL_OUTPUT,
6975
KEY_EVENT, MOUSE_EVENT, WINDOW_BUFFER_SIZE_EVENT,
7076
};
71-
use winapi::um::winuser;
72-
use winapi::um::winnt::{
77+
use windows_sys::Win32::UI::Input::KeyboardAndMouse;
78+
use windows_sys::Win32::Foundation::{
7379
GENERIC_READ, GENERIC_WRITE,
80+
};
81+
use windows_sys::Win32::Storage::FileSystem::{
7482
FILE_SHARE_READ, FILE_SHARE_WRITE,
7583
};
7684

@@ -515,10 +523,10 @@ impl<'a> TerminalReadGuard<'a> {
515523
}
516524

517525
fn mouse_event(&mut self, event: &INPUT_RECORD) -> Option<MouseEvent> {
518-
if event.EventType == MOUSE_EVENT {
519-
let mouse = unsafe { event.Event.MouseEvent() };
526+
if u32::from(event.EventType) == MOUSE_EVENT {
527+
let mouse = unsafe { event.Event.MouseEvent };
520528

521-
let input = if mouse.dwEventFlags & wincon::MOUSE_WHEELED != 0 {
529+
let input = if mouse.dwEventFlags & Console::MOUSE_WHEELED != 0 {
522530
// The high word of `dwButtonState` indicates wheel direction
523531
let direction = (mouse.dwButtonState >> 16) as i16;
524532

@@ -855,7 +863,7 @@ impl<'a> TerminalWriteGuard<'a> {
855863

856864
result_bool(unsafe { WriteConsoleW(
857865
self.writer.out_handle,
858-
buf[n..].as_ptr() as *const VOID,
866+
buf[n..].as_ptr(),
859867
len,
860868
&mut n_dw,
861869
ptr::null_mut()) })?;
@@ -945,26 +953,26 @@ fn as_millis(timeout: Option<Duration>) -> DWORD {
945953
fn fg_code(color: Color) -> WORD {
946954
(match color {
947955
Color::Black => 0,
948-
Color::Blue => wincon::FOREGROUND_BLUE,
949-
Color::Cyan => wincon::FOREGROUND_BLUE | wincon::FOREGROUND_GREEN,
950-
Color::Green => wincon::FOREGROUND_GREEN,
951-
Color::Magenta => wincon::FOREGROUND_BLUE | wincon::FOREGROUND_RED,
952-
Color::Red => wincon::FOREGROUND_RED,
953-
Color::White => wincon::FOREGROUND_RED | wincon::FOREGROUND_GREEN | wincon::FOREGROUND_BLUE,
954-
Color::Yellow => wincon::FOREGROUND_RED | wincon::FOREGROUND_GREEN,
956+
Color::Blue => Console::FOREGROUND_BLUE,
957+
Color::Cyan => Console::FOREGROUND_BLUE | Console::FOREGROUND_GREEN,
958+
Color::Green => Console::FOREGROUND_GREEN,
959+
Color::Magenta => Console::FOREGROUND_BLUE | Console::FOREGROUND_RED,
960+
Color::Red => Console::FOREGROUND_RED,
961+
Color::White => Console::FOREGROUND_RED | Console::FOREGROUND_GREEN | Console::FOREGROUND_BLUE,
962+
Color::Yellow => Console::FOREGROUND_RED | Console::FOREGROUND_GREEN,
955963
}) as WORD
956964
}
957965

958966
fn bg_code(color: Color) -> WORD {
959967
(match color {
960968
Color::Black => 0,
961-
Color::Blue => wincon::BACKGROUND_BLUE,
962-
Color::Cyan => wincon::BACKGROUND_BLUE | wincon::BACKGROUND_GREEN,
963-
Color::Green => wincon::BACKGROUND_GREEN,
964-
Color::Magenta => wincon::BACKGROUND_BLUE | wincon::BACKGROUND_RED,
965-
Color::Red => wincon::BACKGROUND_RED,
966-
Color::White => wincon::BACKGROUND_RED | wincon::BACKGROUND_GREEN | wincon::BACKGROUND_BLUE,
967-
Color::Yellow => wincon::BACKGROUND_RED | wincon::BACKGROUND_GREEN,
969+
Color::Blue => Console::BACKGROUND_BLUE,
970+
Color::Cyan => Console::BACKGROUND_BLUE | Console::BACKGROUND_GREEN,
971+
Color::Green => Console::BACKGROUND_GREEN,
972+
Color::Magenta => Console::BACKGROUND_BLUE | Console::BACKGROUND_RED,
973+
Color::Red => Console::BACKGROUND_RED,
974+
Color::White => Console::BACKGROUND_RED | Console::BACKGROUND_GREEN | Console::BACKGROUND_BLUE,
975+
Color::Yellow => Console::BACKGROUND_RED | Console::BACKGROUND_GREEN,
968976
}) as WORD
969977
}
970978

@@ -973,7 +981,7 @@ fn style_code(style: Style) -> WORD {
973981

974982
if style.contains(Style::BOLD) {
975983
// Closest available approximation for bold text
976-
code |= wincon::FOREGROUND_INTENSITY as WORD;
984+
code |= Console::FOREGROUND_INTENSITY as WORD;
977985
}
978986

979987
code
@@ -1078,9 +1086,9 @@ fn bit_to_button(mut bit: DWORD) -> MouseButton {
10781086
assert!(bit != 0);
10791087

10801088
match bit {
1081-
wincon::FROM_LEFT_1ST_BUTTON_PRESSED => MouseButton::Left,
1082-
wincon::RIGHTMOST_BUTTON_PRESSED => MouseButton::Right,
1083-
wincon::FROM_LEFT_2ND_BUTTON_PRESSED => MouseButton::Middle,
1089+
Console::FROM_LEFT_1ST_BUTTON_PRESSED => MouseButton::Left,
1090+
Console::RIGHTMOST_BUTTON_PRESSED => MouseButton::Right,
1091+
Console::FROM_LEFT_2ND_BUTTON_PRESSED => MouseButton::Middle,
10841092
_ => {
10851093
bit >>= 3;
10861094
let mut n = 3;
@@ -1124,15 +1132,15 @@ fn size_to_coord(size: Size) -> COORD {
11241132
}
11251133

11261134
fn has_alt(state: DWORD) -> bool {
1127-
state & (wincon::LEFT_ALT_PRESSED | wincon::RIGHT_ALT_PRESSED) != 0
1135+
state & (Console::LEFT_ALT_PRESSED | Console::RIGHT_ALT_PRESSED) != 0
11281136
}
11291137

11301138
fn has_ctrl(state: DWORD) -> bool {
1131-
state & (wincon::LEFT_CTRL_PRESSED | wincon::RIGHT_CTRL_PRESSED) != 0
1139+
state & (Console::LEFT_CTRL_PRESSED | Console::RIGHT_CTRL_PRESSED) != 0
11321140
}
11331141

11341142
fn has_shift(state: DWORD) -> bool {
1135-
state & wincon::SHIFT_PRESSED != 0
1143+
state & Console::SHIFT_PRESSED != 0
11361144
}
11371145

11381146
fn to_dword(n: usize) -> DWORD {
@@ -1166,48 +1174,48 @@ fn to_short_neg(n: usize) -> SHORT {
11661174
}
11671175

11681176
fn key_press_event(event: &INPUT_RECORD) -> Option<Key> {
1169-
if event.EventType == KEY_EVENT {
1170-
let key = unsafe { event.Event.KeyEvent() };
1177+
if u32::from(event.EventType) == KEY_EVENT {
1178+
let key = unsafe { event.Event.KeyEvent };
11711179

11721180
if key.bKeyDown == FALSE {
11731181
return None;
11741182
}
11751183

1176-
let key = match key.wVirtualKeyCode as c_int {
1177-
winuser::VK_BACK => Key::Backspace,
1178-
winuser::VK_RETURN => Key::Enter,
1179-
winuser::VK_ESCAPE => Key::Escape,
1180-
winuser::VK_TAB => Key::Tab,
1181-
winuser::VK_UP => Key::Up,
1182-
winuser::VK_DOWN => Key::Down,
1183-
winuser::VK_LEFT => Key::Left,
1184-
winuser::VK_RIGHT => Key::Right,
1185-
winuser::VK_DELETE => Key::Delete,
1186-
winuser::VK_INSERT => Key::Insert,
1187-
winuser::VK_HOME => Key::Home,
1188-
winuser::VK_END => Key::End,
1189-
winuser::VK_PRIOR => Key::PageUp,
1190-
winuser::VK_NEXT => Key::PageDown,
1191-
winuser::VK_F1 => Key::F(1),
1192-
winuser::VK_F2 => Key::F(2),
1193-
winuser::VK_F3 => Key::F(3),
1194-
winuser::VK_F4 => Key::F(4),
1195-
winuser::VK_F5 => Key::F(5),
1196-
winuser::VK_F6 => Key::F(6),
1197-
winuser::VK_F7 => Key::F(7),
1198-
winuser::VK_F8 => Key::F(8),
1199-
winuser::VK_F9 => Key::F(9),
1200-
winuser::VK_F10 => Key::F(10),
1201-
winuser::VK_F11 => Key::F(11),
1202-
winuser::VK_F12 => Key::F(12),
1184+
let key = match key.wVirtualKeyCode as KeyboardAndMouse::VIRTUAL_KEY {
1185+
KeyboardAndMouse::VK_BACK => Key::Backspace,
1186+
KeyboardAndMouse::VK_RETURN => Key::Enter,
1187+
KeyboardAndMouse::VK_ESCAPE => Key::Escape,
1188+
KeyboardAndMouse::VK_TAB => Key::Tab,
1189+
KeyboardAndMouse::VK_UP => Key::Up,
1190+
KeyboardAndMouse::VK_DOWN => Key::Down,
1191+
KeyboardAndMouse::VK_LEFT => Key::Left,
1192+
KeyboardAndMouse::VK_RIGHT => Key::Right,
1193+
KeyboardAndMouse::VK_DELETE => Key::Delete,
1194+
KeyboardAndMouse::VK_INSERT => Key::Insert,
1195+
KeyboardAndMouse::VK_HOME => Key::Home,
1196+
KeyboardAndMouse::VK_END => Key::End,
1197+
KeyboardAndMouse::VK_PRIOR => Key::PageUp,
1198+
KeyboardAndMouse::VK_NEXT => Key::PageDown,
1199+
KeyboardAndMouse::VK_F1 => Key::F(1),
1200+
KeyboardAndMouse::VK_F2 => Key::F(2),
1201+
KeyboardAndMouse::VK_F3 => Key::F(3),
1202+
KeyboardAndMouse::VK_F4 => Key::F(4),
1203+
KeyboardAndMouse::VK_F5 => Key::F(5),
1204+
KeyboardAndMouse::VK_F6 => Key::F(6),
1205+
KeyboardAndMouse::VK_F7 => Key::F(7),
1206+
KeyboardAndMouse::VK_F8 => Key::F(8),
1207+
KeyboardAndMouse::VK_F9 => Key::F(9),
1208+
KeyboardAndMouse::VK_F10 => Key::F(10),
1209+
KeyboardAndMouse::VK_F11 => Key::F(11),
1210+
KeyboardAndMouse::VK_F12 => Key::F(12),
12031211
_ => {
12041212
if has_alt(key.dwControlKeyState) {
12051213
return None;
12061214
}
12071215

12081216
let is_ctrl = has_ctrl(key.dwControlKeyState);
12091217

1210-
let u_char = unsafe { *key.uChar.UnicodeChar() };
1218+
let u_char = unsafe { key.uChar.UnicodeChar };
12111219

12121220
if u_char != 0 {
12131221
match char::from_u32(u_char as u32) {
@@ -1228,8 +1236,8 @@ fn key_press_event(event: &INPUT_RECORD) -> Option<Key> {
12281236
}
12291237

12301238
pub fn size_event(event: &INPUT_RECORD) -> Option<Size> {
1231-
if event.EventType == WINDOW_BUFFER_SIZE_EVENT {
1232-
let size = unsafe { event.Event.WindowBufferSizeEvent() };
1239+
if u32::from(event.EventType) == WINDOW_BUFFER_SIZE_EVENT {
1240+
let size = unsafe { event.Event.WindowBufferSizeEvent };
12331241

12341242
Some(Size{
12351243
lines: size.dwSize.Y as usize,
@@ -1240,10 +1248,8 @@ pub fn size_event(event: &INPUT_RECORD) -> Option<Size> {
12401248
}
12411249
}
12421250

1243-
fn unicode_char(wch: WCHAR) -> CHAR_INFO_Char {
1244-
let mut ch: CHAR_INFO_Char = unsafe { zeroed() };
1245-
1246-
unsafe { *ch.UnicodeChar_mut() = wch; }
1251+
fn unicode_char(wch: WCHAR) -> CHAR_INFO_0 {
1252+
let ch = CHAR_INFO_0 { UnicodeChar: wch };
12471253

12481254
ch
12491255
}
@@ -1299,7 +1305,7 @@ unsafe extern "system" fn ctrl_handler(ctrl_type: DWORD) -> BOOL {
12991305
// Wake up the `WaitForSingleObject` call by
13001306
// generating a key up event, which will be ignored.
13011307
let input = INPUT_RECORD{
1302-
EventType: KEY_EVENT,
1308+
EventType: KEY_EVENT as u16,
13031309
// KEY_EVENT { bKeyDown: FALSE, ... }
13041310
Event: zeroed(),
13051311
};

0 commit comments

Comments
 (0)