Revert "Uhh..ansohus"
[monky] / src / x11.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "config.h"
33 #include "conky.h"
34 #include "logging.h"
35 #include "common.h"
36
37 #include "x11.h"
38 #include <X11/Xlib.h>
39 #include <X11/Xatom.h>
40 #include <X11/Xmd.h>
41 #include <X11/Xutil.h>
42 #ifdef IMLIB2
43 #include "imlib2.h"
44 #endif /* IMLIB2 */
45
46 #ifdef XFT
47 #include <X11/Xft/Xft.h>
48 int use_xft = 0;
49 #endif
50
51 #ifdef HAVE_XDBE
52 int use_xdbe;
53 #endif
54
55 /* some basic X11 stuff */
56 Display *display = NULL;
57 int display_width;
58 int display_height;
59 int screen;
60 static int set_transparent;
61 static int background_colour;
62
63 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
64 int workarea[4];
65
66 /* Window stuff */
67 struct conky_window window;
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         if(window.xftdraw) {
201                 XftDrawDestroy(window.xftdraw);
202         }
203         if(window.gc) {
204                 XFreeGC(display, window.gc);
205         }
206         memset(&window, 0, sizeof(struct conky_window));
207 }
208
209 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
210                 char **argv, int argc)
211 {
212         /* There seems to be some problems with setting transparent background
213          * (on fluxbox this time). It doesn't happen always and I don't know why it
214          * happens but I bet the bug is somewhere here. */
215         set_transparent = set_trans;
216         background_colour = back_colour;
217
218 #ifdef OWN_WINDOW
219         if (own_window) {
220                 if (!find_desktop_window(&window.root, &window.desktop)) {
221                         return;
222                 }
223
224                 if (window.type == TYPE_OVERRIDE) {
225
226                         /* An override_redirect True window.
227                          * No WM hints or button processing needed. */
228                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
229                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
230                                 True, 0, 0 };
231
232                         /* Parent is desktop window (which might be a child of root) */
233                         window.window = XCreateWindow(display, window.desktop, window.x,
234                                         window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
235                                         CWBackPixel | CWOverrideRedirect, &attrs);
236
237                         XLowerWindow(display, window.window);
238
239                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
240                         fflush(stderr);
241                 } else { /* window.type != TYPE_OVERRIDE */
242
243                         /* A window managed by the window manager.
244                          * Process hints and buttons. */
245                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
246                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
247                                         ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
248
249                         XClassHint classHint;
250                         XWMHints wmHint;
251                         Atom xa;
252
253                         if (window.type == TYPE_DOCK) {
254                                 window.x = window.y = 0;
255                         }
256                         /* Parent is root window so WM can take control */
257                         window.window = XCreateWindow(display, window.root, window.x,
258                                         window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
259                                         CWBackPixel | CWOverrideRedirect, &attrs);
260
261                         classHint.res_name = window.class_name;
262                         classHint.res_class = classHint.res_name;
263
264                         wmHint.flags = InputHint | StateHint;
265                         /* allow decorated windows to be given input focus by WM */
266                         wmHint.input =
267                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
268                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
269                                 wmHint.initial_state = WithdrawnState;
270                         } else {
271                                 wmHint.initial_state = NormalState;
272                         }
273
274                         XmbSetWMProperties(display, window.window, window.title, NULL, argv,
275                                         argc, NULL, &wmHint, &classHint);
276
277                         /* Sets an empty WM_PROTOCOLS property */
278                         XSetWMProtocols(display, window.window, NULL, 0);
279
280                         /* Set window type */
281                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
282                                 Atom prop;
283
284                                 switch (window.type) {
285                                         case TYPE_DESKTOP:
286                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
287                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
288                                                 fflush(stderr);
289                                                 break;
290                                         case TYPE_DOCK:
291                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
292                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
293                                                 fflush(stderr);
294                                                 break;
295                                         case TYPE_PANEL:
296                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
297                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
298                                                 fflush(stderr);
299                                                 break;
300                                         case TYPE_NORMAL:
301                                         default:
302                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
303                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
304                                                 fflush(stderr);
305                                                 break;
306                                 }
307                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
308                                                 PropModeReplace, (unsigned char *) &prop, 1);
309                         }
310
311                         /* Set desired hints */
312
313                         /* Window decorations */
314                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
315                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
316                                    fflush(stderr); */
317
318                                 xa = ATOM(_MOTIF_WM_HINTS);
319                                 if (xa != None) {
320                                         long prop[5] = { 2, 0, 0, 0, 0 };
321                                         XChangeProperty(display, window.window, xa, xa, 32,
322                                                         PropModeReplace, (unsigned char *) prop, 5);
323                                 }
324                         }
325
326                         /* Below other windows */
327                         if (TEST_HINT(window.hints, HINT_BELOW)) {
328                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
329                                    fflush(stderr); */
330
331                                 xa = ATOM(_WIN_LAYER);
332                                 if (xa != None) {
333                                         long prop = 0;
334
335                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
336                                                         PropModeAppend, (unsigned char *) &prop, 1);
337                                 }
338
339                                 xa = ATOM(_NET_WM_STATE);
340                                 if (xa != None) {
341                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
342
343                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
344                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
345                                 }
346                         }
347
348                         /* Above other windows */
349                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
350                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
351                                    fflush(stderr); */
352
353                                 xa = ATOM(_WIN_LAYER);
354                                 if (xa != None) {
355                                         long prop = 6;
356
357                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
358                                                         PropModeAppend, (unsigned char *) &prop, 1);
359                                 }
360
361                                 xa = ATOM(_NET_WM_STATE);
362                                 if (xa != None) {
363                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
364
365                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
366                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
367                                 }
368                         }
369
370                         /* Sticky */
371                         if (TEST_HINT(window.hints, HINT_STICKY)) {
372                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
373                                    fflush(stderr); */
374
375                                 xa = ATOM(_NET_WM_DESKTOP);
376                                 if (xa != None) {
377                                         CARD32 xa_prop = 0xFFFFFFFF;
378
379                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
380                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
381                                 }
382
383                                 xa = ATOM(_NET_WM_STATE);
384                                 if (xa != None) {
385                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
386
387                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
388                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
389                                 }
390                         }
391
392                         /* Skip taskbar */
393                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
394                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
395                                    fflush(stderr); */
396
397                                 xa = ATOM(_NET_WM_STATE);
398                                 if (xa != None) {
399                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
400
401                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
402                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
403                                 }
404                         }
405
406                         /* Skip pager */
407                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
408                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
409                                    fflush(stderr); */
410
411                                 xa = ATOM(_NET_WM_STATE);
412                                 if (xa != None) {
413                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
414
415                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
416                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
417                                 }
418                         }
419                 }
420
421                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
422                                 window.window);
423                 fflush(stderr);
424
425                 XMapWindow(display, window.window);
426
427         } else
428 #endif /* OWN_WINDOW */
429         {
430                 XWindowAttributes attrs;
431
432                 if (!window.window) {
433                         window.window = find_desktop_window(&window.root, &window.desktop);
434                 }
435
436                 if (XGetWindowAttributes(display, window.window, &attrs)) {
437                         window.width = attrs.width;
438                         window.height = attrs.height;
439                 }
440
441                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
442         }
443
444         /* Drawable is same as window. This may be changed by double buffering. */
445         window.drawable = window.window;
446
447 #ifdef HAVE_XDBE
448         if (use_xdbe) {
449                 int major, minor;
450
451                 if (!XdbeQueryExtension(display, &major, &minor)) {
452                         use_xdbe = 0;
453                 } else {
454                         window.back_buffer = XdbeAllocateBackBufferName(display,
455                                         window.window, XdbeBackground);
456                         if (window.back_buffer != None) {
457                                 window.drawable = window.back_buffer;
458                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
459                         } else {
460                                 use_xdbe = 0;
461                         }
462                 }
463                 if (!use_xdbe) {
464                         NORM_ERR("failed to set up double buffer");
465                 }
466         }
467         if (!use_xdbe) {
468                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
469         }
470 #endif
471         window.visual = DefaultVisual(display, DefaultScreen(display));
472         window.colourmap = DefaultColormap(display, DefaultScreen(display));
473 #ifdef IMLIB2
474         {
475                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
476         }
477 #endif /* IMLIB2 */
478         XFlush(display);
479
480         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
481 #ifdef OWN_WINDOW
482                         | (own_window ? (StructureNotifyMask |
483                                         ButtonPressMask | ButtonReleaseMask) : 0)
484 #endif
485                         );
486 }
487
488 static Window find_subwindow(Window win, int w, int h)
489 {
490         unsigned int i, j;
491         Window troot, parent, *children;
492         unsigned int n;
493
494         /* search subwindows with same size as display or work area */
495
496         for (i = 0; i < 10; i++) {
497                 XQueryTree(display, win, &troot, &parent, &children, &n);
498
499                 for (j = 0; j < n; j++) {
500                         XWindowAttributes attrs;
501
502                         if (XGetWindowAttributes(display, children[j], &attrs)) {
503                                 /* Window must be mapped and same size as display or
504                                  * work space */
505                                 if (attrs.map_state != 0 && ((attrs.width == display_width
506                                                                 && attrs.height == display_height)
507                                                         || (attrs.width == w && attrs.height == h))) {
508                                         win = children[j];
509                                         break;
510                                 }
511                         }
512                 }
513
514                 XFree(children);
515                 if (j == n) {
516                         break;
517                 }
518         }
519
520         return win;
521 }
522
523 long get_x11_color(const char *name)
524 {
525         XColor color;
526
527         color.pixel = 0;
528         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
529                 /* lets check if it's a hex colour with the # missing in front
530                  * if yes, then do something about it */
531                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
532
533                 newname[0] = '#';
534                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
535                 /* now lets try again */
536                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
537                                         &color)) {
538                         NORM_ERR("can't parse X color '%s'", name);
539                         return 0xFF00FF;
540                 }
541         }
542         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
543                 NORM_ERR("can't allocate X color '%s'", name);
544         }
545
546         return (long) color.pixel;
547 }
548
549 void create_gc(void)
550 {
551         XGCValues values;
552
553         values.graphics_exposures = 0;
554         values.function = GXcopy;
555         window.gc = XCreateGC(display, window.drawable,
556                         GCFunction | GCGraphicsExposures, &values);
557 }
558
559 //Get current desktop number
560 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
561 {
562         Atom actual_type;
563         int actual_format;
564         unsigned long nitems;
565         unsigned long bytes_after;
566         unsigned char *prop = NULL;
567         struct information *current_info = &info;
568
569         if (atom == None) return;
570
571         if ( (XGetWindowProperty( current_display, root, atom,
572                                         0, 1L, False, XA_CARDINAL,
573                                         &actual_type, &actual_format, &nitems,
574                                         &bytes_after, &prop ) == Success ) &&
575                         (actual_type == XA_CARDINAL) &&
576                         (nitems == 1L) && (actual_format == 32) ) {
577                 current_info->x11.desktop.current = prop[0]+1;
578         }
579         if(prop) {
580                 XFree(prop);
581         }
582 }
583
584 //Get total number of available desktops
585 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
586 {
587         Atom actual_type;
588         int actual_format;
589         unsigned long nitems;
590         unsigned long bytes_after;
591         unsigned char *prop = NULL;
592         struct information *current_info = &info;
593
594         if (atom == None) return;
595
596         if ( (XGetWindowProperty( current_display, root, atom,
597                                         0, 1L, False, XA_CARDINAL,
598                                         &actual_type, &actual_format, &nitems,
599                                         &bytes_after, &prop ) == Success ) &&
600                         (actual_type == XA_CARDINAL) &&
601                         (nitems == 1L) && (actual_format == 32) ) {
602                 current_info->x11.desktop.number = prop[0];
603         }
604         if(prop) {
605                 XFree(prop);
606         }
607 }
608
609 //Get all desktop names
610 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
611 {
612         Atom actual_type;
613         int actual_format;
614         unsigned long nitems;
615         unsigned long bytes_after;
616         unsigned char *prop = NULL;
617         struct information *current_info = &info;
618
619         if (atom == None) return;
620
621         if ( (XGetWindowProperty( current_display, root, atom,
622                                         0, (~0L), False, ATOM(UTF8_STRING),
623                                         &actual_type, &actual_format, &nitems,
624                                         &bytes_after, &prop ) == Success ) &&
625                         (actual_type == ATOM(UTF8_STRING)) &&
626                         (nitems > 0L) && (actual_format == 8) ) {
627
628                 if(current_info->x11.desktop.all_names) {
629                         free(current_info->x11.desktop.all_names);
630                         current_info->x11.desktop.all_names = NULL;
631                 }
632                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
633                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
634                 current_info->x11.desktop.nitems = nitems;
635         }
636         if(prop) {
637                 XFree(prop);
638         }
639 }
640
641 //Get current desktop name
642 static inline void get_x11_desktop_current_name(char *names)
643 {
644         struct information *current_info = &info;
645         unsigned int i = 0, j = 0;
646         int k = 0;
647
648         while ( i < current_info->x11.desktop.nitems ) {
649                 if ( names[i++] == '\0' ) {
650                         if ( ++k == current_info->x11.desktop.current ) {
651                                 if (current_info->x11.desktop.name) {
652                                         free(current_info->x11.desktop.name);
653                                         current_info->x11.desktop.name = NULL;
654                                 }
655                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
656                                 //desktop names can be empty but should always be not null
657                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
658                                 break;
659                         }
660                         j = i;
661                 }
662         }
663 }
664
665 void get_x11_desktop_info(Display *current_display, Atom atom)
666 {
667         Window root;
668         static Atom atom_current, atom_number, atom_names;
669         struct information *current_info = &info;
670         XWindowAttributes window_attributes;
671
672         root = RootWindow(current_display, current_info->x11.monitor.current);
673
674         /* Check if we initialise else retrieve changed property */
675         if (atom == 0) {
676                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
677                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
678                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
679                 get_x11_desktop_current(current_display, root, atom_current);
680                 get_x11_desktop_number(current_display, root, atom_number);
681                 get_x11_desktop_names(current_display, root, atom_names);
682                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
683
684                 /* Set the PropertyChangeMask on the root window, if not set */
685                 XGetWindowAttributes(display, root, &window_attributes);
686                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
687                         XSetWindowAttributes attributes;
688                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
689                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
690                         XGetWindowAttributes(display, root, &window_attributes);
691                 }
692         } else {
693                 if (atom == atom_current) {
694                         get_x11_desktop_current(current_display, root, atom_current);
695                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
696                 } else if (atom == atom_number) {
697                         get_x11_desktop_number(current_display, root, atom_number);
698                 } else if (atom == atom_names) {
699                         get_x11_desktop_names(current_display, root, atom_names);
700                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
701                 }
702         }
703 }
704
705 void update_x11info(void)
706 {
707         struct information *current_info = &info;
708         current_info->x11.monitor.number = XScreenCount(display);
709         current_info->x11.monitor.current = XDefaultScreen(display);
710 }
711
712 #ifdef OWN_WINDOW
713 /* reserve window manager space */
714 void set_struts(int sidenum)
715 {
716         Atom strut;
717         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
718                 /* reserve space at left, right, top, bottom */
719                 signed long sizes[12] = {0};
720                 int i;
721
722                 /* define strut depth */
723                 switch (sidenum) {
724                         case 0:
725                                 /* left side */
726                                 sizes[0] = window.x + window.width;
727                                 break;
728                         case 1:
729                                 /* right side */
730                                 sizes[1] = display_width - window.x;
731                                 break;
732                         case 2:
733                                 /* top side */
734                                 sizes[2] = window.y + window.height;
735                                 break;
736                         case 3:
737                                 /* bottom side */
738                                 sizes[3] = display_height - window.y;
739                                 break;
740                 }
741
742                 /* define partial strut length */
743                 if (sidenum <= 1) {
744                         sizes[4 + (sidenum*2)] = window.y;
745                         sizes[5 + (sidenum*2)] = window.y + window.height;
746                 } else if (sidenum <= 3) {
747                         sizes[4 + (sidenum*2)] = window.x;
748                         sizes[5 + (sidenum*2)] = window.x + window.width;
749                 }
750
751                 /* check constraints */
752                 for (i = 0; i < 12; i++) {
753                         if (sizes[i] < 0) {
754                                 sizes[i] = 0;
755                         } else {
756                                 if (i <= 1 || i >= 8) {
757                                         if (sizes[i] > display_width) {
758                                                 sizes[i] = display_width;
759                                         }
760                                 } else {
761                                         if (sizes[i] > display_height) {
762                                                 sizes[i] = display_height;
763                                         }
764                                 }
765                         }
766                 }
767
768                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
769                                 PropModeReplace, (unsigned char *) &sizes, 4);
770
771                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
772                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
773                                         PropModeReplace, (unsigned char *) &sizes, 12);
774                 }
775         }
776 }
777 #endif /* OWN_WINDOW */
778
779 #ifdef HAVE_XDBE
780 void xdbe_swap_buffers(void)
781 {
782         if (use_xdbe) {
783                 XdbeSwapInfo swap;
784
785                 swap.swap_window = window.window;
786                 swap.swap_action = XdbeBackground;
787                 XdbeSwapBuffers(display, &swap, 1);
788         }
789 }
790 #endif /* HAVE_XDBE */
791