Fix segfault in X11-related code
[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, window.title, NULL, argv,
278                                         argc, NULL, &wmHint, &classHint);
279
280                         /* Sets an empty WM_PROTOCOLS property */
281                         XSetWMProtocols(display, window.window, NULL, 0);
282
283                         /* Set window type */
284                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
285                                 Atom prop;
286
287                                 switch (window.type) {
288                                         case TYPE_DESKTOP:
289                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
290                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
291                                                 fflush(stderr);
292                                                 break;
293                                         case TYPE_DOCK:
294                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
295                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
296                                                 fflush(stderr);
297                                                 break;
298                                         case TYPE_PANEL:
299                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
300                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
301                                                 fflush(stderr);
302                                                 break;
303                                         case TYPE_NORMAL:
304                                         default:
305                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
306                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
307                                                 fflush(stderr);
308                                                 break;
309                                 }
310                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
311                                                 PropModeReplace, (unsigned char *) &prop, 1);
312                         }
313
314                         /* Set desired hints */
315
316                         /* Window decorations */
317                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
318                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
319                                    fflush(stderr); */
320
321                                 xa = ATOM(_MOTIF_WM_HINTS);
322                                 if (xa != None) {
323                                         long prop[5] = { 2, 0, 0, 0, 0 };
324                                         XChangeProperty(display, window.window, xa, xa, 32,
325                                                         PropModeReplace, (unsigned char *) prop, 5);
326                                 }
327                         }
328
329                         /* Below other windows */
330                         if (TEST_HINT(window.hints, HINT_BELOW)) {
331                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
332                                    fflush(stderr); */
333
334                                 xa = ATOM(_WIN_LAYER);
335                                 if (xa != None) {
336                                         long prop = 0;
337
338                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
339                                                         PropModeAppend, (unsigned char *) &prop, 1);
340                                 }
341
342                                 xa = ATOM(_NET_WM_STATE);
343                                 if (xa != None) {
344                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
345
346                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
347                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
348                                 }
349                         }
350
351                         /* Above other windows */
352                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
353                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
354                                    fflush(stderr); */
355
356                                 xa = ATOM(_WIN_LAYER);
357                                 if (xa != None) {
358                                         long prop = 6;
359
360                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
361                                                         PropModeAppend, (unsigned char *) &prop, 1);
362                                 }
363
364                                 xa = ATOM(_NET_WM_STATE);
365                                 if (xa != None) {
366                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
367
368                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
369                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
370                                 }
371                         }
372
373                         /* Sticky */
374                         if (TEST_HINT(window.hints, HINT_STICKY)) {
375                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
376                                    fflush(stderr); */
377
378                                 xa = ATOM(_NET_WM_DESKTOP);
379                                 if (xa != None) {
380                                         CARD32 xa_prop = 0xFFFFFFFF;
381
382                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
383                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
384                                 }
385
386                                 xa = ATOM(_NET_WM_STATE);
387                                 if (xa != None) {
388                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
389
390                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
391                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
392                                 }
393                         }
394
395                         /* Skip taskbar */
396                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
397                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
398                                    fflush(stderr); */
399
400                                 xa = ATOM(_NET_WM_STATE);
401                                 if (xa != None) {
402                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
403
404                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
405                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
406                                 }
407                         }
408
409                         /* Skip pager */
410                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
411                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
412                                    fflush(stderr); */
413
414                                 xa = ATOM(_NET_WM_STATE);
415                                 if (xa != None) {
416                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
417
418                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
419                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
420                                 }
421                         }
422                 }
423
424                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
425                                 window.window);
426                 fflush(stderr);
427
428                 XMapWindow(display, window.window);
429
430         } else
431 #endif /* OWN_WINDOW */
432         {
433                 XWindowAttributes attrs;
434
435                 if (!window.window) {
436                         window.window = find_desktop_window(&window.root, &window.desktop);
437                 }
438
439                 if (XGetWindowAttributes(display, window.window, &attrs)) {
440                         window.width = attrs.width;
441                         window.height = attrs.height;
442                 }
443
444                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
445         }
446
447         /* Drawable is same as window. This may be changed by double buffering. */
448         window.drawable = window.window;
449
450 #ifdef HAVE_XDBE
451         if (use_xdbe) {
452                 int major, minor;
453
454                 if (!XdbeQueryExtension(display, &major, &minor)) {
455                         use_xdbe = 0;
456                 } else {
457                         window.back_buffer = XdbeAllocateBackBufferName(display,
458                                         window.window, XdbeBackground);
459                         if (window.back_buffer != None) {
460                                 window.drawable = window.back_buffer;
461                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
462                         } else {
463                                 use_xdbe = 0;
464                         }
465                 }
466                 if (!use_xdbe) {
467                         NORM_ERR("failed to set up double buffer");
468                 }
469         }
470         if (!use_xdbe) {
471                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
472         }
473 #endif
474         window.visual = DefaultVisual(display, DefaultScreen(display));
475         window.colourmap = DefaultColormap(display, DefaultScreen(display));
476 #ifdef IMLIB2
477         {
478                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
479         }
480 #endif /* IMLIB2 */
481         XFlush(display);
482
483         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
484 #ifdef OWN_WINDOW
485                         | (own_window ? (StructureNotifyMask |
486                                         ButtonPressMask | ButtonReleaseMask) : 0)
487 #endif
488                         );
489 }
490
491 static Window find_subwindow(Window win, int w, int h)
492 {
493         unsigned int i, j;
494         Window troot, parent, *children;
495         unsigned int n;
496
497         /* search subwindows with same size as display or work area */
498
499         for (i = 0; i < 10; i++) {
500                 XQueryTree(display, win, &troot, &parent, &children, &n);
501
502                 for (j = 0; j < n; j++) {
503                         XWindowAttributes attrs;
504
505                         if (XGetWindowAttributes(display, children[j], &attrs)) {
506                                 /* Window must be mapped and same size as display or
507                                  * work space */
508                                 if (attrs.map_state != 0 && ((attrs.width == display_width
509                                                                 && attrs.height == display_height)
510                                                         || (attrs.width == w && attrs.height == h))) {
511                                         win = children[j];
512                                         break;
513                                 }
514                         }
515                 }
516
517                 XFree(children);
518                 if (j == n) {
519                         break;
520                 }
521         }
522
523         return win;
524 }
525
526 long get_x11_color(const char *name)
527 {
528         XColor color;
529
530         color.pixel = 0;
531         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
532                 /* lets check if it's a hex colour with the # missing in front
533                  * if yes, then do something about it */
534                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
535
536                 newname[0] = '#';
537                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
538                 /* now lets try again */
539                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
540                                         &color)) {
541                         NORM_ERR("can't parse X color '%s'", name);
542                         return 0xFF00FF;
543                 }
544         }
545         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
546                 NORM_ERR("can't allocate X color '%s'", name);
547         }
548
549         return (long) color.pixel;
550 }
551
552 void create_gc(void)
553 {
554         XGCValues values;
555
556         values.graphics_exposures = 0;
557         values.function = GXcopy;
558         window.gc = XCreateGC(display, window.drawable,
559                         GCFunction | GCGraphicsExposures, &values);
560 }
561
562 //Get current desktop number
563 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
564 {
565         Atom actual_type;
566         int actual_format;
567         unsigned long nitems;
568         unsigned long bytes_after;
569         unsigned char *prop = NULL;
570         struct information *current_info = &info;
571
572         if (atom == None) return;
573
574         if ( (XGetWindowProperty( current_display, root, atom,
575                                         0, 1L, False, XA_CARDINAL,
576                                         &actual_type, &actual_format, &nitems,
577                                         &bytes_after, &prop ) == Success ) &&
578                         (actual_type == XA_CARDINAL) &&
579                         (nitems == 1L) && (actual_format == 32) ) {
580                 current_info->x11.desktop.current = prop[0]+1;
581         }
582         if(prop) {
583                 XFree(prop);
584         }
585 }
586
587 //Get total number of available desktops
588 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
589 {
590         Atom actual_type;
591         int actual_format;
592         unsigned long nitems;
593         unsigned long bytes_after;
594         unsigned char *prop = NULL;
595         struct information *current_info = &info;
596
597         if (atom == None) return;
598
599         if ( (XGetWindowProperty( current_display, root, atom,
600                                         0, 1L, False, XA_CARDINAL,
601                                         &actual_type, &actual_format, &nitems,
602                                         &bytes_after, &prop ) == Success ) &&
603                         (actual_type == XA_CARDINAL) &&
604                         (nitems == 1L) && (actual_format == 32) ) {
605                 current_info->x11.desktop.number = prop[0];
606         }
607         if(prop) {
608                 XFree(prop);
609         }
610 }
611
612 //Get all desktop names
613 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
614 {
615         Atom actual_type;
616         int actual_format;
617         unsigned long nitems;
618         unsigned long bytes_after;
619         unsigned char *prop = NULL;
620         struct information *current_info = &info;
621
622         if (atom == None) return;
623
624         if ( (XGetWindowProperty( current_display, root, atom,
625                                         0, (~0L), False, ATOM(UTF8_STRING),
626                                         &actual_type, &actual_format, &nitems,
627                                         &bytes_after, &prop ) == Success ) &&
628                         (actual_type == ATOM(UTF8_STRING)) &&
629                         (nitems > 0L) && (actual_format == 8) ) {
630
631                 if(current_info->x11.desktop.all_names) {
632                         free(current_info->x11.desktop.all_names);
633                         current_info->x11.desktop.all_names = NULL;
634                 }
635                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
636                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
637                 current_info->x11.desktop.nitems = nitems;
638         }
639         if(prop) {
640                 XFree(prop);
641         }
642 }
643
644 //Get current desktop name
645 static inline void get_x11_desktop_current_name(char *names)
646 {
647         struct information *current_info = &info;
648         unsigned int i = 0, j = 0;
649         int k = 0;
650
651         while ( i < current_info->x11.desktop.nitems ) {
652                 if ( names[i++] == '\0' ) {
653                         if ( ++k == current_info->x11.desktop.current ) {
654                                 if (current_info->x11.desktop.name) {
655                                         free(current_info->x11.desktop.name);
656                                         current_info->x11.desktop.name = NULL;
657                                 }
658                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
659                                 //desktop names can be empty but should always be not null
660                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
661                                 break;
662                         }
663                         j = i;
664                 }
665         }
666 }
667
668 void get_x11_desktop_info(Display *current_display, Atom atom)
669 {
670         Window root;
671         static Atom atom_current, atom_number, atom_names;
672         struct information *current_info = &info;
673         XWindowAttributes window_attributes;
674
675         root = RootWindow(current_display, current_info->x11.monitor.current);
676
677         /* Check if we initialise else retrieve changed property */
678         if (atom == 0) {
679                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
680                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
681                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
682                 get_x11_desktop_current(current_display, root, atom_current);
683                 get_x11_desktop_number(current_display, root, atom_number);
684                 get_x11_desktop_names(current_display, root, atom_names);
685                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
686
687                 /* Set the PropertyChangeMask on the root window, if not set */
688                 XGetWindowAttributes(display, root, &window_attributes);
689                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
690                         XSetWindowAttributes attributes;
691                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
692                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
693                         XGetWindowAttributes(display, root, &window_attributes);
694                 }
695         } else {
696                 if (atom == atom_current) {
697                         get_x11_desktop_current(current_display, root, atom_current);
698                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
699                 } else if (atom == atom_number) {
700                         get_x11_desktop_number(current_display, root, atom_number);
701                 } else if (atom == atom_names) {
702                         get_x11_desktop_names(current_display, root, atom_names);
703                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
704                 }
705         }
706 }
707
708 void update_x11info(void)
709 {
710         struct information *current_info = &info;
711         if (!x_initialised == YES)
712                 return;
713         current_info->x11.monitor.number = XScreenCount(display);
714         current_info->x11.monitor.current = XDefaultScreen(display);
715 }
716
717 #ifdef OWN_WINDOW
718 /* reserve window manager space */
719 void set_struts(int sidenum)
720 {
721         Atom strut;
722         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
723                 /* reserve space at left, right, top, bottom */
724                 signed long sizes[12] = {0};
725                 int i;
726
727                 /* define strut depth */
728                 switch (sidenum) {
729                         case 0:
730                                 /* left side */
731                                 sizes[0] = window.x + window.width;
732                                 break;
733                         case 1:
734                                 /* right side */
735                                 sizes[1] = display_width - window.x;
736                                 break;
737                         case 2:
738                                 /* top side */
739                                 sizes[2] = window.y + window.height;
740                                 break;
741                         case 3:
742                                 /* bottom side */
743                                 sizes[3] = display_height - window.y;
744                                 break;
745                 }
746
747                 /* define partial strut length */
748                 if (sidenum <= 1) {
749                         sizes[4 + (sidenum*2)] = window.y;
750                         sizes[5 + (sidenum*2)] = window.y + window.height;
751                 } else if (sidenum <= 3) {
752                         sizes[4 + (sidenum*2)] = window.x;
753                         sizes[5 + (sidenum*2)] = window.x + window.width;
754                 }
755
756                 /* check constraints */
757                 for (i = 0; i < 12; i++) {
758                         if (sizes[i] < 0) {
759                                 sizes[i] = 0;
760                         } else {
761                                 if (i <= 1 || i >= 8) {
762                                         if (sizes[i] > display_width) {
763                                                 sizes[i] = display_width;
764                                         }
765                                 } else {
766                                         if (sizes[i] > display_height) {
767                                                 sizes[i] = display_height;
768                                         }
769                                 }
770                         }
771                 }
772
773                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
774                                 PropModeReplace, (unsigned char *) &sizes, 4);
775
776                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
777                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
778                                         PropModeReplace, (unsigned char *) &sizes, 12);
779                 }
780         }
781 }
782 #endif /* OWN_WINDOW */
783
784 #ifdef HAVE_XDBE
785 void xdbe_swap_buffers(void)
786 {
787         if (use_xdbe) {
788                 XdbeSwapInfo swap;
789
790                 swap.swap_window = window.window;
791                 swap.swap_action = XdbeBackground;
792                 XdbeSwapBuffers(display, &swap, 1);
793         }
794 }
795 #endif /* HAVE_XDBE */
796