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