2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
14 #include <X11/Xatom.h>
15 #include <X11/Xutil.h>
17 #include <X11/Xft/Xft.h>
28 /* some basic X11 stuff */
33 static int set_transparent;
34 static int background_colour;
36 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
40 struct conky_window window;
42 /* local prototypes */
43 static void update_workarea();
44 static Window find_window_to_draw();
45 static Window find_subwindow(Window win, int w, int h);
50 if ((display = XOpenDisplay(0)) == NULL)
51 CRIT_ERR("can't open display: %s", XDisplayName(0));
53 screen = DefaultScreen(display);
54 display_width = DisplayWidth(display, screen);
55 display_height = DisplayHeight(display, screen);
60 static void update_workarea()
62 Window root = RootWindow(display, screen);
63 unsigned long nitems, bytes;
64 unsigned char *buf = NULL;
68 /* default work area is display */
71 workarea[2] = display_width;
72 workarea[3] = display_height;
74 /* get current desktop */
75 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
76 0, 1, False, XA_CARDINAL, &type, &format,
77 &nitems, &bytes, &buf) == Success
78 && type == XA_CARDINAL && nitems > 0) {
81 /* long desktop = * (long *) buf; */
94 static Window find_window_to_draw()
98 unsigned long nitems, bytes;
100 Window root = RootWindow(display, screen);
102 Window troot, parent, *children;
103 unsigned char *buf = NULL;
105 /* some window managers set __SWM_VROOT to some child of root window */
107 XQueryTree(display, root, &troot, &parent, &children, &n);
108 for (i = 0; i < (int) n; i++) {
109 if (XGetWindowProperty
110 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
111 XA_WINDOW, &type, &format, &nitems, &bytes,
112 &buf) == Success && type == XA_WINDOW) {
113 win = *(Window *) buf;
117 "Conky: drawing to window from __SWM_VROOT property\n");
128 /* get subwindows from root */
129 win = find_subwindow(root, -1, -1);
133 win = find_subwindow(win, workarea[2], workarea[3]);
142 "Conky: drawing to subwindow of root window (%lx)\n",
145 fprintf(stderr, "Conky: drawing to root window\n");
150 /* sets background to ParentRelative for the Window and all parents */
151 inline void set_transparent_background(Window win)
153 static int colour_set = -1;
154 if (set_transparent) {
157 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
161 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
163 XQueryTree(display, parent, &r, &parent, &children, &n);
166 } else if (colour_set != background_colour) {
167 XSetWindowBackground(display, win, background_colour);
168 colour_set = background_colour;
170 //XClearWindow(display, win); not sure why this was here
173 #if defined OWN_WINDOW
174 void init_window(int own_window, int w, int h, int l, int fixed_pos, int set_trans, int back_colour)
176 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour)
179 /* There seems to be some problems with setting transparent background (on
180 * fluxbox this time). It doesn't happen always and I don't know why it
181 * happens but I bet the bug is somewhere here. */
182 set_transparent = set_trans;
183 background_colour = back_colour;
187 /* looks like root pixmap isn't needed for anything */
189 XSetWindowAttributes attrs;
190 XClassHint class_hints;
193 attrs.background_pixel = get_x11_color("green");
196 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
197 CopyFromParent, /* class */
198 CopyFromParent, /* visual */
199 CWBackPixel, &attrs);
202 wmhints.flags = StateHint;
203 wmhints.initial_state = WithdrawnState;
204 XSetWMHints(display, window.window, &wmhints);
208 class_hints.res_class = "conky";
209 class_hints.res_name = "conky";
210 XSetClassHint(display, window.window,
213 /*set_transparent_background(window.window);*/
215 XStoreName(display, window.window, "conky");
217 XClearWindow(display, window.window);
220 XMoveWindow(display, window.window, window.x,
225 /* turn off decorations */
227 XInternAtom(display, "_MOTIF_WM_HINTS", True);
229 long prop[5] = { 2, 0, 0, 0, 0 };
230 XChangeProperty(display, window.window, a,
231 a, 32, PropModeReplace,
232 (unsigned char *) prop, 5);
235 /* set window sticky (to all desktops) */
236 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
238 long prop = 0xFFFFFFFF;
239 XChangeProperty(display, window.window, a,
242 (unsigned char *) &prop,
246 /* make sure the layer is on the bottom */
247 a = XInternAtom(display, "_WIN_LAYER", True);
250 XChangeProperty(display, window.window, a,
253 (unsigned char *) &prop, 1);
258 XMapWindow(display, window.window);
261 /* root / desktop window */
263 XWindowAttributes attrs;
266 window.window = find_window_to_draw();
268 if (XGetWindowAttributes(display, window.window, &attrs)) {
269 window.width = attrs.width;
270 window.height = attrs.height;
274 /* Drawable is same as window. This may be changed by double buffering. */
275 window.drawable = window.window;
280 if (!XdbeQueryExtension(display, &major, &minor)) {
284 XdbeAllocateBackBufferName(display,
287 if (window.back_buffer != None) {
288 window.drawable = window.back_buffer;
290 "Conky: drawing to double buffer\n");
295 ERR("failed to set up double buffer");
298 fprintf(stderr, "Conky: drawing to single buffer\n");
303 /*set_transparent_background(window.window); must be done after double buffer stuff? */
306 set_transparent_background(window.window);
307 XClearWindow(display, window.window);
311 XSelectInput(display, window.window, ExposureMask
314 ? (StructureNotifyMask | PropertyChangeMask) : 0)
319 static Window find_subwindow(Window win, int w, int h)
322 Window troot, parent, *children;
325 /* search subwindows with same size as display or work area */
327 for (i = 0; i < 10; i++) {
328 XQueryTree(display, win, &troot, &parent, &children, &n);
330 for (j = 0; j < n; j++) {
331 XWindowAttributes attrs;
333 if (XGetWindowAttributes
334 (display, children[j], &attrs)) {
335 /* Window must be mapped and same size as display or work space */
336 if (attrs.map_state != 0 &&
337 ((attrs.width == display_width
338 && attrs.height == display_height)
340 && attrs.height == h))) {
355 long get_x11_color(const char *name)
360 (display, DefaultColormap(display, screen), name, &color)) {
361 ERR("can't parse X color '%s'", name);
365 (display, DefaultColormap(display, screen), &color))
366 ERR("can't allocate X color '%s'", name);
368 return (long) color.pixel;
374 values.graphics_exposures = 0;
375 values.function = GXcopy;
376 window.gc = XCreateGC(display, window.drawable,
377 GCFunction | GCGraphicsExposures, &values);