1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * vim: ts=4 sw=4 noet ai cindent syntax=c
37 #include <X11/Xatom.h>
39 #include <X11/Xutil.h>
45 #include <X11/Xft/Xft.h>
53 /* some basic X11 stuff */
54 Display *display = NULL;
58 static int set_transparent;
59 static int background_colour;
61 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
65 struct conky_window window;
67 /* local prototypes */
68 static void update_workarea(void);
69 static Window find_desktop_window(Window *p_root, Window *p_desktop);
70 static Window find_subwindow(Window win, int w, int h);
73 void init_X11(const char *disp)
76 if ((display = XOpenDisplay(disp)) == NULL) {
77 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(0));
81 screen = DefaultScreen(display);
82 display_width = DisplayWidth(display, screen);
83 display_height = DisplayHeight(display, screen);
85 get_x11_desktop_info(display, 0);
90 static void update_workarea(void)
92 /* default work area is display */
95 workarea[2] = display_width;
96 workarea[3] = display_height;
99 /* Find root window and desktop window.
100 * Return desktop window on success,
101 * and set root and desktop byref return values.
102 * Return 0 on failure. */
103 static Window find_desktop_window(Window *p_root, Window *p_desktop)
107 unsigned long nitems, bytes;
109 Window root = RootWindow(display, screen);
111 Window troot, parent, *children;
112 unsigned char *buf = NULL;
114 if (!p_root || !p_desktop) {
118 /* some window managers set __SWM_VROOT to some child of root window */
120 XQueryTree(display, root, &troot, &parent, &children, &n);
121 for (i = 0; i < (int) n; i++) {
122 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
123 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
124 == Success && type == XA_WINDOW) {
125 win = *(Window *) buf;
129 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
144 /* get subwindows from root */
145 win = find_subwindow(root, -1, -1);
149 win = find_subwindow(win, workarea[2], workarea[3]);
158 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
161 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
172 /* sets background to ParentRelative for the Window and all parents */
173 void set_transparent_background(Window win)
175 static int colour_set = -1;
177 if (set_transparent) {
181 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
185 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
187 XQueryTree(display, parent, &r, &parent, &children, &n);
190 } else if (colour_set != background_colour) {
191 XSetWindowBackground(display, win, background_colour);
192 colour_set = background_colour;
194 // XClearWindow(display, win); not sure why this was here
197 void destroy_window(void)
200 XftDrawDestroy(window.xftdraw);
203 XFreeGC(display, window.gc);
205 memset(&window, 0, sizeof(struct conky_window));
208 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
209 char **argv, int argc)
211 /* There seems to be some problems with setting transparent background
212 * (on fluxbox this time). It doesn't happen always and I don't know why it
213 * happens but I bet the bug is somewhere here. */
214 set_transparent = set_trans;
215 background_colour = back_colour;
219 if (!find_desktop_window(&window.root, &window.desktop)) {
223 if (window.type == TYPE_OVERRIDE) {
225 /* An override_redirect True window.
226 * No WM hints or button processing needed. */
227 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
228 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
231 /* Parent is desktop window (which might be a child of root) */
232 window.window = XCreateWindow(display, window.desktop, window.x,
233 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
234 CWBackPixel | CWOverrideRedirect, &attrs);
236 XLowerWindow(display, window.window);
238 fprintf(stderr, PACKAGE_NAME": window type - override\n");
240 } else { /* window.type != TYPE_OVERRIDE */
242 /* A window managed by the window manager.
243 * Process hints and buttons. */
244 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
245 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
246 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
248 XClassHint classHint;
252 if (window.type == TYPE_DOCK) {
253 window.x = window.y = 0;
255 /* Parent is root window so WM can take control */
256 window.window = XCreateWindow(display, window.root, window.x,
257 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
258 CWBackPixel | CWOverrideRedirect, &attrs);
260 classHint.res_name = window.class_name;
261 classHint.res_class = classHint.res_name;
263 wmHint.flags = InputHint | StateHint;
264 /* allow decorated windows to be given input focus by WM */
266 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
267 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
268 wmHint.initial_state = WithdrawnState;
270 wmHint.initial_state = NormalState;
273 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
274 argc, NULL, &wmHint, &classHint);
276 /* Sets an empty WM_PROTOCOLS property */
277 XSetWMProtocols(display, window.window, NULL, 0);
279 /* Set window type */
280 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
283 switch (window.type) {
285 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
286 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
290 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
291 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
295 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
296 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
301 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
302 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
306 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
307 PropModeReplace, (unsigned char *) &prop, 1);
310 /* Set desired hints */
312 /* Window decorations */
313 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
314 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
317 xa = ATOM(_MOTIF_WM_HINTS);
319 long prop[5] = { 2, 0, 0, 0, 0 };
320 XChangeProperty(display, window.window, xa, xa, 32,
321 PropModeReplace, (unsigned char *) prop, 5);
325 /* Below other windows */
326 if (TEST_HINT(window.hints, HINT_BELOW)) {
327 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
330 xa = ATOM(_WIN_LAYER);
334 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
335 PropModeAppend, (unsigned char *) &prop, 1);
338 xa = ATOM(_NET_WM_STATE);
340 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
342 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
343 PropModeAppend, (unsigned char *) &xa_prop, 1);
347 /* Above other windows */
348 if (TEST_HINT(window.hints, HINT_ABOVE)) {
349 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
352 xa = ATOM(_WIN_LAYER);
356 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
357 PropModeAppend, (unsigned char *) &prop, 1);
360 xa = ATOM(_NET_WM_STATE);
362 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
364 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
365 PropModeAppend, (unsigned char *) &xa_prop, 1);
370 if (TEST_HINT(window.hints, HINT_STICKY)) {
371 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
374 xa = ATOM(_NET_WM_DESKTOP);
376 CARD32 xa_prop = 0xFFFFFFFF;
378 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
379 PropModeAppend, (unsigned char *) &xa_prop, 1);
382 xa = ATOM(_NET_WM_STATE);
384 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
386 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
387 PropModeAppend, (unsigned char *) &xa_prop, 1);
392 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
393 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
396 xa = ATOM(_NET_WM_STATE);
398 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
400 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
401 PropModeAppend, (unsigned char *) &xa_prop, 1);
406 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
407 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
410 xa = ATOM(_NET_WM_STATE);
412 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
414 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
415 PropModeAppend, (unsigned char *) &xa_prop, 1);
420 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
424 XMapWindow(display, window.window);
427 #endif /* OWN_WINDOW */
429 XWindowAttributes attrs;
431 if (!window.window) {
432 window.window = find_desktop_window(&window.root, &window.desktop);
435 if (XGetWindowAttributes(display, window.window, &attrs)) {
436 window.width = attrs.width;
437 window.height = attrs.height;
440 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
443 /* Drawable is same as window. This may be changed by double buffering. */
444 window.drawable = window.window;
450 if (!XdbeQueryExtension(display, &major, &minor)) {
453 window.back_buffer = XdbeAllocateBackBufferName(display,
454 window.window, XdbeBackground);
455 if (window.back_buffer != None) {
456 window.drawable = window.back_buffer;
457 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
463 ERR("failed to set up double buffer");
467 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
470 window.visual = DefaultVisual(display, DefaultScreen(display));
471 window.colourmap = DefaultColormap(display, DefaultScreen(display));
474 cimlib_init(display, window.drawable, window.visual, window.colourmap);
479 /* set_transparent_background(window.window);
480 * must be done after double buffer stuff? */
483 set_transparent_background(window.window);
484 XClearWindow(display, window.window);
488 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
490 | (own_window ? (StructureNotifyMask |
491 ButtonPressMask | ButtonReleaseMask) : 0)
496 static Window find_subwindow(Window win, int w, int h)
499 Window troot, parent, *children;
502 /* search subwindows with same size as display or work area */
504 for (i = 0; i < 10; i++) {
505 XQueryTree(display, win, &troot, &parent, &children, &n);
507 for (j = 0; j < n; j++) {
508 XWindowAttributes attrs;
510 if (XGetWindowAttributes(display, children[j], &attrs)) {
511 /* Window must be mapped and same size as display or
513 if (attrs.map_state != 0 && ((attrs.width == display_width
514 && attrs.height == display_height)
515 || (attrs.width == w && attrs.height == h))) {
531 long get_x11_color(const char *name)
536 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
537 /* lets check if it's a hex colour with the # missing in front
538 * if yes, then do something about it */
539 char newname[DEFAULT_TEXT_BUFFER_SIZE];
542 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
543 /* now lets try again */
544 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
546 ERR("can't parse X color '%s'", name);
550 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
551 ERR("can't allocate X color '%s'", name);
554 return (long) color.pixel;
561 values.graphics_exposures = 0;
562 values.function = GXcopy;
563 window.gc = XCreateGC(display, window.drawable,
564 GCFunction | GCGraphicsExposures, &values);
567 //Get current desktop number
568 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
572 unsigned long nitems;
573 unsigned long bytes_after;
574 unsigned char *prop = NULL;
575 struct information *current_info = &info;
577 if ( (XGetWindowProperty( current_display, root, atom,
578 0, 1L, False, XA_CARDINAL,
579 &actual_type, &actual_format, &nitems,
580 &bytes_after, &prop ) == Success ) &&
581 (actual_type == XA_CARDINAL) &&
582 (nitems == 1L) && (actual_format == 32) ) {
583 current_info->x11.desktop.current = prop[0]+1;
590 //Get total number of available desktops
591 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
595 unsigned long nitems;
596 unsigned long bytes_after;
597 unsigned char *prop = NULL;
598 struct information *current_info = &info;
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 ( (XGetWindowProperty( current_display, root, atom,
624 0, (~0L), False, ATOM(UTF8_STRING),
625 &actual_type, &actual_format, &nitems,
626 &bytes_after, &prop ) == Success ) &&
627 (actual_type == ATOM(UTF8_STRING)) &&
628 (nitems > 0L) && (actual_format == 8) ) {
630 if(current_info->x11.desktop.all_names) {
631 free(current_info->x11.desktop.all_names);
632 current_info->x11.desktop.all_names = NULL;
634 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
635 memcpy(current_info->x11.desktop.all_names, prop, nitems);
636 current_info->x11.desktop.nitems = nitems;
643 //Get current desktop name
644 static inline void get_x11_desktop_current_name(char *names)
646 struct information *current_info = &info;
647 unsigned int i = 0, j = 0;
650 while ( i < current_info->x11.desktop.nitems ) {
651 if ( names[i++] == '\0' ) {
652 if ( ++k == current_info->x11.desktop.current ) {
653 if (current_info->x11.desktop.name) {
654 free(current_info->x11.desktop.name);
655 current_info->x11.desktop.name = NULL;
657 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
658 //desktop names can be empty but should always be not null
659 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
667 void get_x11_desktop_info(Display *current_display, Atom atom)
670 static Atom atom_current, atom_number, atom_names;
671 struct information *current_info = &info;
672 XWindowAttributes window_attributes;
674 root = RootWindow(current_display, current_info->x11.monitor.current);
676 //Check if we initialise else retrieve changed property
678 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
679 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
680 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
681 get_x11_desktop_current(current_display, root, atom_current);
682 get_x11_desktop_number(current_display, root, atom_number);
683 get_x11_desktop_names(current_display, root, atom_names);
684 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
686 //Set the PropertyChangeMask on the root window, if not set
687 XGetWindowAttributes(display, root, &window_attributes);
688 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
689 XSetWindowAttributes attributes;
690 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
691 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
692 XGetWindowAttributes(display, root, &window_attributes);
695 if (atom == atom_current) {
696 get_x11_desktop_current(current_display, root, atom_current);
697 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
698 } else if (atom == atom_number) {
699 get_x11_desktop_number(current_display, root, atom_number);
700 } else if (atom == atom_names) {
701 get_x11_desktop_names(current_display, root, atom_names);
702 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
707 void update_x11info(void)
709 struct information *current_info = &info;
710 current_info->x11.monitor.number = XScreenCount(display);
711 current_info->x11.monitor.current = XDefaultScreen(display);
715 /* reserve window manager space */
716 void set_struts(int sidenum)
719 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
720 /* reserve space at left, right, top, bottom */
721 signed long sizes[12] = {0};
724 /* define strut depth */
728 sizes[0] = window.x + window.width;
732 sizes[1] = display_width - window.x;
736 sizes[2] = window.y + window.height;
740 sizes[3] = display_height - window.y;
744 /* define partial strut length */
746 sizes[4 + (sidenum*2)] = window.y;
747 sizes[5 + (sidenum*2)] = window.y + window.height;
748 } else if (sidenum <= 3) {
749 sizes[4 + (sidenum*2)] = window.x;
750 sizes[5 + (sidenum*2)] = window.x + window.width;
753 /* check constraints */
754 for (i = 0; i < 12; i++) {
758 if (i <= 1 || i >= 8) {
759 if (sizes[i] > display_width) {
760 sizes[i] = display_width;
763 if (sizes[i] > display_height) {
764 sizes[i] = display_height;
770 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
771 PropModeReplace, (unsigned char *) &sizes, 4);
773 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
774 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
775 PropModeReplace, (unsigned char *) &sizes, 12);
779 #endif /* OWN_WINDOW */