Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: wip replacement of LL mouse hook with WM_INPUT raw input

Lars Bergera3c99fd3fc1e
diff --git a/Cargo.toml b/Cargo.toml
index d5be7147..bbc0541b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,6 +9,7 @@ regex = "1"
tokio = { version = "1", features = ["full"] }
windows = { version = "0.52", features = [
"implement",
+ "Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
"Win32_Graphics_Dwm",
"Win32_Graphics_Gdi",
diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs
index 2bb63ddf..5f6369e3 100644
--- a/packages/wm/src/common/platform/event_window.rs
+++ b/packages/wm/src/common/platform/event_window.rs
@@ -1,23 +1,42 @@
use std::{
- sync::{Arc, OnceLock},
+ ops::BitAnd,
+ sync::{
+ atomic::{AtomicBool, AtomicU32, Ordering},
+ Arc, OnceLock,
+ },
thread::{self, JoinHandle},
};
use tokio::sync::mpsc;
use tracing::{error, info, warn};
use windows::Win32::{
- Foundation::{HWND, LPARAM, LRESULT, WPARAM},
- UI::WindowsAndMessaging::{
- DefWindowProcW, DestroyWindow, PostQuitMessage, DBT_DEVNODES_CHANGED,
- SPI_ICONVERTICALSPACING, SPI_SETWORKAREA, WM_DESTROY, WM_DEVICECHANGE,
- WM_DISPLAYCHANGE, WM_POWERBROADCAST, WM_SETTINGCHANGE,
+ Devices::HumanInterfaceDevice::{
+ HID_USAGE_GENERIC_MOUSE, HID_USAGE_PAGE_GENERIC, MOUSE_MOVE_ABSOLUTE,
+ MOUSE_MOVE_RELATIVE,
+ },
+ Foundation::{HWND, LPARAM, LRESULT, POINT, WPARAM},
+ UI::{
+ Input::{
+ GetRawInputData, RegisterRawInputDevices, HRAWINPUT, RAWINPUT,
+ RAWINPUTDEVICE, RAWINPUTHEADER, RIDEV_INPUTSINK,
+ RID_DEVICE_INFO_TYPE, RID_INPUT, RIM_TYPEMOUSE,
+ },
+ WindowsAndMessaging::{
+ DefWindowProcW, DestroyWindow, GetCursorPos, PostQuitMessage,
+ DBT_DEVNODES_CHANGED, RI_MOUSE_LEFT_BUTTON_DOWN,
+ RI_MOUSE_LEFT_BUTTON_UP, RI_MOUSE_RIGHT_BUTTON_DOWN,
+ RI_MOUSE_RIGHT_BUTTON_UP, SPI_ICONVERTICALSPACING, SPI_SETWORKAREA,
+ WM_DESTROY, WM_DEVICECHANGE, WM_DISPLAYCHANGE, WM_INPUT,
+ WM_POWERBROADCAST, WM_SETTINGCHANGE,
+ },
},
};
-use crate::user_config::KeybindingConfig;
+use crate::{common::Point, user_config::KeybindingConfig};
use super::{
- KeyboardHook, MouseHook, Platform, PlatformEvent, WinEventHook,
+ KeyboardHook, MouseHook, MouseMoveEvent, Platform, PlatformEvent,
+ WinEventHook,
};
/// Global instance of sender for platform events.
@@ -26,6 +45,26 @@ use super::{
static PLATFORM_EVENT_TX: OnceLock<mpsc::UnboundedSender<PlatformEvent>> =
OnceLock::new();
+/// Whether mouse hook is currently enabled.
+///
+/// For use with window procedure.
+static IS_MOUSE_HOOK_ENABLED: AtomicBool = AtomicBool::new(false);
+
+/// Whether left-click is currently pressed.
+///
+/// For use with window procedure.
+static IS_L_MOUSE_DOWN: AtomicBool = AtomicBool::new(false);
+
+/// Whether right-click is currently pressed.
+///
+/// For use with window procedure.
+static IS_R_MOUSE_DOWN: AtomicBool = AtomicBool::new(false);
+
+/// Timestamp of the last event emission.
+///
+/// For use with window procedure.
+static LAST_MOUSE_EVENT_TIME: AtomicU32 = AtomicU32::new(0);
+
#[derive(Debug)]
pub struct EventWindow {
keyboard_hook: Arc<KeyboardHook>,
@@ -38,7 +77,7 @@ impl EventWindow {
/// Creates an instance of `EventWindow`. Spawns a hidden window and
/// emits platform events.
///
- /// Uses global state (i.e. `PLATFORM_EVENT_TX`) and should thus only
+ /// Uses global state (e.g. `PLATFORM_EVENT_TX`) and should thus only
/// ever be instantiated once in the application's lifetime.
pub fn new(
event_tx: mpsc::UnboundedSender<PlatformEvent>,
@@ -50,7 +89,6 @@ impl EventWindow {
let win_event_hook = WinEventHook::new(event_tx.clone())?;
let keyboard_hook_clone = keyboard_hook.clone();
- let mouse_hook_clone = mouse_hook.clone();
let win_event_hook_clone = win_event_hook.clone();
let window_thread = thread::spawn(move || {
@@ -59,18 +97,31 @@ impl EventWindow {
anyhow::anyhow!("Platform event sender already set.")
})?;
+ IS_MOUSE_HOOK_ENABLED.store(enable_mouse_events, Ordering::Relaxed);
+
// Start hooks for listening to platform events.
keyboard_hook_clone.start()?;
win_event_hook_clone.start()?;
- if enable_mouse_events {
- mouse_hook_clone.start()?;
- }
-
// Create a hidden window with a message loop on the current thread.
let handle =
Platform::create_message_window(Some(event_window_proc))?;
+ let rid = RAWINPUTDEVICE {
+ usUsagePage: HID_USAGE_PAGE_GENERIC,
+ usUsage: HID_USAGE_GENERIC_MOUSE,
+ dwFlags: RIDEV_INPUTSINK,
+ hwndTarget: HWND(handle),
+ };
+
+ // Register our window to receive mouse events.
+ unsafe {
+ RegisterRawInputDevices(
+ &[rid],
+ std::mem::size_of::<RAWINPUTDEVICE>() as u32,
+ )
+ }?;
+
Platform::run_message_loop();
unsafe { DestroyWindow(HWND(handle)) }?;
@@ -141,14 +192,8 @@ pub extern "system" fn event_window_proc(
WM_DISPLAYCHANGE | WM_SETTINGCHANGE | WM_DEVICECHANGE => {
handle_display_change_msg(message, wparam, event_tx)
}
- WM_POWERBROADCAST => {
- event_tx.send(PlatformEvent::PowerModeChanged).unwrap();
- LRESULT(0)
- }
- WM_DESTROY => {
- unsafe { PostQuitMessage(0) };
- LRESULT(0)
- }
+ // WM_INPUT if IS_MOUSE_HOOK_ENABLED.load(Ordering::Relaxed) => {
+ WM_INPUT => handle_input_msg(wparam, lparam, event_tx),
_ => unsafe { DefWindowProcW(handle, message, wparam, lparam) },
};
}
@@ -181,3 +226,86 @@ fn handle_display_change_msg(
LRESULT(0)
}
+
+/// Handles raw input messages for mouse events and emits the corresponding
+/// platform event through an MPSC channel.
+fn handle_input_msg(
+ _wparam: WPARAM,
+ lparam: LPARAM,
+ event_tx: &mpsc::UnboundedSender<PlatformEvent>,
+) -> LRESULT {
+ // let mut raw_input: RAWINPUT = unsafe { std::mem::zeroed() };
+ let mut raw_input: RAWINPUT = RAWINPUT::default();
+ let mut raw_input_size = std::mem::size_of::<RAWINPUT>() as u32;
+
+ let res_size = unsafe {
+ GetRawInputData(
+ HRAWINPUT(lparam.0),
+ RID_INPUT,
+ Some(&mut raw_input as *mut _ as _),
+ &mut raw_input_size,
+ std::mem::size_of::<RAWINPUTHEADER>() as u32,
+ )
+ };
+
+ // Ignore if data is invalid or not a mouse event.
+ if res_size == 0
+ || raw_input_size == u32::MAX
+ || raw_input.header.dwType != RIM_TYPEMOUSE.0
+ {
+ return LRESULT(0);
+ }
+
+ let mouse_input = unsafe { raw_input.data.mouse };
+ let state_flags = mouse_input.usFlags;
+ let button_flags =
+ unsafe { mouse_input.Anonymous.Anonymous.usButtonFlags };
+
+ if has_flags(button_flags, RI_MOUSE_LEFT_BUTTON_DOWN) {
+ IS_L_MOUSE_DOWN.store(true, Ordering::Relaxed);
+ }
+
+ if has_flags(button_flags, RI_MOUSE_LEFT_BUTTON_UP) {
+ IS_L_MOUSE_DOWN.store(false, Ordering::Relaxed);
+ }
+
+ if has_flags(button_flags, RI_MOUSE_RIGHT_BUTTON_DOWN) {
+ IS_R_MOUSE_DOWN.store(true, Ordering::Relaxed);
+ }
+
+ if has_flags(button_flags, RI_MOUSE_RIGHT_BUTTON_UP) {
+ IS_R_MOUSE_DOWN.store(false, Ordering::Relaxed);
+ }
+
+ if has_flags(state_flags, MOUSE_MOVE_RELATIVE)
+ || has_flags(state_flags, MOUSE_MOVE_ABSOLUTE)
+ {
+ let is_mouse_down = IS_L_MOUSE_DOWN.load(Ordering::Relaxed)
+ || IS_R_MOUSE_DOWN.load(Ordering::Relaxed);
+
+ let mut point = POINT { x: 0, y: 0 };
+ unsafe { GetCursorPos(&mut point) };
+
+ let event = MouseMoveEvent {
+ point: Point {
+ x: point.x,
+ y: point.y,
+ },
+ is_mouse_down,
+ };
+
+ if let Err(err) = event_tx.send(PlatformEvent::MouseMove(event)) {
+ warn!("Failed to send platform event '{}'.", err);
+ }
+
+ // LAST_MOUSE_EVENT_TIME.store(mouse_event.time, Ordering::Relaxed);
+ }
+
+ LRESULT(0)
+}
+
+/// Checks whether `short` contains all the bits of `mask`.
+#[inline]
+fn has_flags(short: u16, mask: u32) -> bool {
+ short & mask as u16 == mask as u16
+}