Merge pull request #7 from cosimawei/feat/configurable-input-release-pr

Support configurable SDL input release shortcuts and pointer exit.
This commit is contained in:
Xiaochi You authored and GitHub committed 2026-12-04 16:06:30 +08:00
commit abc65fdbf9
6 files changed
+296 -9

No files matched your search

+15
View File
@@ -28,6 +28,8 @@ them; its `manual` checks are on a Linux host's guest boot.
| `displays=2` | the reims-graphics device's displays, 1 to 4, a window each (QEMU's reims-graphics-pci property; 1 by default) |
| `REIMS_VGPU_WINDOW_IMPL=sdl` | macOS, built with the `macos-sdl` feature (on by default): SDL3's window in place of AppKit's ("On macOS through SDL3" below) |
| `REIMS_VGPU_WINDOW_CAPTURE=0` | capture off: no window ever captures the keyboard or the pointer ("Capture off" below); reims-ctl sets it for a VM whose capture setting is off |
| `REIMS_VGPU_WINDOW_RELEASE_KEYS=Ctrl+Shift+F12` | additional release shortcut for SDL windows on Windows and Linux; when unset, `REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS` can supply it |
| `REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE=1` | on Windows and Linux, allow the pointer to leave a captured SDL window and release input when it leaves |
Unset or unparsable values give the defaults below.
`test: env::unset_or_unparsable_values_are_the_defaults`,
@@ -700,6 +702,19 @@ runtime's own device. The switches above apply, and `resolution=` too.
`test: sdl::shell::the_end_lets_go_of_the_grab_and_the_holds`,
`test: sdl::shell::the_pointer_is_held_over_the_image_in_whole_points`,
`manual` (the grab and the hold)
- An additional release shortcut uses at least one modifier and one to three
ordinary keys. The captured title shows the shortcut, and an empty or
invalid setting uses the default. The built-in release chord still works
with an additional shortcut set.
`test: native::sdl_thread::input::custom_shortcut_releases_capture_without_forwarding_its_final_key`,
`test: native::sdl_thread::input::custom_shortcut_can_hold_two_regular_keys`,
`test: native::sdl_thread::input::invalid_shortcut_keeps_the_default`
- With release on mouse leave enabled, capture allows the pointer out of the
window. The SDL mouse-leave event lifts held keys and buttons and releases
capture. This setting also turns off relative pointer locks in games and
at a shared display edge.
`test: native::sdl_thread::input::leaving_the_window_releases_mouse_and_keyboard_when_enabled`,
`manual` (move the pointer out of a captured SDL window)
- Scrolling: the wheel's notches, or, where the host takes smooth
scrolling, its lines at 10 points each, in no gesture (SDL gives no
phases); the guest gets the device's own direction.
+18
View File
@@ -14,6 +14,9 @@
//! no window ever captures the keyboard or the pointer ([`crate::capture`]). `1`, `true`,
//! `yes` or `on` turns it on. Default: [`CAPTURE_DEFAULT`]. reims-ctl sets it from a VM's
//! settings.
//! - `REIMS_VGPU_WINDOW_RELEASE_KEYS`: an additional SDL window capture release shortcut,
//! such as `Ctrl+Shift+F12`. Reims-Go's capture shortcut plugin can supply it through its
//! own namespace. The original release chord remains available.
use std::sync::OnceLock;
@@ -25,6 +28,11 @@ pub const SCREENS_VAR: &str = "REIMS_VGPU_WINDOW_SCREENS";
pub const IMPL_VAR: &str = "REIMS_VGPU_WINDOW_IMPL";
/// The capture switch's variable.
pub const CAPTURE_VAR: &str = "REIMS_VGPU_WINDOW_CAPTURE";
/// The extra release shortcut for Windows and Linux SDL windows.
pub const RELEASE_KEYS_VAR: &str = "REIMS_VGPU_WINDOW_RELEASE_KEYS";
/// The same shortcut supplied through the Reims-Go capture shortcut plugin.
const PLUGIN_RELEASE_KEYS_VAR: &str = "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_KEYS";
const PLUGIN_RELEASE_ON_LEAVE_VAR: &str = "REIMS_PLUGIN_LOCAL_CAPTURE_SHORTCUT_RELEASE_ON_LEAVE";
/// Whether a click into the image captures input where the capture switch is unset.
pub const CAPTURE_DEFAULT: bool = true;
@@ -40,6 +48,10 @@ pub struct Settings {
pub window_impl: WindowImpl,
/// Whether a click into the image captures input; off, nothing ever does.
pub capture: bool,
/// Additional shortcut text for the SDL window.
pub release_keys: Option<String>,
/// Let the mouse leave the SDL window and release input when it does.
pub release_on_leave: bool,
}
impl Default for Settings {
@@ -49,6 +61,8 @@ impl Default for Settings {
screens: Vec::new(),
window_impl: WindowImpl::AppKit,
capture: CAPTURE_DEFAULT,
release_keys: None,
release_on_leave: false,
}
}
}
@@ -126,6 +140,10 @@ pub fn settings() -> &'static Settings {
Settings {
window_impl: parse_impl(var(IMPL_VAR).as_deref()),
capture: parse_capture(var(CAPTURE_VAR).as_deref()),
release_keys: var(RELEASE_KEYS_VAR).or_else(|| var(PLUGIN_RELEASE_KEYS_VAR)),
release_on_leave: var(PLUGIN_RELEASE_ON_LEAVE_VAR)
.as_deref()
.is_some_and(parse_switch),
..parse(borderless.as_deref(), screens.as_deref())
}
})
@@ -81,7 +81,7 @@ use crate::config::{
};
use crate::game::Game;
use crate::geometry::{self, Rect};
use crate::keys::{KeyEvent, KeyState};
use crate::keys::{KeyEvent, KeyState, Modifiers};
use crate::scroll::POINTS_PER_LINE;
/// How soon after the window gains the keyboard a click is the one that focused it, in
@@ -180,6 +180,99 @@ fn fixed(points: f64) -> i32 {
}
}
/// An additional release shortcut, using USB key positions like SDL's keyboard path.
#[derive(Debug)]
pub(crate) struct ReleaseShortcut {
modifiers: Modifiers,
keys: Vec<u16>,
name: String,
}
impl ReleaseShortcut {
pub(crate) fn parse(value: &str) -> Option<Self> {
let parts = value.split('+').map(str::trim).map(str::to_ascii_uppercase);
let mut modifiers = Modifiers::default();
let mut keys = Vec::new();
let mut key_names = Vec::new();
for part in parts {
let held = match part.as_str() {
"CTRL" => &mut modifiers.control,
"ALT" => &mut modifiers.option,
"SHIFT" => &mut modifiers.shift,
"WIN" | "SUPER" => &mut modifiers.command,
_ => {
let key = Self::parse_key(&part)?;
if keys.len() == 3 || keys.contains(&key) {
return None;
}
keys.push(key);
key_names.push(part);
continue;
}
};
if std::mem::replace(held, true) {
return None;
}
}
if modifiers == Modifiers::default() || keys.is_empty() {
return None;
}
let mut name = String::new();
for (held, label) in [
(modifiers.control, "Ctrl+"),
(modifiers.option, "Alt+"),
(modifiers.shift, "Shift+"),
(
modifiers.command,
if cfg!(windows) { "Win+" } else { "Super+" },
),
] {
if held {
name.push_str(label);
}
}
name.push_str(&key_names.join("+"));
Some(Self {
modifiers,
keys,
name,
})
}
fn parse_key(key_name: &str) -> Option<u16> {
let key = match key_name.as_bytes() {
[letter @ b'A'..=b'Z'] => u16::from(*letter - b'A') + 0x04,
[digit @ b'1'..=b'9'] => u16::from(*digit - b'1') + 0x1e,
[b'0'] => 0x27,
_ => {
let number = key_name.strip_prefix('F')?.parse::<u16>().ok()?;
if !matches!(
key_name,
"F1" | "F2"
| "F3"
| "F4"
| "F5"
| "F6"
| "F7"
| "F8"
| "F9"
| "F10"
| "F11"
| "F12"
) {
return None;
}
0x3a + number - 1
}
};
Some(key)
}
pub(crate) fn name(&self) -> &str {
&self.name
}
}
/// One window's input decisions (the module doc).
#[derive(Default)]
pub(crate) struct Input {
@@ -187,6 +280,8 @@ pub(crate) struct Input {
keys: KeyState,
/// Who gets the keyboard.
capture: Capture,
release_shortcut: Option<ReleaseShortcut>,
release_on_leave: bool,
/// The `BUTTON_*` bits of the buttons the guest was told are down.
buttons: u32,
/// The pointer event last sent.
@@ -249,6 +344,30 @@ impl Input {
Input { capture, ..self }
}
/// Sets an additional shortcut for releasing captured input.
pub(crate) fn with_release_shortcut(self, release_shortcut: Option<ReleaseShortcut>) -> Input {
Input {
release_shortcut,
..self
}
}
pub(crate) fn with_release_on_leave(self, release_on_leave: bool) -> Input {
Input {
release_on_leave,
..self
}
}
/// Let the host have the keyboard and pointer when the mouse leaves the window.
pub(crate) fn mouse_left(&mut self) -> Reaction {
if !self.release_on_leave || !self.is_captured() {
return Reaction::default();
}
self.lift_buttons();
self.release_capture()
}
/// Whether the captured pointer has gone past the image's edge.
#[cfg(test)]
pub(crate) fn is_away(&self) -> bool {
@@ -307,13 +426,26 @@ impl Input {
/// Where the pointer is held over `image`, the image's rectangle in the window's points: the
/// image while captured ([`Capture::pointer_confinement`]).
pub(crate) fn pointer_confinement(&self, image: Rect) -> Option<Rect> {
self.capture.pointer_confinement(image)
if self.release_on_leave {
None
} else {
self.capture.pointer_confinement(image)
}
}
/// What the title ends with: the release chord while captured, in the host's OS's names for
/// its keys, and that a click captures while not ([`Capture::title_suffix`]).
pub(crate) fn title_suffix(&self) -> &'static str {
self.capture.title_suffix(HOST_OS)
pub(crate) fn title_suffix(&self) -> std::borrow::Cow<'_, str> {
if self.is_captured()
&& let Some(shortcut) = &self.release_shortcut
{
return format!(
" \u{2014} input captured (press {} to release)",
shortcut.name
)
.into();
}
self.capture.title_suffix(HOST_OS).into()
}
/// What to send, taken.
@@ -345,7 +477,8 @@ impl Input {
return;
}
if let (Some(point), Some(mode)) = (at.point, guest_mode(at)) {
if self.capture.is_captured()
if !self.release_on_leave
&& self.capture.is_captured()
&& let Some(motion) = self.motion.clone()
&& let Some(hold) = self.game.begin(motion.as_ref(), point)
{
@@ -356,7 +489,8 @@ impl Input {
Some(ToHost::Pointer { at, .. }) => Some(at),
_ => None,
};
if self.capture.is_captured()
if !self.release_on_leave
&& self.capture.is_captured()
&& let Some(beyond) = self.beyond.clone()
&& let Some(hold) =
self.away
@@ -552,6 +686,18 @@ impl Input {
}
return Reaction::default();
}
if self.is_captured()
&& let Some(shortcut) = &self.release_shortcut
&& shortcut.keys.contains(&usage)
&& shortcut
.keys
.iter()
.all(|&key| key == usage || self.keys.is_held(key))
&& shortcut.modifiers == self.keys.held_modifiers()
&& !repeat
{
return self.release_capture();
}
if let Some(event) = self.keys.key_down(usage, repeat) {
self.out.push(ToHost::Key(event));
}
@@ -865,6 +1011,80 @@ mod tests {
assert_eq!(input.title_suffix(), UNCAPTURED_SUFFIX);
}
#[test]
fn custom_shortcut_releases_capture_without_forwarding_its_final_key() {
let mut input =
focused().with_release_shortcut(ReleaseShortcut::parse(" shift + ctrl + f12 "));
input.button_down(BUTTON_LEFT, 1_000_000_000, &at((10.0, 10.0)));
input.button_up(BUTTON_LEFT, &at((10.0, 10.0)));
assert!(input.title_suffix().contains("Ctrl+Shift+F12"));
input.take();
input.key(0xE0, true, false);
input.key(0xE1, true, false);
let reaction = input.key(0x45, true, false);
assert!(reaction.capture_changed && !input.is_captured());
assert_eq!(
input.take(),
vec![
ToHost::Key(KeyEvent::down(0xE0)),
ToHost::Key(KeyEvent::down(0xE1)),
ToHost::Key(KeyEvent::up(0xE1)),
ToHost::Key(KeyEvent::up(0xE0)),
]
);
}
#[test]
fn invalid_shortcut_keeps_the_default() {
for value in [
"",
"F12",
"Ctrl+Shift",
"Ctrl+Ctrl+F12",
"Ctrl+F13",
"Ctrl+F01",
] {
assert!(ReleaseShortcut::parse(value).is_none(), "{value:?}");
}
}
#[test]
fn custom_shortcut_can_hold_two_regular_keys() {
let mut input = focused().with_release_shortcut(ReleaseShortcut::parse("Ctrl+A+Z"));
let point = at((10.0, 10.0));
input.button_down(BUTTON_LEFT, 1_000_000_000, &point);
input.button_up(BUTTON_LEFT, &point);
assert!(input.title_suffix().contains("Ctrl+A+Z"));
assert!(!input.key(0xe0, true, false).capture_changed);
assert!(!input.key(0x04, true, false).capture_changed);
assert!(input.key(0x1d, true, false).capture_changed);
}
#[test]
fn leaving_the_window_releases_mouse_and_keyboard_when_enabled() {
for enabled in [false, true] {
let mut input = focused().with_release_on_leave(enabled);
let point = at((10.0, 10.0));
input.button_down(BUTTON_LEFT, 1_000_000_000, &point);
input.key(0x04, true, false);
assert!(input.is_captured());
assert_eq!(input.pointer_confinement(point.image).is_none(), enabled);
input.take();
assert_eq!(input.mouse_left().capture_changed, enabled);
assert_eq!(input.is_captured(), !enabled);
if enabled {
let events = input.take();
assert!(events.contains(&ToHost::Key(KeyEvent::up(0x04))));
assert!(
events
.iter()
.any(|event| matches!(event, ToHost::Pointer { buttons: 0, .. }))
);
assert!(!input.mouse_left().capture_changed);
}
}
}
/// With capture off, a click in the image is sent but captures nothing, so nothing holds the
/// pointer; the pointer outside the image is sent only in a drag; Ctrl, Alt and the GUI key
/// are the guest's keys and release nothing; the title says capture is off
@@ -113,6 +113,10 @@ impl Input {
self.0.focus_lost().capture_changed
}
pub(crate) fn mouse_left(&mut self) -> bool {
self.0.mouse_left().capture_changed
}
/// Whether input, the keyboard and the pointer, is captured.
pub(crate) fn is_captured(&self) -> bool {
self.0.is_captured()
@@ -124,7 +128,7 @@ impl Input {
}
/// What the title ends with in the current capture state.
pub(crate) fn title_suffix(&self) -> &'static str {
pub(crate) fn title_suffix(&self) -> std::borrow::Cow<'_, str> {
self.0.title_suffix()
}
@@ -211,6 +215,25 @@ pub(crate) fn open(window: &Arc<Window>, borderless: bool, _taken: &[Frame]) ->
display.0
);
let callbacks = window.callbacks();
let settings = crate::env::settings();
let release_shortcut = settings
.release_keys
.as_deref()
.and_then(input::ReleaseShortcut::parse);
if settings
.release_keys
.as_deref()
.is_some_and(|keys| !keys.trim().is_empty())
&& release_shortcut.is_none()
{
log::warn!(target: "reims_vgpu_window", "invalid input release shortcut; using the default");
}
log::info!(target: "reims_vgpu_window",
"input settings: capture={}, release shortcut={}, release on mouse leave={}",
settings.capture,
release_shortcut.as_ref().map_or("default", input::ReleaseShortcut::name),
settings.release_on_leave,
);
Some(Opened {
sdl,
asked: size,
@@ -221,7 +244,9 @@ pub(crate) fn open(window: &Arc<Window>, borderless: bool, _taken: &[Frame]) ->
.and_then(|callbacks| callbacks.beyond.clone()),
callbacks.and_then(|callbacks| callbacks.motion),
)
.with_capture(Capture::with_setting(crate::env::settings().capture)),
.with_capture(Capture::with_setting(settings.capture))
.with_release_shortcut(release_shortcut)
.with_release_on_leave(settings.release_on_leave),
),
state: State {
display,
+5 -1
View File
@@ -388,7 +388,11 @@ impl Shell {
// SDL's cursor is the process's: another window may have hidden it.
self.over_image = false;
self.refresh_cursor();
false
#[cfg(any(target_os = "linux", target_os = "windows"))]
let released = k == SDL_EVENT_WINDOW_MOUSE_LEAVE.0 && self.input.mouse_left();
#[cfg(target_os = "macos")]
let released = false;
released
}
_ => os::input_event(self, kind, event),
};
+5
View File
@@ -0,0 +1,5 @@
{
"capabilities": [
"window-release-shortcut"
]
}