Mercurial > geeqie.yaz
annotate src/slideshow.c @ 759:362c9a769bb7
use fputs instead of printf - patch by Uwe Ohse
author | nadvornik |
---|---|
date | Mon, 26 May 2008 18:22:08 +0000 |
parents | 41dc324ee014 |
children | d6a7fb4b8e7c |
rev | line source |
---|---|
1 | 1 /* |
196 | 2 * Geeqie |
9 | 3 * (C) 2004 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
1 | 5 * |
6 * Author: John Ellis | |
7 * | |
9 | 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! | |
1 | 11 */ |
12 | |
9 | 13 |
281 | 14 #include "main.h" |
9 | 15 #include "collect.h" |
16 #include "image.h" | |
17 #include "slideshow.h" | |
586 | 18 #include "filedata.h" |
1 | 19 |
9 | 20 #include "layout.h" |
21 #include "layout_image.h" | |
22 #include "ui_fileops.h" | |
1 | 23 |
9 | 24 |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
25 static void slideshow_timer_stop(SlideShowData *ss); |
9 | 26 |
1 | 27 |
9 | 28 void slideshow_free(SlideShowData *ss) |
29 { | |
89
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
30 if (!ss) return; |
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
31 |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
32 slideshow_timer_stop(ss); |
1 | 33 |
9 | 34 if (ss->stop_func) ss->stop_func(ss, ss->stop_data); |
3 | 35 |
138 | 36 if (ss->filelist) filelist_free(ss->filelist); |
9 | 37 if (ss->cd) collection_unref(ss->cd); |
38 g_free(ss->layout_path); | |
1 | 39 |
9 | 40 g_list_free(ss->list); |
41 g_list_free(ss->list_done); | |
1 | 42 |
138 | 43 file_data_unref(ss->slide_fd); |
9 | 44 |
45 g_free(ss); | |
1 | 46 } |
47 | |
9 | 48 static GList *generate_list(SlideShowData *ss) |
1 | 49 { |
50 GList *list = NULL; | |
51 | |
9 | 52 if (ss->from_selection) |
53 { | |
54 list = layout_selection_list_by_index(ss->layout); | |
55 } | |
56 else | |
1 | 57 { |
736 | 58 guint i; |
517 | 59 for (i = 0; i < ss->slide_count; i++) |
1 | 60 { |
61 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
62 } | |
9 | 63 list = g_list_reverse(list); |
1 | 64 } |
65 | |
66 return list; | |
67 } | |
68 | |
9 | 69 static GList *generate_random_list(SlideShowData *ss) |
1 | 70 { |
71 GList *src_list = NULL; | |
72 GList *list = NULL; | |
73 GList *work; | |
74 | |
9 | 75 src_list = generate_list(ss); |
1 | 76 |
516 | 77 while (src_list) |
1 | 78 { |
9 | 79 gint p = (double)rand() / ((double)RAND_MAX + 1.0) * g_list_length(src_list); |
1 | 80 work = g_list_nth(src_list, p); |
81 list = g_list_prepend(list, work->data); | |
82 src_list = g_list_remove(src_list, work->data); | |
83 } | |
84 | |
85 return list; | |
86 } | |
87 | |
9 | 88 static void slideshow_list_init(SlideShowData *ss, gint start_index) |
1 | 89 { |
9 | 90 if (ss->list_done) |
1 | 91 { |
9 | 92 g_list_free(ss->list_done); |
93 ss->list_done = NULL; | |
1 | 94 } |
95 | |
9 | 96 if (ss->list) g_list_free(ss->list); |
3 | 97 |
326 | 98 if (options->slideshow.random) |
1 | 99 { |
9 | 100 ss->list = generate_random_list(ss); |
1 | 101 } |
102 else | |
103 { | |
9 | 104 ss->list = generate_list(ss); |
105 if (start_index >= 0) | |
3 | 106 { |
9 | 107 /* start with specified image by skipping to it */ |
108 gint i = 0; | |
109 | |
516 | 110 while (ss->list && i < start_index) |
9 | 111 { |
513
985fdfebd89e
Remove whitespace between function name and first parenthesis for the sake of consistency. (pass 2)
zas_
parents:
475
diff
changeset
|
112 ss->list_done = g_list_prepend(ss->list_done, ss->list->data); |
9 | 113 ss->list = g_list_remove(ss->list, ss->list->data); |
114 i++; | |
115 } | |
3 | 116 } |
117 } | |
118 } | |
119 | |
9 | 120 gint slideshow_should_continue(SlideShowData *ss) |
1 | 121 { |
138 | 122 FileData *imd_fd; |
9 | 123 const gchar *path; |
124 | |
89
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
125 if (!ss) return FALSE; |
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
126 |
138 | 127 imd_fd = image_get_fd(ss->imd); |
9 | 128 |
138 | 129 if ( ((imd_fd == NULL) != (ss->slide_fd == NULL)) || |
130 (imd_fd && ss->slide_fd && imd_fd != ss->slide_fd) ) return FALSE; | |
9 | 131 |
138 | 132 if (ss->filelist) return TRUE; |
9 | 133 |
134 if (ss->cd) | |
135 { | |
136 if (g_list_length(ss->cd->list) == ss->slide_count) | |
137 return TRUE; | |
138 else | |
139 return FALSE; | |
140 } | |
141 | |
142 if (!ss->layout) return FALSE; | |
143 path = layout_get_path(ss->layout); | |
144 | |
145 if (path && ss->layout_path && | |
146 strcmp(path, ss->layout_path) == 0) | |
147 { | |
148 if (ss->from_selection && ss->slide_count == layout_selection_count(ss->layout, NULL)) return TRUE; | |
149 if (!ss->from_selection && ss->slide_count == layout_list_count(ss->layout, NULL)) return TRUE; | |
150 } | |
151 | |
152 return FALSE; | |
153 } | |
154 | |
745
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
155 static gint slideshow_step(SlideShowData *ss, gboolean forward) |
9 | 156 { |
157 gint row; | |
158 | |
159 if (!slideshow_should_continue(ss)) | |
1 | 160 { |
161 return FALSE; | |
162 } | |
163 | |
9 | 164 if (forward) |
165 { | |
166 if (!ss->list) return TRUE; | |
1 | 167 |
9 | 168 row = GPOINTER_TO_INT(ss->list->data); |
513
985fdfebd89e
Remove whitespace between function name and first parenthesis for the sake of consistency. (pass 2)
zas_
parents:
475
diff
changeset
|
169 ss->list_done = g_list_prepend(ss->list_done, ss->list->data); |
9 | 170 ss->list = g_list_remove(ss->list, ss->list->data); |
171 } | |
172 else | |
173 { | |
174 if (!ss->list_done || !ss->list_done->next) return TRUE; | |
3 | 175 |
9 | 176 ss->list = g_list_prepend(ss->list, ss->list_done->data); |
177 ss->list_done = g_list_remove(ss->list_done, ss->list_done->data); | |
178 row = GPOINTER_TO_INT(ss->list_done->data); | |
3 | 179 } |
180 | |
138 | 181 file_data_unref(ss->slide_fd); |
182 ss->slide_fd = NULL; | |
3 | 183 |
138 | 184 if (ss->filelist) |
9 | 185 { |
138 | 186 ss->slide_fd = file_data_ref((FileData *)g_list_nth_data(ss->filelist, row)); |
334 | 187 image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd, options->image.zoom_mode)); |
9 | 188 } |
189 else if (ss->cd) | |
190 { | |
191 CollectInfo *info; | |
3 | 192 |
9 | 193 info = g_list_nth_data(ss->cd->list, row); |
138 | 194 ss->slide_fd = file_data_ref(info->fd); |
9 | 195 |
334 | 196 image_change_from_collection(ss->imd, ss->cd, info, image_zoom_get_default(ss->imd, options->image.zoom_mode)); |
3 | 197 } |
198 else | |
199 { | |
138 | 200 ss->slide_fd = file_data_ref(layout_list_get_fd(ss->layout, row)); |
9 | 201 |
202 if (ss->from_selection) | |
203 { | |
334 | 204 image_change_fd(ss->imd, ss->slide_fd, image_zoom_get_default(ss->imd, options->image.zoom_mode)); |
9 | 205 layout_status_update_info(ss->layout, NULL); |
206 } | |
207 else | |
208 { | |
209 layout_image_set_index(ss->layout, row); | |
210 } | |
3 | 211 } |
212 | |
326 | 213 if (!ss->list && options->slideshow.repeat) |
9 | 214 { |
215 slideshow_list_init(ss, -1); | |
216 } | |
3 | 217 |
9 | 218 if (!ss->list) |
1 | 219 { |
220 return FALSE; | |
221 } | |
222 | |
9 | 223 /* read ahead */ |
1 | 224 |
334 | 225 if (options->image.enable_read_ahead) |
1 | 226 { |
9 | 227 gint r; |
228 if (forward) | |
229 { | |
230 if (!ss->list) return TRUE; | |
231 r = GPOINTER_TO_INT(ss->list->data); | |
232 } | |
233 else | |
234 { | |
235 if (!ss->list_done || !ss->list_done->next) return TRUE; | |
236 r = GPOINTER_TO_INT(ss->list_done->next->data); | |
237 } | |
1 | 238 |
138 | 239 if (ss->filelist) |
9 | 240 { |
138 | 241 image_prebuffer_set(ss->imd, g_list_nth_data(ss->filelist, r)); |
9 | 242 } |
243 else if (ss->cd) | |
244 { | |
245 CollectInfo *info; | |
246 info = g_list_nth_data(ss->cd->list, r); | |
138 | 247 if (info) image_prebuffer_set(ss->imd, info->fd); |
9 | 248 } |
249 else if (ss->from_selection) | |
250 { | |
138 | 251 image_prebuffer_set(ss->imd, layout_list_get_fd(ss->layout, r)); |
9 | 252 } |
1 | 253 } |
254 | |
255 return TRUE; | |
256 } | |
257 | |
3 | 258 static gint slideshow_loop_cb(gpointer data) |
259 { | |
9 | 260 SlideShowData *ss = data; |
1 | 261 |
9 | 262 if (ss->paused) return TRUE; |
1 | 263 |
9 | 264 if (!slideshow_step(ss, TRUE)) |
3 | 265 { |
9 | 266 ss->timeout_id = -1; |
267 slideshow_free(ss); | |
1 | 268 return FALSE; |
269 } | |
270 | |
271 return TRUE; | |
272 } | |
273 | |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
274 static void slideshow_timer_stop(SlideShowData *ss) |
9 | 275 { |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
276 if (ss->timeout_id == -1) return; |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
277 |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
278 g_source_remove(ss->timeout_id); |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
279 ss->timeout_id = -1; |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
280 } |
9 | 281 |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
282 static void slideshow_timer_reset(SlideShowData *ss) |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
283 { |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
284 if (options->slideshow.delay < 1) options->slideshow.delay = 1; |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
285 |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
286 if (ss->timeout_id != -1) g_source_remove(ss->timeout_id); |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
287 ss->timeout_id = g_timeout_add(options->slideshow.delay * 1000 / SLIDESHOW_SUBSECOND_PRECISION, |
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
288 slideshow_loop_cb, ss); |
9 | 289 } |
290 | |
745
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
291 static void slideshow_move(SlideShowData *ss, gboolean forward) |
9 | 292 { |
89
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
293 if (!ss) return; |
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
294 |
745
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
295 if (!slideshow_step(ss, forward)) |
9 | 296 { |
297 slideshow_free(ss); | |
298 return; | |
299 } | |
300 | |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
301 slideshow_timer_reset(ss); |
9 | 302 } |
303 | |
745
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
304 void slideshow_next(SlideShowData *ss) |
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
305 { |
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
306 slideshow_move(ss, TRUE); |
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
307 } |
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
308 |
9 | 309 void slideshow_prev(SlideShowData *ss) |
310 { | |
745
41dc324ee014
slideshow_next(), slideshow_prev(): move code to common slideshow_move(), reducing code redundancy.
zas_
parents:
744
diff
changeset
|
311 slideshow_move(ss, FALSE); |
9 | 312 } |
313 | |
314 static SlideShowData *real_slideshow_start(ImageWindow *imd, LayoutWindow *lw, | |
138 | 315 GList *filelist, gint start_point, |
9 | 316 CollectionData *cd, CollectInfo *start_info, |
317 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) | |
318 { | |
319 SlideShowData *ss; | |
320 gint start_index = -1; | |
321 | |
138 | 322 if (!filelist && !cd && layout_list_count(lw, NULL) < 1) return NULL; |
9 | 323 |
324 ss = g_new0(SlideShowData, 1); | |
325 | |
326 ss->imd = imd; | |
327 | |
138 | 328 ss->filelist = filelist; |
9 | 329 ss->cd = cd; |
330 ss->layout = lw; | |
331 ss->layout_path = NULL; | |
332 | |
333 ss->list = NULL; | |
334 ss->list_done = NULL; | |
335 | |
336 ss->from_selection = FALSE; | |
337 | |
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
338 ss->stop_func = NULL; |
9 | 339 |
340 ss->timeout_id = -1; | |
341 ss->paused = FALSE; | |
342 | |
138 | 343 if (ss->filelist) |
9 | 344 { |
138 | 345 ss->slide_count = g_list_length(ss->filelist); |
9 | 346 } |
347 else if (ss->cd) | |
348 { | |
349 collection_ref(ss->cd); | |
350 ss->slide_count = g_list_length(ss->cd->list); | |
326 | 351 if (!options->slideshow.random && start_info) |
9 | 352 { |
353 start_index = g_list_index(ss->cd->list, start_info); | |
354 } | |
355 } | |
356 else | |
357 { | |
358 /* layout method */ | |
359 | |
360 ss->slide_count = layout_selection_count(ss->layout, NULL); | |
361 ss->layout_path = g_strdup(layout_get_path(ss->layout)); | |
362 if (ss->slide_count < 2) | |
363 { | |
364 ss->slide_count = layout_list_count(ss->layout, NULL); | |
736 | 365 if (!options->slideshow.random && start_point >= 0 && (guint) start_point < ss->slide_count) |
9 | 366 { |
367 start_index = start_point; | |
368 } | |
369 } | |
370 else | |
371 { | |
372 ss->from_selection = TRUE; | |
373 } | |
374 } | |
375 | |
376 slideshow_list_init(ss, start_index); | |
377 | |
138 | 378 ss->slide_fd = file_data_ref(image_get_fd(ss->imd)); |
9 | 379 if (slideshow_step(ss, TRUE)) |
380 { | |
744
eb7e0d3206a2
Move code from slideshow_timer_reset() to new slideshow_timer_stop() and simplify calls.
zas_
parents:
736
diff
changeset
|
381 slideshow_timer_reset(ss); |
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
382 |
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
383 ss->stop_func = stop_func; |
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
384 ss->stop_data = stop_data; |
9 | 385 } |
386 else | |
387 { | |
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
388 slideshow_free(ss); |
9 | 389 ss = NULL; |
390 } | |
391 | |
392 return ss; | |
393 } | |
394 | |
138 | 395 SlideShowData *slideshow_start_from_filelist(ImageWindow *imd, GList *list, |
9 | 396 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) |
397 { | |
398 return real_slideshow_start(imd, NULL, list, -1, NULL, NULL, stop_func, stop_data); | |
399 } | |
400 | |
401 SlideShowData *slideshow_start_from_collection(ImageWindow *imd, CollectionData *cd, | |
402 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data, | |
403 CollectInfo *start_info) | |
404 { | |
405 return real_slideshow_start(imd, NULL, NULL, -1, cd, start_info, stop_func, stop_data); | |
406 } | |
407 | |
408 SlideShowData *slideshow_start(ImageWindow *imd, LayoutWindow *lw, gint start_point, | |
409 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) | |
410 { | |
411 return real_slideshow_start(imd, lw, NULL, start_point, NULL, NULL, stop_func, stop_data); | |
412 } | |
413 | |
414 gint slideshow_paused(SlideShowData *ss) | |
415 { | |
416 if (!ss) return FALSE; | |
417 | |
418 return ss->paused; | |
419 } | |
420 | |
421 void slideshow_pause_set(SlideShowData *ss, gint paused) | |
422 { | |
423 if (!ss) return; | |
424 | |
425 ss->paused = paused; | |
426 } | |
427 | |
428 gint slideshow_pause_toggle(SlideShowData *ss) | |
429 { | |
430 slideshow_pause_set(ss, !slideshow_paused(ss)); | |
431 return slideshow_paused(ss); | |
432 } |