2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "slideshow.h"
29 #include "layout_image.h"
30 #include "ui_fileops.h"
33 static void slideshow_timer_stop(SlideShowData *ss);
36 void slideshow_free(SlideShowData *ss)
40 slideshow_timer_stop(ss);
42 if (ss->stop_func) ss->stop_func(ss, ss->stop_data);
44 if (ss->filelist) filelist_free(ss->filelist);
45 if (ss->cd) collection_unref(ss->cd);
46 file_data_unref(ss->dir_fd);
48 g_list_free(ss->list);
49 g_list_free(ss->list_done);
51 file_data_unref(ss->slide_fd);
56 static GList *generate_list(SlideShowData *ss)
60 if (ss->from_selection)
62 list = layout_selection_list_by_index(ss->lw);
67 for (i = 0; i < ss->slide_count; i++)
69 list = g_list_prepend(list, GINT_TO_POINTER(i));
71 list = g_list_reverse(list);
77 static void ptr_array_add(gpointer data, GPtrArray *array)
79 g_ptr_array_add(array, data);
82 static void list_prepend(gpointer data, GList **list)
84 *list = g_list_prepend(*list, data);
87 static GPtrArray *generate_ptr_array_from_list(GList *src_list)
89 GPtrArray *arr = g_ptr_array_sized_new(g_list_length(src_list));
91 g_list_foreach(src_list, (GFunc) ptr_array_add, arr);
96 static void swap(GPtrArray *array, guint index1, guint index2)
98 gpointer temp = g_ptr_array_index(array, index1);
100 g_ptr_array_index(array, index1) = g_ptr_array_index(array, index2);
101 g_ptr_array_index(array, index2) = temp;
104 static void ptr_array_random_shuffle(GPtrArray *array)
107 for (i = 0; i < array->len; ++i)
109 guint p = (double)rand() / ((double)RAND_MAX + 1.0) * array->len;
114 static GList *generate_random_list(SlideShowData *ss)
117 GPtrArray *src_array;
120 src_list = generate_list(ss);
121 src_array = generate_ptr_array_from_list(src_list);
122 g_list_free(src_list);
124 ptr_array_random_shuffle(src_array);
125 g_ptr_array_foreach(src_array, (GFunc) list_prepend, &list);
126 g_ptr_array_free(src_array, TRUE);
131 static void slideshow_list_init(SlideShowData *ss, gint start_index)
135 g_list_free(ss->list_done);
136 ss->list_done = NULL;
139 if (ss->list) g_list_free(ss->list);
141 if (options->slideshow.random)
143 ss->list = generate_random_list(ss);
147 ss->list = generate_list(ss);
148 if (start_index >= 0)
150 /* start with specified image by skipping to it */
153 while (ss->list && i < start_index)
155 ss->list_done = g_list_prepend(ss->list_done, ss->list->data);
156 ss->list = g_list_remove(ss->list, ss->list->data);
163 gboolean slideshow_should_continue(SlideShowData *ss)
168 if (!ss) return FALSE;
171 imd_fd = layout_image_get_fd(ss->lw);
173 imd_fd = image_get_fd(ss->imd);
175 if ( ((imd_fd == NULL) != (ss->slide_fd == NULL)) ||
176 (imd_fd && ss->slide_fd && imd_fd != ss->slide_fd) ) return FALSE;
178 if (ss->filelist) return TRUE;
182 if (g_list_length(ss->cd->list) == ss->slide_count)
188 dir_fd = ss->lw->dir_fd;
190 if (dir_fd && ss->dir_fd && dir_fd == ss->dir_fd)
192 if (ss->from_selection && ss->slide_count == layout_selection_count(ss->lw, NULL)) return TRUE;
193 if (!ss->from_selection && ss->slide_count == layout_list_count(ss->lw, NULL)) return TRUE;
199 static gboolean slideshow_step(SlideShowData *ss, gboolean forward)
203 if (!slideshow_should_continue(ss))
210 if (!ss->list) return TRUE;
212 row = GPOINTER_TO_INT(ss->list->data);
213 ss->list_done = g_list_prepend(ss->list_done, ss->list->data);
214 ss->list = g_list_remove(ss->list, ss->list->data);
218 if (!ss->list_done || !ss->list_done->next) return TRUE;
220 ss->list = g_list_prepend(ss->list, ss->list_done->data);
221 ss->list_done = g_list_remove(ss->list_done, ss->list_done->data);
222 row = GPOINTER_TO_INT(ss->list_done->data);
225 file_data_unref(ss->slide_fd);
230 ss->slide_fd = file_data_ref((FileData *)g_list_nth_data(ss->filelist, row));
232 layout_set_fd(ss->lw, ss->slide_fd);
234 image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd));
240 info = g_list_nth_data(ss->cd->list, row);
241 ss->slide_fd = file_data_ref(info->fd);
244 image_change_from_collection(ss->lw->image, ss->cd, info, image_zoom_get_default(ss->lw->image));
246 image_change_from_collection(ss->imd, ss->cd, info, image_zoom_get_default(ss->imd));
250 ss->slide_fd = file_data_ref(layout_list_get_fd(ss->lw, row));
252 if (ss->from_selection)
254 layout_set_fd(ss->lw, ss->slide_fd);
255 layout_status_update_info(ss->lw, NULL);
259 layout_image_set_index(ss->lw, row);
263 if (!ss->list && options->slideshow.repeat)
265 slideshow_list_init(ss, -1);
274 if (options->image.enable_read_ahead && (!ss->lw || ss->from_selection))
279 if (!ss->list) return TRUE;
280 r = GPOINTER_TO_INT(ss->list->data);
284 if (!ss->list_done || !ss->list_done->next) return TRUE;
285 r = GPOINTER_TO_INT(ss->list_done->next->data);
290 image_prebuffer_set(ss->imd, g_list_nth_data(ss->filelist, r));
295 info = g_list_nth_data(ss->cd->list, r);
296 if (info) image_prebuffer_set(ss->imd, info->fd);
298 else if (ss->from_selection)
300 image_prebuffer_set(ss->lw->image, layout_list_get_fd(ss->lw, r));
307 static gboolean slideshow_loop_cb(gpointer data)
309 SlideShowData *ss = data;
311 if (ss->paused) return TRUE;
313 if (!slideshow_step(ss, TRUE))
323 static void slideshow_timer_stop(SlideShowData *ss)
325 if (!ss->timeout_id) return;
327 g_source_remove(ss->timeout_id);
331 static void slideshow_timer_reset(SlideShowData *ss)
333 if (options->slideshow.delay < 1) options->slideshow.delay = 1;
335 if (ss->timeout_id) g_source_remove(ss->timeout_id);
336 ss->timeout_id = g_timeout_add(options->slideshow.delay * 1000 / SLIDESHOW_SUBSECOND_PRECISION,
337 slideshow_loop_cb, ss);
340 static void slideshow_move(SlideShowData *ss, gboolean forward)
344 if (!slideshow_step(ss, forward))
350 slideshow_timer_reset(ss);
353 void slideshow_next(SlideShowData *ss)
355 slideshow_move(ss, TRUE);
358 void slideshow_prev(SlideShowData *ss)
360 slideshow_move(ss, FALSE);
363 static SlideShowData *real_slideshow_start(LayoutWindow *target_lw, ImageWindow *imd,
364 GList *filelist, gint start_point,
365 CollectionData *cd, CollectInfo *start_info,
366 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
369 gint start_index = -1;
371 if (!filelist && !cd && layout_list_count(target_lw, NULL) < 1) return NULL;
373 ss = g_new0(SlideShowData, 1);
376 ss->imd = imd; /* FIXME: ss->imd is used only for img-view.c and can be dropped with it */
377 ss->filelist = filelist;
382 ss->slide_count = g_list_length(ss->filelist);
386 collection_ref(ss->cd);
387 ss->slide_count = g_list_length(ss->cd->list);
388 if (!options->slideshow.random && start_info)
390 start_index = g_list_index(ss->cd->list, start_info);
397 ss->slide_count = layout_selection_count(ss->lw, NULL);
398 ss->dir_fd = file_data_ref(ss->lw->dir_fd);
399 if (ss->slide_count < 2)
401 ss->slide_count = layout_list_count(ss->lw, NULL);
402 if (!options->slideshow.random && start_point >= 0 && (guint) start_point < ss->slide_count)
404 start_index = start_point;
409 ss->from_selection = TRUE;
413 slideshow_list_init(ss, start_index);
416 ss->slide_fd = file_data_ref(layout_image_get_fd(ss->lw));
418 ss->slide_fd = file_data_ref(image_get_fd(ss->imd));
420 if (slideshow_step(ss, TRUE))
422 slideshow_timer_reset(ss);
424 ss->stop_func = stop_func;
425 ss->stop_data = stop_data;
436 SlideShowData *slideshow_start_from_filelist(LayoutWindow *target_lw, ImageWindow *imd, GList *list,
437 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
439 return real_slideshow_start(target_lw, imd, list, -1, NULL, NULL, stop_func, stop_data);
442 SlideShowData *slideshow_start_from_collection(LayoutWindow *target_lw, ImageWindow *imd, CollectionData *cd,
443 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data,
444 CollectInfo *start_info)
446 return real_slideshow_start(target_lw, imd, NULL, -1, cd, start_info, stop_func, stop_data);
449 SlideShowData *slideshow_start(LayoutWindow *lw, gint start_point,
450 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data)
452 return real_slideshow_start(lw, NULL, NULL, start_point, NULL, NULL, stop_func, stop_data);
455 gboolean slideshow_paused(SlideShowData *ss)
457 if (!ss) return FALSE;
462 void slideshow_pause_set(SlideShowData *ss, gboolean paused)
469 gboolean slideshow_pause_toggle(SlideShowData *ss)
471 slideshow_pause_set(ss, !slideshow_paused(ss));
472 return slideshow_paused(ss);
474 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */