From ebe19acc1918bd3d22fed57ea257c1ff3f21f615 Mon Sep 17 00:00:00 2001 From: Raphael Amorim Date: Mon, 17 Feb 2025 22:18:37 -0300 Subject: [PATCH] update remaining files --- .../src/platform_impl/windows/drop_handler.rs | 8 +- .../src/platform_impl/windows/event_loop.rs | 66 +++-- rio-window/src/platform_impl/windows/icon.rs | 37 ++- rio-window/src/platform_impl/windows/ime.rs | 8 +- .../src/platform_impl/windows/keyboard.rs | 15 +- .../platform_impl/windows/keyboard_layout.rs | 67 ++--- .../src/platform_impl/windows/monitor.rs | 6 +- .../src/platform_impl/windows/raw_input.rs | 2 +- rio-window/src/platform_impl/windows/util.rs | 2 +- .../src/platform_impl/windows/window.rs | 270 +++++++++++------- .../src/platform_impl/windows/window_state.rs | 12 +- 11 files changed, 286 insertions(+), 207 deletions(-) diff --git a/rio-window/src/platform_impl/windows/drop_handler.rs b/rio-window/src/platform_impl/windows/drop_handler.rs index cfc3e28b7a..1b72d1f6a9 100644 --- a/rio-window/src/platform_impl/windows/drop_handler.rs +++ b/rio-window/src/platform_impl/windows/drop_handler.rs @@ -4,18 +4,16 @@ use std::path::PathBuf; use std::ptr; use std::sync::atomic::{AtomicUsize, Ordering}; -use windows_sys::core::{IUnknown, GUID, HRESULT}; +use windows_sys::core::{GUID, HRESULT}; use windows_sys::Win32::Foundation::{DV_E_FORMATETC, HWND, POINTL, S_OK}; -use windows_sys::Win32::System::Com::{ - IDataObject, DVASPECT_CONTENT, FORMATETC, TYMED_HGLOBAL, -}; +use windows_sys::Win32::System::Com::{DVASPECT_CONTENT, FORMATETC, TYMED_HGLOBAL}; use windows_sys::Win32::System::Ole::{CF_HDROP, DROPEFFECT_COPY, DROPEFFECT_NONE}; use windows_sys::Win32::UI::Shell::{DragFinish, DragQueryFileW, HDROP}; use tracing::debug; use crate::platform_impl::platform::definitions::{ - IDataObjectVtbl, IDropTarget, IDropTargetVtbl, IUnknownVtbl, + IDataObject, IDataObjectVtbl, IDropTarget, IDropTargetVtbl, IUnknown, IUnknownVtbl, }; use crate::platform_impl::platform::WindowId; diff --git a/rio-window/src/platform_impl/windows/event_loop.rs b/rio-window/src/platform_impl/windows/event_loop.rs index 2d6f8933a4..35c6da1f27 100644 --- a/rio-window/src/platform_impl/windows/event_loop.rs +++ b/rio-window/src/platform_impl/windows/event_loop.rs @@ -18,7 +18,6 @@ use std::{mem, panic, ptr}; use crate::utils::Lazy; -use windows_sys::Win32::Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE; use windows_sys::Win32::Foundation::{ GetLastError, FALSE, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WAIT_FAILED, WPARAM, }; @@ -45,7 +44,9 @@ use windows_sys::Win32::UI::Input::Touch::{ CloseTouchInputHandle, GetTouchInputInfo, TOUCHEVENTF_DOWN, TOUCHEVENTF_MOVE, TOUCHEVENTF_UP, TOUCHINPUT, }; -use windows_sys::Win32::UI::Input::{RAWINPUT, RIM_TYPEKEYBOARD, RIM_TYPEMOUSE}; +use windows_sys::Win32::UI::Input::{ + MOUSE_MOVE_RELATIVE, RAWINPUT, RIM_TYPEKEYBOARD, RIM_TYPEMOUSE, +}; use windows_sys::Win32::UI::WindowsAndMessaging::{ CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetClientRect, GetCursorPos, GetMenu, LoadCursorW, MsgWaitForMultipleObjectsEx, PeekMessageW, @@ -432,7 +433,9 @@ impl EventLoop { loop { unsafe { - if PeekMessageW(&mut msg, 0, 0, 0, PM_REMOVE) == false.into() { + if PeekMessageW(&mut msg, ptr::null_mut(), 0, 0, PM_REMOVE) + == false.into() + { break; } @@ -671,10 +674,10 @@ fn create_high_resolution_timer() -> Option { // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION is supported only after // Win10 1803 but it is already default option for rustc // (std uses it to implement `std::thread::sleep`). - if handle == 0 { + if handle.is_null() { None } else { - Some(OwnedHandle::from_raw_handle(handle as *mut c_void)) + Some(OwnedHandle::from_raw_handle(handle)) } } } @@ -973,12 +976,12 @@ fn create_event_target_window() -> HWND { cbClsExtra: 0, cbWndExtra: 0, hInstance: util::get_instance_handle(), - hIcon: 0, - hCursor: 0, // must be null in order for cursor state to work properly - hbrBackground: 0, + hIcon: ptr::null_mut(), + hCursor: ptr::null_mut(), // must be null in order for cursor state to work properly + hbrBackground: ptr::null_mut(), lpszMenuName: ptr::null(), lpszClassName: THREAD_EVENT_TARGET_WINDOW_CLASS.as_ptr(), - hIconSm: 0, + hIconSm: ptr::null_mut(), }; RegisterClassExW(&class); @@ -1001,8 +1004,8 @@ fn create_event_target_window() -> HWND { 0, 0, 0, - 0, - 0, + ptr::null_mut(), + ptr::null_mut(), util::get_instance_handle(), ptr::null(), ); @@ -1338,7 +1341,9 @@ unsafe fn public_window_callback_inner( result = ProcResult::Value(unsafe { DefWindowProcW(window, msg, wparam, lparam) }); if std::mem::take(&mut userdata.window_state_lock().redraw_requested) { - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; } } WM_WINDOWPOSCHANGING => { @@ -1388,7 +1393,7 @@ unsafe fn public_window_callback_inner( unsafe { MonitorFromRect(&new_rect, MONITOR_DEFAULTTONULL) }; match fullscreen { Fullscreen::Borderless(ref mut fullscreen_monitor) => { - if new_monitor != 0 + if !new_monitor.is_null() && fullscreen_monitor .as_ref() .map(|monitor| new_monitor != monitor.hmonitor()) @@ -1851,7 +1856,7 @@ unsafe fn public_window_callback_inner( } WM_KEYUP | WM_SYSKEYUP => { - if msg == WM_SYSKEYUP && unsafe { GetMenu(window) != 0 } { + if msg == WM_SYSKEYUP && unsafe { !GetMenu(window).is_null() } { // let Windows handle event if the window has a native menu, a modal event loop // is started here on Alt key up. result = ProcResult::DefWindowProc(wparam); @@ -2033,7 +2038,7 @@ unsafe fn public_window_callback_inner( // If it is the same as our window, then we're essentially retaining the capture. This // can happen if `SetCapture` is called on our window when it already has the mouse // capture. - if lparam != window { + if lparam != window as isize { userdata.window_state_lock().mouse.capture_count = 0; } result = ProcResult::Value(0); @@ -2042,7 +2047,7 @@ unsafe fn public_window_callback_inner( WM_TOUCH => { let pcount = super::loword(wparam as u32) as usize; let mut inputs = Vec::with_capacity(pcount); - let htouch = lparam; + let htouch = lparam as *mut _; if unsafe { GetTouchInputInfo( htouch, @@ -2301,7 +2306,10 @@ unsafe fn public_window_callback_inner( Some(selected_cursor) => { let hcursor = match selected_cursor { SelectedCursor::Named(cursor_icon) => unsafe { - LoadCursorW(0, util::to_windows_cursor(cursor_icon)) + LoadCursorW( + ptr::null_mut(), + util::to_windows_cursor(cursor_icon), + ) }, SelectedCursor::Custom(cursor) => cursor.as_raw_handle(), }; @@ -2552,7 +2560,7 @@ unsafe fn public_window_callback_inner( unsafe { SetWindowPos( window, - 0, + ptr::null_mut(), new_outer_rect.left, new_outer_rect.top, new_outer_rect.right - new_outer_rect.left, @@ -2640,7 +2648,9 @@ unsafe extern "system" fn thread_event_target_callback( // the git blame and history would be preserved. let callback = || match msg { WM_NCDESTROY => { - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; unsafe { super::set_window_long(window, GWL_USERDATA, 0) }; userdata_removed = true; 0 @@ -2653,13 +2663,17 @@ unsafe extern "system" fn thread_event_target_callback( }, WM_INPUT_DEVICE_CHANGE => { - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; let event = match wparam as u32 { GIDC_ARRIVAL => DeviceEvent::Added, GIDC_REMOVAL => DeviceEvent::Removed, _ => unreachable!(), }; - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; userdata.send_event(Event::DeviceEvent { device_id: wrap_device_id(lparam as u32), @@ -2678,7 +2692,9 @@ unsafe extern "system" fn thread_event_target_callback( } _ if msg == USER_EVENT_MSG_ID.get() => { - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; // synthesis a placeholder UserEvent, so that if the callback is // re-entered it can be buffered for later delivery. the real // user event is still in the mpsc channel and will be pulled @@ -2688,7 +2704,9 @@ unsafe extern "system" fn thread_event_target_callback( 0 } _ if msg == EXEC_MSG_ID.get() => { - unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) }; + unsafe { + RedrawWindow(window, ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT) + }; let mut function: ThreadExecFn = unsafe { Box::from_raw(wparam as *mut _) }; function(); 0 @@ -2718,7 +2736,7 @@ unsafe fn handle_raw_input(userdata: &ThreadMsgTargetData, data: RAWINPUT) { if data.header.dwType == RIM_TYPEMOUSE { let mouse = unsafe { data.data.mouse }; - if util::has_flag(mouse.usFlags as u32, MOUSE_MOVE_RELATIVE) { + if util::has_flag(mouse.usFlags, MOUSE_MOVE_RELATIVE) { let x = mouse.lLastX as f64; let y = mouse.lLastY as f64; diff --git a/rio-window/src/platform_impl/windows/icon.rs b/rio-window/src/platform_impl/windows/icon.rs index c7ae693576..c215833a78 100644 --- a/rio-window/src/platform_impl/windows/icon.rs +++ b/rio-window/src/platform_impl/windows/icon.rs @@ -1,7 +1,7 @@ use std::ffi::c_void; use std::path::Path; use std::sync::Arc; -use std::{fmt, io, mem}; +use std::{fmt, io, mem, ptr}; use cursor_icon::CursorIcon; use windows_sys::core::PCWSTR; @@ -42,7 +42,7 @@ impl RgbaIcon { assert_eq!(and_mask.len(), pixel_count); let handle = unsafe { CreateIcon( - 0, + ptr::null_mut(), self.width as i32, self.height as i32, 1, @@ -51,7 +51,7 @@ impl RgbaIcon { rgba.as_ptr(), ) }; - if handle != 0 { + if !handle.is_null() { Ok(WinIcon::from_handle(handle)) } else { Err(BadIcon::OsError(io::Error::last_os_error())) @@ -70,6 +70,9 @@ struct RaiiIcon { handle: HICON, } +unsafe impl Send for RaiiIcon {} +unsafe impl Sync for RaiiIcon {} + #[derive(Clone)] pub struct WinIcon { inner: Arc, @@ -93,7 +96,7 @@ impl WinIcon { let handle = unsafe { LoadImageW( - 0, + ptr::null_mut(), wide_path.as_ptr(), IMAGE_ICON, width, @@ -101,7 +104,7 @@ impl WinIcon { LR_DEFAULTSIZE | LR_LOADFROMFILE, ) }; - if handle != 0 { + if !handle.is_null() { Ok(WinIcon::from_handle(handle as HICON)) } else { Err(BadIcon::OsError(io::Error::last_os_error())) @@ -124,7 +127,7 @@ impl WinIcon { LR_DEFAULTSIZE, ) }; - if handle != 0 { + if !handle.is_null() { Ok(WinIcon::from_handle(handle as HICON)) } else { Err(BadIcon::OsError(io::Error::last_os_error())) @@ -138,7 +141,12 @@ impl WinIcon { pub fn set_for_window(&self, hwnd: HWND, icon_type: IconType) { unsafe { - SendMessageW(hwnd, WM_SETICON, icon_type as usize, self.as_raw_handle()); + SendMessageW( + hwnd, + WM_SETICON, + icon_type as usize, + self.as_raw_handle() as isize, + ); } } @@ -194,13 +202,13 @@ impl WinCursor { let h = image.height as i32; unsafe { - let hdc_screen = GetDC(0); - if hdc_screen == 0 { + let hdc_screen = GetDC(ptr::null_mut()); + if hdc_screen.is_null() { return Err(io::Error::last_os_error()); } let hbm_color = CreateCompatibleBitmap(hdc_screen, w, h); - ReleaseDC(0, hdc_screen); - if hbm_color == 0 { + ReleaseDC(ptr::null_mut(), hdc_screen); + if hbm_color.is_null() { return Err(io::Error::last_os_error()); } if SetBitmapBits(hbm_color, bgra.len() as u32, bgra.as_ptr() as *const c_void) @@ -213,7 +221,7 @@ impl WinCursor { // Mask created according to https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createbitmap#parameters let mask_bits: Vec = vec![0xff; ((((w + 15) >> 4) << 1) * h) as usize]; let hbm_mask = CreateBitmap(w, h, 1, 1, mask_bits.as_ptr() as *const _); - if hbm_mask == 0 { + if hbm_mask.is_null() { DeleteObject(hbm_color); return Err(io::Error::last_os_error()); } @@ -229,7 +237,7 @@ impl WinCursor { let handle = CreateIconIndirect(&icon_info as *const _); DeleteObject(hbm_color); DeleteObject(hbm_mask); - if handle == 0 { + if handle.is_null() { return Err(io::Error::last_os_error()); } @@ -243,6 +251,9 @@ pub struct RaiiCursor { handle: HCURSOR, } +unsafe impl Send for RaiiCursor {} +unsafe impl Sync for RaiiCursor {} + impl Drop for RaiiCursor { fn drop(&mut self) { unsafe { DestroyCursor(self.handle) }; diff --git a/rio-window/src/platform_impl/windows/ime.rs b/rio-window/src/platform_impl/windows/ime.rs index 9bf565689f..d4a4d27ec8 100644 --- a/rio-window/src/platform_impl/windows/ime.rs +++ b/rio-window/src/platform_impl/windows/ime.rs @@ -3,12 +3,12 @@ use std::os::windows::prelude::OsStringExt; use std::ptr::null_mut; use windows_sys::Win32::Foundation::{POINT, RECT}; -use windows_sys::Win32::Globalization::HIMC; use windows_sys::Win32::UI::Input::Ime::{ ImmAssociateContextEx, ImmGetCompositionStringW, ImmGetContext, ImmReleaseContext, ImmSetCandidateWindow, ImmSetCompositionWindow, ATTR_TARGET_CONVERTED, ATTR_TARGET_NOTCONVERTED, CANDIDATEFORM, CFS_EXCLUDE, CFS_POINT, COMPOSITIONFORM, - GCS_COMPATTR, GCS_COMPSTR, GCS_CURSORPOS, GCS_RESULTSTR, IACE_CHILDREN, IACE_DEFAULT, + GCS_COMPATTR, GCS_COMPSTR, GCS_CURSORPOS, GCS_RESULTSTR, HIMC, IACE_CHILDREN, + IACE_DEFAULT, }; use windows_sys::Win32::UI::WindowsAndMessaging::{GetSystemMetrics, SM_IMMENABLED}; @@ -152,9 +152,9 @@ impl ImeContext { } if allowed { - unsafe { ImmAssociateContextEx(hwnd, 0, IACE_DEFAULT) }; + unsafe { ImmAssociateContextEx(hwnd, null_mut(), IACE_DEFAULT) }; } else { - unsafe { ImmAssociateContextEx(hwnd, 0, IACE_CHILDREN) }; + unsafe { ImmAssociateContextEx(hwnd, null_mut(), IACE_CHILDREN) }; } } diff --git a/rio-window/src/platform_impl/windows/keyboard.rs b/rio-window/src/platform_impl/windows/keyboard.rs index bee47dd0d9..16e83d4a14 100644 --- a/rio-window/src/platform_impl/windows/keyboard.rs +++ b/rio-window/src/platform_impl/windows/keyboard.rs @@ -10,15 +10,14 @@ use windows_sys::Win32::Foundation::{HWND, LPARAM, WPARAM}; use windows_sys::Win32::System::SystemServices::LANG_KOREAN; use windows_sys::Win32::UI::Input::KeyboardAndMouse::{ GetAsyncKeyState, GetKeyState, GetKeyboardLayout, GetKeyboardState, MapVirtualKeyExW, - MAPVK_VK_TO_VSC_EX, MAPVK_VSC_TO_VK_EX, VIRTUAL_KEY, VK_ABNT_C2, VK_ADD, VK_CAPITAL, - VK_CLEAR, VK_CONTROL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, VK_F4, - VK_HOME, VK_INSERT, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, VK_MENU, - VK_MULTIPLY, VK_NEXT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, - VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_PRIOR, - VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN, VK_SCROLL, VK_SHIFT, - VK_SUBTRACT, VK_UP, + HKL, MAPVK_VK_TO_VSC_EX, MAPVK_VSC_TO_VK_EX, VIRTUAL_KEY, VK_ABNT_C2, VK_ADD, + VK_CAPITAL, VK_CLEAR, VK_CONTROL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, + VK_F4, VK_HOME, VK_INSERT, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, + VK_MENU, VK_MULTIPLY, VK_NEXT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, + VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, + VK_PRIOR, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN, VK_SCROLL, + VK_SHIFT, VK_SUBTRACT, VK_UP, }; -use windows_sys::Win32::UI::TextServices::HKL; use windows_sys::Win32::UI::WindowsAndMessaging::{ PeekMessageW, MSG, PM_NOREMOVE, WM_CHAR, WM_DEADCHAR, WM_KEYDOWN, WM_KEYFIRST, WM_KEYLAST, WM_KEYUP, WM_KILLFOCUS, WM_SETFOCUS, WM_SYSCHAR, WM_SYSDEADCHAR, diff --git a/rio-window/src/platform_impl/windows/keyboard_layout.rs b/rio-window/src/platform_impl/windows/keyboard_layout.rs index cdf26c3868..c4c2d30653 100644 --- a/rio-window/src/platform_impl/windows/keyboard_layout.rs +++ b/rio-window/src/platform_impl/windows/keyboard_layout.rs @@ -8,39 +8,40 @@ use crate::utils::Lazy; use smol_str::SmolStr; use windows_sys::Win32::System::SystemServices::{LANG_JAPANESE, LANG_KOREAN}; use windows_sys::Win32::UI::Input::KeyboardAndMouse::{ - GetKeyState, GetKeyboardLayout, MapVirtualKeyExW, ToUnicodeEx, MAPVK_VK_TO_VSC_EX, - VIRTUAL_KEY, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_BACK, VK_BROWSER_BACK, - VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME, VK_BROWSER_REFRESH, - VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_CANCEL, VK_CAPITAL, VK_CLEAR, VK_CONTROL, - VK_CONVERT, VK_CRSEL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, VK_EREOF, - VK_ESCAPE, VK_EXECUTE, VK_EXSEL, VK_F1, VK_F10, VK_F11, VK_F12, VK_F13, VK_F14, - VK_F15, VK_F16, VK_F17, VK_F18, VK_F19, VK_F2, VK_F20, VK_F21, VK_F22, VK_F23, - VK_F24, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_FINAL, VK_GAMEPAD_A, - VK_GAMEPAD_B, VK_GAMEPAD_DPAD_DOWN, VK_GAMEPAD_DPAD_LEFT, VK_GAMEPAD_DPAD_RIGHT, - VK_GAMEPAD_DPAD_UP, VK_GAMEPAD_LEFT_SHOULDER, VK_GAMEPAD_LEFT_THUMBSTICK_BUTTON, - VK_GAMEPAD_LEFT_THUMBSTICK_DOWN, VK_GAMEPAD_LEFT_THUMBSTICK_LEFT, - VK_GAMEPAD_LEFT_THUMBSTICK_RIGHT, VK_GAMEPAD_LEFT_THUMBSTICK_UP, - VK_GAMEPAD_LEFT_TRIGGER, VK_GAMEPAD_MENU, VK_GAMEPAD_RIGHT_SHOULDER, - VK_GAMEPAD_RIGHT_THUMBSTICK_BUTTON, VK_GAMEPAD_RIGHT_THUMBSTICK_DOWN, - VK_GAMEPAD_RIGHT_THUMBSTICK_LEFT, VK_GAMEPAD_RIGHT_THUMBSTICK_RIGHT, - VK_GAMEPAD_RIGHT_THUMBSTICK_UP, VK_GAMEPAD_RIGHT_TRIGGER, VK_GAMEPAD_VIEW, - VK_GAMEPAD_X, VK_GAMEPAD_Y, VK_HANGUL, VK_HANJA, VK_HELP, VK_HOME, VK_ICO_00, - VK_ICO_CLEAR, VK_ICO_HELP, VK_INSERT, VK_JUNJA, VK_KANA, VK_KANJI, VK_LAUNCH_APP1, - VK_LAUNCH_APP2, VK_LAUNCH_MAIL, VK_LAUNCH_MEDIA_SELECT, VK_LBUTTON, VK_LCONTROL, - VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, VK_MBUTTON, VK_MEDIA_NEXT_TRACK, - VK_MEDIA_PLAY_PAUSE, VK_MEDIA_PREV_TRACK, VK_MEDIA_STOP, VK_MENU, VK_MODECHANGE, - VK_MULTIPLY, VK_NAVIGATION_ACCEPT, VK_NAVIGATION_CANCEL, VK_NAVIGATION_DOWN, - VK_NAVIGATION_LEFT, VK_NAVIGATION_MENU, VK_NAVIGATION_RIGHT, VK_NAVIGATION_UP, - VK_NAVIGATION_VIEW, VK_NEXT, VK_NONAME, VK_NONCONVERT, VK_NUMLOCK, VK_NUMPAD0, - VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, - VK_NUMPAD8, VK_NUMPAD9, VK_OEM_1, VK_OEM_102, VK_OEM_2, VK_OEM_3, VK_OEM_4, VK_OEM_5, - VK_OEM_6, VK_OEM_7, VK_OEM_8, VK_OEM_ATTN, VK_OEM_AUTO, VK_OEM_AX, VK_OEM_BACKTAB, - VK_OEM_CLEAR, VK_OEM_COMMA, VK_OEM_COPY, VK_OEM_CUSEL, VK_OEM_ENLW, VK_OEM_FINISH, - VK_OEM_FJ_LOYA, VK_OEM_FJ_MASSHOU, VK_OEM_FJ_ROYA, VK_OEM_FJ_TOUROKU, VK_OEM_JUMP, - VK_OEM_MINUS, VK_OEM_NEC_EQUAL, VK_OEM_PA1, VK_OEM_PA2, VK_OEM_PA3, VK_OEM_PERIOD, - VK_OEM_PLUS, VK_OEM_RESET, VK_OEM_WSCTRL, VK_PA1, VK_PACKET, VK_PAUSE, VK_PLAY, - VK_PRINT, VK_PRIOR, VK_PROCESSKEY, VK_RBUTTON, VK_RCONTROL, VK_RETURN, VK_RIGHT, - VK_RMENU, VK_RSHIFT, VK_RWIN, VK_SCROLL, VK_SELECT, VK_SEPARATOR, VK_SHIFT, VK_SLEEP, + GetKeyState, GetKeyboardLayout, MapVirtualKeyExW, ToUnicodeEx, HKL, + MAPVK_VK_TO_VSC_EX, VIRTUAL_KEY, VK_ACCEPT, VK_ADD, VK_APPS, VK_ATTN, VK_BACK, + VK_BROWSER_BACK, VK_BROWSER_FAVORITES, VK_BROWSER_FORWARD, VK_BROWSER_HOME, + VK_BROWSER_REFRESH, VK_BROWSER_SEARCH, VK_BROWSER_STOP, VK_CANCEL, VK_CAPITAL, + VK_CLEAR, VK_CONTROL, VK_CONVERT, VK_CRSEL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, + VK_DOWN, VK_END, VK_EREOF, VK_ESCAPE, VK_EXECUTE, VK_EXSEL, VK_F1, VK_F10, VK_F11, + VK_F12, VK_F13, VK_F14, VK_F15, VK_F16, VK_F17, VK_F18, VK_F19, VK_F2, VK_F20, + VK_F21, VK_F22, VK_F23, VK_F24, VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, + VK_FINAL, VK_GAMEPAD_A, VK_GAMEPAD_B, VK_GAMEPAD_DPAD_DOWN, VK_GAMEPAD_DPAD_LEFT, + VK_GAMEPAD_DPAD_RIGHT, VK_GAMEPAD_DPAD_UP, VK_GAMEPAD_LEFT_SHOULDER, + VK_GAMEPAD_LEFT_THUMBSTICK_BUTTON, VK_GAMEPAD_LEFT_THUMBSTICK_DOWN, + VK_GAMEPAD_LEFT_THUMBSTICK_LEFT, VK_GAMEPAD_LEFT_THUMBSTICK_RIGHT, + VK_GAMEPAD_LEFT_THUMBSTICK_UP, VK_GAMEPAD_LEFT_TRIGGER, VK_GAMEPAD_MENU, + VK_GAMEPAD_RIGHT_SHOULDER, VK_GAMEPAD_RIGHT_THUMBSTICK_BUTTON, + VK_GAMEPAD_RIGHT_THUMBSTICK_DOWN, VK_GAMEPAD_RIGHT_THUMBSTICK_LEFT, + VK_GAMEPAD_RIGHT_THUMBSTICK_RIGHT, VK_GAMEPAD_RIGHT_THUMBSTICK_UP, + VK_GAMEPAD_RIGHT_TRIGGER, VK_GAMEPAD_VIEW, VK_GAMEPAD_X, VK_GAMEPAD_Y, VK_HANGUL, + VK_HANJA, VK_HELP, VK_HOME, VK_ICO_00, VK_ICO_CLEAR, VK_ICO_HELP, VK_INSERT, + VK_JUNJA, VK_KANA, VK_KANJI, VK_LAUNCH_APP1, VK_LAUNCH_APP2, VK_LAUNCH_MAIL, + VK_LAUNCH_MEDIA_SELECT, VK_LBUTTON, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, + VK_LWIN, VK_MBUTTON, VK_MEDIA_NEXT_TRACK, VK_MEDIA_PLAY_PAUSE, VK_MEDIA_PREV_TRACK, + VK_MEDIA_STOP, VK_MENU, VK_MODECHANGE, VK_MULTIPLY, VK_NAVIGATION_ACCEPT, + VK_NAVIGATION_CANCEL, VK_NAVIGATION_DOWN, VK_NAVIGATION_LEFT, VK_NAVIGATION_MENU, + VK_NAVIGATION_RIGHT, VK_NAVIGATION_UP, VK_NAVIGATION_VIEW, VK_NEXT, VK_NONAME, + VK_NONCONVERT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, + VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_OEM_1, + VK_OEM_102, VK_OEM_2, VK_OEM_3, VK_OEM_4, VK_OEM_5, VK_OEM_6, VK_OEM_7, VK_OEM_8, + VK_OEM_ATTN, VK_OEM_AUTO, VK_OEM_AX, VK_OEM_BACKTAB, VK_OEM_CLEAR, VK_OEM_COMMA, + VK_OEM_COPY, VK_OEM_CUSEL, VK_OEM_ENLW, VK_OEM_FINISH, VK_OEM_FJ_LOYA, + VK_OEM_FJ_MASSHOU, VK_OEM_FJ_ROYA, VK_OEM_FJ_TOUROKU, VK_OEM_JUMP, VK_OEM_MINUS, + VK_OEM_NEC_EQUAL, VK_OEM_PA1, VK_OEM_PA2, VK_OEM_PA3, VK_OEM_PERIOD, VK_OEM_PLUS, + VK_OEM_RESET, VK_OEM_WSCTRL, VK_PA1, VK_PACKET, VK_PAUSE, VK_PLAY, VK_PRINT, + VK_PRIOR, VK_PROCESSKEY, VK_RBUTTON, VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, + VK_RSHIFT, VK_RWIN, VK_SCROLL, VK_SELECT, VK_SEPARATOR, VK_SHIFT, VK_SLEEP, VK_SNAPSHOT, VK_SPACE, VK_SUBTRACT, VK_TAB, VK_UP, VK_VOLUME_DOWN, VK_VOLUME_MUTE, VK_VOLUME_UP, VK_XBUTTON1, VK_XBUTTON2, VK_ZOOM, }; diff --git a/rio-window/src/platform_impl/windows/monitor.rs b/rio-window/src/platform_impl/windows/monitor.rs index d9dac91c88..c2b3365aea 100644 --- a/rio-window/src/platform_impl/windows/monitor.rs +++ b/rio-window/src/platform_impl/windows/monitor.rs @@ -80,11 +80,9 @@ impl VideoModeHandle { pub struct MonitorHandle(HMONITOR); // Send is not implemented for HMONITOR, we have to wrap it and implement it manually. -// For more info see: -// https://github.com/retep998/winapi-rs/issues/360 -// https://github.com/retep998/winapi-rs/issues/396 unsafe impl Send for MonitorHandle {} +unsafe impl Sync for MonitorHandle {} unsafe extern "system" fn monitor_enum_proc( hmonitor: HMONITOR, @@ -101,7 +99,7 @@ pub fn available_monitors() -> VecDeque { let mut monitors: VecDeque = VecDeque::new(); unsafe { EnumDisplayMonitors( - 0, + ptr::null_mut(), ptr::null(), Some(monitor_enum_proc), &mut monitors as *mut _ as LPARAM, diff --git a/rio-window/src/platform_impl/windows/raw_input.rs b/rio-window/src/platform_impl/windows/raw_input.rs index 86cc8fc37e..afa2f893ec 100644 --- a/rio-window/src/platform_impl/windows/raw_input.rs +++ b/rio-window/src/platform_impl/windows/raw_input.rs @@ -159,7 +159,7 @@ pub fn register_all_mice_and_keyboards_for_raw_input( // RIDEV_REMOVE: don't receive device events (requires NULL hwndTarget) let flags = match filter { DeviceEvents::Never => { - window_handle = 0; + window_handle = ptr::null_mut(); RIDEV_REMOVE } DeviceEvents::WhenFocused => RIDEV_DEVNOTIFY, diff --git a/rio-window/src/platform_impl/windows/util.rs b/rio-window/src/platform_impl/windows/util.rs index 71fd3c693b..3ae1f7d865 100644 --- a/rio-window/src/platform_impl/windows/util.rs +++ b/rio-window/src/platform_impl/windows/util.rs @@ -199,7 +199,7 @@ pub(super) fn get_function_impl(library: &str, function: &str) -> Option<*const // Library names we will use are ASCII so we can use the A version to avoid string conversion. let module = unsafe { LoadLibraryA(library.as_ptr()) }; - if module == 0 { + if module.is_null() { return None; } diff --git a/rio-window/src/platform_impl/windows/window.rs b/rio-window/src/platform_impl/windows/window.rs index 1615718c9d..b5e862dfc6 100644 --- a/rio-window/src/platform_impl/windows/window.rs +++ b/rio-window/src/platform_impl/windows/window.rs @@ -78,10 +78,25 @@ use crate::window::{ WindowAttributes, WindowButtons, WindowLevel, }; +#[derive(Clone, Copy)] +#[repr(transparent)] +/// We need to pass the window handle to the event loop thread, which means it needs to be +/// Send+Sync. +struct SyncWindowHandle(HWND); + +unsafe impl Send for SyncWindowHandle {} +unsafe impl Sync for SyncWindowHandle {} + +impl SyncWindowHandle { + fn hwnd(&self) -> HWND { + self.0 + } +} + /// The Win32 implementation of the main `Window` object. pub(crate) struct Window { /// Main handle for the window. - window: HWND, + window: SyncWindowHandle, /// The current window state. window_state: Arc>, @@ -131,9 +146,11 @@ impl Window { let window_state = Arc::clone(&self.window_state); self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::TRANSPARENT, transparent) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::TRANSPARENT, transparent), + ); }); } @@ -145,15 +162,17 @@ impl Window { let window_state = Arc::clone(&self.window_state); self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::VISIBLE, visible) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::VISIBLE, visible), + ); }); } #[inline] pub fn is_visible(&self) -> Option { - Some(unsafe { IsWindowVisible(self.window) == 1 }) + Some(unsafe { IsWindowVisible(self.window.hwnd()) == 1 }) } #[inline] @@ -161,7 +180,7 @@ impl Window { // NOTE: mark that we requested a redraw to handle requests during `WM_PAINT` handling. self.window_state.lock().unwrap().redraw_requested = true; unsafe { - RedrawWindow(self.hwnd(), ptr::null(), 0, RDW_INTERNALPAINT); + RedrawWindow(self.hwnd(), ptr::null(), ptr::null_mut(), RDW_INTERNALPAINT); } } @@ -199,22 +218,24 @@ impl Window { let window = self.window; self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::MAXIMIZED, false) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::MAXIMIZED, false), + ); }); unsafe { SetWindowPos( self.hwnd(), - 0, + ptr::null_mut(), x, y, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE, ); - InvalidateRgn(self.hwnd(), 0, false.into()); + InvalidateRgn(self.hwnd(), ptr::null_mut(), false.into()); } } @@ -261,7 +282,7 @@ impl Window { let _ = &window; WindowState::set_window_flags( window_state.lock().unwrap(), - window, + window.hwnd(), |f| f.set(WindowFlags::MAXIMIZED, false), ); }); @@ -306,9 +327,11 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::RESIZABLE, resizable) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::RESIZABLE, resizable), + ); }); } @@ -325,20 +348,24 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set( - WindowFlags::MINIMIZABLE, - buttons.contains(WindowButtons::MINIMIZE), - ); - f.set( - WindowFlags::MAXIMIZABLE, - buttons.contains(WindowButtons::MAXIMIZE), - ); - f.set( - WindowFlags::CLOSABLE, - buttons.contains(WindowButtons::CLOSE), - ) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| { + f.set( + WindowFlags::MINIMIZABLE, + buttons.contains(WindowButtons::MINIMIZE), + ); + f.set( + WindowFlags::MAXIMIZABLE, + buttons.contains(WindowButtons::MAXIMIZE), + ); + f.set( + WindowFlags::CLOSABLE, + buttons.contains(WindowButtons::CLOSE), + ) + }, + ); }); } @@ -360,7 +387,7 @@ impl Window { /// Returns the `hwnd` of this window. #[inline] pub fn hwnd(&self) -> HWND { - self.window + self.window.hwnd() } #[inline] @@ -369,7 +396,7 @@ impl Window { ) -> Result { let mut window_handle = raw_window_handle::Win32WindowHandle::new(unsafe { // SAFETY: Handle will never be zero. - std::num::NonZeroIsize::new_unchecked(self.window) + std::num::NonZeroIsize::new_unchecked(self.window.hwnd() as isize) }); let hinstance = unsafe { super::get_window_long(self.hwnd(), GWLP_HINSTANCE) }; window_handle.hinstance = std::num::NonZeroIsize::new(hinstance); @@ -407,7 +434,8 @@ impl Window { self.window_state_lock().mouse.selected_cursor = SelectedCursor::Named(icon); self.thread_executor.execute_in_thread(move || unsafe { - let cursor = LoadCursorW(0, util::to_windows_cursor(icon)); + let cursor = + LoadCursorW(ptr::null_mut(), util::to_windows_cursor(icon)); SetCursor(cursor); }); } @@ -448,7 +476,7 @@ impl Window { .lock() .unwrap() .mouse - .set_cursor_flags(window, |f| f.set(CursorFlags::GRABBED, confine)) + .set_cursor_flags(window.hwnd(), |f| f.set(CursorFlags::GRABBED, confine)) .map_err(|e| ExternalError::Os(os_error!(e))); let _ = tx.send(result); }); @@ -525,7 +553,7 @@ impl Window { unsafe { PostMessageW( - window, + window.hwnd(), WM_NCLBUTTONDOWN, wparam, &points as *const _ as LPARAM, @@ -584,7 +612,7 @@ impl Window { // get the current system menu let h_menu = GetSystemMenu(self.hwnd(), 0); - if h_menu == 0 { + if h_menu.is_null() { warn!("The corresponding window doesn't have a system menu"); // This situation should not be treated as an error so just return without showing // menu. @@ -651,9 +679,11 @@ impl Window { let window = self.window; let window_state = Arc::clone(&self.window_state); self.thread_executor.execute_in_thread(move || { - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::IGNORE_CURSOR_EVENT, !hittest) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::IGNORE_CURSOR_EVENT, !hittest), + ); }); Ok(()) @@ -677,9 +707,11 @@ impl Window { &mut window_state.lock().unwrap(), |f| f.set(WindowFlags::MINIMIZED, is_minimized), ); - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::MINIMIZED, minimized) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::MINIMIZED, minimized), + ); }); } @@ -695,9 +727,11 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::MAXIMIZED, maximized) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::MAXIMIZED, maximized), + ); }); } @@ -729,7 +763,7 @@ impl Window { ( Some(Fullscreen::Borderless(Some(monitor))), Some(Fullscreen::Borderless(None)), - ) if *monitor == monitor::current_monitor(window) => return, + ) if *monitor == monitor::current_monitor(window.hwnd()) => return, _ => {} } @@ -750,7 +784,7 @@ impl Window { ChangeDisplaySettingsExW( monitor_info.szDevice.as_ptr(), &*video_mode.native_video_mode, - 0, + ptr::null(), CDS_FULLSCREEN, ptr::null(), ) @@ -767,7 +801,7 @@ impl Window { ChangeDisplaySettingsExW( ptr::null(), ptr::null(), - 0, + ptr::null(), CDS_FULLSCREEN, ptr::null(), ) @@ -792,20 +826,24 @@ impl Window { // fine, taking control back from the DWM and ensuring that the `SetWindowPos` call // below goes through. let mut msg = mem::zeroed(); - PeekMessageW(&mut msg, 0, 0, 0, PM_NOREMOVE); + PeekMessageW(&mut msg, ptr::null(), 0, 0, PM_NOREMOVE); } // Update window style - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set( - WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN, - matches!(fullscreen, Some(Fullscreen::Exclusive(_))), - ); - f.set( - WindowFlags::MARKER_BORDERLESS_FULLSCREEN, - matches!(fullscreen, Some(Fullscreen::Borderless(_))), - ); - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| { + f.set( + WindowFlags::MARKER_EXCLUSIVE_FULLSCREEN, + matches!(fullscreen, Some(Fullscreen::Exclusive(_))), + ); + f.set( + WindowFlags::MARKER_BORDERLESS_FULLSCREEN, + matches!(fullscreen, Some(Fullscreen::Borderless(_))), + ); + }, + ); // Mark as fullscreen window wrt to z-order // @@ -813,7 +851,7 @@ impl Window { // will generate WM_SIZE messages of the old window size that can race with what we set // below unsafe { - taskbar_mark_fullscreen(window, fullscreen.is_some()); + taskbar_mark_fullscreen(window.hwnd(), fullscreen.is_some()); } // Update window bounds @@ -822,7 +860,7 @@ impl Window { // Save window bounds before entering fullscreen let placement = unsafe { let mut placement = mem::zeroed(); - GetWindowPlacement(window, &mut placement); + GetWindowPlacement(window.hwnd(), &mut placement); placement }; @@ -832,7 +870,9 @@ impl Window { let monitor = match &fullscreen { Fullscreen::Exclusive(video_mode) => video_mode.monitor(), Fullscreen::Borderless(Some(monitor)) => monitor.clone(), - Fullscreen::Borderless(None) => monitor::current_monitor(window), + Fullscreen::Borderless(None) => { + monitor::current_monitor(window.hwnd()) + } }; let position: (i32, i32) = monitor.position().into(); @@ -840,15 +880,15 @@ impl Window { unsafe { SetWindowPos( - window, - 0, + window.hwnd(), + ptr::null_mut(), position.0, position.1, size.0 as i32, size.1 as i32, SWP_ASYNCWINDOWPOS | SWP_NOZORDER, ); - InvalidateRgn(window, 0, false.into()); + InvalidateRgn(window.hwnd(), 0, false.into()); } } None => { @@ -858,8 +898,8 @@ impl Window { { drop(window_state_lock); unsafe { - SetWindowPlacement(window, &placement); - InvalidateRgn(window, 0, false.into()); + SetWindowPlacement(window.hwnd(), &placement); + InvalidateRgn(window.hwnd(), ptr::null_mut(), false.into()); } } } @@ -874,9 +914,11 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::MARKER_DECORATIONS, decorations) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::MARKER_DECORATIONS, decorations), + ); }); } @@ -895,16 +937,20 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set( - WindowFlags::ALWAYS_ON_TOP, - level == WindowLevel::AlwaysOnTop, - ); - f.set( - WindowFlags::ALWAYS_ON_BOTTOM, - level == WindowLevel::AlwaysOnBottom, - ); - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| { + f.set( + WindowFlags::ALWAYS_ON_TOP, + level == WindowLevel::AlwaysOnTop, + ); + f.set( + WindowFlags::ALWAYS_ON_BOTTOM, + level == WindowLevel::AlwaysOnBottom, + ); + }, + ); }); } @@ -948,7 +994,11 @@ impl Window { let state = self.window_state.clone(); self.thread_executor.execute_in_thread(move || unsafe { let scale_factor = state.lock().unwrap().scale_factor; - ImeContext::current(window).set_ime_cursor_area(spot, size, scale_factor); + ImeContext::current(window.hwnd()).set_ime_cursor_area( + spot, + size, + scale_factor, + ); }); } @@ -958,7 +1008,7 @@ impl Window { let state = self.window_state.clone(); self.thread_executor.execute_in_thread(move || unsafe { state.lock().unwrap().ime_allowed = allowed; - ImeContext::set_ime_allowed(window, allowed); + ImeContext::set_ime_allowed(window.hwnd(), allowed); }) } @@ -969,7 +1019,7 @@ impl Window { pub fn request_user_attention(&self, request_type: Option) { let window = self.window; let active_window_handle = unsafe { GetActiveWindow() }; - if window == active_window_handle { + if window.hwnd() == active_window_handle { return; } @@ -987,7 +1037,7 @@ impl Window { let flash_info = FLASHWINFO { cbSize: mem::size_of::() as u32, - hwnd: window, + hwnd: window.hwnd(), dwFlags: flags, uCount: count, dwTimeout: 0, @@ -998,7 +1048,7 @@ impl Window { #[inline] pub fn set_theme(&self, theme: Option) { - try_theme(self.window, theme); + try_theme(self.window.hwnd(), theme); } #[inline] @@ -1013,9 +1063,9 @@ impl Window { } pub fn title(&self) -> String { - let len = unsafe { GetWindowTextLengthW(self.window) } + 1; + let len = unsafe { GetWindowTextLengthW(self.window.hwnd()) } + 1; let mut buf = vec![0; len as usize]; - unsafe { GetWindowTextW(self.window, buf.as_mut_ptr(), len) }; + unsafe { GetWindowTextW(self.window.hwnd(), buf.as_mut_ptr(), len) }; util::decode_wide(&buf).to_string_lossy().to_string() } @@ -1032,9 +1082,11 @@ impl Window { self.thread_executor.execute_in_thread(move || { let _ = &window; - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - f.set(WindowFlags::MARKER_UNDECORATED_SHADOW, shadow) - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| f.set(WindowFlags::MARKER_UNDECORATED_SHADOW, shadow), + ); }); } @@ -1056,10 +1108,10 @@ impl Window { let is_visible = window_flags.contains(WindowFlags::VISIBLE); let is_minimized = util::is_minimized(self.hwnd()); - let is_foreground = self.window == unsafe { GetForegroundWindow() }; + let is_foreground = self.window.hwnd() == unsafe { GetForegroundWindow() }; if is_visible && !is_minimized && !is_foreground { - unsafe { force_window_active(self.window) }; + unsafe { force_window_active(self.window.hwnd()) }; } } @@ -1205,9 +1257,11 @@ impl<'a> InitData<'a> { self.attributes.preferred_theme, ); let window_state = Arc::new(Mutex::new(window_state)); - WindowState::set_window_flags(window_state.lock().unwrap(), window, |f| { - *f = self.window_flags - }); + WindowState::set_window_flags( + window_state.lock().unwrap(), + window.hwnd(), + |f| *f = self.window_flags, + ); window_state }; @@ -1216,7 +1270,7 @@ impl<'a> InitData<'a> { unsafe { ImeContext::set_ime_allowed(window, false) }; Window { - window, + window: SyncWindowHandle(window), window_state, thread_executor: self.event_loop.create_thread_executor(), } @@ -1239,7 +1293,7 @@ impl<'a> InitData<'a> { let file_drop_runner = self.event_loop.runner_shared.clone(); let file_drop_handler = FileDropHandler::new( - win.window, + win.window.hwnd(), Box::new(move |event| { if let Ok(e) = event.map_nonuser_event() { file_drop_runner.send_event(e) @@ -1252,7 +1306,7 @@ impl<'a> InitData<'a> { }; assert_eq!( - unsafe { RegisterDragDrop(win.window, handler_interface_ptr) }, + unsafe { RegisterDragDrop(win.window.hwnd(), handler_interface_ptr) }, S_OK ); Some(file_drop_handler) @@ -1456,8 +1510,8 @@ unsafe fn init( CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - parent.unwrap_or(0), - menu.unwrap_or(0), + parent.unwrap_or(ptr::null_mut()), + menu.unwrap_or(ptr::null_mut()), util::get_instance_handle(), &mut initdata as *mut _ as *mut _, ) @@ -1468,7 +1522,7 @@ unsafe fn init( panic::resume_unwind(panic_error) } - if handle == 0 { + if handle.is_null() { return Err(os_error!(io::Error::last_os_error())); } @@ -1481,7 +1535,7 @@ unsafe fn init( // size. if fullscreen.is_some() { win.set_fullscreen(fullscreen.map(Into::into)); - unsafe { force_window_active(win.window) }; + unsafe { force_window_active(win.window.hwnd()) }; } else if maximized { win.set_maximized(true); } @@ -1497,12 +1551,12 @@ unsafe fn register_window_class(class_name: &[u16]) { cbClsExtra: 0, cbWndExtra: 0, hInstance: util::get_instance_handle(), - hIcon: 0, - hCursor: 0, // must be null in order for cursor state to work properly - hbrBackground: 0, + hIcon: ptr::null_mut(), + hCursor: ptr::null_mut(), // must be null in order for cursor state to work properly + hbrBackground: ptr::null_mut(), lpszMenuName: ptr::null(), lpszClassName: class_name.as_ptr(), - hIconSm: 0, + hIconSm: ptr::null_mut(), }; // We ignore errors because registering the same window class twice would trigger diff --git a/rio-window/src/platform_impl/windows/window_state.rs b/rio-window/src/platform_impl/windows/window_state.rs index f39b7e720e..b15232e121 100644 --- a/rio-window/src/platform_impl/windows/window_state.rs +++ b/rio-window/src/platform_impl/windows/window_state.rs @@ -4,8 +4,8 @@ use crate::keyboard::ModifiersState; use crate::platform_impl::platform::{event_loop, util, Fullscreen, SelectedCursor}; use crate::window::{Theme, WindowAttributes}; use bitflags::bitflags; -use std::io; use std::sync::MutexGuard; +use std::{io, ptr}; use windows_sys::Win32::Foundation::{HWND, RECT}; use windows_sys::Win32::Graphics::Gdi::InvalidateRgn; use windows_sys::Win32::UI::WindowsAndMessaging::{ @@ -357,7 +357,7 @@ impl WindowFlags { 0, SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE, ); - InvalidateRgn(window, 0, false.into()); + InvalidateRgn(window, ptr::null_mut(), false.into()); } } @@ -436,7 +436,7 @@ impl WindowFlags { } // Refresh the window frame - SetWindowPos(window, 0, 0, 0, 0, 0, flags); + SetWindowPos(window, ptr::null_mut(), 0, 0, 0, 0, flags); SendMessageW( window, event_loop::SET_RETAIN_STATE_ON_SIZE_MSG_ID.get(), @@ -459,7 +459,7 @@ impl WindowFlags { } util::win_to_err({ - let b_menu = GetMenu(hwnd) != 0; + let b_menu = !GetMenu(hwnd).is_null(); if let (Some(get_dpi_for_window), Some(adjust_window_rect_ex_for_dpi)) = ( *util::GET_DPI_FOR_WINDOW, *util::ADJUST_WINDOW_RECT_EX_FOR_DPI, @@ -501,7 +501,7 @@ impl WindowFlags { let (width, height): (u32, u32) = self.adjust_size(hwnd, size).into(); SetWindowPos( hwnd, - 0, + ptr::null_mut(), 0, 0, width as _, @@ -512,7 +512,7 @@ impl WindowFlags { | SWP_NOMOVE | SWP_NOACTIVATE, ); - InvalidateRgn(hwnd, 0, false.into()); + InvalidateRgn(hwnd, ptr::null_mut(), false.into()); } } }