Clean up x11.c a bit.
[monky] / src / x11.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29
30 #include "config.h"
31 #include "conky.h"
32 #include "logging.h"
33 #include "common.h"
34
35 #include "x11.h"
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xmd.h>
39 #include <X11/Xutil.h>
40 #ifdef IMLIB2
41 #include "imlib2.h"
42 #endif /* IMLIB2 */
43
44 #ifdef XFT
45 #include <X11/Xft/Xft.h>
46 int use_xft = 0;
47 #endif
48
49 #ifdef HAVE_XDBE
50 int use_xdbe;
51 #endif
52
53 /* some basic X11 stuff */
54 Display *display = NULL;
55 int display_width;
56 int display_height;
57 int screen;
58 static int set_transparent;
59 static int background_colour;
60
61 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
62 int workarea[4];
63
64 /* Window stuff */
65 struct conky_window window;
66
67 /* local prototypes */
68 static void update_workarea(void);
69 static Window find_desktop_window(Window *p_root, Window *p_desktop);
70 static Window find_subwindow(Window win, int w, int h);
71
72 /* X11 initializer */
73 void init_X11(const char *disp)
74 {
75         if (!display) {
76                 if ((display = XOpenDisplay(disp)) == NULL) {
77                         CRIT_ERR(NULL, NULL, "can't open display: %s", XDisplayName(0));
78                 }
79         }
80
81         screen = DefaultScreen(display);
82         display_width = DisplayWidth(display, screen);
83         display_height = DisplayHeight(display, screen);
84
85         get_x11_desktop_info(display, 0);
86
87         update_workarea();
88 }
89
90 static void update_workarea(void)
91 {
92         /* default work area is display */
93         workarea[0] = 0;
94         workarea[1] = 0;
95         workarea[2] = display_width;
96         workarea[3] = display_height;
97 }
98
99 /* Find root window and desktop window.
100  * Return desktop window on success,
101  * and set root and desktop byref return values.
102  * Return 0 on failure. */
103 static Window find_desktop_window(Window *p_root, Window *p_desktop)
104 {
105         Atom type;
106         int format, i;
107         unsigned long nitems, bytes;
108         unsigned int n;
109         Window root = RootWindow(display, screen);
110         Window win = root;
111         Window troot, parent, *children;
112         unsigned char *buf = NULL;
113
114         if (!p_root || !p_desktop) {
115                 return 0;
116         }
117
118         /* some window managers set __SWM_VROOT to some child of root window */
119
120         XQueryTree(display, root, &troot, &parent, &children, &n);
121         for (i = 0; i < (int) n; i++) {
122                 if (XGetWindowProperty(display, children[i], ATOM(__SWM_VROOT), 0, 1,
123                                         False, XA_WINDOW, &type, &format, &nitems, &bytes, &buf)
124                                 == Success && type == XA_WINDOW) {
125                         win = *(Window *) buf;
126                         XFree(buf);
127                         XFree(children);
128                         fprintf(stderr,
129                                         PACKAGE_NAME": desktop window (%lx) found from __SWM_VROOT property\n",
130                                         win);
131                         fflush(stderr);
132                         *p_root = win;
133                         *p_desktop = win;
134                         return win;
135                 }
136
137                 if (buf) {
138                         XFree(buf);
139                         buf = 0;
140                 }
141         }
142         XFree(children);
143
144         /* get subwindows from root */
145         win = find_subwindow(root, -1, -1);
146
147         update_workarea();
148
149         win = find_subwindow(win, workarea[2], workarea[3]);
150
151         if (buf) {
152                 XFree(buf);
153                 buf = 0;
154         }
155
156         if (win != root) {
157                 fprintf(stderr,
158                                 PACKAGE_NAME": desktop window (%lx) is subwindow of root window (%lx)\n",
159                                 win, root);
160         } else {
161                 fprintf(stderr, PACKAGE_NAME": desktop window (%lx) is root window\n", win);
162         }
163
164         fflush(stderr);
165
166         *p_root = root;
167         *p_desktop = win;
168
169         return win;
170 }
171
172 /* sets background to ParentRelative for the Window and all parents */
173 void set_transparent_background(Window win)
174 {
175         static int colour_set = -1;
176
177         if (set_transparent) {
178                 Window parent = win;
179                 unsigned int i;
180
181                 for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
182                         Window r, *children;
183                         unsigned int n;
184
185                         XSetWindowBackgroundPixmap(display, parent, ParentRelative);
186
187                         XQueryTree(display, parent, &r, &parent, &children, &n);
188                         XFree(children);
189                 }
190         } else if (colour_set != background_colour) {
191                 XSetWindowBackground(display, win, background_colour);
192                 colour_set = background_colour;
193         }
194         // XClearWindow(display, win); not sure why this was here
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                         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         /* set_transparent_background(window.window);
480          * must be done after double buffer stuff? */
481 #ifdef OWN_WINDOW
482         /* if (own_window) {
483            set_transparent_background(window.window);
484            XClearWindow(display, window.window);
485            } */
486 #endif
487
488         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
489 #ifdef OWN_WINDOW
490                         | (own_window ? (StructureNotifyMask |
491                                         ButtonPressMask | ButtonReleaseMask) : 0)
492 #endif
493                         );
494 }
495
496 static Window find_subwindow(Window win, int w, int h)
497 {
498         unsigned int i, j;
499         Window troot, parent, *children;
500         unsigned int n;
501
502         /* search subwindows with same size as display or work area */
503
504         for (i = 0; i < 10; i++) {
505                 XQueryTree(display, win, &troot, &parent, &children, &n);
506
507                 for (j = 0; j < n; j++) {
508                         XWindowAttributes attrs;
509
510                         if (XGetWindowAttributes(display, children[j], &attrs)) {
511                                 /* Window must be mapped and same size as display or
512                                  * work space */
513                                 if (attrs.map_state != 0 && ((attrs.width == display_width
514                                                                 && attrs.height == display_height)
515                                                         || (attrs.width == w && attrs.height == h))) {
516                                         win = children[j];
517                                         break;
518                                 }
519                         }
520                 }
521
522                 XFree(children);
523                 if (j == n) {
524                         break;
525                 }
526         }
527
528         return win;
529 }
530
531 long get_x11_color(const char *name)
532 {
533         XColor color;
534
535         color.pixel = 0;
536         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
537                 /* lets check if it's a hex colour with the # missing in front
538                  * if yes, then do something about it */
539                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
540
541                 newname[0] = '#';
542                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
543                 /* now lets try again */
544                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
545                                         &color)) {
546                         ERR("can't parse X color '%s'", name);
547                         return 0xFF00FF;
548                 }
549         }
550         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
551                 ERR("can't allocate X color '%s'", name);
552         }
553
554         return (long) color.pixel;
555 }
556
557 void create_gc(void)
558 {
559         XGCValues values;
560
561         values.graphics_exposures = 0;
562         values.function = GXcopy;
563         window.gc = XCreateGC(display, window.drawable,
564                         GCFunction | GCGraphicsExposures, &values);
565 }
566
567 //Get current desktop number
568 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
569 {
570         Atom actual_type;
571         int actual_format;
572         unsigned long nitems;
573         unsigned long bytes_after;
574         unsigned char *prop = NULL;
575         struct information *current_info = &info;
576
577         if ( (XGetWindowProperty( current_display, root, atom,
578                                         0, 1L, False, XA_CARDINAL,
579                                         &actual_type, &actual_format, &nitems,
580                                         &bytes_after, &prop ) == Success ) &&
581                         (actual_type == XA_CARDINAL) &&
582                         (nitems == 1L) && (actual_format == 32) ) {
583                 current_info->x11.desktop.current = prop[0]+1;
584         }
585         if(prop) {
586                 XFree(prop);
587         }
588 }
589
590 //Get total number of available desktops
591 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
592 {
593         Atom actual_type;
594         int actual_format;
595         unsigned long nitems;
596         unsigned long bytes_after;
597         unsigned char *prop = NULL;
598         struct information *current_info = &info;
599
600         if ( (XGetWindowProperty( current_display, root, atom,
601                                         0, 1L, False, XA_CARDINAL,
602                                         &actual_type, &actual_format, &nitems,
603                                         &bytes_after, &prop ) == Success ) &&
604                         (actual_type == XA_CARDINAL) &&
605                         (nitems == 1L) && (actual_format == 32) ) {
606                 current_info->x11.desktop.number = prop[0];
607         }
608         if(prop) {
609                 XFree(prop);
610         }
611 }
612
613 //Get all desktop names
614 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
615 {
616         Atom actual_type;
617         int actual_format;
618         unsigned long nitems;
619         unsigned long bytes_after;
620         unsigned char *prop = NULL;
621         struct information *current_info = &info;
622
623         if ( (XGetWindowProperty( current_display, root, atom,
624                                         0, (~0L), False, ATOM(UTF8_STRING),
625                                         &actual_type, &actual_format, &nitems,
626                                         &bytes_after, &prop ) == Success ) &&
627                         (actual_type == ATOM(UTF8_STRING)) &&
628                         (nitems > 0L) && (actual_format == 8) ) {
629
630                 if(current_info->x11.desktop.all_names) {
631                         free(current_info->x11.desktop.all_names);
632                         current_info->x11.desktop.all_names = NULL;
633                 }
634                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
635                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
636                 current_info->x11.desktop.nitems = nitems;
637         }
638         if(prop) {
639                 XFree(prop);
640         }
641 }
642
643 //Get current desktop name
644 static inline void get_x11_desktop_current_name(char *names)
645 {
646         struct information *current_info = &info;
647         unsigned int i = 0, j = 0;
648         int k = 0;
649
650         while ( i < current_info->x11.desktop.nitems ) {
651                 if ( names[i++] == '\0' ) {
652                         if ( ++k == current_info->x11.desktop.current ) {
653                                 if (current_info->x11.desktop.name) {
654                                         free(current_info->x11.desktop.name);
655                                         current_info->x11.desktop.name = NULL;
656                                 }
657                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
658                                 //desktop names can be empty but should always be not null
659                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
660                                 break;
661                         }
662                         j = i;
663                 }
664         }
665 }
666
667 void get_x11_desktop_info(Display *current_display, Atom atom)
668 {
669         Window root;
670         static Atom atom_current, atom_number, atom_names;
671         struct information *current_info = &info;
672         XWindowAttributes window_attributes;
673
674         root = RootWindow(current_display, current_info->x11.monitor.current);
675
676         //Check if we initialise else retrieve changed property
677         if (atom == 0) {
678                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
679                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
680                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
681                 get_x11_desktop_current(current_display, root, atom_current);
682                 get_x11_desktop_number(current_display, root, atom_number);
683                 get_x11_desktop_names(current_display, root, atom_names);
684                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
685
686                 //Set the PropertyChangeMask on the root window, if not set
687                 XGetWindowAttributes(display, root, &window_attributes);
688                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
689                         XSetWindowAttributes attributes;
690                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
691                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
692                         XGetWindowAttributes(display, root, &window_attributes);
693                 }
694         } else {
695                 if (atom == atom_current) {
696                         get_x11_desktop_current(current_display, root, atom_current);
697                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
698                 } else if (atom == atom_number) {
699                         get_x11_desktop_number(current_display, root, atom_number);
700                 } else if (atom == atom_names) {
701                         get_x11_desktop_names(current_display, root, atom_names);
702                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
703                 }
704         }
705 }
706
707 void update_x11info(void)
708 {
709         struct information *current_info = &info;
710         current_info->x11.monitor.number = XScreenCount(display);
711         current_info->x11.monitor.current = XDefaultScreen(display);
712 }
713
714 #ifdef OWN_WINDOW
715 /* reserve window manager space */
716 void set_struts(int sidenum)
717 {
718         Atom strut;
719         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
720                 /* reserve space at left, right, top, bottom */
721                 signed long sizes[12] = {0};
722                 int i;
723
724                 /* define strut depth */
725                 switch (sidenum) {
726                         case 0:
727                                 /* left side */
728                                 sizes[0] = window.x + window.width;
729                                 break;
730                         case 1:
731                                 /* right side */
732                                 sizes[1] = display_width - window.x;
733                                 break;
734                         case 2:
735                                 /* top side */
736                                 sizes[2] = window.y + window.height;
737                                 break;
738                         case 3:
739                                 /* bottom side */
740                                 sizes[3] = display_height - window.y;
741                                 break;
742                 }
743
744                 /* define partial strut length */
745                 if (sidenum <= 1) {
746                         sizes[4 + (sidenum*2)] = window.y;
747                         sizes[5 + (sidenum*2)] = window.y + window.height;
748                 } else if (sidenum <= 3) {
749                         sizes[4 + (sidenum*2)] = window.x;
750                         sizes[5 + (sidenum*2)] = window.x + window.width;
751                 }
752
753                 /* check constraints */
754                 for (i = 0; i < 12; i++) {
755                         if (sizes[i] < 0) {
756                                 sizes[i] = 0;
757                         } else {
758                                 if (i <= 1 || i >= 8) {
759                                         if (sizes[i] > display_width) {
760                                                 sizes[i] = display_width;
761                                         }
762                                 } else {
763                                         if (sizes[i] > display_height) {
764                                                 sizes[i] = display_height;
765                                         }
766                                 }
767                         }
768                 }
769
770                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
771                                 PropModeReplace, (unsigned char *) &sizes, 4);
772
773                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
774                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
775                                         PropModeReplace, (unsigned char *) &sizes, 12);
776                 }
777         }
778 }
779 #endif /* OWN_WINDOW */