Skip to content

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

feat: emit display settings platform events

Lars Berger9e619b1f813c
diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rs
index 8cefc46b..aea3640c 100644
--- a/packages/wm/src/common/platform/event_listener.rs
+++ b/packages/wm/src/common/platform/event_listener.rs
@@ -40,7 +40,6 @@ impl EventListener {
config_changes_rx: UnboundedReceiver<UserConfig>,
) -> Result<Self> {
let (event_tx, event_rx) = mpsc::unbounded_channel();
- // let event_window = EventWindow::new(Arc::new(event_tx));
let event_window = EventWindow::new(event_tx);
Ok(Self {
diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs
index 3a8f87bc..a5f28306 100644
--- a/packages/wm/src/common/platform/event_window.rs
+++ b/packages/wm/src/common/platform/event_window.rs
@@ -6,18 +6,26 @@ use std::{
use anyhow::{bail, Result};
use tokio::sync::{mpsc::UnboundedSender, oneshot};
use tracing::warn;
-use windows::Win32::{
- Foundation::HWND,
- UI::{
- Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK},
- WindowsAndMessaging::{
- DispatchMessageW, GetMessageW, TranslateMessage,
- EVENT_OBJECT_CLOAKED, EVENT_OBJECT_DESTROY, EVENT_OBJECT_HIDE,
- EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_NAMECHANGE,
- EVENT_OBJECT_SHOW, EVENT_OBJECT_UNCLOAKED, EVENT_SYSTEM_FOREGROUND,
- EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZESTART,
- EVENT_SYSTEM_MOVESIZEEND, MSG, OBJID_WINDOW, WINEVENT_OUTOFCONTEXT,
- WINEVENT_SKIPOWNPROCESS,
+use windows::{
+ core::w,
+ Win32::{
+ Foundation::{HWND, LPARAM, LRESULT, WPARAM},
+ UI::{
+ Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK},
+ WindowsAndMessaging::{
+ CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW,
+ PostQuitMessage, RegisterClassW, TranslateMessage, CS_HREDRAW,
+ CS_VREDRAW, CW_USEDEFAULT, DBT_DEVNODES_CHANGED,
+ EVENT_OBJECT_CLOAKED, EVENT_OBJECT_DESTROY, EVENT_OBJECT_HIDE,
+ EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_NAMECHANGE,
+ EVENT_OBJECT_SHOW, EVENT_OBJECT_UNCLOAKED,
+ EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_MINIMIZEEND,
+ EVENT_SYSTEM_MINIMIZESTART, EVENT_SYSTEM_MOVESIZEEND, MSG,
+ OBJID_WINDOW, SPI_ICONVERTICALSPACING, SPI_SETWORKAREA,
+ WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS, WM_DESTROY,
+ WM_DEVICECHANGE, WM_DISPLAYCHANGE, WM_SETTINGCHANGE, WNDCLASSW,
+ WS_OVERLAPPEDWINDOW,
+ },
},
},
};
@@ -25,10 +33,13 @@ use windows::Win32::{
use super::{NativeWindow, PlatformEvent};
thread_local! {
- // static HOOK_EVENT_TX: OnceCell<Arc<UnboundedSender<PlatformEvent>>> = OnceCell::new();
static HOOK_EVENT_TX: OnceCell<UnboundedSender<PlatformEvent>> = OnceCell::new();
}
+/// Callback passed to `SetWinEventHook` to handle window events.
+///
+/// This function is called on selected window events, and forwards them
+/// through an MPSC channel for the WM to process.
extern "system" fn event_hook_proc(
_hook: HWINEVENTHOOK,
event: u32,
@@ -79,12 +90,65 @@ extern "system" fn event_hook_proc(
};
if let Err(err) = event_tx.send(platform_event) {
- warn!("Failed to send platform event '{}'", err);
+ warn!("Failed to send platform event '{}'.", err);
}
}
});
}
+/// Window procedure for the event window.
+///
+/// This function handles messages for the event window, and forwards
+/// display change events through an MPSC channel for the WM to process.
+pub extern "system" fn event_window_proc(
+ hwnd: HWND,
+ msg: u32,
+ wparam: WPARAM,
+ lparam: LPARAM,
+) -> LRESULT {
+ HOOK_EVENT_TX.with(|event_tx| {
+ if let Some(event_tx) = event_tx.get() {
+ return match msg {
+ WM_DISPLAYCHANGE | WM_SETTINGCHANGE | WM_DEVICECHANGE => {
+ handle_display_change_msg(msg, wparam, event_tx)
+ }
+ WM_DESTROY => {
+ unsafe { PostQuitMessage(0) };
+ LRESULT(0)
+ }
+ _ => unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) },
+ };
+ }
+
+ LRESULT(0)
+ })
+}
+
+/// Handle display change messages and emit the corresponding event.
+fn handle_display_change_msg(
+ msg: u32,
+ wparam: WPARAM,
+ event_tx: &UnboundedSender<PlatformEvent>,
+) -> LRESULT {
+ let should_emit_event = match msg {
+ WM_SETTINGCHANGE => {
+ wparam.0 as u32 == SPI_SETWORKAREA.0
+ || wparam.0 as u32 == SPI_ICONVERTICALSPACING.0
+ }
+ WM_DEVICECHANGE => wparam.0 as u32 == DBT_DEVNODES_CHANGED,
+ _ => true,
+ };
+
+ if should_emit_event {
+ let event = PlatformEvent::DisplaySettingsChanged;
+ if let Err(err) = event_tx.send(event) {
+ warn!("Failed to send platform event '{}'.", err);
+ }
+ }
+
+ LRESULT(0)
+}
+
#[derive(Debug)]
pub struct EventWindow {
abort_tx: Option<oneshot::Sender<()>>,
@@ -119,6 +183,7 @@ impl EventWindow {
}
}
+ /// Create several window event hooks via `SetWinEventHook`.
fn hook_win_events() -> Result<Vec<HWINEVENTHOOK>> {
let event_ranges = [
(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE),
@@ -142,6 +207,7 @@ impl EventWindow {
})
}
+ /// Create a window hook for the specified event range.
unsafe fn hook_win_event(
event_min: u32,
event_max: u32,
@@ -163,9 +229,34 @@ impl EventWindow {
Ok(hook_handle)
}
+ /// Create the event window and its message loop.
unsafe fn create_message_loop(
mut abort_rx: oneshot::Receiver<()>,
) -> Result<()> {
+ let wnd_class = WNDCLASSW {
+ lpszClassName: w!("EventWindow"),
+ style: CS_HREDRAW | CS_VREDRAW,
+ lpfnWndProc: Some(event_window_proc),
+ ..Default::default()
+ };
+
+ RegisterClassW(&wnd_class);
+
+ let hwnd = CreateWindowExW(
+ Default::default(),
+ w!("EventWindow"),
+ w!("EventWindow"),
+ WS_OVERLAPPEDWINDOW,
+ CW_USEDEFAULT,
+ CW_USEDEFAULT,
+ CW_USEDEFAULT,
+ CW_USEDEFAULT,
+ None,
+ None,
+ wnd_class.hInstance,
+ None,
+ );
+
let mut msg = MSG::default();
loop {
@@ -185,9 +276,8 @@ impl EventWindow {
Ok(())
}
+ /// Destroy the event window and stop the message loop.
pub fn destroy(&mut self) {
- // Send a signal to the spawned thread to stop the message loop.
- // if self.abort_tx.send(()).is_err() {
if let Some(abort_tx) = self.abort_tx.take() {
if abort_tx.send(()).is_err() {
warn!("Failed to send abort signal to the event window thread.");