Trim trailing white spaces on empty lines.
[geeqie.git] / src / slideshow.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "collect.h"
16 #include "image.h"
17 #include "slideshow.h"
18 #include "filedata.h"
19
20 #include "layout.h"
21 #include "layout_image.h"
22 #include "ui_fileops.h"
23
24
25 static void slideshow_timer_stop(SlideShowData *ss);
26
27
28 void slideshow_free(SlideShowData *ss)
29 {
30         if (!ss) return;
31
32         slideshow_timer_stop(ss);
33
34         if (ss->stop_func) ss->stop_func(ss, ss->stop_data);
35
36         if (ss->filelist) filelist_free(ss->filelist);
37         if (ss->cd) collection_unref(ss->cd);
38         file_data_unref(ss->dir_fd);
39
40         g_list_free(ss->list);
41         g_list_free(ss->list_done);
42
43         file_data_unref(ss->slide_fd);
44
45         g_free(ss);
46 }
47
48 static GList *generate_list(SlideShowData *ss)
49 {
50         GList *list = NULL;
51
52         if (ss->from_selection)
53                 {
54                 list = layout_selection_list_by_index(ss->lw);
55                 }
56         else
57                 {
58                 guint i;
59                 for (i = 0; i < ss->slide_count; i++)
60                         {
61                         list = g_list_prepend(list, GINT_TO_POINTER(i));
62                         }
63                 list = g_list_reverse(list);
64                 }
65
66         return list;
67 }
68
69 static void ptr_array_add(gpointer data, GPtrArray *array)
70 {
71         g_ptr_array_add(array, data);
72 }
73
74 static void list_prepend(gpointer data, GList **list)
75 {
76         *list = g_list_prepend(*list, data);
77 }
78
79 static GPtrArray *generate_ptr_array_from_list(GList *src_list)
80 {
81         GPtrArray *arr = g_ptr_array_sized_new(g_list_length(src_list));
82
83         g_list_foreach(src_list, (GFunc) ptr_array_add, arr);
84
85         return arr;
86 }
87
88 static void swap(GPtrArray *array, guint index1, guint index2)
89 {
90         gpointer temp = g_ptr_array_index(array, index1);
91
92         g_ptr_array_index(array, index1) = g_ptr_array_index(array, index2);
93         g_ptr_array_index(array, index2) = temp;
94 }
95
96 static void ptr_array_random_shuffle(GPtrArray *array)
97 {
98         guint i;
99         for (i = 0; i < array->len; ++i)
100                 {
101                 guint p = (double)rand() / ((double)RAND_MAX + 1.0) * array->len;
102                 swap(array, i, p);
103                 }
104 }
105
106 static GList *generate_random_list(SlideShowData *ss)
107 {
108         GList *src_list;
109         GPtrArray *src_array;
110         GList *list = NULL;
111
112         src_list = generate_list(ss);
113         src_array = generate_ptr_array_from_list(src_list);
114         g_list_free(src_list);
115
116         ptr_array_random_shuffle(src_array);
117         g_ptr_array_foreach(src_array, (GFunc) list_prepend, &list);
118         g_ptr_array_free(src_array, TRUE);
119
120         return list;
121 }
122
123 static void slideshow_list_init(SlideShowData *ss, gint start_index)
124 {
125         if (ss->list_done)
126                 {
127                 g_list_free(ss->list_done);
128                 ss->list_done = NULL;
129                 }
130
131         if (ss->list) g_list_free(ss->list);
132
133         if (options->slideshow.random)
134                 {
135                 ss->list = generate_random_list(ss);
136                 }
137         else
138                 {
139                 ss->list = generate_list(ss);
140                 if (start_index >= 0)
141                         {
142                         /* start with specified image by skipping to it */
143                         gint i = 0;
144
145                         while (ss->list && i < start_index)
146                                 {
147                                 ss->list_done = g_list_prepend(ss->list_done, ss->list->data);
148                                 ss->list = g_list_remove(ss->list, ss->list->data);
149                                 i++;
150                                 }
151                         }
152                 }
153 }
154
155 gboolean slideshow_should_continue(SlideShowData *ss)
156 {
157         FileData *imd_fd;
158         FileData *dir_fd;
159
160         if (!ss) return FALSE;
161
162         if (ss->lw)
163                 imd_fd = layout_image_get_fd(ss->lw);
164         else
165                 imd_fd = image_get_fd(ss->imd);
166
167         if ( ((imd_fd == NULL) != (ss->slide_fd == NULL)) ||
168             (imd_fd && ss->slide_fd && imd_fd != ss->slide_fd) ) return FALSE;
169
170         if (ss->filelist) return TRUE;
171
172         if (ss->cd)
173                 {
174                 if (g_list_length(ss->cd->list) == ss->slide_count)
175                         return TRUE;
176                 else
177                         return FALSE;
178                 }
179
180         dir_fd = ss->lw->dir_fd;
181
182         if (dir_fd && ss->dir_fd && dir_fd == ss->dir_fd)
183                 {
184                 if (ss->from_selection && ss->slide_count == layout_selection_count(ss->lw, NULL)) return TRUE;
185                 if (!ss->from_selection && ss->slide_count == layout_list_count(ss->lw, NULL)) return TRUE;
186                 }
187
188         return FALSE;
189 }
190
191 static gboolean slideshow_step(SlideShowData *ss, gboolean forward)
192 {
193         gint row;
194
195         if (!slideshow_should_continue(ss))
196                 {
197                 return FALSE;
198                 }
199
200         if (forward)
201                 {
202                 if (!ss->list) return TRUE;
203
204                 row = GPOINTER_TO_INT(ss->list->data);
205                 ss->list_done = g_list_prepend(ss->list_done, ss->list->data);
206                 ss->list = g_list_remove(ss->list, ss->list->data);
207                 }
208         else
209                 {
210                 if (!ss->list_done || !ss->list_done->next) return TRUE;
211
212                 ss->list = g_list_prepend(ss->list, ss->list_done->data);
213                 ss->list_done = g_list_remove(ss->list_done, ss->list_done->data);
214                 row = GPOINTER_TO_INT(ss->list_done->data);
215                 }
216
217         file_data_unref(ss->slide_fd);
218         ss->slide_fd = NULL;
219
220         if (ss->filelist)
221                 {
222                 ss->slide_fd = file_data_ref((FileData *)g_list_nth_data(ss->filelist, row));
223                 if (ss->lw)
224                         layout_set_fd(ss->lw, ss->slide_fd);
225                 else
226                         image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd));
227                 }
228         else if (ss->cd)
229                 {
230                 CollectInfo *info;
231
232                 info = g_list_nth_data(ss->cd->list, row);
233                 ss->slide_fd = file_data_ref(info->fd);
234
235                 if (ss->lw)
236                         image_change_from_collection(ss->lw->image, ss->cd, info, image_zoom_get_default(ss->lw->image));
237                 else
238                         image_change_from_collection(ss->imd, ss->cd, info, image_zoom_get_default(ss->imd));
239                 }
240         else
241                 {
242                 ss->slide_fd = file_data_ref(layout_list_get_fd(ss->lw, row));
243
244                 if (ss->from_selection)
245                         {
246                         layout_set_fd(ss->lw, ss->slide_fd);
247                         layout_status_update_info(ss->lw, NULL);
248                         }
249                 else
250                         {
251                         layout_image_set_index(ss->lw, row);
252                         }
253                 }
254
255         if (!ss->list && options->slideshow.repeat)
256                 {
257                 slideshow_list_init(ss, -1);
258                 }
259
260         if (!ss->list)
261                 {
262                 return FALSE;
263                 }
264
265         /* read ahead */
266         if (options->image.enable_read_ahead && (!ss->lw || ss->from_selection))
267                 {
268                 gint r;
269                 if (forward)
270                         {
271                         if (!ss->list) return TRUE;
272                         r = GPOINTER_TO_INT(ss->list->data);
273                         }
274                 else
275                         {
276                         if (!ss->list_done || !ss->list_done->next) return TRUE;
277                         r = GPOINTER_TO_INT(ss->list_done->next->data);
278                         }
279
280                 if (ss->filelist)
281                         {
282                         image_prebuffer_set(ss->imd, g_list_nth_data(ss->filelist, r));
283                         }
284                 else if (ss->cd)
285                         {
286                         CollectInfo *info;
287                         info = g_list_nth_data(ss->cd->list, r);
288                         if (info) image_prebuffer_set(ss->imd, info->fd);
289                         }
290                 else if (ss->from_selection)
291                         {
292                         image_prebuffer_set(ss->lw->image, layout_list_get_fd(ss->lw, r));
293                         }
294                 }
295
296         return TRUE;
297 }
298
299 static gboolean slideshow_loop_cb(gpointer data)
300 {
301         SlideShowData *ss = data;
302
303         if (ss->paused) return TRUE;
304
305         if (!slideshow_step(ss, TRUE))
306                 {
307                 ss->timeout_id = 0;
308                 slideshow_free(ss);
309                 return FALSE;
310                 }
311
312         return TRUE;
313 }
314
315 static void slideshow_timer_stop(SlideShowData *ss)
316 {
317         if (!ss->timeout_id) return;
318
319         g_source_remove(ss->timeout_id);
320         ss->timeout_id = 0;
321 }
322
323 static void slideshow_timer_reset(SlideShowData *ss)
324 {
325         if (options->slideshow.delay < 1) options->slideshow.delay = 1;
326
327         if (ss->timeout_id) g_source_remove(ss->timeout_id);
328         ss->timeout_id = g_timeout_add(options->slideshow.delay * 1000 / SLIDESHOW_SUBSECOND_PRECISION,
329                                        slideshow_loop_cb, ss);
330 }
331
332 static void slideshow_move(SlideShowData *ss, gboolean forward)
333 {
334         if (!ss) return;
335
336         if (!slideshow_step(ss, forward))
337                 {
338                 slideshow_free(ss);
339                 return;
340                 }
341
342         slideshow_timer_reset(ss);
343 }
344
345 void slideshow_next(SlideShowData *ss)
346 {
347         slideshow_move(ss, TRUE);
348 }
349
350 void slideshow_prev(SlideShowData *ss)
351 {
352         slideshow_move(ss, FALSE);
353 }
354
355 static SlideShowData *real_slideshow_start(LayoutWindow *target_lw, ImageWindow *imd,
356                                            GList *filelist, gint start_point,
357                                            CollectionData *cd, CollectInfo *start_info,
358                                            void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
359 {
360         SlideShowData *ss;
361         gint start_index = -1;
362
363         if (!filelist && !cd && layout_list_count(target_lw, NULL) < 1) return NULL;
364
365         ss = g_new0(SlideShowData, 1);
366
367         ss->lw = target_lw;
368         ss->imd = imd; /* FIXME: ss->imd is used only for img-view.c and can be dropped with it */
369         ss->filelist = filelist;
370         ss->cd = cd;
371
372         if (ss->filelist)
373                 {
374                 ss->slide_count = g_list_length(ss->filelist);
375                 }
376         else if (ss->cd)
377                 {
378                 collection_ref(ss->cd);
379                 ss->slide_count = g_list_length(ss->cd->list);
380                 if (!options->slideshow.random && start_info)
381                         {
382                         start_index = g_list_index(ss->cd->list, start_info);
383                         }
384                 }
385         else
386                 {
387                 /* layout method */
388
389                 ss->slide_count = layout_selection_count(ss->lw, NULL);
390                 ss->dir_fd = file_data_ref(ss->lw->dir_fd);
391                 if (ss->slide_count < 2)
392                         {
393                         ss->slide_count = layout_list_count(ss->lw, NULL);
394                         if (!options->slideshow.random && start_point >= 0 && (guint) start_point < ss->slide_count)
395                                 {
396                                 start_index = start_point;
397                                 }
398                         }
399                 else
400                         {
401                         ss->from_selection = TRUE;
402                         }
403                 }
404
405         slideshow_list_init(ss, start_index);
406
407         if (ss->lw)
408                 ss->slide_fd = file_data_ref(layout_image_get_fd(ss->lw));
409         else
410                 ss->slide_fd = file_data_ref(image_get_fd(ss->imd));
411
412         if (slideshow_step(ss, TRUE))
413                 {
414                 slideshow_timer_reset(ss);
415
416                 ss->stop_func = stop_func;
417                 ss->stop_data = stop_data;
418                 }
419         else
420                 {
421                 slideshow_free(ss);
422                 ss = NULL;
423                 }
424
425         return ss;
426 }
427
428 SlideShowData *slideshow_start_from_filelist(LayoutWindow *target_lw, ImageWindow *imd, GList *list,
429                                               void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
430 {
431         return real_slideshow_start(target_lw, imd, list, -1, NULL, NULL, stop_func, stop_data);
432 }
433
434 SlideShowData *slideshow_start_from_collection(LayoutWindow *target_lw, ImageWindow *imd, CollectionData *cd,
435                                                void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data,
436                                                CollectInfo *start_info)
437 {
438         return real_slideshow_start(target_lw, imd, NULL, -1, cd, start_info, stop_func, stop_data);
439 }
440
441 SlideShowData *slideshow_start(LayoutWindow *lw, gint start_point,
442                                void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
443 {
444         return real_slideshow_start(lw, NULL, NULL, start_point, NULL, NULL, stop_func, stop_data);
445 }
446
447 gboolean slideshow_paused(SlideShowData *ss)
448 {
449         if (!ss) return FALSE;
450
451         return ss->paused;
452 }
453
454 void slideshow_pause_set(SlideShowData *ss, gboolean paused)
455 {
456         if (!ss) return;
457
458         ss->paused = paused;
459 }
460
461 gboolean slideshow_pause_toggle(SlideShowData *ss)
462 {
463         slideshow_pause_set(ss, !slideshow_paused(ss));
464         return slideshow_paused(ss);
465 }
466 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */