XLib 窗口名称问题

发布于 2025-01-06 23:52:36 字数 1677 浏览 3 评论 0原文

4天以来,我尝试了解XLib是如何工作的,我终于明白了这一点。 我试图制作一个简短的程序来检索打开的窗口的名称。 为此,我创建了两个函数:

Window *list (Display *disp, unsigned long *len) {
    Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
    int form;
    unsigned long remain;
    unsigned char *list;

    if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW,
                &type,&form,len,&remain,&list) != Success) {
        return 0;
    }

    return (Window*)list;
}

因此,第一个函数返回所有窗口的窗口对象。 然后,我创建了一个函数来从所有这些窗口中检索名称。

char *name (Display *disp, Window win) {
    Atom prop = XInternAtom(disp,"WM_NAME",False), type;
    int form;
    unsigned long remain, len;
    unsigned char *list;


    if (XGetWindowProperty(disp,win,prop,0,1024,False,XA_STRING,
                &type,&form,&len,&remain,&list) != Success) {

        return NULL;
    }

    return (char*)list;
}

这个函数工作正常,一个 main.c 示例:

int main(int argc, char* argv[]){
    int i;
    unsigned long len;
    XKeyEvent esend;
    Display *disp = XOpenDisplay(NULL);
    Window *list;
    char *name;

        list = (Window*)list(disp,&len);
    for (i=0;i<(int)len;i++) {
        name = name(disp,list[i]);
        printf("%d :  %s \n",i,name);
        free(name);
        }
}

并且,它工作得很好,除了 Skype 窗口之外,它返回:

1 : Xlib 编程手册:键盘和指针事件 - Google Chrome

2 : Debian Web [En fonction] - Oracle VM VirtualBox

3 : XChat: necromoine @ / (+CSTfnst 10:2)

4 :

5 : root@root-Laptop: ~

6 :

并且,数字 4和 6 是空白的(我实际上有两个打开的 Skype 窗口)。

你能帮我一下吗?

since 4 days, I try to see how XLib works, and I have finally understood that.
Si I tried to make a short program wich retrieve open window's name.
For that, I created 2 functions :

Window *list (Display *disp, unsigned long *len) {
    Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
    int form;
    unsigned long remain;
    unsigned char *list;

    if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW,
                &type,&form,len,&remain,&list) != Success) {
        return 0;
    }

    return (Window*)list;
}

So, this first function return a window object of all the windows.
Then, I created a function to retrieve the name from all those windows.

char *name (Display *disp, Window win) {
    Atom prop = XInternAtom(disp,"WM_NAME",False), type;
    int form;
    unsigned long remain, len;
    unsigned char *list;


    if (XGetWindowProperty(disp,win,prop,0,1024,False,XA_STRING,
                &type,&form,&len,&remain,&list) != Success) {

        return NULL;
    }

    return (char*)list;
}

And this function works fine, an main.c example:

int main(int argc, char* argv[]){
    int i;
    unsigned long len;
    XKeyEvent esend;
    Display *disp = XOpenDisplay(NULL);
    Window *list;
    char *name;

        list = (Window*)list(disp,&len);
    for (i=0;i<(int)len;i++) {
        name = name(disp,list[i]);
        printf("%d :  %s \n",i,name);
        free(name);
        }
}

And, It works really fine, except for Skype windows it returns:

1 : Xlib Programming Manual: Keyboard and Pointer Events - Google Chrome

2 : Debian Web [En fonction] - Oracle VM VirtualBox

3 : XChat: necromoine @ / (+CSTfnst 10:2)

4 :

5 : root@root-Laptop: ~

6 :

And, the number 4 and 6 are blank (I actually have two opened skype window).

Can you help me please.

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

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

发布评论

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

评论(1

梦在深巷 2025-01-13 23:52:37

WM_NAME 不一定是简单的字符串。它可能是复合文本(不同类型的字符串),Skype 窗口实际上就是这种情况。您需要使用 AnyPropertyType 而不是 XA_STRING 来获取属性,然后根据实际类型进行格式化。查看 xprops 的源代码以了解它是如何完成的。

A WM_NAME is not necessarily a simple string. It could be a compound text (a different type of string), which is actually the case for Skype windows. You need to use AnyPropertyType instead of XA_STRING to get the property, then format according to actual type. Look at the source of xprops to see how it's done.

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