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"
87 #include "vlc_arrays.h"
89 #include "modules/modules.h"
90 #include "modules/builtin.h"
92 /*****************************************************************************
94 *****************************************************************************/
95 #ifdef HAVE_DYNAMIC_PLUGINS
96 static void AllocateAllPlugins ( vlc_object_t * );
97 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
98 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
99 static module_t * AllocatePlugin( vlc_object_t *, char * );
101 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
102 static int DeleteModule ( module_t *, bool );
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static void DupModule ( module_t * );
105 static void UndupModule ( module_t * );
111 * Creates a module bank structure which will be filled later
112 * on with all the modules found.
113 * \param p_this vlc object structure
116 void __module_InitBank( vlc_object_t *p_this )
118 module_bank_t *p_bank = NULL;
119 libvlc_global_data_t *p_libvlc_global = vlc_global();
121 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
123 if( p_libvlc_global->p_module_bank == NULL )
125 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
126 p_bank->psz_object_name = strdup( "module bank" );
128 p_bank->i_cache = p_bank->i_loaded_cache = 0;
129 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
130 p_bank->b_cache = p_bank->b_cache_dirty =
131 p_bank->b_cache_delete = false;
133 /* Everything worked, attach the object */
134 p_libvlc_global->p_module_bank = p_bank;
135 vlc_object_attach( p_bank, p_libvlc_global );
137 /* Fills the module bank structure with the main module infos.
138 * This is very useful as it will allow us to consider the main
139 * library just as another module, and for instance the configuration
140 * options of main will be available in the module bank structure just
141 * as for every other module. */
142 AllocateBuiltinModule( p_this, vlc_entry__main );
145 p_libvlc_global->p_module_bank->i_usage++;
147 vlc_mutex_unlock( lock );
154 * Unloads all unused plugin modules and empties the module
155 * bank in case of success.
156 * \param p_this vlc object structure
159 void __module_EndBank( vlc_object_t *p_this )
161 module_t * p_next = NULL;
162 libvlc_global_data_t *p_libvlc_global = vlc_global();
164 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
165 if( !p_libvlc_global->p_module_bank )
167 vlc_mutex_unlock( lock );
170 if( --p_libvlc_global->p_module_bank->i_usage )
172 vlc_mutex_unlock( lock );
175 vlc_mutex_unlock( lock );
177 /* Save the configuration */
178 config_AutoSaveConfigFile( p_this );
180 #ifdef HAVE_DYNAMIC_PLUGINS
181 # define p_bank p_libvlc_global->p_module_bank
182 if( p_bank->b_cache ) CacheSave( p_this );
183 while( p_bank->i_loaded_cache-- )
185 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
188 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
189 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
190 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
191 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
192 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
195 if( p_bank->pp_loaded_cache )
197 free( p_bank->pp_loaded_cache );
198 p_bank->pp_loaded_cache = NULL;
200 while( p_bank->i_cache-- )
202 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
203 free( p_bank->pp_cache[p_bank->i_cache] );
204 p_bank->pp_cache[p_bank->i_cache] = NULL;
206 if( p_bank->pp_cache )
208 free( p_bank->pp_cache );
209 p_bank->pp_cache = NULL;
214 vlc_object_detach( p_libvlc_global->p_module_bank );
216 while( p_libvlc_global->p_module_bank->i_children )
218 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
220 if( DeleteModule( p_next, true ) )
222 /* Module deletion failed */
223 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
224 p_next->psz_object_name );
226 /* We just free the module by hand. Niahahahahaha. */
227 vlc_object_detach( p_next );
228 vlc_object_release( p_next );
232 vlc_object_release( p_libvlc_global->p_module_bank );
233 p_libvlc_global->p_module_bank = NULL;
237 * Load all modules which we built with.
239 * Fills the module bank structure with the builtin modules.
240 * \param p_this vlc object structure
243 void __module_LoadBuiltins( vlc_object_t * p_this )
245 libvlc_global_data_t *p_libvlc_global = vlc_global();
247 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
248 if( p_libvlc_global->p_module_bank->b_builtins )
250 vlc_mutex_unlock( lock );
253 p_libvlc_global->p_module_bank->b_builtins = true;
254 vlc_mutex_unlock( lock );
256 msg_Dbg( p_this, "checking builtin modules" );
257 ALLOCATE_ALL_BUILTINS();
263 * Load all plugin modules we can find.
264 * Fills the module bank structure with the plugin modules.
265 * \param p_this vlc object structure
268 void __module_LoadPlugins( vlc_object_t * p_this )
270 #ifdef HAVE_DYNAMIC_PLUGINS
271 libvlc_global_data_t *p_libvlc_global = vlc_global();
273 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
274 if( p_libvlc_global->p_module_bank->b_plugins )
276 vlc_mutex_unlock( lock );
279 p_libvlc_global->p_module_bank->b_plugins = true;
280 vlc_mutex_unlock( lock );
282 msg_Dbg( p_this, "checking plugin modules" );
284 if( config_GetInt( p_this, "plugins-cache" ) )
285 p_libvlc_global->p_module_bank->b_cache = true;
287 if( p_libvlc_global->p_module_bank->b_cache ||
288 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
290 AllocateAllPlugins( p_this );
295 * Checks whether a module implements a capability.
297 * \param m the module
298 * \param cap the capability to check
299 * \return TRUE if the module have the capability
301 bool module_IsCapable( const module_t *m, const char *cap )
303 return !strcmp( m->psz_capability, cap );
307 * Get the internal name of a module
309 * \param m the module
310 * \return the module name
312 const char *module_GetObjName( const module_t *m )
314 return m->psz_object_name;
318 * Get the human-friendly name of a module.
320 * \param m the module
321 * \param long_name TRUE to have the long name of the module
322 * \return the short or long name of the module
324 const char *module_GetName( const module_t *m, bool long_name )
326 if( long_name && ( m->psz_longname != NULL) )
327 return m->psz_longname;
329 return m->psz_shortname ?: m->psz_object_name;
333 * Get the help for a module
335 * \param m the module
338 const char *module_GetHelp( const module_t *m )
346 * Return the best module function, given a capability list.
347 * \param p_this the vlc object
348 * \param psz_capability list of capabilities needed
349 * \param psz_name name of the module asked
350 * \param b_strict TRUE yto use the strict mode
351 * \return the module or NULL in case of a failure
353 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
354 const char *psz_name, bool b_strict )
356 typedef struct module_list_t module_list_t;
358 stats_TimerStart( p_this, "module_Need()", STATS_TIMER_MODULE_NEED );
365 module_list_t *p_next;
368 module_list_t *p_list, *p_first, *p_tmp;
371 int i_which_module, i_index = 0;
376 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
377 bool b_force_backup = p_this->b_force;
380 /* Deal with variables */
381 if( psz_name && psz_name[0] == '$' )
384 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
385 var_Get( p_this, psz_name + 1, &val );
386 psz_var = val.psz_string;
390 /* Count how many different shortcuts were asked for */
391 if( psz_name && *psz_name )
393 char *psz_parser, *psz_last_shortcut;
395 /* If the user wants none, give him none. */
396 if( !strcmp( psz_name, "none" ) )
399 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
400 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
401 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
406 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
408 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
410 if( *psz_parser == ',' )
414 psz_last_shortcut = psz_parser + 1;
418 /* Check if the user wants to override the "strict" mode */
419 if( psz_last_shortcut )
421 if( !strcmp(psz_last_shortcut, "none") )
426 else if( !strcmp(psz_last_shortcut, "any") )
434 /* Sort the modules and test them */
435 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
436 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
438 unsigned i_cpu = vlc_CPU();
440 /* Parse the module list for capabilities and probe each of them */
441 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
443 int i_shortcut_bonus = 0;
445 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
447 /* Test that this module can do what we need */
448 if( !module_IsCapable( p_module, psz_capability ) )
450 /* Don't recurse through the sub-modules because vlc_list_find()
451 * will list them anyway. */
455 /* Test if we have the required CPU */
456 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
461 /* If we required a shortcut, check this plugin provides it. */
462 if( i_shortcuts > 0 )
465 const char *psz_name = psz_shortcuts;
467 /* Let's drop modules with a <= 0 score (unless they are
468 * explicitly requested) */
469 b_trash = p_module->i_score <= 0;
471 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
473 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
476 if( ( c = strchr( psz_name, '@' ) )
477 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
479 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
484 i_shortcut_bonus = i_short * 10000;
489 /* Go to the next shortcut... This is so lame! */
490 psz_name += strlen( psz_name ) + 1;
493 /* If we are in "strict" mode and we couldn't
494 * find the module in the list of provided shortcuts,
495 * then kick the bastard out of here!!! */
499 /* If we didn't require a shortcut, trash <= 0 scored plugins */
500 else if( p_module->i_score <= 0 )
507 /* Store this new module */
508 p_list[ i_index ].p_module = p_module;
509 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
510 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
512 /* Add it to the modules-to-probe list */
515 p_list[ 0 ].p_next = NULL;
520 /* Ok, so at school you learned that quicksort is quick, and
521 * bubble sort sucks raw eggs. But that's when dealing with
522 * thousands of items. Here we have barely 50. */
523 module_list_t *p_newlist = p_first;
525 if( p_first->i_score < p_list[ i_index ].i_score )
527 p_list[ i_index ].p_next = p_first;
528 p_first = &p_list[ i_index ];
532 while( p_newlist->p_next != NULL &&
533 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
535 p_newlist = p_newlist->p_next;
538 p_list[ i_index ].p_next = p_newlist->p_next;
539 p_newlist->p_next = &p_list[ i_index ];
546 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
547 i_index, i_index == 1 ? "" : "s" );
549 /* Lock all candidate modules */
551 while( p_tmp != NULL )
553 vlc_object_yield( p_tmp->p_module );
554 p_tmp = p_tmp->p_next;
557 /* We can release the list, interesting modules were yielded */
558 vlc_list_release( p_all );
560 /* Parse the linked list and use the first successful module */
562 while( p_tmp != NULL )
564 #ifdef HAVE_DYNAMIC_PLUGINS
565 /* Make sure the module is loaded in mem */
566 module_t *p_module = p_tmp->p_module;
567 if( p_module->b_submodule )
568 p_module = (module_t *)p_module->p_parent;
570 if( !p_module->b_builtin && !p_module->b_loaded )
572 module_t *p_new_module =
573 AllocatePlugin( p_this, p_module->psz_filename );
576 CacheMerge( p_this, p_module, p_new_module );
577 vlc_object_attach( p_new_module, p_module );
578 DeleteModule( p_new_module, true );
583 p_this->b_force = p_tmp->b_force;
584 if( p_tmp->p_module->pf_activate
585 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
590 vlc_object_release( p_tmp->p_module );
591 p_tmp = p_tmp->p_next;
594 /* Store the locked module value */
597 p_module = p_tmp->p_module;
598 p_tmp = p_tmp->p_next;
605 /* Unlock the remaining modules */
606 while( p_tmp != NULL )
608 vlc_object_release( p_tmp->p_module );
609 p_tmp = p_tmp->p_next;
613 p_this->b_force = b_force_backup;
615 if( p_module != NULL )
617 msg_Dbg( p_this, "using %s module \"%s\"",
618 psz_capability, p_module->psz_object_name );
620 else if( p_first == NULL )
622 if( !strcmp( psz_capability, "access_demux" ) )
624 msg_Warn( p_this, "no %s module matched \"%s\"",
625 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
629 msg_Err( p_this, "no %s module matched \"%s\"",
630 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
632 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
633 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
636 else if( psz_name != NULL && *psz_name )
638 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
639 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
642 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
644 if( p_module && !p_this->psz_object_name )
646 /* This assumes that p_this is the object which will be using the
647 * module. That's not always the case ... but it is in most cases.
650 p_this->psz_object_name = strdup( psz_alias );
652 p_this->psz_object_name = strdup( p_module->psz_object_name );
655 free( psz_shortcuts );
658 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
659 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
660 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
662 /* Don't forget that the module is still locked */
669 * This function must be called by the thread that called module_Need, to
670 * decrease the reference count and allow for hiding of modules.
671 * \param p_this vlc object structure
672 * \param p_module the module structure
675 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
677 /* Use the close method */
678 if( p_module->pf_deactivate )
680 p_module->pf_deactivate( p_this );
683 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
685 vlc_object_release( p_module );
689 * Get a pointer to a module_t given it's name.
691 * \param p_this vlc object structure
692 * \param psz_name the name of the module
693 * \return a pointer to the module or NULL in case of a failure
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 );
718 * Release a module_t pointer from module_Find().
720 * \param module the module to release
723 void module_Put( module_t *module )
725 vlc_object_release( module );
730 * Tell if a module exists and release it in thic case
732 * \param p_this vlc object structure
733 * \param psz_name th name of the module
734 * \return TRUE if the module exists
736 bool __module_Exists( vlc_object_t *p_this, const char * psz_name )
738 module_t *p_module = __module_Find( p_this, psz_name );
741 module_Put( p_module );
751 * GetModuleNamesForCapability
753 * Return a NULL terminated array with the names of the modules
754 * that have a certain capability.
755 * Free after uses both the string and the table.
756 * \param p_this vlc object structure
757 * \param psz_capability the capability asked
758 * \param pppsz_longname an pointer to an array of string to contain
759 the long names of the modules. If set to NULL the function don't use it.
760 * \return the NULL terminated array
762 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
763 const char *psz_capability,
764 char ***pppsz_longname )
770 /* Do it in two passes : count the number of modules before */
771 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
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 ) )
780 psz_ret = malloc( sizeof(char*) * (count+1) );
782 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
783 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
786 free( *pppsz_longname );
787 *pppsz_longname = NULL;
788 vlc_list_release( p_list );
793 for( i = 0 ; i < p_list->i_count; i++)
795 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
796 const char *psz_module_capability = p_module->psz_capability;
797 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
799 int k = -1; /* hack to handle submodules properly */
800 if( p_module->b_submodule )
802 while( p_module->pp_shortcuts[++k] != NULL );
805 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
806 :p_module->psz_object_name );
808 (*pppsz_longname)[j] = strdup( module_GetName( p_module, true ) );
812 psz_ret[count] = NULL;
814 vlc_list_release( p_list );
820 * Get the configuration of a module
822 * \param module the module
823 * \param psize the size of the configuration returned
824 * \return the configuration as an array
826 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
829 unsigned size = module->confsize;
830 module_config_t *config = malloc( size * sizeof( *config ) );
832 assert( psize != NULL );
838 for( i = 0, j = 0; i < size; i++ )
840 const module_config_t *item = module->p_config + i;
841 if( item->b_internal /* internal option */
842 || item->b_unsaveable /* non-modifiable option */
843 || item->b_removed /* removed option */ )
846 memcpy( config + j, item, sizeof( *config ) );
855 * Release the configuration
857 * \param the configuration
860 void module_PutConfig( module_config_t *config )
865 /*****************************************************************************
866 * Following functions are local.
867 *****************************************************************************/
869 /*****************************************************************************
870 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
872 *****************************************************************************/
873 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
877 bool escaped = false;
881 /* Alloc a buffer to store the path */
882 path = malloc( strlen( paths ) + 1 );
883 if( !path ) return NULL;
885 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
886 for( i = 0, done = 0 ; paths[i]; i++ ) {
887 /* Take care of \\ and \: or \; escapement */
890 path[done++] = paths[i];
892 else if( paths[i] == '\\' )
894 else if( paths[i] == PATH_SEP_CHAR )
897 path[done++] = paths[i];
901 /* Return the remaining paths */
902 if( remaining_paths ) {
903 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
909 /*****************************************************************************
910 * AllocateAllPlugins: load all plugin modules we can find.
911 *****************************************************************************/
912 #ifdef HAVE_DYNAMIC_PLUGINS
913 static void AllocateAllPlugins( vlc_object_t *p_this )
917 vlc_array_t *arraypaths = vlc_array_new();
919 /* Contruct the special search path for system that have a relocatable
920 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
921 #define RETURN_ENOMEM \
923 msg_Err( p_this, "Not enough memory" ); \
927 vlc_array_append( arraypaths, strdup( "modules" ) );
928 #if defined( WIN32 ) || defined( UNDER_CE ) || defined( __APPLE__ ) || defined( SYS_BEOS )
929 if( asprintf( &path, "%s" DIR_SEP "modules",
930 vlc_global()->psz_vlcpath ) < 0 )
932 vlc_array_append( arraypaths, path );
933 if( asprintf( &path, "%s" DIR_SEP "plugins",
934 vlc_global()->psz_vlcpath ) < 0 )
936 vlc_array_append( arraypaths, path );
937 #if ! defined( WIN32 ) && ! defined( UNDER_CE )
938 if( asprintf( &path, "%s", PLUGIN_PATH ) < 0 )
940 vlc_array_append( arraypaths, path );
943 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
945 vlc_array_append( arraypaths, strdup( "plugins" ) );
947 /* If the user provided a plugin path, we add it to the list */
948 char * userpaths = config_GetPsz( p_this, "plugin-path" );
951 for( paths_iter = userpaths; paths_iter; )
953 path = copy_next_paths_token( paths_iter, &paths_iter );
956 vlc_array_append( arraypaths, strdup( path ) );
959 count = vlc_array_count( arraypaths );
960 for( i = 0 ; i < count ; i++ )
962 path = vlc_array_item_at_index( arraypaths, i );
968 msg_Dbg( p_this, "recursively browsing `%s'", path );
970 /* Don't go deeper than 5 subdirectories */
971 AllocatePluginDir( p_this, path, 5 );
976 vlc_array_destroy( arraypaths );
980 /*****************************************************************************
981 * AllocatePluginDir: recursively parse a directory to look for plugins
982 *****************************************************************************/
983 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
986 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
992 #if defined( UNDER_CE ) || defined( _MSC_VER )
994 wchar_t psz_wpath[MAX_PATH + 256];
995 wchar_t psz_wdir[MAX_PATH];
997 char psz_path[MAX_PATH + 256];
998 WIN32_FIND_DATA finddata;
1004 struct dirent * file;
1008 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1013 #if defined( UNDER_CE ) || defined( _MSC_VER )
1015 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1017 rc = GetFileAttributes( psz_wdir );
1018 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1020 /* Parse all files in the directory */
1021 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1023 rc = GetFileAttributes( psz_dir );
1024 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1027 /* Parse all files in the directory */
1028 sprintf( psz_path, "%s\\*", psz_dir );
1031 handle = FindFirstFile( psz_wpath, &finddata );
1033 handle = FindFirstFile( psz_path, &finddata );
1035 if( handle == INVALID_HANDLE_VALUE )
1037 /* Empty directory */
1041 /* Parse the directory and try to load all files it contains. */
1045 unsigned int i_len = wcslen( finddata.cFileName );
1046 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1047 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1049 unsigned int i_len = strlen( finddata.cFileName );
1050 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1053 /* Skip ".", ".." */
1054 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1055 || !strcmp( finddata.cFileName, ".." ) )
1057 if( !FindNextFile( handle, &finddata ) ) break;
1062 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1064 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1067 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1069 else if( i_len > strlen( LIBEXT )
1070 /* We only load files ending with LIBEXT */
1071 && !strncasecmp( psz_path + strlen( psz_path)
1073 LIBEXT, strlen( LIBEXT ) ) )
1075 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1076 int64_t i_time = 0, i_size = 0;
1079 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1082 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1086 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1088 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1089 i_size = attrbuf.nFileSizeHigh;
1091 i_size |= attrbuf.nFileSizeLow;
1093 psz_file = psz_path;
1095 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1098 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1100 /* Close the directory */
1101 FindClose( handle );
1104 dir = opendir( psz_dir );
1110 i_dirlen = strlen( psz_dir );
1112 /* Parse the directory and try to load all files it contains. */
1113 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1115 struct stat statbuf;
1119 /* Skip ".", ".." */
1120 if( !*file->d_name || !strcmp( file->d_name, "." )
1121 || !strcmp( file->d_name, ".." ) )
1126 i_len = strlen( file->d_name );
1127 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1128 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1130 i_stat = stat( psz_file, &statbuf );
1131 if( !i_stat && statbuf.st_mode & S_IFDIR )
1133 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1135 else if( i_len > strlen( LIBEXT )
1136 /* We only load files ending with LIBEXT */
1137 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1138 LIBEXT, strlen( LIBEXT ) ) )
1140 int64_t i_time = 0, i_size = 0;
1144 i_time = statbuf.st_mtime;
1145 i_size = statbuf.st_size;
1148 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1154 /* Close the directory */
1160 /*****************************************************************************
1161 * AllocatePluginFile: load a module into memory and initialize it.
1162 *****************************************************************************
1163 * This function loads a dynamically loadable module and allocates a structure
1164 * for its information data. The module can then be handled by module_Need
1165 * and module_Unneed. It can be removed by DeleteModule.
1166 *****************************************************************************/
1167 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1168 int64_t i_file_time, int64_t i_file_size )
1170 module_t * p_module = NULL;
1171 module_cache_t *p_cache_entry = NULL;
1174 * Check our plugins cache first then load plugin if needed
1177 CacheFind( psz_file, i_file_time, i_file_size );
1179 if( !p_cache_entry )
1181 p_module = AllocatePlugin( p_this, psz_file );
1185 /* If junk dll, don't try to load it */
1186 if( p_cache_entry->b_junk )
1192 module_config_t *p_item = NULL, *p_end = NULL;
1194 p_module = p_cache_entry->p_module;
1195 p_module->b_loaded = false;
1197 /* For now we force loading if the module's config contains
1198 * callbacks or actions.
1199 * Could be optimized by adding an API call.*/
1200 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1201 p_item < p_end; p_item++ )
1203 if( p_item->pf_callback || p_item->i_action )
1205 p_module = AllocatePlugin( p_this, psz_file );
1209 if( p_module == p_cache_entry->p_module )
1210 p_cache_entry->b_used = true;
1216 libvlc_global_data_t *p_libvlc_global = vlc_global();
1218 /* Everything worked fine !
1219 * The module is ready to be added to the list. */
1220 p_module->b_builtin = false;
1222 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1223 p_module->psz_object_name, p_module->psz_longname ); */
1225 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1227 if( !p_libvlc_global->p_module_bank->b_cache )
1230 #define p_bank p_libvlc_global->p_module_bank
1231 /* Add entry to cache */
1233 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1234 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1235 if( !p_bank->pp_cache[p_bank->i_cache] )
1237 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1238 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1239 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1240 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1241 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1242 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1246 return p_module ? 0 : -1;
1249 /*****************************************************************************
1250 * AllocatePlugin: load a module into memory and initialize it.
1251 *****************************************************************************
1252 * This function loads a dynamically loadable module and allocates a structure
1253 * for its information data. The module can then be handled by module_Need
1254 * and module_Unneed. It can be removed by DeleteModule.
1255 *****************************************************************************/
1256 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1258 module_t * p_module = NULL;
1259 module_handle_t handle;
1261 if( module_Load( p_this, psz_file, &handle ) )
1264 /* Now that we have successfully loaded the module, we can
1265 * allocate a structure for it */
1266 p_module = vlc_module_create( p_this );
1267 if( p_module == NULL )
1269 msg_Err( p_this, "out of memory" );
1270 module_Unload( handle );
1274 /* We need to fill these since they may be needed by module_Call() */
1275 p_module->psz_filename = psz_file;
1276 p_module->handle = handle;
1277 p_module->b_loaded = true;
1279 /* Initialize the module: fill p_module, default config */
1280 if( module_Call( p_module ) != 0 )
1282 /* We couldn't call module_init() */
1283 vlc_object_release( p_module );
1284 module_Unload( handle );
1288 DupModule( p_module );
1289 p_module->psz_filename = strdup( p_module->psz_filename );
1291 /* Everything worked fine ! The module is ready to be added to the list. */
1292 p_module->b_builtin = false;
1297 /*****************************************************************************
1298 * DupModule: make a plugin module standalone.
1299 *****************************************************************************
1300 * This function duplicates all strings in the module, so that the dynamic
1301 * object can be unloaded. It acts recursively on submodules.
1302 *****************************************************************************/
1303 static void DupModule( module_t *p_module )
1308 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1310 *pp_shortcut = strdup( *pp_shortcut );
1313 /* We strdup() these entries so that they are still valid when the
1314 * module is unloaded. */
1315 p_module->psz_capability = strdup( p_module->psz_capability );
1316 p_module->psz_shortname = p_module->psz_shortname ?
1317 strdup( p_module->psz_shortname ) : NULL;
1318 p_module->psz_longname = strdup( p_module->psz_longname );
1319 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1322 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1324 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1328 /*****************************************************************************
1329 * UndupModule: free a duplicated module.
1330 *****************************************************************************
1331 * This function frees the allocations done in DupModule().
1332 *****************************************************************************/
1333 static void UndupModule( module_t *p_module )
1338 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1340 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1343 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1345 free( *pp_shortcut );
1348 FREENULL( p_module->psz_object_name );
1349 free( p_module->psz_capability );
1350 free( p_module->psz_shortname );
1351 free( p_module->psz_longname );
1352 free( p_module->psz_help );
1355 #endif /* HAVE_DYNAMIC_PLUGINS */
1357 /*****************************************************************************
1358 * AllocateBuiltinModule: initialize a builtin module.
1359 *****************************************************************************
1360 * This function registers a builtin module and allocates a structure
1361 * for its information data. The module can then be handled by module_Need
1362 * and module_Unneed. It can be removed by DeleteModule.
1363 *****************************************************************************/
1364 static int AllocateBuiltinModule( vlc_object_t * p_this,
1365 int ( *pf_entry ) ( module_t * ) )
1367 module_t * p_module;
1369 /* Now that we have successfully loaded the module, we can
1370 * allocate a structure for it */
1371 p_module = vlc_module_create( p_this );
1372 if( p_module == NULL )
1374 msg_Err( p_this, "out of memory" );
1378 /* Initialize the module : fill p_module->psz_object_name, etc. */
1379 if( pf_entry( p_module ) != 0 )
1381 /* With a well-written module we shouldn't have to print an
1382 * additional error message here, but just make sure. */
1383 msg_Err( p_this, "failed calling entry point in builtin module" );
1384 vlc_object_release( p_module );
1388 /* Everything worked fine ! The module is ready to be added to the list. */
1389 p_module->b_builtin = true;
1391 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1392 p_module->psz_object_name, p_module->psz_longname ); */
1394 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1399 /*****************************************************************************
1400 * DeleteModule: delete a module and its structure.
1401 *****************************************************************************
1402 * This function can only be called if the module isn't being used.
1403 *****************************************************************************/
1404 static int DeleteModule( module_t * p_module, bool b_detach )
1406 if( !p_module ) return VLC_EGENERIC;
1408 vlc_object_detach( p_module );
1410 /* We free the structures that we strdup()ed in Allocate*Module(). */
1411 #ifdef HAVE_DYNAMIC_PLUGINS
1412 if( !p_module->b_builtin )
1414 if( p_module->b_loaded && p_module->b_unloadable )
1416 module_Unload( p_module->handle );
1418 UndupModule( p_module );
1419 free( p_module->psz_filename );
1423 /* Free and detach the object's children */
1424 while( p_module->i_children )
1426 vlc_object_t *p_this = p_module->pp_children[0];
1427 vlc_object_detach( p_this );
1428 vlc_object_release( p_this );
1431 config_Free( p_module );
1432 vlc_object_release( p_module );