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/>.
35 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
43 #include <X11/Xft/Xft.h>
51 /* some basic X11 stuff */
52 Display *display = NULL;
56 static int set_transparent;
57 static int background_colour;
59 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
63 struct conky_window window;
65 /* local prototypes */
66 static void update_workarea(void);
67 static Window find_desktop_window(Window *p_root, Window *p_desktop);
68 static Window find_subwindow(Window win, int w, int h);
71 void init_X11(const char *disp)
74 if ((display = XOpenDisplay(disp)) == NULL) {
75 CRIT_ERR("can't open display: %s", XDisplayName(0));
79 screen = DefaultScreen(display);
80 display_width = DisplayWidth(display, screen);
81 display_height = DisplayHeight(display, screen);
83 get_x11_desktop_info(display, 0);
88 static void update_workarea(void)
90 /* default work area is display */
93 workarea[2] = display_width;
94 workarea[3] = display_height;
97 /* Find root window and desktop window.
98 * Return desktop window on success,
99 * and set root and desktop byref return values.
100 * Return 0 on failure. */
101 static Window find_desktop_window(Window *p_root, Window *p_desktop)
105 unsigned long nitems, bytes;
107 Window root = RootWindow(display, screen);
109 Window troot, parent, *children;
110 unsigned char *buf = NULL;
112 if (!p_root || !p_desktop) {
116 /* some window managers set __SWM_VROOT to some child of root window */
118 XQueryTree(display, root, &troot, &parent, &children, &n);
119 for (i = 0; i < (int) n; i++) {
120 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
121 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
122 == Success && type == XA_WINDOW) {
123 win = *(Window *) buf;
127 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
142 /* get subwindows from root */
143 win = find_subwindow(root, -1, -1);
147 win = find_subwindow(win, workarea[2], workarea[3]);
156 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
159 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
170 /* sets background to ParentRelative for the Window and all parents */
171 void set_transparent_background(Window win)
173 static int colour_set = -1;
175 if (set_transparent) {
179 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
183 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
185 XQueryTree(display, parent, &r, &parent, &children, &n);
188 } else if (colour_set != background_colour) {
189 XSetWindowBackground(display, win, background_colour);
190 colour_set = background_colour;
192 // XClearWindow(display, win); not sure why this was here
195 void destroy_window(void)
197 XDestroyWindow(display, window.window);
198 XFreeGC(display, window.gc);
199 memset(&window, 0, sizeof(struct conky_window));
202 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
203 char **argv, int argc)
205 /* There seems to be some problems with setting transparent background
206 * (on fluxbox this time). It doesn't happen always and I don't know why it
207 * happens but I bet the bug is somewhere here. */
208 set_transparent = set_trans;
209 background_colour = back_colour;
213 if (!find_desktop_window(&window.root, &window.desktop)) {
217 if (window.type == TYPE_OVERRIDE) {
219 /* An override_redirect True window.
220 * No WM hints or button processing needed. */
221 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
222 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
225 /* Parent is desktop window (which might be a child of root) */
226 window.window = XCreateWindow(display, window.desktop, window.x,
227 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
228 CWBackPixel | CWOverrideRedirect, &attrs);
230 XLowerWindow(display, window.window);
232 fprintf(stderr, PACKAGE_NAME": window type - override\n");
234 } else { /* window.type != TYPE_OVERRIDE */
236 /* A window managed by the window manager.
237 * Process hints and buttons. */
238 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
239 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
240 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
242 XClassHint classHint;
246 if (window.type == TYPE_DOCK) {
247 window.x = window.y = 0;
249 /* Parent is root window so WM can take control */
250 window.window = XCreateWindow(display, window.root, window.x,
251 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
252 CWBackPixel | CWOverrideRedirect, &attrs);
254 classHint.res_name = window.class_name;
255 classHint.res_class = classHint.res_name;
257 wmHint.flags = InputHint | StateHint;
258 /* allow decorated windows to be given input focus by WM */
260 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
261 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
262 wmHint.initial_state = WithdrawnState;
264 wmHint.initial_state = NormalState;
267 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
268 argc, NULL, &wmHint, &classHint);
270 /* Sets an empty WM_PROTOCOLS property */
271 XSetWMProtocols(display, window.window, NULL, 0);
273 /* Set window type */
274 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
277 switch (window.type) {
279 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
280 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
284 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
285 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
289 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
290 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
295 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
296 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
300 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
301 PropModeReplace, (unsigned char *) &prop, 1);
304 /* Set desired hints */
306 /* Window decorations */
307 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
308 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
311 xa = ATOM(_MOTIF_WM_HINTS);
313 long prop[5] = { 2, 0, 0, 0, 0 };
314 XChangeProperty(display, window.window, xa, xa, 32,
315 PropModeReplace, (unsigned char *) prop, 5);
319 /* Below other windows */
320 if (TEST_HINT(window.hints, HINT_BELOW)) {
321 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
324 xa = ATOM(_WIN_LAYER);
328 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
329 PropModeAppend, (unsigned char *) &prop, 1);
332 xa = ATOM(_NET_WM_STATE);
334 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
336 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
337 PropModeAppend, (unsigned char *) &xa_prop, 1);
341 /* Above other windows */
342 if (TEST_HINT(window.hints, HINT_ABOVE)) {
343 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
346 xa = ATOM(_WIN_LAYER);
350 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
351 PropModeAppend, (unsigned char *) &prop, 1);
354 xa = ATOM(_NET_WM_STATE);
356 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
358 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
359 PropModeAppend, (unsigned char *) &xa_prop, 1);
364 if (TEST_HINT(window.hints, HINT_STICKY)) {
365 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
368 xa = ATOM(_NET_WM_DESKTOP);
370 CARD32 xa_prop = 0xFFFFFFFF;
372 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
373 PropModeAppend, (unsigned char *) &xa_prop, 1);
376 xa = ATOM(_NET_WM_STATE);
378 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
380 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
381 PropModeAppend, (unsigned char *) &xa_prop, 1);
386 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
387 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
390 xa = ATOM(_NET_WM_STATE);
392 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
394 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
395 PropModeAppend, (unsigned char *) &xa_prop, 1);
400 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
401 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
404 xa = ATOM(_NET_WM_STATE);
406 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
408 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
409 PropModeAppend, (unsigned char *) &xa_prop, 1);
412 } /* else { window.type != TYPE_OVERRIDE */
414 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
418 XMapWindow(display, window.window);
420 } else /* if (own_window) { */
422 /* root / desktop window */
424 XWindowAttributes attrs;
426 if (!window.window) {
427 window.window = find_desktop_window(&window.root, &window.desktop);
430 if (XGetWindowAttributes(display, window.window, &attrs)) {
431 window.width = attrs.width;
432 window.height = attrs.height;
435 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
438 /* Drawable is same as window. This may be changed by double buffering. */
439 window.drawable = window.window;
445 if (!XdbeQueryExtension(display, &major, &minor)) {
448 window.back_buffer = XdbeAllocateBackBufferName(display,
449 window.window, XdbeBackground);
450 if (window.back_buffer != None) {
451 window.drawable = window.back_buffer;
452 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
458 ERR("failed to set up double buffer");
462 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
465 window.visual = DefaultVisual(display, DefaultScreen(display));
466 window.colourmap = DefaultColormap(display, DefaultScreen(display));
469 cimlib_init(display, window.drawable, window.visual, window.colourmap);
474 /* set_transparent_background(window.window);
475 * must be done after double buffer stuff? */
478 set_transparent_background(window.window);
479 XClearWindow(display, window.window);
483 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
485 | (own_window ? (StructureNotifyMask |
486 ButtonPressMask | ButtonReleaseMask) : 0)
491 static Window find_subwindow(Window win, int w, int h)
494 Window troot, parent, *children;
497 /* search subwindows with same size as display or work area */
499 for (i = 0; i < 10; i++) {
500 XQueryTree(display, win, &troot, &parent, &children, &n);
502 for (j = 0; j < n; j++) {
503 XWindowAttributes attrs;
505 if (XGetWindowAttributes(display, children[j], &attrs)) {
506 /* Window must be mapped and same size as display or
508 if (attrs.map_state != 0 && ((attrs.width == display_width
509 && attrs.height == display_height)
510 || (attrs.width == w && attrs.height == h))) {
526 long get_x11_color(const char *name)
531 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
532 /* lets check if it's a hex colour with the # missing in front
533 * if yes, then do something about it */
534 char newname[DEFAULT_TEXT_BUFFER_SIZE];
537 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
538 /* now lets try again */
539 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
541 ERR("can't parse X color '%s'", name);
545 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
546 ERR("can't allocate X color '%s'", name);
549 return (long) color.pixel;
556 values.graphics_exposures = 0;
557 values.function = GXcopy;
558 window.gc = XCreateGC(display, window.drawable,
559 GCFunction | GCGraphicsExposures, &values);
562 //Get current desktop number
563 static inline void get_x11_desktop_current(Display *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 ( (XGetWindowProperty( 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 *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 ( (XGetWindowProperty( display, root, atom,
596 0, 1L, False, XA_CARDINAL,
597 &actual_type, &actual_format, &nitems,
598 &bytes_after, &prop ) == Success ) &&
599 (actual_type == XA_CARDINAL) &&
600 (nitems == 1L) && (actual_format == 32) ) {
601 current_info->x11.desktop.number = prop[0];
608 //Get all desktop names
609 static inline void get_x11_desktop_names(Display *display, Window root, Atom atom)
613 unsigned long nitems;
614 unsigned long bytes_after;
615 unsigned char *prop = NULL;
616 struct information *current_info = &info;
618 if ( (XGetWindowProperty( display, root, atom,
619 0, (~0L), False, ATOM(UTF8_STRING),
620 &actual_type, &actual_format, &nitems,
621 &bytes_after, &prop ) == Success ) &&
622 (actual_type == ATOM(UTF8_STRING)) &&
623 (nitems > 0L) && (actual_format == 8) ) {
625 if(current_info->x11.desktop.all_names) {
626 free(current_info->x11.desktop.all_names);
627 current_info->x11.desktop.all_names = NULL;
629 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
630 memcpy(current_info->x11.desktop.all_names, prop, nitems);
631 current_info->x11.desktop.nitems = nitems;
638 //Get current desktop name
639 static inline void get_x11_desktop_current_name(char *names)
641 struct information *current_info = &info;
642 unsigned int i = 0, j = 0;
645 while ( i < current_info->x11.desktop.nitems ) {
646 if ( names[i++] == '\0' ) {
647 if ( ++k == current_info->x11.desktop.current ) {
648 if (current_info->x11.desktop.name) {
649 free(current_info->x11.desktop.name);
650 current_info->x11.desktop.name = NULL;
652 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
653 //desktop names can be empty but should always be not null
654 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
662 void get_x11_desktop_info(Display *display, Atom atom)
665 static Atom atom_current, atom_number, atom_names;
666 struct information *current_info = &info;
668 root = RootWindow(display, current_info->x11.monitor.current);
670 //Check if we initialise else retrieve changed property
672 atom_current = XInternAtom(display, "_NET_CURRENT_DESKTOP", True);
673 atom_number = XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", True);
674 atom_names = XInternAtom(display, "_NET_DESKTOP_NAMES", True);
675 get_x11_desktop_current(display, root, atom_current);
676 get_x11_desktop_number(display, root, atom_number);
677 get_x11_desktop_names(display, root, atom_names);
678 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
680 //Set the PropertyChangeMask on the root window, if not set
681 XWindowAttributes window_attributes;
682 XGetWindowAttributes(display, root, &window_attributes);
683 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
684 XSetWindowAttributes attributes;
685 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
686 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
687 XGetWindowAttributes(display, root, &window_attributes);
690 if (atom == atom_current) {
691 get_x11_desktop_current(display, root, atom_current);
692 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
693 } else if (atom == atom_number) {
694 get_x11_desktop_number(display, root, atom_number);
695 } else if (atom == atom_names) {
696 get_x11_desktop_names(display, root, atom_names);
697 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
702 void update_x11info(void)
704 struct information *current_info = &info;
705 current_info->x11.monitor.number = XScreenCount(display);
706 current_info->x11.monitor.current = XDefaultScreen(display);
710 /* reserve window manager space */
711 void set_struts(int sidenum)
714 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
715 /* reserve space at left, right, top, bottom */
716 signed long sizes[12] = {0};
719 /* define strut depth */
724 sizes[0] = window.x + window.width;
730 sizes[1] = display_width - window.x;
736 sizes[2] = window.y + window.height;
742 sizes[3] = display_height - window.y;
747 /* define partial strut length */
749 sizes[4 + (sidenum*2)] = window.y;
750 sizes[5 + (sidenum*2)] = window.y + window.height;
751 } else if (sidenum <= 3) {
752 sizes[4 + (sidenum*2)] = window.x;
753 sizes[5 + (sidenum*2)] = window.x + window.width;
756 /* check constraints */
757 for (i = 0; i < 12; i++) {
761 if (i <= 1 || i >= 8) {
762 if (sizes[i] > display_width) {
763 sizes[i] = display_width;
766 if (sizes[i] > display_height) {
767 sizes[i] = display_height;
773 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
774 PropModeReplace, (unsigned char *) &sizes, 4);
776 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
777 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
778 PropModeReplace, (unsigned char *) &sizes, 12);
782 #endif /* OWN_WINDOW */