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