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-2008 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>
40 #include <X11/Xft/Xft.h>
48 /* some basic X11 stuff */
53 static int set_transparent;
54 static int background_colour;
56 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
60 struct conky_window window;
62 /* local prototypes */
63 static void update_workarea(void);
64 static Window find_desktop_window(Window *p_root, Window *p_desktop);
65 static Window find_subwindow(Window win, int w, int h);
70 if ((display = XOpenDisplay(0)) == NULL) {
71 CRIT_ERR("can't open display: %s", XDisplayName(0));
74 screen = DefaultScreen(display);
75 display_width = DisplayWidth(display, screen);
76 display_height = DisplayHeight(display, screen);
81 static void update_workarea(void)
83 Window root = RootWindow(display, screen);
84 unsigned long nitems, bytes;
85 unsigned char *buf = NULL;
89 /* default work area is display */
92 workarea[2] = display_width;
93 workarea[3] = display_height;
95 /* get current desktop */
96 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
97 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
98 == Success && type == XA_CARDINAL && nitems > 0) {
101 /* long desktop = *(long *) buf; */
113 /* Find root window and desktop window.
114 * Return desktop window on success,
115 * and set root and desktop byref return values.
116 * Return 0 on failure. */
117 static Window find_desktop_window(Window *p_root, Window *p_desktop)
121 unsigned long nitems, bytes;
123 Window root = RootWindow(display, screen);
125 Window troot, parent, *children;
126 unsigned char *buf = NULL;
128 if (!p_root || !p_desktop) {
132 /* some window managers set __SWM_VROOT to some child of root window */
134 XQueryTree(display, root, &troot, &parent, &children, &n);
135 for (i = 0; i < (int) n; i++) {
136 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
137 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
138 == Success && type == XA_WINDOW) {
139 win = *(Window *) buf;
143 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
158 /* get subwindows from root */
159 win = find_subwindow(root, -1, -1);
163 win = find_subwindow(win, workarea[2], workarea[3]);
172 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
175 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
186 /* sets background to ParentRelative for the Window and all parents */
187 void set_transparent_background(Window win)
189 static int colour_set = -1;
191 if (set_transparent) {
195 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
199 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
201 XQueryTree(display, parent, &r, &parent, &children, &n);
204 } else if (colour_set != background_colour) {
205 XSetWindowBackground(display, win, background_colour);
206 colour_set = background_colour;
208 // XClearWindow(display, win); not sure why this was here
211 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
212 char **argv, int argc)
214 /* There seems to be some problems with setting transparent background
215 * (on fluxbox this time). It doesn't happen always and I don't know why it
216 * happens but I bet the bug is somewhere here. */
217 set_transparent = set_trans;
218 background_colour = back_colour;
223 if (!find_desktop_window(&window.root, &window.desktop)) {
227 if (window.type == TYPE_OVERRIDE) {
229 /* An override_redirect True window.
230 * No WM hints or button processing needed. */
231 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
232 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
235 /* Parent is desktop window (which might be a child of root) */
236 window.window = XCreateWindow(display, window.desktop, window.x,
237 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
238 CWBackPixel | CWOverrideRedirect, &attrs);
240 XLowerWindow(display, window.window);
242 fprintf(stderr, PACKAGE_NAME": window type - override\n");
244 } else { /* window.type != TYPE_OVERRIDE */
246 /* A window managed by the window manager.
247 * Process hints and buttons. */
248 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
249 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
250 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
252 XClassHint classHint;
256 if (window.type == TYPE_DOCK) {
257 window.x = window.y = 0;
259 /* Parent is root window so WM can take control */
260 window.window = XCreateWindow(display, window.root, window.x,
261 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
262 CWBackPixel | CWOverrideRedirect, &attrs);
264 classHint.res_name = window.class_name;
265 classHint.res_class = classHint.res_name;
267 wmHint.flags = InputHint | StateHint;
268 /* allow decorated windows to be given input focus by WM */
270 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
271 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
272 WithdrawnState : 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");
297 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
298 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
302 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
303 PropModeReplace, (unsigned char *) &prop, 1);
306 /* Set desired hints */
308 /* Window decorations */
309 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
310 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
313 xa = ATOM(_MOTIF_WM_HINTS);
315 long prop[5] = { 2, 0, 0, 0, 0 };
316 XChangeProperty(display, window.window, xa, xa, 32,
317 PropModeReplace, (unsigned char *) prop, 5);
321 /* Below other windows */
322 if (TEST_HINT(window.hints, HINT_BELOW)) {
323 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
326 xa = ATOM(_WIN_LAYER);
330 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
331 PropModeAppend, (unsigned char *) &prop, 1);
334 xa = ATOM(_NET_WM_STATE);
336 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
338 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
339 PropModeAppend, (unsigned char *) &xa_prop, 1);
343 /* Above other windows */
344 if (TEST_HINT(window.hints, HINT_ABOVE)) {
345 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
348 xa = ATOM(_WIN_LAYER);
352 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
353 PropModeAppend, (unsigned char *) &prop, 1);
356 xa = ATOM(_NET_WM_STATE);
358 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
360 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
361 PropModeAppend, (unsigned char *) &xa_prop, 1);
366 if (TEST_HINT(window.hints, HINT_STICKY)) {
367 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
370 xa = ATOM(_NET_WM_DESKTOP);
372 CARD32 xa_prop = 0xFFFFFFFF;
374 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
375 PropModeAppend, (unsigned char *) &xa_prop, 1);
378 xa = ATOM(_NET_WM_STATE);
380 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
382 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
383 PropModeAppend, (unsigned char *) &xa_prop, 1);
388 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
389 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
392 xa = ATOM(_NET_WM_STATE);
394 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
396 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
397 PropModeAppend, (unsigned char *) &xa_prop, 1);
402 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
403 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
406 xa = ATOM(_NET_WM_STATE);
408 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
410 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
411 PropModeAppend, (unsigned char *) &xa_prop, 1);
414 } /* else { window.type != TYPE_OVERRIDE */
416 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
420 XMapWindow(display, window.window);
421 } else /* if (own_window) { */
423 /* root / desktop window */
425 XWindowAttributes attrs;
427 if (!window.window) {
428 window.window = find_desktop_window(&window.root, &window.desktop);
431 if (XGetWindowAttributes(display, window.window, &attrs)) {
432 window.width = attrs.width;
433 window.height = attrs.height;
436 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
439 /* Drawable is same as window. This may be changed by double buffering. */
440 window.drawable = window.window;
446 if (!XdbeQueryExtension(display, &major, &minor)) {
449 window.back_buffer = XdbeAllocateBackBufferName(display,
450 window.window, XdbeBackground);
451 if (window.back_buffer != None) {
452 window.drawable = window.back_buffer;
453 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
459 ERR("failed to set up double buffer");
463 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
469 /* set_transparent_background(window.window);
470 * must be done after double buffer stuff? */
473 set_transparent_background(window.window);
474 XClearWindow(display, window.window);
479 XSelectInput(display, window.window, ExposureMask |
480 (own_window ? (StructureNotifyMask | PropertyChangeMask |
481 ButtonPressMask | ButtonReleaseMask) : 0));
483 XSelectInput(display, window.window, ExposureMask);
487 static Window find_subwindow(Window win, int w, int h)
490 Window troot, parent, *children;
493 /* search subwindows with same size as display or work area */
495 for (i = 0; i < 10; i++) {
496 XQueryTree(display, win, &troot, &parent, &children, &n);
498 for (j = 0; j < n; j++) {
499 XWindowAttributes attrs;
501 if (XGetWindowAttributes(display, children[j], &attrs)) {
502 /* Window must be mapped and same size as display or
504 if (attrs.map_state != 0 && ((attrs.width == display_width
505 && attrs.height == display_height)
506 || (attrs.width == w && attrs.height == h))) {
522 long get_x11_color(const char *name)
527 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
528 /* lets check if it's a hex colour with the # missing in front
529 * if yes, then do something about it */
530 char newname[DEFAULT_TEXT_BUFFER_SIZE];
533 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
534 /* now lets try again */
535 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
537 ERR("can't parse X color '%s'", name);
541 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
542 ERR("can't allocate X color '%s'", name);
545 return (long) color.pixel;
552 values.graphics_exposures = 0;
553 values.function = GXcopy;
554 window.gc = XCreateGC(display, window.drawable,
555 GCFunction | GCGraphicsExposures, &values);
558 void update_x11info(void)
560 struct information *current_info = &info;
561 current_info->x11.monitor.number = XScreenCount(display);
562 current_info->x11.monitor.current = XDefaultScreen(display);