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