XCB Patterns
Much of the XCB API is generated automatically from XML protocol descriptions, and so you see a lot of repeated patterns. This makes it very easy to learn, once you get to grips with the basic concepts.
One of the most common patterns is for iterating over collections. The X11 protocol documentation frequently describes lists of other types in packet structures. XCB provides a structure each of these, looking something like this:
typedef struct { xcb_name_t *data; int rem; int index; } xcb_name_iterator_t;
In this structure, name will be replaced of whatever this is iterating over a list. For example, you can iterate over all of the screens attached to a given connection, like this:
for ( xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(connection)) ; iter.rem ; xcb_screen_next(&iter)) { // Do something }
The rem field of the iterator gives a count of the numbers that are remaining, and reaches zero at the end of the list. The data field always points to the next data elementin this case, an xcb_screen_t.