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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
36 * is set to 64. Don't try to be cleverer. */
37 #ifdef _FILE_OFFSET_BITS
38 #undef _FILE_OFFSET_BITS
41 #include <stdlib.h> /* free(), strtol() */
42 #include <stdio.h> /* sprintf() */
43 #include <string.h> /* strdup() */
50 #ifdef HAVE_SYS_TYPES_H
51 # include <sys/types.h>
53 #ifdef HAVE_SYS_STAT_H
54 # include <sys/stat.h>
60 #if !defined(HAVE_DYNAMIC_PLUGINS)
61 /* no support for plugins */
62 #elif defined(HAVE_DL_DYLD)
63 # if defined(HAVE_MACH_O_DYLD_H)
64 # include <mach-o/dyld.h>
66 #elif defined(HAVE_DL_BEOS)
67 # if defined(HAVE_IMAGE_H)
70 #elif defined(HAVE_DL_WINDOWS)
72 #elif defined(HAVE_DL_DLOPEN)
73 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
76 # if defined(HAVE_SYS_DL_H)
79 #elif defined(HAVE_DL_SHL_LOAD)
80 # if defined(HAVE_DL_H)
85 #include "config/configuration.h"
87 #include "vlc_charset.h"
88 #include "vlc_arrays.h"
90 #include "modules/modules.h"
92 static module_bank_t *p_module_bank = NULL;
93 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
95 int vlc_entry__main( module_t * );
97 /*****************************************************************************
99 *****************************************************************************/
100 #ifdef HAVE_DYNAMIC_PLUGINS
101 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
102 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
104 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
106 static module_t * AllocatePlugin( vlc_object_t *, const char * );
108 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
109 static void DeleteModule ( module_bank_t *, module_t * );
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 static void DupModule ( module_t * );
112 static void UndupModule ( module_t * );
118 * Creates a module bank structure which will be filled later
119 * on with all the modules found.
120 * \param p_this vlc object structure
123 void __module_InitBank( vlc_object_t *p_this )
125 module_bank_t *p_bank = NULL;
127 vlc_mutex_lock( &module_lock );
129 if( p_module_bank == NULL )
131 p_bank = calloc (1, sizeof(*p_bank));
133 p_bank->i_cache = p_bank->i_loaded_cache = 0;
134 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
135 p_bank->b_cache = p_bank->b_cache_dirty = false;
138 /* Everything worked, attach the object */
139 p_module_bank = p_bank;
141 /* Fills the module bank structure with the main module infos.
142 * This is very useful as it will allow us to consider the main
143 * library just as another module, and for instance the configuration
144 * options of main will be available in the module bank structure just
145 * as for every other module. */
146 AllocateBuiltinModule( p_this, vlc_entry__main );
149 p_module_bank->i_usage++;
151 /* We do retain the module bank lock until the plugins are loaded as well.
152 * This is ugly, this staged loading approach is needed: LibVLC gets
153 * some configuration parameters relevant to loading the plugins from
154 * the main (builtin) module. The module bank becomes shared read-only data
155 * once it is ready, so we need to fully serialize initialization.
156 * DO NOT UNCOMMENT the following line unless you managed to squeeze
157 * module_LoadPlugins() before you unlock the mutex. */
158 /*vlc_mutex_unlock( &module_lock );*/
161 #undef module_EndBank
163 * Unloads all unused plugin modules and empties the module
164 * bank in case of success.
165 * \param p_this vlc object structure
168 void module_EndBank( vlc_object_t *p_this, bool b_plugins )
170 module_bank_t *p_bank = p_module_bank;
172 assert (p_bank != NULL);
174 /* Save the configuration */
175 if( !config_GetInt( p_this, "ignore-config" ) )
176 config_AutoSaveConfigFile( p_this );
178 /* If plugins were _not_ loaded, then the caller still has the bank lock
179 * from module_InitBank(). */
181 vlc_mutex_lock( &module_lock );
183 vlc_assert_locked( &module_lock ); not for static mutexes :( */
185 if( --p_bank->i_usage > 0 )
187 vlc_mutex_unlock( &module_lock );
190 p_module_bank = NULL;
191 vlc_mutex_unlock( &module_lock );
193 #ifdef HAVE_DYNAMIC_PLUGINS
194 if( p_bank->b_cache )
195 CacheSave( p_this, p_bank );
196 while( p_bank->i_loaded_cache-- )
198 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
200 DeleteModule( p_bank,
201 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
202 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
203 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
204 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
207 if( p_bank->pp_loaded_cache )
209 free( p_bank->pp_loaded_cache );
210 p_bank->pp_loaded_cache = NULL;
212 while( p_bank->i_cache-- )
214 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
215 free( p_bank->pp_cache[p_bank->i_cache] );
216 p_bank->pp_cache[p_bank->i_cache] = NULL;
218 if( p_bank->pp_cache )
220 free( p_bank->pp_cache );
221 p_bank->pp_cache = NULL;
225 while( p_bank->head != NULL )
226 DeleteModule( p_bank, p_bank->head );
231 #undef module_LoadPlugins
233 * Loads module descriptions for all available plugins.
234 * Fills the module bank structure with the plugin modules.
236 * \param p_this vlc object structure
239 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
241 module_bank_t *p_bank = p_module_bank;
244 /*vlc_assert_locked( &module_lock ); not for static mutexes :( */
246 #ifdef HAVE_DYNAMIC_PLUGINS
247 if( p_bank->i_usage == 1 )
249 msg_Dbg( p_this, "checking plugin modules" );
250 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
252 if( p_module_bank->b_cache || b_cache_delete )
253 CacheLoad( p_this, p_module_bank, b_cache_delete );
254 AllocateAllPlugins( p_this, p_module_bank );
257 p_module_bank->b_plugins = true;
258 vlc_mutex_unlock( &module_lock );
262 * Checks whether a module implements a capability.
264 * \param m the module
265 * \param cap the capability to check
266 * \return TRUE if the module have the capability
268 bool module_provides( const module_t *m, const char *cap )
270 return !strcmp( m->psz_capability, cap );
274 * Get the internal name of a module
276 * \param m the module
277 * \return the module name
279 const char *module_get_object( const module_t *m )
281 return m->psz_object_name;
285 * Get the human-friendly name of a module.
287 * \param m the module
288 * \param long_name TRUE to have the long name of the module
289 * \return the short or long name of the module
291 const char *module_get_name( const module_t *m, bool long_name )
293 if( long_name && ( m->psz_longname != NULL) )
294 return m->psz_longname;
296 return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
300 * Get the help for a module
302 * \param m the module
305 const char *module_get_help( const module_t *m )
311 * Get the capability for a module
313 * \param m the module
314 * return the capability
316 const char *module_get_capability( const module_t *m )
318 return m->psz_capability;
322 * Get the score for a module
324 * \param m the module
325 * return the score for the capability
327 int module_get_score( const module_t *m )
332 module_t *module_hold (module_t *m)
334 vlc_hold (&m->vlc_gc_data);
338 void module_release (module_t *m)
340 vlc_release (&m->vlc_gc_data);
344 * Frees the flat list of VLC modules.
345 * @param list list obtained by module_list_get()
346 * @param length number of items on the list
349 void module_list_free (module_t **list)
354 for (size_t i = 0; list[i] != NULL; i++)
355 module_release (list[i]);
360 * Gets the flat list of VLC modules.
361 * @param n [OUT] pointer to the number of modules or NULL
362 * @return NULL-terminated table of module pointers
363 * (release with module_list_free()), or NULL in case of error.
365 module_t **module_list_get (size_t *n)
367 /* TODO: this whole module lookup is quite inefficient */
368 /* Remove this and improve module_need */
369 module_t **tab = NULL;
372 assert (p_module_bank);
373 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
376 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
379 module_list_free (tab);
384 tab[i++] = module_hold (mod);
385 for (module_t *subm = mod->submodule; subm; subm = subm->next)
386 tab[i++] = module_hold (subm);
394 typedef struct module_list_t
401 static int modulecmp (const void *a, const void *b)
403 const module_list_t *la = a, *lb = b;
404 /* Note that qsort() uses _ascending_ order,
405 * so the smallest module is the one with the biggest score. */
406 return lb->i_score - la->i_score;
412 * Return the best module function, given a capability list.
414 * If the p_this object doesn't have it's psz_object_name set, then
415 * psz_object_name will be set to the module's name, unless the user
416 * provided an alias using the "module name@alias" syntax in which case
417 * psz_object_name will be set to the alias.
419 * \param p_this the vlc object
420 * \param psz_capability list of capabilities needed
421 * \param psz_name name of the module asked
422 * \param b_strict TRUE yto use the strict mode
423 * \return the module or NULL in case of a failure
425 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
426 const char *psz_name, bool b_strict )
428 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
430 module_list_t *p_list;
433 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
434 bool b_force_backup = p_this->b_force;
436 /* Deal with variables */
437 if( psz_name && psz_name[0] == '$' )
439 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
442 /* Count how many different shortcuts were asked for */
443 if( psz_name && *psz_name )
445 char *psz_parser, *psz_last_shortcut;
447 /* If the user wants none, give him none. */
448 if( !strcmp( psz_name, "none" ) )
451 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
452 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
453 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
458 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
460 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
462 if( *psz_parser == ',' )
466 psz_last_shortcut = psz_parser + 1;
470 /* Check if the user wants to override the "strict" mode */
471 if( psz_last_shortcut )
473 if( !strcmp(psz_last_shortcut, "none") )
478 else if( !strcmp(psz_last_shortcut, "any") )
486 /* Sort the modules and test them */
488 module_t **p_all = module_list_get (&count);
489 p_list = malloc( count * sizeof( module_list_t ) );
490 unsigned i_cpu = vlc_CPU();
492 /* Parse the module list for capabilities and probe each of them */
494 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
496 bool b_shortcut_bonus = false;
498 /* Test that this module can do what we need */
499 if( !module_provides( p_module, psz_capability ) )
501 /* Test if we have the required CPU */
502 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
505 /* If we required a shortcut, check this plugin provides it. */
506 if( i_shortcuts > 0 )
508 const char *psz_name = psz_shortcuts;
510 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
512 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
515 if( ( c = strchr( psz_name, '@' ) )
516 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
518 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
523 b_shortcut_bonus = true;
528 /* Go to the next shortcut... This is so lame! */
529 psz_name += strlen( psz_name ) + 1;
532 /* If we are in "strict" mode and we couldn't
533 * find the module in the list of provided shortcuts,
534 * then kick the bastard out of here!!! */
539 /* Trash <= 0 scored plugins (they can only be selected by shortcut) */
540 if( p_module->i_score <= 0 )
544 /* Store this new module */
545 p_list[count].p_module = module_hold (p_module);
546 p_list[count].i_score = p_module->i_score;
547 if( b_shortcut_bonus )
548 p_list[count].i_score += 10000;
549 p_list[count].b_force = b_shortcut_bonus && b_strict;
553 /* We can release the list, interesting modules are held */
554 module_list_free (p_all);
556 /* Sort candidates by descending score */
557 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
558 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
559 count, count == 1 ? "" : "s" );
561 /* Parse the linked list and use the first successful module */
563 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
565 module_t *p_cand = p_list[i].p_module;
566 #ifdef HAVE_DYNAMIC_PLUGINS
567 /* Make sure the module is loaded in mem */
568 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
570 if( !p_real->b_builtin && !p_real->b_loaded )
572 module_t *p_new_module =
573 AllocatePlugin( p_this, p_real->psz_filename );
576 CacheMerge( p_this, p_real, p_new_module );
577 DeleteModule( p_module_bank, p_new_module );
582 p_this->b_force = p_list[i].b_force;
583 if( p_cand->pf_activate
584 && p_cand->pf_activate( p_this ) == VLC_SUCCESS )
587 /* Release the remaining modules */
589 module_release (p_list[i].p_module);
592 module_release( p_cand );
596 p_this->b_force = b_force_backup;
598 if( p_module != NULL )
600 msg_Dbg( p_this, "using %s module \"%s\"",
601 psz_capability, p_module->psz_object_name );
602 if( !p_this->psz_object_name )
604 /* This assumes that p_this is the object which will be using the
605 * module. That's not always the case ... but it is in most cases.
608 p_this->psz_object_name = strdup( psz_alias );
610 p_this->psz_object_name = strdup( p_module->psz_object_name );
613 else if( count == 0 )
615 if( !strcmp( psz_capability, "access_demux" )
616 || !strcmp( psz_capability, "stream_filter" )
617 || !strcmp( psz_capability, "vout_window" ) )
619 msg_Dbg( p_this, "no %s module matched \"%s\"",
620 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
624 msg_Err( p_this, "no %s module matched \"%s\"",
625 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
627 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
628 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
631 else if( psz_name != NULL && *psz_name )
633 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
634 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
637 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
639 free( psz_shortcuts );
642 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
643 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
644 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
646 /* Don't forget that the module is still locked */
653 * This function must be called by the thread that called module_need, to
654 * decrease the reference count and allow for hiding of modules.
655 * \param p_this vlc object structure
656 * \param p_module the module structure
659 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
661 /* Use the close method */
662 if( p_module->pf_deactivate )
664 p_module->pf_deactivate( p_this );
667 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
669 module_release( p_module );
673 * Get a pointer to a module_t given it's name.
675 * \param psz_name the name of the module
676 * \return a pointer to the module or NULL in case of a failure
678 module_t *module_find( const char * psz_name )
680 module_t **list, *module;
682 list = module_list_get (NULL);
686 for (size_t i = 0; (module = list[i]) != NULL; i++)
688 const char *psz_module_name = module->psz_object_name;
690 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
692 module_hold (module);
696 module_list_free (list);
701 * Tell if a module exists and release it in thic case
703 * \param psz_name th name of the module
704 * \return TRUE if the module exists
706 bool module_exists (const char * psz_name)
708 module_t *p_module = module_find (psz_name);
710 module_release (p_module);
711 return p_module != NULL;
715 * Get a pointer to a module_t that matches a shortcut.
716 * This is a temporary hack for SD. Do not re-use (generally multiple modules
717 * can have the same shortcut, so this is *broken* - use module_need()!).
719 * \param psz_shortcut shortcut of the module
720 * \param psz_cap capability of the module
721 * \return a pointer to the module or NULL in case of a failure
723 module_t *module_find_by_shortcut (const char *psz_shortcut)
725 module_t **list, *module;
727 list = module_list_get (NULL);
731 for (size_t i = 0; (module = list[i]) != NULL; i++)
734 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
737 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
739 module_hold (module);
745 module_list_free (list);
750 * GetModuleNamesForCapability
752 * Return a NULL terminated array with the names of the modules
753 * that have a certain capability.
754 * Free after uses both the string and the table.
755 * \param psz_capability the capability asked
756 * \param pppsz_longname an pointer to an array of string to contain
757 the long names of the modules. If set to NULL the function don't use it.
758 * \return the NULL terminated array
760 char ** module_GetModulesNamesForCapability( const char *psz_capability,
761 char ***pppsz_longname )
766 module_t **list = module_list_get (NULL);
768 /* Proceed in two passes: count the number of modules first */
769 for (size_t i = 0; list[i]; i++)
771 module_t *p_module = list[i];
772 const char *psz_module_capability = p_module->psz_capability;
774 if( psz_module_capability
775 && !strcmp( psz_module_capability, psz_capability ) )
779 /* Then get the names */
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 ) )
788 free( *pppsz_longname );
789 *pppsz_longname = NULL;
791 module_list_free (list);
795 for (size_t i = 0, j = 0; list[i]; i++)
797 module_t *p_module = list[i];
798 const char *psz_module_capability = p_module->psz_capability;
800 if( psz_module_capability
801 && !strcmp( psz_module_capability, psz_capability ) )
803 /* Explicit hack: Use the last shortcut. It _should_ be
804 * different from the object name, at least if the object
805 * contains multiple submodules with the same capability. */
807 while( p_module->pp_shortcuts[k] != NULL )
809 assert( k > 0); /* pp_shortcuts[0] is always set */
810 psz_ret[j] = strdup( p_module->pp_shortcuts[k - 1] );
812 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
816 psz_ret[count] = NULL;
818 module_list_free (list);
824 * Get the configuration of a module
826 * \param module the module
827 * \param psize the size of the configuration returned
828 * \return the configuration as an array
830 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
833 unsigned size = module->confsize;
834 module_config_t *config = malloc( size * sizeof( *config ) );
836 assert( psize != NULL );
842 for( i = 0, j = 0; i < size; i++ )
844 const module_config_t *item = module->p_config + i;
845 if( item->b_internal /* internal option */
846 || item->b_unsaveable /* non-modifiable option */
847 || item->b_removed /* removed option */ )
850 memcpy( config + j, item, sizeof( *config ) );
859 * Release the configuration
861 * \param the configuration
864 void module_config_free( module_config_t *config )
869 /*****************************************************************************
870 * Following functions are local.
871 *****************************************************************************/
873 /*****************************************************************************
874 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
876 *****************************************************************************/
877 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
881 bool escaped = false;
885 /* Alloc a buffer to store the path */
886 path = malloc( strlen( paths ) + 1 );
887 if( !path ) return NULL;
889 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
890 for( i = 0, done = 0 ; paths[i]; i++ )
892 /* Take care of \\ and \: or \; escapement */
896 path[done++] = paths[i];
899 else if( paths[i] == '/' )
902 else if( paths[i] == '\\' )
905 else if( paths[i] == PATH_SEP_CHAR )
908 path[done++] = paths[i];
912 /* Return the remaining paths */
913 if( remaining_paths ) {
914 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
920 char *psz_vlcpath = NULL;
922 /*****************************************************************************
923 * AllocateAllPlugins: load all plugin modules we can find.
924 *****************************************************************************/
925 #ifdef HAVE_DYNAMIC_PLUGINS
926 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
928 const char *vlcpath = psz_vlcpath;
931 vlc_array_t *arraypaths = vlc_array_new();
933 /* Contruct the special search path for system that have a relocatable
934 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
936 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
937 vlc_array_append( arraypaths, path );
938 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
939 vlc_array_append( arraypaths, path );
941 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
944 /* If the user provided a plugin path, we add it to the list */
945 char *userpaths = config_GetPsz( p_this, "plugin-path" );
948 for( paths_iter = userpaths; paths_iter; )
950 path = copy_next_paths_token( paths_iter, &paths_iter );
952 vlc_array_append( arraypaths, path );
955 count = vlc_array_count( arraypaths );
956 for( i = 0 ; i < count ; i++ )
958 path = vlc_array_item_at_index( arraypaths, i );
962 msg_Dbg( p_this, "recursively browsing `%s'", path );
964 /* Don't go deeper than 5 subdirectories */
965 AllocatePluginDir( p_this, p_bank, path, 5 );
970 vlc_array_destroy( arraypaths );
974 /*****************************************************************************
975 * AllocatePluginDir: recursively parse a directory to look for plugins
976 *****************************************************************************/
977 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
978 const char *psz_dir, unsigned i_maxdepth )
980 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
986 #if defined( UNDER_CE ) || defined( _MSC_VER )
988 wchar_t psz_wpath[MAX_PATH + 256];
989 wchar_t psz_wdir[MAX_PATH];
991 char psz_path[MAX_PATH + 256];
992 WIN32_FIND_DATA finddata;
998 struct dirent * file;
1002 if( i_maxdepth == 0 )
1005 #if defined( UNDER_CE ) || defined( _MSC_VER )
1007 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1009 rc = GetFileAttributes( psz_wdir );
1010 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1012 /* Parse all files in the directory */
1013 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1015 rc = GetFileAttributes( psz_dir );
1016 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1019 /* Parse all files in the directory */
1020 sprintf( psz_path, "%s\\*", psz_dir );
1023 handle = FindFirstFile( psz_wpath, &finddata );
1025 handle = FindFirstFile( psz_path, &finddata );
1027 if( handle == INVALID_HANDLE_VALUE )
1029 /* Empty directory */
1033 /* Parse the directory and try to load all files it contains. */
1037 unsigned int i_len = wcslen( finddata.cFileName );
1038 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1039 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1041 unsigned int i_len = strlen( finddata.cFileName );
1042 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1045 /* Skip ".", ".." */
1046 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1047 || !strcmp( finddata.cFileName, ".." ) )
1049 if( !FindNextFile( handle, &finddata ) ) break;
1054 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1056 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1059 AllocatePluginDir( p_this, p_bank, psz_path, i_maxdepth - 1 );
1061 else if( i_len > strlen( LIBEXT )
1062 /* We only load files ending with LIBEXT */
1063 && !strncasecmp( psz_path + strlen( psz_path)
1065 LIBEXT, strlen( LIBEXT ) ) )
1067 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1068 int64_t i_time = 0, i_size = 0;
1071 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1074 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1078 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1080 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1081 i_size = attrbuf.nFileSizeHigh;
1083 i_size |= attrbuf.nFileSizeLow;
1085 psz_file = psz_path;
1087 AllocatePluginFile( p_this, p_bank, psz_file, i_time, i_size );
1090 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1092 /* Close the directory */
1093 FindClose( handle );
1096 dir = opendir( psz_dir );
1102 i_dirlen = strlen( psz_dir );
1104 /* Parse the directory and try to load all files it contains. */
1105 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1107 struct stat statbuf;
1111 /* Skip ".", ".." */
1112 if( !*file->d_name || !strcmp( file->d_name, "." )
1113 || !strcmp( file->d_name, ".." ) )
1118 i_len = strlen( file->d_name );
1119 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1120 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1122 i_stat = stat( psz_file, &statbuf );
1123 if( !i_stat && statbuf.st_mode & S_IFDIR )
1125 AllocatePluginDir( p_this, p_bank, psz_file, i_maxdepth - 1 );
1127 else if( i_len > strlen( LIBEXT )
1128 /* We only load files ending with LIBEXT */
1129 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1130 LIBEXT, strlen( LIBEXT ) ) )
1132 int64_t i_time = 0, i_size = 0;
1136 i_time = statbuf.st_mtime;
1137 i_size = statbuf.st_size;
1140 AllocatePluginFile( p_this, p_bank, psz_file, i_time, i_size );
1146 /* Close the directory */
1152 /*****************************************************************************
1153 * AllocatePluginFile: load a module into memory and initialize it.
1154 *****************************************************************************
1155 * This function loads a dynamically loadable module and allocates a structure
1156 * for its information data. The module can then be handled by module_need
1157 * and module_unneed. It can be removed by DeleteModule.
1158 *****************************************************************************/
1159 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
1160 const char *psz_file,
1161 int64_t i_file_time, int64_t i_file_size )
1163 module_t * p_module = NULL;
1164 module_cache_t *p_cache_entry = NULL;
1167 * Check our plugins cache first then load plugin if needed
1169 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
1170 if( !p_cache_entry )
1172 p_module = AllocatePlugin( p_this, psz_file );
1175 /* If junk dll, don't try to load it */
1176 if( p_cache_entry->b_junk )
1180 module_config_t *p_item = NULL, *p_end = NULL;
1182 p_module = p_cache_entry->p_module;
1183 p_module->b_loaded = false;
1185 /* For now we force loading if the module's config contains
1186 * callbacks or actions.
1187 * Could be optimized by adding an API call.*/
1188 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1189 p_item < p_end; p_item++ )
1191 if( p_item->pf_callback || p_item->i_action )
1193 p_module = AllocatePlugin( p_this, psz_file );
1197 if( p_module == p_cache_entry->p_module )
1198 p_cache_entry->b_used = true;
1201 if( p_module == NULL )
1204 /* Everything worked fine !
1205 * The module is ready to be added to the list. */
1206 p_module->b_builtin = false;
1208 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1209 p_module->psz_object_name, p_module->psz_longname ); */
1210 p_module->next = p_bank->head;
1211 p_bank->head = p_module;
1213 if( !p_module_bank->b_cache )
1216 /* Add entry to cache */
1217 module_cache_t **pp_cache = p_bank->pp_cache;
1219 pp_cache = realloc( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1220 if( pp_cache == NULL )
1222 pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1223 if( pp_cache[p_bank->i_cache] == NULL )
1225 pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1226 pp_cache[p_bank->i_cache]->i_time = i_file_time;
1227 pp_cache[p_bank->i_cache]->i_size = i_file_size;
1228 pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1229 pp_cache[p_bank->i_cache]->b_used = true;
1230 pp_cache[p_bank->i_cache]->p_module = p_module;
1231 p_bank->pp_cache = pp_cache;
1236 /*****************************************************************************
1237 * AllocatePlugin: load a module into memory and initialize it.
1238 *****************************************************************************
1239 * This function loads a dynamically loadable module and allocates a structure
1240 * for its information data. The module can then be handled by module_need
1241 * and module_unneed. It can be removed by DeleteModule.
1242 *****************************************************************************/
1243 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1245 module_t * p_module = NULL;
1246 module_handle_t handle;
1248 if( module_Load( p_this, psz_file, &handle ) )
1251 /* Now that we have successfully loaded the module, we can
1252 * allocate a structure for it */
1253 p_module = vlc_module_create( p_this );
1254 if( p_module == NULL )
1256 module_Unload( handle );
1260 p_module->psz_filename = strdup( psz_file );
1261 p_module->handle = handle;
1262 p_module->b_loaded = true;
1264 /* Initialize the module: fill p_module, default config */
1265 if( module_Call( p_this, p_module ) != 0 )
1267 /* We couldn't call module_init() */
1268 free( p_module->psz_filename );
1269 module_release( p_module );
1270 module_Unload( handle );
1274 DupModule( p_module );
1276 /* Everything worked fine ! The module is ready to be added to the list. */
1277 p_module->b_builtin = false;
1282 /*****************************************************************************
1283 * DupModule: make a plugin module standalone.
1284 *****************************************************************************
1285 * This function duplicates all strings in the module, so that the dynamic
1286 * object can be unloaded. It acts recursively on submodules.
1287 *****************************************************************************/
1288 static void DupModule( module_t *p_module )
1292 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1294 *pp_shortcut = strdup( *pp_shortcut );
1297 /* We strdup() these entries so that they are still valid when the
1298 * module is unloaded. */
1299 p_module->psz_capability = strdup( p_module->psz_capability );
1300 p_module->psz_shortname = p_module->psz_shortname ?
1301 strdup( p_module->psz_shortname ) : NULL;
1302 p_module->psz_longname = strdup( p_module->psz_longname );
1303 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1306 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1310 /*****************************************************************************
1311 * UndupModule: free a duplicated module.
1312 *****************************************************************************
1313 * This function frees the allocations done in DupModule().
1314 *****************************************************************************/
1315 static void UndupModule( module_t *p_module )
1319 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1322 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1324 free( *pp_shortcut );
1327 free( p_module->psz_capability );
1328 FREENULL( p_module->psz_shortname );
1329 free( p_module->psz_longname );
1330 FREENULL( p_module->psz_help );
1333 #endif /* HAVE_DYNAMIC_PLUGINS */
1335 /*****************************************************************************
1336 * AllocateBuiltinModule: initialize a builtin module.
1337 *****************************************************************************
1338 * This function registers a builtin module and allocates a structure
1339 * for its information data. The module can then be handled by module_need
1340 * and module_unneed. It can be removed by DeleteModule.
1341 *****************************************************************************/
1342 static int AllocateBuiltinModule( vlc_object_t * p_this,
1343 int ( *pf_entry ) ( module_t * ) )
1345 module_t * p_module;
1347 /* Now that we have successfully loaded the module, we can
1348 * allocate a structure for it */
1349 p_module = vlc_module_create( p_this );
1350 if( p_module == NULL )
1353 /* Initialize the module : fill p_module->psz_object_name, etc. */
1354 if( pf_entry( p_module ) != 0 )
1356 /* With a well-written module we shouldn't have to print an
1357 * additional error message here, but just make sure. */
1358 msg_Err( p_this, "failed calling entry point in builtin module" );
1359 module_release( p_module );
1363 /* Everything worked fine ! The module is ready to be added to the list. */
1364 p_module->b_builtin = true;
1366 p_module->next = p_module_bank->head;
1367 p_module_bank->head = p_module;
1370 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1371 p_module->psz_object_name, p_module->psz_longname ); */
1376 /*****************************************************************************
1377 * DeleteModule: delete a module and its structure.
1378 *****************************************************************************
1379 * This function can only be called if the module isn't being used.
1380 *****************************************************************************/
1381 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1385 /* Unlist the module (if it is in the list) */
1386 module_t **pp_self = &p_bank->head;
1387 while (*pp_self != NULL && *pp_self != p_module)
1388 pp_self = &((*pp_self)->next);
1390 *pp_self = p_module->next;
1392 /* We free the structures that we strdup()ed in Allocate*Module(). */
1393 #ifdef HAVE_DYNAMIC_PLUGINS
1394 if( !p_module->b_builtin )
1396 if( p_module->b_loaded && p_module->b_unloadable )
1398 module_Unload( p_module->handle );
1400 UndupModule( p_module );
1401 free( p_module->psz_filename );
1405 /* Free and detach the object's children */
1406 while (p_module->submodule)
1408 module_t *submodule = p_module->submodule;
1409 p_module->submodule = submodule->next;
1410 module_release (submodule);
1413 config_Free( p_module );
1414 module_release( p_module );