From e9ee8dcd9f53096a0096c01b5027d7584376cbd2 Mon Sep 17 00:00:00 2001 From: killian Date: Fri, 30 Sep 2022 13:49:02 +0200 Subject: [PATCH] Migrate from winapi to windows-sys --- Cargo.lock | 4 +- alacritty/Cargo.toml | 7 ++- alacritty/src/daemon.rs | 2 +- alacritty/src/display/window.rs | 4 +- alacritty/src/main.rs | 2 +- alacritty/src/panic.rs | 15 +++--- alacritty_terminal/Cargo.toml | 11 +++-- alacritty_terminal/src/tty/windows/child.rs | 23 +++++---- alacritty_terminal/src/tty/windows/conpty.rs | 50 ++++++++++---------- 9 files changed, 64 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81fc9b90..40279968 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,7 +39,7 @@ dependencies = [ "serde_yaml", "unicode-width", "wayland-client", - "winapi 0.3.9", + "windows-sys", "x11-dl", "xdg", ] @@ -91,7 +91,7 @@ dependencies = [ "signal-hook-mio", "unicode-width", "vte", - "winapi 0.3.9", + "windows-sys", ] [[package]] diff --git a/alacritty/Cargo.toml b/alacritty/Cargo.toml index 20083ddf..4ca77b11 100644 --- a/alacritty/Cargo.toml +++ b/alacritty/Cargo.toml @@ -62,7 +62,12 @@ x11-dl = { version = "2", optional = true } wayland-client = { version = "0.29.0", features = ["dlopen"], optional = true } [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3.7", features = ["impl-default", "wincon"]} +windows-sys = { version = "0.36", features = [ + "Win32_UI_WindowsAndMessaging", + "Win32_System_Threading", + "Win32_System_Console", + "Win32_Foundation", +]} [target.'cfg(windows)'.build-dependencies] embed-resource = "1.7.2" diff --git a/alacritty/src/daemon.rs b/alacritty/src/daemon.rs index 730a2cc7..df66646a 100644 --- a/alacritty/src/daemon.rs +++ b/alacritty/src/daemon.rs @@ -18,7 +18,7 @@ use { #[cfg(not(windows))] use libc::pid_t; #[cfg(windows)] -use winapi::um::winbase::{CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW}; +use windows_sys::Win32::System::Threading::{CREATE_NEW_PROCESS_GROUP, CREATE_NO_WINDOW}; #[cfg(target_os = "macos")] use crate::macos; diff --git a/alacritty/src/display/window.rs b/alacritty/src/display/window.rs index 95b42345..6c88f56f 100644 --- a/alacritty/src/display/window.rs +++ b/alacritty/src/display/window.rs @@ -47,8 +47,6 @@ use glutin::{self, ContextBuilder, PossiblyCurrent, Rect, WindowedContext}; use objc::{msg_send, sel, sel_impl}; #[cfg(target_os = "macos")] use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; -#[cfg(windows)] -use winapi::shared::minwindef::WORD; use alacritty_terminal::index::Point; @@ -63,7 +61,7 @@ static WINDOW_ICON: &[u8] = include_bytes!("../../extra/logo/compat/alacritty-te /// This should match the definition of IDI_ICON from `alacritty.rc`. #[cfg(windows)] -const IDI_ICON: WORD = 0x101; +const IDI_ICON: u16 = 0x101; /// Context creation flags from probing config. static GL_CONTEXT_CREATION_FLAGS: AtomicU8 = AtomicU8::new(GlContextFlags::SRGB.bits); diff --git a/alacritty/src/main.rs b/alacritty/src/main.rs index 2e7c30a9..569ed30e 100644 --- a/alacritty/src/main.rs +++ b/alacritty/src/main.rs @@ -25,7 +25,7 @@ use glutin::event_loop::EventLoopBuilder as GlutinEventLoopBuilder; use glutin::platform::unix::EventLoopWindowTargetExtUnix; use log::info; #[cfg(windows)] -use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}; +use windows_sys::Win32::System::Console::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}; use alacritty_terminal::tty; diff --git a/alacritty/src/panic.rs b/alacritty/src/panic.rs index 2311d7b9..2637f8d6 100644 --- a/alacritty/src/panic.rs +++ b/alacritty/src/panic.rs @@ -1,7 +1,9 @@ use std::io::Write; -use std::{io, panic, ptr}; +use std::{io, panic}; -use winapi::um::winuser; +use windows_sys::Win32::UI::WindowsAndMessaging::{ + MessageBoxW, MB_ICONERROR, MB_OK, MB_SETFOREGROUND, MB_TASKMODAL, +}; use alacritty_terminal::tty::windows::win32_string; @@ -12,14 +14,11 @@ pub fn attach_handler() { let _ = writeln!(io::stderr(), "{}", panic_info); let msg = format!("{}\n\nPress Ctrl-C to Copy", panic_info); unsafe { - winuser::MessageBoxW( - ptr::null_mut(), + MessageBoxW( + 0isize, win32_string(&msg).as_ptr(), win32_string("Alacritty: Runtime Error").as_ptr(), - winuser::MB_ICONERROR - | winuser::MB_OK - | winuser::MB_SETFOREGROUND - | winuser::MB_TASKMODAL, + MB_ICONERROR | MB_OK | MB_SETFOREGROUND | MB_TASKMODAL, ); } })); diff --git a/alacritty_terminal/Cargo.toml b/alacritty_terminal/Cargo.toml index 4ad14f5a..90fee671 100644 --- a/alacritty_terminal/Cargo.toml +++ b/alacritty_terminal/Cargo.toml @@ -39,11 +39,14 @@ signal-hook-mio = { version = "0.2.1", features = ["support-v0_6"] } [target.'cfg(windows)'.dependencies] miow = "0.3" -winapi = { version = "0.3.7", features = [ - "impl-default", "basetsd", "libloaderapi", "minwindef", "ntdef", "processthreadsapi", "winbase", - "wincon", "wincontypes", "winerror", "winnt", "winuser", "consoleapi", -]} mio-anonymous-pipes = "0.2" +windows-sys = { version = "0.36", features = [ + "Win32_System_Console", + "Win32_Foundation", + "Win32_Security", + "Win32_System_Threading", + "Win32_System_WindowsProgramming", +]} [dev-dependencies] serde_json = "1.0.0" diff --git a/alacritty_terminal/src/tty/windows/child.rs b/alacritty_terminal/src/tty/windows/child.rs index fc163600..91dd1725 100644 --- a/alacritty_terminal/src/tty/windows/child.rs +++ b/alacritty_terminal/src/tty/windows/child.rs @@ -4,14 +4,16 @@ use std::sync::atomic::{AtomicPtr, Ordering}; use mio_extras::channel::{channel, Receiver, Sender}; -use winapi::shared::ntdef::{BOOLEAN, HANDLE, PVOID}; -use winapi::um::winbase::{RegisterWaitForSingleObject, UnregisterWait, INFINITE}; -use winapi::um::winnt::{WT_EXECUTEINWAITTHREAD, WT_EXECUTEONLYONCE}; +use windows_sys::Win32::Foundation::{BOOLEAN, HANDLE}; +use windows_sys::Win32::System::Threading::{ + RegisterWaitForSingleObject, UnregisterWait, WT_EXECUTEINWAITTHREAD, WT_EXECUTEONLYONCE, +}; +use windows_sys::Win32::System::WindowsProgramming::INFINITE; use crate::tty::ChildEvent; /// WinAPI callback to run when child process exits. -extern "system" fn child_exit_callback(ctx: PVOID, timed_out: BOOLEAN) { +extern "system" fn child_exit_callback(ctx: *mut c_void, timed_out: BOOLEAN) { if timed_out != 0 { return; } @@ -29,7 +31,7 @@ impl ChildExitWatcher { pub fn new(child_handle: HANDLE) -> Result { let (event_tx, event_rx) = channel::(); - let mut wait_handle: HANDLE = 0 as HANDLE; + let mut wait_handle: HANDLE = 0; let sender_ref = Box::new(event_tx); let success = unsafe { @@ -37,7 +39,7 @@ impl ChildExitWatcher { &mut wait_handle, child_handle, Some(child_exit_callback), - Box::into_raw(sender_ref) as PVOID, + Box::into_raw(sender_ref).cast(), INFINITE, WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE, ) @@ -46,7 +48,10 @@ impl ChildExitWatcher { if success == 0 { Err(Error::last_os_error()) } else { - Ok(ChildExitWatcher { wait_handle: AtomicPtr::from(wait_handle), event_rx }) + Ok(ChildExitWatcher { + wait_handle: AtomicPtr::from(wait_handle as *mut c_void), + event_rx, + }) } } @@ -58,7 +63,7 @@ impl ChildExitWatcher { impl Drop for ChildExitWatcher { fn drop(&mut self) { unsafe { - UnregisterWait(self.wait_handle.load(Ordering::Relaxed)); + UnregisterWait(self.wait_handle.load(Ordering::Relaxed) as HANDLE); } } } @@ -78,7 +83,7 @@ mod tests { const WAIT_TIMEOUT: Duration = Duration::from_millis(200); let mut child = Command::new("cmd.exe").spawn().unwrap(); - let child_exit_watcher = ChildExitWatcher::new(child.as_raw_handle()).unwrap(); + let child_exit_watcher = ChildExitWatcher::new(child.as_raw_handle() as HANDLE).unwrap(); let mut events = Events::with_capacity(1); let poll = Poll::new().unwrap(); diff --git a/alacritty_terminal/src/tty/windows/conpty.rs b/alacritty_terminal/src/tty/windows/conpty.rs index e856a1b1..3f6349de 100644 --- a/alacritty_terminal/src/tty/windows/conpty.rs +++ b/alacritty_terminal/src/tty/windows/conpty.rs @@ -3,17 +3,17 @@ use std::os::windows::io::IntoRawHandle; use std::{mem, ptr}; use mio_anonymous_pipes::{EventedAnonRead, EventedAnonWrite}; -use winapi::shared::basetsd::{PSIZE_T, SIZE_T}; -use winapi::shared::minwindef::BYTE; -use winapi::shared::ntdef::LPWSTR; -use winapi::shared::winerror::S_OK; -use winapi::um::consoleapi::{ClosePseudoConsole, CreatePseudoConsole, ResizePseudoConsole}; -use winapi::um::processthreadsapi::{ - CreateProcessW, InitializeProcThreadAttributeList, UpdateProcThreadAttribute, - PROCESS_INFORMATION, STARTUPINFOW, + +use windows_sys::core::PWSTR; +use windows_sys::Win32::Foundation::{HANDLE, S_OK}; +use windows_sys::Win32::System::Console::{ + ClosePseudoConsole, CreatePseudoConsole, ResizePseudoConsole, COORD, HPCON, +}; +use windows_sys::Win32::System::Threading::{ + CreateProcessW, InitializeProcThreadAttributeList, UpdateProcThreadAttribute, + EXTENDED_STARTUPINFO_PRESENT, PROCESS_INFORMATION, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, + STARTF_USESTDHANDLES, STARTUPINFOEXW, STARTUPINFOW, }; -use winapi::um::winbase::{EXTENDED_STARTUPINFO_PRESENT, STARTF_USESTDHANDLES, STARTUPINFOEXW}; -use winapi::um::wincontypes::{COORD, HPCON}; use crate::config::PtyConfig; use crate::event::{OnResize, WindowSize}; @@ -39,7 +39,7 @@ impl Drop for Conpty { unsafe impl Send for Conpty {} pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { - let mut pty_handle = 0 as HPCON; + let mut pty_handle: HPCON = 0; // Passing 0 as the size parameter allows the "system default" buffer // size to be used. There may be small performance and memory advantages @@ -52,10 +52,10 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { let result = unsafe { CreatePseudoConsole( window_size.into(), - conin_pty_handle.into_raw_handle(), - conout_pty_handle.into_raw_handle(), + conin_pty_handle.into_raw_handle() as HANDLE, + conout_pty_handle.into_raw_handle() as HANDLE, 0, - &mut pty_handle as *mut HPCON, + &mut pty_handle as *mut _, ) }; @@ -65,11 +65,11 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { // Prepare child process startup info. - let mut size: SIZE_T = 0; + let mut size: usize = 0; - let mut startup_info_ex: STARTUPINFOEXW = Default::default(); + let mut startup_info_ex: STARTUPINFOEXW = unsafe { mem::zeroed() }; - startup_info_ex.StartupInfo.lpTitle = std::ptr::null_mut() as LPWSTR; + startup_info_ex.StartupInfo.lpTitle = std::ptr::null_mut() as PWSTR; startup_info_ex.StartupInfo.cb = mem::size_of::() as u32; @@ -80,7 +80,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { // Create the appropriately sized thread attribute list. unsafe { let failure = - InitializeProcThreadAttributeList(ptr::null_mut(), 1, 0, &mut size as PSIZE_T) > 0; + InitializeProcThreadAttributeList(ptr::null_mut(), 1, 0, &mut size as *mut usize) > 0; // This call was expected to return false. if failure { @@ -88,7 +88,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { } } - let mut attr_list: Box<[BYTE]> = vec![0; size].into_boxed_slice(); + let mut attr_list: Box<[u8]> = vec![0; size].into_boxed_slice(); // Set startup info's attribute list & initialize it // @@ -106,7 +106,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { startup_info_ex.lpAttributeList, 1, 0, - &mut size as PSIZE_T, + &mut size as *mut usize, ) > 0; if !success { @@ -119,8 +119,8 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { success = UpdateProcThreadAttribute( startup_info_ex.lpAttributeList, 0, - 22 | 0x0002_0000, // PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE. - pty_handle, + PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE as usize, + pty_handle as *mut std::ffi::c_void, mem::size_of::(), ptr::null_mut(), ptr::null_mut(), @@ -134,11 +134,11 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { let cmdline = win32_string(&cmdline(config)); let cwd = config.working_directory.as_ref().map(win32_string); - let mut proc_info: PROCESS_INFORMATION = Default::default(); + let mut proc_info: PROCESS_INFORMATION = unsafe { mem::zeroed() }; unsafe { success = CreateProcessW( ptr::null(), - cmdline.as_ptr() as LPWSTR, + cmdline.as_ptr() as PWSTR, ptr::null_mut(), ptr::null_mut(), false as i32, @@ -158,7 +158,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize) -> Option { let conout = EventedAnonRead::new(conout); let child_watcher = ChildExitWatcher::new(proc_info.hProcess).unwrap(); - let conpty = Conpty { handle: pty_handle }; + let conpty = Conpty { handle: pty_handle as HPCON }; Some(Pty::new(conpty, conout, conin, child_watcher)) }