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 void set_transparent_background(Window win)
150 if (set_transparent) {
153 for (i = 0; i < 16 && parent != RootWindow(display, screen); i++) {
157 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
159 XQueryTree(display, parent, &r, &parent, &children, &n);
163 XSetWindowBackground(display, win, background_colour);
165 XClearWindow(display, win);
168 #if defined OWN_WINDOW
169 void init_window(int own_window, int w, int h, int l, int fixed_pos, int set_trans, int back_colour)
171 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour)
174 /* There seems to be some problems with setting transparent background (on
175 * fluxbox this time). It doesn't happen always and I don't know why it
176 * happens but I bet the bug is somewhere here. */
177 set_transparent = set_trans;
178 background_colour = back_colour;
182 /* looks like root pixmap isn't needed for anything */
184 XSetWindowAttributes attrs;
185 XClassHint class_hints;
188 attrs.background_pixel = get_x11_color("green");
191 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
192 CopyFromParent, /* class */
193 CopyFromParent, /* visual */
194 CWBackPixel, &attrs);
196 class_hints.res_class = "conky";
197 class_hints.res_name = "conky";
198 XSetClassHint(display, window.window,
201 set_transparent_background(window.window);
203 XStoreName(display, window.window, "conky");
205 XClearWindow(display, window.window);
208 XMoveWindow(display, window.window, window.x,
213 /* turn off decorations */
215 XInternAtom(display, "_MOTIF_WM_HINTS", True);
217 long prop[5] = { 2, 0, 0, 0, 0 };
218 XChangeProperty(display, window.window, a,
219 a, 32, PropModeReplace,
220 (unsigned char *) prop, 5);
223 /* set window sticky (to all desktops) */
224 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
226 long prop = 0xFFFFFFFF;
227 XChangeProperty(display, window.window, a,
230 (unsigned char *) &prop,
234 /* make sure the layer is on the bottom */
235 a = XInternAtom(display, "_WIN_LAYER", True);
238 XChangeProperty(display, window.window, a,
241 (unsigned char *) &prop, 1);
246 XMapWindow(display, window.window);
249 /* root / desktop window */
251 XWindowAttributes attrs;
254 window.window = find_window_to_draw();
256 if (XGetWindowAttributes(display, window.window, &attrs)) {
257 window.width = attrs.width;
258 window.height = attrs.height;
262 /* Drawable is same as window. This may be changed by double buffering. */
263 window.drawable = window.window;
268 if (!XdbeQueryExtension(display, &major, &minor)) {
272 XdbeAllocateBackBufferName(display,
275 if (window.back_buffer != None) {
276 window.drawable = window.back_buffer;
278 "Conky: drawing to double buffer\n");
283 ERR("failed to set up double buffer");
286 fprintf(stderr, "Conky: drawing to single buffer\n");
291 /*set_transparent_background(window.window); must be done after double buffer stuff? */
294 set_transparent_background(window.window);
295 XClearWindow(display, window.window);
299 XSelectInput(display, window.window, ExposureMask
302 ? (StructureNotifyMask | PropertyChangeMask) : 0)
307 static Window find_subwindow(Window win, int w, int h)
310 Window troot, parent, *children;
313 /* search subwindows with same size as display or work area */
315 for (i = 0; i < 10; i++) {
316 XQueryTree(display, win, &troot, &parent, &children, &n);
318 for (j = 0; j < n; j++) {
319 XWindowAttributes attrs;
321 if (XGetWindowAttributes
322 (display, children[j], &attrs)) {
323 /* Window must be mapped and same size as display or work space */
324 if (attrs.map_state != 0 &&
325 ((attrs.width == display_width
326 && attrs.height == display_height)
328 && attrs.height == h))) {
343 long get_x11_color(const char *name)
348 (display, DefaultColormap(display, screen), name, &color)) {
349 ERR("can't parse X color '%s'", name);
353 (display, DefaultColormap(display, screen), &color))
354 ERR("can't allocate X color '%s'", name);
356 return (long) color.pixel;
362 values.graphics_exposures = 0;
363 values.function = GXcopy;
364 window.gc = XCreateGC(display, window.drawable,
365 GCFunction | GCGraphicsExposures, &values);