Noitidart Noitidart - 9 months ago 75
C++ Question

XCB Custom Message to 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) {
// do something
// Unknown event type, ignore it
debug_log("Unknown event: ", event->response_type);

In another thread I want to send trigger the event loop on the main thread with
and send a
with it. Is this possible?

Answer Source

xcb_wait_for_event() waits for an event to be received from the server. You'd have to send a message to yourself, through the server, but I would suggest an alternative approach:

  1. Use xcb_file_descriptor() to get the underlying file descriptor for the X connection.

  2. Set up an internal pipe that your application can use to send messages to itself, between thread.

  3. Use xcb_poll_for_event() which is a non-blocking version of xcb_wait_for_event(), to implement a non-blocking check if there's an event that has been read, and if so, read it.

  4. Do a non-blocking read on your internal pipe, to check for any internal message from another thread.

  5. If neither step 3 or 4 produced a message, use poll() to wait for one or the other event.

You will also need to use xcb_flush() to flush any events manually, and xcb_connection_has_error() to check for a fatal connection error to the X server.

See the tutorial for more information.