1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
34 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
35 * is set to 64. Don't try to be cleverer. */
36 #ifdef _FILE_OFFSET_BITS
37 #undef _FILE_OFFSET_BITS
40 #include <stdlib.h> /* free(), strtol() */
41 #include <stdio.h> /* sprintf() */
42 #include <string.h> /* strdup() */
49 #ifdef HAVE_SYS_TYPES_H
50 # include <sys/types.h>
52 #ifdef HAVE_SYS_STAT_H
53 # include <sys/stat.h>
59 #if !defined(HAVE_DYNAMIC_PLUGINS)
60 /* no support for plugins */
61 #elif defined(HAVE_DL_DYLD)
62 # if defined(HAVE_MACH_O_DYLD_H)
63 # include <mach-o/dyld.h>
65 #elif defined(HAVE_DL_BEOS)
66 # if defined(HAVE_IMAGE_H)
69 #elif defined(HAVE_DL_WINDOWS)
71 #elif defined(HAVE_DL_DLOPEN)
72 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
75 # if defined(HAVE_SYS_DL_H)
78 #elif defined(HAVE_DL_SHL_LOAD)
79 # if defined(HAVE_DL_H)
84 #include "config/configuration.h"
86 #include "vlc_charset.h"
88 #include "modules/modules.h"
89 #include "modules/builtin.h"
91 /*****************************************************************************
93 *****************************************************************************/
94 #ifdef HAVE_DYNAMIC_PLUGINS
95 static void AllocateAllPlugins ( vlc_object_t * );
96 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
97 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
98 static module_t * AllocatePlugin( vlc_object_t *, char * );
100 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
101 static int DeleteModule ( module_t *, vlc_bool_t );
102 #ifdef HAVE_DYNAMIC_PLUGINS
103 static void DupModule ( module_t * );
104 static void UndupModule ( module_t * );
107 static void module_LoadMain( vlc_object_t *p_this );
112 * Creates a module bank structure which will be filled later
113 * on with all the modules found.
114 * \param p_this vlc object structure
117 void __module_InitBank( vlc_object_t *p_this )
119 module_bank_t *p_bank = NULL;
121 libvlc_global_data_t *p_libvlc_global = vlc_global();
123 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
124 var_Get( p_libvlc_global, "libvlc", &lockval );
125 vlc_mutex_lock( lockval.p_address );
126 if( p_libvlc_global->p_module_bank )
128 p_libvlc_global->p_module_bank->i_usage++;
129 vlc_mutex_unlock( lockval.p_address );
130 var_Destroy( p_libvlc_global, "libvlc" );
133 vlc_mutex_unlock( lockval.p_address );
134 var_Destroy( p_libvlc_global, "libvlc" );
136 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
139 p_bank->psz_object_name = "module bank";
141 p_bank->i_cache = p_bank->i_loaded_cache = 0;
142 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
143 p_bank->b_cache = p_bank->b_cache_dirty =
144 p_bank->b_cache_delete = VLC_FALSE;
146 /* Everything worked, attach the object */
147 p_libvlc_global->p_module_bank = p_bank;
148 vlc_object_attach( p_bank, p_libvlc_global );
150 module_LoadMain( p_this );
157 * Unloads all unused plugin modules and empties the module
158 * bank in case of success.
159 * \param p_this vlc object structure
162 void __module_EndBank( vlc_object_t *p_this )
164 module_t * p_next = NULL;
166 libvlc_global_data_t *p_libvlc_global = vlc_global();
168 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
169 var_Get( p_libvlc_global, "libvlc", &lockval );
170 vlc_mutex_lock( lockval.p_address );
171 if( !p_libvlc_global->p_module_bank )
173 vlc_mutex_unlock( lockval.p_address );
174 var_Destroy( p_libvlc_global, "libvlc" );
177 if( --p_libvlc_global->p_module_bank->i_usage )
179 vlc_mutex_unlock( lockval.p_address );
180 var_Destroy( p_libvlc_global, "libvlc" );
183 vlc_mutex_unlock( lockval.p_address );
184 var_Destroy( p_libvlc_global, "libvlc" );
186 /* Save the configuration */
187 config_AutoSaveConfigFile( p_this );
189 #ifdef HAVE_DYNAMIC_PLUGINS
190 # define p_bank p_libvlc_global->p_module_bank
191 if( p_bank->b_cache ) CacheSave( p_this );
192 while( p_bank->i_loaded_cache-- )
194 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
197 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
198 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
199 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
200 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
201 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
204 if( p_bank->pp_loaded_cache )
206 free( p_bank->pp_loaded_cache );
207 p_bank->pp_loaded_cache = NULL;
209 while( p_bank->i_cache-- )
211 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
212 free( p_bank->pp_cache[p_bank->i_cache] );
213 p_bank->pp_cache[p_bank->i_cache] = NULL;
215 if( p_bank->pp_cache )
217 free( p_bank->pp_cache );
218 p_bank->pp_cache = NULL;
223 vlc_object_detach( p_libvlc_global->p_module_bank );
225 while( p_libvlc_global->p_module_bank->i_children )
227 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
229 if( DeleteModule( p_next, VLC_TRUE ) )
231 /* Module deletion failed */
232 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
233 p_next->psz_object_name );
235 /* We just free the module by hand. Niahahahahaha. */
236 vlc_object_detach( p_next );
237 vlc_object_release( p_next );
241 vlc_object_release( p_libvlc_global->p_module_bank );
242 p_libvlc_global->p_module_bank = NULL;
246 * Load the main program info into the module bank.
248 * Fills the module bank structure with the main module infos.
249 * This is very useful as it will allow us to consider the main program just
250 * as another module, and for instance the configuration options of main will
251 * be available in the module bank structure just as for every other module.
252 * \param p_this vlc object structure
255 static void module_LoadMain( vlc_object_t *p_this )
258 libvlc_global_data_t *p_libvlc_global = vlc_global();
260 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
261 var_Get( p_libvlc_global, "libvlc", &lockval );
262 vlc_mutex_lock( lockval.p_address );
263 if( p_libvlc_global->p_module_bank->b_main )
265 vlc_mutex_unlock( lockval.p_address );
266 var_Destroy( p_libvlc_global, "libvlc" );
269 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
270 vlc_mutex_unlock( lockval.p_address );
271 var_Destroy( p_libvlc_global, "libvlc" );
273 AllocateBuiltinModule( p_this, vlc_entry__main );
277 * Load all modules which we built with.
279 * Fills the module bank structure with the builtin modules.
280 * \param p_this vlc object structure
283 void __module_LoadBuiltins( vlc_object_t * p_this )
286 libvlc_global_data_t *p_libvlc_global = vlc_global();
288 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
289 var_Get( p_libvlc_global, "libvlc", &lockval );
290 vlc_mutex_lock( lockval.p_address );
291 if( p_libvlc_global->p_module_bank->b_builtins )
293 vlc_mutex_unlock( lockval.p_address );
294 var_Destroy( p_libvlc_global, "libvlc" );
297 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
298 vlc_mutex_unlock( lockval.p_address );
299 var_Destroy( p_libvlc_global, "libvlc" );
301 msg_Dbg( p_this, "checking builtin modules" );
302 ALLOCATE_ALL_BUILTINS();
308 * Load all plugin modules we can find.
309 * Fills the module bank structure with the plugin modules.
310 * \param p_this vlc object structure
313 void __module_LoadPlugins( vlc_object_t * p_this )
315 #ifdef HAVE_DYNAMIC_PLUGINS
317 libvlc_global_data_t *p_libvlc_global = vlc_global();
319 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
320 var_Get( p_libvlc_global, "libvlc", &lockval );
321 vlc_mutex_lock( lockval.p_address );
322 if( p_libvlc_global->p_module_bank->b_plugins )
324 vlc_mutex_unlock( lockval.p_address );
325 var_Destroy( p_libvlc_global, "libvlc" );
328 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
329 vlc_mutex_unlock( lockval.p_address );
330 var_Destroy( p_libvlc_global, "libvlc" );
332 msg_Dbg( p_this, "checking plugin modules" );
334 if( config_GetInt( p_this, "plugins-cache" ) )
335 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
337 if( p_libvlc_global->p_module_bank->b_cache ||
338 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
340 AllocateAllPlugins( p_this );
345 * Checks whether a module implements a capability.
347 * \param m the module
348 * \param cap the capability to check
349 * \return TRUE if the module have the capability
351 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
353 return !strcmp( m->psz_capability, cap );
357 * Get the internal name of a module
359 * \param m the module
360 * \return the module name
362 const char *module_GetObjName( const module_t *m )
364 return m->psz_object_name;
368 * Get the human-friendly name of a module.
370 * \param m the module
371 * \param long_name TRUE to have the long name of the module
372 * \return the short or long name of the module
374 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
376 if( long_name && ( m->psz_longname != NULL) )
377 return m->psz_longname;
379 return m->psz_shortname ?: m->psz_object_name;
383 * Get the help for a module
385 * \param m the module
388 const char *module_GetHelp( const module_t *m )
396 * Return the best module function, given a capability list.
397 * \param p_this the vlc object
398 * \param psz_capability list of capabilities needed
399 * \param psz_name name of the module asked
400 * \param b_strict TRUE yto use the strict mode
401 * \return the module or NULL in case of a failure
403 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
404 const char *psz_name, vlc_bool_t b_strict )
406 typedef struct module_list_t module_list_t;
413 module_list_t *p_next;
416 module_list_t *p_list, *p_first, *p_tmp;
419 int i_which_module, i_index = 0;
424 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
425 vlc_bool_t b_force_backup = p_this->b_force;
428 /* Deal with variables */
429 if( psz_name && psz_name[0] == '$' )
432 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
433 var_Get( p_this, psz_name + 1, &val );
434 psz_var = val.psz_string;
438 /* Count how many different shortcuts were asked for */
439 if( psz_name && *psz_name )
441 char *psz_parser, *psz_last_shortcut;
443 /* If the user wants none, give him none. */
444 if( !strcmp( psz_name, "none" ) )
451 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
453 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
455 if( *psz_parser == ',' )
459 psz_last_shortcut = psz_parser + 1;
463 /* Check if the user wants to override the "strict" mode */
464 if( psz_last_shortcut )
466 if( !strcmp(psz_last_shortcut, "none") )
471 else if( !strcmp(psz_last_shortcut, "any") )
473 b_strict = VLC_FALSE;
479 /* Sort the modules and test them */
480 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
481 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
483 unsigned i_cpu = vlc_CPU();
485 /* Parse the module list for capabilities and probe each of them */
486 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
488 int i_shortcut_bonus = 0;
490 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
492 /* Test that this module can do what we need */
493 if( !module_IsCapable( p_module, psz_capability ) )
495 /* Don't recurse through the sub-modules because vlc_list_find()
496 * will list them anyway. */
500 /* Test if we have the required CPU */
501 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
506 /* If we required a shortcut, check this plugin provides it. */
507 if( i_shortcuts > 0 )
510 const char *psz_name = psz_shortcuts;
512 /* Let's drop modules with a <= 0 score (unless they are
513 * explicitly requested) */
514 b_trash = p_module->i_score <= 0;
516 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
518 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
521 if( ( c = strchr( psz_name, '@' ) )
522 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
524 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
529 i_shortcut_bonus = i_short * 10000;
534 /* Go to the next shortcut... This is so lame! */
535 psz_name += strlen( psz_name ) + 1;
538 /* If we are in "strict" mode and we couldn't
539 * find the module in the list of provided shortcuts,
540 * then kick the bastard out of here!!! */
544 /* If we didn't require a shortcut, trash <= 0 scored plugins */
545 else if( p_module->i_score <= 0 )
552 /* Store this new module */
553 p_list[ i_index ].p_module = p_module;
554 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
555 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
557 /* Add it to the modules-to-probe list */
560 p_list[ 0 ].p_next = NULL;
565 /* Ok, so at school you learned that quicksort is quick, and
566 * bubble sort sucks raw eggs. But that's when dealing with
567 * thousands of items. Here we have barely 50. */
568 module_list_t *p_newlist = p_first;
570 if( p_first->i_score < p_list[ i_index ].i_score )
572 p_list[ i_index ].p_next = p_first;
573 p_first = &p_list[ i_index ];
577 while( p_newlist->p_next != NULL &&
578 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
580 p_newlist = p_newlist->p_next;
583 p_list[ i_index ].p_next = p_newlist->p_next;
584 p_newlist->p_next = &p_list[ i_index ];
591 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
592 i_index, i_index == 1 ? "" : "s" );
594 /* Lock all candidate modules */
596 while( p_tmp != NULL )
598 vlc_object_yield( p_tmp->p_module );
599 p_tmp = p_tmp->p_next;
602 /* We can release the list, interesting modules were yielded */
603 vlc_list_release( p_all );
605 /* Parse the linked list and use the first successful module */
607 while( p_tmp != NULL )
609 #ifdef HAVE_DYNAMIC_PLUGINS
610 /* Make sure the module is loaded in mem */
611 module_t *p_module = p_tmp->p_module;
612 if( p_module->b_submodule )
613 p_module = (module_t *)p_module->p_parent;
615 if( !p_module->b_builtin && !p_module->b_loaded )
617 module_t *p_new_module =
618 AllocatePlugin( p_this, p_module->psz_filename );
621 CacheMerge( p_this, p_module, p_new_module );
622 vlc_object_attach( p_new_module, p_module );
623 DeleteModule( p_new_module, VLC_TRUE );
628 p_this->b_force = p_tmp->b_force;
629 if( p_tmp->p_module->pf_activate
630 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
635 vlc_object_release( p_tmp->p_module );
636 p_tmp = p_tmp->p_next;
639 /* Store the locked module value */
642 p_module = p_tmp->p_module;
643 p_tmp = p_tmp->p_next;
650 /* Unlock the remaining modules */
651 while( p_tmp != NULL )
653 vlc_object_release( p_tmp->p_module );
654 p_tmp = p_tmp->p_next;
658 p_this->b_force = b_force_backup;
660 if( p_module != NULL )
662 msg_Dbg( p_this, "using %s module \"%s\"",
663 psz_capability, p_module->psz_object_name );
665 else if( p_first == NULL )
667 if( !strcmp( psz_capability, "access_demux" ) )
669 msg_Warn( p_this, "no %s module matched \"%s\"",
670 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
674 msg_Err( p_this, "no %s module matched \"%s\"",
675 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
677 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
678 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
681 else if( psz_name != NULL && *psz_name )
683 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
684 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
687 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
689 if( p_module && !p_this->psz_object_name )
691 /* This assumes that p_this is the object which will be using the
692 * module. That's not always the case ... but it is in most cases.
695 p_this->psz_object_name = strdup( psz_alias );
697 p_this->psz_object_name = strdup( p_module->psz_object_name );
700 free( psz_shortcuts );
703 /* Don't forget that the module is still locked */
710 * This function must be called by the thread that called module_Need, to
711 * decrease the reference count and allow for hiding of modules.
712 * \param p_this vlc object structure
713 * \param p_module the module structure
716 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
718 /* Use the close method */
719 if( p_module->pf_deactivate )
721 p_module->pf_deactivate( p_this );
724 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
726 vlc_object_release( p_module );
732 * Get a pointer to a module_t given it's name.
734 * \param p_this vlc object structure
735 * \param psz_name the name of the module
736 * \return a pointer to the module or NULL in case of a failure
738 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
742 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
743 for( i = 0 ; i < p_list->i_count; i++)
745 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
746 const char *psz_module_name = p_module->psz_object_name;
747 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
749 /* We can release the list, and return yes */
750 vlc_object_yield( p_module );
751 vlc_list_release( p_list );
755 vlc_list_release( p_list );
761 * Release a module_t pointer from module_Find().
763 * \param module the module to release
766 void module_Put( module_t *module )
768 vlc_object_release( module );
773 * Tell if a module exists and release it in thic case
775 * \param p_this vlc object structure
776 * \param psz_name th name of the module
777 * \return TRUE if the module exists
779 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
781 module_t *p_module = __module_Find( p_this, psz_name );
784 module_Put( p_module );
794 * GetModuleNamesForCapability
796 * Return a NULL terminated array with the names of the modules
797 * that have a certain capability.
798 * Free after uses both the string and the table.
799 * \param p_this vlc object structure
800 * \param psz_capability the capability asked
801 * \param pppsz_longname an pointer to an array of string to contain
802 the long names of the modules. If set to NULL the function don't use it.
803 * \return the NULL terminated array
805 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
806 const char *psz_capability,
807 char ***pppsz_longname )
813 /* Do it in two passes : count the number of modules before */
814 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
815 for( i = 0 ; i < p_list->i_count; i++)
817 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
818 const char *psz_module_capability = p_module->psz_capability;
819 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
823 psz_ret = malloc( sizeof(char*) * (count+1) );
825 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
826 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
828 msg_Err( p_this, "out of memory" );
830 free( *pppsz_longname );
831 *pppsz_longname = NULL;
832 vlc_list_release( p_list );
837 for( i = 0 ; i < p_list->i_count; i++)
839 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
840 const char *psz_module_capability = p_module->psz_capability;
841 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
843 int k = -1; /* hack to handle submodules properly */
844 if( p_module->b_submodule )
846 while( p_module->pp_shortcuts[++k] != NULL );
849 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
850 :p_module->psz_object_name );
852 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
856 psz_ret[count] = NULL;
858 vlc_list_release( p_list );
864 * Get the configuration of a module
866 * \param module the module
867 * \param psize the size of the configuration returned
868 * \return the configuration as an array
870 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
873 unsigned size = module->confsize;
874 module_config_t *config = malloc( size * sizeof( *config ) );
876 assert( psize != NULL );
879 for( i = 0, j = 0; i < size; i++ )
881 const module_config_t *item = module->p_config + i;
882 if( item->b_internal /* internal option */
883 || item->b_unsaveable /* non-modifiable option */
884 || item->b_removed /* removed option */ )
888 memcpy( config + j, item, sizeof( *config ) );
897 * Release the configuration
899 * \param the configuration
902 void module_PutConfig( module_config_t *config )
907 /*****************************************************************************
908 * Following functions are local.
909 *****************************************************************************/
911 /*****************************************************************************
912 * AllocateAllPlugins: load all plugin modules we can find.
913 *****************************************************************************/
914 #ifdef HAVE_DYNAMIC_PLUGINS
915 static void AllocateAllPlugins( vlc_object_t *p_this )
917 /* Yes, there are two NULLs because we replace one with "plugin-path". */
918 #if defined( WIN32 ) || defined( UNDER_CE )
919 const char *path[] = { "modules", "", "plugins", NULL, NULL };
921 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
924 const char *const *ppsz_path;
926 /* If the user provided a plugin path, we add it to the list */
927 char *userpath = config_GetPsz( p_this, "plugin-path" );
928 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
930 for( ppsz_path = path; *ppsz_path != NULL; ppsz_path++ )
934 if( !**ppsz_path ) continue;
936 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
938 /* Handle relative as well as absolute paths */
940 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
941 (*ppsz_path)[1] != ':' )
943 if( (*ppsz_path)[0] != '/' )
946 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
947 vlc_global()->psz_vlcpath, *ppsz_path) )
952 psz_fullpath = strdup( *ppsz_path );
954 if( psz_fullpath == NULL )
957 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
959 /* Don't go deeper than 5 subdirectories */
960 AllocatePluginDir( p_this, psz_fullpath, 5 );
962 free( psz_fullpath );
965 /* Free plugin-path */
969 /*****************************************************************************
970 * AllocatePluginDir: recursively parse a directory to look for plugins
971 *****************************************************************************/
972 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
975 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
981 #if defined( UNDER_CE ) || defined( _MSC_VER )
983 wchar_t psz_wpath[MAX_PATH + 256];
984 wchar_t psz_wdir[MAX_PATH];
986 char psz_path[MAX_PATH + 256];
987 WIN32_FIND_DATA finddata;
993 struct dirent * file;
997 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1002 #if defined( UNDER_CE ) || defined( _MSC_VER )
1004 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1006 rc = GetFileAttributes( psz_wdir );
1007 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1009 /* Parse all files in the directory */
1010 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1012 rc = GetFileAttributes( psz_dir );
1013 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1016 /* Parse all files in the directory */
1017 sprintf( psz_path, "%s\\*", psz_dir );
1020 handle = FindFirstFile( psz_wpath, &finddata );
1022 handle = FindFirstFile( psz_path, &finddata );
1024 if( handle == INVALID_HANDLE_VALUE )
1026 /* Empty directory */
1030 /* Parse the directory and try to load all files it contains. */
1034 unsigned int i_len = wcslen( finddata.cFileName );
1035 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1036 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1038 unsigned int i_len = strlen( finddata.cFileName );
1039 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1042 /* Skip ".", ".." */
1043 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1044 || !strcmp( finddata.cFileName, ".." ) )
1046 if( !FindNextFile( handle, &finddata ) ) break;
1051 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1053 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1056 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1058 else if( i_len > strlen( LIBEXT )
1059 /* We only load files ending with LIBEXT */
1060 && !strncasecmp( psz_path + strlen( psz_path)
1062 LIBEXT, strlen( LIBEXT ) ) )
1064 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1065 int64_t i_time = 0, i_size = 0;
1068 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1071 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1075 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1077 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1078 i_size = attrbuf.nFileSizeHigh;
1080 i_size |= attrbuf.nFileSizeLow;
1082 psz_file = psz_path;
1084 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1087 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1089 /* Close the directory */
1090 FindClose( handle );
1093 dir = opendir( psz_dir );
1099 i_dirlen = strlen( psz_dir );
1101 /* Parse the directory and try to load all files it contains. */
1102 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1104 struct stat statbuf;
1108 /* Skip ".", ".." */
1109 if( !*file->d_name || !strcmp( file->d_name, "." )
1110 || !strcmp( file->d_name, ".." ) )
1115 i_len = strlen( file->d_name );
1116 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1117 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1119 i_stat = stat( psz_file, &statbuf );
1120 if( !i_stat && statbuf.st_mode & S_IFDIR )
1122 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1124 else if( i_len > strlen( LIBEXT )
1125 /* We only load files ending with LIBEXT */
1126 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1127 LIBEXT, strlen( LIBEXT ) ) )
1129 int64_t i_time = 0, i_size = 0;
1133 i_time = statbuf.st_mtime;
1134 i_size = statbuf.st_size;
1137 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1143 /* Close the directory */
1149 /*****************************************************************************
1150 * AllocatePluginFile: load a module into memory and initialize it.
1151 *****************************************************************************
1152 * This function loads a dynamically loadable module and allocates a structure
1153 * for its information data. The module can then be handled by module_Need
1154 * and module_Unneed. It can be removed by DeleteModule.
1155 *****************************************************************************/
1156 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1157 int64_t i_file_time, int64_t i_file_size )
1159 module_t * p_module = NULL;
1160 module_cache_t *p_cache_entry = NULL;
1163 * Check our plugins cache first then load plugin if needed
1166 CacheFind( psz_file, i_file_time, i_file_size );
1168 if( !p_cache_entry )
1170 p_module = AllocatePlugin( p_this, psz_file );
1174 /* If junk dll, don't try to load it */
1175 if( p_cache_entry->b_junk )
1181 module_config_t *p_item = NULL, *p_end = NULL;
1183 p_module = p_cache_entry->p_module;
1184 p_module->b_loaded = VLC_FALSE;
1186 /* For now we force loading if the module's config contains
1187 * callbacks or actions.
1188 * Could be optimized by adding an API call.*/
1189 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1190 p_item < p_end; p_item++ )
1192 if( p_item->pf_callback || p_item->i_action )
1194 p_module = AllocatePlugin( p_this, psz_file );
1198 if( p_module == p_cache_entry->p_module )
1199 p_cache_entry->b_used = VLC_TRUE;
1205 libvlc_global_data_t *p_libvlc_global = vlc_global();
1207 /* Everything worked fine !
1208 * The module is ready to be added to the list. */
1209 p_module->b_builtin = VLC_FALSE;
1211 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1212 p_module->psz_object_name, p_module->psz_longname ); */
1214 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1216 if( !p_libvlc_global->p_module_bank->b_cache )
1219 #define p_bank p_libvlc_global->p_module_bank
1220 /* Add entry to cache */
1222 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1223 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1224 if( !p_bank->pp_cache[p_bank->i_cache] )
1226 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1227 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1228 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1229 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1230 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1231 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1235 return p_module ? 0 : -1;
1238 /*****************************************************************************
1239 * AllocatePlugin: load a module into memory and initialize it.
1240 *****************************************************************************
1241 * This function loads a dynamically loadable module and allocates a structure
1242 * for its information data. The module can then be handled by module_Need
1243 * and module_Unneed. It can be removed by DeleteModule.
1244 *****************************************************************************/
1245 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1247 module_t * p_module = NULL;
1248 module_handle_t handle;
1250 if( module_Load( p_this, psz_file, &handle ) )
1253 /* Now that we have successfully loaded the module, we can
1254 * allocate a structure for it */
1255 p_module = vlc_module_create( p_this );
1256 if( p_module == NULL )
1258 msg_Err( p_this, "out of memory" );
1259 module_Unload( handle );
1263 /* We need to fill these since they may be needed by module_Call() */
1264 p_module->psz_filename = psz_file;
1265 p_module->handle = handle;
1266 p_module->b_loaded = VLC_TRUE;
1268 /* Initialize the module: fill p_module, default config */
1269 if( module_Call( p_module ) != 0 )
1271 /* We couldn't call module_init() */
1272 vlc_object_release( p_module );
1273 module_Unload( handle );
1277 DupModule( p_module );
1278 p_module->psz_filename = strdup( p_module->psz_filename );
1280 /* Everything worked fine ! The module is ready to be added to the list. */
1281 p_module->b_builtin = VLC_FALSE;
1286 /*****************************************************************************
1287 * DupModule: make a plugin module standalone.
1288 *****************************************************************************
1289 * This function duplicates all strings in the module, so that the dynamic
1290 * object can be unloaded. It acts recursively on submodules.
1291 *****************************************************************************/
1292 static void DupModule( module_t *p_module )
1294 const char **pp_shortcut;
1297 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1299 *pp_shortcut = strdup( *pp_shortcut );
1302 /* We strdup() these entries so that they are still valid when the
1303 * module is unloaded. */
1304 p_module->psz_object_name = strdup( p_module->psz_object_name );
1305 p_module->psz_capability = strdup( p_module->psz_capability );
1306 p_module->psz_shortname = p_module->psz_shortname ?
1307 strdup( p_module->psz_shortname ) : NULL;
1308 p_module->psz_longname = strdup( p_module->psz_longname );
1309 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1312 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1314 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1318 /*****************************************************************************
1319 * UndupModule: free a duplicated module.
1320 *****************************************************************************
1321 * This function frees the allocations done in DupModule().
1322 *****************************************************************************/
1323 static void UndupModule( module_t *p_module )
1325 const char **pp_shortcut;
1328 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1330 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1333 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1335 free( (void*)*pp_shortcut );
1338 free( (void*)p_module->psz_object_name );
1339 free( p_module->psz_capability );
1340 free( (void*)p_module->psz_shortname );
1341 free( (void*)p_module->psz_longname );
1342 free( (void*)p_module->psz_help );
1345 #endif /* HAVE_DYNAMIC_PLUGINS */
1347 /*****************************************************************************
1348 * AllocateBuiltinModule: initialize a builtin module.
1349 *****************************************************************************
1350 * This function registers a builtin module and allocates a structure
1351 * for its information data. The module can then be handled by module_Need
1352 * and module_Unneed. It can be removed by DeleteModule.
1353 *****************************************************************************/
1354 static int AllocateBuiltinModule( vlc_object_t * p_this,
1355 int ( *pf_entry ) ( module_t * ) )
1357 module_t * p_module;
1359 /* Now that we have successfully loaded the module, we can
1360 * allocate a structure for it */
1361 p_module = vlc_module_create( p_this );
1362 if( p_module == NULL )
1364 msg_Err( p_this, "out of memory" );
1368 /* Initialize the module : fill p_module->psz_object_name, etc. */
1369 if( pf_entry( p_module ) != 0 )
1371 /* With a well-written module we shouldn't have to print an
1372 * additional error message here, but just make sure. */
1373 msg_Err( p_this, "failed calling entry point in builtin module" );
1374 vlc_object_release( p_module );
1378 /* Everything worked fine ! The module is ready to be added to the list. */
1379 p_module->b_builtin = VLC_TRUE;
1381 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1382 p_module->psz_object_name, p_module->psz_longname ); */
1384 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1389 /*****************************************************************************
1390 * DeleteModule: delete a module and its structure.
1391 *****************************************************************************
1392 * This function can only be called if the module isn't being used.
1393 *****************************************************************************/
1394 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1396 if( !p_module ) return VLC_EGENERIC;
1398 vlc_object_detach( p_module );
1400 /* We free the structures that we strdup()ed in Allocate*Module(). */
1401 #ifdef HAVE_DYNAMIC_PLUGINS
1402 if( !p_module->b_builtin )
1404 if( p_module->b_loaded && p_module->b_unloadable )
1406 module_Unload( p_module->handle );
1408 UndupModule( p_module );
1409 free( p_module->psz_filename );
1413 /* Free and detach the object's children */
1414 while( p_module->i_children )
1416 vlc_object_t *p_this = p_module->pp_children[0];
1417 vlc_object_detach( p_this );
1418 vlc_object_release( p_this );
1421 config_Free( p_module );
1422 vlc_object_release( p_module );