interactive-evdev: switch from epoll(2) to poll(2)

Turns out FreeBSD supports evdev, so this toll can work on it; however
it does not support epoll, so switch to poll, which is portable.

Reported-by: Evgeniy Khramtsov <evgeniy@khramtsov.org>
Signed-off-by: Ran Benita <ran@unusedvar.com>
master
Ran Benita 2021-03-28 12:55:08 +03:00
parent 62b5b4a170
commit 6b65be4c4e
4 changed files with 22 additions and 29 deletions

View File

@ -31,6 +31,7 @@
#include <getopt.h> #include <getopt.h>
#include <limits.h> #include <limits.h>
#include <locale.h> #include <locale.h>
#include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -38,7 +39,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/epoll.h>
#include <linux/input.h> #include <linux/input.h>
#include "xkbcommon/xkbcommon.h" #include "xkbcommon/xkbcommon.h"
@ -313,33 +313,25 @@ read_keyboard(struct keyboard *kbd)
static int static int
loop(struct keyboard *kbds) loop(struct keyboard *kbds)
{ {
int i, ret = 1;
int epfd = -1;
struct keyboard *kbd; struct keyboard *kbd;
struct epoll_event ev; nfds_t nfds, i;
struct epoll_event evs[16]; struct pollfd *fds = NULL;
int ret;
epfd = epoll_create1(0); for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
if (epfd < 0) { fds = calloc(nfds, sizeof(*fds));
fprintf(stderr, "Couldn't create epoll instance: %s\n", if (fds == NULL) {
strerror(errno)); fprintf(stderr, "Out of memory");
goto out; goto out;
} }
for (kbd = kbds; kbd; kbd = kbd->next) { for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
memset(&ev, 0, sizeof(ev)); fds[i].fd = kbd->fd;
ev.events = EPOLLIN; fds[i].events = POLLIN;
ev.data.ptr = kbd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
if (ret) {
fprintf(stderr, "Couldn't add %s to epoll: %s\n",
kbd->path, strerror(errno));
goto out;
}
} }
while (!terminate) { while (!terminate) {
ret = epoll_wait(epfd, evs, 16, -1); ret = poll(fds, nfds, -1);
if (ret < 0) { if (ret < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
@ -348,18 +340,19 @@ loop(struct keyboard *kbds)
goto out; goto out;
} }
for (i = 0; i < ret; i++) { for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
kbd = evs[i].data.ptr; if (fds[i].revents != 0) {
ret = read_keyboard(kbd); ret = read_keyboard(kbd);
if (ret) { if (ret) {
goto out; goto out;
} }
} }
} }
}
ret = 0; ret = 0;
out: out:
close(epfd); free(fds);
return ret; return ret;
} }

View File

@ -14,7 +14,7 @@
.Nm .Nm
is a commandline tool to interactively debug XKB keymaps by listening to is a commandline tool to interactively debug XKB keymaps by listening to
.Pa /dev/input/eventX .Pa /dev/input/eventX
evdev devices (Linux). evdev devices.
. .
.Pp .Pp
.Nm .Nm

View File

@ -40,7 +40,7 @@ Interactive debugger for XKB keymaps for Wayland, see
.Xr xkbcli\-interactive\-wayland 1 .Xr xkbcli\-interactive\-wayland 1
. .
.It Ic interactive\-evdev .It Ic interactive\-evdev
Interactive debugger for XKB keymaps for evdev (Linux), see Interactive debugger for XKB keymaps for evdev, see
.Xr xkbcli\-interactive\-evdev 1 .Xr xkbcli\-interactive\-evdev 1
. .
.It Ic list .It Ic list

View File

@ -55,7 +55,7 @@ usage(void)
#endif #endif
#if HAVE_XKBCLI_INTERACTIVE_EVDEV #if HAVE_XKBCLI_INTERACTIVE_EVDEV
" interactive-evdev\n" " interactive-evdev\n"
" Interactive debugger for XKB keymaps for evdev (Linux)\n" " Interactive debugger for XKB keymaps for evdev\n"
"\n" "\n"
#endif #endif
#if HAVE_XKBCLI_COMPILE_KEYMAP #if HAVE_XKBCLI_COMPILE_KEYMAP