Files
DroidDeck/tools/bionic-runtime/dd-directinput/test/host_test.c
T
The412Banner 9ad5961e4c feat(bionic): dd-directinput, XInput from the pad state for games started without Steam
Wine's xinput reads the HID reports winebus makes from the evdev events fakeinput replays
out of each controller ring, through a queue per handle. A game that reads them slower
than the pad reports gets old stick positions: in Halo: The Master Chief Collection the
character kept moving for a second or two after the stick was let go (Bionic r104,
"Start without Steam"; turning vibration off in DroidDeck and in the game changed nothing).

dd-directinput is an XInput DLL set (xinput1_1..1_4, 9_1_0, uap; ARM64X for system32,
i386 for syswow64) that answers XInputGetState from the snapshot of the whole pad
FakeInputWriter keeps in each ring file's header (seqlock, read with 32-bit loads only).
XInputSetState returns at once; a worker thread hands rumble changes to a renamed copy of
Wine's builtin xinput1_4 (dd-directinput-wine-xinput.dll), so rumble keeps DroidDeck's
force-feedback path without Wine's lock holding up a game thread. Based on Bannerlator's
direct XInput DLL (GPL-3.0).

launch.sh copies the DLLs into the prefix (replacing the links to the tool's builtins,
never writing through them) and sets xinput*=n,b when the game is set to start without
Steam; DD_DIRECTINPUT=0 keeps Wine's own, and a game no longer set to start without Steam
gets Wine's links back. tools/bionic-runtime/build.sh builds and checks the DLLs (export
tables of both ARM64X views, imports, no TLS directory, version resource) and runs the
host test of the ring reader.
2026-12-06 01:14:11 -05:00

143 lines
5.6 KiB
C

