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-2010 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>
58 int own_window_argb_value;
60 /* some basic X11 stuff */
61 Display *display = NULL;
65 static int set_transparent;
66 static int background_colour;
68 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
72 struct conky_window window;
73 char window_created = 0;
75 /* local prototypes */
76 static void update_workarea(void);
77 static Window find_desktop_window(Window *p_root, Window *p_desktop);
78 static Window find_subwindow(Window win, int w, int h);
81 void init_X11(const char *disp)
84 if ((display = XOpenDisplay(disp)) == NULL) {
85 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
89 screen = DefaultScreen(display);
90 display_width = DisplayWidth(display, screen);
91 display_height = DisplayHeight(display, screen);
93 get_x11_desktop_info(display, 0);
98 static void update_workarea(void)
100 /* default work area is display */
103 workarea[2] = display_width;
104 workarea[3] = display_height;
107 /* Find root window and desktop window.
108 * Return desktop window on success,
109 * and set root and desktop byref return values.
110 * Return 0 on failure. */
111 static Window find_desktop_window(Window *p_root, Window *p_desktop)
115 unsigned long nitems, bytes;
117 Window root = RootWindow(display, screen);
119 Window troot, parent, *children;
120 unsigned char *buf = NULL;
122 if (!p_root || !p_desktop) {
126 /* some window managers set __SWM_VROOT to some child of root window */
128 XQueryTree(display, root, &troot, &parent, &children, &n);
129 for (i = 0; i < (int) n; i++) {
130 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
131 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
132 == Success && type == XA_WINDOW) {
133 win = *(Window *) buf;
137 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
152 /* get subwindows from root */
153 win = find_subwindow(root, -1, -1);
157 win = find_subwindow(win, workarea[2], workarea[3]);
166 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
169 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
180 static int colour_set = -1;
181 /* if no argb visual is configured sets background to ParentRelative for the Window and all parents,
182 else real transparency is used */
183 void set_transparent_background(Window win, int alpha)
185 (void)alpha; /* disable warnings when unused */
188 if (have_argb_visual) {
190 if (set_transparent) {
191 XSetWindowBackground(display, win, 0x00);
192 } else if (colour_set != background_colour) {
193 XSetWindowBackground(display, win,
194 background_colour | (alpha << 24));
195 colour_set = background_colour;
198 #endif /* USE_ARGB */
199 // pseudo transparency
201 if (set_transparent) {
205 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
209 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
211 XQueryTree(display, parent, &r, &parent, &children, &n);
214 } else if (colour_set != background_colour) {
215 XSetWindowBackground(display, win, background_colour);
216 colour_set = background_colour;
220 #endif /* USE_ARGB */
224 static int get_argb_visual(Visual** visual, int *depth) {
225 /* code from gtk project, gdk_screen_get_rgba_visual */
226 XVisualInfo visual_template;
227 XVisualInfo *visual_list;
228 int nxvisuals = 0, i;
230 visual_template.screen = screen;
231 visual_list = XGetVisualInfo (display, VisualScreenMask,
232 &visual_template, &nxvisuals);
233 for (i = 0; i < nxvisuals; i++) {
234 if (visual_list[i].depth == 32 &&
235 (visual_list[i].red_mask == 0xff0000 &&
236 visual_list[i].green_mask == 0x00ff00 &&
237 visual_list[i].blue_mask == 0x0000ff)) {
238 *visual = visual_list[i].visual;
239 *depth = visual_list[i].depth;
240 DBGP("Found ARGB Visual");
245 // no argb visual available
246 DBGP("No ARGB Visual found");
250 #endif /* USE_ARGB */
252 void destroy_window(void)
256 XftDrawDestroy(window.xftdraw);
260 XFreeGC(display, window.gc);
262 memset(&window, 0, sizeof(struct conky_window));
266 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
267 char **argv, int argc)
269 /* There seems to be some problems with setting transparent background
270 * (on fluxbox this time). It doesn't happen always and I don't know why it
271 * happens but I bet the bug is somewhere here. */
272 set_transparent = set_trans;
273 background_colour = back_colour;
278 int depth = 0, flags;
279 Visual *visual = NULL;
281 if (!find_desktop_window(&window.root, &window.desktop)) {
286 if (use_argb_visual && get_argb_visual(&visual, &depth)) {
287 have_argb_visual = 1;
288 window.visual = visual;
289 window.colourmap = XCreateColormap(display,
290 DefaultRootWindow(display), window.visual, AllocNone);
292 #endif /* USE_ARGB */
293 window.visual = DefaultVisual(display, screen);
294 window.colourmap = DefaultColormap(display, screen);
295 depth = CopyFromParent;
296 visual = CopyFromParent;
299 #endif /* USE_ARGB */
301 if (window.type == TYPE_OVERRIDE) {
303 /* An override_redirect True window.
304 * No WM hints or button processing needed. */
305 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
306 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
309 if (have_argb_visual) {
310 attrs.colormap = window.colourmap;
311 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
313 #endif /* USE_ARGB */
314 flags = CWBackPixel | CWOverrideRedirect;
317 #endif /* USE_ARGB */
319 /* Parent is desktop window (which might be a child of root) */
320 window.window = XCreateWindow(display, window.desktop, window.x,
321 window.y, w, h, 0, depth, InputOutput, visual,
324 XLowerWindow(display, window.window);
326 fprintf(stderr, PACKAGE_NAME": window type - override\n");
328 } else { /* window.type != TYPE_OVERRIDE */
330 /* A window managed by the window manager.
331 * Process hints and buttons. */
332 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
333 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
334 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
336 XClassHint classHint;
341 if (have_argb_visual) {
342 attrs.colormap = window.colourmap;
343 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
345 #endif /* USE_ARGB */
346 flags = CWBackPixel | CWOverrideRedirect;
349 #endif /* USE_ARGB */
351 if (window.type == TYPE_DOCK) {
352 window.x = window.y = 0;
354 /* Parent is root window so WM can take control */
355 window.window = XCreateWindow(display, window.root, window.x,
356 window.y, w, h, 0, depth, InputOutput, visual,
359 classHint.res_name = window.class_name;
360 classHint.res_class = classHint.res_name;
362 wmHint.flags = InputHint | StateHint;
363 /* allow decorated windows to be given input focus by WM */
365 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
366 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
367 wmHint.initial_state = WithdrawnState;
369 wmHint.initial_state = NormalState;
372 XmbSetWMProperties(display, window.window, NULL, NULL, argv,
373 argc, NULL, &wmHint, &classHint);
374 XStoreName(display, window.window, window.title);
376 /* Sets an empty WM_PROTOCOLS property */
377 XSetWMProtocols(display, window.window, NULL, 0);
379 /* Set window type */
380 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
383 switch (window.type) {
385 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
386 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
390 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
391 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
395 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
396 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
401 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
402 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
406 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
407 PropModeReplace, (unsigned char *) &prop, 1);
410 /* Set desired hints */
412 /* Window decorations */
413 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
414 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
417 xa = ATOM(_MOTIF_WM_HINTS);
419 long prop[5] = { 2, 0, 0, 0, 0 };
420 XChangeProperty(display, window.window, xa, xa, 32,
421 PropModeReplace, (unsigned char *) prop, 5);
425 /* Below other windows */
426 if (TEST_HINT(window.hints, HINT_BELOW)) {
427 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
430 xa = ATOM(_WIN_LAYER);
434 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
435 PropModeAppend, (unsigned char *) &prop, 1);
438 xa = ATOM(_NET_WM_STATE);
440 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
442 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
443 PropModeAppend, (unsigned char *) &xa_prop, 1);
447 /* Above other windows */
448 if (TEST_HINT(window.hints, HINT_ABOVE)) {
449 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
452 xa = ATOM(_WIN_LAYER);
456 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
457 PropModeAppend, (unsigned char *) &prop, 1);
460 xa = ATOM(_NET_WM_STATE);
462 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
464 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
465 PropModeAppend, (unsigned char *) &xa_prop, 1);
470 if (TEST_HINT(window.hints, HINT_STICKY)) {
471 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
474 xa = ATOM(_NET_WM_DESKTOP);
476 CARD32 xa_prop = 0xFFFFFFFF;
478 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
479 PropModeAppend, (unsigned char *) &xa_prop, 1);
482 xa = ATOM(_NET_WM_STATE);
484 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
486 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
487 PropModeAppend, (unsigned char *) &xa_prop, 1);
492 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
493 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
496 xa = ATOM(_NET_WM_STATE);
498 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
500 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
501 PropModeAppend, (unsigned char *) &xa_prop, 1);
506 if (TEST_HINT(window.hints, HINT_FULLSCREEN)) {
507 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
510 xa = ATOM(_NET_WM_STATE);
512 Atom xa_prop = ATOM(_NET_WM_STATE_FULLSCREEN);
514 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
515 PropModeAppend, (unsigned char *) &xa_prop, 1);
520 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
524 XMapWindow(display, window.window);
527 #endif /* OWN_WINDOW */
529 XWindowAttributes attrs;
531 if (!window.window) {
532 window.window = find_desktop_window(&window.root, &window.desktop);
534 window.visual = DefaultVisual(display, screen);
535 window.colourmap = DefaultColormap(display, screen);
537 if (XGetWindowAttributes(display, window.window, &attrs)) {
538 window.width = attrs.width;
539 window.height = attrs.height;
542 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
545 /* Drawable is same as window. This may be changed by double buffering. */
546 window.drawable = window.window;
552 if (!XdbeQueryExtension(display, &major, &minor)) {
555 window.back_buffer = XdbeAllocateBackBufferName(display,
556 window.window, XdbeBackground);
557 if (window.back_buffer != None) {
558 window.drawable = window.back_buffer;
559 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
565 NORM_ERR("failed to set up double buffer");
569 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
574 cimlib_init(display, window.drawable, window.visual, window.colourmap);
579 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
581 | (own_window ? (StructureNotifyMask |
582 ButtonPressMask | ButtonReleaseMask) : 0)
587 static Window find_subwindow(Window win, int w, int h)
590 Window troot, parent, *children;
593 /* search subwindows with same size as display or work area */
595 for (i = 0; i < 10; i++) {
596 XQueryTree(display, win, &troot, &parent, &children, &n);
598 for (j = 0; j < n; j++) {
599 XWindowAttributes attrs;
601 if (XGetWindowAttributes(display, children[j], &attrs)) {
602 /* Window must be mapped and same size as display or
604 if (attrs.map_state != 0 && ((attrs.width == display_width
605 && attrs.height == display_height)
606 || (attrs.width == w && attrs.height == h))) {
626 values.graphics_exposures = 0;
627 values.function = GXcopy;
628 window.gc = XCreateGC(display, window.drawable,
629 GCFunction | GCGraphicsExposures, &values);
632 //Get current desktop number
633 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
637 unsigned long nitems;
638 unsigned long bytes_after;
639 unsigned char *prop = NULL;
640 struct information *current_info = &info;
642 if (atom == None) return;
644 if ( (XGetWindowProperty( current_display, root, atom,
645 0, 1L, False, XA_CARDINAL,
646 &actual_type, &actual_format, &nitems,
647 &bytes_after, &prop ) == Success ) &&
648 (actual_type == XA_CARDINAL) &&
649 (nitems == 1L) && (actual_format == 32) ) {
650 current_info->x11.desktop.current = prop[0]+1;
657 //Get total number of available desktops
658 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
662 unsigned long nitems;
663 unsigned long bytes_after;
664 unsigned char *prop = NULL;
665 struct information *current_info = &info;
667 if (atom == None) return;
669 if ( (XGetWindowProperty( current_display, root, atom,
670 0, 1L, False, XA_CARDINAL,
671 &actual_type, &actual_format, &nitems,
672 &bytes_after, &prop ) == Success ) &&
673 (actual_type == XA_CARDINAL) &&
674 (nitems == 1L) && (actual_format == 32) ) {
675 current_info->x11.desktop.number = prop[0];
682 //Get all desktop names
683 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
687 unsigned long nitems;
688 unsigned long bytes_after;
689 unsigned char *prop = NULL;
690 struct information *current_info = &info;
692 if (atom == None) return;
694 if ( (XGetWindowProperty( current_display, root, atom,
695 0, (~0L), False, ATOM(UTF8_STRING),
696 &actual_type, &actual_format, &nitems,
697 &bytes_after, &prop ) == Success ) &&
698 (actual_type == ATOM(UTF8_STRING)) &&
699 (nitems > 0L) && (actual_format == 8) ) {
701 if(current_info->x11.desktop.all_names) {
702 free(current_info->x11.desktop.all_names);
703 current_info->x11.desktop.all_names = NULL;
705 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
706 memcpy(current_info->x11.desktop.all_names, prop, nitems);
707 current_info->x11.desktop.nitems = nitems;
714 //Get current desktop name
715 static inline void get_x11_desktop_current_name(char *names)
717 struct information *current_info = &info;
718 unsigned int i = 0, j = 0;
721 while ( i < current_info->x11.desktop.nitems ) {
722 if ( names[i++] == '\0' ) {
723 if ( ++k == current_info->x11.desktop.current ) {
724 if (current_info->x11.desktop.name) {
725 free(current_info->x11.desktop.name);
726 current_info->x11.desktop.name = NULL;
728 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
729 //desktop names can be empty but should always be not null
730 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
738 void get_x11_desktop_info(Display *current_display, Atom atom)
741 static Atom atom_current, atom_number, atom_names;
742 struct information *current_info = &info;
743 XWindowAttributes window_attributes;
745 root = RootWindow(current_display, current_info->x11.monitor.current);
747 /* Check if we initialise else retrieve changed property */
749 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
750 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
751 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
752 get_x11_desktop_current(current_display, root, atom_current);
753 get_x11_desktop_number(current_display, root, atom_number);
754 get_x11_desktop_names(current_display, root, atom_names);
755 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
757 /* Set the PropertyChangeMask on the root window, if not set */
758 XGetWindowAttributes(display, root, &window_attributes);
759 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
760 XSetWindowAttributes attributes;
761 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
762 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
763 XGetWindowAttributes(display, root, &window_attributes);
766 if (atom == atom_current) {
767 get_x11_desktop_current(current_display, root, atom_current);
768 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
769 } else if (atom == atom_number) {
770 get_x11_desktop_number(current_display, root, atom_number);
771 } else if (atom == atom_names) {
772 get_x11_desktop_names(current_display, root, atom_names);
773 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
778 int update_x11info(void)
780 struct information *current_info = &info;
781 if (x_initialised != YES)
783 current_info->x11.monitor.number = XScreenCount(display);
784 current_info->x11.monitor.current = XDefaultScreen(display);
789 /* reserve window manager space */
790 void set_struts(int sidenum)
793 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
794 /* reserve space at left, right, top, bottom */
795 signed long sizes[12] = {0};
798 /* define strut depth */
802 sizes[0] = window.x + window.width;
806 sizes[1] = display_width - window.x;
810 sizes[2] = window.y + window.height;
814 sizes[3] = display_height - window.y;
818 /* define partial strut length */
820 sizes[4 + (sidenum*2)] = window.y;
821 sizes[5 + (sidenum*2)] = window.y + window.height;
822 } else if (sidenum <= 3) {
823 sizes[4 + (sidenum*2)] = window.x;
824 sizes[5 + (sidenum*2)] = window.x + window.width;
827 /* check constraints */
828 for (i = 0; i < 12; i++) {
832 if (i <= 1 || i >= 8) {
833 if (sizes[i] > display_width) {
834 sizes[i] = display_width;
837 if (sizes[i] > display_height) {
838 sizes[i] = display_height;
844 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
845 PropModeReplace, (unsigned char *) &sizes, 4);
847 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
848 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
849 PropModeReplace, (unsigned char *) &sizes, 12);
853 #endif /* OWN_WINDOW */
856 void xdbe_swap_buffers(void)
861 swap.swap_window = window.window;
862 swap.swap_action = XdbeBackground;
863 XdbeSwapBuffers(display, &swap, 1);
866 #endif /* HAVE_XDBE */