如何使用 XCB 获取模式名称?

How to get mode name using XCB?

在 Xlib 中,结构 XRRModeInfo 除了 nameLength 字段外,还包含 name 本身。但是在XCB中对应的结构xcb_randr_mode_info_t只包含name_len,而且好像没有获取实际名字字符串的函数

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

那么,如何使用 XCB 获取模式名称?

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.

你知道每个名字的长度,你知道每个名字的长度,所以你只需要计算字节数:

#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;
}