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 */
66 /* local prototypes */
67 static void update_workarea(void);
68 static Window find_desktop_window(Window *p_root, Window *p_desktop);
69 static Window find_subwindow(Window win, int w, int h);
72 void init_X11(const char *disp)
75 if ((display = XOpenDisplay(disp)) == NULL) {
76 CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
80 screen = DefaultScreen(display);
81 display_width = DisplayWidth(display, screen);
82 display_height = DisplayHeight(display, screen);
84 get_x11_desktop_info(display, 0);
89 static void update_workarea(void)
91 /* default work area is display */
94 workarea[2] = display_width;
95 workarea[3] = display_height;
98 /* Find root window and desktop window.
99 * Return desktop window on success,
100 * and set root and desktop byref return values.
101 * Return 0 on failure. */
102 static Window find_desktop_window(Window *p_root, Window *p_desktop)
106 unsigned long nitems, bytes;
108 Window root = RootWindow(display, screen);
110 Window troot, parent, *children;
111 unsigned char *buf = NULL;
113 if (!p_root || !p_desktop) {
117 /* some window managers set __SWM_VROOT to some child of root window */
119 XQueryTree(display, root, &troot, &parent, &children, &n);
120 for (i = 0; i < (int) n; i++) {
121 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
122 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
123 == Success && type == XA_WINDOW) {
124 win = *(Window *) buf;
128 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
143 /* get subwindows from root */
144 win = find_subwindow(root, -1, -1);
148 win = find_subwindow(win, workarea[2], workarea[3]);
157 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
160 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
171 /* sets background to ParentRelative for the Window and all parents */
172 void set_transparent_background(Window win)
174 static int colour_set = -1;
176 if (set_transparent) {
180 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
184 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
186 XQueryTree(display, parent, &r, &parent, &children, &n);
189 } else if (colour_set != background_colour) {
190 XSetWindowBackground(display, win, background_colour);
191 colour_set = background_colour;
195 void destroy_window(void)
198 XftDrawDestroy(window.xftdraw);
201 XFreeGC(display, window.gc);
203 memset(&window, 0, sizeof(struct conky_window));
206 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
207 char **argv, int argc)
209 /* There seems to be some problems with setting transparent background
210 * (on fluxbox this time). It doesn't happen always and I don't know why it
211 * happens but I bet the bug is somewhere here. */
212 set_transparent = set_trans;
213 background_colour = back_colour;
217 if (!find_desktop_window(&window.root, &window.desktop)) {
221 if (window.type == TYPE_OVERRIDE) {
223 /* An override_redirect True window.
224 * No WM hints or button processing needed. */
225 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
226 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
229 /* Parent is desktop window (which might be a child of root) */
230 window.window = XCreateWindow(display, window.desktop, window.x,
231 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
232 CWBackPixel | CWOverrideRedirect, &attrs);
234 XLowerWindow(display, window.window);
236 fprintf(stderr, PACKAGE_NAME": window type - override\n");
238 } else { /* window.type != TYPE_OVERRIDE */
240 /* A window managed by the window manager.
241 * Process hints and buttons. */
242 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
243 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
244 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
246 XClassHint classHint;
250 if (window.type == TYPE_DOCK) {
251 window.x = window.y = 0;
253 /* Parent is root window so WM can take control */
254 window.window = XCreateWindow(display, window.root, window.x,
255 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
256 CWBackPixel | CWOverrideRedirect, &attrs);
258 classHint.res_name = window.class_name;
259 classHint.res_class = classHint.res_name;
261 wmHint.flags = InputHint | StateHint;
262 /* allow decorated windows to be given input focus by WM */
264 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
265 if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
266 wmHint.initial_state = WithdrawnState;
268 wmHint.initial_state = NormalState;
271 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
272 argc, NULL, &wmHint, &classHint);
274 /* Sets an empty WM_PROTOCOLS property */
275 XSetWMProtocols(display, window.window, NULL, 0);
277 /* Set window type */
278 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
281 switch (window.type) {
283 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
284 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
288 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
289 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
293 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
294 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
299 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
300 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
304 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
305 PropModeReplace, (unsigned char *) &prop, 1);
308 /* Set desired hints */
310 /* Window decorations */
311 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
312 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
315 xa = ATOM(_MOTIF_WM_HINTS);
317 long prop[5] = { 2, 0, 0, 0, 0 };
318 XChangeProperty(display, window.window, xa, xa, 32,
319 PropModeReplace, (unsigned char *) prop, 5);
323 /* Below other windows */
324 if (TEST_HINT(window.hints, HINT_BELOW)) {
325 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
328 xa = ATOM(_WIN_LAYER);
332 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
333 PropModeAppend, (unsigned char *) &prop, 1);
336 xa = ATOM(_NET_WM_STATE);
338 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
340 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
341 PropModeAppend, (unsigned char *) &xa_prop, 1);
345 /* Above other windows */
346 if (TEST_HINT(window.hints, HINT_ABOVE)) {
347 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
350 xa = ATOM(_WIN_LAYER);
354 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
355 PropModeAppend, (unsigned char *) &prop, 1);
358 xa = ATOM(_NET_WM_STATE);
360 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
362 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
363 PropModeAppend, (unsigned char *) &xa_prop, 1);
368 if (TEST_HINT(window.hints, HINT_STICKY)) {
369 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
372 xa = ATOM(_NET_WM_DESKTOP);
374 CARD32 xa_prop = 0xFFFFFFFF;
376 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
377 PropModeAppend, (unsigned char *) &xa_prop, 1);
380 xa = ATOM(_NET_WM_STATE);
382 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
384 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
385 PropModeAppend, (unsigned char *) &xa_prop, 1);
390 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
391 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
394 xa = ATOM(_NET_WM_STATE);
396 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
398 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
399 PropModeAppend, (unsigned char *) &xa_prop, 1);
404 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
405 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
408 xa = ATOM(_NET_WM_STATE);
410 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
412 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
413 PropModeAppend, (unsigned char *) &xa_prop, 1);
418 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
422 XMapWindow(display, window.window);
425 #endif /* OWN_WINDOW */
427 XWindowAttributes attrs;
429 if (!window.window) {
430 window.window = find_desktop_window(&window.root, &window.desktop);
433 if (XGetWindowAttributes(display, window.window, &attrs)) {
434 window.width = attrs.width;
435 window.height = attrs.height;
438 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
441 /* Drawable is same as window. This may be changed by double buffering. */
442 window.drawable = window.window;
448 if (!XdbeQueryExtension(display, &major, &minor)) {
451 window.back_buffer = XdbeAllocateBackBufferName(display,
452 window.window, XdbeBackground);
453 if (window.back_buffer != None) {
454 window.drawable = window.back_buffer;
455 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
461 NORM_ERR("failed to set up double buffer");
465 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
468 window.visual = DefaultVisual(display, DefaultScreen(display));
469 window.colourmap = DefaultColormap(display, DefaultScreen(display));
472 cimlib_init(display, window.drawable, window.visual, window.colourmap);
477 XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
479 | (own_window ? (StructureNotifyMask |
480 ButtonPressMask | ButtonReleaseMask) : 0)
485 static Window find_subwindow(Window win, int w, int h)
488 Window troot, parent, *children;
491 /* search subwindows with same size as display or work area */
493 for (i = 0; i < 10; i++) {
494 XQueryTree(display, win, &troot, &parent, &children, &n);
496 for (j = 0; j < n; j++) {
497 XWindowAttributes attrs;
499 if (XGetWindowAttributes(display, children[j], &attrs)) {
500 /* Window must be mapped and same size as display or
502 if (attrs.map_state != 0 && ((attrs.width == display_width
503 && attrs.height == display_height)
504 || (attrs.width == w && attrs.height == h))) {
520 long get_x11_color(const char *name)
525 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
526 /* lets check if it's a hex colour with the # missing in front
527 * if yes, then do something about it */
528 char newname[DEFAULT_TEXT_BUFFER_SIZE];
531 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
532 /* now lets try again */
533 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
535 NORM_ERR("can't parse X color '%s'", name);
539 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
540 NORM_ERR("can't allocate X color '%s'", name);
543 return (long) color.pixel;
550 values.graphics_exposures = 0;
551 values.function = GXcopy;
552 window.gc = XCreateGC(display, window.drawable,
553 GCFunction | GCGraphicsExposures, &values);
556 //Get current desktop number
557 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
561 unsigned long nitems;
562 unsigned long bytes_after;
563 unsigned char *prop = NULL;
564 struct information *current_info = &info;
566 if (atom == None) return;
568 if ( (XGetWindowProperty( current_display, root, atom,
569 0, 1L, False, XA_CARDINAL,
570 &actual_type, &actual_format, &nitems,
571 &bytes_after, &prop ) == Success ) &&
572 (actual_type == XA_CARDINAL) &&
573 (nitems == 1L) && (actual_format == 32) ) {
574 current_info->x11.desktop.current = prop[0]+1;
581 //Get total number of available desktops
582 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
586 unsigned long nitems;
587 unsigned long bytes_after;
588 unsigned char *prop = NULL;
589 struct information *current_info = &info;
591 if (atom == None) return;
593 if ( (XGetWindowProperty( current_display, root, atom,
594 0, 1L, False, XA_CARDINAL,
595 &actual_type, &actual_format, &nitems,
596 &bytes_after, &prop ) == Success ) &&
597 (actual_type == XA_CARDINAL) &&
598 (nitems == 1L) && (actual_format == 32) ) {
599 current_info->x11.desktop.number = prop[0];
606 //Get all desktop names
607 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
611 unsigned long nitems;
612 unsigned long bytes_after;
613 unsigned char *prop = NULL;
614 struct information *current_info = &info;
616 if (atom == None) return;
618 if ( (XGetWindowProperty( current_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 *current_display, Atom atom)
665 static Atom atom_current, atom_number, atom_names;
666 struct information *current_info = &info;
667 XWindowAttributes window_attributes;
669 root = RootWindow(current_display, current_info->x11.monitor.current);
671 /* Check if we initialise else retrieve changed property */
673 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
674 atom_number = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
675 atom_names = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
676 get_x11_desktop_current(current_display, root, atom_current);
677 get_x11_desktop_number(current_display, root, atom_number);
678 get_x11_desktop_names(current_display, root, atom_names);
679 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
681 /* Set the PropertyChangeMask on the root window, if not set */
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(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(current_display, root, atom_number);
695 } else if (atom == atom_names) {
696 get_x11_desktop_names(current_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 */
723 sizes[0] = window.x + window.width;
727 sizes[1] = display_width - window.x;
731 sizes[2] = window.y + window.height;
735 sizes[3] = display_height - window.y;
739 /* define partial strut length */
741 sizes[4 + (sidenum*2)] = window.y;
742 sizes[5 + (sidenum*2)] = window.y + window.height;
743 } else if (sidenum <= 3) {
744 sizes[4 + (sidenum*2)] = window.x;
745 sizes[5 + (sidenum*2)] = window.x + window.width;
748 /* check constraints */
749 for (i = 0; i < 12; i++) {
753 if (i <= 1 || i >= 8) {
754 if (sizes[i] > display_width) {
755 sizes[i] = display_width;
758 if (sizes[i] > display_height) {
759 sizes[i] = display_height;
765 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
766 PropModeReplace, (unsigned char *) &sizes, 4);
768 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
769 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
770 PropModeReplace, (unsigned char *) &sizes, 12);
774 #endif /* OWN_WINDOW */
777 void xdbe_swap_buffers(void)
782 swap.swap_window = window.window;
783 swap.swap_action = XdbeBackground;
784 XdbeSwapBuffers(display, &swap, 1);
787 #endif /* HAVE_XDBE */