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/>.
26 * vim: ts=4 sw=4 noet ai cindent syntax=c
37 #include <X11/Xatom.h>
39 #include <X11/Xutil.h>
45 #include <X11/Xft/Xft.h>
53 /* some basic X11 stuff */
54 Display *display = NULL;
58 static int set_transparent;
59 static int background_colour;
61 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
65 struct conky_window window;
67 /* local prototypes */
68 static void update_workarea(void);
69 static Window find_desktop_window(Window *p_root, Window *p_desktop);
70 static Window find_subwindow(Window win, int w, int h);
73 void init_X11(const char *disp)
76 if ((display = XOpenDisplay(disp)) == NULL) {
77 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(0));
81 screen = DefaultScreen(display);
82 display_width = DisplayWidth(display, screen);
83 display_height = DisplayHeight(display, screen);
85 get_x11_desktop_info(display, 0);
90 static void update_workarea(void)
92 /* default work area is display */
95 workarea[2] = display_width;
96 workarea[3] = display_height;
99 /* Find root window and desktop window.
100 * Return desktop window on success,
101 * and set root and desktop byref return values.
102 * Return 0 on failure. */
103 static Window find_desktop_window(Window *p_root, Window *p_desktop)
107 unsigned long nitems, bytes;
109 Window root = RootWindow(display, screen);
111 Window troot, parent, *children;
112 unsigned char *buf = NULL;
114 if (!p_root || !p_desktop) {
118 /* some window managers set __SWM_VROOT to some child of root window */
120 XQueryTree(display, root, &troot, &parent, &children, &n);
121 for (i = 0; i < (int) n; i++) {
122 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
123 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
124 == Success && type == XA_WINDOW) {
125 win = *(Window *) buf;
129 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
144 /* get subwindows from root */
145 win = find_subwindow(root, -1, -1);
149 win = find_subwindow(win, workarea[2], workarea[3]);
158 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
161 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
172 /* sets background to ParentRelative for the Window and all parents */
173 void set_transparent_background(Window win)
175 static int colour_set = -1;
177 if (set_transparent) {
181 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
185 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
187 XQueryTree(display, parent, &r, &parent, &children, &n);
190 } else if (colour_set != background_colour) {
191 XSetWindowBackground(display, win, background_colour);
192 colour_set = background_colour;
194 // XClearWindow(display, win); not sure why this was here
197 void destroy_window(void)
200 XftDrawDestroy(window.xftdraw);
203 XFreeGC(display, window.gc);
205 memset(&window, 0, sizeof(struct conky_window));
208 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
209 char **argv, int argc)
211 /* There seems to be some problems with setting transparent background
212 * (on fluxbox this time). It doesn't happen always and I don't know why it
213 * happens but I bet the bug is somewhere here. */
214 set_transparent = set_trans;
215 background_colour = back_colour;
219 if (!find_desktop_window(&window.root, &window.desktop)) {
223 if (window.type == TYPE_OVERRIDE) {
225 /* An override_redirect True window.
226 * No WM hints or button processing needed. */
227 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
228 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
231 /* Parent is desktop window (which might be a child of root) */
232 window.window = XCreateWindow(display, window.desktop, window.x,
233 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
234 CWBackPixel | CWOverrideRedirect, &attrs);
236 XLowerWindow(display, window.window);
238 fprintf(stderr, PACKAGE_NAME": window type - override\n");
240 } else { /* window.type != TYPE_OVERRIDE */
242 /* A window managed by the window manager.
243 * Process hints and buttons. */
244 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
245 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
246 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
248 XClassHint classHint;
252 if (window.type == TYPE_DOCK) {
253 window.x = window.y = 0;
255 /* Parent is root window so WM can take control */
256 window.window = XCreateWindow(display, window.root, window.x,
257 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
258 CWBackPixel | CWOverrideRedirect, &attrs);
260 classHint.res_name = window.class_name;
261 classHint.res_class = classHint.res_name;
263 wmHint.flags = InputHint | StateHint;
264 /* allow decorated windows to be given input focus by WM */
266 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
267 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
268 wmHint.initial_state = WithdrawnState;
270 wmHint.initial_state = NormalState;
273 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
274 argc, NULL, &wmHint, &classHint);
276 /* Sets an empty WM_PROTOCOLS property */
277 XSetWMProtocols(display, window.window, NULL, 0);
279 /* Set window type */
280 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
283 switch (window.type) {
285 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
286 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
290 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
291 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
295 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
296 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
301 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
302 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
306 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
307 PropModeReplace, (unsigned char *) &prop, 1);
310 /* Set desired hints */
312 /* Window decorations */
313 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
314 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
317 xa = ATOM(_MOTIF_WM_HINTS);
319 long prop[5] = { 2, 0, 0, 0, 0 };
320 XChangeProperty(display, window.window, xa, xa, 32,
321 PropModeReplace, (unsigned char *) prop, 5);
325 /* Below other windows */
326 if (TEST_HINT(window.hints, HINT_BELOW)) {
327 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
330 xa = ATOM(_WIN_LAYER);
334 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
335 PropModeAppend, (unsigned char *) &prop, 1);
338 xa = ATOM(_NET_WM_STATE);
340 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
342 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
343 PropModeAppend, (unsigned char *) &xa_prop, 1);
347 /* Above other windows */
348 if (TEST_HINT(window.hints, HINT_ABOVE)) {
349 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
352 xa = ATOM(_WIN_LAYER);
356 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
357 PropModeAppend, (unsigned char *) &prop, 1);
360 xa = ATOM(_NET_WM_STATE);
362 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
364 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
365 PropModeAppend, (unsigned char *) &xa_prop, 1);
370 if (TEST_HINT(window.hints, HINT_STICKY)) {
371 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
374 xa = ATOM(_NET_WM_DESKTOP);
376 CARD32 xa_prop = 0xFFFFFFFF;
378 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
379 PropModeAppend, (unsigned char *) &xa_prop, 1);
382 xa = ATOM(_NET_WM_STATE);
384 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
386 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
387 PropModeAppend, (unsigned char *) &xa_prop, 1);
392 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
393 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
396 xa = ATOM(_NET_WM_STATE);
398 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
400 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
401 PropModeAppend, (unsigned char *) &xa_prop, 1);
406 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
407 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
410 xa = ATOM(_NET_WM_STATE);
412 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
414 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
415 PropModeAppend, (unsigned char *) &xa_prop, 1);
418 } /* else { window.type != TYPE_OVERRIDE */
420 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
424 XMapWindow(display, window.window);
426 } else /* if (own_window) { */
428 /* root / desktop window */
430 XWindowAttributes attrs;
432 if (!window.window) {
433 window.window = find_desktop_window(&window.root, &window.desktop);
436 if (XGetWindowAttributes(display, window.window, &attrs)) {
437 window.width = attrs.width;
438 window.height = attrs.height;
441 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
444 /* Drawable is same as window. This may be changed by double buffering. */
445 window.drawable = window.window;
451 if (!XdbeQueryExtension(display, &major, &minor)) {
454 window.back_buffer = XdbeAllocateBackBufferName(display,
455 window.window, XdbeBackground);
456 if (window.back_buffer != None) {
457 window.drawable = window.back_buffer;
458 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
464 ERR("failed to set up double buffer");
468 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
471 window.visual = DefaultVisual(display, DefaultScreen(display));
472 window.colourmap = DefaultColormap(display, DefaultScreen(display));
475 cimlib_init(display, window.drawable, window.visual, window.colourmap);
480 /* set_transparent_background(window.window);
481 * must be done after double buffer stuff? */
484 set_transparent_background(window.window);
485 XClearWindow(display, window.window);
489 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
491 | (own_window ? (StructureNotifyMask |
492 ButtonPressMask | ButtonReleaseMask) : 0)
497 static Window find_subwindow(Window win, int w, int h)
500 Window troot, parent, *children;
503 /* search subwindows with same size as display or work area */
505 for (i = 0; i < 10; i++) {
506 XQueryTree(display, win, &troot, &parent, &children, &n);
508 for (j = 0; j < n; j++) {
509 XWindowAttributes attrs;
511 if (XGetWindowAttributes(display, children[j], &attrs)) {
512 /* Window must be mapped and same size as display or
514 if (attrs.map_state != 0 && ((attrs.width == display_width
515 && attrs.height == display_height)
516 || (attrs.width == w && attrs.height == h))) {
532 long get_x11_color(const char *name)
537 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
538 /* lets check if it's a hex colour with the # missing in front
539 * if yes, then do something about it */
540 char newname[DEFAULT_TEXT_BUFFER_SIZE];
543 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
544 /* now lets try again */
545 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
547 ERR("can't parse X color '%s'", name);
551 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
552 ERR("can't allocate X color '%s'", name);
555 return (long) color.pixel;
562 values.graphics_exposures = 0;
563 values.function = GXcopy;
564 window.gc = XCreateGC(display, window.drawable,
565 GCFunction | GCGraphicsExposures, &values);
568 //Get current desktop number
569 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
573 unsigned long nitems;
574 unsigned long bytes_after;
575 unsigned char *prop = NULL;
576 struct information *current_info = &info;
578 if ( (XGetWindowProperty( current_display, root, atom,
579 0, 1L, False, XA_CARDINAL,
580 &actual_type, &actual_format, &nitems,
581 &bytes_after, &prop ) == Success ) &&
582 (actual_type == XA_CARDINAL) &&
583 (nitems == 1L) && (actual_format == 32) ) {
584 current_info->x11.desktop.current = prop[0]+1;
591 //Get total number of available desktops
592 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
596 unsigned long nitems;
597 unsigned long bytes_after;
598 unsigned char *prop = NULL;
599 struct information *current_info = &info;
601 if ( (XGetWindowProperty( current_display, root, atom,
602 0, 1L, False, XA_CARDINAL,
603 &actual_type, &actual_format, &nitems,
604 &bytes_after, &prop ) == Success ) &&
605 (actual_type == XA_CARDINAL) &&
606 (nitems == 1L) && (actual_format == 32) ) {
607 current_info->x11.desktop.number = prop[0];
614 //Get all desktop names
615 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
619 unsigned long nitems;
620 unsigned long bytes_after;
621 unsigned char *prop = NULL;
622 struct information *current_info = &info;
624 if ( (XGetWindowProperty( current_display, root, atom,
625 0, (~0L), False, ATOM(UTF8_STRING),
626 &actual_type, &actual_format, &nitems,
627 &bytes_after, &prop ) == Success ) &&
628 (actual_type == ATOM(UTF8_STRING)) &&
629 (nitems > 0L) && (actual_format == 8) ) {
631 if(current_info->x11.desktop.all_names) {
632 free(current_info->x11.desktop.all_names);
633 current_info->x11.desktop.all_names = NULL;
635 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
636 memcpy(current_info->x11.desktop.all_names, prop, nitems);
637 current_info->x11.desktop.nitems = nitems;
644 //Get current desktop name
645 static inline void get_x11_desktop_current_name(char *names)
647 struct information *current_info = &info;
648 unsigned int i = 0, j = 0;
651 while ( i < current_info->x11.desktop.nitems ) {
652 if ( names[i++] == '\0' ) {
653 if ( ++k == current_info->x11.desktop.current ) {
654 if (current_info->x11.desktop.name) {
655 free(current_info->x11.desktop.name);
656 current_info->x11.desktop.name = NULL;
658 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
659 //desktop names can be empty but should always be not null
660 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
668 void get_x11_desktop_info(Display *current_display, Atom atom)
671 static Atom atom_current, atom_number, atom_names;
672 struct information *current_info = &info;
673 XWindowAttributes window_attributes;
675 root = RootWindow(current_display, current_info->x11.monitor.current);
677 //Check if we initialise else retrieve changed property
679 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
680 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
681 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
682 get_x11_desktop_current(current_display, root, atom_current);
683 get_x11_desktop_number(current_display, root, atom_number);
684 get_x11_desktop_names(current_display, root, atom_names);
685 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
687 //Set the PropertyChangeMask on the root window, if not set
688 XGetWindowAttributes(display, root, &window_attributes);
689 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
690 XSetWindowAttributes attributes;
691 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
692 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
693 XGetWindowAttributes(display, root, &window_attributes);
696 if (atom == atom_current) {
697 get_x11_desktop_current(current_display, root, atom_current);
698 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
699 } else if (atom == atom_number) {
700 get_x11_desktop_number(current_display, root, atom_number);
701 } else if (atom == atom_names) {
702 get_x11_desktop_names(current_display, root, atom_names);
703 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
708 void update_x11info(void)
710 struct information *current_info = &info;
711 current_info->x11.monitor.number = XScreenCount(display);
712 current_info->x11.monitor.current = XDefaultScreen(display);
716 /* reserve window manager space */
717 void set_struts(int sidenum)
720 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
721 /* reserve space at left, right, top, bottom */
722 signed long sizes[12] = {0};
725 /* define strut depth */
730 sizes[0] = window.x + window.width;
736 sizes[1] = display_width - window.x;
742 sizes[2] = window.y + window.height;
748 sizes[3] = display_height - window.y;
753 /* define partial strut length */
755 sizes[4 + (sidenum*2)] = window.y;
756 sizes[5 + (sidenum*2)] = window.y + window.height;
757 } else if (sidenum <= 3) {
758 sizes[4 + (sidenum*2)] = window.x;
759 sizes[5 + (sidenum*2)] = window.x + window.width;
762 /* check constraints */
763 for (i = 0; i < 12; i++) {
767 if (i <= 1 || i >= 8) {
768 if (sizes[i] > display_width) {
769 sizes[i] = display_width;
772 if (sizes[i] > display_height) {
773 sizes[i] = display_height;
779 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
780 PropModeReplace, (unsigned char *) &sizes, 4);
782 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
783 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
784 PropModeReplace, (unsigned char *) &sizes, 12);
788 #endif /* OWN_WINDOW */