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;
193 /* looks like root pixmap isn't needed for anything */
195 XSetWindowAttributes attrs;
196 XClassHint class_hints;
199 attrs.background_pixel = get_x11_color("green");
202 window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent, /* depth */
203 CopyFromParent, /* class */
204 CopyFromParent, /* visual */
205 CWBackPixel, &attrs);
208 this doesn't work properly
210 wmhints.flags = StateHint;
211 wmhints.initial_state = WithdrawnState;
212 XSetWMHints(display, window.window, &wmhints);*/
216 class_hints.res_class = wm_class_name;
217 class_hints.res_name = wm_class_name;
218 XSetClassHint(display, window.window,
221 /*set_transparent_background(window.window);*/
222 window_name = (char *) malloc(strlen(WINDOW_NAME_FMT) + strlen(nodename)+1);
223 sprintf(window_name, WINDOW_NAME_FMT, nodename);
224 XStoreName(display, window.window, window_name);
226 XClearWindow(display, window.window);
229 XMoveWindow(display, window.window, window.x,
234 /* turn off decorations */
236 XInternAtom(display, "_MOTIF_WM_HINTS", True);
238 long prop[5] = { 2, 0, 0, 0, 0 };
239 XChangeProperty(display, window.window, a,
240 a, 32, PropModeReplace,
241 (unsigned char *) prop, 5);
244 /* set window sticky (to all desktops) */
245 a = XInternAtom(display, "_NET_WM_DESKTOP", True);
247 long prop = 0xFFFFFFFF;
248 XChangeProperty(display, window.window, a,
251 (unsigned char *) &prop,
255 /* make sure the layer is on the bottom */
256 a = XInternAtom(display, "_WIN_LAYER", True);
259 XChangeProperty(display, window.window, a,
262 (unsigned char *) &prop, 1);
267 XMapWindow(display, window.window);
270 /* root / desktop window */
272 XWindowAttributes attrs;
275 window.window = find_window_to_draw();
277 if (XGetWindowAttributes(display, window.window, &attrs)) {
278 window.width = attrs.width;
279 window.height = attrs.height;
283 /* Drawable is same as window. This may be changed by double buffering. */
284 window.drawable = window.window;
289 if (!XdbeQueryExtension(display, &major, &minor)) {
293 XdbeAllocateBackBufferName(display,
296 if (window.back_buffer != None) {
297 window.drawable = window.back_buffer;
299 "Conky: drawing to double buffer\n");
304 ERR("failed to set up double buffer");
307 fprintf(stderr, "Conky: drawing to single buffer\n");
312 /*set_transparent_background(window.window); must be done after double buffer stuff? */
315 set_transparent_background(window.window);
316 XClearWindow(display, window.window);
320 XSelectInput(display, window.window, ExposureMask
323 ? (StructureNotifyMask | PropertyChangeMask) : 0)
328 static Window find_subwindow(Window win, int w, int h)
331 Window troot, parent, *children;
334 /* search subwindows with same size as display or work area */
336 for (i = 0; i < 10; i++) {
337 XQueryTree(display, win, &troot, &parent, &children, &n);
339 for (j = 0; j < n; j++) {
340 XWindowAttributes attrs;
342 if (XGetWindowAttributes
343 (display, children[j], &attrs)) {
344 /* Window must be mapped and same size as display or work space */
345 if (attrs.map_state != 0 &&
346 ((attrs.width == display_width
347 && attrs.height == display_height)
349 && attrs.height == h))) {
364 long get_x11_color(const char *name)
369 (display, DefaultColormap(display, screen), name, &color)) {
370 ERR("can't parse X color '%s'", name);
374 (display, DefaultColormap(display, screen), &color))
375 ERR("can't allocate X color '%s'", name);
377 return (long) color.pixel;
383 values.graphics_exposures = 0;
384 values.function = GXcopy;
385 window.gc = XCreateGC(display, window.drawable,
386 GCFunction | GCGraphicsExposures, &values);