summaryrefslogtreecommitdiff
path: root/src/lib/loaders.c
blob: a2d83dee063194cba04f8b89c51315e140228c7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "common.h"

#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "debug.h"
#include "file.h"
#include "image.h"
#include "loaders.h"

#define DBG_PFX "LOAD"
#define DP(fmt...) DC(DBG_LOAD, fmt)

static ImlibLoader *loaders = NULL;
static char         loaders_loaded = 0;

typedef struct {
   const char         *dso;
   const char         *const *ext;
} KnownLoader;

static const char  *const ext_argb[] = { "argb", NULL };
static const char  *const ext_bmp[] = { "bmp", NULL };
#ifdef BUILD_BZ2_LOADER
static const char  *const ext_bz2[] = { "bz2", NULL };
#endif
static const char  *const ext_ff[] = { "ff", NULL };
#ifdef BUILD_GIF_LOADER
static const char  *const ext_gif[] = { "gif", NULL };
#endif
static const char  *const ext_ico[] = { "ico", NULL };
#ifdef BUILD_ID3_LOADER
static const char  *const ext_id3[] = { "mp3", NULL };
#endif
#ifdef BUILD_JPEG_LOADER
static const char  *const ext_jpeg[] = { "jpg", "jpeg", "jfif", "jfi", NULL };
#endif
static const char  *const ext_lbm[] = { "iff", "ilbm", "lbm", NULL };
#ifdef BUILD_PNG_LOADER
static const char  *const ext_png[] = { "png", NULL };
#endif
static const char  *const ext_pnm[] =
   { "pnm", "ppm", "pgm", "pbm", "pam", NULL };
static const char  *const ext_tga[] = { "tga", NULL };
#ifdef BUILD_TIFF_LOADER
static const char  *const ext_tiff[] = { "tiff", "tif", NULL };
#endif
#ifdef BUILD_WEBP_LOADER
static const char  *const ext_webp[] = { "webp", NULL };
#endif
static const char  *const ext_xbm[] = { "xbm", NULL };
static const char  *const ext_xpm[] = { "xpm", NULL };
#ifdef BUILD_ZLIB_LOADER
static const char  *const ext_zlib[] = { "gz", NULL };
#endif
static const KnownLoader loaders_known[] = {
   {"argb", ext_argb},
   {"bmp", ext_bmp},
#ifdef BUILD_BZ2_LOADER
   {"bz2", ext_bz2},
#endif
   {"ff", ext_ff},
#ifdef BUILD_GIF_LOADER
   {"gif", ext_gif},
#endif
   {"ico", ext_ico},
#ifdef BUILD_ID3_LOADER
   {"id3", ext_id3},
#endif
#ifdef BUILD_JPEG_LOADER
   {"jpeg", ext_jpeg},
#endif
   {"lbm", ext_lbm},
#ifdef BUILD_PNG_LOADER
   {"png", ext_png},
#endif
   {"pnm", ext_pnm},
   {"tga", ext_tga},
#ifdef BUILD_TIFF_LOADER
   {"tiff", ext_tiff},
#endif
#ifdef BUILD_WEBP_LOADER
   {"webp", ext_webp},
#endif
   {"xbm", ext_xbm},
   {"xpm", ext_xpm},
#ifdef BUILD_ZLIB_LOADER
   {"zlib", ext_zlib},
#endif
};

static int
__imlib_IsLoaderLoaded(const char *file)
{
   ImlibLoader        *l;

   for (l = loaders; l; l = l->next)
     {
        if (strcmp(file, l->file) == 0)
           return 1;
     }

   return 0;
}

/* try dlopen()ing the file if we succeed finish filling out the malloced */
/* loader struct and return it */
static ImlibLoader *
__imlib_ProduceLoader(const char *file)
{
   ImlibLoader        *l;
   void                (*l_formats)(ImlibLoader * l);

   DP("%s: %s\n", __func__, file);

   l = malloc(sizeof(ImlibLoader));
   l->num_formats = 0;
   l->formats = NULL;
   l->handle = dlopen(file, RTLD_NOW | RTLD_LOCAL);
   if (!l->handle)
     {
        free(l);
        return NULL;
     }
   l->load2 = dlsym(l->handle, "load2");
   l->load = NULL;
   if (!l->load2)
      l->load = dlsym(l->handle, "load");
   l->save = dlsym(l->handle, "save");
   l_formats = dlsym(l->handle, "formats");

   /* each loader must provide formats() and at least load() or save() */
   if (!l_formats || !(l->load2 || l->load || l->save))
     {
        dlclose(l->handle);
        free(l);
        return NULL;
     }
   l_formats(l);
   l->file = strdup(file);
   l->next = NULL;

   l->next = loaders;
   loaders = l;

   return l;
}

/* fre the struct for a loader and close its dlopen'd handle */
static void
__imlib_ConsumeLoader(ImlibLoader * l)
{
   free(l->file);
   if (l->handle)
      dlclose(l->handle);
   if (l->formats)
     {
        int                 i;

        for (i = 0; i < l->num_formats; i++)
           free(l->formats[i]);
        free(l->formats);
     }
   free(l);
}

/* remove all loaders int eh list we have cached so we can re-load them */
void
__imlib_RemoveAllLoaders(void)
{
   ImlibLoader        *l, *l_next;

   for (l = loaders; l; l = l_next)
     {
        l_next = l->next;
        __imlib_ConsumeLoader(l);
     }
   loaders = NULL;
   loaders_loaded = 0;
}

