Make a dummy window with a name, so xrestop can point its finger at
[dana/xcompmgr.git] / xcompmgr.c
index 8bb23d1..9f90595 100644 (file)
@@ -47,6 +47,8 @@
 #define HAS_NAME_WINDOW_PIXMAP 1
 #endif
 
+#define CAN_DO_USABLE 0
+
 typedef struct _ignore {
     struct _ignore     *next;
     unsigned long      sequence;
@@ -59,6 +61,10 @@ typedef struct _win {
     Pixmap             pixmap;
 #endif
     XWindowAttributes  a;
+#if CAN_DO_USABLE
+    Bool               usable;             /* mapped and all damaged at one point */
+    XRectangle         damage_bounds;      /* bounds of damage */
+#endif
     int                        mode;
     int                        damaged;
     Damage             damage;
@@ -73,7 +79,7 @@ typedef struct _win {
     int                        shadow_width;
     int                        shadow_height;
     unsigned int       opacity;
-
+    Atom                windowType;
     unsigned long      damage_sequence;    /* sequence when damage was created */
 
     /* for drawing translucent windows */
@@ -90,6 +96,7 @@ typedef struct _fade {
     struct _fade       *next;
     win                        *w;
     double             cur;
+    double             finish;
     double             step;
     void               (*callback) (Display *dpy, win *w, Bool gone);
     Display            *dpy;
@@ -117,9 +124,20 @@ int                xfixes_event, xfixes_error;
 int            damage_event, damage_error;
 int            composite_event, composite_error;
 int            render_event, render_error;
+Bool           synchronize;
+int            composite_opcode;
 
 /* find these once and be done with it */
 Atom           opacityAtom;
+Atom            winTypeAtom;
+Atom            winDesktopAtom;
+Atom            winDockAtom;
+Atom            winToolbarAtom;
+Atom            winMenuAtom;
+Atom            winUtilAtom;
+Atom            winSplashAtom;
+Atom            winDialogAtom;
+Atom            winNormalAtom;
 
 /* opacity property name; sometime soon I'll write up an EWMH spec for it */
 #define OPACITY_PROP   "_NET_WM_WINDOW_OPACITY"
@@ -151,22 +169,40 @@ typedef enum _compMode {
 static void
 determine_mode(Display *dpy, win *w);
     
+static double
+get_opacity_percent(Display *dpy, win *w, double def);
+
+static XserverRegion
+win_extents (Display *dpy, win *w);
+
 CompMode    compMode = CompSimple;
 
 int        shadowRadius = 12;
+int         shadowOffsetX = -15;
+int         shadowOffsetY = -15;
+double      shadowOpacity = .75;
 
-double fade_step =     0.05;
+double  fade_in_step =  0.028;
+double  fade_out_step = 0.03;
 int    fade_delta =    10;
 int    fade_time =     0;
-Bool   fadeWindows;
+Bool   fadeWindows = False;
+Bool    excludeDockShadows = False;
+Bool   fadeTrans = False;
+
+Bool   autoRedirect = False;
+
+/* For shadow precomputation */
+int            Gsize = -1;
+unsigned char *shadowCorner = NULL;
+unsigned char *shadowTop = NULL;
 
 int
 get_time_in_milliseconds ()
 {
     struct timeval  tv;
-    struct timezone tz;
 
-    gettimeofday (&tv, &tz);
+    gettimeofday (&tv, NULL);
     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 
@@ -217,9 +253,9 @@ enqueue_fade (Display *dpy, fade *f)
 }
 
 static void
-set_fade (Display *dpy, win *w, Bool in, 
+set_fade (Display *dpy, win *w, double start, double finish, double step,
          void (*callback) (Display *dpy, win *w, Bool gone),
-         Bool gone)
+         Bool gone, Bool exec_callback, Bool override)
 {
     fade    *f;
 
@@ -229,16 +265,27 @@ set_fade (Display *dpy, win *w, Bool in,
        f = malloc (sizeof (fade));
        f->next = 0;
        f->w = w;
-       if (in)
-           f->cur = 0;
-       else
-           f->cur = 1;
+       f->cur = start;
        enqueue_fade (dpy, f);
     }
-    if (in)
-        f->step = fade_step;
+    else if(!override)
+       return;
     else
-       f->step = -fade_step;
+    {
+       if (exec_callback)
+           if (f->callback)
+               (*f->callback)(dpy, f->w, f->gone);
+    }
+
+    if (finish < 0)
+       finish = 0;
+    if (finish > 1)
+       finish = 1;
+    f->finish = finish;
+    if (f->cur < finish)
+        f->step = step;
+    else if (f->cur > finish)
+       f->step = -step;
     f->callback = callback;
     f->gone = gone;
     w->opacity = f->cur * OPAQUE;
@@ -246,6 +293,12 @@ set_fade (Display *dpy, win *w, Bool in,
     printf ("set_fade start %g step %g\n", f->cur, f->step);
 #endif
     determine_mode (dpy, w);
+    if (w->shadow)
+    {
+       XRenderFreePicture (dpy, w->shadow);
+       w->shadow = None;
+       w->extents = win_extents (dpy, w);
+    }
 }
 
 int
@@ -269,6 +322,7 @@ run_fades (Display *dpy)
     int            now = get_time_in_milliseconds();
     fade    *f, *next;
     int            steps;
+    Bool    need_dequeue;
 
 #if 0
     printf ("run fades\n");
@@ -289,25 +343,37 @@ run_fades (Display *dpy)
        printf ("opacity now %g\n", f->cur);
 #endif
        w->opacity = f->cur * OPAQUE;
+       need_dequeue = False;
        if (f->step > 0)
        {
-           if (f->cur >= 1)
-               dequeue_fade (dpy, f);
+           if (f->cur >= f->finish)
+           {
+               w->opacity = f->finish*OPAQUE;
+               need_dequeue = True;
+           }
        }
        else
        {
-           if (f->cur <= 0)
-               dequeue_fade (dpy, f);
+           if (f->cur <= f->finish)
+           {
+               w->opacity = f->finish*OPAQUE;
+               need_dequeue = True;
+           }
        }
        determine_mode (dpy, w);
+       if (w->shadow)
+       {
+           XRenderFreePicture (dpy, w->shadow);
+           w->shadow = None;
+           w->extents = win_extents(dpy, w);
+       }
+       /* Must do this last as it might destroy f->w in callbacks */
+       if (need_dequeue)
+               dequeue_fade (dpy, f);
     }
     fade_time = now + fade_delta;
 }
 
-#define SHADOW_OPACITY 0.75
-#define SHADOW_OFFSET_X        (-shadowRadius * 5 / 4)
-#define SHADOW_OFFSET_Y        (-shadowRadius * 5 / 4)
-
 static double
 gaussian (double r, double x, double y)
 {
@@ -416,6 +482,42 @@ sum_gaussian (conv *map, double opacity, int x, int y, int width, int height)
     return ((unsigned char) (v * opacity * 255.0));
 }
 
+/* precompute shadow corners and sides to save time for large windows */
+static void
+presum_gaussian (conv *map)
+{
+    int center = map->size/2;
+    int opacity, x, y;
+
+    Gsize = map->size;
+
+    if (shadowCorner)
+       free ((void *)shadowCorner);
+    if (shadowTop)
+       free ((void *)shadowTop);
+
+    shadowCorner = (unsigned char *)(malloc ((Gsize + 1) * (Gsize + 1) * 26));
+    shadowTop = (unsigned char *)(malloc ((Gsize + 1) * 26));
+    
+    for (x = 0; x <= Gsize; x++)
+    {
+       shadowTop[25 * (Gsize + 1) + x] = sum_gaussian (map, 1, x - center, center, Gsize * 2, Gsize * 2);
+       for(opacity = 0; opacity < 25; opacity++)
+           shadowTop[opacity * (Gsize + 1) + x] = shadowTop[25 * (Gsize + 1) + x] * opacity / 25;
+       for(y = 0; y <= x; y++)
+       {
+           shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
+               = sum_gaussian (map, 1, x - center, y - center, Gsize * 2, Gsize * 2);
+           shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
+               = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
+           for(opacity = 0; opacity < 25; opacity++)
+               shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x]
+                   = shadowCorner[opacity * (Gsize + 1) * (Gsize + 1) + x * (Gsize + 1) + y]
+                   = shadowCorner[25 * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x] * opacity / 25;
+       }
+    }
+}
+
 static XImage *
 make_shadow (Display *dpy, double opacity, int width, int height)
 {
@@ -429,7 +531,7 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     int                    x, y;
     unsigned char   d;
     int                    x_diff;
-    
+    int             opacity_int = (int)(opacity * 25);
     data = malloc (swidth * sheight * sizeof (unsigned char));
     if (!data)
        return 0;
@@ -452,8 +554,10 @@ 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);
+    if (Gsize > 0)
+       d = shadowTop[opacity_int * (Gsize + 1) + Gsize];
+    else
+       d = sum_gaussian (gaussianMap, opacity, center, center, width, height);
     memset(data, d, sheight * swidth);
     
     /*
@@ -469,7 +573,10 @@ 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 (gaussianMap, opacity, x - center, y - center, width, height);
+           if (xlimit == Gsize && ylimit == Gsize)
+               d = shadowCorner[opacity_int * (Gsize + 1) * (Gsize + 1) + y * (Gsize + 1) + x];
+           else
+               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;
@@ -484,7 +591,10 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     {
        for (y = 0; y < ylimit; y++)
        {
-           d = sum_gaussian (gaussianMap, opacity, center, y - center, width, height);
+           if (ylimit == Gsize)
+               d = shadowTop[opacity_int * (Gsize + 1) + y];
+           else
+               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);
        }
@@ -496,7 +606,10 @@ make_shadow (Display *dpy, double opacity, int width, int height)
     
     for (x = 0; x < xlimit; x++)
     {
-       d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
+       if (xlimit == Gsize)
+           d = shadowTop[opacity_int * (Gsize + 1) + x];
+       else
+           d = sum_gaussian (gaussianMap, opacity, x - center, center, width, height);
        for (y = gsize; y < sheight - gsize; y++)
        {
            data[y * swidth + x] = d;
@@ -508,11 +621,13 @@ make_shadow (Display *dpy, double opacity, int width, int height)
 }
 
 static Picture
-shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, int *hp)
+shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int height, int *wp, int *hp)
 {
     XImage  *shadowImage;
     Pixmap  shadowPixmap;
+    Pixmap  finalPixmap;
     Picture shadowPicture;
+    Picture finalPicture;
     GC     gc;
     
     shadowImage = make_shadow (dpy, opacity, width, height);
@@ -522,10 +637,30 @@ shadow_picture (Display *dpy, double opacity, int width, int height, int *wp, in
                                  shadowImage->width,
                                  shadowImage->height,
                                  8);
+    if (!shadowPixmap)
+    {
+       XDestroyImage (shadowImage);
+       return None;
+    }
+
     shadowPicture = XRenderCreatePicture (dpy, shadowPixmap,
                                          XRenderFindStandardFormat (dpy, PictStandardA8),
                                          0, 0);
+    if (!shadowPicture)
+    {
+       XDestroyImage (shadowImage);
+       XFreePixmap (dpy, shadowPixmap);
+       return None;
+    }
+
     gc = XCreateGC (dpy, shadowPixmap, 0, 0);
+    if (!gc)
+    {
+       XDestroyImage (shadowImage);
+       XFreePixmap (dpy, shadowPixmap);
+       XRenderFreePicture (dpy, shadowPicture);
+       return None;
+    }
     
     XPutImage (dpy, shadowPixmap, gc, shadowImage, 0, 0, 0, 0, 
               shadowImage->width,
@@ -547,11 +682,20 @@ solid_picture (Display *dpy, Bool argb, double a, double r, double g, double b)
     XRenderColor               c;
 
     pixmap = XCreatePixmap (dpy, root, 1, 1, argb ? 32 : 8);
+    if (!pixmap)
+       return None;
+
     pa.repeat = True;
     picture = XRenderCreatePicture (dpy, pixmap,
                                    XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8),
                                    CPRepeat,
                                    &pa);
+    if (!picture)
+    {
+       XFreePixmap (dpy, pixmap);
+       return None;
+    }
+
     c.alpha = a * 0xffff;
     c.red = r * 0xffff;
     c.green = g * 0xffff;
@@ -685,7 +829,7 @@ win_extents (Display *dpy, win *w)
     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 (compMode != CompSimple)
+    if (compMode != CompSimple && !(w->windowType == winDockAtom && excludeDockShadows))
     {
        if (compMode == CompServerShadows || w->mode != WINDOW_ARGB)
        {
@@ -700,14 +844,14 @@ win_extents (Display *dpy, win *w)
            }
            else
            {
-               w->shadow_dx = SHADOW_OFFSET_X;
-               w->shadow_dy = SHADOW_OFFSET_Y;
+               w->shadow_dx = shadowOffsetX;
+               w->shadow_dy = shadowOffsetY;
                if (!w->shadow)
                {
-                   double      opacity = SHADOW_OPACITY;
+                   double      opacity = shadowOpacity;
                    if (w->mode == WINDOW_TRANS)
-                       opacity = opacity * TRANS_OPACITY;
-                   w->shadow = shadow_picture (dpy, opacity,
+                       opacity = opacity * ((double)w->opacity)/((double)OPAQUE);
+                   w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
                                                w->a.width + w->a.border_width * 2,
                                                w->a.height + w->a.border_width * 2,
                                                &w->shadow_width, &w->shadow_height);
@@ -796,11 +940,17 @@ paint_all (Display *dpy, XserverRegion region)
 #endif
     for (w = list; w; w = w->next)
     {
-       if (w->a.map_state != IsViewable)
+#if CAN_DO_USABLE
+       if (!w->usable)
            continue;
+#endif
        /* never painted, ignore it */
        if (!w->damaged)
            continue;
+       /* if invisible, ignore it */
+       if (w->a.x + w->a.width < 1 || w->a.y + w->a.height < 1
+           || w->a.x >= root_width || w->a.y >= root_height)
+           continue;
        if (!w->picture)
        {
            XRenderPictureAttributes    pa;
@@ -848,16 +998,25 @@ paint_all (Display *dpy, XserverRegion region)
            w->extents = win_extents (dpy, w);
        if (w->mode == WINDOW_SOLID)
        {
+           int x, y, wid, hei;
+#if HAS_NAME_WINDOW_PIXMAP
+           x = w->a.x;
+           y = w->a.y;
+           wid = w->a.width + w->a.border_width * 2;
+           hei = w->a.height + w->a.border_width * 2;
+#else
+           x = w->a.x + w->a.border_width;
+           y = w->a.y + w->a.border_width;
+           wid = w->a.width;
+           hei = w->a.height;
+#endif
            XFixesSetPictureClipRegion (dpy, rootBuffer, 0, 0, region);
            set_ignore (dpy, NextRequest (dpy));
            XFixesSubtractRegion (dpy, region, region, w->borderSize);
            set_ignore (dpy, NextRequest (dpy));
            XRenderComposite (dpy, PictOpSrc, w->picture, None, rootBuffer,
                              0, 0, 0, 0, 
-                             w->a.x + w->a.border_width,
-                             w->a.y + w->a.border_width,
-                             w->a.width,
-                             w->a.height);
+                             x, y, wid, hei);
        }
        if (!w->borderClip)
        {
@@ -880,6 +1039,9 @@ paint_all (Display *dpy, XserverRegion region)
        case CompSimple:
            break;
        case CompServerShadows:
+           /* dont' bother drawing shadows on desktop windows */
+           if (w->windowType == winDesktopAtom)
+               break;
            set_ignore (dpy, NextRequest (dpy));
            if (w->opacity != OPAQUE && !w->shadowPict)
                w->shadowPict = solid_picture (dpy, True,
@@ -894,7 +1056,8 @@ paint_all (Display *dpy, XserverRegion region)
                              w->shadow_width, w->shadow_height);
            break;
        case CompClientShadows:
-           if (w->shadow)
+           /* don't bother drawing shadows on desktop windows */
+           if (w->shadow && w->windowType != winDesktopAtom)
            {
                XRenderComposite (dpy, PictOpOver, blackPicture, w->shadow, rootBuffer,
                                  0, 0, 0, 0,
@@ -909,23 +1072,41 @@ paint_all (Display *dpy, XserverRegion region)
                                          (double) w->opacity / OPAQUE, 0, 0, 0);
        if (w->mode == WINDOW_TRANS)
        {
+           int x, y, wid, hei;
+#if HAS_NAME_WINDOW_PIXMAP
+           x = w->a.x;
+           y = w->a.y;
+           wid = w->a.width + w->a.border_width * 2;
+           hei = w->a.height + w->a.border_width * 2;
+#else
+           x = w->a.x + w->a.border_width;
+           y = w->a.y + w->a.border_width;
+           wid = w->a.width;
+           hei = w->a.height;
+#endif
            set_ignore (dpy, NextRequest (dpy));
            XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
                              0, 0, 0, 0, 
-                             w->a.x + w->a.border_width,
-                             w->a.y + w->a.border_width,
-                             w->a.width,
-                             w->a.height);
+                             x, y, wid, hei);
        }
        else if (w->mode == WINDOW_ARGB)
        {
+           int x, y, wid, hei;
+#if HAS_NAME_WINDOW_PIXMAP
+           x = w->a.x;
+           y = w->a.y;
+           wid = w->a.width + w->a.border_width * 2;
+           hei = w->a.height + w->a.border_width * 2;
+#else
+           x = w->a.x + w->a.border_width;
+           y = w->a.y + w->a.border_width;
+           wid = w->a.width;
+           hei = w->a.height;
+#endif
            set_ignore (dpy, NextRequest (dpy));
            XRenderComposite (dpy, PictOpOver, w->picture, w->alphaPict, rootBuffer,
                              0, 0, 0, 0, 
-                             w->a.x + w->a.border_width,
-                             w->a.y + w->a.border_width,
-                             w->a.width,
-                             w->a.height);
+                             x, y, wid, hei);
        }
        XFixesDestroyRegion (dpy, w->borderClip);
        w->borderClip = None;
@@ -952,13 +1133,10 @@ add_damage (Display *dpy, XserverRegion damage)
 }
 
 static void
-repair_win (Display *dpy, Window id)
+repair_win (Display *dpy, win *w)
 {
-    win                    *w = find_win (dpy, id);
     XserverRegion   parts;
 
-    if (!w)
-       return;
     if (!w->damaged)
     {
        parts = win_extents (dpy, w);
@@ -995,22 +1173,29 @@ map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
 
     if (!w)
        return;
-    w->a.map_state = IsViewable;
 
-    /* make sure we know if property was changed */
-    XSelectInput(dpy, id, PropertyChangeMask);
+    w->a.map_state = IsViewable;
+    
+    /* This needs to be here or else we lose transparency messages */
+    XSelectInput (dpy, id, PropertyChangeMask);
 
+#if CAN_DO_USABLE
+    w->damage_bounds.x = w->damage_bounds.y = 0;
+    w->damage_bounds.width = w->damage_bounds.height = 0;
+#endif
     w->damaged = 0;
-    clipChanged = True;
+
     if (fade && fadeWindows)
-       set_fade (dpy, w, True, 0, False);
+       set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
 }
 
 static void
 finish_unmap_win (Display *dpy, win *w)
 {
-    w->a.map_state = IsUnmapped;
     w->damaged = 0;
+#if CAN_DO_USABLE
+    w->usable = False;
+#endif
     if (w->extents != None)
     {
        add_damage (dpy, w->extents);    /* destroys region */
@@ -1070,9 +1255,10 @@ unmap_win (Display *dpy, Window id, Bool fade)
     win *w = find_win (dpy, id);
     if (!w)
        return;
+    w->a.map_state = IsUnmapped;
 #if HAS_NAME_WINDOW_PIXMAP
     if (w->pixmap && fade && fadeWindows)
-       set_fade (dpy, w, False, unmap_callback, False);
+       set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, unmap_callback, False, False, True);
     else
 #endif
        finish_unmap_win (dpy, w);
@@ -1089,11 +1275,11 @@ get_opacity_prop(Display *dpy, win *w, unsigned int def)
     int format;
     unsigned long n, left;
 
-    char *data;
-    XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False, 
+    unsigned char *data;
+    int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False, 
                       XA_CARDINAL, &actual, &format, 
-                      &n, &left, (unsigned char **) &data);
-    if (data != None)
+                                   &n, &left, &data);
+    if (result == Success && data != NULL)
     {
        unsigned int i;
        memcpy (&i, data, sizeof (unsigned int));
@@ -1103,10 +1289,44 @@ get_opacity_prop(Display *dpy, win *w, unsigned int def)
     return def;
 }
 
+/* Get the opacity property from the window in a percent format
+   not found: default
+   otherwise: the value
+*/
+static double
+get_opacity_percent(Display *dpy, win *w, double def)
+{
+    unsigned int opacity = get_opacity_prop (dpy, w, (unsigned int)(OPAQUE*def));
+
+    return opacity*1.0/OPAQUE;
+}
+
 /* determine mode for window all in one place.
    Future might check for menu flag and other cool things
 */
 
+static Atom
+get_wintype_prop(Display * dpy, Window w)
+{
+    Atom actual;
+    int format;
+    unsigned long n, left;
+
+    unsigned char *data;
+    int result = XGetWindowProperty (dpy, w, winTypeAtom, 0L, 1L, False,
+                                    XA_ATOM, &actual, &format,
+                                    &n, &left, &data);
+
+    if (result == Success && data != None)
+    {
+       Atom a;
+       memcpy (&a, data, sizeof (Atom));
+       XFree ( (void *) data);
+       return a;
+    }
+    return winNormalAtom;
+}
+
 static void
 determine_mode(Display *dpy, win *w)
 {
@@ -1158,6 +1378,40 @@ determine_mode(Display *dpy, win *w)
     }
 }
 
