From e0a64d75c7018ffa479abc1c893da91aa1f5a048 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Mon, 20 Apr 2020 15:42:32 +0200 Subject: [PATCH] added test folder --- tests/ioctl.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/ioctl.c diff --git a/tests/ioctl.c b/tests/ioctl.c new file mode 100644 index 0000000..3503ec3 --- /dev/null +++ b/tests/ioctl.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define test_bit(yalv, abs_b) ((((char *)abs_b)[yalv/8] & (1< 0) + +const char ev_root[] = "/dev/input/"; + +int main (void) +{ + /* Open the event directory */ + DIR *ev_dir = opendir(ev_root); + if (!ev_dir) + exit(-1); + + for (;;) { + struct dirent *file_ent; + char ev_path[sizeof(ev_root) + NAME_MAX + 1]; + int tmp_fd; + unsigned char evtype_b[EV_MAX]; + + if ((file_ent = readdir(ev_dir)) == NULL) + break; + /* Filter out non character devices */ + if (file_ent->d_type != DT_CHR) + continue; + + /* Compose absolute path from relative */ + memset(ev_path, 0, sizeof(ev_path)); + strncpy(ev_path, ev_root, sizeof(ev_root) + NAME_MAX); + strncat(ev_path, file_ent->d_name, sizeof(ev_root) + NAME_MAX); + fprintf(stderr, "%s\n", ev_path); + + /* Open device and check if it can give key events otherwise ignore it */ + tmp_fd = open(ev_path, O_RDONLY | O_NONBLOCK); + if (tmp_fd < 0) { + fprintf(stdout, "Could not open device %s\n", ev_path); + continue; + } + fprintf(stderr, "%s\n", ev_path); + + memset(evtype_b, 0, sizeof(evtype_b)); + if (ioctl(tmp_fd, EVIOCGBIT(0, EV_MAX), evtype_b) < 0) { + fprintf(stdout, "Could not read capabilities of device %s\n", + ev_path); + close(tmp_fd); + continue; + } + fprintf(stderr, "%s\n", ev_path); + + if (test_bit(EV_KEY, evtype_b)) + fprintf(stdout, "device %s has it!\n", ev_path); + + + fprintf(stdout, "\n\n\n\n"); + close(tmp_fd); + } + + printf("%d", EV_MAX); + closedir(ev_dir); +}