move get_x11_color() to colours.c
[monky] / src / x11.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "logging.h"
34 #include "common.h"
35
36 #include "x11.h"
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xmd.h>
40 #include <X11/Xutil.h>
41 #ifdef IMLIB2
42 #include "imlib2.h"
43 #endif /* IMLIB2 */
44
45 #ifdef XFT
46 #include <X11/Xft/Xft.h>
47 int use_xft = 0;
48 #endif
49
50 #ifdef HAVE_XDBE
51 int use_xdbe;
52 #endif
53
54 /* some basic X11 stuff */
55 Display *display = NULL;
56 int display_width;
57 int display_height;
58 int screen;
59 static int set_transparent;
60 static int background_colour;
61
62 /* workarea from _NET_WORKAREA, this is where window / text is aligned */
63 int workarea[4];
64
65 /* Window stuff */
66 struct conky_window window;
67 char window_created = 0;
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 }
197
198 void destroy_window(void)
199 {
200 #ifdef XFT
201         if(window.xftdraw) {
202                 XftDrawDestroy(window.xftdraw);
203         }
204 #endif
205         if(window.gc) {
206                 XFreeGC(display, window.gc);
207         }
208         memset(&window, 0, sizeof(struct conky_window));
209 }
210
211 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
212                 char **argv, int argc)
213 {
214         /* There seems to be some problems with setting transparent background
215          * (on fluxbox this time). It doesn't happen always and I don't know why it
216          * happens but I bet the bug is somewhere here. */
217         set_transparent = set_trans;
218         background_colour = back_colour;
219         window_created = 1;
220
221 #ifdef OWN_WINDOW
222         if (own_window) {
223                 if (!find_desktop_window(&window.root, &window.desktop)) {
224                         return;
225                 }
226
227                 if (window.type == TYPE_OVERRIDE) {
228
229                         /* An override_redirect True window.
230                          * No WM hints or button processing needed. */
231                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
232                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
233                                 True, 0, 0 };
234
235                         /* Parent is desktop window (which might be a child of root) */
236                         window.window = XCreateWindow(display, window.desktop, window.x,
237                                         window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
238                                         CWBackPixel | CWOverrideRedirect, &attrs);
239
240                         XLowerWindow(display, window.window);
241
242                         fprintf(stderr, PACKAGE_NAME": window type - override\n");
243                         fflush(stderr);
244                 } else { /* window.type != TYPE_OVERRIDE */
245
246                         /* A window managed by the window manager.
247                          * Process hints and buttons. */
248                         XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
249                                 Always, 0L, 0L, False, StructureNotifyMask | ExposureMask |
250                                         ButtonPressMask | ButtonReleaseMask, 0L, False, 0, 0 };
251
252                         XClassHint classHint;
253                         XWMHints wmHint;
254                         Atom xa;
255
256                         if (window.type == TYPE_DOCK) {
257                                 window.x = window.y = 0;
258                         }
259                         /* Parent is root window so WM can take control */
260                         window.window = XCreateWindow(display, window.root, window.x,
261                                         window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
262                                         CWBackPixel | CWOverrideRedirect, &attrs);
263
264                         classHint.res_name = window.class_name;
265                         classHint.res_class = classHint.res_name;
266
267                         wmHint.flags = InputHint | StateHint;
268                         /* allow decorated windows to be given input focus by WM */
269                         wmHint.input =
270                                 TEST_HINT(window.hints, HINT_UNDECORATED) ? False : True;
271                         if (window.type == TYPE_DOCK || window.type == TYPE_PANEL) {
272                                 wmHint.initial_state = WithdrawnState;
273                         } else {
274                                 wmHint.initial_state = NormalState;
275                         }
276
277                         XmbSetWMProperties(display, window.window, NULL, NULL, argv,
278                                         argc, NULL, &wmHint, &classHint);
279                         XStoreName(display, window.window, window.title);
280
281                         /* Sets an empty WM_PROTOCOLS property */
282                         XSetWMProtocols(display, window.window, NULL, 0);
283
284                         /* Set window type */
285                         if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
286                                 Atom prop;
287
288                                 switch (window.type) {
289                                         case TYPE_DESKTOP:
290                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
291                                                 fprintf(stderr, PACKAGE_NAME": window type - desktop\n");
292                                                 fflush(stderr);
293                                                 break;
294                                         case TYPE_DOCK:
295                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
296                                                 fprintf(stderr, PACKAGE_NAME": window type - dock\n");
297                                                 fflush(stderr);
298                                                 break;
299                                         case TYPE_PANEL:
300                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
301                                                 fprintf(stderr, PACKAGE_NAME": window type - panel\n");
302                                                 fflush(stderr);
303                                                 break;
304                                         case TYPE_NORMAL:
305                                         default:
306                                                 prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
307                                                 fprintf(stderr, PACKAGE_NAME": window type - normal\n");
308                                                 fflush(stderr);
309                                                 break;
310                                 }
311                                 XChangeProperty(display, window.window, xa, XA_ATOM, 32,
312                                                 PropModeReplace, (unsigned char *) &prop, 1);
313                         }
314
315                         /* Set desired hints */
316
317                         /* Window decorations */
318                         if (TEST_HINT(window.hints, HINT_UNDECORATED)) {
319                                 /* fprintf(stderr, PACKAGE_NAME": hint - undecorated\n");
320                                    fflush(stderr); */
321
322                                 xa = ATOM(_MOTIF_WM_HINTS);
323                                 if (xa != None) {
324                                         long prop[5] = { 2, 0, 0, 0, 0 };
325                                         XChangeProperty(display, window.window, xa, xa, 32,
326                                                         PropModeReplace, (unsigned char *) prop, 5);
327                                 }
328                         }
329
330                         /* Below other windows */
331                         if (TEST_HINT(window.hints, HINT_BELOW)) {
332                                 /* fprintf(stderr, PACKAGE_NAME": hint - below\n");
333                                    fflush(stderr); */
334
335                                 xa = ATOM(_WIN_LAYER);
336                                 if (xa != None) {
337                                         long prop = 0;
338
339                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
340                                                         PropModeAppend, (unsigned char *) &prop, 1);
341                                 }
342
343                                 xa = ATOM(_NET_WM_STATE);
344                                 if (xa != None) {
345                                         Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
346
347                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
348                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
349                                 }
350                         }
351
352                         /* Above other windows */
353                         if (TEST_HINT(window.hints, HINT_ABOVE)) {
354                                 /* fprintf(stderr, PACKAGE_NAME": hint - above\n");
355                                    fflush(stderr); */
356
357                                 xa = ATOM(_WIN_LAYER);
358                                 if (xa != None) {
359                                         long prop = 6;
360
361                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
362                                                         PropModeAppend, (unsigned char *) &prop, 1);
363                                 }
364
365                                 xa = ATOM(_NET_WM_STATE);
366                                 if (xa != None) {
367                                         Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
368
369                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
370                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
371                                 }
372                         }
373
374                         /* Sticky */
375                         if (TEST_HINT(window.hints, HINT_STICKY)) {
376                                 /* fprintf(stderr, PACKAGE_NAME": hint - sticky\n");
377                                    fflush(stderr); */
378
379                                 xa = ATOM(_NET_WM_DESKTOP);
380                                 if (xa != None) {
381                                         CARD32 xa_prop = 0xFFFFFFFF;
382
383                                         XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
384                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
385                                 }
386
387                                 xa = ATOM(_NET_WM_STATE);
388                                 if (xa != None) {
389                                         Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
390
391                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
392                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
393                                 }
394                         }
395
396                         /* Skip taskbar */
397                         if (TEST_HINT(window.hints, HINT_SKIP_TASKBAR)) {
398                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_taskbar\n");
399                                    fflush(stderr); */
400
401                                 xa = ATOM(_NET_WM_STATE);
402                                 if (xa != None) {
403                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
404
405                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
406                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
407                                 }
408                         }
409
410                         /* Skip pager */
411                         if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
412                                 /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
413                                    fflush(stderr); */
414
415                                 xa = ATOM(_NET_WM_STATE);
416                                 if (xa != None) {
417                                         Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
418
419                                         XChangeProperty(display, window.window, xa, XA_ATOM, 32,
420                                                         PropModeAppend, (unsigned char *) &xa_prop, 1);
421                                 }
422                         }
423                 }
424
425                 fprintf(stderr, PACKAGE_NAME": drawing to created window (0x%lx)\n",
426                                 window.window);
427                 fflush(stderr);
428
429                 XMapWindow(display, window.window);
430
431         } else
432 #endif /* OWN_WINDOW */
433         {
434                 XWindowAttributes attrs;
435
436                 if (!window.window) {
437                         window.window = find_desktop_window(&window.root, &window.desktop);
438                 }
439
440                 if (XGetWindowAttributes(display, window.window, &attrs)) {
441                         window.width = attrs.width;
442                         window.height = attrs.height;
443                 }
444
445                 fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
446         }
447
448         /* Drawable is same as window. This may be changed by double buffering. */
449         window.drawable = window.window;
450
451 #ifdef HAVE_XDBE
452         if (use_xdbe) {
453                 int major, minor;
454
455                 if (!XdbeQueryExtension(display, &major, &minor)) {
456                         use_xdbe = 0;
457                 } else {
458                         window.back_buffer = XdbeAllocateBackBufferName(display,
459                                         window.window, XdbeBackground);
460                         if (window.back_buffer != None) {
461                                 window.drawable = window.back_buffer;
462                                 fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
463                         } else {
464                                 use_xdbe = 0;
465                         }
466                 }
467                 if (!use_xdbe) {
468                         NORM_ERR("failed to set up double buffer");
469                 }
470         }
471         if (!use_xdbe) {
472                 fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
473         }
474 #endif
475         window.visual = DefaultVisual(display, DefaultScreen(display));
476         window.colourmap = DefaultColormap(display, DefaultScreen(display));
477 #ifdef IMLIB2
478         {
479                 cimlib_init(display, window.drawable, window.visual, window.colourmap);
480         }
481 #endif /* IMLIB2 */
482         XFlush(display);
483
484         XSelectInput(display, window.window, ExposureMask | PropertyChangeMask
485 #ifdef OWN_WINDOW
486                         | (own_window ? (StructureNotifyMask |
487                                         ButtonPressMask | ButtonReleaseMask) : 0)
488 #endif
489                         );
490 }
491
492 static Window find_subwindow(Window win, int w, int h)
493 {
494         unsigned int i, j;
495         Window troot, parent, *children;
496         unsigned int n;
497
498         /* search subwindows with same size as display or work area */
499
500         for (i = 0; i < 10; i++) {
501                 XQueryTree(display, win, &troot, &parent, &children, &n);
502
503                 for (j = 0; j < n; j++) {
504                         XWindowAttributes attrs;
505
506                         if (XGetWindowAttributes(display, children[j], &attrs)) {
507                                 /* Window must be mapped and same size as display or
508                                  * work space */
509                                 if (attrs.map_state != 0 && ((attrs.width == display_width
510                                                                 && attrs.height == display_height)
511                                                         || (attrs.width == w && attrs.height == h))) {
512                                         win = children[j];
513                                         break;
514                                 }
515                         }
516                 }
517
518                 XFree(children);
519                 if (j == n) {
520                         break;
521                 }
522         }
523
524         return win;
525 }
526
527 void create_gc(void)
528 {
529         XGCValues values;
530
531         values.graphics_exposures = 0;
532         values.function = GXcopy;
533         window.gc = XCreateGC(display, window.drawable,
534                         GCFunction | GCGraphicsExposures, &values);
535 }
536
537 //Get current desktop number
538 static inline void get_x11_desktop_current(Display *current_display, Window root, Atom atom)
539 {
540         Atom actual_type;
541         int actual_format;
542         unsigned long nitems;
543         unsigned long bytes_after;
544         unsigned char *prop = NULL;
545         struct information *current_info = &info;
546
547         if (atom == None) return;
548
549         if ( (XGetWindowProperty( current_display, root, atom,
550                                         0, 1L, False, XA_CARDINAL,
551                                         &actual_type, &actual_format, &nitems,
552                                         &bytes_after, &prop ) == Success ) &&
553                         (actual_type == XA_CARDINAL) &&
554                         (nitems == 1L) && (actual_format == 32) ) {
555                 current_info->x11.desktop.current = prop[0]+1;
556         }
557         if(prop) {
558                 XFree(prop);
559         }
560 }
561
562 //Get total number of available desktops
563 static inline void get_x11_desktop_number(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 (atom == None) return;
573
574         if ( (XGetWindowProperty( current_display, root, atom,
575                                         0, 1L, False, XA_CARDINAL,
576                                         &actual_type, &actual_format, &nitems,
577                                         &bytes_after, &prop ) == Success ) &&
578                         (actual_type == XA_CARDINAL) &&
579                         (nitems == 1L) && (actual_format == 32) ) {
580                 current_info->x11.desktop.number = prop[0];
581         }
582         if(prop) {
583                 XFree(prop);
584         }
585 }
586
587 //Get all desktop names
588 static inline void get_x11_desktop_names(Display *current_display, Window root, Atom atom)
589 {
590         Atom actual_type;
591         int actual_format;
592         unsigned long nitems;
593         unsigned long bytes_after;
594         unsigned char *prop = NULL;
595         struct information *current_info = &info;
596
597         if (atom == None) return;
598
599         if ( (XGetWindowProperty( current_display, root, atom,
600                                         0, (~0L), False, ATOM(UTF8_STRING),
601                                         &actual_type, &actual_format, &nitems,
602                                         &bytes_after, &prop ) == Success ) &&
603                         (actual_type == ATOM(UTF8_STRING)) &&
604                         (nitems > 0L) && (actual_format == 8) ) {
605
606                 if(current_info->x11.desktop.all_names) {
607                         free(current_info->x11.desktop.all_names);
608                         current_info->x11.desktop.all_names = NULL;
609                 }
610                 current_info->x11.desktop.all_names = malloc(nitems*sizeof(char));
611                 memcpy(current_info->x11.desktop.all_names, prop, nitems);
612                 current_info->x11.desktop.nitems = nitems;
613         }
614         if(prop) {
615                 XFree(prop);
616         }
617 }
618
619 //Get current desktop name
620 static inline void get_x11_desktop_current_name(char *names)
621 {
622         struct information *current_info = &info;
623         unsigned int i = 0, j = 0;
624         int k = 0;
625
626         while ( i < current_info->x11.desktop.nitems ) {
627                 if ( names[i++] == '\0' ) {
628                         if ( ++k == current_info->x11.desktop.current ) {
629                                 if (current_info->x11.desktop.name) {
630                                         free(current_info->x11.desktop.name);
631                                         current_info->x11.desktop.name = NULL;
632                                 }
633                                 current_info->x11.desktop.name = malloc((i-j)*sizeof(char));
634                                 //desktop names can be empty but should always be not null
635                                 strcpy( current_info->x11.desktop.name, (char *)&names[j] );
636                                 break;
637                         }
638                         j = i;
639                 }
640         }
641 }
642
643 void get_x11_desktop_info(Display *current_display, Atom atom)
644 {
645         Window root;
646         static Atom atom_current, atom_number, atom_names;
647         struct information *current_info = &info;
648         XWindowAttributes window_attributes;
649
650         root = RootWindow(current_display, current_info->x11.monitor.current);
651
652         /* Check if we initialise else retrieve changed property */
653         if (atom == 0) {
654                 atom_current = XInternAtom(current_display, "_NET_CURRENT_DESKTOP", True);
655                 atom_number  = XInternAtom(current_display, "_NET_NUMBER_OF_DESKTOPS", True);
656                 atom_names   = XInternAtom(current_display, "_NET_DESKTOP_NAMES", True);
657                 get_x11_desktop_current(current_display, root, atom_current);
658                 get_x11_desktop_number(current_display, root, atom_number);
659                 get_x11_desktop_names(current_display, root, atom_names);
660                 get_x11_desktop_current_name(current_info->x11.desktop.all_names);
661
662                 /* Set the PropertyChangeMask on the root window, if not set */
663                 XGetWindowAttributes(display, root, &window_attributes);
664                 if (!(window_attributes.your_event_mask & PropertyChangeMask)) {
665                         XSetWindowAttributes attributes;
666                         attributes.event_mask = window_attributes.your_event_mask | PropertyChangeMask;
667                         XChangeWindowAttributes(display, root, CWEventMask, &attributes);
668                         XGetWindowAttributes(display, root, &window_attributes);
669                 }
670         } else {
671                 if (atom == atom_current) {
672                         get_x11_desktop_current(current_display, root, atom_current);
673                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
674                 } else if (atom == atom_number) {
675                         get_x11_desktop_number(current_display, root, atom_number);
676                 } else if (atom == atom_names) {
677                         get_x11_desktop_names(current_display, root, atom_names);
678                         get_x11_desktop_current_name(current_info->x11.desktop.all_names);
679                 }
680         }
681 }
682
683 void update_x11info(void)
684 {
685         struct information *current_info = &info;
686         if (!x_initialised == YES)
687                 return;
688         current_info->x11.monitor.number = XScreenCount(display);
689         current_info->x11.monitor.current = XDefaultScreen(display);
690 }
691
692 #ifdef OWN_WINDOW
693 /* reserve window manager space */
694 void set_struts(int sidenum)
695 {
696         Atom strut;
697         if ((strut = ATOM(_NET_WM_STRUT)) != None) {
698                 /* reserve space at left, right, top, bottom */
699                 signed long sizes[12] = {0};
700                 int i;
701
702                 /* define strut depth */
703                 switch (sidenum) {
704                         case 0:
705                                 /* left side */
706                                 sizes[0] = window.x + window.width;
707                                 break;
708                         case 1:
709                                 /* right side */
710                                 sizes[1] = display_width - window.x;
711                                 break;
712                         case 2:
713                                 /* top side */
714                                 sizes[2] = window.y + window.height;
715                                 break;
716                         case 3:
717                                 /* bottom side */
718                                 sizes[3] = display_height - window.y;
719                                 break;
720                 }
721
722                 /* define partial strut length */
723                 if (sidenum <= 1) {
724                         sizes[4 + (sidenum*2)] = window.y;
725                         sizes[5 + (sidenum*2)] = window.y + window.height;
726                 } else if (sidenum <= 3) {
727                         sizes[4 + (sidenum*2)] = window.x;
728                         sizes[5 + (sidenum*2)] = window.x + window.width;
729                 }
730
731                 /* check constraints */
732                 for (i = 0; i < 12; i++) {
733                         if (sizes[i] < 0) {
734                                 sizes[i] = 0;
735                         } else {
736                                 if (i <= 1 || i >= 8) {
737                                         if (sizes[i] > display_width) {
738                                                 sizes[i] = display_width;
739                                         }
740                                 } else {
741                                         if (sizes[i] > display_height) {
742                                                 sizes[i] = display_height;
743                                         }
744                                 }
745                         }
746                 }
747
748                 XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
749                                 PropModeReplace, (unsigned char *) &sizes, 4);
750
751                 if ((strut = ATOM(_NET_WM_STRUT_PARTIAL)) != None) {
752                         XChangeProperty(display, window.window, strut, XA_CARDINAL, 32,
753                                         PropModeReplace, (unsigned char *) &sizes, 12);
754                 }
755         }
756 }
757 #endif /* OWN_WINDOW */
758
759 #ifdef HAVE_XDBE
760 void xdbe_swap_buffers(void)
761 {
762         if (use_xdbe) {
763                 XdbeSwapInfo swap;
764
765                 swap.swap_window = window.window;
766                 swap.swap_action = XdbeBackground;
767                 XdbeSwapBuffers(display, &swap, 1);
768         }
769 }
770 #endif /* HAVE_XDBE */
771