cef68cc9a3fa1f68b58f3587db4c0ad51af45906
[mikachu/openbox.git] / tests / icons.cpp
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include <X11/Xatom.h>
4 #include <X11/cursorfont.h>
5 #include <cstdlib>
6 #include <cstdio>
7 #include <cassert>
8
9 Window findClient(Display *d, Window win)
10 {
11   Window r, *children;
12   unsigned int n;
13   XQueryTree(d, win, &r, &r, &children, &n);
14   for (unsigned int i = 0; i < n; ++i) {
15     Window w = findClient(d, children[i]);
16     if (w) return w;
17   }
18
19   // try me
20   Atom state = XInternAtom(d, "WM_STATE", true);
21   Atom ret_type;
22   int ret_format;
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)
30     return None;
31   return win; // found it!
32 }
33
34 int main(int argc, char **argv)
35 {
36   Display *d = XOpenDisplay(NULL);
37   int s = DefaultScreen(d);
38   Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", true);
39   Atom ret_type;
40   unsigned int winw = 0, winh = 0;
41   int ret_format;
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];
46
47   printf("Click on a window with an icon...\n");
48   
49   //int id = strtol(argv[1], NULL, 16);
50   XUngrabPointer(d, CurrentTime);
51   Window id;
52   Cursor cur = XCreateFontCursor(d, XC_crosshair);
53   XGrabPointer(d, RootWindow(d, s), false, ButtonPressMask, GrabModeAsync,
54                GrabModeAsync, None, cur, CurrentTime);
55   XEvent ev;
56   while (true) {
57     XNextEvent(d, &ev);
58     if (ev.type == ButtonPress) {
59       XUngrabPointer(d, CurrentTime);
60       id = ev.xbutton.subwindow;
61       id = findClient(d, id);
62       break;
63     }
64   }
65
66   printf("Using window 0x%lx\n", id);
67   
68   long offset = 0;
69   int image = 0;
70   
71   do {
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");
78       return 1;
79     }
80     unsigned int w = prop_return[image][0];
81     XFree(prop_return[image]);
82
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");
89       return 1;
90     }
91     unsigned int h = prop_return[image][0];
92     XFree(prop_return[image]);
93
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");
100       return 1;
101     }
102     offset += w*h;
103
104     printf("Image dimentions: %d, %d\n", w, h);
105   
106     i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
107                             ZPixmap, 0, NULL, w, h, 32, 0);
108     assert(i[image]);
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];
116
117       // background color
118       unsigned char bgr = 40;
119       unsigned char bgg = 0x8f;
120       unsigned char bgb = 40;
121       
122       r = bgr + (r - bgr) * alpha / 256;
123       g = bgg + (g - bgg) * alpha / 256;
124       b = bgb + (b - bgb) * alpha / 256;
125
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;
129     }
130
131     winw += w;
132     if (h > winh) winh = h;
133
134     ++image;
135   } while (ret_bytesleft > 0 && image < MAX_IMAGES);
136   
137   Window win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
138                                    0, 0, 0);
139   assert(win);
140   XMapWindow(d, win);
141
142   Pixmap p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
143   XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
144
145   unsigned int x = 0;
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);
149     x += i[j]->width;
150     XFree(prop_return[j]);
151     XDestroyImage(i[j]);
152   }
153     
154   XSetWindowBackgroundPixmap(d, win, p);
155   XClearWindow(d, win);
156
157   XFlush(d);
158
159   getchar();
160
161   XFreePixmap(d, p);
162   XCloseDisplay(d);
163 }