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>
59 /* some basic X11 stuff */
60 Display *display = NULL;
64 static int set_transparent;
65 static int background_colour;
67 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
71 struct conky_window window;
72 char window_created = 0;
74 /* local prototypes */
75 static void update_workarea(void);
76 static Window find_desktop_window(Window *p_root, Window *p_desktop);
77 static Window find_subwindow(Window win, int w, int h);
80 void init_X11(const char *disp)
83 if ((display = XOpenDisplay(disp)) == NULL) {
84 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
88 screen = DefaultScreen(display);
89 display_width = DisplayWidth(display, screen);
90 display_height = DisplayHeight(display, screen);
92 get_x11_desktop_info(display, 0);
97 static void update_workarea(void)
99 /* default work area is display */
102 workarea[2] = display_width;
103 workarea[3] = display_height;
106 /* Find root window and desktop window.
107 * Return desktop window on success,
108 * and set root and desktop byref return values.
109 * Return 0 on failure. */
110 static Window find_desktop_window(Window *p_root, Window *p_desktop)
114 unsigned long nitems, bytes;
116 Window root = RootWindow(display, screen);
118 Window troot, parent, *children;
119 unsigned char *buf = NULL;
121 if (!p_root || !p_desktop) {
125 /* some window managers set __SWM_VROOT to some child of root window */
127 XQueryTree(display, root, &troot, &parent, &children, &n);
128 for (i = 0; i < (int) n; i++) {
129 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
130 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
131 == Success && type == XA_WINDOW) {
132 win = *(Window *) buf;
136 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
151 /* get subwindows from root */
152 win = find_subwindow(root, -1, -1);
156 win = find_subwindow(win, workarea[2], workarea[3]);
165 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
168 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
179 /* if no argb visual is configured sets background to ParentRelative for the Window and all parents,
180 else real transparency is used */
181 void set_transparent_background(Window win)
183 static int colour_set = -1;
186 if (have_argb_visual) {
188 if (set_transparent) {
189 XSetWindowBackground(display, win, 0x00);
190 } else if (colour_set != background_colour) {
191 XSetWindowBackground(display, win,
192 background_colour | (0xff << 24));
193 colour_set = background_colour;
196 #endif /* USE_ARGB */
197 // pseudo transparency
199 if (set_transparent) {
203 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
207 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
209 XQueryTree(display, parent, &r, &parent, &children, &n);
212 } else if (colour_set != background_colour) {
213 XSetWindowBackground(display, win, background_colour);
214 colour_set = background_colour;
218 #endif /* USE_ARGB */
222 static int get_argb_visual(Visual** visual, int *depth) {
223 /* code from gtk project, gdk_screen_get_rgba_visual */
224 XVisualInfo visual_template;
225 XVisualInfo *visual_list;
226 int nxvisuals = 0, i;
228 visual_template.screen = screen;
229 visual_list = XGetVisualInfo (display, VisualScreenMask,
230 &visual_template, &nxvisuals);
231 for (i = 0; i < nxvisuals; i++) {
232 if (visual_list[i].depth == 32 &&
233 (visual_list[i].red_mask == 0xff0000 &&
234 visual_list[i].green_mask == 0x00ff00 &&
235 visual_list[i].blue_mask == 0x0000ff)) {
236 *visual = visual_list[i].visual;
237 *depth = visual_list[i].depth;
241 // no argb visual available
244 #endif /* USE_ARGB */
246 void destroy_window(void)
250 XftDrawDestroy(window.xftdraw);
254 XFreeGC(display, window.gc);
256 memset(&window, 0, sizeof(struct conky_window));
259 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
260 char **argv, int argc)
262 /* There seems to be some problems with setting transparent background
263 * (on fluxbox this time). It doesn't happen always and I don't know why it
264 * happens but I bet the bug is somewhere here. */
265 set_transparent = set_trans;
266 background_colour = back_colour;
271 int depth = 0, flags;
272 Visual *visual = NULL;
274 if (!find_desktop_window(&window.root, &window.desktop)) {
279 if (use_argb_visual && get_argb_visual(&visual, &depth)) {
280 have_argb_visual = 1;
281 window.visual = visual;
282 window.colourmap = XCreateColormap(display,
283 DefaultRootWindow(display), window.visual, AllocNone);
285 #endif /* USE_ARGB */
286 window.visual = DefaultVisual(display, screen);
287 window.colourmap = DefaultColormap(display, screen);
288 depth = CopyFromParent;
289 visual = CopyFromParent;
292 #endif /* USE_ARGB */
294 if (window.type == TYPE_OVERRIDE) {
296 /* An override_redirect True window.
297 * No WM hints or button processing needed. */
298 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
299 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
302 if (have_argb_visual) {
303 attrs.colormap = window.colourmap;
304 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
306 #endif /* USE_ARGB */
307 flags = CWBackPixel | CWOverrideRedirect;
310 #endif /* USE_ARGB */
312 /* Parent is desktop window (which might be a child of root) */
313 window.window = XCreateWindow(display, window.desktop, window.x,
314 window.y, w, h, 0, depth, InputOutput, visual,
317 XLowerWindow(display, window.window);
319 fprintf(stderr, PACKAGE_NAME": window type - override\n");
321 } else { /* window.type != TYPE_OVERRIDE */
323 /* A window managed by the window manager.
324 * Process hints and buttons. */
325 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
326 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
327 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
329 XClassHint classHint;
334 if (have_argb_visual) {
335 attrs.colormap = window.colourmap;
336 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
338 #endif /* USE_ARGB */
339 flags = CWBackPixel | CWOverrideRedirect;
342 #endif /* USE_ARGB */
344 if (window.type == TYPE_DOCK) {
345 window.x = window.y = 0;
347 /* Parent is root window so WM can take control */
348 window.window = XCreateWindow(display, window.root, window.x,
349 window.y, w, h, 0, depth, InputOutput, visual,
352 classHint.res_name = window.class_name;
353 classHint.res_class = classHint.res_name;
355 wmHint.flags = InputHint | StateHint;
356 /* allow decorated windows to be given input focus by WM */
358 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
359 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
360 wmHint.initial_state = WithdrawnState;
362 wmHint.initial_state = NormalState;
365 XmbSetWMProperties(display, window.window, NULL, NULL, argv,
366 argc, NULL, &wmHint, &classHint);
367 XStoreName(display, window.window, window.title);
369 /* Sets an empty WM_PROTOCOLS property */
370 XSetWMProtocols(display, window.window, NULL, 0);
372 /* Set window type */
373 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
376 switch (window.type) {
378 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
379 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
383 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
384 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
388 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
389 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
394 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
395 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
399 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
400 PropModeReplace, (unsigned char *) &prop, 1);
403 /* Set desired hints */
405 /* Window decorations */
406 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
407 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
410 xa = ATOM(_MOTIF_WM_HINTS);
412 long prop[5] = { 2, 0, 0, 0, 0 };
413 XChangeProperty(display, window.window, xa, xa, 32,
414 PropModeReplace, (unsigned char *) prop, 5);
418 /* Below other windows */
419 if (TEST_HINT(window.hints, HINT_BELOW)) {
420 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
423 xa = ATOM(_WIN_LAYER);
427 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
428 PropModeAppend, (unsigned char *) &prop, 1);
431 xa = ATOM(_NET_WM_STATE);
433 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
435 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
436 PropModeAppend, (unsigned char *) &xa_prop, 1);
440 /* Above other windows */
441 if (TEST_HINT(window.hints, HINT_ABOVE)) {
442 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
445 xa = ATOM(_WIN_LAYER);
449 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
450 PropModeAppend, (unsigned char *) &prop, 1);
453 xa = ATOM(_NET_WM_STATE);
455 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
457 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
458 PropModeAppend, (unsigned char *) &xa_prop, 1);
463 if (TEST_HINT(window.hints, HINT_STICKY)) {
464 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
467 xa = ATOM(_NET_WM_DESKTOP);
469 CARD32 xa_prop = 0xFFFFFFFF;
471 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
472 PropModeAppend, (unsigned char *) &xa_prop, 1);
475 xa = ATOM(_NET_WM_STATE);
477 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
479 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
480 PropModeAppend, (unsigned char *) &xa_prop, 1);
485 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
486 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
489 xa = ATOM(_NET_WM_STATE);
491 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
493 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
494 PropModeAppend, (unsigned char *) &xa_prop, 1);
499 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
500 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
503 xa = ATOM(_NET_WM_STATE);
505 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
507 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
508 PropModeAppend, (unsigned char *) &xa_prop, 1);
513 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
517 XMapWindow(display, window.window);
520 #endif /* OWN_WINDOW */
522 XWindowAttributes attrs;
524 if (!window.window) {
525 window.window = find_desktop_window(&window.root, &window.desktop);
528 if (XGetWindowAttributes(display, window.window, &attrs)) {
529 window.width = attrs.width;
530 window.height = attrs.height;
533 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
536 /* Drawable is same as window. This may be changed by double buffering. */
537 window.drawable = window.window;
543 if (!XdbeQueryExtension(display, &major, &minor)) {
546 window.back_buffer = XdbeAllocateBackBufferName(display,
547 window.window, XdbeBackground);
548 if (window.back_buffer != None) {
549 window.drawable = window.back_buffer;
550 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
556 NORM_ERR("failed to set up double buffer");
560 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
565 cimlib_init(display, window.drawable, window.visual, window.colourmap);
570 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
572 | (own_window ? (StructureNotifyMask |
573 ButtonPressMask | ButtonReleaseMask) : 0)
578 static Window find_subwindow(Window win, int w, int h)
581 Window troot, parent, *children;
584 /* search subwindows with same size as display or work area */
586 for (i = 0; i < 10; i++) {
587 XQueryTree(display, win, &troot, &parent, &children, &n);
589 for (j = 0; j < n; j++) {
590 XWindowAttributes attrs;
592 if (XGetWindowAttributes(display, children[j], &attrs)) {
593 /* Window must be mapped and same size as display or
595 if (attrs.map_state != 0 && ((attrs.width == display_width
596 && attrs.height == display_height)
597 || (attrs.width == w && attrs.height == h))) {
617 values.graphics_exposures = 0;
618 values.function = GXcopy;
619 window.gc = XCreateGC(display, window.drawable,
620 GCFunction | GCGraphicsExposures, &values);
623 //Get current desktop number
624 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
628 unsigned long nitems;
629 unsigned long bytes_after;
630 unsigned char *prop = NULL;
631 struct information *current_info = &info;
633 if (atom == None) return;
635 if ( (XGetWindowProperty( current_display, root, atom,
636 0, 1L, False, XA_CARDINAL,
637 &actual_type, &actual_format, &nitems,
638 &bytes_after, &prop ) == Success ) &&
639 (actual_type == XA_CARDINAL) &&
640 (nitems == 1L) && (actual_format == 32) ) {
641 current_info->x11.desktop.current = prop[0]+1;
648 //Get total number of available desktops
649 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
653 unsigned long nitems;
654 unsigned long bytes_after;
655 unsigned char *prop = NULL;
656 struct information *current_info = &info;
658 if (atom == None) return;
660 if ( (XGetWindowProperty( current_display, root, atom,
661 0, 1L, False, XA_CARDINAL,
662 &actual_type, &actual_format, &nitems,
663 &bytes_after, &prop ) == Success ) &&
664 (actual_type == XA_CARDINAL) &&
665 (nitems == 1L) && (actual_format == 32) ) {
666 current_info->x11.desktop.number = prop[0];
673 //Get all desktop names
674 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
678 unsigned long nitems;
679 unsigned long bytes_after;
680 unsigned char *prop = NULL;
681 struct information *current_info = &info;
683 if (atom == None) return;
685 if ( (XGetWindowProperty( current_display, root, atom,
686 0, (~0L), False, ATOM(UTF8_STRING),
687 &actual_type, &actual_format, &nitems,
688 &bytes_after, &prop ) == Success ) &&
689 (actual_type == ATOM(UTF8_STRING)) &&
690 (nitems > 0L) && (actual_format == 8) ) {
692 if(current_info->x11.desktop.all_names) {
693 free(current_info->x11.desktop.all_names);
694 current_info->x11.desktop.all_names = NULL;
696 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
697 memcpy(current_info->x11.desktop.all_names, prop, nitems);
698 current_info->x11.desktop.nitems = nitems;
705 //Get current desktop name
706 static inline void get_x11_desktop_current_name(char *names)
708 struct information *current_info = &info;
709 unsigned int i = 0, j = 0;
712 while ( i < current_info->x11.desktop.nitems ) {
713 if ( names[i++] == '\0' ) {
714 if ( ++k == current_info->x11.desktop.current ) {
715 if (current_info->x11.desktop.name) {
716 free(current_info->x11.desktop.name);
717 current_info->x11.desktop.name = NULL;
719 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
720 //desktop names can be empty but should always be not null
721 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
729 void get_x11_desktop_info(Display *current_display, Atom atom)
732 static Atom atom_current, atom_number, atom_names;
733 struct information *current_info = &info;
734 XWindowAttributes window_attributes;
736 root = RootWindow(current_display, current_info->x11.monitor.current);
738 /* Check if we initialise else retrieve changed property */
740 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
741 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
742 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
743 get_x11_desktop_current(current_display, root, atom_current);
744 get_x11_desktop_number(current_display, root, atom_number);
745 get_x11_desktop_names(current_display, root, atom_names);
746 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
748 /* Set the PropertyChangeMask on the root window, if not set */
749 XGetWindowAttributes(display, root, &window_attributes);
750 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
751 XSetWindowAttributes attributes;
752 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
753 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
754 XGetWindowAttributes(display, root, &window_attributes);
757 if (atom == atom_current) {
758 get_x11_desktop_current(current_display, root, atom_current);
759 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
760 } else if (atom == atom_number) {
761 get_x11_desktop_number(current_display, root, atom_number);
762 } else if (atom == atom_names) {
763 get_x11_desktop_names(current_display, root, atom_names);
764 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
769 void update_x11info(void)
771 struct information *current_info = &info;
772 if (!x_initialised == YES)
774 current_info->x11.monitor.number = XScreenCount(display);
775 current_info->x11.monitor.current = XDefaultScreen(display);
779 /* reserve window manager space */
780 void set_struts(int sidenum)
783 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
784 /* reserve space at left, right, top, bottom */
785 signed long sizes[12] = {0};
788 /* define strut depth */
792 sizes[0] = window.x + window.width;
796 sizes[1] = display_width - window.x;
800 sizes[2] = window.y + window.height;
804 sizes[3] = display_height - window.y;
808 /* define partial strut length */
810 sizes[4 + (sidenum*2)] = window.y;
811 sizes[5 + (sidenum*2)] = window.y + window.height;
812 } else if (sidenum <= 3) {
813 sizes[4 + (sidenum*2)] = window.x;
814 sizes[5 + (sidenum*2)] = window.x + window.width;
817 /* check constraints */
818 for (i = 0; i < 12; i++) {
822 if (i <= 1 || i >= 8) {
823 if (sizes[i] > display_width) {
824 sizes[i] = display_width;
827 if (sizes[i] > display_height) {
828 sizes[i] = display_height;
834 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
835 PropModeReplace, (unsigned char *) &sizes, 4);
837 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
838 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
839 PropModeReplace, (unsigned char *) &sizes, 12);
843 #endif /* OWN_WINDOW */
846 void xdbe_swap_buffers(void)
851 swap.swap_window = window.window;
852 swap.swap_action = XdbeBackground;
853 XdbeSwapBuffers(display, &swap, 1);
856 #endif /* HAVE_XDBE */