4 #include <X11/cursorfont.h>
9 Window findClient(Display *d, Window win)
13 XQueryTree(d, win, &r, &r, &children, &n);
14 for (unsigned int i = 0; i < n; ++i) {
15 Window w = findClient(d, children[i]);
20 Atom state = XInternAtom(d, "WM_STATE", true);
23 unsigned long ret_items, ret_bytesleft;
24 unsigned long *prop_return;
25 XGetWindowProperty(d, win, state, 0, 1,
26 false, state, &ret_type, &ret_format,
27 &ret_items, &ret_bytesleft,
28 (unsigned char**) &prop_return);
29 if (ret_type == None || ret_items < 1)
31 return win; // found it!
34 int main(int argc, char **argv)
36 Display *d = XOpenDisplay(NULL);
37 int s = DefaultScreen(d);
38 Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", true);
40 unsigned int winw = 0, winh = 0;
42 unsigned long ret_items, ret_bytesleft;
43 const int MAX_IMAGES = 10;
44 unsigned long *prop_return[MAX_IMAGES];
45 XImage *i[MAX_IMAGES];
47 printf("Click on a window with an icon...\n");
49 //int id = strtol(argv[1], NULL, 16);
50 XUngrabPointer(d, CurrentTime);
52 Cursor cur = XCreateFontCursor(d, XC_crosshair);
53 XGrabPointer(d, RootWindow(d, s), false, ButtonPressMask, GrabModeAsync,
54 GrabModeAsync, None, cur, CurrentTime);
58 if (ev.type == ButtonPress) {
59 XUngrabPointer(d, CurrentTime);
60 id = ev.xbutton.subwindow;
61 id = findClient(d, id);
66 printf("Using window 0x%lx\n", id);
72 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
73 false, XA_CARDINAL, &ret_type, &ret_format,
74 &ret_items, &ret_bytesleft,
75 (unsigned char**) &prop_return[image]);
76 if (ret_type == None || ret_items < 1) {
77 printf("No icon found\n");
80 unsigned int w = prop_return[image][0];
81 XFree(prop_return[image]);
83 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
84 false, XA_CARDINAL, &ret_type, &ret_format,
85 &ret_items, &ret_bytesleft,
86 (unsigned char**) &prop_return[image]);
87 if (ret_type == None || ret_items < 1) {
88 printf("Failed to get height\n");
91 unsigned int h = prop_return[image][0];
92 XFree(prop_return[image]);
94 XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
95 false, XA_CARDINAL, &ret_type, &ret_format,
96 &ret_items, &ret_bytesleft,
97 (unsigned char**) &prop_return[image]);
98 if (ret_type == None || ret_items < w*h) {
99 printf("Failed to get image data\n");
104 printf("Image dimentions: %d, %d\n", w, h);
106 i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
107 ZPixmap, 0, NULL, w, h, 32, 0);
109 i[image]->byte_order = LSBFirst;
110 i[image]->data = (char*)prop_return[image];
111 for (unsigned int j = 0; j < w*h; j++) {
112 unsigned char alpha = (unsigned char)i[image]->data[j*4+3];
113 unsigned char r = (unsigned char) i[image]->data[j*4+0];
114 unsigned char g = (unsigned char) i[image]->data[j*4+1];
115 unsigned char b = (unsigned char) i[image]->data[j*4+2];
118 unsigned char bgr = 40;
119 unsigned char bgg = 0x8f;
120 unsigned char bgb = 40;
122 r = bgr + (r - bgr) * alpha / 256;
123 g = bgg + (g - bgg) * alpha / 256;
124 b = bgb + (b - bgb) * alpha / 256;
126 i[image]->data[j*4+0] = (char) r;
127 i[image]->data[j*4+1] = (char) g;
128 i[image]->data[j*4+2] = (char) b;
132 if (h > winh) winh = h;
135 } while (ret_bytesleft > 0 && image < MAX_IMAGES);
137 Window win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
142 Pixmap p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
143 XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
146 for (int j = 0; j < image; ++j) {
147 XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
148 i[j]->width, i[j]->height);
150 XFree(prop_return[j]);
154 XSetWindowBackgroundPixmap(d, win, p);
155 XClearWindow(d, win);