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"
91 #include "modules/builtin.h"
93 static module_bank_t *p_module_bank = NULL;
94 static vlc_mutex_t module_lock = VLC_STATIC_MUTEX;
96 /*****************************************************************************
98 *****************************************************************************/
99 #ifdef HAVE_DYNAMIC_PLUGINS
100 static void AllocateAllPlugins( vlc_object_t *, module_bank_t * );
101 static void AllocatePluginDir( vlc_object_t *, module_bank_t *, const char *,
103 static int AllocatePluginFile( vlc_object_t *, module_bank_t *, const char *,
105 static module_t * AllocatePlugin( vlc_object_t *, const char * );
107 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
108 static void DeleteModule ( module_bank_t *, module_t * );
109 #ifdef HAVE_DYNAMIC_PLUGINS
110 static void DupModule ( module_t * );
111 static void UndupModule ( module_t * );
117 * Creates a module bank structure which will be filled later
118 * on with all the modules found.
119 * \param p_this vlc object structure
122 void __module_InitBank( vlc_object_t *p_this )
124 module_bank_t *p_bank = NULL;
126 vlc_mutex_lock( &module_lock );
128 if( p_module_bank == NULL )
130 p_bank = calloc (1, sizeof(*p_bank));
132 p_bank->i_cache = p_bank->i_loaded_cache = 0;
133 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
134 p_bank->b_cache = p_bank->b_cache_dirty = false;
137 /* Everything worked, attach the object */
138 p_module_bank = p_bank;
140 /* Fills the module bank structure with the main module infos.
141 * This is very useful as it will allow us to consider the main
142 * library just as another module, and for instance the configuration
143 * options of main will be available in the module bank structure just
144 * as for every other module. */
145 AllocateBuiltinModule( p_this, vlc_entry__main );
148 p_module_bank->i_usage++;
150 vlc_mutex_unlock( &module_lock );
157 * Unloads all unused plugin modules and empties the module
158 * bank in case of success.
159 * \param p_this vlc object structure
162 void __module_EndBank( vlc_object_t *p_this )
164 module_bank_t *p_bank;
166 /* Save the configuration */
167 config_AutoSaveConfigFile( p_this );
169 vlc_mutex_lock( &module_lock );
170 p_bank = p_module_bank;
171 assert (p_bank != NULL);
172 if( --p_bank->i_usage > 0 )
174 vlc_mutex_unlock( &module_lock );
177 p_module_bank = NULL;
178 vlc_mutex_unlock( &module_lock );
180 #ifdef HAVE_DYNAMIC_PLUGINS
181 if( p_bank->b_cache )
182 CacheSave( p_this, p_bank );
183 while( p_bank->i_loaded_cache-- )
185 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
187 DeleteModule( p_bank,
188 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
189 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
190 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
191 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
194 if( p_bank->pp_loaded_cache )
196 free( p_bank->pp_loaded_cache );
197 p_bank->pp_loaded_cache = NULL;
199 while( p_bank->i_cache-- )
201 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
202 free( p_bank->pp_cache[p_bank->i_cache] );
203 p_bank->pp_cache[p_bank->i_cache] = NULL;
205 if( p_bank->pp_cache )
207 free( p_bank->pp_cache );
208 p_bank->pp_cache = NULL;
212 while( p_bank->head != NULL )
213 DeleteModule( p_bank, p_bank->head );
219 * Load all modules which we built with.
221 * Fills the module bank structure with the builtin modules.
222 * \param p_this vlc object structure
225 void __module_LoadBuiltins( vlc_object_t * p_this )
227 vlc_mutex_lock( &module_lock );
228 if( p_module_bank->b_builtins )
230 vlc_mutex_unlock( &module_lock );
233 p_module_bank->b_builtins = true;
234 vlc_mutex_unlock( &module_lock );
236 msg_Dbg( p_this, "checking builtin modules" );
237 /* FIXME: race here - do this under the lock!! */
238 ALLOCATE_ALL_BUILTINS();
241 #undef module_LoadPlugins
245 * Load all plugin modules we can find.
246 * Fills the module bank structure with the plugin modules.
247 * \param p_this vlc object structure
250 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
252 #ifdef HAVE_DYNAMIC_PLUGINS
253 vlc_mutex_lock( &module_lock );
254 if( p_module_bank->b_plugins )
256 vlc_mutex_unlock( &module_lock );
259 p_module_bank->b_plugins = true;
260 vlc_mutex_unlock( &module_lock );
262 msg_Dbg( p_this, "checking plugin modules" );
263 p_module_bank->b_cache = config_GetInt( p_this, "plugins-cache" ) > 0;
265 if( p_module_bank->b_cache || b_cache_delete )
266 CacheLoad( p_this, p_module_bank, b_cache_delete );
268 /* FIXME: race - do this under the lock */
269 AllocateAllPlugins( p_this, p_module_bank );
274 * Checks whether a module implements a capability.
276 * \param m the module
277 * \param cap the capability to check
278 * \return TRUE if the module have the capability
280 bool module_provides( const module_t *m, const char *cap )
282 return !strcmp( m->psz_capability, cap );
286 * Get the internal name of a module
288 * \param m the module
289 * \return the module name
291 const char *module_get_object( const module_t *m )
293 return m->psz_object_name;
297 * Get the human-friendly name of a module.
299 * \param m the module
300 * \param long_name TRUE to have the long name of the module
301 * \return the short or long name of the module
303 const char *module_get_name( const module_t *m, bool long_name )
305 if( long_name && ( m->psz_longname != NULL) )
306 return m->psz_longname;
308 return m->psz_shortname ?: m->psz_object_name;
312 * Get the help for a module
314 * \param m the module
317 const char *module_get_help( const module_t *m )
323 * Get the capability for a module
325 * \param m the module
326 * return the capability
328 const char *module_get_capability( const module_t *m )
330 return m->psz_capability;
334 * Get the score for a module
336 * \param m the module
337 * return the score for the capability
339 int module_get_score( const module_t *m )
344 module_t *module_hold (module_t *m)
346 vlc_hold (&m->vlc_gc_data);
350 void module_release (module_t *m)
352 vlc_release (&m->vlc_gc_data);
356 * Frees the flat list of VLC modules.
357 * @param list list obtained by module_list_get()
358 * @param length number of items on the list
361 void module_list_free (module_t **list)
366 for (size_t i = 0; list[i] != NULL; i++)
367 module_release (list[i]);
372 * Gets the flat list of VLC modules.
373 * @param n [OUT] pointer to the number of modules or NULL
374 * @return NULL-terminated table of module pointers
375 * (release with module_list_free()), or NULL in case of error.
377 module_t **module_list_get (size_t *n)
379 /* TODO: this whole module lookup is quite inefficient */
380 /* Remove this and improve module_need */
381 module_t **tab = NULL;
384 assert (p_module_bank);
385 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
388 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
391 module_list_free (tab);
396 tab[i++] = module_hold (mod);
397 for (module_t *subm = mod->submodule; subm; subm = subm->next)
398 tab[i++] = module_hold (subm);
406 typedef struct module_list_t
413 static int modulecmp (const void *a, const void *b)
415 const module_list_t *la = a, *lb = b;
416 /* Note that qsort() uses _ascending_ order,
417 * so the smallest module is the one with the biggest score. */
418 return lb->i_score - la->i_score;
424 * Return the best module function, given a capability list.
426 * If the p_this object doesn't have it's psz_object_name set, then
427 * psz_object_name will be set to the module's name, unless the user
428 * provided an alias using the "module name@alias" syntax in which case
429 * psz_object_name will be set to the alias.
431 * \param p_this the vlc object
432 * \param psz_capability list of capabilities needed
433 * \param psz_name name of the module asked
434 * \param b_strict TRUE yto use the strict mode
435 * \return the module or NULL in case of a failure
437 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
438 const char *psz_name, bool b_strict )
440 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
442 module_list_t *p_list;
445 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
446 bool b_force_backup = p_this->b_force;
448 /* Deal with variables */
449 if( psz_name && psz_name[0] == '$' )
451 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
454 /* Count how many different shortcuts were asked for */
455 if( psz_name && *psz_name )
457 char *psz_parser, *psz_last_shortcut;
459 /* If the user wants none, give him none. */
460 if( !strcmp( psz_name, "none" ) )
463 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
464 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
465 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
470 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
472 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
474 if( *psz_parser == ',' )
478 psz_last_shortcut = psz_parser + 1;
482 /* Check if the user wants to override the "strict" mode */
483 if( psz_last_shortcut )
485 if( !strcmp(psz_last_shortcut, "none") )
490 else if( !strcmp(psz_last_shortcut, "any") )
498 /* Sort the modules and test them */
500 module_t **p_all = module_list_get (&count);
501 p_list = malloc( count * sizeof( module_list_t ) );
502 unsigned i_cpu = vlc_CPU();
504 /* Parse the module list for capabilities and probe each of them */
506 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
508 bool b_shortcut_bonus = false;
510 /* Test that this module can do what we need */
511 if( !module_provides( p_module, psz_capability ) )
513 /* Test if we have the required CPU */
514 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
517 /* If we required a shortcut, check this plugin provides it. */
518 if( i_shortcuts > 0 )
520 const char *psz_name = psz_shortcuts;
522 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
524 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
527 if( ( c = strchr( psz_name, '@' ) )
528 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
530 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
535 b_shortcut_bonus = true;
540 /* Go to the next shortcut... This is so lame! */
541 psz_name += strlen( psz_name ) + 1;
544 /* If we are in "strict" mode and we couldn't
545 * find the module in the list of provided shortcuts,
546 * then kick the bastard out of here!!! */
550 /* If we didn't require a shortcut, trash <= 0 scored plugins */
551 else if( p_module->i_score <= 0 )
557 /* Store this new module */
558 p_list[count].p_module = module_hold (p_module);
559 p_list[count].i_score = p_module->i_score;
560 if( b_shortcut_bonus )
561 p_list[count].i_score += 10000;
562 p_list[count].b_force = b_shortcut_bonus && b_strict;
566 /* We can release the list, interesting modules are held */
567 module_list_free (p_all);
569 /* Sort candidates by descending score */
570 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
572 /* FIXME: Remove this hack after finding a general solution for %z's */
573 msg_Dbg( p_this, "looking for %s module: %u candidate%s", psz_capability,
574 count, count == 1 ? "" : "s" );
576 msg_Dbg( p_this, "looking for %s module: %zu candidate%s", psz_capability,
577 count, count == 1 ? "" : "s" );
580 /* Parse the linked list and use the first successful module */
582 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
584 module_t *p_cand = p_list[i].p_module;
585 #ifdef HAVE_DYNAMIC_PLUGINS
586 /* Make sure the module is loaded in mem */
587 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
589 if( !p_real->b_builtin && !p_real->b_loaded )
591 module_t *p_new_module =
592 AllocatePlugin( p_this, p_real->psz_filename );
595 CacheMerge( p_this, p_real, p_new_module );
596 DeleteModule( p_module_bank, p_new_module );
601 p_this->b_force = p_list[i].b_force;
602 if( p_cand->pf_activate
603 && p_cand->pf_activate( p_this ) == VLC_SUCCESS )
606 /* Release the remaining modules */
608 module_release (p_list[i].p_module);
611 module_release( p_cand );
615 p_this->b_force = b_force_backup;
617 if( p_module != NULL )
619 msg_Dbg( p_this, "using %s module \"%s\"",
620 psz_capability, p_module->psz_object_name );
621 if( !p_this->psz_object_name )
623 /* This assumes that p_this is the object which will be using the
624 * module. That's not always the case ... but it is in most cases.
627 p_this->psz_object_name = strdup( psz_alias );
629 p_this->psz_object_name = strdup( p_module->psz_object_name );
632 else if( count == 0 )
634 if( !strcmp( psz_capability, "access_demux" )
635 || !strcmp( psz_capability, "stream_filter" )
636 || !strcmp( psz_capability, "vout_window" ) )
638 msg_Dbg( p_this, "no %s module matched \"%s\"",
639 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
643 msg_Err( p_this, "no %s module matched \"%s\"",
644 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
646 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
647 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
650 else if( psz_name != NULL && *psz_name )
652 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
653 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
656 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
658 free( psz_shortcuts );
661 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
662 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
663 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
665 /* Don't forget that the module is still locked */
672 * This function must be called by the thread that called module_need, to
673 * decrease the reference count and allow for hiding of modules.
674 * \param p_this vlc object structure
675 * \param p_module the module structure
678 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
680 /* Use the close method */
681 if( p_module->pf_deactivate )
683 p_module->pf_deactivate( p_this );
686 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
688 module_release( p_module );
692 * Get a pointer to a module_t given it's name.
694 * \param psz_name the name of the module
695 * \return a pointer to the module or NULL in case of a failure
697 module_t *module_find( const char * psz_name )
699 module_t **list, *module;
701 list = module_list_get (NULL);
705 for (size_t i = 0; (module = list[i]) != NULL; i++)
707 const char *psz_module_name = module->psz_object_name;
709 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
711 module_hold (module);
715 module_list_free (list);
720 * Tell if a module exists and release it in thic case
722 * \param psz_name th name of the module
723 * \return TRUE if the module exists
725 bool module_exists (const char * psz_name)
727 module_t *p_module = module_find (psz_name);
729 module_release (p_module);
730 return p_module != NULL;
734 * Get a pointer to a module_t that matches a shortcut.
735 * This is a temporary hack for SD. Do not re-use (generally multiple modules
736 * can have the same shortcut, so this is *broken* - use module_need()!).
738 * \param psz_shortcut shortcut of the module
739 * \param psz_cap capability of the module
740 * \return a pointer to the module or NULL in case of a failure
742 module_t *module_find_by_shortcut (const char *psz_shortcut)
744 module_t **list, *module;
746 list = module_list_get (NULL);
750 for (size_t i = 0; (module = list[i]) != NULL; i++)
753 (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
756 if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
758 module_hold (module);
764 module_list_free (list);
769 * GetModuleNamesForCapability
771 * Return a NULL terminated array with the names of the modules
772 * that have a certain capability.
773 * Free after uses both the string and the table.
774 * \param psz_capability the capability asked
775 * \param pppsz_longname an pointer to an array of string to contain
776 the long names of the modules. If set to NULL the function don't use it.
777 * \return the NULL terminated array
779 char ** module_GetModulesNamesForCapability( const char *psz_capability,
780 char ***pppsz_longname )
785 module_t **list = module_list_get (NULL);
787 /* Do it in two passes : count the number of modules before */
788 for (size_t i = 0; list[i]; i++)
790 module_t *p_module = list[i];
791 const char *psz_module_capability = p_module->psz_capability;
793 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
797 psz_ret = malloc( sizeof(char*) * (count+1) );
799 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
800 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
805 free( *pppsz_longname );
806 *pppsz_longname = NULL;
808 module_list_free (list);
812 for (size_t i = 0, j = 0; list[i]; i++)
814 module_t *p_module = list[i];
815 const char *psz_module_capability = p_module->psz_capability;
817 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
819 int k = -1; /* hack to handle submodules properly */
820 if( p_module->b_submodule )
822 while( p_module->pp_shortcuts[++k] != NULL );
825 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
826 :p_module->psz_object_name );
828 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
832 psz_ret[count] = NULL;
834 module_list_free (list);
840 * Get the configuration of a module
842 * \param module the module
843 * \param psize the size of the configuration returned
844 * \return the configuration as an array
846 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
849 unsigned size = module->confsize;
850 module_config_t *config = malloc( size * sizeof( *config ) );
852 assert( psize != NULL );
858 for( i = 0, j = 0; i < size; i++ )
860 const module_config_t *item = module->p_config + i;
861 if( item->b_internal /* internal option */
862 || item->b_unsaveable /* non-modifiable option */
863 || item->b_removed /* removed option */ )
866 memcpy( config + j, item, sizeof( *config ) );
875 * Release the configuration
877 * \param the configuration
880 void module_config_free( module_config_t *config )
885 /*****************************************************************************
886 * Following functions are local.
887 *****************************************************************************/
889 /*****************************************************************************
890 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
892 *****************************************************************************/
893 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
897 bool escaped = false;
901 /* Alloc a buffer to store the path */
902 path = malloc( strlen( paths ) + 1 );
903 if( !path ) return NULL;
905 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
906 for( i = 0, done = 0 ; paths[i]; i++ )
908 /* Take care of \\ and \: or \; escapement */
912 path[done++] = paths[i];
915 else if( paths[i] == '/' )
918 else if( paths[i] == '\\' )
921 else if( paths[i] == PATH_SEP_CHAR )
924 path[done++] = paths[i];
928 /* Return the remaining paths */
929 if( remaining_paths ) {
930 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
936 char *psz_vlcpath = NULL;
938 /*****************************************************************************
939 * AllocateAllPlugins: load all plugin modules we can find.
940 *****************************************************************************/
941 #ifdef HAVE_DYNAMIC_PLUGINS
942 static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
944 const char *vlcpath = psz_vlcpath;
947 vlc_array_t *arraypaths = vlc_array_new();
949 /* Contruct the special search path for system that have a relocatable
950 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
952 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
953 vlc_array_append( arraypaths, path );
954 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
955 vlc_array_append( arraypaths, path );
957 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
960 /* If the user provided a plugin path, we add it to the list */
961 char *userpaths = config_GetPsz( p_this, "plugin-path" );
964 for( paths_iter = userpaths; paths_iter; )
966 path = copy_next_paths_token( paths_iter, &paths_iter );
968 vlc_array_append( arraypaths, path );
971 count = vlc_array_count( arraypaths );
972 for( i = 0 ; i < count ; i++ )
974 path = vlc_array_item_at_index( arraypaths, i );
978 msg_Dbg( p_this, "recursively browsing `%s'", path );
980 /* Don't go deeper than 5 subdirectories */
981 AllocatePluginDir( p_this, p_bank, path, 5 );
986 vlc_array_destroy( arraypaths );
990 /*****************************************************************************
991 * AllocatePluginDir: recursively parse a directory to look for plugins
992 *****************************************************************************/
993 static void AllocatePluginDir( vlc_object_t *p_this, module_bank_t *p_bank,
994 const char *psz_dir, unsigned i_maxdepth )
996 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
1002 #if defined( UNDER_CE ) || defined( _MSC_VER )
1004 wchar_t psz_wpath[MAX_PATH + 256];
1005 wchar_t psz_wdir[MAX_PATH];
1007 char psz_path[MAX_PATH + 256];
1008 WIN32_FIND_DATA finddata;
1014 struct dirent * file;
1018 if( i_maxdepth == 0 )
1021 #if defined( UNDER_CE ) || defined( _MSC_VER )
1023 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1025 rc = GetFileAttributes( psz_wdir );
1026 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1028 /* Parse all files in the directory */
1029 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1031 rc = GetFileAttributes( psz_dir );
1032 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1035 /* Parse all files in the directory */
1036 sprintf( psz_path, "%s\\*", psz_dir );
1039 handle = FindFirstFile( psz_wpath, &finddata );
1041 handle = FindFirstFile( psz_path, &finddata );
1043 if( handle == INVALID_HANDLE_VALUE )
1045 /* Empty directory */
1049 /* Parse the directory and try to load all files it contains. */
1053 unsigned int i_len = wcslen( finddata.cFileName );
1054 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1055 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1057 unsigned int i_len = strlen( finddata.cFileName );
1058 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1061 /* Skip ".", ".." */
1062 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1063 || !strcmp( finddata.cFileName, ".." ) )
1065 if( !FindNextFile( handle, &finddata ) ) break;
1070 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1072 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1075 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1077 else if( i_len > strlen( LIBEXT )
1078 /* We only load files ending with LIBEXT */
1079 && !strncasecmp( psz_path + strlen( psz_path)
1081 LIBEXT, strlen( LIBEXT ) ) )
1083 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1084 int64_t i_time = 0, i_size = 0;
1087 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1090 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1094 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1096 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1097 i_size = attrbuf.nFileSizeHigh;
1099 i_size |= attrbuf.nFileSizeLow;
1101 psz_file = psz_path;
1103 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1106 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1108 /* Close the directory */
1109 FindClose( handle );
1112 dir = opendir( psz_dir );
1118 i_dirlen = strlen( psz_dir );
1120 /* Parse the directory and try to load all files it contains. */
1121 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1123 struct stat statbuf;
1127 /* Skip ".", ".." */
1128 if( !*file->d_name || !strcmp( file->d_name, "." )
1129 || !strcmp( file->d_name, ".." ) )
1134 i_len = strlen( file->d_name );
1135 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1136 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1138 i_stat = stat( psz_file, &statbuf );
1139 if( !i_stat && statbuf.st_mode & S_IFDIR )
1141 AllocatePluginDir( p_this, p_bank, psz_file, i_maxdepth - 1 );
1143 else if( i_len > strlen( LIBEXT )
1144 /* We only load files ending with LIBEXT */
1145 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1146 LIBEXT, strlen( LIBEXT ) ) )
1148 int64_t i_time = 0, i_size = 0;
1152 i_time = statbuf.st_mtime;
1153 i_size = statbuf.st_size;
1156 AllocatePluginFile( p_this, p_bank, psz_file, i_time, i_size );
1162 /* Close the directory */
1168 /*****************************************************************************
1169 * AllocatePluginFile: load a module into memory and initialize it.
1170 *****************************************************************************
1171 * This function loads a dynamically loadable module and allocates a structure
1172 * for its information data. The module can then be handled by module_need
1173 * and module_unneed. It can be removed by DeleteModule.
1174 *****************************************************************************/
1175 static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
1176 const char *psz_file,
1177 int64_t i_file_time, int64_t i_file_size )
1179 module_t * p_module = NULL;
1180 module_cache_t *p_cache_entry = NULL;
1183 * Check our plugins cache first then load plugin if needed
1185 p_cache_entry = CacheFind( p_bank, psz_file, i_file_time, i_file_size );
1186 if( !p_cache_entry )
1188 p_module = AllocatePlugin( p_this, psz_file );
1192 /* If junk dll, don't try to load it */
1193 if( p_cache_entry->b_junk )
1199 module_config_t *p_item = NULL, *p_end = NULL;
1201 p_module = p_cache_entry->p_module;
1202 p_module->b_loaded = false;
1204 /* For now we force loading if the module's config contains
1205 * callbacks or actions.
1206 * Could be optimized by adding an API call.*/
1207 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1208 p_item < p_end; p_item++ )
1210 if( p_item->pf_callback || p_item->i_action )
1212 p_module = AllocatePlugin( p_this, psz_file );
1216 if( p_module == p_cache_entry->p_module )
1217 p_cache_entry->b_used = true;
1223 /* Everything worked fine !
1224 * The module is ready to be added to the list. */
1225 p_module->b_builtin = false;
1227 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1228 p_module->psz_object_name, p_module->psz_longname ); */
1229 p_module->next = p_bank->head;
1230 p_bank->head = p_module;
1232 if( !p_module_bank->b_cache )
1235 /* Add entry to cache */
1237 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1238 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1239 if( !p_bank->pp_cache[p_bank->i_cache] )
1241 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1242 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1243 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1244 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1245 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1246 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1250 return p_module ? 0 : -1;
1253 /*****************************************************************************
1254 * AllocatePlugin: load a module into memory and initialize it.
1255 *****************************************************************************
1256 * This function loads a dynamically loadable module and allocates a structure
1257 * for its information data. The module can then be handled by module_need
1258 * and module_unneed. It can be removed by DeleteModule.
1259 *****************************************************************************/
1260 static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
1262 module_t * p_module = NULL;
1263 module_handle_t handle;
1265 if( module_Load( p_this, psz_file, &handle ) )
1268 /* Now that we have successfully loaded the module, we can
1269 * allocate a structure for it */
1270 p_module = vlc_module_create( p_this );
1271 if( p_module == NULL )
1273 module_Unload( handle );
1277 /* We need to fill these since they may be needed by module_Call() */
1278 p_module->psz_filename = psz_file;
1279 p_module->handle = handle;
1280 p_module->b_loaded = true;
1282 /* Initialize the module: fill p_module, default config */
1283 if( module_Call( p_this, p_module ) != 0 )
1285 /* We couldn't call module_init() */
1286 module_release( p_module );
1287 module_Unload( handle );
1291 DupModule( p_module );
1292 p_module->psz_filename = strdup( p_module->psz_filename );
1294 /* Everything worked fine ! The module is ready to be added to the list. */
1295 p_module->b_builtin = false;
1300 /*****************************************************************************
1301 * DupModule: make a plugin module standalone.
1302 *****************************************************************************
1303 * This function duplicates all strings in the module, so that the dynamic
1304 * object can be unloaded. It acts recursively on submodules.
1305 *****************************************************************************/
1306 static void DupModule( module_t *p_module )
1310 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1312 *pp_shortcut = strdup( *pp_shortcut );
1315 /* We strdup() these entries so that they are still valid when the
1316 * module is unloaded. */
1317 p_module->psz_capability = strdup( p_module->psz_capability );
1318 p_module->psz_shortname = p_module->psz_shortname ?
1319 strdup( p_module->psz_shortname ) : NULL;
1320 p_module->psz_longname = strdup( p_module->psz_longname );
1321 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1324 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
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 )
1337 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1340 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1342 free( *pp_shortcut );
1345 free( p_module->psz_capability );
1346 FREENULL( p_module->psz_shortname );
1347 free( p_module->psz_longname );
1348 FREENULL( p_module->psz_help );
1351 #endif /* HAVE_DYNAMIC_PLUGINS */
1353 /*****************************************************************************
1354 * AllocateBuiltinModule: initialize a builtin module.
1355 *****************************************************************************
1356 * This function registers a builtin module and allocates a structure
1357 * for its information data. The module can then be handled by module_need
1358 * and module_unneed. It can be removed by DeleteModule.
1359 *****************************************************************************/
1360 static int AllocateBuiltinModule( vlc_object_t * p_this,
1361 int ( *pf_entry ) ( module_t * ) )
1363 module_t * p_module;
1365 /* Now that we have successfully loaded the module, we can
1366 * allocate a structure for it */
1367 p_module = vlc_module_create( p_this );
1368 if( p_module == NULL )
1371 /* Initialize the module : fill p_module->psz_object_name, etc. */
1372 if( pf_entry( p_module ) != 0 )
1374 /* With a well-written module we shouldn't have to print an
1375 * additional error message here, but just make sure. */
1376 msg_Err( p_this, "failed calling entry point in builtin module" );
1377 module_release( p_module );
1381 /* Everything worked fine ! The module is ready to be added to the list. */
1382 p_module->b_builtin = true;
1384 p_module->next = p_module_bank->head;
1385 p_module_bank->head = p_module;
1388 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1389 p_module->psz_object_name, p_module->psz_longname ); */
1394 /*****************************************************************************
1395 * DeleteModule: delete a module and its structure.
1396 *****************************************************************************
1397 * This function can only be called if the module isn't being used.
1398 *****************************************************************************/
1399 static void DeleteModule( module_bank_t *p_bank, module_t * p_module )
1403 /* Unlist the module (if it is in the list) */
1404 module_t **pp_self = &p_bank->head;
1405 while (*pp_self != NULL && *pp_self != p_module)
1406 pp_self = &((*pp_self)->next);
1408 *pp_self = p_module->next;
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->submodule)
1426 module_t *submodule = p_module->submodule;
1427 p_module->submodule = submodule->next;
1428 module_release (submodule);
1431 config_Free( p_module );
1432 module_release( p_module );