XCB Custom Message to Event Loop

2019-09-15 15:50发布

I'm trying to send message to server, so it sends just back to my app's event loop.

On my main thread I have this event loop:

const int MY_CUST_MSG(877);
xcb_generic_event_t *event;
    while (event = xcb_wait_for_event(connection)) {
        switch (event->response_type & ~0x80) {
            case MY_CUST_MSG:
                    // do something
                break;
            default:
                // Unknown event type, ignore it
                debug_log("Unknown event: ", event->response_type);
        }
        free(event);
    }

In another thread I want to send trigger the event loop on the main thread with MY_CUST_MSG and send a the address to the std::string with it. Is this possible? My main event loop will then read that address as a string.

I tried this:

int main() {
    int rezconnect = xcb_connection_has_error(connection = xcb_connect(NULL, &default_screen));
    if (rezconnect) return rezconnect;

    xcb_screen_t *firstscreen;
    firstscreen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;

    rootwin = firstscreen->root;


    // create InputOnly window
    xcb_window_t msgw = xcb_generate_id(connection);
    const uint32_t values[] = { true };
    xcb_create_window(connection, 0, msgw, rootwin, 0, 0, 10, 10, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_CW_OVERRIDE_REDIRECT, values);


    std::string* payload_ptr = new std::string("rawr rawr rawr");

    xcb_client_message_event_t event;

    event.response_type = XCB_CLIENT_MESSAGE;
    event.format = 32;
    event.type = 877; // A__NET_WM_DESKTOP;
    event.window = msgw;
    event.data.data32[0] = payload_ptr;

    xcb_send_event(connection, 0, rootwin, XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY, (const char *)&event);


    xcb_generic_event_t *e;
    while (e = xcb_wait_for_event(connection)) {
        switch (e->response_type & ~0x80) {
            case 877: {
                xcb_client_message_event_t *ce = (xcb_client_message_event_t *)e;

                std::unique_ptr<std::string> payload_ptr(reinterpret_cast<std::string*>(ce.data.data32[0]));
                std::string payload_str = *payload_ptr;

                debug_log("got payload_str:", payload_str);
            }
            default:
                // Unknown e type, ignore it
                debug_log("Unknown e: ", e->response_type);
        }
        free(e);
    }

    xcb_disconnect(connection);

    return 0;
}

0条回答
登录 后发表回答