x11 patch 1291420
[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                         wmhints.flags = StateHint;
203                         wmhints.initial_state = WithdrawnState;
204                         XSetWMHints(display, window.window, &wmhints);
205
206
207
208                         class_hints.res_class = "conky";
209                         class_hints.res_name = "conky";
210                         XSetClassHint(display, window.window,
211                                       &class_hints);
212
213                         /*set_transparent_background(window.window);*/
214
215                         XStoreName(display, window.window, "conky");
216
217                         XClearWindow(display, window.window);
218
219                         if (!fixed_pos)
220                                 XMoveWindow(display, window.window, window.x,
221                         window.y);
222                 }
223
224                 {
225                         /* turn off decorations */
226                         Atom a =
227                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
228                         if (a != None) {
229                                 long prop[5] = { 2, 0, 0, 0, 0 };
230                                 XChangeProperty(display, window.window, a,
231                                                 a, 32, PropModeReplace,
232                                                 (unsigned char *) prop, 5);
233                         }
234
235                         /* set window sticky (to all desktops) */
236                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
237                         if (a != None) {
238                                 long prop = 0xFFFFFFFF;
239                                 XChangeProperty(display, window.window, a,
240                                                 XA_CARDINAL, 32,
241                                                 PropModeReplace,
242                                                 (unsigned char *) &prop,
243                                                 1);
244                         }
245                         if(l) {
246                         /* make sure the layer is on the bottom */
247          a = XInternAtom(display, "_WIN_LAYER", True);
248          if (a != None) {
249             long prop = 0;
250             XChangeProperty(display, window.window, a,
251             XA_CARDINAL, 32,
252             PropModeReplace,
253             (unsigned char *) &prop, 1);
254          }
255                         }
256                 }
257
258                 XMapWindow(display, window.window);
259         } else
260 #endif
261                 /* root / desktop window */
262         {
263                 XWindowAttributes attrs;
264
265                 if (!window.window)
266                         window.window = find_window_to_draw();
267
268                 if (XGetWindowAttributes(display, window.window, &attrs)) {
269                         window.width = attrs.width;
270                         window.height = attrs.height;
271                 }
272         }
273
274         /* Drawable is same as window. This may be changed by double buffering. */
275         window.drawable = window.window;
276
277 #ifdef XDBE
278         if (use_xdbe) {
279                 int major, minor;
280                 if (!XdbeQueryExtension(display, &major, &minor)) {
281                         use_xdbe = 0;
282                 } else {
283                         window.back_buffer =
284                             XdbeAllocateBackBufferName(display,
285                                                        window.window,
286                                                        XdbeBackground);
287                         if (window.back_buffer != None) {
288                                 window.drawable = window.back_buffer;
289                                 fprintf(stderr,
290                                         "Conky: drawing to double buffer\n");
291                         } else
292                                 use_xdbe = 0;
293                 }
294                 if (!use_xdbe)
295                         ERR("failed to set up double buffer");
296         }
297         if (!use_xdbe)
298                 fprintf(stderr, "Conky: drawing to single buffer\n");
299 #endif
300
301         XFlush(display);
302
303         /*set_transparent_background(window.window); must be done after double buffer stuff? */
304 #ifdef OWN_WINDOW
305         /*if (own_window) {
306         set_transparent_background(window.window);
307                 XClearWindow(display, window.window);
308 }*/
309 #endif
310
311         XSelectInput(display, window.window, ExposureMask
312 #ifdef OWN_WINDOW
313                      | (own_window
314                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
315 #endif
316             );
317 }
318
319 static Window find_subwindow(Window win, int w, int h)
320 {
321         unsigned int i, j;
322         Window troot, parent, *children;
323         unsigned int n;
324
325         /* search subwindows with same size as display or work area */
326
327         for (i = 0; i < 10; i++) {
328                 XQueryTree(display, win, &troot, &parent, &children, &n);
329
330                 for (j = 0; j < n; j++) {
331                         XWindowAttributes attrs;
332
333                         if (XGetWindowAttributes
334                             (display, children[j], &attrs)) {
335                                 /* Window must be mapped and same size as display or work space */
336                                 if (attrs.map_state != 0 &&
337                                     ((attrs.width == display_width
338                                       && attrs.height == display_height)
339                                      || (attrs.width == w
340                                          && attrs.height == h))) {
341                                         win = children[j];
342                                         break;
343                                 }
344                         }
345                 }
346
347                 XFree(children);
348                 if (j == n)
349                         break;
350         }
351
352         return win;
353 }
354
355 long get_x11_color(const char *name)
356 {
357         XColor color;
358         color.pixel = 0;
359         if (!XParseColor
360             (display, DefaultColormap(display, screen), name, &color)) {
361                 ERR("can't parse X color '%s'", name);
362                 return 0xFF00FF;
363         }
364         if (!XAllocColor
365             (display, DefaultColormap(display, screen), &color))
366                 ERR("can't allocate X color '%s'", name);
367
368         return (long) color.pixel;
369 }
370
371 void create_gc()
372 {
373         XGCValues values;
374         values.graphics_exposures = 0;
375         values.function = GXcopy;
376         window.gc = XCreateGC(display, window.drawable,
377                               GCFunction | GCGraphicsExposures, &values);
378 }
379
380 #endif /* X11 */