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 */
52 Display *display = NULL;
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)
74 if ((display = XOpenDisplay(disp)) == NULL) {
75 CRIT_ERR("can't open display: %s", XDisplayName(0));
79 screen = DefaultScreen(display);
80 display_width = DisplayWidth(display, screen);
81 display_height = DisplayHeight(display, screen);
86 static void update_workarea(void)
88 Window root = RootWindow(display, screen);
89 unsigned long nitems, bytes;
90 unsigned char *buf = NULL;
94 /* default work area is display */
97 workarea[2] = display_width;
98 workarea[3] = display_height;
100 /* get current desktop */
101 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
102 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
103 == Success && type == XA_CARDINAL && nitems > 0) {
106 /* long desktop = *(long *) buf; */
118 /* Find root window and desktop window.
119 * Return desktop window on success,
120 * and set root and desktop byref return values.
121 * Return 0 on failure. */
122 static Window find_desktop_window(Window *p_root, Window *p_desktop)
126 unsigned long nitems, bytes;
128 Window root = RootWindow(display, screen);
130 Window troot, parent, *children;
131 unsigned char *buf = NULL;
133 if (!p_root || !p_desktop) {
137 /* some window managers set __SWM_VROOT to some child of root window */
139 XQueryTree(display, root, &troot, &parent, &children, &n);
140 for (i = 0; i < (int) n; i++) {
141 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
142 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
143 == Success && type == XA_WINDOW) {
144 win = *(Window *) buf;
148 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
163 /* get subwindows from root */
164 win = find_subwindow(root, -1, -1);
168 win = find_subwindow(win, workarea[2], workarea[3]);
177 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
180 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
191 /* sets background to ParentRelative for the Window and all parents */
192 void set_transparent_background(Window win)
194 static int colour_set = -1;
196 if (set_transparent) {
200 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
204 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
206 XQueryTree(display, parent, &r, &parent, &children, &n);
209 } else if (colour_set != background_colour) {
210 XSetWindowBackground(display, win, background_colour);
211 colour_set = background_colour;
213 // XClearWindow(display, win); not sure why this was here
216 void destroy_window(void)
218 XDestroyWindow(display, window.window);
219 memset(&window, 0, sizeof(struct conky_window));
222 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
223 char **argv, int argc)
225 /* There seems to be some problems with setting transparent background
226 * (on fluxbox this time). It doesn't happen always and I don't know why it
227 * happens but I bet the bug is somewhere here. */
228 set_transparent = set_trans;
229 background_colour = back_colour;
233 if (!find_desktop_window(&window.root, &window.desktop)) {
237 if (window.type == TYPE_OVERRIDE) {
239 /* An override_redirect True window.
240 * No WM hints or button processing needed. */
241 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
242 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
245 /* Parent is desktop window (which might be a child of root) */
246 window.window = XCreateWindow(display, window.desktop, window.x,
247 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
248 CWBackPixel | CWOverrideRedirect, &attrs);
250 XLowerWindow(display, window.window);
252 fprintf(stderr, PACKAGE_NAME": window type - override\n");
254 } else { /* window.type != TYPE_OVERRIDE */
256 /* A window managed by the window manager.
257 * Process hints and buttons. */
258 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
259 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
260 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
262 XClassHint classHint;
266 if (window.type == TYPE_DOCK) {
267 window.x = window.y = 0;
269 /* Parent is root window so WM can take control */
270 window.window = XCreateWindow(display, window.root, window.x,
271 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
272 CWBackPixel | CWOverrideRedirect, &attrs);
274 classHint.res_name = window.class_name;
275 classHint.res_class = classHint.res_name;
277 wmHint.flags = InputHint | StateHint;
278 /* allow decorated windows to be given input focus by WM */
280 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
281 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
282 WithdrawnState : NormalState);
284 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
285 argc, NULL, &wmHint, &classHint);
287 /* Sets an empty WM_PROTOCOLS property */
288 XSetWMProtocols(display, window.window, NULL, 0);
290 /* Set window type */
291 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
294 switch (window.type) {
296 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
297 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
301 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
302 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
307 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
308 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
312 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
313 PropModeReplace, (unsigned char *) &prop, 1);
316 /* Set desired hints */
318 /* Window decorations */
319 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
320 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
323 xa = ATOM(_MOTIF_WM_HINTS);
325 long prop[5] = { 2, 0, 0, 0, 0 };
326 XChangeProperty(display, window.window, xa, xa, 32,
327 PropModeReplace, (unsigned char *) prop, 5);
331 /* Below other windows */
332 if (TEST_HINT(window.hints, HINT_BELOW)) {
333 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
336 xa = ATOM(_WIN_LAYER);
340 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
341 PropModeAppend, (unsigned char *) &prop, 1);
344 xa = ATOM(_NET_WM_STATE);
346 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
348 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
349 PropModeAppend, (unsigned char *) &xa_prop, 1);
353 /* Above other windows */
354 if (TEST_HINT(window.hints, HINT_ABOVE)) {
355 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
358 xa = ATOM(_WIN_LAYER);
362 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
363 PropModeAppend, (unsigned char *) &prop, 1);
366 xa = ATOM(_NET_WM_STATE);
368 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
370 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
371 PropModeAppend, (unsigned char *) &xa_prop, 1);
376 if (TEST_HINT(window.hints, HINT_STICKY)) {
377 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
380 xa = ATOM(_NET_WM_DESKTOP);
382 CARD32 xa_prop = 0xFFFFFFFF;
384 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
385 PropModeAppend, (unsigned char *) &xa_prop, 1);
388 xa = ATOM(_NET_WM_STATE);
390 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
392 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
393 PropModeAppend, (unsigned char *) &xa_prop, 1);
398 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
399 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
402 xa = ATOM(_NET_WM_STATE);
404 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
406 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
407 PropModeAppend, (unsigned char *) &xa_prop, 1);
412 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
413 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
416 xa = ATOM(_NET_WM_STATE);
418 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
420 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
421 PropModeAppend, (unsigned char *) &xa_prop, 1);
424 } /* else { window.type != TYPE_OVERRIDE */
426 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
430 XMapWindow(display, window.window);
432 } else /* if (own_window) { */
434 /* root / desktop window */
436 XWindowAttributes attrs;
438 if (!window.window) {
439 window.window = find_desktop_window(&window.root, &window.desktop);
442 if (XGetWindowAttributes(display, window.window, &attrs)) {
443 window.width = attrs.width;
444 window.height = attrs.height;
447 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
450 /* Drawable is same as window. This may be changed by double buffering. */
451 window.drawable = window.window;
457 if (!XdbeQueryExtension(display, &major, &minor)) {
460 window.back_buffer = XdbeAllocateBackBufferName(display,
461 window.window, XdbeBackground);
462 if (window.back_buffer != None) {
463 window.drawable = window.back_buffer;
464 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
470 ERR("failed to set up double buffer");
474 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
481 visual = DefaultVisual(display, DefaultScreen(display));
482 colourmap = DefaultColormap(display, DefaultScreen(display));
483 cimlib_init(display, window.drawable, visual, colourmap);
488 /* set_transparent_background(window.window);
489 * must be done after double buffer stuff? */
492 set_transparent_background(window.window);
493 XClearWindow(display, window.window);
497 XSelectInput(display, window.window, ExposureMask
499 | (own_window ? (StructureNotifyMask | PropertyChangeMask |
500 ButtonPressMask | ButtonReleaseMask) : 0)
505 static Window find_subwindow(Window win, int w, int h)
508 Window troot, parent, *children;
511 /* search subwindows with same size as display or work area */
513 for (i = 0; i < 10; i++) {
514 XQueryTree(display, win, &troot, &parent, &children, &n);
516 for (j = 0; j < n; j++) {
517 XWindowAttributes attrs;
519 if (XGetWindowAttributes(display, children[j], &attrs)) {
520 /* Window must be mapped and same size as display or
522 if (attrs.map_state != 0 && ((attrs.width == display_width
523 && attrs.height == display_height)
524 || (attrs.width == w && attrs.height == h))) {
540 long get_x11_color(const char *name)
545 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
546 /* lets check if it's a hex colour with the # missing in front
547 * if yes, then do something about it */
548 char newname[DEFAULT_TEXT_BUFFER_SIZE];
551 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
552 /* now lets try again */
553 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
555 ERR("can't parse X color '%s'", name);
559 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
560 ERR("can't allocate X color '%s'", name);
563 return (long) color.pixel;
570 values.graphics_exposures = 0;
571 values.function = GXcopy;
572 window.gc = XCreateGC(display, window.drawable,
573 GCFunction | GCGraphicsExposures, &values);
576 void update_x11info(void)
578 struct information *current_info = &info;
579 current_info->x11.monitor.number = XScreenCount(display);
580 current_info->x11.monitor.current = XDefaultScreen(display);