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