Input: fix AZERTY scancode mapping, focus reset, GameInput QI validation, and UnregisterCallback race #99

Merged
raylitebull merged 1 commit from upstream-pr/100 into main 2026-11-23 11:25:27 +00:00
raylitebull commented 2026-11-23 11:11:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-23T11:11:21Z.


Summary

Fixes keyboard layout mapping, focus-loss input decay, config gating, COM QueryInterface IID checks, and a callback unregistration UAF race in crates/xcl/src/hle/{kbm.rs, gameinput.rs}:

  1. Physical scancode mapping for WASD / Q / E (kbm.rs):

    • Replaced hardcoded QWERTY virtual-key codes (0x57/0x41/0x53/0x44/0x51/0x45) with AT Set 1 physical scancodes (0x11, 0x1E, 0x1F, 0x20, 0x10, 0x12) translated via MapVirtualKeyExW(..., MAPVK_VSC_TO_VK_EX, layout) using the foreground window's active keyboard layout (falling back to QWERTY VK if unmapped). Fixes movement and Q/A collisions on AZERTY/Dvorak/Colemak layouts.
  2. Focus-loss mouse velocity & wheel reset (kbm.rs):

    • Added Mouse::reset / reset_mouse_state() clearing DX, DY, WHEEL, velocity, wheel, and wheel_rest on !focused and WM_ACTIVATE deactivation so stale camera/bumper motion does not replay on refocus.
  3. gameinput::poll() respecting config().keyboard_mouse = false (gameinput.rs):

    • Skips both keyboard() and kbm::apply() when keyboard_mouse is disabled while preserving XCL_PRESS_AT / XCL_PRESS_A_AT scripted inputs.
  4. Strict QueryInterface IID validation (gameinput.rs):

    • Verified official GameInput v0 IIDs (IGameInput, IGameInputDevice, IGameInputReading, IUnknown) against the Windows SDK GameInput.h.
    • Returns E_POINTER on null out, S_OK (with AddRef for Reading) only for IID_IUnknown or the object's own IID, and E_NOINTERFACE with *out = null for unknown/null IIDs (instead of unconditionally returning S_OK).
  5. UnregisterCallback / StopCallback synchronization (gameinput.rs):

    • Tracked both reading callbacks and async register_device_callback threads with Arc<CallbackEntry> (stopped, in_flight, running_threads, Condvar) and RAII CallbackGuard.
    • UnregisterCallback now waits up to timeout_us for in-flight invocations on other threads to finish (without self-deadlocking on re-entrant unregistration), eliminating UAF when callers free context after UnregisterCallback returns true.

Testing

  • Added 8 deterministic unit tests (2 in hle::kbm::tests, 6 in hle::gameinput::tests) covering MapVirtualKeyExW scancode translation, focus-loss reset, keyboard_mouse = false gating, QueryInterface IID checks on all 3 interfaces, and multi-threaded UnregisterCallback wait/timeout/re-entrancy synchronization (stress-tested 5× back-to-back with 0 failures).
  • Verified after rebasing onto origin/main (60599a9): cargo build --workspace and cargo test --workspace -- --test-threads=4 pass (467 passed, 0 failed, 36 ignored, 0 new warnings).
