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_desktop_window();
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_desktop_window()
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: desktop window (%lx) found from __SWM_VROOT property\n", win);
132 /* get subwindows from root */
133 win = find_subwindow(root, -1, -1);
137 win = find_subwindow(win, workarea[2], workarea[3]);
146 "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
148 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
153 /* sets background to ParentRelative for the Window and all parents */
154 inline void set_transparent_background(Window win)
156 static int colour_set = -1;
157 if (set_transparent) {
160 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
164 XSetWindowBackgroundPixmap(display, parent, ParentRelative);
166 XQueryTree(display, parent, &r, &parent, &children, &n);
169 } else if (colour_set != background_colour) {
170 XSetWindowBackground(display, win, background_colour);
171 colour_set = background_colour;
173 //XClearWindow(display, win); not sure why this was here
176 #if defined OWN_WINDOW
177 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)
179 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour, char * nodename)
182 /* There seems to be some problems with setting transparent background (on
183 * fluxbox this time). It doesn't happen always and I don't know why it
184 * happens but I bet the bug is somewhere here. */
185 set_transparent = set_trans;
186 background_colour = back_colour;
188 wm_class_name = (char *)wm_class_name;
190 fixed_pos = (int)fixed_pos;
191 nodename = (char *)nodename;
196 /* DRASTICALLY SIMPLIFIED -
197 * override_redirect=True impedes the WM from manipulating
198 * the window, adding decorations, etc. we do not register
199 * for button events so you should have menu clicking over
200 * the conky window now too. PHK. */
201 XSetWindowAttributes attrs = {
202 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
203 StructureNotifyMask|ExposureMask,
206 window.root = find_desktop_window();
208 window.window = XCreateWindow(display, window.root,
209 window.x, window.y, w, h, 0,
213 CWBackPixel|CWOverrideRedirect, &attrs);
215 fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
217 XLowerWindow(display, window.window);
219 XMapWindow(display, window.window);
224 /* root / desktop window */
226 XWindowAttributes attrs;
229 window.window = find_desktop_window();
231 if (XGetWindowAttributes(display, window.window, &attrs)) {
232 window.width = attrs.width;
233 window.height = attrs.height;
236 fprintf(stderr, "Conky: drawing to desktop window\n");
239 /* Drawable is same as window. This may be changed by double buffering. */
240 window.drawable = window.window;
245 if (!XdbeQueryExtension(display, &major, &minor)) {
249 XdbeAllocateBackBufferName(display,
252 if (window.back_buffer != None) {
253 window.drawable = window.back_buffer;
255 "Conky: drawing to double buffer\n");
260 ERR("failed to set up double buffer");
263 fprintf(stderr, "Conky: drawing to single buffer\n");
268 /*set_transparent_background(window.window); must be done after double buffer stuff? */
271 set_transparent_background(window.window);
272 XClearWindow(display, window.window);
276 XSelectInput(display, window.window, ExposureMask
279 ? (StructureNotifyMask | PropertyChangeMask) : 0)
284 static Window find_subwindow(Window win, int w, int h)
287 Window troot, parent, *children;
290 /* search subwindows with same size as display or work area */
292 for (i = 0; i < 10; i++) {
293 XQueryTree(display, win, &troot, &parent, &children, &n);
295 for (j = 0; j < n; j++) {
296 XWindowAttributes attrs;
298 if (XGetWindowAttributes
299 (display, children[j], &attrs)) {
300 /* Window must be mapped and same size as display or work space */
301 if (attrs.map_state != 0 &&
302 ((attrs.width == display_width
303 && attrs.height == display_height)
305 && attrs.height == h))) {
320 long get_x11_color(const char *name)
325 (display, DefaultColormap(display, screen), name, &color)) {
326 /* lets check if it's a hex colour with the # missing in front
327 * if yes, then do something about it
331 strncpy(&newname[1], name, 62);
332 /* now lets try again */
333 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
334 ERR("can't parse X color '%s'", name);
339 (display, DefaultColormap(display, screen), &color))
340 ERR("can't allocate X color '%s'", name);
342 return (long) color.pixel;
348 values.graphics_exposures = 0;
349 values.function = GXcopy;
350 window.gc = XCreateGC(display, window.drawable,
351 GCFunction | GCGraphicsExposures, &values);