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 XCloseDisplay(display);
76 if ((display = XOpenDisplay(disp)) == NULL) {
77 CRIT_ERR("can't open display: %s", XDisplayName(0));
80 screen = DefaultScreen(display);
81 display_width = DisplayWidth(display, screen);
82 display_height = DisplayHeight(display, screen);
87 static void update_workarea(void)
89 Window root = RootWindow(display, screen);
90 unsigned long nitems, bytes;
91 unsigned char *buf = NULL;
95 /* default work area is display */
98 workarea[2] = display_width;
99 workarea[3] = display_height;
101 /* get current desktop */
102 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP), 0, 1,
103 False, XA_CARDINAL, &type, &format, &nitems, &bytes, &buf)
104 == Success && type == XA_CARDINAL && nitems > 0) {
107 /* long desktop = *(long *) buf; */
119 /* Find root window and desktop window.
120 * Return desktop window on success,
121 * and set root and desktop byref return values.
122 * Return 0 on failure. */
123 static Window find_desktop_window(Window *p_root, Window *p_desktop)
127 unsigned long nitems, bytes;
129 Window root = RootWindow(display, screen);
131 Window troot, parent, *children;
132 unsigned char *buf = NULL;
134 if (!p_root || !p_desktop) {
138 /* some window managers set __SWM_VROOT to some child of root window */
140 XQueryTree(display, root, &troot, &parent, &children, &n);
141 for (i = 0; i < (int) n; i++) {
142 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
143 False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
144 == Success && type == XA_WINDOW) {
145 win = *(Window *) buf;
149 PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
164 /* get subwindows from root */
165 win = find_subwindow(root, -1, -1);
169 win = find_subwindow(win, workarea[2], workarea[3]);
178 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
181 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
192 /* sets background to ParentRelative for the Window and all parents */
193 void set_transparent_background(Window win)
195 static int colour_set = -1;
197 if (set_transparent) {
201 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
205 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
207 XQueryTree(display, parent, &r, &parent, &children, &n);
210 } else if (colour_set != background_colour) {
211 XSetWindowBackground(display, win, background_colour);
212 colour_set = background_colour;
214 // XClearWindow(display, win); not sure why this was here
217 void destroy_window(void)
219 XDestroyWindow(display, window.window);
220 memset(&window, 0, sizeof(struct conky_window));
223 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
224 char **argv, int argc)
226 /* There seems to be some problems with setting transparent background
227 * (on fluxbox this time). It doesn't happen always and I don't know why it
228 * happens but I bet the bug is somewhere here. */
229 set_transparent = set_trans;
230 background_colour = back_colour;
235 if (!find_desktop_window(&window.root, &window.desktop)) {
239 if (window.type == TYPE_OVERRIDE) {
241 /* An override_redirect True window.
242 * No WM hints or button processing needed. */
243 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
244 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
247 /* Parent is desktop window (which might be a child of root) */
248 window.window = XCreateWindow(display, window.desktop, window.x,
249 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
250 CWBackPixel | CWOverrideRedirect, &attrs);
252 XLowerWindow(display, window.window);
254 fprintf(stderr, PACKAGE_NAME": window type - override\n");
256 } else { /* window.type != TYPE_OVERRIDE */
258 /* A window managed by the window manager.
259 * Process hints and buttons. */
260 XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
261 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
262 ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
264 XClassHint classHint;
268 if (window.type == TYPE_DOCK) {
269 window.x = window.y = 0;
271 /* Parent is root window so WM can take control */
272 window.window = XCreateWindow(display, window.root, window.x,
273 window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
274 CWBackPixel | CWOverrideRedirect, &attrs);
276 classHint.res_name = window.class_name;
277 classHint.res_class = classHint.res_name;
279 wmHint.flags = InputHint | StateHint;
280 /* allow decorated windows to be given input focus by WM */
282 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
283 wmHint.initial_state = ((window.type == TYPE_DOCK) ?
284 WithdrawnState : NormalState);
286 XmbSetWMProperties(display, window.window, window.title, NULL, argv,
287 argc, NULL, &wmHint, &classHint);
289 /* Sets an empty WM_PROTOCOLS property */
290 XSetWMProtocols(display, window.window, NULL, 0);
292 /* Set window type */
293 if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
296 switch (window.type) {
298 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
299 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
303 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
304 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
309 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
310 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
314 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
315 PropModeReplace, (unsigned char *) &prop, 1);
318 /* Set desired hints */
320 /* Window decorations */
321 if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
322 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
325 xa = ATOM(_MOTIF_WM_HINTS);
327 long prop[5] = { 2, 0, 0, 0, 0 };
328 XChangeProperty(display, window.window, xa, xa, 32,
329 PropModeReplace, (unsigned char *) prop, 5);
333 /* Below other windows */
334 if (TEST_HINT(window.hints, HINT_BELOW)) {
335 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
338 xa = ATOM(_WIN_LAYER);
342 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
343 PropModeAppend, (unsigned char *) &prop, 1);
346 xa = ATOM(_NET_WM_STATE);
348 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
350 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
351 PropModeAppend, (unsigned char *) &xa_prop, 1);
355 /* Above other windows */
356 if (TEST_HINT(window.hints, HINT_ABOVE)) {
357 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
360 xa = ATOM(_WIN_LAYER);
364 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
365 PropModeAppend, (unsigned char *) &prop, 1);
368 xa = ATOM(_NET_WM_STATE);
370 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
372 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
373 PropModeAppend, (unsigned char *) &xa_prop, 1);
378 if (TEST_HINT(window.hints, HINT_STICKY)) {
379 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
382 xa = ATOM(_NET_WM_DESKTOP);
384 CARD32 xa_prop = 0xFFFFFFFF;
386 XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
387 PropModeAppend, (unsigned char *) &xa_prop, 1);
390 xa = ATOM(_NET_WM_STATE);
392 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
394 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
395 PropModeAppend, (unsigned char *) &xa_prop, 1);
400 if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
401 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
404 xa = ATOM(_NET_WM_STATE);
406 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
408 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
409 PropModeAppend, (unsigned char *) &xa_prop, 1);
414 if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
415 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
418 xa = ATOM(_NET_WM_STATE);
420 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
422 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
423 PropModeAppend, (unsigned char *) &xa_prop, 1);
426 } /* else { window.type != TYPE_OVERRIDE */
428 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
432 XMapWindow(display, window.window);
434 } else /* if (own_window) { */
436 /* root / desktop window */
438 XWindowAttributes attrs;
440 if (!window.window) {
441 window.window = find_desktop_window(&window.root, &window.desktop);
444 if (XGetWindowAttributes(display, window.window, &attrs)) {
445 window.width = attrs.width;
446 window.height = attrs.height;
449 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
452 /* Drawable is same as window. This may be changed by double buffering. */
453 window.drawable = window.window;
459 if (!XdbeQueryExtension(display, &major, &minor)) {
462 window.back_buffer = XdbeAllocateBackBufferName(display,
463 window.window, XdbeBackground);
464 if (window.back_buffer != None) {
465 window.drawable = window.back_buffer;
466 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
472 ERR("failed to set up double buffer");
476 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
483 visual = DefaultVisual(display, DefaultScreen(display));
484 colourmap = DefaultColormap(display, DefaultScreen(display));
485 cimlib_init(display, window.drawable, visual, colourmap);
490 /* set_transparent_background(window.window);
491 * must be done after double buffer stuff? */
494 set_transparent_background(window.window);
495 XClearWindow(display, window.window);
500 XSelectInput(display, window.window, ExposureMask |
501 (own_window ? (StructureNotifyMask | PropertyChangeMask |
502 ButtonPressMask | ButtonReleaseMask) : 0));
504 XSelectInput(display, window.window, ExposureMask);
508 static Window find_subwindow(Window win, int w, int h)
511 Window troot, parent, *children;
514 /* search subwindows with same size as display or work area */
516 for (i = 0; i < 10; i++) {
517 XQueryTree(display, win, &troot, &parent, &children, &n);
519 for (j = 0; j < n; j++) {
520 XWindowAttributes attrs;
522 if (XGetWindowAttributes(display, children[j], &attrs)) {
523 /* Window must be mapped and same size as display or
525 if (attrs.map_state != 0 && ((attrs.width == display_width
526 && attrs.height == display_height)
527 || (attrs.width == w && attrs.height == h))) {
543 long get_x11_color(const char *name)
548 if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
549 /* lets check if it's a hex colour with the # missing in front
550 * if yes, then do something about it */
551 char newname[DEFAULT_TEXT_BUFFER_SIZE];
554 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
555 /* now lets try again */
556 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
558 ERR("can't parse X color '%s'", name);
562 if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
563 ERR("can't allocate X color '%s'", name);
566 return (long) color.pixel;
573 values.graphics_exposures = 0;
574 values.function = GXcopy;
575 window.gc = XCreateGC(display, window.drawable,
576 GCFunction | GCGraphicsExposures, &values);
579 void update_x11info(void)
581 struct information *current_info = &info;
582 current_info->x11.monitor.number = XScreenCount(display);
583 current_info->x11.monitor.current = XDefaultScreen(display);