62310a6b0509a5073c95667d4dc0237ebf783f8c
[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                         return 1;
242                 }
243         }
244         // no argb visual available
245         DBGP("No ARGB Visual found");
246         return 0;
247 }
248 #endif /* USE_ARGB */
249
250 void destroy_window(void)
251 {
252 #ifdef XFT
253         if(window.xftdraw) {
254                 XftDrawDestroy(window.xftdraw);
255         }
256 #endif
257         if(window.gc) {
258                 XFreeGC(display, window.gc);
259         }
260         memset(&window, 0, sizeof(struct conky_window));
261         colour_set = -1;
262 }
263
264 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
265                 char **argv, int argc)
266 {
267         /* There seems to be some problems with setting transparent background
268          * (on fluxbox this time). It doesn't happen always and I don't know why it
269          * happens but I bet the bug is somewhere here. */
270         set_transparent = set_trans;
271         background_colour = back_colour;
272         window_created = 1;
273
274 #ifdef OWN_WINDOW
275         if (own_window) {
276                 int depth = 0, flags;
277                 Visual *visual = NULL;
278                 
279                 if (!find_desktop_window(&window.root, &window.desktop)) {
280                         return;
281                 }
282                 
283 #ifdef USE_ARGB
284                 if (use_argb_visual && get_argb_visual(&visual, &depth)) {
285                         have_argb_visual = 1;
286                         window.visual = visual;
287                         window.colourmap = XCreateColormap(display,
288                                 DefaultRootWindow(display), window.visual, AllocNone);
289                 } else {
290 #endif /* USE_ARGB */
291                         window.visual = DefaultVisual(display, screen);
292                         window.colourmap = DefaultColormap(display, screen);
293                         depth = CopyFromParent;
294                         visual = CopyFromParent;
295 #ifdef USE_ARGB
296                 }
297 #endif /* USE_ARGB */
298
299                 if (window.type == TYPE_OVERRIDE) {
300
301                         /* An override_redirect True window.
302                          * No WM hints or button processing needed. */
303                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
304                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
305                                 True, 0, 0 };
306 #ifdef USE_ARGB
307                         if (have_argb_visual) {
308                                 attrs.colormap = window.colourmap;
309                                 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
310                         } else {
311 #endif /* USE_ARGB */
312                                 flags = CWBackPixel | CWOverrideRedirect;
313 #ifdef USE_ARGB
314                         }
315 #endif /* USE_ARGB */
316
317                         /* Parent is desktop window (which might be a child of root) */
318                         window.window = XCreateWindow(display, window.desktop, window.x,
319                                         window.y, w, h, 0, depth, InputOutput, visual,
320                                         flags, &attrs);
321
322                         XLowerWindow(display, window.window);
323
324                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
325                         fflush(stderr);
326                 } else { /* window.type != TYPE_OVERRIDE */
327
328                         /* A window managed by the window manager.
329                          * Process hints and buttons. */
330                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
331                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
332                                         ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
333
334                         XClassHint classHint;
335                         XWMHints wmHint;
336                         Atom xa;
337                         
338 #ifdef USE_ARGB
339                         if (have_argb_visual) {
340                                 attrs.colormap = window.colourmap;
341                                 flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
342                         } else {
343 #endif /* USE_ARGB */
344                                 flags = CWBackPixel | CWOverrideRedirect;
345 #ifdef USE_ARGB
346                         }
347 #endif /* USE_ARGB */
348
349                         if (window.type == TYPE_DOCK) {
350                                 window.x = window.y = 0;
351                         }
352                         /* Parent is root window so WM can take control */
353                         window.window = XCreateWindow(display, window.root, window.x,
354                                         window.y, w, h, 0, depth, InputOutput, visual,
355                                         flags, &attrs);
356
357                         classHint.res_name = window.class_name;
358                         classHint.res_class = classHint.res_name;
359
360                         wmHint.flags = InputHint | StateHint;
361                         /* allow decorated windows to be given input focus by WM */
362                         wmHint.input =
363                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
364                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
365                                 wmHint.initial_state = WithdrawnState;
366                         } else {
367                                 wmHint.initial_state = NormalState;
368                         }
369
370                         XmbSetWMProperties(display, window.window, NULL, NULL, argv,
371                                         argc, NULL, &wmHint, &classHint);
372                         XStoreName(display, window.window, window.title);
373
374                         /* Sets an empty WM_PROTOCOLS property */
375                         XSetWMProtocols(display, window.window, NULL, 0);
376
377                         /* Set window type */
378                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
379                                 Atom prop;
380
381                                 switch (window.type) {
382                                         case TYPE_DESKTOP:
383                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
384                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
385                                                 fflush(stderr);
386                                                 break;
387                                         case TYPE_DOCK:
388                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
389                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
390                                                 fflush(stderr);
391                                                 break;
392                                         case TYPE_PANEL:
393                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
394                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
395                                                 fflush(stderr);
396                                                 break;
397                                         case TYPE_NORMAL:
398                                         default:
399                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
400                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
401                                                 fflush(stderr);
402                                                 break;
403                                 }
404                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
405                                                 PropModeReplace, (unsigned char *) &prop, 1);
406                         }
407
408                         /* Set desired hints */
409
410                         /* Window decorations */
411                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
412                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
413                                    fflush(stderr); */
414
415                                 xa = ATOM(_MOTIF_WM_HINTS);
416                                 if (xa != None) {
417                                         long prop[5] = { 2, 0, 0, 0, 0 };
418                                         XChangeProperty(display, window.window, xa, xa, 32,
419                                                         PropModeReplace, (unsigned char *) prop, 5);
420                                 }
421                         }
422
423                         /* Below other windows */
424                         if (TEST_HINT(window.hints, HINT_BELOW)) {
425                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
426                                    fflush(stderr); */
427
428                                 xa = ATOM(_WIN_LAYER);
429                                 if (xa != None) {
430                                         long prop = 0;
431
432                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
433                                                         PropModeAppend, (unsigned char *) &prop, 1);
434                                 }
435
436                                 xa = ATOM(_NET_WM_STATE);
437                                 if (xa != None) {
438                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
439
440                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
441                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
442                                 }
443                         }
444
445                         /* Above other windows */
446                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
447                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
448                                    fflush(stderr); */
449
450                                 xa = ATOM(_WIN_LAYER);
451                                 if (xa != None) {
452                                         long prop = 6;
453
454                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
455                                                         PropModeAppend, (unsigned char *) &prop, 1);
456                                 }
457
458                                 xa = ATOM(_NET_WM_STATE);
459                                 if (xa != None) {
460                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
461
462                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
463                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
464                                 }
465                         }
466
467                         /* Sticky */
468                         if (TEST_HINT(window.hints, HINT_STICKY)) {
469                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
470                                    fflush(stderr); */
471
472                                 xa = ATOM(_NET_WM_DESKTOP);
473                                 if (xa != None) {
474                                         CARD32 xa_prop = 0xFFFFFFFF;
475
476                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
477                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
478                                 }
479
480                                 xa = ATOM(_NET_WM_STATE);
481                                 if (xa != None) {
482                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
483
484                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
485                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
486                                 }
487                         }
488
489                         /* Skip taskbar */
490                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
491                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
492                                    fflush(stderr); */
493
494                                 xa = ATOM(_NET_WM_STATE);
495                                 if (xa != None) {
496                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
497
498                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
499                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
500                                 }
501                         }
502
503                         /* Skip pager */
504                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
505                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
506                                    fflush(stderr); */
507
508                                 xa = ATOM(_NET_WM_STATE);
509                                 if (xa != None) {
510                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
511
512                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
513                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
514                                 }
515                         }
516                 }
517
518                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
519                                 window.window);
520                 fflush(stderr);
521
522                 XMapWindow(display, window.window);
523
524         } else
525 #endif /* OWN_WINDOW */
526         {
527                 XWindowAttributes attrs;
528
529                 if (!window.window) {
530                         window.window = find_desktop_window(&window.root, &window.desktop);
531                 }
532
533                 if (XGetWindowAttributes(display, window.window, &attrs)) {
534                         window.width = attrs.width;
535                         window.height = attrs.height;
536                 }
537
538                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
539         }
540
541         /* Drawable is same as window. This may be changed by double buffering. */
542         window.drawable = window.window;
543
544 #ifdef HAVE_XDBE
545         if (use_xdbe) {
546                 int major, minor;
547
548                 if (!XdbeQueryExtension(display, &major, &minor)) {
549                         use_xdbe = 0;
550                 } else {
551                         window.back_buffer = XdbeAllocateBackBufferName(display,
552                                         window.window, XdbeBackground);
553                         if (window.back_buffer != None) {
554                                 window.drawable = window.back_buffer;
555                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
556                         } else {
557                                 use_xdbe = 0;
558                         }
559                 }
560                 if (!use_xdbe) {
561                         NORM_ERR("failed to set up double buffer");
562                 }
563         }
564         if (!use_xdbe) {
565                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
566         }
567 #endif
568 #ifdef IMLIB2
569         {
570                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
571         }
572 #endif /* IMLIB2 */
573         XFlush(display);
574
575         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
576 #ifdef OWN_WINDOW
577                         | (own_window ? (StructureNotifyMask |
578                                         ButtonPressMask | ButtonReleaseMask) : 0)
579 #endif
580                         );
581 }
582
583 static Window find_subwindow(Window win, int w, int h)
584 {
585         unsigned int i, j;
586         Window troot, parent, *children;
587         unsigned int n;
588
589         /* search subwindows with same size as display or work area */
590
591         for (i = 0; i < 10; i++) {
592                 XQueryTree(display, win, &troot, &parent, &children, &n);
593
594                 for (j = 0; j < n; j++) {
595                         XWindowAttributes attrs;
596
597                         if (XGetWindowAttributes(display, children[j], &attrs)) {
598                                 /* Window must be mapped and same size as display or
599                                  * work space */
600                                 if (attrs.map_state != 0 && ((attrs.width == display_width
601                                                                 && attrs.height == display_height)
602                                                         || (attrs.width == w && attrs.height == h))) {
603                                         win = children[j];
604                                         break;
605                                 }
606                         }
607                 }
608
609                 XFree(children);
610                 if (j == n) {
611                         break;
612                 }
613         }
614
615         return win;
616 }
617
618 void create_gc(void)
619 {
620         XGCValues values;
621
622         values.graphics_exposures = 0;
623         values.function = GXcopy;
624         window.gc = XCreateGC(display, window.drawable,
625                         GCFunction | GCGraphicsExposures, &values);
626 }
627
628 //Get current desktop number
629 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
630 {
631         Atom actual_type;
632         int actual_format;
633         unsigned long nitems;
634         unsigned long bytes_after;
635         unsigned char *prop = NULL;
636         struct information *current_info = &info;
637
638         if (atom == None) return;
639
640         if ( (XGetWindowProperty( current_display, root, atom,
641                                         0, 1L, False, XA_CARDINAL,
642                                         &actual_type, &actual_format, &nitems,
643                                         &bytes_after, &prop ) == Success ) &&
644                         (actual_type == XA_CARDINAL) &&
645                         (nitems == 1L) && (actual_format == 32) ) {
646                 current_info->x11.desktop.current = prop[0]+1;
647         }
648         if(prop) {
649                 XFree(prop);
650         }
651 }
652
653 //Get total number of available desktops
654 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
655 {
656         Atom actual_type;
657         int actual_format;
658         unsigned long nitems;
659         unsigned long bytes_after;
660         unsigned char *prop = NULL;
661         struct information *current_info = &info;
662
663         if (atom == None) return;
664
665         if ( (XGetWindowProperty( current_display, root, atom,
666                                         0, 1L, False, XA_CARDINAL,
667                                         &actual_type, &actual_format, &nitems,
668                                         &bytes_after, &prop ) == Success ) &&
669                         (actual_type == XA_CARDINAL) &&
670                         (nitems == 1L) && (actual_format == 32) ) {
671                 current_info->x11.desktop.number = prop[0];
672         }
673         if(prop) {
674                 XFree(prop);
675         }
676 }
677
678 //Get all desktop names
679 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
680 {
681         Atom actual_type;
682         int actual_format;
683         unsigned long nitems;
684         unsigned long bytes_after;
685         unsigned char *prop = NULL;
686         struct information *current_info = &info;
687
688         if (atom == None) return;
689
690         if ( (XGetWindowProperty( current_display, root, atom,
691                                         0, (~0L), False, ATOM(UTF8_STRING),
692                                         &actual_type, &actual_format, &nitems,
693                                         &bytes_after, &prop ) == Success ) &&
694                         (actual_type == ATOM(UTF8_STRING)) &&
695                         (nitems > 0L) && (actual_format == 8) ) {
696
697                 if(current_info->x11.desktop.all_names) {
698                         free(current_info->x11.desktop.all_names);
699                         current_info->x11.desktop.all_names = NULL;
700                 }
701                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
702                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
703                 current_info->x11.desktop.nitems = nitems;
704         }
705         if(prop) {
706                 XFree(prop);
707         }
708 }
709
710 //Get current desktop name
711 static inline void get_x11_desktop_current_name(char *names)
712 {
713         struct information *current_info = &info;
714         unsigned int i = 0, j = 0;
715         int k = 0;
716
717         while ( i < current_info->x11.desktop.nitems ) {
718                 if ( names[i++] == '\0' ) {
719                         if ( ++k == current_info->x11.desktop.current ) {
720                                 if (current_info->x11.desktop.name) {
721                                         free(current_info->x11.desktop.name);
722                                         current_info->x11.desktop.name = NULL;
723                                 }
724                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
725                                 //desktop names can be empty but should always be not null
726                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
727                                 break;
728                         }
729                         j = i;
730                 }
731         }
732 }
733
734 void get_x11_desktop_info(Display *current_display, Atom atom)
735 {
736         Window root;
737         static Atom atom_current, atom_number, atom_names;
738         struct information *current_info = &info;
739         XWindowAttributes window_attributes;
740
741         root = RootWindow(current_display, current_info->x11.monitor.current);
742
743         /* Check if we initialise else retrieve changed property */
744         if (atom == 0) {
745                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
746                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
747                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
748                 get_x11_desktop_current(current_display, root, atom_current);
749                 get_x11_desktop_number(current_display, root, atom_number);
750                 get_x11_desktop_names(current_display, root, atom_names);
751                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
752
753                 /* Set the PropertyChangeMask on the root window, if not set */
754                 XGetWindowAttributes(display, root, &window_attributes);
755                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
756                         XSetWindowAttributes attributes;
757                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
758                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
759                         XGetWindowAttributes(display, root, &window_attributes);
760                 }
761         } else {
762                 if (atom == atom_current) {
763                         get_x11_desktop_current(current_display, root, atom_current);
764                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
765                 } else if (atom == atom_number) {
766                         get_x11_desktop_number(current_display, root, atom_number);
767                 } else if (atom == atom_names) {
768                         get_x11_desktop_names(current_display, root, atom_names);
769                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
770                 }
771         }
772 }
773
774 void update_x11info(void)
775 {
776         struct information *current_info = &info;
777         if (!x_initialised == YES)
778                 return;
779         current_info->x11.monitor.number = XScreenCount(display);
780         current_info->x11.monitor.current = XDefaultScreen(display);
781 }
782
783 #ifdef OWN_WINDOW
784 /* reserve window manager space */
785 void set_struts(int sidenum)
786 {
787         Atom strut;
788         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
789                 /* reserve space at left, right, top, bottom */
790                 signed long sizes[12] = {0};
791                 int i;
792
793                 /* define strut depth */
794                 switch (sidenum) {
795                         case 0:
796                                 /* left side */
797                                 sizes[0] = window.x + window.width;
798                                 break;
799                         case 1:
800                                 /* right side */
801                                 sizes[1] = display_width - window.x;
802                                 break;
803                         case 2:
804                                 /* top side */
805                                 sizes[2] = window.y + window.height;
806                                 break;
807                         case 3:
808                                 /* bottom side */
809                                 sizes[3] = display_height - window.y;
810                                 break;
811                 }
812
813                 /* define partial strut length */
814                 if (sidenum <= 1) {
815                         sizes[4 + (sidenum*2)] = window.y;
816                         sizes[5 + (sidenum*2)] = window.y + window.height;
817                 } else if (sidenum <= 3) {
818                         sizes[4 + (sidenum*2)] = window.x;
819                         sizes[5 + (sidenum*2)] = window.x + window.width;
820                 }
821
822                 /* check constraints */
823                 for (i = 0; i < 12; i++) {
824                         if (sizes[i] < 0) {
825                                 sizes[i] = 0;
826                         } else {
827                                 if (i <= 1 || i >= 8) {
828                                         if (sizes[i] > display_width) {
829                                                 sizes[i] = display_width;
830                                         }
831                                 } else {
832                                         if (sizes[i] > display_height) {
833                                                 sizes[i] = display_height;
834                                         }
835                                 }
836                         }
837                 }
838
839                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
840                                 PropModeReplace, (unsigned char *) &sizes, 4);
841
842                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
843                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
844                                         PropModeReplace, (unsigned char *) &sizes, 12);
845                 }
846         }
847 }
848 #endif /* OWN_WINDOW */
849
850 #ifdef HAVE_XDBE
851 void xdbe_swap_buffers(void)
852 {
853         if (use_xdbe) {
854                 XdbeSwapInfo swap;
855
856                 swap.swap_window = window.window;
857                 swap.swap_action = XdbeBackground;
858                 XdbeSwapBuffers(display, &swap, 1);
859         }
860 }
861 #endif /* HAVE_XDBE */
862