Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion runtime/shims/xgameruntime_offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,6 +54,7 @@ using XUserHandle = void*;

// XGameErr.h values, from the GDK "Error codes" page.
constexpr HRESULT E_GAMESTREAMING_NOT_INITIALIZED = static_cast<HRESULT>(0x89245400);
constexpr HRESULT E_GAMEUSER_USER_NOT_FOUND = static_cast<HRESULT>(0x89245104);

constexpr GUID kIUnknown = {0x00000000, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions runtime/shims/xgameruntime_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> g_user_added{false};
std::unordered_set<const void*> g_handles;

XUserHandle new_handle() {
Expand Down Expand Up @@ -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:
Expand All @@ -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<uintptr_t>(data->context) == static_cast<uintptr_t>(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;
}
Expand Down Expand Up @@ -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<std::mutex> lock(g_handles_mutex);
Expand Down
22 changes: 18 additions & 4 deletions runtime/test/xgameruntime_offline_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HRESULT>(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*);
Expand Down Expand Up @@ -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<FindUser>(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<void*>(1); // a stale value, so "no handle" has to be written
check(at<FindUser>(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<AddAsync>(user_object(query), 7)(user_object(query), 0x01, &add) == S_OK && finished(&add) &&
at<AddResult>(user_object(query), 8)(user_object(query), &add, &added) == S_OK && added,
"XUserAddAsync adds the default user");
check(at<FindUser>(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<CloseUser>(user_object(query), 4)(user_object(query), user);
check(rt::shims::xgameruntime_user_open_handles() == 0, "XUserCloseHandle closes it");
at<CloseUser>(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;
Expand Down
Loading