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 */
61 static int set_transparent;
62 static int background_colour;
64 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
68 struct conky_window window;
70 /* local prototypes */
71 static void update_workarea(void);
72 static Window find_desktop_window(Window *p_root, Window *p_desktop);
73 static Window find_subwindow(Window win, int w, int h);
76 void init_X11(const char *disp)
78 if ((display = XOpenDisplay(disp)) == NULL) {
79 CRIT_ERR("can't open display: %s", XDisplayName(0));
82 screen = DefaultScreen(display);
83 display_width = DisplayWidth(display, screen);
84 display_height = DisplayHeight(display, screen);
89 static void update_workarea(void)
91 Window root = RootWindow(display, screen);
92 unsigned long nitems, bytes;
93 unsigned char *buf = NULL;
97 /* default work area is display */
100 workarea[2] = display_width;
101 workarea[3] = display_height;
103 /* get current desktop */
104 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
105 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
106 == Success && type == XA_CARDINAL && nitems > 0) {
109 /* long desktop = *(long *) buf; */
121 /* Find root window and desktop window.
122 * Return desktop window on success,
123 * and set root and desktop byref return values.
124 * Return 0 on failure. */
125 static Window find_desktop_window(Window *p_root, Window *p_desktop)
129 unsigned long nitems, bytes;
131 Window root = RootWindow(display, screen);
133 Window troot, parent, *children;
134 unsigned char *buf = NULL;
136 if (!p_root || !p_desktop) {
140 /* some window managers set __SWM_VROOT to some child of root window */
142 XQueryTree(display, root, &troot, &parent, &children, &n);
143 for (i = 0; i < (int) n; i++) {
144 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
145 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
146 == Success && type == XA_WINDOW) {
147 win = *(Window *) buf;
151 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
166 /* get subwindows from root */
167 win = find_subwindow(root, -1, -1);
171 win = find_subwindow(win, workarea[2], workarea[3]);
180 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
183 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
194 /* sets background to ParentRelative for the Window and all parents */
195 void set_transparent_background(Window win)
197 static int colour_set = -1;
199 if (set_transparent) {
203 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
207 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
209 XQueryTree(display, parent, &r, &parent, &children, &n);
212 } else if (colour_set != background_colour) {
213 XSetWindowBackground(display, win, background_colour);
214 colour_set = background_colour;
216 // XClearWindow(display, win); not sure why this was here
219 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
220 char **argv, int argc)
222 /* There seems to be some problems with setting transparent background
223 * (on fluxbox this time). It doesn't happen always and I don't know why it
224 * happens but I bet the bug is somewhere here. */
225 set_transparent = set_trans;
226 background_colour = back_colour;
231 if (!find_desktop_window(&window.root, &window.desktop)) {
235 if (window.type == TYPE_OVERRIDE) {
237 /* An override_redirect True window.
238 * No WM hints or button processing needed. */
239 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
240 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
243 /* Parent is desktop window (which might be a child of root) */
244 window.window = XCreateWindow(display, window.desktop, window.x,
245 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
246 CWBackPixel | CWOverrideRedirect, &attrs);
248 XLowerWindow(display, window.window);
250 fprintf(stderr, PACKAGE_NAME": window type - override\n");
252 } else { /* window.type != TYPE_OVERRIDE */
254 /* A window managed by the window manager.
255 * Process hints and buttons. */
256 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
257 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
258 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
260 XClassHint classHint;
264 if (window.type == TYPE_DOCK) {
265 window.x = window.y = 0;
267 /* Parent is root window so WM can take control */
268 window.window = XCreateWindow(display, window.root, window.x,
269 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
270 CWBackPixel | CWOverrideRedirect, &attrs);
272 classHint.res_name = window.class_name;
273 classHint.res_class = classHint.res_name;
275 wmHint.flags = InputHint | StateHint;
276 /* allow decorated windows to be given input focus by WM */
278 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
279 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
280 WithdrawnState : NormalState);
282 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
283 argc, NULL, &wmHint, &classHint);
285 /* Sets an empty WM_PROTOCOLS property */
286 XSetWMProtocols(display, window.window, NULL, 0);
288 /* Set window type */
289 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
292 switch (window.type) {
294 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
295 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
299 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
300 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
305 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
306 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
310 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
311 PropModeReplace, (unsigned char *) &prop, 1);
314 /* Set desired hints */
316 /* Window decorations */
317 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
318 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
321 xa = ATOM(_MOTIF_WM_HINTS);
323 long prop[5] = { 2, 0, 0, 0, 0 };
324 XChangeProperty(display, window.window, xa, xa, 32,
325 PropModeReplace, (unsigned char *) prop, 5);
329 /* Below other windows */
330 if (TEST_HINT(window.hints, HINT_BELOW)) {
331 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
334 xa = ATOM(_WIN_LAYER);
338 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
339 PropModeAppend, (unsigned char *) &prop, 1);
342 xa = ATOM(_NET_WM_STATE);
344 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
346 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
347 PropModeAppend, (unsigned char *) &xa_prop, 1);
351 /* Above other windows */
352 if (TEST_HINT(window.hints, HINT_ABOVE)) {
353 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
356 xa = ATOM(_WIN_LAYER);
360 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
361 PropModeAppend, (unsigned char *) &prop, 1);
364 xa = ATOM(_NET_WM_STATE);
366 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
368 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
369 PropModeAppend, (unsigned char *) &xa_prop, 1);
374 if (TEST_HINT(window.hints, HINT_STICKY)) {
375 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
378 xa = ATOM(_NET_WM_DESKTOP);
380 CARD32 xa_prop = 0xFFFFFFFF;
382 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
383 PropModeAppend, (unsigned char *) &xa_prop, 1);
386 xa = ATOM(_NET_WM_STATE);
388 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
390 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
391 PropModeAppend, (unsigned char *) &xa_prop, 1);
396 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
397 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
400 xa = ATOM(_NET_WM_STATE);
402 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
404 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
405 PropModeAppend, (unsigned char *) &xa_prop, 1);
410 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
411 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
414 xa = ATOM(_NET_WM_STATE);
416 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
418 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
419 PropModeAppend, (unsigned char *) &xa_prop, 1);
422 } /* else { window.type != TYPE_OVERRIDE */
424 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
428 XMapWindow(display, window.window);
430 } else /* if (own_window) { */
432 /* root / desktop window */
434 XWindowAttributes attrs;
436 if (!window.window) {
437 window.window = find_desktop_window(&window.root, &window.desktop);
440 if (XGetWindowAttributes(display, window.window, &attrs)) {
441 window.width = attrs.width;
442 window.height = attrs.height;
445 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
448 /* Drawable is same as window. This may be changed by double buffering. */
449 window.drawable = window.window;
455 if (!XdbeQueryExtension(display, &major, &minor)) {
458 window.back_buffer = XdbeAllocateBackBufferName(display,
459 window.window, XdbeBackground);
460 if (window.back_buffer != None) {
461 window.drawable = window.back_buffer;
462 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
468 ERR("failed to set up double buffer");
472 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
476 visual = DefaultVisual(display, DefaultScreen(display));
477 depth = DefaultDepth(display, DefaultScreen(display));
478 colourmap = DefaultColormap(display, DefaultScreen(display));
479 /* set our cache to 4MiB so it doesn't have to go hit the disk as long as */
480 /* the images we use use less than 4MiB of RAM (that is uncompressed) */
481 imlib_set_cache_size(4092 * 1024);
482 /* set the font cache to 512KiB - again to avoid re-loading */
483 imlib_set_font_cache_size(512 * 1024);
484 /* set the maximum number of colors to allocate for 8bpp and less to 128 */
485 imlib_set_color_usage(128);
486 /* dither for depths < 24bpp */
487 imlib_context_set_dither(1);
488 /* set the display , visual, colormap and drawable we are using */
489 imlib_context_set_display(display);
490 imlib_context_set_visual(visual);
491 imlib_context_set_colormap(colourmap);
492 imlib_context_set_drawable(window.drawable);
496 /* set_transparent_background(window.window);
497 * must be done after double buffer stuff? */
500 set_transparent_background(window.window);
501 XClearWindow(display, window.window);
506 XSelectInput(display, window.window, ExposureMask |
507 (own_window ? (StructureNotifyMask | PropertyChangeMask |
508 ButtonPressMask | ButtonReleaseMask) : 0));
510 XSelectInput(display, window.window, ExposureMask);
514 static Window find_subwindow(Window win, int w, int h)
517 Window troot, parent, *children;
520 /* search subwindows with same size as display or work area */
522 for (i = 0; i < 10; i++) {
523 XQueryTree(display, win, &troot, &parent, &children, &n);
525 for (j = 0; j < n; j++) {
526 XWindowAttributes attrs;
528 if (XGetWindowAttributes(display, children[j], &attrs)) {
529 /* Window must be mapped and same size as display or
531 if (attrs.map_state != 0 && ((attrs.width == display_width
532 && attrs.height == display_height)
533 || (attrs.width == w && attrs.height == h))) {
549 long get_x11_color(const char *name)
554 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
555 /* lets check if it's a hex colour with the # missing in front
556 * if yes, then do something about it */
557 char newname[DEFAULT_TEXT_BUFFER_SIZE];
560 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
561 /* now lets try again */
562 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
564 ERR("can't parse X color '%s'", name);
568 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
569 ERR("can't allocate X color '%s'", name);
572 return (long) color.pixel;
579 values.graphics_exposures = 0;
580 values.function = GXcopy;
581 window.gc = XCreateGC(display, window.drawable,
582 GCFunction | GCGraphicsExposures, &values);
585 void update_x11info(void)
587 struct information *current_info = &info;
588 current_info->x11.monitor.number = XScreenCount(display);
589 current_info->x11.monitor.current = XDefaultScreen(display);