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;
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 * );
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_lock( &global_lock );
125 if( p_module_bank == NULL )
127 p_bank = calloc (1, sizeof(*p_bank));
129 p_bank->i_cache = p_bank->i_loaded_cache = 0;
130 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
131 p_bank->b_cache = p_bank->b_cache_dirty = false;
134 /* Everything worked, attach the object */
135 p_module_bank = p_bank;
137 /* Fills the module bank structure with the main module infos.
138 * This is very useful as it will allow us to consider the main
139 * library just as another module, and for instance the configuration
140 * options of main will be available in the module bank structure just
141 * as for every other module. */
142 AllocateBuiltinModule( p_this, vlc_entry__main );
145 p_module_bank->i_usage++;
147 vlc_mutex_unlock( &global_lock );
154 * Unloads all unused plugin modules and empties the module
155 * bank in case of success.
156 * \param p_this vlc object structure
159 void __module_EndBank( vlc_object_t *p_this )
161 module_bank_t *p_bank;
163 vlc_mutex_lock( &global_lock );
164 p_bank = p_module_bank;
165 assert (p_bank != NULL);
166 if( --p_bank->i_usage > 0 )
168 vlc_mutex_unlock( &global_lock );
171 /*FIXME: For thread safety, we need to:
172 p_module_bank = NULL; - immediately, but that will crash the cache */
173 vlc_mutex_unlock( &global_lock );
175 /* Save the configuration */
176 config_AutoSaveConfigFile( p_this );
178 #ifdef HAVE_DYNAMIC_PLUGINS
179 if( p_bank->b_cache ) CacheSave( p_this, p_module_bank );
180 while( p_bank->i_loaded_cache-- )
182 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
185 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
186 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
187 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
188 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
191 if( p_bank->pp_loaded_cache )
193 free( p_bank->pp_loaded_cache );
194 p_bank->pp_loaded_cache = NULL;
196 while( p_bank->i_cache-- )
198 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
199 free( p_bank->pp_cache[p_bank->i_cache] );
200 p_bank->pp_cache[p_bank->i_cache] = NULL;
202 if( p_bank->pp_cache )
204 free( p_bank->pp_cache );
205 p_bank->pp_cache = NULL;
209 while( p_bank->head != NULL )
210 DeleteModule( p_bank->head );
212 p_module_bank = NULL; /* FIXME: do this inside the lock */
217 * Load all modules which we built with.
219 * Fills the module bank structure with the builtin modules.
220 * \param p_this vlc object structure
223 void __module_LoadBuiltins( vlc_object_t * p_this )
225 vlc_mutex_lock( &global_lock );
226 if( p_module_bank->b_builtins )
228 vlc_mutex_unlock( &global_lock );
231 p_module_bank->b_builtins = true;
232 vlc_mutex_unlock( &global_lock );
234 msg_Dbg( p_this, "checking builtin modules" );
235 ALLOCATE_ALL_BUILTINS();
238 #undef module_LoadPlugins
242 * Load all plugin modules we can find.
243 * Fills the module bank structure with the plugin modules.
244 * \param p_this vlc object structure
247 void module_LoadPlugins( vlc_object_t * p_this, bool b_cache_delete )
249 #ifdef HAVE_DYNAMIC_PLUGINS
250 vlc_mutex_lock( &global_lock );
251 if( p_module_bank->b_plugins )
253 vlc_mutex_unlock( &global_lock );
256 p_module_bank->b_plugins = true;
257 vlc_mutex_unlock( &global_lock );
259 msg_Dbg( p_this, "checking plugin modules" );
261 if( config_GetInt( p_this, "plugins-cache" ) )
262 p_module_bank->b_cache = true;
264 if( p_module_bank->b_cache || b_cache_delete )
265 CacheLoad( p_this, p_module_bank, b_cache_delete );
267 AllocateAllPlugins( p_this );
272 * Checks whether a module implements a capability.
274 * \param m the module
275 * \param cap the capability to check
276 * \return TRUE if the module have the capability
278 bool module_provides( const module_t *m, const char *cap )
280 return !strcmp( m->psz_capability, cap );
284 * Get the internal name of a module
286 * \param m the module
287 * \return the module name
289 const char *module_get_object( const module_t *m )
291 return m->psz_object_name;
295 * Get the human-friendly name of a module.
297 * \param m the module
298 * \param long_name TRUE to have the long name of the module
299 * \return the short or long name of the module
301 const char *module_get_name( const module_t *m, bool long_name )
303 if( long_name && ( m->psz_longname != NULL) )
304 return m->psz_longname;
306 return m->psz_shortname ?: m->psz_object_name;
310 * Get the help for a module
312 * \param m the module
315 const char *module_get_help( const module_t *m )
320 module_t *module_hold (module_t *m)
322 vlc_hold (&m->vlc_gc_data);
326 void module_release (module_t *m)
328 vlc_release (&m->vlc_gc_data);
332 * Frees the flat list of VLC modules.
333 * @param list list obtained by module_list_get
334 * @param length number of items on the list
337 void module_list_free (module_t **list)
342 for (size_t i = 0; list[i] != NULL; i++)
343 module_release (list[i]);
348 * Gets the flat list of VLC modules.
349 * @param n [OUT] pointer to the number of modules or NULL
350 * @return NULL-terminated table of module pointers
351 * (release with module_list_free()), or NULL in case of error.
353 module_t **module_list_get (size_t *n)
355 /* TODO: this whole module lookup is quite inefficient */
356 /* Remove this and improve module_need */
357 module_t **tab = NULL;
360 for (module_t *mod = p_module_bank->head; mod; mod = mod->next)
363 nt = realloc (tab, (i + 2 + mod->submodule_count) * sizeof (*tab));
366 module_list_free (tab);
371 tab[i++] = module_hold (mod);
372 for (module_t *subm = mod->submodule; subm; subm = subm->next)
373 tab[i++] = module_hold (subm);
381 typedef struct module_list_t
388 static int modulecmp (const void *a, const void *b)
390 const module_list_t *la = a, *lb = b;
391 /* Note that qsort() uses _ascending_ order,
392 * so the smallest module is the one with the biggest score. */
393 return lb->i_score - la->i_score;
399 * Return the best module function, given a capability list.
401 * If the p_this object doesn't have it's psz_object_name set, then
402 * psz_object_name will be set to the module's name, unless the user
403 * provided an alias using the "module name@alias" syntax in which case
404 * psz_object_name will be set to the alias.
406 * \param p_this the vlc object
407 * \param psz_capability list of capabilities needed
408 * \param psz_name name of the module asked
409 * \param b_strict TRUE yto use the strict mode
410 * \return the module or NULL in case of a failure
412 module_t * __module_need( vlc_object_t *p_this, const char *psz_capability,
413 const char *psz_name, bool b_strict )
415 stats_TimerStart( p_this, "module_need()", STATS_TIMER_MODULE_NEED );
417 module_list_t *p_list;
420 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
421 bool b_force_backup = p_this->b_force;
423 /* Deal with variables */
424 if( psz_name && psz_name[0] == '$' )
426 psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
429 /* Count how many different shortcuts were asked for */
430 if( psz_name && *psz_name )
432 char *psz_parser, *psz_last_shortcut;
434 /* If the user wants none, give him none. */
435 if( !strcmp( psz_name, "none" ) )
438 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
439 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
440 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
445 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
447 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
449 if( *psz_parser == ',' )
453 psz_last_shortcut = psz_parser + 1;
457 /* Check if the user wants to override the "strict" mode */
458 if( psz_last_shortcut )
460 if( !strcmp(psz_last_shortcut, "none") )
465 else if( !strcmp(psz_last_shortcut, "any") )
473 /* Sort the modules and test them */
475 module_t **p_all = module_list_get (&count);
476 p_list = malloc( count * sizeof( module_list_t ) );
477 unsigned i_cpu = vlc_CPU();
479 /* Parse the module list for capabilities and probe each of them */
481 for (size_t i = 0; (p_module = p_all[i]) != NULL; i++)
483 bool b_shortcut_bonus = false;
485 /* Test that this module can do what we need */
486 if( !module_provides( p_module, psz_capability ) )
488 /* Test if we have the required CPU */
489 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
492 /* If we required a shortcut, check this plugin provides it. */
493 if( i_shortcuts > 0 )
495 const char *psz_name = psz_shortcuts;
497 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
499 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
502 if( ( c = strchr( psz_name, '@' ) )
503 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
505 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
510 b_shortcut_bonus = true;
515 /* Go to the next shortcut... This is so lame! */
516 psz_name += strlen( psz_name ) + 1;
519 /* If we are in "strict" mode and we couldn't
520 * find the module in the list of provided shortcuts,
521 * then kick the bastard out of here!!! */
525 /* If we didn't require a shortcut, trash <= 0 scored plugins */
526 else if( p_module->i_score <= 0 )
532 /* Store this new module */
533 p_list[count].p_module = module_hold (p_module);
534 p_list[count].i_score = p_module->i_score;
535 if( b_shortcut_bonus )
536 p_list[count].i_score += 10000;
537 p_list[count].b_force = b_shortcut_bonus && b_strict;
541 /* We can release the list, interesting modules are held */
542 module_list_free (p_all);
544 /* Sort candidates by descending score */
545 qsort (p_list, count, sizeof (p_list[0]), modulecmp);
546 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
547 count, count == 1 ? "" : "s" );
549 /* Parse the linked list and use the first successful module */
551 for (size_t i = 0; (i < count) && (p_module == NULL); i++)
553 module_t *p_cand = p_list[i].p_module;
554 #ifdef HAVE_DYNAMIC_PLUGINS
555 /* Make sure the module is loaded in mem */
556 module_t *p_real = p_cand->b_submodule ? p_cand->parent : p_cand;
558 if( !p_real->b_builtin && !p_real->b_loaded )
560 module_t *p_new_module =
561 AllocatePlugin( p_this, p_real->psz_filename );
564 CacheMerge( p_this, p_real, p_new_module );
565 DeleteModule( p_new_module );
570 p_this->b_force = p_list[i].b_force;
571 if( p_cand->pf_activate
572 && p_cand->pf_activate( p_this ) == VLC_SUCCESS )
575 /* Release the remaining modules */
577 module_release (p_list[i].p_module);
580 module_release( p_cand );
584 p_this->b_force = b_force_backup;
586 if( p_module != NULL )
588 msg_Dbg( p_this, "using %s module \"%s\"",
589 psz_capability, p_module->psz_object_name );
590 if( !p_this->psz_object_name )
592 /* This assumes that p_this is the object which will be using the
593 * module. That's not always the case ... but it is in most cases.
596 p_this->psz_object_name = strdup( psz_alias );
598 p_this->psz_object_name = strdup( p_module->psz_object_name );
601 else if( count == 0 )
603 if( !strcmp( psz_capability, "access_demux" )
604 || !strcmp( psz_capability, "vout_window" ) )
606 msg_Dbg( p_this, "no %s module matched \"%s\"",
607 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
611 msg_Err( p_this, "no %s module matched \"%s\"",
612 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
614 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
615 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
618 else if( psz_name != NULL && *psz_name )
620 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
621 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
624 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
626 free( psz_shortcuts );
629 stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
630 stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
631 stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
633 /* Don't forget that the module is still locked */
640 * This function must be called by the thread that called module_need, to
641 * decrease the reference count and allow for hiding of modules.
642 * \param p_this vlc object structure
643 * \param p_module the module structure
646 void __module_unneed( vlc_object_t * p_this, module_t * p_module )
648 /* Use the close method */
649 if( p_module->pf_deactivate )
651 p_module->pf_deactivate( p_this );
654 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
656 module_release( p_module );
660 * Get a pointer to a module_t given it's name.
662 * \param p_this vlc object structure
663 * \param psz_name the name of the module
664 * \return a pointer to the module or NULL in case of a failure
666 module_t *module_find( const char * psz_name )
668 module_t **list, *module;
670 list = module_list_get (NULL);
674 for (size_t i = 0; (module = list[i]) != NULL; i++)
676 const char *psz_module_name = module->psz_object_name;
678 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
680 module_hold (module);
684 module_list_free (list);
689 * Tell if a module exists and release it in thic case
691 * \param p_this vlc object structure
692 * \param psz_name th name of the module
693 * \return TRUE if the module exists
695 bool module_exists (const char * psz_name)
697 module_t *p_module = module_find (psz_name);
699 module_release (p_module);
700 return p_module != NULL;
704 * GetModuleNamesForCapability
706 * Return a NULL terminated array with the names of the modules
707 * that have a certain capability.
708 * Free after uses both the string and the table.
709 * \param psz_capability the capability asked
710 * \param pppsz_longname an pointer to an array of string to contain
711 the long names of the modules. If set to NULL the function don't use it.
712 * \return the NULL terminated array
714 char ** module_GetModulesNamesForCapability( const char *psz_capability,
715 char ***pppsz_longname )
720 module_t **list = module_list_get (NULL);
722 /* Do it in two passes : count the number of modules before */
723 for (size_t i = 0; list[i]; i++)
725 module_t *p_module = list[i];
726 const char *psz_module_capability = p_module->psz_capability;
728 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
732 psz_ret = malloc( sizeof(char*) * (count+1) );
734 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
735 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
738 free( *pppsz_longname );
739 *pppsz_longname = NULL;
740 module_list_free (list);
744 for (size_t i = 0, j = 0; list[i]; i++)
746 module_t *p_module = list[i];
747 const char *psz_module_capability = p_module->psz_capability;
749 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
751 int k = -1; /* hack to handle submodules properly */
752 if( p_module->b_submodule )
754 while( p_module->pp_shortcuts[++k] != NULL );
757 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
758 :p_module->psz_object_name );
760 (*pppsz_longname)[j] = strdup( module_get_name( p_module, true ) );
764 psz_ret[count] = NULL;
766 module_list_free (list);
772 * Get the configuration of a module
774 * \param module the module
775 * \param psize the size of the configuration returned
776 * \return the configuration as an array
778 module_config_t *module_config_get( const module_t *module, unsigned *restrict psize )
781 unsigned size = module->confsize;
782 module_config_t *config = malloc( size * sizeof( *config ) );
784 assert( psize != NULL );
790 for( i = 0, j = 0; i < size; i++ )
792 const module_config_t *item = module->p_config + i;
793 if( item->b_internal /* internal option */
794 || item->b_unsaveable /* non-modifiable option */
795 || item->b_removed /* removed option */ )
798 memcpy( config + j, item, sizeof( *config ) );
807 * Release the configuration
809 * \param the configuration
812 void module_config_free( module_config_t *config )
817 /*****************************************************************************
818 * Following functions are local.
819 *****************************************************************************/
821 /*****************************************************************************
822 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
824 *****************************************************************************/
825 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
829 bool escaped = false;
833 /* Alloc a buffer to store the path */
834 path = malloc( strlen( paths ) + 1 );
835 if( !path ) return NULL;
837 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
838 for( i = 0, done = 0 ; paths[i]; i++ )
840 /* Take care of \\ and \: or \; escapement */
844 path[done++] = paths[i];
847 else if( paths[i] == '/' )
850 else if( paths[i] == '\\' )
853 else if( paths[i] == PATH_SEP_CHAR )
856 path[done++] = paths[i];
860 /* Return the remaining paths */
861 if( remaining_paths ) {
862 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
868 char *psz_vlcpath = NULL;
870 /*****************************************************************************
871 * AllocateAllPlugins: load all plugin modules we can find.
872 *****************************************************************************/
873 #ifdef HAVE_DYNAMIC_PLUGINS
874 static void AllocateAllPlugins( vlc_object_t *p_this )
876 const char *vlcpath = psz_vlcpath;
879 vlc_array_t *arraypaths = vlc_array_new();
881 /* Contruct the special search path for system that have a relocatable
882 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
884 if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
885 vlc_array_append( arraypaths, path );
886 if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
887 vlc_array_append( arraypaths, path );
889 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
892 /* If the user provided a plugin path, we add it to the list */
893 char *userpaths = config_GetPsz( p_this, "plugin-path" );
896 for( paths_iter = userpaths; paths_iter; )
898 path = copy_next_paths_token( paths_iter, &paths_iter );
900 vlc_array_append( arraypaths, path );
903 count = vlc_array_count( arraypaths );
904 for( i = 0 ; i < count ; i++ )
906 path = vlc_array_item_at_index( arraypaths, i );
910 msg_Dbg( p_this, "recursively browsing `%s'", path );
912 /* Don't go deeper than 5 subdirectories */
913 AllocatePluginDir( p_this, path, 5 );
918 vlc_array_destroy( arraypaths );
922 /*****************************************************************************
923 * AllocatePluginDir: recursively parse a directory to look for plugins
924 *****************************************************************************/
925 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
928 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
934 #if defined( UNDER_CE ) || defined( _MSC_VER )
936 wchar_t psz_wpath[MAX_PATH + 256];
937 wchar_t psz_wdir[MAX_PATH];
939 char psz_path[MAX_PATH + 256];
940 WIN32_FIND_DATA finddata;
946 struct dirent * file;
950 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
955 #if defined( UNDER_CE ) || defined( _MSC_VER )
957 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
959 rc = GetFileAttributes( psz_wdir );
960 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
962 /* Parse all files in the directory */
963 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
965 rc = GetFileAttributes( psz_dir );
966 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
969 /* Parse all files in the directory */
970 sprintf( psz_path, "%s\\*", psz_dir );
973 handle = FindFirstFile( psz_wpath, &finddata );
975 handle = FindFirstFile( psz_path, &finddata );
977 if( handle == INVALID_HANDLE_VALUE )
979 /* Empty directory */
983 /* Parse the directory and try to load all files it contains. */
987 unsigned int i_len = wcslen( finddata.cFileName );
988 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
989 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
991 unsigned int i_len = strlen( finddata.cFileName );
992 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
996 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
997 || !strcmp( finddata.cFileName, ".." ) )
999 if( !FindNextFile( handle, &finddata ) ) break;
1004 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1006 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1009 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1011 else if( i_len > strlen( LIBEXT )
1012 /* We only load files ending with LIBEXT */
1013 && !strncasecmp( psz_path + strlen( psz_path)
1015 LIBEXT, strlen( LIBEXT ) ) )
1017 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1018 int64_t i_time = 0, i_size = 0;
1021 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1024 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1028 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1030 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1031 i_size = attrbuf.nFileSizeHigh;
1033 i_size |= attrbuf.nFileSizeLow;
1035 psz_file = psz_path;
1037 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1040 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1042 /* Close the directory */
1043 FindClose( handle );
1046 dir = opendir( psz_dir );
1052 i_dirlen = strlen( psz_dir );
1054 /* Parse the directory and try to load all files it contains. */
1055 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1057 struct stat statbuf;
1061 /* Skip ".", ".." */
1062 if( !*file->d_name || !strcmp( file->d_name, "." )
1063 || !strcmp( file->d_name, ".." ) )
1068 i_len = strlen( file->d_name );
1069 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1070 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1072 i_stat = stat( psz_file, &statbuf );
1073 if( !i_stat && statbuf.st_mode & S_IFDIR )
1075 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1077 else if( i_len > strlen( LIBEXT )
1078 /* We only load files ending with LIBEXT */
1079 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1080 LIBEXT, strlen( LIBEXT ) ) )
1082 int64_t i_time = 0, i_size = 0;
1086 i_time = statbuf.st_mtime;
1087 i_size = statbuf.st_size;
1090 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1096 /* Close the directory */
1102 /*****************************************************************************
1103 * AllocatePluginFile: load a module into memory and initialize it.
1104 *****************************************************************************
1105 * This function loads a dynamically loadable module and allocates a structure
1106 * for its information data. The module can then be handled by module_need
1107 * and module_unneed. It can be removed by DeleteModule.
1108 *****************************************************************************/
1109 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1110 int64_t i_file_time, int64_t i_file_size )
1112 module_t * p_module = NULL;
1113 module_cache_t *p_cache_entry = NULL;
1116 * Check our plugins cache first then load plugin if needed
1119 CacheFind( p_module_bank, psz_file, i_file_time, i_file_size );
1121 if( !p_cache_entry )
1123 p_module = AllocatePlugin( p_this, psz_file );
1127 /* If junk dll, don't try to load it */
1128 if( p_cache_entry->b_junk )
1134 module_config_t *p_item = NULL, *p_end = NULL;
1136 p_module = p_cache_entry->p_module;
1137 p_module->b_loaded = false;
1139 /* For now we force loading if the module's config contains
1140 * callbacks or actions.
1141 * Could be optimized by adding an API call.*/
1142 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1143 p_item < p_end; p_item++ )
1145 if( p_item->pf_callback || p_item->i_action )
1147 p_module = AllocatePlugin( p_this, psz_file );
1151 if( p_module == p_cache_entry->p_module )
1152 p_cache_entry->b_used = true;
1158 /* Everything worked fine !
1159 * The module is ready to be added to the list. */
1160 p_module->b_builtin = false;
1162 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1163 p_module->psz_object_name, p_module->psz_longname ); */
1164 p_module->next = p_module_bank->head;
1165 p_module_bank->head = p_module;
1167 if( !p_module_bank->b_cache )
1170 #define p_bank p_module_bank
1171 /* Add entry to cache */
1173 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1174 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1175 if( !p_bank->pp_cache[p_bank->i_cache] )
1177 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1178 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1179 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1180 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1181 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1182 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1187 return p_module ? 0 : -1;
1190 /*****************************************************************************
1191 * AllocatePlugin: load a module into memory and initialize it.
1192 *****************************************************************************
1193 * This function loads a dynamically loadable module and allocates a structure
1194 * for its information data. The module can then be handled by module_need
1195 * and module_unneed. It can be removed by DeleteModule.
1196 *****************************************************************************/
1197 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1199 module_t * p_module = NULL;
1200 module_handle_t handle;
1202 if( module_Load( p_this, psz_file, &handle ) )
1205 /* Now that we have successfully loaded the module, we can
1206 * allocate a structure for it */
1207 p_module = vlc_module_create( p_this );
1208 if( p_module == NULL )
1210 module_Unload( handle );
1214 /* We need to fill these since they may be needed by module_Call() */
1215 p_module->psz_filename = psz_file;
1216 p_module->handle = handle;
1217 p_module->b_loaded = true;
1219 /* Initialize the module: fill p_module, default config */
1220 if( module_Call( p_this, p_module ) != 0 )
1222 /* We couldn't call module_init() */
1223 module_release( p_module );
1224 module_Unload( handle );
1228 DupModule( p_module );
1229 p_module->psz_filename = strdup( p_module->psz_filename );
1231 /* Everything worked fine ! The module is ready to be added to the list. */
1232 p_module->b_builtin = false;
1237 /*****************************************************************************
1238 * DupModule: make a plugin module standalone.
1239 *****************************************************************************
1240 * This function duplicates all strings in the module, so that the dynamic
1241 * object can be unloaded. It acts recursively on submodules.
1242 *****************************************************************************/
1243 static void DupModule( module_t *p_module )
1247 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1249 *pp_shortcut = strdup( *pp_shortcut );
1252 /* We strdup() these entries so that they are still valid when the
1253 * module is unloaded. */
1254 p_module->psz_capability = strdup( p_module->psz_capability );
1255 p_module->psz_shortname = p_module->psz_shortname ?
1256 strdup( p_module->psz_shortname ) : NULL;
1257 p_module->psz_longname = strdup( p_module->psz_longname );
1258 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1261 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1265 /*****************************************************************************
1266 * UndupModule: free a duplicated module.
1267 *****************************************************************************
1268 * This function frees the allocations done in DupModule().
1269 *****************************************************************************/
1270 static void UndupModule( module_t *p_module )
1274 for (module_t *subm = p_module->submodule; subm; subm = subm->next)
1277 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1279 free( *pp_shortcut );
1282 free( p_module->psz_capability );
1283 FREENULL( p_module->psz_shortname );
1284 free( p_module->psz_longname );
1285 FREENULL( p_module->psz_help );
1288 #endif /* HAVE_DYNAMIC_PLUGINS */
1290 /*****************************************************************************
1291 * AllocateBuiltinModule: initialize a builtin module.
1292 *****************************************************************************
1293 * This function registers a builtin module and allocates a structure
1294 * for its information data. The module can then be handled by module_need
1295 * and module_unneed. It can be removed by DeleteModule.
1296 *****************************************************************************/
1297 static int AllocateBuiltinModule( vlc_object_t * p_this,
1298 int ( *pf_entry ) ( module_t * ) )
1300 module_t * p_module;
1302 /* Now that we have successfully loaded the module, we can
1303 * allocate a structure for it */
1304 p_module = vlc_module_create( p_this );
1305 if( p_module == NULL )
1308 /* Initialize the module : fill p_module->psz_object_name, etc. */
1309 if( pf_entry( p_module ) != 0 )
1311 /* With a well-written module we shouldn't have to print an
1312 * additional error message here, but just make sure. */
1313 msg_Err( p_this, "failed calling entry point in builtin module" );
1314 module_release( p_module );
1318 /* Everything worked fine ! The module is ready to be added to the list. */
1319 p_module->b_builtin = true;
1321 p_module->next = p_module_bank->head;
1322 p_module_bank->head = p_module;
1325 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1326 p_module->psz_object_name, p_module->psz_longname ); */
1331 /*****************************************************************************
1332 * DeleteModule: delete a module and its structure.
1333 *****************************************************************************
1334 * This function can only be called if the module isn't being used.
1335 *****************************************************************************/
1336 static void DeleteModule( module_t * p_module )
1340 /* Unlist the module (if it is in the list) */
1341 module_t **pp_self = &p_module_bank->head;
1342 while (*pp_self != NULL && *pp_self != p_module)
1343 pp_self = &((*pp_self)->next);
1345 *pp_self = p_module->next;
1347 /* We free the structures that we strdup()ed in Allocate*Module(). */
1348 #ifdef HAVE_DYNAMIC_PLUGINS
1349 if( !p_module->b_builtin )
1351 if( p_module->b_loaded && p_module->b_unloadable )
1353 module_Unload( p_module->handle );
1355 UndupModule( p_module );
1356 free( p_module->psz_filename );
1360 /* Free and detach the object's children */
1361 while (p_module->submodule)
1363 module_t *submodule = p_module->submodule;
1364 p_module->submodule = submodule->next;
1365 module_release (submodule);
1368 config_Free( p_module );
1369 module_release( p_module );