Skip to content

Commit

Permalink
interactive-evdev: switch from epoll(2) to poll(2)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bluetech committed Mar 28, 2021
1 parent 62b5b4a commit 6b65be4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
45 changes: 19 additions & 26 deletions tools/interactive-evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#include <getopt.h>
#include <limits.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/epoll.h>
#include <linux/input.h>

#include "xkbcommon/xkbcommon.h"
Expand Down Expand Up @@ -313,33 +313,25 @@ read_keyboard(struct keyboard *kbd)
static int
loop(struct keyboard *kbds)
{
int i, ret = 1;
int epfd = -1;
struct keyboard *kbd;
struct epoll_event ev;
struct epoll_event evs[16];
nfds_t nfds, i;
struct pollfd *fds = NULL;
int ret;

epfd = epoll_create1(0);
if (epfd < 0) {
fprintf(stderr, "Couldn't create epoll instance: %s\n",
strerror(errno));
for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
fds = calloc(nfds, sizeof(*fds));
if (fds == NULL) {
fprintf(stderr, "Out of memory");
goto out;
}

for (kbd = kbds; kbd; kbd = kbd->next) {
memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
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;
}
for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
fds[i].fd = kbd->fd;
fds[i].events = POLLIN;
}

while (!terminate) {
ret = epoll_wait(epfd, evs, 16, -1);
ret = poll(fds, nfds, -1);
if (ret < 0) {
if (errno == EINTR)
continue;
Expand All @@ -348,18 +340,19 @@ loop(struct keyboard *kbds)
goto out;
}

for (i = 0; i < ret; i++) {
kbd = evs[i].data.ptr;
ret = read_keyboard(kbd);
if (ret) {
goto out;
for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
if (fds[i].revents != 0) {
ret = read_keyboard(kbd);
if (ret) {
goto out;
}
}
}
}

ret = 0;
out:
close(epfd);
free(fds);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/xkbcli-interactive-evdev.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
.Nm
is a commandline tool to interactively debug XKB keymaps by listening to
.Pa /dev/input/eventX
evdev devices (Linux).
evdev devices.
.
.Pp
.Nm
Expand Down
2 changes: 1 addition & 1 deletion tools/xkbcli.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Interactive debugger for XKB keymaps for Wayland, see
.Xr xkbcli\-interactive\-wayland 1
.
.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
.
.It Ic list
Expand Down
2 changes: 1 addition & 1 deletion tools/xkbcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ usage(void)
#endif
#if HAVE_XKBCLI_INTERACTIVE_EVDEV
" interactive-evdev\n"
" Interactive debugger for XKB keymaps for evdev (Linux)\n"
" Interactive debugger for XKB keymaps for evdev\n"
"\n"
#endif
#if HAVE_XKBCLI_COMPILE_KEYMAP
Expand Down

0 comments on commit 6b65be4

Please sign in to comment.