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