Track whether cliplists have changed and avoid computation when not. Shrink
[dana/xcompmgr.git] / xcompmgr.c
index 4949e02..8adb0c0 100644 (file)
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
+
+/* Modified by Matthew Hawn. I don't know what to say here so follow what it 
+   says above. Not that I can really do anything about it
+*/
+
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -39,8 +45,8 @@ typedef struct _win {
     struct _win                *next;
     Window             id;
     XWindowAttributes  a;
-    int                        damaged;
     int                        mode;
+    int                        damaged;
     Damage             damage;
     Picture            picture;
     XserverRegion      borderSize;
@@ -51,6 +57,8 @@ typedef struct _win {
     int                        shadow_width;
     int                        shadow_height;
 
+    unsigned long      damage_sequence;    /* sequence when damage was created */
+
     /* for drawing translucent windows */
     XserverRegion      borderClip;
     struct _win                *prev_trans;
@@ -71,21 +79,32 @@ Picture             transPicture;
 Picture                blackPicture;
 Picture                rootTile;
 XserverRegion  allDamage;
+Bool           clipChanged;
 int            root_height, root_width;
-conv            *gussianMap;
 
-#define BACKGROUND_PROP        "_XROOTPMAP_ID"
+
+/* find these once and be done with it */
+Atom            transPropAtom;
+Atom            intAtom;
+
+/* translucency property name */
+#define TRANS_PROP "CM_TRANSLUCENCY"
+
+conv            *gaussianMap;
 
 #define WINDOW_SOLID   0
 #define WINDOW_TRANS   1
 #define WINDOW_ARGB    2
 
 #define TRANS_OPACITY  0.75
-#define SHADOW_RADIUS  15
+#define SHADOW_RADIUS  8
 #define SHADOW_OPACITY 0.75
-#define SHADOW_OFFSET_X        (-SHADOW_RADIUS)
-#define SHADOW_OFFSET_Y        (-SHADOW_RADIUS)
+#define SHADOW_OFFSET_X        (-SHADOW_RADIUS * 3 / 2)
+#define SHADOW_OFFSET_Y        (-SHADOW_RADIUS * 2 / 2)
 
+#define DEBUG_REPAINT 0
+#define DEBUG_EVENTS 0
+#define MONITOR_REPAINT 0
 
 static double
 gaussian (double r, double x, double y)
@@ -191,7 +210,7 @@ sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
     if (v > 1)
        v = 1;
     
-    return ((unsigned int) (v * opacity * 255.0));
+    return ((unsigned char) (v * opacity * 255.0));
 }
 
 static XImage *
@@ -199,13 +218,14 @@ make_shadow (Display *dpy, double opacity, int width, int height)
 {
     XImage         *ximage;
     unsigned char   *data;
-    int                    gsize = gussianMap->size;
+    int                    gsize = gaussianMap->size;
     int                    ylimit, xlimit;
     int                    swidth = width + gsize;
     int                    sheight = height + gsize;
     int                    center = gsize / 2;
     int                    x, y;
     unsigned char   d;
+    int                    x_diff;
     
     data = malloc (swidth * sheight * sizeof (unsigned char));
     ximage = XCreateImage (dpy,
@@ -220,6 +240,13 @@ make_shadow (Display *dpy, double opacity, int width, int height)
      */
 
     /*
+     * center (fill the complete data array)
+     */
+
+    d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
+    memset(data, d, sheight * swidth);
+    
+    /*
      * corners
      */
     ylimit = gsize;
@@ -232,7 +259,7 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     for (y = 0; y < ylimit; y++)
        for (x = 0; x < xlimit; x++)
        {
-           d = sum_gaussian (gussianMap, opacity, x - center, y - center, width, height);
+           d = sum_gaussian (gaussianMap, opacity, x - center, y - center, width, height);
            data[y * swidth + x] = d;
            data[(sheight - y - 1) * swidth + x] = d;
            data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
@@ -242,13 +269,21 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     /*
      * top/bottom
      */
-    for (y = 0; y < ylimit; y++)
+    x_diff = swidth - (gsize * 2);
+    if (x_diff > 0 && ylimit > 0)
     {
-       d = sum_gaussian (gussianMap, opacity, center, y - center, width, height);
-       for (x = gsize; x < swidth - gsize; x++)
+       for (y = 0; y < ylimit; y++)
        {
-           data[y * swidth + x] = d;
-           data[(sheight - y - 1) * swidth + x] = d;
+           d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
+           memset (&data[y * swidth + gsize], d, x_diff);
+           memset (&data[(sheight - y - 1) * swidth + gsize], d, x_diff);
+#if 0
+           for (x = gsize; x < swidth - gsize; x++)
+           {
+               data[y * swidth + x] = d;
+               data[(sheight - y - 1) * swidth + x] = d;
+           }
+#endif
        }
     }
 
@@ -258,7 +293,7 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     
     for (x = 0; x < xlimit; x++)
     {
-       d = sum_gaussian (gussianMap, opacity, x - center, center, width, height);
+       d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
        for (y = gsize; y < sheight - gsize; y++)
        {
            data[y * swidth + x] = d;
@@ -270,7 +305,7 @@ make_shadow (Display *dpy, double opacity, int width, int height)
      * center
      */
 
-    d = sum_gaussian (gussianMap, opacity, center, center, width, height);
+    d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
     for (y = ylimit; y < sheight - ylimit; y++)
        for (x = xlimit; x < swidth - xlimit; x++)
            data[y * swidth + x] = d;
@@ -313,6 +348,12 @@ find_win (Display *dpy, Window id)
     return 0;
 }
 
+static char *backgroundProps[] = {
+    "_XROOTPMAP_ID",
+    "_XSETROOT_ID",
+    0,
+};
+    
 static Picture
 root_tile (Display *dpy)
 {
@@ -325,17 +366,23 @@ root_tile (Display *dpy)
     unsigned char   *prop;
     Bool           fill;
     XRenderPictureAttributes   pa;
+    int                    p;
 
-    if (XGetWindowProperty (dpy, root, XInternAtom (dpy, BACKGROUND_PROP, False),
-                           0, 4, False, AnyPropertyType,
-                           &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
-       actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
+    pixmap = None;
+    for (p = 0; backgroundProps[p]; p++)
     {
-       memcpy (&pixmap, prop, 4);
-       XFree (prop);
-       fill = False;
+       if (XGetWindowProperty (dpy, root, XInternAtom (dpy, backgroundProps[p], False),
+                               0, 4, False, AnyPropertyType,
+                               &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success &&
+           actual_type == XInternAtom (dpy, "PIXMAP", False) && actual_format == 32 && nitems == 1)
+       {
+           memcpy (&pixmap, prop, 4);
+           XFree (prop);
+           fill = False;
+           break;
+       }
     }
-    else
+    if (!pixmap)
     {
        pixmap = XCreatePixmap (dpy, root, 1, 1, DefaultDepth (dpy, scr));
        fill = True;
@@ -373,30 +420,44 @@ win_extents (Display *dpy, win *w)
 {
     XRectangle     r;
     
-    if (w->mode == WINDOW_ARGB)
-    {
-       r.x = w->a.x;
-       r.y = w->a.y;
-       r.width = w->a.width + w->a.border_width * 2;
-       r.height = w->a.height + w->a.border_width * 2;
-    }
-    else
+    r.x = w->a.x;
+    r.y = w->a.y;
+    r.width = w->a.width + w->a.border_width * 2;
+    r.height = w->a.height + w->a.border_width * 2;
+    if (w->mode != WINDOW_ARGB)
     {
+       XRectangle  sr;
+       
        if (!w->shadow)
        {
            double      opacity = SHADOW_OPACITY;
            if (w->mode == WINDOW_TRANS)
                opacity = opacity * TRANS_OPACITY;
-           w->shadow = shadow_picture (dpy, opacity, 
-                                       w->a.width, w->a.height,
+           w->shadow = shadow_picture (dpy, opacity,
+                                       w->a.width + w->a.border_width * 2,
+                                       w->a.height + w->a.border_width * 2,
                                        &w->shadow_width, &w->shadow_height);
            w->shadow_dx = SHADOW_OFFSET_X;
            w->shadow_dy = SHADOW_OFFSET_Y;
        }
-       r.x = w->a.x + w->a.border_width + w->shadow_dx;
-       r.y = w->a.y + w->a.border_width + w->shadow_dy;
-       r.width = w->shadow_width;
-       r.height = w->shadow_height;
+       sr.x = w->a.x + w->shadow_dx;
+       sr.y = w->a.y + w->shadow_dy;
+       sr.width = w->shadow_width;
+       sr.height = w->shadow_height;
+       if (sr.x < r.x)
+       {
+           r.width = (r.x + r.width) - sr.x;
+           r.x = sr.x;
+       }
+       if (sr.y < r.y)
+       {
+           r.height = (r.y + r.height) - sr.y;
+           r.y = sr.y;
+       }
+       if (sr.width > r.width)
+           r.width = sr.width;
+       if (sr.height > r.height)
+           r.height = sr.height;
     }
     return XFixesCreateRegion (dpy, &r, 1);
 }
@@ -407,7 +468,9 @@ border_size (Display *dpy, win *w)
     XserverRegion   border;
     border = XFixesCreateRegionFromWindow (dpy, w->id, WindowRegionBounding);
     /* translate this */
-    XFixesUnionRegion (dpy, border, border, w->a.x, w->a.y, None, 0, 0);
+    XFixesTranslateRegion (dpy, border,
+                          w->a.x + w->a.border_width,
+                          w->a.y + w->a.border_width);
     return border;
 }
 
@@ -426,6 +489,9 @@ paint_all (Display *dpy, XserverRegion region)
        r.height = root_height;
        region = XFixesCreateRegion (dpy, &r, 1);
     }
+#if MONITOR_REPAINT
+    rootBuffer = rootPicture;
+#else
     if (!rootBuffer)
     {
        Pixmap  rootPixmap = XCreatePixmap (dpy, root, root_width, root_height,
@@ -436,24 +502,53 @@ paint_all (Display *dpy, XserverRegion region)
                                           0, 0);
        XFreePixmap (dpy, rootPixmap);
     }
+#endif
     XFixesSetPictureClipRegion (dpy, rootPicture, 0, 0, region);
+#if MONITOR_REPAINT
+    XRenderComposite (dpy, PictOpSrc, blackPicture, None, rootPicture,
+                     0, 0, 0, 0, 0, 0, root_width, root_height);
+#endif
+#if DEBUG_REPAINT
+    printf ("paint:");
+#endif
     for (w = list; w; w = w->next)
     {
        if (w->a.map_state != IsViewable)
            continue;
+       /* never painted, ignore it */
+       if (!w->damaged)
+           continue;
        if (!w->picture)
            continue;
-       
-       if (w->borderSize)
-           XFixesDestroyRegion (dpy, w->borderSize);
-       w->borderSize = border_size (dpy, w);
-       if (w->extents)
-           XFixesDestroyRegion (dpy, w->extents);
-       w->extents = win_extents (dpy, w);
+#if DEBUG_REPAINT
+       printf (" 0x%x", w->id);
+#endif
+       if (clipChanged)
+       {
+           if (w->borderSize)
+           {
+               XFixesDestroyRegion (dpy, w->borderSize);
+               w->borderSize = None;
+           }
+           if (w->extents)
+           {
+               XFixesDestroyRegion (dpy, w->extents);
+               w->extents = None;
+           }
+           if (w->borderClip)
+           {
+               XFixesDestroyRegion (dpy, w->borderClip);
+               w->borderClip = None;
+           }
+       }
+       if (!w->borderSize)
+           w->borderSize = border_size (dpy, w);
+       if (!w->extents)
+           w->extents = win_extents (dpy, w);
        if (w->mode == WINDOW_SOLID)
        {
            XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
-           XFixesSubtractRegion (dpy, region, region, 0, 0, w->borderSize, 0, 0);
+           XFixesSubtractRegion (dpy, region, region, w->borderSize);
            XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
                              0, 0, 0, 0, 
                              w->a.x + w->a.border_width,
@@ -461,11 +556,18 @@ paint_all (Display *dpy, XserverRegion region)
                              w->a.width,
                              w->a.height);
        }
-       w->borderClip = XFixesCreateRegion (dpy, 0, 0);
-       XFixesUnionRegion (dpy, w->borderClip, region, 0, 0, None, 0, 0);
+       if (!w->borderClip)
+       {
+           w->borderClip = XFixesCreateRegion (dpy, 0, 0);
+           XFixesCopyRegion (dpy, w->borderClip, region);
+       }
        w->prev_trans = t;
        t = w;
     }
+#if DEBUG_REPAINT
+    printf ("\n");
+    fflush (stdout);
+#endif
     XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
     paint_root (dpy);
     for (w = t; w; w = w->prev_trans)
@@ -475,8 +577,8 @@ paint_all (Display *dpy, XserverRegion region)
        {
            XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
                              0, 0, 0, 0,
-                             w->a.x + w->a.border_width + w->shadow_dx,
-                             w->a.y + w->a.border_width + w->shadow_dy,
+                             w->a.x + w->shadow_dx,
+                             w->a.y + w->shadow_dy,
                              w->shadow_width, w->shadow_height);
        }
        if (w->mode == WINDOW_TRANS)
@@ -497,9 +599,12 @@ paint_all (Display *dpy, XserverRegion region)
        w->borderClip = None;
     }
     XFixesDestroyRegion (dpy, region);
-    XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
-    XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
-                     0, 0, 0, 0, 0, 0, root_width, root_height);
+    if (rootBuffer != rootPicture)
+    {
+       XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, None);
+       XRenderComposite (dpy, PictOpSrc, rootBuffer, None, rootPicture,
+                         0, 0, 0, 0, 0, 0, root_width, root_height);
+    }
 }
 
 static void
@@ -507,7 +612,7 @@ add_damage (Display *dpy, XserverRegion damage)
 {
     if (allDamage)
     {
-       XFixesUnionRegion (dpy, allDamage, allDamage, 0, 0, damage, 0, 0);
+       XFixesUnionRegion (dpy, allDamage, allDamage, damage);
        XFixesDestroyRegion (dpy, damage);
     }
     else
@@ -522,29 +627,37 @@ repair_win (Display *dpy, Window id)
 
     if (!w)
        return;
-/*    printf ("repair 0x%x\n", w->id); */
-    parts = XFixesCreateRegion (dpy, 0, 0);
-    /* translate region */
-    XDamageSubtract (dpy, w->damage, None, parts);
-    XFixesUnionRegion (dpy, parts, parts, w->a.x, w->a.y, None, 0, 0);
+    if (!w->damaged)
+    {
+       parts = win_extents (dpy, w);
+       XDamageSubtract (dpy, w->damage, None, None);
+    }
+    else
+    {
+       parts = XFixesCreateRegion (dpy, 0, 0);
+       XDamageSubtract (dpy, w->damage, None, parts);
+       XFixesTranslateRegion (dpy, parts,
+                              w->a.x + w->a.border_width,
+                              w->a.y + w->a.border_width);
+    }
     add_damage (dpy, parts);
+    w->damaged = 1;
 }
 
 static void
-map_win (Display *dpy, Window id)
+map_win (Display *dpy, Window id, unsigned long sequence)
 {
     win                    *w = find_win (dpy, id);
-    XserverRegion   region;
 
     if (!w)
        return;
     w->a.map_state = IsViewable;
-    if (w->picture)
-    {
-       w->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
-       region = win_extents (dpy, w);
-       add_damage (dpy, region);
-    }
+
+    /* make sure we know if property was changed */
+    XSelectInput(dpy, id, PropertyChangeMask);
+
+    w->damaged = 0;
+    clipChanged = True;
 }
 
 static void
@@ -555,16 +668,103 @@ unmap_win (Display *dpy, Window id)
     if (!w)
        return;
     w->a.map_state = IsUnmapped;
-    if (w->damage != None)
-    {
-       XDamageDestroy (dpy, w->damage);
-       w->damage = None;
-    }
+    w->damaged = 0;
     if (w->extents != None)
     {
        add_damage (dpy, w->extents);    /* destroys region */
        w->extents = None;
     }
+
+    /* don't care about properties anymore */
+    XSelectInput(dpy, id, 0);
+    clipChanged = True;
+}
+
+
+
+/* Get the translucency prop from window
+   not found: -1
+   otherwise the value
+
+ */
+static int
+get_trans_prop(Display *dpy, win *w)
+{
+   Atom actual;
+    int format;
+    unsigned long n, left;
+    
+    char *data;
+    XGetWindowProperty(dpy, w->id, transPropAtom, 0L, 1L, False, intAtom, &actual, &format, 
+                      &n, &left, (unsigned char **) &data);
+    if (data != None)
+    {
+      int i = (int) *data;
+      XFree( (void *) data);
+      return i;
+    }
+    return -1;
+
+}
+
+/* determine mode for window all in one place.
+   Future might check for menu flag and other cool things
+*/
+
+
+static int 
+determine_mode(Display *dpy, win *w)
+{
+    int mode;
+
+    /* if trans prop == -1 fall back on  previous tests*/
+
+    int p = get_trans_prop(dpy, w);
+    if  (  p  != -1 )
+    {
+      if (p > 0)
+       {
+        /* don't really care about the value as long as > 0 */
+         mode = WINDOW_TRANS;
+       }
+      else
+       {
+         mode = WINDOW_SOLID;
+       }
+    }
+
+    
+
+    else 
+    {
+      XRenderPictFormat *format;
+      if (w->a.class == InputOnly)
+      {
+       format = 0;
+      }
+      else
+      {
+       format = XRenderFindVisualFormat (dpy, w->a.visual);
+      }
+
+
+
+      if (format && format->type == PictTypeDirect && format->direct.alphaMask)
+      {
+       mode = WINDOW_ARGB;
+      }
+      else if (w->a.override_redirect)
+      {
+       /* changed this as a lot of window managers set this for all top
+           level windows */
+       mode = WINDOW_TRANS;
+      }
+      else
+      {
+       mode = WINDOW_SOLID;
+      }
+    }
+    return mode;
 }
 
 static void
@@ -591,6 +791,7 @@ add_win (Display *dpy, Window id, Window prev)
        free (new);
        return;
     }
+    new->damage = None;
     new->damaged = 0;
     new->damage = None;
     pa.subwindow_mode = IncludeInferiors;
@@ -598,6 +799,7 @@ add_win (Display *dpy, Window id, Window prev)
     {
        new->picture = 0;
        format = 0;
+       new->damage_sequence = 0;
     }
     else
     {
@@ -606,21 +808,56 @@ add_win (Display *dpy, Window id, Window prev)
                                             format,
                                             CPSubwindowMode,
                                             &pa);
+       new->damage_sequence = NextRequest (dpy);
+       new->damage = XDamageCreate (dpy, id, XDamageReportNonEmpty);
     }
                                         
     new->shadow = None;
