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 );
109 /*****************************************************************************
110 * module_InitBank: create the module bank.
111 *****************************************************************************
112 * This function creates a module bank structure which will be filled later
113 * on with all the modules found.
114 *****************************************************************************/
115 void __module_InitBank( vlc_object_t *p_this )
117 module_bank_t *p_bank = NULL;
119 libvlc_global_data_t *p_libvlc_global = vlc_global();
121 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
122 var_Get( p_libvlc_global, "libvlc", &lockval );
123 vlc_mutex_lock( lockval.p_address );
124 if( p_libvlc_global->p_module_bank )
126 p_libvlc_global->p_module_bank->i_usage++;
127 vlc_mutex_unlock( lockval.p_address );
128 var_Destroy( p_libvlc_global, "libvlc" );
131 vlc_mutex_unlock( lockval.p_address );
132 var_Destroy( p_libvlc_global, "libvlc" );
134 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
137 p_bank->psz_object_name = "module bank";
139 p_bank->i_cache = p_bank->i_loaded_cache = 0;
140 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
141 p_bank->b_cache = p_bank->b_cache_dirty =
142 p_bank->b_cache_delete = VLC_FALSE;
144 /* Everything worked, attach the object */
145 p_libvlc_global->p_module_bank = p_bank;
146 vlc_object_attach( p_bank, p_libvlc_global );
148 module_LoadMain( p_this );
152 /*****************************************************************************
153 * module_EndBank: empty the module bank.
154 *****************************************************************************
155 * This function unloads all unused plugin modules and empties the module
156 * bank in case of success.
157 *****************************************************************************/
158 void __module_EndBank( vlc_object_t *p_this )
160 module_t * p_next = NULL;
162 libvlc_global_data_t *p_libvlc_global = vlc_global();
164 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
165 var_Get( p_libvlc_global, "libvlc", &lockval );
166 vlc_mutex_lock( lockval.p_address );
167 if( !p_libvlc_global->p_module_bank )
169 vlc_mutex_unlock( lockval.p_address );
170 var_Destroy( p_libvlc_global, "libvlc" );
173 if( --p_libvlc_global->p_module_bank->i_usage )
175 vlc_mutex_unlock( lockval.p_address );
176 var_Destroy( p_libvlc_global, "libvlc" );
179 vlc_mutex_unlock( lockval.p_address );
180 var_Destroy( p_libvlc_global, "libvlc" );
182 config_AutoSaveConfigFile( p_this );
184 #ifdef HAVE_DYNAMIC_PLUGINS
185 # define p_bank p_libvlc_global->p_module_bank
186 if( p_bank->b_cache ) CacheSave( p_this );
187 while( p_bank->i_loaded_cache-- )
189 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
191 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
192 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
193 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
194 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
195 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
198 if( p_bank->pp_loaded_cache )
200 free( p_bank->pp_loaded_cache );
201 p_bank->pp_loaded_cache = NULL;
203 while( p_bank->i_cache-- )
205 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
206 free( p_bank->pp_cache[p_bank->i_cache] );
207 p_bank->pp_cache[p_bank->i_cache] = NULL;
209 if( p_bank->pp_cache )
211 free( p_bank->pp_cache );
212 p_bank->pp_cache = NULL;
217 vlc_object_detach( p_libvlc_global->p_module_bank );
219 while( p_libvlc_global->p_module_bank->i_children )
221 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
223 if( DeleteModule( p_next, VLC_TRUE ) )
225 /* Module deletion failed */
226 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
227 p_next->psz_object_name );
229 /* We just free the module by hand. Niahahahahaha. */
230 vlc_object_detach( p_next );
231 vlc_object_destroy( p_next );
235 vlc_object_destroy( p_libvlc_global->p_module_bank );
236 p_libvlc_global->p_module_bank = NULL;
239 /*****************************************************************************
240 * module_LoadMain: load the main program info into the module bank.
241 *****************************************************************************
242 * This function fills the module bank structure with the main module infos.
243 * This is very useful as it will allow us to consider the main program just
244 * as another module, and for instance the configuration options of main will
245 * be available in the module bank structure just as for every other module.
246 *****************************************************************************/
247 static void module_LoadMain( vlc_object_t *p_this )
250 libvlc_global_data_t *p_libvlc_global = vlc_global();
252 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
253 var_Get( p_libvlc_global, "libvlc", &lockval );
254 vlc_mutex_lock( lockval.p_address );
255 if( p_libvlc_global->p_module_bank->b_main )
257 vlc_mutex_unlock( lockval.p_address );
258 var_Destroy( p_libvlc_global, "libvlc" );
261 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
262 vlc_mutex_unlock( lockval.p_address );
263 var_Destroy( p_libvlc_global, "libvlc" );
265 AllocateBuiltinModule( p_this, vlc_entry__main );
268 /*****************************************************************************
269 * module_LoadBuiltins: load all modules which we built with.
270 *****************************************************************************
271 * This function fills the module bank structure with the builtin modules.
272 *****************************************************************************/
273 void __module_LoadBuiltins( vlc_object_t * p_this )
276 libvlc_global_data_t *p_libvlc_global = vlc_global();
278 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
279 var_Get( p_libvlc_global, "libvlc", &lockval );
280 vlc_mutex_lock( lockval.p_address );
281 if( p_libvlc_global->p_module_bank->b_builtins )
283 vlc_mutex_unlock( lockval.p_address );
284 var_Destroy( p_libvlc_global, "libvlc" );
287 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
288 vlc_mutex_unlock( lockval.p_address );
289 var_Destroy( p_libvlc_global, "libvlc" );
291 msg_Dbg( p_this, "checking builtin modules" );
292 ALLOCATE_ALL_BUILTINS();
295 /*****************************************************************************
296 * module_LoadPlugins: load all plugin modules we can find.
297 *****************************************************************************
298 * This function fills the module bank structure with the plugin modules.
299 *****************************************************************************/
300 void __module_LoadPlugins( vlc_object_t * p_this )
302 #ifdef HAVE_DYNAMIC_PLUGINS
304 libvlc_global_data_t *p_libvlc_global = vlc_global();
306 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
307 var_Get( p_libvlc_global, "libvlc", &lockval );
308 vlc_mutex_lock( lockval.p_address );
309 if( p_libvlc_global->p_module_bank->b_plugins )
311 vlc_mutex_unlock( lockval.p_address );
312 var_Destroy( p_libvlc_global, "libvlc" );
315 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
316 vlc_mutex_unlock( lockval.p_address );
317 var_Destroy( p_libvlc_global, "libvlc" );
319 msg_Dbg( p_this, "checking plugin modules" );
321 if( config_GetInt( p_this, "plugins-cache" ) )
322 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
324 if( p_libvlc_global->p_module_bank->b_cache ||
325 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
327 AllocateAllPlugins( p_this );
331 /*****************************************************************************
332 * module_IsCapable: checks whether a module implements a capability.
333 *****************************************************************************/
334 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
336 return !strcmp( m->psz_capability, cap );
339 /*****************************************************************************
340 * module_GetObjName: internal name of a module.
341 *****************************************************************************/
342 const char *module_GetObjName( const module_t *m )
344 return m->psz_object_name;
347 /*****************************************************************************
348 * module_GetName: human-friendly name of a module.
349 *****************************************************************************/
350 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
352 if( long_name && ( m->psz_longname != NULL) )
353 return m->psz_longname;
355 return m->psz_shortname ?: m->psz_object_name;
358 const char *module_GetHelp( const module_t *m )
363 /*****************************************************************************
364 * module_Need: return the best module function, given a capability list.
365 *****************************************************************************
366 * This function returns the module that best fits the asked capabilities.
367 *****************************************************************************/
368 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
369 const char *psz_name, vlc_bool_t b_strict )
371 typedef struct module_list_t module_list_t;
378 module_list_t *p_next;
381 module_list_t *p_list, *p_first, *p_tmp;
384 int i_which_module, i_index = 0;
389 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
390 vlc_bool_t b_force_backup = p_this->b_force;
393 /* Deal with variables */
394 if( psz_name && psz_name[0] == '$' )
397 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
398 var_Get( p_this, psz_name + 1, &val );
399 psz_var = val.psz_string;
403 /* Count how many different shortcuts were asked for */
404 if( psz_name && *psz_name )
406 char *psz_parser, *psz_last_shortcut;
408 /* If the user wants none, give him none. */
409 if( !strcmp( psz_name, "none" ) )
416 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
418 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
420 if( *psz_parser == ',' )
424 psz_last_shortcut = psz_parser + 1;
428 /* Check if the user wants to override the "strict" mode */
429 if( psz_last_shortcut )
431 if( !strcmp(psz_last_shortcut, "none") )
436 else if( !strcmp(psz_last_shortcut, "any") )
438 b_strict = VLC_FALSE;
444 /* Sort the modules and test them */
445 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
446 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
448 unsigned i_cpu = vlc_CPU();
450 /* Parse the module list for capabilities and probe each of them */
451 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
453 int i_shortcut_bonus = 0;
455 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
457 /* Test that this module can do what we need */
458 if( !module_IsCapable( p_module, psz_capability ) )
460 /* Don't recurse through the sub-modules because vlc_list_find()
461 * will list them anyway. */
465 /* Test if we have the required CPU */
466 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
471 /* If we required a shortcut, check this plugin provides it. */
472 if( i_shortcuts > 0 )
475 const char *psz_name = psz_shortcuts;
477 /* Let's drop modules with a <= 0 score (unless they are
478 * explicitly requested) */
479 b_trash = p_module->i_score <= 0;
481 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
483 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
486 if( ( c = strchr( psz_name, '@' ) )
487 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
489 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
494 i_shortcut_bonus = i_short * 10000;
499 /* Go to the next shortcut... This is so lame! */
500 psz_name += strlen( psz_name ) + 1;
503 /* If we are in "strict" mode and we couldn't
504 * find the module in the list of provided shortcuts,
505 * then kick the bastard out of here!!! */
509 /* If we didn't require a shortcut, trash <= 0 scored plugins */
510 else if( p_module->i_score <= 0 )
517 /* Store this new module */
518 p_list[ i_index ].p_module = p_module;
519 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
520 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
522 /* Add it to the modules-to-probe list */
525 p_list[ 0 ].p_next = NULL;
530 /* Ok, so at school you learned that quicksort is quick, and
531 * bubble sort sucks raw eggs. But that's when dealing with
532 * thousands of items. Here we have barely 50. */
533 module_list_t *p_newlist = p_first;
535 if( p_first->i_score < p_list[ i_index ].i_score )
537 p_list[ i_index ].p_next = p_first;
538 p_first = &p_list[ i_index ];
542 while( p_newlist->p_next != NULL &&
543 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
545 p_newlist = p_newlist->p_next;
548 p_list[ i_index ].p_next = p_newlist->p_next;
549 p_newlist->p_next = &p_list[ i_index ];
556 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
557 i_index, i_index == 1 ? "" : "s" );
559 /* Lock all candidate modules */
561 while( p_tmp != NULL )
563 vlc_object_yield( p_tmp->p_module );
564 p_tmp = p_tmp->p_next;
567 /* We can release the list, interesting modules were yielded */
568 vlc_list_release( p_all );
570 /* Parse the linked list and use the first successful module */
572 while( p_tmp != NULL )
574 #ifdef HAVE_DYNAMIC_PLUGINS
575 /* Make sure the module is loaded in mem */
576 module_t *p_module = p_tmp->p_module;
577 if( p_module->b_submodule )
578 p_module = (module_t *)p_module->p_parent;
580 if( !p_module->b_builtin && !p_module->b_loaded )
582 module_t *p_new_module =
583 AllocatePlugin( p_this, p_module->psz_filename );
586 CacheMerge( p_this, p_module, p_new_module );
587 vlc_object_attach( p_new_module, p_module );
588 DeleteModule( p_new_module, VLC_TRUE );
593 p_this->b_force = p_tmp->b_force;
594 if( p_tmp->p_module->pf_activate
595 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
600 vlc_object_release( p_tmp->p_module );
601 p_tmp = p_tmp->p_next;
604 /* Store the locked module value */
607 p_module = p_tmp->p_module;
608 p_tmp = p_tmp->p_next;
615 /* Unlock the remaining modules */
616 while( p_tmp != NULL )
618 vlc_object_release( p_tmp->p_module );
619 p_tmp = p_tmp->p_next;
623 p_this->b_force = b_force_backup;
625 if( p_module != NULL )
627 msg_Dbg( p_this, "using %s module \"%s\"",
628 psz_capability, p_module->psz_object_name );
630 else if( p_first == NULL )
632 if( !strcmp( psz_capability, "access_demux" ) )
634 msg_Warn( p_this, "no %s module matched \"%s\"",
635 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
639 msg_Err( p_this, "no %s module matched \"%s\"",
640 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
642 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
643 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
646 else if( psz_name != NULL && *psz_name )
648 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
649 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
652 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
654 if( p_module && !p_this->psz_object_name )
656 /* This assumes that p_this is the object which will be using the
657 * module. That's not always the case ... but it is in most cases.
660 p_this->psz_object_name = strdup( psz_alias );
662 p_this->psz_object_name = strdup( p_module->psz_object_name );
665 free( psz_shortcuts );
668 /* Don't forget that the module is still locked */
672 /*****************************************************************************
673 * module_Unneed: decrease the usage count of a module.
674 *****************************************************************************
675 * This function must be called by the thread that called module_Need, to
676 * decrease the reference count and allow for hiding of modules.
677 *****************************************************************************/
678 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
680 /* Use the close method */
681 if( p_module->pf_deactivate )
683 p_module->pf_deactivate( p_this );
686 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
688 vlc_object_release( p_module );
693 /*****************************************************************************
694 * module_Find: get a pointer to a module_t given it's name.
695 *****************************************************************************/
696 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
700 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
701 for( i = 0 ; i < p_list->i_count; i++)
703 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
704 const char *psz_module_name = p_module->psz_object_name;
705 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
707 /* We can release the list, and return yes */
708 vlc_object_yield( p_module );
709 vlc_list_release( p_list );
713 vlc_list_release( p_list );
718 /*****************************************************************************
719 * module_Put: release a module_t pointer from module_Find().
720 *****************************************************************************/
721 void module_Put ( module_t *module )
723 vlc_object_release ( module );
727 /*****************************************************************************
728 * module_Exists: tell if a module exists.
729 *****************************************************************************
730 * This function is a boolean function that tells if a module exist or not.
731 *****************************************************************************/
732 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
734 module_t *p_module = __module_Find( p_this, psz_name );
737 module_Put( p_module );
746 /*****************************************************************************
747 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
748 * names of the modules that have a certain capability.
749 * Free after uses both the string and the table.
750 *****************************************************************************/
751 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
752 const char * psz_capability,
753 char ***pppsz_longname )
759 /* Do it in two passes */
760 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
761 for( i = 0 ; i < p_list->i_count; i++)
763 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
764 const char *psz_module_capability = p_module->psz_capability;
765 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
768 psz_ret = malloc( sizeof(char*) * (count+1) );
770 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
772 for( i = 0 ; i < p_list->i_count; i++)
774 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
775 const char *psz_module_capability = p_module->psz_capability;
776 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
778 int k = -1; /* hack to handle submodules properly */
779 if( p_module->b_submodule )
781 while( p_module->pp_shortcuts[++k] != NULL );
784 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
785 :p_module->psz_object_name );
787 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
791 psz_ret[count] = NULL;
793 vlc_list_release( p_list );
799 module_config_t *module_GetConfig (const module_t *module, unsigned *restrict psize)
802 unsigned size = module->confsize;
803 module_config_t *config = malloc (size * sizeof (*config));
805 assert (psize != NULL);
808 for (i = 0, j = 0; i < size; i++)
810 const module_config_t *item = module->p_config + i;
811 if (item->b_internal /* internal option */
812 || item->b_unsaveable /* non-modifiable option */
813 || item->b_removed /* removed option */)
817 memcpy (config + j, item, sizeof (*config));
825 void module_PutConfig (module_config_t *config)
830 /*****************************************************************************
831 * Following functions are local.
832 *****************************************************************************/
834 /*****************************************************************************
835 * AllocateAllPlugins: load all plugin modules we can find.
836 *****************************************************************************/
837 #ifdef HAVE_DYNAMIC_PLUGINS
838 static void AllocateAllPlugins( vlc_object_t *p_this )
840 /* Yes, there are two NULLs because we replace one with "plugin-path". */
841 #if defined( WIN32 ) || defined( UNDER_CE )
842 const char *path[] = { "modules", "", "plugins", NULL, NULL };
844 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
847 const char *const *ppsz_path;
849 /* If the user provided a plugin path, we add it to the list */
850 char *userpath = config_GetPsz( p_this, "plugin-path" );
851 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
853 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
857 if (!**ppsz_path) continue;
859 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
861 /* Handle relative as well as absolute paths */
863 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
864 (*ppsz_path)[1] != ':' )
866 if( (*ppsz_path)[0] != '/' )
869 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
870 vlc_global()->psz_vlcpath, *ppsz_path) )
875 psz_fullpath = strdup( *ppsz_path );
877 if( psz_fullpath == NULL )
880 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
882 /* Don't go deeper than 5 subdirectories */
883 AllocatePluginDir( p_this, psz_fullpath, 5 );
885 free( psz_fullpath );
888 /* Free plugin-path */
892 /*****************************************************************************
893 * AllocatePluginDir: recursively parse a directory to look for plugins
894 *****************************************************************************/
895 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
898 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
904 #if defined( UNDER_CE ) || defined( _MSC_VER )
906 wchar_t psz_wpath[MAX_PATH + 256];
907 wchar_t psz_wdir[MAX_PATH];
909 char psz_path[MAX_PATH + 256];
910 WIN32_FIND_DATA finddata;
916 struct dirent * file;
920 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
925 #if defined( UNDER_CE ) || defined( _MSC_VER )
927 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
929 rc = GetFileAttributes( psz_wdir );
930 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
932 /* Parse all files in the directory */
933 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
935 rc = GetFileAttributes( psz_dir );
936 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
939 /* Parse all files in the directory */
940 sprintf( psz_path, "%s\\*", psz_dir );
943 handle = FindFirstFile( psz_wpath, &finddata );
945 handle = FindFirstFile( psz_path, &finddata );
947 if( handle == INVALID_HANDLE_VALUE )
949 /* Empty directory */
953 /* Parse the directory and try to load all files it contains. */
957 unsigned int i_len = wcslen( finddata.cFileName );
958 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
959 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
961 unsigned int i_len = strlen( finddata.cFileName );
962 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
966 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
967 || !strcmp( finddata.cFileName, ".." ) )
969 if( !FindNextFile( handle, &finddata ) ) break;
974 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
976 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
979 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
981 else if( i_len > strlen( LIBEXT )
982 /* We only load files ending with LIBEXT */
983 && !strncasecmp( psz_path + strlen( psz_path)
985 LIBEXT, strlen( LIBEXT ) ) )
987 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
988 int64_t i_time = 0, i_size = 0;
991 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
994 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
998 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1000 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1001 i_size = attrbuf.nFileSizeHigh;
1003 i_size |= attrbuf.nFileSizeLow;
1005 psz_file = psz_path;
1007 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1010 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1012 /* Close the directory */
1013 FindClose( handle );
1016 dir = opendir( psz_dir );
1022 i_dirlen = strlen( psz_dir );
1024 /* Parse the directory and try to load all files it contains. */
1025 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1027 struct stat statbuf;
1031 /* Skip ".", ".." */
1032 if( !*file->d_name || !strcmp( file->d_name, "." )
1033 || !strcmp( file->d_name, ".." ) )
1038 i_len = strlen( file->d_name );
1039 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1040 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1042 i_stat = stat( psz_file, &statbuf );
1043 if( !i_stat && statbuf.st_mode & S_IFDIR )
1045 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1047 else if( i_len > strlen( LIBEXT )
1048 /* We only load files ending with LIBEXT */
1049 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1050 LIBEXT, strlen( LIBEXT ) ) )
1052 int64_t i_time = 0, i_size = 0;
1056 i_time = statbuf.st_mtime;
1057 i_size = statbuf.st_size;
1060 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1066 /* Close the directory */
1072 /*****************************************************************************
1073 * AllocatePluginFile: load a module into memory and initialize it.
1074 *****************************************************************************
1075 * This function loads a dynamically loadable module and allocates a structure
1076 * for its information data. The module can then be handled by module_Need
1077 * and module_Unneed. It can be removed by DeleteModule.
1078 *****************************************************************************/
1079 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1080 int64_t i_file_time, int64_t i_file_size )
1082 module_t * p_module = NULL;
1083 module_cache_t *p_cache_entry = NULL;
1086 * Check our plugins cache first then load plugin if needed
1089 CacheFind( psz_file, i_file_time, i_file_size );
1091 if( !p_cache_entry )
1093 p_module = AllocatePlugin( p_this, psz_file );
1097 /* If junk dll, don't try to load it */
1098 if( p_cache_entry->b_junk )
1104 module_config_t *p_item = NULL, *p_end = NULL;
1106 p_module = p_cache_entry->p_module;
1107 p_module->b_loaded = VLC_FALSE;
1109 /* For now we force loading if the module's config contains
1110 * callbacks or actions.
1111 * Could be optimized by adding an API call.*/
1112 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1113 p_item < p_end; p_item++ )
1115 if( p_item->pf_callback || p_item->i_action )
1117 p_module = AllocatePlugin( p_this, psz_file );
1121 if( p_module == p_cache_entry->p_module )
1122 p_cache_entry->b_used = VLC_TRUE;
1128 libvlc_global_data_t *p_libvlc_global = vlc_global();
1130 /* Everything worked fine !
1131 * The module is ready to be added to the list. */
1132 p_module->b_builtin = VLC_FALSE;
1134 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1135 p_module->psz_object_name, p_module->psz_longname ); */
1137 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1139 if( !p_libvlc_global->p_module_bank->b_cache )
1142 #define p_bank p_libvlc_global->p_module_bank
1143 /* Add entry to cache */
1145 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1146 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1147 if( !p_bank->pp_cache[p_bank->i_cache] )
1149 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1150 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1151 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1152 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1153 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1154 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1158 return p_module ? 0 : -1;
1161 /*****************************************************************************
1162 * AllocatePlugin: load a module into memory and initialize it.
1163 *****************************************************************************
1164 * This function loads a dynamically loadable module and allocates a structure
1165 * for its information data. The module can then be handled by module_Need
1166 * and module_Unneed. It can be removed by DeleteModule.
1167 *****************************************************************************/
1168 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1170 module_t * p_module = NULL;
1171 module_handle_t handle;
1173 if( module_Load( p_this, psz_file, &handle ) )
1176 /* Now that we have successfully loaded the module, we can
1177 * allocate a structure for it */
1178 p_module = vlc_module_create( p_this );
1179 if( p_module == NULL )
1181 msg_Err( p_this, "out of memory" );
1182 module_Unload( handle );
1186 /* We need to fill these since they may be needed by module_Call() */
1187 p_module->psz_filename = psz_file;
1188 p_module->handle = handle;
1189 p_module->b_loaded = VLC_TRUE;
1191 /* Initialize the module: fill p_module, default config */
1192 if( module_Call( p_module ) != 0 )
1194 /* We couldn't call module_init() */
1195 vlc_object_destroy( p_module );
1196 module_Unload( handle );
1200 DupModule( p_module );
1201 p_module->psz_filename = strdup( p_module->psz_filename );
1203 /* Everything worked fine ! The module is ready to be added to the list. */
1204 p_module->b_builtin = VLC_FALSE;
1209 /*****************************************************************************
1210 * DupModule: make a plugin module standalone.
1211 *****************************************************************************
1212 * This function duplicates all strings in the module, so that the dynamic
1213 * object can be unloaded. It acts recursively on submodules.
1214 *****************************************************************************/
1215 static void DupModule( module_t *p_module )
1217 const char **pp_shortcut;
1220 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1222 *pp_shortcut = strdup( *pp_shortcut );
1225 /* We strdup() these entries so that they are still valid when the
1226 * module is unloaded. */
1227 p_module->psz_object_name = strdup( p_module->psz_object_name );
1228 p_module->psz_capability = strdup( p_module->psz_capability );
1229 p_module->psz_shortname = p_module->psz_shortname ?
1230 strdup( p_module->psz_shortname ) : NULL;
1231 p_module->psz_longname = strdup( p_module->psz_longname );
1232 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1235 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1237 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1241 /*****************************************************************************
1242 * UndupModule: free a duplicated module.
1243 *****************************************************************************
1244 * This function frees the allocations done in DupModule().
1245 *****************************************************************************/
1246 static void UndupModule( module_t *p_module )
1248 const char **pp_shortcut;
1251 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1253 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1256 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1258 free( (void*)*pp_shortcut );
1261 free( (void*)p_module->psz_object_name );
1262 free( p_module->psz_capability );
1263 free( (void*)p_module->psz_shortname );
1264 free( (void*)p_module->psz_longname );
1265 free( (void*)p_module->psz_help );
1268 #endif /* HAVE_DYNAMIC_PLUGINS */
1270 /*****************************************************************************
1271 * AllocateBuiltinModule: initialize a builtin module.
1272 *****************************************************************************
1273 * This function registers a builtin module and allocates a structure
1274 * for its information data. The module can then be handled by module_Need
1275 * and module_Unneed. It can be removed by DeleteModule.
1276 *****************************************************************************/
1277 static int AllocateBuiltinModule( vlc_object_t * p_this,
1278 int ( *pf_entry ) ( module_t * ) )
1280 module_t * p_module;
1282 /* Now that we have successfully loaded the module, we can
1283 * allocate a structure for it */
1284 p_module = vlc_module_create( p_this );
1285 if( p_module == NULL )
1287 msg_Err( p_this, "out of memory" );
1291 /* Initialize the module : fill p_module->psz_object_name, etc. */
1292 if( pf_entry( p_module ) != 0 )
1294 /* With a well-written module we shouldn't have to print an
1295 * additional error message here, but just make sure. */
1296 msg_Err( p_this, "failed calling entry point in builtin module" );
1297 vlc_object_destroy( p_module );
1301 /* Everything worked fine ! The module is ready to be added to the list. */
1302 p_module->b_builtin = VLC_TRUE;
1304 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1305 p_module->psz_object_name, p_module->psz_longname ); */
1307 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1312 /*****************************************************************************
1313 * DeleteModule: delete a module and its structure.
1314 *****************************************************************************
1315 * This function can only be called if the module isn't being used.
1316 *****************************************************************************/
1317 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1319 if( !p_module ) return VLC_EGENERIC;
1321 vlc_object_detach( p_module );
1323 /* We free the structures that we strdup()ed in Allocate*Module(). */
1324 #ifdef HAVE_DYNAMIC_PLUGINS
1325 if( !p_module->b_builtin )
1327 if( p_module->b_loaded && p_module->b_unloadable )
1329 module_Unload( p_module->handle );
1331 UndupModule( p_module );
1332 free( p_module->psz_filename );
1336 /* Free and detach the object's children */
1337 while( p_module->i_children )
1339 vlc_object_t *p_this = p_module->pp_children[0];
1340 vlc_object_detach( p_this );
1341 vlc_object_destroy( p_this );
1344 config_Free( p_module );
1345 vlc_object_destroy( p_module );