【问题标题】:Simple C Program that creates 2 X11 windows创建 2 个 X11 窗口的简单 C 程序
【发布时间】:2016-11-19 04:45:54
【问题描述】:

我想在 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(在事件循环中)的教程,如果没有这两个,我无法完成这项工作。

【问题讨论】:

    标签: c linux window x11


    【解决方案1】:

    这不是错误,而是功能。 You left out the event loop.

    虽然你巧妙地调用了 XNextEvent 两次,但 X 协议是异步的,所以当你调用 XNextEvent 时服务器可能仍在设置实际窗口,所以没有什么可做的。

    Tutorial here.

    【讨论】:

    • 谢谢伙计。我将 XNextEvent 排除在事件循环之外的原因是我不想让它阻塞。似乎我需要做的就是while(XPending(local_display)) { XNextEvent(local_display, &event)};
    猜你喜欢
    • 1970-01-01
    • 2022-11-13
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2021-10-28
    • 2021-02-03
    相关资源
    最近更新 更多