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