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