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