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() */
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
51 #ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
58 #if !defined(HAVE_DYNAMIC_PLUGINS)
59 /* no support for plugins */
60 #elif defined(HAVE_DL_DYLD)
61 # if defined(HAVE_MACH_O_DYLD_H)
62 # include <mach-o/dyld.h>
64 #elif defined(HAVE_DL_BEOS)
65 # if defined(HAVE_IMAGE_H)
68 #elif defined(HAVE_DL_WINDOWS)
70 #elif defined(HAVE_DL_DLOPEN)
71 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
74 # if defined(HAVE_SYS_DL_H)
77 #elif defined(HAVE_DL_SHL_LOAD)
78 # if defined(HAVE_DL_H)
83 #include "config/configuration.h"
85 #include "vlc_charset.h"
87 #include "modules/modules.h"
88 #include "modules/builtin.h"
90 /*****************************************************************************
92 *****************************************************************************/
93 #ifdef HAVE_DYNAMIC_PLUGINS
94 static void AllocateAllPlugins ( vlc_object_t * );
95 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
96 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
97 static module_t * AllocatePlugin( vlc_object_t *, char * );
99 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
100 static int DeleteModule ( module_t *, vlc_bool_t );
101 #ifdef HAVE_DYNAMIC_PLUGINS
102 static void DupModule ( module_t * );
103 static void UndupModule ( module_t * );
106 static void module_LoadMain( vlc_object_t *p_this );
108 /*****************************************************************************
109 * module_InitBank: create the module bank.
110 *****************************************************************************
111 * This function creates a module bank structure which will be filled later
112 * on with all the modules found.
113 *****************************************************************************/
114 void __module_InitBank( vlc_object_t *p_this )
116 module_bank_t *p_bank = NULL;
118 libvlc_global_data_t *p_libvlc_global = vlc_global();
120 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
121 var_Get( p_libvlc_global, "libvlc", &lockval );
122 vlc_mutex_lock( lockval.p_address );
123 if( p_libvlc_global->p_module_bank )
125 p_libvlc_global->p_module_bank->i_usage++;
126 vlc_mutex_unlock( lockval.p_address );
127 var_Destroy( p_libvlc_global, "libvlc" );
130 vlc_mutex_unlock( lockval.p_address );
131 var_Destroy( p_libvlc_global, "libvlc" );
133 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
136 p_bank->psz_object_name = "module bank";
138 p_bank->i_cache = p_bank->i_loaded_cache = 0;
139 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
140 p_bank->b_cache = p_bank->b_cache_dirty =
141 p_bank->b_cache_delete = VLC_FALSE;
143 /* Everything worked, attach the object */
144 p_libvlc_global->p_module_bank = p_bank;
145 vlc_object_attach( p_bank, p_libvlc_global );
147 module_LoadMain( p_this );
151 /*****************************************************************************
152 * module_EndBank: empty the module bank.
153 *****************************************************************************
154 * This function unloads all unused plugin modules and empties the module
155 * bank in case of success.
156 *****************************************************************************/
157 void __module_EndBank( vlc_object_t *p_this )
159 module_t * p_next = NULL;
161 libvlc_global_data_t *p_libvlc_global = vlc_global();
163 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
164 var_Get( p_libvlc_global, "libvlc", &lockval );
165 vlc_mutex_lock( lockval.p_address );
166 if( !p_libvlc_global->p_module_bank )
168 vlc_mutex_unlock( lockval.p_address );
169 var_Destroy( p_libvlc_global, "libvlc" );
172 if( --p_libvlc_global->p_module_bank->i_usage )
174 vlc_mutex_unlock( lockval.p_address );
175 var_Destroy( p_libvlc_global, "libvlc" );
178 vlc_mutex_unlock( lockval.p_address );
179 var_Destroy( p_libvlc_global, "libvlc" );
181 config_AutoSaveConfigFile( p_this );
183 #ifdef HAVE_DYNAMIC_PLUGINS
184 # define p_bank p_libvlc_global->p_module_bank
185 if( p_bank->b_cache ) CacheSave( p_this );
186 while( p_bank->i_loaded_cache-- )
188 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
190 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
191 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
192 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
193 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
194 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
197 if( p_bank->pp_loaded_cache )
199 free( p_bank->pp_loaded_cache );
200 p_bank->pp_loaded_cache = NULL;
202 while( p_bank->i_cache-- )
204 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
205 free( p_bank->pp_cache[p_bank->i_cache] );
206 p_bank->pp_cache[p_bank->i_cache] = NULL;
208 if( p_bank->pp_cache )
210 free( p_bank->pp_cache );
211 p_bank->pp_cache = NULL;
216 vlc_object_detach( p_libvlc_global->p_module_bank );
218 while( p_libvlc_global->p_module_bank->i_children )
220 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
222 if( DeleteModule( p_next, VLC_TRUE ) )
224 /* Module deletion failed */
225 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
226 p_next->psz_object_name );
228 /* We just free the module by hand. Niahahahahaha. */
229 vlc_object_detach( p_next );
230 vlc_object_destroy( p_next );
234 vlc_object_destroy( p_libvlc_global->p_module_bank );
235 p_libvlc_global->p_module_bank = NULL;
238 /*****************************************************************************
239 * module_LoadMain: load the main program info into the module bank.
240 *****************************************************************************
241 * This function fills the module bank structure with the main module infos.
242 * This is very useful as it will allow us to consider the main program just
243 * as another module, and for instance the configuration options of main will
244 * be available in the module bank structure just as for every other module.
245 *****************************************************************************/
246 static void module_LoadMain( vlc_object_t *p_this )
249 libvlc_global_data_t *p_libvlc_global = vlc_global();
251 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
252 var_Get( p_libvlc_global, "libvlc", &lockval );
253 vlc_mutex_lock( lockval.p_address );
254 if( p_libvlc_global->p_module_bank->b_main )
256 vlc_mutex_unlock( lockval.p_address );
257 var_Destroy( p_libvlc_global, "libvlc" );
260 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
261 vlc_mutex_unlock( lockval.p_address );
262 var_Destroy( p_libvlc_global, "libvlc" );
264 AllocateBuiltinModule( p_this, vlc_entry__main );
267 /*****************************************************************************
268 * module_LoadBuiltins: load all modules which we built with.
269 *****************************************************************************
270 * This function fills the module bank structure with the builtin modules.
271 *****************************************************************************/
272 void __module_LoadBuiltins( vlc_object_t * p_this )
275 libvlc_global_data_t *p_libvlc_global = vlc_global();
277 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
278 var_Get( p_libvlc_global, "libvlc", &lockval );
279 vlc_mutex_lock( lockval.p_address );
280 if( p_libvlc_global->p_module_bank->b_builtins )
282 vlc_mutex_unlock( lockval.p_address );
283 var_Destroy( p_libvlc_global, "libvlc" );
286 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
287 vlc_mutex_unlock( lockval.p_address );
288 var_Destroy( p_libvlc_global, "libvlc" );
290 msg_Dbg( p_this, "checking builtin modules" );
291 ALLOCATE_ALL_BUILTINS();
294 /*****************************************************************************
295 * module_LoadPlugins: load all plugin modules we can find.
296 *****************************************************************************
297 * This function fills the module bank structure with the plugin modules.
298 *****************************************************************************/
299 void __module_LoadPlugins( vlc_object_t * p_this )
301 #ifdef HAVE_DYNAMIC_PLUGINS
303 libvlc_global_data_t *p_libvlc_global = vlc_global();
305 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
306 var_Get( p_libvlc_global, "libvlc", &lockval );
307 vlc_mutex_lock( lockval.p_address );
308 if( p_libvlc_global->p_module_bank->b_plugins )
310 vlc_mutex_unlock( lockval.p_address );
311 var_Destroy( p_libvlc_global, "libvlc" );
314 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
315 vlc_mutex_unlock( lockval.p_address );
316 var_Destroy( p_libvlc_global, "libvlc" );
318 msg_Dbg( p_this, "checking plugin modules" );
320 if( config_GetInt( p_this, "plugins-cache" ) )
321 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
323 if( p_libvlc_global->p_module_bank->b_cache ||
324 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
326 AllocateAllPlugins( p_this );
330 /*****************************************************************************
331 * module_IsCapable: checks whether a module implements a capability.
332 *****************************************************************************/
333 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
335 return !strcmp( m->psz_capability, cap );
338 /*****************************************************************************
339 * module_GetObjName: internal name of a module.
340 *****************************************************************************/
341 const char *module_GetObjName( const module_t *m )
343 return m->psz_object_name;
346 /*****************************************************************************
347 * module_GetName: human-friendly name of a module.
348 *****************************************************************************/
349 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
351 if( long_name && ( m->psz_longname != NULL) )
352 return m->psz_longname;
354 return m->psz_shortname ?: m->psz_object_name;
357 const char *module_GetHelp( const module_t *m )
362 /*****************************************************************************
363 * module_Need: return the best module function, given a capability list.
364 *****************************************************************************
365 * This function returns the module that best fits the asked capabilities.
366 *****************************************************************************/
367 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
368 const char *psz_name, vlc_bool_t b_strict )
370 typedef struct module_list_t module_list_t;
377 module_list_t *p_next;
380 module_list_t *p_list, *p_first, *p_tmp;
383 int i_which_module, i_index = 0;
388 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
389 vlc_bool_t b_force_backup = p_this->b_force;
392 /* Deal with variables */
393 if( psz_name && psz_name[0] == '$' )
396 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
397 var_Get( p_this, psz_name + 1, &val );
398 psz_var = val.psz_string;
402 /* Count how many different shortcuts were asked for */
403 if( psz_name && *psz_name )
405 char *psz_parser, *psz_last_shortcut;
407 /* If the user wants none, give him none. */
408 if( !strcmp( psz_name, "none" ) )
415 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
417 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
419 if( *psz_parser == ',' )
423 psz_last_shortcut = psz_parser + 1;
427 /* Check if the user wants to override the "strict" mode */
428 if( psz_last_shortcut )
430 if( !strcmp(psz_last_shortcut, "none") )
435 else if( !strcmp(psz_last_shortcut, "any") )
437 b_strict = VLC_FALSE;
443 /* Sort the modules and test them */
444 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
445 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
447 unsigned i_cpu = vlc_CPU();
449 /* Parse the module list for capabilities and probe each of them */
450 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
452 int i_shortcut_bonus = 0;
454 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
456 /* Test that this module can do what we need */
457 if( !module_IsCapable( p_module, psz_capability ) )
459 /* Don't recurse through the sub-modules because vlc_list_find()
460 * will list them anyway. */
464 /* Test if we have the required CPU */
465 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
470 /* If we required a shortcut, check this plugin provides it. */
471 if( i_shortcuts > 0 )
474 const char *psz_name = psz_shortcuts;
476 /* Let's drop modules with a <= 0 score (unless they are
477 * explicitly requested) */
478 b_trash = p_module->i_score <= 0;
480 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
482 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
485 if( ( c = strchr( psz_name, '@' ) )
486 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
488 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
493 i_shortcut_bonus = i_short * 10000;
498 /* Go to the next shortcut... This is so lame! */
499 psz_name += strlen( psz_name ) + 1;
502 /* If we are in "strict" mode and we couldn't
503 * find the module in the list of provided shortcuts,
504 * then kick the bastard out of here!!! */
508 /* If we didn't require a shortcut, trash <= 0 scored plugins */
509 else if( p_module->i_score <= 0 )
516 /* Store this new module */
517 p_list[ i_index ].p_module = p_module;
518 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
519 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
521 /* Add it to the modules-to-probe list */
524 p_list[ 0 ].p_next = NULL;
529 /* Ok, so at school you learned that quicksort is quick, and
530 * bubble sort sucks raw eggs. But that's when dealing with
531 * thousands of items. Here we have barely 50. */
532 module_list_t *p_newlist = p_first;
534 if( p_first->i_score < p_list[ i_index ].i_score )
536 p_list[ i_index ].p_next = p_first;
537 p_first = &p_list[ i_index ];
541 while( p_newlist->p_next != NULL &&
542 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
544 p_newlist = p_newlist->p_next;
547 p_list[ i_index ].p_next = p_newlist->p_next;
548 p_newlist->p_next = &p_list[ i_index ];
555 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
556 i_index, i_index == 1 ? "" : "s" );
558 /* Lock all candidate modules */
560 while( p_tmp != NULL )
562 vlc_object_yield( p_tmp->p_module );
563 p_tmp = p_tmp->p_next;
566 /* We can release the list, interesting modules were yielded */
567 vlc_list_release( p_all );
569 /* Parse the linked list and use the first successful module */
571 while( p_tmp != NULL )
573 #ifdef HAVE_DYNAMIC_PLUGINS
574 /* Make sure the module is loaded in mem */
575 module_t *p_module = p_tmp->p_module;
576 if( p_module->b_submodule )
577 p_module = (module_t *)p_module->p_parent;
579 if( !p_module->b_builtin && !p_module->b_loaded )
581 module_t *p_new_module =
582 AllocatePlugin( p_this, p_module->psz_filename );
585 CacheMerge( p_this, p_module, p_new_module );
586 vlc_object_attach( p_new_module, p_module );
587 DeleteModule( p_new_module, VLC_TRUE );
592 p_this->b_force = p_tmp->b_force;
593 if( p_tmp->p_module->pf_activate
594 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
599 vlc_object_release( p_tmp->p_module );
600 p_tmp = p_tmp->p_next;
603 /* Store the locked module value */
606 p_module = p_tmp->p_module;
607 p_tmp = p_tmp->p_next;
614 /* Unlock the remaining modules */
615 while( p_tmp != NULL )
617 vlc_object_release( p_tmp->p_module );
618 p_tmp = p_tmp->p_next;
622 p_this->b_force = b_force_backup;
624 if( p_module != NULL )
626 msg_Dbg( p_this, "using %s module \"%s\"",
627 psz_capability, p_module->psz_object_name );
629 else if( p_first == NULL )
631 if( !strcmp( psz_capability, "access_demux" ) )
633 msg_Warn( p_this, "no %s module matched \"%s\"",
634 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
638 msg_Err( p_this, "no %s module matched \"%s\"",
639 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
641 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
642 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
645 else if( psz_name != NULL && *psz_name )
647 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
648 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
651 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
653 if( p_module && !p_this->psz_object_name )
655 /* This assumes that p_this is the object which will be using the
656 * module. That's not always the case ... but it is in most cases.
659 p_this->psz_object_name = strdup( psz_alias );
661 p_this->psz_object_name = strdup( p_module->psz_object_name );
664 free( psz_shortcuts );
667 /* Don't forget that the module is still locked */
671 /*****************************************************************************
672 * module_Unneed: decrease the usage count of a module.
673 *****************************************************************************
674 * This function must be called by the thread that called module_Need, to
675 * decrease the reference count and allow for hiding of modules.
676 *****************************************************************************/
677 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
679 /* Use the close method */
680 if( p_module->pf_deactivate )
682 p_module->pf_deactivate( p_this );
685 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
687 vlc_object_release( p_module );
692 /*****************************************************************************
693 * module_Find: get a pointer to a module_t given it's name.
694 *****************************************************************************/
695 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
699 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
700 for( i = 0 ; i < p_list->i_count; i++)
702 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
703 const char *psz_module_name = p_module->psz_object_name;
704 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
706 /* We can release the list, and return yes */
707 vlc_object_yield( p_module );
708 vlc_list_release( p_list );
712 vlc_list_release( p_list );
717 /*****************************************************************************
718 * module_Put: release a module_t pointer from module_Find().
719 *****************************************************************************/
720 void module_Put ( module_t *module )
722 vlc_object_release ( module );
726 /*****************************************************************************
727 * module_Exists: tell if a module exists.
728 *****************************************************************************
729 * This function is a boolean function that tells if a module exist or not.
730 *****************************************************************************/
731 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
733 module_t *p_module = __module_Find( p_this, psz_name );
736 module_Put( p_module );
745 /*****************************************************************************
746 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
747 * names of the modules that have a certain capability.
748 * Free after uses both the string and the table.
749 *****************************************************************************/
750 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
751 const char * psz_capability,
752 char ***pppsz_longname )
758 /* Do it in two passes */
759 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
760 for( i = 0 ; i < p_list->i_count; i++)
762 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
763 const char *psz_module_capability = p_module->psz_capability;
764 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
767 psz_ret = malloc( sizeof(char*) * (count+1) );
769 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
771 for( i = 0 ; i < p_list->i_count; i++)
773 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
774 const char *psz_module_capability = p_module->psz_capability;
775 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
777 int k = -1; /* hack to handle submodules properly */
778 if( p_module->b_submodule )
780 while( p_module->pp_shortcuts[++k] != NULL );
783 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
784 :p_module->psz_object_name );
786 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
790 psz_ret[count] = NULL;
792 vlc_list_release( p_list );
798 module_config_t *module_GetConfig (const module_t *module, unsigned *restrict psize)
800 unsigned size = module->confsize;
801 module_config_t *config = malloc (size * sizeof (*config));
803 assert (psize != NULL);
806 for (unsigned i = 0, j = 0; i < size; i++)
808 const module_config_t *item = module->p_config + i;
809 if (item->b_internal /* internal option */
810 || item->b_unsaveable /* non-modifiable option */
811 || item->b_removed /* removed option */)
815 memcpy (config + j, item, sizeof (*config));
822 void module_PutConfig (module_config_t *config)
827 /*****************************************************************************
828 * Following functions are local.
829 *****************************************************************************/
831 /*****************************************************************************
832 * AllocateAllPlugins: load all plugin modules we can find.
833 *****************************************************************************/
834 #ifdef HAVE_DYNAMIC_PLUGINS
835 static void AllocateAllPlugins( vlc_object_t *p_this )
837 /* Yes, there are two NULLs because we replace one with "plugin-path". */
838 #if defined( WIN32 ) || defined( UNDER_CE )
839 const char *path[] = { "modules", "", "plugins", NULL, NULL };
841 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
844 const char *const *ppsz_path;
846 /* If the user provided a plugin path, we add it to the list */
847 char *userpath = config_GetPsz( p_this, "plugin-path" );
848 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
850 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
854 if (!**ppsz_path) continue;
856 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
858 /* Handle relative as well as absolute paths */
860 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
861 (*ppsz_path)[1] != ':' )
863 if( (*ppsz_path)[0] != '/' )
866 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
867 vlc_global()->psz_vlcpath, *ppsz_path) )
872 psz_fullpath = strdup( *ppsz_path );
874 if( psz_fullpath == NULL )
877 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
879 /* Don't go deeper than 5 subdirectories */
880 AllocatePluginDir( p_this, psz_fullpath, 5 );
882 free( psz_fullpath );
885 /* Free plugin-path */
889 /*****************************************************************************
890 * AllocatePluginDir: recursively parse a directory to look for plugins
891 *****************************************************************************/
892 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
895 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
901 #if defined( UNDER_CE ) || defined( _MSC_VER )
903 wchar_t psz_wpath[MAX_PATH + 256];
904 wchar_t psz_wdir[MAX_PATH];
906 char psz_path[MAX_PATH + 256];
907 WIN32_FIND_DATA finddata;
913 struct dirent * file;
917 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
922 #if defined( UNDER_CE ) || defined( _MSC_VER )
924 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
926 rc = GetFileAttributes( psz_wdir );
927 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
929 /* Parse all files in the directory */
930 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
932 rc = GetFileAttributes( psz_dir );
933 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
936 /* Parse all files in the directory */
937 sprintf( psz_path, "%s\\*", psz_dir );
940 handle = FindFirstFile( psz_wpath, &finddata );
942 handle = FindFirstFile( psz_path, &finddata );
944 if( handle == INVALID_HANDLE_VALUE )
946 /* Empty directory */
950 /* Parse the directory and try to load all files it contains. */
954 unsigned int i_len = wcslen( finddata.cFileName );
955 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
956 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
958 unsigned int i_len = strlen( finddata.cFileName );
959 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
963 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
964 || !strcmp( finddata.cFileName, ".." ) )
966 if( !FindNextFile( handle, &finddata ) ) break;
971 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
973 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
976 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
978 else if( i_len > strlen( LIBEXT )
979 /* We only load files ending with LIBEXT */
980 && !strncasecmp( psz_path + strlen( psz_path)
982 LIBEXT, strlen( LIBEXT ) ) )
984 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
985 int64_t i_time = 0, i_size = 0;
988 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
991 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
995 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
997 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
998 i_size = attrbuf.nFileSizeHigh;
1000 i_size |= attrbuf.nFileSizeLow;
1002 psz_file = psz_path;
1004 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1007 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1009 /* Close the directory */
1010 FindClose( handle );
1013 dir = opendir( psz_dir );
1019 i_dirlen = strlen( psz_dir );
1021 /* Parse the directory and try to load all files it contains. */
1022 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1024 struct stat statbuf;
1028 /* Skip ".", ".." */
1029 if( !*file->d_name || !strcmp( file->d_name, "." )
1030 || !strcmp( file->d_name, ".." ) )
1035 i_len = strlen( file->d_name );
1036 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1037 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1039 i_stat = stat( psz_file, &statbuf );
1040 if( !i_stat && statbuf.st_mode & S_IFDIR )
1042 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1044 else if( i_len > strlen( LIBEXT )
1045 /* We only load files ending with LIBEXT */
1046 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1047 LIBEXT, strlen( LIBEXT ) ) )
1049 int64_t i_time = 0, i_size = 0;
1053 i_time = statbuf.st_mtime;
1054 i_size = statbuf.st_size;
1057 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1063 /* Close the directory */
1069 /*****************************************************************************
1070 * AllocatePluginFile: load a module into memory and initialize it.
1071 *****************************************************************************
1072 * This function loads a dynamically loadable module and allocates a structure
1073 * for its information data. The module can then be handled by module_Need
1074 * and module_Unneed. It can be removed by DeleteModule.
1075 *****************************************************************************/
1076 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1077 int64_t i_file_time, int64_t i_file_size )
1079 module_t * p_module = NULL;
1080 module_cache_t *p_cache_entry = NULL;
1083 * Check our plugins cache first then load plugin if needed
1086 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1088 if( !p_cache_entry )
1090 p_module = AllocatePlugin( p_this, psz_file );
1094 /* If junk dll, don't try to load it */
1095 if( p_cache_entry->b_junk )
1101 module_config_t *p_item = NULL, *p_end = NULL;
1103 p_module = p_cache_entry->p_module;
1104 p_module->b_loaded = VLC_FALSE;
1106 /* For now we force loading if the module's config contains
1107 * callbacks or actions.
1108 * Could be optimized by adding an API call.*/
1109 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1110 p_item < p_end; p_item++ )
1112 if( p_item->pf_callback || p_item->i_action )
1114 p_module = AllocatePlugin( p_this, psz_file );
1118 if( p_module == p_cache_entry->p_module )
1119 p_cache_entry->b_used = VLC_TRUE;
1125 libvlc_global_data_t *p_libvlc_global = vlc_global();
1127 /* Everything worked fine !
1128 * The module is ready to be added to the list. */
1129 p_module->b_builtin = VLC_FALSE;
1131 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1132 p_module->psz_object_name, p_module->psz_longname ); */
1134 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1136 if( !p_libvlc_global->p_module_bank->b_cache )
1139 #define p_bank p_libvlc_global->p_module_bank
1140 /* Add entry to cache */
1142 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1143 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1144 if( !p_bank->pp_cache[p_bank->i_cache] )
1146 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1147 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1148 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1149 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1150 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1151 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1155 return p_module ? 0 : -1;
1158 /*****************************************************************************
1159 * AllocatePlugin: load a module into memory and initialize it.
1160 *****************************************************************************
1161 * This function loads a dynamically loadable module and allocates a structure
1162 * for its information data. The module can then be handled by module_Need
1163 * and module_Unneed. It can be removed by DeleteModule.
1164 *****************************************************************************/
1165 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1167 module_t * p_module = NULL;
1168 module_handle_t handle;
1170 if( module_Load( p_this, psz_file, &handle ) )
1173 /* Now that we have successfully loaded the module, we can
1174 * allocate a structure for it */
1175 p_module = vlc_module_create( p_this );
1176 if( p_module == NULL )
1178 msg_Err( p_this, "out of memory" );
1179 module_Unload( handle );
1183 /* We need to fill these since they may be needed by module_Call() */
1184 p_module->psz_filename = psz_file;
1185 p_module->handle = handle;
1186 p_module->b_loaded = VLC_TRUE;
1188 /* Initialize the module: fill p_module, default config */
1189 if( module_Call( p_module ) != 0 )
1191 /* We couldn't call module_init() */
1192 vlc_object_destroy( p_module );
1193 module_Unload( handle );
1197 DupModule( p_module );
1198 p_module->psz_filename = strdup( p_module->psz_filename );
1200 /* Everything worked fine ! The module is ready to be added to the list. */
1201 p_module->b_builtin = VLC_FALSE;
1206 /*****************************************************************************
1207 * DupModule: make a plugin module standalone.
1208 *****************************************************************************
1209 * This function duplicates all strings in the module, so that the dynamic
1210 * object can be unloaded. It acts recursively on submodules.
1211 *****************************************************************************/
1212 static void DupModule( module_t *p_module )
1214 const char **pp_shortcut;
1217 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1219 *pp_shortcut = strdup( *pp_shortcut );
1222 /* We strdup() these entries so that they are still valid when the
1223 * module is unloaded. */
1224 p_module->psz_object_name = strdup( p_module->psz_object_name );
1225 p_module->psz_capability = strdup( p_module->psz_capability );
1226 p_module->psz_shortname = p_module->psz_shortname ?
1227 strdup( p_module->psz_shortname ) : NULL;
1228 p_module->psz_longname = strdup( p_module->psz_longname );
1229 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1232 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1234 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1238 /*****************************************************************************
1239 * UndupModule: free a duplicated module.
1240 *****************************************************************************
1241 * This function frees the allocations done in DupModule().
1242 *****************************************************************************/
1243 static void UndupModule( module_t *p_module )
1245 const char **pp_shortcut;
1248 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1250 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1253 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1255 free( (void*)*pp_shortcut );
1258 free( (void*)p_module->psz_object_name );
1259 free( p_module->psz_capability );
1260 free( (void*)p_module->psz_shortname );
1261 free( (void*)p_module->psz_longname );
1262 free( (void*)p_module->psz_help );
1265 #endif /* HAVE_DYNAMIC_PLUGINS */
1267 /*****************************************************************************
1268 * AllocateBuiltinModule: initialize a builtin module.
1269 *****************************************************************************
1270 * This function registers a builtin module and allocates a structure
1271 * for its information data. The module can then be handled by module_Need
1272 * and module_Unneed. It can be removed by DeleteModule.
1273 *****************************************************************************/
1274 static int AllocateBuiltinModule( vlc_object_t * p_this,
1275 int ( *pf_entry ) ( module_t * ) )
1277 module_t * p_module;
1279 /* Now that we have successfully loaded the module, we can
1280 * allocate a structure for it */
1281 p_module = vlc_module_create( p_this );
1282 if( p_module == NULL )
1284 msg_Err( p_this, "out of memory" );
1288 /* Initialize the module : fill p_module->psz_object_name, etc. */
1289 if( pf_entry( p_module ) != 0 )
1291 /* With a well-written module we shouldn't have to print an
1292 * additional error message here, but just make sure. */
1293 msg_Err( p_this, "failed calling entry point in builtin module" );
1294 vlc_object_destroy( p_module );
1298 /* Everything worked fine ! The module is ready to be added to the list. */
1299 p_module->b_builtin = VLC_TRUE;
1301 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1302 p_module->psz_object_name, p_module->psz_longname ); */
1304 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1309 /*****************************************************************************
1310 * DeleteModule: delete a module and its structure.
1311 *****************************************************************************
1312 * This function can only be called if the module isn't being used.
1313 *****************************************************************************/
1314 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1316 if( !p_module ) return VLC_EGENERIC;
1318 vlc_object_detach( p_module );
1320 /* We free the structures that we strdup()ed in Allocate*Module(). */
1321 #ifdef HAVE_DYNAMIC_PLUGINS
1322 if( !p_module->b_builtin )
1324 if( p_module->b_loaded && p_module->b_unloadable )
1326 module_Unload( p_module->handle );
1328 UndupModule( p_module );
1329 free( p_module->psz_filename );
1333 /* Free and detach the object's children */
1334 while( p_module->i_children )
1336 vlc_object_t *p_this = p_module->pp_children[0];
1337 vlc_object_detach( p_this );
1338 vlc_object_destroy( p_this );
1341 config_Free( p_module );
1342 vlc_object_destroy( p_module );