2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
14 #include <X11/Xatom.h>
15 #include <X11/Xutil.h>
17 #include <X11/Xft/Xft.h>
30 #define WINDOW_NAME_FMT "%s - conky"
32 /* some basic X11 stuff */
37 static int set_transparent;
38 static int background_colour;
40 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
44 struct conky_window window;
46 /* local prototypes */
47 static void update_workarea();
48 static Window find_desktop_window();
49 static Window find_subwindow(Window win, int w, int h);
54 if ((display = XOpenDisplay(0)) == NULL)
55 CRIT_ERR("can't open display: %s", XDisplayName(0));
57 screen = DefaultScreen(display);
58 display_width = DisplayWidth(display, screen);
59 display_height = DisplayHeight(display, screen);
64 static void update_workarea()
66 Window root = RootWindow(display, screen);
67 unsigned long nitems, bytes;
68 unsigned char *buf = NULL;
72 /* default work area is display */
75 workarea[2] = display_width;
76 workarea[3] = display_height;
78 /* get current desktop */
79 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
80 0, 1, False, XA_CARDINAL, &type, &format,
81 &nitems, &bytes, &buf) == Success
82 && type == XA_CARDINAL && nitems > 0) {
85 /* long desktop = * (long *) buf; */
98 static Window find_desktop_window()
102 unsigned long nitems, bytes;
104 Window root = RootWindow(display, screen);
106 Window troot, parent, *children;
107 unsigned char *buf = NULL;
109 /* some window managers set __SWM_VROOT to some child of root window */
111 XQueryTree(display, root, &troot, &parent, &children, &n);
112 for (i = 0; i < (int) n; i++) {
113 if (XGetWindowProperty
114 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
115 XA_WINDOW, &type, &format, &nitems, &bytes,
116 &buf) == Success && type == XA_WINDOW) {
117 win = *(Window *) buf;
121 "Conky: desktop window (%lx) found from __SWM_VROOT property\n", win);
133 /* get subwindows from root */
134 win = find_subwindow(root, -1, -1);
138 win = find_subwindow(win, workarea[2], workarea[3]);
147 "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
149 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
155 /* sets background to ParentRelative for the Window and all parents */
156 inline void set_transparent_background(Window win)
158 static int colour_set = -1;
159 if (set_transparent) {
162 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
166 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
168 XQueryTree(display, parent, &r, &parent, &children, &n);
171 } else if (colour_set != background_colour) {
172 XSetWindowBackground(display, win, background_colour);
173 colour_set = background_colour;
175 //XClearWindow(display, win); not sure why this was here
178 void init_window(int own_window, int w, int h, int set_trans, int back_colour, char * nodename,
179 char **argv, int argc)
181 /* There seems to be some problems with setting transparent background (on
182 * fluxbox this time). It doesn't happen always and I don't know why it
183 * happens but I bet the bug is somewhere here. */
184 set_transparent = set_trans;
185 background_colour = back_colour;
187 nodename = (char *)nodename;
192 /* Allow WM control of conky again. Shielding conky from the WM
193 * via override redirect creates more problems than it's worth and
194 * makes it impossible to use tools like devilspie to manage the
195 * conky windows beyond the parametsrs we offer. Also, button
196 * press events are now explicitly forwarded to the root window. */
197 XSetWindowAttributes attrs = {
198 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
199 StructureNotifyMask|ExposureMask|ButtonPressMask,
202 XClassHint classHint;
205 char window_title[256];
207 window.root = find_desktop_window();
209 window.window = XCreateWindow(display, window.root,
210 window.x, window.y, w, h, 0,
214 CWBackPixel|CWOverrideRedirect,
217 fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
220 classHint.res_name = window.wm_class_name;
221 classHint.res_class = classHint.res_name;
223 wmHint.flags = InputHint | StateHint;
224 wmHint.input = False;
225 wmHint.initial_state = NormalState;
227 sprintf(window_title,WINDOW_NAME_FMT,nodename);
229 XmbSetWMProperties (display, window.window, window_title, NULL,
231 NULL, &wmHint, &classHint);
233 /* Sets an empty WM_PROTOCOLS property */
234 XSetWMProtocols(display,window.window,NULL,0);
237 /* Set NORMAL window type for _NET_WM_WINDOW_TYPE. */
238 xa = ATOM(_NET_WM_WINDOW_TYPE);
240 Atom prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
241 XChangeProperty(display, window.window, xa,
244 (unsigned char *) &prop,
248 /* Set desired hints */
250 /* Window decorations */
251 if (TEST_HINT(window.hints,HINT_UNDECORATED)) {
252 fprintf(stderr, "Conky: hint - undecorated\n"); fflush(stderr);
254 xa = ATOM(_MOTIF_WM_HINTS);
256 long prop[5] = { 2, 0, 0, 0, 0 };
257 XChangeProperty(display, window.window, xa,
258 xa, 32, PropModeReplace,
259 (unsigned char *) prop, 5);
263 /* Below other windows */
264 if (TEST_HINT(window.hints,HINT_BELOW)) {
265 fprintf(stderr, "Conky: hint - below\n"); fflush(stderr);
267 xa = ATOM(_WIN_LAYER);
270 XChangeProperty(display, window.window, xa,
273 (unsigned char *) &prop, 1);
276 xa = ATOM("_NET_WM_STATE");
278 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
279 XChangeProperty(display, window.window, xa,
282 (unsigned char *) &xa_prop,
287 /* Above other windows */
288 if (TEST_HINT(window.hints,HINT_ABOVE)) {
289 fprintf(stderr, "Conky: hint - above\n"); fflush(stderr);
291 xa = ATOM(_WIN_LAYER);
294 XChangeProperty(display, window.window, xa,
297 (unsigned char *) &prop, 1);
300 xa = ATOM("_NET_WM_STATE");
302 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
303 XChangeProperty(display, window.window, xa,
306 (unsigned char *) &xa_prop,
312 if (TEST_HINT(window.hints,HINT_STICKY)) {
313 fprintf(stderr, "Conky: hint - sticky\n"); fflush(stderr);
315 xa = ATOM("_NET_WM_STATE");
317 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
318 XChangeProperty(display, window.window, xa,
321 (unsigned char *) &xa_prop,
327 if (TEST_HINT(window.hints,HINT_SKIP_TASKBAR)) {
328 fprintf(stderr, "Conky: hint - skip_taskbar\n"); fflush(stderr);
330 xa = ATOM("_NET_WM_STATE");
332 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
333 XChangeProperty(display, window.window, xa,
336 (unsigned char *) &xa_prop,
342 if (TEST_HINT(window.hints,HINT_SKIP_PAGER)) {
343 fprintf(stderr, "Conky: hint - skip_pager\n"); fflush(stderr);
345 xa = ATOM("_NET_WM_STATE");
347 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
348 XChangeProperty(display, window.window, xa,
351 (unsigned char *) &xa_prop,
358 XMapWindow(display, window.window);
363 /* root / desktop window */
365 XWindowAttributes attrs;
368 window.window = find_desktop_window();
370 if (XGetWindowAttributes(display, window.window, &attrs)) {
371 window.width = attrs.width;
372 window.height = attrs.height;
375 fprintf(stderr, "Conky: drawing to desktop window\n");
378 /* Drawable is same as window. This may be changed by double buffering. */
379 window.drawable = window.window;
384 if (!XdbeQueryExtension(display, &major, &minor)) {
388 XdbeAllocateBackBufferName(display,
391 if (window.back_buffer != None) {
392 window.drawable = window.back_buffer;
394 "Conky: drawing to double buffer\n");
399 ERR("failed to set up double buffer");
402 fprintf(stderr, "Conky: drawing to single buffer\n");
407 /*set_transparent_background(window.window); must be done after double buffer stuff? */
410 set_transparent_background(window.window);
411 XClearWindow(display, window.window);
415 XSelectInput(display, window.window, ExposureMask
418 ? (StructureNotifyMask | PropertyChangeMask | ButtonPressMask) : 0)
423 static Window find_subwindow(Window win, int w, int h)
426 Window troot, parent, *children;
429 /* search subwindows with same size as display or work area */
431 for (i = 0; i < 10; i++) {
432 XQueryTree(display, win, &troot, &parent, &children, &n);
434 for (j = 0; j < n; j++) {
435 XWindowAttributes attrs;
437 if (XGetWindowAttributes
438 (display, children[j], &attrs)) {
439 /* Window must be mapped and same size as display or work space */
440 if (attrs.map_state != 0 &&
441 ((attrs.width == display_width
442 && attrs.height == display_height)
444 && attrs.height == h))) {
459 long get_x11_color(const char *name)
464 (display, DefaultColormap(display, screen), name, &color)) {
465 /* lets check if it's a hex colour with the # missing in front
466 * if yes, then do something about it
470 strncpy(&newname[1], name, 62);
471 /* now lets try again */
472 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
473 ERR("can't parse X color '%s'", name);
478 (display, DefaultColormap(display, screen), &color))
479 ERR("can't allocate X color '%s'", name);
481 return (long) color.pixel;
487 values.graphics_exposures = 0;
488 values.function = GXcopy;
489 window.gc = XCreateGC(display, window.drawable,
490 GCFunction | GCGraphicsExposures, &values);