File: plugins.c

package info (click to toggle)
libg3d 0.0.8-20
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,656 kB
  • ctags: 2,803
  • sloc: ansic: 27,295; sh: 9,288; xml: 4,123; makefile: 864; cpp: 13
file content (537 lines) | stat: -rw-r--r-- 12,159 bytes parent folder | download | duplicates (6)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <sys/param.h>	/* PATH_MAX */

#include <string.h>
#include <unistd.h>

#include <g3d/config.h>
#include <g3d/stream.h>
#include <g3d/types.h>
#include <g3d/plugins.h>

static void plugins_free_plugin(G3DPlugin *plugin)
{
	if(!plugin)
		return;

	g_free(plugin->name);
	g_free(plugin->path);
	g_strfreev(plugin->extensions);
	if(plugin->module)
		g_module_close(plugin->module);

	g_free(plugin);
}

#define PLUGIN_GET_SYMBOL(symbol, pointer) \
	do { \
		if(g_module_symbol(plugin->module, symbol, \
			(gpointer *)&(pointer)) != TRUE) \
			pointer = NULL; \
	} while(0);

static gboolean plugins_loaddirectory(G3DContext *context,
	const gchar *dirname)
{
	GDir *plugindir;
	G3DPlugin *plugin;
	gchar **last, **dir, **dirnames;
	gchar **ext, **exts;
	gchar *path;
	const gchar *filename;
	guint32 type = G3D_PLUGIN_UNKNOWN;

	plugindir = g_dir_open(dirname, 0, NULL);
	if(!plugindir)
		return FALSE;

	dirnames = g_strsplit(dirname, G_DIR_SEPARATOR_S, 0);
	dir = last = dirnames;

	while(*dir != NULL)
	{
		if(*dir != NULL)
			last = dir;

		dir ++;
	}

	if(strcmp("import", *last) == 0)
		type = G3D_PLUGIN_IMPORT;
	else if(strcmp("image", *last) == 0)
		type = G3D_PLUGIN_IMAGE;

	g_strfreev(dirnames);

	while((filename = g_dir_read_name(plugindir)) != NULL) {
#ifdef G_OS_WIN32
		if(g_strcasecmp(filename + strlen(filename) - 4, ".dll") == 0) {
#else
		if(g_strcasecmp(filename + strlen(filename) - 3, ".so") == 0) {
#endif
			plugin = g_new0(G3DPlugin, 1);

			plugin->name = g_strdup(filename);
			plugin->path = g_strdup(dirname);
			plugin->type = type;

			path = g_strdup_printf("%s%c%s", dirname, G_DIR_SEPARATOR,
				filename);

			plugin->module = g_module_open(path, 0);
			if(plugin->module == NULL) {
				g_warning("libg3d: plugins: failed to load %s: %s\n",
					path, g_module_error());

				plugins_free_plugin(plugin);
			} else {
				PLUGIN_GET_SYMBOL("plugin_extensions", plugin->ext_func);
				PLUGIN_GET_SYMBOL("plugin_description", plugin->desc_func);
				PLUGIN_GET_SYMBOL("plugin_init", plugin->init_func);
				PLUGIN_GET_SYMBOL("plugin_cleanup", plugin->cleanup_func);
				PLUGIN_GET_SYMBOL("plugin_load_model", plugin->loadmodel_func);
				PLUGIN_GET_SYMBOL("plugin_load_model_from_stream",
					plugin->loadmodelstream_func);
				PLUGIN_GET_SYMBOL("plugin_load_image", plugin->loadimage_func);
				PLUGIN_GET_SYMBOL("plugin_load_image_from_stream",
					plugin->loadimagestream_func);

				/* append plugin to list */
				context->plugins = g_slist_append(context->plugins, plugin);

				/* handle managed extensions */
				if(plugin->ext_func) {
					ext = exts = plugin->ext_func(context);
					while(*ext != NULL) {
						if(plugin->type == G3D_PLUGIN_IMAGE)
							g_hash_table_insert(context->exts_image,
								*ext, plugin);
						else if(plugin->type == G3D_PLUGIN_IMPORT)
							g_hash_table_insert(context->exts_import,
								*ext, plugin);

						ext ++;
					}
					plugin->extensions = exts;
				}

				/* init plugin if possible */
				if(plugin->init_func)
				{
					plugin->user_data = plugin->init_func(context);
				}

			} /* plugin->module != NULL */

			g_free(path);
		} /* .la file */

	} /* while g_dir_read_name(...) */

	g_dir_close(plugindir);

	return TRUE;
}

#undef PLUGIN_GET_SYMBOL

#ifdef USE_LIBMAGIC

static gboolean plugins_magic_init(G3DContext *context)
{
	context->magic_cookie = magic_open(
		MAGIC_SYMLINK
#if 0
		| MAGIC_CHECK
#endif
#if DEBUG > 2
		| MAGIC_DEBUG
#endif
		);

#if DEBUG > 0
	g_debug("checking and loading %s", MAGIC_FILENAME);
#endif

	if(context->magic_cookie == NULL) {
		g_warning("magic_open() failed");
		return FALSE;
	}

	if(magic_load(context->magic_cookie, MAGIC_FILENAME) != 0) {
		g_warning("magic_load() failed: %s (%d)",
			magic_error(context->magic_cookie),
			magic_errno(context->magic_cookie));
		magic_close(context->magic_cookie);
		context->magic_cookie = NULL;
		return FALSE;
	}

	return TRUE;
}

static void plugins_magic_cleanup(G3DContext *context)
{
	if(context->magic_cookie != NULL)
		magic_close(context->magic_cookie);
	context->magic_cookie = NULL;
}

static G3DPlugin *plugins_magic_lookup(G3DContext *context,
	const gchar *filename)
{
	gchar *name;
	const gchar *type;
	GSList *item;
	G3DPlugin *tp, *plugin = NULL;

	if(context->magic_cookie == NULL)
		return NULL;

	type = magic_file(context->magic_cookie, filename);
	if((type == NULL) || (strlen(type) == 0))
		return NULL;

	name = g_strdup_printf("imp_%s.so", type);
	for(item = context->plugins; item != NULL; item = item->next) {
		tp = (G3DPlugin *)item->data;
		if(tp->type != G3D_PLUGIN_IMPORT)
			continue;

		if(strcmp(name, tp->name) == 0)
			plugin = tp;
	}
	g_free(name);

#if DEBUG > 0
	if(plugin != NULL)
		g_debug("libmagic detected plugin %s for %s",
			plugin->name, filename);
#endif

	return plugin;
}

#endif /* USE_LIBMAGIC */

gboolean g3d_plugins_init(G3DContext *context)
{
	context->exts_import = g_hash_table_new(g_str_hash, g_str_equal);
	context->exts_image = g_hash_table_new(g_str_hash, g_str_equal);

#ifdef G_OS_WIN32
	plugins_loaddirectory(context, "plugins\\image");
	plugins_loaddirectory(context, "plugins\\import");
#else
	plugins_loaddirectory(context, PLUGIN_DIR "/image");
	plugins_loaddirectory(context, PLUGIN_DIR "/import");
#endif

#ifdef USE_LIBMAGIC
	plugins_magic_init(context);
#endif

	return TRUE;
}

/**
 * g3d_plugins_cleanup:
 * @context: an initialized context
 *
 * Tries to free any memory allocated during g3d_plugins_init.
 */

void g3d_plugins_cleanup(G3DContext *context)
{
	GSList *plist;
	G3DPlugin *plugin;
	gchar **pext;

#ifdef USE_LIBMAGIC
	plugins_magic_cleanup(context);
#endif

	plist = context->plugins;
	while(plist)
	{
		plugin = (G3DPlugin *)plist->data;

#if DEBUG > 2
		g_debug("cleaning up plugin '%s'", plugin->name);
#endif

		/* cleanup plugin-specific data */
		if(plugin->cleanup_func)
			plugin->cleanup_func(plugin->user_data);

		/* remove extensions from hash tables */
		pext = plugin->extensions;
		while(*pext)
		{
			if(plugin->type == G3D_PLUGIN_IMAGE)
				g_hash_table_remove(context->exts_image, *pext);
			else if(plugin->type == G3D_PLUGIN_IMPORT)
				g_hash_table_remove(context->exts_import, *pext);

			pext ++;
		}

		/* cleanup struct data */
		plugins_free_plugin(plugin);

		/* free list item */
		plist = g_slist_remove(plist, plugin);
	}

	/* remove hash tables */
	g_hash_table_destroy(context->exts_image);
	g_hash_table_destroy(context->exts_import);
}

gchar *g3d_plugins_get_filetype(const gchar *filename)
{
	gchar *ext, *lcext;

	ext = strrchr(filename, '.');
	if(ext == NULL) {
		g_warning("can't determine file type: %s", filename);
		return NULL;
	}
	ext ++;
	if(strlen(ext) == 0) {
		g_warning("zero length file extension: %s", filename);
		return NULL;
	}

	lcext = g_strdup(ext);
	g_strdown(lcext);

	return lcext;
}

static G3DPlugin *get_plugin_for_type(G3DContext *context,
	const gchar *filename)
{
	G3DPlugin *plugin = NULL;
	gchar *lcext;

#ifdef USE_LIBMAGIC
	plugin = plugins_magic_lookup(context, filename);
#endif

	if(plugin == NULL)
	{
		/* try to get type by extension */
		lcext = g3d_plugins_get_filetype(filename);
		if(lcext == NULL)
			return NULL;

		plugin = g_hash_table_lookup(context->exts_import, lcext);

		g_free(lcext);
	}

	if(plugin == NULL)
	{
		g_warning("no handler for file '%s' found", filename);
		return NULL;
	}

	return plugin;
}

gboolean g3d_plugins_load_model(G3DContext *context, const gchar *filename,
	G3DModel *model)
{
	G3DPlugin *plugin = NULL;
	G3DStream *stream;
	gchar *basename, *dirname;
	gboolean retval = FALSE;
	gchar *olddir;

	plugin = get_plugin_for_type(context, filename);
	if(plugin == NULL)
		return FALSE;

	basename = g_path_get_basename(filename);
	dirname = g_path_get_dirname(filename);

	olddir = g_get_current_dir();
	/* TODO: since glib 2.8 there is a g_chdir() wrapper, use it if
	 * for some reason a glib >= 2.8 is required */
	chdir(dirname);

	if(plugin->loadmodelstream_func != NULL) {
		/* try to load the model via the more generic G3DStream interface */
		stream = g3d_stream_open_file(basename, "rb");
		if(stream) {
			retval = plugin->loadmodelstream_func(context, stream, model,
				plugin->user_data);
			g3d_stream_close(stream);
		}
		else {
			g_warning("failed to open '%s'", basename);
		}
	} else {
		retval = plugin->loadmodel_func(context, basename, model,
			plugin->user_data);
	}

	if(retval)
		model->plugin = plugin;

	g_free(basename);
	g_free(dirname);

	chdir(olddir);
	g_free(olddir);

	return retval;
}

gboolean g3d_plugins_load_model_from_stream(G3DContext *context,
	G3DStream *stream, G3DModel *model)
{
	G3DPlugin *plugin = NULL;

	g_return_val_if_fail(stream != NULL, FALSE);

	plugin = get_plugin_for_type(context, stream->uri);
	if(plugin == NULL)
		return FALSE;

	if((plugin->loadmodelstream_func != NULL) && plugin->loadmodelstream_func(
		context, stream, model, plugin->user_data)) {
		model->plugin = plugin;
		return TRUE;
	}
	return FALSE;
}

gboolean g3d_plugins_load_image(G3DContext *context, const gchar *filename,
	G3DImage *image)
{
	G3DPlugin *plugin;
	G3DStream *stream;
	gchar *lcext;
	gboolean retval;

	lcext = g3d_plugins_get_filetype(filename);
    if(lcext == NULL)
		return FALSE;

	plugin = g_hash_table_lookup(context->exts_image, lcext);
	if(plugin == NULL) {
		g_warning("no handler for filetype '.%s' found", lcext);
		g_free(lcext);
		return FALSE;
	}
	g_free(lcext);

	if(plugin->loadimagestream_func != NULL) {
		/* prefer stream loader */
		stream = g3d_stream_open_file(filename, "rb");
		if(stream) {
			retval = g3d_plugins_load_image_from_stream(context, stream,
				image);
			g3d_stream_close(stream);
			return retval;
		} else {
			g_warning("failed to open stream for '%s'", filename);
			return FALSE;
		}
	}

	if(plugin->loadimage_func == NULL) {
		g_warning("can't find symbol 'plugin_load_image' in %s", plugin->name);
		return FALSE;
	}

	retval = plugin->loadimage_func(context, filename, image,
		plugin->user_data);

	return retval;
}

gboolean g3d_plugins_load_image_from_stream(G3DContext *context,
	G3DStream *stream, G3DImage *image)
{
	G3DPlugin *plugin;
	gchar *lcext;

	lcext = g3d_plugins_get_filetype(stream->uri);
    if(lcext == NULL)
		return FALSE;

	plugin = g_hash_table_lookup(context->exts_image, lcext);
	if(plugin == NULL) {
		g_warning("no handler for filetype '.%s' found", lcext);
		g_free(lcext);
		return FALSE;
	}
	g_free(lcext);

	if(plugin->loadimagestream_func == NULL) {
		g_warning("can't find symbol 'plugin_load_image_from_stream' in %s",
			plugin->name);
		return FALSE;
	}

	return plugin->loadimagestream_func(context, stream, image,
		plugin->user_data);
}

struct plugins_ext_list {
	guint32 pos;
	gchar **list;
};

static void plugins_list_ext(gpointer key, gpointer value, gpointer user_data)
{
	struct plugins_ext_list *data;

	data = (struct plugins_ext_list *)user_data;

	data->list[data->pos] = g_strdup(key);
	data->pos ++;
}

gchar **g3d_plugins_get_image_extensions(G3DContext *context)
{
	gchar **list;
	guint32 n;
	struct plugins_ext_list data;

	n = g_hash_table_size(context->exts_image);
	list = g_new0(gchar *, n + 1);

	data.pos = 0;
	data.list = list;

	/* fill extension list */
	g_hash_table_foreach(context->exts_image, plugins_list_ext, &data);

	return list;
}