/* find all the loaders we can find and load them up to see what they can */
/* load / save */
static void
__imlib_LoadAllLoaders(void)
{
   int                 i, num;
   char              **list;

   DP("%s\n", __func__);

   /* list all the loaders imlib can find */
   list = __imlib_ListModules(__imlib_PathToLoaders(), &num);
   /* no loaders? well don't load anything */
   if (!list)
      return;

   /* go through the list of filenames for loader .so's and load them */
   /* (or try) and if it succeeds, append to our loader list */
   for (i = num - 1; i >= 0; i--)
     {
        if (!__imlib_IsLoaderLoaded(list[i]))
           __imlib_ProduceLoader(list[i]);
        free(list[i]);
     }
   free(list);

   loaders_loaded = 1;
}

ImlibLoader       **
__imlib_GetLoaderList(void)
{
   if (!loaders_loaded)
      __imlib_LoadAllLoaders();
   return &loaders;
}

static ImlibLoader *
__imlib_LookupKnownLoader(const char *format)
{
   const KnownLoader  *kl;
   ImlibLoader        *l;
   unsigned int        i;
   const char         *const *exts;
   char                nbuf[4096];

   kl = NULL;
   for (i = 0; i < ARRAY_SIZE(loaders_known); i++)
     {
        for (exts = loaders_known[i].ext; *exts; exts++)
          {
             if (strcasecmp(format, *exts) != 0)
                continue;
             kl = &loaders_known[i];
             goto done;
          }
     }

 done:
   l = NULL;
   if (kl)
     {
        snprintf(nbuf, sizeof(nbuf), "%s/%s.so", __imlib_PathToLoaders(),
                 kl->dso);
        l = __imlib_ProduceLoader(nbuf);
     }
   DP("%s: '%s' -> '%s': %p\n", __func__, format, kl ? kl->dso : "-", l);
   return l;
}

static ImlibLoader *
__imlib_LookupLoadedLoader(const char *format, int for_save)
{
   ImlibLoader        *l;

   DP("%s: fmt='%s'\n", __func__, format);

   /* go through the loaders - first loader that claims to handle that */
   /* image type (extension wise) wins as a first guess to use - NOTE */
   /* this is an OPTIMISATION - it is possible the file has no extension */
   /* or has an unrecognised one but still is loadable by a loader. */
   /* if thkis initial loader failes to load the load mechanism will */
   /* systematically go from most recently used to least recently used */
   /* loader until one succeeds - or none are left and all have failed */
   /* and only if all fail does the laod fail. the lao9der that does */
   /* succeed gets it way to the head of the list so it's going */
   /* to be used first next time in this search mechanims - this */
   /* assumes you tend to laod a few image types and ones generally */
   /* of the same format */
   for (l = loaders; l; l = l->next)
     {
        int                 i;

        /* go through all the formats that loader supports */
        for (i = 0; i < l->num_formats; i++)
          {
             /* does it match ? */
             if (strcasecmp(l->formats[i], format) == 0)
               {
                  /* does it provide the function we need? */
                  if ((for_save && l->save) ||
                      (!for_save && (l->load2 || l->load)))
                     goto done;
               }
          }
     }

 done:
   DP("%s: fmt='%s': %s\n", __func__, format, l ? l->file : "-");
   return l;
}

__EXPORT__ ImlibLoader *
__imlib_FindBestLoaderForFormat(const char *format, int for_save)
{
   ImlibLoader        *l;

   DP("%s: fmt='%s'\n", __func__, format);

   if (!format || format[0] == '\0')
      return NULL;

   if (loaders)
     {
        /* At least one loader loaded */
        l = __imlib_LookupLoadedLoader(format, for_save);
        if (l || loaders_loaded)
           goto done;
     }

   l = __imlib_LookupKnownLoader(format);
   if (l)
      goto done;

   __imlib_LoadAllLoaders();

   l = __imlib_LookupLoadedLoader(format, for_save);

 done:
   DP("%s: fmt='%s': %s\n", __func__, format, l ? l->file : "-");
   return l;
}

__EXPORT__ ImlibLoader *
__imlib_FindBestLoaderForFile(const char *file, int for_save)
{
   ImlibLoader        *l;

   DP("%s: file='%s'\n", __func__, file);

   l = __imlib_FindBestLoaderForFormat(__imlib_FileExtension(file), for_save);

   return l;
}

ImlibLoader        *
__imlib_FindBestLoaderForFileFormat(const char *file, const char *format,
                                    int for_save)
{
   DP("%s: file='%s' ext='%s'\n", __func__, file, format);

   /* if the format is provided ("png" "jpg" etc.) use that */
   /* otherwise us the file extension */
   if (format)
      return __imlib_FindBestLoaderForFormat(format, for_save);
   else
      return __imlib_FindBestLoaderForFile(file, for_save);
}

__EXPORT__ void
__imlib_LoaderSetFormats(ImlibLoader * l,
                         const char *const *fmt, unsigned int num)
{
   unsigned int        i;

   l->num_formats = num;
   l->formats = malloc(sizeof(char *) * num);

   for (i = 0; i < num; i++)
      l->formats[i] = strdup(fmt[i]);
}