+    new->shadow_dx = 0;
+    new->shadow_dy = 0;
+    new->shadow_width = 0;
+    new->shadow_height = 0;
     new->borderSize = None;
     new->extents = None;
-    if (format && format->type == PictTypeDirect && format->direct.alphaMask)
-       new->mode = WINDOW_ARGB;
-    else if (new->a.override_redirect)
-       new->mode = WINDOW_TRANS;
-    else
-       new->mode = WINDOW_SOLID;
+
+
+    /* moved mode setting to one place */
+    new->mode = determine_mode(dpy, new);
+
     new->next = *p;
     *p = new;
     if (new->a.map_state == IsViewable)
-       map_win (dpy, id);
+       map_win (dpy, id, new->damage_sequence - 1);
+}
+
+void
+restack_win (Display *dpy, win *w, Window new_above)
+{
+    Window  old_above;
+    
+    if (w->next)
+       old_above = w->next->id;
+    else
+       old_above = None;
+    if (old_above != new_above)
+    {
+       win **prev;
+
+       /* unhook */
+       for (prev = &list; *prev; prev = &(*prev)->next)
+           if ((*prev) == w)
+               break;
+       *prev = w->next;
+       
+       /* rehook */
+       for (prev = &list; *prev; prev = &(*prev)->next)
+       {
+           if ((*prev)->id == new_above)
+               break;
+       }
+       w->next = *prev;
+       *prev = w;
+    }
 }
 
 static void
