using EWM hints STICKY/SKIP_PAGER/SKIP_TASKBAR; window type NORMAL
[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: Use EWMH window type NORMAL for _NET_WM_WINDOW_TYPE. 
255                            For XFCE 4+, keeps the window from going under the desktop. */
256                         a = XInternAtom(display, "_NET_WM_WINDOW_TYPE", True);
257                         if (a != None) {
258                                 Atom prop = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", True);
259                                 XChangeProperty(display, window.window, a,
260                                                 XA_ATOM, 32,
261                                                 PropModeReplace,
262                                                 (unsigned char *) &prop,
263                                                 1);
264                         }
265
266                         /* PHK: Add EWMH hint STICKY to _NET_WM_STATE */
267                         a = XInternAtom(display, "_NET_WM_STATE", True);
268                         if (a != None) {
269                                 Atom prop = XInternAtom(display, "_NET_WM_STATE_STICKY", True);
270                                 XChangeProperty(display, window.window, a,
271                                                 XA_ATOM, 32,
272                                                 PropModeAppend,
273                                                 (unsigned char *) &prop,
274                                                 1);
275                         }
276
277                         /* PHK: Add EWMH hint SKIP_TASKBAR to _NET_WM_STATE */
278                         a = XInternAtom(display, "_NET_WM_STATE", True);
279                         if (a != None) {
280                                 Atom prop = XInternAtom(display, "_NET_WM_STATE_SKIP_TASKBAR", True);
281                                 XChangeProperty(display, window.window, a,
282                                                 XA_ATOM, 32,
283                                                 PropModeAppend,
284                                                 (unsigned char *) &prop,
285                                                 1);
286                         }
287
288                         /* PHK: Add EWMH hint SKIP_PAGER to _NET_WM_STATE */
289                         a = XInternAtom(display, "_NET_WM_STATE", True);
290                         if (a != None) {
291                                 Atom prop = XInternAtom(display, "_NET_WM_STATE_SKIP_PAGER", True);
292                                 XChangeProperty(display, window.window, a,
293                                                 XA_ATOM, 32,
294                                                 PropModeAppend,
295                                                 (unsigned char *) &prop,
296                                                 1);
297                         }
298
299                         if(l) {
300                                 /* make sure the layer is on the bottom */
301                                 a = XInternAtom(display, "_WIN_LAYER", True);
302                                 if (a != None) {
303                                         long prop = 0;
304                                         XChangeProperty(display, window.window, a,
305                                                         XA_CARDINAL, 32,
306                                                         PropModeReplace,
307                                                         (unsigned char *) &prop, 1);
308                                 }
309                                 /* PHK: also append EWMH hint for BELOW also */
310                                 a = XInternAtom(display, "_NET_WM_STATE", True);
311                                 if (a != None) {
312                                         Atom prop = XInternAtom(display, "_NET_WM_STATE_BELOW", True);
313                                         XChangeProperty(display, window.window, a,
314                                                 XA_ATOM, 32,
315                                                 PropModeAppend,
316                                                 (unsigned char *) &prop,
317                                                 1);
318                                 }
319                         }
320                 }
321
322                 XMapWindow(display, window.window);
323         } else
324 #endif
325                 /* root / desktop window */
326         {
327                 XWindowAttributes attrs;
328
329                 if (!window.window)
330                         window.window = find_window_to_draw();
331
332                 if (XGetWindowAttributes(display, window.window, &attrs)) {
333                         window.width = attrs.width;
334                         window.height = attrs.height;
335                 }
336         }
337
338         /* Drawable is same as window. This may be changed by double buffering. */
339         window.drawable = window.window;
340
341 #ifdef XDBE
342         if (use_xdbe) {
343                 int major, minor;
344                 if (!XdbeQueryExtension(display, &major, &minor)) {
345                         use_xdbe = 0;
346                 } else {
347                         window.back_buffer =
348                             XdbeAllocateBackBufferName(display,
349                                                        window.window,
350                                                        XdbeBackground);
351                         if (window.back_buffer != None) {
352                                 window.drawable = window.back_buffer;
353                                 fprintf(stderr,
354                                         "Conky: drawing to double buffer\n");
355                         } else
356                                 use_xdbe = 0;
357                 }
358                 if (!use_xdbe)
359                         ERR("failed to set up double buffer");
360         }
361         if (!use_xdbe)
362                 fprintf(stderr, "Conky: drawing to single buffer\n");
363 #endif
364
365         XFlush(display);
366
367         /*set_transparent_background(window.window); must be done after double buffer stuff? */
368 #ifdef OWN_WINDOW
369         /*if (own_window) {
370         set_transparent_background(window.window);
371                 XClearWindow(display, window.window);
372 }*/
373 #endif
374
375         XSelectInput(display, window.window, ExposureMask
376 #ifdef OWN_WINDOW
377                      | (own_window
378                         ? (StructureNotifyMask | PropertyChangeMask) : 0)
379 #endif
380             );
381 }
382
383 static Window find_subwindow(Window win, int w, int h)
384 {
385         unsigned int i, j;
386         Window troot, parent, *children;
387         unsigned int n;
388
389         /* search subwindows with same size as display or work area */
390
391         for (i = 0; i < 10; i++) {
392                 XQueryTree(display, win, &troot, &parent, &children, &n);
393
394                 for (j = 0; j < n; j++) {
395                         XWindowAttributes attrs;
396
397                         if (XGetWindowAttributes
398                             (display, children[j], &attrs)) {
399                                 /* Window must be mapped and same size as display or work space */
400                                 if (attrs.map_state != 0 &&
401                                     ((attrs.width == display_width
402                                       && attrs.height == display_height)
403                                      || (attrs.width == w
404                                          && attrs.height == h))) {
405                                         win = children[j];
406                                         break;
407                                 }
408                         }
409                 }
410
411                 XFree(children);
412                 if (j == n)
413                         break;
414         }
415
416         return win;
417 }
418
419 long get_x11_color(const char *name)
420 {
421         XColor color;
422         color.pixel = 0;
423         if (!XParseColor
424             (display, DefaultColormap(display, screen), name, &color)) {
425                 /* lets check if it's a hex colour with the # missing in front
426                  * if yes, then do something about it
427                  */
428                 char newname[64];
429                 newname[0] = '#';
430                 strncpy(&newname[1], name, 62);
431                 /* now lets try again */
432                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
433                         ERR("can't parse X color '%s'", name);
434                         return 0xFF00FF;
435                 }
436         }
437         if (!XAllocColor
438             (display, DefaultColormap(display, screen), &color))
439                 ERR("can't allocate X color '%s'", name);
440
441         return (long) color.pixel;
442 }
443
444 void create_gc()
445 {
446         XGCValues values;
447         values.graphics_exposures = 0;
448         values.function = GXcopy;
449         window.gc = XCreateGC(display, window.drawable,
450                               GCFunction | GCGraphicsExposures, &values);
451 }
452
453 #endif /* X11 */