1

In Xlib the structure XRRModeInfo contains, aside from nameLength field, the name itself. But in XCB the corresponding structure xcb_randr_mode_info_t only contains name_len, and there seems to be no function to get actual name string.

I do see all the mode names in the string returned by xcb_randr_get_screen_resources_names(), but they are all concatenated, and I don't know how to find the offset of a particular mode in this string.

So, how can I get the mode name using XCB?

4

1 回答 1

1

我确实在 xcb_randr_get_screen_resources_names() 返回的字符串中看到了所有模式名称,但它们都是串联的,我不知道如何在这个字符串中找到特定模式的偏移量。

您知道各个名称的长度,并且知道每个名称的长度,因此您只需计算字节数:

#include <stdio.h>
#include <xcb/randr.h>

int main()
{
        xcb_connection_t *c = xcb_connect(NULL, NULL);
        xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
        // TODO: Error handling
        // TODO: Checking if the RandR extension is available
        xcb_randr_get_screen_resources_reply_t *reply =
                xcb_randr_get_screen_resources_reply(c,
                                xcb_randr_get_screen_resources(c, screen->root),
                                NULL);
        xcb_randr_mode_info_iterator_t iter = xcb_randr_get_screen_resources_modes_iterator(reply);
        uint8_t *names = xcb_randr_get_screen_resources_names(reply);
        while (iter.rem) {
                xcb_randr_mode_info_t *mode = iter.data;
                printf("Mode %d has size %dx%d and name %.*s\n",
                                mode->id, mode->width, mode->height, mode->name_len, names);
                names += mode->name_len;
                xcb_randr_mode_info_next(&iter);
        }
        free(reply);
        xcb_disconnect(c);
        return 0;
}
于 2020-04-05T12:28:49.357 回答