[Xcb] Best way to manage server responses in a non-blocking way?

Juan Lao Tebar nitzing at gmail.com
Mon Oct 26 16:08:10 PDT 2015


Hi,

I'm Juan Lao, new user in this mail list. I'm glad to meet you, guys.

I'm creating an intermediate layer between the XCB library and the
logic of my program.

- This layer must NOT block the execution of the main program. There
is a function in the layer that, when it's called from the main
program, it reads all the pending events and replies using
xcb_poll_for_event and xcb_poll_for_reply.

- The layer is object-oriented and implements a MVC architecture: you
register "listeners" and, when an event or a reply is received, the
layer calls your listener.

Ok this is, in a very summarized way, how I'm trying to implement it:

### Main program ###

void myEventReceived() {
    printf("an event has been received!");
}

void myReplyReceived() {
    printf("a reply has been received!");
}

int main() {
    layer_eventCallback = myEventReceived;
    layer_replyCallback = myReplyReceived;

    while (true) {
        layer_dispatchResponses();
        sleep(5);
    }
}

### Layer ###

void (*layer_eventCallback)();
void (*layer_replyCallback)();

void layer_dispatchEvents() {
    bool continueChecking = true;

    while (continueChecking) {
        continueChecking = (layer_nextEvent() || layer_nextReply());
    }
}

void layer_nextEvent() {
    xcb_generic_event_t* e = xcb_poll_for_event(layer_connection);

    if (e == NULL) {
        return false;
    }

    layer_eventCallback();
    free(e);
    return true;
}

void layer_nextReply() {
    // Analog as layer_nextEvent(), but using xcb_poll_for_reply
}

####################

So my questions here are:

1) Is this the correct way to read events and replies from the server?
I understand that if I call xcb_poll_for_event when the next response
is a reply, it will return a NULL. But I'm not sure if I will be able
to read the reply using xcb_poll_for_reply after that...

2) I can not find any example of xcb_poll_for_reply on internet. Is
there any documentation about it? I'm not sure about how to implement
layer_nextReply, since in XCB there is no "generic" replies. (maybe
iterating a list of stored cookies doing the propper castings?)

Well, I think that's all. Thank you guys in advance for any kind of
help on this subject :)

Greetings,
Juan Lao.


More information about the Xcb mailing list