Noitidart Noitidart - 24 days ago 10
C++ Question

Trigger from thread to main thread in XCB Event Loop

Does anyone have any ideas on how I can get my main thread event loop which looks like:

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


To react to a message from a secondary thread?

Answer

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.

Comments