upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:08:37 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:08:37 -0500
commitbca17fcf82c92faef3003a282a37f5f65dc3ff7b (patch)
tree0ea61c85f78fff4316872995bf45bcffb2f64866
parentd92b805c3844a7430948d8f06e007888154bf281 (diff)
parentb170687d2501efcf6bccf60988f308eab43464b3 (diff)
Merge PR #9590
-rw-r--r--docs/runtime_imports.md10
-rw-r--r--runtime/shims/xgameruntime_offline.cpp97
-rw-r--r--runtime/test/xgameruntime_offline_test.cpp88
3 files changed, 176 insertions, 19 deletions
diff --git a/docs/runtime_imports.md b/docs/runtime_imports.md
index 934dae376..e0e5f6dfc 100644
--- a/docs/runtime_imports.md
+++ b/docs/runtime_imports.md
@@ -458,8 +458,14 @@ on the executable, the rest is not:
458- **HDR:** `XDisplayTryEnableHdrMode` answers Disabled, because the host output is SDR. With `LEONIDA_HDR=1` it 458- **HDR:** `XDisplayTryEnableHdrMode` answers Disabled, because the host output is SDR. With `LEONIDA_HDR=1` it
459 answers Enabled with the documented default tone-map luminances (0.01, 1000, 1000 nits). 459 answers Enabled with the documented default tone-map luminances (0.01, 1000, 1000 nits).
460- **Devices:** every device belongs to the offline user (`XUserFindForDevice` hands out an `XUserHandle` the title 460- **Devices:** every device belongs to the offline user (`XUserFindForDevice` hands out an `XUserHandle` the title
461 closes). Association and audio-endpoint registrations hand out tokens that never fire. The user has no audio 461 closes). `XUserRegisterForDeviceAssociationChanged` (slot 4) and
462 endpoint of its own (`ERROR_NOT_FOUND`). Finding a controller shows no UI and gives an all-zero device id, as 462 `XUserRegisterForDefaultAudioEndpointUtf16Changed` (7) keep each registration (its queue, duplicated, its context
463 and callback) under a token unique across both kinds and never 0; a null callback or token is `E_INVALIDARG`, and
464 queue and context are optional. The matching unregister (5, 8) removes it at once, with or without `wait`, and
465 returns true for every token, as `XUserUnregisterForChangeEvent` does: a token already removed, never handed out or
466 of the other kind changes nothing. Offline the device association and the default audio endpoint never change, so
467 no event is raised and no callback is ever queued or called. The user has no audio endpoint of its own
468 (`ERROR_NOT_FOUND`). Finding a controller shows no UI and gives an all-zero device id, as
463 documented for desktop. `APP_LOCAL_DEVICE_ID` is taken to be 32 bytes; no caller in the executable confirms it. 469 documented for desktop. `APP_LOCAL_DEVICE_ID` is taken to be 32 bytes; no caller in the executable confirms it.
464 470
465`xgameruntime_offline_test` drives the five classes; `xgameruntime_offline_hdr` runs it again with 471`xgameruntime_offline_test` drives the five classes; `xgameruntime_offline_hdr` runs it again with
diff --git a/runtime/shims/xgameruntime_offline.cpp b/runtime/shims/xgameruntime_offline.cpp
index 0d12597a4..cc7a91d21 100644
--- a/runtime/shims/xgameruntime_offline.cpp
+++ b/runtime/shims/xgameruntime_offline.cpp
@@ -15,8 +15,9 @@
15// XGameStreamingImpl the game is never streamed: initialising succeeds, IsStreaming is false, there are no 15// XGameStreamingImpl the game is never streamed: initialising succeeds, IsStreaming is false, there are no
16// clients, the connection-state registration hands out a token that never fires. 16// clients, the connection-state registration hands out a token that never fires.
17// XDisplayImpl XDisplayTryEnableHdrMode: Disabled (the host output is SDR), Enabled with LEONIDA_HDR=1. 17// XDisplayImpl XDisplayTryEnableHdrMode: Disabled (the host output is SDR), Enabled with LEONIDA_HDR=1.
18// XUserDeviceImpl every device belongs to the offline user; associations and audio endpoints never change; the 18// XUserDeviceImpl every device belongs to the offline user; associations and audio endpoints never change, so
19// user has no audio endpoint of its own; finding a controller shows no UI (as on desktop). 19// their change registrations are kept until unregistered and never called back; the user has
20// no audio endpoint of its own; finding a controller shows no UI (as on desktop).
20// XAccessibilityImpl high contrast is Off, closed captions can't be configured, no voice is installed (enumerating 21// XAccessibilityImpl high contrast is Off, closed captions can't be configured, no voice is installed (enumerating
21// calls back zero times), XSpeechSynthesizerCreate fails (E_FAIL) with a null handle, the other 22// calls back zero times), XSpeechSynthesizerCreate fails (E_FAIL) with a null handle, the other
22// speech calls refuse the handle they were never given (E_INVALIDARG), speech-to-text display 23// speech calls refuse the handle they were never given (E_INVALIDARG), speech-to-text display
@@ -34,6 +35,7 @@
34#include <cstring> 35#include <cstring>
35#include <iterator> 36#include <iterator>
36#include <mutex> 37#include <mutex>
38#include <vector>
37 39
38#include <XAsync.h> 40#include <XAsync.h>
39#include <XAsyncProvider.h> 41#include <XAsyncProvider.h>
@@ -455,18 +457,74 @@ HRESULT STDMETHODCALLTYPE device_find_user(void*, const APP_LOCAL_DEVICE_ID* dev
455 457
456// 4: HRESULT XUserRegisterForDeviceAssociationChanged(XTaskQueueHandle queue, void* context, 458// 4: HRESULT XUserRegisterForDeviceAssociationChanged(XTaskQueueHandle queue, void* context,
457// XUserDeviceAssociationChangedCallback* callback, XTaskQueueRegistrationToken* token) 459// XUserDeviceAssociationChangedCallback* callback, XTaskQueueRegistrationToken* token)
458// 7: HRESULT XUserRegisterForDefaultAudioEndpointUtf16Changed(XTaskQueueHandle, void*, callback, token*) 460// 5: bool XUserUnregisterForDeviceAssociationChanged(XTaskQueueRegistrationToken token, bool wait)
459// Associations and endpoints never change offline: a token, no callback. 461// 7: HRESULT XUserRegisterForDefaultAudioEndpointUtf16Changed(XTaskQueueHandle queue, void* context,
460HRESULT STDMETHODCALLTYPE device_register(void*, XTaskQueueHandle, void*, void* callback, 462// XUserDefaultAudioEndpointUtf16ChangedCallback* callback, XTaskQueueRegistrationToken* token)
463// 8: bool XUserUnregisterForDefaultAudioEndpointUtf16Changed(XTaskQueueRegistrationToken token, bool wait)
464//
465// Each registration is kept (its kind, a duplicate of its queue, its context and callback) under a token unique
466// across both kinds and never 0, until the title unregisters it with the matching function. Offline, the device
467// association and the default audio endpoint never change, so no event is ever raised: no callback is queued or
468// run, and nothing here calls one. Unregistering therefore never has to wait, whatever `wait` says, and removes the
469// registration at once. It returns true for every token, as XUserUnregisterForChangeEvent does: a token already
470// unregistered, never handed out or of the other kind is left alone (a title polling with wait = false would spin
471// on false). queue and context are _In_opt_; a null queue stays null (the process queue, if an event ever came);
472// callback and token are required (E_INVALIDARG).
473//
474// The state is namespace-scope and the lock a constinit SRW lock, not function-local statics: these calls run on
475// the title's threads.
476enum class DeviceEvent : uint32_t { Association = 0, AudioEndpoint = 1 };
477
478struct DeviceRegistration {
479 uint64_t token;
480 DeviceEvent kind;
481 XTaskQueueHandle queue; // our duplicate, closed on unregister; null for the process queue
482 void* context;
483 void* callback;
484};
485
486constinit SRWLOCK g_device_lock = SRWLOCK_INIT;
487std::vector<DeviceRegistration> g_device_registrations; // guarded by g_device_lock
488constinit uint64_t g_device_next_token = 1; // guarded by g_device_lock
489
490template <DeviceEvent Kind>
491HRESULT STDMETHODCALLTYPE device_register(void*, XTaskQueueHandle queue, void* context, void* callback,
461 XTaskQueueRegistrationToken* token) { 492 XTaskQueueRegistrationToken* token) {
462 if (!callback || !token) return E_INVALIDARG; 493 if (!callback || !token) return E_INVALIDARG;
463 std::lock_guard<std::mutex> lock(g_streaming_mutex); 494 XTaskQueueHandle kept = nullptr;
464 token->token = g_next_token++; 495 if (queue) {
496 const HRESULT hr = XTaskQueueDuplicateHandle(queue, &kept);
497 if (FAILED(hr)) return hr;
498 }
499 uint64_t issued = 0;
500 AcquireSRWLockExclusive(&g_device_lock);
501 try {
502 g_device_registrations.push_back({g_device_next_token, Kind, kept, context, callback});
503 issued = g_device_next_token++;
504 } catch (...) { // out of memory: no exception reaches the title
505 }
506 ReleaseSRWLockExclusive(&g_device_lock);
507 if (!issued) {
508 if (kept) XTaskQueueCloseHandle(kept);
509 return E_OUTOFMEMORY;
510 }
511 token->token = issued;
465 return S_OK; 512 return S_OK;
466} 513}
467 514
468// 5, 8: bool ...Unregister...(XTaskQueueRegistrationToken token, bool wait): never pending. 515template <DeviceEvent Kind> bool STDMETHODCALLTYPE device_unregister(void*, XTaskQueueRegistrationToken token, bool) {
469bool STDMETHODCALLTYPE device_unregister(void*, XTaskQueueRegistrationToken, bool) { return true; } 516 XTaskQueueHandle queue = nullptr;
517 AcquireSRWLockExclusive(&g_device_lock);
518 for (auto it = g_device_registrations.begin(); it != g_device_registrations.end(); ++it)
519 if (it->token == token.token && it->kind == Kind) {
520 queue = it->queue;
521 g_device_registrations.erase(it);
522 break;
523 }
524 ReleaseSRWLockExclusive(&g_device_lock);
525 if (queue) XTaskQueueCloseHandle(queue);
526 return true;
527}
470 528
471// 6: HRESULT XUserGetDefaultAudioEndpointUtf16(XUserLocalId user, XUserDefaultAudioEndpointKind kind, 529// 6: HRESULT XUserGetDefaultAudioEndpointUtf16(XUserLocalId user, XUserDefaultAudioEndpointKind kind,
472// size_t endpointIdUtf16Count, wchar_t* endpointIdUtf16, size_t* endpointIdUtf16Used) 530// size_t endpointIdUtf16Count, wchar_t* endpointIdUtf16, size_t* endpointIdUtf16Used)
@@ -512,11 +570,11 @@ HRESULT STDMETHODCALLTYPE device_query_interface(void*, REFIID iid, void** out);
512const void* const kUserDeviceVtable[] = { 570const void* const kUserDeviceVtable[] = {
513 FN(device_query_interface), FN(object_add_ref), FN(object_release), // 0-2 IUnknown 571 FN(device_query_interface), FN(object_add_ref), FN(object_release), // 0-2 IUnknown
514 FN(device_find_user), // 3 XUserFindForDevice 572 FN(device_find_user), // 3 XUserFindForDevice
515 FN(device_register), // 4 XUserRegisterForDeviceAssociationChanged 573 FN(device_register<DeviceEvent::Association>), // 4 XUserRegisterForDeviceAssociationChanged
516 FN(device_unregister), // 5 XUserUnregisterForDeviceAssociationChanged 574 FN(device_unregister<DeviceEvent::Association>), // 5 XUserUnregisterForDeviceAssociationChanged
517 FN(device_audio_endpoint), // 6 XUserGetDefaultAudioEndpointUtf16 575 FN(device_audio_endpoint), // 6 XUserGetDefaultAudioEndpointUtf16
518 FN(device_register), // 7 XUserRegisterForDefaultAudioEndpointUtf16Changed 576 FN(device_register<DeviceEvent::AudioEndpoint>), // 7 XUserRegisterForDefaultAudioEndpointUtf16Changed
519 FN(device_unregister), // 8 XUserUnregisterForDefaultAudioEndpointUtf16Changed 577 FN(device_unregister<DeviceEvent::AudioEndpoint>), // 8 XUserUnregisterForDefaultAudioEndpointUtf16Changed
520 FN(device_find_controller_async), // 9 XUserFindControllerForUserWithUiAsync 578 FN(device_find_controller_async), // 9 XUserFindControllerForUserWithUiAsync
521 FN(device_find_controller_result), // 10 XUserFindControllerForUserWithUiResult 579 FN(device_find_controller_result), // 10 XUserFindControllerForUserWithUiResult
522}; 580};
@@ -685,4 +743,15 @@ bool xgameruntime_offline_query(const GUID& class_id, REFIID interface_id, void*
685// For tests: how many game events were written. 743// For tests: how many game events were written.
686long xgameruntime_offline_game_events() { return g_events.load(); } 744long xgameruntime_offline_game_events() { return g_events.load(); }
687 745
746// For tests: how many XUserDeviceImpl registrations of one kind (0 device association, 1 default audio endpoint)
747// are kept.
748long xgameruntime_offline_device_registrations(uint32_t kind) {
749 long n = 0;
750 AcquireSRWLockShared(&g_device_lock);
751 for (const auto& registration : g_device_registrations)
752 if (static_cast<uint32_t>(registration.kind) == kind) ++n;
753 ReleaseSRWLockShared(&g_device_lock);
754 return n;
755}
756
688} // namespace rt::shims 757} // namespace rt::shims
diff --git a/runtime/test/xgameruntime_offline_test.cpp b/runtime/test/xgameruntime_offline_test.cpp
index 764099bcb..7668b6164 100644
--- a/runtime/test/xgameruntime_offline_test.cpp
+++ b/runtime/test/xgameruntime_offline_test.cpp
@@ -18,6 +18,7 @@
18namespace rt::shims { 18namespace rt::shims {
19long xgameruntime_offline_game_events(); 19long xgameruntime_offline_game_events();
20long xgameruntime_user_open_handles(); 20long xgameruntime_user_open_handles();
21long xgameruntime_offline_device_registrations(uint32_t kind);
21} 22}
22 23
23namespace { 24namespace {
@@ -92,6 +93,89 @@ void* user_object(HRESULT(WINAPI* query)(const GUID*, REFIID, void**)) {
92 93
93void CALLBACK on_state(void*, uint64_t, uint32_t) {} 94void CALLBACK on_state(void*, uint64_t, uint32_t) {}
94 95
96// XUserDeviceImpl slots 4/5 (device association) and 7/8 (default audio endpoint): kept until unregistered, never
97// called back offline.
98long g_device_callbacks = 0;
99void CALLBACK on_association_changed(void* context, const void*) {
100 ++g_device_callbacks;
101 if (context) ++*static_cast<long*>(context);
102}
103void CALLBACK on_audio_endpoint_changed(void* context, uint64_t, uint32_t, const wchar_t*) {
104 ++g_device_callbacks;
105 if (context) ++*static_cast<long*>(context);
106}
107
108constexpr uint32_t kAssociation = 0, kAudioEndpoint = 1; // rt::shims::xgameruntime_offline_device_registrations
109
110void check_device_registrations(void* device) {
111 using rt::shims::xgameruntime_offline_device_registrations;
112 auto reg = [&](int slot, XTaskQueueHandle queue, void* context, void* callback, XTaskQueueRegistrationToken* t) {
113 return at<Register>(device, slot)(device, queue, context, callback, t);
114 };
115 auto unreg = [&](int slot, XTaskQueueRegistrationToken t, bool wait) {
116 return at<Unregister>(device, slot)(device, t, wait);
117 };
118 void* association = reinterpret_cast<void*>(&on_association_changed);
119 void* audio = reinterpret_cast<void*>(&on_audio_endpoint_changed);
120
121 XTaskQueueRegistrationToken t{};
122 check(reg(4, nullptr, nullptr, nullptr, &t) == E_INVALIDARG && reg(4, nullptr, nullptr, association, nullptr) ==
123 E_INVALIDARG && reg(7, nullptr, nullptr, nullptr, &t) == E_INVALIDARG &&
124 reg(7, nullptr, nullptr, audio, nullptr) == E_INVALIDARG,
125 "device registrations need a callback and a token (E_INVALIDARG)");
126 check(xgameruntime_offline_device_registrations(kAssociation) == 0 &&
127 xgameruntime_offline_device_registrations(kAudioEndpoint) == 0,
128 "a refused device registration is not kept");
129
130 // queue and context are optional; one registration of each kind on a manual queue the test dispatches.
131 XTaskQueueHandle manual = nullptr;
132 check(SUCCEEDED(XTaskQueueCreate(XTaskQueueDispatchMode::Manual, XTaskQueueDispatchMode::Manual, &manual)),
133 "create a manual queue for the device callbacks");
134 long calls = 0;
135 XTaskQueueRegistrationToken a1{}, a2{}, e1{}, e2{};
136 check(reg(4, nullptr, nullptr, association, &a1) == S_OK && reg(4, manual, &calls, association, &a2) == S_OK &&
137 reg(7, nullptr, nullptr, audio, &e1) == S_OK && reg(7, manual, &calls, audio, &e2) == S_OK,
138 "device association and audio endpoint registrations succeed with or without a queue and context");
139 const uint64_t tokens[] = {a1.token, a2.token, e1.token, e2.token};
140 bool distinct = true;
141 for (int i = 0; i < 4; ++i)
142 for (int j = 0; j < 4; ++j) distinct = distinct && tokens[i] != 0 && (i == j || tokens[i] != tokens[j]);
143 check(distinct, "device registration tokens are non-zero and unique across both kinds");
144 check(xgameruntime_offline_device_registrations(kAssociation) == 2 &&
145 xgameruntime_offline_device_registrations(kAudioEndpoint) == 2,
146 "each device registration is kept under its token");
147
148 check(!XTaskQueueDispatch(manual, XTaskQueuePort::Work, 0) &&
149 !XTaskQueueDispatch(manual, XTaskQueuePort::Completion, 0),
150 "nothing is queued for a device registration offline");
151 // The title closing its own queue handle doesn't end a registration: the shim keeps its own reference.
152 XTaskQueueCloseHandle(manual);
153 check(xgameruntime_offline_device_registrations(kAssociation) == 2,
154 "a registration outlives the title's handle to its queue");
155
156 // A token of the other kind, one never handed out, or 0: unregistering is harmless (true) and removes nothing.
157 check(unreg(5, e1, true) && unreg(8, a1, false) && unreg(5, XTaskQueueRegistrationToken{0}, false) &&
158 unreg(8, XTaskQueueRegistrationToken{~0ull}, true),
159 "unregistering a foreign, unissued or zero token returns true");
160 check(xgameruntime_offline_device_registrations(kAssociation) == 2 &&
161 xgameruntime_offline_device_registrations(kAudioEndpoint) == 2,
162 "and leaves every registration in place");
163
164 // Unregistering removes the registration at once, with or without wait (nothing is ever pending).
165 check(unreg(5, a1, false) && xgameruntime_offline_device_registrations(kAssociation) == 1,
166 "XUserUnregisterForDeviceAssociationChanged (wait = false) removes its registration");
167 check(unreg(5, a2, true) && xgameruntime_offline_device_registrations(kAssociation) == 0 &&
168 xgameruntime_offline_device_registrations(kAudioEndpoint) == 2,
169 "XUserUnregisterForDeviceAssociationChanged (wait = true) removes only its own");
170 check(unreg(8, e1, true) && unreg(8, e2, false) && xgameruntime_offline_device_registrations(kAudioEndpoint) == 0,
171 "XUserUnregisterForDefaultAudioEndpointUtf16Changed removes its registrations");
172 check(unreg(5, a1, true) && unreg(8, e2, false) && xgameruntime_offline_device_registrations(kAssociation) == 0 &&
173 xgameruntime_offline_device_registrations(kAudioEndpoint) == 0,
174 "a second unregister is harmless (true)");
175
176 check(g_device_callbacks == 0 && calls == 0, "no device association or audio endpoint callback ever ran");
177}
178
95} // namespace 179} // namespace
96 180
97int main() { 181int main() {
@@ -178,9 +262,7 @@ int main() {
178 check(rt::shims::xgameruntime_user_open_handles() == 1, "the handle is an XUserHandle the title closes"); 262 check(rt::shims::xgameruntime_user_open_handles() == 1, "the handle is an XUserHandle the title closes");
179 at<CloseUser>(user_object(query), 4)(user_object(query), user); 263 at<CloseUser>(user_object(query), 4)(user_object(query), user);
180 check(rt::shims::xgameruntime_user_open_handles() == 0, "XUserCloseHandle closes it"); 264 check(rt::shims::xgameruntime_user_open_handles() == 0, "XUserCloseHandle closes it");
181 check(at<Register>(device, 4)(device, nullptr, nullptr, reinterpret_cast<void*>(&on_state), &token) == S_OK && 265 check_device_registrations(device);
182 at<Unregister>(device, 5)(device, token, false),
183 "device association registration and unregistration");
184 wchar_t endpoint[8] = {L'x'}; 266 wchar_t endpoint[8] = {L'x'};
185 size_t used = 99; 267 size_t used = 99;
186 check(FAILED(at<Endpoint>(device, 6)(device, 1, 0, 8, endpoint, &used)) && endpoint[0] == 0 && used == 0, 268 check(FAILED(at<Endpoint>(device, 6)(device, 1, 0, 8, endpoint, &used)) && endpoint[0] == 0 && used == 0,