"""Compile the real SDL axis pipeline: settings must not admit a held phantom stick. Authored device samples exercise inversion, rest/crossing recovery, independent devices and every dead zone. No controller, game data or global input is used. """ import argparse from pathlib import Path import subprocess import tempfile RUN_ALL_ARGS = ["--cc", "gcc"] ROOT = Path(__file__).resolve().parents[1] def function(text, marker): start = text.index(marker) end = text.index("{", start) + 1 depth = 1 while depth: depth += (text[end] == "{") - (text[end] == "}") end += 1 return text[start:end] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--cc", default="gcc") parser.add_argument("--source", type=Path, default=ROOT) args = parser.parse_args() pad = (args.source / "host/pad_sdl.c").read_text(encoding="utf-8") hid = (args.source / "host/nn_hid.c").read_text(encoding="utf-8") start = pad.index(" const int inv_x = rx_option(16, 0);") end = pad.index(" for (int k = 0; k < 4; k++)\n if ((v[k]", start) fixture = r'''#include #include #include enum { AX_LX, AX_LY, AX_RX, AX_RY }; static struct { unsigned rested; signed char held[4]; } pads[2]; static int options[20]; static int rx_option(int id, int fallback) { (void)fallback; return options[id]; } static int16_t hos_sdl_pad_axis(void *gc, int axis) { return ((int16_t *)gc)[axis]; } static int hos_stick_own(int stick) { (void)stick; return -1; } /* (no dead zone of a stick's own: the Stick dead zone row's) */ ''' + "\n".join((function(hid, "int32_t hos_stick_axis(int16_t v, int zone) {"), function(hid, "int32_t hos_stick_axis_own("), function(hid, "void hos_filter_stick("), function(pad, "static int32_t stick_value("), function(pad, "static int stick_rests("))) + r''' static void poll(int i, int16_t sample[4], int32_t output[4]) { void *gc = sample; ''' + pad[start:end] + r''' memcpy(output, v, sizeof v); } #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0) static int expected(int raw, int axis) { int value = raw < -32767 ? -32767 : raw; if (axis == AX_LY || axis == AX_RY) value = -value; if ((axis == AX_RX && options[16]) || (axis == AX_RY && options[17])) value = -value; return value; } int main(void) { const int cuts[6] = {7849, 1638, 3277, 4915, 6553, 9830}; unsigned scenarios = 0; for (int zone = 0; zone < 6; zone++) for (int axis = AX_RX; axis <= AX_RY; axis++) { const int levels[4] = {32767, -32768, cuts[zone] + 1, -cuts[zone] - 1}; for (unsigned level = 0; level < 4; level++) { int16_t raw[4] = {0}, neutral[4] = {0}; int32_t output[4]; memset(pads, 0, sizeof pads); memset(options, 0, sizeof options); options[11] = zone; raw[axis] = (int16_t)levels[level]; /* Device 0 is stuck from connection; device 1 has actually rested. */ poll(1, neutral, output); for (int n = 0; n < 12; n++) { options[16] = n & 1; options[17] = (n >> 1) & 1; poll(0, raw, output); CHECK(output[axis] == 0); poll(1, raw, output); CHECK(output[axis] == expected(raw[axis], axis)); } /* Setting changes did not consume the real worn-stick crossing. */ raw[axis] = raw[axis] < 0 ? 25000 : -25000; poll(0, raw, output); CHECK(output[axis] == expected(raw[axis], axis)); raw[axis] = (int16_t)levels[level]; poll(0, raw, output); CHECK(output[axis] == expected(raw[axis], axis)); scenarios++; /* Neutral still admits an axis after any inversion history. */ memset(pads, 0, sizeof pads); poll(0, raw, output); CHECK(output[axis] == 0); options[axis == AX_RX ? 16 : 17] ^= 1; poll(0, raw, output); CHECK(output[axis] == 0); raw[axis] = (int16_t)cuts[zone]; poll(0, raw, output); CHECK(output[axis] == 0); raw[axis] = (int16_t)levels[level]; poll(0, raw, output); CHECK(output[axis] == expected(raw[axis], axis)); scenarios++; } /* Both right axes toggle independently; left axes retain their mapping. */ memset(pads, 0, sizeof pads); int16_t raw[4] = {0}; int32_t output[4]; poll(0, raw, output); raw[0] = 22000; raw[1] = -24000; raw[2] = -28000; raw[3] = 30000; for (int setting = 0; setting < 4; setting++) { options[16] = setting & 1; options[17] = (setting >> 1) & 1; poll(0, raw, output); for (int k = 0; k < 4; k++) CHECK(output[k] == expected(raw[k], k)); } scenarios++; } printf("PASS: %u axis/device/rest scenarios; all dead zones and inversion combinations\n", scenarios); return 0; } ''' with tempfile.TemporaryDirectory(prefix="stick-inversion-rest-") as folder: c = Path(folder) / "fixture.c" exe = Path(folder) / "fixture.exe" c.write_text(fixture, encoding="utf-8") subprocess.run([args.cc, "-std=c11", "-Wall", "-Wextra", "-Werror", "-Wno-deprecated-declarations", str(c), "-o", str(exe)], check=True) subprocess.run([str(exe)], check=True, timeout=30) if __name__ == "__main__": main()