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;
68 /* local prototypes */
69 static void update_workarea(void);
70 static Window find_desktop_window(Window *p_root, Window *p_desktop);
71 static Window find_subwindow(Window win, int w, int h);
74 void init_X11(const char *disp)
77 if ((display = XOpenDisplay(disp)) == NULL) {
78 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
82 screen = DefaultScreen(display);
83 display_width = DisplayWidth(display, screen);
84 display_height = DisplayHeight(display, screen);
86 get_x11_desktop_info(display, 0);
91 static void update_workarea(void)
93 /* default work area is display */
96 workarea[2] = display_width;
97 workarea[3] = display_height;
100 /* Find root window and desktop window.
101 * Return desktop window on success,
102 * and set root and desktop byref return values.
103 * Return 0 on failure. */
104 static Window find_desktop_window(Window *p_root, Window *p_desktop)
108 unsigned long nitems, bytes;
110 Window root = RootWindow(display, screen);
112 Window troot, parent, *children;
113 unsigned char *buf = NULL;
115 if (!p_root || !p_desktop) {
119 /* some window managers set __SWM_VROOT to some child of root window */
121 XQueryTree(display, root, &troot, &parent, &children, &n);
122 for (i = 0; i < (int) n; i++) {
123 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
124 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
125 == Success && type == XA_WINDOW) {
126 win = *(Window *) buf;
130 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
145 /* get subwindows from root */
146 win = find_subwindow(root, -1, -1);
150 win = find_subwindow(win, workarea[2], workarea[3]);
159 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
162 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
173 /* sets background to ParentRelative for the Window and all parents */
174 void set_transparent_background(Window win)
176 static int colour_set = -1;
178 if (set_transparent) {
182 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
186 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
188 XQueryTree(display, parent, &r, &parent, &children, &n);
191 } else if (colour_set != background_colour) {
192 XSetWindowBackground(display, win, background_colour);
193 colour_set = background_colour;
197 void destroy_window(void)
201 XftDrawDestroy(window.xftdraw);
205 XFreeGC(display, window.gc);
207 memset(&window, 0, sizeof(struct conky_window));
210 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
211 char **argv, int argc)
213 /* There seems to be some problems with setting transparent background
214 * (on fluxbox this time). It doesn't happen always and I don't know why it
215 * happens but I bet the bug is somewhere here. */
216 set_transparent = set_trans;
217 background_colour = back_colour;
221 if (!find_desktop_window(&window.root, &window.desktop)) {
225 if (window.type == TYPE_OVERRIDE) {
227 /* An override_redirect True window.
228 * No WM hints or button processing needed. */
229 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
230 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
233 /* Parent is desktop window (which might be a child of root) */
234 window.window = XCreateWindow(display, window.desktop, window.x,
235 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
236 CWBackPixel | CWOverrideRedirect, &attrs);
238 XLowerWindow(display, window.window);
240 fprintf(stderr, PACKAGE_NAME": window type - override\n");
242 } else { /* window.type != TYPE_OVERRIDE */
244 /* A window managed by the window manager.
245 * Process hints and buttons. */
246 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
247 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
248 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
250 XClassHint classHint;
254 if (window.type == TYPE_DOCK) {
255 window.x = window.y = 0;
257 /* Parent is root window so WM can take control */
258 window.window = XCreateWindow(display, window.root, window.x,
259 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
260 CWBackPixel | CWOverrideRedirect, &attrs);
262 classHint.res_name = window.class_name;
263 classHint.res_class = classHint.res_name;
265 wmHint.flags = InputHint | StateHint;
266 /* allow decorated windows to be given input focus by WM */
268 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
269 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
270 wmHint.initial_state = WithdrawnState;
272 wmHint.initial_state = NormalState;
275 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
276 argc, NULL, &wmHint, &classHint);
278 /* Sets an empty WM_PROTOCOLS property */
279 XSetWMProtocols(display, window.window, NULL, 0);
281 /* Set window type */
282 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
285 switch (window.type) {
287 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
288 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
292 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
293 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
297 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
298 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
303 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
304 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
308 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
309 PropModeReplace, (unsigned char *) &prop, 1);
312 /* Set desired hints */
314 /* Window decorations */
315 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
316 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
319 xa = ATOM(_MOTIF_WM_HINTS);
321 long prop[5] = { 2, 0, 0, 0, 0 };
322 XChangeProperty(display, window.window, xa, xa, 32,
323 PropModeReplace, (unsigned char *) prop, 5);
327 /* Below other windows */
328 if (TEST_HINT(window.hints, HINT_BELOW)) {
329 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
332 xa = ATOM(_WIN_LAYER);
336 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
337 PropModeAppend, (unsigned char *) &prop, 1);
340 xa = ATOM(_NET_WM_STATE);
342 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
344 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
345 PropModeAppend, (unsigned char *) &xa_prop, 1);
349 /* Above other windows */
350 if (TEST_HINT(window.hints, HINT_ABOVE)) {
351 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
354 xa = ATOM(_WIN_LAYER);
358 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
359 PropModeAppend, (unsigned char *) &prop, 1);
362 xa = ATOM(_NET_WM_STATE);
364 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
366 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
367 PropModeAppend, (unsigned char *) &xa_prop, 1);
372 if (TEST_HINT(window.hints, HINT_STICKY)) {
373 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
376 xa = ATOM(_NET_WM_DESKTOP);
378 CARD32 xa_prop = 0xFFFFFFFF;
380 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
381 PropModeAppend, (unsigned char *) &xa_prop, 1);
384 xa = ATOM(_NET_WM_STATE);
386 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
388 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
389 PropModeAppend, (unsigned char *) &xa_prop, 1);
394 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
395 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
398 xa = ATOM(_NET_WM_STATE);
400 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
402 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
403 PropModeAppend, (unsigned char *) &xa_prop, 1);
408 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
409 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
412 xa = ATOM(_NET_WM_STATE);
414 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
416 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
417 PropModeAppend, (unsigned char *) &xa_prop, 1);
422 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
426 XMapWindow(display, window.window);
429 #endif /* OWN_WINDOW */
431 XWindowAttributes attrs;
433 if (!window.window) {
434 window.window = find_desktop_window(&window.root, &window.desktop);
437 if (XGetWindowAttributes(display, window.window, &attrs)) {
438 window.width = attrs.width;
439 window.height = attrs.height;
442 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
445 /* Drawable is same as window. This may be changed by double buffering. */
446 window.drawable = window.window;
452 if (!XdbeQueryExtension(display, &major, &minor)) {
455 window.back_buffer = XdbeAllocateBackBufferName(display,
456 window.window, XdbeBackground);
457 if (window.back_buffer != None) {
458 window.drawable = window.back_buffer;
459 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
465 NORM_ERR("failed to set up double buffer");
469 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
472 window.visual = DefaultVisual(display, DefaultScreen(display));
473 window.colourmap = DefaultColormap(display, DefaultScreen(display));
476 cimlib_init(display, window.drawable, window.visual, window.colourmap);
481 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
483 | (own_window ? (StructureNotifyMask |
484 ButtonPressMask | ButtonReleaseMask) : 0)
489 static Window find_subwindow(Window win, int w, int h)
492 Window troot, parent, *children;
495 /* search subwindows with same size as display or work area */
497 for (i = 0; i < 10; i++) {
498 XQueryTree(display, win, &troot, &parent, &children, &n);
500 for (j = 0; j < n; j++) {
501 XWindowAttributes attrs;
503 if (XGetWindowAttributes(display, children[j], &attrs)) {
504 /* Window must be mapped and same size as display or
506 if (attrs.map_state != 0 && ((attrs.width == display_width
507 && attrs.height == display_height)
508 || (attrs.width == w && attrs.height == h))) {
524 long get_x11_color(const char *name)
529 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
530 /* lets check if it's a hex colour with the # missing in front
531 * if yes, then do something about it */
532 char newname[DEFAULT_TEXT_BUFFER_SIZE];
535 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
536 /* now lets try again */
537 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
539 NORM_ERR("can't parse X color '%s'", name);
543 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
544 NORM_ERR("can't allocate X color '%s'", name);
547 return (long) color.pixel;
554 values.graphics_exposures = 0;
555 values.function = GXcopy;
556 window.gc = XCreateGC(display, window.drawable,
557 GCFunction | GCGraphicsExposures, &values);
560 //Get current desktop number
561 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
565 unsigned long nitems;
566 unsigned long bytes_after;
567 unsigned char *prop = NULL;
568 struct information *current_info = &info;
570 if (atom == None) return;
572 if ( (XGetWindowProperty( current_display, root, atom,
573 0, 1L, False, XA_CARDINAL,
574 &actual_type, &actual_format, &nitems,
575 &bytes_after, &prop ) == Success ) &&
576 (actual_type == XA_CARDINAL) &&
577 (nitems == 1L) && (actual_format == 32) ) {
578 current_info->x11.desktop.current = prop[0]+1;
585 //Get total number of available desktops
586 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
590 unsigned long nitems;
591 unsigned long bytes_after;
592 unsigned char *prop = NULL;
593 struct information *current_info = &info;
595 if (atom == None) return;
597 if ( (XGetWindowProperty( current_display, root, atom,
598 0, 1L, False, XA_CARDINAL,
599 &actual_type, &actual_format, &nitems,
600 &bytes_after, &prop ) == Success ) &&
601 (actual_type == XA_CARDINAL) &&
602 (nitems == 1L) && (actual_format == 32) ) {
603 current_info->x11.desktop.number = prop[0];
610 //Get all desktop names
611 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
615 unsigned long nitems;
616 unsigned long bytes_after;
617 unsigned char *prop = NULL;
618 struct information *current_info = &info;
620 if (atom == None) return;
622 if ( (XGetWindowProperty( current_display, root, atom,
623 0, (~0L), False, ATOM(UTF8_STRING),
624 &actual_type, &actual_format, &nitems,
625 &bytes_after, &prop ) == Success ) &&
626 (actual_type == ATOM(UTF8_STRING)) &&
627 (nitems > 0L) && (actual_format == 8) ) {
629 if(current_info->x11.desktop.all_names) {
630 free(current_info->x11.desktop.all_names);
631 current_info->x11.desktop.all_names = NULL;
633 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
634 memcpy(current_info->x11.desktop.all_names, prop, nitems);
635 current_info->x11.desktop.nitems = nitems;
642 //Get current desktop name
643 static inline void get_x11_desktop_current_name(char *names)
645 struct information *current_info = &info;
646 unsigned int i = 0, j = 0;
649 while ( i < current_info->x11.desktop.nitems ) {
650 if ( names[i++] == '\0' ) {
651 if ( ++k == current_info->x11.desktop.current ) {
652 if (current_info->x11.desktop.name) {
653 free(current_info->x11.desktop.name);
654 current_info->x11.desktop.name = NULL;
656 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
657 //desktop names can be empty but should always be not null
658 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
666 void get_x11_desktop_info(Display *current_display, Atom atom)
669 static Atom atom_current, atom_number, atom_names;
670 struct information *current_info = &info;
671 XWindowAttributes window_attributes;
673 root = RootWindow(current_display, current_info->x11.monitor.current);
675 /* Check if we initialise else retrieve changed property */
677 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
678 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
679 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
680 get_x11_desktop_current(current_display, root, atom_current);
681 get_x11_desktop_number(current_display, root, atom_number);
682 get_x11_desktop_names(current_display, root, atom_names);
683 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
685 /* Set the PropertyChangeMask on the root window, if not set */
686 XGetWindowAttributes(display, root, &window_attributes);
687 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
688 XSetWindowAttributes attributes;
689 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
690 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
691 XGetWindowAttributes(display, root, &window_attributes);
694 if (atom == atom_current) {
695 get_x11_desktop_current(current_display, root, atom_current);
696 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
697 } else if (atom == atom_number) {
698 get_x11_desktop_number(current_display, root, atom_number);
699 } else if (atom == atom_names) {
700 get_x11_desktop_names(current_display, root, atom_names);
701 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
706 void update_x11info(void)
708 struct information *current_info = &info;
709 if (!x_initialised == YES)
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 */
729 sizes[0] = window.x + window.width;
733 sizes[1] = display_width - window.x;
737 sizes[2] = window.y + window.height;
741 sizes[3] = display_height - window.y;
745 /* define partial strut length */
747 sizes[4 + (sidenum*2)] = window.y;
748 sizes[5 + (sidenum*2)] = window.y + window.height;
749 } else if (sidenum <= 3) {
750 sizes[4 + (sidenum*2)] = window.x;
751 sizes[5 + (sidenum*2)] = window.x + window.width;
754 /* check constraints */
755 for (i = 0; i < 12; i++) {
759 if (i <= 1 || i >= 8) {
760 if (sizes[i] > display_width) {
761 sizes[i] = display_width;
764 if (sizes[i] > display_height) {
765 sizes[i] = display_height;
771 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
772 PropModeReplace, (unsigned char *) &sizes, 4);
774 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
775 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
776 PropModeReplace, (unsigned char *) &sizes, 12);
780 #endif /* OWN_WINDOW */
783 void xdbe_swap_buffers(void)
788 swap.swap_window = window.window;
789 swap.swap_action = XdbeBackground;
790 XdbeSwapBuffers(display, &swap, 1);
793 #endif /* HAVE_XDBE */