Skip to content
Open
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
6 changes: 5 additions & 1 deletion docs/gameinput.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ button) run on that thread, as GameInput runs them on its own threads. Once the
`IGameInputDispatcher::Dispatch` runs them on the title's thread, oldest first, within its quota: at least one queued
event per call (a quota of 0 runs exactly one), no further event once the quota has passed, and only the events
queued when the call started (later ones wait for the next call, so even an unlimited quota returns). The wait
handle is signalled again while events remain.
handle is signalled again while events remain. An event is queued only when a registered callback wants it (a
reading with no reading callback, say, is no work), so readings nobody hears cannot build a backlog that a title
running one item a frame (`Dispatch(0)`) never catches up on, with a later pad's connect event stuck behind it. The
first few `RegisterDeviceCallback` calls (filter, kind, status filter, enumeration) and the first `CreateDispatcher`
and `Dispatch` calls are logged, since they decide how the title hears of a pad plugged in after start.

The API surface:

Expand Down
31 changes: 29 additions & 2 deletions runtime/shims/gameinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,19 @@ State& g() {
return *s;
}

bool matches(const Callback& c, const Event& e);

// Under g().mu. Queues an event only when a registered callback wants it: GameInput dispatches callbacks, and with
// none listening there is no work. Otherwise every reading (a pad's moving stick, a sensor pad's 125 Hz stream) would
// queue an item nobody hears, and a title holding a dispatcher that runs one item a frame (Dispatch(0)) would fall
// further behind every frame: a pad plugged in after start would have its connect event stuck behind that backlog and
// never reach the title (RT-GAMEINPUT-LATE-PAD).
void queue_event(const Event& e) {
State& s = g();
if (std::none_of(s.callbacks.begin(), s.callbacks.end(), [&e](const Callback& c) { return matches(c, e); })) return;
if (e.device) e.device->AddRef();
if (e.reading) e.reading->AddRef();
g().queue.push_back(e);
s.queue.push_back(e);
}

