Introducing the icon cache.
[dana/openbox.git] / tests / icons.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    icons.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include <X11/Xlib.h>
20 #include <X11/Xutil.h>
21 #include <X11/Xatom.h>
22 #include <X11/cursorfont.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <glib.h>
27
28 Window findClient(Display *d, Window win)
29 {
30     Window r, *children;
31     unsigned int n, i;
32     Atom state = XInternAtom(d, "WM_STATE", True);
33     Atom ret_type;
34     int ret_format;
35     unsigned long ret_items, ret_bytesleft;
36     unsigned long *prop_return;
37
38     XQueryTree(d, win, &r, &r, &children, &n);
39     for (i = 0; i < n; ++i) {
40         Window w = findClient(d, children[i]);
41         if (w) return w;
42     }
43
44     // try me
45     XGetWindowProperty(d, win, state, 0, 1,
46                        False, state, &ret_type, &ret_format,
47                        &ret_items, &ret_bytesleft,
48                        (unsigned char**) &prop_return);
49     if (ret_type == None || ret_items < 1)
50         return None;
51     return win; // found it!
52 }
53
54 int main(int argc, char **argv)
55 {
56     Display *d = XOpenDisplay(NULL);
57     int s = DefaultScreen(d);
58     Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", True);
59     Atom ret_type;
60     unsigned int winw = 0, winh = 0;
61     int ret_format;
62     unsigned long ret_items, ret_bytesleft;
63     const int MAX_IMAGES = 10;
64     unsigned long *prop_return[MAX_IMAGES];
65     XImage *i[MAX_IMAGES];
66     long offset = 0;
67     unsigned int image = 0;
68     unsigned int j; // loop counter
69     Window id, win;
70     Pixmap p;
71     Cursor cur;
72     XEvent ev;
73
74     printf("Click on a window with an icon...\n");
75
76     //int id = strtol(argv[1], NULL, 16);
77     XUngrabPointer(d, CurrentTime);
78     cur = XCreateFontCursor(d, XC_crosshair);
79     XGrabPointer(d, RootWindow(d, s), False, ButtonPressMask, GrabModeAsync,
80                  GrabModeAsync, None, cur, CurrentTime);
81     while (1) {
82         XNextEvent(d, &ev);
83         if (ev.type == ButtonPress) {
84             XUngrabPointer(d, CurrentTime);
85             id = findClient(d, ev.xbutton.subwindow);
86             break;
87         }
88     }
89
90     printf("Using window 0x%lx\n", id);
91
92     do {
93         unsigned int w, h;
94
95         XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
96                            False, XA_CARDINAL, &ret_type, &ret_format,
97                            &ret_items, &ret_bytesleft,
98                            (unsigned char**) &prop_return[image]);
99         if (ret_type == None || ret_items < 1) {
100             printf("No icon found\n");
101             return 1;
102         }
103         w = prop_return[image][0];
104         XFree(prop_return[image]);
105
106         XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
107                            False, XA_CARDINAL, &ret_type, &ret_format,
108                            &ret_items, &ret_bytesleft,
109                            (unsigned char**) &prop_return[image]);
110         if (ret_type == None || ret_items < 1) {
111             printf("Failed to get height\n");
112             return 1;
113         }
114         h = prop_return[image][0];
115         XFree(prop_return[image]);
116
117         XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
118                            False, XA_CARDINAL, &ret_type, &ret_format,
119                            &ret_items, &ret_bytesleft,
120                            (unsigned char**) &prop_return[image]);
121         if (ret_type == None || ret_items < w*h) {
122             printf("Failed to get image data\n");
123             return 1;
124         }
125         offset += w*h;
126
127         printf("Found icon with size %dx%d\n", w, h);
128
129         i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
130                                 ZPixmap, 0, NULL, w, h, 32, 0);
131         assert(i[image]);
132         i[image]->byte_order = LSBFirst;
133         i[image]->data = (char*)prop_return[image];
134         for (j = 0; j < w*h; j++) {
135             unsigned char alpha = (unsigned char)i[image]->data[j*4+3];
136             unsigned char r = (unsigned char) i[image]->data[j*4+0];
137             unsigned char g = (unsigned char) i[image]->data[j*4+1];
138             unsigned char b = (unsigned char) i[image]->data[j*4+2];
139
140             // background color
141             unsigned char bgr = 0;
142             unsigned char bgg = 0;
143             unsigned char bgb = 0;
144
145             r = bgr + (r - bgr) * alpha / 256;
146             g = bgg + (g - bgg) * alpha / 256;
147             b = bgb + (b - bgb) * alpha / 256;
148
149             i[image]->data[j*4+0] = (char) r;
150             i[image]->data[j*4+1] = (char) g;
151             i[image]->data[j*4+2] = (char) b;
152         }
153
154         winw += w;
155         if (h > winh) winh = h;
156
157         ++image;
158     } while (ret_bytesleft > 0 && image < MAX_IMAGES);
159
160 #define hashsize(n) ((guint32)1<<(n))
161 #define hashmask(n) (hashsize(n)-1)
162 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
163
164 #define mix(a,b,c) \
165 { \
166   a -= c;  a ^= rot(c, 4);  c += b; \
167   b -= a;  b ^= rot(a, 6);  a += c; \
168   c -= b;  c ^= rot(b, 8);  b += a; \
169   a -= c;  a ^= rot(c,16);  c += b; \
170   b -= a;  b ^= rot(a,19);  a += c; \
171   c -= b;  c ^= rot(b, 4);  b += a; \
172 }
173
174 #define final(a,b,c) \
175 { \
176   c ^= b; c -= rot(b,14); \
177   a ^= c; a -= rot(c,11); \
178   b ^= a; b -= rot(a,25); \
179   c ^= b; c -= rot(b,16); \
180   a ^= c; a -= rot(c,4);  \
181   b ^= a; b -= rot(a,14); \
182   c ^= b; c -= rot(b,24); \
183 }
184
185     /* hash the images */
186     for (j = 0; j < image; ++j) {
187         unsigned int w, h, length;
188         guint32 a,b,c;
189         guint32 initval = 0xf00d;
190         const guint32 *k = (guint32*)i[j]->data;
191
192         w = i[j]->width;
193         h = i[j]->height;
194         length = w * h;
195
196         /* Set up the internal state */
197         a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
198
199         /*---------------------------------------- handle most of the key */
200         while (length > 3)
201         {
202             a += k[0];
203             b += k[1];
204             c += k[2];
205             mix(a,b,c);
206             length -= 3;
207             k += 3;
208         }
209
210         /*--------------------------------- handle the last 3 uint32_t's */
211         switch(length)           /* all the case statements fall through */
212         {
213         case 3 : c+=k[2];
214         case 2 : b+=k[1];
215         case 1 : a+=k[0];
216             final(a,b,c);
217         case 0:     /* case 0: nothing left to add */
218             break;
219         }
220         /*------------------------------------ report the result */
221         printf("image[%d] %ux%u %lu\n", j, w, h, c);
222     }
223
224     win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
225                               0, 0, 0);
226     assert(win);
227     XMapWindow(d, win);
228
229     p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
230     XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
231
232     for (j = 0; j < image; ++j) {
233         static unsigned int x = 0;
234
235         XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
236                   i[j]->width, i[j]->height);
237         x += i[j]->width;
238         XDestroyImage(i[j]);
239     }
240
241     XSetWindowBackgroundPixmap(d, win, p);
242     XClearWindow(d, win);
243
244     XFlush(d);
245
246     getchar();
247
248     XFreePixmap(d, p);
249     XCloseDisplay(d);
250 }