Revert these two commits temporarily to make the buttons merge easier
[openbox.git] / tests / icons.c
blobe2477c25e1522ee4d95211a158db33f074677519
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 icons.c for the Openbox window manager
4 Copyright (c) 2003-2007 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
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>
28 Window findClient(Display *d, Window win)
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;
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;
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!
54 int main(int argc, char **argv)
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 unsigned int bs = sizeof(long);
75 printf("Click on a window with an icon...\n");
77 //int id = strtol(argv[1], NULL, 16);
78 XUngrabPointer(d, CurrentTime);
79 cur = XCreateFontCursor(d, XC_crosshair);
80 XGrabPointer(d, RootWindow(d, s), False, ButtonPressMask, GrabModeAsync,
81 GrabModeAsync, None, cur, CurrentTime);
82 while (1) {
83 XNextEvent(d, &ev);
84 if (ev.type == ButtonPress) {
85 XUngrabPointer(d, CurrentTime);
86 id = findClient(d, ev.xbutton.subwindow);
87 break;
91 printf("Using window 0x%lx\n", id);
93 do {
94 unsigned int w, h;
96 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
97 False, XA_CARDINAL, &ret_type, &ret_format,
98 &ret_items, &ret_bytesleft,
99 (unsigned char**) &prop_return[image]);
100 if (ret_type == None || ret_items < 1) {
101 printf("No icon found\n");
102 return 1;
104 w = prop_return[image][0];
105 XFree(prop_return[image]);
107 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
108 False, XA_CARDINAL, &ret_type, &ret_format,
109 &ret_items, &ret_bytesleft,
110 (unsigned char**) &prop_return[image]);
111 if (ret_type == None || ret_items < 1) {
112 printf("Failed to get height\n");
113 return 1;
115 h = prop_return[image][0];
116 XFree(prop_return[image]);
118 XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
119 False, XA_CARDINAL, &ret_type, &ret_format,
120 &ret_items, &ret_bytesleft,
121 (unsigned char**) &prop_return[image]);
122 if (ret_type == None || ret_items < w*h) {
123 printf("Failed to get image data\n");
124 return 1;
126 offset += w*h;
128 printf("Found icon with size %dx%d\n", w, h);
130 i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
131 ZPixmap, 0, NULL, w, h, 32, 0);
132 assert(i[image]);
133 i[image]->byte_order = LSBFirst;
134 i[image]->data = (char*)prop_return[image];
135 for (j = 0; j < w*h; j++) {
136 unsigned char alpha = (unsigned char)i[image]->data[j*bs+3];
137 unsigned char r = (unsigned char) i[image]->data[j*bs+0];
138 unsigned char g = (unsigned char) i[image]->data[j*bs+1];
139 unsigned char b = (unsigned char) i[image]->data[j*bs+2];
141 // background color
142 unsigned char bgr = 0;
143 unsigned char bgg = 0;
144 unsigned char bgb = 0;
146 r = bgr + (r - bgr) * alpha / 256;
147 g = bgg + (g - bgg) * alpha / 256;
148 b = bgb + (b - bgb) * alpha / 256;
150 i[image]->data[j*4+0] = (char) r;
151 i[image]->data[j*4+1] = (char) g;
152 i[image]->data[j*4+2] = (char) b;
155 winw += w;
156 if (h > winh) winh = h;
158 ++image;
159 } while (ret_bytesleft > 0 && image < MAX_IMAGES);
161 #define hashsize(n) ((guint32)1<<(n))
162 #define hashmask(n) (hashsize(n)-1)
163 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
165 #define mix(a,b,c) \
167 a -= c; a ^= rot(c, 4); c += b; \
168 b -= a; b ^= rot(a, 6); a += c; \
169 c -= b; c ^= rot(b, 8); b += a; \
170 a -= c; a ^= rot(c,16); c += b; \
171 b -= a; b ^= rot(a,19); a += c; \
172 c -= b; c ^= rot(b, 4); b += a; \
175 #define final(a,b,c) \
177 c ^= b; c -= rot(b,14); \
178 a ^= c; a -= rot(c,11); \
179 b ^= a; b -= rot(a,25); \
180 c ^= b; c -= rot(b,16); \
181 a ^= c; a -= rot(c,4); \
182 b ^= a; b -= rot(a,14); \
183 c ^= b; c -= rot(b,24); \
186 /* hash the images */
187 for (j = 0; j < image; ++j) {
188 unsigned int w, h, length;
189 guint32 a,b,c;
190 guint32 initval = 0xf00d;
191 const guint32 *k = (guint32*)i[j]->data;
193 w = i[j]->width;
194 h = i[j]->height;
195 length = w * h;
197 /* Set up the internal state */
198 a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
200 /*---------------------------------------- handle most of the key */
201 while (length > 3)
203 a += k[0];
204 b += k[1];
205 c += k[2];
206 mix(a,b,c);
207 length -= 3;
208 k += 3;
211 /*--------------------------------- handle the last 3 uint32_t's */
212 switch(length) /* all the case statements fall through */
214 case 3 : c+=k[2];
215 case 2 : b+=k[1];
216 case 1 : a+=k[0];
217 final(a,b,c);
218 case 0: /* case 0: nothing left to add */
219 break;
221 /*------------------------------------ report the result */
222 printf("image[%d] %ux%u %lu\n", j, w, h, c);
225 win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
226 0, 0, 0);
227 assert(win);
228 XMapWindow(d, win);
230 p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
231 XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
233 for (j = 0; j < image; ++j) {
234 static unsigned int x = 0;
236 XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
237 i[j]->width, i[j]->height);
238 x += i[j]->width;
239 XDestroyImage(i[j]);
242 XSetWindowBackgroundPixmap(d, win, p);
243 XClearWindow(d, win);
245 XFlush(d);
247 getchar();
249 XFreePixmap(d, p);
250 XCloseDisplay(d);