@@ -648,7 +885,7 @@ configure_win (Display *dpy, XConfigureEvent *ce)
     {
        damage = XFixesCreateRegion (dpy, 0, 0);
        if (w->extents != None) 
-           XFixesUnionRegion (dpy, damage, w->extents, 0, 0, None, 0, 0);
+           XFixesCopyRegion (dpy, damage, w->extents);
     }
     w->a.x = ce->x;
     w->a.y = ce->y;
@@ -662,36 +899,29 @@ configure_win (Display *dpy, XConfigureEvent *ce)
     w->a.height = ce->height;
     w->a.border_width = ce->border_width;
     w->a.override_redirect = ce->override_redirect;
-    if (w->next)
-       above = w->next->id;
-    else
-       above = None;
-    if (above != ce->above)
-    {
-       win **prev;
-
-       /* unhook */
-       for (prev = &list; *prev; prev = &(*prev)->next)
-           if ((*prev) == w)
-               break;
-       *prev = w->next;
-       
-       /* rehook */
-       for (prev = &list; *prev; prev = &(*prev)->next)
-       {
-           if ((*prev)->id == ce->above)
-               break;
-       }
-       w->next = *prev;
-       *prev = w;
-    }
+    restack_win (dpy, w, ce->above);
     if (damage)
     {
        XserverRegion   extents = win_extents (dpy, w);
-       XFixesUnionRegion (dpy, damage, damage, 0, 0, extents, 0, 0);
+       XFixesUnionRegion (dpy, damage, damage, extents);
        XFixesDestroyRegion (dpy, extents);
        add_damage (dpy, damage);
     }