+static Atom
+determine_wintype (Display *dpy, Window w)
+{
+    Window       root_return, parent_return;
+    Window      *children = NULL;
+    unsigned int nchildren, i;
+    Atom         type;
+
+    type = get_wintype_prop (dpy, w);
+    if (type != winNormalAtom)
+       return type;
+
+    if (!XQueryTree (dpy, w, &root_return, &parent_return, &children,
+                           &nchildren))
+    {
+       /* XQueryTree failed. */
+       if (children)
+           XFree ((void *)children);
+       return winNormalAtom;
+    }
+
+    for (i = 0;i < nchildren;i++)
+    {
+       type = determine_wintype (dpy, children[i]);
+       if (type != winNormalAtom)
+           return type;
+    }
+
+    if (children)
+       XFree ((void *)children);
+
+    return winNormalAtom;
+}
+
 static void
 add_win (Display *dpy, Window id, Window prev)
 {
@@ -1182,6 +1436,9 @@ add_win (Display *dpy, Window id, Window prev)
        return;
     }
     new->damaged = 0;
+#if CAN_DO_USABLE
+    new->usable = False;
+#endif
 #if HAS_NAME_WINDOW_PIXMAP
     new->pixmap = None;
 #endif
@@ -1211,13 +1468,14 @@ add_win (Display *dpy, Window id, Window prev)
     new->prev_trans = 0;
 
     /* moved mode setting to one place */
