mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
fix: restore Factorio ARM64 controller input through readv (#1195)
This commit is contained in:
1 parent
67ab3fa634
commit
92dbd1cd5a
3 files changed
+297
-24
No files matched your search
@@ -9,7 +9,7 @@ for bad in libstdc++.so.6 libgcc_s.so.1; do
|
||||
done
|
||||
syms() { "${READELF:-readelf}" -Ws "$1" | awk '$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'; }
|
||||
FAKE=$(syms "$D/libfakeinput.so")
|
||||
for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do
|
||||
for sym in open openat ioctl read readv close poll ppoll select stat fstat access scandir; do
|
||||
grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; }
|
||||
done
|
||||
if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then
|
||||
|
||||
@@ -18,24 +18,44 @@ READER = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
static int vector_count;
|
||||
|
||||
static ssize_t controller_read(int fd, void *buffer, size_t size)
|
||||
{
|
||||
if (!vector_count)
|
||||
return read(fd, buffer, size);
|
||||
struct iovec vectors[3] = {
|
||||
{buffer, sizeof(struct input_event)},
|
||||
{(char *)buffer + sizeof(struct input_event), 0},
|
||||
{(char *)buffer + sizeof(struct input_event), size - sizeof(struct input_event)},
|
||||
};
|
||||
if (vector_count == 1) {
|
||||
vectors[0].iov_len = size;
|
||||
return readv(fd, vectors, 1);
|
||||
}
|
||||
return readv(fd, vectors, 3);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
vector_count = argc > 1 ? (argv[1][0] == '1' ? 1 : 3) : 0;
|
||||
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)
|
||||
while (controller_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));
|
||||
ssize_t got = controller_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);
|
||||
@@ -65,6 +85,146 @@ int main(void)
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
READV_CHECKS = r"""
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct input_event events[64];
|
||||
int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK);
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
while (read(fd, events, sizeof(events)) > 0)
|
||||
;
|
||||
struct iovec vector = {events, sizeof(events)};
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EAGAIN)
|
||||
return 2;
|
||||
if (readv(fd, NULL, 0) != 0)
|
||||
return 3;
|
||||
vector.iov_len = 0;
|
||||
if (readv(fd, &vector, 1) != 0)
|
||||
return 4;
|
||||
vector.iov_len = sizeof(struct input_event) - 1;
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EINVAL)
|
||||
return 5;
|
||||
ssize_t (*invoke)(int, const struct iovec *, int) = readv;
|
||||
if (invoke(fd, NULL, -1) != -1 || errno != EINVAL)
|
||||
return 6;
|
||||
if (invoke(fd, NULL, 1025) != -1 || errno != EINVAL)
|
||||
return 7;
|
||||
if (invoke(fd, NULL, 1) != -1 || errno != EFAULT)
|
||||
return 8;
|
||||
long page = sysconf(_SC_PAGESIZE);
|
||||
void *area = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (area == MAP_FAILED)
|
||||
return 10;
|
||||
struct iovec *boundary = (struct iovec *)((char *)area + page - sizeof(struct iovec));
|
||||
*boundary = vector;
|
||||
if (mprotect((char *)area + page, page, PROT_NONE))
|
||||
return 11;
|
||||
if (invoke(fd, boundary, 2) != -1 || errno != EFAULT)
|
||||
return 12;
|
||||
if (invoke(fd, boundary + 1, 1) != -1 || errno != EFAULT)
|
||||
return 13;
|
||||
if (munmap(area, 2 * page))
|
||||
return 14;
|
||||
if (invoke(fd, boundary, 1) != -1 || errno != EFAULT)
|
||||
return 15;
|
||||
int pipefd[2];
|
||||
char text[5] = {0};
|
||||
struct iovec parts[2] = {{text, 1}, {text + 1, 3}};
|
||||
if (pipe(pipefd) || write(pipefd[1], "test", 4) != 4 ||
|
||||
readv(pipefd[0], parts, 2) != 4 || strcmp(text, "test"))
|
||||
return 9;
|
||||
puts("ok");
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
READV_DESTINATION_CHECKS = r"""
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct input_event events[64];
|
||||
int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK);
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
if (argc > 1)
|
||||
while (read(fd, events, sizeof(events)) > 0)
|
||||
;
|
||||
puts("ready");
|
||||
fflush(stdout);
|
||||
char go;
|
||||
if (read(0, &go, 1) != 1)
|
||||
return 2;
|
||||
long page = sysconf(_SC_PAGESIZE);
|
||||
char *area = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (area == MAP_FAILED || mprotect(area + page, page, PROT_NONE))
|
||||
return 3;
|
||||
struct iovec vector = {area + page, sizeof(struct input_event)};
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EFAULT)
|
||||
return 4;
|
||||
if (read(fd, vector.iov_base, vector.iov_len) != -1 || errno != EFAULT)
|
||||
return 5;
|
||||
if (mprotect(area, page, PROT_READ))
|
||||
return 6;
|
||||
vector.iov_base = area;
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EFAULT)
|
||||
return 7;
|
||||
if (mprotect(area, page, PROT_READ | PROT_WRITE) || munmap(area + page, page))
|
||||
return 8;
|
||||
vector.iov_base = area + page;
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EFAULT)
|
||||
return 9;
|
||||
vector.iov_base = area + page - 1;
|
||||
if (readv(fd, &vector, 1) != -1 || errno != EFAULT)
|
||||
return 15;
|
||||
vector.iov_base = area + page - sizeof(struct input_event);
|
||||
vector.iov_len = 2 * sizeof(struct input_event);
|
||||
if (readv(fd, &vector, 1) != sizeof(struct input_event))
|
||||
return 10;
|
||||
struct input_event *first = vector.iov_base;
|
||||
if (first->type != EV_KEY || first->code != BTN_A)
|
||||
return 11;
|
||||
struct iovec parts[2] = {{events, sizeof(struct input_event)}, {NULL, sizeof(struct input_event)}};
|
||||
if (readv(fd, parts, 2) != sizeof(struct input_event))
|
||||
return 12;
|
||||
if (events[0].type != (argc > 1 ? EV_ABS : EV_KEY) || events[0].code != (argc > 1 ? ABS_Y : BTN_B))
|
||||
return 13;
|
||||
vector.iov_base = events;
|
||||
vector.iov_len = sizeof(events);
|
||||
ssize_t got = readv(fd, &vector, 1);
|
||||
if (got != (argc > 1 ? 1 : 18) * sizeof(struct input_event) ||
|
||||
events[0].type != (argc > 1 ? EV_SYN : EV_KEY) || events[0].code != (argc > 1 ? SYN_REPORT : BTN_X))
|
||||
return 14;
|
||||
munmap(area, page);
|
||||
puts("ok");
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
OVERLAY_READ = r"""
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
ssize_t read(int fd, void *buffer, size_t size)
|
||||
{
|
||||
struct iovec vector = {buffer, size};
|
||||
return readv(fd, &vector, 1);
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def can_build(bits):
|
||||
@@ -82,15 +242,16 @@ class FakeinputRingTest(unittest.TestCase):
|
||||
self.tmp = Path(tempfile.mkdtemp())
|
||||
self.addCleanup(shutil.rmtree, self.tmp, True)
|
||||
|
||||
def ring(self):
|
||||
def ring(self, virtual=True):
|
||||
hooks = self.tmp / "hooks"
|
||||
hooks.mkdir()
|
||||
(hooks / "event16").touch()
|
||||
ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION)
|
||||
(hooks / ("event16" if virtual else "event0")).touch()
|
||||
ring = bytearray(HEADER + CAPACITY * EVENT + (DESCRIPTION if virtual else 0))
|
||||
struct.pack_into("<IIIIQQQ", ring, 0, RING_MAGIC, 2, EVENT, CAPACITY, 5, 0, 0)
|
||||
struct.pack_into("<IiHHHHI80s", ring, HEADER + CAPACITY * EVENT, DESCRIPTION_MAGIC, os.getpid(),
|
||||
3, 0x28de, 0x11ff, 1, 16, b"Microsoft X-Box 360 pad 0")
|
||||
path = hooks / ".uinput-event16"
|
||||
if virtual:
|
||||
struct.pack_into("<IiHHHHI80s", ring, HEADER + CAPACITY * EVENT, DESCRIPTION_MAGIC, os.getpid(),
|
||||
3, 0x28de, 0x11ff, 1, 16, b"Microsoft X-Box 360 pad 0")
|
||||
path = hooks / ".uinput-event16" if virtual else self.tmp / "ring0"
|
||||
path.write_bytes(bytes(ring))
|
||||
return hooks, path
|
||||
|
||||
@@ -106,11 +267,22 @@ class FakeinputRingTest(unittest.TestCase):
|
||||
subprocess.run(["gcc", bits, "-o", str(binary), str(self.tmp / (name + ".c"))], check=True)
|
||||
return library, binary
|
||||
|
||||
def read_through(self, bits):
|
||||
library, reader = self.build(bits, READER, "reader")
|
||||
hooks, path = self.ring()
|
||||
def read_through(self, bits, vectors=None, overlay=False, virtual=True, program=READER):
|
||||
if not virtual:
|
||||
program = program.replace("event16", "event0")
|
||||
library, reader = self.build(bits, program, "reader")
|
||||
hooks, path = self.ring(virtual=virtual)
|
||||
env = dict(os.environ, LD_PRELOAD=str(library), FAKE_EVDEV_DIR=str(hooks))
|
||||
process = subprocess.Popen([str(reader)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env, text=True)
|
||||
if not virtual:
|
||||
env["FAKE_EVDEV_MEMFD_PATHS"] = "0=" + str(path)
|
||||
if overlay:
|
||||
source = self.tmp / "overlay.c"
|
||||
source.write_text(OVERLAY_READ)
|
||||
overlay_library = self.tmp / "overlay.so"
|
||||
subprocess.run(["gcc", bits, "-shared", "-fPIC", "-o", str(overlay_library), str(source)], check=True)
|
||||
env["LD_PRELOAD"] = str(overlay_library) + ":" + str(library)
|
||||
command = [str(reader)] + ([str(vectors)] if vectors else [])
|
||||
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env, text=True)
|
||||
self.addCleanup(process.kill)
|
||||
self.assertEqual(process.stdout.readline(), "ready\n")
|
||||
with open(path, "r+b") as ring:
|
||||
@@ -131,6 +303,43 @@ class FakeinputRingTest(unittest.TestCase):
|
||||
self.assertEqual(self.read_through("-m64"),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_readv_reads_events_instead_of_the_backing_file(self):
|
||||
self.assertEqual(self.read_through("-m64", vectors=1),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_readv_reads_the_original_controller_ring(self):
|
||||
self.assertEqual(self.read_through("-m64", vectors=1, virtual=False),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_an_overlay_read_fallback_to_readv_reads_events_without_recursion(self):
|
||||
self.assertEqual(self.read_through("-m64", overlay=True),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_readv_reads_event_aligned_vectors_and_skips_empty_ones(self):
|
||||
self.assertEqual(self.read_through("-m64", vectors=3),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_a_32_bit_guest_reads_the_same_events_with_readv(self):
|
||||
self.assertEqual(self.read_through("-m32", vectors=1),
|
||||
"1700000000.250000 1 304 1\n1700000001.250000 3 1 -32768\n1700000002.250000 0 0 0\n")
|
||||
|
||||
def test_readv_preserves_nonblocking_errors_and_other_descriptors(self):
|
||||
library, probe = self.build("-m64", READV_CHECKS, "readv-checks")
|
||||
hooks, _ = self.ring()
|
||||
run = subprocess.run([str(probe)], capture_output=True, text=True, timeout=10,
|
||||
env=dict(os.environ, LD_PRELOAD=str(library), FAKE_EVDEV_DIR=str(hooks)))
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
self.assertEqual(run.stdout, "ok\n")
|
||||
|
||||
def test_readv_keyframes_handle_destination_faults_and_partial_progress(self):
|
||||
self.assertEqual(self.read_through("-m64", program=READV_DESTINATION_CHECKS), "ok\n")
|
||||
|
||||
def test_readv_ring_events_handle_destination_faults_and_partial_progress(self):
|
||||
self.assertEqual(self.read_through("-m64", vectors=1, program=READV_DESTINATION_CHECKS), "ok\n")
|
||||
|
||||
def test_a_32_bit_guest_handles_readv_destination_faults(self):
|
||||
self.assertEqual(self.read_through("-m32", program=READV_DESTINATION_CHECKS), "ok\n")
|
||||
|
||||
def test_an_event_node_rejects_joydev_requests_so_sdl_reads_its_id(self):
|
||||
library, probe = self.build("-m64", PROBE, "probe")
|
||||
hooks, _ = self.ring()
|
||||
|
||||
Reference in new issue
Block a user