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 *****************************************************************************/
30 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
31 * is set to 64. Don't try to be cleverer. */
32 #ifdef _FILE_OFFSET_BITS
33 #undef _FILE_OFFSET_BITS
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if !defined(HAVE_DYNAMIC_PLUGINS)
55 /* no support for plugins */
56 #elif defined(HAVE_DL_DYLD)
57 # if defined(HAVE_MACH_O_DYLD_H)
58 # include <mach-o/dyld.h>
60 #elif defined(HAVE_DL_BEOS)
61 # if defined(HAVE_IMAGE_H)
64 #elif defined(HAVE_DL_WINDOWS)
66 #elif defined(HAVE_DL_DLOPEN)
67 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 # if defined(HAVE_SYS_DL_H)
73 #elif defined(HAVE_DL_SHL_LOAD)
74 # if defined(HAVE_DL_H)
79 #include "config/config.h"
81 #include "vlc_charset.h"
83 #include "modules/modules.h"
84 #include "modules/builtin.h"
86 /*****************************************************************************
88 *****************************************************************************/
89 #ifdef HAVE_DYNAMIC_PLUGINS
90 static void AllocateAllPlugins ( vlc_object_t * );
91 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
92 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
93 static module_t * AllocatePlugin( vlc_object_t *, char * );
95 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
96 static int DeleteModule ( module_t *, vlc_bool_t );
97 #ifdef HAVE_DYNAMIC_PLUGINS
98 static void DupModule ( module_t * );
99 static void UndupModule ( module_t * );
102 static void module_LoadMain( vlc_object_t *p_this );
104 /*****************************************************************************
105 * module_InitBank: create the module bank.
106 *****************************************************************************
107 * This function creates a module bank structure which will be filled later
108 * on with all the modules found.
109 *****************************************************************************/
110 void __module_InitBank( vlc_object_t *p_this )
112 module_bank_t *p_bank = NULL;
114 libvlc_global_data_t *p_libvlc_global = vlc_global();
116 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
117 var_Get( p_libvlc_global, "libvlc", &lockval );
118 vlc_mutex_lock( lockval.p_address );
119 if( p_libvlc_global->p_module_bank )
121 p_libvlc_global->p_module_bank->i_usage++;
122 vlc_mutex_unlock( lockval.p_address );
123 var_Destroy( p_libvlc_global, "libvlc" );
126 vlc_mutex_unlock( lockval.p_address );
127 var_Destroy( p_libvlc_global, "libvlc" );
129 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
132 p_bank->psz_object_name = "module bank";
134 p_bank->i_cache = p_bank->i_loaded_cache = 0;
135 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
136 p_bank->b_cache = p_bank->b_cache_dirty =
137 p_bank->b_cache_delete = VLC_FALSE;
139 /* Everything worked, attach the object */
140 p_libvlc_global->p_module_bank = p_bank;
141 vlc_object_attach( p_bank, p_libvlc_global );
143 module_LoadMain( p_this );
147 /*****************************************************************************
148 * module_EndBank: empty the module bank.
149 *****************************************************************************
150 * This function unloads all unused plugin modules and empties the module
151 * bank in case of success.
152 *****************************************************************************/
153 void __module_EndBank( vlc_object_t *p_this )
155 module_t * p_next = NULL;
157 libvlc_global_data_t *p_libvlc_global = vlc_global();
159 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
160 var_Get( p_libvlc_global, "libvlc", &lockval );
161 vlc_mutex_lock( lockval.p_address );
162 if( !p_libvlc_global->p_module_bank )
164 vlc_mutex_unlock( lockval.p_address );
165 var_Destroy( p_libvlc_global, "libvlc" );
168 if( --p_libvlc_global->p_module_bank->i_usage )
170 vlc_mutex_unlock( lockval.p_address );
171 var_Destroy( p_libvlc_global, "libvlc" );
174 vlc_mutex_unlock( lockval.p_address );
175 var_Destroy( p_libvlc_global, "libvlc" );
177 config_AutoSaveConfigFile( p_this );
179 #ifdef HAVE_DYNAMIC_PLUGINS
180 # define p_bank p_libvlc_global->p_module_bank
181 if( p_bank->b_cache ) CacheSave( p_this );
182 while( p_bank->i_loaded_cache-- )
184 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
186 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
187 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
188 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
189 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
190 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
193 if( p_bank->pp_loaded_cache )
195 free( p_bank->pp_loaded_cache );
196 p_bank->pp_loaded_cache = NULL;
198 while( p_bank->i_cache-- )
200 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
201 free( p_bank->pp_cache[p_bank->i_cache] );
202 p_bank->pp_cache[p_bank->i_cache] = NULL;
204 if( p_bank->pp_cache )
206 free( p_bank->pp_cache );
207 p_bank->pp_cache = NULL;
212 vlc_object_detach( p_libvlc_global->p_module_bank );
214 while( p_libvlc_global->p_module_bank->i_children )
216 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
218 if( DeleteModule( p_next, VLC_TRUE ) )
220 /* Module deletion failed */
221 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
222 p_next->psz_object_name );
224 /* We just free the module by hand. Niahahahahaha. */
225 vlc_object_detach( p_next );
226 vlc_object_destroy( p_next );
230 vlc_object_destroy( p_libvlc_global->p_module_bank );
231 p_libvlc_global->p_module_bank = NULL;
234 /*****************************************************************************
235 * module_LoadMain: load the main program info into the module bank.
236 *****************************************************************************
237 * This function fills the module bank structure with the main module infos.
238 * This is very useful as it will allow us to consider the main program just
239 * as another module, and for instance the configuration options of main will
240 * be available in the module bank structure just as for every other module.
241 *****************************************************************************/
242 static void module_LoadMain( vlc_object_t *p_this )
245 libvlc_global_data_t *p_libvlc_global = vlc_global();
247 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
248 var_Get( p_libvlc_global, "libvlc", &lockval );
249 vlc_mutex_lock( lockval.p_address );
250 if( p_libvlc_global->p_module_bank->b_main )
252 vlc_mutex_unlock( lockval.p_address );
253 var_Destroy( p_libvlc_global, "libvlc" );
256 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
257 vlc_mutex_unlock( lockval.p_address );
258 var_Destroy( p_libvlc_global, "libvlc" );
260 AllocateBuiltinModule( p_this, vlc_entry__main );
263 /*****************************************************************************
264 * module_LoadBuiltins: load all modules which we built with.
265 *****************************************************************************
266 * This function fills the module bank structure with the builtin modules.
267 *****************************************************************************/
268 void __module_LoadBuiltins( vlc_object_t * p_this )
271 libvlc_global_data_t *p_libvlc_global = vlc_global();
273 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
274 var_Get( p_libvlc_global, "libvlc", &lockval );
275 vlc_mutex_lock( lockval.p_address );
276 if( p_libvlc_global->p_module_bank->b_builtins )
278 vlc_mutex_unlock( lockval.p_address );
279 var_Destroy( p_libvlc_global, "libvlc" );
282 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
283 vlc_mutex_unlock( lockval.p_address );
284 var_Destroy( p_libvlc_global, "libvlc" );
286 msg_Dbg( p_this, "checking builtin modules" );
287 ALLOCATE_ALL_BUILTINS();
290 /*****************************************************************************
291 * module_LoadPlugins: load all plugin modules we can find.
292 *****************************************************************************
293 * This function fills the module bank structure with the plugin modules.
294 *****************************************************************************/
295 void __module_LoadPlugins( vlc_object_t * p_this )
297 #ifdef HAVE_DYNAMIC_PLUGINS
299 libvlc_global_data_t *p_libvlc_global = vlc_global();
301 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
302 var_Get( p_libvlc_global, "libvlc", &lockval );
303 vlc_mutex_lock( lockval.p_address );
304 if( p_libvlc_global->p_module_bank->b_plugins )
306 vlc_mutex_unlock( lockval.p_address );
307 var_Destroy( p_libvlc_global, "libvlc" );
310 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
311 vlc_mutex_unlock( lockval.p_address );
312 var_Destroy( p_libvlc_global, "libvlc" );
314 msg_Dbg( p_this, "checking plugin modules" );
316 if( config_GetInt( p_this, "plugins-cache" ) )
317 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
319 if( p_libvlc_global->p_module_bank->b_cache ||
320 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
322 AllocateAllPlugins( p_this );
326 /*****************************************************************************
327 * module_IsCapable: checks whether a module implements a capability.
328 *****************************************************************************/
329 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
331 return !strcmp( m->psz_capability, cap );
334 /*****************************************************************************
335 * module_IsSubModule: checks whether a module is a sub-module.
336 *****************************************************************************/
337 vlc_bool_t module_IsSubModule( const module_t *m )
339 return m->b_submodule;
342 /*****************************************************************************
343 * module_GetObjName: internal name of a module.
344 *****************************************************************************/
345 const char *module_GetObjName( const module_t *m )
347 return m->psz_object_name;
350 /*****************************************************************************
351 * module_GetName: human-friendly name of a module.
352 *****************************************************************************/
353 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
355 if( long_name && ( m->psz_longname != NULL) )
356 return m->psz_longname;
358 return m->psz_shortname ?: m->psz_object_name;
361 const char *module_GetHelp( const module_t *m )
366 /*****************************************************************************
367 * module_Need: return the best module function, given a capability list.
368 *****************************************************************************
369 * This function returns the module that best fits the asked capabilities.
370 *****************************************************************************/
371 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
372 const char *psz_name, vlc_bool_t b_strict )
374 typedef struct module_list_t module_list_t;
381 module_list_t *p_next;
384 module_list_t *p_list, *p_first, *p_tmp;
387 int i_which_module, i_index = 0;
392 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
393 vlc_bool_t b_force_backup = p_this->b_force;
396 /* Deal with variables */
397 if( psz_name && psz_name[0] == '$' )
400 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
401 var_Get( p_this, psz_name + 1, &val );
402 psz_var = val.psz_string;
406 /* Count how many different shortcuts were asked for */
407 if( psz_name && *psz_name )
409 char *psz_parser, *psz_last_shortcut;
411 /* If the user wants none, give him none. */
412 if( !strcmp( psz_name, "none" ) )
419 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
421 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
423 if( *psz_parser == ',' )
427 psz_last_shortcut = psz_parser + 1;
431 /* Check if the user wants to override the "strict" mode */
432 if( psz_last_shortcut )
434 if( !strcmp(psz_last_shortcut, "none") )
439 else if( !strcmp(psz_last_shortcut, "any") )
441 b_strict = VLC_FALSE;
447 /* Sort the modules and test them */
448 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
449 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
451 unsigned i_cpu = vlc_CPU();
453 /* Parse the module list for capabilities and probe each of them */
454 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
456 int i_shortcut_bonus = 0;
458 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
460 /* Test that this module can do what we need */
461 if( !module_IsCapable( p_module, psz_capability ) )
463 /* Don't recurse through the sub-modules because vlc_list_find()
464 * will list them anyway. */
468 /* Test if we have the required CPU */
469 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
474 /* If we required a shortcut, check this plugin provides it. */
475 if( i_shortcuts > 0 )
478 const char *psz_name = psz_shortcuts;
480 /* Let's drop modules with a <= 0 score (unless they are
481 * explicitly requested) */
482 b_trash = p_module->i_score <= 0;
484 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
486 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
489 if( ( c = strchr( psz_name, '@' ) )
490 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
492 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
497 i_shortcut_bonus = i_short * 10000;
502 /* Go to the next shortcut... This is so lame! */
503 psz_name += strlen( psz_name ) + 1;
506 /* If we are in "strict" mode and we couldn't
507 * find the module in the list of provided shortcuts,
508 * then kick the bastard out of here!!! */
512 /* If we didn't require a shortcut, trash <= 0 scored plugins */
513 else if( p_module->i_score <= 0 )
520 /* Store this new module */
521 p_list[ i_index ].p_module = p_module;
522 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
523 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
525 /* Add it to the modules-to-probe list */
528 p_list[ 0 ].p_next = NULL;
533 /* Ok, so at school you learned that quicksort is quick, and
534 * bubble sort sucks raw eggs. But that's when dealing with
535 * thousands of items. Here we have barely 50. */
536 module_list_t *p_newlist = p_first;
538 if( p_first->i_score < p_list[ i_index ].i_score )
540 p_list[ i_index ].p_next = p_first;
541 p_first = &p_list[ i_index ];
545 while( p_newlist->p_next != NULL &&
546 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
548 p_newlist = p_newlist->p_next;
551 p_list[ i_index ].p_next = p_newlist->p_next;
552 p_newlist->p_next = &p_list[ i_index ];
559 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
560 i_index, i_index == 1 ? "" : "s" );
562 /* Lock all candidate modules */
564 while( p_tmp != NULL )
566 vlc_object_yield( p_tmp->p_module );
567 p_tmp = p_tmp->p_next;
570 /* We can release the list, interesting modules were yielded */
571 vlc_list_release( p_all );
573 /* Parse the linked list and use the first successful module */
575 while( p_tmp != NULL )
577 #ifdef HAVE_DYNAMIC_PLUGINS
578 /* Make sure the module is loaded in mem */
579 module_t *p_module = p_tmp->p_module;
580 if( p_module->b_submodule )
581 p_module = (module_t *)p_module->p_parent;
583 if( !p_module->b_builtin && !p_module->b_loaded )
585 module_t *p_new_module =
586 AllocatePlugin( p_this, p_module->psz_filename );
589 CacheMerge( p_this, p_module, p_new_module );
590 vlc_object_attach( p_new_module, p_module );
591 DeleteModule( p_new_module, VLC_TRUE );
596 p_this->b_force = p_tmp->b_force;
597 if( p_tmp->p_module->pf_activate
598 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
603 vlc_object_release( p_tmp->p_module );
604 p_tmp = p_tmp->p_next;
607 /* Store the locked module value */
610 p_module = p_tmp->p_module;
611 p_tmp = p_tmp->p_next;
618 /* Unlock the remaining modules */
619 while( p_tmp != NULL )
621 vlc_object_release( p_tmp->p_module );
622 p_tmp = p_tmp->p_next;
626 p_this->b_force = b_force_backup;
628 if( p_module != NULL )
630 msg_Dbg( p_this, "using %s module \"%s\"",
631 psz_capability, p_module->psz_object_name );
633 else if( p_first == NULL )
635 if( !strcmp( psz_capability, "access_demux" ) )
637 msg_Warn( p_this, "no %s module matched \"%s\"",
638 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
642 msg_Err( p_this, "no %s module matched \"%s\"",
643 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
645 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
646 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
649 else if( psz_name != NULL && *psz_name )
651 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
652 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
655 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
657 if( p_module && !p_this->psz_object_name )
659 /* This assumes that p_this is the object which will be using the
660 * module. That's not always the case ... but it is in most cases.
663 p_this->psz_object_name = strdup( psz_alias );
665 p_this->psz_object_name = strdup( p_module->psz_object_name );
668 free( psz_shortcuts );
671 /* Don't forget that the module is still locked */
675 /*****************************************************************************
676 * module_Unneed: decrease the usage count of a module.
677 *****************************************************************************
678 * This function must be called by the thread that called module_Need, to
679 * decrease the reference count and allow for hiding of modules.
680 *****************************************************************************/
681 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
683 /* Use the close method */
684 if( p_module->pf_deactivate )
686 p_module->pf_deactivate( p_this );
689 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
691 vlc_object_release( p_module );
696 /*****************************************************************************
697 * module_Find: get a pointer to a module_t given it's name.
698 *****************************************************************************/
699 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
703 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
704 for( i = 0 ; i < p_list->i_count; i++)
706 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
707 const char *psz_module_name = p_module->psz_object_name;
708 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
710 /* We can release the list, and return yes */
711 vlc_object_yield( p_module );
712 vlc_list_release( p_list );
716 vlc_list_release( p_list );
721 /*****************************************************************************
722 * module_Put: release a module_t pointer from module_Find().
723 *****************************************************************************/
724 void module_Put ( module_t *module )
726 vlc_object_release ( module );
730 /*****************************************************************************
731 * module_Exists: tell if a module exists.
732 *****************************************************************************
733 * This function is a boolean function that tells if a module exist or not.
734 *****************************************************************************/
735 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
737 module_t *p_module = __module_Find( p_this, psz_name );
740 module_Put( p_module );
749 /*****************************************************************************
750 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
751 * names of the modules that have a certain capability.
752 * Free after uses both the string and the table.
753 *****************************************************************************/
754 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
755 const char * psz_capability,
756 char ***pppsz_longname )
762 /* Do it in two passes */
763 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
764 for( i = 0 ; i < p_list->i_count; i++)
766 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
767 const char *psz_module_capability = p_module->psz_capability;
768 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
771 psz_ret = malloc( sizeof(char*) * (count+1) );
773 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
775 for( i = 0 ; i < p_list->i_count; i++)
777 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
778 const char *psz_module_capability = p_module->psz_capability;
779 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
781 int k = -1; /* hack to handle submodules properly */
782 if( p_module->b_submodule )
784 while( p_module->pp_shortcuts[++k] != NULL );
787 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
788 :p_module->psz_object_name );
790 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
794 psz_ret[count] = NULL;
796 vlc_list_release( p_list );
802 module_config_t *module_GetConfig (const module_t *module, unsigned *restrict psize)
804 unsigned size = module->confsize;
805 module_config_t *config;
807 assert (psize != NULL);
810 config = malloc (size * sizeof (*config));
812 memcpy (config, module->p_config, size * sizeof (*config));
817 void module_PutConfig (module_config_t *config)
819 if(config) free (config);
822 /*****************************************************************************
823 * Following functions are local.
824 *****************************************************************************/
826 /*****************************************************************************
827 * AllocateAllPlugins: load all plugin modules we can find.
828 *****************************************************************************/
829 #ifdef HAVE_DYNAMIC_PLUGINS
830 static void AllocateAllPlugins( vlc_object_t *p_this )
832 /* Yes, there are two NULLs because we replace one with "plugin-path". */
833 #if defined( WIN32 ) || defined( UNDER_CE )
834 const char *path[] = { "modules", "", "plugins", NULL, NULL };
836 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
839 const char *const *ppsz_path;
841 /* If the user provided a plugin path, we add it to the list */
842 char *userpath = config_GetPsz( p_this, "plugin-path" );
843 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
845 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
849 if (!**ppsz_path) continue;
851 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
853 /* Handle relative as well as absolute paths */
855 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
856 (*ppsz_path)[1] != ':' )
858 if( (*ppsz_path)[0] != '/' )
861 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
862 vlc_global()->psz_vlcpath, *ppsz_path) )
867 psz_fullpath = strdup( *ppsz_path );
869 if( psz_fullpath == NULL )
872 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
874 /* Don't go deeper than 5 subdirectories */
875 AllocatePluginDir( p_this, psz_fullpath, 5 );
877 free( psz_fullpath );
880 /* Free plugin-path */
884 /*****************************************************************************
885 * AllocatePluginDir: recursively parse a directory to look for plugins
886 *****************************************************************************/
887 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
890 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
896 #if defined( UNDER_CE ) || defined( _MSC_VER )
898 wchar_t psz_wpath[MAX_PATH + 256];
899 wchar_t psz_wdir[MAX_PATH];
901 char psz_path[MAX_PATH + 256];
902 WIN32_FIND_DATA finddata;
908 struct dirent * file;
912 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
917 #if defined( UNDER_CE ) || defined( _MSC_VER )
919 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
921 rc = GetFileAttributes( psz_wdir );
922 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
924 /* Parse all files in the directory */
925 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
927 rc = GetFileAttributes( psz_dir );
928 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
931 /* Parse all files in the directory */
932 sprintf( psz_path, "%s\\*", psz_dir );
935 handle = FindFirstFile( psz_wpath, &finddata );
937 handle = FindFirstFile( psz_path, &finddata );
939 if( handle == INVALID_HANDLE_VALUE )
941 /* Empty directory */
945 /* Parse the directory and try to load all files it contains. */
949 unsigned int i_len = wcslen( finddata.cFileName );
950 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
951 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
953 unsigned int i_len = strlen( finddata.cFileName );
954 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
958 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
959 || !strcmp( finddata.cFileName, ".." ) )
961 if( !FindNextFile( handle, &finddata ) ) break;
966 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
968 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
971 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
973 else if( i_len > strlen( LIBEXT )
974 /* We only load files ending with LIBEXT */
975 && !strncasecmp( psz_path + strlen( psz_path)
977 LIBEXT, strlen( LIBEXT ) ) )
979 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
980 int64_t i_time = 0, i_size = 0;
983 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
986 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
990 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
992 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
993 i_size = attrbuf.nFileSizeHigh;
995 i_size |= attrbuf.nFileSizeLow;
999 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1002 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1004 /* Close the directory */
1005 FindClose( handle );
1008 dir = opendir( psz_dir );
1014 i_dirlen = strlen( psz_dir );
1016 /* Parse the directory and try to load all files it contains. */
1017 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1019 struct stat statbuf;
1023 /* Skip ".", ".." */
1024 if( !*file->d_name || !strcmp( file->d_name, "." )
1025 || !strcmp( file->d_name, ".." ) )
1030 i_len = strlen( file->d_name );
1031 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1032 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1034 i_stat = stat( psz_file, &statbuf );
1035 if( !i_stat && statbuf.st_mode & S_IFDIR )
1037 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1039 else if( i_len > strlen( LIBEXT )
1040 /* We only load files ending with LIBEXT */
1041 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1042 LIBEXT, strlen( LIBEXT ) ) )
1044 int64_t i_time = 0, i_size = 0;
1048 i_time = statbuf.st_mtime;
1049 i_size = statbuf.st_size;
1052 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1058 /* Close the directory */
1064 /*****************************************************************************
1065 * AllocatePluginFile: load a module into memory and initialize it.
1066 *****************************************************************************
1067 * This function loads a dynamically loadable module and allocates a structure
1068 * for its information data. The module can then be handled by module_Need
1069 * and module_Unneed. It can be removed by DeleteModule.
1070 *****************************************************************************/
1071 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1072 int64_t i_file_time, int64_t i_file_size )
1074 module_t * p_module = NULL;
1075 module_cache_t *p_cache_entry = NULL;
1078 * Check our plugins cache first then load plugin if needed
1081 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1083 if( !p_cache_entry )
1085 p_module = AllocatePlugin( p_this, psz_file );
1089 /* If junk dll, don't try to load it */
1090 if( p_cache_entry->b_junk )
1096 module_config_t *p_item = NULL, *p_end = NULL;
1098 p_module = p_cache_entry->p_module;
1099 p_module->b_loaded = VLC_FALSE;
1101 /* For now we force loading if the module's config contains
1102 * callbacks or actions.
1103 * Could be optimized by adding an API call.*/
1104 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1105 p_item < p_end; p_item++ )
1107 if( p_item->pf_callback || p_item->i_action )
1109 p_module = AllocatePlugin( p_this, psz_file );
1113 if( p_module == p_cache_entry->p_module )
1114 p_cache_entry->b_used = VLC_TRUE;
1120 libvlc_global_data_t *p_libvlc_global = vlc_global();
1122 /* Everything worked fine !
1123 * The module is ready to be added to the list. */
1124 p_module->b_builtin = VLC_FALSE;
1126 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1127 p_module->psz_object_name, p_module->psz_longname ); */
1129 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1131 if( !p_libvlc_global->p_module_bank->b_cache )
1134 #define p_bank p_libvlc_global->p_module_bank
1135 /* Add entry to cache */
1137 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1138 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1139 if( !p_bank->pp_cache[p_bank->i_cache] )
1141 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1142 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1143 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1144 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1145 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1146 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1150 return p_module ? 0 : -1;
1153 /*****************************************************************************
1154 * AllocatePlugin: load a module into memory and initialize it.
1155 *****************************************************************************
1156 * This function loads a dynamically loadable module and allocates a structure
1157 * for its information data. The module can then be handled by module_Need
1158 * and module_Unneed. It can be removed by DeleteModule.
1159 *****************************************************************************/
1160 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1162 module_t * p_module = NULL;
1163 module_handle_t handle;
1165 if( module_Load( p_this, psz_file, &handle ) )
1168 /* Now that we have successfully loaded the module, we can
1169 * allocate a structure for it */
1170 p_module = vlc_module_create( p_this );
1171 if( p_module == NULL )
1173 msg_Err( p_this, "out of memory" );
1174 module_Unload( handle );
1178 /* We need to fill these since they may be needed by module_Call() */
1179 p_module->psz_filename = psz_file;
1180 p_module->handle = handle;
1181 p_module->b_loaded = VLC_TRUE;
1183 /* Initialize the module: fill p_module, default config */
1184 if( module_Call( p_module ) != 0 )
1186 /* We couldn't call module_init() */
1187 vlc_object_destroy( p_module );
1188 module_Unload( handle );
1192 DupModule( p_module );
1193 p_module->psz_filename = strdup( p_module->psz_filename );
1195 /* Everything worked fine ! The module is ready to be added to the list. */
1196 p_module->b_builtin = VLC_FALSE;
1201 /*****************************************************************************
1202 * DupModule: make a plugin module standalone.
1203 *****************************************************************************
1204 * This function duplicates all strings in the module, so that the dynamic
1205 * object can be unloaded. It acts recursively on submodules.
1206 *****************************************************************************/
1207 static void DupModule( module_t *p_module )
1209 const char **pp_shortcut;
1212 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1214 *pp_shortcut = strdup( *pp_shortcut );
1217 /* We strdup() these entries so that they are still valid when the
1218 * module is unloaded. */
1219 p_module->psz_object_name = strdup( p_module->psz_object_name );
1220 p_module->psz_capability = strdup( p_module->psz_capability );
1221 p_module->psz_shortname = p_module->psz_shortname ?
1222 strdup( p_module->psz_shortname ) : NULL;
1223 p_module->psz_longname = strdup( p_module->psz_longname );
1224 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1227 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1229 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1233 /*****************************************************************************
1234 * UndupModule: free a duplicated module.
1235 *****************************************************************************
1236 * This function frees the allocations done in DupModule().
1237 *****************************************************************************/
1238 static void UndupModule( module_t *p_module )
1240 const char **pp_shortcut;
1243 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1245 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1248 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1250 free( (void*)*pp_shortcut );
1253 free( (void*)p_module->psz_object_name );
1254 free( p_module->psz_capability );
1255 free( (void*)p_module->psz_shortname );
1256 free( (void*)p_module->psz_longname );
1257 free( (void*)p_module->psz_help );
1260 #endif /* HAVE_DYNAMIC_PLUGINS */
1262 /*****************************************************************************
1263 * AllocateBuiltinModule: initialize a builtin module.
1264 *****************************************************************************
1265 * This function registers a builtin module and allocates a structure
1266 * for its information data. The module can then be handled by module_Need
1267 * and module_Unneed. It can be removed by DeleteModule.
1268 *****************************************************************************/
1269 static int AllocateBuiltinModule( vlc_object_t * p_this,
1270 int ( *pf_entry ) ( module_t * ) )
1272 module_t * p_module;
1274 /* Now that we have successfully loaded the module, we can
1275 * allocate a structure for it */
1276 p_module = vlc_module_create( p_this );
1277 if( p_module == NULL )
1279 msg_Err( p_this, "out of memory" );
1283 /* Initialize the module : fill p_module->psz_object_name, etc. */
1284 if( pf_entry( p_module ) != 0 )
1286 /* With a well-written module we shouldn't have to print an
1287 * additional error message here, but just make sure. */
1288 msg_Err( p_this, "failed calling entry point in builtin module" );
1289 vlc_object_destroy( p_module );
1293 /* Everything worked fine ! The module is ready to be added to the list. */
1294 p_module->b_builtin = VLC_TRUE;
1296 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1297 p_module->psz_object_name, p_module->psz_longname ); */
1299 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1304 /*****************************************************************************
1305 * DeleteModule: delete a module and its structure.
1306 *****************************************************************************
1307 * This function can only be called if the module isn't being used.
1308 *****************************************************************************/
1309 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1311 if( !p_module ) return VLC_EGENERIC;
1313 vlc_object_detach( p_module );
1315 /* We free the structures that we strdup()ed in Allocate*Module(). */
1316 #ifdef HAVE_DYNAMIC_PLUGINS
1317 if( !p_module->b_builtin )
1319 if( p_module->b_loaded && p_module->b_unloadable )
1321 module_Unload( p_module->handle );
1323 UndupModule( p_module );
1324 free( p_module->psz_filename );
1328 /* Free and detach the object's children */
1329 while( p_module->i_children )
1331 vlc_object_t *p_this = p_module->pp_children[0];
1332 vlc_object_detach( p_this );
1333 vlc_object_destroy( p_this );
1336 config_Free( p_module );
1337 vlc_object_destroy( p_module );