问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9364668/xlib-window-name-problems