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