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