+    clipChanged = True;
+}
+
+static void
+circulate_win (Display *dpy, XCirculateEvent *ce)
+{
+    win            *w = find_win (dpy, ce->window);
+    Window  new_above;
+
+    if (ce->place == PlaceOnTop)
+       new_above = list->id;
+    else
+       new_above = None;
+    restack_win (dpy, w, new_above);
+    clipChanged = True;
 }
 
 static void
@@ -703,12 +933,12 @@ destroy_win (Display *dpy, Window id, Bool gone)
        if (w->id == id)
        {
            if (!gone)
-           {
                unmap_win (dpy, id);
-               if (w->picture)
-                   XRenderFreePicture (dpy, w->picture);
-           }
            *prev = w->next;
+           if (w->picture)
+               XRenderFreePicture (dpy, w->picture);
+           if (w->damage != None)
+               XDamageDestroy (dpy, w->damage);
            free (w);
            break;
        }
@@ -757,21 +987,63 @@ expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
     add_damage (dpy, region);
 }
 
-#define INTERVAL    0
 
-#if INTERVAL
 static int
-time_in_millis (void)
+ev_serial (XEvent *ev)
+{
+    if (ev->type & 0x7f != KeymapNotify)
+       return ev->xany.serial;
+    return NextRequest (ev->xany.display);
+}
+
+int                damage_event, damage_error;
+
+static char *
+ev_name (XEvent *ev)
 {
-    struct timeval  tp;
+    static char        buf[128];
+    switch (ev->type & 0x7f) {
+    case Expose:
+       return "Expose";
+    case MapNotify:
+       return "Map";
+    case UnmapNotify:
+       return "Unmap";
+    case ReparentNotify:
+       return "Reparent";
+    case CirculateNotify:
+       return "Circulate";
+    default:
+       if (ev->type == damage_event + XDamageNotify)
+           return "Damage";
+       sprintf (buf, "Event %d", ev->type);
+       return buf;
+    }
+}
 
