XLib 窗口名称问题
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 useAnyPropertyType
instead ofXA_STRING
to get the property, then format according to actual type. Look at the source ofxprops
to see how it's done.