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_GetObjName: internal name of a module.
336 *****************************************************************************/
337 const char *module_GetObjName( const module_t *m )
339 return m->psz_object_name;
342 /*****************************************************************************
343 * module_GetName: human-friendly name of a module.
344 *****************************************************************************/
345 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
347 if( long_name && ( m->psz_longname != NULL) )
348 return m->psz_longname;
350 return m->psz_shortname ?: m->psz_object_name;
353 const char *module_GetHelp( const module_t *m )
358 /*****************************************************************************
359 * module_Need: return the best module function, given a capability list.
360 *****************************************************************************
361 * This function returns the module that best fits the asked capabilities.
362 *****************************************************************************/
363 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
364 const char *psz_name, vlc_bool_t b_strict )
366 typedef struct module_list_t module_list_t;
373 module_list_t *p_next;
376 module_list_t *p_list, *p_first, *p_tmp;
379 int i_which_module, i_index = 0;
384 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
385 vlc_bool_t b_force_backup = p_this->b_force;
388 /* Deal with variables */
389 if( psz_name && psz_name[0] == '$' )
392 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
393 var_Get( p_this, psz_name + 1, &val );
394 psz_var = val.psz_string;
398 /* Count how many different shortcuts were asked for */
399 if( psz_name && *psz_name )
401 char *psz_parser, *psz_last_shortcut;
403 /* If the user wants none, give him none. */
404 if( !strcmp( psz_name, "none" ) )
411 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
413 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
415 if( *psz_parser == ',' )
419 psz_last_shortcut = psz_parser + 1;
423 /* Check if the user wants to override the "strict" mode */
424 if( psz_last_shortcut )
426 if( !strcmp(psz_last_shortcut, "none") )
431 else if( !strcmp(psz_last_shortcut, "any") )
433 b_strict = VLC_FALSE;
439 /* Sort the modules and test them */
440 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
441 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
443 unsigned i_cpu = vlc_CPU();
445 /* Parse the module list for capabilities and probe each of them */
446 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
448 int i_shortcut_bonus = 0;
450 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
452 /* Test that this module can do what we need */
453 if( !module_IsCapable( p_module, psz_capability ) )
455 /* Don't recurse through the sub-modules because vlc_list_find()
456 * will list them anyway. */
460 /* Test if we have the required CPU */
461 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
466 /* If we required a shortcut, check this plugin provides it. */
467 if( i_shortcuts > 0 )
470 const char *psz_name = psz_shortcuts;
472 /* Let's drop modules with a <= 0 score (unless they are
473 * explicitly requested) */
474 b_trash = p_module->i_score <= 0;
476 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
478 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
481 if( ( c = strchr( psz_name, '@' ) )
482 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
484 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
489 i_shortcut_bonus = i_short * 10000;
494 /* Go to the next shortcut... This is so lame! */
495 psz_name += strlen( psz_name ) + 1;
498 /* If we are in "strict" mode and we couldn't
499 * find the module in the list of provided shortcuts,
500 * then kick the bastard out of here!!! */
504 /* If we didn't require a shortcut, trash <= 0 scored plugins */
505 else if( p_module->i_score <= 0 )
512 /* Store this new module */
513 p_list[ i_index ].p_module = p_module;
514 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
515 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
517 /* Add it to the modules-to-probe list */
520 p_list[ 0 ].p_next = NULL;
525 /* Ok, so at school you learned that quicksort is quick, and
526 * bubble sort sucks raw eggs. But that's when dealing with
527 * thousands of items. Here we have barely 50. */
528 module_list_t *p_newlist = p_first;
530 if( p_first->i_score < p_list[ i_index ].i_score )
532 p_list[ i_index ].p_next = p_first;
533 p_first = &p_list[ i_index ];
537 while( p_newlist->p_next != NULL &&
538 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
540 p_newlist = p_newlist->p_next;
543 p_list[ i_index ].p_next = p_newlist->p_next;
544 p_newlist->p_next = &p_list[ i_index ];
551 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
552 i_index, i_index == 1 ? "" : "s" );
554 /* Lock all candidate modules */
556 while( p_tmp != NULL )
558 vlc_object_yield( p_tmp->p_module );
559 p_tmp = p_tmp->p_next;
562 /* We can release the list, interesting modules were yielded */
563 vlc_list_release( p_all );
565 /* Parse the linked list and use the first successful module */
567 while( p_tmp != NULL )
569 #ifdef HAVE_DYNAMIC_PLUGINS
570 /* Make sure the module is loaded in mem */
571 module_t *p_module = p_tmp->p_module;
572 if( p_module->b_submodule )
573 p_module = (module_t *)p_module->p_parent;
575 if( !p_module->b_builtin && !p_module->b_loaded )
577 module_t *p_new_module =
578 AllocatePlugin( p_this, p_module->psz_filename );
581 CacheMerge( p_this, p_module, p_new_module );
582 vlc_object_attach( p_new_module, p_module );
583 DeleteModule( p_new_module, VLC_TRUE );
588 p_this->b_force = p_tmp->b_force;
589 if( p_tmp->p_module->pf_activate
590 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
595 vlc_object_release( p_tmp->p_module );
596 p_tmp = p_tmp->p_next;
599 /* Store the locked module value */
602 p_module = p_tmp->p_module;
603 p_tmp = p_tmp->p_next;
610 /* Unlock the remaining modules */
611 while( p_tmp != NULL )
613 vlc_object_release( p_tmp->p_module );
614 p_tmp = p_tmp->p_next;
618 p_this->b_force = b_force_backup;
620 if( p_module != NULL )
622 msg_Dbg( p_this, "using %s module \"%s\"",
623 psz_capability, p_module->psz_object_name );
625 else if( p_first == NULL )
627 if( !strcmp( psz_capability, "access_demux" ) )
629 msg_Warn( p_this, "no %s module matched \"%s\"",
630 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
634 msg_Err( p_this, "no %s module matched \"%s\"",
635 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
637 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
638 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
641 else if( psz_name != NULL && *psz_name )
643 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
644 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
647 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
649 if( p_module && !p_this->psz_object_name )
651 /* This assumes that p_this is the object which will be using the
652 * module. That's not always the case ... but it is in most cases.
655 p_this->psz_object_name = strdup( psz_alias );
657 p_this->psz_object_name = strdup( p_module->psz_object_name );
660 free( psz_shortcuts );
663 /* Don't forget that the module is still locked */
667 /*****************************************************************************
668 * module_Unneed: decrease the usage count of a module.
669 *****************************************************************************
670 * This function must be called by the thread that called module_Need, to
671 * decrease the reference count and allow for hiding of modules.
672 *****************************************************************************/
673 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
675 /* Use the close method */
676 if( p_module->pf_deactivate )
678 p_module->pf_deactivate( p_this );
681 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
683 vlc_object_release( p_module );
688 /*****************************************************************************
689 * module_FindName: get a pointer to a module_t given it's name.
690 *****************************************************************************/
691 module_t *__module_FindName( vlc_object_t *p_this, const char * psz_name )
695 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
696 for( i = 0 ; i < p_list->i_count; i++)
698 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
699 const char *psz_module_name = p_module->psz_object_name;
700 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
702 /* We can release the list, and return yes */
703 vlc_object_yield( p_module );
704 vlc_list_release( p_list );
708 vlc_list_release( p_list );
712 /*****************************************************************************
713 * module_Exists: tell if a module exists.
714 *****************************************************************************
715 * This function is a boolean function that tells if a module exist or not.
716 *****************************************************************************/
717 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
719 module_t *p_module = __module_FindName( p_this, psz_name );
722 vlc_object_release( p_module );
731 /*****************************************************************************
732 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
733 * names of the modules that have a certain capability.
734 * Free after uses both the string and the table.
735 *****************************************************************************/
736 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
737 const char * psz_capability,
738 char ***pppsz_longname )
744 /* Do it in two passes */
745 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
746 for( i = 0 ; i < p_list->i_count; i++)
748 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
749 const char *psz_module_capability = p_module->psz_capability;
750 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
753 psz_ret = malloc( sizeof(char*) * (count+1) );
755 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
757 for( i = 0 ; i < p_list->i_count; i++)
759 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
760 const char *psz_module_capability = p_module->psz_capability;
761 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
763 int k = -1; /* hack to handle submodules properly */
764 if( p_module->b_submodule )
766 while( p_module->pp_shortcuts[++k] != NULL );
769 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
770 :p_module->psz_object_name );
772 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
776 psz_ret[count] = NULL;
778 vlc_list_release( p_list );
784 /*****************************************************************************
785 * Following functions are local.
786 *****************************************************************************/
788 /*****************************************************************************
789 * AllocateAllPlugins: load all plugin modules we can find.
790 *****************************************************************************/
791 #ifdef HAVE_DYNAMIC_PLUGINS
792 static void AllocateAllPlugins( vlc_object_t *p_this )
794 /* Yes, there are two NULLs because we replace one with "plugin-path". */
795 #if defined( WIN32 ) || defined( UNDER_CE )
796 const char *path[] = { "modules", "", "plugins", NULL, NULL };
798 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
801 const char *const *ppsz_path;
803 /* If the user provided a plugin path, we add it to the list */
804 char *userpath = config_GetPsz( p_this, "plugin-path" );
805 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
807 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
811 if (!**ppsz_path) continue;
813 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
815 /* Handle relative as well as absolute paths */
817 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
818 (*ppsz_path)[1] != ':' )
820 if( (*ppsz_path)[0] != '/' )
823 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
824 vlc_global()->psz_vlcpath, *ppsz_path) )
829 psz_fullpath = strdup( *ppsz_path );
831 if( psz_fullpath == NULL )
834 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
836 /* Don't go deeper than 5 subdirectories */
837 AllocatePluginDir( p_this, psz_fullpath, 5 );
839 free( psz_fullpath );
842 /* Free plugin-path */
846 /*****************************************************************************
847 * AllocatePluginDir: recursively parse a directory to look for plugins
848 *****************************************************************************/
849 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
852 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
858 #if defined( UNDER_CE ) || defined( _MSC_VER )
860 wchar_t psz_wpath[MAX_PATH + 256];
861 wchar_t psz_wdir[MAX_PATH];
863 char psz_path[MAX_PATH + 256];
864 WIN32_FIND_DATA finddata;
870 struct dirent * file;
874 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
879 #if defined( UNDER_CE ) || defined( _MSC_VER )
881 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
883 rc = GetFileAttributes( psz_wdir );
884 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
886 /* Parse all files in the directory */
887 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
889 rc = GetFileAttributes( psz_dir );
890 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
893 /* Parse all files in the directory */
894 sprintf( psz_path, "%s\\*", psz_dir );
897 handle = FindFirstFile( psz_wpath, &finddata );
899 handle = FindFirstFile( psz_path, &finddata );
901 if( handle == INVALID_HANDLE_VALUE )
903 /* Empty directory */
907 /* Parse the directory and try to load all files it contains. */
911 unsigned int i_len = wcslen( finddata.cFileName );
912 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
913 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
915 unsigned int i_len = strlen( finddata.cFileName );
916 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
920 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
921 || !strcmp( finddata.cFileName, ".." ) )
923 if( !FindNextFile( handle, &finddata ) ) break;
928 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
930 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
933 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
935 else if( i_len > strlen( LIBEXT )
936 /* We only load files ending with LIBEXT */
937 && !strncasecmp( psz_path + strlen( psz_path)
939 LIBEXT, strlen( LIBEXT ) ) )
941 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
942 int64_t i_time = 0, i_size = 0;
945 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
948 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
952 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
954 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
955 i_size = attrbuf.nFileSizeHigh;
957 i_size |= attrbuf.nFileSizeLow;
961 AllocatePluginFile( p_this, psz_file, i_time, i_size );
964 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
966 /* Close the directory */
970 dir = opendir( psz_dir );
976 i_dirlen = strlen( psz_dir );
978 /* Parse the directory and try to load all files it contains. */
979 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
986 if( !*file->d_name || !strcmp( file->d_name, "." )
987 || !strcmp( file->d_name, ".." ) )
992 i_len = strlen( file->d_name );
993 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
995 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
997 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
1000 i_stat = stat( psz_file, &statbuf );
1001 if( !i_stat && statbuf.st_mode & S_IFDIR )
1003 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1005 else if( i_len > strlen( LIBEXT )
1006 /* We only load files ending with LIBEXT */
1007 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1008 LIBEXT, strlen( LIBEXT ) ) )
1010 int64_t i_time = 0, i_size = 0;
1014 i_time = statbuf.st_mtime;
1015 i_size = statbuf.st_size;
1018 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1024 /* Close the directory */
1030 /*****************************************************************************
1031 * AllocatePluginFile: load a module into memory and initialize it.
1032 *****************************************************************************
1033 * This function loads a dynamically loadable module and allocates a structure
1034 * for its information data. The module can then be handled by module_Need
1035 * and module_Unneed. It can be removed by DeleteModule.
1036 *****************************************************************************/
1037 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1038 int64_t i_file_time, int64_t i_file_size )
1040 module_t * p_module = NULL;
1041 module_cache_t *p_cache_entry = NULL;
1044 * Check our plugins cache first then load plugin if needed
1047 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1049 if( !p_cache_entry )
1051 p_module = AllocatePlugin( p_this, psz_file );
1055 /* If junk dll, don't try to load it */
1056 if( p_cache_entry->b_junk )
1062 module_config_t *p_item = NULL, *p_end = NULL;
1064 p_module = p_cache_entry->p_module;
1065 p_module->b_loaded = VLC_FALSE;
1067 /* For now we force loading if the module's config contains
1068 * callbacks or actions.
1069 * Could be optimized by adding an API call.*/
1070 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1071 p_item < p_end; p_item++ )
1073 if( p_item->pf_callback || p_item->i_action )
1075 p_module = AllocatePlugin( p_this, psz_file );
1079 if( p_module == p_cache_entry->p_module )
1080 p_cache_entry->b_used = VLC_TRUE;
1086 libvlc_global_data_t *p_libvlc_global = vlc_global();
1088 /* Everything worked fine !
1089 * The module is ready to be added to the list. */
1090 p_module->b_builtin = VLC_FALSE;
1092 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1093 p_module->psz_object_name, p_module->psz_longname ); */
1095 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1097 if( !p_libvlc_global->p_module_bank->b_cache )
1100 #define p_bank p_libvlc_global->p_module_bank
1101 /* Add entry to cache */
1103 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1104 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1105 if( !p_bank->pp_cache[p_bank->i_cache] )
1107 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1108 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1109 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1110 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1111 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1112 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1116 return p_module ? 0 : -1;
1119 /*****************************************************************************
1120 * AllocatePlugin: load a module into memory and initialize it.
1121 *****************************************************************************
1122 * This function loads a dynamically loadable module and allocates a structure
1123 * for its information data. The module can then be handled by module_Need
1124 * and module_Unneed. It can be removed by DeleteModule.
1125 *****************************************************************************/
1126 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1128 module_t * p_module = NULL;
1129 module_handle_t handle;
1131 if( module_Load( p_this, psz_file, &handle ) )
1134 /* Now that we have successfully loaded the module, we can
1135 * allocate a structure for it */
1136 p_module = vlc_module_create( p_this );
1137 if( p_module == NULL )
1139 msg_Err( p_this, "out of memory" );
1140 module_Unload( handle );
1144 /* We need to fill these since they may be needed by module_Call() */
1145 p_module->psz_filename = psz_file;
1146 p_module->handle = handle;
1147 p_module->b_loaded = VLC_TRUE;
1149 /* Initialize the module: fill p_module, default config */
1150 if( module_Call( p_module ) != 0 )
1152 /* We couldn't call module_init() */
1153 vlc_object_destroy( p_module );
1154 module_Unload( handle );
1158 DupModule( p_module );
1159 p_module->psz_filename = strdup( p_module->psz_filename );
1161 /* Everything worked fine ! The module is ready to be added to the list. */
1162 p_module->b_builtin = VLC_FALSE;
1167 /*****************************************************************************
1168 * DupModule: make a plugin module standalone.
1169 *****************************************************************************
1170 * This function duplicates all strings in the module, so that the dynamic
1171 * object can be unloaded. It acts recursively on submodules.
1172 *****************************************************************************/
1173 static void DupModule( module_t *p_module )
1175 const char **pp_shortcut;
1178 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1180 *pp_shortcut = strdup( *pp_shortcut );
1183 /* We strdup() these entries so that they are still valid when the
1184 * module is unloaded. */
1185 p_module->psz_object_name = strdup( p_module->psz_object_name );
1186 p_module->psz_capability = strdup( p_module->psz_capability );
1187 p_module->psz_shortname = p_module->psz_shortname ?
1188 strdup( p_module->psz_shortname ) : NULL;
1189 p_module->psz_longname = strdup( p_module->psz_longname );
1190 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1193 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1195 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1199 /*****************************************************************************
1200 * UndupModule: free a duplicated module.
1201 *****************************************************************************
1202 * This function frees the allocations done in DupModule().
1203 *****************************************************************************/
1204 static void UndupModule( module_t *p_module )
1206 const char **pp_shortcut;
1209 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1211 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1214 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1216 free( (void*)*pp_shortcut );
1219 free( (void*)p_module->psz_object_name );
1220 free( p_module->psz_capability );
1221 free( (void*)p_module->psz_shortname );
1222 free( (void*)p_module->psz_longname );
1223 free( (void*)p_module->psz_help );
1226 #endif /* HAVE_DYNAMIC_PLUGINS */
1228 /*****************************************************************************
1229 * AllocateBuiltinModule: initialize a builtin module.
1230 *****************************************************************************
1231 * This function registers a builtin module and allocates a structure
1232 * for its information data. The module can then be handled by module_Need
1233 * and module_Unneed. It can be removed by DeleteModule.
1234 *****************************************************************************/
1235 static int AllocateBuiltinModule( vlc_object_t * p_this,
1236 int ( *pf_entry ) ( module_t * ) )
1238 module_t * p_module;
1240 /* Now that we have successfully loaded the module, we can
1241 * allocate a structure for it */
1242 p_module = vlc_module_create( p_this );
1243 if( p_module == NULL )
1245 msg_Err( p_this, "out of memory" );
1249 /* Initialize the module : fill p_module->psz_object_name, etc. */
1250 if( pf_entry( p_module ) != 0 )
1252 /* With a well-written module we shouldn't have to print an
1253 * additional error message here, but just make sure. */
1254 msg_Err( p_this, "failed calling entry point in builtin module" );
1255 vlc_object_destroy( p_module );
1259 /* Everything worked fine ! The module is ready to be added to the list. */
1260 p_module->b_builtin = VLC_TRUE;
1262 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1263 p_module->psz_object_name, p_module->psz_longname ); */
1265 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1270 /*****************************************************************************
1271 * DeleteModule: delete a module and its structure.
1272 *****************************************************************************
1273 * This function can only be called if the module isn't being used.
1274 *****************************************************************************/
1275 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1277 if( !p_module ) return VLC_EGENERIC;
1279 vlc_object_detach( p_module );
1281 /* We free the structures that we strdup()ed in Allocate*Module(). */
1282 #ifdef HAVE_DYNAMIC_PLUGINS
1283 if( !p_module->b_builtin )
1285 if( p_module->b_loaded && p_module->b_unloadable )
1287 module_Unload( p_module->handle );
1289 UndupModule( p_module );
1290 free( p_module->psz_filename );
1294 /* Free and detach the object's children */
1295 while( p_module->i_children )
1297 vlc_object_t *p_this = p_module->pp_children[0];
1298 vlc_object_detach( p_this );
1299 vlc_object_destroy( p_this );
1302 config_Free( p_module );
1303 vlc_object_destroy( p_module );