Skip to content

Instantly share code, notes, and snippets.

@dvdhrm
Created September 20, 2012 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvdhrm/3756232 to your computer and use it in GitHub Desktop.
Save dvdhrm/3756232 to your computer and use it in GitHub Desktop.
uinput test script for 200+ input devices
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment