Add vim modelines.
[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                 } /* else { window.type != TYPE_OVERRIDE */
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 /* if (own_window) { */
427 #endif
428                 /* root / desktop window */
429         {
430                 XWindowAttributes attrs;
431
432                 if (!window.window) {
433                         window.window = find_desktop_window(&window.root, &window.desktop);
434                 }
435
436                 if (XGetWindowAttributes(display, window.window, &attrs)) {
437                         window.width = attrs.width;
438                         window.height = attrs.height;
439                 }
440
441                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
442         }
443
444         /* Drawable is same as window. This may be changed by double buffering. */
445         window.drawable = window.window;
446
447 #ifdef HAVE_XDBE
448         if (use_xdbe) {
449                 int major, minor;
450
451                 if (!XdbeQueryExtension(display, &major, &minor)) {
452                         use_xdbe = 0;
453                 } else {
454                         window.back_buffer = XdbeAllocateBackBufferName(display,
455                                 window.window, XdbeBackground);
456                         if (window.back_buffer != None) {
457                                 window.drawable = window.back_buffer;
458                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
459                         } else {
460                                 use_xdbe = 0;
461                         }
462                 }
463                 if (!use_xdbe) {
464                         ERR("failed to set up double buffer");
465                 }
466         }
467         if (!use_xdbe) {
468                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
469         }
470 #endif
471         window.visual = DefaultVisual(display, DefaultScreen(display));
472         window.colourmap = DefaultColormap(display, DefaultScreen(display));
473 #ifdef IMLIB2
474         {
475                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
476         }
477 #endif /* IMLIB2 */
478         XFlush(display);
479
480         /* set_transparent_background(window.window);
481          * must be done after double buffer stuff? */
482 #ifdef OWN_WINDOW
483         /* if (own_window) {
484                 set_transparent_background(window.window);
485                 XClearWindow(display, window.window);
486         } */
487 #endif
488
489         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
490 #ifdef OWN_WINDOW
491                 | (own_window ? (StructureNotifyMask |
492                 ButtonPressMask | ButtonReleaseMask) : 0)
493 #endif
494                 );
495 }
496
497 static Window find_subwindow(Window win, int w, int h)
498 {
499         unsigned int i, j;
500         Window troot, parent, *children;
501         unsigned int n;
502
503         /* search subwindows with same size as display or work area */
504
505         for (i = 0; i < 10; i++) {
506                 XQueryTree(display, win, &troot, &parent, &children, &n);
507
508                 for (j = 0; j < n; j++) {
509                         XWindowAttributes attrs;
510
511                         if (XGetWindowAttributes(display, children[j], &attrs)) {
512                                 /* Window must be mapped and same size as display or
513                                  * work space */
514                                 if (attrs.map_state != 0 && ((attrs.width == display_width
515                                                 && attrs.height == display_height)
516                                                 || (attrs.width == w && attrs.height == h))) {
517                                         win = children[j];
518                                         break;
519                                 }
520                         }
521                 }
522
523                 XFree(children);
524                 if (j == n) {
525                         break;
526                 }
527         }
528
529         return win;
530 }
531
532 long get_x11_color(const char *name)
533 {
534         XColor color;
535
536         color.pixel = 0;
537         if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
538                 /* lets check if it's a hex colour with the # missing in front
539                  * if yes, then do something about it */
540                 char newname[DEFAULT_TEXT_BUFFER_SIZE];
541
542                 newname[0] = '#';
543                 strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
544                 /* now lets try again */
545                 if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
546                                 &color)) {
547                         ERR("can't parse X color '%s'", name);
548                         return 0xFF00FF;
549                 }
550         }
551         if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
552                 ERR("can't allocate X color '%s'", name);
553         }
554
555         return (long) color.pixel;
556 }
557
558 void create_gc(void)
559 {
560         XGCValues values;
561
562         values.graphics_exposures = 0;
563         values.function = GXcopy;
564         window.gc = XCreateGC(display, window.drawable,
565                 GCFunction | GCGraphicsExposures, &values);
566 }
567
568 //Get current desktop number
569 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
570 {
571         Atom actual_type;
572         int actual_format;
573         unsigned long nitems;
574         unsigned long bytes_after;
575         unsigned char *prop = NULL;
576         struct information *current_info = &info;
577
578         if ( (XGetWindowProperty( current_display, root, atom,
579                                   0, 1L, False, XA_CARDINAL,
580                                   &actual_type, &actual_format, &nitems,
581                                   &bytes_after, &prop ) == Success ) &&
582              (actual_type == XA_CARDINAL) &&
583              (nitems == 1L) && (actual_format == 32) ) {
584           current_info->x11.desktop.current = prop[0]+1;
585         }
586         if(prop) {
587           XFree(prop);
588         }
589 }
590
591 //Get total number of available desktops
592 static inline void get_x11_desktop_number(Display *current_display, Window root, Atom atom)
593 {
594         Atom actual_type;
595         int actual_format;
596         unsigned long nitems;
597         unsigned long bytes_after;
598         unsigned char *prop = NULL;
599         struct information *current_info = &info;
600
601         if ( (XGetWindowProperty( current_display, root, atom,
602                                   0, 1L, False, XA_CARDINAL,
603                                   &actual_type, &actual_format, &nitems,
604                                   &bytes_after, &prop ) == Success ) &&
605              (actual_type == XA_CARDINAL) &&
606              (nitems == 1L) && (actual_format == 32) ) {
607           current_info->x11.desktop.number = prop[0];
608         }
609         if(prop) {
610           XFree(prop);
611         }
612 }
613
614 //Get all desktop names
615 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
616 {
617         Atom actual_type;
618         int actual_format;
619         unsigned long nitems;
620         unsigned long bytes_after;
621         unsigned char *prop = NULL;
622         struct information *current_info = &info;
623
624         if ( (XGetWindowProperty( current_display, root, atom,
625                                   0, (~0L), False, ATOM(UTF8_STRING),
626                                   &actual_type, &actual_format, &nitems,
627                                   &bytes_after, &prop ) == Success ) &&
628              (actual_type == ATOM(UTF8_STRING)) &&
629              (nitems > 0L) && (actual_format == 8) ) {
630
631           if(current_info->x11.desktop.all_names) {
632             free(current_info->x11.desktop.all_names);
633             current_info->x11.desktop.all_names = NULL;
634           }
635           current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
636           memcpy(current_info->x11.desktop.all_names, prop, nitems);
637           current_info->x11.desktop.nitems = nitems;
638         }
639         if(prop) {
640           XFree(prop);
641         }
642 }
643
644 //Get current desktop name
645 static inline void get_x11_desktop_current_name(char *names)
646 {
647         struct information *current_info = &info;
648         unsigned int i = 0, j = 0;
649         int k = 0;
650
651         while ( i < current_info->x11.desktop.nitems ) {
652           if ( names[i++] == '\0' ) {
653             if ( ++k == current_info->x11.desktop.current ) {
654               if (current_info->x11.desktop.name) {
655                 free(current_info->x11.desktop.name);
656                 current_info->x11.desktop.name = NULL;
657               }
658               current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
659               //desktop names can be empty but should always be not null
660               strcpy( current_info->x11.desktop.name, (char *)&names[j] );
661               break;
662             }
663             j = i;
664           }
665         }
666 }
667
668 void get_x11_desktop_info(Display *current_display, Atom atom)
669 {
670         Window root;
671         static Atom atom_current, atom_number, atom_names;
672         struct information *current_info = &info;
673         XWindowAttributes window_attributes;
674
675         root = RootWindow(current_display, current_info->x11.monitor.current);
676
677         //Check if we initialise else retrieve changed property
678         if (atom == 0) {
679           atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
680           atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
681           atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
682           get_x11_desktop_current(current_display, root, atom_current);
683           get_x11_desktop_number(current_display, root, atom_number);
684           get_x11_desktop_names(current_display, root, atom_names);
685           get_x11_desktop_current_name(current_info->x11.desktop.all_names);
686
687           //Set the PropertyChangeMask on the root window, if not set
688           XGetWindowAttributes(display, root, &window_attributes);
689           if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
690             XSetWindowAttributes attributes;
691             attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
692             XChangeWindowAttributes(display, root, CWEventMask, &attributes);
693             XGetWindowAttributes(display, root, &window_attributes);
694           }
695         } else {
696           if (atom == atom_current) {
697             get_x11_desktop_current(current_display, root, atom_current);
698             get_x11_desktop_current_name(current_info->x11.desktop.all_names);
699           } else if (atom == atom_number) {
700             get_x11_desktop_number(current_display, root, atom_number);
701           } else if (atom == atom_names) {
702             get_x11_desktop_names(current_display, root, atom_names);
703             get_x11_desktop_current_name(current_info->x11.desktop.all_names);
704           }
705         }
706 }
707
708 void update_x11info(void)
709 {
710         struct information *current_info = &info;
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                         {
729                                 /* left side */
730                                 sizes[0] = window.x + window.width;
731                                 break;
732                         }
733                         case 1:
734                         {
735                                 /* right side */
736                                 sizes[1] = display_width - window.x;
737                                 break;
738                         }
739                         case 2:
740                         {
741                                 /* top side */
742                                 sizes[2] = window.y + window.height;
743                                 break;
744                         }
745                         case 3:
746                         {
747                                 /* bottom side */
748                                 sizes[3] = display_height - window.y;
749                                 break;
750                         }
751                 }
752
753                 /* define partial strut length */
754                 if (sidenum <= 1) {
755                         sizes[4 + (sidenum*2)] = window.y;
756                         sizes[5 + (sidenum*2)] = window.y + window.height;
757                 } else if (sidenum <= 3) {
758                         sizes[4 + (sidenum*2)] = window.x;
759                         sizes[5 + (sidenum*2)] = window.x + window.width;
760                 }
761
762                 /* check constraints */
763                 for (i = 0; i < 12; i++) {
764                         if (sizes[i] < 0) {
765                                 sizes[i] = 0;
766                         } else {
767                                 if (i <= 1 || i >= 8) {
768                                         if (sizes[i] > display_width) {
769                                                 sizes[i] = display_width;
770                                         }
771                                 } else {
772                                         if (sizes[i] > display_height) {
773                                                 sizes[i] = display_height;
774                                         }
775                                 }
776                         }
777                 }
778
779                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
780                                 PropModeReplace, (unsigned char *) &sizes, 4);
781
782                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
783                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
784                                         PropModeReplace, (unsigned char *) &sizes, 12);
785                 }
786         }
787 }
788 #endif /* OWN_WINDOW */