Search code examples
selecteventsx11xlibkqueue

`select` works but `kqueue` doesn't when waiting on X11 connection


This blocks until there's an X11 event available:

int x11_fd = ConnectionNumber(display);
fd_set in_fds;
FD_ZERO(&in_fds);
FD_SET(x11_fd, &in_fds);
select(x11_fd + 1, &in_fds, NULL, NULL, NULL);

This just immediately returns:

int x11_fd = ConnectionNumber(display);
int kq = kqueue();
struct kevent ev;
EV_SET(&ev, x11_fd, EVFILT_READ, EV_ADD, 0, 0, 0);
kevent(kq, &ev, 1, NULL, 0, NULL);

Why? Am I using kqueue incorrectly?


Solution

  • I'm fairly sure I misunderstood the API of kqueue.

    The call shown in the OP only adds the filters to the queue, it doesn't wait on them. In order to do that, a separate array of kevents needs to be passed to the eventlist param.

    kevent(kq, NULL, 0, &ev, 1, NULL);