-    new->opacity = get_opacity_prop(dpy, new, OPAQUE);
+    new->opacity = get_opacity_prop (dpy, new, OPAQUE);
+    new->windowType = determine_wintype (dpy, new->id);
     determine_mode (dpy, new);
     
     new->next = *p;
     *p = new;
     if (new->a.map_state == IsViewable)
-       map_win (dpy, id, new->damage_sequence - 1, False);
+       map_win (dpy, id, new->damage_sequence - 1, True);
 }
 
 void
@@ -1271,7 +1529,9 @@ configure_win (Display *dpy, XConfigureEvent *ce)
        }
        return;
     }
-    if (w->a.map_state == IsViewable)
+#if CAN_DO_USABLE
+    if (w->usable)
+#endif
     {
        damage = XFixesCreateRegion (dpy, 0, 0);
        if (w->extents != None) 
@@ -1320,6 +1580,9 @@ circulate_win (Display *dpy, XCirculateEvent *ce)
     win            *w = find_win (dpy, ce->window);
     Window  new_above;
 
+    if (!w)
+       return;
+
     if (ce->place == PlaceOnTop)
        new_above = list->id;
     else
@@ -1343,6 +1606,7 @@ finish_destroy_win (Display *dpy, Window id, Bool gone)
            {
                set_ignore (dpy, NextRequest (dpy));
                XRenderFreePicture (dpy, w->picture);
+               w->picture = None;
            }
            if (w->alphaPict)
            {
@@ -1358,6 +1622,7 @@ finish_destroy_win (Display *dpy, Window id, Bool gone)
            {
                set_ignore (dpy, NextRequest (dpy));
                XDamageDestroy (dpy, w->damage);
+               w->damage = None;
            }
            cleanup_fade (dpy, w);
            free (w);
@@ -1379,7 +1644,7 @@ destroy_win (Display *dpy, Window id, Bool gone, Bool fade)
     win *w = find_win (dpy, id);
 #if HAS_NAME_WINDOW_PIXMAP
     if (w && w->pixmap && fade && fadeWindows)
-       set_fade (dpy, w, False, destroy_callback, gone);
+       set_fade (dpy, w, w->opacity*1.0/OPAQUE, 0.0, fade_out_step, destroy_callback, gone, False, True);
     else
 #endif
     {
@@ -1410,7 +1675,59 @@ dump_wins (void)
 static void
 damage_win (Display *dpy, XDamageNotifyEvent *de)
 {
-    repair_win (dpy, de->drawable);
+    win        *w = find_win (dpy, de->drawable);
+
+    if (!w)
+       return;
+#if CAN_DO_USABLE
+    if (!w->usable)
+    {
+       if (w->damage_bounds.width == 0 || w->damage_bounds.height == 0)
+       {
+           w->damage_bounds = de->area;
+       }
+       else
+       {
+           if (de->area.x < w->damage_bounds.x)
+           {
+               w->damage_bounds.width += (w->damage_bounds.x - de->area.x);
+               w->damage_bounds.x = de->area.x;
+           }
+           if (de->area.y < w->damage_bounds.y)
+           {
+               w->damage_bounds.height += (w->damage_bounds.y - de->area.y);
+               w->damage_bounds.y = de->area.y;
+           }
+           if (de->area.x + de->area.width > w->damage_bounds.x + w->damage_bounds.width)
+               w->damage_bounds.width = de->area.x + de->area.width - w->damage_bounds.x;
+           if (de->area.y + de->area.height > w->damage_bounds.y + w->damage_bounds.height)
+               w->damage_bounds.height = de->area.y + de->area.height - w->damage_bounds.y;
+       }
+#if 0
+       printf ("unusable damage %d, %d: %d x %d bounds %d, %d: %d x %d\n",
+               de->area.x,
+               de->area.y,
+               de->area.width,
+               de->area.height,
+               w->damage_bounds.x,
+               w->damage_bounds.y,
+               w->damage_bounds.width,
+               w->damage_bounds.height);
+#endif
+       if (w->damage_bounds.x <= 0 && 
+           w->damage_bounds.y <= 0 &&
+           w->a.width <= w->damage_bounds.x + w->damage_bounds.width &&
+           w->a.height <= w->damage_bounds.y + w->damage_bounds.height)
+       {
+           clipChanged = True;
+           if (fadeWindows)
+               set_fade (dpy, w, 0, get_opacity_percent (dpy, w, 1.0), fade_in_step, 0, False, True, True);
+           w->usable = True;
+       }
+    }
+    if (w->usable)
+#endif
+       repair_win (dpy, w);
 }
 
 static int
@@ -1422,6 +1739,12 @@ error (Display *dpy, XErrorEvent *ev)
     if (should_ignore (dpy, ev->serial))
        return 0;
     
+    if (ev->request_code == composite_opcode &&
+       ev->minor_code == X_CompositeRedirectSubwindows)
+    {
+       fprintf (stderr, "Another composite manager is already running\n");
+       exit (1);
+    }
     
     o = ev->error_code - xfixes_error;
     switch (o) {
@@ -1446,7 +1769,7 @@ error (Display *dpy, XErrorEvent *ev)
     printf ("error %d request %d minor %d serial %d\n",
            ev->error_code, ev->request_code, ev->minor_code, ev->serial);
 
-    abort ();
+/*    abort ();            this is just annoying to most people */
     return 0;
 }
 
@@ -1515,10 +1838,40 @@ ev_window (XEvent *ev)
 void
 usage (char *program)
 {
-    fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c]\n", program);
+    fprintf (stderr, "%s v1.1.2\n", program);
+    fprintf (stderr, "usage: %s [options]\n", program);
+    fprintf (stderr, "Options\n");
+    fprintf (stderr, "   -d display\n      Specifies which display should be managed.\n");
+    fprintf (stderr, "   -r radius\n      Specifies the blur radius for client-side shadows. (default 12)\n");
+    fprintf (stderr, "   -o opacity\n      Specifies the translucency for client-side shadows. (default .75)\n");
+    fprintf (stderr, "   -l left-offset\n      Specifies the left offset for client-side shadows. (default -15)\n");
+    fprintf (stderr, "   -t top-offset\n      Specifies the top offset for clinet-side shadows. (default -15)\n");
+    fprintf (stderr, "   -I fade-in-step\n      Specifies the opacity change between steps while fading in. (default 0.028)\n");
+    fprintf (stderr, "   -O fade-out-step\n      Specifies the opacity change between steps while fading out. (default 0.03)\n");
+    fprintf (stderr, "   -D fade-delta-time\n      Specifies the time between steps in a fade in milliseconds. (default 10)\n");
+    fprintf (stderr, "   -a\n      Use automatic server-side compositing. Faster, but no special effects.\n");
+    fprintf (stderr, "   -c\n      Draw client-side shadows with fuzzy edges.\n");
+    fprintf (stderr, "   -C\n      Avoid drawing shadows on dock/panel windows.\n");
+    fprintf (stderr, "   -f\n      Fade windows in/out when opening/closing.\n");
+    fprintf (stderr, "   -F\n      Fade windows during opacity changes.\n");
+    fprintf (stderr, "   -n\n      Normal client-side compositing with transparency support\n");
+    fprintf (stderr, "   -s\n      Draw server-side shadows with sharp edges.\n");
+    fprintf (stderr, "   -S\n      Enable synchronous operation (for debugging).\n");
     exit (1);
 }
 
+static void
+give_me_a_name (void)
+{
+    Window w;
+
+    w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
+                            None);
+
+    Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
+                         NULL);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -1543,24 +1896,63 @@ main (int argc, char **argv)
     char           *display = 0;
     int                    o;
 
-    while ((o = getopt (argc, argv, "d:scnf")) != -1)
+    while ((o = getopt (argc, argv, "D:I:O:d:r:o:l:t:scnfFCaS")) != -1)
     {
        switch (o) {
        case 'd':
            display = optarg;
            break;
+       case 'D':
+           fade_delta = atoi (optarg);
+           if (fade_delta < 1)
+               fade_delta = 10;
+           break;
+       case 'I':
+           fade_in_step = atof (optarg);
+           if (fade_in_step <= 0)
+               fade_in_step = 0.01;
+           break;
+       case 'O':
+           fade_out_step = atof (optarg);
+           if (fade_out_step <= 0)
+               fade_out_step = 0.01;
+           break;
        case 's':
            compMode = CompServerShadows;
            break;
        case 'c':
            compMode = CompClientShadows;
            break;
+       case 'C':
+           excludeDockShadows = True;
+           break;
        case 'n':
            compMode = CompSimple;
            break;
        case 'f':
            fadeWindows = True;
            break;
+       case 'F':
+           fadeTrans = True;
+           break;
+       case 'a':
+           autoRedirect = True;
+           break;
+       case 'S':
+           synchronize = True;
+           break;
+       case 'r':
+           shadowRadius = atoi (optarg);
+           break;
+       case 'o':
+           shadowOpacity = atof (optarg);
+           break;
+       case 'l':
+           shadowOffsetX = atoi (optarg);
+           break;
+       case 't':
+           shadowOffsetY = atoi (optarg);
+           break;
        default:
            usage (argv[0]);
            break;
@@ -1574,9 +1966,8 @@ main (int argc, char **argv)
        exit (1);
     }
     XSetErrorHandler (error);
-#if 0
-    XSynchronize (dpy, 1);
-#endif
+    if (synchronize)
+       XSynchronize (dpy, 1);
     scr = DefaultScreen (dpy);
     root = RootWindow (dpy, scr);
 
@@ -1585,7 +1976,8 @@ main (int argc, char **argv)
        fprintf (stderr, "No render extension\n");
        exit (1);
     }
-    if (!XCompositeQueryExtension (dpy, &composite_event, &composite_error))
+    if (!XQueryExtension (dpy, COMPOSITE_NAME, &composite_opcode,
+                         &composite_event, &composite_error))
     {
        fprintf (stderr, "No composite extension\n");
        exit (1);
@@ -1606,13 +1998,28 @@ main (int argc, char **argv)
        fprintf (stderr, "No XFixes extension\n");
        exit (1);
     }
+
+    give_me_a_name();
+
     /* get atoms */
     opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
+    winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
+    winDesktopAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DESKTOP", False);
+    winDockAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DOCK", False);
+    winToolbarAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR", False);
+    winMenuAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_MENU", False);
+    winUtilAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_UTILITY", False);
+    winSplashAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_SPLASH", False);
+    winDialogAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
+    winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
 
     pa.subwindow_mode = IncludeInferiors;
 
     if (compMode == CompClientShadows)
+    {
        gaussianMap = make_gaussian_map(dpy, shadowRadius);
+       presum_gaussian (gaussianMap);
+    }
 
     root_width = DisplayWidth (dpy, scr);
     root_height = DisplayHeight (dpy, scr);
@@ -1628,24 +2035,32 @@ main (int argc, char **argv)
     allDamage = None;
     clipChanged = True;
     XGrabServer (dpy);
-    XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
-    XSelectInput (dpy, root, 
-                 SubstructureNotifyMask|
-                 ExposureMask|
-                 StructureNotifyMask|
-                 PropertyChangeMask);
-    XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
-    for (i = 0; i < nchildren; i++)
-       add_win (dpy, children[i], i ? children[i-1] : None);
-    XFree (children);
+    if (autoRedirect)
+       XCompositeRedirectSubwindows (dpy, root, CompositeRedirectAutomatic);
+    else
+    {
+       XCompositeRedirectSubwindows (dpy, root, CompositeRedirectManual);
+       XSelectInput (dpy, root, 
+                     SubstructureNotifyMask|
+                     ExposureMask|
+                     StructureNotifyMask|
+                     PropertyChangeMask);
+       XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren);
+       for (i = 0; i < nchildren; i++)
+           add_win (dpy, children[i], i ? children[i-1] : None);
+       XFree (children);
+    }
     XUngrabServer (dpy);
     ufd.fd = ConnectionNumber (dpy);
     ufd.events = POLLIN;
