From deafaa8c923ba0189036badd8590c9e3e5eb3694 Mon Sep 17 00:00:00 2001 From: xaos Date: Mon, 11 Nov 2024 15:58:58 +0100 Subject: [PATCH] Support old uinput interface Try to use UI_GET_VERSION ioctl to check whether to fall back to old uinput interface (for kernels < 4.5) as described in [1] and [2]. [1]: kernel.org/doc/html/latest/input/uinput.html#uinput-old-interface [2]: github.com/torvalds/linux/blob/master/include/uapi/linux/uinput.h --- Daemon/ydotoold.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/Daemon/ydotoold.c b/Daemon/ydotoold.c index de09904..cc4b31d 100644 --- a/Daemon/ydotoold.c +++ b/Daemon/ydotoold.c @@ -149,19 +149,38 @@ static void uinput_setup(int fd, enum ydotool_uinput_setup_options setup_opt) { } } - static const struct uinput_setup usetup = { - .name = "ydotoold virtual device", - .id = { - .bustype = BUS_VIRTUAL, - .vendor = 0x2333, - .product = 0x6666, - .version = 1 + int rc, version; + rc = ioctl(fd, UI_GET_VERSION, &version); + + // https://kernel.org/doc/html/latest/input/uinput.html#uinput-old-interface + if (rc == 0 && version >= 5) { + static const struct uinput_setup usetup = { + .name = "ydotoold virtual device", + .id = { + .bustype = BUS_VIRTUAL, + .vendor = 0x2333, + .product = 0x6666, + .version = 1 + } + }; + + if (ioctl(fd, UI_DEV_SETUP, &usetup)) { + perror("UI_DEV_SETUP ioctl failed"); + exit(2); } - }; + } else { + // see /include/uapi/linux/uinput.h + static const struct uinput_user_dev uud = { + .name = "ydotoold virtual device", + .id = { + .bustype = BUS_VIRTUAL, + .vendor = 0x2333, + .product = 0x6666, + .version = 1 + } + }; - if (ioctl(fd, UI_DEV_SETUP, &usetup)) { - perror("UI_DEV_SETUP ioctl failed"); - exit(2); + write(fd, &uud, sizeof(uud)); } if (ioctl(fd, UI_DEV_CREATE)) {