Various uninteresting changes to the test programs.
[mikachu/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 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 Window findClient(Display *d, Window win)
32 {
33     Window r, *children;
34     unsigned int n, i;
35     Atom state = XInternAtom(d, "WM_STATE", True);
36     Atom ret_type;
37     int ret_format;
38     unsigned long ret_items, ret_bytesleft;
39     unsigned long *prop_return;
40
41     XQueryTree(d, win, &r, &r, &children, &n);
42     for (i = 0; i < n; ++i) {
43         Window w = findClient(d, children[i]);
44         if (w) return w;
45     }
46
47     // try me
48     XGetWindowProperty(d, win, state, 0, 1,
49                        False, state, &ret_type, &ret_format,
50                        &ret_items, &ret_bytesleft,
51                        (unsigned char**) &prop_return);
52     if (ret_type == None || ret_items < 1)
53         return None;
54     return win; // found it!
55 }
56
57 int main(int argc, char **argv)
58 {
59     Display *d = XOpenDisplay(NULL);
60     int s = DefaultScreen(d);
61     Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", True);
62     Atom ret_type;
63     unsigned int winw = 0, winh = 0;
64     int ret_format;
65     unsigned long ret_items, ret_bytesleft;
66     const int MAX_IMAGES = 10;
67     unsigned long *prop_return[MAX_IMAGES];
68     XImage *i[MAX_IMAGES];
69     long offset = 0;
70     unsigned int image = 0;
71     unsigned int j; // loop counter
72     Window id, win;
73     Pixmap p;
74     Cursor cur;
75     XEvent ev;
76     unsigned int bs = sizeof(long);
77
78     printf("Click on a window with an icon...\n");
79
80     //int id = strtol(argv[1], NULL, 16);
81     XUngrabPointer(d, CurrentTime);
82     cur = XCreateFontCursor(d, XC_crosshair);
83     XGrabPointer(d, RootWindow(d, s), False, ButtonPressMask, GrabModeAsync,
84                  GrabModeAsync, None, cur, CurrentTime);
85     while (1) {
86         XNextEvent(d, &ev);
87         if (ev.type == ButtonPress) {
88             XUngrabPointer(d, CurrentTime);
89             id = findClient(d, ev.xbutton.subwindow);
90             break;
91         }
92     }
93
94     printf("Using window 0x%lx\n", id);
95
96     do {
97         unsigned int w, h;
98
99         XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
100                            False, XA_CARDINAL, &ret_type, &ret_format,
101                            &ret_items, &ret_bytesleft,
102                            (unsigned char**) &prop_return[image]);
103         if (ret_type == None || ret_items < 1) {
104             printf("No icon found\n");
105             return 1;
106         }
107         w = prop_return[image][0];
108         XFree(prop_return[image]);
109
110         XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
111                            False, XA_CARDINAL, &ret_type, &ret_format,
112                            &ret_items, &ret_bytesleft,
113                            (unsigned char**) &prop_return[image]);
114         if (ret_type == None || ret_items < 1) {
115             printf("Failed to get height\n");
116             return 1;
117         }
118         h = prop_return[image][0];
119         XFree(prop_return[image]);
120
121         XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
122                            False, XA_CARDINAL, &ret_type, &ret_format,
123                            &ret_items, &ret_bytesleft,
124                            (unsigned char**) &prop_return[image]);
125         if (ret_type == None || ret_items < w*h) {
126             printf("Failed to get image data\n");
127             return 1;
128         }
129         offset += w*h;
130
131         printf("Found icon with size %dx%d\n", w, h);
132
133         i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
134                                 ZPixmap, 0, NULL, w, h, 32, 0);
135         assert(i[image]);
136         i[image]->byte_order = LSBFirst;
137         i[image]->data = (char*)prop_return[image];
138         for (j = 0; j < w*h; j++) {
139             unsigned char alpha = (unsigned char)i[image]->data[j*bs+3];
140             unsigned char r = (unsigned char) i[image]->data[j*bs+0];
141             unsigned char g = (unsigned char) i[image]->data[j*bs+1];
142             unsigned char b = (unsigned char) i[image]->data[j*bs+2];
143
144             // background color
145             unsigned char bgr = 0;
146             unsigned char bgg = 0;
147             unsigned char bgb = 0;
148
149             r = bgr + (r - bgr) * alpha / 256;
150             g = bgg + (g - bgg) * alpha / 256;
151             b = bgb + (b - bgb) * alpha / 256;
152
153             i[image]->data[j*4+0] = (char) r;
154             i[image]->data[j*4+1] = (char) g;
155             i[image]->data[j*4+2] = (char) b;
156         }
157
158         winw += w;
159         if (h > winh) winh = h;
160
161         ++image;
162     } while (ret_bytesleft > 0 && image < MAX_IMAGES);
163
164 #define hashsize(n) ((guint32)1<<(n))
165 #define hashmask(n) (hashsize(n)-1)
166 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
167
168 #define mix(a,b,c) \
169 { \
170   a -= c;  a ^= rot(c, 4);  c += b; \
171   b -= a;  b ^= rot(a, 6);  a += c; \
172   c -= b;  c ^= rot(b, 8);  b += a; \
173   a -= c;  a ^= rot(c,16);  c += b; \
174   b -= a;  b ^= rot(a,19);  a += c; \
175   c -= b;  c ^= rot(b, 4);  b += a; \
176 }
177
178 #define final(a,b,c) \
179 { \
180   c ^= b; c -= rot(b,14); \
181   a ^= c; a -= rot(c,11); \
182   b ^= a; b -= rot(a,25); \
183   c ^= b; c -= rot(b,16); \
184   a ^= c; a -= rot(c,4);  \
185   b ^= a; b -= rot(a,14); \
186   c ^= b; c -= rot(b,24); \
187 }
188
189     /* hash the images */
190     for (j = 0; j < image; ++j) {
191         unsigned int w, h, length;
192         guint32 a,b,c;
193         guint32 initval = 0xf00d;
194         const guint32 *k = (guint32*)i[j]->data;
195
196         w = i[j]->width;
197         h = i[j]->height;
198         length = w * h;
199
200         /* Set up the internal state */
201         a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
202
203         /*---------------------------------------- handle most of the key */
204         while (length > 3)
205         {
206             a += k[0];
207             b += k[1];
208             c += k[2];
209             mix(a,b,c);
210             length -= 3;
211             k += 3;
212         }
213
214         /*--------------------------------- handle the last 3 uint32_t's */
215         switch(length)           /* all the case statements fall through */
216         {
217         case 3 : c+=k[2];
218         case 2 : b+=k[1];
219         case 1 : a+=k[0];
220             final(a,b,c);
221         case 0:     /* case 0: nothing left to add */
222             break;
223         }
224         /*------------------------------------ report the result */
225         printf("image[%d] %ux%u %lu\n", j, w, h, c);
226     }
227
228     win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
229                               0, 0, 0);
230     assert(win);
231     XMapWindow(d, win);
232
233     p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
234     XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
235
236     for (j = 0; j < image; ++j) {
237         static unsigned int x = 0;
238
239         XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
240                   i[j]->width, i[j]->height);
241         x += i[j]->width;
242         char *filename;
243         asprintf(&filename, "icon%i.raw", j);
244         int fd = open(filename, O_CREAT | O_RDWR);
245         write(fd, i[j]->data, i[j]->width * i[j]->height * i[j]->depth);
246         XDestroyImage(i[j]);
247     }
248
249     XSetWindowBackgroundPixmap(d, win, p);
250     XClearWindow(d, win);
251
252     XFlush(d);
253
254     getchar();
255
256     XFreePixmap(d, p);
257     XCloseDisplay(d);
258 }