Magicloud
Magicloud

Reputation: 857

How to get the screen of a window with XCB?

In Xlib, XWindowAttributes structure contains a pointer to the screen that the window is on. But in the corresponding structure of XCB (xcb_get_window_attributes_reply_t), there is no such member.

What should I do?

Upvotes: 5

Views: 2998

Answers (3)

alpheratz
alpheratz

Reputation: 1

As @peoro pointed out, you should first get the root window of the screen containing the window, and then loop through all the screens until you found the one that matches the previous mentioned root window id.

Here's an example:

/* compile: cc main.c -o main -lxcb */

#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>

int
main(void)
{
    xcb_connection_t *conn;
    xcb_window_t window;
    xcb_get_geometry_cookie_t geometry_cookie;
    xcb_get_geometry_reply_t *geometry_reply;
    xcb_screen_t *screen;
    xcb_screen_iterator_t it;

    /* put the id of the window here */
    window = 0x1000002;
    conn = xcb_connect(NULL, NULL);
    geometry_cookie = xcb_get_geometry_unchecked(conn, window);
    geometry_reply = xcb_get_geometry_reply(conn, geometry_cookie, NULL);
    it = xcb_setup_roots_iterator(xcb_get_setup(conn));

    for (screen = it.data; it.rem != 0; xcb_screen_next(&it), screen = it.data) {
        if (screen->root == geometry_reply->root) {
            printf("window %lu found on screen with %hux%hu dimensions\n",
                window, screen->width_in_pixels, screen->height_in_pixels);
            break;
        }
    }

    free(geometry_reply);
    xcb_disconnect(conn);

    return 0;
}

Upvotes: 0

peoro
peoro

Reputation: 26060

I don't think there's a direct way to get the screen of a window.

What you can do is find the root window ancestor of your window, then iterate over all the screens until you find the one owning your root.

Upvotes: 0

bakkaa
bakkaa

Reputation: 683

xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;

you should read this tutorial http://xcb.freedesktop.org/tutorial/

Upvotes: 3

Related Questions