1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
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/>.
35 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
43 #include <X11/Xft/Xft.h>
51 /* some basic X11 stuff */
52 Display *display = NULL;
56 static int set_transparent;
57 static int background_colour;
59 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
63 struct conky_window window;
65 /* local prototypes */
66 static void update_workarea(void);
67 static Window find_desktop_window(Window *p_root, Window *p_desktop);
68 static Window find_subwindow(Window win, int w, int h);
71 void init_X11(const char *disp)
74 if ((display = XOpenDisplay(disp)) == NULL) {
75 CRIT_ERR("can't open display: %s", XDisplayName(0));
79 screen = DefaultScreen(display);
80 display_width = DisplayWidth(display, screen);
81 display_height = DisplayHeight(display, screen);
86 static void update_workarea(void)
88 /* default work area is display */
91 workarea[2] = display_width;
92 workarea[3] = display_height;
95 /* Find root window and desktop window.
96 * Return desktop window on success,
97 * and set root and desktop byref return values.
98 * Return 0 on failure. */
99 static Window find_desktop_window(Window *p_root, Window *p_desktop)
103 unsigned long nitems, bytes;
105 Window root = RootWindow(display, screen);
107 Window troot, parent, *children;
108 unsigned char *buf = NULL;
110 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(display, children[i], ATOM(__SWM_VROOT), 0, 1,
119 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
120 == Success && type == XA_WINDOW) {
121 win = *(Window *) buf;
125 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
140 /* get subwindows from root */
141 win = find_subwindow(root, -1, -1);
145 win = find_subwindow(win, workarea[2], workarea[3]);
154 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
157 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
168 /* sets background to ParentRelative for the Window and all parents */
169 void set_transparent_background(Window win)
171 static int colour_set = -1;
173 if (set_transparent) {
177 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
181 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
183 XQueryTree(display, parent, &r, &parent, &children, &n);
186 } else if (colour_set != background_colour) {
187 XSetWindowBackground(display, win, background_colour);
188 colour_set = background_colour;
190 // XClearWindow(display, win); not sure why this was here
193 void destroy_window(void)
195 XDestroyWindow(display, window.window);
196 XFreeGC(display, window.gc);
197 memset(&window, 0, sizeof(struct conky_window));
200 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
201 char **argv, int argc)
203 /* There seems to be some problems with setting transparent background
204 * (on fluxbox this time). It doesn't happen always and I don't know why it
205 * happens but I bet the bug is somewhere here. */
206 set_transparent = set_trans;
207 background_colour = back_colour;
211 if (!find_desktop_window(&window.root, &window.desktop)) {
215 if (window.type == TYPE_OVERRIDE) {
217 /* An override_redirect True window.
218 * No WM hints or button processing needed. */
219 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
220 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
223 /* Parent is desktop window (which might be a child of root) */
224 window.window = XCreateWindow(display, window.desktop, window.x,
225 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
226 CWBackPixel | CWOverrideRedirect, &attrs);
228 XLowerWindow(display, window.window);
230 fprintf(stderr, PACKAGE_NAME": window type - override\n");
232 } else { /* window.type != TYPE_OVERRIDE */
234 /* A window managed by the window manager.
235 * Process hints and buttons. */
236 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
237 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
238 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
240 XClassHint classHint;
244 if (window.type == TYPE_DOCK) {
245 window.x = window.y = 0;
247 /* Parent is root window so WM can take control */
248 window.window = XCreateWindow(display, window.root, window.x,
249 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
250 CWBackPixel | CWOverrideRedirect, &attrs);
252 classHint.res_name = window.class_name;
253 classHint.res_class = classHint.res_name;
255 wmHint.flags = InputHint | StateHint;
256 /* allow decorated windows to be given input focus by WM */
258 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
259 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
260 wmHint.initial_state = WithdrawnState;
262 wmHint.initial_state = NormalState;
265 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
266 argc, NULL, &wmHint, &classHint);
268 /* Sets an empty WM_PROTOCOLS property */
269 XSetWMProtocols(display, window.window, NULL, 0);
271 /* Set window type */
272 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
275 switch (window.type) {
277 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
278 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
282 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
283 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
287 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
288 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
293 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
294 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
298 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
299 PropModeReplace, (unsigned char *) &prop, 1);
302 /* Set desired hints */
304 /* Window decorations */
305 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
306 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
309 xa = ATOM(_MOTIF_WM_HINTS);
311 long prop[5] = { 2, 0, 0, 0, 0 };
312 XChangeProperty(display, window.window, xa, xa, 32,
313 PropModeReplace, (unsigned char *) prop, 5);
317 /* Below other windows */
318 if (TEST_HINT(window.hints, HINT_BELOW)) {
319 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
322 xa = ATOM(_WIN_LAYER);
326 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
327 PropModeAppend, (unsigned char *) &prop, 1);
330 xa = ATOM(_NET_WM_STATE);
332 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
334 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
335 PropModeAppend, (unsigned char *) &xa_prop, 1);
339 /* Above other windows */
340 if (TEST_HINT(window.hints, HINT_ABOVE)) {
341 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
344 xa = ATOM(_WIN_LAYER);
348 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
349 PropModeAppend, (unsigned char *) &prop, 1);
352 xa = ATOM(_NET_WM_STATE);
354 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
356 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
357 PropModeAppend, (unsigned char *) &xa_prop, 1);
362 if (TEST_HINT(window.hints, HINT_STICKY)) {
363 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
366 xa = ATOM(_NET_WM_DESKTOP);
368 CARD32 xa_prop = 0xFFFFFFFF;
370 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
371 PropModeAppend, (unsigned char *) &xa_prop, 1);
374 xa = ATOM(_NET_WM_STATE);
376 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
378 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
379 PropModeAppend, (unsigned char *) &xa_prop, 1);
384 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
385 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
388 xa = ATOM(_NET_WM_STATE);
390 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
392 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
393 PropModeAppend, (unsigned char *) &xa_prop, 1);
398 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
399 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
402 xa = ATOM(_NET_WM_STATE);
404 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
406 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
407 PropModeAppend, (unsigned char *) &xa_prop, 1);
410 } /* else { window.type != TYPE_OVERRIDE */
412 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
416 XMapWindow(display, window.window);
418 } else /* if (own_window) { */
420 /* root / desktop window */
422 XWindowAttributes attrs;
424 if (!window.window) {
425 window.window = find_desktop_window(&window.root, &window.desktop);
428 if (XGetWindowAttributes(display, window.window, &attrs)) {
429 window.width = attrs.width;
430 window.height = attrs.height;
433 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
436 /* Drawable is same as window. This may be changed by double buffering. */
437 window.drawable = window.window;
443 if (!XdbeQueryExtension(display, &major, &minor)) {
446 window.back_buffer = XdbeAllocateBackBufferName(display,
447 window.window, XdbeBackground);
448 if (window.back_buffer != None) {
449 window.drawable = window.back_buffer;
450 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
456 ERR("failed to set up double buffer");
460 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
463 window.visual = DefaultVisual(display, DefaultScreen(display));
464 window.colourmap = DefaultColormap(display, DefaultScreen(display));
467 cimlib_init(display, window.drawable, window.visual, window.colourmap);
472 /* set_transparent_background(window.window);
473 * must be done after double buffer stuff? */
476 set_transparent_background(window.window);
477 XClearWindow(display, window.window);
481 XSelectInput(display, window.window, ExposureMask
483 | (own_window ? (StructureNotifyMask | PropertyChangeMask |
484 ButtonPressMask | ButtonReleaseMask) : 0)
489 static Window find_subwindow(Window win, int w, int h)
492 Window troot, parent, *children;
495 /* search subwindows with same size as display or work area */
497 for (i = 0; i < 10; i++) {
498 XQueryTree(display, win, &troot, &parent, &children, &n);
500 for (j = 0; j < n; j++) {
501 XWindowAttributes attrs;
503 if (XGetWindowAttributes(display, children[j], &attrs)) {
504 /* Window must be mapped and same size as display or
506 if (attrs.map_state != 0 && ((attrs.width == display_width
507 && attrs.height == display_height)
508 || (attrs.width == w && attrs.height == h))) {
524 long get_x11_color(const char *name)
529 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
530 /* lets check if it's a hex colour with the # missing in front
531 * if yes, then do something about it */
532 char newname[DEFAULT_TEXT_BUFFER_SIZE];
535 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
536 /* now lets try again */
537 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
539 ERR("can't parse X color '%s'", name);
543 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
544 ERR("can't allocate X color '%s'", name);
547 return (long) color.pixel;
554 values.graphics_exposures = 0;
555 values.function = GXcopy;
556 window.gc = XCreateGC(display, window.drawable,
557 GCFunction | GCGraphicsExposures, &values);
560 void update_x11info(void)
563 Atom actual_type, atom;
565 unsigned long nitems;
566 unsigned long bytes_after;
567 unsigned char *prop = NULL;
569 struct information *current_info = &info;
570 current_info->x11.monitor.number = XScreenCount(display);
571 current_info->x11.monitor.current = XDefaultScreen(display);
573 root = RootWindow(display, current_info->x11.monitor.current);
575 //Get current desktop number
576 if ((atom = XInternAtom(display, "_NET_CURRENT_DESKTOP", True)) != None) {
577 if ( (XGetWindowProperty( display, root, atom,
578 0, 1L, False, XA_CARDINAL,
579 &actual_type, &actual_format, &nitems,
580 &bytes_after, &prop ) == Success ) &&
581 (actual_type == XA_CARDINAL) &&
583 current_info->x11.desktop.current = prop[0]+1;
587 //Get total number of available desktops
588 if ((atom = XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", True)) != None) {
589 if ( (XGetWindowProperty( display, root, atom,
590 0, 1L, False, XA_CARDINAL,
591 &actual_type, &actual_format, &nitems,
592 &bytes_after, &prop ) == Success ) &&
593 (actual_type == XA_CARDINAL) &&
595 current_info->x11.desktop.number = prop[0];
599 //Get current desktop name
600 if ((atom = XInternAtom(display, "_NET_DESKTOP_NAMES", True)) != None) {
601 if ( (XGetWindowProperty( display, root, atom,
602 0, (~0L), False, ATOM(UTF8_STRING),
603 &actual_type, &actual_format, &nitems,
604 &bytes_after, &prop ) == Success ) &&
605 (actual_type == ATOM(UTF8_STRING)) &&
607 unsigned int i = 0, j = 0;
609 while ( i < nitems ) {
610 if ( prop[i++] == '\0' ) {
611 if ( ++k == current_info->x11.desktop.current ) {
612 if(current_info->x11.desktop.name) {
613 free(current_info->x11.desktop.name);
614 current_info->x11.desktop.name = NULL;
616 current_info->x11.desktop.name = malloc(i-j);
617 //desktop names can be empty but should always be not null
618 strcpy( current_info->x11.desktop.name, (char *)&prop[j] );
629 /* reserve window manager space */
630 void set_struts(int sidenum)
633 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
634 /* reserve space at left, right, top, bottom */
635 signed long sizes[12] = {0};
638 /* define strut depth */
643 sizes[0] = window.x + window.width;
649 sizes[1] = display_width - window.x;
655 sizes[2] = window.y + window.height;
661 sizes[3] = display_height - window.y;
666 /* define partial strut length */
668 sizes[4 + (sidenum*2)] = window.y;
669 sizes[5 + (sidenum*2)] = window.y + window.height;
670 } else if (sidenum <= 3) {
671 sizes[4 + (sidenum*2)] = window.x;
672 sizes[5 + (sidenum*2)] = window.x + window.width;
675 /* check constraints */
676 for (i = 0; i < 12; i++) {
680 if (i <= 1 || i >= 8) {
681 if (sizes[i] > display_width) {
682 sizes[i] = display_width;
685 if (sizes[i] > display_height) {
686 sizes[i] = display_height;
692 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
693 PropModeReplace, (unsigned char *) &sizes, 4);
695 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
696 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
697 PropModeReplace, (unsigned char *) &sizes, 12);