2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
11 #include <X11/Xatom.h>
12 #include <X11/Xutil.h>
14 #include <X11/Xft/Xft.h>
25 /* some basic X11 stuff */
30 static int set_transparent;
31 static int background_colour;
33 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
37 struct conky_window window;
39 /* local prototypes */
40 static void update_workarea();
41 static Window find_window_to_draw();
42 static Window find_subwindow(Window win, int w, int h);
47 if ((display = XOpenDisplay(0)) == NULL)
48 CRIT_ERR("can't open display: %s", XDisplayName(0));
50 screen = DefaultScreen(display);
51 display_width = DisplayWidth(display, screen);
52 display_height = DisplayHeight(display, screen);
57 static void update_workarea()
59 Window root = RootWindow(display, screen);
60 unsigned long nitems, bytes;
61 unsigned char *buf = NULL;
65 /* default work area is display */
68 workarea[2] = display_width;
69 workarea[3] = display_height;
71 /* get current desktop */
72 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
73 0, 1, False, XA_CARDINAL, &type, &format,
74 &nitems, &bytes, &buf) == Success
75 && type == XA_CARDINAL && nitems > 0) {
78 /* long desktop = * (long *) buf; */
91 static Window find_window_to_draw()
95 unsigned long nitems, bytes;
97 Window root = RootWindow(display, screen);
99 Window troot, parent, *children;
100 unsigned char *buf = NULL;
102 /* some window managers set __SWM_VROOT to some child of root window */
104 XQueryTree(display, root, &troot, &parent, &children, &n);
105 for (i = 0; i < (int) n; i++) {
106 if (XGetWindowProperty
107 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
108 XA_WINDOW, &type, &format, &nitems, &bytes,
109 &buf) == Success && type == XA_WINDOW) {
110 win = *(Window *) buf;
114 "Conky: drawing to window from __SWM_VROOT property\n");
125 /* get subwindows from root */
126 win = find_subwindow(root, -1, -1);
130 win = find_subwindow(win, workarea[2], workarea[3]);
139 "Conky: drawing to subwindow of root window (%lx)\n",
142 fprintf(stderr, "Conky: drawing to root window\n");
147 /* sets background to ParentRelative for the Window and all parents */
148 inline void set_transparent_background(Window win)
150 static int colour_set = -1;
151 if (set_transparent) {
154 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
158 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
160 XQueryTree(display, parent, &r, &parent, &children, &n);
163 } else if (colour_set != background_colour) {
164 XSetWindowBackground(display, win, background_colour);
165 colour_set = background_colour;
167 //XClearWindow(display, win); not sure why this was here
170 #if defined OWN_WINDOW
171 void init_window(int own_window, int w, int h, int l, int fixed_pos, int set_trans, int back_colour)
173 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour)
176 /* There seems to be some problems with setting transparent background (on
177 * fluxbox this time). It doesn't happen always and I don't know why it
178 * happens but I bet the bug is somewhere here. */
179 set_transparent = set_trans;
180 background_colour = back_colour;
184 /* looks like root pixmap isn't needed for anything */
186 XSetWindowAttributes attrs;
187 XClassHint class_hints;
190 attrs.background_pixel = get_x11_color("green");
193 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
194 CopyFromParent, /* class */
195 CopyFromParent, /* visual */
196 CWBackPixel, &attrs);
198 class_hints.res_class = "conky";
199 class_hints.res_name = "conky";
200 XSetClassHint(display, window.window,
203 /*set_transparent_background(window.window);*/
205 XStoreName(display, window.window, "conky");
207 XClearWindow(display, window.window);
210 XMoveWindow(display, window.window, window.x,
215 /* turn off decorations */
217 XInternAtom(display, "_MOTIF_WM_HINTS", True);
219 long prop[5] = { 2, 0, 0, 0, 0 };
220 XChangeProperty(display, window.window, a,
221 a, 32, PropModeReplace,
222 (unsigned char *) prop, 5);
225 /* set window sticky (to all desktops) */
226 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
228 long prop = 0xFFFFFFFF;
229 XChangeProperty(display, window.window, a,
232 (unsigned char *) &prop,
236 /* make sure the layer is on the bottom */
237 a = XInternAtom(display, "_WIN_LAYER", True);
240 XChangeProperty(display, window.window, a,
243 (unsigned char *) &prop, 1);
248 XMapWindow(display, window.window);
251 /* root / desktop window */
253 XWindowAttributes attrs;
256 window.window = find_window_to_draw();
258 if (XGetWindowAttributes(display, window.window, &attrs)) {
259 window.width = attrs.width;
260 window.height = attrs.height;
264 /* Drawable is same as window. This may be changed by double buffering. */
265 window.drawable = window.window;
270 if (!XdbeQueryExtension(display, &major, &minor)) {
274 XdbeAllocateBackBufferName(display,
277 if (window.back_buffer != None) {
278 window.drawable = window.back_buffer;
280 "Conky: drawing to double buffer\n");
285 ERR("failed to set up double buffer");
288 fprintf(stderr, "Conky: drawing to single buffer\n");
293 /*set_transparent_background(window.window); must be done after double buffer stuff? */
296 set_transparent_background(window.window);
297 XClearWindow(display, window.window);
301 XSelectInput(display, window.window, ExposureMask
304 ? (StructureNotifyMask | PropertyChangeMask) : 0)
309 static Window find_subwindow(Window win, int w, int h)
312 Window troot, parent, *children;
315 /* search subwindows with same size as display or work area */
317 for (i = 0; i < 10; i++) {
318 XQueryTree(display, win, &troot, &parent, &children, &n);
320 for (j = 0; j < n; j++) {
321 XWindowAttributes attrs;
323 if (XGetWindowAttributes
324 (display, children[j], &attrs)) {
325 /* Window must be mapped and same size as display or work space */
326 if (attrs.map_state != 0 &&
327 ((attrs.width == display_width
328 && attrs.height == display_height)
330 && attrs.height == h))) {
345 long get_x11_color(const char *name)
350 (display, DefaultColormap(display, screen), name, &color)) {
351 ERR("can't parse X color '%s'", name);
355 (display, DefaultColormap(display, screen), &color))
356 ERR("can't allocate X color '%s'", name);
358 return (long) color.pixel;
364 values.graphics_exposures = 0;
365 values.function = GXcopy;
366 window.gc = XCreateGC(display, window.drawable,
367 GCFunction | GCGraphicsExposures, &values);