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 this doesn't work properly
204 wmhints.flags = StateHint;
205 wmhints.initial_state = WithdrawnState;
206 XSetWMHints(display, window.window, &wmhints);*/
210 class_hints.res_class = "conky";
211 class_hints.res_name = "conky";
212 XSetClassHint(display, window.window,
215 /*set_transparent_background(window.window);*/
217 XStoreName(display, window.window, "conky");
219 XClearWindow(display, window.window);
222 XMoveWindow(display, window.window, window.x,
227 /* turn off decorations */
229 XInternAtom(display, "_MOTIF_WM_HINTS", True);
231 long prop[5] = { 2, 0, 0, 0, 0 };
232 XChangeProperty(display, window.window, a,
233 a, 32, PropModeReplace,
234 (unsigned char *) prop, 5);
237 /* set window sticky (to all desktops) */
238 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
240 long prop = 0xFFFFFFFF;
241 XChangeProperty(display, window.window, a,
244 (unsigned char *) &prop,
248 /* make sure the layer is on the bottom */
249 a = XInternAtom(display, "_WIN_LAYER", True);
252 XChangeProperty(display, window.window, a,
255 (unsigned char *) &prop, 1);
260 XMapWindow(display, window.window);
263 /* root / desktop window */
265 XWindowAttributes attrs;
268 window.window = find_window_to_draw();
270 if (XGetWindowAttributes(display, window.window, &attrs)) {
271 window.width = attrs.width;
272 window.height = attrs.height;
276 /* Drawable is same as window. This may be changed by double buffering. */
277 window.drawable = window.window;
282 if (!XdbeQueryExtension(display, &major, &minor)) {
286 XdbeAllocateBackBufferName(display,
289 if (window.back_buffer != None) {
290 window.drawable = window.back_buffer;
292 "Conky: drawing to double buffer\n");
297 ERR("failed to set up double buffer");
300 fprintf(stderr, "Conky: drawing to single buffer\n");
305 /*set_transparent_background(window.window); must be done after double buffer stuff? */
308 set_transparent_background(window.window);
309 XClearWindow(display, window.window);
313 XSelectInput(display, window.window, ExposureMask
316 ? (StructureNotifyMask | PropertyChangeMask) : 0)
321 static Window find_subwindow(Window win, int w, int h)
324 Window troot, parent, *children;
327 /* search subwindows with same size as display or work area */
329 for (i = 0; i < 10; i++) {
330 XQueryTree(display, win, &troot, &parent, &children, &n);
332 for (j = 0; j < n; j++) {
333 XWindowAttributes attrs;
335 if (XGetWindowAttributes
336 (display, children[j], &attrs)) {
337 /* Window must be mapped and same size as display or work space */
338 if (attrs.map_state != 0 &&
339 ((attrs.width == display_width
340 && attrs.height == display_height)
342 && attrs.height == h))) {
357 long get_x11_color(const char *name)
362 (display, DefaultColormap(display, screen), name, &color)) {
363 ERR("can't parse X color '%s'", name);
367 (display, DefaultColormap(display, screen), &color))
368 ERR("can't allocate X color '%s'", name);
370 return (long) color.pixel;
376 values.graphics_exposures = 0;
377 values.function = GXcopy;
378 window.gc = XCreateGC(display, window.drawable,
379 GCFunction | GCGraphicsExposures, &values);