]> git.sesse.net Git - vlc/blob - src/misc/plugins.c
1b2778e275336d2ad4beb3444876e8d341e9dd6a
[vlc] / src / misc / plugins.c
1 /*****************************************************************************
2  * plugins.c : Dynamic plugin management functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  * 
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.
17  *
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  *****************************************************************************/
22 #include "defs.h"
23
24 #include "config.h"
25
26 #include <stdlib.h>                                      /* free(), strtol() */
27 #include <stdio.h>                                              /* sprintf() */
28 #include <string.h>                                            /* strerror() */
29 #include <errno.h>                                                 /* ENOMEM */
30
31 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
32 #include <dlfcn.h>                           /* dlopen(), dlsym(), dlclose() */
33
34 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
35 #include <image.h>
36
37 #else
38 #error no dynamic plugins available on your system !
39 #endif
40
41 #ifdef SYS_BEOS
42 #include "beos_specific.h"
43 #endif
44
45 #include "common.h"
46
47 #include "intf_msg.h"
48 #include "plugins.h"
49
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 );
54
55 plugin_bank_t * bank_Create( void )
56 {
57     plugin_bank_t *p_bank;
58     int i;
59
60     /* Allocate structure */
61     p_bank = malloc( sizeof( plugin_bank_t ) );
62     if( !p_bank )
63     {
64         intf_ErrMsg("plugin bank error: %s\n", strerror( ENOMEM ) );
65         return( NULL );
66     }
67
68     /* Initialize structure */
69     for( i = 0 ; i < MAX_PLUGIN_COUNT ; i++ )
70     {
71         p_bank->p_info[ i ] = NULL;
72     }
73     p_bank->i_plugin_count = MAX_PLUGIN_COUNT;
74
75     intf_Msg("Plugin bank initialized\n");
76     return( p_bank );
77 }
78
79 void bank_Init( plugin_bank_t * p_bank )
80 {
81     plugin_id_t tmp;
82     char * psz_filename;
83
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 );
88
89     /* Arch plugins */
90     SEEK_PLUGIN( "beos" );
91
92     /* Low level Video */
93     SEEK_PLUGIN( "x11" );
94     SEEK_PLUGIN( "fb" );
95     SEEK_PLUGIN( "glide" );
96     SEEK_PLUGIN( "mga" );
97      
98     /* High level Video */
99     SEEK_PLUGIN( "gnome" );
100     SEEK_PLUGIN( "ggi" );
101     SEEK_PLUGIN( "sdl" );
102    
103     /* Video calculus */
104     SEEK_PLUGIN( "yuvmmx" );
105     SEEK_PLUGIN( "yuv" );
106
107     /* Audio pluins */
108     SEEK_PLUGIN( "dsp" );
109     SEEK_PLUGIN( "esd" );
110     
111     /* Dummy plugin */
112     SEEK_PLUGIN( "dummy" );
113
114 #undef SEEK_PLUGIN
115 }
116
117 void bank_Destroy( plugin_bank_t * p_bank )
118 {
119     int i;
120     for( i = 0 ; i < p_bank->i_plugin_count ; i++ )
121     {
122         if( p_bank->p_info[ i ] != NULL )
123         {
124             free( p_bank->p_info[ i ]-> psz_filename );
125         }
126     }
127
128     free( p_bank );
129 }
130
131 /*
132  * Following functions are local
133  */
134
135 char * TestPlugin ( plugin_id_t *p_plugin_id, char * psz_name )
136 {
137     int i_count, i_length;
138     char * psz_plugin;
139     char * psz_plugin_path[ ] =
140     {
141         ".",
142         "lib", /* this one should disappear */
143         PLUGIN_PATH,
144         NULL
145     };
146
147     i_length = strlen( psz_name );
148
149     for ( i_count = 0 ; psz_plugin_path[ i_count ] ; i_count++ )
150     {
151 #ifdef SYS_BEOS
152         char * psz_program_path;
153         
154         psz_program_path = beos_GetProgramPath();
155         psz_plugin = malloc( strlen(psz_plugin_path[i_count]) +
156                              strlen(psz_program_path) + i_length + 6 );
157         sprintf( psz_plugin, "%s/%s/%s.so", psz_program_path,
158                  psz_plugin_path[i_count], psz_name );        
159
160         *p_plugin_id = load_add_on( psz_plugin );
161 #else
162         psz_plugin = malloc( strlen(psz_plugin_path[i_count]) + i_length + 5 );
163         sprintf( psz_plugin, "%s/%s.so", psz_plugin_path[i_count], psz_name );
164
165         *p_plugin_id = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
166 #endif
167
168 #ifdef SYS_BEOS
169         if( *p_plugin_id >= 0 )
170 #else
171         if( *p_plugin_id != NULL )
172 #endif
173         {
174             /* plugin successfuly dlopened */
175             return( psz_plugin );
176         }
177 #ifndef SYS_BEOS
178         else
179         {
180             intf_DbgMsg( "%s\n", dlerror() );
181         }
182 #endif
183
184         free( psz_plugin );
185     }
186
187     return( NULL );
188 }
189
190
191 int AllocatePlugin( plugin_id_t plugin_id, plugin_bank_t * p_bank,
192                     char * psz_filename )
193 {
194     typedef plugin_info_t * ( get_config_t ) ( void );
195     get_config_t * p_func;   
196     int i;
197
198     for( i = 0 ; i < p_bank->i_plugin_count ; i++ )
199     {
200         if( p_bank->p_info[ i ] == NULL )
201         {
202             break;
203         }
204     }
205
206     /* no room to store that plugin, quit */
207     if( i == p_bank->i_plugin_count )
208     {
209         intf_ErrMsg( "plugin bank error: reached max plugin count (%i), "
210                      "increase MAX_PLUGIN_COUNT\n", p_bank->i_plugin_count );
211         return( -1 );
212     }
213
214     /* system-specific dynamic symbol loading */
215     GET_PLUGIN( p_func, plugin_id, "GetConfig" );
216
217     /* if it failed, just quit */
218     if( !p_func )
219     {
220         return( -1 );
221     }
222
223     /* run the plugin function to initialize the structure */
224     p_bank->p_info[ i ]            = p_func( );
225     p_bank->p_info[ i ]->plugin_id = plugin_id;
226     p_bank->p_info[ i ]->psz_filename = strdup( psz_filename );
227
228
229     /* Tell the world we found it */
230     intf_Msg( "Plugin %i: %s %s [0x%x]\n", i,
231               p_bank->p_info[ i ]->psz_name,
232               p_bank->p_info[ i ]->psz_version,
233               p_bank->p_info[ i ]->i_score );
234
235     /* return nicely */
236     return( 0 );
237 }
238
239 #if 0
240 void TrashPlugin ( plugin_id_t plugin_id )
241 {
242 #ifdef SYS_BEOS
243     unload_add_on( plugin_id );
244 #else
245     dlclose( plugin_id );
246 #endif
247 }
248 #endif
249