12 #include <xcb/render.h>
17 void (*window_show)(d_window_t *w);
18 void (*window_hide)(d_window_t *w);
21 // unsigned int fade_step_size;
22 unsigned int fade_step_time;
23 unsigned int fade_total_time;
25 struct timeval next_timeout;
32 uint16_t current_alpha;
33 struct timeval start_time;
34 struct timeval end_time;
37 static void fade_window_show(d_window_t *w);
38 static void fade_window_hide(d_window_t *w);
39 static void start_fade(d_window_t *w, uint16_t start_alpha, uint16_t end_alpha,
43 fade_init(d_screen_t *sc, int id)
47 data_t *d = malloc(sizeof(data_t));
48 d->window_show = sc->window_show;
49 d->window_hide = sc->window_hide;
50 screen_add_plugin_data(sc, plugin_id, d);
52 sc->window_show = fade_window_show;
53 sc->window_hide = fade_window_hide;
55 d->fades = list_new();
56 d->fade_step_time = 5000; /* 5 milliseconds */
57 d->fade_total_time = 180000; /* 0.18 seconds */
61 fade_free(d_screen_t *sc)
66 d = screen_find_plugin_data(sc, plugin_id);
67 screen_remove_plugin_data(sc, plugin_id);
69 for (it = list_top(d->fades); it; it = it->next) {
70 fade_t *fade = it->data;
71 window_set_opacity(fade->w, 0xffff);
72 window_zombie_unref(fade->w);
73 window_unref(fade->w);
81 fade_window_show(d_window_t *w)
86 d = screen_find_plugin_data(w->sc, plugin_id);
88 /* kill the zombies off cuz a new copy is being shown */
89 for (it = list_top(d->fades); it; it = it->next) {
90 fade_t *fade = it->data;
92 window_zombie_unref(w);
100 if (!window_is_input_only(w))
101 start_fade(w, 0x2000, 0xffff, TRUE);
105 fade_window_hide(d_window_t *w)
109 if (!window_is_input_only(w))
110 start_fade(w, 0xffff, 0x0002, FALSE);
112 d = screen_find_plugin_data(w->sc, plugin_id);
117 start_fade(d_window_t *w, uint16_t start_alpha, uint16_t end_alpha,
126 d = screen_find_plugin_data(w->sc, plugin_id);
127 gettimeofday(&now, NULL);
129 if (list_length(d->fades) == 0) {
130 d->next_timeout = now;
131 time_add(&d->next_timeout, d->fade_step_time);
134 /* look for an existing one */
136 for (it = list_top(d->fades); it; it = it->next) {
138 if (fade->w == w) break;
141 fade = malloc(sizeof(fade_t));
143 list_append(d->fades, fade);
149 fade->start_alpha = start_alpha;
150 fade->end_alpha = end_alpha;
153 fade->start_time = now;
154 fade->end_time = now;
155 time_add(&fade->end_time, d->fade_total_time);
156 fade->current_alpha = start_alpha;
158 window_set_opacity(w, fade->current_alpha);
161 /* figure out how far we have to go to finish the fade from where
162 we are from the previous fade */
166 total = ABS(fade->end_alpha - fade->start_alpha);
167 remain = ABS(fade->current_alpha - fade->end_alpha);
168 percent = (double)remain / total;
170 //printf("start %d end %d current %d\n", fade->start, fade->end,
172 //printf("remain %lu total %lu percent %f\n", remain, total, percent);
174 fade->start_time = now;
175 time_add(&fade->start_time, -(1-percent)*d->fade_total_time);
176 fade->end_time = now;
177 time_add(&fade->end_time, percent*d->fade_total_time);
179 if (newfade || refalways) {
180 window_zombie_ref(w);
186 fade_next_timeout(struct d_screen *sc, struct timeval *tv)
190 d = screen_find_plugin_data(sc, plugin_id);
192 if (list_length(d->fades) == 0)
195 *tv = d->next_timeout;
201 fade_timeout(struct d_screen *sc, const struct timeval *now)
204 d_list_it_t *it, *next;
206 d = screen_find_plugin_data(sc, plugin_id);
208 for (it = list_top(d->fades); it; it = next) {
209 fade_t *fade = it->data;
213 if (time_compare(&fade->end_time, now) <= 0) {
215 fade->current_alpha = fade->end_alpha;
218 struct timeval total_time, time_left;
219 unsigned long remain, total;
222 time_difference(&fade->end_time, now, &time_left);
223 time_difference(&fade->end_time, &fade->start_time, &total_time);
226 /* count milliseconds */
227 remain = time_left.tv_sec * 1000 + time_left.tv_usec / 1000;
228 total = total_time.tv_sec * 1000 + total_time.tv_usec / 1000;
229 percent = (double)remain / total;
231 //printf("done %lu total %lu percent %f\n", done, total, percent);
233 if (fade->end_alpha > fade->start_alpha)
235 fade->current_alpha = fade->end_alpha -
236 (fade->end_alpha - fade->start_alpha) * percent;
239 fade->current_alpha = fade->end_alpha +
240 (fade->start_alpha - fade->end_alpha) * percent;
243 window_set_opacity(fade->w, fade->current_alpha);
245 if (fade->current_alpha == fade->end_alpha) {
246 list_delete_link(d->fades, it);
247 window_zombie_unref(fade->w);
248 window_unref(fade->w);
253 d->next_timeout = *now;
254 time_add(&d->next_timeout, d->fade_step_time);