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