public
Created

uinput test script for 200+ input devices

  • Download Gist
uinput_test.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
 
static const char *uinput_path = "/dev/uinput";
static const char *uinput_name = "Test Device";
 
static int uinput_create(void)
{
int ret, fd;
struct uinput_user_dev udev;
 
fd = open(uinput_path, O_RDWR | O_CLOEXEC | O_NONBLOCK);
if (fd < 0) {
fprintf(stderr, "cannot open uinput (%d): %m\n", errno);
return -errno;
}
 
memset(&udev, 0, sizeof(udev));
strncpy(udev.name, uinput_name, UINPUT_MAX_NAME_SIZE);
udev.id.bustype = BUS_VIRTUAL;
udev.id.vendor = 0x0306;
udev.id.product = 0x0101;
udev.id.version = 0;
 
ret = write(fd, &udev, sizeof(udev));
if (ret != sizeof(udev)) {
fprintf(stderr, "cannot init uinput (%d): %m\n", errno);
close(fd);
return -errno;
}
 
ret = ioctl(fd, UI_SET_EVBIT, EV_KEY);
if (ret) {
fprintf(stderr, "cannot set EV_KEY (%d): %m\n", errno);
close(fd);
return -errno;
}
 
ret = ioctl(fd, UI_SET_KEYBIT, KEY_A);
if (ret) {
fprintf(stderr, "cannot set KEY_A (%d): %m\n", errno);
close(fd);
return -errno;
}
 
ret = ioctl(fd, UI_DEV_CREATE);
if (ret) {
fprintf(stderr, "cannot create uinput (%d): %m\n", errno);
close(fd);
return -errno;
}
 
return fd;
}
 
static void uinput_destroy(int fd)
{
ioctl(fd, UI_DEV_DESTROY);
close(fd);
}
 
int main(int argc, char **argv)
{
int i, max, slp, step, *fds;
 
if (argc < 2)
max = 10;
else
max = atoi(argv[1]);
 
if (max <= 0) {
fprintf(stderr, "'max' argument smaller than 1: %d\n", max);
return EINVAL;
}
 
if (argc < 3)
slp = 30;
else
slp = atoi(argv[2]);
 
if (slp <= 0) {
fprintf(stderr, "'sleep' argument smaller than 1: %d\n", slp);
return EINVAL;
}
 
fprintf(stderr, "creating %d uinput devices\n", max);
 
fds = malloc(sizeof(int) * max);
if (!fds) {
fprintf(stderr, "cannot allocate memory for FD array\n");
return ENOMEM;
}
 
for (i = 0; i < max; ++i) {
fds[i] = uinput_create();
fprintf(stderr, "creating uinput device #%d: %s\n",
i, (fds[i] >= 0) ? "success" : "failed");
}
 
fprintf(stderr, "sleeping for %d seconds\n", slp);
step = slp / 5;
for (i = 0; i < step; ++i) {
fprintf(stderr, "%d seconds to go\n",
slp - i * 5);
sleep(5);
}
step = slp % 5;
if (step) {
fprintf(stderr, "%d seconds to go\n", step);
sleep(step);
}
 
fprintf(stderr, "destroying uinput devices\n");
for (i = 0; i < max; ++i) {
if (fds[i] >= 0)
uinput_destroy(fds[i]);
}
 
fprintf(stderr, "exiting\n");
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.