own_window additions, such as background colour and transparency switch
[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 void set_transparent_background(Window win)
149 {
150         if (set_transparent) {
151                 Window parent = win;
152                 unsigned int i;
153                 for (i = 0; i < 16 && parent != RootWindow(display, screen); i++) {
154                         Window r, *children;
155                         unsigned int n;
156                         
157                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
158         
159                         XQueryTree(display, parent, &r, &parent, &children, &n);
160                         XFree(children);
161                 }
162         } else {
163                 XSetWindowBackground(display, win, background_colour);
164         }
165         XClearWindow(display, win);
166 }
167
168 #if defined OWN_WINDOW
169 void init_window(int own_window, int w, int h, int l, int fixed_pos, int set_trans, int back_colour)
170 #else
171 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour)
172 #endif
173 {
174         /* There seems to be some problems with setting transparent background (on
175          * fluxbox this time). It doesn't happen always and I don't know why it
176          * happens but I bet the bug is somewhere here. */
177         set_transparent = set_trans;
178         background_colour = back_colour;
179 #ifdef OWN_WINDOW
180         if (own_window) {
181
182                 /* looks like root pixmap isn't needed for anything */
183                 {
184                         XSetWindowAttributes attrs;
185                         XClassHint class_hints;
186
187                         /* just test color
188                         attrs.background_pixel = get_x11_color("green");
189                         */
190
191                         window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent,        /* depth */
192                                                       CopyFromParent,   /* class */
193                                                       CopyFromParent,   /* visual */
194                                                       CWBackPixel, &attrs);
195
196                         class_hints.res_class = "conky";
197                         class_hints.res_name = "conky";
198                         XSetClassHint(display, window.window,
199                                       &class_hints);
200
201                         set_transparent_background(window.window);
202
203                         XStoreName(display, window.window, "conky");
204
205                         XClearWindow(display, window.window);
206
207                         if (!fixed_pos)
208                                 XMoveWindow(display, window.window, window.x,
209                         window.y);
210                 }
211
212                 {
213                         /* turn off decorations */
214                         Atom a =
215                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
216                         if (a != None) {
217                                 long prop[5] = { 2, 0, 0, 0, 0 };
218                                 XChangeProperty(display, window.window, a,
219                                                 a, 32, PropModeReplace,
220                                                 (unsigned char *) prop, 5);
221                         }
222
223                         /* set window sticky (to all desktops) */
224                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
225                         if (a != None) {
226                                 long prop = 0xFFFFFFFF;
227                                 XChangeProperty(display, window.window, a,
228                                                 XA_CARDINAL, 32,
229                                                 PropModeReplace,
230                                                 (unsigned char *) &prop,
231                                                 1);
232                         }
233                         if(l) {
234                         /* make sure the layer is on the bottom */
235          a = XInternAtom(display, "_WIN_LAYER", True);
236          if (a != None) {
237             long prop = 0;
238             XChangeProperty(display, window.window, a,
239             XA_CARDINAL, 32,
240             PropModeReplace,
241             (unsigned char *) &prop, 1);
242          }
243                         }
244                 }
245
246                 XMapWindow(display, window.window);
247         } else
248 #endif
249                 /* root / desktop window */
250         {
251                 XWindowAttributes attrs;
252
253                 if (!window.window)
254                         window.window = find_window_to_draw();
255
256                 if (XGetWindowAttributes(display, window.window, &attrs)) {
257                         window.width = attrs.width;
258                         window.height = attrs.height;
259                 }
260         }
261
262         /* Drawable is same as window. This may be changed by double buffering. */
263         window.drawable = window.window;
264
265 #ifdef XDBE
266         if (use_xdbe) {
267                 int major, minor;
268                 if (!XdbeQueryExtension(display, &major, &minor)) {
269                         use_xdbe = 0;
270                 } else {
271                         window.back_buffer =
272                             XdbeAllocateBackBufferName(display,
273                                                        window.window,
274                                                        XdbeBackground);
275                         if (window.back_buffer != None) {
276                                 window.drawable = window.back_buffer;
277                                 fprintf(stderr,
278                                         "Conky: drawing to double buffer\n");
279                         } else
280                                 use_xdbe = 0;
281                 }
282                 if (!use_xdbe)
283                         ERR("failed to set up double buffer");
284         }
285         if (!use_xdbe)
286                 fprintf(stderr, "Conky: drawing to single buffer\n");
287 #endif
288
289         XFlush(display);
290
291         /*set_transparent_background(window.window); must be done after double buffer stuff? */
292 #ifdef OWN_WINDOW
293         if (own_window) {
294                 set_transparent_background(window.window);
295                 XClearWindow(display, window.window);
296         }
297 #endif
298
299         XSelectInput(display, window.window, ExposureMask
300 #ifdef OWN_WINDOW
301                      | (own_window
302                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
303 #endif
304             );
305 }
306
307 static Window find_subwindow(Window win, int w, int h)
308 {
309         unsigned int i, j;
310         Window troot, parent, *children;
311         unsigned int n;
312
313         /* search subwindows with same size as display or work area */
314
315         for (i = 0; i < 10; i++) {
316                 XQueryTree(display, win, &troot, &parent, &children, &n);
317
318                 for (j = 0; j < n; j++) {
319                         XWindowAttributes attrs;
320
321                         if (XGetWindowAttributes
322                             (display, children[j], &attrs)) {
323                                 /* Window must be mapped and same size as display or work space */
324                                 if (attrs.map_state != 0 &&
325                                     ((attrs.width == display_width
326                                       && attrs.height == display_height)
327                                      || (attrs.width == w
328                                          && attrs.height == h))) {
329                                         win = children[j];
330                                         break;
331                                 }
332                         }
333                 }
334
335                 XFree(children);
336                 if (j == n)
337                         break;
338         }
339
340         return win;
341 }
342
343 long get_x11_color(const char *name)
344 {
345         XColor color;
346         color.pixel = 0;
347         if (!XParseColor
348             (display, DefaultColormap(display, screen), name, &color)) {
349                 ERR("can't parse X color '%s'", name);
350                 return 0xFF00FF;
351         }
352         if (!XAllocColor
353             (display, DefaultColormap(display, screen), &color))
354                 ERR("can't allocate X color '%s'", name);
355
356         return (long) color.pixel;
357 }
358
359 void create_gc()
360 {
361         XGCValues values;
362         values.graphics_exposures = 0;
363         values.function = GXcopy;
364         window.gc = XCreateGC(display, window.drawable,
365                               GCFunction | GCGraphicsExposures, &values);
366 }