如何使用libxcb获取顶级窗口及其名称

发布于 2025-01-22 23:33:12 字数 1972 浏览 1 评论 0原文

我有一个带有2个打开窗口的Linux桌面:一个终端和一个浏览器。我试图用libxcb获取那些窗口的名称。这是我在 httpps:///wwww.systutorials。 com/doc/linux/man/3-xcb_query_tree_reply/

这是我的代码:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <xcb/xcb.h>

void get_children(xcb_connection_t* c, xcb_window_t window, xcb_window_t** children, int* count)
{
    *count = 0;
    *children = NULL;

    auto cookie = xcb_query_tree(c, window);
    auto reply = xcb_query_tree_reply(c, cookie, NULL);
    if (reply)
    {
        *count = xcb_query_tree_children_length(reply);
        *children = xcb_query_tree_children(reply);

        free(reply);
    }
}

void get_name(xcb_connection_t* c, xcb_window_t window, char** name, int* length)
{
    *length = 0;
    *name = NULL;

    auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 0);
    auto reply = xcb_get_property_reply(c, cookie, NULL);
    if (reply)
    {
        *length = xcb_get_property_value_length(reply);
        *name = (char*)xcb_get_property_value(reply);

        free(reply);
    }
}

int main()
{
    auto c = xcb_connect(":0.0", NULL);
    auto screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
    auto rootWindow = screen->root;

    int nChildren;
    xcb_window_t* children;
    get_children(c, screen->root, &children, &nChildren);
    for (int i = 0; i < nChildren; i++)
    {
        auto wid = children[i];

        int length;
        char* name;
        get_name(c, wid, &name, &length);
        printf("%u %d\n", wid, length);
    }

    return 0;
}

这是我的代码:返回40个Windows,其名称为0

20971989 0
20971802 0
20972112 0
20972308 0
... (truncated for brevity)

。 -l 。

我在做什么错?

I have a Linux desktop with 2 open windows: a terminal and a browser. I'm trying to get the name of those windows with libxcb. Here's my code based on examples I found at https://www.systutorials.com/docs/linux/man/3-xcb_query_tree_reply/

Here's my code:

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <xcb/xcb.h>

void get_children(xcb_connection_t* c, xcb_window_t window, xcb_window_t** children, int* count)
{
    *count = 0;
    *children = NULL;

    auto cookie = xcb_query_tree(c, window);
    auto reply = xcb_query_tree_reply(c, cookie, NULL);
    if (reply)
    {
        *count = xcb_query_tree_children_length(reply);
        *children = xcb_query_tree_children(reply);

        free(reply);
    }
}

void get_name(xcb_connection_t* c, xcb_window_t window, char** name, int* length)
{
    *length = 0;
    *name = NULL;

    auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 0);
    auto reply = xcb_get_property_reply(c, cookie, NULL);
    if (reply)
    {
        *length = xcb_get_property_value_length(reply);
        *name = (char*)xcb_get_property_value(reply);

        free(reply);
    }
}

int main()
{
    auto c = xcb_connect(":0.0", NULL);
    auto screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
    auto rootWindow = screen->root;

    int nChildren;
    xcb_window_t* children;
    get_children(c, screen->root, &children, &nChildren);
    for (int i = 0; i < nChildren; i++)
    {
        auto wid = children[i];

        int length;
        char* name;
        get_name(c, wid, &name, &length);
        printf("%u %d\n", wid, length);
    }

    return 0;
}

This returns 40 windows all with their name's length of 0. For example:

20971989 0
20971802 0
20972112 0
20972308 0
... (truncated for brevity)

I'm trying to get something like the output of wmctrl -l.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2025-01-29 23:33:12

我弄清楚了这个问题。我需要在XCB_GET_PROPERTY函数调用中添加长度。以下代码有效。

auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 1000);

I figured out the problem. I needed to add a length to the xcb_get_property function call. The following code works.

auto cookie = xcb_get_property(c, 0, window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文