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