compat-common: hand out a stand-in IGameInput when the host has no GameInput #326

Closed
github-bot wants to merge 1 commit from gh-pr/326/head into gh-pr/326/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 19:03 UTC · original
GDAgent:fix/compat-gameinput-standalone → main · +246 −0 · 1 files · 1 commits · closed without merging

Library / area: compat-common (gamepad.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: observed behaviour of a very popular AAA title on native Windows 11 (not run under Proton); GDK IGameInput reference for the vtable layout; the repo's existing virtual-pad hooks.

What

When the real GameInputCreate fails, the hook now returns S_OK with a static stand-in IGameInput instead of passing the failure on. The stand-in goes through the same patch_object path as a real object, so the virtual Xbox gamepad, the control channel and the host-pad bridge attach exactly as they do to a host GameInput. If a real object was patched before, the stand-in is not adopted and the original HRESULT is returned. now_us keeps its behaviour; its QueryPerformanceCounter fallback moves into qpc_us so the stand-in timestamp does not recurse.

Why

On a console GameInputCreate never fails and titles use the returned interface without checking the HRESULT. A very popular AAA title calls a method on the null interface right after a failed GameInputCreate and dies with an access violation 5.7 s into the run. Hosts without a running GameInput service (a fresh VM or Windows Sandbox, where GameInputSvc stops right after START_PENDING) return 0xd0000034 from the real call.

Relation to #293 (which says GameInput on such hosts is solved by shipping Microsoft's GameInput redistributable): that remains the way to get real host devices (keyboard, XInput and other pads through the host). This change does not replace it. It only applies when the real GameInputCreate has already failed, so nothing changes on hosts where the redistributable or the service works. It removes the need for the redistributable just to get a virtual pad and a title that does not crash on a null interface; host devices stay unavailable in that mode.

Behaviour of the stand-in, method by method

Each of the 22 methods either really works (backed by the virtual pad or the host clock) or returns a real error code. Nothing claims a host device exists.

Real (virtual pad, through the existing hooks that wrap the stand-in):

  • GetCurrentReading / GetNextReading / GetPreviousReading / GetTemporalReading for the virtual pad or any-device queries: the virtual pad's readings, as with a host object.
  • RegisterDeviceCallback for the pad: the registration is recorded and the virtual pad is announced, as with a host object.
  • FindDeviceFromId / FindDeviceFromObject for the virtual pad: the virtual device.
  • GetCurrentTimestamp: QueryPerformanceCounter in microseconds.

Stand-in behaviour for everything that is not the virtual pad (there are no host devices):

  • GetCurrentReading, GetNextReading, GetPreviousReading, GetTemporalReading: GAMEINPUT_E_READING_NOT_FOUND, null reading.
  • RegisterReadingCallback: E_NOTIMPL, zero token (same answer the virtual pad already gets; callback-driven readings are not supported).
  • RegisterDeviceCallback, RegisterSystemButtonCallback, RegisterKeyboardLayoutCallback: a registration with a unique token that is remembered. These are event subscriptions whose sources do not exist here, so they never fire. A null token pointer is E_INVALIDARG.
  • StopCallback, UnregisterCallback: forget only tokens that were issued; UnregisterCallback returns false for an unknown or already removed token.
  • CreateDispatcher, CreateAggregateDevice: E_NOTIMPL, null out pointer.
  • FindDeviceFromId, FindDeviceFromObject, FindDeviceFromPlatformHandle, FindDeviceFromPlatformString (anything but the virtual pad): E_NOTFOUND (HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) with a null out pointer.
  • EnableOemDeviceSupport, SetFocusPolicy: accepted with no effect, because there are no host devices to apply them to. The return type of EnableOemDeviceSupport was not checked against the public header; the stand-in returns 0.
  • QueryInterface returns the object for any IID (and AddRef), like the other virtual objects of this module; Release never frees the static object.

Tests

  • gamepad::tests::failed_create_hands_out_a_standin_gameinput_with_the_virtual_pad: adopts the stand-in twice (same object, patched vtable), refuses a null out pointer, and calls QI, Release, the timestamp, GetCurrentReading (READING_NOT_FOUND, null out), RegisterReadingCallback (E_NOTIMPL, zero token), RegisterDeviceCallback and RegisterSystemButtonCallback (distinct non-zero tokens; null token pointer is E_INVALIDARG), StopCallback, UnregisterCallback (issued token true once, then false; stopped and unknown tokens false), CreateDispatcher (E_NOTIMPL), FindDeviceFromId (virtual pad found, other id E_NOTFOUND), FindDeviceFromObject, EnableOemDeviceSupport and SetFocusPolicy through the patched vtable.
  • cargo test -p compat-common --lib gamepad: 22 passed, 0 failed.
  • Native Windows 11; not run under Proton.

Not verified

  • In a title: not re-run in a title after this exact change; the null-interface crash that motivated it was seen in a very popular AAA title. The stand-in was not tested on a host without GameInput (the unit test adopts the stand-in directly).

  • E_NOTFOUND as the code for an unknown device and 0 for EnableOemDeviceSupport were not compared against a real GameInput.

  • If a real GameInputCreate failed first and succeeded later in the same process, the real object would not be wrapped (its vtable differs from the stand-in's). Not handled; hosts do not change GameInput availability mid-run in practice.

  • No real-device behaviour (keyboard, XInput) is provided in stand-in mode.

  • cargo test -p compat-common (gamepad tests) passes

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

**@⁠GDAgent** on GitHub · 2026-11-23 19:03 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/326) `GDAgent:fix/compat-gameinput-standalone` → `main` · +246 −0 · 1 files · 1 commits · closed without merging **Library / area:** `compat-common` (`gamepad.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** observed behaviour of a very popular AAA title on native Windows 11 (not run under Proton); GDK `IGameInput` reference for the vtable layout; the repo's existing virtual-pad hooks. ## What When the real `GameInputCreate` fails, the hook now returns `S_OK` with a static stand-in `IGameInput` instead of passing the failure on. The stand-in goes through the same `patch_object` path as a real object, so the virtual Xbox gamepad, the control channel and the host-pad bridge attach exactly as they do to a host GameInput. If a real object was patched before, the stand-in is not adopted and the original HRESULT is returned. `now_us` keeps its behaviour; its QueryPerformanceCounter fallback moves into `qpc_us` so the stand-in timestamp does not recurse. ## Why On a console `GameInputCreate` never fails and titles use the returned interface without checking the HRESULT. A very popular AAA title calls a method on the null interface right after a failed `GameInputCreate` and dies with an access violation 5.7 s into the run. Hosts without a running GameInput service (a fresh VM or Windows Sandbox, where `GameInputSvc` stops right after START_PENDING) return `0xd0000034` from the real call. Relation to #293 (which says GameInput on such hosts is solved by shipping Microsoft's `GameInput` redistributable): that remains the way to get real host devices (keyboard, XInput and other pads through the host). This change does not replace it. It only applies when the real `GameInputCreate` has already failed, so nothing changes on hosts where the redistributable or the service works. It removes the need for the redistributable just to get a virtual pad and a title that does not crash on a null interface; host devices stay unavailable in that mode. ## Behaviour of the stand-in, method by method Each of the 22 methods either really works (backed by the virtual pad or the host clock) or returns a real error code. Nothing claims a host device exists. Real (virtual pad, through the existing hooks that wrap the stand-in): - `GetCurrentReading` / `GetNextReading` / `GetPreviousReading` / `GetTemporalReading` for the virtual pad or any-device queries: the virtual pad's readings, as with a host object. - `RegisterDeviceCallback` for the pad: the registration is recorded and the virtual pad is announced, as with a host object. - `FindDeviceFromId` / `FindDeviceFromObject` for the virtual pad: the virtual device. - `GetCurrentTimestamp`: QueryPerformanceCounter in microseconds. Stand-in behaviour for everything that is not the virtual pad (there are no host devices): - `GetCurrentReading`, `GetNextReading`, `GetPreviousReading`, `GetTemporalReading`: `GAMEINPUT_E_READING_NOT_FOUND`, null reading. - `RegisterReadingCallback`: `E_NOTIMPL`, zero token (same answer the virtual pad already gets; callback-driven readings are not supported). - `RegisterDeviceCallback`, `RegisterSystemButtonCallback`, `RegisterKeyboardLayoutCallback`: a registration with a unique token that is remembered. These are event subscriptions whose sources do not exist here, so they never fire. A null token pointer is `E_INVALIDARG`. - `StopCallback`, `UnregisterCallback`: forget only tokens that were issued; `UnregisterCallback` returns false for an unknown or already removed token. - `CreateDispatcher`, `CreateAggregateDevice`: `E_NOTIMPL`, null out pointer. - `FindDeviceFromId`, `FindDeviceFromObject`, `FindDeviceFromPlatformHandle`, `FindDeviceFromPlatformString` (anything but the virtual pad): `E_NOTFOUND` (`HRESULT_FROM_WIN32(ERROR_NOT_FOUND)`) with a null out pointer. - `EnableOemDeviceSupport`, `SetFocusPolicy`: accepted with no effect, because there are no host devices to apply them to. The return type of `EnableOemDeviceSupport` was not checked against the public header; the stand-in returns 0. - `QueryInterface` returns the object for any IID (and `AddRef`), like the other virtual objects of this module; `Release` never frees the static object. ## Tests - `gamepad::tests::failed_create_hands_out_a_standin_gameinput_with_the_virtual_pad`: adopts the stand-in twice (same object, patched vtable), refuses a null out pointer, and calls QI, Release, the timestamp, `GetCurrentReading` (`READING_NOT_FOUND`, null out), `RegisterReadingCallback` (`E_NOTIMPL`, zero token), `RegisterDeviceCallback` and `RegisterSystemButtonCallback` (distinct non-zero tokens; null token pointer is `E_INVALIDARG`), `StopCallback`, `UnregisterCallback` (issued token true once, then false; stopped and unknown tokens false), `CreateDispatcher` (`E_NOTIMPL`), `FindDeviceFromId` (virtual pad found, other id `E_NOTFOUND`), `FindDeviceFromObject`, `EnableOemDeviceSupport` and `SetFocusPolicy` through the patched vtable. - `cargo test -p compat-common --lib gamepad`: 22 passed, 0 failed. - Native Windows 11; not run under Proton. ## Not verified - In a title: not re-run in a title after this exact change; the null-interface crash that motivated it was seen in a very popular AAA title. The stand-in was not tested on a host without GameInput (the unit test adopts the stand-in directly). - `E_NOTFOUND` as the code for an unknown device and `0` for `EnableOemDeviceSupport` were not compared against a real GameInput. - If a real `GameInputCreate` failed first and succeeded later in the same process, the real object would not be wrapped (its vtable differs from the stand-in's). Not handled; hosts do not change GameInput availability mid-run in practice. - No real-device behaviour (keyboard, XInput) is provided in stand-in mode. - [x] `cargo test -p compat-common` (gamepad tests) passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
On a console GameInputCreate always succeeds, and titles use the returned
IGameInput without checking the HRESULT. A very popular AAA title calls a
method on the null interface right after a failed GameInputCreate and dies
with an access violation 5.7 s into the run. Hosts without a running
GameInput service (a fresh VM or Windows Sandbox, where GameInputSvc stops
right after START_PENDING) return 0xd0000034 from the real GameInputCreate.

When the real call fails, the hook now returns S_OK with a static stand-in
object whose 22 IGameInput methods behave like GameInput with no host
devices; each one either works or returns a real error code:
- GetCurrentTimestamp: QueryPerformanceCounter in microseconds;
- GetCurrentReading, GetNextReading, GetPreviousReading, GetTemporalReading:
  GAMEINPUT_E_READING_NOT_FOUND with a null reading (no host devices);
- RegisterReadingCallback: E_NOTIMPL with a zero token, as already done for
  the virtual pad (callback-driven readings are not supported);
- RegisterDeviceCallback, RegisterSystemButtonCallback,
  RegisterKeyboardLayoutCallback: a registration with a unique token that is
  remembered; no host source exists, so these never fire; a null token
  pointer is E_INVALIDARG;
- StopCallback and UnregisterCallback forget only tokens that were issued
  (UnregisterCallback returns false for an unknown or already removed token);
- CreateDispatcher and CreateAggregateDevice: E_NOTIMPL;
- FindDeviceFromId, FindDeviceFromObject, FindDeviceFromPlatformHandle and
  FindDeviceFromPlatformString: the virtual pad is found through the
  existing hooks, anything else is E_NOTFOUND;
- EnableOemDeviceSupport and SetFocusPolicy: accepted with no effect, since
  there are no host devices to apply them to.
QueryInterface returns the object for any IID, like the other virtual
objects of this module.

The stand-in goes through the same patch_object path as a real object, so
the virtual Xbox gamepad, the control channel and the host-pad bridge
attach exactly as they do to the host GameInput. If a real object was
patched before, the stand-in is not adopted and the original HRESULT is
returned. The QueryPerformanceCounter fallback of now_us moves into
qpc_us so the stand-in timestamp does not recurse.

Test: failed_create_hands_out_a_standin_gameinput_with_the_virtual_pad
adopts the stand-in twice (same object, patched vtable), refuses a null
out pointer, and calls QI, Release, the timestamp, GetCurrentReading,
RegisterReadingCallback (E_NOTIMPL), RegisterDeviceCallback and
RegisterSystemButtonCallback (distinct tokens; null token is E_INVALIDARG),
StopCallback, UnregisterCallback (known, repeated and unknown tokens),
CreateDispatcher, FindDeviceFromId (virtual pad found, other id E_NOTFOUND),
FindDeviceFromObject, EnableOemDeviceSupport and SetFocusPolicy through the
patched vtable. compat-common gamepad tests: 22 passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:18:55 +00:00
github-bot closed this pull request 2026-11-26 06:18:55 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:49 UTC · original

Applied on main in 9af87c0 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/326#issuecomment-7164352044) Applied on main in 9af87c0 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.