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