bool matches(const Callback& c, const Event& e) {
Expand Down Expand Up @@ -2326,7 +2335,13 @@ class Dispatcher final : public IGameInputDispatcher {
return r;
}
// Runs the queued callbacks on the caller's thread within the quota (see deliver_within).
bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); }
bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override {
static std::atomic<bool> s_logged{false};
if (!s_logged.exchange(true))
std::fprintf(stderr, "[rt] gameinput: Dispatch first call (quota %llu us)\n",
static_cast<unsigned long long>(quotaInMicroseconds));
return deliver_within(quotaInMicroseconds);
}
HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override {
if (!handle) return E_POINTER;
*handle = nullptr;
Expand Down Expand Up @@ -2473,6 +2488,15 @@ class GameInput final : public IGameInput {
if (token) *token = kInvalidCallbackToken;
if (!fn) return E_INVALIDARG;
if (device && as_device(device)->info.deviceFamily == GameInputFamilyAggregate) return E_NOTIMPL;
// How a title learns of its pads decides whether one plugged in later reaches it, so the first few
// registrations are logged.
static std::atomic<int> s_logged{0};
if (s_logged.fetch_add(1) < 8)
std::fprintf(stderr, "[rt] gameinput: RegisterDeviceCallback(%s, kind 0x%x, status 0x%x, %s enumeration)\n",
device ? "one device" : "all devices", kind, status_filter,
enumeration == GameInputBlockingEnumeration ? "blocking"
: enumeration == GameInputAsyncEnumeration ? "async"
: "no");
State& s = g();
GameInputCallbackToken t;
std::vector<Event> now;
Expand Down Expand Up @@ -2542,6 +2566,9 @@ class GameInput final : public IGameInput {

HRESULT STDMETHODCALLTYPE CreateDispatcher(IGameInputDispatcher** out) override {
if (!out) return E_POINTER;
static std::atomic<bool> s_logged{false};
if (!s_logged.exchange(true))
std::fprintf(stderr, "[rt] gameinput: CreateDispatcher first call (callbacks now run in Dispatch)\n");
*out = new Dispatcher;
return S_OK;
}
Expand Down
60 changes: 60 additions & 0 deletions runtime/test/gameinput_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,65 @@ void test_haptic_motors(IGameInput* input) {

} // namespace

// A pad plugged in after the title started, with the title holding a dispatcher (RT-GAMEINPUT-LATE-PAD). The title
// registers its device callback with no pad present, a first pad arrives and is moved on every poll (the keyboard pad's
// mouse stick), and the title runs one queued item a frame (Dispatch(0)). No reading callback is registered, so those
// readings are work no callback wants: they must not queue ahead of the second pad's connect event.
void test_late_pad_behind_dispatcher(IGameInput* input) {
check(rt::gameinput::connected_count() == 0, "late pad: nothing connected at the start");
IGameInputDispatcher* dispatcher = nullptr;
check(input->CreateDispatcher(&dispatcher) == S_OK && dispatcher, "late pad: CreateDispatcher");
if (!dispatcher) return;
GameInputCallbackToken token = 0;
check(input->RegisterDeviceCallback(nullptr, GameInputKindGamepad, GameInputDeviceConnected,
GameInputBlockingEnumeration, nullptr, on_device, &token) == S_OK,
"late pad: RegisterDeviceCallback with no pad present");
const auto connects_to_us = [&](IGameInputDevice** last) {
size_t n = 0;
for (const DeviceEvent& e : g_device_events)
if (e.token == token && e.previous == GameInputDeviceNoStatus && (e.current & GameInputDeviceConnected)) {
++n;
if (last) *last = e.device;
}
return n;
};
Pad first, late;
attach(first, "Leonida virtual Xbox pad", 0x045E, 0x0B12, true);
rt::gameinput::poll();
while (dispatcher->Dispatch(0)) {}
check(connects_to_us(nullptr) == 1, "late pad: the first pad's connect reaches the callback");

int16_t x = 0;
const auto frame = [&] { // four polls with the first pad's stick moving, then one item dispatched
for (int i = 0; i < 4; ++i) {
x = static_cast<int16_t>(x == 8000 ? -8000 : 8000);
SDL_SetJoystickVirtualAxis(first.js, SDL_GAMEPAD_AXIS_RIGHTX, x);
rt::gameinput::poll();
}
dispatcher->Dispatch(0);
};
for (int f = 0; f < 50; ++f) frame();
attach(late, "Leonida virtual DualSense", 0x054C, 0x0CE6, false, true);
for (int f = 0; f < 10 && connects_to_us(nullptr) < 2; ++f) frame();
IGameInputDevice* late_dev = nullptr;
check(connects_to_us(&late_dev) == 2 && late_dev && late_dev->GetDeviceInfo()->vendorId == 0x054C,
"late pad: a pad connected later reaches the callback within a few frames");
if (late_dev && late.js) {
SDL_SetJoystickVirtualButton(late.js, SDL_GAMEPAD_BUTTON_SOUTH, true);
rt::gameinput::poll();
IGameInputReading* r = current(input, late_dev);
check(buttons_of(r) & GameInputGamepadA, "late pad: its A press reads through the device it was handed");
if (r) r->Release();
}
input->UnregisterCallback(token, 0);
while (dispatcher->Dispatch(UINT64_MAX)) {}
dispatcher->Release();
detach(first);
detach(late);
rt::gameinput::poll();
check(rt::gameinput::connected_count() == 0, "late pad: both pads closed");
}

int main(int argc, char** argv) {
// Only virtual pads: no real controller on the test machine may show up.
for (const char* hint : {SDL_HINT_JOYSTICK_HIDAPI, SDL_HINT_JOYSTICK_RAWINPUT, SDL_HINT_JOYSTICK_WGI,
Expand Down Expand Up @@ -2294,6 +2353,7 @@ int main(int argc, char** argv) {
test_haptic_motors(input);
test_raw_hid(input);
test_raw_report_during_reconnect(input);
test_late_pad_behind_dispatcher(input);
input->Release();

std::printf("%d checks, %d failures\n", g_checks, g_failures);
Expand Down
Loading