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 module_bank_t *p_module_bank;
95 /*****************************************************************************
97 *****************************************************************************/
98 #ifdef HAVE_DYNAMIC_PLUGINS
99 static void AllocateAllPlugins ( vlc_object_t * );
100 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
101 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
102 static module_t * AllocatePlugin( vlc_object_t *, char * );
104 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
105 static void DeleteModule ( module_t *, bool );
106 #ifdef HAVE_DYNAMIC_PLUGINS
107 static void DupModule ( module_t * );
108 static void UndupModule ( module_t * );
114 * Creates a module bank structure which will be filled later
115 * on with all the modules found.
116 * \param p_this vlc object structure
119 void __module_InitBank( vlc_object_t *p_this )
121 module_bank_t *p_bank = NULL;
123 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
125 if( p_module_bank == NULL )
127 p_bank = vlc_custom_create( p_this, sizeof(module_bank_t),
128 VLC_OBJECT_GENERIC, "module bank");
130 p_bank->i_cache = p_bank->i_loaded_cache = 0;
131 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
132 p_bank->b_cache = p_bank->b_cache_dirty =
133 p_bank->b_cache_delete = false;
136 /* Everything worked, attach the object */
137 p_module_bank = p_bank;
139 /* Fills the module bank structure with the main module infos.
140 * This is very useful as it will allow us to consider the main
141 * library just as another module, and for instance the configuration
142 * options of main will be available in the module bank structure just
143 * as for every other module. */
144 AllocateBuiltinModule( p_this, vlc_entry__main );
147 p_module_bank->i_usage++;
149 vlc_mutex_unlock( lock );
156 * Unloads all unused plugin modules and empties the module
157 * bank in case of success.
158 * \param p_this vlc object structure
161 void __module_EndBank( vlc_object_t *p_this )
163 module_t * p_next = NULL;
165 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
168 vlc_mutex_unlock( lock );
171 if( --p_module_bank->i_usage )
173 vlc_mutex_unlock( lock );
176 vlc_mutex_unlock( lock );
178 /* Save the configuration */
179 config_AutoSaveConfigFile( p_this );
181 #ifdef HAVE_DYNAMIC_PLUGINS
182 # define p_bank p_module_bank
183 if( p_bank->b_cache ) CacheSave( p_this );
184 while( p_bank->i_loaded_cache-- )
186 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
189 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
190 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
191 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
192 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
193 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
196 if( p_bank->pp_loaded_cache )
198 free( p_bank->pp_loaded_cache );
199 p_bank->pp_loaded_cache = NULL;
201 while( p_bank->i_cache-- )
203 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
204 free( p_bank->pp_cache[p_bank->i_cache] );
205 p_bank->pp_cache[p_bank->i_cache] = NULL;
207 if( p_bank->pp_cache )
209 free( p_bank->pp_cache );
210 p_bank->pp_cache = NULL;
215 while( vlc_internals( p_module_bank )->i_children )
217 p_next = (module_t *)vlc_internals( p_module_bank )->pp_children[0];
218 DeleteModule( p_next, true );
221 vlc_object_release( p_module_bank );
222 p_module_bank = NULL;
226 * Load all modules which we built with.
228 * Fills the module bank structure with the builtin modules.
229 * \param p_this vlc object structure
232 void __module_LoadBuiltins( vlc_object_t * p_this )
234 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
235 if( p_module_bank->b_builtins )
237 vlc_mutex_unlock( lock );
240 p_module_bank->b_builtins = true;
241 vlc_mutex_unlock( lock );
243 msg_Dbg( p_this, "checking builtin modules" );
244 ALLOCATE_ALL_BUILTINS();
250 * Load all plugin modules we can find.
251 * Fills the module bank structure with the plugin modules.
252 * \param p_this vlc object structure
255 void __module_LoadPlugins( vlc_object_t * p_this )
257 #ifdef HAVE_DYNAMIC_PLUGINS
258 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
259 if( p_module_bank->b_plugins )
261 vlc_mutex_unlock( lock );
264 p_module_bank->b_plugins = true;
265 vlc_mutex_unlock( lock );
267 msg_Dbg( p_this, "checking plugin modules" );
269 if( config_GetInt( p_this, "plugins-cache" ) )
270 p_module_bank->b_cache = true;
272 if( p_module_bank->b_cache ||
273 p_module_bank->b_cache_delete ) CacheLoad( p_this );
275 AllocateAllPlugins( p_this );
280 * Checks whether a module implements a capability.
282 * \param m the module
283 * \param cap the capability to check
284 * \return TRUE if the module have the capability
286 bool module_IsCapable( const module_t *m, const char *cap )
288 return !strcmp( m->psz_capability, cap );
292 * Get the internal name of a module
294 * \param m the module
295 * \return the module name
297 const char *module_GetObjName( const module_t *m )
299 return m->psz_object_name;
303 * Get the human-friendly name of a module.
305 * \param m the module
306 * \param long_name TRUE to have the long name of the module
307 * \return the short or long name of the module
309 const char *module_GetName( const module_t *m, bool long_name )
311 if( long_name && ( m->psz_longname != NULL) )
312 return m->psz_longname;
314 return m->psz_shortname ?: m->psz_object_name;
318 * Get the help for a module
320 * \param m the module
323 const char *module_GetHelp( const module_t *m )
328 module_t *module_hold (module_t *m)
330 vlc_hold (&m->vlc_gc_data);
334 void module_release (module_t *m)
336 vlc_release (&m->vlc_gc_data);
340 * Frees the flat list of VLC modules.
341 * @param list list obtained by module_list_get
342 * @param length number of items on the list
345 void module_list_free (module_t **list)
350 for (size_t i = 0; list[i] != NULL; i++)
351 module_release (list[i]);
356 * Gets the flat list of VLC modules.
357 * @param n [OUT] pointer to the number of modules or NULL
358 * @return NULL-terminated table of module pointers
359 * (release with module_list_free()), or NULL in case of error.
361 module_t **module_list_get (size_t *n)
363 /* TODO: this whole module lookup is quite inefficient */
364 /* Remove this and improve module_Need */
365 module_t **tab = NULL;
368 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
371 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
374 module_list_free (tab);
379 tab[i++] = module_hold (mod);
380 for (module_t *subm = mod->submodule; subm; subm = subm->next)
381 tab[i++] = module_hold (subm);
392 * Return the best module function, given a capability list.
394 * If the p_this object doesn't have it's psz_object_name set, then
395 * psz_object_name will be set to the module's name, unless the user
396 * provided an alias using the "module name@alias" syntax in which case
397 * psz_object_name will be set to the alias.
399 * \param p_this the vlc object
400 * \param psz_capability list of capabilities needed
401 * \param psz_name name of the module asked
402 * \param b_strict TRUE yto use the strict mode
403 * \return the module or NULL in case of a failure
405 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
406 const char *psz_name, bool b_strict )
408 typedef struct module_list_t module_list_t;
410 stats_TimerStart( p_this, "module_Need()", STATS_TIMER_MODULE_NEED );
417 module_list_t *p_next;
420 module_list_t *p_list, *p_first, *p_tmp;
422 int i_which_module, i_index = 0;
427 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
428 bool b_force_backup = p_this->b_force;
431 /* Deal with variables */
432 if( psz_name && psz_name[0] == '$' )
434 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
437 /* Count how many different shortcuts were asked for */
438 if( psz_name && *psz_name )
440 char *psz_parser, *psz_last_shortcut;
442 /* If the user wants none, give him none. */
443 if( !strcmp( psz_name, "none" ) )
446 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
447 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
448 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
453 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
455 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
457 if( *psz_parser == ',' )
461 psz_last_shortcut = psz_parser + 1;
465 /* Check if the user wants to override the "strict" mode */
466 if( psz_last_shortcut )
468 if( !strcmp(psz_last_shortcut, "none") )
473 else if( !strcmp(psz_last_shortcut, "any") )
481 /* Sort the modules and test them */
483 module_t **p_all = module_list_get (&count);
484 p_list = malloc( count * sizeof( module_list_t ) );
486 unsigned i_cpu = vlc_CPU();
488 /* Parse the module list for capabilities and probe each of them */
489 for( i_which_module = 0; i_which_module < count; i_which_module++ )
491 int i_shortcut_bonus = 0;
492 p_module = p_all[i_which_module];
494 /* Test that this module can do what we need */
495 if( !module_IsCapable( p_module, psz_capability ) )
497 /* Don't recurse through the sub-modules because vlc_list_find()
498 * will list them anyway. */
502 /* Test if we have the required CPU */
503 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
508 /* If we required a shortcut, check this plugin provides it. */
509 if( i_shortcuts > 0 )
512 const char *psz_name = psz_shortcuts;
514 /* Let's drop modules with a <= 0 score (unless they are
515 * explicitly requested) */
516 b_trash = p_module->i_score <= 0;
518 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
520 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
523 if( ( c = strchr( psz_name, '@' ) )
524 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
526 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
531 i_shortcut_bonus = i_short * 10000;
536 /* Go to the next shortcut... This is so lame! */
537 psz_name += strlen( psz_name ) + 1;
540 /* If we are in "strict" mode and we couldn't
541 * find the module in the list of provided shortcuts,
542 * then kick the bastard out of here!!! */
546 /* If we didn't require a shortcut, trash <= 0 scored plugins */
547 else if( p_module->i_score <= 0 )
554 /* Store this new module */
555 p_list[ i_index ].p_module = p_module;
556 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
557 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
559 /* Add it to the modules-to-probe list */
562 p_list[ 0 ].p_next = NULL;
567 /* Ok, so at school you learned that quicksort is quick, and
568 * bubble sort sucks raw eggs. But that's when dealing with
569 * thousands of items. Here we have barely 50. */
570 module_list_t *p_newlist = p_first;
572 if( p_first->i_score < p_list[ i_index ].i_score )
574 p_list[ i_index ].p_next = p_first;
575 p_first = &p_list[ i_index ];
579 while( p_newlist->p_next != NULL &&
580 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
582 p_newlist = p_newlist->p_next;
585 p_list[ i_index ].p_next = p_newlist->p_next;
586 p_newlist->p_next = &p_list[ i_index ];
593 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
594 i_index, i_index == 1 ? "" : "s" );
596 /* Lock all candidate modules */
598 while( p_tmp != NULL )
600 module_hold( p_tmp->p_module );
601 p_tmp = p_tmp->p_next;
604 /* We can release the list, interesting modules were held */
605 module_list_free (p_all);
607 /* Parse the linked list and use the first successful module */
609 while( p_tmp != NULL )
611 #ifdef HAVE_DYNAMIC_PLUGINS
612 /* Make sure the module is loaded in mem */
613 module_t *p_module = p_tmp->p_module;
614 if( p_module->b_submodule )
615 p_module = p_module->parent;
617 if( !p_module->b_builtin && !p_module->b_loaded )
619 module_t *p_new_module =
620 AllocatePlugin( p_this, p_module->psz_filename );
623 CacheMerge( p_this, p_module, p_new_module );
624 DeleteModule( p_new_module, false );
629 p_this->b_force = p_tmp->b_force;
630 if( p_tmp->p_module->pf_activate
631 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
636 module_release( p_tmp->p_module );
637 p_tmp = p_tmp->p_next;
640 /* Store the locked module value */
643 p_module = p_tmp->p_module;
644 p_tmp = p_tmp->p_next;
651 /* Unlock the remaining modules */
652 while( p_tmp != NULL )
654 module_release( p_tmp->p_module );
655 p_tmp = p_tmp->p_next;
659 p_this->b_force = b_force_backup;
661 if( p_module != NULL )
663 msg_Dbg( p_this, "using %s module \"%s\"",
664 psz_capability, p_module->psz_object_name );
666 else if( p_first == NULL )
668 if( !strcmp( psz_capability, "access_demux" ) )
670 msg_Warn( p_this, "no %s module matched \"%s\"",
671 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
675 msg_Err( p_this, "no %s module matched \"%s\"",
676 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
678 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
679 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
682 else if( psz_name != NULL && *psz_name )
684 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
685 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
688 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
690 if( p_module && !p_this->psz_object_name )
692 /* This assumes that p_this is the object which will be using the
693 * module. That's not always the case ... but it is in most cases.
696 p_this->psz_object_name = strdup( psz_alias );
698 p_this->psz_object_name = strdup( p_module->psz_object_name );
701 free( psz_shortcuts );
704 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
705 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
706 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
708 /* Don't forget that the module is still locked */
715 * This function must be called by the thread that called module_Need, to
716 * decrease the reference count and allow for hiding of modules.
717 * \param p_this vlc object structure
718 * \param p_module the module structure
721 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
723 /* Use the close method */
724 if( p_module->pf_deactivate )
726 p_module->pf_deactivate( p_this );
729 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
731 module_release( p_module );
735 * Get a pointer to a module_t given it's name.
737 * \param p_this vlc object structure
738 * \param psz_name the name of the module
739 * \return a pointer to the module or NULL in case of a failure
741 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
743 module_t **list, *module;
745 list = module_list_get (NULL);
749 for (size_t i = 0; (module = list[i]) != NULL; i++)
751 const char *psz_module_name = module->psz_object_name;
753 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
755 module_hold (module);
759 module_list_free (list);
764 * Tell if a module exists and release it in thic case
766 * \param p_this vlc object structure
767 * \param psz_name th name of the module
768 * \return TRUE if the module exists
770 bool __module_Exists( vlc_object_t *p_this, const char * psz_name )
772 module_t *p_module = __module_Find( p_this, psz_name );
774 module_release (p_module);
779 * GetModuleNamesForCapability
781 * Return a NULL terminated array with the names of the modules
782 * that have a certain capability.
783 * Free after uses both the string and the table.
784 * \param p_this vlc object structure
785 * \param psz_capability the capability asked
786 * \param pppsz_longname an pointer to an array of string to contain
787 the long names of the modules. If set to NULL the function don't use it.
788 * \return the NULL terminated array
790 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
791 const char *psz_capability,
792 char ***pppsz_longname )
797 module_t **list = module_list_get (NULL);
799 /* Do it in two passes : count the number of modules before */
800 for (size_t i = 0; list[i]; i++)
802 module_t *p_module = list[i];
803 const char *psz_module_capability = p_module->psz_capability;
805 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
809 psz_ret = malloc( sizeof(char*) * (count+1) );
811 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
812 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
815 free( *pppsz_longname );
816 *pppsz_longname = NULL;
817 module_list_free (list);
821 for (size_t i = 0, j = 0; list[i]; i++)
823 module_t *p_module = list[i];
824 const char *psz_module_capability = p_module->psz_capability;
826 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
828 int k = -1; /* hack to handle submodules properly */
829 if( p_module->b_submodule )
831 while( p_module->pp_shortcuts[++k] != NULL );
834 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
835 :p_module->psz_object_name );
837 (*pppsz_longname)[j] = strdup( module_GetName( p_module, true ) );
841 psz_ret[count] = NULL;
843 module_list_free (list);
849 * Get the configuration of a module
851 * \param module the module
852 * \param psize the size of the configuration returned
853 * \return the configuration as an array
855 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
858 unsigned size = module->confsize;
859 module_config_t *config = malloc( size * sizeof( *config ) );
861 assert( psize != NULL );
867 for( i = 0, j = 0; i < size; i++ )
869 const module_config_t *item = module->p_config + i;
870 if( item->b_internal /* internal option */
871 || item->b_unsaveable /* non-modifiable option */
872 || item->b_removed /* removed option */ )
875 memcpy( config + j, item, sizeof( *config ) );
884 * Release the configuration
886 * \param the configuration
889 void module_PutConfig( module_config_t *config )
894 /*****************************************************************************
895 * Following functions are local.
896 *****************************************************************************/
898 /*****************************************************************************
899 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
901 *****************************************************************************/
902 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
906 bool escaped = false;
910 /* Alloc a buffer to store the path */
911 path = malloc( strlen( paths ) + 1 );
912 if( !path ) return NULL;
914 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
915 for( i = 0, done = 0 ; paths[i]; i++ )
917 /* Take care of \\ and \: or \; escapement */
921 path[done++] = paths[i];
924 else if( paths[i] == '/' )
927 else if( paths[i] == '\\' )
930 else if( paths[i] == PATH_SEP_CHAR )
933 path[done++] = paths[i];
937 /* Return the remaining paths */
938 if( remaining_paths ) {
939 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
945 /*****************************************************************************
946 * AllocateAllPlugins: load all plugin modules we can find.
947 *****************************************************************************/
948 #ifdef HAVE_DYNAMIC_PLUGINS
949 static void AllocateAllPlugins( vlc_object_t *p_this )
951 const char *vlcpath = vlc_global()->psz_vlcpath;
954 vlc_array_t *arraypaths = vlc_array_new();
956 /* Contruct the special search path for system that have a relocatable
957 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
959 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
960 vlc_array_append( arraypaths, path );
961 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
962 vlc_array_append( arraypaths, path );
964 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
967 /* If the user provided a plugin path, we add it to the list */
968 char *userpaths = config_GetPsz( p_this, "plugin-path" );
971 for( paths_iter = userpaths; paths_iter; )
973 path = copy_next_paths_token( paths_iter, &paths_iter );
975 vlc_array_append( arraypaths, path );
978 count = vlc_array_count( arraypaths );
979 for( i = 0 ; i < count ; i++ )
981 path = vlc_array_item_at_index( arraypaths, i );
985 msg_Dbg( p_this, "recursively browsing `%s'", path );
987 /* Don't go deeper than 5 subdirectories */
988 AllocatePluginDir( p_this, path, 5 );
993 vlc_array_destroy( arraypaths );
997 /*****************************************************************************
998 * AllocatePluginDir: recursively parse a directory to look for plugins
999 *****************************************************************************/
1000 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
1003 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
1009 #if defined( UNDER_CE ) || defined( _MSC_VER )
1011 wchar_t psz_wpath[MAX_PATH + 256];
1012 wchar_t psz_wdir[MAX_PATH];
1014 char psz_path[MAX_PATH + 256];
1015 WIN32_FIND_DATA finddata;
1021 struct dirent * file;
1025 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1030 #if defined( UNDER_CE ) || defined( _MSC_VER )
1032 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1034 rc = GetFileAttributes( psz_wdir );
1035 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1037 /* Parse all files in the directory */
1038 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1040 rc = GetFileAttributes( psz_dir );
1041 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1044 /* Parse all files in the directory */
1045 sprintf( psz_path, "%s\\*", psz_dir );
1048 handle = FindFirstFile( psz_wpath, &finddata );
1050 handle = FindFirstFile( psz_path, &finddata );
1052 if( handle == INVALID_HANDLE_VALUE )
1054 /* Empty directory */
1058 /* Parse the directory and try to load all files it contains. */
1062 unsigned int i_len = wcslen( finddata.cFileName );
1063 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1064 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1066 unsigned int i_len = strlen( finddata.cFileName );
1067 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1070 /* Skip ".", ".." */
1071 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1072 || !strcmp( finddata.cFileName, ".." ) )
1074 if( !FindNextFile( handle, &finddata ) ) break;
1079 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1081 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1084 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1086 else if( i_len > strlen( LIBEXT )
1087 /* We only load files ending with LIBEXT */
1088 && !strncasecmp( psz_path + strlen( psz_path)
1090 LIBEXT, strlen( LIBEXT ) ) )
1092 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1093 int64_t i_time = 0, i_size = 0;
1096 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1099 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1103 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1105 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1106 i_size = attrbuf.nFileSizeHigh;
1108 i_size |= attrbuf.nFileSizeLow;
1110 psz_file = psz_path;
1112 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1115 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1117 /* Close the directory */
1118 FindClose( handle );
1121 dir = opendir( psz_dir );
1127 i_dirlen = strlen( psz_dir );
1129 /* Parse the directory and try to load all files it contains. */
1130 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1132 struct stat statbuf;
1136 /* Skip ".", ".." */
1137 if( !*file->d_name || !strcmp( file->d_name, "." )
1138 || !strcmp( file->d_name, ".." ) )
1143 i_len = strlen( file->d_name );
1144 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1145 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1147 i_stat = stat( psz_file, &statbuf );
1148 if( !i_stat && statbuf.st_mode & S_IFDIR )
1150 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1152 else if( i_len > strlen( LIBEXT )
1153 /* We only load files ending with LIBEXT */
1154 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1155 LIBEXT, strlen( LIBEXT ) ) )
1157 int64_t i_time = 0, i_size = 0;
1161 i_time = statbuf.st_mtime;
1162 i_size = statbuf.st_size;
1165 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1171 /* Close the directory */
1177 /*****************************************************************************
1178 * AllocatePluginFile: load a module into memory and initialize it.
1179 *****************************************************************************
1180 * This function loads a dynamically loadable module and allocates a structure
1181 * for its information data. The module can then be handled by module_Need
1182 * and module_Unneed. It can be removed by DeleteModule.
1183 *****************************************************************************/
1184 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1185 int64_t i_file_time, int64_t i_file_size )
1187 module_t * p_module = NULL;
1188 module_cache_t *p_cache_entry = NULL;
1191 * Check our plugins cache first then load plugin if needed
1194 CacheFind( psz_file, i_file_time, i_file_size );
1196 if( !p_cache_entry )
1198 p_module = AllocatePlugin( p_this, psz_file );
1202 /* If junk dll, don't try to load it */
1203 if( p_cache_entry->b_junk )
1209 module_config_t *p_item = NULL, *p_end = NULL;
1211 p_module = p_cache_entry->p_module;
1212 p_module->b_loaded = false;
1214 /* For now we force loading if the module's config contains
1215 * callbacks or actions.
1216 * Could be optimized by adding an API call.*/
1217 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1218 p_item < p_end; p_item++ )
1220 if( p_item->pf_callback || p_item->i_action )
1222 p_module = AllocatePlugin( p_this, psz_file );
1226 if( p_module == p_cache_entry->p_module )
1227 p_cache_entry->b_used = true;
1233 /* Everything worked fine !
1234 * The module is ready to be added to the list. */
1235 p_module->b_builtin = false;
1237 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1238 p_module->psz_object_name, p_module->psz_longname ); */
1239 p_module->next = p_module_bank->head;
1240 p_module_bank->head = p_module;
1242 if( !p_module_bank->b_cache )
1245 #define p_bank p_module_bank
1246 /* Add entry to cache */
1248 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1249 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1250 if( !p_bank->pp_cache[p_bank->i_cache] )
1252 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1253 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1254 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1255 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1256 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1257 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1262 return p_module ? 0 : -1;
1265 /*****************************************************************************
1266 * AllocatePlugin: load a module into memory and initialize it.
1267 *****************************************************************************
1268 * This function loads a dynamically loadable module and allocates a structure
1269 * for its information data. The module can then be handled by module_Need
1270 * and module_Unneed. It can be removed by DeleteModule.
1271 *****************************************************************************/
1272 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1274 module_t * p_module = NULL;
1275 module_handle_t handle;
1277 if( module_Load( p_this, psz_file, &handle ) )
1280 /* Now that we have successfully loaded the module, we can
1281 * allocate a structure for it */
1282 p_module = vlc_module_create( p_this );
1283 if( p_module == NULL )
1285 module_Unload( handle );
1289 /* We need to fill these since they may be needed by module_Call() */
1290 p_module->psz_filename = psz_file;
1291 p_module->handle = handle;
1292 p_module->b_loaded = true;
1294 /* Initialize the module: fill p_module, default config */
1295 if( module_Call( p_this, p_module ) != 0 )
1297 /* We couldn't call module_init() */
1298 module_release( p_module );
1299 module_Unload( handle );
1303 DupModule( p_module );
1304 p_module->psz_filename = strdup( p_module->psz_filename );
1306 /* Everything worked fine ! The module is ready to be added to the list. */
1307 p_module->b_builtin = false;
1312 /*****************************************************************************
1313 * DupModule: make a plugin module standalone.
1314 *****************************************************************************
1315 * This function duplicates all strings in the module, so that the dynamic
1316 * object can be unloaded. It acts recursively on submodules.
1317 *****************************************************************************/
1318 static void DupModule( module_t *p_module )
1322 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1324 *pp_shortcut = strdup( *pp_shortcut );
1327 /* We strdup() these entries so that they are still valid when the
1328 * module is unloaded. */
1329 p_module->psz_capability = strdup( p_module->psz_capability );
1330 p_module->psz_shortname = p_module->psz_shortname ?
1331 strdup( p_module->psz_shortname ) : NULL;
1332 p_module->psz_longname = strdup( p_module->psz_longname );
1333 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1336 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1340 /*****************************************************************************
1341 * UndupModule: free a duplicated module.
1342 *****************************************************************************
1343 * This function frees the allocations done in DupModule().
1344 *****************************************************************************/
1345 static void UndupModule( module_t *p_module )
1349 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1352 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1354 free( *pp_shortcut );
1357 FREENULL( p_module->psz_object_name );
1358 free( p_module->psz_capability );
1359 free( p_module->psz_shortname );
1360 free( p_module->psz_longname );
1361 free( p_module->psz_help );
1364 #endif /* HAVE_DYNAMIC_PLUGINS */
1366 /*****************************************************************************
1367 * AllocateBuiltinModule: initialize a builtin module.
1368 *****************************************************************************
1369 * This function registers a builtin module and allocates a structure
1370 * for its information data. The module can then be handled by module_Need
1371 * and module_Unneed. It can be removed by DeleteModule.
1372 *****************************************************************************/
1373 static int AllocateBuiltinModule( vlc_object_t * p_this,
1374 int ( *pf_entry ) ( module_t * ) )
1376 module_t * p_module;
1378 /* Now that we have successfully loaded the module, we can
1379 * allocate a structure for it */
1380 p_module = vlc_module_create( p_this );
1381 if( p_module == NULL )
1384 /* Initialize the module : fill p_module->psz_object_name, etc. */
1385 if( pf_entry( p_module ) != 0 )
1387 /* With a well-written module we shouldn't have to print an
1388 * additional error message here, but just make sure. */
1389 msg_Err( p_this, "failed calling entry point in builtin module" );
1390 module_release( p_module );
1394 /* Everything worked fine ! The module is ready to be added to the list. */
1395 p_module->b_builtin = true;
1397 p_module->next = p_module_bank->head;
1398 p_module_bank->head = p_module;
1401 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1402 p_module->psz_object_name, p_module->psz_longname ); */
1407 /*****************************************************************************
1408 * DeleteModule: delete a module and its structure.
1409 *****************************************************************************
1410 * This function can only be called if the module isn't being used.
1411 *****************************************************************************/
1412 static void DeleteModule( module_t * p_module, bool b_detach )
1416 /* We free the structures that we strdup()ed in Allocate*Module(). */
1417 #ifdef HAVE_DYNAMIC_PLUGINS
1418 if( !p_module->b_builtin )
1420 if( p_module->b_loaded && p_module->b_unloadable )
1422 module_Unload( p_module->handle );
1424 UndupModule( p_module );
1425 free( p_module->psz_filename );
1429 /* Free and detach the object's children */
1430 while (p_module->submodule)
1432 module_t *submodule = p_module->submodule;
1433 p_module->submodule = submodule->next;
1434 module_release (submodule);
1437 config_Free( p_module );
1438 module_release( p_module );