6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
23 #include <glib/gmem.h>
24 #include <glib/gstrfuncs.h>
29 #define SOEXT ".dylib"
34 /** Dummy plugin used as a plugin directory marker */
35 #define DUMMY_PLUGIN ("lxdream_dummy" SOEXT)
37 const char *plugin_type_string[] = { "undefined", "audio driver", "input driver" };
39 int main(int argc, char *argv[]);
40 static const char *exec_path = NULL;
43 * Return the full path to the main binary
45 static const char *get_exec_path()
47 if( exec_path == NULL ) {
50 /* Use dladdr for this, since it should be available for any platform
51 * that we can support plugins on at all.
53 if( dladdr( main, &dli) ) {
54 gchar *path = g_strdup( dli.dli_fname );
55 char *i = strrchr( path, '/' );
67 static gboolean plugin_load( const gchar *plugin_path )
69 void *so = dlopen(plugin_path, RTLD_NOW|RTLD_LOCAL);
71 WARN("Failed to load plugin '%s': %s", plugin_path, dlerror());
75 struct plugin_struct *plugin = (struct plugin_struct *)dlsym(so,"lxdream_plugin_entry");
76 if( plugin == NULL ) {
77 WARN("Failed to load plugin: '%s': Not an lxdream plugin", plugin_path);
82 if( strcmp(lxdream_short_version, plugin->version) != 0 ) {
83 WARN("Failed to load plugin: '%s': Incompatible version (%s)", plugin_path, plugin->version);
88 if( plugin->type == PLUGIN_NONE ) {
89 /* 'dummy' plugin - we don't actually want to load it */
94 if( plugin->type < PLUGIN_MIN_TYPE || plugin->type > PLUGIN_MAX_TYPE ) {
95 WARN("Failed to load plugin: '%s': Unrecognized plugin type (%d)", plugin_path, plugin->type );
100 if( plugin->register_plugin() == FALSE ) {
101 WARN("Failed to load plugin: '%s': Initialization failed", plugin_path);
105 DEBUG("Loaded %s '%s'", plugin_type_string[plugin->type], plugin->name);
109 static gboolean has_plugins( const gchar *path )
113 gchar *dummy_name = g_strdup_printf( "%s/%s", path, DUMMY_PLUGIN );
114 if( stat( dummy_name, &st ) == 0 ) {
122 * Scan the plugin dir and load all valid plugins.
124 static int plugin_load_all( const gchar *plugin_dir )
126 int plugin_count = 0;
129 DIR *dir = opendir(plugin_dir);
131 WARN( "Unable to open plugin directory '%s'", plugin_dir );
135 while( (ent = readdir(dir)) != NULL ) {
136 const char *ext = strrchr(ent->d_name, '.');
137 if( ext != NULL && strcasecmp(SOEXT,ext) == 0 ) {
138 char *libname = g_strdup_printf( "%s/%s", plugin_dir,ent->d_name );
139 if( plugin_load( libname ) ) {
150 const char *path = get_exec_path();
151 if( path == NULL || !has_plugins(path) ) {
152 path = get_plugin_path();
155 DEBUG( "Plugin directory: %s", path );
156 return plugin_load_all( path );
.