startup_stubs: add GameInput keyboard readings and GetPreviousReading history #1429

Merged
tedin7 merged 6 commits from upstream-pr/1436 into main 2026-11-24 12:41:58 +00:00
tedin7 commented 2026-11-24 11:30:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:30:42Z.


Summary

Implements W2-1 in src/startup_stubs/gameinput.cpp (GameInput.dll stand-in):

  • Adds GameInputKindKeyboard (0x10) support with a distinct virtual keyboard IGameInputDevice (the_keyboard_device, GameInputDeviceInfo with supportedInput = GameInputKindKeyboard), enumerated via RegisterDeviceCallback and resolvable via FindDeviceFrom*.
  • Wires IGameInputReading::GetKeyCount (vtable slot 14) and IGameInputReading::GetKeyState (vtable slot 15) using the public v0 GameInputKeyState 12-byte layout (scanCode, codePoint, virtualKey, isDeadKey), sampling X2W_INPUT_KEYS (hex VK list) and focused GetAsyncKeyState.
  • Implements a 64-entry ring-buffer history in src/startup_stubs/gameinput.cpp so IGameInput::GetPreviousReading (slot 6) and IGameInput::GetNextReading (slot 5) walk reading sequences cleanly (GAMEINPUT_E_READING_NOT_FOUND / GAMEINPUT_E_REFERENCE_READING_TOO_OLD), while preserving 100% of existing gamepad and X2W_INPUT_PRESS/X2W_INPUT_SCRIPT behavior.
  • Adds tests/startup_gameinput_keys.cpp (startup_gameinput_keys CTest target in CMakeLists.txt) covering keyboard readings, GetKeyCount/GetKeyState, GetPreviousReading/GetNextReading history chain, combined keyboard+gamepad readings, and negative controls.

