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 */
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)
73 if ((display = XOpenDisplay(disp)) == NULL) {
74 CRIT_ERR("can't open display: %s", XDisplayName(0));
77 screen = DefaultScreen(display);
78 display_width = DisplayWidth(display, screen);
79 display_height = DisplayHeight(display, screen);
84 static void update_workarea(void)
86 Window root = RootWindow(display, screen);
87 unsigned long nitems, bytes;
88 unsigned char *buf = NULL;
92 /* default work area is display */
95 workarea[2] = display_width;
96 workarea[3] = display_height;
98 /* get current desktop */
99 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
100 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
101 == Success && type == XA_CARDINAL && nitems > 0) {
104 /* long desktop = *(long *) buf; */
116 /* Find root window and desktop window.
117 * Return desktop window on success,
118 * and set root and desktop byref return values.
119 * Return 0 on failure. */
120 static Window find_desktop_window(Window *p_root, Window *p_desktop)
124 unsigned long nitems, bytes;
126 Window root = RootWindow(display, screen);
128 Window troot, parent, *children;
129 unsigned char *buf = NULL;
131 if (!p_root || !p_desktop) {
135 /* some window managers set __SWM_VROOT to some child of root window */
137 XQueryTree(display, root, &troot, &parent, &children, &n);
138 for (i = 0; i < (int) n; i++) {
139 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
140 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
141 == Success && type == XA_WINDOW) {
142 win = *(Window *) buf;
146 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
161 /* get subwindows from root */
162 win = find_subwindow(root, -1, -1);
166 win = find_subwindow(win, workarea[2], workarea[3]);
175 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
178 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
189 /* sets background to ParentRelative for the Window and all parents */
190 void set_transparent_background(Window win)
192 static int colour_set = -1;
194 if (set_transparent) {
198 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
202 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
204 XQueryTree(display, parent, &r, &parent, &children, &n);
207 } else if (colour_set != background_colour) {
208 XSetWindowBackground(display, win, background_colour);
209 colour_set = background_colour;
211 // XClearWindow(display, win); not sure why this was here
214 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
215 char **argv, int argc)
217 /* There seems to be some problems with setting transparent background
218 * (on fluxbox this time). It doesn't happen always and I don't know why it
219 * happens but I bet the bug is somewhere here. */
220 set_transparent = set_trans;
221 background_colour = back_colour;
226 if (!find_desktop_window(&window.root, &window.desktop)) {
230 if (window.type == TYPE_OVERRIDE) {
232 /* An override_redirect True window.
233 * No WM hints or button processing needed. */
234 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
235 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
238 /* Parent is desktop window (which might be a child of root) */
239 window.window = XCreateWindow(display, window.desktop, window.x,
240 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
241 CWBackPixel | CWOverrideRedirect, &attrs);
243 XLowerWindow(display, window.window);
245 fprintf(stderr, PACKAGE_NAME": window type - override\n");
247 } else { /* window.type != TYPE_OVERRIDE */
249 /* A window managed by the window manager.
250 * Process hints and buttons. */
251 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
252 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
253 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
255 XClassHint classHint;
259 if (window.type == TYPE_DOCK) {
260 window.x = window.y = 0;
262 /* Parent is root window so WM can take control */
263 window.window = XCreateWindow(display, window.root, window.x,
264 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
265 CWBackPixel | CWOverrideRedirect, &attrs);
267 classHint.res_name = window.class_name;
268 classHint.res_class = classHint.res_name;
270 wmHint.flags = InputHint | StateHint;
271 /* allow decorated windows to be given input focus by WM */
273 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
274 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
275 WithdrawnState : NormalState);
277 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
278 argc, NULL, &wmHint, &classHint);
280 /* Sets an empty WM_PROTOCOLS property */
281 XSetWMProtocols(display, window.window, NULL, 0);
283 /* Set window type */
284 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
287 switch (window.type) {
289 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
290 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
294 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
295 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
300 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
301 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
305 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
306 PropModeReplace, (unsigned char *) &prop, 1);
309 /* Set desired hints */
311 /* Window decorations */
312 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
313 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
316 xa = ATOM(_MOTIF_WM_HINTS);
318 long prop[5] = { 2, 0, 0, 0, 0 };
319 XChangeProperty(display, window.window, xa, xa, 32,
320 PropModeReplace, (unsigned char *) prop, 5);
324 /* Below other windows */
325 if (TEST_HINT(window.hints, HINT_BELOW)) {
326 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
329 xa = ATOM(_WIN_LAYER);
333 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
334 PropModeAppend, (unsigned char *) &prop, 1);
337 xa = ATOM(_NET_WM_STATE);
339 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
341 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
342 PropModeAppend, (unsigned char *) &xa_prop, 1);
346 /* Above other windows */
347 if (TEST_HINT(window.hints, HINT_ABOVE)) {
348 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
351 xa = ATOM(_WIN_LAYER);
355 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
356 PropModeAppend, (unsigned char *) &prop, 1);
359 xa = ATOM(_NET_WM_STATE);
361 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
363 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
364 PropModeAppend, (unsigned char *) &xa_prop, 1);
369 if (TEST_HINT(window.hints, HINT_STICKY)) {
370 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
373 xa = ATOM(_NET_WM_DESKTOP);
375 CARD32 xa_prop = 0xFFFFFFFF;
377 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
378 PropModeAppend, (unsigned char *) &xa_prop, 1);
381 xa = ATOM(_NET_WM_STATE);
383 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
385 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
386 PropModeAppend, (unsigned char *) &xa_prop, 1);
391 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
392 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
395 xa = ATOM(_NET_WM_STATE);
397 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
399 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
400 PropModeAppend, (unsigned char *) &xa_prop, 1);
405 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
406 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
409 xa = ATOM(_NET_WM_STATE);
411 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
413 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
414 PropModeAppend, (unsigned char *) &xa_prop, 1);
417 } /* else { window.type != TYPE_OVERRIDE */
419 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
423 XMapWindow(display, window.window);
425 } else /* if (own_window) { */
427 /* root / desktop window */
429 XWindowAttributes attrs;
431 if (!window.window) {
432 window.window = find_desktop_window(&window.root, &window.desktop);
435 if (XGetWindowAttributes(display, window.window, &attrs)) {
436 window.width = attrs.width;
437 window.height = attrs.height;
440 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
443 /* Drawable is same as window. This may be changed by double buffering. */
444 window.drawable = window.window;
450 if (!XdbeQueryExtension(display, &major, &minor)) {
453 window.back_buffer = XdbeAllocateBackBufferName(display,
454 window.window, XdbeBackground);
455 if (window.back_buffer != None) {
456 window.drawable = window.back_buffer;
457 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
463 ERR("failed to set up double buffer");
467 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
474 visual = DefaultVisual(display, DefaultScreen(display));
475 colourmap = DefaultColormap(display, DefaultScreen(display));
476 cimlib_init(display, window.drawable, visual, colourmap);
481 /* set_transparent_background(window.window);
482 * must be done after double buffer stuff? */
485 set_transparent_background(window.window);
486 XClearWindow(display, window.window);
491 XSelectInput(display, window.window, ExposureMask |
492 (own_window ? (StructureNotifyMask | PropertyChangeMask |
493 ButtonPressMask | ButtonReleaseMask) : 0));
495 XSelectInput(display, window.window, ExposureMask);
499 static Window find_subwindow(Window win, int w, int h)
502 Window troot, parent, *children;
505 /* search subwindows with same size as display or work area */
507 for (i = 0; i < 10; i++) {
508 XQueryTree(display, win, &troot, &parent, &children, &n);
510 for (j = 0; j < n; j++) {
511 XWindowAttributes attrs;
513 if (XGetWindowAttributes(display, children[j], &attrs)) {
514 /* Window must be mapped and same size as display or
516 if (attrs.map_state != 0 && ((attrs.width == display_width
517 && attrs.height == display_height)
518 || (attrs.width == w && attrs.height == h))) {
534 long get_x11_color(const char *name)
539 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
540 /* lets check if it's a hex colour with the # missing in front
541 * if yes, then do something about it */
542 char newname[DEFAULT_TEXT_BUFFER_SIZE];
545 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
546 /* now lets try again */
547 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
549 ERR("can't parse X color '%s'", name);
553 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
554 ERR("can't allocate X color '%s'", name);
557 return (long) color.pixel;
564 values.graphics_exposures = 0;
565 values.function = GXcopy;
566 window.gc = XCreateGC(display, window.drawable,
567 GCFunction | GCGraphicsExposures, &values);
570 void update_x11info(void)
572 struct information *current_info = &info;
573 current_info->x11.monitor.number = XScreenCount(display);
574 current_info->x11.monitor.current = XDefaultScreen(display);