Files
DroidDeck/tools/bionic-runtime/dd-directinput/build.sh
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

273 lines
13 KiB
Bash
Executable File

#!/usr/bin/env bash
# dd-directinput: build the XInput DLLs DroidDeck-Bionic puts in a prefix for games set to start without Steam
# (launch.sh). Used by tools/bionic-runtime/build.sh.
#
# build.sh toolchain fetch the pinned llvm-mingw into $LLVM_MINGW (default ~/.cache/...)
# build.sh dlls <outdir> <outdir>/{arm64x,i386}/<dll>.dll for every XInput DLL name, each export table
# (both views of an ARM64X image), import list and machine type checked
# build.sh host <outdir> <outdir>/host_test (native, cc): the ring snapshot reader against a writer
#
# The PE side uses bylaws/llvm-mingw (its arm64ec target does not predefine __x86_64__, unlike upstream's).
# One C source (src/dd_directinput.c) is linked once per DLL name with that name's .def file (def/), so each
# DLL has exactly the exports and ordinals of the Windows DLL it stands in for. i386 .def files get stdcall
# decorations here and the link strips them (--kill-at).
#
# arm64x (system32 of the arm64ec layer): the source is compiled for ARM64EC and for native ARM64 and both
# objects are linked into one ARM64X image (lld: -m arm64xpe, the .def for the EC view, -defarm64native for
# the native view; each view gets its own CRT, the native one taken from the toolchain's own aarch64 link
# line). An x64 game loads the EC view, a native ARM64 process (Wine's own control.exe, ...) the ARM64 view;
# an ARM64EC-only image fails there with c000007b. i386 goes to syswow64 for 32-bit games.
#
# Every DLL carries a VERSIONINFO resource with the file version of the Wine builtin DLL it replaces, so a
# program reading it sees the same number as before.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
LLVM_MINGW_VER=20250920
case "$(uname -m)" in
aarch64|arm64)
LLVM_MINGW_HOST=aarch64; HOST_SUFFIX=-aarch64
LLVM_MINGW_SHA256=bce5cc755c613515fd44e1ee9523123d854103abae147571adb645450036274d ;;
*)
LLVM_MINGW_HOST=x86_64; HOST_SUFFIX=
LLVM_MINGW_SHA256=8dd8c34fc051a50c2fae86015f35057f8aae93fe1e19b34537ef1269a8b4c772 ;;
esac
LLVM_MINGW_TAR="llvm-mingw-${LLVM_MINGW_VER}-ucrt-ubuntu-22.04-${LLVM_MINGW_HOST}.tar.xz"
LLVM_MINGW_URL="https://github.com/bylaws/llvm-mingw/releases/download/${LLVM_MINGW_VER}/${LLVM_MINGW_TAR}"
LLVM_MINGW="${LLVM_MINGW:-$HOME/.cache/llvm-mingw-bylaws-${LLVM_MINGW_VER}${HOST_SUFFIX}}"
DLLS="xinput1_1 xinput1_2 xinput1_3 xinput1_4 xinput9_1_0 xinputuap"
ARCHES="arm64x i386"
MARKER="dd-directinput: "
say() { printf '== dd-directinput: %s\n' "$*"; }
die() { printf '::error::dd-directinput: %s\n' "$*" >&2; exit 1; }
toolchain() {
if [ ! -x "$LLVM_MINGW/bin/arm64ec-w64-mingw32-clang" ]; then
say "fetching $LLVM_MINGW_TAR"
local tmp; tmp="$(mktemp -d)"
curl -fsSL --retry 5 --retry-delay 5 -o "$tmp/t.tar.xz" "$LLVM_MINGW_URL"
echo "$LLVM_MINGW_SHA256 $tmp/t.tar.xz" | sha256sum -c -
mkdir -p "$LLVM_MINGW"
tar -xJf "$tmp/t.tar.xz" -C "$LLVM_MINGW" --strip-components=1
rm -rf "$tmp"
fi
export PATH="$LLVM_MINGW/bin:$PATH"
for t in i686 arm64ec aarch64; do
command -v "$t-w64-mingw32-clang" >/dev/null || die "llvm-mingw has no $t-w64-mingw32-clang"
done
for t in i686 aarch64; do
command -v "$t-w64-mingw32-windres" >/dev/null || die "llvm-mingw has no $t-w64-mingw32-windres"
done
say "llvm-mingw $LLVM_MINGW_VER: $(arm64ec-w64-mingw32-clang --version | head -1)"
}
windres_for() {
case "$1" in
i386) echo i686-w64-mingw32-windres ;;
arm64x) echo aarch64-w64-mingw32-windres ;;
*) die "unknown arch $1" ;;
esac
}
# File version of the Wine builtin each DLL replaces (dlls/<name>/version.rc).
DLL_VERSIONS='xinput1_1:9,12,589,0 xinput1_2:9,14,701,0 xinput1_3:9,15,779,0 xinput1_4:6,3,9600,16384 xinput9_1_0:9,10,455,0 xinputuap:10,0,19041,746'
version_res() { # version_res <arch> <dll> <tmpdir> -> path of a COFF object with the DLL's VERSIONINFO
local ver="" kv rc="$3/$2.rc" obj="$3/$2-ver-$1.o"
for kv in $DLL_VERSIONS; do [ "${kv%%:*}" = "$2" ] && ver="${kv#*:}"; done
[ -n "$ver" ] || die "no version for $2 in DLL_VERSIONS"
cat >"$rc" <<EOF
1 VERSIONINFO
FILEVERSION $ver
PRODUCTVERSION $ver
FILEFLAGSMASK 0x3f
FILEFLAGS 0x0
FILEOS 0x40004
FILETYPE 0x2
FILESUBTYPE 0x0
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "DroidDeck"
VALUE "FileDescription", "dd-directinput controller API (DroidDeck)"
VALUE "FileVersion", "${ver//,/.}"
VALUE "InternalName", "$2"
VALUE "LegalCopyright", "GPL-3.0"
VALUE "OriginalFilename", "$2.dll"
VALUE "ProductName", "DroidDeck"
VALUE "ProductVersion", "${ver//,/.}"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
EOF
"$(windres_for "$1")" "$rc" -O coff -o "$obj"
echo "$obj"
}
# stdcall argument bytes of every export (i386 decoration).
I386_DECOR='DllMain:12 XInputGetState:8 XInputSetState:8 XInputGetStateEx:8 XInputGetCapabilities:12 XInputGetDSoundAudioDeviceGuids:12 XInputGetBatteryInformation:12 XInputGetKeystroke:12 XInputWaitForGuideButton:12 XInputEnable:4 XInputCancelGuideButtonWait:4 XInputPowerOffController:4 XInputGetAudioDeviceIds:20 XInputGetCapabilitiesEx:16'
def_for() { # def_for <arch> <dll> <tmpdir> -> path of the .def to link with
local src="$HERE/def/$2.def"
if [ "$1" != i386 ]; then echo "$src"; return; fi
awk -v table="$I386_DECOR" 'BEGIN { n = split(table, a, " "); for (i = 1; i <= n; i++) { split(a[i], kv, ":"); m[kv[1]] = kv[2] } }
($1 in m) { sub($1, $1 "@" m[$1]) } { print }' "$src" > "$3/$2.def"
echo "$3/$2.def"
}
# expected "<ordinal> <name|->" lines of a .def (NONAME exports have no name)
def_exports() {
awk 'f && NF { ord = ""; for (i = 2; i <= NF; i++) if ($i ~ /^@[0-9]+$/) ord = substr($i, 2);
name = $1; for (i = 2; i <= NF; i++) if ($i == "NONAME") name = "-";
print (ord == "" ? "?" : ord), name }
/^EXPORTS/ { f = 1 }' "$1" | sort
}
# actual "<ordinal> <name|->" lines of a DLL (unused ordinals between the numbered ones have RVA 0 and are
# skipped). dll_exports <file> [main|hybrid]: llvm-readobj prints an ARM64X image's native view first, then
# its EC view inside "HybridObject {".
dll_exports() {
llvm-readobj --coff-exports "$1" | awk -v view="${2:-main}" '
/HybridObject \{/ { h = 1; next }
(view == "main" && h) || (view == "hybrid" && !h) { next }
/^ *Export \{/ { o = ""; n = "-"; r = "" }
/^ *Ordinal:/ { o = $2 }
/^ *Name:/ { if (NF >= 2) n = $2 }
/^ *RVA:/ { r = $2 }
/^ *\}/ { if (o != "" && r != "" && r != "0x0") print o, n; o = "" }' | sort
}
compare_exports() { # compare_exports <label> <file> <def> <view>
local want got
want="$(def_exports "$HERE/def/$3.def")"
got="$(dll_exports "$2" "$4")"
if [ "$want" != "$got" ]; then
printf 'want:\n%s\ngot:\n%s\n' "$want" "$got"
die "$1 exports differ from def/$3.def"
fi
}
check_dll() { # check_dll <arch> <dll name> <file>
local arch="$1" name="$2" f="$3" fmts fmt hyb machine lc imports bad res
fmts="$(llvm-readobj --file-headers "$f" | sed -n 's/^ *Format: //p')"
fmt="$(sed -n 1p <<<"$fmts")"
hyb="$(sed -n 2p <<<"$fmts")"
case "$arch" in
i386) [ "$fmt" = "COFF-i386" ] && [ -z "$hyb" ] || die "$f is $fmt${hyb:+ + $hyb}, want COFF-i386" ;;
arm64x)
# Native view: an ARM64 PE (what an ARM64 process maps). EC view: ARM64EC (what an x64 process sees
# through the ARM64X dynamic relocations). Both need the CHPE metadata.
machine="$(python3 -c 'import struct,sys; d=open(sys.argv[1],"rb").read(4096); o=struct.unpack_from("<I",d,0x3c)[0]; print(hex(struct.unpack_from("<H",d,o+4)[0]) if d[o:o+4]==b"PE\0\0" else "?")' "$f")"
[ "$machine" = 0xaa64 ] || die "$f PE header machine is $machine, want 0xaa64 (ARM64)"
[ "$hyb" = "COFF-ARM64EC" ] || die "$f has no ARM64EC hybrid view (formats: $(echo $fmts))"
lc="$(llvm-readobj --coff-load-config "$f")"
grep -q 'CHPEMetadata' <<<"$lc" || die "$f has no CHPE metadata"
grep -q 'Arm64X' <<<"$lc" || die "$f has no ARM64X dynamic relocations"
# One TLS directory serves both views and would list native ARM64 callbacks (see src/notls.c).
llvm-readobj --file-headers "$f" | grep -q 'TLSTableRVA: 0x[1-9a-fA-F]' && die "$f has a TLS directory"
fmt="$fmt/$hyb/$machine"
;;
esac
compare_exports "$arch/$name.dll" "$f" "$name" main
[ "$arch" != arm64x ] || compare_exports "$arch/$name.dll (EC view)" "$f" "$name" hybrid
imports="$(llvm-readobj --coff-imports "$f" | sed -n 's/^ *Name: //p' | tr 'A-Z' 'a-z' | sort -u)"
bad="$(grep -vxE 'kernel32\.dll|ucrtbase\.dll|msvcrt\.dll|api-ms-win-crt-[a-z0-9-]+\.dll' <<<"$imports" || true)"
[ -z "$bad" ] || die "$arch/$name.dll imports $(echo $bad) (only kernel32 and the C runtime are allowed)"
grep -aqF "$MARKER" "$f" || die "$arch/$name.dll lacks its '$MARKER' marker (launch.sh and the rumble path look for it)"
res="$(llvm-readobj --coff-resources "$f")"
grep -q 'Type: VERSIONINFO (ID 16)' <<<"$res" || die "$arch/$name.dll has no VERSIONINFO resource"
printf ' %-7s %-15s %6d bytes %-24s imports: %s\n' "$arch" "$name.dll" "$(stat -c %s "$f")" "$fmt" "$(echo $imports)"
}
# The objects and archives the toolchain links into an aarch64 DLL (CRT start-up, mingw/ucrt/kernel32 import
# libraries, compiler-rt), as absolute paths: the native view of an ARM64X image needs its own.
native_link_inputs() { # native_link_inputs <tmpdir>
printf 'int x;\n' > "$1/empty.c"
aarch64-w64-mingw32-clang -shared -o "$1/empty.dll" "$1/empty.c" -### 2>&1 | python3 -c '
import os, shlex, sys
line = [l for l in sys.stdin.read().splitlines() if "ld.lld" in l or "lld-link" in l]
if not line:
sys.exit("no link line in the -### output")
toks = shlex.split(line[-1])
dirs = [t[2:] for t in toks if t.startswith("-L")]
out = []
for t in toks[1:]:
if t.startswith("-l"):
name = t[2:]
hit = next((os.path.join(d, c) for d in dirs for c in ("lib%s.a" % name, "lib%s.dll.a" % name, "%s.lib" % name)
if os.path.isfile(os.path.join(d, c))), None)
if not hit:
sys.exit("cannot resolve -l%s in %s" % (name, dirs))
out.append(hit)
elif t.endswith((".o", ".a", ".obj", ".lib")) and os.path.isabs(t) and os.path.isfile(t):
out.append(t)
seen = set()
print("\n".join(p for p in out if not (p in seen or seen.add(p))))
'
}
# notls-*.o (src/notls.c) come before the CRT so the image gets no TLS directory.
link_arm64x() { # link_arm64x <out> <def> <ec obj> <native obj> <native inputs file> <ec notls obj> <native notls obj> <version res obj>
local nat=()
mapfile -t nat <"$5"
arm64ec-w64-mingw32-clang -shared -s -Wl,--no-insert-timestamp -Wl,-marm64xpe "-Wl,--Xlink=-defarm64native:$2" \
-o "$1" "$3" "$6" "$4" "$7" "$8" "$2" "${nat[@]}" -lkernel32
}
CFLAGS_COMMON="-std=gnu11 -O2 -Wall -Wextra -Werror -fno-strict-aliasing -I$HERE/src"
dlls() {
local out="${1:?usage: build.sh dlls <outdir>}" tmp arch name def
toolchain
mkdir -p "$out"; out="$(cd "$out" && pwd)"
tmp="$(mktemp -d)"
for arch in $ARCHES; do
mkdir -p "$out/$arch"
if [ "$arch" = arm64x ]; then
arm64ec-w64-mingw32-clang $CFLAGS_COMMON -DDDI_HYBRID=1 -c "$HERE/src/dd_directinput.c" -o "$tmp/ddi-arm64ec.o"
aarch64-w64-mingw32-clang $CFLAGS_COMMON -DDDI_HYBRID=1 -c "$HERE/src/dd_directinput.c" -o "$tmp/ddi-aarch64.o"
arm64ec-w64-mingw32-clang $CFLAGS_COMMON -c "$HERE/src/notls.c" -o "$tmp/notls-arm64ec.o"
aarch64-w64-mingw32-clang $CFLAGS_COMMON -c "$HERE/src/notls.c" -o "$tmp/notls-aarch64.o"
native_link_inputs "$tmp" >"$tmp/native-inputs"
for name in $DLLS; do
link_arm64x "$out/$arch/$name.dll" "$HERE/def/$name.def" "$tmp/ddi-arm64ec.o" "$tmp/ddi-aarch64.o" "$tmp/native-inputs" \
"$tmp/notls-arm64ec.o" "$tmp/notls-aarch64.o" "$(version_res "$arch" "$name" "$tmp")"
check_dll "$arch" "$name" "$out/$arch/$name.dll"
done
continue
fi
# i386: SSE2 so a full fence is mfence
i686-w64-mingw32-clang $CFLAGS_COMMON -msse2 -mfpmath=sse -c "$HERE/src/dd_directinput.c" -o "$tmp/ddi-i386.o"
for name in $DLLS; do
def="$(def_for "$arch" "$name" "$tmp")"
i686-w64-mingw32-clang -shared -s -Wl,--no-insert-timestamp -Wl,--kill-at -o "$out/$arch/$name.dll" "$tmp/ddi-i386.o" \
"$(version_res "$arch" "$name" "$tmp")" "$def" -lkernel32
check_dll "$arch" "$name" "$out/$arch/$name.dll"
done
done
rm -rf "$tmp"
say "$(ls "$out"/*/*.dll | wc -l) DLLs in $out"
}
host() {
local out="${1:?usage: build.sh host <outdir>}"
mkdir -p "$out"
"${CC:-cc}" -std=gnu11 -O2 -Wall -Wextra -Werror -pthread -I"$HERE/src" -o "$out/host_test" "$HERE/test/host_test.c"
say "host_test built for $(uname -m)"
}
case "${1:-}" in
toolchain) toolchain ;;
dlls) dlls "${2:-}" ;;
host) host "${2:-}" ;;
*) die "usage: $0 toolchain | dlls <outdir> | host <outdir>" ;;
esac