Add year 2009 to copyright info everywhere.
[geeqie.git] / src / slideshow.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 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->layout);
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 gint slideshow_should_continue(SlideShowData *ss)
156 {
157         FileData *imd_fd;
158         FileData *dir_fd;
159
160         if (!ss) return FALSE;
161
162         imd_fd = image_get_fd(ss->imd);
163
164         if ( ((imd_fd == NULL) != (ss->slide_fd == NULL)) ||
165             (imd_fd && ss->slide_fd && imd_fd != ss->slide_fd) ) return FALSE;
166
167         if (ss->filelist) return TRUE;
168
169         if (ss->cd)
170                 {
171                 if (g_list_length(ss->cd->list) == ss->slide_count)
172                         return TRUE;
173                 else
174                         return FALSE;
175                 }
176
177         if (!ss->layout) return FALSE;
178         dir_fd = ss->layout->dir_fd;
179
180         if (dir_fd && ss->dir_fd && dir_fd == ss->dir_fd)
181                 {
182                 if (ss->from_selection && ss->slide_count == layout_selection_count(ss->layout, NULL)) return TRUE;
183                 if (!ss->from_selection && ss->slide_count == layout_list_count(ss->layout, NULL)) return TRUE;
184                 }
185
186         return FALSE;
187 }
188
189 static gint slideshow_step(SlideShowData *ss, gboolean forward)
190 {
191         gint row;
192
193         if (!slideshow_should_continue(ss))
194                 {
195                 return FALSE;
196                 }
197
198         if (forward)
199                 {
200                 if (!ss->list) return TRUE;
201
202                 row = GPOINTER_TO_INT(ss->list->data);
203                 ss->list_done = g_list_prepend(ss->list_done, ss->list->data);
204                 ss->list = g_list_remove(ss->list, ss->list->data);
205                 }
206         else
207                 {
208                 if (!ss->list_done || !ss->list_done->next) return TRUE;
209
210                 ss->list = g_list_prepend(ss->list, ss->list_done->data);
211                 ss->list_done = g_list_remove(ss->list_done, ss->list_done->data);
212                 row = GPOINTER_TO_INT(ss->list_done->data);
213                 }
214
215         file_data_unref(ss->slide_fd);
216         ss->slide_fd = NULL;
217
218         if (ss->filelist)
219                 {
220                 ss->slide_fd = file_data_ref((FileData *)g_list_nth_data(ss->filelist, row));
221                 image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd));
222                 }
223         else if (ss->cd)
224                 {
225                 CollectInfo *info;
226
227                 info = g_list_nth_data(ss->cd->list, row);
228                 ss->slide_fd = file_data_ref(info->fd);
229
230                 image_change_from_collection(ss->imd, ss->cd, info, image_zoom_get_default(ss->imd));
231                 }
232         else
233                 {
234                 ss->slide_fd = file_data_ref(layout_list_get_fd(ss->layout, row));
235
236                 if (ss->from_selection)
237                         {
238                         image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd));
239                         layout_status_update_info(ss->layout, NULL);
240                         }
241                 else
242                         {
243                         layout_image_set_index(ss->layout, row);
244                         }
245                 }
246
247         if (!ss->list && options->slideshow.repeat)
248                 {
249                 slideshow_list_init(ss, -1);
250                 }
251
252         if (!ss->list)
253                 {
254                 return FALSE;
255                 }
256
257         /* read ahead */
258
259         if (options->image.enable_read_ahead)
260                 {
261                 gint r;
262                 if (forward)
263                         {
264                         if (!ss->list) return TRUE;
265                         r = GPOINTER_TO_INT(ss->list->data);
266                         }
267                 else
268                         {
269                         if (!ss->list_done || !ss->list_done->next) return TRUE;
270                         r = GPOINTER_TO_INT(ss->list_done->next->data);
271                         }
272
273                 if (ss->filelist)
274                         {
275                         image_prebuffer_set(ss->imd, g_list_nth_data(ss->filelist, r));
276                         }
277                 else if (ss->cd)
278                         {
279                         CollectInfo *info;
280                         info = g_list_nth_data(ss->cd->list, r);
281                         if (info) image_prebuffer_set(ss->imd, info->fd);
282                         }
283                 else if (ss->from_selection)
284                         {
285                         image_prebuffer_set(ss->imd, layout_list_get_fd(ss->layout, r));
286                         }
287                 }
288
289         return TRUE;
290 }
291
292 static gint slideshow_loop_cb(gpointer data)
293 {
294         SlideShowData *ss = data;
295
296         if (ss->paused) return TRUE;
297
298         if (!slideshow_step(ss, TRUE))
299                 {
300                 ss->timeout_id = -1;
301                 slideshow_free(ss);
302                 return FALSE;
303                 }
304
305         return TRUE;
306 }
307
308 static void slideshow_timer_stop(SlideShowData *ss)
309 {
310         if (ss->timeout_id == -1) return;
311
312         g_source_remove(ss->timeout_id);
313         ss->timeout_id = -1;
314 }
315
316 static void slideshow_timer_reset(SlideShowData *ss)
317 {
318         if (options->slideshow.delay < 1) options->slideshow.delay = 1;
319
320         if (ss->timeout_id != -1) g_source_remove(ss->timeout_id);
321         ss->timeout_id = g_timeout_add(options->slideshow.delay * 1000 / SLIDESHOW_SUBSECOND_PRECISION,
322                                        slideshow_loop_cb, ss);
323 }
324
325 static void slideshow_move(SlideShowData *ss, gboolean forward)
326 {
327         if (!ss) return;
328
329         if (!slideshow_step(ss, forward))
330                 {
331                 slideshow_free(ss);
332                 return;
333                 }
334
335         slideshow_timer_reset(ss);
336 }
337
338 void slideshow_next(SlideShowData *ss)
339 {
340         slideshow_move(ss, TRUE);
341 }
342
343 void slideshow_prev(SlideShowData *ss)
344 {
345         slideshow_move(ss, FALSE);
346 }
347
348 static SlideShowData *real_slideshow_start(ImageWindow *imd, LayoutWindow *lw,
349                                            GList *filelist, gint start_point,
350                                            CollectionData *cd, CollectInfo *start_info,
351                                            void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
352 {
353         SlideShowData *ss;
354         gint start_index = -1;
355
356         if (!filelist && !cd && layout_list_count(lw, NULL) < 1) return NULL;
357
358         ss = g_new0(SlideShowData, 1);
359
360         ss->imd = imd;
361
362         ss->filelist = filelist;
363         ss->cd = cd;
364         ss->layout = lw;
365         ss->dir_fd = NULL;
366
367         ss->list = NULL;
368         ss->list_done = NULL;
369
370         ss->from_selection = FALSE;
371
372         ss->stop_func = NULL;
373
374         ss->timeout_id = -1;
375         ss->paused = FALSE;
376
377         if (ss->filelist)
378                 {
379                 ss->slide_count = g_list_length(ss->filelist);
380                 }
381         else if (ss->cd)
382                 {
383                 collection_ref(ss->cd);
384                 ss->slide_count = g_list_length(ss->cd->list);
385                 if (!options->slideshow.random && start_info)
386                         {
387                         start_index = g_list_index(ss->cd->list, start_info);
388                         }
389                 }
390         else
391                 {
392                 /* layout method */
393
394                 ss->slide_count = layout_selection_count(ss->layout, NULL);
395                 ss->dir_fd = file_data_ref(ss->layout->dir_fd);
396                 if (ss->slide_count < 2)
397                         {
398                         ss->slide_count = layout_list_count(ss->layout, NULL);
399                         if (!options->slideshow.random && start_point >= 0 && (guint) start_point < ss->slide_count)
400                                 {
401                                 start_index = start_point;
402                                 }
403                         }
404                 else
405                         {
406                         ss->from_selection = TRUE;
407                         }
408                 }
409
410         slideshow_list_init(ss, start_index);
411
412         ss->slide_fd = file_data_ref(image_get_fd(ss->imd));
413         if (slideshow_step(ss, TRUE))
414                 {
415                 slideshow_timer_reset(ss);
416
417                 ss->stop_func = stop_func;
418                 ss->stop_data = stop_data;
419                 }
420         else
421                 {
422                 slideshow_free(ss);
423                 ss = NULL;
424                 }
425
426         return ss;
427 }
428
429 SlideShowData *slideshow_start_from_filelist(ImageWindow *imd, GList *list,
430                                               void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
431 {
432         return real_slideshow_start(imd, NULL, list, -1, NULL, NULL, stop_func, stop_data);
433 }
434
435 SlideShowData *slideshow_start_from_collection(ImageWindow *imd, CollectionData *cd,
436                                                void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data,
437                                                CollectInfo *start_info)
438 {
439         return real_slideshow_start(imd, NULL, NULL, -1, cd, start_info, stop_func, stop_data);
440 }
441
442 SlideShowData *slideshow_start(ImageWindow *imd, LayoutWindow *lw, gint start_point,
443                                void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
444 {
445         return real_slideshow_start(imd, lw, NULL, start_point, NULL, NULL, stop_func, stop_data);
446 }
447
448 gint slideshow_paused(SlideShowData *ss)
449 {
450         if (!ss) return FALSE;
451
452         return ss->paused;
453 }
454
455 void slideshow_pause_set(SlideShowData *ss, gint paused)
456 {
457         if (!ss) return;
458
459         ss->paused = paused;
460 }
461
462 gint slideshow_pause_toggle(SlideShowData *ss)
463 {
464         slideshow_pause_set(ss, !slideshow_paused(ss));
465         return slideshow_paused(ss);
466 }
467 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */