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