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))) {
531 values.graphics_exposures = 0;
532 values.function = GXcopy;
533 window.gc = XCreateGC(display, window.drawable,
534 GCFunction | GCGraphicsExposures, &values);
537 //Get current desktop number
538 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
542 unsigned long nitems;
543 unsigned long bytes_after;
544 unsigned char *prop = NULL;
545 struct information *current_info = &info;
547 if (atom == None) return;
549 if ( (XGetWindowProperty( current_display, root, atom,
550 0, 1L, False, XA_CARDINAL,
551 &actual_type, &actual_format, &nitems,
552 &bytes_after, &prop ) == Success ) &&
553 (actual_type == XA_CARDINAL) &&
554 (nitems == 1L) && (actual_format == 32) ) {
555 current_info->x11.desktop.current = prop[0]+1;
562 //Get total number of available desktops
563 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
567 unsigned long nitems;
568 unsigned long bytes_after;
569 unsigned char *prop = NULL;
570 struct information *current_info = &info;
572 if (atom == None) return;
574 if ( (XGetWindowProperty( current_display, root, atom,
575 0, 1L, False, XA_CARDINAL,
576 &actual_type, &actual_format, &nitems,
577 &bytes_after, &prop ) == Success ) &&
578 (actual_type == XA_CARDINAL) &&
579 (nitems == 1L) && (actual_format == 32) ) {
580 current_info->x11.desktop.number = prop[0];
587 //Get all desktop names
588 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
592 unsigned long nitems;
593 unsigned long bytes_after;
594 unsigned char *prop = NULL;
595 struct information *current_info = &info;
597 if (atom == None) return;
599 if ( (XGetWindowProperty( current_display, root, atom,
600 0, (~0L), False, ATOM(UTF8_STRING),
601 &actual_type, &actual_format, &nitems,
602 &bytes_after, &prop ) == Success ) &&
603 (actual_type == ATOM(UTF8_STRING)) &&
604 (nitems > 0L) && (actual_format == 8) ) {
606 if(current_info->x11.desktop.all_names) {
607 free(current_info->x11.desktop.all_names);
608 current_info->x11.desktop.all_names = NULL;
610 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
611 memcpy(current_info->x11.desktop.all_names, prop, nitems);
612 current_info->x11.desktop.nitems = nitems;
619 //Get current desktop name
620 static inline void get_x11_desktop_current_name(char *names)
622 struct information *current_info = &info;
623 unsigned int i = 0, j = 0;
626 while ( i < current_info->x11.desktop.nitems ) {
627 if ( names[i++] == '\0' ) {
628 if ( ++k == current_info->x11.desktop.current ) {
629 if (current_info->x11.desktop.name) {
630 free(current_info->x11.desktop.name);
631 current_info->x11.desktop.name = NULL;
633 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
634 //desktop names can be empty but should always be not null
635 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
643 void get_x11_desktop_info(Display *current_display, Atom atom)
646 static Atom atom_current, atom_number, atom_names;
647 struct information *current_info = &info;
648 XWindowAttributes window_attributes;
650 root = RootWindow(current_display, current_info->x11.monitor.current);
652 /* Check if we initialise else retrieve changed property */
654 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
655 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
656 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
657 get_x11_desktop_current(current_display, root, atom_current);
658 get_x11_desktop_number(current_display, root, atom_number);
659 get_x11_desktop_names(current_display, root, atom_names);
660 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
662 /* Set the PropertyChangeMask on the root window, if not set */
663 XGetWindowAttributes(display, root, &window_attributes);
664 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
665 XSetWindowAttributes attributes;
666 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
667 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
668 XGetWindowAttributes(display, root, &window_attributes);
671 if (atom == atom_current) {
672 get_x11_desktop_current(current_display, root, atom_current);
673 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
674 } else if (atom == atom_number) {
675 get_x11_desktop_number(current_display, root, atom_number);
676 } else if (atom == atom_names) {
677 get_x11_desktop_names(current_display, root, atom_names);
678 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
683 void update_x11info(void)
685 struct information *current_info = &info;
686 if (!x_initialised == YES)
688 current_info->x11.monitor.number = XScreenCount(display);
689 current_info->x11.monitor.current = XDefaultScreen(display);
693 /* reserve window manager space */
694 void set_struts(int sidenum)
697 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
698 /* reserve space at left, right, top, bottom */
699 signed long sizes[12] = {0};
702 /* define strut depth */
706 sizes[0] = window.x + window.width;
710 sizes[1] = display_width - window.x;
714 sizes[2] = window.y + window.height;
718 sizes[3] = display_height - window.y;
722 /* define partial strut length */
724 sizes[4 + (sidenum*2)] = window.y;
725 sizes[5 + (sidenum*2)] = window.y + window.height;
726 } else if (sidenum <= 3) {
727 sizes[4 + (sidenum*2)] = window.x;
728 sizes[5 + (sidenum*2)] = window.x + window.width;
731 /* check constraints */
732 for (i = 0; i < 12; i++) {
736 if (i <= 1 || i >= 8) {
737 if (sizes[i] > display_width) {
738 sizes[i] = display_width;
741 if (sizes[i] > display_height) {
742 sizes[i] = display_height;
748 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
749 PropModeReplace, (unsigned char *) &sizes, 4);
751 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
752 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
753 PropModeReplace, (unsigned char *) &sizes, 12);
757 #endif /* OWN_WINDOW */
760 void xdbe_swap_buffers(void)
765 swap.swap_window = window.window;
766 swap.swap_action = XdbeBackground;
767 XdbeSwapBuffers(display, &swap, 1);
770 #endif /* HAVE_XDBE */