🐛 Let no input of a window reach the guest while its close asks

From a close the person asks for to the answer of its question, the SDL
window's input sends the guest no key, button, pointer or wheel. SDL on Windows
hands Alt+F4's close over before F4's down, so F4 went down in the guest after
the close had let every key go, and the task dialog took its up: the guest held
F4, repeating, until Cancel. Now that down, the keys and clicks the question
takes, and the ups SDL's message box queues as it shows reach nothing, and no
key is held in the guest during the question or after it.

The register's Leaving entry on Linux and Windows says so, with the test of the
pattern.
This commit is contained in:
Anees Iqbal committed 2026-11-25 13:03:56 -08:00
1 parent 9971cb5fc1
commit 2992852a4f
3 files changed
+110 -9

No files matched your search

+10 -6
View File
@@ -660,16 +660,20 @@ runtime's own device. The switches above apply, and `resolution=` too.
Turn Off; on Windows a task dialog the window owns, Turn Off left of
Cancel. Turn Off asks the host to end the VM; Cancel, or the question
dismissed, leaves the window and the VM running, nothing held in the
guest and the input released. While the question is up, the library's UI
thread waits for it: the windows' frames keep drawing, and their events
wait. When to ask is as on macOS; where SDL shows no question (no zenity
and no X server), the close goes ahead as before, and the reason is
logged. The window closes at the display's release, once its swapchain is
gone.
guest and the input released. From the close to the answer nothing of
the window's input reaches the guest: not a key that goes down after the
close let the keys go (F4 of Alt+F4 on Windows, which SDL hands over
after the close), nor the keys and clicks the question takes, nor their
ups. While the question is up, the library's UI thread waits for it: the
windows' frames keep drawing, and their events wait. When to ask is as on
macOS; where SDL shows no question (no zenity and no X server), the close
goes ahead as before, and the reason is logged. The window closes at the
display's release, once its swapchain is gone.
`test: close::a_close_asks_only_where_the_host_would_hear_of_it`,
`test: close::only_turn_off_tells_the_host`,
`test: sdl::shell::a_close_asks_where_the_host_hears_and_tells_it_once_turned_off`,
`test: native::sdl_thread::input::a_cancelled_close_leaves_nothing_held_and_the_input_goes_on`,
`test: native::sdl_thread::input::a_key_down_after_the_close_s_lift_never_reaches_the_guest`,
`test: native::sdl_thread::sdl::the_question_s_default_is_cancel`,
`test: native::sdl_thread::sdl::an_answer_is_turn_off_only_by_its_button`,
`manual` (the question on Wayland, X11 and Windows; Cancel, then Turn Off)
@@ -30,6 +30,14 @@
//! (`super::super::HOST_OS`): Ctrl+Alt+Super on Linux, Ctrl+Alt+Win on Windows.
//! - **No stuck keys**: losing the keyboard, releasing capture and closing send an up for every
//! key held; losing the keyboard and closing lift the buttons held too.
//! - **While a close's question is open** ([`Input::question_opened`], `crate::close`), nothing of
//! the window's reaches the guest, no key, button, pointer or wheel, from the close's lift of
//! every key and button to the answer. A key that goes down after the lift never reaches it,
//! nor does its up: SDL on Windows hands Alt+F4's close over before F4's down (read from SDL
//! 3.4.18's code, `WIN_WindowProc`), and a question in a dialog of its own takes the ups of the
//! keys held as it shows (SDL's message box lifts the keys SDL holds as it shows,
//! `SDL_ResetKeyboard`, and their ups come once it is answered). The keys and clicks that answer
//! the question are the question's.
//! - **With capture off** ([`crate::capture`], the VM's setting): a click in the image is sent,
//! but nothing is captured, grabbed, held or locked; the pointer is sent over the image alone,
//! and outside it only in a drag; keys go to the guest while the window has the keyboard, but
@@ -194,6 +202,8 @@ pub(crate) struct Input {
motion: Option<Arc<dyn PointerMotion>>,
/// Whether the captured pointer drives it.
game: Game,
/// A close's question is open over the window: nothing reaches the guest (the module doc).
question: bool,
}
impl std::fmt::Debug for Input {
@@ -326,7 +336,7 @@ impl Input {
/// drag ([`Capture::pointer_outside_image`]). A place SDL reports while the pointer is locked
/// is SDL's own (the lock's warp): the pointer's moves are [`Self::moved_by`]'s.
pub(crate) fn moved(&mut self, at: &At) {
if self.is_locked() {
if self.is_locked() || self.question {
return;
}
if let (Some(point), Some(mode)) = (at.point, guest_mode(at)) {
@@ -364,6 +374,9 @@ impl Input {
/// points, on, coming back where it lands on the window's display, away from the edge, or
/// driving the guest's mouse from where it is once the guest hides its cursor.
pub(crate) fn moved_by(&mut self, by: (f64, f64), at: &At) {
if self.question {
return;
}
if let Some(motion) = self.motion.clone() {
if self.game.is_on() {
let cursor = cursor_point(at);
@@ -409,6 +422,9 @@ impl Input {
/// Button `bit` went down at `timestamp` (SDL's event clock).
pub(crate) fn button_down(&mut self, bit: u32, timestamp: u64, at: &At) -> Reaction {
if self.question {
return Reaction::default();
}
if self.game.press(self.motion.as_deref(), bit) {
return Reaction::default();
}
@@ -448,6 +464,9 @@ impl Input {
/// Button `bit` went up: sent where the guest was told it is down, wherever the pointer is,
/// by the route it went down by (the guest's mouse, or the tablet).
pub(crate) fn button_up(&mut self, bit: u32, at: &At) {
if self.question {
return;
}
if self.game.release(self.motion.as_deref(), bit) {
return;
}
@@ -472,6 +491,9 @@ impl Input {
/// The wheel turned (the module doc).
pub(crate) fn wheel(&mut self, wheel: Wheel, at: &At) {
if self.question {
return;
}
let (Some(point), Some(_)) = (at.point, guest_mode(at)) else {
return;
};
@@ -506,7 +528,7 @@ impl Input {
/// A key, `usage`, went down (`down`) or up; `repeat` for the host's repeat of a held key.
pub(crate) fn key(&mut self, usage: u16, down: bool, repeat: bool) -> Reaction {
if !self.focused {
if !self.focused || self.question {
return Reaction::default();
}
if !down {
@@ -561,6 +583,20 @@ impl Input {
self.capture.release();
}
/// A close's question opened over the window: every key and button held is lifted, capture
/// ends, and nothing reaches the guest until it is answered ([`Self::question_answered`], the
/// module doc).
pub(crate) fn question_opened(&mut self) {
self.closing();
self.question = true;
}
/// The question was answered: the input goes on. Nothing is held in the guest: what went down
/// while the question was open never reached it, so its ups go nowhere either.
pub(crate) fn question_answered(&mut self) {
self.question = false;
}
/// Ups for every key held.
fn lift_keys(&mut self) {
let ups = self.keys.release_all();
@@ -915,6 +951,60 @@ mod tests {
assert!(r.capture_changed && input.is_captured());
}
/// While a close's question is open nothing reaches the guest, and no key is held there during
/// it or after: Alt+F4 as SDL hands it over on Windows (Alt's down, the close, then F4's
/// down), the clicks, moves and wheel the question gets, and the ups of Alt and F4 coming
/// once it is answered (SDL's message box lifts the keys SDL holds as it shows); after the
/// answer the input goes on
#[test]
fn a_key_down_after_the_close_s_lift_never_reaches_the_guest() {
const ALT: u16 = 0xE2;
const F4: u16 = 0x3D;
let mut input = focused();
input.button_down(BUTTON_LEFT, 1_000_000_000, &at((10.0, 10.0)));
input.key(ALT, true, false);
assert_eq!(
input.take(),
vec![
pointer(20, 20, BUTTON_LEFT),
ToHost::Key(KeyEvent::down(ALT))
]
);
input.question_opened();
assert!(!input.is_captured());
assert_eq!(
input.take(),
vec![ToHost::Key(KeyEvent::up(ALT)), pointer(20, 20, 0)]
);
input.key(F4, true, false);
input.key(F4, true, true);
input.key(0x28, true, false);
input.key(0x28, false, false);
input.moved(&at((30.0, 30.0)));
input.button_down(BUTTON_LEFT, 2_000_000_000, &at((30.0, 30.0)));
input.button_up(BUTTON_LEFT, &at((30.0, 30.0)));
input.wheel(
Wheel {
notches: (0, 1),
lines: (0.0, 1.0),
flipped: false,
},
&at((30.0, 30.0)),
);
assert_eq!(input.take(), vec![], "nothing while the question is open");
assert!(!input.is_captured());
input.question_answered();
input.key(ALT, false, false);
input.key(F4, false, false);
assert_eq!(input.take(), vec![], "no up for a key the guest never had");
input.key(0x05, true, false);
input.moved(&at((40.0, 20.0)));
assert_eq!(
input.take(),
vec![ToHost::Key(KeyEvent::down(0x05)), pointer(80, 40, 0)]
);
}
/// The wheel over the image sends notches up and left in the device's own direction, and
/// lines as smooth scrolling at 10 points each
#[test]
@@ -462,14 +462,21 @@ const CANCEL_ID: c_int = 0;
/// wait meanwhile, while the render threads draw on. So it is posted, to run after the events at
/// hand with no window borrowed; the answer goes to [`shell::answered`]. A window whose display
/// ended before the question showed asks nothing.
pub(crate) fn ask(shell: &Shell, ids: Vec<u64>) {
///
/// From the close to the answer, nothing of the window's input reaches the guest
/// ([`input::Input::question_opened`]): the events at hand before the question shows, a key that
/// went down after the close's lift among them, and the ups of the keys the question's dialog
/// takes, which come once it is answered.
pub(crate) fn ask(shell: &mut Shell, ids: Vec<u64>) {
let id = shell.window.id();
shell.input.0.question_opened();
super::ui::post(move || {
let parent = shell::with_shell(id, |s| (!s.closed).then_some(s.sdl)).flatten();
let answer = match parent {
Some(parent) => question(parent),
None => Answer::Cancel,
};
shell::with_shell(id, |s| s.input.0.question_answered());
shell::answered(&ids, answer);
});
}