set extended wm hint _NET_WM_WINDOW_TYPE to _UTILITY
[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_window_to_draw();
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_window_to_draw()
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: drawing to window from __SWM_VROOT property\n");
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: drawing to subwindow of root window (%lx)\n",
147                         win);
148         else
149                 fprintf(stderr, "Conky: drawing to root window\n");
150
151         return win;
152 }
153
154 /* sets background to ParentRelative for the Window and all parents */
155 inline void set_transparent_background(Window win)
156 {
157         static int colour_set = -1;
158         if (set_transparent) {
159                 Window parent = win;
160                 unsigned int i;
161                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
162                         Window r, *children;
163                         unsigned int n;
164                         
165                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
166         
167                         XQueryTree(display, parent, &r, &parent, &children, &n);
168                         XFree(children);
169                         }
170         } else if (colour_set != background_colour) {
171                 XSetWindowBackground(display, win, background_colour);
172                 colour_set = background_colour;
173 }
174         //XClearWindow(display, win); not sure why this was here
175 }
176
177 #if defined OWN_WINDOW
178 void init_window(int own_window, char* wm_class_name, int w, int h, int l, int fixed_pos, int set_trans, int back_colour, char * nodename)
179 #else
180 void init_window(int own_window, int w, int h, int l, int set_trans, int back_colour, char * nodename)
181 #endif
182 {
183         /* There seems to be some problems with setting transparent background (on
184          * fluxbox this time). It doesn't happen always and I don't know why it
185          * happens but I bet the bug is somewhere here. */
186         set_transparent = set_trans;
187         background_colour = back_colour;
188         char * window_name = NULL;
189 #ifdef OWN_WINDOW
190         if (own_window) {
191
192                 /* looks like root pixmap isn't needed for anything */
193                 {
194                         XSetWindowAttributes attrs;
195                         XClassHint class_hints;
196
197                         /* just test color
198                         attrs.background_pixel = get_x11_color("green");
199                         */
200
201                         window.window = XCreateWindow(display, RootWindow(display, screen), window.x, window.y, w, h, 0, CopyFromParent,        /* depth */
202                                                       CopyFromParent,   /* class */
203                                                       CopyFromParent,   /* visual */
204                                                       CWBackPixel, &attrs);
205 /*                      XWMHints wmhints;
206
207                         this doesn't work properly
208
209                         wmhints.flags = StateHint;
210                         wmhints.initial_state = WithdrawnState;
211                         XSetWMHints(display, window.window, &wmhints);*/
212
213
214
215                         class_hints.res_class = wm_class_name;
216                         class_hints.res_name = wm_class_name;
217                         XSetClassHint(display, window.window,
218                                       &class_hints);
219
220                         /*set_transparent_background(window.window);*/
221                         window_name = (char *) malloc(strlen(WINDOW_NAME_FMT) + strlen(nodename)+1);
222                         sprintf(window_name, WINDOW_NAME_FMT, nodename);
223                         XStoreName(display, window.window, window_name);
224                         free(window_name);
225                         XClearWindow(display, window.window);
226
227                         if (!fixed_pos)
228                                 XMoveWindow(display, window.window, window.x,
229                         window.y);
230                 }
231
232                 {
233                         /* turn off decorations */
234                         Atom a =
235                             XInternAtom(display, "_MOTIF_WM_HINTS", True);
236                         if (a != None) {
237                                 long prop[5] = { 2, 0, 0, 0, 0 };
238                                 XChangeProperty(display, window.window, a,
239                                                 a, 32, PropModeReplace,
240                                                 (unsigned char *) prop, 5);
241                         }
242
243                         /* set window sticky (to all desktops) */
244                         a = XInternAtom(display, "_NET_WM_DESKTOP", True);
245                         if (a != None) {
246                                 long prop = 0xFFFFFFFF;
247                                 XChangeProperty(display, window.window, a,
248                                                 XA_CARDINAL, 32,
249                                                 PropModeReplace,
250                                                 (unsigned char *) &prop,
251                                                 1);
252                         }
253
254                         /* PHK: Set EWMH _NET_WM_WINDOW_TYPE to _NET_WM_WINDOW_TYPE_UTILITY. 
255                            This addresses the issue where conky can disappear under the desktop
256                            while running in its own window with property on_bottom=yes and the 
257                            desktop gets raised over it.  As a utility window, this wont happen. 
258                         */
259                         a = XInternAtom(display, "_NET_WM_WINDOW_TYPE", True);
260                         if (a != None) {
261                                 Atom prop = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", True);
262                                 XChangeProperty(display, window.window, a,
263                                                 XA_ATOM, 32,
264                                                 PropModeReplace,
265                                                 (unsigned char *) &prop,
266                                                 1);
267                         }
268
269                         if(l) {
270                                 /* make sure the layer is on the bottom */
271                                 a = XInternAtom(display, "_WIN_LAYER", True);
272                                 if (a != None) {
273                                         long prop = 0;
274                                         XChangeProperty(display, window.window, a,
275                                                         XA_CARDINAL, 32,
276                                                         PropModeReplace,
277                                                         (unsigned char *) &prop, 1);
278                                 }
279                         }
280                 }
281
282                 XMapWindow(display, window.window);
283         } else
284 #endif
285                 /* root / desktop window */
286         {
287                 XWindowAttributes attrs;
288
289                 if (!window.window)
290                         window.window = find_window_to_draw();
291
292                 if (XGetWindowAttributes(display, window.window, &attrs)) {
293                         window.width = attrs.width;
294                         window.height = attrs.height;
295                 }
296         }
297
298         /* Drawable is same as window. This may be changed by double buffering. */
299         window.drawable = window.window;
300
301 #ifdef XDBE
302         if (use_xdbe) {
303                 int major, minor;
304                 if (!XdbeQueryExtension(display, &major, &minor)) {
305                         use_xdbe = 0;
306                 } else {
307                         window.back_buffer =
308                             XdbeAllocateBackBufferName(display,
309                                                        window.window,
310                                                        XdbeBackground);
311                         if (window.back_buffer != None) {
312                                 window.drawable = window.back_buffer;
313                                 fprintf(stderr,
314                                         "Conky: drawing to double buffer\n");
315                         } else
316                                 use_xdbe = 0;
317                 }
318                 if (!use_xdbe)
319                         ERR("failed to set up double buffer");
320         }
321         if (!use_xdbe)
322                 fprintf(stderr, "Conky: drawing to single buffer\n");
323 #endif
324
325         XFlush(display);
326
327         /*set_transparent_background(window.window); must be done after double buffer stuff? */
328 #ifdef OWN_WINDOW
329         /*if (own_window) {
330         set_transparent_background(window.window);
331                 XClearWindow(display, window.window);
332 }*/
333 #endif
334
335         XSelectInput(display, window.window, ExposureMask
336 #ifdef OWN_WINDOW
337                      | (own_window
338                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
339 #endif
340             );
341 }
342
343 static Window find_subwindow(Window win, int w, int h)
344 {
345         unsigned int i, j;
346         Window troot, parent, *children;
347         unsigned int n;
348
349         /* search subwindows with same size as display or work area */
350
351         for (i = 0; i < 10; i++) {
352                 XQueryTree(display, win, &troot, &parent, &children, &n);
353
354                 for (j = 0; j < n; j++) {
355                         XWindowAttributes attrs;
356
357                         if (XGetWindowAttributes
358                             (display, children[j], &attrs)) {
359                                 /* Window must be mapped and same size as display or work space */
360                                 if (attrs.map_state != 0 &&
361                                     ((attrs.width == display_width
362                                       && attrs.height == display_height)
363                                      || (attrs.width == w
364                                          && attrs.height == h))) {
365                                         win = children[j];
366                                         break;
367                                 }
368                         }
369                 }
370
371                 XFree(children);
372                 if (j == n)
373                         break;
374         }
375
376         return win;
377 }
378
379 long get_x11_color(const char *name)
380 {
381         XColor color;
382         color.pixel = 0;
383         if (!XParseColor
384             (display, DefaultColormap(display, screen), name, &color)) {
385                 /* lets check if it's a hex colour with the # missing in front
386                  * if yes, then do something about it
387                  */
388                 char newname[64];
389                 newname[0] = '#';
390                 strncpy(&newname[1], name, 62);
391                 /* now lets try again */
392                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
393                         ERR("can't parse X color '%s'", name);
394                         return 0xFF00FF;
395                 }
396         }
397         if (!XAllocColor
398             (display, DefaultColormap(display, screen), &color))
399                 ERR("can't allocate X color '%s'", name);
400
401         return (long) color.pixel;
402 }
403
404 void create_gc()
405 {
406         XGCValues values;
407         values.graphics_exposures = 0;
408         values.function = GXcopy;
409         window.gc = XCreateGC(display, window.drawable,
410                               GCFunction | GCGraphicsExposures, &values);
411 }
412
413 #endif /* X11 */