我想在linux中创建2个窗口,我稍后会从一个单独的线程中绘制。我目前有一个非确定性错误,我创建的第二个窗口有时无法创建(尽管没有错误)。
这是代码。
static void create_x_window(Display *display, Window *win, int width, int height)
{
int screen_num = DefaultScreen(display);
unsigned long background = WhitePixel(display, screen_num);
unsigned long border = BlackPixel(display, screen_num);
*win = XCreateSimpleWindow(display, DefaultRootWindow(display), /* display, parent */
0,0, /* x, y */
width, height, /* width, height */
2, border, /* border width & colour */
background); /* background colour */
XSelectInput(display, *win, ButtonPressMask|StructureNotifyMask);
XMapWindow(display, *win);
}
int main(void) {
XInitThreads(); // prevent threaded XIO errors
local_display = XOpenDisplay(":0.0");
Window self_win, remote_win;
XEvent self_event, remote_event;
create_x_window(local_display, &remote_win, 640,480);
// this line flushes buffer and blocks so that the window doesn't crash for a reason i dont know yet
XNextEvent(local_display, &remote_event);
create_x_window(local_display, &self_win, 320, 240);
// this line flushes buffer and blocks so that the window doesn't crash for a reason i dont know yet
XNextEvent(local_display, &self_event);
while (1) {
}
return 0;
}
我并不真正关心在窗口中捕获输入,但是我发现了一个有XSelectInput和XNextEvent(在事件循环中)的教程,而且我在没有这个的情况下无法完成这项工作。
答案 0 :(得分:1)
这不是一个错误,它是一个功能。 You left out the event loop
虽然您巧妙地调用了XNextEvent两次,但X协议是异步的,因此在您调用XNextEvent时服务器可能仍然在设置实际窗口,因此无需执行任何操作。