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>
30 #define WINDOW_NAME_FMT "%s - conky"
32 /* some basic X11 stuff */
37 static int set_transparent;
38 static int background_colour;
40 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
44 struct conky_window window;
46 /* local prototypes */
47 static void update_workarea();
48 static Window find_window_to_draw();
49 static Window find_subwindow(Window win, int w, int h);
54 if ((display = XOpenDisplay(0)) == NULL)
55 CRIT_ERR("can't open display: %s", XDisplayName(0));
57 screen = DefaultScreen(display);
58 display_width = DisplayWidth(display, screen);
59 display_height = DisplayHeight(display, screen);
64 static void update_workarea()
66 Window root = RootWindow(display, screen);
67 unsigned long nitems, bytes;
68 unsigned char *buf = NULL;
72 /* default work area is display */
75 workarea[2] = display_width;
76 workarea[3] = display_height;
78 /* get current desktop */
79 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
80 0, 1, False, XA_CARDINAL, &type, &format,
81 &nitems, &bytes, &buf) == Success
82 && type == XA_CARDINAL && nitems > 0) {
85 /* long desktop = * (long *) buf; */
98 static Window find_window_to_draw()
102 unsigned long nitems, bytes;
104 Window root = RootWindow(display, screen);
106 Window troot, parent, *children;
107 unsigned char *buf = NULL;
109 /* some window managers set __SWM_VROOT to some child of root window */
111 XQueryTree(display, root, &troot, &parent, &children, &n);
112 for (i = 0; i < (int) n; i++) {
113 if (XGetWindowProperty
114 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
115 XA_WINDOW, &type, &format, &nitems, &bytes,
116 &buf) == Success && type == XA_WINDOW) {
117 win = *(Window *) buf;
121 "Conky: drawing to window from __SWM_VROOT property\n");
132 /* get subwindows from root */
133 win = find_subwindow(root, -1, -1);
137 win = find_subwindow(win, workarea[2], workarea[3]);
146 "Conky: drawing to subwindow of root window (%lx)\n",
149 fprintf(stderr, "Conky: drawing to root window\n");
154 /* sets background to ParentRelative for the Window and all parents */
155 inline void set_transparent_background(Window win)
157 static int colour_set = -1;
158 if (set_transparent) {
161 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
165 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
167 XQueryTree(display, parent, &r, &parent, &children, &n);
170 } else if (colour_set != background_colour) {
171 XSetWindowBackground(display, win, background_colour);
172 colour_set = background_colour;
174 //XClearWindow(display, win); not sure why this was here
177 #if defined OWN_WINDOW
178 void init_window(int own_window, char* wm_class_name, int w, int h, int l, int fixed_pos, int set_trans, int back_colour, char * nodename)
180 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour, char * nodename)
183 /* There seems to be some problems with setting transparent background (on
184 * fluxbox this time). It doesn't happen always and I don't know why it
185 * happens but I bet the bug is somewhere here. */
186 set_transparent = set_trans;
187 background_colour = back_colour;
188 char * window_name = NULL;
192 /* looks like root pixmap isn't needed for anything */
194 XSetWindowAttributes attrs;
195 XClassHint class_hints;
198 attrs.background_pixel = get_x11_color("green");
201 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
202 CopyFromParent, /* class */
203 CopyFromParent, /* visual */
204 CWBackPixel, &attrs);
207 this doesn't work properly
209 wmhints.flags = StateHint;
210 wmhints.initial_state = WithdrawnState;
211 XSetWMHints(display, window.window, &wmhints);*/
215 class_hints.res_class = wm_class_name;
216 class_hints.res_name = wm_class_name;
217 XSetClassHint(display, window.window,
220 /*set_transparent_background(window.window);*/
221 window_name = (char *) malloc(strlen(WINDOW_NAME_FMT) + strlen(nodename)+1);
222 sprintf(window_name, WINDOW_NAME_FMT, nodename);
223 XStoreName(display, window.window, window_name);
225 XClearWindow(display, window.window);
228 XMoveWindow(display, window.window, window.x,
233 /* turn off decorations */
235 XInternAtom(display, "_MOTIF_WM_HINTS", True);
237 long prop[5] = { 2, 0, 0, 0, 0 };
238 XChangeProperty(display, window.window, a,
239 a, 32, PropModeReplace,
240 (unsigned char *) prop, 5);
243 /* set window sticky (to all desktops) */
244 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
246 long prop = 0xFFFFFFFF;
247 XChangeProperty(display, window.window, a,
250 (unsigned char *) &prop,
254 /* make sure the layer is on the bottom */
255 a = XInternAtom(display, "_WIN_LAYER", True);
258 XChangeProperty(display, window.window, a,
261 (unsigned char *) &prop, 1);
266 XMapWindow(display, window.window);
269 /* root / desktop window */
271 XWindowAttributes attrs;
274 window.window = find_window_to_draw();
276 if (XGetWindowAttributes(display, window.window, &attrs)) {
277 window.width = attrs.width;
278 window.height = attrs.height;
282 /* Drawable is same as window. This may be changed by double buffering. */
283 window.drawable = window.window;
288 if (!XdbeQueryExtension(display, &major, &minor)) {
292 XdbeAllocateBackBufferName(display,
295 if (window.back_buffer != None) {
296 window.drawable = window.back_buffer;
298 "Conky: drawing to double buffer\n");
303 ERR("failed to set up double buffer");
306 fprintf(stderr, "Conky: drawing to single buffer\n");
311 /*set_transparent_background(window.window); must be done after double buffer stuff? */
314 set_transparent_background(window.window);
315 XClearWindow(display, window.window);
319 XSelectInput(display, window.window, ExposureMask
322 ? (StructureNotifyMask | PropertyChangeMask) : 0)
327 static Window find_subwindow(Window win, int w, int h)
330 Window troot, parent, *children;
333 /* search subwindows with same size as display or work area */
335 for (i = 0; i < 10; i++) {
336 XQueryTree(display, win, &troot, &parent, &children, &n);
338 for (j = 0; j < n; j++) {
339 XWindowAttributes attrs;
341 if (XGetWindowAttributes
342 (display, children[j], &attrs)) {
343 /* Window must be mapped and same size as display or work space */
344 if (attrs.map_state != 0 &&
345 ((attrs.width == display_width
346 && attrs.height == display_height)
348 && attrs.height == h))) {
363 long get_x11_color(const char *name)
368 (display, DefaultColormap(display, screen), name, &color)) {
369 ERR("can't parse X color '%s'", name);
373 (display, DefaultColormap(display, screen), &color))
374 ERR("can't allocate X color '%s'", name);
376 return (long) color.pixel;
382 values.graphics_exposures = 0;
383 values.function = GXcopy;
384 window.gc = XCreateGC(display, window.drawable,
385 GCFunction | GCGraphicsExposures, &values);