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