-    gettimeofday (&tp, 0);
-    return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
+static Window
+ev_window (XEvent *ev)
+{
+    switch (ev->type) {
+    case Expose:
+       return ev->xexpose.window;
+    case MapNotify:
+       return ev->xmap.window;
+    case UnmapNotify:
+       return ev->xunmap.window;
+    case ReparentNotify:
+       return ev->xreparent.window;
+    case CirculateNotify:
+       return ev->xcirculate.window;
+    default:
+       if (ev->type == damage_event + XDamageNotify)
+           return ((XDamageNotifyEvent *) ev)->drawable;
+       return 0;
+    }
 }
-#endif
 
 int
-main (void)
+main (int argc, char **argv)
 {
     XEvent         ev;
     int                    event_base, error_base;
@@ -781,16 +1053,17 @@ main (void)
     Pixmap         blackPixmap;
     unsigned int    nchildren;
     int                    i;
-    int                    damage_event, damage_error;
     int                    xfixes_event, xfixes_error;
     XRenderPictureAttributes   pa;
     XRenderColor               c;
     XRectangle     *expose_rects = 0;
     int                    size_expose = 0;
     int                    n_expose = 0;
-#if INTERVAL
-    int                    timeout;
-#endif
+    struct pollfd   ufd;
+    int                    n;
+    int                    last_update;
+    int                    now;
+    int                    p;
 
     dpy = XOpenDisplay (0);
     if (!dpy)
@@ -799,11 +1072,19 @@ main (void)
        exit (1);
     }
     XSetErrorHandler (error);