Verification

  • git diff --check
  • python3 tests/docs_links_check.py
  • python3 tests/bidi_controls_check.py
  • python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py preflight /root/wt/x2w-h-gameinput (VERDICT: OK)

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6580784903 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:30:42Z. --- ## Summary Implements `W2-1` in `src/startup_stubs/gameinput.cpp` (`GameInput.dll` stand-in): - Adds `GameInputKindKeyboard` (`0x10`) support with a distinct virtual keyboard `IGameInputDevice` (`the_keyboard_device`, `GameInputDeviceInfo` with `supportedInput = GameInputKindKeyboard`), enumerated via `RegisterDeviceCallback` and resolvable via `FindDeviceFrom*`. - Wires `IGameInputReading::GetKeyCount` (vtable slot 14) and `IGameInputReading::GetKeyState` (vtable slot 15) using the public v0 `GameInputKeyState` 12-byte layout (`scanCode`, `codePoint`, `virtualKey`, `isDeadKey`), sampling `X2W_INPUT_KEYS` (hex VK list) and focused `GetAsyncKeyState`. - Implements a 64-entry ring-buffer history in `src/startup_stubs/gameinput.cpp` so `IGameInput::GetPreviousReading` (slot 6) and `IGameInput::GetNextReading` (slot 5) walk reading sequences cleanly (`GAMEINPUT_E_READING_NOT_FOUND` / `GAMEINPUT_E_REFERENCE_READING_TOO_OLD`), while preserving 100% of existing gamepad and `X2W_INPUT_PRESS`/`X2W_INPUT_SCRIPT` behavior. - Adds `tests/startup_gameinput_keys.cpp` (`startup_gameinput_keys` CTest target in `CMakeLists.txt`) covering keyboard readings, `GetKeyCount`/`GetKeyState`, `GetPreviousReading`/`GetNextReading` history chain, combined keyboard+gamepad readings, and negative controls. ## Verification - `git diff --check` - `python3 tests/docs_links_check.py` - `python3 tests/bidi_controls_check.py` - `python3 /root/.local/state/xbox2windows/pipeline/x2w_pipe.py preflight /root/wt/x2w-h-gameinput` (`VERDICT: OK`) AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 12:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:45:32Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 35a913a4d5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038601173 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038601173). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:45:32Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `35a913a4d5` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". </details>
tedin7 commented 2026-11-24 12:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:38Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038667282 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038667282). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:38Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-24 12:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:41Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038667836 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038667836). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:41Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-24 12:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:44Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038668439 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038668439). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:44Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-24 12:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:47Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038669203 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038669203). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:47Z. **Review:** COMMENTED --- _No description provided._
tedin7 commented 2026-11-24 12:03:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:35:37Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 35a913a4d523527886ca567a22b1e7a8c14cde0b:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target startup_gameinput_keys startup_stub_gameinput -- -j 6): 3.83s (configure 1.78s)
    • Test (ctest --preset dev -R '^startup_gameinput_keys$' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.13s: startup_gameinput_keys 0.06s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target startup_gameinput_keys startup_stub_gameinput -- -j 6): 1.09s (configure 1.68s)
    • Test (ctest --preset release -R '^startup_gameinput_keys$' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (0.12s: startup_gameinput_keys 0.03s)
  • sccache: 0/4 hits; wall 14.4s
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7130296302 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#issuecomment-7130296302). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:35:37Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `35a913a4d523527886ca567a22b1e7a8c14cde0b`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target startup_gameinput_keys startup_stub_gameinput -- -j 6`): `3.83s` (configure `1.78s`) - Test (`ctest --preset dev -R '^startup_gameinput_keys$' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.13s`: `startup_gameinput_keys` `0.06s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target startup_gameinput_keys startup_stub_gameinput -- -j 6`): `1.09s` (configure `1.68s`) - Test (`ctest --preset release -R '^startup_gameinput_keys$' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`0.12s`: `startup_gameinput_keys` `0.03s`) - `sccache`: 0/4 hits; wall `14.4s`
chatgpt-codex-connector[bot commented 2026-11-24 12:03:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:35:46Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T11:45:36.128967Z 35a913a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7130299589 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#issuecomment-7130299589). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:35:46Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"35a913a4d523527886ca567a22b1e7a8c14cde0b","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T11:45:36.128967Z">2026-11-24T11:45:36.128967Z</relative-time> | `35a913a` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:45:33Z.

Code review location: src/startup_stubs/gameinput.cpp; line 522

@@ -350,11 +513,17 @@ HRESULT __stdcall input_register_short_callback(Object*, Object*, void*, void*,
 }
 void __stdcall input_stop_callback(Object*, u64) {}
 bool __stdcall input_unregister_callback(Object*, u64, u64) { return true; }
-HRESULT __stdcall input_find_device(Object*, const void*, Object** device)
+HRESULT __stdcall input_find_device(Object*, const void* id, Object** device)
 {
     static LONG calls;
     note("GameInput::FindDeviceFrom*", calls);
     if (!device) return E_POINTER;
+    if (id && (std::memcmp(id, keyboard_device_info + 0x20, 32) == 0 ||
+               std::memcmp(id, keyboard_device_info + 0x40, 32) == 0)) {

P1 Badge Separate handle-based lookups from device ID comparisons

When a caller uses v0 FindDeviceFromPlatformHandle, build() routes slot 18 to this handler, just as it does slots 16–19. A Win32 handle is an opaque numeric value, not a pointer to a 32-byte device ID, so the new memcmp dereferences the handle and causes an access violation. The parent handler did not dereference this argument; an isolated before/after harness succeeds against the parent and crashes against this change with a handle-shaped value of 0x4. Give the differently typed lookup methods separate handlers, returning an appropriate failure for unsupported lookups rather than treating every argument as an ID.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679499016 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679499016). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:45:33Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 522 ~~~~diff @@ -350,11 +513,17 @@ HRESULT __stdcall input_register_short_callback(Object*, Object*, void*, void*, } void __stdcall input_stop_callback(Object*, u64) {} bool __stdcall input_unregister_callback(Object*, u64, u64) { return true; } -HRESULT __stdcall input_find_device(Object*, const void*, Object** device) +HRESULT __stdcall input_find_device(Object*, const void* id, Object** device) { static LONG calls; note("GameInput::FindDeviceFrom*", calls); if (!device) return E_POINTER; + if (id && (std::memcmp(id, keyboard_device_info + 0x20, 32) == 0 || + std::memcmp(id, keyboard_device_info + 0x40, 32) == 0)) { ~~~~ --- **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Separate handle-based lookups from device ID comparisons** When a caller uses v0 `FindDeviceFromPlatformHandle`, `build()` routes slot 18 to this handler, just as it does slots 16–19. A Win32 handle is an opaque numeric value, not a pointer to a 32-byte device ID, so the new `memcmp` dereferences the handle and causes an access violation. The parent handler did not dereference this argument; an isolated before/after harness succeeds against the parent and crashes against this change with a handle-shaped value of `0x4`. Give the differently typed lookup methods separate handlers, returning an appropriate failure for unsupported lookups rather than treating every argument as an ID. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:45:33Z.

Code review location: src/startup_stubs/gameinput.cpp; line 459

@@ -304,19 +448,33 @@ HRESULT __stdcall input_next_reading(Object*, Reading* reference, std::uint32_t
     note("GameInput::GetNextReading", calls, reinterpret_cast<u64>(reference), kind);
     if (!out) return E_POINTER;
     *out = nullptr;
-    if (!wants_gamepad(kind) || (device && device != &the_device)) return reading_not_found;
-    Reading* now = current_reading();
-    if (reference && now->sequence <= reference->sequence) {
-        reading_release(now);
-        return reading_not_found;
-    }
-    *out = now;
+    if (!reference) return E_INVALIDARG;
+    const HRESULT check_hr = validate_kind_and_device(kind, device);
+    if (FAILED(check_hr)) return check_hr;
+    capture_current();
+    HistoryEntry entry{};
+    const int found = find_history(reference->sequence + 1, entry);
+    if (found == -2) return reading_too_old;
+    if (found != 0) return reading_not_found;
+    *out = make_reading(entry, kind, device);

P2 Badge Filter history traversal by the requested device and input kind

For a gamepad-only caller, changing only X2W_INPUT_KEYS advances the shared sequence in capture_current(), and this lookup then returns that keyboard-only change as a new gamepad reading. An isolated comparison with the parent reproduces S_OK and an advanced gamepad sequence despite identical gamepad state; after enough keyboard-only changes, the same gamepad reference instead produces reading_too_old. GetPreviousReading has the same unfiltered traversal. Preserve device/kind-specific history, or record which device changed and skip nonmatching entries, rather than relabeling every adjacent global snapshot as the requested input.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679499029 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679499029). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:45:33Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 459 ~~~~diff @@ -304,19 +448,33 @@ HRESULT __stdcall input_next_reading(Object*, Reading* reference, std::uint32_t note("GameInput::GetNextReading", calls, reinterpret_cast<u64>(reference), kind); if (!out) return E_POINTER; *out = nullptr; - if (!wants_gamepad(kind) || (device && device != &the_device)) return reading_not_found; - Reading* now = current_reading(); - if (reference && now->sequence <= reference->sequence) { - reading_release(now); - return reading_not_found; - } - *out = now; + if (!reference) return E_INVALIDARG; + const HRESULT check_hr = validate_kind_and_device(kind, device); + if (FAILED(check_hr)) return check_hr; + capture_current(); + HistoryEntry entry{}; + const int found = find_history(reference->sequence + 1, entry); + if (found == -2) return reading_too_old; + if (found != 0) return reading_not_found; + *out = make_reading(entry, kind, device); ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Filter history traversal by the requested device and input kind** For a gamepad-only caller, changing only `X2W_INPUT_KEYS` advances the shared sequence in `capture_current()`, and this lookup then returns that keyboard-only change as a new gamepad reading. An isolated comparison with the parent reproduces `S_OK` and an advanced gamepad sequence despite identical gamepad state; after enough keyboard-only changes, the same gamepad reference instead produces `reading_too_old`. `GetPreviousReading` has the same unfiltered traversal. Preserve device/kind-specific history, or record which device changed and skip nonmatching entries, rather than relabeling every adjacent global snapshot as the requested input. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:45:33Z.

Code review location: src/startup_stubs/gameinput.cpp; line 211

@@ -169,6 +182,38 @@ GamepadState sample()
     return s;
 }
 
+std::uint32_t sample_keys(KeyState* out, std::size_t capacity)
+{
+    std::uint32_t count = 0;
+    auto push_vk = [&](std::uint32_t vk) {
+        if (count >= capacity || vk == 0 || vk > 0xFE) return;
+        const auto vkey = static_cast<std::uint8_t>(vk);
+        for (std::uint32_t i = 0; i < count; ++i)
+            if (out[i].virtualKey == vkey) return;
+        KeyState ks{};
+        ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));
+        const UINT ch = MapVirtualKeyW(vk, MAPVK_VK_TO_CHAR);
+        if ((ch & 0x80000000u) == 0 && (ch & 0xFFFFu) != 0) ks.codePoint = ch & 0xFFFFu;
+        ks.virtualKey = vkey;
+        ks.isDeadKey = false;
+        out[count++] = ks;
+    };
+
+    char env[256]{};
+    if (GetEnvironmentVariableA("X2W_INPUT_KEYS", env, sizeof(env)) > 0) {
+        for (char* item = std::strtok(env, ", "); item && count < capacity; item = std::strtok(nullptr, ", ")) {
+            unsigned vk = 0;
+            if (std::sscanf(item, "%x", &vk) == 1) push_vk(vk);
+        }
+    }
+    if (process_has_focus()) {
+        for (std::uint32_t vk = 0x08; vk <= 0xFE && count < capacity; ++vk) {
+            if ((GetAsyncKeyState(static_cast<int>(vk)) & 0x8000) != 0) push_vk(vk);

P2 Badge Skip generic modifier aliases when sampling pressed keys

With a focused window and only left Shift held, both GetAsyncKeyState(VK_SHIFT) and GetAsyncKeyState(VK_LSHIFT) report the key as down. This loop appends both because push_vk deduplicates only identical virtual-key values, so one physical key becomes two KeyState entries; Ctrl and Alt have the same problem. This makes key counts and physical-key/chord tracking incorrect. Poll only the side-specific modifier codes, or normalize the generic aliases before appending them.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679499034 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679499034). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:45:33Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 211 ~~~~diff @@ -169,6 +182,38 @@ GamepadState sample() return s; } +std::uint32_t sample_keys(KeyState* out, std::size_t capacity) +{ + std::uint32_t count = 0; + auto push_vk = [&](std::uint32_t vk) { + if (count >= capacity || vk == 0 || vk > 0xFE) return; + const auto vkey = static_cast<std::uint8_t>(vk); + for (std::uint32_t i = 0; i < count; ++i) + if (out[i].virtualKey == vkey) return; + KeyState ks{}; + ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC)); + const UINT ch = MapVirtualKeyW(vk, MAPVK_VK_TO_CHAR); + if ((ch & 0x80000000u) == 0 && (ch & 0xFFFFu) != 0) ks.codePoint = ch & 0xFFFFu; + ks.virtualKey = vkey; + ks.isDeadKey = false; + out[count++] = ks; + }; + + char env[256]{}; + if (GetEnvironmentVariableA("X2W_INPUT_KEYS", env, sizeof(env)) > 0) { + for (char* item = std::strtok(env, ", "); item && count < capacity; item = std::strtok(nullptr, ", ")) { + unsigned vk = 0; + if (std::sscanf(item, "%x", &vk) == 1) push_vk(vk); + } + } + if (process_has_focus()) { + for (std::uint32_t vk = 0x08; vk <= 0xFE && count < capacity; ++vk) { + if ((GetAsyncKeyState(static_cast<int>(vk)) & 0x8000) != 0) push_vk(vk); ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Skip generic modifier aliases when sampling pressed keys** With a focused window and only left Shift held, both `GetAsyncKeyState(VK_SHIFT)` and `GetAsyncKeyState(VK_LSHIFT)` report the key as down. This loop appends both because `push_vk` deduplicates only identical virtual-key values, so one physical key becomes two `KeyState` entries; Ctrl and Alt have the same problem. This makes key counts and physical-key/chord tracking incorrect. Poll only the side-specific modifier codes, or normalize the generic aliases before appending them. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
chatgpt-codex-connector[bot commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T11:45:33Z.

Code review location: src/startup_stubs/gameinput.cpp; line 194

@@ -169,6 +182,38 @@ GamepadState sample()
     return s;
 }
 
+std::uint32_t sample_keys(KeyState* out, std::size_t capacity)
+{
+    std::uint32_t count = 0;
+    auto push_vk = [&](std::uint32_t vk) {
+        if (count >= capacity || vk == 0 || vk > 0xFE) return;
+        const auto vkey = static_cast<std::uint8_t>(vk);
+        for (std::uint32_t i = 0; i < count; ++i)
+            if (out[i].virtualKey == vkey) return;
+        KeyState ks{};
+        ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));

P2 Badge Preserve extended prefixes in keyboard scan codes

For an extended key such as Left Arrow (VK_LEFT, also injectable with X2W_INPUT_KEYS=25), MAPVK_VK_TO_VSC returns 0x4B without the E0 prefix, making its scan code indistinguishable from keypad 4. Right Ctrl and right Alt similarly lose their extended prefixes. Scan-code-based navigation and modifier bindings therefore receive the wrong key identity; the existing keyboard fixture in tests/cpu/gameinput_provider.cpp explicitly represents Left Arrow as 0xE04B. Use the extended scan-code mapping and preserve the prefix in the returned KeyState.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679499047 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679499047). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T11:45:33Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 194 ~~~~diff @@ -169,6 +182,38 @@ GamepadState sample() return s; } +std::uint32_t sample_keys(KeyState* out, std::size_t capacity) +{ + std::uint32_t count = 0; + auto push_vk = [&](std::uint32_t vk) { + if (count >= capacity || vk == 0 || vk > 0xFE) return; + const auto vkey = static_cast<std::uint8_t>(vk); + for (std::uint32_t i = 0; i < count; ++i) + if (out[i].virtualKey == vkey) return; + KeyState ks{}; + ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC)); ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve extended prefixes in keyboard scan codes** For an extended key such as Left Arrow (`VK_LEFT`, also injectable with `X2W_INPUT_KEYS=25`), `MAPVK_VK_TO_VSC` returns `0x4B` without the `E0` prefix, making its scan code indistinguishable from keypad 4. Right Ctrl and right Alt similarly lose their extended prefixes. Scan-code-based navigation and modifier bindings therefore receive the wrong key identity; the existing keyboard fixture in `tests/cpu/gameinput_provider.cpp` explicitly represents Left Arrow as `0xE04B`. Use the extended scan-code mapping and preserve the prefix in the returned `KeyState`. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
tedin7 commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:38Z.

Code review location: src/startup_stubs/gameinput.cpp; line 522

@@ -350,11 +513,17 @@ HRESULT __stdcall input_register_short_callback(Object*, Object*, void*, void*,
 }
 void __stdcall input_stop_callback(Object*, u64) {}
 bool __stdcall input_unregister_callback(Object*, u64, u64) { return true; }
-HRESULT __stdcall input_find_device(Object*, const void*, Object** device)
+HRESULT __stdcall input_find_device(Object*, const void* id, Object** device)
 {
     static LONG calls;
     note("GameInput::FindDeviceFrom*", calls);
     if (!device) return E_POINTER;
+    if (id && (std::memcmp(id, keyboard_device_info + 0x20, 32) == 0 ||
+               std::memcmp(id, keyboard_device_info + 0x40, 32) == 0)) {

Fixed in 054c159d: separate handlers: FindDeviceFromId (slot 16) compares IDs; FindDeviceFromPlatformHandle (slot 18) never dereferences the handle and returns device_not_found; Object/PlatformString keep the old behavior. Test: handle 0x4 + id lookup.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679553520 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679553520). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:38Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 522 ~~~~diff @@ -350,11 +513,17 @@ HRESULT __stdcall input_register_short_callback(Object*, Object*, void*, void*, } void __stdcall input_stop_callback(Object*, u64) {} bool __stdcall input_unregister_callback(Object*, u64, u64) { return true; } -HRESULT __stdcall input_find_device(Object*, const void*, Object** device) +HRESULT __stdcall input_find_device(Object*, const void* id, Object** device) { static LONG calls; note("GameInput::FindDeviceFrom*", calls); if (!device) return E_POINTER; + if (id && (std::memcmp(id, keyboard_device_info + 0x20, 32) == 0 || + std::memcmp(id, keyboard_device_info + 0x40, 32) == 0)) { ~~~~ --- Fixed in 054c159d: separate handlers: FindDeviceFromId (slot 16) compares IDs; FindDeviceFromPlatformHandle (slot 18) never dereferences the handle and returns device_not_found; Object/PlatformString keep the old behavior. Test: handle 0x4 + id lookup.
tedin7 commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:41Z.

Code review location: src/startup_stubs/gameinput.cpp; line 459

@@ -304,19 +448,33 @@ HRESULT __stdcall input_next_reading(Object*, Reading* reference, std::uint32_t
     note("GameInput::GetNextReading", calls, reinterpret_cast<u64>(reference), kind);
     if (!out) return E_POINTER;
     *out = nullptr;
-    if (!wants_gamepad(kind) || (device && device != &the_device)) return reading_not_found;
-    Reading* now = current_reading();
-    if (reference && now->sequence <= reference->sequence) {
-        reading_release(now);
-        return reading_not_found;
-    }
-    *out = now;
+    if (!reference) return E_INVALIDARG;
+    const HRESULT check_hr = validate_kind_and_device(kind, device);
+    if (FAILED(check_hr)) return check_hr;
+    capture_current();
+    HistoryEntry entry{};
+    const int found = find_history(reference->sequence + 1, entry);
+    if (found == -2) return reading_too_old;
+    if (found != 0) return reading_not_found;
+    *out = make_reading(entry, kind, device);

Fixed in 054c159d: history entries now record which device changed; GetNextReading/GetPreviousReading skip entries that did not change the requested device/kind. Test: keyboard-only changes invisible to gamepad callers and vice versa.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679554032 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679554032). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:41Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 459 ~~~~diff @@ -304,19 +448,33 @@ HRESULT __stdcall input_next_reading(Object*, Reading* reference, std::uint32_t note("GameInput::GetNextReading", calls, reinterpret_cast<u64>(reference), kind); if (!out) return E_POINTER; *out = nullptr; - if (!wants_gamepad(kind) || (device && device != &the_device)) return reading_not_found; - Reading* now = current_reading(); - if (reference && now->sequence <= reference->sequence) { - reading_release(now); - return reading_not_found; - } - *out = now; + if (!reference) return E_INVALIDARG; + const HRESULT check_hr = validate_kind_and_device(kind, device); + if (FAILED(check_hr)) return check_hr; + capture_current(); + HistoryEntry entry{}; + const int found = find_history(reference->sequence + 1, entry); + if (found == -2) return reading_too_old; + if (found != 0) return reading_not_found; + *out = make_reading(entry, kind, device); ~~~~ --- Fixed in 054c159d: history entries now record which device changed; GetNextReading/GetPreviousReading skip entries that did not change the requested device/kind. Test: keyboard-only changes invisible to gamepad callers and vice versa.
tedin7 commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:44Z.

Code review location: src/startup_stubs/gameinput.cpp; line 211

@@ -169,6 +182,38 @@ GamepadState sample()
     return s;
 }
 
+std::uint32_t sample_keys(KeyState* out, std::size_t capacity)
+{
+    std::uint32_t count = 0;
+    auto push_vk = [&](std::uint32_t vk) {
+        if (count >= capacity || vk == 0 || vk > 0xFE) return;
+        const auto vkey = static_cast<std::uint8_t>(vk);
+        for (std::uint32_t i = 0; i < count; ++i)
+            if (out[i].virtualKey == vkey) return;
+        KeyState ks{};
+        ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));
+        const UINT ch = MapVirtualKeyW(vk, MAPVK_VK_TO_CHAR);
+        if ((ch & 0x80000000u) == 0 && (ch & 0xFFFFu) != 0) ks.codePoint = ch & 0xFFFFu;
+        ks.virtualKey = vkey;
+        ks.isDeadKey = false;
+        out[count++] = ks;
+    };
+
+    char env[256]{};
+    if (GetEnvironmentVariableA("X2W_INPUT_KEYS", env, sizeof(env)) > 0) {
+        for (char* item = std::strtok(env, ", "); item && count < capacity; item = std::strtok(nullptr, ", ")) {
+            unsigned vk = 0;
+            if (std::sscanf(item, "%x", &vk) == 1) push_vk(vk);
+        }
+    }
+    if (process_has_focus()) {
+        for (std::uint32_t vk = 0x08; vk <= 0xFE && count < capacity; ++vk) {
+            if ((GetAsyncKeyState(static_cast<int>(vk)) & 0x8000) != 0) push_vk(vk);

Fixed in 054c159d: VK_SHIFT/VK_CONTROL/VK_MENU aliases are never reported; only side-specific keys remain. Test: X2W_INPUT_KEYS=10,11,12,A0,A2 yields 2 keys.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679554542 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679554542). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:44Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 211 ~~~~diff @@ -169,6 +182,38 @@ GamepadState sample() return s; } +std::uint32_t sample_keys(KeyState* out, std::size_t capacity) +{ + std::uint32_t count = 0; + auto push_vk = [&](std::uint32_t vk) { + if (count >= capacity || vk == 0 || vk > 0xFE) return; + const auto vkey = static_cast<std::uint8_t>(vk); + for (std::uint32_t i = 0; i < count; ++i) + if (out[i].virtualKey == vkey) return; + KeyState ks{}; + ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC)); + const UINT ch = MapVirtualKeyW(vk, MAPVK_VK_TO_CHAR); + if ((ch & 0x80000000u) == 0 && (ch & 0xFFFFu) != 0) ks.codePoint = ch & 0xFFFFu; + ks.virtualKey = vkey; + ks.isDeadKey = false; + out[count++] = ks; + }; + + char env[256]{}; + if (GetEnvironmentVariableA("X2W_INPUT_KEYS", env, sizeof(env)) > 0) { + for (char* item = std::strtok(env, ", "); item && count < capacity; item = std::strtok(nullptr, ", ")) { + unsigned vk = 0; + if (std::sscanf(item, "%x", &vk) == 1) push_vk(vk); + } + } + if (process_has_focus()) { + for (std::uint32_t vk = 0x08; vk <= 0xFE && count < capacity; ++vk) { + if ((GetAsyncKeyState(static_cast<int>(vk)) & 0x8000) != 0) push_vk(vk); ~~~~ --- Fixed in 054c159d: VK_SHIFT/VK_CONTROL/VK_MENU aliases are never reported; only side-specific keys remain. Test: X2W_INPUT_KEYS=10,11,12,A0,A2 yields 2 keys.
tedin7 commented 2026-11-24 12:03:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T11:50:47Z.

Code review location: src/startup_stubs/gameinput.cpp; line 194

@@ -169,6 +182,38 @@ GamepadState sample()
     return s;
 }
 
+std::uint32_t sample_keys(KeyState* out, std::size_t capacity)
+{
+    std::uint32_t count = 0;
+    auto push_vk = [&](std::uint32_t vk) {
+        if (count >= capacity || vk == 0 || vk > 0xFE) return;
+        const auto vkey = static_cast<std::uint8_t>(vk);
+        for (std::uint32_t i = 0; i < count; ++i)
+            if (out[i].virtualKey == vkey) return;
+        KeyState ks{};
+        ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC));

Fixed in 054c159d: scan codes use MAPVK_VK_TO_VSC_EX and the navigation cluster is prefixed with E0 (Left Arrow 0xE04B vs Numpad4 0x4B, RCtrl 0xE01D, RAlt 0xE038). Test added.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4679555289 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#discussion_r4679555289). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T11:50:47Z. **Code review location:** `src/startup_stubs/gameinput.cpp`; line 194 ~~~~diff @@ -169,6 +182,38 @@ GamepadState sample() return s; } +std::uint32_t sample_keys(KeyState* out, std::size_t capacity) +{ + std::uint32_t count = 0; + auto push_vk = [&](std::uint32_t vk) { + if (count >= capacity || vk == 0 || vk > 0xFE) return; + const auto vkey = static_cast<std::uint8_t>(vk); + for (std::uint32_t i = 0; i < count; ++i) + if (out[i].virtualKey == vkey) return; + KeyState ks{}; + ks.scanCode = static_cast<std::uint32_t>(MapVirtualKeyW(vk, MAPVK_VK_TO_VSC)); ~~~~ --- Fixed in 054c159d: scan codes use MAPVK_VK_TO_VSC_EX and the navigation cluster is prefixed with E0 (Left Arrow 0xE04B vs Numpad4 0x4B, RCtrl 0xE01D, RAlt 0xE038). Test added.
upstream-sync closed this pull request 2026-11-24 13:00:25 +00:00
R0Y00 commented 2026-11-24 13:00:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T12:09:11Z.

Review: CHANGES_REQUESTED


The device lookup split and key fixes address the earlier findings. One history compatibility case remains on 054c159d. GetSequenceNumber still returns the shared event counter for every kind, so a keyboard-only change increments a gamepad reading's number, even though Next now skips that entry. The shared 64-entry ring also lets only keyboard events age out an otherwise current gamepad reference. Please keep an internal ordering sequence separate from per-kind sequence/history retention (or limit the first PR to keyboard sampling). Add one control showing the existing gamepad sequence is unchanged after repeated keyboard-only reads. GameInput's documented sequence is per kind: https://learn.microsoft.com/en-us/xbox/gdk/docs/reference/input/gameinput/interfaces/igameinputreading/methods/igameinputreading_getsequencenumber

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038925071 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6038925071). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T12:09:11Z. **Review:** CHANGES_REQUESTED --- The device lookup split and key fixes address the earlier findings. One history compatibility case remains on 054c159d. GetSequenceNumber still returns the shared event counter for every kind, so a keyboard-only change increments a gamepad reading's number, even though Next now skips that entry. The shared 64-entry ring also lets only keyboard events age out an otherwise current gamepad reference. Please keep an internal ordering sequence separate from per-kind sequence/history retention (or limit the first PR to keyboard sampling). Add one control showing the existing gamepad sequence is unchanged after repeated keyboard-only reads. GameInput's documented sequence is per kind: https://learn.microsoft.com/en-us/xbox/gdk/docs/reference/input/gameinput/interfaces/igameinputreading/methods/igameinputreading_getsequencenumber
R0Y00 commented 2026-11-24 13:00:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T12:36:55Z.

Review: APPROVED


Windows Release startup_gameinput_keys passed on this revision. From a gamepad event, the previous keyboard result is the nearest earlier keyboard event. From that keyboard event, previous steps back one entry. Preserved the per-kind eviction control. Microsoft GameInput stream contract: https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getpreviousreading

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6039302260 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#pullrequestreview-6039302260). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T12:36:55Z. **Review:** APPROVED --- Windows Release startup_gameinput_keys passed on this revision. From a gamepad event, the previous keyboard result is the nearest earlier keyboard event. From that keyboard event, previous steps back one entry. Preserved the per-kind eviction control. Microsoft GameInput stream contract: https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getpreviousreading
tedin7 commented 2026-11-24 13:00:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T12:20:52Z.


@R0Y00 Fixed in 9e056e3c (sequence change in 06edf01a): each reading now keeps an internal ordering counter separate from the per-kind sequence numbers. GetSequenceNumber returns the gamepad or keyboard sequence for the requested kind, and each kind has its own 64-entry retention ring, so keyboard events can no longer advance a gamepad sequence or age out a gamepad reference. Next/Previous pick the nearest entry of the wanted kind(s) by the internal order. Tests added in startup_gameinput_keys: the gamepad sequence is unchanged after repeated keyboard-only reads, and a gamepad reference survives 100 keyboard-only events (GetNextReading is not_found, not too_old). Win2025 Debug and Release /O2: startup_gameinput_keys 1/1 passed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7131311480 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1436#issuecomment-7131311480). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T12:20:52Z. --- @R0Y00 Fixed in 9e056e3c (sequence change in 06edf01a): each reading now keeps an internal ordering counter separate from the per-kind sequence numbers. GetSequenceNumber returns the gamepad or keyboard sequence for the requested kind, and each kind has its own 64-entry retention ring, so keyboard events can no longer advance a gamepad sequence or age out a gamepad reference. Next/Previous pick the nearest entry of the wanted kind(s) by the internal order. Tests added in startup_gameinput_keys: the gamepad sequence is unchanged after repeated keyboard-only reads, and a gamepad reference survives 100 keyboard-only events (GetNextReading is not_found, not too_old). Win2025 Debug and Release /O2: startup_gameinput_keys 1/1 passed.
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
R0Y00/Xbox2Windows!1429
No description provided.