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