Plug 3 memory leaks, and make fading windows always draw so that fast desktop changin...
[dana/xcompmgr.git] / xcompmgr.c
1 /*
2  * $Id$
3  *
4  * Copyright © 2003 Keith Packard
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of Keith Packard not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  Keith Packard makes no
13  * representations about the suitability of this software for any purpose.  It
14  * is provided "as is" without express or implied warranty.
15  *
16  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24
25
26 /* Modified by Matthew Hawn. I don't know what to say here so follow what it 
27    says above. Not that I can really do anything about it
28 */
29
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <math.h>
35 #include <sys/poll.h>
36 #include <sys/time.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <getopt.h>
40 #include <X11/Xlib.h>
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/Xcomposite.h>
44 #include <X11/extensions/Xdamage.h>
45 #include <X11/extensions/Xrender.h>
46
47 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
48 #define HAS_NAME_WINDOW_PIXMAP 1
49 #endif
50
51 #define CAN_DO_USABLE 0
52
53 typedef enum {
54     WINTYPE_DESKTOP,
55     WINTYPE_DOCK,
56     WINTYPE_TOOLBAR,
57     WINTYPE_MENU,
58     WINTYPE_UTILITY,
59     WINTYPE_SPLASH,
60     WINTYPE_DIALOG,
61     WINTYPE_NORMAL,
62     WINTYPE_DROPDOWN_MENU,
63     WINTYPE_POPUP_MENU,
64     WINTYPE_TOOLTIP,
65     WINTYPE_NOTIFY,
66     WINTYPE_COMBO,
67     WINTYPE_DND,
68     NUM_WINTYPES
69 } wintype;
70
71 typedef struct _ignore {
72     struct _ignore      *next;
73     unsigned long       sequence;
74 } ignore;
75
76 typedef struct _win {
77     struct _win         *next;
78     Window              id;
79 #if HAS_NAME_WINDOW_PIXMAP
80     Pixmap              pixmap;
81 #endif
82     XWindowAttributes   a;
83 #if CAN_DO_USABLE
84     Bool                usable;             /* mapped and all damaged at one point */
85     XRectangle          damage_bounds;      /* bounds of damage */
86 #endif
87     int                 mode;
88     int                 damaged;
89     Damage              damage;
90     Picture             picture;
91     Picture             alphaPict;
92     Picture             shadowPict;
93     XserverRegion       borderSize;
94     XserverRegion       extents;
95     Picture             shadow;
96     int                 shadow_dx;
97     int                 shadow_dy;
98     int                 shadow_width;
99     int                 shadow_height;
100     unsigned int        opacity;
101     wintype             windowType;
102     unsigned long       damage_sequence;    /* sequence when damage was created */
103
104     Bool                need_configure;
105     XConfigureEvent     queue_configure;
106
107     /* for drawing translucent windows */
108     XserverRegion       borderClip;
109     struct _win         *prev_trans;
110 } win;
111
112 typedef struct _conv {
113     int     size;
114     double  *data;
115 } conv;
116
117 typedef struct _fade {
118     struct _fade        *next;
119     win                 *w;
120     double              cur;
121     double              finish;
122     double              step;
123     void                (*callback) (Display *dpy, win *w);
124     Display             *dpy;
125 } fade;
126
127 win             *list;
128 fade            *fades;
129 Display         *dpy;
130 int             scr;
131 Window          root;
132 Picture         rootPicture;
133 Picture         rootBuffer;
134 Picture         blackPicture;
135 Picture         transBlackPicture;
136 Picture         rootTile;
137 XserverRegion   allDamage;
138 Bool            clipChanged;
139 #if HAS_NAME_WINDOW_PIXMAP
140 Bool            hasNamePixmap;
141 #endif
142 int             root_height, root_width;
143 ignore          *ignore_head, **ignore_tail = &ignore_head;
144 int             xfixes_event, xfixes_error;
145 int             damage_event, damage_error;
146 int             composite_event, composite_error;
147 int             render_event, render_error;
148 Bool            synchronize;
149 int             composite_opcode;
150
151 /* find these once and be done with it */
152 Atom            opacityAtom;
153 Atom            winTypeAtom;
154 Atom            winType[NUM_WINTYPES];
155 double          winTypeOpacity[NUM_WINTYPES];
156 Bool            winTypeShadow[NUM_WINTYPES];
157 Bool            winTypeFade[NUM_WINTYPES];
158
159 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
160 #define OPACITY_PROP    "_NET_WM_WINDOW_OPACITY"
161 #define REGISTER_PROP   "_NET_WM_CM_S"
162
163 #define TRANSLUCENT     0xe0000000
164 #define OPAQUE          0xffffffff
165
166 conv            *gaussianMap;
167
168 #define WINDOW_SOLID    0
169 #define WINDOW_TRANS    1
170 #define WINDOW_ARGB     2
171
172 #define TRANS_OPACITY   0.75
173
174 #define DEBUG_REPAINT 0
175 #define DEBUG_EVENTS 0
176 #define MONITOR_REPAINT 0
177
178 #define SHADOWS         1
179 #define SHARP_SHADOW    0
180
181 typedef enum _compMode {
182     CompSimple,         /* looks like a regular X server */
183     CompServerShadows,  /* use window alpha for shadow; sharp, but precise */
184     CompClientShadows,  /* use window extents for shadow, blurred */
185 } CompMode;
186
187 static void
188 determine_mode(Display *dpy, win *w);
189     
190 static double
191 get_opacity_percent(Display *dpy, win *w);
192
193 static XserverRegion
194 win_extents (Display *dpy, win *w);
195
196 CompMode    compMode = CompSimple;
197
198 int         shadowRadius = 12;
199 int         shadowOffsetX = -15;
200 int         shadowOffsetY = -15;
201 double      shadowOpacity = .75;
202
203 double  fade_in_step =  0.028;
204 double  fade_out_step = 0.03;
205 int     fade_delta =    10;
206 int     fade_time =     0;
207 Bool    fadeTrans = False;
208
209 Bool    autoRedirect = False;
210
211 /* For shadow precomputation */
212 int            Gsize = -1;
213 unsigned char *shadowCorner = NULL;
214 unsigned char *shadowTop = NULL;
215
216 int
217 get_time_in_milliseconds ()
218 {
219     struct timeval  tv;
220
221     gettimeofday (&tv, NULL);
222     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
223 }
224
225 fade *
226 find_fade (win *w)
227 {
228     fade    *f;
229     
230     for (f = fades; f; f = f->next)
231     {
232         if (f->w == w)
233             return f;
234     }
235     return 0;
236 }
237
238 void
239 dequeue_fade (Display *dpy, fade *f)
240 {
241     fade    **prev;
242
243     for (prev = &fades; *prev; prev = &(*prev)->next)
244         if (*prev == f)
245         {
246             *prev = f->next;
247             if (f->callback)
248                 (*f->callback) (dpy, f->w);
249             free (f);
250             break;
251         }
252 }
253
254 void
255 cleanup_fade (Display *dpy, win *w)
256 {
257     fade *f = find_fade (w);
258     if (f)
259         dequeue_fade (dpy, f);
260 }
261
262 void
263 enqueue_fade (Display *dpy, fade *f)
264 {
265     if (!fades)
266         fade_time = get_time_in_milliseconds () + fade_delta;
267     f->next = fades;
268     fades = f;
269 }
270
271 static void
272 set_fade (Display *dpy, win *w, double start, double finish, double step,
273           void (*callback) (Display *dpy, win *w),
274           Bool exec_callback, Bool override)
275 {
276     fade    *f;
277
278     f = find_fade (w);
279     if (!f)
280     {
281         f = malloc (sizeof (fade));
282         f->next = 0;
283         f->w = w;
284         f->cur = start;
285         enqueue_fade (dpy, f);
286     }
287     else if(!override)
288         return;
289     else
290     {
291         if (exec_callback)
292             if (f->callback)
293                 (*f->callback)(dpy, f->w);
294     }
295
296     if (finish < 0)
297         finish = 0;
298     if (finish > 1)
299         finish = 1;
300     f->finish = finish;
301     if (f->cur < finish)
302         f->step = step;
303     else if (f->cur > finish)
304         f->step = -step;
305     f->callback = callback;
306     w->opacity = f->cur * OPAQUE;
307 #if 0
308     printf ("set_fade start %g step %g\n", f->cur, f->step);
309 #endif
310     determine_mode (dpy, w);
311     if (w->shadow)
312     {
313         XRenderFreePicture (dpy, w->shadow);
314         w->shadow = None;
315
316         if (w->extents != None)
317             XFixesDestroyRegion (dpy, w->extents);
318
319         /* rebuild the shadow */
320         w->extents = win_extents (dpy, w);
321     }
322
323     /* fading windows need to be drawn, mark them as damaged.
324        when a window maps, if it tries to fade in but it already at the right
325        opacity (map/unmap/map fast) then it will never get drawn without this
326        until it repaints */
327     w->damaged = 1;
328 }
329
330 int
331 fade_timeout (void)
332 {
333     int now;
334     int delta;
335     if (!fades)
336         return -1;
337     now = get_time_in_milliseconds();
338     delta = fade_time - now;
339     if (delta < 0)
340         delta = 0;
341 /*    printf ("timeout %d\n", delta); */
342     return delta;
343 }
344
345 void
346 run_fades (Display *dpy)
347 {
348     int     now = get_time_in_milliseconds();
349     fade    *next = fades;
350     int     steps;
351     Bool    need_dequeue;
352
353 #if 0
354     printf ("run fades\n");
355 #endif
356     if (fade_time - now > 0)
357         return;
358     steps = 1 + (now - fade_time) / fade_delta;
359
360     while (next)
361     {
362         fade *f = next;
363         win *w = f->w;
364         next = f->next;
365         f->cur += f->step * steps;
366         if (f->cur >= 1)
367             f->cur = 1;
368         else if (f->cur < 0)
369             f->cur = 0;
370 #if 0
371         printf ("opacity now %g\n", f->cur);
372 #endif
373         w->opacity = f->cur * OPAQUE;
374         need_dequeue = False;
375         if (f->step > 0)
376         {
377             if (f->cur >= f->finish)
378             {
379                 w->opacity = f->finish*OPAQUE;
380                 need_dequeue = True;
381             }
382         }
383         else
384         {
385             if (f->cur <= f->finish)
386             {
387                 w->opacity = f->finish*OPAQUE;
388                 need_dequeue = True;
389             }
390         }
391         determine_mode (dpy, w);
392         if (w->shadow)
393         {
394             XRenderFreePicture (dpy, w->shadow);
395             w->shadow = None;
396
397             if (w->extents != None)
398                 XFixesDestroyRegion (dpy, w->extents);
399
400             /* rebuild the shadow */
401             w->extents = win_extents(dpy, w);
402         }
403         /* Must do this last as it might destroy f->w in callbacks */
404         if (need_dequeue)
405                 dequeue_fade (dpy, f);
406     }
407     fade_time = now + fade_delta;
408 }
409
410 static double
411 gaussian (double r, double x, double y)
412 {
413     return ((1 / (sqrt (2 * M_PI * r))) *
414             exp ((- (x * x + y * y)) / (2 * r * r)));
415 }
416
417
418 static conv *
419 make_gaussian_map (Display *dpy, double r)
420 {
421     conv            *c;
422     int             size = ((int) ceil ((r * 3)) + 1) & ~1;
423     int             center = size / 2;
424     int             x, y;
425     double          t;
426     double          g;
427     
428     c = malloc (sizeof (conv) + size * size * sizeof (double));
429     c->size = size;
430     c->data = (double *) (c + 1);
431     t = 0.0;
432     for (y = 0; y < size; y++)
433         for (x = 0; x < size; x++)
434         {
435             g = gaussian (r, (double) (x - center), (double) (y - center));
436             t += g;
437             c->data[y * size + x] = g;
438         }
439 /*    printf ("gaussian total %f\n", t); */
440     for (y = 0; y < size; y++)
441         for (x = 0; x < size; x++)
442         {
443             c->data[y*size + x] /= t;
444         }
445     return c;
446 }
447
448 /*
449  * A picture will help
450  *
451  *      -center   0                width  width+center
452  *  -center +-----+-------------------+-----+
453  *          |     |                   |     |
454  *          |     |                   |     |
455  *        0 +-----+-------------------+-----+
456  *          |     |                   |     |
457  *          |     |                   |     |
458  *          |     |                   |     |
459  *   height +-----+-------------------+-----+
460  *          |     |                   |     |
461  * height+  |     |                   |     |
462  *  center  +-----+-------------------+-----+
463  */
464  
465 static unsigned char
466 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
467 {
468     int     fx, fy;
469     double  *g_data;
470     double  *g_line = map->data;
471     int     g_size = map->size;
472     int     center = g_size / 2;
473     int     fx_start, fx_end;
474     int     fy_start, fy_end;
475     double  v;
476     
477     /*
478      * Compute set of filter values which are "in range",
479      * that's the set with:
480      *  0 <= x + (fx-center) && x + (fx-center) < width &&
481      *  0 <= y + (fy-center) && y + (fy-center) < height
482      *
483      *  0 <= x + (fx - center)  x + fx - center < width
484      *  center - x <= fx        fx < width + center - x
485      */
486
487     fx_start = center - x;
488     if (fx_start < 0)
489         fx_start = 0;
490     fx_end = width + center - x;
491     if (fx_end > g_size)
492         fx_end = g_size;
493
494     fy_start = center - y;
495     if (fy_start < 0)
496         fy_start = 0;
497     fy_end = height + center - y;
498     if (fy_end > g_size)
499         fy_end = g_size;
500
501     g_line = g_line + fy_start * g_size + fx_start;
502     
503     v = 0;
504     for (fy = fy_start; fy < fy_end; fy++)
505     {
506         g_data = g_line;
507         g_line += g_size;
508         
509         for (fx = fx_start; fx < fx_end; fx++)
510             v += *g_data++;
511     }
512     if (v > 1)
513         v = 1;
514     
515     return ((unsigned char) (v * opacity * 255.0));
516 }
517
518 /* precompute shadow corners and sides to save time for large windows */
519 static void
520 presum_gaussian (conv *map)
521 {
522     int center = map->size/2;
523     int opacity, x, y;
524
525     Gsize = map->size;
526
527     if (shadowCorner)
528         free ((void *)shadowCorner);
529     if (shadowTop)
530         free ((void *)shadowTop);
531
532     shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
533     shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
534     
535     for (x = 0; x <= Gsize; x++)
536     {
537         shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
538         for(opacity = 0; opacity < 25; opacity++)
539             shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
540         for(y = 0; y <= x; y++)
541         {
542             shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
543                 = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
544             shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
545                 = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
546             for(opacity = 0; opacity < 25; opacity++)
547                 shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
548                     = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
549                     = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
550         }
551     }
552 }
553
554 static XImage *
555 make_shadow (Display *dpy, double opacity, int width, int height)
556 {
557     XImage          *ximage;
558     unsigned char   *data;
559     int             gsize = gaussianMap->size;
560     int             ylimit, xlimit;
561     int             swidth = width + gsize;
562     int             sheight = height + gsize;
563     int             center = gsize / 2;
564     int             x, y;
565     unsigned char   d;
566     int             x_diff;
567     int             opacity_int = (int)(opacity * 25);
568     data = malloc (swidth * sheight * sizeof (unsigned char));
569     if (!data)
570         return 0;
571     ximage = XCreateImage (dpy,
572                            DefaultVisual(dpy, DefaultScreen(dpy)),
573                            8,
574                            ZPixmap,
575                            0,
576                            (char *) data,
577                            swidth, sheight, 8, swidth * sizeof (unsigned char));
578     if (!ximage)
579     {
580         free (data);
581         return 0;
582     }
583     /*
584      * Build the gaussian in sections
585      */
586
587     /*
588      * center (fill the complete data array)
589      */
590     if (Gsize > 0)
591         d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
592     else
593         d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
594     memset(data, d, sheight * swidth);
595     
596     /*
597      * corners
598      */
599     ylimit = gsize;
600     if (ylimit > sheight / 2)
601         ylimit = (sheight + 1) / 2;
602     xlimit = gsize;
603     if (xlimit > swidth / 2)
604         xlimit = (swidth + 1) / 2;
605
606     for (y = 0; y < ylimit; y++)
607         for (x = 0; x < xlimit; x++)
608         {
609             if (xlimit == Gsize && ylimit == Gsize)
610                 d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
611             else
612                 d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
613             data[y * swidth + x] = d;
614             data[(sheight - y - 1) * swidth + x] = d;
615             data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
616             data[y * swidth + (swidth - x - 1)] = d;
617         }
618
619     /*
620      * top/bottom
621      */
622     x_diff = swidth - (gsize * 2);
623     if (x_diff > 0 && ylimit > 0)
624     {
625         for (y = 0; y < ylimit; y++)
626         {
627             if (ylimit == Gsize)
628                 d = shadowTop[opacity_int * (Gsize + 1) + y];
629             else
630                 d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
631             memset (&data[y * swidth + gsize], d, x_diff);
632             memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
633         }
634     }
635
636     /*
637      * sides
638      */
639     
640     for (x = 0; x < xlimit; x++)
641     {
642         if (xlimit == Gsize)
643             d = shadowTop[opacity_int * (Gsize + 1) + x];
644         else
645             d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
646         for (y = gsize; y < sheight - gsize; y++)
647         {
648             data[y * swidth + x] = d;
649             data[y * swidth + (swidth - x - 1)] = d;
650         }
651     }
652
653     return ximage;
654 }
655
656 static Picture
657 shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
658 {
659     XImage  *shadowImage;
660     Pixmap  shadowPixmap;
661     Picture shadowPicture;
662     GC      gc;
663     
664     shadowImage = make_shadow (dpy, opacity, width, height);
665     if (!shadowImage)
666         return None;
667     shadowPixmap = XCreatePixmap (dpy, root, 
668                                   shadowImage->width,
669                                   shadowImage->height,
670                                   8);
671     if (!shadowPixmap)
672     {
673         XDestroyImage (shadowImage);
674         return None;
675     }
676
677     shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
678                                           XRenderFindStandardFormat (dpy, PictStandardA8),
679                                           0, 0);
680     if (!shadowPicture)
681     {
682         XDestroyImage (shadowImage);
683         XFreePixmap (dpy, shadowPixmap);
684         return None;
685     }
686
687     gc = XCreateGC (dpy, shadowPixmap, 0, 0);
688     if (!gc)
689     {
690         XDestroyImage (shadowImage);
691         XFreePixmap (dpy, shadowPixmap);
692         XRenderFreePicture (dpy, shadowPicture);
693         return None;
694     }
695     
696     XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0, 
697                shadowImage->width,
698                shadowImage->height);
699     *wp = shadowImage->width;
700     *hp = shadowImage->height;
701     XFreeGC (dpy, gc);
702     XDestroyImage (shadowImage);
703     XFreePixmap (dpy, shadowPixmap);
704     return shadowPicture;
705 }
706
707 Picture
708 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
709 {
710     Pixmap                      pixmap;
711     Picture                     picture;
712     XRenderPictureAttributes    pa;
713     XRenderColor                c;
714
715     pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
716     if (!pixmap)
717         return None;
718
719     pa.repeat = True;
720     picture = XRenderCreatePicture (dpy, pixmap,
721                                     XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
722                                     CPRepeat,
723                                     &pa);
724     if (!picture)
725     {
726         XFreePixmap (dpy, pixmap);
727         return None;
728     }
729
730     c.alpha = a * 0xffff;
731     c.red = r * 0xffff;
732     c.green = g * 0xffff;
733     c.blue = b * 0xffff;
734     XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
735     XFreePixmap (dpy, pixmap);
736     return picture;
737 }
738
739 void
740 discard_ignore (Display *dpy, unsigned long sequence)
741 {
742     while (ignore_head)
743     {
744         if ((long) (sequence - ignore_head->sequence) > 0)
745         {
746             ignore  *next = ignore_head->next;
747             free (ignore_head);
748             ignore_head = next;
749             if (!ignore_head)
750                 ignore_tail = &ignore_head;
751         }
752         else
753             break;
754     }
755 }
756
757 void
758 set_ignore (Display *dpy, unsigned long sequence)
759 {
760     ignore  *i = malloc (sizeof (ignore));
761     if (!i)
762         return;
763     i->sequence = sequence;
764     i->next = 0;
765     *ignore_tail = i;
766     ignore_tail = &i->next;
767 }
768
769 int
770 should_ignore (Display *dpy, unsigned long sequence)
771 {
772     discard_ignore (dpy, sequence);
773     return ignore_head && ignore_head->sequence == sequence;
774 }
775
776 static win *
777 find_win (Display *dpy, Window id)
778 {
779     win *w;
780
781     for (w = list; w; w = w->next)
782         if (w->id == id)
783             return w;
784     return 0;
785 }
786
787 static const char *backgroundProps[] = {
788     "_XROOTPMAP_ID",
789     "_XSETROOT_ID",
790     0,
791 };
792     
793 static Picture
794 root_tile (Display *dpy)
795 {
796     Picture         picture;
797     Atom            actual_type;
798     Pixmap          pixmap;
799     int             actual_format;
800     unsigned long   nitems;
801     unsigned long   bytes_after;
802     unsigned char   *prop;
803     Bool            fill;
804     XRenderPictureAttributes    pa;
805     int             p;
806
807     pixmap = None;
808     for (p = 0; backgroundProps[p]; p++)
809     {
810         if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
811                                 0, 4, False, AnyPropertyType,
812                                 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
813             actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
814         {
815             memcpy (&pixmap, prop, 4);
816             XFree (prop);
817             fill = False;
818             break;
819         }
820     }
821     if (!pixmap)
822     {
823         pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
824         fill = True;
825     }
826     pa.repeat = True;
827     picture = XRenderCreatePicture (dpy, pixmap,
828                                     XRenderFindVisualFormat (dpy,
829                                                              DefaultVisual (dpy, scr)),
830                                     CPRepeat, &pa);
831     if (fill)
832     {
833         XRenderColor    c;
834         
835         c.red = c.green = c.blue = 0x8080;
836         c.alpha = 0xffff;
837         XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 
838                               0, 0, 1, 1);
839     }
840     return picture;
841 }
842
843 static void
844 paint_root (Display *dpy)
845 {
846     if (!rootTile)
847         rootTile = root_tile (dpy);
848     
849     XRenderComposite (dpy, PictOpSrc,
850                       rootTile, None, rootBuffer,
851                       0, 0, 0, 0, 0, 0, root_width, root_height);
852 }
853
854 static XserverRegion
855 win_extents (Display *dpy, win *w)
856 {
857     XRectangle      r;
858     
859     r.x = w->a.x;
860     r.y = w->a.y;
861     r.width = w->a.width + w->a.border_width * 2;
862     r.height = w->a.height + w->a.border_width * 2;
863     if (winTypeShadow[w->windowType])
864     {
865         if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
866         {
867             XRectangle  sr;
868
869             if (compMode == CompServerShadows)
870             {
871                 w->shadow_dx = 2;
872                 w->shadow_dy = 7;
873                 w->shadow_width = w->a.width;
874                 w->shadow_height = w->a.height;
875             }
876             else
877             {
878                 w->shadow_dx = shadowOffsetX;
879                 w->shadow_dy = shadowOffsetY;
880                 if (!w->shadow)
881                 {
882                     double      opacity = shadowOpacity;
883                     if (w->mode == WINDOW_TRANS)
884                         opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
885                     w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
886                                                 w->a.width + w->a.border_width * 2,
887                                                 w->a.height + w->a.border_width * 2,
888                                                 &w->shadow_width, &w->shadow_height);
889                 }
890             }
891             sr.x = w->a.x + w->shadow_dx;
892             sr.y = w->a.y + w->shadow_dy;
893             sr.width = w->shadow_width;
894             sr.height = w->shadow_height;
895             if (sr.x < r.x)
896             {
897                 r.width = (r.x + r.width) - sr.x;
898                 r.x = sr.x;
899             }
900             if (sr.y < r.y)
901             {
902                 r.height = (r.y + r.height) - sr.y;
903                 r.y = sr.y;
904             }
905             if (sr.x + sr.width > r.x + r.width)
906                 r.width = sr.x + sr.width - r.x;
907             if (sr.y + sr.height > r.y + r.height)
908                 r.height = sr.y + sr.height - r.y;
909         }
910     }
911     return XFixesCreateRegion (dpy, &r, 1);
912 }
913
914 static XserverRegion
915 border_size (Display *dpy, win *w)
916 {
917     XserverRegion   border;
918     /*
919      * if window doesn't exist anymore,  this will generate an error
920      * as well as not generate a region.  Perhaps a better XFixes
921      * architecture would be to have a request that copies instead
922      * of creates, that way you'd just end up with an empty region
923      * instead of an invalid XID.
924      */
925     set_ignore (dpy, NextRequest (dpy));
926     border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
927     /* translate this */
928     set_ignore (dpy, NextRequest (dpy));
929     XFixesTranslateRegion (dpy, border,
930                            w->a.x + w->a.border_width,
931                            w->a.y + w->a.border_width);
932     return border;
933 }
934
935 static void
936 paint_all (Display *dpy, XserverRegion region)
937 {
938     win *w;
939     win *t = 0;
940     
941     if (!region)
942     {
943         XRectangle  r;
944         r.x = 0;
945         r.y = 0;
946         r.width = root_width;
947         r.height = root_height;
948         region = XFixesCreateRegion (dpy, &r, 1);
949     }
950 #if MONITOR_REPAINT
951     rootBuffer = rootPicture;
952 #else
953     if (!rootBuffer)
954     {
955         Pixmap  rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
956                                             DefaultDepth (dpy, scr));
957         rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
958                                            XRenderFindVisualFormat (dpy,
959                                                                     DefaultVisual (dpy, scr)),
960                                            0, 0);
961         XFreePixmap (dpy, rootPixmap);
962     }
963 #endif
964     XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
965 #if MONITOR_REPAINT
966     XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
967                       0, 0, 0, 0, 0, 0, root_width, root_height);
968 #endif
969 #if DEBUG_REPAINT
970     printf ("paint:");
971 #endif
972     for (w = list; w; w = w->next)
973     {
974 #if CAN_DO_USABLE
975         if (!w->usable)
976             continue;
977 #endif
978         /* never painted, ignore it */
979         if (!w->damaged)
980             continue;
981         /* if invisible, ignore it */
982         if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
983             || w->a.x >= root_width || w->a.y >= root_height)
984             continue;
985         if (!w->picture)
986         {
987             XRenderPictureAttributes    pa;
988             XRenderPictFormat           *format;
989             Drawable                    draw = w->id;
990             
991 #if HAS_NAME_WINDOW_PIXMAP
992             if (hasNamePixmap && !w->pixmap)
993                 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
994             if (w->pixmap)
995                 draw = w->pixmap;
996 #endif
997             format = XRenderFindVisualFormat (dpy, w->a.visual);
998             pa.subwindow_mode = IncludeInferiors;
999             w->picture = XRenderCreatePicture (dpy, draw,
1000                                                format,
1001                                                CPSubwindowMode,
1002                                                &pa);
1003         }
1004 #if DEBUG_REPAINT
1005         printf (" 0x%x", w->id);
1006 #endif
1007         if (clipChanged)
1008         {
1009             if (w->borderSize)
1010             {
1011                 set_ignore (dpy, NextRequest (dpy));
1012                 XFixesDestroyRegion (dpy, w->borderSize);
1013                 w->borderSize = None;
1014             }
1015             if (w->extents)
1016             {
1017                 XFixesDestroyRegion (dpy, w->extents);
1018                 w->extents = None;
1019             }
1020             if (w->borderClip)
1021             {
1022                 XFixesDestroyRegion (dpy, w->borderClip);
1023                 w->borderClip = None;
1024             }
1025         }
1026         if (!w->borderSize)
1027             w->borderSize = border_size (dpy, w);
1028         if (!w->extents)
1029             w->extents = win_extents (dpy, w);
1030         if (w->mode == WINDOW_SOLID)
1031         {
1032             int x, y, wid, hei;
1033 #if HAS_NAME_WINDOW_PIXMAP
1034             x = w->a.x;
1035             y = w->a.y;
1036             wid = w->a.width + w->a.border_width * 2;
1037             hei = w->a.height + w->a.border_width * 2;
1038 #else
1039             x = w->a.x + w->a.border_width;
1040             y = w->a.y + w->a.border_width;
1041             wid = w->a.width;
1042             hei = w->a.height;
1043 #endif
1044             XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1045             set_ignore (dpy, NextRequest (dpy));
1046             XFixesSubtractRegion (dpy, region, region, w->borderSize);
1047             set_ignore (dpy, NextRequest (dpy));
1048             XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
1049                               0, 0, 0, 0, 
1050                               x, y, wid, hei);
1051         }
1052         if (!w->borderClip)
1053         {
1054             w->borderClip = XFixesCreateRegion (dpy, 0, 0);
1055             XFixesCopyRegion (dpy, w->borderClip, region);
1056         }
1057         w->prev_trans = t;
1058         t = w;
1059     }
1060 #if DEBUG_REPAINT
1061     printf ("\n");
1062     fflush (stdout);
1063 #endif
1064     XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
1065     paint_root (dpy);
1066     for (w = t; w; w = w->prev_trans)
1067     {
1068         XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
1069         if (winTypeShadow[w->windowType]) {
1070             switch (compMode) {
1071             case CompSimple:
1072                 break;
1073             case CompServerShadows:
1074                 set_ignore (dpy, NextRequest (dpy));
1075                 if (w->opacity != OPAQUE && !w->shadowPict)
1076                     w->shadowPict = solid_picture (dpy, True,
1077                                                    (double) w->opacity / OPAQUE * 0.3,
1078                                                    0, 0, 0);
1079                 XRenderComposite (dpy, PictOpOver,
1080                                   w->shadowPict ? w->shadowPict : transBlackPicture,
1081                                   w->picture, rootBuffer,
1082                                   0, 0, 0, 0,
1083                                   w->a.x + w->shadow_dx,
1084                                   w->a.y + w->shadow_dy,
1085                                   w->shadow_width, w->shadow_height);
1086                 break;
1087             case CompClientShadows:
1088                 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
1089                                   0, 0, 0, 0,
1090                                   w->a.x + w->shadow_dx,
1091                                   w->a.y + w->shadow_dy,
1092                                   w->shadow_width, w->shadow_height);
1093                 break;
1094             }
1095         }
1096         if (w->opacity != OPAQUE && !w->alphaPict)
1097             w->alphaPict = solid_picture (dpy, False, 
1098                                           (double) w->opacity / OPAQUE, 0, 0, 0);
1099         if (w->mode == WINDOW_TRANS)
1100         {
1101             int x, y, wid, hei;
1102 #if HAS_NAME_WINDOW_PIXMAP
1103             x = w->a.x;
1104             y = w->a.y;
1105             wid = w->a.width + w->a.border_width * 2;
1106             hei = w->a.height + w->a.border_width * 2;
1107 #else
1108             x = w->a.x + w->a.border_width;
1109             y = w->a.y + w->a.border_width;
1110             wid = w->a.width;
1111             hei = w->a.height;
1112 #endif
1113             set_ignore (dpy, NextRequest (dpy));
1114             XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1115                               0, 0, 0, 0, 
1116                               x, y, wid, hei);
1117         }
1118         else if (w->mode == WINDOW_ARGB)
1119         {
1120             int x, y, wid, hei;
1121 #if HAS_NAME_WINDOW_PIXMAP
1122             x = w->a.x;
1123             y = w->a.y;
1124             wid = w->a.width + w->a.border_width * 2;
1125             hei = w->a.height + w->a.border_width * 2;
1126 #else
1127             x = w->a.x + w->a.border_width;
1128             y = w->a.y + w->a.border_width;
1129             wid = w->a.width;
1130             hei = w->a.height;
1131 #endif
1132             set_ignore (dpy, NextRequest (dpy));
1133             XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
1134                               0, 0, 0, 0, 
1135                               x, y, wid, hei);
1136         }
1137         XFixesDestroyRegion (dpy, w->borderClip);
1138         w->borderClip = None;
1139     }
1140     XFixesDestroyRegion (dpy, region);
1141     if (rootBuffer != rootPicture)
1142     {
1143         XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
1144         XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
1145                           0, 0, 0, 0, 0, 0, root_width, root_height);
1146     }
1147 }
1148
1149 static void
1150 add_damage (Display *dpy, XserverRegion damage)
1151 {
1152     if (allDamage)
1153     {
1154         XFixesUnionRegion (dpy, allDamage, allDamage, damage);
1155         XFixesDestroyRegion (dpy, damage);
1156     }
1157     else
1158         allDamage = damage;
1159 }
1160
1161 static void
1162 repair_win (Display *dpy, win *w)
1163 {
1164     XserverRegion   parts;
1165
1166     if (!w->damaged)
1167     {
1168         parts = win_extents (dpy, w);
1169         set_ignore (dpy, NextRequest (dpy));
1170         XDamageSubtract (dpy, w->damage, None, None);
1171     }
1172     else
1173     {
1174         XserverRegion   o;
1175         parts = XFixesCreateRegion (dpy, 0, 0);
1176         set_ignore (dpy, NextRequest (dpy));
1177         XDamageSubtract (dpy, w->damage, None, parts);
1178         XFixesTranslateRegion (dpy, parts,
1179                                w->a.x + w->a.border_width,
1180                                w->a.y + w->a.border_width);
1181         if (compMode == CompServerShadows)
1182         {
1183             o = XFixesCreateRegion (dpy, 0, 0);
1184             XFixesCopyRegion (dpy, o, parts);
1185             XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1186             XFixesUnionRegion (dpy, parts, parts, o);
1187             XFixesDestroyRegion (dpy, o);
1188         }
1189     }
1190     add_damage (dpy, parts);
1191     w->damaged = 1;
1192 }
1193
1194 static const char*
1195 wintype_name(wintype type)
1196 {
1197     const char *t;
1198     switch (type) {
1199     case WINTYPE_DESKTOP: t = "desktop"; break;
1200     case WINTYPE_DOCK:    t = "dock"; break;
1201     case WINTYPE_TOOLBAR: t = "toolbar"; break;
1202     case WINTYPE_MENU:    t = "menu"; break;
1203     case WINTYPE_UTILITY: t = "utility"; break;
1204     case WINTYPE_SPLASH:  t = "slash"; break;
1205     case WINTYPE_DIALOG:  t = "dialog"; break;
1206     case WINTYPE_NORMAL:  t = "normal"; break;
1207     case WINTYPE_DROPDOWN_MENU: t = "dropdown"; break;
1208     case WINTYPE_POPUP_MENU: t = "popup"; break;
1209     case WINTYPE_TOOLTIP: t = "tooltip"; break;
1210     case WINTYPE_NOTIFY:  t = "notification"; break;
1211     case WINTYPE_COMBO:   t = "combo"; break;
1212     case WINTYPE_DND:     t = "dnd"; break;
1213     default:              t = "unknown"; break;
1214     }
1215     return t;
1216 }
1217
1218 static wintype
1219 get_wintype_prop(Display * dpy, Window w)
1220 {
1221     Atom actual;
1222     wintype ret;
1223     int format;
1224     unsigned long n, left, off;
1225     unsigned char *data;
1226
1227     ret = (wintype)-1;
1228     off = 0;
1229
1230     do {
1231         set_ignore (dpy, NextRequest (dpy));
1232         int result = XGetWindowProperty (dpy, w, winTypeAtom, off, 1L, False,
1233                                          XA_ATOM, &actual, &format,
1234                                          &n, &left, &data);
1235
1236         if (result != Success)
1237             break;
1238         if (data != None)
1239         {
1240             int i;
1241
1242             for (i = 0; i < NUM_WINTYPES; ++i) {
1243                 Atom a;
1244                 memcpy (&a, data, sizeof (Atom));
1245                 if (a == winType[i]) {
1246                     /* known type */
1247                     ret = i;
1248                     break;
1249                 }
1250             }
1251
1252             XFree ( (void *) data);
1253         }
1254
1255         ++off;
1256     } while (left >= 4 && ret == (wintype)-1);
1257
1258     return ret;
1259 }
1260
1261 static wintype
1262 determine_wintype (Display *dpy, Window w, Window top)
1263 {
1264     Window       root_return, parent_return;
1265     Window      *children = NULL;
1266     unsigned int nchildren, i;
1267     wintype      type;
1268
1269     type = get_wintype_prop (dpy, w);
1270     if (type != (wintype)-1)
1271         return type;
1272
1273     set_ignore (dpy, NextRequest (dpy));
1274     if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
1275                             &nchildren))
1276     {
1277         /* XQueryTree failed. */
1278         if (children)
1279             XFree ((void *)children);
1280         return (wintype)-1;
1281     }
1282
1283     for (i = 0;i < nchildren;i++)
1284     {
1285         type = determine_wintype (dpy, children[i], top);
1286         if (type != (wintype)-1)
1287             return type;
1288     }
1289
1290     if (children)
1291         XFree ((void *)children);
1292
1293     if (w != top)
1294         return (wintype)-1;
1295     else
1296         return WINTYPE_NORMAL;
1297 }
1298
1299 static unsigned int
1300 get_opacity_prop (Display *dpy, win *w, unsigned int def);
1301
1302 static void
1303 configure_win (Display *dpy, XConfigureEvent *ce);
1304
1305 static void
1306 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1307 {
1308     win         *w = find_win (dpy, id);
1309
1310     if (!w)
1311         return;
1312
1313     w->a.map_state = IsViewable;
1314
1315     w->windowType = determine_wintype (dpy, w->id, w->id);
1316 #if 0
1317     printf("window 0x%x type %s\n", w->id, wintype_name(w->windowType));
1318 #endif
1319
1320     /* select before reading the property so that no property changes are lost */
1321     XSelectInput (dpy, id, PropertyChangeMask);
1322     w->opacity = get_opacity_prop (dpy, w, OPAQUE);
1323
1324     determine_mode (dpy, w);
1325
1326 #if CAN_DO_USABLE
1327     w->damage_bounds.x = w->damage_bounds.y = 0;
1328     w->damage_bounds.width = w->damage_bounds.height = 0;
1329 #endif
1330     w->damaged = 0;
1331
1332     if (fade && winTypeFade[w->windowType])
1333         set_fade (dpy, w, 0, get_opacity_percent (dpy, w), fade_in_step, 0, True, True);
1334
1335     /* if any configure events happened while the window was unmapped, then
1336        configure the window to its correct place */
1337     if (w->need_configure)
1338         configure_win (dpy, &w->queue_configure);
1339 }
1340
1341 static void
1342 finish_unmap_win (Display *dpy, win *w)
1343 {
1344     w->damaged = 0;
1345 #if CAN_DO_USABLE
1346     w->usable = False;
1347 #endif
1348     if (w->extents != None)
1349     {
1350         add_damage (dpy, w->extents);    /* destroys region */
1351         w->extents = None;
1352     }
1353     
1354 #if HAS_NAME_WINDOW_PIXMAP
1355     if (w->pixmap)
1356     {
1357         XFreePixmap (dpy, w->pixmap);
1358         w->pixmap = None;
1359     }
1360 #endif
1361
1362     if (w->picture)
1363     {
1364         set_ignore (dpy, NextRequest (dpy));
1365         XRenderFreePicture (dpy, w->picture);
1366         w->picture = None;
1367     }
1368
1369     if (w->borderSize)
1370     {
1371         set_ignore (dpy, NextRequest (dpy));
1372         XFixesDestroyRegion (dpy, w->borderSize);
1373         w->borderSize = None;
1374     }
1375     if (w->shadow)
1376     {
1377         XRenderFreePicture (dpy, w->shadow);
1378         w->shadow = None;
1379     }
1380     if (w->borderClip)
1381     {
1382         XFixesDestroyRegion (dpy, w->borderClip);
1383         w->borderClip = None;
1384     }
1385
1386     clipChanged = True;
1387 }
1388
1389 #if HAS_NAME_WINDOW_PIXMAP
1390 static void
1391 unmap_callback (Display *dpy, win *w)
1392 {
1393     finish_unmap_win (dpy, w);
1394 }
1395 #endif
1396
1397 static void
1398 unmap_win (Display *dpy, Window id, Bool fade)
1399 {
1400     win *w = find_win (dpy, id);
1401     if (!w)
1402         return;
1403     w->a.map_state = IsUnmapped;
1404
1405     /* don't care about properties anymore */
1406     set_ignore (dpy, NextRequest (dpy));
1407     XSelectInput(dpy, w->id, 0);
1408
1409 #if HAS_NAME_WINDOW_PIXMAP
1410     if (w->pixmap && fade && winTypeFade[w->windowType])
1411         set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, True);
1412     else
1413 #endif
1414         finish_unmap_win (dpy, w);
1415 }
1416
1417 /* Get the opacity prop from window
1418    not found: default
1419    otherwise the value
1420  */
1421 static unsigned int
1422 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1423 {
1424     Atom actual;
1425     int format;
1426     unsigned long n, left;
1427
1428     unsigned char *data;
1429     int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False, 
1430                        XA_CARDINAL, &actual, &format, 
1431                                     &n, &left, &data);
1432     if (result == Success && data != NULL)
1433     {
1434         unsigned int i;
1435         memcpy (&i, data, sizeof (unsigned int));
1436         XFree( (void *) data);
1437         return i;
1438     }
1439     return def;
1440 }
1441
1442 /* Get the opacity property from the window in a percent format
1443    not found: default
1444    otherwise: the value
1445 */
1446 static double
1447 get_opacity_percent(Display *dpy, win *w)
1448 {
1449     double def = winTypeOpacity[w->windowType];
1450     unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
1451
1452     return opacity*1.0/OPAQUE;
1453 }
1454
1455 static void
1456 determine_mode(Display *dpy, win *w)
1457 {
1458     int mode;
1459     XRenderPictFormat *format;
1460
1461     /* if trans prop == -1 fall back on  previous tests*/
1462
1463     if (w->alphaPict)
1464     {
1465         XRenderFreePicture (dpy, w->alphaPict);
1466         w->alphaPict = None;
1467     }
1468     if (w->shadowPict)
1469     {
1470         XRenderFreePicture (dpy, w->shadowPict);
1471         w->shadowPict = None;
1472     }
1473
1474     if (w->a.class == InputOnly)
1475     {
1476         format = 0;
1477     }
1478     else
1479     {
1480         format = XRenderFindVisualFormat (dpy, w->a.visual);
1481     }
1482
1483     if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1484     {
1485         mode = WINDOW_ARGB;
1486     }
1487     else if (w->opacity != OPAQUE)
1488     {
1489         mode = WINDOW_TRANS;
1490     }
1491     else
1492     {
1493         mode = WINDOW_SOLID;
1494     }
1495     w->mode = mode;
1496     if (w->extents)
1497     {
1498         XserverRegion damage;
1499         damage = XFixesCreateRegion (dpy, 0, 0);
1500         XFixesCopyRegion (dpy, damage, w->extents);
1501         add_damage (dpy, damage);
1502     }
1503 }
1504
1505 static void
1506 add_win (Display *dpy, Window id, Window prev)
1507 {
1508     win                         *new = malloc (sizeof (win));
1509     win                         **p;
1510     
1511     if (!new)
1512         return;
1513     if (prev)
1514     {
1515         for (p = &list; *p; p = &(*p)->next)
1516             if ((*p)->id == prev)
1517                 break;
1518     }
1519     else
1520         p = &list;
1521     new->id = id;
1522     set_ignore (dpy, NextRequest (dpy));
1523     if (!XGetWindowAttributes (dpy, id, &new->a))
1524     {
1525         free (new);
1526         return;
1527     }
1528     new->damaged = 0;
1529 #if CAN_DO_USABLE
1530     new->usable = False;
1531 #endif
1532 #if HAS_NAME_WINDOW_PIXMAP
1533     new->pixmap = None;
1534 #endif
1535     new->picture = None;
1536     if (new->a.class == InputOnly)
1537     {
1538         new->damage_sequence = 0;
1539         new->damage = None;
1540     }
1541     else
1542     {
1543         new->damage_sequence = NextRequest (dpy);
1544         new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1545     }
1546     new->alphaPict = None;
1547     new->shadowPict = None;
1548     new->borderSize = None;
1549     new->extents = None;
1550     new->shadow = None;
1551     new->shadow_dx = 0;
1552     new->shadow_dy = 0;
1553     new->shadow_width = 0;
1554     new->shadow_height = 0;
1555     new->opacity = OPAQUE;
1556     new->need_configure = False;
1557
1558     new->borderClip = None;
1559     new->prev_trans = 0;
1560
1561     new->next = *p;
1562     *p = new;
1563     if (new->a.map_state == IsViewable)
1564         map_win (dpy, id, new->damage_sequence - 1, True);
1565 }
1566
1567 void
1568 restack_win (Display *dpy, win *w, Window new_above)
1569 {
1570     Window  old_above;
1571     
1572     if (w->next)
1573         old_above = w->next->id;
1574     else
1575         old_above = None;
1576     if (old_above != new_above)
1577     {
1578         win **prev;
1579
1580         /* unhook */
1581         for (prev = &list; *prev; prev = &(*prev)->next)
1582             if ((*prev) == w)
1583                 break;
1584         *prev = w->next;
1585         
1586         /* rehook */
1587         for (prev = &list; *prev; prev = &(*prev)->next)
1588         {
1589             if ((*prev)->id == new_above)
1590                 break;
1591         }
1592         w->next = *prev;
1593         *prev = w;
1594     }
1595 }
1596
1597 static void
1598 configure_win (Display *dpy, XConfigureEvent *ce)
1599 {
1600     win             *w = find_win (dpy, ce->window);
1601     XserverRegion   damage = None;
1602     
1603     if (!w)
1604     {
1605         if (ce->window == root)
1606         {
1607             if (rootBuffer)
1608             {
1609                 XRenderFreePicture (dpy, rootBuffer);
1610                 rootBuffer = None;
1611             }
1612             root_width = ce->width;
1613             root_height = ce->height;
1614         }
1615         return;
1616     }
1617
1618     if (w->a.map_state == IsUnmapped) {
1619         /* save the configure event for when the window maps */
1620         w->need_configure = True;
1621         w->queue_configure = *ce;
1622     }
1623     else {
1624         w->need_configure = False;
1625
1626 #if CAN_DO_USABLE
1627         if (w->usable)
1628 #endif
1629         {
1630             damage = XFixesCreateRegion (dpy, 0, 0);
1631             if (w->extents != None)
1632                 XFixesCopyRegion (dpy, damage, w->extents);
1633         }
1634
1635         w->a.x = ce->x;
1636         w->a.y = ce->y;
1637         if (w->a.width != ce->width || w->a.height != ce->height)
1638         {
1639 #if HAS_NAME_WINDOW_PIXMAP
1640             if (w->pixmap)
1641             {
1642                 XFreePixmap (dpy, w->pixmap);
1643                 w->pixmap = None;
1644                 if (w->picture)
1645                 {
1646                     XRenderFreePicture (dpy, w->picture);
1647                     w->picture = None;
1648                 }
1649             }
1650 #endif
1651             if (w->shadow)
1652             {
1653                 XRenderFreePicture (dpy, w->shadow);
1654                 w->shadow = None;
1655             }
1656         }
1657         w->a.width = ce->width;
1658         w->a.height = ce->height;
1659         w->a.border_width = ce->border_width;
1660         if (w->a.map_state != IsUnmapped && damage)
1661         {
1662             XserverRegion       extents = win_extents (dpy, w);
1663             XFixesUnionRegion (dpy, damage, damage, extents);
1664             XFixesDestroyRegion (dpy, extents);
1665             add_damage (dpy, damage);
1666         }
1667     }
1668     w->a.override_redirect = ce->override_redirect;
1669     restack_win (dpy, w, ce->above);
1670     clipChanged = True;
1671 }
1672
1673 static void
1674 circulate_win (Display *dpy, XCirculateEvent *ce)
1675 {
1676     win     *w = find_win (dpy, ce->window);
1677     Window  new_above;
1678
1679     if (!w)
1680         return;
1681
1682     if (ce->place == PlaceOnTop)
1683         new_above = list->id;
1684     else
1685         new_above = None;
1686     restack_win (dpy, w, new_above);
1687     clipChanged = True;
1688 }
1689
1690 static void
1691 finish_destroy_win (Display *dpy, Window id)
1692 {
1693     win **prev, *w;
1694
1695     for (prev = &list; (w = *prev); prev = &w->next)
1696         if (w->id == id)
1697         {
1698             finish_unmap_win (dpy, w);
1699             *prev = w->next;
1700             if (w->alphaPict)
1701             {
1702                 XRenderFreePicture (dpy, w->alphaPict);
1703                 w->alphaPict = None;
1704             }
1705             if (w->shadowPict)
1706             {
1707                 XRenderFreePicture (dpy, w->shadowPict);
1708                 w->shadowPict = None;
1709             }
1710             if (w->damage != None)
1711             {
1712                 set_ignore (dpy, NextRequest (dpy));
1713                 XDamageDestroy (dpy, w->damage);
1714                 w->damage = None;
1715             }
1716
1717             cleanup_fade (dpy, w);
1718             free (w);
1719             break;
1720         }
1721 }
1722
1723 #if HAS_NAME_WINDOW_PIXMAP
1724 static void
1725 destroy_callback (Display *dpy, win *w)
1726 {
1727     finish_destroy_win (dpy, w->id);
1728 }
1729 #endif
1730
1731 static void
1732 destroy_win (Display *dpy, Window id, Bool fade)
1733 {
1734     win *w = find_win (dpy, id);
1735
1736 #if HAS_NAME_WINDOW_PIXMAP
1737     if (w && w->pixmap && fade && winTypeFade[w->windowType])
1738         set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step,
1739                   destroy_callback, False, (w->a.map_state != IsUnmapped));
1740     else
1741 #endif
1742     {
1743         finish_destroy_win (dpy, id);
1744     }
1745 }
1746
1747 /*
1748 static void
1749 dump_win (win *w)
1750 {
1751     printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1752             w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1753 }
1754
1755
1756 static void
1757 dump_wins (void)
1758 {
1759     win *w;
1760
1761     printf ("windows:\n");
1762     for (w = list; w; w = w->next)
1763         dump_win (w);
1764 }
1765 */
1766
1767 static void
1768 damage_win (Display *dpy, XDamageNotifyEvent *de)
1769 {
1770     win *w = find_win (dpy, de->drawable);
1771
1772     if (!w)
1773         return;
1774 #if CAN_DO_USABLE
1775     if (!w->usable)
1776     {
1777         if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1778         {
1779             w->damage_bounds = de->area;
1780         }
1781         else
1782         {
1783             if (de->area.x < w->damage_bounds.x)
1784             {
1785                 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1786                 w->damage_bounds.x = de->area.x;
1787             }
1788             if (de->area.y < w->damage_bounds.y)
1789             {
1790                 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1791                 w->damage_bounds.y = de->area.y;
1792             }
1793             if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1794                 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1795             if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1796                 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1797         }
1798 #if 0
1799         printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1800                 de->area.x,
1801                 de->area.y,
1802                 de->area.width,
1803                 de->area.height,
1804                 w->damage_bounds.x,
1805                 w->damage_bounds.y,
1806                 w->damage_bounds.width,
1807                 w->damage_bounds.height);
1808 #endif
1809         if (w->damage_bounds.x <= 0 && 
1810             w->damage_bounds.y <= 0 &&
1811             w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1812             w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1813         {
1814             clipChanged = True;
1815             if (winTypeFade[w->windowType])
1816                 set_fade (dpy, w, 0, get_opacity_percent (dpy, w), fade_in_step, 0, True, True);
1817             w->usable = True;
1818         }
1819     }
1820     if (w->usable)
1821 #endif
1822         repair_win (dpy, w);
1823 }
1824
1825 static int
1826 error (Display *dpy, XErrorEvent *ev)
1827 {
1828     int     o;
1829     const char    *name = 0;
1830     
1831     if (should_ignore (dpy, ev->serial))
1832         return 0;
1833     
1834     if (ev->request_code == composite_opcode &&
1835         ev->minor_code == X_CompositeRedirectSubwindows)
1836     {
1837         fprintf (stderr, "Another composite manager is already running\n");
1838         exit (1);
1839     }
1840     
1841     o = ev->error_code - xfixes_error;
1842     switch (o) {
1843     case BadRegion: name = "BadRegion"; break;
1844     default: break;
1845     }
1846     o = ev->error_code - damage_error;
1847     switch (o) {
1848     case BadDamage: name = "BadDamage"; break;
1849     default: break;
1850     }
1851     o = ev->error_code - render_error;
1852     switch (o) {
1853     case BadPictFormat: name ="BadPictFormat"; break;
1854     case BadPicture: name ="BadPicture"; break;
1855     case BadPictOp: name ="BadPictOp"; break;
1856     case BadGlyphSet: name ="BadGlyphSet"; break;
1857     case BadGlyph: name ="BadGlyph"; break;
1858     default: break;
1859     }
1860         
1861     printf ("error %d request %d minor %d serial %lu\n",
1862             ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1863
1864 /*    abort ();     this is just annoying to most people */
1865     return 0;
1866 }
1867
1868 static void
1869 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1870 {
1871     XserverRegion  region = XFixesCreateRegion (dpy, rects, nrects);
1872     
1873     add_damage (dpy, region);
1874 }
1875
1876 #if DEBUG_EVENTS
1877 static int
1878 ev_serial (XEvent *ev)
1879 {
1880     if (ev->type & 0x7f != KeymapNotify)
1881         return ev->xany.serial;
1882     return NextRequest (ev->xany.display);
1883 }
1884
1885 static char *
1886 ev_name (XEvent *ev)
1887 {
1888     static char buf[128];
1889     switch (ev->type & 0x7f) {
1890     case Expose:
1891         return "Expose";
1892     case MapNotify:
1893         return "Map";
1894     case UnmapNotify:
1895         return "Unmap";
1896     case ReparentNotify:
1897         return "Reparent";
1898     case CirculateNotify:
1899         return "Circulate";
1900     default:
1901         if (ev->type == damage_event + XDamageNotify)
1902             return "Damage";
1903         sprintf (buf, "Event %d", ev->type);
1904         return buf;
1905     }
1906 }
1907
1908 static Window
1909 ev_window (XEvent *ev)
1910 {
1911     switch (ev->type) {
1912     case Expose:
1913         return ev->xexpose.window;
1914     case MapNotify:
1915         return ev->xmap.window;
1916     case UnmapNotify:
1917         return ev->xunmap.window;
1918     case ReparentNotify:
1919         return ev->xreparent.window;
1920     case CirculateNotify:
1921         return ev->xcirculate.window;
1922     default:
1923         if (ev->type == damage_event + XDamageNotify)
1924             return ((XDamageNotifyEvent *) ev)->drawable;
1925         return 0;
1926     }
1927 }
1928 #endif
1929
1930 void
1931 usage (char *program)
1932 {
1933     fprintf (stderr, "%s v1.1.3\n", program);
1934     fprintf (stderr, "usage: %s [options]\n", program);
1935     fprintf (stderr, "Options\n");
1936     fprintf (stderr, "   -d display\n      Specifies which display should be managed.\n");
1937     fprintf (stderr, "   -r radius\n      Specifies the blur radius for client-side shadows. (default 12)\n");
1938     fprintf (stderr, "   -o opacity\n      Specifies the translucency for client-side shadows. (default .75)\n");
1939     fprintf (stderr, "   -l left-offset\n      Specifies the left offset for client-side shadows. (default -15)\n");
1940     fprintf (stderr, "   -t top-offset\n      Specifies the top offset for clinet-side shadows. (default -15)\n");
1941     fprintf (stderr, "   -I fade-in-step\n      Specifies the opacity change between steps while fading in. (default 0.028)\n");
1942     fprintf (stderr, "   -O fade-out-step\n      Specifies the opacity change between steps while fading out. (default 0.03)\n");
1943     fprintf (stderr, "   -D fade-delta-time\n      Specifies the time between steps in a fade in milliseconds. (default 10)\n");
1944     fprintf (stderr, "   -m opacity\n      Specifies the opacity for menus. (default 1.0)\n");
1945     fprintf (stderr, "   -a\n      Use automatic server-side compositing. Faster, but no special effects.\n");
1946     fprintf (stderr, "   -c\n      Draw client-side shadows with fuzzy edges.\n");
1947     fprintf (stderr, "   -C\n      Avoid drawing shadows on dock/panel windows.\n");
1948     fprintf (stderr, "   -f\n      Fade windows in/out when opening/closing.\n");
1949     fprintf (stderr, "   -F\n      Fade windows during opacity changes.\n");
1950     fprintf (stderr, "   -n\n      Normal client-side compositing with transparency support\n");
1951     fprintf (stderr, "   -s\n      Draw server-side shadows with sharp edges.\n");
1952     fprintf (stderr, "   -S\n      Enable synchronous operation (for debugging).\n");
1953     exit (1);
1954 }
1955
1956 static void
1957 register_cm (int scr)
1958 {
1959     Window w;
1960     Atom a;
1961     char *buf;
1962     int len, s;
1963
1964     if (scr < 0) return;
1965
1966     w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
1967                              None);
1968
1969     Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
1970                           NULL);
1971
1972     len = strlen(REGISTER_PROP) + 2;
1973     s = scr;
1974     while (s >= 10) {
1975         ++len;
1976         s /= 10;
1977     }
1978     buf = malloc(len);
1979     snprintf(buf, len, REGISTER_PROP"%d", scr);
1980
1981     a = XInternAtom (dpy, buf, False);
1982     free(buf);
1983
1984     XSetSelectionOwner (dpy, a, w, 0);
1985 }
1986
1987 int
1988 main (int argc, char **argv)
1989 {
1990     XEvent          ev;
1991     Window          root_return, parent_return;
1992     Window          *children;
1993     unsigned int    nchildren;
1994     int             i;
1995     XRenderPictureAttributes    pa;
1996     XRectangle      *expose_rects = 0;
1997     int             size_expose = 0;
1998     int             n_expose = 0;
1999     struct pollfd   ufd;
2000     int             p;
2001     int             composite_major, composite_minor;
2002     char            *display = 0;
2003     int             o;
2004     Bool            noDockShadow = False;
2005
2006     for (i = 0; i < NUM_WINTYPES; ++i) {
2007         winTypeFade[i] = False;
2008         winTypeShadow[i] = False;
2009         winTypeOpacity[i] = 1.0;
2010     }
2011
2012     /* don't bother to draw a shadow for the desktop */
2013     winTypeShadow[WINTYPE_DESKTOP] = False;
2014
2015     while ((o = getopt (argc, argv, "D:I:O:d:r:o:m:l:t:scnfFCaS")) != -1)
2016     {
2017         switch (o) {
2018         case 'd':
2019             display = optarg;
2020             break;
2021         case 'D':
2022             fade_delta = atoi (optarg);
2023             if (fade_delta < 1)
2024                 fade_delta = 10;
2025             break;
2026         case 'I':
2027             fade_in_step = atof (optarg);
2028             if (fade_in_step <= 0)
2029                 fade_in_step = 0.01;
2030             break;
2031         case 'O':
2032             fade_out_step = atof (optarg);
2033             if (fade_out_step <= 0)
2034                 fade_out_step = 0.01;
2035             break;
2036         case 's':
2037             compMode = CompServerShadows;
2038             for (i = 0; i < NUM_WINTYPES; ++i)
2039                 winTypeShadow[i] = True;
2040             break;
2041         case 'c':
2042             compMode = CompClientShadows;
2043             for (i = 0; i < NUM_WINTYPES; ++i)
2044                 winTypeShadow[i] = True;
2045             break;
2046         case 'C':
2047             noDockShadow = True;
2048             break;
2049         case 'n':
2050             compMode = CompSimple;
2051             for (i = 0; i < NUM_WINTYPES; ++i)
2052                 winTypeShadow[i] = False;
2053             break;
2054         case 'm':
2055             winTypeOpacity[WINTYPE_DROPDOWN_MENU] = atof (optarg);
2056             winTypeOpacity[WINTYPE_POPUP_MENU] = atof (optarg);
2057             break;
2058         case 'f':
2059             for (i = 0; i < NUM_WINTYPES; ++i)
2060                 winTypeFade[i] = True;
2061             break;
2062         case 'F':
2063             fadeTrans = True;
2064             break;
2065         case 'a':
2066             autoRedirect = True;
2067             break;
2068         case 'S':
2069             synchronize = True;
2070             break;
2071         case 'r':
2072             shadowRadius = atoi (optarg);
2073             break;
2074         case 'o':
2075             shadowOpacity = atof (optarg);
2076             break;
2077         case 'l':
2078             shadowOffsetX = atoi (optarg);
2079             break;
2080         case 't':
2081             shadowOffsetY = atoi (optarg);
2082             break;
2083         default:
2084             usage (argv[0]);
2085             break;
2086         }
2087     }
2088
2089     if (noDockShadow)
2090         winTypeShadow[WINTYPE_DOCK] = False;
2091     
2092     dpy = XOpenDisplay (display);
2093     if (!dpy)
2094     {
2095         fprintf (stderr, "Can't open display\n");
2096         exit (1);
2097     }
2098     XSetErrorHandler (error);
2099     if (synchronize)
2100         XSynchronize (dpy, 1);
2101     scr = DefaultScreen (dpy);
2102     root = RootWindow (dpy, scr);
2103
2104     if (!XRenderQueryExtension (dpy, &render_event, &render_error))
2105     {
2106         fprintf (stderr, "No render extension\n");
2107         exit (1);
2108     }
2109     if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
2110                           &composite_event, &composite_error))
2111     {
2112         fprintf (stderr, "No composite extension\n");
2113         exit (1);
2114     }
2115     XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
2116 #if HAS_NAME_WINDOW_PIXMAP
2117     if (composite_major > 0 || composite_minor >= 2)
2118         hasNamePixmap = True;
2119 #endif
2120
2121     if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
2122     {
2123         fprintf (stderr, "No damage extension\n");
2124         exit (1);
2125     }
2126     if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
2127     {
2128         fprintf (stderr, "No XFixes extension\n");
2129         exit (1);
2130     }
2131
2132     register_cm(scr);
2133
2134     /* get atoms */
2135     opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
2136     winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
2137     winType[WINTYPE_DESKTOP] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
2138     winType[WINTYPE_DOCK] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
2139     winType[WINTYPE_TOOLBAR] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
2140     winType[WINTYPE_MENU] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
2141     winType[WINTYPE_UTILITY] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
2142     winType[WINTYPE_SPLASH] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
2143     winType[WINTYPE_DIALOG] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2144     winType[WINTYPE_NORMAL] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
2145     winType[WINTYPE_DROPDOWN_MENU] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", False);
2146     winType[WINTYPE_POPUP_MENU] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_POPUP_MENU", False);
2147     winType[WINTYPE_TOOLTIP] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
2148     winType[WINTYPE_NOTIFY] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NOTIFICATION", False);
2149     winType[WINTYPE_COMBO] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_COMBO", False);
2150     winType[WINTYPE_DND] = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DND", False);
2151
2152     pa.subwindow_mode = IncludeInferiors;
2153
2154     if (compMode == CompClientShadows)
2155     {
2156         gaussianMap = make_gaussian_map(dpy, shadowRadius);
2157         presum_gaussian (gaussianMap);
2158     }
2159
2160     root_width = DisplayWidth (dpy, scr);
2161     root_height = DisplayHeight (dpy, scr);
2162
2163     rootPicture = XRenderCreatePicture (dpy, root, 
2164                                         XRenderFindVisualFormat (dpy,
2165                                                                  DefaultVisual (dpy, scr)),
2166                                         CPSubwindowMode,
2167                                         &pa);
2168     blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
2169     if (compMode == CompServerShadows)
2170         transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
2171     allDamage = None;
2172     clipChanged = True;
2173     XGrabServer (dpy);
2174     if (autoRedirect)
2175         XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
2176     else
2177     {
2178         XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
2179         XSelectInput (dpy, root, 
2180                       SubstructureNotifyMask|
2181                       ExposureMask|
2182                       StructureNotifyMask|
2183                       PropertyChangeMask);
2184         XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
2185         for (i = 0; i < nchildren; i++)
2186             add_win (dpy, children[i], i ? children[i-1] : None);
2187         XFree (children);
2188     }
2189     XUngrabServer (dpy);
2190     ufd.fd = ConnectionNumber (dpy);
2191     ufd.events = POLLIN;
2192     if (!autoRedirect)
2193         paint_all (dpy, None);
2194     for (;;)
2195     {
2196         /*      dump_wins (); */
2197         do {
2198             if (autoRedirect)
2199                 XFlush (dpy);
2200             if (!QLength (dpy))
2201             {
2202                  if (poll (&ufd, 1, fade_timeout()) == 0)
2203                  {
2204                     run_fades (dpy);
2205                     break;
2206                  }
2207             }
2208
2209             XNextEvent (dpy, &ev);
2210             if ((ev.type & 0x7f) != KeymapNotify)
2211                 discard_ignore (dpy, ev.xany.serial);
2212 #if DEBUG_EVENTS
2213             printf ("event %10.10s serial 0x%08x window 0x%08x\n",
2214                     ev_name(&ev), ev_serial (&ev), ev_window (&ev));
2215 #endif
2216             if (!autoRedirect) switch (ev.type) {
2217             case CreateNotify:
2218                 add_win (dpy, ev.xcreatewindow.window, 0);
2219                 break;
2220             case ConfigureNotify:
2221                 configure_win (dpy, &ev.xconfigure);
2222                 break;
2223             case DestroyNotify:
2224                 destroy_win (dpy, ev.xdestroywindow.window, True);
2225                 break;
2226             case MapNotify:
2227                 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
2228                 break;
2229             case UnmapNotify:
2230                 unmap_win (dpy, ev.xunmap.window, True);
2231                 break;
2232             case ReparentNotify:
2233                 if (ev.xreparent.parent == root)
2234                     add_win (dpy, ev.xreparent.window, 0);
2235                 else
2236                     destroy_win (dpy, ev.xreparent.window, True);
2237                 break;
2238             case CirculateNotify:
2239                 circulate_win (dpy, &ev.xcirculate);
2240                 break;
2241             case Expose:
2242                 if (ev.xexpose.window == root)
2243                 {
2244                     int more = ev.xexpose.count + 1;
2245                     if (n_expose == size_expose)
2246                     {
2247                         if (expose_rects)
2248                         {
2249                             expose_rects = realloc (expose_rects, 
2250                                                     (size_expose + more) * 
2251                                                     sizeof (XRectangle));
2252                             size_expose += more;
2253                         }
2254                         else
2255                         {
2256                             expose_rects = malloc (more * sizeof (XRectangle));
2257                             size_expose = more;
2258                         }
2259                     }
2260                     expose_rects[n_expose].x = ev.xexpose.x;
2261                     expose_rects[n_expose].y = ev.xexpose.y;
2262                     expose_rects[n_expose].width = ev.xexpose.width;
2263                     expose_rects[n_expose].height = ev.xexpose.height;
2264                     n_expose++;
2265                     if (ev.xexpose.count == 0)
2266                     {
2267                         expose_root (dpy, root, expose_rects, n_expose);
2268                         n_expose = 0;
2269                     }
2270                 }
2271                 break;
2272             case PropertyNotify:
2273                 for (p = 0; backgroundProps[p]; p++)
2274                 {
2275                     if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
2276                     {
2277                         if (rootTile)
2278                         {
2279                             XClearArea (dpy, root, 0, 0, 0, 0, True);
2280                             XRenderFreePicture (dpy, rootTile);
2281                             rootTile = None;
2282                             break;
2283                         }
2284                     }
2285                 }
2286                 /* check if Trans property was changed */
2287                 if (ev.xproperty.atom == opacityAtom)
2288                 {
2289                     /* reset mode and redraw window */
2290                     win * w = find_win(dpy, ev.xproperty.window);
2291                     if (w)
2292                     {
2293                         if (fadeTrans)
2294                             set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w),
2295                                       fade_out_step, 0, True, False);
2296                         else
2297                         {
2298                             w->opacity = get_opacity_prop(dpy, w, OPAQUE);
2299                             determine_mode(dpy, w);
2300                             if (w->shadow)
2301                             {
2302                                 XRenderFreePicture (dpy, w->shadow);
2303                                 w->shadow = None;
2304
2305                                 if (w->extents != None)
2306                                     XFixesDestroyRegion (dpy, w->extents);
2307
2308                                 /* rebuild the shadow */
2309                                 w->extents = win_extents (dpy, w);
2310                             }
2311                         }
2312                     }
2313                 }
2314                 break;
2315             default:
2316                 if (ev.type == damage_event + XDamageNotify)
2317                     damage_win (dpy, (XDamageNotifyEvent *) &ev);
2318                 break;
2319             }
2320         } while (QLength (dpy));
2321         if (allDamage && !autoRedirect)
2322         {
2323             static int  paint;
2324             paint_all (dpy, allDamage);
2325             paint++;
2326             XSync (dpy, False);
2327             allDamage = None;
2328             clipChanged = False;
2329         }
2330     }
2331 }