1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 #include <X11/Xatom.h>
40 #include <X11/Xutil.h>
46 #include <X11/Xft/Xft.h>
54 /* some basic X11 stuff */
55 Display *display = NULL;
59 static int set_transparent;
60 static int background_colour;
62 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
66 struct conky_window window;
67 char window_created = 0;
69 /* local prototypes */
70 static void update_workarea(void);
71 static Window find_desktop_window(Window *p_root, Window *p_desktop);
72 static Window find_subwindow(Window win, int w, int h);
75 void init_X11(const char *disp)
78 if ((display = XOpenDisplay(disp)) == NULL) {
79 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
83 screen = DefaultScreen(display);
84 display_width = DisplayWidth(display, screen);
85 display_height = DisplayHeight(display, screen);
87 get_x11_desktop_info(display, 0);
92 static void update_workarea(void)
94 /* default work area is display */
97 workarea[2] = display_width;
98 workarea[3] = display_height;
101 /* Find root window and desktop window.
102 * Return desktop window on success,
103 * and set root and desktop byref return values.
104 * Return 0 on failure. */
105 static Window find_desktop_window(Window *p_root, Window *p_desktop)
109 unsigned long nitems, bytes;
111 Window root = RootWindow(display, screen);
113 Window troot, parent, *children;
114 unsigned char *buf = NULL;
116 if (!p_root || !p_desktop) {
120 /* some window managers set __SWM_VROOT to some child of root window */
122 XQueryTree(display, root, &troot, &parent, &children, &n);
123 for (i = 0; i < (int) n; i++) {
124 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
125 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
126 == Success && type == XA_WINDOW) {
127 win = *(Window *) buf;
131 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
146 /* get subwindows from root */
147 win = find_subwindow(root, -1, -1);
151 win = find_subwindow(win, workarea[2], workarea[3]);
160 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
163 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
174 /* sets background to ParentRelative for the Window and all parents */
175 void set_transparent_background(Window win)
177 static int colour_set = -1;
179 if (set_transparent) {
183 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
187 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
189 XQueryTree(display, parent, &r, &parent, &children, &n);
192 } else if (colour_set != background_colour) {
193 XSetWindowBackground(display, win, background_colour);
194 colour_set = background_colour;
198 void destroy_window(void)
202 XftDrawDestroy(window.xftdraw);
206 XFreeGC(display, window.gc);
208 memset(&window, 0, sizeof(struct conky_window));
211 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
212 char **argv, int argc)
214 /* There seems to be some problems with setting transparent background
215 * (on fluxbox this time). It doesn't happen always and I don't know why it
216 * happens but I bet the bug is somewhere here. */
217 set_transparent = set_trans;
218 background_colour = back_colour;
223 if (!find_desktop_window(&window.root, &window.desktop)) {
227 if (window.type == TYPE_OVERRIDE) {
229 /* An override_redirect True window.
230 * No WM hints or button processing needed. */
231 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
232 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
235 /* Parent is desktop window (which might be a child of root) */
236 window.window = XCreateWindow(display, window.desktop, window.x,
237 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
238 CWBackPixel | CWOverrideRedirect, &attrs);
240 XLowerWindow(display, window.window);
242 fprintf(stderr, PACKAGE_NAME": window type - override\n");
244 } else { /* window.type != TYPE_OVERRIDE */
246 /* A window managed by the window manager.
247 * Process hints and buttons. */
248 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
249 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
250 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
252 XClassHint classHint;
256 if (window.type == TYPE_DOCK) {
257 window.x = window.y = 0;
259 /* Parent is root window so WM can take control */
260 window.window = XCreateWindow(display, window.root, window.x,
261 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
262 CWBackPixel | CWOverrideRedirect, &attrs);
264 classHint.res_name = window.class_name;
265 classHint.res_class = classHint.res_name;
267 wmHint.flags = InputHint | StateHint;
268 /* allow decorated windows to be given input focus by WM */
270 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
271 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
272 wmHint.initial_state = WithdrawnState;
274 wmHint.initial_state = NormalState;
277 XmbSetWMProperties(display, window.window, NULL, NULL, argv,
278 argc, NULL, &wmHint, &classHint);
279 XStoreName(display, window.window, window.title);
281 /* Sets an empty WM_PROTOCOLS property */
282 XSetWMProtocols(display, window.window, NULL, 0);
284 /* Set window type */
285 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
288 switch (window.type) {
290 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
291 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
295 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
296 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
300 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
301 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
306 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
307 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
311 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
312 PropModeReplace, (unsigned char *) &prop, 1);
315 /* Set desired hints */
317 /* Window decorations */
318 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
319 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
322 xa = ATOM(_MOTIF_WM_HINTS);
324 long prop[5] = { 2, 0, 0, 0, 0 };
325 XChangeProperty(display, window.window, xa, xa, 32,
326 PropModeReplace, (unsigned char *) prop, 5);
330 /* Below other windows */
331 if (TEST_HINT(window.hints, HINT_BELOW)) {
332 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
335 xa = ATOM(_WIN_LAYER);
339 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
340 PropModeAppend, (unsigned char *) &prop, 1);
343 xa = ATOM(_NET_WM_STATE);
345 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
347 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
348 PropModeAppend, (unsigned char *) &xa_prop, 1);
352 /* Above other windows */
353 if (TEST_HINT(window.hints, HINT_ABOVE)) {
354 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
357 xa = ATOM(_WIN_LAYER);
361 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
362 PropModeAppend, (unsigned char *) &prop, 1);
365 xa = ATOM(_NET_WM_STATE);
367 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
369 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
370 PropModeAppend, (unsigned char *) &xa_prop, 1);
375 if (TEST_HINT(window.hints, HINT_STICKY)) {
376 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
379 xa = ATOM(_NET_WM_DESKTOP);
381 CARD32 xa_prop = 0xFFFFFFFF;
383 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
384 PropModeAppend, (unsigned char *) &xa_prop, 1);
387 xa = ATOM(_NET_WM_STATE);
389 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
391 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
392 PropModeAppend, (unsigned char *) &xa_prop, 1);
397 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
398 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
401 xa = ATOM(_NET_WM_STATE);
403 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
405 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
406 PropModeAppend, (unsigned char *) &xa_prop, 1);
411 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
412 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
415 xa = ATOM(_NET_WM_STATE);
417 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
419 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
420 PropModeAppend, (unsigned char *) &xa_prop, 1);
425 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
429 XMapWindow(display, window.window);
432 #endif /* OWN_WINDOW */
434 XWindowAttributes attrs;
436 if (!window.window) {
437 window.window = find_desktop_window(&window.root, &window.desktop);
440 if (XGetWindowAttributes(display, window.window, &attrs)) {
441 window.width = attrs.width;
442 window.height = attrs.height;
445 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
448 /* Drawable is same as window. This may be changed by double buffering. */
449 window.drawable = window.window;
455 if (!XdbeQueryExtension(display, &major, &minor)) {
458 window.back_buffer = XdbeAllocateBackBufferName(display,
459 window.window, XdbeBackground);
460 if (window.back_buffer != None) {
461 window.drawable = window.back_buffer;
462 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
468 NORM_ERR("failed to set up double buffer");
472 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
475 window.visual = DefaultVisual(display, DefaultScreen(display));
476 window.colourmap = DefaultColormap(display, DefaultScreen(display));
479 cimlib_init(display, window.drawable, window.visual, window.colourmap);
484 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
486 | (own_window ? (StructureNotifyMask |
487 ButtonPressMask | ButtonReleaseMask) : 0)
492 static Window find_subwindow(Window win, int w, int h)
495 Window troot, parent, *children;
498 /* search subwindows with same size as display or work area */
500 for (i = 0; i < 10; i++) {
501 XQueryTree(display, win, &troot, &parent, &children, &n);
503 for (j = 0; j < n; j++) {
504 XWindowAttributes attrs;
506 if (XGetWindowAttributes(display, children[j], &attrs)) {
507 /* Window must be mapped and same size as display or
509 if (attrs.map_state != 0 && ((attrs.width == display_width
510 && attrs.height == display_height)
511 || (attrs.width == w && attrs.height == h))) {
527 long get_x11_color(const char *name)
532 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
533 /* lets check if it's a hex colour with the # missing in front
534 * if yes, then do something about it */
535 char newname[DEFAULT_TEXT_BUFFER_SIZE];
538 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
539 /* now lets try again */
540 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
542 NORM_ERR("can't parse X color '%s'", name);
546 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
547 NORM_ERR("can't allocate X color '%s'", name);
550 return (long) color.pixel;
557 values.graphics_exposures = 0;
558 values.function = GXcopy;
559 window.gc = XCreateGC(display, window.drawable,
560 GCFunction | GCGraphicsExposures, &values);
563 //Get current desktop number
564 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
568 unsigned long nitems;
569 unsigned long bytes_after;
570 unsigned char *prop = NULL;
571 struct information *current_info = &info;
573 if (atom == None) return;
575 if ( (XGetWindowProperty( current_display, root, atom,
576 0, 1L, False, XA_CARDINAL,
577 &actual_type, &actual_format, &nitems,
578 &bytes_after, &prop ) == Success ) &&
579 (actual_type == XA_CARDINAL) &&
580 (nitems == 1L) && (actual_format == 32) ) {
581 current_info->x11.desktop.current = prop[0]+1;
588 //Get total number of available desktops
589 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
593 unsigned long nitems;
594 unsigned long bytes_after;
595 unsigned char *prop = NULL;
596 struct information *current_info = &info;
598 if (atom == None) return;
600 if ( (XGetWindowProperty( current_display, root, atom,
601 0, 1L, False, XA_CARDINAL,
602 &actual_type, &actual_format, &nitems,
603 &bytes_after, &prop ) == Success ) &&
604 (actual_type == XA_CARDINAL) &&
605 (nitems == 1L) && (actual_format == 32) ) {
606 current_info->x11.desktop.number = prop[0];
613 //Get all desktop names
614 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
618 unsigned long nitems;
619 unsigned long bytes_after;
620 unsigned char *prop = NULL;
621 struct information *current_info = &info;
623 if (atom == None) return;
625 if ( (XGetWindowProperty( current_display, root, atom,
626 0, (~0L), False, ATOM(UTF8_STRING),
627 &actual_type, &actual_format, &nitems,
628 &bytes_after, &prop ) == Success ) &&
629 (actual_type == ATOM(UTF8_STRING)) &&
630 (nitems > 0L) && (actual_format == 8) ) {
632 if(current_info->x11.desktop.all_names) {
633 free(current_info->x11.desktop.all_names);
634 current_info->x11.desktop.all_names = NULL;
636 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
637 memcpy(current_info->x11.desktop.all_names, prop, nitems);
638 current_info->x11.desktop.nitems = nitems;
645 //Get current desktop name
646 static inline void get_x11_desktop_current_name(char *names)
648 struct information *current_info = &info;
649 unsigned int i = 0, j = 0;
652 while ( i < current_info->x11.desktop.nitems ) {
653 if ( names[i++] == '\0' ) {
654 if ( ++k == current_info->x11.desktop.current ) {
655 if (current_info->x11.desktop.name) {
656 free(current_info->x11.desktop.name);
657 current_info->x11.desktop.name = NULL;
659 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
660 //desktop names can be empty but should always be not null
661 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
669 void get_x11_desktop_info(Display *current_display, Atom atom)
672 static Atom atom_current, atom_number, atom_names;
673 struct information *current_info = &info;
674 XWindowAttributes window_attributes;
676 root = RootWindow(current_display, current_info->x11.monitor.current);
678 /* Check if we initialise else retrieve changed property */
680 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
681 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
682 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
683 get_x11_desktop_current(current_display, root, atom_current);
684 get_x11_desktop_number(current_display, root, atom_number);
685 get_x11_desktop_names(current_display, root, atom_names);
686 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
688 /* Set the PropertyChangeMask on the root window, if not set */
689 XGetWindowAttributes(display, root, &window_attributes);
690 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
691 XSetWindowAttributes attributes;
692 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
693 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
694 XGetWindowAttributes(display, root, &window_attributes);
697 if (atom == atom_current) {
698 get_x11_desktop_current(current_display, root, atom_current);
699 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
700 } else if (atom == atom_number) {
701 get_x11_desktop_number(current_display, root, atom_number);
702 } else if (atom == atom_names) {
703 get_x11_desktop_names(current_display, root, atom_names);
704 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
709 void update_x11info(void)
711 struct information *current_info = &info;
712 if (!x_initialised == YES)
714 current_info->x11.monitor.number = XScreenCount(display);
715 current_info->x11.monitor.current = XDefaultScreen(display);
719 /* reserve window manager space */
720 void set_struts(int sidenum)
723 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
724 /* reserve space at left, right, top, bottom */
725 signed long sizes[12] = {0};
728 /* define strut depth */
732 sizes[0] = window.x + window.width;
736 sizes[1] = display_width - window.x;
740 sizes[2] = window.y + window.height;
744 sizes[3] = display_height - window.y;
748 /* define partial strut length */
750 sizes[4 + (sidenum*2)] = window.y;
751 sizes[5 + (sidenum*2)] = window.y + window.height;
752 } else if (sidenum <= 3) {
753 sizes[4 + (sidenum*2)] = window.x;
754 sizes[5 + (sidenum*2)] = window.x + window.width;
757 /* check constraints */
758 for (i = 0; i < 12; i++) {
762 if (i <= 1 || i >= 8) {
763 if (sizes[i] > display_width) {
764 sizes[i] = display_width;
767 if (sizes[i] > display_height) {
768 sizes[i] = display_height;
774 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
775 PropModeReplace, (unsigned char *) &sizes, 4);
777 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
778 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
779 PropModeReplace, (unsigned char *) &sizes, 12);
783 #endif /* OWN_WINDOW */
786 void xdbe_swap_buffers(void)
791 swap.swap_window = window.window;
792 swap.swap_action = XdbeBackground;
793 XdbeSwapBuffers(display, &swap, 1);
796 #endif /* HAVE_XDBE */