Added command-line options for changing values relevant to fading.
[dana/xcompmgr.git] / xcompmgr.c
index 7e542fe..7dcbf75 100644 (file)
@@ -178,6 +178,9 @@ win_extents (Display *dpy, win *w);
 CompMode    compMode = CompSimple;
 
 int        shadowRadius = 12;
+int         shadowOffsetX = -15;
+int         shadowOffsetY = -15;
+double      shadowOpacity = .75;
 
 double  fade_in_step =  0.028;
 double  fade_out_step = 0.03;
@@ -189,6 +192,11 @@ 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 ()
 {
@@ -340,7 +348,7 @@ run_fades (Display *dpy)
            {
                w->opacity = f->finish*OPAQUE;
                dequeue_fade (dpy, f);
-       }
+           }
        }
        else
        {
@@ -348,7 +356,7 @@ run_fades (Display *dpy)
            {
                w->opacity = f->finish*OPAQUE;
                dequeue_fade (dpy, f);
-       }
+           }
        }
        determine_mode (dpy, w);
        if (w->shadow)
@@ -361,10 +369,6 @@ run_fades (Display *dpy)
     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)
 {
@@ -473,6 +477,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)
 {
@@ -486,7 +526,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;
@@ -509,8 +549,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);
     
     /*
@@ -526,7 +568,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;
@@ -541,7 +586,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);
        }
@@ -553,7 +601,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;
@@ -581,10 +632,30 @@ shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int
                                  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,
@@ -606,11 +677,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;
@@ -759,11 +839,11 @@ 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 * ((double)w->opacity)/((double)OPAQUE);
                    w->shadow = shadow_picture (dpy, opacity, w->alphaPict,
@@ -1186,7 +1266,7 @@ get_opacity_prop(Display *dpy, win *w, unsigned int def)
     int result = XGetWindowProperty(dpy, w->id, opacityAtom, 0L, 1L, False, 
                       XA_CARDINAL, &actual, &format, 
                                    &n, &left, &data);
-    if (result == Success && data != None)
+    if (result == Success && data != NULL)
     {
        unsigned int i;
        memcpy (&i, data, sizeof (unsigned int));
@@ -1289,7 +1369,7 @@ static Atom
 determine_wintype (Display *dpy, Window w)
 {
     Window       root_return, parent_return;
-    Window      *children;
+    Window      *children = NULL;
     unsigned int nchildren, i;
     Atom         type;
 
@@ -1301,6 +1381,8 @@ determine_wintype (Display *dpy, Window w)
                            &nchildren))
     {
        /* XQueryTree failed. */
+       if (children)
+           XFree ((void *)children);
        return winNormalAtom;
     }
 
@@ -1311,6 +1393,9 @@ determine_wintype (Display *dpy, Window w)
            return type;
     }
 
+    if (children)
+       XFree ((void *)children);
+
     return winNormalAtom;
 }
 
@@ -1482,6 +1567,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
@@ -1737,7 +1825,25 @@ ev_window (XEvent *ev)
 void
 usage (char *program)
 {
-    fprintf (stderr, "usage: %s [-d display] [-n] [-s] [-c] [-a] [-f] [-F] [-C]\n", program);
+    fprintf (stderr, "%s v1.0\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);
 }
 
@@ -1765,12 +1871,27 @@ main (int argc, char **argv)
     char           *display = 0;
     int                    o;
 
-    while ((o = getopt (argc, argv, "d:scnfFCaS")) != -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;
@@ -1795,6 +1916,18 @@ main (int argc, char **argv)
        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;
@@ -1855,7 +1988,10 @@ main (int argc, char **argv)
     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);