Mercurial > geeqie
annotate src/slideshow.c @ 516:b7e99bfeadc9
Convert the minority of while() to while ().
| author | zas_ |
|---|---|
| date | Thu, 24 Apr 2008 15:49:57 +0000 |
| parents | 985fdfebd89e |
| children | d84f2210a73c |
| 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" | |
| 138 | 18 #include "filelist.h" |
| 1 | 19 |
| 9 | 20 #include "layout.h" |
| 21 #include "layout_image.h" | |
| 22 #include "ui_fileops.h" | |
| 1 | 23 |
| 9 | 24 |
| 25 static void slideshow_timer_reset(SlideShowData *ss, gint reset); | |
| 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 |
| 9 | 32 slideshow_timer_reset(ss, FALSE); |
| 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 { |
| 58 gint i; | |
| 9 | 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 | |
| 155 static gint slideshow_step(SlideShowData *ss, gint forward) | |
| 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 | |
| 9 | 274 static void slideshow_timer_reset(SlideShowData *ss, gint reset) |
| 275 { | |
| 276 if (reset) | |
| 277 { | |
| 326 | 278 if (options->slideshow.delay < 1) options->slideshow.delay = 1; |
| 9 | 279 |
| 280 if (ss->timeout_id != -1) g_source_remove(ss->timeout_id); | |
| 326 | 281 ss->timeout_id = g_timeout_add(options->slideshow.delay * 1000 / SLIDESHOW_SUBSECOND_PRECISION, |
| 9 | 282 slideshow_loop_cb, ss); |
| 283 } | |
| 284 else if (ss->timeout_id != -1) | |
| 285 { | |
| 286 g_source_remove(ss->timeout_id); | |
| 287 ss->timeout_id = -1; | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 void slideshow_next(SlideShowData *ss) | |
| 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 |
| 9 | 295 if (!slideshow_step(ss, TRUE)) |
| 296 { | |
| 297 slideshow_free(ss); | |
| 298 return; | |
| 299 } | |
| 300 | |
| 301 slideshow_timer_reset(ss, TRUE); | |
| 302 } | |
| 303 | |
| 304 void slideshow_prev(SlideShowData *ss) | |
| 305 { | |
|
89
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
306 if (!ss) return; |
|
b3149a34d0dd
Sat Oct 28 14:41:10 2006 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
307 |
| 9 | 308 if (!slideshow_step(ss, FALSE)) |
| 309 { | |
| 310 slideshow_free(ss); | |
| 311 return; | |
| 312 } | |
| 313 | |
| 314 slideshow_timer_reset(ss, TRUE); | |
| 315 } | |
| 316 | |
| 317 static SlideShowData *real_slideshow_start(ImageWindow *imd, LayoutWindow *lw, | |
| 138 | 318 GList *filelist, gint start_point, |
| 9 | 319 CollectionData *cd, CollectInfo *start_info, |
| 320 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) | |
| 321 { | |
| 322 SlideShowData *ss; | |
| 323 gint start_index = -1; | |
| 324 | |
| 138 | 325 if (!filelist && !cd && layout_list_count(lw, NULL) < 1) return NULL; |
| 9 | 326 |
| 327 ss = g_new0(SlideShowData, 1); | |
| 328 | |
| 329 ss->imd = imd; | |
| 330 | |
| 138 | 331 ss->filelist = filelist; |
| 9 | 332 ss->cd = cd; |
| 333 ss->layout = lw; | |
| 334 ss->layout_path = NULL; | |
| 335 | |
| 336 ss->list = NULL; | |
| 337 ss->list_done = NULL; | |
| 338 | |
| 339 ss->from_selection = FALSE; | |
| 340 | |
|
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
341 ss->stop_func = NULL; |
| 9 | 342 |
| 343 ss->timeout_id = -1; | |
| 344 ss->paused = FALSE; | |
| 345 | |
| 138 | 346 if (ss->filelist) |
| 9 | 347 { |
| 138 | 348 ss->slide_count = g_list_length(ss->filelist); |
| 9 | 349 } |
| 350 else if (ss->cd) | |
| 351 { | |
| 352 collection_ref(ss->cd); | |
| 353 ss->slide_count = g_list_length(ss->cd->list); | |
| 326 | 354 if (!options->slideshow.random && start_info) |
| 9 | 355 { |
| 356 start_index = g_list_index(ss->cd->list, start_info); | |
| 357 } | |
| 358 } | |
| 359 else | |
| 360 { | |
| 361 /* layout method */ | |
| 362 | |
| 363 ss->slide_count = layout_selection_count(ss->layout, NULL); | |
| 364 ss->layout_path = g_strdup(layout_get_path(ss->layout)); | |
| 365 if (ss->slide_count < 2) | |
| 366 { | |
| 367 ss->slide_count = layout_list_count(ss->layout, NULL); | |
| 326 | 368 if (!options->slideshow.random && start_point >= 0 && start_point < ss->slide_count) |
| 9 | 369 { |
| 370 start_index = start_point; | |
| 371 } | |
| 372 } | |
| 373 else | |
| 374 { | |
| 375 ss->from_selection = TRUE; | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 slideshow_list_init(ss, start_index); | |
| 380 | |
| 138 | 381 ss->slide_fd = file_data_ref(image_get_fd(ss->imd)); |
| 9 | 382 if (slideshow_step(ss, TRUE)) |
| 383 { | |
| 384 slideshow_timer_reset(ss, TRUE); | |
|
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
385 |
|
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
386 ss->stop_func = stop_func; |
|
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
387 ss->stop_data = stop_data; |
| 9 | 388 } |
| 389 else | |
| 390 { | |
|
119
197b8d2e52ce
Thu Nov 30 09:55:42 2006 John Ellis <johne@verizon.net>
gqview
parents:
89
diff
changeset
|
391 slideshow_free(ss); |
| 9 | 392 ss = NULL; |
| 393 } | |
| 394 | |
| 395 return ss; | |
| 396 } | |
| 397 | |
| 138 | 398 SlideShowData *slideshow_start_from_filelist(ImageWindow *imd, GList *list, |
| 9 | 399 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) |
| 400 { | |
| 401 return real_slideshow_start(imd, NULL, list, -1, NULL, NULL, stop_func, stop_data); | |
| 402 } | |
| 403 | |
| 404 SlideShowData *slideshow_start_from_collection(ImageWindow *imd, CollectionData *cd, | |
| 405 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data, | |
| 406 CollectInfo *start_info) | |
| 407 { | |
| 408 return real_slideshow_start(imd, NULL, NULL, -1, cd, start_info, stop_func, stop_data); | |
| 409 } | |
| 410 | |
| 411 SlideShowData *slideshow_start(ImageWindow *imd, LayoutWindow *lw, gint start_point, | |
| 412 void (*stop_func)(SlideShowData *, gpointer), gpointer stop_data) | |
| 413 { | |
| 414 return real_slideshow_start(imd, lw, NULL, start_point, NULL, NULL, stop_func, stop_data); | |
| 415 } | |
| 416 | |
| 417 gint slideshow_paused(SlideShowData *ss) | |
| 418 { | |
| 419 if (!ss) return FALSE; | |
| 420 | |
| 421 return ss->paused; | |
| 422 } | |
| 423 | |
| 424 void slideshow_pause_set(SlideShowData *ss, gint paused) | |
| 425 { | |
| 426 if (!ss) return; | |
| 427 | |
| 428 ss->paused = paused; | |
| 429 } | |
| 430 | |
| 431 gint slideshow_pause_toggle(SlideShowData *ss) | |
| 432 { | |
| 433 slideshow_pause_set(ss, !slideshow_paused(ss)); | |
| 434 return slideshow_paused(ss); | |
| 435 } |
