如何使用XCB获取窗口的屏幕?

4
在Xlib中,XWindowAttributes结构包含指向窗口所在屏幕的指针。但是在XCB对应的结构(xcb_get_window_attributes_reply_t)中没有这样的成员。
我该怎么办?
3个回答

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

您应该阅读此教程http://xcb.freedesktop.org/tutorial/

请您自行前往上述链接查看相关技术内容。

但是如果有多个屏幕,连接的窗口不在同一个屏幕上怎么办? - Magicloud
我不知道。我刚开始学习X11。也许你可以在X11文档中找到一些东西。 - bakkaa

0

我认为没有直接获取窗口屏幕的方法。

你可以找到你的窗口的根窗口祖先,然后迭代所有屏幕,直到找到拥有你的根窗口的那个屏幕。


0

正如@peoro所指出的,你应该先获取包含该窗口的屏幕的根窗口,然后循环遍历所有屏幕,直到找到与之前提到的根窗口id匹配的屏幕。

这里是一个例子:

/* 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接