From d93d8ed0ec466d2ff7d2205209d2f008a6ef4f22 Mon Sep 17 00:00:00 2001 From: Salim Hammami Date: Tue, 24 Nov 2026 00:28:40 +0100 Subject: [PATCH] xgameruntime: XUserFindForDevice answers E_GAMEUSER_USER_NOT_FOUND until XUserAddAsync (RT-382) The GDK reference says XUserFindForDevice cannot return a user handle to a user that was not signed in with a call to XUserAddAsync. Track whether the offline user was added (XUserAddAsync, or the local user by id) and answer E_GAMEUSER_USER_NOT_FOUND with no handle before that. The HRESULT is an assumption: the reference names no code. docs/runtime_imports.md updated; the offline test asks before and after the add. Co-Authored-By: Claude Sonnet 5.5 # Conflicts: # docs/runtime_imports.md # runtime/test/xgameruntime_offline_test.cpp --- docs/runtime_imports.md | 7 +++++-- runtime/shims/xgameruntime_offline.cpp | 10 +++++++++- runtime/shims/xgameruntime_user.cpp | 8 ++++++++ runtime/test/xgameruntime_offline_test.cpp | 22 ++++++++++++++++++---- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md index 0d82077815..9e2576933b 100644 --- a/docs/runtime_imports.md +++ b/docs/runtime_imports.md @@ -471,8 +471,11 @@ on the executable, the rest is not: reference; only the argument counts were measured, from `tools/gdk_runtime_calls.py` on the executable. - **HDR:** `XDisplayTryEnableHdrMode` answers Disabled, because the host output is SDR. With `LEONIDA_HDR=1` it answers Enabled with the documented default tone-map luminances (0.01, 1000, 1000 nits). -- **Devices:** every device belongs to the offline user (`XUserFindForDevice` hands out an `XUserHandle` the title - closes). `XUserRegisterForDeviceAssociationChanged` (slot 4) and +- **Devices:** every device belongs to the offline user, once the title has added it: `XUserFindForDevice` hands out + an `XUserHandle` the title closes only after `XUserAddAsync` (or the add-by-id call for the local user) has + completed. Before that it answers `E_GAMEUSER_USER_NOT_FOUND` and writes no handle, because the reference says it + "cannot return a user handle to a user that hasn't been signed in with a call to `XUserAddAsync`" (RT-382). + **ASSUMPTION:** that HRESULT; the reference page names no code for this case. `XUserRegisterForDeviceAssociationChanged` (slot 4) and `XUserRegisterForDefaultAudioEndpointUtf16Changed` (7) keep each registration (its queue, duplicated, its context and callback) under a token unique across both kinds and never 0; a null callback or token is `E_INVALIDARG`, and queue and context are optional. The matching unregister (5, 8) removes it at once, with or without `wait`, and diff --git a/runtime/shims/xgameruntime_offline.cpp b/runtime/shims/xgameruntime_offline.cpp index 23a5e9d81c..35b0a2650d 100644 --- a/runtime/shims/xgameruntime_offline.cpp +++ b/runtime/shims/xgameruntime_offline.cpp @@ -45,6 +45,7 @@ namespace rt::shims { void* xgameruntime_user_new_handle(); // xgameruntime_user.cpp +bool xgameruntime_user_was_added(); // xgameruntime_user.cpp: the title has called XUserAddAsync (RT-382) } namespace { @@ -53,6 +54,7 @@ using XUserHandle = void*; // XGameErr.h values, from the GDK "Error codes" page. constexpr HRESULT E_GAMESTREAMING_NOT_INITIALIZED = static_cast(0x89245400); +constexpr HRESULT E_GAMEUSER_USER_NOT_FOUND = static_cast(0x89245104); constexpr GUID kIUnknown = {0x00000000, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}; @@ -450,9 +452,15 @@ struct APP_LOCAL_DEVICE_ID { const char kFindControllerIdentity = 0; // 3: HRESULT XUserFindForDevice(const APP_LOCAL_DEVICE_ID* deviceId, XUserHandle* handle): every device belongs to -// the one offline user. +// the one offline user, once the title has added it. The reference says the call "cannot return a user handle to a +// user that hasn't been signed in with a call to XUserAddAsync", so before that it answers +// E_GAMEUSER_USER_NOT_FOUND and writes no handle (RT-382). +// ASSUMPTION: that HRESULT. The reference page names no code for this case; E_GAMEUSER_USER_NOT_FOUND is the +// documented XUser code closest to "no such user" and is the one the rest of the user shim answers with. HRESULT STDMETHODCALLTYPE device_find_user(void*, const APP_LOCAL_DEVICE_ID* device, XUserHandle* out) { if (!device || !out) return E_INVALIDARG; + *out = nullptr; + if (!rt::shims::xgameruntime_user_was_added()) return E_GAMEUSER_USER_NOT_FOUND; *out = rt::shims::xgameruntime_user_new_handle(); return S_OK; } diff --git a/runtime/shims/xgameruntime_user.cpp b/runtime/shims/xgameruntime_user.cpp index e323956463..5f26b79956 100644 --- a/runtime/shims/xgameruntime_user.cpp +++ b/runtime/shims/xgameruntime_user.cpp @@ -91,6 +91,9 @@ struct XUserLocal { uint32_t unused; }; std::mutex g_handles_mutex; +// Set once the title has added the offline user (XUserAddAsync, or by id). XUserFindForDevice hands out the user only +// after that: the reference says it cannot return a user that was not signed in with a call to XUserAddAsync (RT-382). +std::atomic g_user_added{false}; std::unordered_set g_handles; XUserHandle new_handle() { @@ -152,6 +155,7 @@ HRESULT CALLBACK add_provider(XAsyncOp op, const XAsyncProviderData* data) { case XAsyncOp::Begin: return XAsyncSchedule(data->async, 0); case XAsyncOp::DoWork: + g_user_added.store(true); // from here XUserFindForDevice may answer with the user (RT-382) XAsyncComplete(data->async, S_OK, sizeof(XUserHandle)); return S_OK; case XAsyncOp::GetResult: @@ -171,6 +175,7 @@ HRESULT CALLBACK add_by_id_provider(XAsyncOp op, const XAsyncProviderData* data) return XAsyncSchedule(data->async, 0); case XAsyncOp::DoWork: { const bool known = reinterpret_cast(data->context) == static_cast(kOfflineXuid); + if (known) g_user_added.store(true); // adding the local user by id also signs it in (RT-382) XAsyncComplete(data->async, known ? S_OK : E_GAMEUSER_USER_NOT_FOUND, known ? sizeof(XUserHandle) : 0); return S_OK; } @@ -535,6 +540,9 @@ bool xgameruntime_user_query(const GUID& class_id, REFIID interface_id, void** o // xgameruntime_offline.cpp). The title closes it with XUserCloseHandle like any other. void* xgameruntime_user_new_handle() { return new_handle(); } +// Whether the title has added the offline user yet; XUserFindForDevice (xgameruntime_offline.cpp) asks (RT-382). +bool xgameruntime_user_was_added() { return g_user_added.load(); } + // For tests: the XUserHandles a title holds open. long xgameruntime_user_open_handles() { std::lock_guard lock(g_handles_mutex); diff --git a/runtime/test/xgameruntime_offline_test.cpp b/runtime/test/xgameruntime_offline_test.cpp index 66b1100544..487bb60e5a 100644 --- a/runtime/test/xgameruntime_offline_test.cpp +++ b/runtime/test/xgameruntime_offline_test.cpp @@ -62,6 +62,9 @@ using Register = HRESULT(STDMETHODCALLTYPE*)(void*, XTaskQueueHandle, void*, voi using TryHdr = uint32_t(STDMETHODCALLTYPE*)(void*, uint32_t, float*); using FindUser = HRESULT(STDMETHODCALLTYPE*)(void*, const uint8_t*, void**); using CloseUser = void(STDMETHODCALLTYPE*)(void*, void*); +using AddAsync = HRESULT(STDMETHODCALLTYPE*)(void*, uint32_t, XAsyncBlock*); +using AddResult = HRESULT(STDMETHODCALLTYPE*)(void*, XAsyncBlock*, void**); +constexpr HRESULT kUserNotFound = static_cast(0x89245104); // E_GAMEUSER_USER_NOT_FOUND using Unregister = bool(STDMETHODCALLTYPE*)(void*, XTaskQueueRegistrationToken, bool); using Endpoint = HRESULT(STDMETHODCALLTYPE*)(void*, uint64_t, uint32_t, size_t, wchar_t*, size_t*); using FindController = HRESULT(STDMETHODCALLTYPE*)(void*, void*, XAsyncBlock*); @@ -280,11 +283,22 @@ int main() { check(query(&kUserDeviceClass, kUserDeviceClass, &device) == S_OK && device, "XUserDeviceImpl for its class id"); if (!device) return 1; uint8_t device_id[32] = {7}; - void* user = nullptr; - check(at(device, 3)(device, device_id, &user) == S_OK && user, "XUserFindForDevice: the offline user"); - check(rt::shims::xgameruntime_user_open_handles() == 1, "the handle is an XUserHandle the title closes"); + // RT-382: the reference says XUserFindForDevice cannot return a user who has not been added with XUserAddAsync. + void* user = reinterpret_cast(1); // a stale value, so "no handle" has to be written + check(at(device, 3)(device, device_id, &user) == kUserNotFound && !user, + "XUserFindForDevice before XUserAddAsync: E_GAMEUSER_USER_NOT_FOUND and no handle"); + check(rt::shims::xgameruntime_user_open_handles() == 0, "no XUserHandle was handed out before the add"); + XAsyncBlock add{}; + void* added = nullptr; + check(at(user_object(query), 7)(user_object(query), 0x01, &add) == S_OK && finished(&add) && + at(user_object(query), 8)(user_object(query), &add, &added) == S_OK && added, + "XUserAddAsync adds the default user"); + check(at(device, 3)(device, device_id, &user) == S_OK && user, + "XUserFindForDevice after XUserAddAsync: the offline user"); + check(rt::shims::xgameruntime_user_open_handles() == 2, "the add result and the found user are two XUserHandles"); at(user_object(query), 4)(user_object(query), user); - check(rt::shims::xgameruntime_user_open_handles() == 0, "XUserCloseHandle closes it"); + at(user_object(query), 4)(user_object(query), added); + check(rt::shims::xgameruntime_user_open_handles() == 0, "XUserCloseHandle closes both"); check_device_registrations(device); wchar_t endpoint[8] = {L'x'}; size_t used = 99;