trying to improve the timers some
[dana/dcompmgr.git] / fade.c
diff --git a/fade.c b/fade.c
index 8244333..eb59ec6 100644 (file)
--- a/fade.c
+++ b/fade.c
@@ -52,8 +52,8 @@ fade_init(d_screen_t *sc, int id)
     sc->window_hide = fade_window_hide;
 
     d->fades = list_new();
-    d->fade_step_time = 5000;     /* 5 milliseconds */
-    d->fade_total_time = 150000;  /* 0.15 seconds */
+    d->fade_step_time = 10000;     /* 5 milliseconds */
+    d->fade_total_time = 200000;  /* 0.15 seconds */
 }
 
 void
@@ -82,7 +82,7 @@ fade_window_show(d_window_t *w)
     data_t *d;
 
     if (!window_is_input_only(w))
-        start_fade(w, 0, 0xffff);
+        start_fade(w, 0x0000, 0xffff);
 
     d = screen_find_plugin_data(w->sc, plugin_id);
     d->window_show(w);    
@@ -94,7 +94,7 @@ fade_window_hide(d_window_t *w)
     data_t *d;
 
     if (!window_is_input_only(w))
-        start_fade(w, 0xffff, 0);
+        start_fade(w, 0xffff, 0x0000);
 
     d = screen_find_plugin_data(w->sc, plugin_id);
     d->window_hide(w);
@@ -192,30 +192,38 @@ fade_timeout(struct d_screen *sc, const struct timeval *now)
 
     for (it = list_top(d->fades); it; it = next) {
         fade_t *fade = it->data;
-        struct timeval time_done, total_time;
-        unsigned long done, total;
-        double percent;
 
         next = it->next;
 
-        time_difference(now, &fade->start_time, &time_done);
-        time_difference(&fade->end_time, &fade->start_time, &total_time);
-
-        /* count milliseconds */
-        done = time_done.tv_sec * 1000 + time_done.tv_usec / 1000;
-        total = total_time.tv_sec * 1000 + total_time.tv_usec / 1000;
-        percent = (double)done / total;
-
-        //printf("done %lu total %lu percent %f\n", done, total, percent);
-
-        if (percent >= 1)
+        if (time_compare(&fade->end_time, now) <= 0) {
+            /* done */
             fade->current_alpha = fade->end_alpha;
-        else if (fade->end_alpha > fade->start_alpha)
-            fade->current_alpha = fade->start_alpha +
-                (fade->end_alpha - fade->start_alpha) * percent;
-        else
-            fade->current_alpha = fade->start_alpha -
-                (fade->start_alpha - fade->end_alpha) * percent;
+        }
+        else {
+            struct timeval total_time, time_left;
+            unsigned long remain, total;
+            double percent;
+
+            time_difference(&fade->end_time, now, &time_left);
+            time_difference(&fade->end_time, &fade->start_time, &total_time);
+
+
+            /* count milliseconds */
+            remain = time_left.tv_sec * 1000 + time_left.tv_usec / 1000;
+            total = total_time.tv_sec * 1000 + total_time.tv_usec / 1000;
+            percent = (double)remain / total;
+
+            //printf("done %lu total %lu percent %f\n", done, total, percent);
+
+            if (fade->end_alpha > fade->start_alpha)
+                /* increasing */
+                fade->current_alpha = fade->end_alpha -
+                    (fade->end_alpha - fade->start_alpha) * percent;
+            else
+                /* decreasing */
+                fade->current_alpha = fade->end_alpha +
+                    (fade->start_alpha - fade->end_alpha) * percent;
+        }
 
         window_set_opacity(fade->w, fade->current_alpha);
 
@@ -227,5 +235,6 @@ fade_timeout(struct d_screen *sc, const struct timeval *now)
             g_free(fade);
         }
     }
+    d->next_timeout = *now;
     time_add(&d->next_timeout, d->fade_step_time);
 }