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>
28 Window
findClient(Display
*d
, Window win
)
32 Atom state
= XInternAtom(d
, "WM_STATE", True
);
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
]);
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)
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
);
60 unsigned int winw
= 0, winh
= 0;
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
];
67 unsigned int image
= 0;
68 unsigned int j
; // loop counter
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
);
84 if (ev
.type
== ButtonPress
) {
85 XUngrabPointer(d
, CurrentTime
);
86 id
= findClient(d
, ev
.xbutton
.subwindow
);
91 printf("Using window 0x%lx\n", id
);
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");
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");
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");
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);
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];
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
;
156 if (h
> winh
) winh
= h
;
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))))
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
;
190 guint32 initval
= 0xf00d;
191 const guint32
*k
= (guint32
*)i
[j
]->data
;
197 /* Set up the internal state */
198 a
= b
= c
= 0xdeadbeef + (((guint32
)length
)<<2) + initval
;
200 /*---------------------------------------- handle most of the key */
211 /*--------------------------------- handle the last 3 uint32_t's */
212 switch(length
) /* all the case statements fall through */
218 case 0: /* case 0: nothing left to add */
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
,
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
);
242 XSetWindowBackgroundPixmap(d
, win
, p
);
243 XClearWindow(d
, win
);