<!-- greyfinch-sync:ivall/xcl:issue:5451945634 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/100). Originally posted by [raylitebull](https://github.com/raylitebull) on 2026-11-23T11:11:21Z. --- ## Summary Fixes keyboard layout mapping, focus-loss input decay, config gating, COM `QueryInterface` IID checks, and a callback unregistration UAF race in `crates/xcl/src/hle/{kbm.rs, gameinput.rs}`: 1. **Physical scancode mapping for WASD / Q / E (`kbm.rs`):** - Replaced hardcoded QWERTY virtual-key codes (`0x57`/`0x41`/`0x53`/`0x44`/`0x51`/`0x45`) with AT Set 1 physical scancodes (`0x11`, `0x1E`, `0x1F`, `0x20`, `0x10`, `0x12`) translated via `MapVirtualKeyExW(..., MAPVK_VSC_TO_VK_EX, layout)` using the foreground window's active keyboard layout (falling back to QWERTY VK if unmapped). Fixes movement and Q/A collisions on AZERTY/Dvorak/Colemak layouts. 2. **Focus-loss mouse velocity & wheel reset (`kbm.rs`):** - Added `Mouse::reset` / `reset_mouse_state()` clearing `DX`, `DY`, `WHEEL`, `velocity`, `wheel`, and `wheel_rest` on `!focused` and `WM_ACTIVATE` deactivation so stale camera/bumper motion does not replay on refocus. 3. **`gameinput::poll()` respecting `config().keyboard_mouse = false` (`gameinput.rs`):** - Skips both `keyboard()` and `kbm::apply()` when `keyboard_mouse` is disabled while preserving `XCL_PRESS_AT` / `XCL_PRESS_A_AT` scripted inputs. 4. **Strict `QueryInterface` IID validation (`gameinput.rs`):** - Verified official GameInput v0 IIDs (`IGameInput`, `IGameInputDevice`, `IGameInputReading`, `IUnknown`) against the Windows SDK `GameInput.h`. - Returns `E_POINTER` on null `out`, `S_OK` (with `AddRef` for `Reading`) only for `IID_IUnknown` or the object's own IID, and `E_NOINTERFACE` with `*out = null` for unknown/null IIDs (instead of unconditionally returning `S_OK`). 5. **`UnregisterCallback` / `StopCallback` synchronization (`gameinput.rs`):** - Tracked both reading callbacks and async `register_device_callback` threads with `Arc<CallbackEntry>` (`stopped`, `in_flight`, `running_threads`, `Condvar`) and RAII `CallbackGuard`. - `UnregisterCallback` now waits up to `timeout_us` for in-flight invocations on other threads to finish (without self-deadlocking on re-entrant unregistration), eliminating UAF when callers free `context` after `UnregisterCallback` returns `true`. ## Testing - Added 8 deterministic unit tests (`2` in `hle::kbm::tests`, `6` in `hle::gameinput::tests`) covering `MapVirtualKeyExW` scancode translation, focus-loss reset, `keyboard_mouse = false` gating, `QueryInterface` IID checks on all 3 interfaces, and multi-threaded `UnregisterCallback` wait/timeout/re-entrancy synchronization (stress-tested 5× back-to-back with 0 failures). - Verified after rebasing onto `origin/main` (`60599a9`): `cargo build --workspace` and `cargo test --workspace -- --test-threads=4` pass (467 passed, 0 failed, 36 ignored, 0 new warnings).
upstream-sync closed this pull request 2026-11-24 06:57:10 +00:00
ivall commented 2026-11-24 07:00:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T11:25:45Z.


Merged. One addition (02d1302): since QueryInterface now refuses IIDs other than the v0 ones, each refused IID is logged once – a title built against a newer GameInput would ask that way, and before it simply got the v0 object. Your gating merged cleanly with the controller changes on main (connect / disconnect logging, slot search once a second). Merged together with #95–#102 and checked on Windows 11 with an RX 9070: cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; XDefiant (title screen, a scripted A reaches its "Xbox network unavailable" dialog), Unicorn Overlord and Minecraft run. Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7099631698 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/100#issuecomment-7099631698). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T11:25:45Z. --- Merged. One addition (02d1302): since `QueryInterface` now refuses IIDs other than the v0 ones, each refused IID is logged once – a title built against a newer GameInput would ask that way, and before it simply got the v0 object. Your gating merged cleanly with the controller changes on `main` (connect / disconnect logging, slot search once a second). Merged together with #95–#102 and checked on Windows 11 with an RX 9070: `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; XDefiant (title screen, a scripted A reaches its "Xbox network unavailable" dialog), Unicorn Overlord and Minecraft run. Thanks!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!99
No description provided.