From cd72ef8ec3192205d028a9587e94d3d40e4a0eb2 Mon Sep 17 00:00:00 2001 From: ates Date: Thu, 26 Nov 2026 23:47:28 +0300 Subject: [PATCH] [M2-E3] gameinput: implement device-callback lifecycle and SetFocusPolicy (#481) Signed-off-by: ates --- CHANGELOG.md | 1 + shims/gameinput/gameinput.cpp | 80 ++++++++++++++++++--- tests/conformance/test_audio_input/main.cpp | 40 ++++++++++- 3 files changed, 110 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b16cd7b..657c2a96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game remains unsupported. ### Added +- Implemented the `IGameInput` v0 device-callback lifecycle (`RegisterDeviceCallback`, `StopCallback`, `UnregisterCallback`) and `SetFocusPolicy` in `shims/gameinput/gameinput.cpp` (#481). `RegisterDeviceCallback` validates `callbackFunc`, `inputKind`, `statusFilter`, and `enumerationKind` (`0..=2`), allocates unique monotonic `GameInputCallbackToken` handles under `SRWLOCK`, and supports optional null `callbackToken` output pointers; `StopCallback` marks active registrations stopped while keeping them unregisterable once via `UnregisterCallback`; `SetFocusPolicy` records focus-policy flags without stub noise. Expanded `test_audio_input.exe` to cover validation, token uniqueness, stop/unregister idempotence, and focus-policy calls. - `dxbc-accc` now reaches Windows guests as `bx_shader_bridge.dll`, the x86-64 Windows build of the same crate (#445). It exports exactly the three `bx_shader_bridge.h` entry points and needs no MinGW runtime DLL. `shader-transpiler/crates/dxbc-accc/guest/build.sh` builds the DLL and checks its exports and imports. It also builds `check_bridge.exe`, a C guest program compiled against the frozen header, which sends a real DXC SM 6.6 blob and eight synthetic containers (DXIL with and without trailing bytes, `ACCC`/`FSPX` v1 and v0, dual, legacy `SHEX`, malformed) through the DLL loaded by full path. It checks classification (exact chunk sizes, root signature flag, header digest, declared size), prepare (only the declared container is copied), free, and prints a DXIL vs. `ACCC` count. The build script always uses `/target` and rustup's toolchain, and checks the whole Export Address Table and an import allowlist. On one Apple M5 Pro with CrossOver 26.3, in a prefix and a D3DMetal bottle, it reported `dxil=3 accc_only=2 dual=1 legacy=1 invalid=2` and passed. Synthetic containers only. Staging the DLL in `build-shims`, an `abi/defs` export list, and the `d3d12_xs` PSO hook (#466) are separate changes. - `xgameruntime.dll!QueryApiImpl` now serves offline `XGameSave` and `XPackage` as COM `IUnknown` objects under BetterXen synthetic keys (#468, not yet closed). A title linked against Microsoft's `xgameruntime.lib` does not request these keys. Every synthetic key and COM slot order now lives in `shims/xgameruntime/synthetic_keys.h`, the single place to find or replace them. Signatures, structure layouts, `E_GS_*` values and container-name rules follow the public Microsoft Learn reference. - **XGameSave:** provider init, containers, updates, blob writes, submit and read (both asynchronous), enumeration and the close calls. Saves are separated by SCID: each container is one file under `C:\bx_pls\\saves\\`, replaced atomically on submit. A named mutex per SCID directory serializes access across threads and processes. Damaged files (truncated, trailing bytes, duplicate or NUL-bearing names) read as `ERROR_FILE_CORRUPT`, and enumeration skips them and unreadable files with a log line. Offline choices where the reference is silent (16 MiB including blob names, 256-byte blob names, no sync, retry after a failed submit) are listed in `game_save.cpp`. diff --git a/shims/gameinput/gameinput.cpp b/shims/gameinput/gameinput.cpp index dea10417..b2f860c0 100644 --- a/shims/gameinput/gameinput.cpp +++ b/shims/gameinput/gameinput.cpp @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include "bx_log.h" @@ -10,6 +12,30 @@ constexpr GUID game_input_v0 = {0x11be2a7e, 0x4254, 0x445a, {0x9c, 0x09, 0xff, 0xc4, 0x0f, 0x00, 0x69, 0x18}}; using Callback = void (WINAPI*)(); +// GameInputEnumerationKind values from Microsoft's public GDK GameInput (v0) reference: +// 0 = GameInputNoEnumeration, 1 = GameInputAsyncEnumeration, 2 = GameInputBlockingEnumeration. +constexpr uint32_t kMaxEnumerationKind = 2; + +struct SrwExclusiveLock { + explicit SrwExclusiveLock(SRWLOCK& lock) : lock_(lock) { AcquireSRWLockExclusive(&lock_); } + ~SrwExclusiveLock() { ReleaseSRWLockExclusive(&lock_); } + SrwExclusiveLock(const SrwExclusiveLock&) = delete; + SrwExclusiveLock& operator=(const SrwExclusiveLock&) = delete; + +private: + SRWLOCK& lock_; +}; + +struct RegisteredDeviceCallback { + void* device = nullptr; + uint32_t input_kind = 0; + uint32_t status_filter = 0; + uint32_t enumeration_kind = 0; + void* context = nullptr; + Callback callback = nullptr; + bool stopped = false; +}; + template HRESULT unavailable(T* out) { if (!out) return E_POINTER; @@ -19,6 +45,11 @@ HRESULT unavailable(T* out) { class GameInput final : public IUnknown { LONG refs = 0; + SRWLOCK callback_lock_ = SRWLOCK_INIT; + uint64_t next_token_ = 1; + std::unordered_map callbacks_; + std::atomic focus_policy_{0}; + public: HRESULT WINAPI QueryInterface(REFIID iid, void** out) override { if (!out) return E_POINTER; @@ -54,9 +85,32 @@ public: BX_VTABLE_STUB_LOG("IGameInput", 8, "RegisterReadingCallback"); return unavailable(out); } - virtual HRESULT WINAPI RegisterDeviceCallback(void*, uint32_t, uint32_t, uint32_t, void*, Callback, uint64_t* out) { - BX_VTABLE_STUB_LOG("IGameInput", 9, "RegisterDeviceCallback"); - return unavailable(out); + virtual HRESULT WINAPI RegisterDeviceCallback( + void* device, + uint32_t input_kind, + uint32_t status_filter, + uint32_t enumeration_kind, + void* context, + Callback callback, + uint64_t* out_token + ) { + if (out_token) *out_token = 0; + if (!callback || input_kind == 0 || status_filter == 0 || enumeration_kind > kMaxEnumerationKind) { + return E_INVALIDARG; + } + SrwExclusiveLock guard(callback_lock_); + const uint64_t token = next_token_++; + callbacks_.emplace(token, RegisteredDeviceCallback{ + device, + input_kind, + status_filter, + enumeration_kind, + context, + callback, + false, + }); + if (out_token) *out_token = token; + return S_OK; } virtual HRESULT WINAPI RegisterSystemButtonCallback(void*, uint32_t, void*, Callback, uint64_t* out) { BX_VTABLE_STUB_LOG("IGameInput", 10, "RegisterSystemButtonCallback"); @@ -66,12 +120,18 @@ public: BX_VTABLE_STUB_LOG("IGameInput", 11, "RegisterKeyboardLayoutCallback"); return unavailable(out); } - virtual void WINAPI StopCallback(uint64_t) { - BX_VTABLE_STUB_LOG("IGameInput", 12, "StopCallback"); + virtual void WINAPI StopCallback(uint64_t token) { + if (token == 0) return; + SrwExclusiveLock guard(callback_lock_); + auto it = callbacks_.find(token); + if (it != callbacks_.end()) { + it->second.stopped = true; + } } - virtual bool WINAPI UnregisterCallback(uint64_t, uint64_t) { - BX_VTABLE_STUB_LOG("IGameInput", 13, "UnregisterCallback"); - return false; + virtual bool WINAPI UnregisterCallback(uint64_t token, uint64_t /*timeout_us*/) { + if (token == 0) return false; + SrwExclusiveLock guard(callback_lock_); + return callbacks_.erase(token) > 0; } virtual HRESULT WINAPI CreateDispatcher(void** out) { BX_VTABLE_STUB_LOG("IGameInput", 14, "CreateDispatcher"); @@ -101,8 +161,8 @@ public: BX_VTABLE_STUB_LOG("IGameInput", 20, "EnableOemDeviceSupport"); return E_NOTIMPL; } - virtual void WINAPI SetFocusPolicy(uint32_t) { - BX_VTABLE_STUB_LOG("IGameInput", 21, "SetFocusPolicy"); + virtual void WINAPI SetFocusPolicy(uint32_t policy) { + focus_policy_.store(policy, std::memory_order_relaxed); } }; diff --git a/tests/conformance/test_audio_input/main.cpp b/tests/conformance/test_audio_input/main.cpp index f2550155..ae094ff0 100644 --- a/tests/conformance/test_audio_input/main.cpp +++ b/tests/conformance/test_audio_input/main.cpp @@ -27,6 +27,10 @@ HRESULT WINAPI XDspDisconnect(void*); #define CHECK(expr) do { if (!(expr)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #expr); return 1; } } while (0) +namespace { +void WINAPI dummy_device_callback(uint64_t, void*, void*, uint64_t, uint32_t, uint32_t) {} +} // namespace + int main() { void* object = nullptr; CHECK(AcpHalCreate(&object) == S_OK && object); @@ -148,8 +152,42 @@ int main() { using Reading = HRESULT (WINAPI*)(IUnknown*, uint32_t, void*, void**); void* reading = reinterpret_cast(uintptr_t{1}); CHECK(reinterpret_cast(table[4])(v0, 0, nullptr, &reading) == E_NOTIMPL && !reading); + + using RegisterDeviceCb = HRESULT (WINAPI*)( + IUnknown*, void*, uint32_t, uint32_t, uint32_t, void*, void*, uint64_t*); + using StopCb = void (WINAPI*)(IUnknown*, uint64_t); + using UnregisterCb = bool (WINAPI*)(IUnknown*, uint64_t, uint64_t); + using SetFocusPolicyFn = void (WINAPI*)(IUnknown*, uint32_t); + auto reg_dev = reinterpret_cast(table[9]); + auto stop_cb = reinterpret_cast(table[12]); + auto unreg_cb = reinterpret_cast(table[13]); + auto set_focus = reinterpret_cast(table[21]); + void* cb_fn = reinterpret_cast(&dummy_device_callback); + + uint64_t tok1 = 99, tok2 = 0; + CHECK(reg_dev(v0, nullptr, 1, 1, 0, nullptr, nullptr, &tok1) == E_INVALIDARG && tok1 == 0); + tok1 = 99; + CHECK(reg_dev(v0, nullptr, 0, 1, 0, nullptr, cb_fn, &tok1) == E_INVALIDARG && tok1 == 0); + tok1 = 99; + CHECK(reg_dev(v0, nullptr, 1, 0, 0, nullptr, cb_fn, &tok1) == E_INVALIDARG && tok1 == 0); + tok1 = 99; + CHECK(reg_dev(v0, nullptr, 1, 1, 3, nullptr, cb_fn, &tok1) == E_INVALIDARG && tok1 == 0); + + CHECK(reg_dev(v0, nullptr, 0x10000, 0x1, 2, &tok1, cb_fn, &tok1) == S_OK && tok1 != 0); + CHECK(reg_dev(v0, nullptr, 0x10000, 0x1, 1, nullptr, cb_fn, &tok2) == S_OK && tok2 > tok1); + CHECK(reg_dev(v0, nullptr, 0x10000, 0x1, 0, nullptr, cb_fn, nullptr) == S_OK); + + CHECK(!unreg_cb(v0, 0, 5000)); + CHECK(!unreg_cb(v0, 0xdeadbeefULL, 5000)); + stop_cb(v0, tok1); + CHECK(unreg_cb(v0, tok1, 5000)); + CHECK(!unreg_cb(v0, tok1, 5000)); + CHECK(unreg_cb(v0, tok2, 0)); + CHECK(!unreg_cb(v0, tok2, 0)); + set_focus(v0, 0x2); + CHECK(v0->Release() == 1); CHECK(input->Release() == 0); FreeLibrary(dll); - std::puts("PASS: audio mappings, APU and DSP queues, GameInput ordinal 1"); + std::puts("PASS: audio mappings, APU and DSP queues, GameInput ordinal 1 and device callbacks"); }