Add powerbox hook
[gtk-with-powerbox.git] / gdk-pixbuf / queryloaders.c
blobacb19fa36d45cebcafe185a90b7406da0d58d902
1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library
3 * queryloaders.c:
5 * Copyright (C) 2002 The Free Software Foundation
6 *
7 * Author: Matthias Clasen <maclas@gmx.de>
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.
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.
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.
25 #include "config.h"
27 #include <glib.h>
28 #include <glib/gprintf.h>
29 #include <gmodule.h>
31 #include <errno.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #include "gdk-pixbuf/gdk-pixbuf.h"
38 #include "gdk-pixbuf/gdk-pixbuf-private.h"
39 #include "gdk-pixbuf/gdk-pixbuf-io.h"
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))
48 #ifdef G_OS_WIN32
49 #include <windows.h>
50 #endif
52 static void
53 print_escaped (const char *str)
55 gchar *tmp = g_strescape (str, "");
56 g_printf ("\"%s\" ", tmp);
57 g_free (tmp);
60 static int
61 loader_sanity_check (const char *path, GdkPixbufFormat *info, GdkPixbufModule *vtable)
63 const GdkPixbufModulePattern *pattern;
64 const char *error = "";
66 for (pattern = info->signature; pattern->prefix; pattern++)
68 int prefix_len = strlen (pattern->prefix);
69 if (prefix_len == 0)
71 error = "empty pattern";
73 goto error;
75 if (pattern->mask)
77 int mask_len = strlen (pattern->mask);
78 if (mask_len != prefix_len)
80 error = "mask length mismatch";
82 goto error;
84 if (strspn (pattern->mask, " !xzn*") < mask_len)
86 error = "bad char in mask";
88 goto error;
93 if (!vtable->load && !vtable->begin_load && !vtable->load_animation)
95 error = "no load method implemented";
97 goto error;
100 if (vtable->begin_load && (!vtable->stop_load || !vtable->load_increment))
102 error = "incremental loading support incomplete";
104 goto error;
107 if ((info->flags & GDK_PIXBUF_FORMAT_WRITABLE) && !(vtable->save || vtable->save_to_callback))
109 error = "loader claims to support saving but doesn't implement save";
110 goto error;
113 return 1;
115 error:
116 g_fprintf (stderr, "Loader sanity check failed for %s: %s\n",
117 path, error);
119 return 0;
122 static void
123 write_loader_info (const char *path, GdkPixbufFormat *info)
125 const GdkPixbufModulePattern *pattern;
126 char **mime;
127 char **ext;
129 g_printf("\"%s\"\n", path);
130 g_printf ("\"%s\" %u \"%s\" \"%s\" \"%s\"\n",
131 info->name, info->flags,
132 info->domain ? info->domain : GETTEXT_PACKAGE, info->description, info->license);
133 for (mime = info->mime_types; *mime; mime++) {
134 g_printf ("\"%s\" ", *mime);
136 g_printf ("\"\"\n");
137 for (ext = info->extensions; *ext; ext++) {
138 g_printf ("\"%s\" ", *ext);
140 g_printf ("\"\"\n");
141 for (pattern = info->signature; pattern->prefix; pattern++) {
142 print_escaped (pattern->prefix);
143 print_escaped (pattern->mask ? (const char *)pattern->mask : "");
144 g_printf ("%d\n", pattern->relevance);
146 g_printf ("\n");
149 static void
150 query_module (const char *dir, const char *file)
152 char *path;
153 GModule *module;
154 void (*fill_info) (GdkPixbufFormat *info);
155 void (*fill_vtable) (GdkPixbufModule *module);
157 if (g_path_is_absolute (file))
158 path = g_strdup (file);
159 else
160 path = g_build_filename (dir, file, NULL);
162 module = g_module_open (path, 0);
163 if (module &&
164 g_module_symbol (module, "fill_info", (gpointer *) &fill_info) &&
165 g_module_symbol (module, "fill_vtable", (gpointer *) &fill_vtable)) {
166 GdkPixbufFormat *info;
167 GdkPixbufModule *vtable;
169 #ifdef G_OS_WIN32
170 /* Replace backslashes in path with forward slashes, so that
171 * it reads in without problems.
174 char *p = path;
175 while (*p) {
176 if (*p == '\\')
177 *p = '/';
178 p++;
181 #endif
182 info = g_new0 (GdkPixbufFormat, 1);
183 vtable = g_new0 (GdkPixbufModule, 1);
185 vtable->module = module;
187 (*fill_info) (info);
188 (*fill_vtable) (vtable);
190 if (loader_sanity_check (path, info, vtable))
191 write_loader_info (path, info);
193 g_free (info);
194 g_free (vtable);
196 else {
197 if (module == NULL)
198 g_fprintf (stderr, "g_module_open() failed for %s: %s\n", path,
199 g_module_error());
200 else
201 g_fprintf (stderr, "Cannot load loader %s\n", path);
203 if (module)
204 g_module_close (module);
205 g_free (path);
208 int main (int argc, char **argv)
210 gint i;
211 gchar *prgname;
213 #ifdef G_OS_WIN32
214 gchar *libdir;
215 gchar *runtime_prefix;
216 gchar *slash;
218 if (g_ascii_strncasecmp (PIXBUF_LIBDIR, GTK_PREFIX, strlen (GTK_PREFIX)) == 0 &&
219 G_IS_DIR_SEPARATOR (PIXBUF_LIBDIR[strlen (GTK_PREFIX)])) {
220 /* GTK_PREFIX is a prefix of PIXBUF_LIBDIR, as it
221 * normally is. Replace that prefix in PIXBUF_LIBDIR
222 * with the installation directory on this machine.
223 * We assume this invokation of
224 * gdk-pixbuf-query-loaders is run from either a "bin"
225 * subdirectory of the installation directory, or in
226 * the installation directory itself.
228 if (G_WIN32_HAVE_WIDECHAR_API ()) {
229 wchar_t fn[1000];
230 GetModuleFileNameW (NULL, fn, G_N_ELEMENTS (fn));
231 runtime_prefix = g_utf16_to_utf8 (fn, -1, NULL, NULL, NULL);
233 else {
234 char fn[1000];
235 GetModuleFileNameA (NULL, fn, G_N_ELEMENTS (fn));
236 runtime_prefix = g_locale_to_utf8 (fn, -1, NULL, NULL, NULL);
238 slash = strrchr (runtime_prefix, '\\');
239 *slash = '\0';
240 slash = strrchr (runtime_prefix, '\\');
241 if (slash != NULL && g_ascii_strcasecmp (slash + 1, "bin") == 0) {
242 *slash = '\0';
245 libdir = g_strconcat (runtime_prefix,
246 "/",
247 PIXBUF_LIBDIR + strlen (GTK_PREFIX) + 1,
248 NULL);
250 else {
251 libdir = PIXBUF_LIBDIR;
254 #undef PIXBUF_LIBDIR
255 #define PIXBUF_LIBDIR libdir
257 #endif
258 prgname = g_get_prgname ();
259 g_printf ("# GdkPixbuf Image Loader Modules file\n"
260 "# Automatically generated file, do not edit\n"
261 "# Created by %s from gtk+-%s\n"
262 "#\n",
263 (prgname ? prgname : "gdk-pixbuf-query-loaders"),
264 GDK_PIXBUF_VERSION);
266 if (argc == 1) {
267 #ifdef USE_GMODULE
268 const char *path;
269 GDir *dir;
271 path = g_getenv ("GDK_PIXBUF_MODULEDIR");
272 #ifdef G_OS_WIN32
273 if (path != NULL && *path != '\0')
274 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
275 #endif
276 if (path == NULL || *path == '\0')
277 path = PIXBUF_LIBDIR;
279 g_printf ("# LoaderDir = %s\n#\n", path);
281 dir = g_dir_open (path, 0, NULL);
282 if (dir) {
283 const char *dent;
285 while ((dent = g_dir_read_name (dir))) {
286 gint len = strlen (dent);
287 if (len > SOEXT_LEN &&
288 strcmp (dent + len - SOEXT_LEN, SOEXT) == 0) {
289 query_module (path, dent);
292 g_dir_close (dir);
294 #else
295 g_printf ("# dynamic loading of modules not supported\n");
296 #endif
298 else {
299 char *cwd = g_get_current_dir ();
301 for (i = 1; i < argc; i++) {
302 char *infilename = argv[i];
303 #ifdef G_OS_WIN32
304 infilename = g_locale_to_utf8 (infilename,
305 -1, NULL, NULL, NULL);
306 #endif
307 query_module (cwd, infilename);
309 g_free (cwd);
312 return 0;