xprop是一个命令行工具,用于查询和设置X Window的Property.
一些用法举例:
xprop -root WM_NAME
: 显示窗口名称xprop -id 0x200007
:列出指定窗口的全部Propertyxprop -id 0x200007 -set WM_NAME "Win Title"
:设定窗口的名称#include <X11/X.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xproto.h> #include <X11/Xatom.h> #include <iostream> #include <list> class XWindowPropertyPlayer { public: Window window; Display *display; Display *getXDisplay(){ return display;} Window getXWindow() {return window;} XWindowPropertyPlayer(){ connect(); } void connect(){ display = XOpenDisplay( NULL ); if(display==NULL){ std::cout<<"Error: connect to DISPLAY FAIL"<<std::endl; exit(0); } } void listWindowProperties(Window window){ int nProp; Atom *head = XListProperties(display, window, &nProp); for(int i=0;i<nProp;i++){ char *atom_name = XGetAtomName(display, *(head+i)); std::cout<<i<<" @ "<< atom_name <<std::endl; XFree(atom_name); } } void getProperty(Window window, Atom property){ Atom type; int format; unsigned long len, remaining; unsigned char* data; // TODO: check whether it is a X Window ID? int size = 64; // 64*4=256 bytes int result = XGetWindowProperty( display, window, property, 0, // Offset size, // Result length to return false, // Delete: false AnyPropertyType, // Requested type &type, &format, // Actual return type, format &len, // Actual return length &remaining, // Bytes remaining after the return &data ); if ( result != Success ) { std::cout<<"FAIL to XGetWindowProperty"<<std::endl; return; } if(remaining>0){ size = size*4 + remaining; XFree(data); result = XGetWindowProperty(display, window, property, 0, // Offset size, // Result length to return false, // Delete false AnyPropertyType, // Requested type &type, // Actual return type &format, // Actual return format &len, // Actual returned length. &remaining, // Bytes remaining after the return &data ); } char *atom_name = XGetAtomName(display,property); std::cout<<"INFO: "<< atom_name << " = " << data <<std::endl; XFree(atom_name); XFree(data); } void getProperty(Window window, const char *name){ Atom property = XInternAtom( getXDisplay(), name, false ); getProperty(window, property); } void setProperty(Window dstWnd, const char *name, const char *value){ const unsigned char *data = (const unsigned char *) value; int size = strlen(value); Atom property = XInternAtom( getXDisplay(), name, false ); XChangeProperty( getXDisplay(), dstWnd, property, XA_STRING, 8, PropModeReplace, (unsigned char *)data, size); XFlush(getXDisplay()); } }; //end class int main(int argc, char **argv){ if(argc>=3 && strcmp(argv[1],"props")==0){ XWindowPropertyPlayer player; Window wnd = (Window) atoi(argv[2]); player.listWindowProperties(wnd); return 0; } if(argc==4 && strcmp(argv[1],"prop")==0){ XWindowPropertyPlayer player; Window wnd = (Window) atoi(argv[2]); player.getProperty(wnd, argv[3]); return 0; } // guit prop 12345 name value if(argc==5 && strcmp(argv[1],"prop")==0){ XWindowPropertyPlayer player; Window wnd = (Window) atoi(argv[2]); player.setProperty(wnd, argv[3], argv[4]); return 0; } return 0; }