1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
34 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
35 * is set to 64. Don't try to be cleverer. */
36 #ifdef _FILE_OFFSET_BITS
37 #undef _FILE_OFFSET_BITS
40 #include <stdlib.h> /* free(), strtol() */
41 #include <stdio.h> /* sprintf() */
42 #include <string.h> /* strdup() */
49 #ifdef HAVE_SYS_TYPES_H
50 # include <sys/types.h>
52 #ifdef HAVE_SYS_STAT_H
53 # include <sys/stat.h>
59 #if !defined(HAVE_DYNAMIC_PLUGINS)
60 /* no support for plugins */
61 #elif defined(HAVE_DL_DYLD)
62 # if defined(HAVE_MACH_O_DYLD_H)
63 # include <mach-o/dyld.h>
65 #elif defined(HAVE_DL_BEOS)
66 # if defined(HAVE_IMAGE_H)
69 #elif defined(HAVE_DL_WINDOWS)
71 #elif defined(HAVE_DL_DLOPEN)
72 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
75 # if defined(HAVE_SYS_DL_H)
78 #elif defined(HAVE_DL_SHL_LOAD)
79 # if defined(HAVE_DL_H)
84 #include "config/configuration.h"
86 #include "vlc_charset.h"
87 #include "vlc_arrays.h"
89 #include "modules/modules.h"
90 #include "modules/builtin.h"
92 /*****************************************************************************
94 *****************************************************************************/
95 #ifdef HAVE_DYNAMIC_PLUGINS
96 static void AllocateAllPlugins ( vlc_object_t * );
97 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
98 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
99 static module_t * AllocatePlugin( vlc_object_t *, char * );
101 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
102 static int DeleteModule ( module_t *, bool );
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static void DupModule ( module_t * );
105 static void UndupModule ( module_t * );
111 * Creates a module bank structure which will be filled later
112 * on with all the modules found.
113 * \param p_this vlc object structure
116 void __module_InitBank( vlc_object_t *p_this )
118 module_bank_t *p_bank = NULL;
119 libvlc_global_data_t *p_libvlc_global = vlc_global();
121 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
123 if( p_libvlc_global->p_module_bank == NULL )
125 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
126 p_bank->psz_object_name = "module bank";
128 p_bank->i_cache = p_bank->i_loaded_cache = 0;
129 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
130 p_bank->b_cache = p_bank->b_cache_dirty =
131 p_bank->b_cache_delete = false;
133 /* Everything worked, attach the object */
134 p_libvlc_global->p_module_bank = p_bank;
135 vlc_object_attach( p_bank, p_libvlc_global );
137 /* Fills the module bank structure with the main module infos.
138 * This is very useful as it will allow us to consider the main
139 * library just as another module, and for instance the configuration
140 * options of main will be available in the module bank structure just
141 * as for every other module. */
142 AllocateBuiltinModule( p_this, vlc_entry__main );
145 p_libvlc_global->p_module_bank->i_usage++;
147 vlc_mutex_unlock( lock );
154 * Unloads all unused plugin modules and empties the module
155 * bank in case of success.
156 * \param p_this vlc object structure
159 void __module_EndBank( vlc_object_t *p_this )
161 module_t * p_next = NULL;
162 libvlc_global_data_t *p_libvlc_global = vlc_global();
164 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
165 if( !p_libvlc_global->p_module_bank )
167 vlc_mutex_unlock( lock );
170 if( --p_libvlc_global->p_module_bank->i_usage )
172 vlc_mutex_unlock( lock );
175 vlc_mutex_unlock( lock );
177 /* Save the configuration */
178 config_AutoSaveConfigFile( p_this );
180 #ifdef HAVE_DYNAMIC_PLUGINS
181 # define p_bank p_libvlc_global->p_module_bank
182 if( p_bank->b_cache ) CacheSave( p_this );
183 while( p_bank->i_loaded_cache-- )
185 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
188 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
189 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
190 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
191 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
192 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
195 if( p_bank->pp_loaded_cache )
197 free( p_bank->pp_loaded_cache );
198 p_bank->pp_loaded_cache = NULL;
200 while( p_bank->i_cache-- )
202 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
203 free( p_bank->pp_cache[p_bank->i_cache] );
204 p_bank->pp_cache[p_bank->i_cache] = NULL;
206 if( p_bank->pp_cache )
208 free( p_bank->pp_cache );
209 p_bank->pp_cache = NULL;
214 vlc_object_detach( p_libvlc_global->p_module_bank );
216 while( p_libvlc_global->p_module_bank->i_children )
218 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
220 if( DeleteModule( p_next, true ) )
222 /* Module deletion failed */
223 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
224 p_next->psz_object_name );
226 /* We just free the module by hand. Niahahahahaha. */
227 vlc_object_detach( p_next );
228 vlc_object_release( p_next );
232 vlc_object_release( p_libvlc_global->p_module_bank );
233 p_libvlc_global->p_module_bank = NULL;
237 * Load all modules which we built with.
239 * Fills the module bank structure with the builtin modules.
240 * \param p_this vlc object structure
243 void __module_LoadBuiltins( vlc_object_t * p_this )
245 libvlc_global_data_t *p_libvlc_global = vlc_global();
247 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
248 if( p_libvlc_global->p_module_bank->b_builtins )
250 vlc_mutex_unlock( lock );
253 p_libvlc_global->p_module_bank->b_builtins = true;
254 vlc_mutex_unlock( lock );
256 msg_Dbg( p_this, "checking builtin modules" );
257 ALLOCATE_ALL_BUILTINS();
263 * Load all plugin modules we can find.
264 * Fills the module bank structure with the plugin modules.
265 * \param p_this vlc object structure
268 void __module_LoadPlugins( vlc_object_t * p_this )
270 #ifdef HAVE_DYNAMIC_PLUGINS
271 libvlc_global_data_t *p_libvlc_global = vlc_global();
273 vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
274 if( p_libvlc_global->p_module_bank->b_plugins )
276 vlc_mutex_unlock( lock );
279 p_libvlc_global->p_module_bank->b_plugins = true;
280 vlc_mutex_unlock( lock );
282 msg_Dbg( p_this, "checking plugin modules" );
284 if( config_GetInt( p_this, "plugins-cache" ) )
285 p_libvlc_global->p_module_bank->b_cache = true;
287 if( p_libvlc_global->p_module_bank->b_cache ||
288 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
290 AllocateAllPlugins( p_this );
295 * Checks whether a module implements a capability.
297 * \param m the module
298 * \param cap the capability to check
299 * \return TRUE if the module have the capability
301 bool module_IsCapable( const module_t *m, const char *cap )
303 return !strcmp( m->psz_capability, cap );
307 * Get the internal name of a module
309 * \param m the module
310 * \return the module name
312 const char *module_GetObjName( const module_t *m )
314 return m->psz_object_name;
318 * Get the human-friendly name of a module.
320 * \param m the module
321 * \param long_name TRUE to have the long name of the module
322 * \return the short or long name of the module
324 const char *module_GetName( const module_t *m, bool long_name )
326 if( long_name && ( m->psz_longname != NULL) )
327 return m->psz_longname;
329 return m->psz_shortname ?: m->psz_object_name;
333 * Get the help for a module
335 * \param m the module
338 const char *module_GetHelp( const module_t *m )
346 * Return the best module function, given a capability list.
347 * \param p_this the vlc object
348 * \param psz_capability list of capabilities needed
349 * \param psz_name name of the module asked
350 * \param b_strict TRUE yto use the strict mode
351 * \return the module or NULL in case of a failure
353 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
354 const char *psz_name, bool b_strict )
356 typedef struct module_list_t module_list_t;
363 module_list_t *p_next;
366 module_list_t *p_list, *p_first, *p_tmp;
369 int i_which_module, i_index = 0;
374 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
375 bool b_force_backup = p_this->b_force;
378 /* Deal with variables */
379 if( psz_name && psz_name[0] == '$' )
382 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
383 var_Get( p_this, psz_name + 1, &val );
384 psz_var = val.psz_string;
388 /* Count how many different shortcuts were asked for */
389 if( psz_name && *psz_name )
391 char *psz_parser, *psz_last_shortcut;
393 /* If the user wants none, give him none. */
394 if( !strcmp( psz_name, "none" ) )
401 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
403 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
405 if( *psz_parser == ',' )
409 psz_last_shortcut = psz_parser + 1;
413 /* Check if the user wants to override the "strict" mode */
414 if( psz_last_shortcut )
416 if( !strcmp(psz_last_shortcut, "none") )
421 else if( !strcmp(psz_last_shortcut, "any") )
429 /* Sort the modules and test them */
430 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
431 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
433 unsigned i_cpu = vlc_CPU();
435 /* Parse the module list for capabilities and probe each of them */
436 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
438 int i_shortcut_bonus = 0;
440 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
442 /* Test that this module can do what we need */
443 if( !module_IsCapable( p_module, psz_capability ) )
445 /* Don't recurse through the sub-modules because vlc_list_find()
446 * will list them anyway. */
450 /* Test if we have the required CPU */
451 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
456 /* If we required a shortcut, check this plugin provides it. */
457 if( i_shortcuts > 0 )
460 const char *psz_name = psz_shortcuts;
462 /* Let's drop modules with a <= 0 score (unless they are
463 * explicitly requested) */
464 b_trash = p_module->i_score <= 0;
466 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
468 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
471 if( ( c = strchr( psz_name, '@' ) )
472 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
474 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
479 i_shortcut_bonus = i_short * 10000;
484 /* Go to the next shortcut... This is so lame! */
485 psz_name += strlen( psz_name ) + 1;
488 /* If we are in "strict" mode and we couldn't
489 * find the module in the list of provided shortcuts,
490 * then kick the bastard out of here!!! */
494 /* If we didn't require a shortcut, trash <= 0 scored plugins */
495 else if( p_module->i_score <= 0 )
502 /* Store this new module */
503 p_list[ i_index ].p_module = p_module;
504 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
505 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
507 /* Add it to the modules-to-probe list */
510 p_list[ 0 ].p_next = NULL;
515 /* Ok, so at school you learned that quicksort is quick, and
516 * bubble sort sucks raw eggs. But that's when dealing with
517 * thousands of items. Here we have barely 50. */
518 module_list_t *p_newlist = p_first;
520 if( p_first->i_score < p_list[ i_index ].i_score )
522 p_list[ i_index ].p_next = p_first;
523 p_first = &p_list[ i_index ];
527 while( p_newlist->p_next != NULL &&
528 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
530 p_newlist = p_newlist->p_next;
533 p_list[ i_index ].p_next = p_newlist->p_next;
534 p_newlist->p_next = &p_list[ i_index ];
541 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
542 i_index, i_index == 1 ? "" : "s" );
544 /* Lock all candidate modules */
546 while( p_tmp != NULL )
548 vlc_object_yield( p_tmp->p_module );
549 p_tmp = p_tmp->p_next;
552 /* We can release the list, interesting modules were yielded */
553 vlc_list_release( p_all );
555 /* Parse the linked list and use the first successful module */
557 while( p_tmp != NULL )
559 #ifdef HAVE_DYNAMIC_PLUGINS
560 /* Make sure the module is loaded in mem */
561 module_t *p_module = p_tmp->p_module;
562 if( p_module->b_submodule )
563 p_module = (module_t *)p_module->p_parent;
565 if( !p_module->b_builtin && !p_module->b_loaded )
567 module_t *p_new_module =
568 AllocatePlugin( p_this, p_module->psz_filename );
571 CacheMerge( p_this, p_module, p_new_module );
572 vlc_object_attach( p_new_module, p_module );
573 DeleteModule( p_new_module, true );
578 p_this->b_force = p_tmp->b_force;
579 if( p_tmp->p_module->pf_activate
580 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
585 vlc_object_release( p_tmp->p_module );
586 p_tmp = p_tmp->p_next;
589 /* Store the locked module value */
592 p_module = p_tmp->p_module;
593 p_tmp = p_tmp->p_next;
600 /* Unlock the remaining modules */
601 while( p_tmp != NULL )
603 vlc_object_release( p_tmp->p_module );
604 p_tmp = p_tmp->p_next;
608 p_this->b_force = b_force_backup;
610 if( p_module != NULL )
612 msg_Dbg( p_this, "using %s module \"%s\"",
613 psz_capability, p_module->psz_object_name );
615 else if( p_first == NULL )
617 if( !strcmp( psz_capability, "access_demux" ) )
619 msg_Warn( p_this, "no %s module matched \"%s\"",
620 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
624 msg_Err( p_this, "no %s module matched \"%s\"",
625 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
627 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
628 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
631 else if( psz_name != NULL && *psz_name )
633 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
634 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
637 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
639 if( p_module && !p_this->psz_object_name )
641 /* This assumes that p_this is the object which will be using the
642 * module. That's not always the case ... but it is in most cases.
644 p_this->psz_object_name = p_module->psz_object_name;
647 free( psz_shortcuts );
650 /* Don't forget that the module is still locked */
657 * This function must be called by the thread that called module_Need, to
658 * decrease the reference count and allow for hiding of modules.
659 * \param p_this vlc object structure
660 * \param p_module the module structure
663 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
665 /* Use the close method */
666 if( p_module->pf_deactivate )
668 p_module->pf_deactivate( p_this );
671 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
673 vlc_object_release( p_module );
679 * Get a pointer to a module_t given it's name.
681 * \param p_this vlc object structure
682 * \param psz_name the name of the module
683 * \return a pointer to the module or NULL in case of a failure
685 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
689 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
690 for( i = 0 ; i < p_list->i_count; i++)
692 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
693 const char *psz_module_name = p_module->psz_object_name;
694 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
696 /* We can release the list, and return yes */
697 vlc_object_yield( p_module );
698 vlc_list_release( p_list );
702 vlc_list_release( p_list );
708 * Release a module_t pointer from module_Find().
710 * \param module the module to release
713 void module_Put( module_t *module )
715 vlc_object_release( module );
720 * Tell if a module exists and release it in thic case
722 * \param p_this vlc object structure
723 * \param psz_name th name of the module
724 * \return TRUE if the module exists
726 bool __module_Exists( vlc_object_t *p_this, const char * psz_name )
728 module_t *p_module = __module_Find( p_this, psz_name );
731 module_Put( p_module );
741 * GetModuleNamesForCapability
743 * Return a NULL terminated array with the names of the modules
744 * that have a certain capability.
745 * Free after uses both the string and the table.
746 * \param p_this vlc object structure
747 * \param psz_capability the capability asked
748 * \param pppsz_longname an pointer to an array of string to contain
749 the long names of the modules. If set to NULL the function don't use it.
750 * \return the NULL terminated array
752 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
753 const char *psz_capability,
754 char ***pppsz_longname )
760 /* Do it in two passes : count the number of modules before */
761 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
762 for( i = 0 ; i < p_list->i_count; i++)
764 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
765 const char *psz_module_capability = p_module->psz_capability;
766 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
770 psz_ret = malloc( sizeof(char*) * (count+1) );
772 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
773 if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
776 free( *pppsz_longname );
777 *pppsz_longname = NULL;
778 vlc_list_release( p_list );
783 for( i = 0 ; i < p_list->i_count; i++)
785 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
786 const char *psz_module_capability = p_module->psz_capability;
787 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
789 int k = -1; /* hack to handle submodules properly */
790 if( p_module->b_submodule )
792 while( p_module->pp_shortcuts[++k] != NULL );
795 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
796 :p_module->psz_object_name );
798 (*pppsz_longname)[j] = strdup( module_GetName( p_module, true ) );
802 psz_ret[count] = NULL;
804 vlc_list_release( p_list );
810 * Get the configuration of a module
812 * \param module the module
813 * \param psize the size of the configuration returned
814 * \return the configuration as an array
816 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
819 unsigned size = module->confsize;
820 module_config_t *config = malloc( size * sizeof( *config ) );
822 assert( psize != NULL );
828 for( i = 0, j = 0; i < size; i++ )
830 const module_config_t *item = module->p_config + i;
831 if( item->b_internal /* internal option */
832 || item->b_unsaveable /* non-modifiable option */
833 || item->b_removed /* removed option */ )
836 memcpy( config + j, item, sizeof( *config ) );
845 * Release the configuration
847 * \param the configuration
850 void module_PutConfig( module_config_t *config )
855 /*****************************************************************************
856 * Following functions are local.
857 *****************************************************************************/
859 /*****************************************************************************
860 * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
862 *****************************************************************************/
863 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
867 bool escaped = false;
871 /* Alloc a buffer to store the path */
872 path = malloc( strlen( paths ) + 1 );
873 if( !path ) return NULL;
875 /* Look for PATH_SEP_CHAR (a ':' or a ';') */
876 for( i = 0; paths[i]; i++ ) {
877 /* Take care of \\ and \: or \; escapement */
882 else if( paths[i] == '\\' )
884 else if( paths[i] == PATH_SEP_CHAR )
891 /* Return the remaining paths */
892 if( remaining_paths ) {
893 *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
899 /*****************************************************************************
900 * AllocateAllPlugins: load all plugin modules we can find.
901 *****************************************************************************/
902 #ifdef HAVE_DYNAMIC_PLUGINS
903 static void AllocateAllPlugins( vlc_object_t *p_this )
907 vlc_array_t *arraypaths = vlc_array_new();
909 /* Contruct the special search path for system that have a relocatable
910 * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
911 #define RETURN_ENOMEM \
913 msg_Err( p_this, "Not enough memory" ); \
917 vlc_array_append( arraypaths, strdup( "modules" ) );
918 #if defined( WIN32 ) || defined( UNDER_CE ) || defined( __APPLE__ ) || defined( SYS_BEOS )
919 if( asprintf( &path, "%s" DIR_SEP "modules",
920 vlc_global()->psz_vlcpath ) < 0 )
922 vlc_array_append( arraypaths, path );
923 if( asprintf( &path, "%s" DIR_SEP "plugins",
924 vlc_global()->psz_vlcpath ) < 0 )
926 vlc_array_append( arraypaths, path );
927 #if ! defined( WIN32 ) && ! defined( UNDER_CE )
928 if( asprintf( &path, "%s", PLUGIN_PATH ) < 0 )
930 vlc_array_append( arraypaths, path );
933 vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
935 vlc_array_append( arraypaths, strdup( "plugins" ) );
937 /* If the user provided a plugin path, we add it to the list */
938 char * userpaths = config_GetPsz( p_this, "plugin-path" );
941 for( paths_iter = userpaths; paths_iter; )
943 path = copy_next_paths_token( paths_iter, &paths_iter );
946 vlc_array_append( arraypaths, strdup( path ) );
949 count = vlc_array_count( arraypaths );
950 for( i = 0 ; i < count ; i++ )
952 path = vlc_array_item_at_index( arraypaths, i );
958 msg_Dbg( p_this, "recursively browsing `%s'", path );
960 /* Don't go deeper than 5 subdirectories */
961 AllocatePluginDir( p_this, path, 5 );
966 vlc_array_destroy( arraypaths );
970 /*****************************************************************************
971 * AllocatePluginDir: recursively parse a directory to look for plugins
972 *****************************************************************************/
973 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
976 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
982 #if defined( UNDER_CE ) || defined( _MSC_VER )
984 wchar_t psz_wpath[MAX_PATH + 256];
985 wchar_t psz_wdir[MAX_PATH];
987 char psz_path[MAX_PATH + 256];
988 WIN32_FIND_DATA finddata;
994 struct dirent * file;
998 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
1003 #if defined( UNDER_CE ) || defined( _MSC_VER )
1005 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
1007 rc = GetFileAttributes( psz_wdir );
1008 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1010 /* Parse all files in the directory */
1011 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
1013 rc = GetFileAttributes( psz_dir );
1014 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
1017 /* Parse all files in the directory */
1018 sprintf( psz_path, "%s\\*", psz_dir );
1021 handle = FindFirstFile( psz_wpath, &finddata );
1023 handle = FindFirstFile( psz_path, &finddata );
1025 if( handle == INVALID_HANDLE_VALUE )
1027 /* Empty directory */
1031 /* Parse the directory and try to load all files it contains. */
1035 unsigned int i_len = wcslen( finddata.cFileName );
1036 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1037 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1039 unsigned int i_len = strlen( finddata.cFileName );
1040 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1043 /* Skip ".", ".." */
1044 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1045 || !strcmp( finddata.cFileName, ".." ) )
1047 if( !FindNextFile( handle, &finddata ) ) break;
1052 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1054 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1057 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1059 else if( i_len > strlen( LIBEXT )
1060 /* We only load files ending with LIBEXT */
1061 && !strncasecmp( psz_path + strlen( psz_path)
1063 LIBEXT, strlen( LIBEXT ) ) )
1065 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1066 int64_t i_time = 0, i_size = 0;
1069 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1072 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1076 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1078 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1079 i_size = attrbuf.nFileSizeHigh;
1081 i_size |= attrbuf.nFileSizeLow;
1083 psz_file = psz_path;
1085 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1088 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1090 /* Close the directory */
1091 FindClose( handle );
1094 dir = opendir( psz_dir );
1100 i_dirlen = strlen( psz_dir );
1102 /* Parse the directory and try to load all files it contains. */
1103 while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1105 struct stat statbuf;
1109 /* Skip ".", ".." */
1110 if( !*file->d_name || !strcmp( file->d_name, "." )
1111 || !strcmp( file->d_name, ".." ) )
1116 i_len = strlen( file->d_name );
1117 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1118 sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1120 i_stat = stat( psz_file, &statbuf );
1121 if( !i_stat && statbuf.st_mode & S_IFDIR )
1123 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1125 else if( i_len > strlen( LIBEXT )
1126 /* We only load files ending with LIBEXT */
1127 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1128 LIBEXT, strlen( LIBEXT ) ) )
1130 int64_t i_time = 0, i_size = 0;
1134 i_time = statbuf.st_mtime;
1135 i_size = statbuf.st_size;
1138 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1144 /* Close the directory */
1150 /*****************************************************************************
1151 * AllocatePluginFile: load a module into memory and initialize it.
1152 *****************************************************************************
1153 * This function loads a dynamically loadable module and allocates a structure
1154 * for its information data. The module can then be handled by module_Need
1155 * and module_Unneed. It can be removed by DeleteModule.
1156 *****************************************************************************/
1157 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1158 int64_t i_file_time, int64_t i_file_size )
1160 module_t * p_module = NULL;
1161 module_cache_t *p_cache_entry = NULL;
1164 * Check our plugins cache first then load plugin if needed
1167 CacheFind( psz_file, i_file_time, i_file_size );
1169 if( !p_cache_entry )
1171 p_module = AllocatePlugin( p_this, psz_file );
1175 /* If junk dll, don't try to load it */
1176 if( p_cache_entry->b_junk )
1182 module_config_t *p_item = NULL, *p_end = NULL;
1184 p_module = p_cache_entry->p_module;
1185 p_module->b_loaded = false;
1187 /* For now we force loading if the module's config contains
1188 * callbacks or actions.
1189 * Could be optimized by adding an API call.*/
1190 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1191 p_item < p_end; p_item++ )
1193 if( p_item->pf_callback || p_item->i_action )
1195 p_module = AllocatePlugin( p_this, psz_file );
1199 if( p_module == p_cache_entry->p_module )
1200 p_cache_entry->b_used = true;
1206 libvlc_global_data_t *p_libvlc_global = vlc_global();
1208 /* Everything worked fine !
1209 * The module is ready to be added to the list. */
1210 p_module->b_builtin = false;
1212 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1213 p_module->psz_object_name, p_module->psz_longname ); */
1215 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1217 if( !p_libvlc_global->p_module_bank->b_cache )
1220 #define p_bank p_libvlc_global->p_module_bank
1221 /* Add entry to cache */
1223 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1224 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1225 if( !p_bank->pp_cache[p_bank->i_cache] )
1227 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1228 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1229 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1230 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1231 p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1232 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1236 return p_module ? 0 : -1;
1239 /*****************************************************************************
1240 * AllocatePlugin: load a module into memory and initialize it.
1241 *****************************************************************************
1242 * This function loads a dynamically loadable module and allocates a structure
1243 * for its information data. The module can then be handled by module_Need
1244 * and module_Unneed. It can be removed by DeleteModule.
1245 *****************************************************************************/
1246 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1248 module_t * p_module = NULL;
1249 module_handle_t handle;
1251 if( module_Load( p_this, psz_file, &handle ) )
1254 /* Now that we have successfully loaded the module, we can
1255 * allocate a structure for it */
1256 p_module = vlc_module_create( p_this );
1257 if( p_module == NULL )
1259 msg_Err( p_this, "out of memory" );
1260 module_Unload( handle );
1264 /* We need to fill these since they may be needed by module_Call() */
1265 p_module->psz_filename = psz_file;
1266 p_module->handle = handle;
1267 p_module->b_loaded = true;
1269 /* Initialize the module: fill p_module, default config */
1270 if( module_Call( p_module ) != 0 )
1272 /* We couldn't call module_init() */
1273 vlc_object_release( p_module );
1274 module_Unload( handle );
1278 DupModule( p_module );
1279 p_module->psz_filename = strdup( p_module->psz_filename );
1281 /* Everything worked fine ! The module is ready to be added to the list. */
1282 p_module->b_builtin = false;
1287 /*****************************************************************************
1288 * DupModule: make a plugin module standalone.
1289 *****************************************************************************
1290 * This function duplicates all strings in the module, so that the dynamic
1291 * object can be unloaded. It acts recursively on submodules.
1292 *****************************************************************************/
1293 static void DupModule( module_t *p_module )
1295 const char **pp_shortcut;
1298 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1300 *pp_shortcut = strdup( *pp_shortcut );
1303 /* We strdup() these entries so that they are still valid when the
1304 * module is unloaded. */
1305 p_module->psz_object_name = strdup( p_module->psz_object_name );
1306 p_module->psz_capability = strdup( p_module->psz_capability );
1307 p_module->psz_shortname = p_module->psz_shortname ?
1308 strdup( p_module->psz_shortname ) : NULL;
1309 p_module->psz_longname = strdup( p_module->psz_longname );
1310 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1313 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1315 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1319 /*****************************************************************************
1320 * UndupModule: free a duplicated module.
1321 *****************************************************************************
1322 * This function frees the allocations done in DupModule().
1323 *****************************************************************************/
1324 static void UndupModule( module_t *p_module )
1326 const char **pp_shortcut;
1329 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1331 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1334 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1336 free( (void*)*pp_shortcut );
1339 free( (void*)p_module->psz_object_name );
1340 free( p_module->psz_capability );
1341 free( (void*)p_module->psz_shortname );
1342 free( (void*)p_module->psz_longname );
1343 free( (void*)p_module->psz_help );
1346 #endif /* HAVE_DYNAMIC_PLUGINS */
1348 /*****************************************************************************
1349 * AllocateBuiltinModule: initialize a builtin module.
1350 *****************************************************************************
1351 * This function registers a builtin module and allocates a structure
1352 * for its information data. The module can then be handled by module_Need
1353 * and module_Unneed. It can be removed by DeleteModule.
1354 *****************************************************************************/
1355 static int AllocateBuiltinModule( vlc_object_t * p_this,
1356 int ( *pf_entry ) ( module_t * ) )
1358 module_t * p_module;
1360 /* Now that we have successfully loaded the module, we can
1361 * allocate a structure for it */
1362 p_module = vlc_module_create( p_this );
1363 if( p_module == NULL )
1365 msg_Err( p_this, "out of memory" );
1369 /* Initialize the module : fill p_module->psz_object_name, etc. */
1370 if( pf_entry( p_module ) != 0 )
1372 /* With a well-written module we shouldn't have to print an
1373 * additional error message here, but just make sure. */
1374 msg_Err( p_this, "failed calling entry point in builtin module" );
1375 vlc_object_release( p_module );
1379 /* Everything worked fine ! The module is ready to be added to the list. */
1380 p_module->b_builtin = true;
1382 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1383 p_module->psz_object_name, p_module->psz_longname ); */
1385 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1390 /*****************************************************************************
1391 * DeleteModule: delete a module and its structure.
1392 *****************************************************************************
1393 * This function can only be called if the module isn't being used.
1394 *****************************************************************************/
1395 static int DeleteModule( module_t * p_module, bool b_detach )
1397 if( !p_module ) return VLC_EGENERIC;
1399 vlc_object_detach( p_module );
1401 /* We free the structures that we strdup()ed in Allocate*Module(). */
1402 #ifdef HAVE_DYNAMIC_PLUGINS
1403 if( !p_module->b_builtin )
1405 if( p_module->b_loaded && p_module->b_unloadable )
1407 module_Unload( p_module->handle );
1409 UndupModule( p_module );
1410 free( p_module->psz_filename );
1414 /* Free and detach the object's children */
1415 while( p_module->i_children )
1417 vlc_object_t *p_this = p_module->pp_children[0];
1418 vlc_object_detach( p_this );
1419 vlc_object_release( p_this );
1422 config_Free( p_module );
1423 vlc_object_release( p_module );