first stereo support
[geeqie.git] / src / image-load.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2010 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 "image-load.h"
16
17 #include "exif.h"
18 #include "filedata.h"
19 #include "ui_fileops.h"
20 #include "gq-marshal.h"
21
22 #include <fcntl.h>
23 #include <sys/mman.h>
24
25 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT   4096
26 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT       1
27
28
29 /**************************************************************************************/
30 /* image loader class */
31
32
33 enum {
34         SIGNAL_AREA_READY = 0,
35         SIGNAL_ERROR,
36         SIGNAL_DONE,
37         SIGNAL_PERCENT,
38         SIGNAL_SIZE,
39         SIGNAL_COUNT
40 };
41
42 static guint signals[SIGNAL_COUNT] = { 0 };
43
44 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
45 static void image_loader_class_init(ImageLoaderClass *class);
46 static void image_loader_finalize(GObject *object);
47 static void image_loader_stop(ImageLoader *il);
48
49 GType image_loader_get_type(void)
50 {
51         static GType type = 0;
52         if (type == 0) 
53                 {
54                 static const GTypeInfo info = {
55                         sizeof(ImageLoaderClass),
56                         NULL,   /* base_init */
57                         NULL,   /* base_finalize */
58                         (GClassInitFunc)image_loader_class_init, /* class_init */
59                         NULL,   /* class_finalize */
60                         NULL,   /* class_data */
61                         sizeof(ImageLoader),
62                         0,      /* n_preallocs */
63                         (GInstanceInitFunc)image_loader_init, /* instance_init */
64                         NULL    /* value_table */
65                         };
66                 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
67                 }
68         return type;
69 }
70
71 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
72 {
73         ImageLoader *il = (ImageLoader *)instance;
74
75         il->pixbuf = NULL;
76         il->idle_id = 0;
77         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
78         il->done = FALSE;
79         il->loader = NULL;
80
81         il->bytes_read = 0;
82         il->bytes_total = 0;
83
84         il->idle_done_id = 0;
85
86         il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
87         il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
88         il->mapped_file = NULL;
89
90         il->requested_width = 0;
91         il->requested_height = 0;
92         il->shrunk = FALSE;
93
94         il->can_destroy = TRUE;
95
96 #ifdef HAVE_GTHREAD
97         il->data_mutex = g_mutex_new();
98         il->can_destroy_cond = g_cond_new();
99 #endif
100         DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
101 }
102
103 static void image_loader_class_init(ImageLoaderClass *class)
104 {
105         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
106
107 //      gobject_class->set_property = image_loader_set_property;
108 //      gobject_class->get_property = image_loader_get_property;
109
110         gobject_class->finalize = image_loader_finalize;
111
112
113         signals[SIGNAL_AREA_READY] =
114                 g_signal_new("area_ready",
115                              G_OBJECT_CLASS_TYPE(gobject_class),
116                              G_SIGNAL_RUN_LAST,
117                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
118                              NULL, NULL,
119                              gq_marshal_VOID__INT_INT_INT_INT,
120                              G_TYPE_NONE, 4,
121                              G_TYPE_INT,
122                              G_TYPE_INT,
123                              G_TYPE_INT,
124                              G_TYPE_INT);
125
126         signals[SIGNAL_ERROR] =
127                 g_signal_new("error",
128                              G_OBJECT_CLASS_TYPE(gobject_class),
129                              G_SIGNAL_RUN_LAST,
130                              G_STRUCT_OFFSET(ImageLoaderClass, error),
131                              NULL, NULL,
132                              g_cclosure_marshal_VOID__VOID,
133                              G_TYPE_NONE, 0);
134
135         signals[SIGNAL_DONE] =
136                 g_signal_new("done",
137                              G_OBJECT_CLASS_TYPE(gobject_class),
138                              G_SIGNAL_RUN_LAST,
139                              G_STRUCT_OFFSET(ImageLoaderClass, done),
140                              NULL, NULL,
141                              g_cclosure_marshal_VOID__VOID,
142                              G_TYPE_NONE, 0);
143
144         signals[SIGNAL_PERCENT] =
145                 g_signal_new("percent",
146                              G_OBJECT_CLASS_TYPE(gobject_class),
147                              G_SIGNAL_RUN_LAST,
148                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
149                              NULL, NULL,
150                              g_cclosure_marshal_VOID__DOUBLE,
151                              G_TYPE_NONE, 1,
152                              G_TYPE_DOUBLE);
153
154         signals[SIGNAL_SIZE] =
155                 g_signal_new("size_prepared",
156                              G_OBJECT_CLASS_TYPE(gobject_class),
157                              G_SIGNAL_RUN_LAST,
158                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
159                              NULL, NULL,
160                              gq_marshal_VOID__INT_INT,
161                              G_TYPE_NONE, 2,
162                              G_TYPE_INT,
163                              G_TYPE_INT);
164
165 }
166
167 static void image_loader_finalize(GObject *object)
168 {
169         ImageLoader *il = (ImageLoader *)object;
170
171         image_loader_stop(il);
172
173         if (il->error) DEBUG_1("%s", image_loader_get_error(il));
174
175         DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
176
177         if (il->idle_done_id)
178                 {
179                 g_source_remove(il->idle_done_id);
180                 il->idle_done_id = 0;
181                 }
182
183         while (g_source_remove_by_user_data(il)) 
184                 {
185                 DEBUG_2("pending signals detected");
186                 }
187         
188         while (il->area_param_list) 
189                 {
190                 DEBUG_1("pending area_ready signals detected");
191                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
192                 g_free(il->area_param_list->data);
193                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
194                 }
195
196         while (il->area_param_delayed_list) 
197                 {
198                 g_free(il->area_param_delayed_list->data);
199                 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
200                 }
201
202         if (il->pixbuf) g_object_unref(il->pixbuf);
203         
204         if (il->error) g_error_free(il->error);
205
206         file_data_unref(il->fd);
207 #ifdef HAVE_GTHREAD
208         g_mutex_free(il->data_mutex);
209         g_cond_free(il->can_destroy_cond);
210 #endif
211 }
212
213 void image_loader_free(ImageLoader *il)
214 {
215         if (!il) return;
216         g_object_unref(G_OBJECT(il));
217 }
218
219
220 ImageLoader *image_loader_new(FileData *fd)
221 {
222         ImageLoader *il;
223
224         if (!fd) return NULL;
225
226         il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
227         
228         il->fd = file_data_ref(fd);
229         
230         return il;
231 }
232
233 /**************************************************************************************/
234 /* send signals via idle calbacks - the callback are executed in the main thread */
235
236 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
237 struct _ImageLoaderAreaParam {
238         ImageLoader *il;
239         guint x;
240         guint y;
241         guint w;
242         guint h;
243 };
244
245
246 static gboolean image_loader_emit_area_ready_cb(gpointer data)
247 {
248         ImageLoaderAreaParam *par = data;
249         ImageLoader *il = par->il;
250         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
251         g_mutex_lock(il->data_mutex);
252         il->area_param_list = g_list_remove(il->area_param_list, par);
253         g_free(par);
254         g_mutex_unlock(il->data_mutex);
255         
256         return FALSE;
257 }
258
259 static gboolean image_loader_emit_done_cb(gpointer data)
260 {
261         ImageLoader *il = data;
262         g_signal_emit(il, signals[SIGNAL_DONE], 0);
263         return FALSE;
264 }
265
266 static gboolean image_loader_emit_error_cb(gpointer data)
267 {
268         ImageLoader *il = data;
269         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
270         return FALSE;
271 }
272
273 static gboolean image_loader_emit_percent_cb(gpointer data)
274 {
275         ImageLoader *il = data;
276         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
277         return FALSE;
278 }
279
280 /* DONE and ERROR are emited only once, thus they can have normal priority
281    PERCENT and AREA_READY should be processed ASAP
282 */
283
284 static void image_loader_emit_done(ImageLoader *il)
285 {
286         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
287 }
288
289 static void image_loader_emit_error(ImageLoader *il)
290 {
291         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
292 }
293
294 static void image_loader_emit_percent(ImageLoader *il)
295 {
296         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
297 }
298
299 /* this function expects that il->data_mutex is locked by caller */
300 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
301 {
302         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
303         par->il = il;
304         par->x = x;
305         par->y = y;
306         par->w = w;
307         par->h = h;
308         
309         il->area_param_list = g_list_prepend(il->area_param_list, par);
310         
311         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
312 }
313
314 /**************************************************************************************/
315 /* the following functions may be executed in separate thread */
316
317 /* this function expects that il->data_mutex is locked by caller */
318 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
319 {
320         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
321         par->il = il;
322         par->x = x;
323         par->y = y;
324         par->w = w;
325         par->h = h;
326         
327         il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
328 }
329
330
331
332 static gboolean image_loader_get_stopping(ImageLoader *il)
333 {
334         gboolean ret;
335         if (!il) return FALSE;
336
337         g_mutex_lock(il->data_mutex);
338         ret = il->stopping;
339         g_mutex_unlock(il->data_mutex);
340
341         return ret;
342 }
343
344
345 static void image_loader_sync_pixbuf(ImageLoader *il)
346 {
347         GdkPixbuf *pb;
348         
349         g_mutex_lock(il->data_mutex);
350         
351         if (!il->loader) 
352                 {
353                 g_mutex_unlock(il->data_mutex);
354                 return;
355                 }
356
357         pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
358
359         if (pb == il->pixbuf)
360                 {
361                 g_mutex_unlock(il->data_mutex);
362                 return;
363                 }
364
365         if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
366                 {
367                 g_object_set_data(G_OBJECT(pb), "stereo_sbs", GINT_TO_POINTER(1));
368                 }
369
370         if (il->pixbuf) g_object_unref(il->pixbuf);
371
372         il->pixbuf = pb;
373         if (il->pixbuf) g_object_ref(il->pixbuf);
374
375         g_mutex_unlock(il->data_mutex);
376 }
377
378 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
379                                  guint x, guint y, guint w, guint h,
380                                  gpointer data)
381 {
382         ImageLoader *il = data;
383
384         if (!image_loader_get_pixbuf(il))
385                 {
386                 image_loader_sync_pixbuf(il);
387                 if (!image_loader_get_pixbuf(il))
388                         {
389                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
390                         }
391                 }
392
393         g_mutex_lock(il->data_mutex);
394         if (il->delay_area_ready)
395                 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
396         else
397                 image_loader_emit_area_ready(il, x, y, w, h);
398         g_mutex_unlock(il->data_mutex);
399 }
400
401 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
402 {
403         GdkPixbuf *pb;
404         guchar *pix;
405         size_t h, rs;
406         
407         /* a workaround for 
408            http://bugzilla.gnome.org/show_bug.cgi?id=547669 
409            http://bugzilla.gnome.org/show_bug.cgi?id=589334
410         */
411         gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
412         if (strcmp(format, "svg") == 0 ||
413             strcmp(format, "xpm") == 0)
414                 {
415                 g_free(format);
416                 return;
417                 }
418         
419         g_free(format);
420
421         pb = gdk_pixbuf_loader_get_pixbuf(loader);
422         
423         h = gdk_pixbuf_get_height(pb);
424         rs = gdk_pixbuf_get_rowstride(pb);
425         pix = gdk_pixbuf_get_pixels(pb);
426         
427         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
428
429 }
430
431 static void image_loader_size_cb(GdkPixbufLoader *loader,
432                                  gint width, gint height, gpointer data)
433 {
434         ImageLoader *il = data;
435         GdkPixbufFormat *format;
436         gchar **mime_types;
437         gboolean scale = FALSE;
438         gint n;
439
440         g_mutex_lock(il->data_mutex);
441         if (il->requested_width < 1 || il->requested_height < 1) 
442                 {
443                 g_mutex_unlock(il->data_mutex);
444                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
445                 return;
446                 }
447         g_mutex_unlock(il->data_mutex);
448
449         format = gdk_pixbuf_loader_get_format(loader);
450         if (!format) return;
451
452         mime_types = gdk_pixbuf_format_get_mime_types(format);
453         n = 0;
454         while (mime_types[n])
455                 {
456                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
457                 n++;
458                 }
459         g_strfreev(mime_types);
460
461         if (!scale)
462                 {
463                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
464                 return;
465                 }
466
467         g_mutex_lock(il->data_mutex);
468
469         gint nw, nh;
470         if (width > il->requested_width || height > il->requested_height)
471                 {
472
473                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
474                         {
475                         nw = il->requested_width;
476                         nh = (gdouble)nw / width * height;
477                         if (nh < 1) nh = 1;
478                         }
479                 else
480                         {
481                         nh = il->requested_height;
482                         nw = (gdouble)nh / height * width;
483                         if (nw < 1) nw = 1;
484                         }
485
486                 gdk_pixbuf_loader_set_size(loader, nw, nh);
487                 il->shrunk = TRUE;
488                 }
489         g_mutex_unlock(il->data_mutex);
490
491         g_signal_emit(il, signals[SIGNAL_SIZE], 0, nw, nh);
492 }
493
494 static void image_loader_stop_loader(ImageLoader *il)
495 {
496         if (!il) return;
497
498         if (il->loader)
499                 {
500                 /* some loaders do not have a pixbuf till close, order is important here */
501                 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
502                 image_loader_sync_pixbuf(il);
503                 g_object_unref(G_OBJECT(il->loader));
504                 il->loader = NULL;
505                 }
506         g_mutex_lock(il->data_mutex);
507         il->done = TRUE;
508         g_mutex_unlock(il->data_mutex);
509 }
510
511 static void image_loader_setup_loader(ImageLoader *il)
512 {
513         g_mutex_lock(il->data_mutex);
514         il->loader = gdk_pixbuf_loader_new();
515
516         g_signal_connect(G_OBJECT(il->loader), "area_updated",
517                          G_CALLBACK(image_loader_area_updated_cb), il);
518         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
519                          G_CALLBACK(image_loader_size_cb), il);
520         g_signal_connect(G_OBJECT(il->loader), "area_prepared",
521                          G_CALLBACK(image_loader_area_prepared_cb), il);
522         g_mutex_unlock(il->data_mutex);
523 }
524
525
526 static void image_loader_done(ImageLoader *il)
527 {
528         image_loader_stop_loader(il);
529
530         image_loader_emit_done(il);
531 }
532
533 static void image_loader_error(ImageLoader *il)
534 {
535         image_loader_stop_loader(il);
536
537         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
538
539         image_loader_emit_error(il);
540 }
541
542 static gboolean image_loader_continue(ImageLoader *il)
543 {
544         gint b;
545         gint c;
546
547         if (!il) return FALSE;
548
549         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
550         while (c > 0 && !image_loader_get_stopping(il))
551                 {
552                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
553
554                 if (b == 0)
555                         {
556                         image_loader_done(il);
557                         return FALSE;
558                         }
559
560                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
561                         {
562                         image_loader_error(il);
563                         return FALSE;
564                         }
565
566                 il->bytes_read += b;
567
568                 c--;
569                 }
570
571         if (il->bytes_total > 0)
572                 {
573                 image_loader_emit_percent(il);
574                 }
575
576         return TRUE;
577 }
578
579 static gboolean image_loader_begin(ImageLoader *il)
580 {
581         gssize b;
582         
583         if (il->pixbuf) return FALSE;
584
585         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
586         if (b < 1) return FALSE;
587
588         image_loader_setup_loader(il);
589
590         if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
591                 {
592                 image_loader_stop_loader(il);
593                 return FALSE;
594                 }
595
596         il->bytes_read += b;
597
598         /* read until size is known */
599         while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
600                 {
601                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
602                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
603                         {
604                         image_loader_stop_loader(il);
605                         return FALSE;
606                         }
607                 il->bytes_read += b;
608                 }
609         if (!il->pixbuf) image_loader_sync_pixbuf(il);
610
611         if (il->bytes_read == il->bytes_total || b < 1)
612                 {
613                 /* done, handle (broken) loaders that do not have pixbuf till close */
614                 image_loader_stop_loader(il);
615
616                 if (!il->pixbuf) return FALSE;
617
618                 image_loader_done(il);
619                 return TRUE;
620                 }
621
622         if (!il->pixbuf)
623                 {
624                 image_loader_stop_loader(il);
625                 return FALSE;
626                 }
627
628         return TRUE;
629 }
630
631 /**************************************************************************************/
632 /* the following functions are always executed in the main thread */
633
634
635 static gboolean image_loader_setup_source(ImageLoader *il)
636 {
637         struct stat st;
638         gchar *pathl;
639
640         if (!il || il->loader || il->mapped_file) return FALSE;
641
642         il->mapped_file = NULL;
643
644         if (il->fd)
645                 {
646                 ExifData *exif = exif_read_fd(il->fd);
647
648                 if (options->thumbnails.use_exif)
649                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
650                 else
651                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
652
653                 if (il->mapped_file)
654                         {
655                         il->preview = TRUE;
656                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
657                         }
658                 exif_free_fd(il->fd, exif);
659                 }
660
661         
662         if (!il->mapped_file)
663                 {
664                 /* normal file */
665                 gint load_fd;
666         
667                 pathl = path_from_utf8(il->fd->path);
668                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
669                 g_free(pathl);
670                 if (load_fd == -1) return FALSE;
671
672                 if (fstat(load_fd, &st) == 0)
673                         {
674                         il->bytes_total = st.st_size;
675                         }
676                 else
677                         {
678                         close(load_fd);
679                         return FALSE;
680                         }
681                 
682                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
683                 close(load_fd);
684                 if (il->mapped_file == MAP_FAILED)
685                         {
686                         il->mapped_file = 0;
687                         return FALSE;
688                         }
689                 il->preview = FALSE;
690                 }
691                 
692         return TRUE;
693 }
694
695 static void image_loader_stop_source(ImageLoader *il)
696 {
697         if (!il) return;
698         
699         if (il->mapped_file)
700                 {
701                 if (il->preview)
702                         {
703                         exif_free_preview(il->mapped_file);
704                         }
705                 else
706                         {
707                         munmap(il->mapped_file, il->bytes_total);
708                         }
709                 il->mapped_file = NULL;
710                 }
711 }
712
713 static void image_loader_stop(ImageLoader *il)
714 {
715         if (!il) return;
716
717         if (il->idle_id)
718                 {
719                 g_source_remove(il->idle_id);
720                 il->idle_id = 0;
721                 }
722                 
723         if (il->thread)
724                 {
725                 /* stop loader in the other thread */
726                 g_mutex_lock(il->data_mutex);
727                 il->stopping = TRUE;
728                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
729                 g_mutex_unlock(il->data_mutex);
730                 }
731
732         image_loader_stop_loader(il);
733         image_loader_stop_source(il);
734         
735 }
736
737 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
738 {
739         g_mutex_lock(il->data_mutex);
740         il->delay_area_ready = enable;
741         if (!enable)
742                 {
743                 /* send delayed */
744                 GList *list, *work;
745                 list = g_list_reverse(il->area_param_delayed_list);
746                 il->area_param_delayed_list = NULL;
747                 g_mutex_unlock(il->data_mutex);
748
749                 work = list;
750
751                 while (work)
752                         {
753                         ImageLoaderAreaParam *par = work->data;
754                         work = work->next;
755                         
756                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
757                         g_free(par);
758                         }
759                 g_list_free(list);
760                 }
761         else
762                 {
763                 /* just unlock */
764                 g_mutex_unlock(il->data_mutex);
765                 }
766 }
767
768
769 /**************************************************************************************/
770 /* execution via idle calls */
771
772 static gboolean image_loader_idle_cb(gpointer data)
773 {
774         gboolean ret = FALSE;
775         ImageLoader *il = data;
776
777         if (il->idle_id)
778                 {
779                 ret = image_loader_continue(il);
780                 }
781         
782         if (!ret)
783                 {
784                 image_loader_stop_source(il);
785                 }
786         
787         return ret;
788 }
789
790
791 static gboolean image_loader_start_idle(ImageLoader *il)
792 {
793         gboolean ret;
794         
795         if (!il) return FALSE;
796
797         if (!il->fd) return FALSE;
798
799         if (!image_loader_setup_source(il)) return FALSE;
800         
801         ret = image_loader_begin(il);
802
803         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
804         return ret;
805 }
806
807 /**************************************************************************************/
808 /* execution via thread */
809
810 #ifdef HAVE_GTHREAD
811 static GThreadPool *image_loader_thread_pool = NULL;
812
813 static GCond *image_loader_prio_cond = NULL;
814 static GMutex *image_loader_prio_mutex = NULL;
815 static gint image_loader_prio_num = 0;
816
817
818 static void image_loader_thread_enter_high(void)
819 {
820         g_mutex_lock(image_loader_prio_mutex);
821         image_loader_prio_num++;
822         g_mutex_unlock(image_loader_prio_mutex);
823 }
824
825 static void image_loader_thread_leave_high(void)
826 {
827         g_mutex_lock(image_loader_prio_mutex);
828         image_loader_prio_num--;
829         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
830         g_mutex_unlock(image_loader_prio_mutex);
831 }
832
833 static void image_loader_thread_wait_high(void)
834 {
835         g_mutex_lock(image_loader_prio_mutex);
836         while (image_loader_prio_num) 
837                 {
838                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
839                 }
840
841         g_mutex_unlock(image_loader_prio_mutex);
842 }
843
844
845 static void image_loader_thread_run(gpointer data, gpointer user_data)
846 {
847         ImageLoader *il = data;
848         gboolean cont;
849         gboolean err;
850         
851         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
852                 {
853                 /* low prio, wait untill high prio tasks finishes */
854                 image_loader_thread_wait_high();
855                 }
856         else
857                 {
858                 /* high prio */
859                 image_loader_thread_enter_high();
860                 }
861         
862         err = !image_loader_begin(il);
863         
864         if (err)
865                 {
866                 /* 
867                 loader failed, we have to send signal 
868                 (idle mode returns the image_loader_begin return value directly)
869                 (success is always reported indirectly from image_loader_begin)
870                 */
871                 image_loader_emit_error(il);
872                 }
873         
874         cont = !err;
875         
876         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
877                 {
878                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
879                         {
880                         /* low prio, wait untill high prio tasks finishes */
881                         image_loader_thread_wait_high();
882                         }
883                 cont = image_loader_continue(il);
884                 }
885         image_loader_stop_loader(il);
886
887         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
888                 {
889                 /* high prio */
890                 image_loader_thread_leave_high();
891                 }
892
893         g_mutex_lock(il->data_mutex);
894         il->can_destroy = TRUE;
895         g_cond_signal(il->can_destroy_cond);
896         g_mutex_unlock(il->data_mutex);
897
898 }
899
900
901 static gboolean image_loader_start_thread(ImageLoader *il)
902 {
903         if (!il) return FALSE;
904
905         if (!il->fd) return FALSE;
906
907         il->thread = TRUE;
908         
909         if (!image_loader_setup_source(il)) return FALSE;
910
911         if (!image_loader_thread_pool) 
912                 {
913                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
914                 image_loader_prio_cond = g_cond_new();
915                 image_loader_prio_mutex = g_mutex_new();
916                 }
917
918         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
919
920         g_thread_pool_push(image_loader_thread_pool, il, NULL);
921         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
922                 
923         return TRUE;
924 }
925 #endif /* HAVE_GTHREAD */
926
927
928 /**************************************************************************************/
929 /* public interface */
930
931
932 gboolean image_loader_start(ImageLoader *il)
933 {
934         if (!il) return FALSE;
935
936         if (!il->fd) return FALSE;
937
938 #ifdef HAVE_GTHREAD
939         return image_loader_start_thread(il);
940 #else
941         return image_loader_start_idle(il);
942 #endif
943 }
944
945
946 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
947 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
948 {
949         GdkPixbuf *ret;
950         if (!il) return NULL;
951         
952         g_mutex_lock(il->data_mutex);
953         ret = il->pixbuf;
954         g_mutex_unlock(il->data_mutex);
955         return ret;
956 }
957
958 gchar *image_loader_get_format(ImageLoader *il)
959 {
960         GdkPixbufFormat *format;
961         gchar **mimev;
962         gchar *mime;
963
964         if (!il || !il->loader) return NULL;
965
966         format = gdk_pixbuf_loader_get_format(il->loader);
967         if (!format) return NULL;
968
969         mimev = gdk_pixbuf_format_get_mime_types(format);
970         if (!mimev) return NULL;
971
972         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
973         mime = g_strdup(mimev[0]);
974         g_strfreev(mimev);
975
976         return mime;
977 }
978
979 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
980 {
981         if (!il) return;
982
983         g_mutex_lock(il->data_mutex);
984         il->requested_width = width;
985         il->requested_height = height;
986         g_mutex_unlock(il->data_mutex);
987 }
988
989 void image_loader_set_buffer_size(ImageLoader *il, guint count)
990 {
991         if (!il) return;
992
993         g_mutex_lock(il->data_mutex);
994         il->idle_read_loop_count = count ? count : 1;
995         g_mutex_unlock(il->data_mutex);
996 }
997
998 void image_loader_set_priority(ImageLoader *il, gint priority)
999 {
1000         if (!il) return;
1001
1002         if (il->thread) return; /* can't change prio if the thread already runs */
1003         il->idle_priority = priority;
1004 }
1005
1006
1007 gdouble image_loader_get_percent(ImageLoader *il)
1008 {
1009         gdouble ret;
1010         if (!il) return 0.0;
1011         
1012         g_mutex_lock(il->data_mutex);
1013         if (il->bytes_total == 0) 
1014                 {
1015                 ret = 0.0;
1016                 }
1017         else
1018                 {
1019                 ret = (gdouble)il->bytes_read / il->bytes_total;
1020                 }
1021         g_mutex_unlock(il->data_mutex);
1022         return ret;
1023 }
1024
1025 gboolean image_loader_get_is_done(ImageLoader *il)
1026 {
1027         gboolean ret;
1028         if (!il) return FALSE;
1029
1030         g_mutex_lock(il->data_mutex);
1031         ret = il->done;
1032         g_mutex_unlock(il->data_mutex);
1033
1034         return ret;
1035 }
1036
1037 FileData *image_loader_get_fd(ImageLoader *il)
1038 {
1039         FileData *ret;
1040         if (!il) return NULL;
1041
1042         g_mutex_lock(il->data_mutex);
1043         ret = il->fd;
1044         g_mutex_unlock(il->data_mutex);
1045
1046         return ret;
1047 }
1048
1049 gboolean image_loader_get_shrunk(ImageLoader *il)
1050 {
1051         gboolean ret;
1052         if (!il) return FALSE;
1053
1054         g_mutex_lock(il->data_mutex);
1055         ret = il->shrunk;
1056         g_mutex_unlock(il->data_mutex);
1057         return ret;
1058 }
1059
1060 const gchar *image_loader_get_error(ImageLoader *il)
1061 {
1062         const gchar *ret = NULL;
1063         if (!il) return NULL;
1064         g_mutex_lock(il->data_mutex);
1065         if (il->error) ret = il->error->message;
1066         g_mutex_unlock(il->data_mutex);
1067         return ret;
1068 }
1069
1070
1071 /* FIXME - this can be rather slow and blocks until the size is known */
1072 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1073 {
1074         ImageLoader *il;
1075         gboolean success;
1076
1077         il = image_loader_new(fd);
1078
1079         success = image_loader_start_idle(il);
1080
1081         if (success && il->pixbuf)
1082                 {
1083                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1084                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1085                 }
1086         else
1087                 {
1088                 if (width) *width = -1;
1089                 if (height) *height = -1;
1090                 }
1091
1092         image_loader_free(il);
1093
1094         return success;
1095 }
1096 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */