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