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 */
31 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
35 struct conky_window window;
37 /* local prototypes */
38 static void update_workarea();
39 static Window find_window_to_draw();
40 static Window find_subwindow(Window win, int w, int h);
45 if ((display = XOpenDisplay(0)) == NULL)
46 CRIT_ERR("can't open display: %s", XDisplayName(0));
48 screen = DefaultScreen(display);
49 display_width = DisplayWidth(display, screen);
50 display_height = DisplayHeight(display, screen);
55 static void update_workarea()
57 Window root = RootWindow(display, screen);
58 unsigned long nitems, bytes;
59 unsigned char *buf = NULL;
63 /* default work area is display */
66 workarea[2] = display_width;
67 workarea[3] = display_height;
69 /* get current desktop */
70 if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
71 0, 1, False, XA_CARDINAL, &type, &format,
72 &nitems, &bytes, &buf) == Success
73 && type == XA_CARDINAL && nitems > 0) {
76 /* long desktop = * (long *) buf; */
89 static Window find_window_to_draw()
93 unsigned long nitems, bytes;
95 Window root = RootWindow(display, screen);
97 Window troot, parent, *children;
98 unsigned char *buf = NULL;
100 /* some window managers set __SWM_VROOT to some child of root window */
102 XQueryTree(display, root, &troot, &parent, &children, &n);
103 for (i = 0; i < (int) n; i++) {
104 if (XGetWindowProperty
105 (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
106 XA_WINDOW, &type, &format, &nitems, &bytes,
107 &buf) == Success && type == XA_WINDOW) {
108 win = *(Window *) buf;
112 "Conky: drawing to window from __SWM_VROOT property\n");
123 /* get subwindows from root */
124 win = find_subwindow(root, -1, -1);
128 win = find_subwindow(win, workarea[2], workarea[3]);
137 "Conky: drawing to subwindow of root window (%lx)\n",
140 fprintf(stderr, "Conky: drawing to root window\n");
145 /* sets background to ParentRelative for the Window and all parents */
146 void set_transparent_background(Window win)
150 for (i = 0; i < 16 && parent != RootWindow(display, screen); i++) {
154 XSetWindowBackgroundPixmap(display, parent,
157 XQueryTree(display, parent, &r, &parent, &children, &n);
160 XClearWindow(display, win);
163 #if defined OWN_WINDOW
164 void init_window(int own_window, int w, int h, int l, int fixed_pos)
166 void init_window(int own_window, int w, int h, int l)
169 /* There seems to be some problems with setting transparent background (on
170 * fluxbox this time). It doesn't happen always and I don't know why it
171 * happens but I bet the bug is somewhere here. */
174 /* looks like root pixmap isn't needed for anything */
176 XSetWindowAttributes attrs;
177 XClassHint class_hints;
180 attrs.background_pixel = get_x11_color("green");
183 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
184 CopyFromParent, /* class */
185 CopyFromParent, /* visual */
186 CWBackPixel, &attrs);
188 class_hints.res_class = "conky";
189 class_hints.res_name = "conky";
190 XSetClassHint(display, window.window,
193 set_transparent_background(window.window);
195 XStoreName(display, window.window, "conky");
197 XClearWindow(display, window.window);
200 XMoveWindow(display, window.window, window.x,
205 /* turn off decorations */
207 XInternAtom(display, "_MOTIF_WM_HINTS", True);
209 long prop[5] = { 2, 0, 0, 0, 0 };
210 XChangeProperty(display, window.window, a,
211 a, 32, PropModeReplace,
212 (unsigned char *) prop, 5);
215 /* set window sticky (to all desktops) */
216 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
218 long prop = 0xFFFFFFFF;
219 XChangeProperty(display, window.window, a,
222 (unsigned char *) &prop,
226 /* make sure the layer is on the bottom */
227 a = XInternAtom(display, "_WIN_LAYER", True);
230 XChangeProperty(display, window.window, a,
233 (unsigned char *) &prop, 1);
238 XMapWindow(display, window.window);
241 /* root / desktop window */
243 XWindowAttributes attrs;
246 window.window = find_window_to_draw();
248 if (XGetWindowAttributes(display, window.window, &attrs)) {
249 window.width = attrs.width;
250 window.height = attrs.height;
254 /* Drawable is same as window. This may be changed by double buffering. */
255 window.drawable = window.window;
260 if (!XdbeQueryExtension(display, &major, &minor)) {
264 XdbeAllocateBackBufferName(display,
267 if (window.back_buffer != None) {
268 window.drawable = window.back_buffer;
270 "Conky: drawing to double buffer\n");
275 ERR("failed to set up double buffer");
278 fprintf(stderr, "Conky: drawing to single buffer\n");
283 /* set_transparent_background() must be done after double buffer stuff? */
286 set_transparent_background(window.window);
287 XClearWindow(display, window.window);
291 XSelectInput(display, window.window, ExposureMask
294 ? (StructureNotifyMask | PropertyChangeMask) : 0)
299 static Window find_subwindow(Window win, int w, int h)
302 Window troot, parent, *children;
305 /* search subwindows with same size as display or work area */
307 for (i = 0; i < 10; i++) {
308 XQueryTree(display, win, &troot, &parent, &children, &n);
310 for (j = 0; j < n; j++) {
311 XWindowAttributes attrs;
313 if (XGetWindowAttributes
314 (display, children[j], &attrs)) {
315 /* Window must be mapped and same size as display or work space */
316 if (attrs.map_state != 0 &&
317 ((attrs.width == display_width
318 && attrs.height == display_height)
320 && attrs.height == h))) {
335 long get_x11_color(const char *name)
340 (display, DefaultColormap(display, screen), name, &color)) {
341 ERR("can't parse X color '%s'", name);
345 (display, DefaultColormap(display, screen), &color))
346 ERR("can't allocate X color '%s'", name);
348 return (long) color.pixel;
354 values.graphics_exposures = 0;
355 values.function = GXcopy;
356 window.gc = XCreateGC(display, window.drawable,
357 GCFunction | GCGraphicsExposures, &values);