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