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(Window *p_root, Window *p_desktop);
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 /* Find root window and desktop window. Return desktop window on success,
99 * and set root and desktop byref return values. Return 0 on failure. */
100 static Window find_desktop_window(Window *p_root, Window *p_desktop)
104 unsigned long nitems, bytes;
106 Window root = RootWindow(display, screen);
108 Window troot, parent, *children;
109 unsigned char *buf = NULL;
111 if (!p_root || !p_desktop)
114 /* some window managers set __SWM_VROOT to some child of root window */
116 XQueryTree(display, root, &troot, &parent, &children, &n);
117 for (i = 0; i < (int) n; i++) {
118 if (XGetWindowProperty
119 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
120 XA_WINDOW, &type, &format, &nitems, &bytes,
121 &buf) == Success && type == XA_WINDOW) {
122 win = *(Window *) buf;
126 "Conky: desktop window (%lx) found from __SWM_VROOT property\n", win);
140 /* get subwindows from root */
141 win = find_subwindow(root, -1, -1);
145 win = find_subwindow(win, workarea[2], workarea[3]);
154 "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
156 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
166 /* sets background to ParentRelative for the Window and all parents */
167 inline void set_transparent_background(Window win)
169 static int colour_set = -1;
170 if (set_transparent) {
173 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
177 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
179 XQueryTree(display, parent, &r, &parent, &children, &n);
182 } else if (colour_set != background_colour) {
183 XSetWindowBackground(display, win, background_colour);
184 colour_set = background_colour;
186 //XClearWindow(display, win); not sure why this was here
189 void init_window(int own_window, int w, int h, int set_trans, int back_colour, char * nodename,
190 char **argv, int argc)
192 /* There seems to be some problems with setting transparent background (on
193 * fluxbox this time). It doesn't happen always and I don't know why it
194 * happens but I bet the bug is somewhere here. */
195 set_transparent = set_trans;
196 background_colour = back_colour;
198 nodename = (char *)nodename;
203 /* Allow WM control of conky again. Shielding conky from the WM
204 * via override redirect creates more problems than it's worth and
205 * makes it impossible to use tools like devilspie to manage the
206 * conky windows beyond the parametsrs we offer. Also, button
207 * press events are now explicitly forwarded to the root window. */
208 XSetWindowAttributes attrs = {
209 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
210 StructureNotifyMask|ExposureMask|ButtonPressMask,
213 XClassHint classHint;
216 char window_title[256];
218 /* We want to parent the window to the root so it's under WM control,
219 * but we want to forward button clicks to the desktop window for menus.
220 * On some desktop systems, the desktop window != root window. */
221 if ( !find_desktop_window( &window.root, &window.desktop ) )
224 window.window = XCreateWindow(display, window.root,
225 window.x, window.y, w, h, 0,
229 CWBackPixel|CWOverrideRedirect,
232 fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
235 classHint.res_name = window.wm_class_name;
236 classHint.res_class = classHint.res_name;
238 wmHint.flags = InputHint | StateHint;
239 wmHint.input = False;
240 wmHint.initial_state = NormalState;
242 sprintf(window_title,WINDOW_NAME_FMT,nodename);
244 XmbSetWMProperties (display, window.window, window_title, NULL,
246 NULL, &wmHint, &classHint);
248 /* Sets an empty WM_PROTOCOLS property */
249 XSetWMProtocols(display,window.window,NULL,0);
252 /* Set NORMAL window type for _NET_WM_WINDOW_TYPE. */
253 xa = ATOM(_NET_WM_WINDOW_TYPE);
255 Atom prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
256 XChangeProperty(display, window.window, xa,
259 (unsigned char *) &prop,
263 /* Set desired hints */
265 /* Window decorations */
266 if (TEST_HINT(window.hints,HINT_UNDECORATED)) {
267 fprintf(stderr, "Conky: hint - undecorated\n"); fflush(stderr);
269 xa = ATOM(_MOTIF_WM_HINTS);
271 long prop[5] = { 2, 0, 0, 0, 0 };
272 XChangeProperty(display, window.window, xa,
273 xa, 32, PropModeReplace,
274 (unsigned char *) prop, 5);
278 /* Below other windows */
279 if (TEST_HINT(window.hints,HINT_BELOW)) {
280 fprintf(stderr, "Conky: hint - below\n"); fflush(stderr);
282 xa = ATOM(_WIN_LAYER);
285 XChangeProperty(display, window.window, xa,
288 (unsigned char *) &prop, 1);
291 xa = ATOM(_NET_WM_STATE);
293 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
294 XChangeProperty(display, window.window, xa,
297 (unsigned char *) &xa_prop,
302 /* Above other windows */
303 if (TEST_HINT(window.hints,HINT_ABOVE)) {
304 fprintf(stderr, "Conky: hint - above\n"); fflush(stderr);
306 xa = ATOM(_WIN_LAYER);
309 XChangeProperty(display, window.window, xa,
312 (unsigned char *) &prop, 1);
315 xa = ATOM(_NET_WM_STATE);
317 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
318 XChangeProperty(display, window.window, xa,
321 (unsigned char *) &xa_prop,
327 if (TEST_HINT(window.hints,HINT_STICKY)) {
328 fprintf(stderr, "Conky: hint - sticky\n"); fflush(stderr);
330 xa = ATOM(_NET_WM_DESKTOP);
332 uint32_t xa_prop = UINT32_MAX;
333 XChangeProperty(display, window.window, xa,
336 (unsigned char *) &xa_prop,
340 xa = ATOM(_NET_WM_STATE);
342 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
343 XChangeProperty(display, window.window, xa,
346 (unsigned char *) &xa_prop,
352 if (TEST_HINT(window.hints,HINT_SKIP_TASKBAR)) {
353 fprintf(stderr, "Conky: hint - skip_taskbar\n"); fflush(stderr);
355 xa = ATOM(_NET_WM_STATE);
357 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
358 XChangeProperty(display, window.window, xa,
361 (unsigned char *) &xa_prop,
367 if (TEST_HINT(window.hints,HINT_SKIP_PAGER)) {
368 fprintf(stderr, "Conky: hint - skip_pager\n"); fflush(stderr);
370 xa = ATOM(_NET_WM_STATE);
372 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
373 XChangeProperty(display, window.window, xa,
376 (unsigned char *) &xa_prop,
381 XMapWindow(display, window.window);
386 /* root / desktop window */
388 XWindowAttributes attrs;
391 window.window = find_desktop_window( &window.root, &window.desktop );
393 if (XGetWindowAttributes(display, window.window, &attrs)) {
394 window.width = attrs.width;
395 window.height = attrs.height;
398 fprintf(stderr, "Conky: drawing to desktop window\n");
401 /* Drawable is same as window. This may be changed by double buffering. */
402 window.drawable = window.window;
407 if (!XdbeQueryExtension(display, &major, &minor)) {
411 XdbeAllocateBackBufferName(display,
414 if (window.back_buffer != None) {
415 window.drawable = window.back_buffer;
417 "Conky: drawing to double buffer\n");
422 ERR("failed to set up double buffer");
425 fprintf(stderr, "Conky: drawing to single buffer\n");
430 /*set_transparent_background(window.window); must be done after double buffer stuff? */
433 set_transparent_background(window.window);
434 XClearWindow(display, window.window);
438 XSelectInput(display, window.window, ExposureMask
441 ? (StructureNotifyMask | PropertyChangeMask | ButtonPressMask) : 0)
446 static Window find_subwindow(Window win, int w, int h)
449 Window troot, parent, *children;
452 /* search subwindows with same size as display or work area */
454 for (i = 0; i < 10; i++) {
455 XQueryTree(display, win, &troot, &parent, &children, &n);
457 for (j = 0; j < n; j++) {
458 XWindowAttributes attrs;
460 if (XGetWindowAttributes
461 (display, children[j], &attrs)) {
462 /* Window must be mapped and same size as display or work space */
463 if (attrs.map_state != 0 &&
464 ((attrs.width == display_width
465 && attrs.height == display_height)
467 && attrs.height == h))) {
482 long get_x11_color(const char *name)
487 (display, DefaultColormap(display, screen), name, &color)) {
488 /* lets check if it's a hex colour with the # missing in front
489 * if yes, then do something about it
493 strncpy(&newname[1], name, 62);
494 /* now lets try again */
495 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
496 ERR("can't parse X color '%s'", name);
501 (display, DefaultColormap(display, screen), &color))
502 ERR("can't allocate X color '%s'", name);
504 return (long) color.pixel;
510 values.graphics_exposures = 0;
511 values.function = GXcopy;
512 window.gc = XCreateGC(display, window.drawable,
513 GCFunction | GCGraphicsExposures, &values);