$head, mem leak fixes
[monky] / src / x11.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include "conky.h"
10 #include <X11/Xlib.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xutil.h>
13 #ifdef XFT
14 #include <X11/Xft/Xft.h>
15 #endif
16
17 #ifdef XDBE
18 int use_xdbe;
19 #endif
20
21 #ifdef XFT
22 int use_xft = 0;
23 #endif
24
25 /* some basic X11 stuff */
26 Display *display;
27 int display_width;
28 int display_height;
29 int screen;
30 static int set_transparent;
31 static int background_colour;
32
33 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
34 int workarea[4];
35
36 /* Window stuff */
37 struct conky_window window;
38
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);
43
44 /* X11 initializer */
45 void init_X11()
46 {
47         if ((display = XOpenDisplay(0)) == NULL)
48                 CRIT_ERR("can't open display: %s", XDisplayName(0));
49
50         screen = DefaultScreen(display);
51         display_width = DisplayWidth(display, screen);
52         display_height = DisplayHeight(display, screen);
53
54         update_workarea();
55 }
56
57 static void update_workarea()
58 {
59         Window root = RootWindow(display, screen);
60         unsigned long nitems, bytes;
61         unsigned char *buf = NULL;
62         Atom type;
63         int format;
64
65         /* default work area is display */
66         workarea[0] = 0;
67         workarea[1] = 0;
68         workarea[2] = display_width;
69         workarea[3] = display_height;
70
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) {
76
77                 //Currently unused 
78                 /*  long desktop = * (long *) buf; */
79
80                 XFree(buf);
81                 buf = 0;
82
83         }
84
85         if (buf) {
86                 XFree(buf);
87                 buf = 0;
88         }
89 }
90
91 static Window find_window_to_draw()
92 {
93         Atom type;
94         int format, i;
95         unsigned long nitems, bytes;
96         unsigned int n;
97         Window root = RootWindow(display, screen);
98         Window win = root;
99         Window troot, parent, *children;
100         unsigned char *buf = NULL;
101
102         /* some window managers set __SWM_VROOT to some child of root window */
103
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;
111                         XFree(buf);
112                         XFree(children);
113                         fprintf(stderr,
114                                 "Conky: drawing to window from __SWM_VROOT property\n");
115                         return win;
116                 }
117
118                 if (buf) {
119                         XFree(buf);
120                         buf = 0;
121                 }
122         }
123         XFree(children);
124
125         /* get subwindows from root */
126         win = find_subwindow(root, -1, -1);
127
128         update_workarea();
129
130         win = find_subwindow(win, workarea[2], workarea[3]);
131
132         if (buf) {
133                 XFree(buf);
134                 buf = 0;
135         }
136
137         if (win != root)
138                 fprintf(stderr,
139                         "Conky: drawing to subwindow of root window (%lx)\n",
140                         win);
141         else
142                 fprintf(stderr, "Conky: drawing to root window\n");
143
144         return win;
145 }
146
147 /* sets background to ParentRelative for the Window and all parents */
148 inline void set_transparent_background(Window win)
149 {
150         static int colour_set = -1;
151         if (set_transparent) {
152                 Window parent = win;
153                 unsigned int i;
154                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
155                         Window r, *children;
156                         unsigned int n;
157                         
158                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
159         
160                         XQueryTree(display, parent, &r, &parent, &children, &n);
161                         XFree(children);
162                         }
163         } else if (colour_set != background_colour) {
164                 XSetWindowBackground(display, win, background_colour);
165                 colour_set = background_colour;
166         }
167         //XClearWindow(display, win); not sure why this was here
168 }
169
170 #if defined OWN_WINDOW
171 void init_window(int own_window, int w, int h, int l, int fixed_pos, int set_trans, int back_colour)
172 #else
173 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour)
174 #endif
175 {
176         /* There seems to be some problems with setting transparent background (on
177          * fluxbox this time). It doesn't happen always and I don't know why it
178          * happens but I bet the bug is somewhere here. */
179         set_transparent = set_trans;
180         background_colour = back_colour;
181 #ifdef OWN_WINDOW
182         if (own_window) {
183
184                 /* looks like root pixmap isn't needed for anything */
185                 {
186                         XSetWindowAttributes attrs;
187                         XClassHint class_hints;
188
189                         /* just test color
190                         attrs.background_pixel = get_x11_color("green");
191                         */
192
193                         window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent,        /* depth */
194                                                       CopyFromParent,   /* class */
195                                                       CopyFromParent,   /* visual */
196                                                       CWBackPixel, &attrs);
197
198                         class_hints.res_class = "conky";
199                         class_hints.res_name = "conky";
200                         XSetClassHint(display, window.window,
201                                       &class_hints);
202
203                         /*set_transparent_background(window.window);*/
204
205                         XStoreName(display, window.window, "conky");
206
207                         XClearWindow(display, window.window);
208
209                         if (!fixed_pos)
210                                 XMoveWindow(display, window.window, window.x,
211                         window.y);
212                 }
213
214                 {
215                         /* turn off decorations */
216                         Atom a =
217                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
218                         if (a != None) {
219                                 long prop[5] = { 2, 0, 0, 0, 0 };
220                                 XChangeProperty(display, window.window, a,
221                                                 a, 32, PropModeReplace,
222                                                 (unsigned char *) prop, 5);
223                         }
224
225                         /* set window sticky (to all desktops) */
226                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
227                         if (a != None) {
228                                 long prop = 0xFFFFFFFF;
229                                 XChangeProperty(display, window.window, a,
230                                                 XA_CARDINAL, 32,
231                                                 PropModeReplace,
232                                                 (unsigned char *) &prop,
233                                                 1);
234                         }
235                         if(l) {
236                         /* make sure the layer is on the bottom */
237          a = XInternAtom(display, "_WIN_LAYER", True);
238          if (a != None) {
239             long prop = 0;
240             XChangeProperty(display, window.window, a,
241             XA_CARDINAL, 32,
242             PropModeReplace,
243             (unsigned char *) &prop, 1);
244          }
245                         }
246                 }
247
248                 XMapWindow(display, window.window);
249         } else
250 #endif
251                 /* root / desktop window */
252         {
253                 XWindowAttributes attrs;
254
255                 if (!window.window)
256                         window.window = find_window_to_draw();
257
258                 if (XGetWindowAttributes(display, window.window, &attrs)) {
259                         window.width = attrs.width;
260                         window.height = attrs.height;
261                 }
262         }
263
264         /* Drawable is same as window. This may be changed by double buffering. */
265         window.drawable = window.window;
266
267 #ifdef XDBE
268         if (use_xdbe) {
269                 int major, minor;
270                 if (!XdbeQueryExtension(display, &major, &minor)) {
271                         use_xdbe = 0;
272                 } else {
273                         window.back_buffer =
274                             XdbeAllocateBackBufferName(display,
275                                                        window.window,
276                                                        XdbeBackground);
277                         if (window.back_buffer != None) {
278                                 window.drawable = window.back_buffer;
279                                 fprintf(stderr,
280                                         "Conky: drawing to double buffer\n");
281                         } else
282                                 use_xdbe = 0;
283                 }
284                 if (!use_xdbe)
285                         ERR("failed to set up double buffer");
286         }
287         if (!use_xdbe)
288                 fprintf(stderr, "Conky: drawing to single buffer\n");
289 #endif
290
291         XFlush(display);
292
293         /*set_transparent_background(window.window); must be done after double buffer stuff? */
294 #ifdef OWN_WINDOW
295         /*if (own_window) {
296         set_transparent_background(window.window);
297                 XClearWindow(display, window.window);
298 }*/
299 #endif
300
301         XSelectInput(display, window.window, ExposureMask
302 #ifdef OWN_WINDOW
303                      | (own_window
304                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
305 #endif
306             );
307 }
308
309 static Window find_subwindow(Window win, int w, int h)
310 {
311         unsigned int i, j;
312         Window troot, parent, *children;
313         unsigned int n;
314
315         /* search subwindows with same size as display or work area */
316
317         for (i = 0; i < 10; i++) {
318                 XQueryTree(display, win, &troot, &parent, &children, &n);
319
320                 for (j = 0; j < n; j++) {
321                         XWindowAttributes attrs;
322
323                         if (XGetWindowAttributes
324                             (display, children[j], &attrs)) {
325                                 /* Window must be mapped and same size as display or work space */
326                                 if (attrs.map_state != 0 &&
327                                     ((attrs.width == display_width
328                                       && attrs.height == display_height)
329                                      || (attrs.width == w
330                                          && attrs.height == h))) {
331                                         win = children[j];
332                                         break;
333                                 }
334                         }
335                 }
336
337                 XFree(children);
338                 if (j == n)
339                         break;
340         }
341
342         return win;
343 }
344
345 long get_x11_color(const char *name)
346 {
347         XColor color;
348         color.pixel = 0;
349         if (!XParseColor
350             (display, DefaultColormap(display, screen), name, &color)) {
351                 ERR("can't parse X color '%s'", name);
352                 return 0xFF00FF;
353         }
354         if (!XAllocColor
355             (display, DefaultColormap(display, screen), &color))
356                 ERR("can't allocate X color '%s'", name);
357
358         return (long) color.pixel;
359 }
360
361 void create_gc()
362 {
363         XGCValues values;
364         values.graphics_exposures = 0;
365         values.function = GXcopy;
366         window.gc = XCreateGC(display, window.drawable,
367                               GCFunction | GCGraphicsExposures, &values);
368 }