From e922d910ca7ec9263a2107ab1ea424d811d1e879 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 26 Nov 2026 20:21:25 -0800 Subject: [PATCH] :bug: Mark a borderless window full screen to the Windows shell On a Windows host with the taskbar set to hide itself, a borderless window let the taskbar come up over it when the pointer met the screen's bottom edge. The window is SDL's desktop full screen, a popup over its monitor's rect, but SDL 3.4.18 never tells the shell a window is full screen: it makes the taskbar list only for a window's progress. The window now marks itself full screen through the shell's documented call, ITaskbarList2::MarkFullscreenWindow, once SDL has made it so as it shows, and takes the mark away before it hides at the display's end. The taskbar list is made and initialised as its contract asks (CoCreateInstance of CLSID_TaskbarList on the UI thread, which made the window, with COM initialised there, then HrInit) and is held while the mark is. A step Windows refuses is logged, and the window opens as before. Linux and macOS are unchanged. What changes for the host: a borderless window on Windows keeps an auto-hide taskbar down while it is active. features.md gains the entry, with its manual check on a Windows host. --- Cargo.lock | 1 + crates/reims-vgpu-window/Cargo.toml | 9 + crates/reims-vgpu-window/features.md | 7 + .../src/native/sdl_thread/sdl.rs | 21 +- .../reims-vgpu-window/src/native/windows.rs | 226 +++++++++++++++++- 5 files changed, 260 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d928eb94..106e75ec2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,7 @@ dependencies = [ "objc2-quartz-core", "reims-vgpu-vulkan", "sdl3-sys", + "windows-sys", ] [[package]] diff --git a/crates/reims-vgpu-window/Cargo.toml b/crates/reims-vgpu-window/Cargo.toml index 4fb544cfa..2c5cd2d2f 100644 --- a/crates/reims-vgpu-window/Cargo.toml +++ b/crates/reims-vgpu-window/Cargo.toml @@ -25,6 +25,15 @@ sdl3-sys.workspace = true # libwayland-client's calls, looked up in the library SDL opened (`native/linux/feedback.rs`). libc.workspace = true +[target.'cfg(target_os = "windows")'.dependencies] +# The shell's full-screen mark of a borderless window (`native/windows.rs`): COM, and the taskbar +# list's class ID. +windows-sys = { workspace = true, features = [ + "Win32_Foundation", + "Win32_System_Com", + "Win32_UI_Shell", +] } + [features] default = ["macos-sdl"] # SDL's library left unbuilt and unlinked: for a check of the Linux or Windows target on another diff --git a/crates/reims-vgpu-window/features.md b/crates/reims-vgpu-window/features.md index 03e0e0001..b31a90535 100644 --- a/crates/reims-vgpu-window/features.md +++ b/crates/reims-vgpu-window/features.md @@ -479,6 +479,13 @@ runtime's own device. The switches above apply, and `resolution=` too. screen rounded to whole points, so its pixels can pass the screen's by one (3841 × 2161 on a 3840 × 2160 screen at 2.1); the reims-graphics guest is offered the screen's own 3840 × 2160 (above). `manual` +- On a Windows host a borderless window is SDL's full screen, and it tells + the shell so (`ITaskbarList2::MarkFullscreenWindow`) from when it shows + until it hides at the display's end: a taskbar set to hide itself stays + down while the window is active, with the pointer at the screen's bottom + edge too. Where Windows refuses the mark, that is logged and the window + opens as before. `manual` (a Windows host with the taskbar set to hide + itself) - The host's screens are SDL's: the primary first, then top to bottom and left to right, each at its scale (fractional ones included) and the refresh rate it is set to (its current mode's, as above); a change of diff --git a/crates/reims-vgpu-window/src/native/sdl_thread/sdl.rs b/crates/reims-vgpu-window/src/native/sdl_thread/sdl.rs index e691ef573..7a2778b96 100644 --- a/crates/reims-vgpu-window/src/native/sdl_thread/sdl.rs +++ b/crates/reims-vgpu-window/src/native/sdl_thread/sdl.rs @@ -20,7 +20,10 @@ //! pointer stay in SDL's sizes; the question a close asks in the window is laid out at the told //! scale (`super::question`). //! -//! **Borderless**: through the compositor's own full screen on its screen, over its panels. +//! **Borderless**: through the compositor's own full screen on its screen, over its panels. On +//! Windows the shell is also told the window is full screen, so a taskbar set to hide itself stays +//! down over it (`super::super::windows`, "Full screen to the shell"), until the window hides at +//! the display's end ([`close()`]). //! //! **The surface.** The window is made for Vulkan: its render thread makes the window's Vulkan //! surface of SDL's window (`super::render`), so SDL's window goes only once that thread has let @@ -80,6 +83,9 @@ pub(crate) struct State { /// The question a close asks in the window, while it is open ([`ask`]). #[cfg(target_os = "linux")] question: Option, + /// The shell's mark of the window as full screen, while SDL has it so. + #[cfg(target_os = "windows")] + full_screen: Option, } /// A window's input: SDL's events, queued until the shell sends them (`super::input`). @@ -192,6 +198,10 @@ pub(crate) fn open(window: &Arc, borderless: bool, _taken: &[Frame]) -> } SDL_ShowWindow(sdl); } + // SAFETY: `sdl` is the window just made, on this thread; a borderless one is full screen once + // shown (SDL applies the full screen asked of a hidden window as it shows it). + #[cfg(target_os = "windows")] + let full_screen = unsafe { super::super::windows::FullScreenMark::of(sdl, window.title()) }; window.surface().set_target(Target(sdl)); window.surface().set_visible(true); log::debug!( @@ -217,6 +227,8 @@ pub(crate) fn open(window: &Arc, borderless: bool, _taken: &[Frame]) -> display, #[cfg(target_os = "linux")] question: None, + #[cfg(target_os = "windows")] + full_screen, }, }) } @@ -716,8 +728,9 @@ fn answer_of(shown: bool, chosen: c_int) -> Answer { } } -/// The display ended: the window hides, and a question open in it ends as cancelled. SDL's -/// window goes once the render thread has let go (`crate::sdl::shell::released`), never before. +/// The display ended: the window hides, and a question open in it ends as cancelled; on Windows +/// the shell's full-screen mark goes first. SDL's window goes once the render thread has let go +/// (`crate::sdl::shell::released`), never before. pub(crate) fn close(shell: &mut Shell) -> bool { #[cfg(target_os = "linux")] if let Some(question) = shell.os.question.take() { @@ -725,6 +738,8 @@ pub(crate) fn close(shell: &mut Shell) -> bool { let ids = question.ids; super::ui::post(move || shell::answered(&ids, Answer::Cancel)); } + #[cfg(target_os = "windows")] + drop(shell.os.full_screen.take()); // SAFETY: the window is SDL's, on this thread. unsafe { SDL_HideWindow(shell.sdl) }; false diff --git a/crates/reims-vgpu-window/src/native/windows.rs b/crates/reims-vgpu-window/src/native/windows.rs index 989a0655e..9fefb5e9e 100644 --- a/crates/reims-vgpu-window/src/native/windows.rs +++ b/crates/reims-vgpu-window/src/native/windows.rs @@ -31,8 +31,31 @@ //! E0 5B and E0 5C otherwise) as `SDL_SCANCODE_LGUI` and `SDL_SCANCODE_RGUI`, the guest's left //! and right ⌘ (read from SDL 3.4.18's code, `WIN_KeyboardHookProc` and //! `scancodes_windows.h`). +//! - **Full screen to the shell.** A borderless window is SDL's desktop full screen: a popup +//! window over its monitor's whole rect (read from SDL 3.4.18's code, +//! `WIN_SetWindowFullscreen`). SDL never tells the shell a window is full screen (read from its +//! code: it makes the taskbar list only for a window's progress), and a taskbar set to hide +//! itself then comes up over the window when the pointer meets the screen's bottom edge +//! (reported on a Windows 11 host). So the window marks it full screen +//! ([`FullScreenMark`], `ITaskbarList2::MarkFullscreenWindow`) from the moment SDL has made it +//! so until it hides at the display's end: Microsoft's documentation of the call says the +//! taskbar then goes to the bottom of the z-order while the window is active, and that taking +//! the mark away leaves the window to the shell's own detection. -use sdl3_sys::everything::SDL_Window; +use std::ffi::c_void; +use std::ptr::{NonNull, null, null_mut}; + +use sdl3_sys::everything::{ + SDL_GetPointerProperty, SDL_GetWindowFlags, SDL_GetWindowProperties, + SDL_PROP_WINDOW_WIN32_HWND_POINTER, SDL_WINDOW_FULLSCREEN, SDL_Window, +}; +use windows_sys::Win32::Foundation::{FALSE, HWND, RPC_E_CHANGED_MODE, S_FALSE, S_OK, TRUE}; +use windows_sys::Win32::System::Com::{ + CLSCTX_INPROC_SERVER, COINIT_APARTMENTTHREADED, CoCreateInstance, CoInitializeEx, + CoUninitialize, +}; +use windows_sys::Win32::UI::Shell::TaskbarList; +use windows_sys::core::{BOOL, GUID, HRESULT}; use crate::capture::HostOs; @@ -47,3 +70,204 @@ pub(super) const HOST_OS: HostOs = HostOs::Windows; pub(super) unsafe fn compositor_device(_window: *mut SDL_Window) -> Option<(u32, u32)> { None } + +/// `ITaskbarList2`'s interface ID, as the Windows SDK's `shobjidl.h` defines it (windows-sys has +/// the taskbar list's class ID, `CLSID_TaskbarList`, but no COM interfaces). +const IID_ITASKBARLIST2: GUID = GUID::from_u128(0x602d4995_b13a_429b_a66e_1935e44f4317); + +/// An `ITaskbarList2` object: a pointer to its methods. +#[repr(C)] +struct TaskbarList2 { + /// Its methods. + methods: *const TaskbarList2Methods, +} + +/// `ITaskbarList2`'s methods, in `shobjidl.h`'s order: `IUnknown`'s, `ITaskbarList`'s, then its +/// own. +#[repr(C)] +struct TaskbarList2Methods { + /// `QueryInterface` and `AddRef`, which the mark does not call. + _unknown: [*const c_void; 2], + /// `Release`. + release: unsafe extern "system" fn(*mut TaskbarList2) -> u32, + /// `HrInit`, which comes before any other of the list's methods. + hr_init: unsafe extern "system" fn(*mut TaskbarList2) -> HRESULT, + /// `AddTab`, `DeleteTab`, `ActivateTab` and `SetActiveAlt`, which the mark does not call. + _tabs: [*const c_void; 4], + /// `MarkFullscreenWindow`. + mark_fullscreen_window: unsafe extern "system" fn(*mut TaskbarList2, HWND, BOOL) -> HRESULT, +} + +/// The shell's mark of a full-screen window as full screen (the module doc): made marking it, +/// dropped taking the mark away, on the UI thread, which made the window. +/// +/// It follows the call's documented contract: the taskbar list (`CLSID_TaskbarList`) is made with +/// `CoCreateInstance` on a thread where COM is initialised, and `HrInit` is called before any of +/// its other methods. COM is SDL's on this thread already, single-threaded (its video start +/// initialises it, `WIN_VideoInit`, read from SDL 3.4.18's code); the mark initialises it again +/// for itself, which COM counts, and lets that go after the list. The list is held while the mark +/// is. +pub(super) struct FullScreenMark { + /// The taskbar list. + list: NonNull, + /// The window marked. + hwnd: HWND, + /// Whether the mark's `CoInitializeEx` is matched by a `CoUninitialize`: not where COM was + /// the thread's already, multi-threaded (`RPC_E_CHANGED_MODE`). + uninitialize: bool, +} + +impl FullScreenMark { + /// Marks SDL's window `window` full screen to the shell, where SDL has it full screen; `None` + /// where it is not, or where Windows refuses a step, which is logged for the window titled + /// `title`. + /// + /// # Safety + /// + /// On the UI thread, which made `window`, a window SDL has open. + pub(super) unsafe fn of(window: *mut SDL_Window, title: &str) -> Option { + // SAFETY: SDL's window, on its thread (the caller's promise); the handle lives while + // SDL's window does. + let (flags, hwnd) = unsafe { + ( + SDL_GetWindowFlags(window), + SDL_GetPointerProperty( + SDL_GetWindowProperties(window), + SDL_PROP_WINDOW_WIN32_HWND_POINTER, + null_mut(), + ), + ) + }; + if (flags & SDL_WINDOW_FULLSCREEN).0 == 0 { + return None; + } + if hwnd.is_null() { + unmarked(title, "SDL names no window handle", None); + return None; + } + // SAFETY: SDL's window's handle, on the thread that made the window. + match unsafe { Self::mark(hwnd) } { + Ok(mark) => { + log::info!( + target: "reims_vgpu_window", + "window {title:?}: marked full screen to the shell, so a taskbar set to hide \ + itself stays down over it" + ); + Some(mark) + } + Err((step, refused)) => { + unmarked(title, step, Some(refused)); + None + } + } + } + + /// Marks the window `hwnd` full screen to the shell; else the step Windows refused, and the + /// `HRESULT` it refused it with. + /// + /// # Safety + /// + /// On the thread that made `hwnd`, a live window. + unsafe fn mark(hwnd: HWND) -> Result { + // SAFETY: COM's start on this thread, matched by `let_go` where it is this call's. + let started = unsafe { CoInitializeEx(null(), COINIT_APARTMENTTHREADED as u32) }; + let uninitialize = match started { + S_OK | S_FALSE => true, + RPC_E_CHANGED_MODE => false, + refused => return Err(("CoInitializeEx", refused)), + }; + let mut object = null_mut(); + // SAFETY: COM is initialised on this thread; the IDs are the class's and the interface's. + let made = unsafe { + CoCreateInstance( + &TaskbarList, + null_mut(), + CLSCTX_INPROC_SERVER, + &IID_ITASKBARLIST2, + &mut object, + ) + }; + let Some(list) = NonNull::new(object.cast::()).filter(|_| made >= 0) else { + // SAFETY: no list was made; COM's start is let go where it was this call's. + unsafe { let_go(None, uninitialize) }; + return Err(("CoCreateInstance", made)); + }; + // SAFETY: the list just made, on this thread: `HrInit` first, as its contract asks, then + // the mark, of the caller's window. + let refused = unsafe { + let methods = &*list.as_ref().methods; + let init = (methods.hr_init)(list.as_ptr()); + if init < 0 { + Some(("HrInit", init)) + } else { + let marked = (methods.mark_fullscreen_window)(list.as_ptr(), hwnd, TRUE); + (marked < 0).then_some(("MarkFullscreenWindow", marked)) + } + }; + if let Some(refused) = refused { + // SAFETY: the list is this call's, and marks nothing; COM's start as above. + unsafe { let_go(Some(list), uninitialize) }; + return Err(refused); + } + Ok(Self { + list, + hwnd, + uninitialize, + }) + } +} + +impl Drop for FullScreenMark { + /// Takes the mark away, then lets the list and COM go, in that order. + fn drop(&mut self) { + // SAFETY: the mark's list, initialised, on the UI thread, which made it and the window; + // the window has not gone yet (`crate::sdl::shell` destroys SDL's window after its OS + // side lets go). + let unmarked = unsafe { + let methods = &*self.list.as_ref().methods; + (methods.mark_fullscreen_window)(self.list.as_ptr(), self.hwnd, FALSE) + }; + if unmarked < 0 { + log::debug!( + target: "reims_vgpu_window", + "the shell's full-screen mark was not taken away: {}", + hresult(unmarked) + ); + } + // SAFETY: the mark's list, released once, here; COM's start as in `mark`. + unsafe { let_go(Some(self.list), self.uninitialize) }; + } +} + +/// Releases `list`, then lets COM go on this thread where `uninitialize`. +/// +/// # Safety +/// +/// `list` is a live list of this thread's, released no more after this; `uninitialize` only for +/// a `CoInitializeEx` of this thread's that nothing else matches. +unsafe fn let_go(list: Option>, uninitialize: bool) { + if let Some(list) = list { + // SAFETY: the caller's promise. + unsafe { ((*list.as_ref().methods).release)(list.as_ptr()) }; + } + if uninitialize { + // SAFETY: the caller's promise. + unsafe { CoUninitialize() }; + } +} + +/// Logs that the window titled `title` is not marked full screen: at `step`, which Windows +/// refused with `refused` where it returned an `HRESULT`. +fn unmarked(title: &str, step: &str, refused: Option) { + let refused = refused.map_or_else(String::new, |r| format!(": {}", hresult(r))); + log::warn!( + target: "reims_vgpu_window", kind:% = "no full-screen mark"; + "window {title:?}: not marked full screen to the shell ({step}{refused}), so a taskbar \ + set to hide itself can come up over it" + ); +} + +/// An `HRESULT` as Windows prints one: `0x80004005`. +fn hresult(result: HRESULT) -> String { + format!("{:#010x}", result.cast_unsigned()) +}