]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/queryloaders.c
[quartz] Delete the typedef of GdkDevicePrivate
[~andy/gtk] / gdk-pixbuf / queryloaders.c
1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library
3  * queryloaders.c:
4  *
5  * Copyright (C) 2002 The Free Software Foundation
6  *
7  * Author: Matthias Clasen <maclas@gmx.de>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28 #include <glib/gprintf.h>
29 #include <gmodule.h>
30
31 #include <errno.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "gdk-pixbuf/gdk-pixbuf.h"
38 #include "gdk-pixbuf/gdk-pixbuf-private.h"
39 #include "gdk-pixbuf/gdk-pixbuf-io.h"
40
41 #ifdef USE_LA_MODULES
42 #define SOEXT ".la"
43 #else
44 #define SOEXT ("." G_MODULE_SUFFIX)
45 #endif
46 #define SOEXT_LEN (strlen (SOEXT))
47
48 #ifdef G_OS_WIN32
49 #include <windows.h>
50 #endif
51
52 static void
53 print_escaped (GString *contents, const char *str)
54 {
55         gchar *tmp = g_strescape (str, "");
56         g_string_append_printf (contents, "\"%s\" ", tmp);
57         g_free (tmp);
58 }
59
60 static int
61 loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *vtable)
62 {
63         const GdkPixbufModulePattern *pattern;
64         const char *error = "";
65
66         for (pattern = info->signature; pattern->prefix; pattern++)
67         {
68                 int prefix_len = strlen (pattern->prefix);
69                 if (prefix_len == 0)
70                 {
71                         error = "empty pattern";
72
73                         goto error;
74                 }
75                 if (pattern->mask)
76                 {
77                         int mask_len = strlen (pattern->mask);
78                         if (mask_len != prefix_len)
79                         {
80                                 error = "mask length mismatch";
81
82                                 goto error;
83                         }
84                         if (strspn (pattern->mask, " !xzn*") < mask_len)
85                         {
86                                 error = "bad char in mask";
87
88                                 goto error;
89                         }
90                 }
91         }
92
93         if (!vtable->load && !vtable->begin_load && !vtable->load_animation)
94         {
95                 error = "no load method implemented";
96
97                 goto error;
98         }
99
100         if (vtable->begin_load && (!vtable->stop_load || !vtable->load_increment))
101         {
102                 error = "incremental loading support incomplete";
103
104                 goto error;
105         }
106
107         if ((info->flags & GDK_PIXBUF_FORMAT_WRITABLE) && !(vtable->save || vtable->save_to_callback))
108         {
109                 error = "loader claims to support saving but doesn't implement save";
110                 goto error;
111         }
112
113         return 1;
114
115  error:
116         g_fprintf (stderr, "Loader sanity check failed for %s: %s\n",
117                    path, error);
118
119         return 0;
120 }
121
122 static void
123 write_loader_info (GString *contents, const char *path, GdkPixbufFormat *info)
124 {
125         const GdkPixbufModulePattern *pattern;
126         char **mime;
127         char **ext;
128
129         g_string_append_printf (contents, "\"%s\"\n", path);
130         g_string_append_printf (contents, "\"%s\" %u \"%s\" \"%s\" \"%s\"\n",
131                   info->name,
132                   info->flags,
133                   info->domain ? info->domain : GETTEXT_PACKAGE,
134                   info->description,
135                   info->license ? info->license : "");
136         for (mime = info->mime_types; *mime; mime++) {
137                 g_string_append_printf (contents, "\"%s\" ", *mime);
138         }
139         g_string_append (contents, "\"\"\n");
140         for (ext = info->extensions; *ext; ext++) {
141                 g_string_append_printf (contents, "\"%s\" ", *ext);
142         }
143         g_string_append (contents, "\"\"\n");
144         for (pattern = info->signature; pattern->prefix; pattern++) {
145                 print_escaped (contents, pattern->prefix);
146                 print_escaped (contents, pattern->mask ? (const char *)pattern->mask : "");
147                 g_string_append_printf (contents, "%d\n", pattern->relevance);
148         }
149         g_string_append_c (contents, '\n');
150 }
151
152 static void
153 query_module (GString *contents, const char *dir, const char *file)
154 {
155         char *path;
156         GModule *module;
157         void                    (*fill_info)     (GdkPixbufFormat *info);
158         void                    (*fill_vtable)   (GdkPixbufModule *module);
159         gpointer fill_info_ptr;
160         gpointer fill_vtable_ptr;
161
162         if (g_path_is_absolute (file))
163                 path = g_strdup (file);
164         else
165                 path = g_build_filename (dir, file, NULL);
166
167         module = g_module_open (path, 0);
168         if (module &&
169             g_module_symbol (module, "fill_info", &fill_info_ptr) &&
170             g_module_symbol (module, "fill_vtable", &fill_vtable_ptr)) {
171                 GdkPixbufFormat *info;
172                 GdkPixbufModule *vtable;
173
174 #ifdef G_OS_WIN32
175                 /* Replace backslashes in path with forward slashes, so that
176                  * it reads in without problems.
177                  */
178                 {
179                         char *p = path;
180                         while (*p) {
181                                 if (*p == '\\')
182                                         *p = '/';
183                                 p++;
184                         }
185                 }
186 #endif
187                 info = g_new0 (GdkPixbufFormat, 1);
188                 vtable = g_new0 (GdkPixbufModule, 1);
189
190                 vtable->module = module;
191
192                 fill_info = fill_info_ptr;
193                 fill_vtable = fill_vtable_ptr;
194
195                 (*fill_info) (info);
196                 (*fill_vtable) (vtable);
197
198                 if (loader_sanity_check (path, info, vtable))
199                         write_loader_info (contents, path, info);
200
201                 g_free (info);
202                 g_free (vtable);
203         }
204         else {
205                 if (module == NULL)
206                         g_fprintf (stderr, "g_module_open() failed for %s: %s\n", path,
207                                    g_module_error());
208                 else
209                         g_fprintf (stderr, "Cannot load loader %s\n", path);
210         }
211         if (module)
212                 g_module_close (module);
213         g_free (path);
214 }
215
216 static gchar *
217 gdk_pixbuf_get_module_file (void)
218 {
219         gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
220
221         if (!result)
222                 result = g_build_filename (GTK_LIBDIR, "gtk-3.0", GTK_BINARY_VERSION, "loaders.cache", NULL);
223
224         return result;
225 }
226
227 int main (int argc, char **argv)
228 {
229         gint i;
230         gchar *prgname;
231         GString *contents;
232         gchar *cache_file = NULL;
233         gint first_file = 1;
234
235 #ifdef G_OS_WIN32
236         gchar *libdir;
237         gchar *runtime_prefix;
238         gchar *slash;
239
240         if (g_ascii_strncasecmp (PIXBUF_LIBDIR, GTK_PREFIX, strlen (GTK_PREFIX)) == 0 &&
241             G_IS_DIR_SEPARATOR (PIXBUF_LIBDIR[strlen (GTK_PREFIX)])) {
242                 /* GTK_PREFIX is a prefix of PIXBUF_LIBDIR, as it
243                  * normally is. Replace that prefix in PIXBUF_LIBDIR
244                  * with the installation directory on this machine.
245                  * We assume this invokation of
246                  * gdk-pixbuf-query-loaders is run from either a "bin"
247                  * subdirectory of the installation directory, or in
248                  * the installation directory itself.
249                  */
250                 wchar_t fn[1000];
251                 GetModuleFileNameW (NULL, fn, G_N_ELEMENTS (fn));
252                 runtime_prefix = g_utf16_to_utf8 (fn, -1, NULL, NULL, NULL);
253                 slash = strrchr (runtime_prefix, '\\');
254                 *slash = '\0';
255                 slash = strrchr (runtime_prefix, '\\');
256                 /* If running from some weird location, or from the
257                  * build directory (either in the .libs folder where
258                  * libtool places the real executable when using a
259                  * wrapper, or directly from the gdk-pixbuf folder),
260                  * use the compile-time libdir.
261                  */
262                 if (slash == NULL ||
263                     g_ascii_strcasecmp (slash + 1, ".libs") == 0 ||
264                     g_ascii_strcasecmp (slash + 1, "gdk-pixbuf") == 0) {
265                         libdir = PIXBUF_LIBDIR;
266                 }
267                 else {
268                         if (slash != NULL && g_ascii_strcasecmp (slash + 1, "bin") == 0) {
269                                 *slash = '\0';
270                         }
271
272                         libdir = g_strconcat (runtime_prefix,
273                                               "/",
274                                               PIXBUF_LIBDIR + strlen (GTK_PREFIX) + 1,
275                                               NULL);
276                 }
277         }
278         else {
279                 libdir = PIXBUF_LIBDIR;
280         }
281
282 #undef PIXBUF_LIBDIR
283 #define PIXBUF_LIBDIR libdir
284
285 #endif
286
287         if (argc > 1 && strcmp (argv[1], "--update-cache") == 0) {
288                 cache_file = gdk_pixbuf_get_module_file ();
289                 first_file = 2;
290         }
291
292         contents = g_string_new ("");
293
294         prgname = g_get_prgname ();
295         g_string_append_printf (contents,
296                                 "# GdkPixbuf Image Loader Modules file\n"
297                                 "# Automatically generated file, do not edit\n"
298                                 "# Created by %s from gtk+-%s\n"
299                                 "#\n",
300                                 (prgname ? prgname : "gdk-pixbuf-query-loaders-3.0"),
301                                 GDK_PIXBUF_VERSION);
302
303         if (argc == first_file) {
304 #ifdef USE_GMODULE
305                 const char *path;
306                 GDir *dir;
307
308                 path = g_getenv ("GDK_PIXBUF_MODULEDIR");
309 #ifdef G_OS_WIN32
310                 if (path != NULL && *path != '\0')
311                         path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
312 #endif
313                 if (path == NULL || *path == '\0')
314                         path = PIXBUF_LIBDIR;
315
316                 g_string_append_printf (contents, "# LoaderDir = %s\n#\n", path);
317
318                 dir = g_dir_open (path, 0, NULL);
319                 if (dir) {
320                         const char *dent;
321
322                         while ((dent = g_dir_read_name (dir))) {
323                                 gint len = strlen (dent);
324                                 if (len > SOEXT_LEN &&
325                                     strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
326                                         query_module (contents, path, dent);
327                                 }
328                         }
329                         g_dir_close (dir);
330                 }
331 #else
332                 g_string_append_printf (contents, "# dynamic loading of modules not supported\n");
333 #endif
334         }
335         else {
336                 char *cwd = g_get_current_dir ();
337
338                 for (i = 1; i < argc; i++) {
339                         char *infilename = argv[i];
340 #ifdef G_OS_WIN32
341                         infilename = g_locale_to_utf8 (infilename,
342                                                        -1, NULL, NULL, NULL);
343 #endif
344                         query_module (contents, cwd, infilename);
345                 }
346                 g_free (cwd);
347         }
348
349         if (cache_file) {
350                 GError *err;
351
352                 err = NULL;
353                 if (!g_file_set_contents (cache_file, contents->str, -1, &err)) {
354                         g_fprintf (stderr, "%s\n", err->message);
355                 }
356         }
357         else
358                 g_print ("%s\n", contents->str);
359
360         return 0;
361 }