Fix some minor memleaks, fix crash on reload.
[monky] / src / x11.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "logging.h"
34 #include "common.h"
35
36 #include "x11.h"
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xmd.h>
40 #include <X11/Xutil.h>
41 #ifdef IMLIB2
42 #include "imlib2.h"
43 #endif /* IMLIB2 */
44
45 #ifdef XFT
46 #include <X11/Xft/Xft.h>
47 int use_xft = 0;
48 #endif
49
50 #ifdef HAVE_XDBE
51 int use_xdbe;
52 #endif
53
54 #ifdef USE_ARGB
55 int use_argb_visual;
56 int own_window_argb_value;
57 int have_argb_visual;
58 #endif /* USE_ARGB */
59
60 /* some basic X11 stuff */
61 Display *display = NULL;
62 int display_width;
63 int display_height;
64 int screen;
65 static int set_transparent;
66 static int background_colour;
67
68 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
69 int workarea[4];
70
71 /* Window stuff */
72 struct conky_window window;
73 char window_created = 0;
74
75 /* local prototypes */
76 static void update_workarea(void);
77 static Window find_desktop_window(Window *p_root, Window *p_desktop);
78 static Window find_subwindow(Window win, int w, int h);
79
80 /* X11 initializer */
81 void init_X11(const char *disp)
82 {
83         if (!display) {
84                 if ((display = XOpenDisplay(disp)) == NULL) {
85                         CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(disp));
86                 }
87         }
88
89         screen = DefaultScreen(display);
90         display_width = DisplayWidth(display, screen);
91         display_height = DisplayHeight(display, screen);
92
93         get_x11_desktop_info(display, 0);
94
95         update_workarea();
96 }
97
98 static void update_workarea(void)
99 {
100         /* default work area is display */
101         workarea[0] = 0;
102         workarea[1] = 0;
103         workarea[2] = display_width;
104         workarea[3] = display_height;
105 }
106
107 /* Find root window and desktop window.
108  * Return desktop window on success,
109  * and set root and desktop byref return values.
110  * Return 0 on failure. */
111 static Window find_desktop_window(Window *p_root, Window *p_desktop)
112 {
113         Atom type;
114         int format, i;
115         unsigned long nitems, bytes;
116         unsigned int n;
117         Window root = RootWindow(display, screen);
118         Window win = root;
119         Window troot, parent, *children;
120         unsigned char *buf = NULL;
121
122         if (!p_root || !p_desktop) {
123                 return 0;
124         }
125
126         /* some window managers set __SWM_VROOT to some child of root window */
127
128         XQueryTree(display, root, &troot, &parent, &children, &n);
129         for (i = 0; i < (int) n; i++) {
130                 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
131                                         False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
132                                 == Success && type == XA_WINDOW) {
133                         win = *(Window *) buf;
134                         XFree(buf);
135                         XFree(children);
136                         fprintf(stderr,
137                                         PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
138                                         win);
139                         fflush(stderr);
140                         *p_root = win;
141                         *p_desktop = win;
142                         return win;
143                 }
144
145                 if (buf) {
146                         XFree(buf);
147                         buf = 0;
148                 }
149         }
150         XFree(children);
151
152         /* get subwindows from root */
153         win = find_subwindow(root, -1, -1);
154
155         update_workarea();
156
157         win = find_subwindow(win, workarea[2], workarea[3]);
158
159         if (buf) {
160                 XFree(buf);
161                 buf = 0;
162         }
163
164         if (win != root) {
165                 fprintf(stderr,
166                                 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
167                                 win, root);
168         } else {
169                 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
170         }
171
172         fflush(stderr);
173
174         *p_root = root;
175         *p_desktop = win;
176
177         return win;
178 }
179
180 static int colour_set = -1;
181 /* if no argb visual is configured sets background to ParentRelative for the Window and all parents,
182    else real transparency is used */
183 void set_transparent_background(Window win, int alpha)
184 {
185         (void)alpha; /* disable warnings when unused */
186
187 #ifdef USE_ARGB
188         if (have_argb_visual) {
189                 // real transparency
190                 if (set_transparent) {
191                         XSetWindowBackground(display, win, 0x00);
192                 } else if (colour_set != background_colour) {
193                         XSetWindowBackground(display, win,
194                                 background_colour | (alpha << 24));
195                         colour_set = background_colour;
196                 }
197         } else {
198 #endif /* USE_ARGB */
199         // pseudo transparency
200         
201         if (set_transparent) {
202                 Window parent = win;
203                 unsigned int i;
204
205                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
206                         Window r, *children;
207                         unsigned int n;
208
209                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
210
211                         XQueryTree(display, parent, &r, &parent, &children, &n);
212                         XFree(children);
213                 }
214         } else if (colour_set != background_colour) {
215                 XSetWindowBackground(display, win, background_colour);
216                 colour_set = background_colour;
217         }
218 #ifdef USE_ARGB
219         }
220 #endif /* USE_ARGB */
221 }
222
223 #ifdef USE_ARGB
224 static int get_argb_visual(Visual** visual, int *depth) {
225         /* code from gtk project, gdk_screen_get_rgba_visual */
226         XVisualInfo visual_template;
227         XVisualInfo *visual_list;
228         int nxvisuals = 0, i;
229         
230         visual_template.screen = screen;
231         visual_list = XGetVisualInfo (display, VisualScreenMask,
232                                 &visual_template, &nxvisuals);
233         for (i = 0; i < nxvisuals; i++) {
234                 if (visual_list[i].depth == 32 &&
235                          (visual_list[i].red_mask   == 0xff0000 &&
236                           visual_list[i].green_mask == 0x00ff00 &&
237                           visual_list[i].blue_mask  == 0x0000ff)) {
238                         *visual = visual_list[i].visual;
239                         *depth = visual_list[i].depth;
240                         DBGP("Found ARGB Visual");
241                         XFree(visual_list);
242                         return 1;
243                 }
244         }
245         // no argb visual available
246         DBGP("No ARGB Visual found");
247         XFree(visual_list);
248         return 0;
249 }
250 #endif /* USE_ARGB */
251
252 void destroy_window(void)
253 {
254 #ifdef XFT
255         if(window.xftdraw) {
256                 XftDrawDestroy(window.xftdraw);
257         }
258 #endif
259         if(window.gc) {
260                 XFreeGC(display, window.gc);
261         }
262         memset(&window, 0, sizeof(struct conky_window));
263         colour_set = -1;
264 }
265
266 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
267                 char **argv, int argc)
268 {
269         /* There seems to be some problems with setting transparent background
270          * (on fluxbox this time). It doesn't happen always and I don't know why it
271          * happens but I bet the bug is somewhere here. */
272         set_transparent = set_trans;
273         background_colour = back_colour;
274         window_created = 1;
275
276 #ifdef OWN_WINDOW
277         if (own_window) {
278                 int depth = 0, flags;
279                 Visual *visual = NULL;
280                 
281                 if (!find_desktop_window(&window.root, &window.desktop)) {
282                         return;
283                 }
284                 
285 #ifdef USE_ARGB
286                 if (use_argb_visual && get_argb_visual(&visual, &depth)) {
287                         have_argb_visual = 1;
288                         window.visual = visual;
289                         window.colourmap = XCreateColormap(display,
290                                 DefaultRootWindow(display), window.visual, AllocNone);
291                 } else {
292 #endif /* USE_ARGB */
293                         window.visual = DefaultVisual(display, screen);
294                         window.colourmap = DefaultColormap(display, screen);
295                         depth = CopyFromParent;
296                         visual = CopyFromParent;
297 #ifdef USE_ARGB
298                 }
299 #endif /* USE_ARGB */
300
301                 if (window.type == TYPE_OVERRIDE) {
302
303                         /* An override_redirect True window.
304                          * No WM hints or button processing needed. */
305                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
306                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
307                                 True, 0, 0 };
308 #ifdef USE_ARGB
309                         if (have_argb_visual) {
310                                 attrs.colormap = window.colourmap;
311                                 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
312                         } else {
313 #endif /* USE_ARGB */
314                                 flags = CWBackPixel | CWOverrideRedirect;
315 #ifdef USE_ARGB
316                         }
317 #endif /* USE_ARGB */
318
319                         /* Parent is desktop window (which might be a child of root) */
320                         window.window = XCreateWindow(display, window.desktop, window.x,
321                                         window.y, w, h, 0, depth, InputOutput, visual,
322                                         flags, &attrs);
323
324                         XLowerWindow(display, window.window);
325
326                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
327                         fflush(stderr);
328                 } else { /* window.type != TYPE_OVERRIDE */
329
330                         /* A window managed by the window manager.
331                          * Process hints and buttons. */
332                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
333                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
334                                         ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
335
336                         XClassHint classHint;
337                         XWMHints wmHint;
338                         Atom xa;
339                         
340 #ifdef USE_ARGB
341                         if (have_argb_visual) {
342                                 attrs.colormap = window.colourmap;
343                                 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
344                         } else {
345 #endif /* USE_ARGB */
346                                 flags = CWBackPixel | CWOverrideRedirect;
347 #ifdef USE_ARGB
348                         }
349 #endif /* USE_ARGB */
350
351                         if (window.type == TYPE_DOCK) {
352                                 window.x = window.y = 0;
353                         }
354                         /* Parent is root window so WM can take control */
355                         window.window = XCreateWindow(display, window.root, window.x,
356                                         window.y, w, h, 0, depth, InputOutput, visual,
357                                         flags, &attrs);
358
359                         classHint.res_name = window.class_name;
360                         classHint.res_class = classHint.res_name;
361
362                         wmHint.flags = InputHint | StateHint;
363                         /* allow decorated windows to be given input focus by WM */
364                         wmHint.input =
365                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
366                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
367                                 wmHint.initial_state = WithdrawnState;
368                         } else {
369                                 wmHint.initial_state = NormalState;
370                         }
371
372                         XmbSetWMProperties(display, window.window, NULL, NULL, argv,
373                                         argc, NULL, &wmHint, &classHint);
374                         XStoreName(display, window.window, window.title);
375
376                         /* Sets an empty WM_PROTOCOLS property */
377                         XSetWMProtocols(display, window.window, NULL, 0);
378
379                         /* Set window type */
380                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
381                                 Atom prop;
382
383                                 switch (window.type) {
384                                         case TYPE_DESKTOP:
385                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
386                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
387                                                 fflush(stderr);
388                                                 break;
389                                         case TYPE_DOCK:
390                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
391                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
392                                                 fflush(stderr);
393                                                 break;
394                                         case TYPE_PANEL:
395                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
396                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
397                                                 fflush(stderr);
398                                                 break;
399                                         case TYPE_NORMAL:
400                                         default:
401                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
402                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
403                                                 fflush(stderr);
404                                                 break;
405                                 }
406                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
407                                                 PropModeReplace, (unsigned char *) &prop, 1);
408                         }
409
410                         /* Set desired hints */
411
412                         /* Window decorations */
413                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
414                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
415                                    fflush(stderr); */
416
417                                 xa = ATOM(_MOTIF_WM_HINTS);
418                                 if (xa != None) {
419                                         long prop[5] = { 2, 0, 0, 0, 0 };
420                                         XChangeProperty(display, window.window, xa, xa, 32,
421                                                         PropModeReplace, (unsigned char *) prop, 5);
422                                 }
423                         }
424
425                         /* Below other windows */
426                         if (TEST_HINT(window.hints, HINT_BELOW)) {
427                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
428                                    fflush(stderr); */
429
430                                 xa = ATOM(_WIN_LAYER);
431                                 if (xa != None) {
432                                         long prop = 0;
433
434                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
435                                                         PropModeAppend, (unsigned char *) &prop, 1);
436                                 }
437
438                                 xa = ATOM(_NET_WM_STATE);
439                                 if (xa != None) {
440                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
441
442                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
443                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
444                                 }
445                         }
446
447                         /* Above other windows */
448                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
449                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
450                                    fflush(stderr); */
451
452                                 xa = ATOM(_WIN_LAYER);
453                                 if (xa != None) {
454                                         long prop = 6;
455
456                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
457                                                         PropModeAppend, (unsigned char *) &prop, 1);
458                                 }
459
460                                 xa = ATOM(_NET_WM_STATE);
461                                 if (xa != None) {
462                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
463
464                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
465                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
466                                 }
467                         }
468
469                         /* Sticky */
470                         if (TEST_HINT(window.hints, HINT_STICKY)) {
471                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
472                                    fflush(stderr); */
473
474                                 xa = ATOM(_NET_WM_DESKTOP);
475                                 if (xa != None) {
476                                         CARD32 xa_prop = 0xFFFFFFFF;
477
478                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
479                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
480                                 }
481
482                                 xa = ATOM(_NET_WM_STATE);
483                                 if (xa != None) {
484                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
485
486                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
487                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
488                                 }
489                         }
490
491                         /* Skip taskbar */
492                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
493                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
494                                    fflush(stderr); */
495
496                                 xa = ATOM(_NET_WM_STATE);
497                                 if (xa != None) {
498                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
499
500                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
501                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
502                                 }
503                         }
504
505                         /* Skip pager */
506                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
507                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
508                                    fflush(stderr); */
509
510                                 xa = ATOM(_NET_WM_STATE);
511                                 if (xa != None) {
512                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
513
514                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
515                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
516                                 }
517                         }
518                 }
519
520                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
521                                 window.window);
522                 fflush(stderr);
523
524                 XMapWindow(display, window.window);
525
526         } else
527 #endif /* OWN_WINDOW */
528         {
529                 XWindowAttributes attrs;
530
531                 if (!window.window) {
532                         window.window = find_desktop_window(&window.root, &window.desktop);
533                 }
534
535                 if (XGetWindowAttributes(display, window.window, &attrs)) {
536                         window.width = attrs.width;
537                         window.height = attrs.height;
538                 }
539
540                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
541         }
542
543         /* Drawable is same as window. This may be changed by double buffering. */
544         window.drawable = window.window;
545
546 #ifdef HAVE_XDBE
547         if (use_xdbe) {
548                 int major, minor;
549
550                 if (!XdbeQueryExtension(display, &major, &minor)) {
551                         use_xdbe = 0;
552                 } else {
553                         window.back_buffer = XdbeAllocateBackBufferName(display,
554                                         window.window, XdbeBackground);
555                         if (window.back_buffer != None) {
556                                 window.drawable = window.back_buffer;
557                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
558                         } else {
559                                 use_xdbe = 0;
560                         }
561                 }
562                 if (!use_xdbe) {
563                         NORM_ERR("failed to set up double buffer");
564                 }
565         }
566         if (!use_xdbe) {
567                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
568         }
569 #endif
570 #ifdef IMLIB2
571         {
572                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
573         }
574 #endif /* IMLIB2 */
575         XFlush(display);
576
577         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
578 #ifdef OWN_WINDOW
579                         | (own_window ? (StructureNotifyMask |
580                                         ButtonPressMask | ButtonReleaseMask) : 0)
581 #endif
582                         );
583 }
584
585 static Window find_subwindow(Window win, int w, int h)
586 {
587         unsigned int i, j;
588         Window troot, parent, *children;
589         unsigned int n;
590
591         /* search subwindows with same size as display or work area */
592
593         for (i = 0; i < 10; i++) {
594                 XQueryTree(display, win, &troot, &parent, &children, &n);
595
596                 for (j = 0; j < n; j++) {
597                         XWindowAttributes attrs;
598
599                         if (XGetWindowAttributes(display, children[j], &attrs)) {
600                                 /* Window must be mapped and same size as display or
601                                  * work space */
602                                 if (attrs.map_state != 0 && ((attrs.width == display_width
603                                                                 && attrs.height == display_height)
604                                                         || (attrs.width == w && attrs.height == h))) {
605                                         win = children[j];
606                                         break;
607                                 }
608                         }
609                 }
610
611                 XFree(children);
612                 if (j == n) {
613                         break;
614                 }
615         }
616
617         return win;
618 }
619
620 void create_gc(void)
621 {
622         XGCValues values;
623
624         values.graphics_exposures = 0;
625         values.function = GXcopy;
626         window.gc = XCreateGC(display, window.drawable,
627                         GCFunction | GCGraphicsExposures, &values);
628 }
629
630 //Get current desktop number
631 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
632 {
633         Atom actual_type;
634         int actual_format;
635         unsigned long nitems;
636         unsigned long bytes_after;
637         unsigned char *prop = NULL;
638         struct information *current_info = &info;
639
640         if (atom == None) return;
641
642         if ( (XGetWindowProperty( current_display, root, atom,
643                                         0, 1L, False, XA_CARDINAL,
644                                         &actual_type, &actual_format, &nitems,
645                                         &bytes_after, &prop ) == Success ) &&
646                         (actual_type == XA_CARDINAL) &&
647                         (nitems == 1L) && (actual_format == 32) ) {
648                 current_info->x11.desktop.current = prop[0]+1;
649         }
650         if(prop) {
651                 XFree(prop);
652         }
653 }
654
655 //Get total number of available desktops
656 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
657 {
658         Atom actual_type;
659         int actual_format;
660         unsigned long nitems;
661         unsigned long bytes_after;
662         unsigned char *prop = NULL;
663         struct information *current_info = &info;
664
665         if (atom == None) return;
666
667         if ( (XGetWindowProperty( current_display, root, atom,
668                                         0, 1L, False, XA_CARDINAL,
669                                         &actual_type, &actual_format, &nitems,
670                                         &bytes_after, &prop ) == Success ) &&
671                         (actual_type == XA_CARDINAL) &&
672                         (nitems == 1L) && (actual_format == 32) ) {
673                 current_info->x11.desktop.number = prop[0];
674         }
675         if(prop) {
676                 XFree(prop);
677         }
678 }
679
680 //Get all desktop names
681 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
682 {
683         Atom actual_type;
684         int actual_format;
685         unsigned long nitems;
686         unsigned long bytes_after;
687         unsigned char *prop = NULL;
688         struct information *current_info = &info;
689
690         if (atom == None) return;
691
692         if ( (XGetWindowProperty( current_display, root, atom,
693                                         0, (~0L), False, ATOM(UTF8_STRING),
694                                         &actual_type, &actual_format, &nitems,
695                                         &bytes_after, &prop ) == Success ) &&
696                         (actual_type == ATOM(UTF8_STRING)) &&
697                         (nitems > 0L) && (actual_format == 8) ) {
698
699                 if(current_info->x11.desktop.all_names) {
700                         free(current_info->x11.desktop.all_names);
701                         current_info->x11.desktop.all_names = NULL;
702                 }
703                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
704                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
705                 current_info->x11.desktop.nitems = nitems;
706         }
707         if(prop) {
708                 XFree(prop);
709         }
710 }
711
712 //Get current desktop name
713 static inline void get_x11_desktop_current_name(char *names)
714 {
715         struct information *current_info = &info;
716         unsigned int i = 0, j = 0;
717         int k = 0;
718
719         while ( i < current_info->x11.desktop.nitems ) {
720                 if ( names[i++] == '\0' ) {
721                         if ( ++k == current_info->x11.desktop.current ) {
722                                 if (current_info->x11.desktop.name) {
723                                         free(current_info->x11.desktop.name);
724                                         current_info->x11.desktop.name = NULL;
725                                 }
726                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
727                                 //desktop names can be empty but should always be not null
728                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
729                                 break;
730                         }
731                         j = i;
732                 }
733         }
734 }
735
736 void get_x11_desktop_info(Display *current_display, Atom atom)
737 {
738         Window root;
739         static Atom atom_current, atom_number, atom_names;
740         struct information *current_info = &info;
741         XWindowAttributes window_attributes;
742
743         root = RootWindow(current_display, current_info->x11.monitor.current);
744
745         /* Check if we initialise else retrieve changed property */
746         if (atom == 0) {
747                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
748                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
749                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
750                 get_x11_desktop_current(current_display, root, atom_current);
751                 get_x11_desktop_number(current_display, root, atom_number);
752                 get_x11_desktop_names(current_display, root, atom_names);
753                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
754
755                 /* Set the PropertyChangeMask on the root window, if not set */
756                 XGetWindowAttributes(display, root, &window_attributes);
757                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
758                         XSetWindowAttributes attributes;
759                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
760                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
761                         XGetWindowAttributes(display, root, &window_attributes);
762                 }
763         } else {
764                 if (atom == atom_current) {
765                         get_x11_desktop_current(current_display, root, atom_current);
766                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
767                 } else if (atom == atom_number) {
768                         get_x11_desktop_number(current_display, root, atom_number);
769                 } else if (atom == atom_names) {
770                         get_x11_desktop_names(current_display, root, atom_names);
771                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
772                 }
773         }
774 }
775
776 void update_x11info(void)
777 {
778         struct information *current_info = &info;
779         if (!x_initialised == YES)
780                 return;
781         current_info->x11.monitor.number = XScreenCount(display);
782         current_info->x11.monitor.current = XDefaultScreen(display);
783 }
784
785 #ifdef OWN_WINDOW
786 /* reserve window manager space */
787 void set_struts(int sidenum)
788 {
789         Atom strut;
790         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
791                 /* reserve space at left, right, top, bottom */
792                 signed long sizes[12] = {0};
793                 int i;
794
795                 /* define strut depth */
796                 switch (sidenum) {
797                         case 0:
798                                 /* left side */
799                                 sizes[0] = window.x + window.width;
800                                 break;
801                         case 1:
802                                 /* right side */
803                                 sizes[1] = display_width - window.x;
804                                 break;
805                         case 2:
806                                 /* top side */
807                                 sizes[2] = window.y + window.height;
808                                 break;
809                         case 3:
810                                 /* bottom side */
811                                 sizes[3] = display_height - window.y;
812                                 break;
813                 }
814
815                 /* define partial strut length */
816                 if (sidenum <= 1) {
817                         sizes[4 + (sidenum*2)] = window.y;
818                         sizes[5 + (sidenum*2)] = window.y + window.height;
819                 } else if (sidenum <= 3) {
820                         sizes[4 + (sidenum*2)] = window.x;
821                         sizes[5 + (sidenum*2)] = window.x + window.width;
822                 }
823
824                 /* check constraints */
825                 for (i = 0; i < 12; i++) {
826                         if (sizes[i] < 0) {
827                                 sizes[i] = 0;
828                         } else {
829                                 if (i <= 1 || i >= 8) {
830                                         if (sizes[i] > display_width) {
831                                                 sizes[i] = display_width;
832                                         }
833                                 } else {
834                                         if (sizes[i] > display_height) {
835                                                 sizes[i] = display_height;
836                                         }
837                                 }
838                         }
839                 }
840
841                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
842                                 PropModeReplace, (unsigned char *) &sizes, 4);
843
844                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
845                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
846                                         PropModeReplace, (unsigned char *) &sizes, 12);
847                 }
848         }
849 }
850 #endif /* OWN_WINDOW */
851
852 #ifdef HAVE_XDBE
853 void xdbe_swap_buffers(void)
854 {
855         if (use_xdbe) {
856                 XdbeSwapInfo swap;
857
858                 swap.swap_window = window.window;
859                 swap.swap_action = XdbeBackground;
860                 XdbeSwapBuffers(display, &swap, 1);
861         }
862 }
863 #endif /* HAVE_XDBE */
864