基本上你可以使用ConnectionNumber()
宏来获取XNextEvent()
正在读取的fd .这让我select()
自己等待Xlib fd 和其他一些fd 上的数据.现在select()
是阻止,而不是XNextEvent()
.select()
通过写入第二个fd,我可以轻松地从信号处理程序中解锁.
事件循环的伪代码:
/* Get X11 fd */
x11_fd = ConnectionNumber(display);
while(1) {
/* Create a File Description Set containing x11_fd and other_fd */
FD_ZERO(&in_fds);
FD_SET(x11_fd, &in_fds);
FD_SET(other_fd, &in_fds);
/* Wait for X Event or exit signal */
ret = select(nfds, &in_fds, ...);
if (FD_ISSET(other_fd, &in_fds) {
/* Do any cleanup and exit */
} else {
while (XEventsQueued(display, QueuedAlready) > 0) {
/* Process X events */
}
}
}
Run Code Online (Sandbox Code Playgroud)