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)
801 unsigned size = module->confsize;
802 module_config_t *config = malloc (size * sizeof (*config));
804 assert (psize != NULL);
807 for (unsigned i = 0, j = 0; i < size; i++)
809 const module_config_t *item = module->p_config + i;
810 if (item->b_internal /* internal option */
811 || item->b_unsaveable /* non-modifiable option */
812 || item->b_removed /* removed option */)
816 memcpy (config + j, item, sizeof (*config));
823 void module_PutConfig (module_config_t *config)
828 /*****************************************************************************
829 * Following functions are local.
830 *****************************************************************************/
832 /*****************************************************************************
833 * AllocateAllPlugins: load all plugin modules we can find.
834 *****************************************************************************/
835 #ifdef HAVE_DYNAMIC_PLUGINS
836 static void AllocateAllPlugins( vlc_object_t *p_this )
838 /* Yes, there are two NULLs because we replace one with "plugin-path". */
839 #if defined( WIN32 ) || defined( UNDER_CE )
840 const char *path[] = { "modules", "", "plugins", NULL, NULL };
842 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
845 const char *const *ppsz_path;
847 /* If the user provided a plugin path, we add it to the list */
848 char *userpath = config_GetPsz( p_this, "plugin-path" );
849 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
851 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
855 if (!**ppsz_path) continue;
857 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
859 /* Handle relative as well as absolute paths */
861 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
862 (*ppsz_path)[1] != ':' )
864 if( (*ppsz_path)[0] != '/' )
867 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
868 vlc_global()->psz_vlcpath, *ppsz_path) )
873 psz_fullpath = strdup( *ppsz_path );
875 if( psz_fullpath == NULL )
878 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
880 /* Don't go deeper than 5 subdirectories */
881 AllocatePluginDir( p_this, psz_fullpath, 5 );
883 free( psz_fullpath );
886 /* Free plugin-path */
890 /*****************************************************************************
891 * AllocatePluginDir: recursively parse a directory to look for plugins
892 *****************************************************************************/
893 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
896 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
902 #if defined( UNDER_CE ) || defined( _MSC_VER )
904 wchar_t psz_wpath[MAX_PATH + 256];
905 wchar_t psz_wdir[MAX_PATH];
907 char psz_path[MAX_PATH + 256];
908 WIN32_FIND_DATA finddata;
914 struct dirent * file;
918 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
923 #if defined( UNDER_CE ) || defined( _MSC_VER )
925 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
927 rc = GetFileAttributes( psz_wdir );
928 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
930 /* Parse all files in the directory */
931 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
933 rc = GetFileAttributes( psz_dir );
934 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
937 /* Parse all files in the directory */
938 sprintf( psz_path, "%s\\*", psz_dir );
941 handle = FindFirstFile( psz_wpath, &finddata );
943 handle = FindFirstFile( psz_path, &finddata );
945 if( handle == INVALID_HANDLE_VALUE )
947 /* Empty directory */
951 /* Parse the directory and try to load all files it contains. */
955 unsigned int i_len = wcslen( finddata.cFileName );
956 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
957 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
959 unsigned int i_len = strlen( finddata.cFileName );
960 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
964 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
965 || !strcmp( finddata.cFileName, ".." ) )
967 if( !FindNextFile( handle, &finddata ) ) break;
972 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
974 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
977 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
979 else if( i_len > strlen( LIBEXT )
980 /* We only load files ending with LIBEXT */
981 && !strncasecmp( psz_path + strlen( psz_path)
983 LIBEXT, strlen( LIBEXT ) ) )
985 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
986 int64_t i_time = 0, i_size = 0;
989 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
992 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
996 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
998 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
999 i_size = attrbuf.nFileSizeHigh;
1001 i_size |= attrbuf.nFileSizeLow;
1003 psz_file = psz_path;
1005 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1008 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1010 /* Close the directory */
1011 FindClose( handle );
1014 dir = opendir( psz_dir );
1020 i_dirlen = strlen( psz_dir );
1022 /* Parse the directory and try to load all files it contains. */
1023 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1025 struct stat statbuf;
1029 /* Skip ".", ".." */
1030 if( !*file->d_name || !strcmp( file->d_name, "." )
1031 || !strcmp( file->d_name, ".." ) )
1036 i_len = strlen( file->d_name );
1037 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1038 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1040 i_stat = stat( psz_file, &statbuf );
1041 if( !i_stat && statbuf.st_mode & S_IFDIR )
1043 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1045 else if( i_len > strlen( LIBEXT )
1046 /* We only load files ending with LIBEXT */
1047 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1048 LIBEXT, strlen( LIBEXT ) ) )
1050 int64_t i_time = 0, i_size = 0;
1054 i_time = statbuf.st_mtime;
1055 i_size = statbuf.st_size;
1058 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1064 /* Close the directory */
1070 /*****************************************************************************
1071 * AllocatePluginFile: load a module into memory and initialize it.
1072 *****************************************************************************
1073 * This function loads a dynamically loadable module and allocates a structure
1074 * for its information data. The module can then be handled by module_Need
1075 * and module_Unneed. It can be removed by DeleteModule.
1076 *****************************************************************************/
1077 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1078 int64_t i_file_time, int64_t i_file_size )
1080 module_t * p_module = NULL;
1081 module_cache_t *p_cache_entry = NULL;
1084 * Check our plugins cache first then load plugin if needed
1087 CacheFind( psz_file, i_file_time, i_file_size );
1089 if( !p_cache_entry )
1091 p_module = AllocatePlugin( p_this, psz_file );
1095 /* If junk dll, don't try to load it */
1096 if( p_cache_entry->b_junk )
1102 module_config_t *p_item = NULL, *p_end = NULL;
1104 p_module = p_cache_entry->p_module;
1105 p_module->b_loaded = VLC_FALSE;
1107 /* For now we force loading if the module's config contains
1108 * callbacks or actions.
1109 * Could be optimized by adding an API call.*/
1110 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1111 p_item < p_end; p_item++ )
1113 if( p_item->pf_callback || p_item->i_action )
1115 p_module = AllocatePlugin( p_this, psz_file );
1119 if( p_module == p_cache_entry->p_module )
1120 p_cache_entry->b_used = VLC_TRUE;
1126 libvlc_global_data_t *p_libvlc_global = vlc_global();
1128 /* Everything worked fine !
1129 * The module is ready to be added to the list. */
1130 p_module->b_builtin = VLC_FALSE;
1132 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1133 p_module->psz_object_name, p_module->psz_longname ); */
1135 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1137 if( !p_libvlc_global->p_module_bank->b_cache )
1140 #define p_bank p_libvlc_global->p_module_bank
1141 /* Add entry to cache */
1143 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1144 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1145 if( !p_bank->pp_cache[p_bank->i_cache] )
1147 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1148 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1149 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1150 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1151 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1152 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1156 return p_module ? 0 : -1;
1159 /*****************************************************************************
1160 * AllocatePlugin: load a module into memory and initialize it.
1161 *****************************************************************************
1162 * This function loads a dynamically loadable module and allocates a structure
1163 * for its information data. The module can then be handled by module_Need
1164 * and module_Unneed. It can be removed by DeleteModule.
1165 *****************************************************************************/
1166 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1168 module_t * p_module = NULL;
1169 module_handle_t handle;
1171 if( module_Load( p_this, psz_file, &handle ) )
1174 /* Now that we have successfully loaded the module, we can
1175 * allocate a structure for it */
1176 p_module = vlc_module_create( p_this );
1177 if( p_module == NULL )
1179 msg_Err( p_this, "out of memory" );
1180 module_Unload( handle );
1184 /* We need to fill these since they may be needed by module_Call() */
1185 p_module->psz_filename = psz_file;
1186 p_module->handle = handle;
1187 p_module->b_loaded = VLC_TRUE;
1189 /* Initialize the module: fill p_module, default config */
1190 if( module_Call( p_module ) != 0 )
1192 /* We couldn't call module_init() */
1193 vlc_object_destroy( p_module );
1194 module_Unload( handle );
1198 DupModule( p_module );
1199 p_module->psz_filename = strdup( p_module->psz_filename );
1201 /* Everything worked fine ! The module is ready to be added to the list. */
1202 p_module->b_builtin = VLC_FALSE;
1207 /*****************************************************************************
1208 * DupModule: make a plugin module standalone.
1209 *****************************************************************************
1210 * This function duplicates all strings in the module, so that the dynamic
1211 * object can be unloaded. It acts recursively on submodules.
1212 *****************************************************************************/
1213 static void DupModule( module_t *p_module )
1215 const char **pp_shortcut;
1218 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1220 *pp_shortcut = strdup( *pp_shortcut );
1223 /* We strdup() these entries so that they are still valid when the
1224 * module is unloaded. */
1225 p_module->psz_object_name = strdup( p_module->psz_object_name );
1226 p_module->psz_capability = strdup( p_module->psz_capability );
1227 p_module->psz_shortname = p_module->psz_shortname ?
1228 strdup( p_module->psz_shortname ) : NULL;
1229 p_module->psz_longname = strdup( p_module->psz_longname );
1230 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1233 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1235 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1239 /*****************************************************************************
1240 * UndupModule: free a duplicated module.
1241 *****************************************************************************
1242 * This function frees the allocations done in DupModule().
1243 *****************************************************************************/
1244 static void UndupModule( module_t *p_module )
1246 const char **pp_shortcut;
1249 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1251 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1254 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1256 free( (void*)*pp_shortcut );
1259 free( (void*)p_module->psz_object_name );
1260 free( p_module->psz_capability );
1261 free( (void*)p_module->psz_shortname );
1262 free( (void*)p_module->psz_longname );
1263 free( (void*)p_module->psz_help );
1266 #endif /* HAVE_DYNAMIC_PLUGINS */
1268 /*****************************************************************************
1269 * AllocateBuiltinModule: initialize a builtin module.
1270 *****************************************************************************
1271 * This function registers a builtin module and allocates a structure
1272 * for its information data. The module can then be handled by module_Need
1273 * and module_Unneed. It can be removed by DeleteModule.
1274 *****************************************************************************/
1275 static int AllocateBuiltinModule( vlc_object_t * p_this,
1276 int ( *pf_entry ) ( module_t * ) )
1278 module_t * p_module;
1280 /* Now that we have successfully loaded the module, we can
1281 * allocate a structure for it */
1282 p_module = vlc_module_create( p_this );
1283 if( p_module == NULL )
1285 msg_Err( p_this, "out of memory" );
1289 /* Initialize the module : fill p_module->psz_object_name, etc. */
1290 if( pf_entry( p_module ) != 0 )
1292 /* With a well-written module we shouldn't have to print an
1293 * additional error message here, but just make sure. */
1294 msg_Err( p_this, "failed calling entry point in builtin module" );
1295 vlc_object_destroy( p_module );
1299 /* Everything worked fine ! The module is ready to be added to the list. */
1300 p_module->b_builtin = VLC_TRUE;
1302 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1303 p_module->psz_object_name, p_module->psz_longname ); */
1305 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1310 /*****************************************************************************
1311 * DeleteModule: delete a module and its structure.
1312 *****************************************************************************
1313 * This function can only be called if the module isn't being used.
1314 *****************************************************************************/
1315 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1317 if( !p_module ) return VLC_EGENERIC;
1319 vlc_object_detach( p_module );
1321 /* We free the structures that we strdup()ed in Allocate*Module(). */
1322 #ifdef HAVE_DYNAMIC_PLUGINS
1323 if( !p_module->b_builtin )
1325 if( p_module->b_loaded && p_module->b_unloadable )
1327 module_Unload( p_module->handle );
1329 UndupModule( p_module );
1330 free( p_module->psz_filename );
1334 /* Free and detach the object's children */
1335 while( p_module->i_children )
1337 vlc_object_t *p_this = p_module->pp_children[0];
1338 vlc_object_detach( p_this );
1339 vlc_object_destroy( p_this );
1342 config_Free( p_module );
1343 vlc_object_destroy( p_module );