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/>.
32 #include <X11/Xatom.h>
34 #include <X11/Xutil.h>
36 #include <X11/Xft/Xft.h>
47 /* some basic X11 stuff */
52 static int set_transparent;
53 static int background_colour;
55 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
59 struct conky_window window;
61 /* local prototypes */
62 static void update_workarea(void);
63 static Window find_desktop_window(Window *p_root, Window *p_desktop);
64 static Window find_subwindow(Window win, int w, int h);
69 if ((display = XOpenDisplay(0)) == NULL) {
70 CRIT_ERR("can't open display: %s", XDisplayName(0));
73 screen = DefaultScreen(display);
74 display_width = DisplayWidth(display, screen);
75 display_height = DisplayHeight(display, screen);
80 static void update_workarea(void)
82 Window root = RootWindow(display, screen);
83 unsigned long nitems, bytes;
84 unsigned char *buf = NULL;
88 /* default work area is display */
91 workarea[2] = display_width;
92 workarea[3] = display_height;
94 /* get current desktop */
95 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
96 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
97 == Success && type == XA_CARDINAL && nitems > 0) {
100 /* long desktop = *(long *) buf; */
112 /* Find root window and desktop window.
113 * Return desktop window on success,
114 * and set root and desktop byref return values.
115 * Return 0 on failure. */
116 static Window find_desktop_window(Window *p_root, Window *p_desktop)
120 unsigned long nitems, bytes;
122 Window root = RootWindow(display, screen);
124 Window troot, parent, *children;
125 unsigned char *buf = NULL;
127 if (!p_root || !p_desktop) {
131 /* some window managers set __SWM_VROOT to some child of root window */
133 XQueryTree(display, root, &troot, &parent, &children, &n);
134 for (i = 0; i < (int) n; i++) {
135 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
136 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
137 == Success && type == XA_WINDOW) {
138 win = *(Window *) buf;
142 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
157 /* get subwindows from root */
158 win = find_subwindow(root, -1, -1);
162 win = find_subwindow(win, workarea[2], workarea[3]);
171 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
174 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
185 /* sets background to ParentRelative for the Window and all parents */
186 void set_transparent_background(Window win)
188 static int colour_set = -1;
190 if (set_transparent) {
194 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
198 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
200 XQueryTree(display, parent, &r, &parent, &children, &n);
203 } else if (colour_set != background_colour) {
204 XSetWindowBackground(display, win, background_colour);
205 colour_set = background_colour;
207 // XClearWindow(display, win); not sure why this was here
210 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
211 char **argv, int argc)
213 /* There seems to be some problems with setting transparent background
214 * (on fluxbox this time). It doesn't happen always and I don't know why it
215 * happens but I bet the bug is somewhere here. */
216 set_transparent = set_trans;
217 background_colour = back_colour;
222 if (!find_desktop_window(&window.root, &window.desktop)) {
226 if (window.type == TYPE_OVERRIDE) {
228 /* An override_redirect True window.
229 * No WM hints or button processing needed. */
230 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
231 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
234 /* Parent is desktop window (which might be a child of root) */
235 window.window = XCreateWindow(display, window.desktop, window.x,
236 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
237 CWBackPixel | CWOverrideRedirect, &attrs);
239 XLowerWindow(display, window.window);
241 fprintf(stderr, PACKAGE_NAME": window type - override\n");
243 } else { /* window.type != TYPE_OVERRIDE */
245 /* A window managed by the window manager.
246 * Process hints and buttons. */
247 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
248 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
249 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
251 XClassHint classHint;
255 if (window.type == TYPE_DOCK) {
256 window.x = window.y = 0;
258 /* Parent is root window so WM can take control */
259 window.window = XCreateWindow(display, window.root, window.x,
260 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
261 CWBackPixel | CWOverrideRedirect, &attrs);
263 classHint.res_name = window.class_name;
264 classHint.res_class = classHint.res_name;
266 wmHint.flags = InputHint | StateHint;
267 /* allow decorated windows to be given input focus by WM */
269 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
270 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
271 WithdrawnState : 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");
296 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
297 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
301 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
302 PropModeReplace, (unsigned char *) &prop, 1);
305 /* Set desired hints */
307 /* Window decorations */
308 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
309 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
312 xa = ATOM(_MOTIF_WM_HINTS);
314 long prop[5] = { 2, 0, 0, 0, 0 };
315 XChangeProperty(display, window.window, xa, xa, 32,
316 PropModeReplace, (unsigned char *) prop, 5);
320 /* Below other windows */
321 if (TEST_HINT(window.hints, HINT_BELOW)) {
322 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
325 xa = ATOM(_WIN_LAYER);
329 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
330 PropModeAppend, (unsigned char *) &prop, 1);
333 xa = ATOM(_NET_WM_STATE);
335 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
337 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
338 PropModeAppend, (unsigned char *) &xa_prop, 1);
342 /* Above other windows */
343 if (TEST_HINT(window.hints, HINT_ABOVE)) {
344 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
347 xa = ATOM(_WIN_LAYER);
351 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
352 PropModeAppend, (unsigned char *) &prop, 1);
355 xa = ATOM(_NET_WM_STATE);
357 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
359 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
360 PropModeAppend, (unsigned char *) &xa_prop, 1);
365 if (TEST_HINT(window.hints, HINT_STICKY)) {
366 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
369 xa = ATOM(_NET_WM_DESKTOP);
371 CARD32 xa_prop = 0xFFFFFFFF;
373 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
374 PropModeAppend, (unsigned char *) &xa_prop, 1);
377 xa = ATOM(_NET_WM_STATE);
379 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
381 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
382 PropModeAppend, (unsigned char *) &xa_prop, 1);
387 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
388 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
391 xa = ATOM(_NET_WM_STATE);
393 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
395 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
396 PropModeAppend, (unsigned char *) &xa_prop, 1);
401 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
402 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
405 xa = ATOM(_NET_WM_STATE);
407 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
409 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
410 PropModeAppend, (unsigned char *) &xa_prop, 1);
413 } /* else { window.type != TYPE_OVERRIDE */
415 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
419 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");
468 /* set_transparent_background(window.window);
469 * must be done after double buffer stuff? */
472 set_transparent_background(window.window);
473 XClearWindow(display, window.window);
478 XSelectInput(display, window.window, ExposureMask |
479 (own_window ? (StructureNotifyMask | PropertyChangeMask |
480 ButtonPressMask | ButtonReleaseMask) : 0));
482 XSelectInput(display, window.window, ExposureMask);
486 static Window find_subwindow(Window win, int w, int h)
489 Window troot, parent, *children;
492 /* search subwindows with same size as display or work area */
494 for (i = 0; i < 10; i++) {
495 XQueryTree(display, win, &troot, &parent, &children, &n);
497 for (j = 0; j < n; j++) {
498 XWindowAttributes attrs;
500 if (XGetWindowAttributes(display, children[j], &attrs)) {
501 /* Window must be mapped and same size as display or
503 if (attrs.map_state != 0 && ((attrs.width == display_width
504 && attrs.height == display_height)
505 || (attrs.width == w && attrs.height == h))) {
521 long get_x11_color(const char *name)
526 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
527 /* lets check if it's a hex colour with the # missing in front
528 * if yes, then do something about it */
529 char newname[DEFAULT_TEXT_BUFFER_SIZE];
532 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
533 /* now lets try again */
534 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
536 ERR("can't parse X color '%s'", name);
540 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
541 ERR("can't allocate X color '%s'", name);
544 return (long) color.pixel;
551 values.graphics_exposures = 0;
552 values.function = GXcopy;
553 window.gc = XCreateGC(display, window.drawable,
554 GCFunction | GCGraphicsExposures, &values);
557 void update_x11info(void)
559 struct information *current_info = &info;
560 current_info->x11.monitor.number = XScreenCount(display);
561 current_info->x11.monitor.current = XDefaultScreen(display);