Fix the windowsize
[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 (il->pixbuf) g_object_unref(il->pixbuf);
366
367         il->pixbuf = pb;
368         if (il->pixbuf) g_object_ref(il->pixbuf);
369
370         g_mutex_unlock(il->data_mutex);
371 }
372
373 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
374                                  guint x, guint y, guint w, guint h,
375                                  gpointer data)
376 {
377         ImageLoader *il = data;
378
379         if (!image_loader_get_pixbuf(il))
380                 {
381                 image_loader_sync_pixbuf(il);
382                 if (!image_loader_get_pixbuf(il))
383                         {
384                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
385                         }
386                 }
387
388         g_mutex_lock(il->data_mutex);
389         if (il->delay_area_ready)
390                 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
391         else
392                 image_loader_emit_area_ready(il, x, y, w, h);
393         g_mutex_unlock(il->data_mutex);
394 }
395
396 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
397 {
398         GdkPixbuf *pb;
399         guchar *pix;
400         size_t h, rs;
401         
402         /* a workaround for 
403            http://bugzilla.gnome.org/show_bug.cgi?id=547669 
404            http://bugzilla.gnome.org/show_bug.cgi?id=589334
405         */
406         gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
407         if (strcmp(format, "svg") == 0 ||
408             strcmp(format, "xpm") == 0)
409                 {
410                 g_free(format);
411                 return;
412                 }
413         
414         g_free(format);
415
416         pb = gdk_pixbuf_loader_get_pixbuf(loader);
417         
418         h = gdk_pixbuf_get_height(pb);
419         rs = gdk_pixbuf_get_rowstride(pb);
420         pix = gdk_pixbuf_get_pixels(pb);
421         
422         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
423
424 }
425
426 static void image_loader_size_cb(GdkPixbufLoader *loader,
427                                  gint width, gint height, gpointer data)
428 {
429         ImageLoader *il = data;
430         GdkPixbufFormat *format;
431         gchar **mime_types;
432         gboolean scale = FALSE;
433         gint n;
434
435         g_mutex_lock(il->data_mutex);
436         if (il->requested_width < 1 || il->requested_height < 1) 
437                 {
438                 g_mutex_unlock(il->data_mutex);
439                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
440                 return;
441                 }
442         g_mutex_unlock(il->data_mutex);
443
444         format = gdk_pixbuf_loader_get_format(loader);
445         if (!format) return;
446
447         mime_types = gdk_pixbuf_format_get_mime_types(format);
448         n = 0;
449         while (mime_types[n])
450                 {
451                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
452                 n++;
453                 }
454         g_strfreev(mime_types);
455
456         if (!scale)
457                 {
458                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
459                 return;
460                 }
461
462         g_mutex_lock(il->data_mutex);
463
464         gint nw, nh;
465         if (width > il->requested_width || height > il->requested_height)
466                 {
467
468                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
469                         {
470                         nw = il->requested_width;
471                         nh = (gdouble)nw / width * height;
472                         if (nh < 1) nh = 1;
473                         }
474                 else
475                         {
476                         nh = il->requested_height;
477                         nw = (gdouble)nh / height * width;
478                         if (nw < 1) nw = 1;
479                         }
480
481                 gdk_pixbuf_loader_set_size(loader, nw, nh);
482                 il->shrunk = TRUE;
483                 }
484         g_mutex_unlock(il->data_mutex);
485
486         g_signal_emit(il, signals[SIGNAL_SIZE], 0, nw, nh);
487 }
488
489 static void image_loader_stop_loader(ImageLoader *il)
490 {
491         if (!il) return;
492
493         if (il->loader)
494                 {
495                 /* some loaders do not have a pixbuf till close, order is important here */
496                 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
497                 image_loader_sync_pixbuf(il);
498                 g_object_unref(G_OBJECT(il->loader));
499                 il->loader = NULL;
500                 }
501         g_mutex_lock(il->data_mutex);
502         il->done = TRUE;
503         g_mutex_unlock(il->data_mutex);
504 }
505
506 static void image_loader_setup_loader(ImageLoader *il)
507 {
508         g_mutex_lock(il->data_mutex);
509         il->loader = gdk_pixbuf_loader_new();
510
511         g_signal_connect(G_OBJECT(il->loader), "area_updated",
512                          G_CALLBACK(image_loader_area_updated_cb), il);
513         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
514                          G_CALLBACK(image_loader_size_cb), il);
515         g_signal_connect(G_OBJECT(il->loader), "area_prepared",
516                          G_CALLBACK(image_loader_area_prepared_cb), il);
517         g_mutex_unlock(il->data_mutex);
518 }
519
520
521 static void image_loader_done(ImageLoader *il)
522 {
523         image_loader_stop_loader(il);
524
525         image_loader_emit_done(il);
526 }
527
528 static void image_loader_error(ImageLoader *il)
529 {
530         image_loader_stop_loader(il);
531
532         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
533
534         image_loader_emit_error(il);
535 }
536
537 static gboolean image_loader_continue(ImageLoader *il)
538 {
539         gint b;
540         gint c;
541
542         if (!il) return FALSE;
543
544         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
545         while (c > 0 && !image_loader_get_stopping(il))
546                 {
547                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
548
549                 if (b == 0)
550                         {
551                         image_loader_done(il);
552                         return FALSE;
553                         }
554
555                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
556                         {
557                         image_loader_error(il);
558                         return FALSE;
559                         }
560
561                 il->bytes_read += b;
562
563                 c--;
564                 }
565
566         if (il->bytes_total > 0)
567                 {
568                 image_loader_emit_percent(il);
569                 }
570
571         return TRUE;
572 }
573
574 static gboolean image_loader_begin(ImageLoader *il)
575 {
576         gssize b;
577         
578         if (il->pixbuf) return FALSE;
579
580         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
581         if (b < 1) return FALSE;
582
583         image_loader_setup_loader(il);
584
585         if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
586                 {
587                 image_loader_stop_loader(il);
588                 return FALSE;
589                 }
590
591         il->bytes_read += b;
592
593         /* read until size is known */
594         while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
595                 {
596                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
597                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
598                         {
599                         image_loader_stop_loader(il);
600                         return FALSE;
601                         }
602                 il->bytes_read += b;
603                 }
604         if (!il->pixbuf) image_loader_sync_pixbuf(il);
605
606         if (il->bytes_read == il->bytes_total || b < 1)
607                 {
608                 /* done, handle (broken) loaders that do not have pixbuf till close */
609                 image_loader_stop_loader(il);
610
611                 if (!il->pixbuf) return FALSE;
612
613                 image_loader_done(il);
614                 return TRUE;
615                 }
616
617         if (!il->pixbuf)
618                 {
619                 image_loader_stop_loader(il);
620                 return FALSE;
621                 }
622
623         return TRUE;
624 }
625
626 /**************************************************************************************/
627 /* the following functions are always executed in the main thread */
628
629
630 static gboolean image_loader_setup_source(ImageLoader *il)
631 {
632         struct stat st;
633         gchar *pathl;
634
635         if (!il || il->loader || il->mapped_file) return FALSE;
636
637         il->mapped_file = NULL;
638
639         if (il->fd)
640                 {
641                 ExifData *exif = exif_read_fd(il->fd);
642
643                 if (options->thumbnails.use_exif)
644                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
645                 else
646                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
647
648                 if (il->mapped_file)
649                         {
650                         il->preview = TRUE;
651                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
652                         }
653                 exif_free_fd(il->fd, exif);
654                 }
655
656         
657         if (!il->mapped_file)
658                 {
659                 /* normal file */
660                 gint load_fd;
661         
662                 pathl = path_from_utf8(il->fd->path);
663                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
664                 g_free(pathl);
665                 if (load_fd == -1) return FALSE;
666
667                 if (fstat(load_fd, &st) == 0)
668                         {
669                         il->bytes_total = st.st_size;
670                         }
671                 else
672                         {
673                         close(load_fd);
674                         return FALSE;
675                         }
676                 
677                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
678                 close(load_fd);
679                 if (il->mapped_file == MAP_FAILED)
680                         {
681                         il->mapped_file = 0;
682                         return FALSE;
683                         }
684                 il->preview = FALSE;
685                 }
686                 
687         return TRUE;
688 }
689
690 static void image_loader_stop_source(ImageLoader *il)
691 {
692         if (!il) return;
693         
694         if (il->mapped_file)
695                 {
696                 if (il->preview)
697                         {
698                         exif_free_preview(il->mapped_file);
699                         }
700                 else
701                         {
702                         munmap(il->mapped_file, il->bytes_total);
703                         }
704                 il->mapped_file = NULL;
705                 }
706 }
707
708 static void image_loader_stop(ImageLoader *il)
709 {
710         if (!il) return;
711
712         if (il->idle_id)
713                 {
714                 g_source_remove(il->idle_id);
715                 il->idle_id = 0;
716                 }
717                 
718         if (il->thread)
719                 {
720                 /* stop loader in the other thread */
721                 g_mutex_lock(il->data_mutex);
722                 il->stopping = TRUE;
723                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
724                 g_mutex_unlock(il->data_mutex);
725                 }
726
727         image_loader_stop_loader(il);
728         image_loader_stop_source(il);
729         
730 }
731
732 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
733 {
734         g_mutex_lock(il->data_mutex);
735         il->delay_area_ready = enable;
736         if (!enable)
737                 {
738                 /* send delayed */
739                 GList *list, *work;
740                 list = g_list_reverse(il->area_param_delayed_list);
741                 il->area_param_delayed_list = NULL;
742                 g_mutex_unlock(il->data_mutex);
743
744                 work = list;
745
746                 while (work)
747                         {
748                         ImageLoaderAreaParam *par = work->data;
749                         work = work->next;
750                         
751                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
752                         g_free(par);
753                         }
754                 g_list_free(list);
755                 }
756         else
757                 {
758                 /* just unlock */
759                 g_mutex_unlock(il->data_mutex);
760                 }
761 }
762
763
764 /**************************************************************************************/
765 /* execution via idle calls */
766
767 static gboolean image_loader_idle_cb(gpointer data)
768 {
769         gboolean ret = FALSE;
770         ImageLoader *il = data;
771
772         if (il->idle_id)
773                 {
774                 ret = image_loader_continue(il);
775                 }
776         
777         if (!ret)
778                 {
779                 image_loader_stop_source(il);
780                 }
781         
782         return ret;
783 }
784
785
786 static gboolean image_loader_start_idle(ImageLoader *il)
787 {
788         gboolean ret;
789         
790         if (!il) return FALSE;
791
792         if (!il->fd) return FALSE;
793
794         if (!image_loader_setup_source(il)) return FALSE;
795         
796         ret = image_loader_begin(il);
797
798         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
799         return ret;
800 }
801
802 /**************************************************************************************/
803 /* execution via thread */
804
805 #ifdef HAVE_GTHREAD
806 static GThreadPool *image_loader_thread_pool = NULL;
807
808 static GCond *image_loader_prio_cond = NULL;
809 static GMutex *image_loader_prio_mutex = NULL;
810 static gint image_loader_prio_num = 0;
811
812
813 static void image_loader_thread_enter_high(void)
814 {
815         g_mutex_lock(image_loader_prio_mutex);
816         image_loader_prio_num++;
817         g_mutex_unlock(image_loader_prio_mutex);
818 }
819
820 static void image_loader_thread_leave_high(void)
821 {
822         g_mutex_lock(image_loader_prio_mutex);
823         image_loader_prio_num--;
824         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
825         g_mutex_unlock(image_loader_prio_mutex);
826 }
827
828 static void image_loader_thread_wait_high(void)
829 {
830         g_mutex_lock(image_loader_prio_mutex);
831         while (image_loader_prio_num) 
832                 {
833                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
834                 }
835
836         g_mutex_unlock(image_loader_prio_mutex);
837 }
838
839
840 static void image_loader_thread_run(gpointer data, gpointer user_data)
841 {
842         ImageLoader *il = data;
843         gboolean cont;
844         gboolean err;
845         
846         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
847                 {
848                 /* low prio, wait untill high prio tasks finishes */
849                 image_loader_thread_wait_high();
850                 }
851         else
852                 {
853                 /* high prio */
854                 image_loader_thread_enter_high();
855                 }
856         
857         err = !image_loader_begin(il);
858         
859         if (err)
860                 {
861                 /* 
862                 loader failed, we have to send signal 
863                 (idle mode returns the image_loader_begin return value directly)
864                 (success is always reported indirectly from image_loader_begin)
865                 */
866                 image_loader_emit_error(il);
867                 }
868         
869         cont = !err;
870         
871         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
872                 {
873                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
874                         {
875                         /* low prio, wait untill high prio tasks finishes */
876                         image_loader_thread_wait_high();
877                         }
878                 cont = image_loader_continue(il);
879                 }
880         image_loader_stop_loader(il);
881
882         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
883                 {
884                 /* high prio */
885                 image_loader_thread_leave_high();
886                 }
887
888         g_mutex_lock(il->data_mutex);
889         il->can_destroy = TRUE;
890         g_cond_signal(il->can_destroy_cond);
891         g_mutex_unlock(il->data_mutex);
892
893 }
894
895
896 static gboolean image_loader_start_thread(ImageLoader *il)
897 {
898         if (!il) return FALSE;
899
900         if (!il->fd) return FALSE;
901
902         il->thread = TRUE;
903         
904         if (!image_loader_setup_source(il)) return FALSE;
905
906         if (!image_loader_thread_pool) 
907                 {
908                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
909                 image_loader_prio_cond = g_cond_new();
910                 image_loader_prio_mutex = g_mutex_new();
911                 }
912
913         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
914
915         g_thread_pool_push(image_loader_thread_pool, il, NULL);
916         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
917                 
918         return TRUE;
919 }
920 #endif /* HAVE_GTHREAD */
921
922
923 /**************************************************************************************/
924 /* public interface */
925
926
927 gboolean image_loader_start(ImageLoader *il)
928 {
929         if (!il) return FALSE;
930
931         if (!il->fd) return FALSE;
932
933 #ifdef HAVE_GTHREAD
934         return image_loader_start_thread(il);
935 #else
936         return image_loader_start_idle(il);
937 #endif
938 }
939
940
941 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
942 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
943 {
944         GdkPixbuf *ret;
945         if (!il) return NULL;
946         
947         g_mutex_lock(il->data_mutex);
948         ret = il->pixbuf;
949         g_mutex_unlock(il->data_mutex);
950         return ret;
951 }
952
953 gchar *image_loader_get_format(ImageLoader *il)
954 {
955         GdkPixbufFormat *format;
956         gchar **mimev;
957         gchar *mime;
958
959         if (!il || !il->loader) return NULL;
960
961         format = gdk_pixbuf_loader_get_format(il->loader);
962         if (!format) return NULL;
963
964         mimev = gdk_pixbuf_format_get_mime_types(format);
965         if (!mimev) return NULL;
966
967         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
968         mime = g_strdup(mimev[0]);
969         g_strfreev(mimev);
970
971         return mime;
972 }
973
974 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
975 {
976         if (!il) return;
977
978         g_mutex_lock(il->data_mutex);
979         il->requested_width = width;
980         il->requested_height = height;
981         g_mutex_unlock(il->data_mutex);
982 }
983
984 void image_loader_set_buffer_size(ImageLoader *il, guint count)
985 {
986         if (!il) return;
987
988         g_mutex_lock(il->data_mutex);
989         il->idle_read_loop_count = count ? count : 1;
990         g_mutex_unlock(il->data_mutex);
991 }
992
993 void image_loader_set_priority(ImageLoader *il, gint priority)
994 {
995         if (!il) return;
996
997         if (il->thread) return; /* can't change prio if the thread already runs */
998         il->idle_priority = priority;
999 }
1000
1001
1002 gdouble image_loader_get_percent(ImageLoader *il)
1003 {
1004         gdouble ret;
1005         if (!il) return 0.0;
1006         
1007         g_mutex_lock(il->data_mutex);
1008         if (il->bytes_total == 0) 
1009                 {
1010                 ret = 0.0;
1011                 }
1012         else
1013                 {
1014                 ret = (gdouble)il->bytes_read / il->bytes_total;
1015                 }
1016         g_mutex_unlock(il->data_mutex);
1017         return ret;
1018 }
1019
1020 gboolean image_loader_get_is_done(ImageLoader *il)
1021 {
1022         gboolean ret;
1023         if (!il) return FALSE;
1024
1025         g_mutex_lock(il->data_mutex);
1026         ret = il->done;
1027         g_mutex_unlock(il->data_mutex);
1028
1029         return ret;
1030 }
1031
1032 FileData *image_loader_get_fd(ImageLoader *il)
1033 {
1034         FileData *ret;
1035         if (!il) return NULL;
1036
1037         g_mutex_lock(il->data_mutex);
1038         ret = il->fd;
1039         g_mutex_unlock(il->data_mutex);
1040
1041         return ret;
1042 }
1043
1044 gboolean image_loader_get_shrunk(ImageLoader *il)
1045 {
1046         gboolean ret;
1047         if (!il) return FALSE;
1048
1049         g_mutex_lock(il->data_mutex);
1050         ret = il->shrunk;
1051         g_mutex_unlock(il->data_mutex);
1052         return ret;
1053 }
1054
1055 const gchar *image_loader_get_error(ImageLoader *il)
1056 {
1057         const gchar *ret = NULL;
1058         if (!il) return NULL;
1059         g_mutex_lock(il->data_mutex);
1060         if (il->error) ret = il->error->message;
1061         g_mutex_unlock(il->data_mutex);
1062         return ret;
1063 }
1064
1065
1066 /* FIXME - this can be rather slow and blocks until the size is known */
1067 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1068 {
1069         ImageLoader *il;
1070         gboolean success;
1071
1072         il = image_loader_new(fd);
1073
1074         success = image_loader_start_idle(il);
1075
1076         if (success && il->pixbuf)
1077                 {
1078                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1079                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1080                 }
1081         else
1082                 {
1083                 if (width) *width = -1;
1084                 if (height) *height = -1;
1085                 }
1086
1087         image_loader_free(il);
1088
1089         return success;
1090 }
1091 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */