1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
39 #include <X11/Xatom.h>
41 #include <X11/Xutil.h>
47 #include <X11/Xft/Xft.h>
55 /* some basic X11 stuff */
56 Display *display = NULL;
60 static int set_transparent;
61 static int background_colour;
63 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
67 struct conky_window window;
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)
201 XftDrawDestroy(window.xftdraw);
204 XFreeGC(display, window.gc);
206 memset(&window, 0, sizeof(struct conky_window));
209 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
210 char **argv, int argc)
212 /* There seems to be some problems with setting transparent background
213 * (on fluxbox this time). It doesn't happen always and I don't know why it
214 * happens but I bet the bug is somewhere here. */
215 set_transparent = set_trans;
216 background_colour = back_colour;
220 if (!find_desktop_window(&window.root, &window.desktop)) {
224 if (window.type == TYPE_OVERRIDE) {
226 /* An override_redirect True window.
227 * No WM hints or button processing needed. */
228 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
229 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
232 /* Parent is desktop window (which might be a child of root) */
233 window.window = XCreateWindow(display, window.desktop, window.x,
234 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
235 CWBackPixel | CWOverrideRedirect, &attrs);
237 XLowerWindow(display, window.window);
239 fprintf(stderr, PACKAGE_NAME": window type - override\n");
241 } else { /* window.type != TYPE_OVERRIDE */
243 /* A window managed by the window manager.
244 * Process hints and buttons. */
245 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
246 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
247 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
249 XClassHint classHint;
253 if (window.type == TYPE_DOCK) {
254 window.x = window.y = 0;
256 /* Parent is root window so WM can take control */
257 window.window = XCreateWindow(display, window.root, window.x,
258 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
259 CWBackPixel | CWOverrideRedirect, &attrs);
261 classHint.res_name = window.class_name;
262 classHint.res_class = classHint.res_name;
264 wmHint.flags = InputHint | StateHint;
265 /* allow decorated windows to be given input focus by WM */
267 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
268 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
269 wmHint.initial_state = WithdrawnState;
271 wmHint.initial_state = NormalState;
274 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
275 argc, NULL, &wmHint, &classHint);
277 /* Sets an empty WM_PROTOCOLS property */
278 XSetWMProtocols(display, window.window, NULL, 0);
280 /* Set window type */
281 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
284 switch (window.type) {
286 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
287 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
291 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
292 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
296 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
297 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
302 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
303 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
307 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
308 PropModeReplace, (unsigned char *) &prop, 1);
311 /* Set desired hints */
313 /* Window decorations */
314 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
315 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
318 xa = ATOM(_MOTIF_WM_HINTS);
320 long prop[5] = { 2, 0, 0, 0, 0 };
321 XChangeProperty(display, window.window, xa, xa, 32,
322 PropModeReplace, (unsigned char *) prop, 5);
326 /* Below other windows */
327 if (TEST_HINT(window.hints, HINT_BELOW)) {
328 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
331 xa = ATOM(_WIN_LAYER);
335 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
336 PropModeAppend, (unsigned char *) &prop, 1);
339 xa = ATOM(_NET_WM_STATE);
341 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
343 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
344 PropModeAppend, (unsigned char *) &xa_prop, 1);
348 /* Above other windows */
349 if (TEST_HINT(window.hints, HINT_ABOVE)) {
350 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
353 xa = ATOM(_WIN_LAYER);
357 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
358 PropModeAppend, (unsigned char *) &prop, 1);
361 xa = ATOM(_NET_WM_STATE);
363 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
365 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
366 PropModeAppend, (unsigned char *) &xa_prop, 1);
371 if (TEST_HINT(window.hints, HINT_STICKY)) {
372 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
375 xa = ATOM(_NET_WM_DESKTOP);
377 CARD32 xa_prop = 0xFFFFFFFF;
379 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
380 PropModeAppend, (unsigned char *) &xa_prop, 1);
383 xa = ATOM(_NET_WM_STATE);
385 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
387 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
388 PropModeAppend, (unsigned char *) &xa_prop, 1);
393 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
394 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
397 xa = ATOM(_NET_WM_STATE);
399 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
401 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
402 PropModeAppend, (unsigned char *) &xa_prop, 1);
407 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
408 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
411 xa = ATOM(_NET_WM_STATE);
413 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
415 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
416 PropModeAppend, (unsigned char *) &xa_prop, 1);
421 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
425 XMapWindow(display, window.window);
428 #endif /* OWN_WINDOW */
430 XWindowAttributes attrs;
432 if (!window.window) {
433 window.window = find_desktop_window(&window.root, &window.desktop);
436 if (XGetWindowAttributes(display, window.window, &attrs)) {
437 window.width = attrs.width;
438 window.height = attrs.height;
441 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
444 /* Drawable is same as window. This may be changed by double buffering. */
445 window.drawable = window.window;
451 if (!XdbeQueryExtension(display, &major, &minor)) {
454 window.back_buffer = XdbeAllocateBackBufferName(display,
455 window.window, XdbeBackground);
456 if (window.back_buffer != None) {
457 window.drawable = window.back_buffer;
458 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
464 NORM_ERR("failed to set up double buffer");
468 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
471 window.visual = DefaultVisual(display, DefaultScreen(display));
472 window.colourmap = DefaultColormap(display, DefaultScreen(display));
475 cimlib_init(display, window.drawable, window.visual, window.colourmap);
480 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
482 | (own_window ? (StructureNotifyMask |
483 ButtonPressMask | ButtonReleaseMask) : 0)
488 static Window find_subwindow(Window win, int w, int h)
491 Window troot, parent, *children;
494 /* search subwindows with same size as display or work area */
496 for (i = 0; i < 10; i++) {
497 XQueryTree(display, win, &troot, &parent, &children, &n);
499 for (j = 0; j < n; j++) {
500 XWindowAttributes attrs;
502 if (XGetWindowAttributes(display, children[j], &attrs)) {
503 /* Window must be mapped and same size as display or
505 if (attrs.map_state != 0 && ((attrs.width == display_width
506 && attrs.height == display_height)
507 || (attrs.width == w && attrs.height == h))) {
523 long get_x11_color(const char *name)
528 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
529 /* lets check if it's a hex colour with the # missing in front
530 * if yes, then do something about it */
531 char newname[DEFAULT_TEXT_BUFFER_SIZE];
534 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
535 /* now lets try again */
536 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
538 NORM_ERR("can't parse X color '%s'", name);
542 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
543 NORM_ERR("can't allocate X color '%s'", name);
546 return (long) color.pixel;
553 values.graphics_exposures = 0;
554 values.function = GXcopy;
555 window.gc = XCreateGC(display, window.drawable,
556 GCFunction | GCGraphicsExposures, &values);
559 //Get current desktop number
560 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
564 unsigned long nitems;
565 unsigned long bytes_after;
566 unsigned char *prop = NULL;
567 struct information *current_info = &info;
569 if (atom == None) return;
571 if ( (XGetWindowProperty( current_display, root, atom,
572 0, 1L, False, XA_CARDINAL,
573 &actual_type, &actual_format, &nitems,
574 &bytes_after, &prop ) == Success ) &&
575 (actual_type == XA_CARDINAL) &&
576 (nitems == 1L) && (actual_format == 32) ) {
577 current_info->x11.desktop.current = prop[0]+1;
584 //Get total number of available desktops
585 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
589 unsigned long nitems;
590 unsigned long bytes_after;
591 unsigned char *prop = NULL;
592 struct information *current_info = &info;
594 if (atom == None) return;
596 if ( (XGetWindowProperty( current_display, root, atom,
597 0, 1L, False, XA_CARDINAL,
598 &actual_type, &actual_format, &nitems,
599 &bytes_after, &prop ) == Success ) &&
600 (actual_type == XA_CARDINAL) &&
601 (nitems == 1L) && (actual_format == 32) ) {
602 current_info->x11.desktop.number = prop[0];
609 //Get all desktop names
610 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
614 unsigned long nitems;
615 unsigned long bytes_after;
616 unsigned char *prop = NULL;
617 struct information *current_info = &info;
619 if (atom == None) return;
621 if ( (XGetWindowProperty( current_display, root, atom,
622 0, (~0L), False, ATOM(UTF8_STRING),
623 &actual_type, &actual_format, &nitems,
624 &bytes_after, &prop ) == Success ) &&
625 (actual_type == ATOM(UTF8_STRING)) &&
626 (nitems > 0L) && (actual_format == 8) ) {
628 if(current_info->x11.desktop.all_names) {
629 free(current_info->x11.desktop.all_names);
630 current_info->x11.desktop.all_names = NULL;
632 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
633 memcpy(current_info->x11.desktop.all_names, prop, nitems);
634 current_info->x11.desktop.nitems = nitems;
641 //Get current desktop name
642 static inline void get_x11_desktop_current_name(char *names)
644 struct information *current_info = &info;
645 unsigned int i = 0, j = 0;
648 while ( i < current_info->x11.desktop.nitems ) {
649 if ( names[i++] == '\0' ) {
650 if ( ++k == current_info->x11.desktop.current ) {
651 if (current_info->x11.desktop.name) {
652 free(current_info->x11.desktop.name);
653 current_info->x11.desktop.name = NULL;
655 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
656 //desktop names can be empty but should always be not null
657 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
665 void get_x11_desktop_info(Display *current_display, Atom atom)
668 static Atom atom_current, atom_number, atom_names;
669 struct information *current_info = &info;
670 XWindowAttributes window_attributes;
672 root = RootWindow(current_display, current_info->x11.monitor.current);
674 /* Check if we initialise else retrieve changed property */
676 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
677 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
678 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
679 get_x11_desktop_current(current_display, root, atom_current);
680 get_x11_desktop_number(current_display, root, atom_number);
681 get_x11_desktop_names(current_display, root, atom_names);
682 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
684 /* Set the PropertyChangeMask on the root window, if not set */
685 XGetWindowAttributes(display, root, &window_attributes);
686 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
687 XSetWindowAttributes attributes;
688 attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
689 XChangeWindowAttributes(display, root, CWEventMask, &attributes);
690 XGetWindowAttributes(display, root, &window_attributes);
693 if (atom == atom_current) {
694 get_x11_desktop_current(current_display, root, atom_current);
695 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
696 } else if (atom == atom_number) {
697 get_x11_desktop_number(current_display, root, atom_number);
698 } else if (atom == atom_names) {
699 get_x11_desktop_names(current_display, root, atom_names);
700 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
705 void update_x11info(void)
707 struct information *current_info = &info;
708 current_info->x11.monitor.number = XScreenCount(display);
709 current_info->x11.monitor.current = XDefaultScreen(display);
713 /* reserve window manager space */
714 void set_struts(int sidenum)
717 if ((strut = ATOM(_NET_WM_STRUT)) != None) {
718 /* reserve space at left, right, top, bottom */
719 signed long sizes[12] = {0};
722 /* define strut depth */
726 sizes[0] = window.x + window.width;
730 sizes[1] = display_width - window.x;
734 sizes[2] = window.y + window.height;
738 sizes[3] = display_height - window.y;
742 /* define partial strut length */
744 sizes[4 + (sidenum*2)] = window.y;
745 sizes[5 + (sidenum*2)] = window.y + window.height;
746 } else if (sidenum <= 3) {
747 sizes[4 + (sidenum*2)] = window.x;
748 sizes[5 + (sidenum*2)] = window.x + window.width;
751 /* check constraints */
752 for (i = 0; i < 12; i++) {
756 if (i <= 1 || i >= 8) {
757 if (sizes[i] > display_width) {
758 sizes[i] = display_width;
761 if (sizes[i] > display_height) {
762 sizes[i] = display_height;
768 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
769 PropModeReplace, (unsigned char *) &sizes, 4);
771 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
772 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
773 PropModeReplace, (unsigned char *) &sizes, 12);
777 #endif /* OWN_WINDOW */