1 /*****************************************************************************
2 * plugins.c : Dynamic plugin management functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
26 #include <stdlib.h> /* free(), strtol() */
27 #include <stdio.h> /* sprintf() */
28 #include <string.h> /* strerror() */
29 #include <errno.h> /* ENOMEM */
31 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
32 #include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
34 #elif defined(HAVE_IMAGE_H) /* BeOS */
38 #error no dynamic plugins available on your system !
42 #include "beos_specific.h"
50 /* Local prototypes */
51 char * TestPlugin ( plugin_id_t *p_plugin_id, char * psz_name );
52 int AllocatePlugin ( plugin_id_t plugin_id, plugin_bank_t * p_bank,
53 char * psz_filename );
55 plugin_bank_t * bank_Create( void )
57 plugin_bank_t *p_bank;
60 /* Allocate structure */
61 p_bank = malloc( sizeof( plugin_bank_t ) );
64 intf_ErrMsg("plugin bank error: %s\n", strerror( ENOMEM ) );
68 /* Initialize structure */
69 for( i = 0 ; i < MAX_PLUGIN_COUNT ; i++ )
71 p_bank->p_info[ i ] = NULL;
73 p_bank->i_plugin_count = MAX_PLUGIN_COUNT;
75 intf_Msg("Plugin bank initialized\n");
79 void bank_Init( plugin_bank_t * p_bank )
84 /* FIXME: we should browse all directories to get plugins */
85 #define SEEK_PLUGIN( name ) \
86 psz_filename = TestPlugin( &tmp, name ); \
87 if( psz_filename ) AllocatePlugin( tmp, p_bank, psz_filename );
89 SEEK_PLUGIN( "beos" );
92 SEEK_PLUGIN( "gnome" );
95 SEEK_PLUGIN( "yuvmmx" );
97 SEEK_PLUGIN( "dummy" );
102 void bank_Destroy( plugin_bank_t * p_bank )
108 * Following functions are local
111 int AllocatePlugin( plugin_id_t plugin_id, plugin_bank_t * p_bank,
112 char * psz_filename )
114 typedef plugin_info_t * ( get_config_t ) ( void );
115 get_config_t * p_func;
118 for( i = 0 ; i < p_bank->i_plugin_count ; i++ )
120 if( p_bank->p_info[ i ] == NULL )
126 /* no room to store that plugin, quit */
127 if( i == p_bank->i_plugin_count )
129 intf_ErrMsg( "plugin bank error: reached max plugin count (%i), "
130 "increase MAX_PLUGIN_COUNT\n", p_bank->i_plugin_count );
134 /* system-specific dynamic symbol loading */
135 GET_PLUGIN( p_func, plugin_id, "GetConfig" );
137 /* if it failed, just quit */
143 /* run the plugin function to initialize the structure */
144 p_bank->p_info[ i ] = p_func( );
145 p_bank->p_info[ i ]->plugin_id = plugin_id;
147 /* Tell the world we found it */
148 intf_Msg( "Found plugin: %s (version %s)\n", p_bank->p_info[ i ]->psz_name,
149 p_bank->p_info[ i ]->psz_version );
156 char * TestPlugin ( plugin_id_t *p_plugin_id, char * psz_name )
158 int i_count, i_length;
160 char * psz_plugin_path[ ] =
163 "lib", /* this one should disappear */
168 i_length = strlen( psz_name );
170 for ( i_count = 0 ; psz_plugin_path[ i_count ] ; i_count++ )
173 char * psz_program_path;
175 psz_program_path = beos_GetProgramPath();
176 psz_plugin = malloc( strlen(psz_plugin_path[i_count]) +
177 strlen(psz_program_path) + i_length + 5 );
178 sprintf( psz_plugin, "%s/%s/%s.so", psz_program_path,
179 psz_plugin_path[i_count], psz_name );
181 *p_plugin_id = load_add_on( psz_plugin );
183 psz_plugin = malloc( strlen(psz_plugin_path[i_count]) + i_length + 5 );
184 sprintf( psz_plugin, "%s/%s.so", psz_plugin_path[i_count], psz_name );
186 *p_plugin_id = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
190 if( *p_plugin_id >= 0 )
192 if( *p_plugin_id != NULL )
195 /* plugin successfuly dlopened */
196 return( psz_plugin );
201 intf_DbgMsg( "%s\n", dlerror() );
212 void TrashPlugin ( plugin_id_t plugin_id )
215 unload_add_on( plugin_id );
217 dlclose( plugin_id );