如何戒烟的xlib的XNextEvent例行的阻塞 [英] how to quit the blocking of xlib's XNextEvent
问题描述
在Windows下,图形用户界面线程通常所说的GetMessage等待消息,
当另一个线程中使用PoseMessage把一个消息到队列中,那么
GUI线程将返回的GetMessage(退出阻塞)。
Under windows, the GUI thread usually call GetMessage to waiting for message, when another thread use PoseMessage put a message into the queue, then the GUI thread will return GetMessage (quit blocking).
有谁可以告诉我,当我使用XNextEvent例行Xwindows下,以等待
事件中,我怎么能唤醒的图形用户界面在另一个线程的线程。有一些
像PoseMessage API可以使用吗?
Does anyone can tell me, when I use XNextEvent under XWindows to waiting for event, how can I "wakeup" the GUI thread in another thread. Is there some API like PoseMessage I can use ?.
推荐答案
没有。这就是为什么大多数的UI框架(GTK,KDE等)使用自定义主循环才能够监听多个事件源。
No. This is why most UI frameworks (Gtk, KDE, etc) use custom main loops to be able to listen for more event sources.
在内部,XNextEvent例行使用插座,所以它调用选择()
来知道什么时候输入可用。所以,你能:呼叫 ConnectionNumber(显示)
来得到你需要传递的文件描述符选择()
Internally, XNextEvent uses a socket, so it calls select()
to know when input is available. So can you: Call ConnectionNumber(display)
to get the file descriptor that you need to pass select()
这可以让你听好文件描述符。
That allows you to listen for several file descriptors.
样$ C $从 HTTP C://www.linuxquestions。组织/问题/ showthread.php?p = 2431345#post2431345
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Display *dis;
Window win;
int x11_fd;
fd_set in_fds;
struct timeval tv;
XEvent ev;
int main() {
dis = XOpenDisplay(NULL);
win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 256, 256, \
0, BlackPixel (dis, 0), BlackPixel(dis, 0));
// You don't need all of these. Make the mask as you normally would.
XSelectInput(dis, win,
ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
ButtonPressMask | ButtonReleaseMask | StructureNotifyMask
);
XMapWindow(dis, win);
XFlush(dis);
// This returns the FD of the X11 display (or something like that)
x11_fd = ConnectionNumber(dis);
// Main loop
while(1) {
// Create a File Description Set containing x11_fd
FD_ZERO(&in_fds);
FD_SET(x11_fd, &in_fds);
// Set our timer. One second sounds good.
tv.tv_usec = 0;
tv.tv_sec = 1;
// Wait for X Event or a Timer
int num_ready_fds = select(x11_fd + 1, &in_fds, NULL, NULL, &tv);
if (num_ready_fds > 0)
printf("Event Received!\n");
else if (num_ready_fds == 0)
// Handle timer here
printf("Timer Fired!\n");
else
printf("An error occured!\n");
// Handle XEvents and flush the input
while(XPending(dis))
XNextEvent(dis, &ev);
}
return(0);
}
这篇关于如何戒烟的xlib的XNextEvent例行的阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!