+#if 0
+    XSynchronize (dpy, 1);
+#endif
     scr = DefaultScreen (dpy);
     root = RootWindow (dpy, scr);
+
+    /* get atoms */
+    transPropAtom = XInternAtom(dpy, TRANS_PROP, False);
+    intAtom = XInternAtom(dpy, "INTEGER", True);
+
     pa.subwindow_mode = IncludeInferiors;
 
-    gussianMap = make_gaussian_map(dpy, SHADOW_RADIUS);
+    gaussianMap = make_gaussian_map(dpy, SHADOW_RADIUS);
 
     transPixmap = XCreatePixmap (dpy, root, 1, 1, 8);
     pa.repeat = True;
@@ -814,10 +1095,10 @@ main (void)
     c.red = c.green = c.blue = 0;
     c.alpha = 0xc0c0;
     XRenderFillRectangle (dpy, PictOpSrc, transPicture, &c, 0, 0, 1, 1);
-    
+
     root_width = DisplayWidth (dpy, scr);
     root_height = DisplayHeight (dpy, scr);
-    
+
     rootPicture = XRenderCreatePicture (dpy, root, 
                                        XRenderFindVisualFormat (dpy,
                                                                 DefaultVisual (dpy, scr)),
@@ -848,6 +1129,7 @@ main (void)
        exit (1);
     }
     allDamage = None;
