Added .gitignore.
[dana/xcompmgr.git] / xcompmgr.c
index dcb0843..a142cff 100644 (file)
@@ -322,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");
@@ -342,21 +343,22 @@ 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 >= f->finish)
            {
                w->opacity = f->finish*OPAQUE;
-               dequeue_fade (dpy, f);
-       }
+               need_dequeue = True;
+           }
        }
        else
        {
            if (f->cur <= f->finish)
            {
                w->opacity = f->finish*OPAQUE;
-               dequeue_fade (dpy, f);
-       }
+               need_dequeue = True;
+           }
        }
        determine_mode (dpy, w);
        if (w->shadow)
@@ -365,6 +367,9 @@ run_fades (Display *dpy)
            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;
 }
@@ -620,9 +625,7 @@ shadow_picture (Display *dpy, double opacity, Picture alpha_pict, int width, int
 {
     XImage  *shadowImage;
     Pixmap  shadowPixmap;
-    Pixmap  finalPixmap;
     Picture shadowPicture;
-    Picture finalPicture;
     GC     gc;
     
     shadowImage = make_shadow (dpy, opacity, width, height);
@@ -677,11 +680,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;
@@ -933,6 +945,10 @@ paint_all (Display *dpy, XserverRegion region)
        /* 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;
@@ -1021,6 +1037,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,
@@ -1035,7 +1054,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,
@@ -1143,11 +1163,13 @@ repair_win (Display *dpy, win *w)
     w->damaged = 1;
 }
 
+static unsigned int
+get_opacity_prop (Display *dpy, win *w, unsigned int def);
+
 static void
 map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
 {
     win                *w = find_win (dpy, id);
-    Drawable   back;
 
     if (!w)
        return;
@@ -1157,6 +1179,10 @@ map_win (Display *dpy, Window id, unsigned long sequence, Bool fade)
     /* This needs to be here or else we lose transparency messages */
     XSelectInput (dpy, id, PropertyChangeMask);
 
+    /* This needs to be here since we don't get PropertyNotify when unmapped */
+    w->opacity = get_opacity_prop (dpy, w, OPAQUE);
+    determine_mode (dpy, w);
+
 #if CAN_DO_USABLE
     w->damage_bounds.x = w->damage_bounds.y = 0;
     w->damage_bounds.width = w->damage_bounds.height = 0;
@@ -1257,7 +1283,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));
@@ -1310,7 +1336,6 @@ determine_mode(Display *dpy, win *w)
 {
     int mode;
     XRenderPictFormat *format;
-    unsigned int default_opacity;
 
     /* if trans prop == -1 fall back on  previous tests*/
 
@@ -1360,7 +1385,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;
 
@@ -1372,6 +1397,8 @@ determine_wintype (Display *dpy, Window w)
                            &nchildren))
     {
        /* XQueryTree failed. */
+       if (children)
+           XFree ((void *)children);
        return winNormalAtom;
     }
 
@@ -1382,6 +1409,9 @@ determine_wintype (Display *dpy, Window w)
            return type;
     }
 
+    if (children)
+       XFree ((void *)children);
+
     return winNormalAtom;
 }
 
@@ -1440,10 +1470,7 @@ add_win (Display *dpy, Window id, Window prev)
     new->borderClip = None;
     new->prev_trans = 0;
 
-    /* moved mode setting to one place */
-    new->opacity = get_opacity_prop (dpy, new, OPAQUE);
     new->windowType = determine_wintype (dpy, new->id);
-    determine_mode (dpy, new);
     
     new->next = *p;
     *p = new;
@@ -1485,7 +1512,6 @@ static void
 configure_win (Display *dpy, XConfigureEvent *ce)
 {
     win                    *w = find_win (dpy, ce->window);
-    Window         above;
     XserverRegion   damage = None;
     
     if (!w)
@@ -1553,6 +1579,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
@@ -1569,7 +1598,7 @@ finish_destroy_win (Display *dpy, Window id, Bool gone)
     for (prev = &list; (w = *prev); prev = &w->next)
        if (w->id == id)
        {
-           if (!gone)
+           if (gone)
                finish_unmap_win (dpy, w);
            *prev = w->next;
            if (w->picture)
@@ -1736,7 +1765,7 @@ error (Display *dpy, XErrorEvent *ev)
     default: break;
     }
        
-    printf ("error %d request %d minor %d serial %d\n",
+    printf ("error %d request %d minor %d serial %lu\n",
            ev->error_code, ev->request_code, ev->minor_code, ev->serial);
 
 /*    abort ();            this is just annoying to most people */
@@ -1751,7 +1780,7 @@ expose_root (Display *dpy, Window root, XRectangle *rects, int nrects)
     add_damage (dpy, region);
 }
 
-
+#if DEBUG_EVENTS
 static int
 ev_serial (XEvent *ev)
 {
@@ -1760,7 +1789,6 @@ ev_serial (XEvent *ev)
     return NextRequest (ev->xany.display);
 }
 
-
 static char *
 ev_name (XEvent *ev)
 {
@@ -1804,11 +1832,12 @@ ev_window (XEvent *ev)
        return 0;
     }
 }
+#endif
 
 void
 usage (char *program)
 {
-    fprintf (stderr, "%s v1.0\n", program);
+    fprintf (stderr, "%s v1.1.3\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");
@@ -1816,6 +1845,9 @@ usage (char *program)
     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");
@@ -1827,36 +1859,63 @@ usage (char *program)
     exit (1);
 }
 
+static void
+register_cm (void)
+{
+    Window w;
+    Atom a;
+
+    w = XCreateSimpleWindow (dpy, RootWindow (dpy, 0), 0, 0, 1, 1, 0, None,
+                            None);
+
+    Xutf8SetWMProperties (dpy, w, "xcompmgr", "xcompmgr", NULL, 0, NULL, NULL,
+                         NULL);
+
+    /* FIXME: Don't hard code the screen number */
+    a = XInternAtom (dpy, "_NET_WM_CM_S0", False);
+
+    XSetSelectionOwner (dpy, a, w, 0);
+}
+
 int
 main (int argc, char **argv)
 {
     XEvent         ev;
     Window         root_return, parent_return;
     Window         *children;
-    Pixmap         transPixmap;
-    Pixmap         blackPixmap;
     unsigned int    nchildren;
     int                    i;
     XRenderPictureAttributes   pa;
-    XRenderColor               c;
     XRectangle     *expose_rects = 0;
     int                    size_expose = 0;
     int                    n_expose = 0;
     struct pollfd   ufd;
-    int                    n;
-    int                    last_update;
-    int                    now;
     int                    p;
     int                    composite_major, composite_minor;
     char           *display = 0;
     int                    o;
 
-    while ((o = getopt (argc, argv, "d:r:o:l:t: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;
@@ -1938,6 +1997,9 @@ main (int argc, char **argv)
        fprintf (stderr, "No XFixes extension\n");
        exit (1);
     }
+
+    register_cm();
+
     /* get atoms */
     opacityAtom = XInternAtom (dpy, OPACITY_PROP, False);
     winTypeAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE", False);
@@ -2008,7 +2070,7 @@ main (int argc, char **argv)
            }
 
            XNextEvent (dpy, &ev);
-           if (ev.type & 0x7f != KeymapNotify)
+           if ((ev.type & 0x7f) != KeymapNotify)
                discard_ignore (dpy, ev.xany.serial);
 #if DEBUG_EVENTS
            printf ("event %10.10s serial 0x%08x window 0x%08x\n",