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.
20 #include <X11/Xutil.h>
21 #include <X11/Xatom.h>
22 #include <X11/cursorfont.h>
27 #include <sys/types.h>
31 Window findClient(Display *d, Window win)
35 Atom state = XInternAtom(d, "WM_STATE", True);
38 unsigned long ret_items, ret_bytesleft;
39 unsigned long *prop_return;
41 XQueryTree(d, win, &r, &r, &children, &n);
42 for (i = 0; i < n; ++i) {
43 Window w = findClient(d, children[i]);
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)
54 return win; // found it!
57 int main(int argc, char **argv)
59 Display *d = XOpenDisplay(NULL);
60 int s = DefaultScreen(d);
61 Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", True);
63 unsigned int winw = 0, winh = 0;
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];
70 unsigned int image = 0;
71 unsigned int j; // loop counter
76 unsigned int bs = sizeof(long);
78 printf("Click on a window with an icon...\n");
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);
87 if (ev.type == ButtonPress) {
88 XUngrabPointer(d, CurrentTime);
89 id = findClient(d, ev.xbutton.subwindow);
94 printf("Using window 0x%lx\n", id);
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");
107 w = prop_return[image][0];
108 XFree(prop_return[image]);
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");
118 h = prop_return[image][0];
119 XFree(prop_return[image]);
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");
131 printf("Found icon with size %dx%d\n", w, h);
133 i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
134 ZPixmap, 0, NULL, w, h, 32, 0);
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];
145 unsigned char bgr = 0;
146 unsigned char bgg = 0;
147 unsigned char bgb = 0;
149 r = bgr + (r - bgr) * alpha / 256;
150 g = bgg + (g - bgg) * alpha / 256;
151 b = bgb + (b - bgb) * alpha / 256;
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;
159 if (h > winh) winh = h;
162 } while (ret_bytesleft > 0 && image < MAX_IMAGES);
164 #define hashsize(n) ((guint32)1<<(n))
165 #define hashmask(n) (hashsize(n)-1)
166 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
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; \
178 #define final(a,b,c) \
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); \
189 /* hash the images */
190 for (j = 0; j < image; ++j) {
191 unsigned int w, h, length;
193 guint32 initval = 0xf00d;
194 const guint32 *k = (guint32*)i[j]->data;
200 /* Set up the internal state */
201 a = b = c = 0xdeadbeef + (((guint32)length)<<2) + initval;
203 /*---------------------------------------- handle most of the key */
214 /*--------------------------------- handle the last 3 uint32_t's */
215 switch(length) /* all the case statements fall through */
221 case 0: /* case 0: nothing left to add */
224 /*------------------------------------ report the result */
225 printf("image[%d] %ux%u %lu\n", j, w, h, c);
228 win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
233 p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
234 XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
236 for (j = 0; j < image; ++j) {
237 static unsigned int x = 0;
239 XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
240 i[j]->width, i[j]->height);
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);
249 XSetWindowBackgroundPixmap(d, win, p);
250 XClearWindow(d, win);