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 Window findClient(Display *d, Window win)
31 Atom state = XInternAtom(d, "WM_STATE", True);
34 unsigned long ret_items, ret_bytesleft;
35 unsigned long *prop_return;
37 XQueryTree(d, win, &r, &r, &children, &n);
38 for (i = 0; i < n; ++i) {
39 Window w = findClient(d, children[i]);
44 XGetWindowProperty(d, win, state, 0, 1,
45 False, state, &ret_type, &ret_format,
46 &ret_items, &ret_bytesleft,
47 (unsigned char**) &prop_return);
48 if (ret_type == None || ret_items < 1)
50 return win; // found it!
53 int main(int argc, char **argv)
55 Display *d = XOpenDisplay(NULL);
56 int s = DefaultScreen(d);
57 Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", True);
59 unsigned int winw = 0, winh = 0;
61 unsigned long ret_items, ret_bytesleft;
62 const int MAX_IMAGES = 10;
63 unsigned long *prop_return[MAX_IMAGES];
64 XImage *i[MAX_IMAGES];
66 unsigned int image = 0;
67 unsigned int j; // loop counter
73 printf("Click on a window with an icon...\n");
75 //int id = strtol(argv[1], NULL, 16);
76 XUngrabPointer(d, CurrentTime);
77 cur = XCreateFontCursor(d, XC_crosshair);
78 XGrabPointer(d, RootWindow(d, s), False, ButtonPressMask, GrabModeAsync,
79 GrabModeAsync, None, cur, CurrentTime);
82 if (ev.type == ButtonPress) {
83 XUngrabPointer(d, CurrentTime);
84 id = findClient(d, ev.xbutton.subwindow);
89 printf("Using window 0x%lx\n", id);
94 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
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 < 1) {
99 printf("No icon found\n");
102 w = prop_return[image][0];
103 XFree(prop_return[image]);
105 XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
106 False, XA_CARDINAL, &ret_type, &ret_format,
107 &ret_items, &ret_bytesleft,
108 (unsigned char**) &prop_return[image]);
109 if (ret_type == None || ret_items < 1) {
110 printf("Failed to get height\n");
113 h = prop_return[image][0];
114 XFree(prop_return[image]);
116 XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
117 False, XA_CARDINAL, &ret_type, &ret_format,
118 &ret_items, &ret_bytesleft,
119 (unsigned char**) &prop_return[image]);
120 if (ret_type == None || ret_items < w*h) {
121 printf("Failed to get image data\n");
126 printf("Found icon with size %dx%d\n", w, h);
128 i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
129 ZPixmap, 0, NULL, w, h, 32, 0);
131 i[image]->byte_order = LSBFirst;
132 i[image]->data = (char*)prop_return[image];
133 for (j = 0; j < w*h; j++) {
134 unsigned char alpha = (unsigned char)i[image]->data[j*4+3];
135 unsigned char r = (unsigned char) i[image]->data[j*4+0];
136 unsigned char g = (unsigned char) i[image]->data[j*4+1];
137 unsigned char b = (unsigned char) i[image]->data[j*4+2];
140 unsigned char bgr = 0;
141 unsigned char bgg = 0;
142 unsigned char bgb = 0;
144 r = bgr + (r - bgr) * alpha / 256;
145 g = bgg + (g - bgg) * alpha / 256;
146 b = bgb + (b - bgb) * alpha / 256;
148 i[image]->data[j*4+0] = (char) r;
149 i[image]->data[j*4+1] = (char) g;
150 i[image]->data[j*4+2] = (char) b;
154 if (h > winh) winh = h;
157 } while (ret_bytesleft > 0 && image < MAX_IMAGES);
159 win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
164 p = XCreatePixmap(d, win, winw, winh, DefaultDepth(d, s));
165 XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
167 for (j = 0; j < image; ++j) {
168 static unsigned int x = 0;
170 XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
171 i[j]->width, i[j]->height);
176 XSetWindowBackgroundPixmap(d, win, p);
177 XClearWindow(d, win);