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