+    clipChanged = True;
     XGrabServer (dpy);
     XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
     XSelectInput (dpy, root, 
@@ -861,22 +1143,15 @@ main (void)
     XFree (children);
     XUngrabServer (dpy);
     paint_all (dpy, None);
-#if INTERVAL
-    last_update = time_in_millis ();
-#endif
     for (;;)
     {
-#if INTERVAL
-       int busy_start = 0;
-#endif
-/*     dump_wins (); */
+       /*      dump_wins (); */
        do {
            XNextEvent (dpy, &ev);
-#if INTERVAL
-           if (!busy_start)
-               busy_start = time_in_millis();
+#if DEBUG_EVENTS
+           printf ("event %10.10s serial 0x%08x window 0x%08x\n",
+                   ev_name(&ev), ev_serial (&ev), ev_window (&ev));
 #endif
-/*         printf ("event %d\n", ev.type); */
            switch (ev.type) {
            case CreateNotify:
                add_win (dpy, ev.xcreatewindow.window, 0);
@@ -888,7 +1163,7 @@ main (void)
                destroy_win (dpy, ev.xdestroywindow.window, True);
                break;
            case MapNotify:
-               map_win (dpy, ev.xmap.window);
+               map_win (dpy, ev.xmap.window, ev.xmap.serial);
                break;
            case UnmapNotify:
                unmap_win (dpy, ev.xunmap.window);
@@ -899,6 +1174,9 @@ main (void)
                else
                    destroy_win (dpy, ev.xreparent.window, False);
                break;
+           case CirculateNotify:
+               circulate_win (dpy, &ev.xcirculate);
+               break;
            case Expose:
                if (ev.xexpose.window == root)
                {
@@ -931,13 +1209,34 @@ main (void)
                }
                break;
            case PropertyNotify:
-               if (ev.xproperty.atom == XInternAtom (dpy, BACKGROUND_PROP, False))
+               for (p = 0; backgroundProps[p]; p++)
                {
-                   if (rootTile)
+                   if (ev.xproperty.atom == XInternAtom (dpy, backgroundProps[p], False))
                    {
-                       XClearArea (dpy, root, 0, 0, 0, 0, True);
-                       XRenderFreePicture (dpy, rootTile);
-                       rootTile = None;
+                       if (rootTile)
+                       {
+                           XClearArea (dpy, root, 0, 0, 0, 0, True);
+                           XRenderFreePicture (dpy, rootTile);
+                           rootTile = None;
+                           break;
+                       }
+                   }
+               }
+               /* check if Trans property was changed */
+               if (ev.xproperty.atom == transPropAtom)
+               {
+                   /* reset mode and redraw window */
+                   win * w = find_win(dpy, ev.xproperty.window);
+                   if (w)
+                   {
+                       w->mode = determine_mode(dpy, w);
+                       if (w->extents)
+                       {
+                           XserverRegion damage;
+                           damage = XFixesCreateRegion (dpy, 0, 0);
+                           XFixesCopyRegion (dpy, damage, w->extents);
+                           add_damage (dpy, damage);
+                       }
                    }
                }
                break;
@@ -946,29 +1245,15 @@ main (void)
                    damage_win (dpy, (XDamageNotifyEvent *) &ev);
                break;
            }
-       } while (XEventsQueued (dpy, QueuedAfterReading));
-#if INTERVAL
-       now = time_in_millis ();
-/*     printf ("\t\tbusy %d\n", now - busy_start); */
-       timeout = INTERVAL - (now - last_update);
-       if (timeout > 0)
-       {
-           ufd.fd = ConnectionNumber (dpy);
-           ufd.events = POLLIN;
-           n = poll (&ufd, 1, timeout);
-           if (n > 0 && (ufd.revents & POLLIN) && XEventsQueued (dpy, QueuedAfterReading))
-               continue;
-       }
-#endif
+       } while (QLength (dpy));
        if (allDamage)
        {
-#if INTERVAL
-           int old_update = last_update;
-           last_update = time_in_millis();
-/*         printf ("delta %d\n", last_update - old_update); */
-#endif
+           static int  paint;
            paint_all (dpy, allDamage);
+           paint++;
+           XSync (dpy, False);
            allDamage = None;
+           clipChanged = False;
        }
     }
 }