conky back under WM control for own_window yes
[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 #include <stdio.h>
21
22 #ifdef XDBE
23 int use_xdbe;
24 #endif
25
26 #ifdef XFT
27 int use_xft = 0;
28 #endif
29
30 #define WINDOW_NAME_FMT "%s - conky" 
31
32 /* some basic X11 stuff */
33 Display *display;
34 int display_width;
35 int display_height;
36 int screen;
37 static int set_transparent;
38 static int background_colour;
39
40 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
41 int workarea[4];
42
43 /* Window stuff */
44 struct conky_window window;
45
46 /* local prototypes */
47 static void update_workarea();
48 static Window find_desktop_window();
49 static Window find_subwindow(Window win, int w, int h);
50
51 /* X11 initializer */
52 void init_X11()
53 {
54         if ((display = XOpenDisplay(0)) == NULL)
55                 CRIT_ERR("can't open display: %s", XDisplayName(0));
56
57         screen = DefaultScreen(display);
58         display_width = DisplayWidth(display, screen);
59         display_height = DisplayHeight(display, screen);
60
61         update_workarea();
62 }
63
64 static void update_workarea()
65 {
66         Window root = RootWindow(display, screen);
67         unsigned long nitems, bytes;
68         unsigned char *buf = NULL;
69         Atom type;
70         int format;
71
72         /* default work area is display */
73         workarea[0] = 0;
74         workarea[1] = 0;
75         workarea[2] = display_width;
76         workarea[3] = display_height;
77
78         /* get current desktop */
79         if (XGetWindowProperty(display, root, ATOM(_NET_CURRENT_DESKTOP),
80                                0, 1, False, XA_CARDINAL, &type, &format,
81                                &nitems, &bytes, &buf) == Success
82             && type == XA_CARDINAL && nitems > 0) {
83
84                 //Currently unused 
85                 /*  long desktop = * (long *) buf; */
86
87                 XFree(buf);
88                 buf = 0;
89
90         }
91
92         if (buf) {
93                 XFree(buf);
94                 buf = 0;
95         }
96 }
97
98 static Window find_desktop_window()
99 {
100         Atom type;
101         int format, i;
102         unsigned long nitems, bytes;
103         unsigned int n;
104         Window root = RootWindow(display, screen);
105         Window win = root;
106         Window troot, parent, *children;
107         unsigned char *buf = NULL;
108
109         /* some window managers set __SWM_VROOT to some child of root window */
110
111         XQueryTree(display, root, &troot, &parent, &children, &n);
112         for (i = 0; i < (int) n; i++) {
113                 if (XGetWindowProperty
114                     (display, children[i], ATOM(__SWM_VROOT), 0, 1, False,
115                      XA_WINDOW, &type, &format, &nitems, &bytes,
116                      &buf) == Success && type == XA_WINDOW) {
117                         win = *(Window *) buf;
118                         XFree(buf);
119                         XFree(children);
120                         fprintf(stderr,
121                                 "Conky: desktop window (%lx) found from __SWM_VROOT property\n", win);
122                         return win;
123                 }
124
125                 if (buf) {
126                         XFree(buf);
127                         buf = 0;
128                 }
129         }
130         XFree(children);
131
132         /* get subwindows from root */
133         win = find_subwindow(root, -1, -1);
134
135         update_workarea();
136
137         win = find_subwindow(win, workarea[2], workarea[3]);
138
139         if (buf) {
140                 XFree(buf);
141                 buf = 0;
142         }
143
144         if (win != root)
145                 fprintf(stderr,
146                         "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
147         else
148                 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
149
150         return win;
151 }
152
153 /* sets background to ParentRelative for the Window and all parents */
154 inline void set_transparent_background(Window win)
155 {
156         static int colour_set = -1;
157         if (set_transparent) {
158                 Window parent = win;
159                 unsigned int i;
160                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
161                         Window r, *children;
162                         unsigned int n;
163                         
164                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
165         
166                         XQueryTree(display, parent, &r, &parent, &children, &n);
167                         XFree(children);
168                         }
169         } else if (colour_set != background_colour) {
170                 XSetWindowBackground(display, win, background_colour);
171                 colour_set = background_colour;
172 }
173         //XClearWindow(display, win); not sure why this was here
174 }
175
176 void init_window(int own_window, int w, int h, int set_trans, int back_colour, char * nodename, 
177                  char **argv, int argc)
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
185         nodename = (char *)nodename;
186
187 #ifdef OWN_WINDOW
188         if (own_window) {
189                 {
190                         /* Allow WM control of conky again.  Shielding conky from the WM 
191                          * via override redirect creates more problems than it's worth and
192                          * makes it impossible to use tools like devilspie to manage the
193                          * conky windows beyond the parametsrs we offer.  Also, button 
194                          * press events are now explicitly forwarded to the root window. */
195                         XSetWindowAttributes attrs = {
196                                 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
197                                 StructureNotifyMask|ExposureMask|ButtonPressMask,
198                                 0L,False,0,0 };
199
200                         XClassHint classHint;
201                         XWMHints wmHint;
202                         char window_title[256];
203
204                         window.root = find_desktop_window();
205
206                         window.window = XCreateWindow(display, window.root, 
207                                                       window.x, window.y, w, h, 0, 
208                                                       CopyFromParent,
209                                                       InputOutput,
210                                                       CopyFromParent,
211                                                       CWBackPixel|CWOverrideRedirect, &attrs);
212
213                         fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
214
215                         classHint.res_name = "conky";
216                         classHint.res_class = classHint.res_name;
217
218                         wmHint.flags = InputHint | StateHint;
219                         wmHint.input = False;
220                         wmHint.initial_state = NormalState;
221
222                         sprintf(window_title,WINDOW_NAME_FMT,nodename);
223
224                         XmbSetWMProperties (display, window.window, window_title, NULL, 
225                                             argv, argc,
226                                             NULL, &wmHint, &classHint);
227
228                         /* Sets an empty WM_PROTOCOLS property */
229                         XSetWMProtocols(display,window.window,NULL,0);
230
231                         XLowerWindow(display, window.window);
232                         XMapWindow(display, window.window);
233
234                 }
235         } else
236 #endif
237                 /* root / desktop window */
238         {
239                 XWindowAttributes attrs;
240
241                 if (!window.window)
242                         window.window = find_desktop_window();
243
244                 if (XGetWindowAttributes(display, window.window, &attrs)) {
245                         window.width = attrs.width;
246                         window.height = attrs.height;
247                 }
248
249                 fprintf(stderr, "Conky: drawing to desktop window\n");
250         }
251
252         /* Drawable is same as window. This may be changed by double buffering. */
253         window.drawable = window.window;
254
255 #ifdef XDBE
256         if (use_xdbe) {
257                 int major, minor;
258                 if (!XdbeQueryExtension(display, &major, &minor)) {
259                         use_xdbe = 0;
260                 } else {
261                         window.back_buffer =
262                             XdbeAllocateBackBufferName(display,
263                                                        window.window,
264                                                        XdbeBackground);
265                         if (window.back_buffer != None) {
266                                 window.drawable = window.back_buffer;
267                                 fprintf(stderr,
268                                         "Conky: drawing to double buffer\n");
269                         } else
270                                 use_xdbe = 0;
271                 }
272                 if (!use_xdbe)
273                         ERR("failed to set up double buffer");
274         }
275         if (!use_xdbe)
276                 fprintf(stderr, "Conky: drawing to single buffer\n");
277 #endif
278
279         XFlush(display);
280
281         /*set_transparent_background(window.window); must be done after double buffer stuff? */
282 #ifdef OWN_WINDOW
283         /*if (own_window) {
284         set_transparent_background(window.window);
285                 XClearWindow(display, window.window);
286 }*/
287 #endif
288
289         XSelectInput(display, window.window, ExposureMask
290 #ifdef OWN_WINDOW
291                      | (own_window
292                         ? (StructureNotifyMask | PropertyChangeMask | ButtonPressMask) : 0)
293 #endif
294             );
295 }
296
297 static Window find_subwindow(Window win, int w, int h)
298 {
299         unsigned int i, j;
300         Window troot, parent, *children;
301         unsigned int n;
302
303         /* search subwindows with same size as display or work area */
304
305         for (i = 0; i < 10; i++) {
306                 XQueryTree(display, win, &troot, &parent, &children, &n);
307
308                 for (j = 0; j < n; j++) {
309                         XWindowAttributes attrs;
310
311                         if (XGetWindowAttributes
312                             (display, children[j], &attrs)) {
313                                 /* Window must be mapped and same size as display or work space */
314                                 if (attrs.map_state != 0 &&
315                                     ((attrs.width == display_width
316                                       && attrs.height == display_height)
317                                      || (attrs.width == w
318                                          && attrs.height == h))) {
319                                         win = children[j];
320                                         break;
321                                 }
322                         }
323                 }
324
325                 XFree(children);
326                 if (j == n)
327                         break;
328         }
329
330         return win;
331 }
332
333 long get_x11_color(const char *name)
334 {
335         XColor color;
336         color.pixel = 0;
337         if (!XParseColor
338             (display, DefaultColormap(display, screen), name, &color)) {
339                 /* lets check if it's a hex colour with the # missing in front
340                  * if yes, then do something about it
341                  */
342                 char newname[64];
343                 newname[0] = '#';
344                 strncpy(&newname[1], name, 62);
345                 /* now lets try again */
346                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
347                         ERR("can't parse X color '%s'", name);
348                         return 0xFF00FF;
349                 }
350         }
351         if (!XAllocColor
352             (display, DefaultColormap(display, screen), &color))
353                 ERR("can't allocate X color '%s'", name);
354
355         return (long) color.pixel;
356 }
357
358 void create_gc()
359 {
360         XGCValues values;
361         values.graphics_exposures = 0;
362         values.function = GXcopy;
363         window.gc = XCreateGC(display, window.drawable,
364                               GCFunction | GCGraphicsExposures, &values);
365 }
366
367 #endif /* X11 */