From 1997b2f7f661239a843cb104784ddcf8096ae391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Thu, 1 Sep 2022 07:43:14 +0200 Subject: [PATCH] Attempt to fix windows build after upgrade --- Cargo.lock | 50 ++++++++++++++--------------- Cargo.toml | 5 +-- build.rs | 15 --------- src/display_picker/mod.rs | 2 +- src/display_picker/windows.rs | 59 +++++++++++++++++++---------------- src/zoom_picker.rs | 17 +++++----- 6 files changed, 68 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b47d4a6..aa74860 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2289,15 +2289,15 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows" -version = "0.32.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbedf6db9096bc2364adce0ae0aa636dcd89f3c3f2cd67947062aaf0ca2a10ec" +checksum = "f1c4bd0a50ac6020f65184721f758dba47bb9fbc2133df715ec74a237b26794a" dependencies = [ - "windows_aarch64_msvc 0.32.0", - "windows_i686_gnu 0.32.0", - "windows_i686_msvc 0.32.0", - "windows_x86_64_gnu 0.32.0", - "windows_x86_64_msvc 0.32.0", + "windows_aarch64_msvc 0.39.0", + "windows_i686_gnu 0.39.0", + "windows_i686_msvc 0.39.0", + "windows_x86_64_gnu 0.39.0", + "windows_x86_64_msvc 0.39.0", ] [[package]] @@ -2313,12 +2313,6 @@ dependencies = [ "windows_x86_64_msvc 0.36.1", ] -[[package]] -name = "windows_aarch64_msvc" -version = "0.32.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" - [[package]] name = "windows_aarch64_msvc" version = "0.36.1" @@ -2326,10 +2320,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" [[package]] -name = "windows_i686_gnu" -version = "0.32.0" +name = "windows_aarch64_msvc" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" +checksum = "ec7711666096bd4096ffa835238905bb33fb87267910e154b18b44eaabb340f2" [[package]] name = "windows_i686_gnu" @@ -2338,10 +2332,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" [[package]] -name = "windows_i686_msvc" -version = "0.32.0" +name = "windows_i686_gnu" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" +checksum = "763fc57100a5f7042e3057e7e8d9bdd7860d330070251a73d003563a3bb49e1b" [[package]] name = "windows_i686_msvc" @@ -2350,10 +2344,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" [[package]] -name = "windows_x86_64_gnu" -version = "0.32.0" +name = "windows_i686_msvc" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" +checksum = "7bc7cbfe58828921e10a9f446fcaaf649204dcfe6c1ddd712c5eebae6bda1106" [[package]] name = "windows_x86_64_gnu" @@ -2362,10 +2356,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" [[package]] -name = "windows_x86_64_msvc" -version = "0.32.0" +name = "windows_x86_64_gnu" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" +checksum = "6868c165637d653ae1e8dc4d82c25d4f97dd6605eaa8d784b5c6e0ab2a252b65" [[package]] name = "windows_x86_64_msvc" @@ -2373,6 +2367,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +[[package]] +name = "windows_x86_64_msvc" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e4d40883ae9cae962787ca76ba76390ffa29214667a111db9e0a1ad8377e809" + [[package]] name = "winit" version = "0.26.1" diff --git a/Cargo.toml b/Cargo.toml index f251658..7eec069 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,10 +29,7 @@ once_cell = "1" x11rb = { version = "0.9", features = ["image", "cursor", "resource_manager"] } [target.'cfg(windows)'.dependencies] -windows = "0.32" - -[target.'cfg(windows)'.build-dependencies] -windows = "0.32" +windows = { version = "0.39", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_System_LibraryLoader", "Win32_UI_WindowsAndMessaging"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] native-dialog = "0.6" diff --git a/build.rs b/build.rs index ef8c840..fa27fda 100644 --- a/build.rs +++ b/build.rs @@ -1,19 +1,4 @@ fn main() { - #[cfg(windows)] - windows::build! { - Windows::Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, PWSTR, WPARAM, HWND}, - Windows::Win32::Graphics::Gdi::{ - BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, ReleaseDC, - SelectObject, SetStretchBltMode, UpdateWindow, CLR_INVALID, HBITMAP, HDC, - StretchBlt, Rectangle - }, - Windows::Win32::System::LibraryLoader::GetModuleHandleW, - Windows::Win32::UI::WindowsAndMessaging::{ - CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow, - SHOW_WINDOW_CMD, WINDOW_EX_STYLE, WINDOW_STYLE, RegisterClassExW, ShowWindow, WNDCLASSEXW, - } - } - #[cfg(target_os = "macos")] { println!("cargo:rustc-link-lib=framework=Foundation"); diff --git a/src/display_picker/mod.rs b/src/display_picker/mod.rs index 2a86be8..469bc42 100644 --- a/src/display_picker/mod.rs +++ b/src/display_picker/mod.rs @@ -32,7 +32,7 @@ pub fn init_display_picker() -> Option> { .ok() .map(|conn| Rc::new(conn) as Rc); #[cfg(windows)] - return Some(Rc::new(windows::WinConn::new()) as Rc); + return Some(Rc::new(windows::WinConn::new().ok()?) as Rc); #[cfg(target_os = "macos")] return Some(Rc::new(macos::MacConn) as Rc); #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))] diff --git a/src/display_picker/windows.rs b/src/display_picker/windows.rs index cdd8300..d28d007 100644 --- a/src/display_picker/windows.rs +++ b/src/display_picker/windows.rs @@ -1,28 +1,31 @@ #![cfg(windows)] -windows::include_bindings!(); - -use std::ptr::{null, null_mut}; +use std::ptr::null; use crate::color::Color; use crate::display_picker::DisplayPicker; -use anyhow::{Error, Result}; +use anyhow::{Context, Error, Result}; use egui::Color32; -use Windows::Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, PWSTR, WPARAM}; -use Windows::Win32::Graphics::Gdi::{ - BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, Rectangle, - ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt, UpdateWindow, CLR_INVALID, - COLORONCOLOR, HBITMAP, HDC, SRCCOPY, -}; -use Windows::Win32::System::LibraryLoader::GetModuleHandleW; -use Windows::Win32::UI::WindowsAndMessaging::{ - CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow, - RegisterClassExW, ShowWindow, WNDCLASSEXW, +use windows::{ + core::PCWSTR, + Win32::Foundation::{HINSTANCE, LPARAM, LRESULT, POINT, WPARAM}, + Win32::Graphics::Gdi::{ + BitBlt, CreateCompatibleBitmap, CreateCompatibleDC, DeleteDC, GetDC, GetPixel, Rectangle, + ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt, UpdateWindow, CLR_INVALID, + COLORONCOLOR, HBITMAP, HDC, SRCCOPY, + }, + Win32::System::LibraryLoader::GetModuleHandleW, + Win32::UI::WindowsAndMessaging::{ + CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetDesktopWindow, MoveWindow, + RegisterClassExW, ShowWindow, WNDCLASSEXW, + }, }; -pub use Windows::Win32::Foundation::HWND; -pub use Windows::Win32::UI::WindowsAndMessaging::{ - SHOW_WINDOW_CMD, SW_SHOWDEFAULT, WINDOW_EX_STYLE, WINDOW_STYLE, WS_BORDER, WS_POPUP, +pub use windows::{ + Win32::Foundation::HWND, + Win32::UI::WindowsAndMessaging::{ + SHOW_WINDOW_CMD, SW_SHOWDEFAULT, WINDOW_EX_STYLE, WINDOW_STYLE, WS_BORDER, WS_POPUP, + }, }; macro_rules! handle_winapi_call { @@ -102,11 +105,13 @@ pub struct WinConn { } impl WinConn { - pub fn new() -> Self { - WinConn { + pub fn new() -> Result { + Ok(WinConn { device_context: unsafe { GetDC(None) }, - hinstance: unsafe { GetModuleHandleW(PWSTR(null_mut())) }, - } + hinstance: unsafe { + GetModuleHandleW(PCWSTR::null()).context("failed to get module handle")? + }, + }) } pub fn get_cursor_pos(&self) -> Result { @@ -121,7 +126,7 @@ impl WinConn { } pub fn get_pixel(&self, pos: POINT) -> Result { - let color = unsafe { GetPixel(&self.device_context, pos.x, pos.y) }; + let color = unsafe { GetPixel(self.device_context, pos.x, pos.y) }; if color == CLR_INVALID { return Err(Error::msg("failed to get pixel")); } @@ -164,25 +169,25 @@ impl DisplayPickerExt for WinConn { height: i32, style: WINDOW_STYLE, ) -> Result { - let mut class_name = class_name.encode_utf16().collect::>(); + let class_name = class_name.encode_utf16().collect::>(); let mut class = WNDCLASSEXW { cbSize: std::mem::size_of::() as u32, ..Default::default() }; - class.lpszClassName = PWSTR(class_name.as_mut_ptr()); + class.lpszClassName = PCWSTR::from_raw(class_name.as_ptr()); class.hInstance = self.hinstance; class.lpfnWndProc = Some(wnd_proc); - let mut window_name = window_name.encode_utf16().collect::>(); + let window_name = window_name.encode_utf16().collect::>(); unsafe { RegisterClassExW(&class as *const WNDCLASSEXW) }; unsafe { Ok(CreateWindowExW( WINDOW_EX_STYLE(0), - PWSTR(class_name.as_mut_ptr()), - PWSTR(window_name.as_mut_ptr()), + PCWSTR::from_raw(class_name.as_ptr()), + PCWSTR::from_raw(window_name.as_ptr()), style, x, y, diff --git a/src/zoom_picker.rs b/src/zoom_picker.rs index 8d9ec22..7e5ad67 100644 --- a/src/zoom_picker.rs +++ b/src/zoom_picker.rs @@ -15,6 +15,13 @@ use x11rb::protocol::xproto; #[cfg(windows)] use crate::display_picker::windows::{HWND, SW_SHOWDEFAULT, WS_BORDER, WS_POPUP}; + +#[cfg(any(target_os = "linux"))] +const ZOOM_IMAGE_WIDTH: u16 = ZOOM_WIN_WIDTH / ZOOM_SCALE as u16; +#[cfg(any(target_os = "linux"))] +const ZOOM_IMAGE_HEIGHT: u16 = ZOOM_WIN_HEIGHT / ZOOM_SCALE as u16; +#[cfg(any(target_os = "linux"))] +const ZOOM_WIN_BORDER_WIDTH: u32 = 2; #[cfg(any(target_os = "linux", windows))] static CURSOR_PICKER_WINDOW_NAME: &str = "epick - cursor picker"; #[cfg(any(target_os = "linux", windows))] @@ -24,18 +31,12 @@ const ZOOM_WIN_WIDTH: u16 = 160; #[cfg(any(target_os = "linux", windows))] const ZOOM_WIN_HEIGHT: u16 = 160; #[cfg(any(target_os = "linux", windows))] -const ZOOM_IMAGE_WIDTH: u16 = ZOOM_WIN_WIDTH / ZOOM_SCALE as u16; -#[cfg(any(target_os = "linux", windows))] -const ZOOM_IMAGE_HEIGHT: u16 = ZOOM_WIN_HEIGHT / ZOOM_SCALE as u16; -#[cfg(any(target_os = "linux", windows))] const ZOOM_WIN_OFFSET: i32 = 50; #[cfg(any(target_os = "linux", windows))] const ZOOM_WIN_POINTER_DIAMETER: u16 = 10; #[cfg(windows)] const ZOOM_WIN_POINTER_RADIUS: u16 = ZOOM_WIN_POINTER_DIAMETER / 2; #[cfg(any(target_os = "linux", windows))] -const ZOOM_WIN_BORDER_WIDTH: u32 = 2; -#[cfg(any(target_os = "linux", windows))] const ZOOM_IMAGE_X_OFFSET: i32 = ((ZOOM_WIN_WIDTH / 2) as f32 / ZOOM_SCALE) as i32; #[cfg(any(target_os = "linux", windows))] const ZOOM_IMAGE_Y_OFFSET: i32 = ((ZOOM_WIN_HEIGHT / 2) as f32 / ZOOM_SCALE) as i32; @@ -105,8 +106,8 @@ impl ZoomPicker { if let Ok(window) = picker.spawn_window( "EPICK_DIALOG", CURSOR_PICKER_WINDOW_NAME, - (cursor_pos.0 - ZOOM_IMAGE_X_OFFSET), - (cursor_pos.1 - ZOOM_IMAGE_Y_OFFSET), + cursor_pos.0 - ZOOM_IMAGE_X_OFFSET, + cursor_pos.1 - ZOOM_IMAGE_Y_OFFSET, ZOOM_WIN_WIDTH as i32, ZOOM_WIN_HEIGHT as i32, WS_POPUP | WS_BORDER,