Add name window pixmap support
[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 <X11/Xlib.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include <X11/extensions/Xcomposite.h>
43 #include <X11/extensions/Xdamage.h>
44 #include <X11/extensions/Xrender.h>
45
46 #if COMPOSITE_MAJOR > 0 || COMPOSITE_MINOR >= 2
47 #define HAS_NAME_WINDOW_PIXMAP 1
48 #endif
49
50 #define CAN_DO_USABLE 0
51
52 typedef struct _ignore {
53     struct _ignore      *next;
54     unsigned long       sequence;
55 } ignore;
56
57 typedef struct _win {
58     struct _win         *next;
59     Window              id;
60 #if HAS_NAME_WINDOW_PIXMAP
61     Pixmap              pixmap;
62 #endif
63     XWindowAttributes   a;
64 #if CAN_DO_USABLE
65     Bool                usable;             /* mapped and all damaged at one point */
66     XRectangle          damage_bounds;      /* bounds of damage */
67 #endif
68     int                 mode;
69     int                 damaged;
70     Damage              damage;
71     Picture             picture;
72     Picture             alphaPict;
73     Picture             shadowPict;
74     XserverRegion       borderSize;
75     XserverRegion       extents;
76     Picture             shadow;
77     int                 shadow_dx;
78     int                 shadow_dy;
79     int                 shadow_width;
80     int                 shadow_height;
81     unsigned int        opacity;
82
83     unsigned long       damage_sequence;    /* sequence when damage was created */
84
85     /* for drawing translucent windows */
86     XserverRegion       borderClip;
87     struct _win         *prev_trans;
88 } win;
89
90 typedef struct _conv {
91     int     size;
92     double  *data;
93 } conv;
94
95 typedef struct _fade {
96     struct _fade        *next;
97     win                 *w;
98     double              cur;
99     double              step;
100     void                (*callback) (Display *dpy, win *w, Bool gone);
101     Display             *dpy;
102     Bool                gone;
103 } fade;
104
105 win             *list;
106 fade            *fades;
107 Display         *dpy;
108 int             scr;
109 Window          root;
110 Picture         rootPicture;
111 Picture         rootBuffer;
112 Picture         blackPicture;
113 Picture         transBlackPicture;
114 Picture         rootTile;
115 XserverRegion   allDamage;
116 Bool            clipChanged;
117 #if HAS_NAME_WINDOW_PIXMAP
118 Bool            hasNamePixmap;
119 #endif
120 int             root_height, root_width;
121 ignore          *ignore_head, **ignore_tail = &ignore_head;
122 int             xfixes_event, xfixes_error;
123 int             damage_event, damage_error;
124 int             composite_event, composite_error;
125 int             render_event, render_error;
126 Bool            synchronize;
127 int             composite_opcode;
128
129 /* find these once and be done with it */
130 Atom            opacityAtom;
131
132 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
133 #define OPACITY_PROP    "_NET_WM_WINDOW_OPACITY"
134
135 #define TRANSLUCENT     0xe0000000
136 #define OPAQUE          0xffffffff
137
138 conv            *gaussianMap;
139
140 #define WINDOW_SOLID    0
141 #define WINDOW_TRANS    1
142 #define WINDOW_ARGB     2
143
144 #define TRANS_OPACITY   0.75
145
146 #define DEBUG_REPAINT 0
147 #define DEBUG_EVENTS 0
148 #define MONITOR_REPAINT 0
149
150 #define SHADOWS         1
151 #define SHARP_SHADOW    0
152
153 typedef enum _compMode {
154     CompSimple,         /* looks like a regular X server */
155     CompServerShadows,  /* use window alpha for shadow; sharp, but precise */
156     CompClientShadows,  /* use window extents for shadow, blurred */
157 } CompMode;
158
159 static void
160 determine_mode(Display *dpy, win *w);
161     
162 CompMode    compMode = CompSimple;
163
164 int         shadowRadius = 12;
165
166 double  fade_step =     0.05;
167 int     fade_delta =    10;
168 int     fade_time =     0;
169 Bool    fadeWindows;
170
171 int
172 get_time_in_milliseconds ()
173 {
174     struct timeval  tv;
175
176     gettimeofday (&tv, NULL);
177     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
178 }
179
180 fade *
181 find_fade (win *w)
182 {
183     fade    *f;
184     
185     for (f = fades; f; f = f->next)
186     {
187         if (f->w == w)
188             return f;
189     }
190     return 0;
191 }
192
193 void
194 dequeue_fade (Display *dpy, fade *f)
195 {
196     fade    **prev;
197
198     for (prev = &fades; *prev; prev = &(*prev)->next)
199         if (*prev == f)
200         {
201             *prev = f->next;
202             if (f->callback)
203                 (*f->callback) (dpy, f->w, f->gone);
204             free (f);
205             break;
206         }
207 }
208
209 void
210 cleanup_fade (Display *dpy, win *w)
211 {
212     fade *f = find_fade (w);
213     if (f)
214         dequeue_fade (dpy, f);
215 }
216
217 void
218 enqueue_fade (Display *dpy, fade *f)
219 {
220     if (!fades)
221         fade_time = get_time_in_milliseconds () + fade_delta;
222     f->next = fades;
223     fades = f;
224 }
225
226 static void
227 set_fade (Display *dpy, win *w, Bool in, 
228           void (*callback) (Display *dpy, win *w, Bool gone),
229           Bool gone)
230 {
231     fade    *f;
232
233     f = find_fade (w);
234     if (!f)
235     {
236         f = malloc (sizeof (fade));
237         f->next = 0;
238         f->w = w;
239         if (in)
240             f->cur = 0;
241         else
242             f->cur = 1;
243         enqueue_fade (dpy, f);
244     }
245     if (in)
246         f->step = fade_step;
247     else
248         f->step = -fade_step;
249     f->callback = callback;
250     f->gone = gone;
251     w->opacity = f->cur * OPAQUE;
252 #if 0
253     printf ("set_fade start %g step %g\n", f->cur, f->step);
254 #endif
255     determine_mode (dpy, w);
256 }
257
258 int
259 fade_timeout (void)
260 {
261     int now;
262     int delta;
263     if (!fades)
264         return -1;
265     now = get_time_in_milliseconds();
266     delta = fade_time - now;
267     if (delta < 0)
268         delta = 0;
269 /*    printf ("timeout %d\n", delta); */
270     return delta;
271 }
272
273 void
274 run_fades (Display *dpy)
275 {
276     int     now = get_time_in_milliseconds();
277     fade    *f, *next;
278     int     steps;
279
280 #if 0
281     printf ("run fades\n");
282 #endif
283     if (fade_time - now > 0)
284         return;
285     steps = 1 + (now - fade_time) / fade_delta;
286     for (next = fades; f = next; )
287     {
288         win *w = f->w;
289         next = f->next;
290         f->cur += f->step * steps;
291         if (f->cur >= 1)
292             f->cur = 1;
293         else if (f->cur < 0)
294             f->cur = 0;
295 #if 0
296         printf ("opacity now %g\n", f->cur);
297 #endif
298         w->opacity = f->cur * OPAQUE;
299         if (f->step > 0)
300         {
301             if (f->cur >= 1)
302                 dequeue_fade (dpy, f);
303         }
304         else
305         {
306             if (f->cur <= 0)
307                 dequeue_fade (dpy, f);
308         }
309         determine_mode (dpy, w);
310     }
311     fade_time = now + fade_delta;
312 }
313
314 #define SHADOW_OPACITY  0.75
315 #define SHADOW_OFFSET_X (-shadowRadius * 5 / 4)
316 #define SHADOW_OFFSET_Y (-shadowRadius * 5 / 4)
317
318 static double
319 gaussian (double r, double x, double y)
320 {
321     return ((1 / (sqrt (2 * M_PI * r))) *
322             exp ((- (x * x + y * y)) / (2 * r * r)));
323 }
324
325
326 static conv *
327 make_gaussian_map (Display *dpy, double r)
328 {
329     conv            *c;
330     int             size = ((int) ceil ((r * 3)) + 1) & ~1;
331     int             center = size / 2;
332     int             x, y;
333     double          t;
334     double          g;
335     
336     c = malloc (sizeof (conv) + size * size * sizeof (double));
337     c->size = size;
338     c->data = (double *) (c + 1);
339     t = 0.0;
340     for (y = 0; y < size; y++)
341         for (x = 0; x < size; x++)
342         {
343             g = gaussian (r, (double) (x - center), (double) (y - center));
344             t += g;
345             c->data[y * size + x] = g;
346         }
347 /*    printf ("gaussian total %f\n", t); */
348     for (y = 0; y < size; y++)
349         for (x = 0; x < size; x++)
350         {
351             c->data[y*size + x] /= t;
352         }
353     return c;
354 }
355
356 /*
357  * A picture will help
358  *
359  *      -center   0                width  width+center
360  *  -center +-----+-------------------+-----+
361  *          |     |                   |     |
362  *          |     |                   |     |
363  *        0 +-----+-------------------+-----+
364  *          |     |                   |     |
365  *          |     |                   |     |
366  *          |     |                   |     |
367  *   height +-----+-------------------+-----+
368  *          |     |                   |     |
369  * height+  |     |                   |     |
370  *  center  +-----+-------------------+-----+
371  */
372  
373 static unsigned char
374 sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
375 {
376     int     fx, fy;
377     double  *g_data;
378     double  *g_line = map->data;
379     int     g_size = map->size;
380     int     center = g_size / 2;
381     int     fx_start, fx_end;
382     int     fy_start, fy_end;
383     double  v;
384     
385     /*
386      * Compute set of filter values which are "in range",
387      * that's the set with:
388      *  0 <= x + (fx-center) && x + (fx-center) < width &&
389      *  0 <= y + (fy-center) && y + (fy-center) < height
390      *
391      *  0 <= x + (fx - center)  x + fx - center < width
392      *  center - x <= fx        fx < width + center - x
393      */
394
395     fx_start = center - x;
396     if (fx_start < 0)
397         fx_start = 0;
398     fx_end = width + center - x;
399     if (fx_end > g_size)
400         fx_end = g_size;
401
402     fy_start = center - y;
403     if (fy_start < 0)
404         fy_start = 0;
405     fy_end = height + center - y;
406     if (fy_end > g_size)
407         fy_end = g_size;
408
409     g_line = g_line + fy_start * g_size + fx_start;
410     
411     v = 0;
412     for (fy = fy_start; fy < fy_end; fy++)
413     {
414         g_data = g_line;
415         g_line += g_size;
416         
417         for (fx = fx_start; fx < fx_end; fx++)
418             v += *g_data++;
419     }
420     if (v > 1)
421         v = 1;
422     
423     return ((unsigned char) (v * opacity * 255.0));
424 }
425
426 static XImage *
427 make_shadow (Display *dpy, double opacity, int width, int height)
428 {
429     XImage          *ximage;
430     unsigned char   *data;
431     int             gsize = gaussianMap->size;
432     int             ylimit, xlimit;
433     int             swidth = width + gsize;
434     int             sheight = height + gsize;
435     int             center = gsize / 2;
436     int             x, y;
437     unsigned char   d;
438     int             x_diff;
439     
440     data = malloc (swidth * sheight * sizeof (unsigned char));
441     if (!data)
442         return 0;
443     ximage = XCreateImage (dpy,
444                            DefaultVisual(dpy, DefaultScreen(dpy)),
445                            8,
446                            ZPixmap,
447                            0,
448                            (char *) data,
449                            swidth, sheight, 8, swidth * sizeof (unsigned char));
450     if (!ximage)
451     {
452         free (data);
453         return 0;
454     }
455     /*
456      * Build the gaussian in sections
457      */
458
459     /*
460      * center (fill the complete data array)
461      */
462
463     d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
464     memset(data, d, sheight * swidth);
465     
466     /*
467      * corners
468      */
469     ylimit = gsize;
470     if (ylimit > sheight / 2)
471         ylimit = (sheight + 1) / 2;
472     xlimit = gsize;
473     if (xlimit > swidth / 2)
474         xlimit = (swidth + 1) / 2;
475
476     for (y = 0; y < ylimit; y++)
477         for (x = 0; x < xlimit; x++)
478         {
479             d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
480             data[y * swidth + x] = d;
481             data[(sheight - y - 1) * swidth + x] = d;
482             data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
483             data[y * swidth + (swidth - x - 1)] = d;
484         }
485
486     /*
487      * top/bottom
488      */
489     x_diff = swidth - (gsize * 2);
490     if (x_diff > 0 && ylimit > 0)
491     {
492         for (y = 0; y < ylimit; y++)
493         {
494             d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
495             memset (&data[y * swidth + gsize], d, x_diff);
496             memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
497         }
498     }
499
500     /*
501      * sides
502      */
503     
504     for (x = 0; x < xlimit; x++)
505     {
506         d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
507         for (y = gsize; y < sheight - gsize; y++)
508         {
509             data[y * swidth + x] = d;
510             data[y * swidth + (swidth - x - 1)] = d;
511         }
512     }
513
514     return ximage;
515 }
516
517 static Picture
518 shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
519 {
520     XImage  *shadowImage;
521     Pixmap  shadowPixmap;
522     Picture shadowPicture;
523     GC      gc;
524     
525     shadowImage = make_shadow (dpy, opacity, width, height);
526     if (!shadowImage)
527         return None;
528     shadowPixmap = XCreatePixmap (dpy, root, 
529                                   shadowImage->width,
530                                   shadowImage->height,
531                                   8);
532     shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
533                                           XRenderFindStandardFormat (dpy, PictStandardA8),
534                                           0, 0);
535     gc = XCreateGC (dpy, shadowPixmap, 0, 0);
536     
537     XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0, 
538                shadowImage->width,
539                shadowImage->height);
540     *wp = shadowImage->width;
541     *hp = shadowImage->height;
542     XFreeGC (dpy, gc);
543     XDestroyImage (shadowImage);
544     XFreePixmap (dpy, shadowPixmap);
545     return shadowPicture;
546 }
547
548 Picture
549 solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
550 {
551     Pixmap                      pixmap;
552     Picture                     picture;
553     XRenderPictureAttributes    pa;
554     XRenderColor                c;
555
556     pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
557     pa.repeat = True;
558     picture = XRenderCreatePicture (dpy, pixmap,
559                                     XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
560                                     CPRepeat,
561                                     &pa);
562     c.alpha = a * 0xffff;
563     c.red = r * 0xffff;
564     c.green = g * 0xffff;
565     c.blue = b * 0xffff;
566     XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 0, 0, 1, 1);
567     XFreePixmap (dpy, pixmap);
568     return picture;
569 }
570
571 void
572 discard_ignore (Display *dpy, unsigned long sequence)
573 {
574     while (ignore_head)
575     {
576         if ((long) (sequence - ignore_head->sequence) > 0)
577         {
578             ignore  *next = ignore_head->next;
579             free (ignore_head);
580             ignore_head = next;
581             if (!ignore_head)
582                 ignore_tail = &ignore_head;
583         }
584         else
585             break;
586     }
587 }
588
589 void
590 set_ignore (Display *dpy, unsigned long sequence)
591 {
592     ignore  *i = malloc (sizeof (ignore));
593     if (!i)
594         return;
595     i->sequence = sequence;
596     i->next = 0;
597     *ignore_tail = i;
598     ignore_tail = &i->next;
599 }
600
601 int
602 should_ignore (Display *dpy, unsigned long sequence)
603 {
604     discard_ignore (dpy, sequence);
605     return ignore_head && ignore_head->sequence == sequence;
606 }
607
608 static win *
609 find_win (Display *dpy, Window id)
610 {
611     win *w;
612
613     for (w = list; w; w = w->next)
614         if (w->id == id)
615             return w;
616     return 0;
617 }
618
619 static char *backgroundProps[] = {
620     "_XROOTPMAP_ID",
621     "_XSETROOT_ID",
622     0,
623 };
624     
625 static Picture
626 root_tile (Display *dpy)
627 {
628     Picture         picture;
629     Atom            actual_type;
630     Pixmap          pixmap;
631     int             actual_format;
632     unsigned long   nitems;
633     unsigned long   bytes_after;
634     unsigned char   *prop;
635     Bool            fill;
636     XRenderPictureAttributes    pa;
637     int             p;
638
639     pixmap = None;
640     for (p = 0; backgroundProps[p]; p++)
641     {
642         if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
643                                 0, 4, False, AnyPropertyType,
644                                 &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
645             actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
646         {
647             memcpy (&pixmap, prop, 4);
648             XFree (prop);
649             fill = False;
650             break;
651         }
652     }
653     if (!pixmap)
654     {
655         pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
656         fill = True;
657     }
658     pa.repeat = True;
659     picture = XRenderCreatePicture (dpy, pixmap,
660                                     XRenderFindVisualFormat (dpy,
661                                                              DefaultVisual (dpy, scr)),
662                                     CPRepeat, &pa);
663     if (fill)
664     {
665         XRenderColor    c;
666         
667         c.red = c.green = c.blue = 0x8080;
668         c.alpha = 0xffff;
669         XRenderFillRectangle (dpy, PictOpSrc, picture, &c, 
670                               0, 0, 1, 1);
671     }
672     return picture;
673 }
674
675 static void
676 paint_root (Display *dpy)
677 {
678     if (!rootTile)
679         rootTile = root_tile (dpy);
680     
681     XRenderComposite (dpy, PictOpSrc,
682                       rootTile, None, rootBuffer,
683                       0, 0, 0, 0, 0, 0, root_width, root_height);
684 }
685
686 static XserverRegion
687 win_extents (Display *dpy, win *w)
688 {
689     XRectangle      r;
690     
691     r.x = w->a.x;
692     r.y = w->a.y;
693     r.width = w->a.width + w->a.border_width * 2;
694     r.height = w->a.height + w->a.border_width * 2;
695     if (compMode != CompSimple)
696     {
697         if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
698         {
699             XRectangle  sr;
700
701             if (compMode == CompServerShadows)
702             {
703                 w->shadow_dx = 2;
704                 w->shadow_dy = 7;
705                 w->shadow_width = w->a.width;
706                 w->shadow_height = w->a.height;
707             }
708             else
709             {
710                 w->shadow_dx = SHADOW_OFFSET_X;
711                 w->shadow_dy = SHADOW_OFFSET_Y;
712                 if (!w->shadow)
713                 {
714                     double      opacity = SHADOW_OPACITY;
715                     if (w->mode == WINDOW_TRANS)
716                         opacity = opacity * TRANS_OPACITY;
717                     w->shadow = shadow_picture (dpy, opacity,
718                                                 w->a.width + w->a.border_width * 2,
719                                                 w->a.height + w->a.border_width * 2,
720                                                 &w->shadow_width, &w->shadow_height);
721                 }
722             }
723             sr.x = w->a.x + w->shadow_dx;
724             sr.y = w->a.y + w->shadow_dy;
725             sr.width = w->shadow_width;
726             sr.height = w->shadow_height;
727             if (sr.x < r.x)
728             {
729                 r.width = (r.x + r.width) - sr.x;
730                 r.x = sr.x;
731             }
732             if (sr.y < r.y)
733             {
734                 r.height = (r.y + r.height) - sr.y;
735                 r.y = sr.y;
736             }
737             if (sr.x + sr.width > r.x + r.width)
738                 r.width = sr.x + sr.width - r.x;
739             if (sr.y + sr.height > r.y + r.height)
740                 r.height = sr.y + sr.height - r.y;
741         }
742     }
743     return XFixesCreateRegion (dpy, &r, 1);
744 }
745
746 static XserverRegion
747 border_size (Display *dpy, win *w)
748 {
749     XserverRegion   border;
750     /*
751      * if window doesn't exist anymore,  this will generate an error
752      * as well as not generate a region.  Perhaps a better XFixes
753      * architecture would be to have a request that copies instead
754      * of creates, that way you'd just end up with an empty region
755      * instead of an invalid XID.
756      */
757     set_ignore (dpy, NextRequest (dpy));
758     border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
759     /* translate this */
760     set_ignore (dpy, NextRequest (dpy));
761     XFixesTranslateRegion (dpy, border,
762                            w->a.x + w->a.border_width,
763                            w->a.y + w->a.border_width);
764     return border;
765 }
766
767 static void
768 paint_all (Display *dpy, XserverRegion region)
769 {
770     win *w;
771     win *t = 0;
772     
773     if (!region)
774     {
775         XRectangle  r;
776         r.x = 0;
777         r.y = 0;
778         r.width = root_width;
779         r.height = root_height;
780         region = XFixesCreateRegion (dpy, &r, 1);
781     }
782 #if MONITOR_REPAINT
783     rootBuffer = rootPicture;
784 #else
785     if (!rootBuffer)
786     {
787         Pixmap  rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
788                                             DefaultDepth (dpy, scr));
789         rootBuffer = XRenderCreatePicture (dpy, rootPixmap,
790                                            XRenderFindVisualFormat (dpy,
791                                                                     DefaultVisual (dpy, scr)),
792                                            0, 0);
793         XFreePixmap (dpy, rootPixmap);
794     }
795 #endif
796     XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
797 #if MONITOR_REPAINT
798     XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
799                       0, 0, 0, 0, 0, 0, root_width, root_height);
800 #endif
801 #if DEBUG_REPAINT
802     printf ("paint:");
803 #endif
804     for (w = list; w; w = w->next)
805     {
806 #if CAN_DO_USABLE
807         if (!w->usable)
808             continue;
809 #endif
810         /* never painted, ignore it */
811         if (!w->damaged)
812             continue;
813         if (!w->picture)
814         {
815             XRenderPictureAttributes    pa;
816             XRenderPictFormat           *format;
817             Drawable                    draw = w->id;
818             
819 #if HAS_NAME_WINDOW_PIXMAP
820             if (hasNamePixmap && !w->pixmap)
821                 w->pixmap = XCompositeNameWindowPixmap (dpy, w->id);
822             if (w->pixmap)
823                 draw = w->pixmap;
824 #endif
825             format = XRenderFindVisualFormat (dpy, w->a.visual);
826             pa.subwindow_mode = IncludeInferiors;
827             w->picture = XRenderCreatePicture (dpy, draw,
828                                                format,
829                                                CPSubwindowMode,
830                                                &pa);
831         }
832 #if DEBUG_REPAINT
833         printf (" 0x%x", w->id);
834 #endif
835         if (clipChanged)
836         {
837             if (w->borderSize)
838             {
839                 set_ignore (dpy, NextRequest (dpy));
840                 XFixesDestroyRegion (dpy, w->borderSize);
841                 w->borderSize = None;
842             }
843             if (w->extents)
844             {
845                 XFixesDestroyRegion (dpy, w->extents);
846                 w->extents = None;
847             }
848             if (w->borderClip)
849             {
850                 XFixesDestroyRegion (dpy, w->borderClip);
851                 w->borderClip = None;
852             }
853         }
854         if (!w->borderSize)
855             w->borderSize = border_size (dpy, w);
856         if (!w->extents)
857             w->extents = win_extents (dpy, w);
858         if (w->mode == WINDOW_SOLID)
859         {
860             int x, y, wid, hei;
861 #if HAS_NAME_WINDOW_PIXMAP
862             x = w->a.x;
863             y = w->a.y;
864             wid = w->a.width + w->a.border_width * 2;
865             hei = w->a.height + w->a.border_width * 2;
866 #else
867             x = w->a.x + w->a.border_width;
868             y = w->a.y + w->a.border_width;
869             wid = w->a.width;
870             hei = w->a.height;
871 #endif
872             XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
873             set_ignore (dpy, NextRequest (dpy));
874             XFixesSubtractRegion (dpy, region, region, w->borderSize);
875             set_ignore (dpy, NextRequest (dpy));
876             XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
877                               0, 0, 0, 0, 
878                               x, y, wid, hei);
879         }
880         if (!w->borderClip)
881         {
882             w->borderClip = XFixesCreateRegion (dpy, 0, 0);
883             XFixesCopyRegion (dpy, w->borderClip, region);
884         }
885         w->prev_trans = t;
886         t = w;
887     }
888 #if DEBUG_REPAINT
889     printf ("\n");
890     fflush (stdout);
891 #endif
892     XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
893     paint_root (dpy);
894     for (w = t; w; w = w->prev_trans)
895     {
896         XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, w->borderClip);
897         switch (compMode) {
898         case CompSimple:
899             break;
900         case CompServerShadows:
901             set_ignore (dpy, NextRequest (dpy));
902             if (w->opacity != OPAQUE && !w->shadowPict)
903                 w->shadowPict = solid_picture (dpy, True,
904                                                (double) w->opacity / OPAQUE * 0.3,
905                                                0, 0, 0);
906             XRenderComposite (dpy, PictOpOver, 
907                               w->shadowPict ? w->shadowPict : transBlackPicture,
908                               w->picture, rootBuffer,
909                               0, 0, 0, 0,
910                               w->a.x + w->shadow_dx,
911                               w->a.y + w->shadow_dy,
912                               w->shadow_width, w->shadow_height);
913             break;
914         case CompClientShadows:
915             if (w->shadow)
916             {
917                 XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
918                                   0, 0, 0, 0,
919                                   w->a.x + w->shadow_dx,
920                                   w->a.y + w->shadow_dy,
921                                   w->shadow_width, w->shadow_height);
922             }
923             break;
924         }
925         if (w->opacity != OPAQUE && !w->alphaPict)
926             w->alphaPict = solid_picture (dpy, False, 
927                                           (double) w->opacity / OPAQUE, 0, 0, 0);
928         if (w->mode == WINDOW_TRANS)
929         {
930             int x, y, wid, hei;
931 #if HAS_NAME_WINDOW_PIXMAP
932             x = w->a.x;
933             y = w->a.y;
934             wid = w->a.width + w->a.border_width * 2;
935             hei = w->a.height + w->a.border_width * 2;
936 #else
937             x = w->a.x + w->a.border_width;
938             y = w->a.y + w->a.border_width;
939             wid = w->a.width;
940             hei = w->a.height;
941 #endif
942             set_ignore (dpy, NextRequest (dpy));
943             XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
944                               0, 0, 0, 0, 
945                               x, y, wid, hei);
946         }
947         else if (w->mode == WINDOW_ARGB)
948         {
949             int x, y, wid, hei;
950 #if HAS_NAME_WINDOW_PIXMAP
951             x = w->a.x;
952             y = w->a.y;
953             wid = w->a.width + w->a.border_width * 2;
954             hei = w->a.height + w->a.border_width * 2;
955 #else
956             x = w->a.x + w->a.border_width;
957             y = w->a.y + w->a.border_width;
958             wid = w->a.width;
959             hei = w->a.height;
960 #endif
961             set_ignore (dpy, NextRequest (dpy));
962             XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
963                               0, 0, 0, 0, 
964                               x, y, wid, hei);
965         }
966         XFixesDestroyRegion (dpy, w->borderClip);
967         w->borderClip = None;
968     }
969     XFixesDestroyRegion (dpy, region);
970     if (rootBuffer != rootPicture)
971     {
972         XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
973         XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
974                           0, 0, 0, 0, 0, 0, root_width, root_height);
975     }
976 }
977
978 static void
979 add_damage (Display *dpy, XserverRegion damage)
980 {
981     if (allDamage)
982     {
983         XFixesUnionRegion (dpy, allDamage, allDamage, damage);
984         XFixesDestroyRegion (dpy, damage);
985     }
986     else
987         allDamage = damage;
988 }
989
990 static void
991 repair_win (Display *dpy, win *w)
992 {
993     XserverRegion   parts;
994
995     if (!w->damaged)
996     {
997         parts = win_extents (dpy, w);
998         set_ignore (dpy, NextRequest (dpy));
999         XDamageSubtract (dpy, w->damage, None, None);
1000     }
1001     else
1002     {
1003         XserverRegion   o;
1004         parts = XFixesCreateRegion (dpy, 0, 0);
1005         set_ignore (dpy, NextRequest (dpy));
1006         XDamageSubtract (dpy, w->damage, None, parts);
1007         XFixesTranslateRegion (dpy, parts,
1008                                w->a.x + w->a.border_width,
1009                                w->a.y + w->a.border_width);
1010         if (compMode == CompServerShadows)
1011         {
1012             o = XFixesCreateRegion (dpy, 0, 0);
1013             XFixesCopyRegion (dpy, o, parts);
1014             XFixesTranslateRegion (dpy, o, w->shadow_dx, w->shadow_dy);
1015             XFixesUnionRegion (dpy, parts, parts, o);
1016             XFixesDestroyRegion (dpy, o);
1017         }
1018     }
1019     add_damage (dpy, parts);
1020     w->damaged = 1;
1021 }
1022
1023 static void
1024 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
1025 {
1026     win         *w = find_win (dpy, id);
1027     Drawable    back;
1028
1029     if (!w)
1030         return;
1031     w->a.map_state = IsViewable;
1032     
1033 #if CAN_DO_USABLE
1034     w->damage_bounds.x = w->damage_bounds.y = 0;
1035     w->damage_bounds.width = w->damage_bounds.height = 0;
1036 #endif
1037     w->damaged = 0;
1038 }
1039
1040 static void
1041 finish_unmap_win (Display *dpy, win *w)
1042 {
1043     w->damaged = 0;
1044 #if CAN_DO_USABLE
1045     w->usable = False;
1046 #endif
1047     if (w->extents != None)
1048     {
1049         add_damage (dpy, w->extents);    /* destroys region */
1050         w->extents = None;
1051     }
1052     
1053 #if HAS_NAME_WINDOW_PIXMAP
1054     if (w->pixmap)
1055     {
1056         XFreePixmap (dpy, w->pixmap);
1057         w->pixmap = None;
1058     }
1059 #endif
1060
1061     if (w->picture)
1062     {
1063         set_ignore (dpy, NextRequest (dpy));
1064         XRenderFreePicture (dpy, w->picture);
1065         w->picture = None;
1066     }
1067
1068     /* don't care about properties anymore */
1069     set_ignore (dpy, NextRequest (dpy));
1070     XSelectInput(dpy, w->id, 0);
1071
1072     if (w->borderSize)
1073     {
1074         set_ignore (dpy, NextRequest (dpy));
1075         XFixesDestroyRegion (dpy, w->borderSize);
1076         w->borderSize = None;
1077     }
1078     if (w->shadow)
1079     {
1080         XRenderFreePicture (dpy, w->shadow);
1081         w->shadow = None;
1082     }
1083     if (w->borderClip)
1084     {
1085         XFixesDestroyRegion (dpy, w->borderClip);
1086         w->borderClip = None;
1087     }
1088
1089     clipChanged = True;
1090 }
1091
1092 #if HAS_NAME_WINDOW_PIXMAP
1093 static void
1094 unmap_callback (Display *dpy, win *w, Bool gone)
1095 {
1096     finish_unmap_win (dpy, w);
1097 }
1098 #endif
1099
1100 static void
1101 unmap_win (Display *dpy, Window id, Bool fade)
1102 {
1103     win *w = find_win (dpy, id);
1104     if (!w)
1105         return;
1106 #if HAS_NAME_WINDOW_PIXMAP
1107     if (w->pixmap && fade && fadeWindows)
1108         set_fade (dpy, w, False, unmap_callback, False);
1109     else
1110 #endif
1111         finish_unmap_win (dpy, w);
1112 }
1113
1114 /* Get the opacity prop from window
1115    not found: default
1116    otherwise the value
1117  */
1118 static unsigned int
1119 get_opacity_prop(Display *dpy, win *w, unsigned int def)
1120 {
1121     Atom actual;
1122     int format;
1123     unsigned long n, left;
1124
1125     char *data;
1126     XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False, 
1127                        XA_CARDINAL, &actual, &format, 
1128                        &n, &left, (unsigned char **) &data);
1129     if (data != None)
1130     {
1131         unsigned int i;
1132         memcpy (&i, data, sizeof (unsigned int));
1133         XFree( (void *) data);
1134         return i;
1135     }
1136     return def;
1137 }
1138
1139 /* determine mode for window all in one place.
1140    Future might check for menu flag and other cool things
1141 */
1142
1143 static void
1144 determine_mode(Display *dpy, win *w)
1145 {
1146     int mode;
1147     XRenderPictFormat *format;
1148     unsigned int default_opacity;
1149
1150     /* if trans prop == -1 fall back on  previous tests*/
1151
1152     if (w->alphaPict)
1153     {
1154         XRenderFreePicture (dpy, w->alphaPict);
1155         w->alphaPict = None;
1156     }
1157     if (w->shadowPict)
1158     {
1159         XRenderFreePicture (dpy, w->shadowPict);
1160         w->shadowPict = None;
1161     }
1162
1163     if (w->a.class == InputOnly)
1164     {
1165         format = 0;
1166     }
1167     else
1168     {
1169         format = XRenderFindVisualFormat (dpy, w->a.visual);
1170     }
1171
1172     if (format && format->type == PictTypeDirect && format->direct.alphaMask)
1173     {
1174         mode = WINDOW_ARGB;
1175     }
1176     else if (w->opacity != OPAQUE)
1177     {
1178         mode = WINDOW_TRANS;
1179     }
1180     else
1181     {
1182         mode = WINDOW_SOLID;
1183     }
1184     w->mode = mode;
1185     if (w->extents)
1186     {
1187         XserverRegion damage;
1188         damage = XFixesCreateRegion (dpy, 0, 0);
1189         XFixesCopyRegion (dpy, damage, w->extents);
1190         add_damage (dpy, damage);
1191     }
1192 }
1193
1194 static void
1195 add_win (Display *dpy, Window id, Window prev)
1196 {
1197     win                         *new = malloc (sizeof (win));
1198     win                         **p;
1199     
1200     if (!new)
1201         return;
1202     if (prev)
1203     {
1204         for (p = &list; *p; p = &(*p)->next)
1205             if ((*p)->id == prev)
1206                 break;
1207     }
1208     else
1209         p = &list;
1210     new->id = id;
1211     set_ignore (dpy, NextRequest (dpy));
1212     if (!XGetWindowAttributes (dpy, id, &new->a))
1213     {
1214         free (new);
1215         return;
1216     }
1217     new->damaged = 0;
1218 #if CAN_DO_USABLE
1219     new->usable = False;
1220 #endif
1221 #if HAS_NAME_WINDOW_PIXMAP
1222     new->pixmap = None;
1223 #endif
1224     new->picture = None;
1225     if (new->a.class == InputOnly)
1226     {
1227         new->damage_sequence = 0;
1228         new->damage = None;
1229     }
1230     else
1231     {
1232         new->damage_sequence = NextRequest (dpy);
1233         new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
1234     }
1235     new->alphaPict = None;
1236     new->shadowPict = None;
1237     new->borderSize = None;
1238     new->extents = None;
1239     new->shadow = None;
1240     new->shadow_dx = 0;
1241     new->shadow_dy = 0;
1242     new->shadow_width = 0;
1243     new->shadow_height = 0;
1244     new->opacity = OPAQUE;
1245
1246     new->borderClip = None;
1247     new->prev_trans = 0;
1248
1249     /* moved mode setting to one place */
1250     XSelectInput(dpy, id, PropertyChangeMask);
1251     new->opacity = get_opacity_prop(dpy, new, OPAQUE);
1252     determine_mode (dpy, new);
1253     
1254     new->next = *p;
1255     *p = new;
1256     if (new->a.map_state == IsViewable)
1257         map_win (dpy, id, new->damage_sequence - 1, False);
1258 }
1259
1260 void
1261 restack_win (Display *dpy, win *w, Window new_above)
1262 {
1263     Window  old_above;
1264     
1265     if (w->next)
1266         old_above = w->next->id;
1267     else
1268         old_above = None;
1269     if (old_above != new_above)
1270     {
1271         win **prev;
1272
1273         /* unhook */
1274         for (prev = &list; *prev; prev = &(*prev)->next)
1275             if ((*prev) == w)
1276                 break;
1277         *prev = w->next;
1278         
1279         /* rehook */
1280         for (prev = &list; *prev; prev = &(*prev)->next)
1281         {
1282             if ((*prev)->id == new_above)
1283                 break;
1284         }
1285         w->next = *prev;
1286         *prev = w;
1287     }
1288 }
1289
1290 static void
1291 configure_win (Display *dpy, XConfigureEvent *ce)
1292 {
1293     win             *w = find_win (dpy, ce->window);
1294     Window          above;
1295     XserverRegion   damage = None;
1296     
1297     if (!w)
1298     {
1299         if (ce->window == root)
1300         {
1301             if (rootBuffer)
1302             {
1303                 XRenderFreePicture (dpy, rootBuffer);
1304                 rootBuffer = None;
1305             }
1306             root_width = ce->width;
1307             root_height = ce->height;
1308         }
1309         return;
1310     }
1311 #if CAN_DO_USABLE
1312     if (w->usable)
1313 #endif
1314     {
1315         damage = XFixesCreateRegion (dpy, 0, 0);
1316         if (w->extents != None) 
1317             XFixesCopyRegion (dpy, damage, w->extents);
1318     }
1319     w->a.x = ce->x;
1320     w->a.y = ce->y;
1321     if (w->a.width != ce->width || w->a.height != ce->height)
1322     {
1323 #if HAS_NAME_WINDOW_PIXMAP
1324         if (w->pixmap)
1325         {
1326             XFreePixmap (dpy, w->pixmap);
1327             w->pixmap = None;
1328             if (w->picture)
1329             {
1330                 XRenderFreePicture (dpy, w->picture);
1331                 w->picture = None;
1332             }
1333         }
1334 #endif
1335         if (w->shadow)
1336         {
1337             XRenderFreePicture (dpy, w->shadow);
1338             w->shadow = None;
1339         }
1340     }
1341     w->a.width = ce->width;
1342     w->a.height = ce->height;
1343     w->a.border_width = ce->border_width;
1344     w->a.override_redirect = ce->override_redirect;
1345     restack_win (dpy, w, ce->above);
1346     if (damage)
1347     {
1348         XserverRegion   extents = win_extents (dpy, w);
1349         XFixesUnionRegion (dpy, damage, damage, extents);
1350         XFixesDestroyRegion (dpy, extents);
1351         add_damage (dpy, damage);
1352     }
1353     clipChanged = True;
1354 }
1355
1356 static void
1357 circulate_win (Display *dpy, XCirculateEvent *ce)
1358 {
1359     win     *w = find_win (dpy, ce->window);
1360     Window  new_above;
1361
1362     if (ce->place == PlaceOnTop)
1363         new_above = list->id;
1364     else
1365         new_above = None;
1366     restack_win (dpy, w, new_above);
1367     clipChanged = True;
1368 }
1369
1370 static void
1371 finish_destroy_win (Display *dpy, Window id, Bool gone)
1372 {
1373     win **prev, *w;
1374
1375     for (prev = &list; (w = *prev); prev = &w->next)
1376         if (w->id == id)
1377         {
1378             if (!gone)
1379                 finish_unmap_win (dpy, w);
1380             *prev = w->next;
1381             if (w->picture)
1382             {
1383                 set_ignore (dpy, NextRequest (dpy));
1384                 XRenderFreePicture (dpy, w->picture);
1385             }
1386             if (w->alphaPict)
1387             {
1388                 XRenderFreePicture (dpy, w->alphaPict);
1389                 w->alphaPict = None;
1390             }
1391             if (w->shadowPict)
1392             {
1393                 XRenderFreePicture (dpy, w->shadowPict);
1394                 w->shadowPict = None;
1395             }
1396             if (w->damage != None)
1397             {
1398                 set_ignore (dpy, NextRequest (dpy));
1399                 XDamageDestroy (dpy, w->damage);
1400             }
1401             cleanup_fade (dpy, w);
1402             free (w);
1403             break;
1404         }
1405 }
1406
1407 #if HAS_NAME_WINDOW_PIXMAP
1408 static void
1409 destroy_callback (Display *dpy, win *w, Bool gone)
1410 {
1411     finish_destroy_win (dpy, w->id, gone);
1412 }
1413 #endif
1414
1415 static void
1416 destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
1417 {
1418     win *w = find_win (dpy, id);
1419 #if HAS_NAME_WINDOW_PIXMAP
1420     if (w && w->pixmap && fade && fadeWindows)
1421         set_fade (dpy, w, False, destroy_callback, gone);
1422     else
1423 #endif
1424     {
1425         finish_destroy_win (dpy, id, gone);
1426     }
1427 }
1428
1429 /*
1430 static void
1431 dump_win (win *w)
1432 {
1433     printf ("\t%08lx: %d x %d + %d + %d (%d)\n", w->id,
1434             w->a.width, w->a.height, w->a.x, w->a.y, w->a.border_width);
1435 }
1436
1437
1438 static void
1439 dump_wins (void)
1440 {
1441     win *w;
1442
1443     printf ("windows:\n");
1444     for (w = list; w; w = w->next)
1445         dump_win (w);
1446 }
1447 */
1448
1449 static void
1450 damage_win (Display *dpy, XDamageNotifyEvent *de)
1451 {
1452     win *w = find_win (dpy, de->drawable);
1453
1454     if (!w)
1455         return;
1456 #if CAN_DO_USABLE
1457     if (!w->usable)
1458     {
1459         if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
1460         {
1461             w->damage_bounds = de->area;
1462         }
1463         else
1464         {
1465             if (de->area.x < w->damage_bounds.x)
1466             {
1467                 w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
1468                 w->damage_bounds.x = de->area.x;
1469             }
1470             if (de->area.y < w->damage_bounds.y)
1471             {
1472                 w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
1473                 w->damage_bounds.y = de->area.y;
1474             }
1475             if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
1476                 w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
1477             if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
1478                 w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
1479         }
1480 #if 0
1481         printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
1482                 de->area.x,
1483                 de->area.y,
1484                 de->area.width,
1485                 de->area.height,
1486                 w->damage_bounds.x,
1487                 w->damage_bounds.y,
1488                 w->damage_bounds.width,
1489                 w->damage_bounds.height);
1490 #endif
1491         if (w->damage_bounds.x <= 0 && 
1492             w->damage_bounds.y <= 0 &&
1493             w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
1494             w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
1495         {
1496             clipChanged = True;
1497             if (fadeWindows)
1498                 set_fade (dpy, w, True, 0, False);
1499             w->usable = True;
1500         }
1501     }
1502     if (w->usable)
1503 #endif
1504         repair_win (dpy, w);
1505 }
1506
1507 static int
1508 error (Display *dpy, XErrorEvent *ev)
1509 {
1510     int     o;
1511     char    *name = 0;
1512     
1513     if (should_ignore (dpy, ev->serial))
1514         return 0;
1515     
1516     if (ev->request_code == composite_opcode &&
1517         ev->minor_code == X_CompositeRedirectSubwindows)
1518     {
1519         fprintf (stderr, "Another composite manager is already running\n");
1520         exit (1);
1521     }
1522     
1523     o = ev->error_code - xfixes_error;
1524     switch (o) {
1525     case BadRegion: name = "BadRegion"; break;
1526     default: break;
1527     }
1528     o = ev->error_code - damage_error;
1529     switch (o) {
1530     case BadDamage: name = "BadDamage"; break;
1531     default: break;
1532     }
1533     o = ev->error_code - render_error;
1534     switch (o) {
1535     case BadPictFormat: name ="BadPictFormat"; break;
1536     case BadPicture: name ="BadPicture"; break;
1537     case BadPictOp: name ="BadPictOp"; break;
1538     case BadGlyphSet: name ="BadGlyphSet"; break;
1539     case BadGlyph: name ="BadGlyph"; break;
1540     default: break;
1541     }
1542         
1543     printf ("error %d request %d minor %d serial %d\n",
1544             ev->error_code, ev->request_code, ev->minor_code, ev->serial);
1545
1546     abort ();
1547     return 0;
1548 }
1549
1550 static void
1551 expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
1552 {
1553     XserverRegion  region = XFixesCreateRegion (dpy, rects, nrects);
1554     
1555     add_damage (dpy, region);
1556 }
1557
1558
1559 static int
1560 ev_serial (XEvent *ev)
1561 {
1562     if (ev->type & 0x7f != KeymapNotify)
1563         return ev->xany.serial;
1564     return NextRequest (ev->xany.display);
1565 }
1566
1567
1568 static char *
1569 ev_name (XEvent *ev)
1570 {
1571     static char buf[128];
1572     switch (ev->type & 0x7f) {
1573     case Expose:
1574         return "Expose";
1575     case MapNotify:
1576         return "Map";
1577     case UnmapNotify:
1578         return "Unmap";
1579     case ReparentNotify:
1580         return "Reparent";
1581     case CirculateNotify:
1582         return "Circulate";
1583     default:
1584         if (ev->type == damage_event + XDamageNotify)
1585             return "Damage";
1586         sprintf (buf, "Event %d", ev->type);
1587         return buf;
1588     }
1589 }
1590
1591 static Window
1592 ev_window (XEvent *ev)
1593 {
1594     switch (ev->type) {
1595     case Expose:
1596         return ev->xexpose.window;
1597     case MapNotify:
1598         return ev->xmap.window;
1599     case UnmapNotify:
1600         return ev->xunmap.window;
1601     case ReparentNotify:
1602         return ev->xreparent.window;
1603     case CirculateNotify:
1604         return ev->xcirculate.window;
1605     default:
1606         if (ev->type == damage_event + XDamageNotify)
1607             return ((XDamageNotifyEvent *) ev)->drawable;
1608         return 0;
1609     }
1610 }
1611
1612 void
1613 usage (char *program)
1614 {
1615     fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c]\n", program);
1616     exit (1);
1617 }
1618
1619 int
1620 main (int argc, char **argv)
1621 {
1622     XEvent          ev;
1623     Window          root_return, parent_return;
1624     Window          *children;
1625     Pixmap          transPixmap;
1626     Pixmap          blackPixmap;
1627     unsigned int    nchildren;
1628     int             i;
1629     XRenderPictureAttributes    pa;
1630     XRenderColor                c;
1631     XRectangle      *expose_rects = 0;
1632     int             size_expose = 0;
1633     int             n_expose = 0;
1634     struct pollfd   ufd;
1635     int             n;
1636     int             last_update;
1637     int             now;
1638     int             p;
1639     int             composite_major, composite_minor;
1640     char            *display = 0;
1641     int             o;
1642
1643     while ((o = getopt (argc, argv, "d:scnfS")) != -1)
1644     {
1645         switch (o) {
1646         case 'd':
1647             display = optarg;
1648             break;
1649         case 's':
1650             compMode = CompServerShadows;
1651             break;
1652         case 'c':
1653             compMode = CompClientShadows;
1654             break;
1655         case 'n':
1656             compMode = CompSimple;
1657             break;
1658         case 'f':
1659             fadeWindows = True;
1660             break;
1661         case 'S':
1662             synchronize = True;
1663             break;
1664         default:
1665             usage (argv[0]);
1666             break;
1667         }
1668     }
1669     
1670     dpy = XOpenDisplay (display);
1671     if (!dpy)
1672     {
1673         fprintf (stderr, "Can't open display\n");
1674         exit (1);
1675     }
1676     XSetErrorHandler (error);
1677     if (synchronize)
1678         XSynchronize (dpy, 1);
1679     scr = DefaultScreen (dpy);
1680     root = RootWindow (dpy, scr);
1681
1682     if (!XRenderQueryExtension (dpy, &render_event, &render_error))
1683     {
1684         fprintf (stderr, "No render extension\n");
1685         exit (1);
1686     }
1687     if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
1688                           &composite_event, &composite_error))
1689     {
1690         fprintf (stderr, "No composite extension\n");
1691         exit (1);
1692     }
1693     XCompositeQueryVersion (dpy, &composite_major, &composite_minor);
1694 #if HAS_NAME_WINDOW_PIXMAP
1695     if (composite_major > 0 || composite_minor >= 2)
1696         hasNamePixmap = True;
1697 #endif
1698
1699     if (!XDamageQueryExtension (dpy, &damage_event, &damage_error))
1700     {
1701         fprintf (stderr, "No damage extension\n");
1702         exit (1);
1703     }
1704     if (!XFixesQueryExtension (dpy, &xfixes_event, &xfixes_error))
1705     {
1706         fprintf (stderr, "No XFixes extension\n");
1707         exit (1);
1708     }
1709     /* get atoms */
1710     opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
1711
1712     pa.subwindow_mode = IncludeInferiors;
1713
1714     if (compMode == CompClientShadows)
1715         gaussianMap = make_gaussian_map(dpy, shadowRadius);
1716
1717     root_width = DisplayWidth (dpy, scr);
1718     root_height = DisplayHeight (dpy, scr);
1719
1720     rootPicture = XRenderCreatePicture (dpy, root, 
1721                                         XRenderFindVisualFormat (dpy,
1722                                                                  DefaultVisual (dpy, scr)),
1723                                         CPSubwindowMode,
1724                                         &pa);
1725     blackPicture = solid_picture (dpy, True, 1, 0, 0, 0);
1726     if (compMode == CompServerShadows)
1727         transBlackPicture = solid_picture (dpy, True, 0.3, 0, 0, 0);
1728     allDamage = None;
1729     clipChanged = True;
1730     XGrabServer (dpy);
1731     XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
1732     XSelectInput (dpy, root, 
1733                   SubstructureNotifyMask|
1734                   ExposureMask|
1735                   StructureNotifyMask|
1736                   PropertyChangeMask);
1737     XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
1738     for (i = 0; i < nchildren; i++)
1739         add_win (dpy, children[i], i ? children[i-1] : None);
1740     XFree (children);
1741     XUngrabServer (dpy);
1742     ufd.fd = ConnectionNumber (dpy);
1743     ufd.events = POLLIN;
1744     paint_all (dpy, None);
1745     for (;;)
1746     {
1747         /*      dump_wins (); */
1748         do {
1749             if (!QLength (dpy))
1750             {
1751                  if (poll (&ufd, 1, fade_timeout()) == 0)
1752                  {
1753                     run_fades (dpy);
1754                     break;
1755                  }
1756             }
1757
1758             XNextEvent (dpy, &ev);
1759             if (ev.type & 0x7f != KeymapNotify)
1760                 discard_ignore (dpy, ev.xany.serial);
1761 #if DEBUG_EVENTS
1762             printf ("event %10.10s serial 0x%08x window 0x%08x\n",
1763                     ev_name(&ev), ev_serial (&ev), ev_window (&ev));
1764 #endif
1765             switch (ev.type) {
1766             case CreateNotify:
1767                 add_win (dpy, ev.xcreatewindow.window, 0);
1768                 break;
1769             case ConfigureNotify:
1770                 configure_win (dpy, &ev.xconfigure);
1771                 break;
1772             case DestroyNotify:
1773                 destroy_win (dpy, ev.xdestroywindow.window, True, True);
1774                 break;
1775             case MapNotify:
1776                 map_win (dpy, ev.xmap.window, ev.xmap.serial, True);
1777                 break;
1778             case UnmapNotify:
1779                 unmap_win (dpy, ev.xunmap.window, True);
1780                 break;
1781             case ReparentNotify:
1782                 if (ev.xreparent.parent == root)
1783                     add_win (dpy, ev.xreparent.window, 0);
1784                 else
1785                     destroy_win (dpy, ev.xreparent.window, False, True);
1786                 break;
1787             case CirculateNotify:
1788                 circulate_win (dpy, &ev.xcirculate);
1789                 break;
1790             case Expose:
1791                 if (ev.xexpose.window == root)
1792                 {
1793                     int more = ev.xexpose.count + 1;
1794                     if (n_expose == size_expose)
1795                     {
1796                         if (expose_rects)
1797                         {
1798                             expose_rects = realloc (expose_rects, 
1799                                                     (size_expose + more) * 
1800                                                     sizeof (XRectangle));
1801                             size_expose += more;
1802                         }
1803                         else
1804                         {
1805                             expose_rects = malloc (more * sizeof (XRectangle));
1806                             size_expose = more;
1807                         }
1808                     }
1809                     expose_rects[n_expose].x = ev.xexpose.x;
1810                     expose_rects[n_expose].y = ev.xexpose.y;
1811                     expose_rects[n_expose].width = ev.xexpose.width;
1812                     expose_rects[n_expose].height = ev.xexpose.height;
1813                     n_expose++;
1814                     if (ev.xexpose.count == 0)
1815                     {
1816                         expose_root (dpy, root, expose_rects, n_expose);
1817                         n_expose = 0;
1818                     }
1819                 }
1820                 break;
1821             case PropertyNotify:
1822                 for (p = 0; backgroundProps[p]; p++)
1823                 {
1824                     if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
1825                     {
1826                         if (rootTile)
1827                         {
1828                             XClearArea (dpy, root, 0, 0, 0, 0, True);
1829                             XRenderFreePicture (dpy, rootTile);
1830                             rootTile = None;
1831                             break;
1832                         }
1833                     }
1834                 }
1835                 /* check if Trans property was changed */
1836                 if (ev.xproperty.atom == opacityAtom)
1837                 {
1838                     /* reset mode and redraw window */
1839                     win * w = find_win(dpy, ev.xproperty.window);
1840                     if (w)
1841                     {
1842                         w->opacity = get_opacity_prop(dpy, w, OPAQUE);
1843                         determine_mode(dpy, w);
1844                     }
1845                 }
1846                 break;
1847             default:
1848                 if (ev.type == damage_event + XDamageNotify)
1849                     damage_win (dpy, (XDamageNotifyEvent *) &ev);
1850                 break;
1851             }
1852         } while (QLength (dpy));
1853         if (allDamage)
1854         {
1855             static int  paint;
1856             paint_all (dpy, allDamage);
1857             paint++;
1858             XSync (dpy, False);
1859             allDamage = None;
1860             clipChanged = False;
1861         }
1862     }
1863 }