创建 2 X11 windows 的简单 C 程序

Simple C Program that creates 2 X11 windows

我想在 linux 中创建 2 个 windows,我稍后会从一个单独的线程中绘制。我目前有一个不确定的错误,我创建的第二个 window 有时不会被创建(虽然没有错误)。

这是代码。

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;
}

我不太关心在 windows 中捕获输入,但我找到了一个包含 XSelectInput 和 XNextEvent(在事件循环中)的教程,如果没有其中任何一个,我无法完成这项工作。

这不是错误,而是一项功能。 .

虽然您巧妙地调用了两次 XNextEvent,但 X 协议是异步的,因此在您调用 XNextEvent 时服务器可能仍在设置实际 window,因此无事可做。

Tutorial here.