-    paint_all (dpy, None);
+    if (!autoRedirect)
+       paint_all (dpy, None);
     for (;;)
     {
        /*      dump_wins (); */
        do {
+           if (autoRedirect)
+               XFlush (dpy);
             if (!QLength (dpy))
             {
                 if (poll (&ufd, 1, fade_timeout()) == 0)
@@ -1662,7 +2077,7 @@ main (int argc, char **argv)
            printf ("event %10.10s serial 0x%08x window 0x%08x\n",
                    ev_name(&ev), ev_serial (&ev), ev_window (&ev));
 #endif
-           switch (ev.type) {
+           if (!autoRedirect) switch (ev.type) {
            case CreateNotify:
                add_win (dpy, ev.xcreatewindow.window, 0);
                break;
@@ -1739,8 +2154,20 @@ main (int argc, char **argv)
                    win * w = find_win(dpy, ev.xproperty.window);
                    if (w)
                    {
+                       if (fadeTrans)
+                           set_fade (dpy, w, w->opacity*1.0/OPAQUE, get_opacity_percent (dpy, w, 1.0),
+                                     fade_out_step, 0, False, True, False);
+                       else
+                       {
                        w->opacity = get_opacity_prop(dpy, w, OPAQUE);
                        determine_mode(dpy, w);
+                           if (w->shadow)
+                           {
+                               XRenderFreePicture (dpy, w->shadow);
+                               w->shadow = None;
+                               w->extents = win_extents (dpy, w);
+                           }
+                       }
                    }
                }
                break;
@@ -1750,7 +2177,7 @@ main (int argc, char **argv)
                break;
            }
        } while (QLength (dpy));
-       if (allDamage)
+       if (allDamage && !autoRedirect)
        {
            static int  paint;
            paint_all (dpy, allDamage);