added own_window_hints
[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                         fflush(stderr);
123                         return win;
124                 }
125
126                 if (buf) {
127                         XFree(buf);
128                         buf = 0;
129                 }
130         }
131         XFree(children);
132
133         /* get subwindows from root */
134         win = find_subwindow(root, -1, -1);
135
136         update_workarea();
137
138         win = find_subwindow(win, workarea[2], workarea[3]);
139
140         if (buf) {
141                 XFree(buf);
142                 buf = 0;
143         }
144
145         if (win != root)
146                 fprintf(stderr,
147                         "Conky: desktop window (%lx) is subwindow of root window (%lx)\n",win,root);
148         else
149                 fprintf(stderr, "Conky: desktop window (%lx) is root window\n",win);
150
151         fflush(stderr);
152         return win;
153 }
154
155 /* sets background to ParentRelative for the Window and all parents */
156 inline void set_transparent_background(Window win)
157 {
158         static int colour_set = -1;
159         if (set_transparent) {
160                 Window parent = win;
161                 unsigned int i;
162                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
163                         Window r, *children;
164                         unsigned int n;
165                         
166                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
167         
168                         XQueryTree(display, parent, &r, &parent, &children, &n);
169                         XFree(children);
170                         }
171         } else if (colour_set != background_colour) {
172                 XSetWindowBackground(display, win, background_colour);
173                 colour_set = background_colour;
174 }
175         //XClearWindow(display, win); not sure why this was here
176 }
177
178 void init_window(int own_window, int w, int h, int set_trans, int back_colour, char * nodename, 
179                  char **argv, int argc)
180 {
181         /* There seems to be some problems with setting transparent background (on
182          * fluxbox this time). It doesn't happen always and I don't know why it
183          * happens but I bet the bug is somewhere here. */
184         set_transparent = set_trans;
185         background_colour = back_colour;
186
187         nodename = (char *)nodename;
188
189 #ifdef OWN_WINDOW
190         if (own_window) {
191                 {
192                         /* Allow WM control of conky again.  Shielding conky from the WM 
193                          * via override redirect creates more problems than it's worth and
194                          * makes it impossible to use tools like devilspie to manage the
195                          * conky windows beyond the parametsrs we offer.  Also, button 
196                          * press events are now explicitly forwarded to the root window. */
197                         XSetWindowAttributes attrs = {
198                                 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
199                                 StructureNotifyMask|ExposureMask|ButtonPressMask,
200                                 0L,False,0,0 };
201
202                         XClassHint classHint;
203                         XWMHints wmHint;
204                         Atom xa;
205                         char window_title[256];
206
207                         window.root = find_desktop_window();
208
209                         window.window = XCreateWindow(display, window.root, 
210                                                       window.x, window.y, w, h, 0, 
211                                                       CopyFromParent,
212                                                       InputOutput,
213                                                       CopyFromParent,
214                                                       CWBackPixel|CWOverrideRedirect,
215                                                       &attrs);
216
217                         fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
218                         fflush(stderr);
219
220                         classHint.res_name = window.wm_class_name;
221                         classHint.res_class = classHint.res_name;
222
223                         wmHint.flags = InputHint | StateHint;
224                         wmHint.input = False;
225                         wmHint.initial_state = NormalState;
226
227                         sprintf(window_title,WINDOW_NAME_FMT,nodename);
228
229                         XmbSetWMProperties (display, window.window, window_title, NULL, 
230                                             argv, argc,
231                                             NULL, &wmHint, &classHint);
232
233                         /* Sets an empty WM_PROTOCOLS property */
234                         XSetWMProtocols(display,window.window,NULL,0);
235
236
237                         /* Set NORMAL window type for _NET_WM_WINDOW_TYPE. */
238                         xa = ATOM(_NET_WM_WINDOW_TYPE);
239                         if (xa != None) {
240                                 Atom prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
241                                 XChangeProperty(display, window.window, xa,
242                                                 XA_ATOM, 32,
243                                                 PropModeReplace,
244                                                 (unsigned char *) &prop,
245                                                 1);
246                         }
247
248                         /* Set desired hints */
249                         
250                         /* Window decorations */
251                         if (TEST_HINT(window.hints,HINT_UNDECORATED)) {
252                             fprintf(stderr, "Conky: hint - undecorated\n"); fflush(stderr);
253
254                             xa = ATOM(_MOTIF_WM_HINTS);
255                             if (xa != None) {
256                                 long prop[5] = { 2, 0, 0, 0, 0 };
257                                 XChangeProperty(display, window.window, xa,
258                                                 xa, 32, PropModeReplace,
259                                                 (unsigned char *) prop, 5);
260                             }
261                         }
262
263                         /* Below other windows */
264                         if (TEST_HINT(window.hints,HINT_BELOW)) {
265                             fprintf(stderr, "Conky: hint - below\n"); fflush(stderr);
266
267                             xa = ATOM(_WIN_LAYER);
268                             if (xa != None) {
269                                 long prop = 0;
270                                 XChangeProperty(display, window.window, xa,
271                                                 XA_CARDINAL, 32,
272                                                 PropModeReplace,
273                                                 (unsigned char *) &prop, 1);
274                             }
275                         
276                             xa = ATOM("_NET_WM_STATE");
277                             if (xa != None) {
278                                 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
279                                 XChangeProperty(display, window.window, xa,
280                                         XA_ATOM, 32,
281                                         PropModeAppend,
282                                         (unsigned char *) &xa_prop,
283                                         1);
284                             }
285                         }
286
287                         /* Above other windows */
288                         if (TEST_HINT(window.hints,HINT_ABOVE)) {
289                             fprintf(stderr, "Conky: hint - above\n"); fflush(stderr);
290
291                             xa = ATOM(_WIN_LAYER);
292                             if (xa != None) {
293                                 long prop = 6;
294                                 XChangeProperty(display, window.window, xa,
295                                                 XA_CARDINAL, 32,
296                                                 PropModeReplace,
297                                                 (unsigned char *) &prop, 1);
298                             }
299
300                             xa = ATOM("_NET_WM_STATE");
301                             if (xa != None) {
302                                 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
303                                 XChangeProperty(display, window.window, xa,
304                                         XA_ATOM, 32,
305                                         PropModeAppend,
306                                         (unsigned char *) &xa_prop,
307                                         1);
308                             }
309                         }
310
311                         /* Sticky */
312                         if (TEST_HINT(window.hints,HINT_STICKY)) {
313                             fprintf(stderr, "Conky: hint - sticky\n"); fflush(stderr);
314
315                             xa = ATOM("_NET_WM_STATE");
316                             if (xa != None) {
317                                 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
318                                 XChangeProperty(display, window.window, xa,
319                                         XA_ATOM, 32,
320                                         PropModeAppend,
321                                         (unsigned char *) &xa_prop,
322                                         1);
323                             }
324                         }
325
326                         /* Skip taskbar */
327                         if (TEST_HINT(window.hints,HINT_SKIP_TASKBAR)) {
328                             fprintf(stderr, "Conky: hint - skip_taskbar\n"); fflush(stderr);
329
330                             xa = ATOM("_NET_WM_STATE");
331                             if (xa != None) {
332                                 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
333                                 XChangeProperty(display, window.window, xa,
334                                         XA_ATOM, 32,
335                                         PropModeAppend,
336                                         (unsigned char *) &xa_prop,
337                                         1);
338                             }
339                         }
340
341                         /* Skip pager */
342                         if (TEST_HINT(window.hints,HINT_SKIP_PAGER)) {
343                             fprintf(stderr, "Conky: hint - skip_pager\n"); fflush(stderr);
344
345                             xa = ATOM("_NET_WM_STATE");
346                             if (xa != None) {
347                                 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
348                                 XChangeProperty(display, window.window, xa,
349                                         XA_ATOM, 32,
350                                         PropModeAppend,
351                                         (unsigned char *) &xa_prop,
352                                         1);
353                             }
354                         }
355
356
357
358                         XMapWindow(display, window.window);
359
360                 }
361         } else
362 #endif
363                 /* root / desktop window */
364         {
365                 XWindowAttributes attrs;
366
367                 if (!window.window)
368                         window.window = find_desktop_window();
369
370                 if (XGetWindowAttributes(display, window.window, &attrs)) {
371                         window.width = attrs.width;
372                         window.height = attrs.height;
373                 }
374
375                 fprintf(stderr, "Conky: drawing to desktop window\n");
376         }
377
378         /* Drawable is same as window. This may be changed by double buffering. */
379         window.drawable = window.window;
380
381 #ifdef XDBE
382         if (use_xdbe) {
383                 int major, minor;
384                 if (!XdbeQueryExtension(display, &major, &minor)) {
385                         use_xdbe = 0;
386                 } else {
387                         window.back_buffer =
388                             XdbeAllocateBackBufferName(display,
389                                                        window.window,
390                                                        XdbeBackground);
391                         if (window.back_buffer != None) {
392                                 window.drawable = window.back_buffer;
393                                 fprintf(stderr,
394                                         "Conky: drawing to double buffer\n");
395                         } else
396                                 use_xdbe = 0;
397                 }
398                 if (!use_xdbe)
399                         ERR("failed to set up double buffer");
400         }
401         if (!use_xdbe)
402                 fprintf(stderr, "Conky: drawing to single buffer\n");
403 #endif
404
405         XFlush(display);
406
407         /*set_transparent_background(window.window); must be done after double buffer stuff? */
408 #ifdef OWN_WINDOW
409         /*if (own_window) {
410         set_transparent_background(window.window);
411                 XClearWindow(display, window.window);
412 }*/
413 #endif
414
415         XSelectInput(display, window.window, ExposureMask
416 #ifdef OWN_WINDOW
417                      | (own_window
418                         ? (StructureNotifyMask | PropertyChangeMask | ButtonPressMask) : 0)
419 #endif
420             );
421 }
422
423 static Window find_subwindow(Window win, int w, int h)
424 {
425         unsigned int i, j;
426         Window troot, parent, *children;
427         unsigned int n;
428
429         /* search subwindows with same size as display or work area */
430
431         for (i = 0; i < 10; i++) {
432                 XQueryTree(display, win, &troot, &parent, &children, &n);
433
434                 for (j = 0; j < n; j++) {
435                         XWindowAttributes attrs;
436
437                         if (XGetWindowAttributes
438                             (display, children[j], &attrs)) {
439                                 /* Window must be mapped and same size as display or work space */
440                                 if (attrs.map_state != 0 &&
441                                     ((attrs.width == display_width
442                                       && attrs.height == display_height)
443                                      || (attrs.width == w
444                                          && attrs.height == h))) {
445                                         win = children[j];
446                                         break;
447                                 }
448                         }
449                 }
450
451                 XFree(children);
452                 if (j == n)
453                         break;
454         }
455
456         return win;
457 }
458
459 long get_x11_color(const char *name)
460 {
461         XColor color;
462         color.pixel = 0;
463         if (!XParseColor
464             (display, DefaultColormap(display, screen), name, &color)) {
465                 /* lets check if it's a hex colour with the # missing in front
466                  * if yes, then do something about it
467                  */
468                 char newname[64];
469                 newname[0] = '#';
470                 strncpy(&newname[1], name, 62);
471                 /* now lets try again */
472                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
473                         ERR("can't parse X color '%s'", name);
474                         return 0xFF00FF;
475                 }
476         }
477         if (!XAllocColor
478             (display, DefaultColormap(display, screen), &color))
479                 ERR("can't allocate X color '%s'", name);
480
481         return (long) color.pixel;
482 }
483
484 void create_gc()
485 {
486         XGCValues values;
487         values.graphics_exposures = 0;
488         values.function = GXcopy;
489         window.gc = XCreateGC(display, window.drawable,
490                               GCFunction | GCGraphicsExposures, &values);
491 }
492
493 #endif /* X11 */