import os from pathlib import Path import shutil import struct import subprocess import sys import tempfile import unittest SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" RING_MAGIC = 0x46494252 DESCRIPTION_MAGIC = 0x46495544 HEADER = 64 EVENT = 24 CAPACITY = 4096 DESCRIPTION = 1824 READER = r""" #include #include #include #include int main(void) { struct input_event events[64]; int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); if (fd < 0) { perror("open"); return 1; } while (read(fd, events, sizeof(events)) > 0) ; puts("ready"); fflush(stdout); char go; if (read(0, &go, 1) != 1) return 1; ssize_t got = read(fd, events, sizeof(events)); for (ssize_t i = 0; i < got / (ssize_t)sizeof(events[0]); i++) printf("%ld.%06ld %u %u %d\n", (long)events[i].time.tv_sec, (long)events[i].time.tv_usec, events[i].type, events[i].code, events[i].value); return got > 0 ? 0 : 1; } """ PROBE = r""" #include #include #include #include #include #include int main(void) { char name[128] = ""; struct input_id id = {0}; int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); if (fd < 0) return 1; int joydev = ioctl(fd, JSIOCGNAME(sizeof(name)), name); printf("JSIOCGNAME %d %s\n", joydev, joydev < 0 && errno == EINVAL ? "EINVAL" : name); if (ioctl(fd, EVIOCGID, &id) < 0 || ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) return 1; printf("%04x:%04x %s\n", id.vendor, id.product, name); return 0; } """ def can_build(bits): with tempfile.TemporaryDirectory() as tmp: probe = Path(tmp) / "probe.cpp" probe.write_text("#include \nint main() { return std::string().size(); }\n") return subprocess.run(["g++", bits, "-static-libstdc++", "-o", str(Path(tmp) / "probe"), str(probe)], capture_output=True).returncode == 0 @unittest.skipUnless(sys.platform == "linux" and os.uname().machine == "x86_64" and shutil.which("g++") and shutil.which("gcc"), "needs gcc and g++ on an x86-64 Linux host") class FakeinputRingTest(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.tmp, True) def ring(self): hooks = self.tmp / "hooks" hooks.mkdir() (hooks / "event16").touch() ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION) struct.pack_into("