From 0e6841bd8a35e7a403e2393e817f03d6fc89c11c Mon Sep 17 00:00:00 2001 From: JobyGitGud Date: Sun, 11 May 2025 18:11:58 -0500 Subject: [PATCH] Clean up dead code and fix warnings while preserving Steam drop functionality and cross-platform skeleton code --- src/platform/mod.rs | 2 +- src/platform/windows/cursor.rs | 9 ++-- src/platform/windows/hook.rs | 97 ---------------------------------- src/platform/windows/mod.rs | 4 +- src/platform/windows/state.rs | 22 ++------ src/platform/windows/types.rs | 34 ++---------- src/platform/windows/window.rs | 13 ----- 7 files changed, 14 insertions(+), 167 deletions(-) delete mode 100644 src/platform/windows/hook.rs delete mode 100644 src/platform/windows/window.rs diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 2b0b37d..873c902 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -20,7 +20,7 @@ mod unsupported; // Export the platform-specific functions #[cfg(windows)] -pub use windows::{hide_system_cursor, update_cursor_image, restore_cursor, handle_mouse_move}; +pub use windows::{hide_system_cursor, update_cursor_image, restore_cursor}; #[cfg(target_os = "macos")] pub use macos::{hide_system_cursor, update_cursor_image, restore_cursor, render_cursor}; diff --git a/src/platform/windows/cursor.rs b/src/platform/windows/cursor.rs index 76baef1..84c1bb3 100644 --- a/src/platform/windows/cursor.rs +++ b/src/platform/windows/cursor.rs @@ -5,10 +5,10 @@ use std::ptr::null_mut; use std::sync::atomic::Ordering; use winapi::{ um::winuser::{ - GetDC, ReleaseDC, GetCursorPos, LoadCursorW, SetSystemCursor, CopyIcon, - CreateCursor, SystemParametersInfoW, SPI_SETCURSORS, SetCursor, + GetDC, ReleaseDC, LoadCursorW, SetSystemCursor, CopyIcon, + SystemParametersInfoW, SPI_SETCURSORS, SetCursor, MAKEINTRESOURCEW, IDC_ARROW, CreateIconIndirect, ICONINFO, - GetIconInfo, DestroyIcon, + DestroyIcon, }, um::wingdi::{ CreateCompatibleDC, DeleteDC, SelectObject, DeleteObject, @@ -16,7 +16,6 @@ use winapi::{ }, um::consoleapi::SetConsoleCtrlHandler, shared::minwindef::{TRUE, FALSE, DWORD}, - shared::windef::{POINT, HBITMAP}, }; use ::image::{RgbaImage, open as image_open}; @@ -364,6 +363,8 @@ pub fn restore_cursor() { } // Function to handle mouse movement and update the cursor +// Kept for potential future use but currently not needed with SetSystemCursor approach +#[allow(dead_code)] pub fn handle_mouse_move() { // With SetSystemCursor, we don't need to constantly update the cursor on mouse move // This function is kept for API compatibility, but it's now a no-op diff --git a/src/platform/windows/hook.rs b/src/platform/windows/hook.rs deleted file mode 100644 index 42c3ed2..0000000 --- a/src/platform/windows/hook.rs +++ /dev/null @@ -1,97 +0,0 @@ -// Windows platform mouse hook management - -use std::ptr::null_mut; -use std::sync::atomic::Ordering; -use winapi::{ - um::winuser::{ - SetWindowsHookExW, UnhookWindowsHookEx, CallNextHookEx, - WH_MOUSE_LL, HC_ACTION, MSLLHOOKSTRUCT, - }, - um::libloaderapi::GetModuleHandleW, - shared::minwindef::{WPARAM, LPARAM, LRESULT}, -}; - -use super::types::*; -use super::state::*; - -// Low-level mouse hook procedure -pub unsafe extern "system" fn low_level_mouse_proc(n_code: i32, w_param: WPARAM, l_param: LPARAM) -> LRESULT { - if n_code == HC_ACTION { - // Extract mouse position from the hook struct for any mouse event - let p_msll_hook_struct = l_param as *const MSLLHOOKSTRUCT; - if !p_msll_hook_struct.is_null() { - let msll_hook_struct = *p_msll_hook_struct; - let current_pos = msll_hook_struct.pt; - - // Store the current position for reference - *LAST_CURSOR_POS.lock().unwrap() = Some(current_pos); - - // We don't need to do anything special for mouse movement anymore - // since we're using SetSystemCursor to set all system cursors - } - } - - // Call the next hook in the chain. - // Pass the HHOOK of our own hook, loaded from CURRENT_HOOK_FOR_CALLBACK. - CallNextHookEx( - CURRENT_HOOK_FOR_CALLBACK.load(Ordering::Relaxed) as _, - n_code, - w_param, - l_param, - ) -} - -// Install mouse hook function -pub fn install_mouse_hook_if_needed() -> bool { - let mut hook_guard = MOUSE_HOOK_HANDLE.lock().unwrap(); - if hook_guard.is_none() { - unsafe { - // GetModuleHandleW(null_mut()) gets the HMODULE for the current process (EXE) - // which is correct for a hook procedure within the same process. - let h_instance = GetModuleHandleW(null_mut()); - if h_instance.is_null() { - eprintln!("Failed to get module handle for setting mouse hook: {}", std::io::Error::last_os_error()); - return false; - } - - let hook = SetWindowsHookExW( - WH_MOUSE_LL, // Hook type: Low-level mouse - Some(low_level_mouse_proc), // Pointer to hook procedure - h_instance, // HMODULE of the DLL containing the hook proc (or current process) - 0 // Thread ID (0 for all threads on current desktop) - ); - - if !hook.is_null() { - // Create wrapped handle - let sync_hook = SyncHHOOK(hook); - *hook_guard = Some(sync_hook); - CURRENT_HOOK_FOR_CALLBACK.store(hook as *mut _, Ordering::SeqCst); - println!("Low-level mouse hook installed successfully."); - return true; - } else { - eprintln!("Failed to install low-level mouse hook: {}", std::io::Error::last_os_error()); - return false; - } - } - } - true // Already installed -} - -// Uninstall mouse hook function -pub fn uninstall_mouse_hook() { - let mut hook_guard = MOUSE_HOOK_HANDLE.lock().unwrap(); - if let Some(hook_wrapper) = hook_guard.take() { // .take() removes it from Option and returns it - unsafe { - // Extract the raw handle using .0 - let hook = hook_wrapper.0; - if UnhookWindowsHookEx(hook) != 0 { - CURRENT_HOOK_FOR_CALLBACK.store(std::ptr::null_mut(), Ordering::SeqCst); - println!("Low-level mouse hook uninstalled successfully."); - } else { - eprintln!("Failed to uninstall low-level mouse hook: {}", std::io::Error::last_os_error()); - // Put it back if unhooking failed, though this is unlikely. - *hook_guard = Some(hook_wrapper); - } - } - } -} diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index b66ba6c..2130cc0 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -2,9 +2,7 @@ mod types; mod cursor; -mod window; -mod hook; mod state; // Re-export the public API -pub use cursor::{hide_system_cursor, update_cursor_image, restore_cursor, handle_mouse_move}; +pub use cursor::{hide_system_cursor, update_cursor_image, restore_cursor}; diff --git a/src/platform/windows/state.rs b/src/platform/windows/state.rs index 412f7d1..aa1e86c 100644 --- a/src/platform/windows/state.rs +++ b/src/platform/windows/state.rs @@ -1,10 +1,9 @@ // Windows platform state management -use std::sync::{Mutex, atomic::{AtomicBool, AtomicPtr}}; -use std::time::Instant; +use std::sync::{Mutex, atomic::AtomicBool}; use winapi::{ - shared::windef::POINT, shared::minwindef::DWORD, + shared::windef::POINT, }; use ::image::RgbaImage; use once_cell::sync::{Lazy, OnceCell}; @@ -25,21 +24,8 @@ pub static LAST_CURSOR_POS: Lazy>> = Lazy::new(|| Mutex::new // Use Lazy>> for WinAPI handles with our thread-safe wrappers pub static CUSTOM_CURSOR: Lazy>> = Lazy::new(|| Mutex::new(None)); -// Legacy variables - deprecated, no longer used with direct system cursor approach -// Kept only for API compatibility -#[deprecated(since = "0.2.0", note = "No longer used with direct system cursor approach")] -pub static CURSOR_WINDOW: Lazy>> = Lazy::new(|| Mutex::new(None)); -#[deprecated(since = "0.2.0", note = "No longer used with direct system cursor approach")] -pub static CURSOR_BITMAP: Lazy>> = Lazy::new(|| Mutex::new(None)); -#[deprecated(since = "0.2.0", note = "No longer used with direct system cursor approach")] -pub static CURSOR_DC: Lazy>> = Lazy::new(|| Mutex::new(None)); - -// Mouse hook related variables - still used for tracking cursor position -pub static MOUSE_HOOK_HANDLE: Lazy>> = Lazy::new(|| Mutex::new(None)); -pub static CURRENT_HOOK_FOR_CALLBACK: AtomicPtr = - AtomicPtr::new(std::ptr::null_mut()); -#[deprecated(since = "0.2.0", note = "No longer used with direct system cursor approach")] -pub static LAST_MOUSE_MOVE_TIME: Lazy>> = Lazy::new(|| Mutex::new(None)); +// Note: Legacy variables for window-based cursor approach and mouse hook tracking +// have been removed as part of code cleanup // Use OnceCell for INIT to avoid `static mut` pub static INIT: OnceCell<()> = OnceCell::new(); diff --git a/src/platform/windows/types.rs b/src/platform/windows/types.rs index 537c3e6..ca658ef 100644 --- a/src/platform/windows/types.rs +++ b/src/platform/windows/types.rs @@ -1,44 +1,20 @@ // Windows platform types and constants -use std::time::Duration; + use winapi::{ - shared::windef::{HWND, HCURSOR, HBITMAP, HDC, HHOOK}, + shared::windef::HCURSOR, shared::minwindef::DWORD, }; // --- Thread-Safe Handle Wrappers --- // These wrappers make raw Windows handles safe to share between threads -// Wrapper for HWND (window handle) -#[derive(Copy, Clone, Debug)] -pub struct SyncHWND(pub HWND); -unsafe impl Send for SyncHWND {} -unsafe impl Sync for SyncHWND {} - // Wrapper for HCURSOR (cursor handle) #[derive(Copy, Clone, Debug)] pub struct SyncHCURSOR(pub HCURSOR); unsafe impl Send for SyncHCURSOR {} unsafe impl Sync for SyncHCURSOR {} -// Wrapper for HBITMAP (bitmap handle) -#[derive(Copy, Clone, Debug)] -pub struct SyncHBITMAP(pub HBITMAP); -unsafe impl Send for SyncHBITMAP {} -unsafe impl Sync for SyncHBITMAP {} - -// Wrapper for HDC (device context handle) -#[derive(Copy, Clone, Debug)] -pub struct SyncHDC(pub HDC); -unsafe impl Send for SyncHDC {} -unsafe impl Sync for SyncHDC {} - -// Wrapper for HHOOK (hook handle) -#[derive(Copy, Clone, Debug)] -pub struct SyncHHOOK(pub HHOOK); -unsafe impl Send for SyncHHOOK {} -unsafe impl Sync for SyncHHOOK {} - // --- Constants --- // Constants for SystemParametersInfo @@ -46,11 +22,7 @@ pub const SPI_SETCURSORS_A: u32 = 0x0057; pub const SPIF_UPDATEINIFILE: u32 = 0x01; pub const SPIF_SENDCHANGE: u32 = 0x02; -// Window class name for our cursor window -pub const CURSOR_WINDOW_CLASS: &str = "CursorRushOverlay"; - -// Mouse update interval (throttle to ~120Hz) -pub const MOUSE_UPDATE_INTERVAL: Duration = Duration::from_millis(8); +// Note: Constants for window-based cursor approach have been removed as part of code cleanup // Define all system cursor IDs we'll replace pub static SYSTEM_CURSORS: [DWORD; 11] = [ diff --git a/src/platform/windows/window.rs b/src/platform/windows/window.rs deleted file mode 100644 index cc51f6f..0000000 --- a/src/platform/windows/window.rs +++ /dev/null @@ -1,13 +0,0 @@ -// Windows platform window management -// This file is kept for compatibility but no longer creates a transparent window -// since we're now using SetSystemCursor to set the Windows cursor directly - -use super::types::*; - -// Create a cursor window - now a stub function that returns None -// since we no longer need a transparent window for the cursor -pub unsafe fn create_cursor_window() -> Option { - // Return None to indicate no window was created - // This is intentional as we're now using SetSystemCursor instead - None -}