/*
* dd-directinput host test: the ring snapshot reader (dd_directinput_ring.h) against a writer that follows
* FakeInputWriter.writeSnapshotLocked, on a shared file mapping (the app and the guest map the same file),
* plus the XInput mapping and the FAKE_EVDEV_MEMFD_PATHS parser.
*/
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "dd_directinput_ring.h"
static int failures;
#define CHECK(c, ...) do { if (!(c)) { failures++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
static uint8_t *wmap;
static volatile int stop;
static void put32(uint32_t off, uint32_t v) { __atomic_store_n((uint32_t *)(void *)(wmap + off), v, __ATOMIC_RELAXED); }
static void put64(uint32_t off, uint64_t v) { __atomic_store_n((uint64_t *)(void *)(wmap + off), v, __ATOMIC_RELAXED); }
static void header(void) {
put32(DDI_OFF_MAGIC, DDI_RING_MAGIC);
put32(DDI_OFF_VERSION, DDI_RING_VERSION);
put32(DDI_OFF_EVENT_SIZE, DDI_RING_EVENT_SIZE);
put32(DDI_OFF_CAPACITY, DDI_RING_CAPACITY);
}
/* FakeInputWriter.writeSnapshotLocked: seq + 1, fence, fields, fence, seq + 2 */
static void publish(uint64_t *seq, uint32_t buttons, const int16_t axes[8]) {
put64(DDI_OFF_SNAP_SEQ, *seq + 1);
__atomic_thread_fence(__ATOMIC_RELEASE);
put32(DDI_OFF_SNAP_BUTTONS, buttons);
for (int k = 0; k < 8; k++)
__atomic_store_n((int16_t *)(void *)(wmap + DDI_OFF_SNAP_AXES + 2 * k), axes[k], __ATOMIC_RELAXED);
__atomic_thread_fence(__ATOMIC_RELEASE);
put64(DDI_OFF_SNAP_SEQ, *seq + 2);
*seq += 2;
}
/* Every field carries the same counter, so a torn read shows up as fields that differ. */
static uint64_t writer_seq;
static void *writer(void *arg) {
(void)arg;
uint64_t seq = writer_seq; /* the sequence continues: a reused value would let a stale read pass (ABA) */
uint32_t n = 1;
while (!stop) {
int16_t v = (int16_t)(n & 0x3fff);
int16_t axes[8] = {v, v, v, v, v, v, v, v};
publish(&seq, (uint32_t)v, axes);
n++;
}
return NULL;
}
static void take(void *ctx, uint32_t slot, const char *p, size_t len) {
char *out = ctx;
char buf[64];
snprintf(buf, sizeof buf, "%u:%.*s|", slot, (int)len, p);
strcat(out, buf);
}
DDI_DEFINE_PARSE_PATHS(parse_paths, char)
int main(void) {
char path[] = "/tmp/ddi-ring-XXXXXX";
int fd = mkstemp(path);
CHECK(fd >= 0, "mkstemp");
size_t size = DDI_RING_HEADER_SIZE + DDI_RING_CAPACITY * DDI_RING_EVENT_SIZE;
CHECK(ftruncate(fd, (off_t)size) == 0, "ftruncate");
wmap = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
const uint8_t *rmap = mmap(NULL, DDI_RING_HEADER_SIZE, PROT_READ, MAP_SHARED, fd, 0);
CHECK(wmap != MAP_FAILED && rmap != MAP_FAILED, "mmap");
unlink(path);
CHECK(!ddi_valid(rmap), "an empty file is not a ring");
header();
CHECK(ddi_valid(rmap), "a version-2 header is a ring");
put32(DDI_OFF_VERSION, 1);
CHECK(!ddi_valid(rmap), "version 1 is refused");
put32(DDI_OFF_VERSION, DDI_RING_VERSION);
/* mapping */
uint64_t seq = 0;
int16_t axes[8] = {1000, 2000, -3000, -32767, 255, 128, -1, 1};
uint32_t buttons = (1u << DDI_B_A) | (1u << DDI_B_TR) | (1u << DDI_B_START) | (1u << DDI_B_MODE) | (1u << DDI_B_QAM);
publish(&seq, buttons, axes);
ddi_snap s;
CHECK(ddi_snapshot(rmap, &s, 64), "snapshot of a settled ring");
CHECK(s.seq == 2, "seq %u", s.seq);
ddi_pad p;
ddi_map(&s, 0, &p);
CHECK(p.buttons == (DDI_XI_A | DDI_XI_RIGHT_SHOULDER | DDI_XI_START | DDI_XI_DPAD_LEFT | DDI_XI_DPAD_DOWN),
"buttons 0x%04x (no Guide, no QAM, hat left + down)", p.buttons);
CHECK(p.lx == 1000 && p.ly == -2000 && p.rx == -3000 && p.ry == 32767, "sticks %d %d %d %d (Y flipped)", p.lx, p.ly,
p.rx, p.ry);
CHECK(p.rt == 255 && p.lt == 128, "triggers lt %u rt %u", p.lt, p.rt);
ddi_map(&s, 1, &p);
CHECK(p.buttons & DDI_XI_GUIDE, "Guide in GetStateEx");
int16_t edge[8] = {INT16_MIN, INT16_MIN, 0, 0, -5, 300, 0, -1};
publish(&seq, 0, edge);
CHECK(ddi_snapshot(rmap, &s, 64), "snapshot");
ddi_map(&s, 0, &p);
CHECK(p.ly == INT16_MAX && p.lt == 255 && p.rt == 0 && p.buttons == DDI_XI_DPAD_UP, "edges: ly %d lt %u rt %u b 0x%x",
p.ly, p.lt, p.rt, p.buttons);
/* an odd seq (a write in progress) is never taken */
put64(DDI_OFF_SNAP_SEQ, seq + 1);
CHECK(!ddi_snapshot(rmap, &s, 64), "a snapshot mid-write is refused");
put64(DDI_OFF_SNAP_SEQ, seq);
/* no torn reads against a running writer; the state before its first publish is consistent too */
int16_t zero[8] = {0};
publish(&seq, 0, zero);
writer_seq = seq;
pthread_t t;
pthread_create(&t, NULL, writer, NULL);
unsigned ok = 0, lapped = 0, torn = 0;
for (unsigned i = 0; i < 2000000u; i++) {
if (!ddi_snapshot(rmap, &s, 64)) { lapped++; continue; }
ok++;
for (int k = 1; k < 8; k++)
if (s.axes[k] != s.axes[0]) { torn++; break; }
if ((int16_t)s.buttons != s.axes[0]) torn++;
}
stop = 1;
pthread_join(t, NULL);
CHECK(torn == 0, "%u torn snapshots", torn);
CHECK(ok > 0, "no snapshot taken");
printf("concurrent: %u snapshots, %u lapped, %u torn\n", ok, lapped, torn);
/* FAKE_EVDEV_MEMFD_PATHS */
char got[256] = "";
parse_paths("0=/a/r0;1=/a/r1;4=/a/mouse;x=bad;2=;3=/a/r3", take, got, 4);
CHECK(!strcmp(got, "0:/a/r0|1:/a/r1|3:/a/r3|"), "parsed '%s' (slot 4 = mouse and malformed entries skipped)", got);
printf(failures ? "host_test: %d failure(s)\n" : "host_test: OK\n", failures);
return failures ? 1 : 0;
}