Window: keep Alt and F10 from freezing the title in menu mode
Alt or F10 sends WM_SYSCOMMAND SC_KEYMENU, and the default handling runs a modal menu loop on the thread that owns the window, which is the title's. The title stops drawing and reading input until a key ends the loop, with nothing on screen to say so, and Grand Theft Auto VI aborts itself about half a minute later (the deliberate write to 0 at +0x23cf683). The window procedure now drops SC_KEYMENU, next to its SC_CLOSE handling. The mouse can still open the window menu. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d2a4bae1f4
commit
ff8694a69a
1 file changed
+69
@@ -174,6 +174,15 @@ extern "system" fn window_proc(
|
||||
end_title("window closed", Box::new(|| {}));
|
||||
return 0;
|
||||
}
|
||||
// Alt or F10 puts a window into menu mode: the system runs a modal menu
|
||||
// loop on the thread that owns the window, which is the title's. The
|
||||
// title then stops (no frames, no input) until a key ends the loop, and
|
||||
// nothing shows it, since the window has no menu bar; Grand Theft Auto
|
||||
// VI aborts itself about half a minute later. A console has no window
|
||||
// menu. The mouse still opens it from the title bar.
|
||||
if message == wm::WM_SYSCOMMAND && wparam & 0xFFF0 == wm::SC_KEYMENU as usize {
|
||||
return 0;
|
||||
}
|
||||
// SAFETY: the procedure `hook` stored, called the way the system would.
|
||||
unsafe {
|
||||
let title = wm::GetPropW(hwnd, TITLE_PROC);
|
||||
@@ -366,4 +375,64 @@ mod tests {
|
||||
wm::DestroyWindow(hwnd);
|
||||
}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// The system commands `recording` received, low four bits cleared.
|
||||
static COMMANDS: std::cell::RefCell<Vec<usize>> = const { std::cell::RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// Stands in for a title's procedure: notes system commands and acts
|
||||
/// on none of them (no menu loop, no minimising).
|
||||
extern "system" fn recording(
|
||||
hwnd: HWND,
|
||||
message: u32,
|
||||
wparam: WPARAM,
|
||||
lparam: LPARAM,
|
||||
) -> LRESULT {
|
||||
if message == wm::WM_SYSCOMMAND {
|
||||
COMMANDS.with(|c| c.borrow_mut().push(wparam & 0xFFF0));
|
||||
return 0;
|
||||
}
|
||||
// SAFETY: the system's default procedure, called as the system would.
|
||||
unsafe { wm::DefWindowProcW(hwnd, message, wparam, lparam) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_keyboard_menu_never_reaches_the_title() {
|
||||
let null = std::ptr::null_mut();
|
||||
let hwnd = CreateWindowExW(
|
||||
0,
|
||||
w!("STATIC"),
|
||||
w!("menu"),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
10,
|
||||
10,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
std::ptr::null(),
|
||||
);
|
||||
assert!(!hwnd.is_null());
|
||||
// SAFETY: our window, owned by this thread, so each message is
|
||||
// handled before SendMessageW returns. `recording` stands in for the
|
||||
// title's procedure and the original is put back before the window
|
||||
// is destroyed.
|
||||
unsafe {
|
||||
let title = wm::GetPropW(hwnd, TITLE_PROC);
|
||||
wm::SetPropW(hwnd, TITLE_PROC, recording as *const () as *mut c_void);
|
||||
let command = |c: u32, key: u8| {
|
||||
wm::SendMessageW(hwnd, wm::WM_SYSCOMMAND, c as usize, LPARAM::from(key))
|
||||
};
|
||||
command(wm::SC_KEYMENU, 0); // Alt or F10
|
||||
command(wm::SC_KEYMENU, b' '); // Alt+Space
|
||||
command(wm::SC_KEYMENU | 1, 0); // the low bits are the system's
|
||||
command(wm::SC_MINIMIZE, 0);
|
||||
wm::SetPropW(hwnd, TITLE_PROC, title);
|
||||
wm::DestroyWindow(hwnd);
|
||||
}
|
||||
let seen = COMMANDS.with(|c| c.borrow().clone());
|
||||
assert_eq!(seen, [wm::SC_MINIMIZE as usize]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user