Cpp xcb

From wikinotes

XCB is the successort to Xorg's Xlib. It is smaller, faster, and has more of an emphasis on asynchronous execution.


Documentation

Documentation
man xcb_* man pages are probably the best source of documentation.
public API docs https://xcb.freedesktop.org/PublicApi/
doxygen docs https://xcb.freedesktop.org/manual/modules.html
intro to xcb https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html
/usr/include/xcb/*.h most dependable source of detailed info.
Examples
so - get output info https://stackoverflow.com/questions/22108822/how-do-i-get-the-resolution-of-randr-outputs-through-the-xcb-randr-extension/27141466#27141466
xedgewarp src https://github.com/Airblader/xedgewarp
i3wm src

bindings

python xcffib

Compilation

Make sure to add `pkg-config --cflags --libs xcb` to your Makefile's CPATH.

Some of xcb's headers make use of other libraries which must be linked against. For example, randr must be included. You'll see it in /usr/lib/libxcb-randr.so .

pkg-config --cflags --libs xcb xcb-randr

Connection

#include <iostream>
#include <xcb/xcb.h>

int 
main(int argc, char *argv[])
{
    // var declarations
    xcb_connection_t            * conn;
    const xcb_setup_t           * xsetup;
    int                           default_screen_num;

    // connect, get xsetup obj, disconnect
    conn = xcb_connect(NULL, &default_screen_num);
    xsetup = xcb_get_setup(conn);
    xcb_disconnect(conn);
    
    return(0);
}

Screens

get vector containing all screens


#include <iostream>
#include <vector>
#include <xcb/xcb.h>

std::vector<xcb_screen_t*>
get_screens(const xcb_setup_t  * xsetup)
{
    std::vector<xcb_screen_t*>  screens;
    xcb_screen_iterator_t       screen_iterator;
    xcb_screen_t                *screen;
    int                         screen_count;
    int                         screen_num;

    // define screens array (sizeof screen count)
    screen_count = xcb_setup_roots_length(xsetup);

    // get all screens
    screen_num = screen_count;
    screen_iterator = xcb_setup_roots_iterator(xsetup);
    for (; screen_iterator.rem; --screen_num, xcb_screen_next(&screen_iterator))
    {
        screen = screen_iterator.data;
        std::cout << screen_num << std::endl;
        screens.push_back(screen);
        if (screen_num == 0){
            break;
        }
    }
    return screens;
}

general screen info

xcb_connection_t            * conn;
int                           default_screen_num;
conn = xcb_connect(NULL, &default_screen_num);
// default_screen_num is now set

void
  print_general_info(
      xcb_connection_t   * conn, 
      const xcb_setup_t  * xsetup,
      int                  default_screen_num
  )
{
    xcb_screen_t                  * screen;
    int                             screen_count;
    xcb_get_input_focus_cookie_t    focus_request;
    xcb_get_input_focus_reply_t   * focus_reply;
    xcb_window_t                    focus_window;

    screen = get_first_screen(xsetup);
    screen_count = xcb_setup_roots_length(xsetup);

    focus_request = xcb_get_input_focus(conn);
    focus_reply = xcb_get_input_focus_reply(conn, focus_request, NULL);
    focus_window = focus_reply->focus;

    printf("============\n");
    printf("General Info\n");
    printf("============\n");
    printf("default screen: %d\n", default_screen_num);
    printf("screen count: %d\n", screen_count);
    printf("window with focus: %d\n", focus_window);
}

specific screen info

void 
  print_screen_info(
      const xcb_setup_t  * xsetup,
      xcb_screen_t       * screen
  )
{
    printf("\n");
    printf("==============\n");
    printf("Screen (%p)\n", &screen);
    printf("==============\n");
    printf("root-window-id: %d\n", screen->root);
    printf("resolution: %dx%d\n", screen->width_in_pixels, screen->height_in_pixels);
    printf("\n");
}

Windows

general window info

void 
  print_window_info(
      xcb_connection_t  *conn, 
      xcb_window_t      window
  )
{
    xcb_get_geometry_cookie_t geom_request;
    xcb_get_geometry_reply_t *geom;

    geom_request = xcb_get_geometry(conn, window);
    geom = xcb_get_geometry_reply(conn, geom_request, NULL);

    printf("================\n");
    printf("Window id(%d)\n", window);
    printf("================\n");
    printf("pos: (%d, %d)\n", geom->x, geom->y);
    printf("resolution: %dx%d\n", geom->width, geom->height);
}