import os from pathlib import Path import shutil import socket 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 GAME = r""" #include #include #include #include #include #include static int rumble(int fd, unsigned short strong) { struct ff_effect effect; memset(&effect, 0, sizeof(effect)); effect.id = -1; effect.type = FF_RUMBLE; effect.u.rumble.strong_magnitude = strong; effect.u.rumble.weak_magnitude = 20000; if (ioctl(fd, EVIOCSFF, &effect) < 0) return -1; struct input_event play; memset(&play, 0, sizeof(play)); play.type = EV_FF; play.code = effect.id; play.value = 1; if (write(fd, &play, sizeof(play)) != sizeof(play)) return -1; return effect.id; } int main(int argc, char **argv) { int fd = open("/dev/input/event16", O_RDWR | O_NONBLOCK); if (fd < 0) return 1; if (argv[1][0] == 'p') { int other = open("/dev/input/event16", O_RDWR | O_NONBLOCK); if (other < 0 || rumble(fd, 40000) < 0 || rumble(other, 30000) < 0) return 2; close(fd); return 0; } if (argv[1][0] == 'k') { pid_t child = fork(); if (child == 0) { if (rumble(fd, 40000) < 0) _exit(2); close(fd); _exit(0); } int status = 0; waitpid(child, &status, 0); return WEXITSTATUS(status); } struct ff_effect effect; effect.id = rumble(fd, 40000); if (effect.id < 0) return 2; if (argv[1][0] == 'e') ioctl(fd, EVIOCRMFF, effect.id); else if (argv[1][0] == 'c') close(fd); else if (argv[1][0] == 'f') { pid_t child = fork(); if (child == 0) { close(fd); _exit(0); } waitpid(child, NULL, 0); } return 0; } """ @unittest.skipUnless(sys.platform == "linux" and shutil.which("g++") and shutil.which("gcc"), "needs Linux, gcc and g++") class FakeinputRumbleStopTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.tmp = Path(tempfile.mkdtemp()) cls.addClassCleanup(shutil.rmtree, cls.tmp, True) cls.library = cls.tmp / "libfakeinput.so" subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", "-std=c++17", "-static-libstdc++", "-static-libgcc", "-Wl,--exclude-libs,ALL", "-o", str(cls.library), str(SOURCE), "-ldl"], check=True) cls.game = cls.tmp / "game" (cls.tmp / "game.c").write_text(GAME) subprocess.run(["gcc", "-o", str(cls.game), str(cls.tmp / "game.c")], check=True) cls.hooks = cls.tmp / "hooks" cls.hooks.mkdir() (cls.hooks / "event16").touch() ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION) struct.pack_into("