minor macro changes
[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 HAVE_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                 if ( !find_desktop_window( &window.root, &window.desktop ) )
205                         return;
206
207                 if (window.type == TYPE_OVERRIDE) {
208         
209                         /* 
210                            An override_redirect True window.  No WM hints or button processing needed. 
211                         */
212                         XSetWindowAttributes attrs = {
213                                 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
214                                 StructureNotifyMask|ExposureMask,
215                                 0L,
216                                 True,
217                                 0,0 };
218
219                         /* Parent is desktop window (which might be a child of root) */
220                         window.window = XCreateWindow(display, 
221                                                    window.desktop, 
222                                                    window.x, window.y, w, h, 0, 
223                                                    CopyFromParent,
224                                                    InputOutput,
225                                                    CopyFromParent,
226                                                    CWBackPixel|CWOverrideRedirect,
227                                                    &attrs);
228
229                         XLowerWindow(display, window.window);   
230
231                         fprintf(stderr, "Conky: window type - override\n"); fflush(stderr);
232
233                 }
234
235                 else { /* window.type != TYPE_OVERRIDE */
236
237                         /* 
238                            A window managed by the window manager.  Process hints and buttons. 
239                         */
240                         XSetWindowAttributes attrs = {
241                                 ParentRelative,0L,0,0L,0,0,Always,0L,0L,False,
242                                 StructureNotifyMask|ExposureMask|ButtonPressMask|ButtonReleaseMask,
243                                 0L,
244                                 False,
245                                 0,0 };
246
247                         XClassHint classHint;
248                         XWMHints wmHint;
249                         Atom xa;
250                         char window_title[256];
251
252                         /* Parent is root window so WM can take control */
253                         window.window = XCreateWindow(display, 
254                                                    window.root, 
255                                                    window.x, window.y, w, h, 0, 
256                                                    CopyFromParent,
257                                                    InputOutput,
258                                                    CopyFromParent,
259                                                    CWBackPixel|CWOverrideRedirect,
260                                                    &attrs);
261
262                         classHint.res_name = window.wm_class_name;
263                         classHint.res_class = classHint.res_name;
264
265                         wmHint.flags = InputHint | StateHint;
266                         wmHint.input = False;
267                         wmHint.initial_state = NormalState;
268
269                         sprintf(window_title,WINDOW_NAME_FMT,nodename);
270
271                         XmbSetWMProperties (display, window.window, window_title, NULL, 
272                                             argv, argc,
273                                             NULL, &wmHint, &classHint);
274
275                         /* Sets an empty WM_PROTOCOLS property */
276                         XSetWMProtocols(display,window.window,NULL,0);
277
278
279                         /* Set window type */
280                         if ( (xa = ATOM(_NET_WM_WINDOW_TYPE)) != None ) 
281                         {
282                                 Atom prop;
283                                 switch(window.type) {
284                                 case TYPE_DESKTOP:
285                                         {
286                                         prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
287                                         fprintf(stderr, "Conky: window type - desktop\n"); fflush(stderr);
288                                         }
289                                         break;                          
290                                 case TYPE_NORMAL:
291                                 default:
292                                         {
293                                         prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
294                                         fprintf(stderr, "Conky: window type - normal\n"); fflush(stderr);
295                                         }
296                                         break;
297                                 }
298                                 XChangeProperty(display, window.window, xa,
299                                                 XA_ATOM, 32,
300                                                 PropModeReplace,
301                                                 (unsigned char *) &prop, 1);
302                         }
303
304                         /* Set desired hints */
305                         
306                         /* Window decorations */
307                         if (TEST_HINT(window.hints,HINT_UNDECORATED)) {
308                             fprintf(stderr, "Conky: hint - undecorated\n"); fflush(stderr);
309
310                             xa = ATOM(_MOTIF_WM_HINTS);
311                             if (xa != None) {
312                                 long prop[5] = { 2, 0, 0, 0, 0 };
313                                 XChangeProperty(display, window.window, xa,
314                                                 xa, 32, PropModeReplace,
315                                                 (unsigned char *) prop, 5);
316                             }
317                         }
318
319                         /* Below other windows */
320                         if (TEST_HINT(window.hints,HINT_BELOW)) {
321                             fprintf(stderr, "Conky: hint - below\n"); fflush(stderr);
322
323                             xa = ATOM(_WIN_LAYER);
324                             if (xa != None) {
325                                 long prop = 0;
326                                 XChangeProperty(display, window.window, xa,
327                                                 XA_CARDINAL, 32,
328                                                 PropModeAppend,
329                                                 (unsigned char *) &prop, 1);
330                             }
331                         
332                             xa = ATOM(_NET_WM_STATE);
333                             if (xa != None) {
334                                 Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
335                                 XChangeProperty(display, window.window, xa,
336                                         XA_ATOM, 32,
337                                         PropModeAppend,
338                                         (unsigned char *) &xa_prop,
339                                         1);
340                             }
341                         }
342
343                         /* Above other windows */
344                         if (TEST_HINT(window.hints,HINT_ABOVE)) {
345                             fprintf(stderr, "Conky: hint - above\n"); fflush(stderr);
346
347                             xa = ATOM(_WIN_LAYER);
348                             if (xa != None) {
349                                 long prop = 6;
350                                 XChangeProperty(display, window.window, xa,
351                                                 XA_CARDINAL, 32,
352                                                 PropModeAppend,
353                                                 (unsigned char *) &prop, 1);
354                             }
355
356                             xa = ATOM(_NET_WM_STATE);
357                             if (xa != None) {
358                                 Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
359                                 XChangeProperty(display, window.window, xa,
360                                         XA_ATOM, 32,
361                                         PropModeAppend,
362                                         (unsigned char *) &xa_prop,
363                                         1);
364                             }
365                         }
366
367                         /* Sticky */
368                         if (TEST_HINT(window.hints,HINT_STICKY)) {
369                             fprintf(stderr, "Conky: hint - sticky\n"); fflush(stderr);
370
371                             xa = ATOM(_NET_WM_DESKTOP);
372                             if (xa != None) {
373                                 CARD32 xa_prop = 0xFFFFFFFF;
374                                 XChangeProperty(display, window.window, xa,
375                                         XA_CARDINAL, 32,
376                                         PropModeAppend,
377                                         (unsigned char *) &xa_prop,
378                                         1);
379                             }
380
381                             xa = ATOM(_NET_WM_STATE);
382                             if (xa != None) {
383                                 Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
384                                 XChangeProperty(display, window.window, xa,
385                                         XA_ATOM, 32,
386                                         PropModeAppend,
387                                         (unsigned char *) &xa_prop,
388                                         1);
389                             }
390                         }
391
392                         /* Skip taskbar */
393                         if (TEST_HINT(window.hints,HINT_SKIP_TASKBAR)) {
394                             fprintf(stderr, "Conky: hint - skip_taskbar\n"); fflush(stderr);
395
396                             xa = ATOM(_NET_WM_STATE);
397                             if (xa != None) {
398                                 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
399                                 XChangeProperty(display, window.window, xa,
400                                         XA_ATOM, 32,
401                                         PropModeAppend,
402                                         (unsigned char *) &xa_prop,
403                                         1);
404                             }
405                         }
406
407                         /* Skip pager */
408                         if (TEST_HINT(window.hints,HINT_SKIP_PAGER)) {
409                             fprintf(stderr, "Conky: hint - skip_pager\n"); fflush(stderr);
410
411                             xa = ATOM(_NET_WM_STATE);
412                             if (xa != None) {
413                                 Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
414                                 XChangeProperty(display, window.window, xa,
415                                         XA_ATOM, 32,
416                                         PropModeAppend,
417                                         (unsigned char *) &xa_prop,
418                                         1);
419                             }
420                         }
421
422                 } /* else { window.type != TYPE_OVERRIDE */
423                         
424                 fprintf(stderr, "Conky: drawing to created window (%lx)\n", window.window);
425                 fflush(stderr);
426
427                 XMapWindow(display, window.window);
428
429         } else /* if (own_window) { */
430 #endif
431                 /* root / desktop window */
432         {
433                 XWindowAttributes attrs;
434
435                 if (!window.window)
436                         window.window = find_desktop_window( &window.root, &window.desktop );
437
438                 if (XGetWindowAttributes(display, window.window, &attrs)) {
439                         window.width = attrs.width;
440                         window.height = attrs.height;
441                 }
442
443                 fprintf(stderr, "Conky: drawing to desktop window\n");
444         }
445
446         /* Drawable is same as window. This may be changed by double buffering. */
447         window.drawable = window.window;
448
449 #ifdef HAVE_XDBE
450         if (use_xdbe) {
451                 int major, minor;
452                 if (!XdbeQueryExtension(display, &major, &minor)) {
453                         use_xdbe = 0;
454                 } else {
455                         window.back_buffer =
456                             XdbeAllocateBackBufferName(display,
457                                                        window.window,
458                                                        XdbeBackground);
459                         if (window.back_buffer != None) {
460                                 window.drawable = window.back_buffer;
461                                 fprintf(stderr,
462                                         "Conky: drawing to double buffer\n");
463                         } else
464                                 use_xdbe = 0;
465                 }
466                 if (!use_xdbe)
467                         ERR("failed to set up double buffer");
468         }
469         if (!use_xdbe)
470                 fprintf(stderr, "Conky: drawing to single buffer\n");
471 #endif
472
473         XFlush(display);
474
475         /*set_transparent_background(window.window); must be done after double buffer stuff? */
476 #ifdef OWN_WINDOW
477         /*if (own_window) {
478         set_transparent_background(window.window);
479                 XClearWindow(display, window.window);
480 }*/
481 #endif
482
483         XSelectInput(display, window.window, ExposureMask
484 #ifdef OWN_WINDOW
485                      | (own_window
486                         ? (StructureNotifyMask | PropertyChangeMask | 
487                            ButtonPressMask | ButtonReleaseMask) : 0)
488 #endif
489             );
490 }
491
492 static Window find_subwindow(Window win, int w, int h)
493 {
494         unsigned int i, j;
495         Window troot, parent, *children;
496         unsigned int n;
497
498         /* search subwindows with same size as display or work area */
499
500         for (i = 0; i < 10; i++) {
501                 XQueryTree(display, win, &troot, &parent, &children, &n);
502
503                 for (j = 0; j < n; j++) {
504                         XWindowAttributes attrs;
505
506                         if (XGetWindowAttributes
507                             (display, children[j], &attrs)) {
508                                 /* Window must be mapped and same size as display or work space */
509                                 if (attrs.map_state != 0 &&
510                                     ((attrs.width == display_width
511                                       && attrs.height == display_height)
512                                      || (attrs.width == w
513                                          && attrs.height == h))) {
514                                         win = children[j];
515                                         break;
516                                 }
517                         }
518                 }
519
520                 XFree(children);
521                 if (j == n)
522                         break;
523         }
524
525         return win;
526 }
527
528 long get_x11_color(const char *name)
529 {
530         XColor color;
531         color.pixel = 0;
532         if (!XParseColor
533             (display, DefaultColormap(display, screen), name, &color)) {
534                 /* lets check if it's a hex colour with the # missing in front
535                  * if yes, then do something about it
536                  */
537                 char newname[64];
538                 newname[0] = '#';
539                 strncpy(&newname[1], name, 62);
540                 /* now lets try again */
541                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0], &color)) {
542                         ERR("can't parse X color '%s'", name);
543                         return 0xFF00FF;
544                 }
545         }
546         if (!XAllocColor
547             (display, DefaultColormap(display, screen), &color))
548                 ERR("can't allocate X color '%s'", name);
549
550         return (long) color.pixel;
551 }
552
553 void create_gc()
554 {
555         XGCValues values;
556         values.graphics_exposures = 0;
557         values.function = GXcopy;
558         window.gc = XCreateGC(display, window.drawable,
559                               GCFunction | GCGraphicsExposures, &values);
560 }
561
562 #endif /* X11 */