2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <X11/Xatom.h>
35 #include <X11/Xutil.h>
37 #include <X11/Xft/Xft.h>
48 /* some basic X11 stuff */
53 static int set_transparent;
54 static int background_colour;
56 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
60 struct conky_window window;
62 /* local prototypes */
63 static void update_workarea();
64 static Window find_desktop_window(Window *p_root, Window *p_desktop);
65 static Window find_subwindow(Window win, int w, int h);
70 if ((display = XOpenDisplay(0)) == NULL)
71 CRIT_ERR("can't open display: %s", XDisplayName(0));
73 screen = DefaultScreen(display);
74 display_width = DisplayWidth(display, screen);
75 display_height = DisplayHeight(display, screen);
80 static void update_workarea()
82 Window root = RootWindow(display, screen);
83 unsigned long nitems, bytes;
84 unsigned char *buf = NULL;
88 /* default work area is display */
91 workarea[2] = display_width;
92 workarea[3] = display_height;
94 /* get current desktop */
95 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
96 0, 1, False, XA_CARDINAL, &type, &format,
97 &nitems, &bytes, &buf) == Success
98 && type == XA_CARDINAL && nitems > 0) {
101 /* long desktop = * (long *) buf; */
114 /* Find root window and desktop window. Return desktop window on success,
115 * and set root and desktop byref return values. Return 0 on failure. */
116 static Window find_desktop_window(Window *p_root, Window *p_desktop)
120 unsigned long nitems, bytes;
122 Window root = RootWindow(display, screen);
124 Window troot, parent, *children;
125 unsigned char *buf = NULL;
127 if (!p_root || !p_desktop)
130 /* some window managers set __SWM_VROOT to some child of root window */
132 XQueryTree(display, root, &troot, &parent, &children, &n);
133 for (i = 0; i < (int) n; i++) {
134 if (XGetWindowProperty
135 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
136 XA_WINDOW, &type, &format, &nitems, &bytes,
137 &buf) == Success && type == XA_WINDOW) {
138 win = *(Window *) buf;
142 "Conky: desktop window (%lx) found from __SWM_VROOT property\n", win);
156 /* get subwindows from root */
157 win = find_subwindow(root, -1, -1);
161 win = find_subwindow(win, workarea[2], workarea[3]);
170 "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
172 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
182 /* sets background to ParentRelative for the Window and all parents */
183 inline void set_transparent_background(Window win)
185 static int colour_set = -1;
186 if (set_transparent) {
189 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
193 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
195 XQueryTree(display, parent, &r, &parent, &children, &n);
198 } else if (colour_set != background_colour) {
199 XSetWindowBackground(display, win, background_colour);
200 colour_set = background_colour;
202 //XClearWindow(display, win); not sure why this was here
205 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
206 char **argv, int argc)
208 /* There seems to be some problems with setting transparent background (on
209 * fluxbox this time). It doesn't happen always and I don't know why it
210 * happens but I bet the bug is somewhere here. */
211 set_transparent = set_trans;
212 background_colour = back_colour;
217 if ( !find_desktop_window( &window.root, &window.desktop ) )
220 if (window.type == TYPE_OVERRIDE) {
223 An override_redirect True window. No WM hints or button processing needed.
225 XSetWindowAttributes attrs = {
226 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
227 StructureNotifyMask|ExposureMask,
232 /* Parent is desktop window (which might be a child of root) */
233 window.window = XCreateWindow(display,
235 window.x, window.y, w, h, 0,
239 CWBackPixel|CWOverrideRedirect,
242 XLowerWindow(display, window.window);
244 fprintf(stderr, "Conky: window type - override\n"); fflush(stderr);
248 else { /* window.type != TYPE_OVERRIDE */
251 A window managed by the window manager. Process hints and buttons.
253 XSetWindowAttributes attrs = {
254 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
255 StructureNotifyMask|ExposureMask|ButtonPressMask|ButtonReleaseMask,
260 XClassHint classHint;
264 /* Parent is root window so WM can take control */
265 window.window = XCreateWindow(display,
267 window.x, window.y, w, h, 0,
271 CWBackPixel|CWOverrideRedirect,
274 classHint.res_name = window.class_name;
275 classHint.res_class = classHint.res_name;
277 wmHint.flags = InputHint | StateHint;
278 /* allow decorated windows to be given input focus by WM */
279 wmHint.input = TEST_HINT(window.hints,HINT_UNDECORATED) ? False : True;
280 wmHint.initial_state = NormalState;
282 XmbSetWMProperties (display, window.window, window.title, NULL,
284 NULL, &wmHint, &classHint);
286 /* Sets an empty WM_PROTOCOLS property */
287 XSetWMProtocols(display,window.window,NULL,0);
290 /* Set window type */
291 if ( (xa = ATOM(_NET_WM_WINDOW_TYPE)) != None )
294 switch(window.type) {
297 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
298 fprintf(stderr, "Conky: window type - desktop\n"); fflush(stderr);
304 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
305 fprintf(stderr, "Conky: window type - normal\n"); fflush(stderr);
309 XChangeProperty(display, window.window, xa,
312 (unsigned char *) &prop, 1);
315 /* Set desired hints */
317 /* Window decorations */
318 if (TEST_HINT(window.hints,HINT_UNDECORATED)) {
319 /*fprintf(stderr, "Conky: hint - undecorated\n"); fflush(stderr);*/
321 xa = ATOM(_MOTIF_WM_HINTS);
323 long prop[5] = { 2, 0, 0, 0, 0 };
324 XChangeProperty(display, window.window, xa,
325 xa, 32, PropModeReplace,
326 (unsigned char *) prop, 5);
330 /* Below other windows */
331 if (TEST_HINT(window.hints,HINT_BELOW)) {
332 /*fprintf(stderr, "Conky: hint - below\n"); fflush(stderr); */
334 xa = ATOM(_WIN_LAYER);
337 XChangeProperty(display, window.window, xa,
340 (unsigned char *) &prop, 1);
343 xa = ATOM(_NET_WM_STATE);
345 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
346 XChangeProperty(display, window.window, xa,
349 (unsigned char *) &xa_prop,
354 /* Above other windows */
355 if (TEST_HINT(window.hints,HINT_ABOVE)) {
356 /*fprintf(stderr, "Conky: hint - above\n"); fflush(stderr);*/
358 xa = ATOM(_WIN_LAYER);
361 XChangeProperty(display, window.window, xa,
364 (unsigned char *) &prop, 1);
367 xa = ATOM(_NET_WM_STATE);
369 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
370 XChangeProperty(display, window.window, xa,
373 (unsigned char *) &xa_prop,
379 if (TEST_HINT(window.hints,HINT_STICKY)) {
380 /*fprintf(stderr, "Conky: hint - sticky\n"); fflush(stderr); */
382 xa = ATOM(_NET_WM_DESKTOP);
384 CARD32 xa_prop = 0xFFFFFFFF;
385 XChangeProperty(display, window.window, xa,
388 (unsigned char *) &xa_prop,
392 xa = ATOM(_NET_WM_STATE);
394 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
395 XChangeProperty(display, window.window, xa,
398 (unsigned char *) &xa_prop,
404 if (TEST_HINT(window.hints,HINT_SKIP_TASKBAR)) {
405 /*fprintf(stderr, "Conky: hint - skip_taskbar\n"); fflush(stderr);*/
407 xa = ATOM(_NET_WM_STATE);
409 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
410 XChangeProperty(display, window.window, xa,
413 (unsigned char *) &xa_prop,
419 if (TEST_HINT(window.hints,HINT_SKIP_PAGER)) {
420 /*fprintf(stderr, "Conky: hint - skip_pager\n"); fflush(stderr);*/
422 xa = ATOM(_NET_WM_STATE);
424 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
425 XChangeProperty(display, window.window, xa,
428 (unsigned char *) &xa_prop,
433 } /* else { window.type != TYPE_OVERRIDE */
435 fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
438 XMapWindow(display, window.window);
440 } else /* if (own_window) { */
442 /* root / desktop window */
444 XWindowAttributes attrs;
447 window.window = find_desktop_window( &window.root, &window.desktop );
449 if (XGetWindowAttributes(display, window.window, &attrs)) {
450 window.width = attrs.width;
451 window.height = attrs.height;
454 fprintf(stderr, "Conky: drawing to desktop window\n");
457 /* Drawable is same as window. This may be changed by double buffering. */
458 window.drawable = window.window;
463 if (!XdbeQueryExtension(display, &major, &minor)) {
467 XdbeAllocateBackBufferName(display,
470 if (window.back_buffer != None) {
471 window.drawable = window.back_buffer;
473 "Conky: drawing to double buffer\n");
478 ERR("failed to set up double buffer");
481 fprintf(stderr, "Conky: drawing to single buffer\n");
486 /*set_transparent_background(window.window); must be done after double buffer stuff? */
489 set_transparent_background(window.window);
490 XClearWindow(display, window.window);
494 XSelectInput(display, window.window, ExposureMask
497 ? (StructureNotifyMask | PropertyChangeMask |
498 ButtonPressMask | ButtonReleaseMask) : 0)
503 static Window find_subwindow(Window win, int w, int h)
506 Window troot, parent, *children;
509 /* search subwindows with same size as display or work area */
511 for (i = 0; i < 10; i++) {
512 XQueryTree(display, win, &troot, &parent, &children, &n);
514 for (j = 0; j < n; j++) {
515 XWindowAttributes attrs;
517 if (XGetWindowAttributes
518 (display, children[j], &attrs)) {
519 /* Window must be mapped and same size as display or work space */
520 if (attrs.map_state != 0 &&
521 ((attrs.width == display_width
522 && attrs.height == display_height)
524 && attrs.height == h))) {
539 long get_x11_color(const char *name)
544 (display, DefaultColormap(display, screen), name, &color)) {
545 /* lets check if it's a hex colour with the # missing in front
546 * if yes, then do something about it
550 strncpy(&newname[1], name, 62);
551 /* now lets try again */
552 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
553 ERR("can't parse X color '%s'", name);
558 (display, DefaultColormap(display, screen), &color))
559 ERR("can't allocate X color '%s'", name);
561 return (long) color.pixel;
567 values.graphics_exposures = 0;
568 values.function = GXcopy;
569 window.gc = XCreateGC(display, window.drawable,
570 GCFunction | GCGraphicsExposures, &values);