关闭窗口时,xcb_poll_for_event未检测到XCB_CLIENT_MESSAGE事件

时间:2017-11-23 10:25:37

标签: c++ linux xcb

我将我的应用程序移植到Linux,我正在使用XCB库进行窗口处理。我需要检测窗口何时关闭,以便应用程序可以退出。但是,由于系统的设计方式,系统无法阻止主窗口循环。这在Windows中很简单,因为您只使用PeekMessage。但是,当我尝试使用XCB_CLIENT_MESSAGE检测xcb_poll_for_event时,xcb似乎不起作用。当我尝试注入WM_DELETE_WINDOW协议时,窗口上的关闭按钮实际上没有功能。

窗口设置:

// initialize XCB
this->connection = xcb_connect(NULL, NULL);
this->screen = xcb_setup_roots_iterator(xcb_get_setup(this->connection)).data;;

// create window
u32 mask = 0;
u32 values[1];
this->window = xcb_generate_id(this->connection);
mask = XCB_CW_EVENT_MASK;
values[0] = XCB_EVENT_MASK_EXPOSURE;
xcb_create_window(this->connection, 0, this->window, this->screen->root, 0, 0, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, this->screen->root_visual, mask, values);

// setup close handler event
xcb_intern_atom_cookie_t protocolCookie = xcb_intern_atom_unchecked(this->connection, 1, 12, "WM_PROTOCOLS");
xcb_intern_atom_reply_t* protocolReply = xcb_intern_atom_reply(this->connection, protocolCookie, 0);
xcb_intern_atom_cookie_t closeCookie = xcb_intern_atom_unchecked(this->connection, 0, 16, "WM_DELETE_WINDOW");
this->m_closeReply = xcb_intern_atom_reply(this->connection, closeCookie, 0);
xcb_change_property(this->connection, XCB_PROP_MODE_REPLACE, this->window, protocolReply->atom, 4, 32, 1, &(this->m_closeReply->atom));
free(protocolReply);

// map and flush
xcb_map_window(this->connection, this->window);
xcb_flush(this->connection);

消息循环:

// handle all incoming messages
xcb_generic_event_t* e;
while(e = xcb_poll_for_event(connection))
{
    // take action from message
    switch(e->response_type & ~0x80)
    {
        case XCB_EXPOSE:
            // invalidated
            xcb_flush(connection);
            break;
        case XCB_CLIENT_MESSAGE:
            // close window
            if(((xcb_client_message_event_t*)e)->data.data32[0] == (*this->m_closeReply).atom)
                return false;
            break;
    }

    // cleanup
    free(e);
}

当我用xcb_poll_for_event替换xcb_wait_for_event时窗口关闭完美无缺,但是窗口循环在等待消息时被阻止。我只需要知道在使用xcb_poll_for_event时从未检测到的事件我做错了什么。

0 个答案:

没有答案