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 *****************************************************************************/
28 #include "../libvlc.h"
30 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
31 * is set to 64. Don't try to be cleverer. */
32 #ifdef _FILE_OFFSET_BITS
33 #undef _FILE_OFFSET_BITS
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if !defined(HAVE_DYNAMIC_PLUGINS)
55 /* no support for plugins */
56 #elif defined(HAVE_DL_DYLD)
57 # if defined(HAVE_MACH_O_DYLD_H)
58 # include <mach-o/dyld.h>
60 #elif defined(HAVE_DL_BEOS)
61 # if defined(HAVE_IMAGE_H)
64 #elif defined(HAVE_DL_WINDOWS)
66 #elif defined(HAVE_DL_DLOPEN)
67 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 # if defined(HAVE_SYS_DL_H)
73 #elif defined(HAVE_DL_SHL_LOAD)
74 # if defined(HAVE_DL_H)
79 #include "config/config.h"
82 #include "vlc_charset.h"
84 #include "modules/modules.h"
85 #include "modules/builtin.h"
87 #if defined( WIN32 ) || defined( UNDER_CE )
88 /* Avoid name collisions */
89 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
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 *, vlc_bool_t );
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static void DupModule ( module_t * );
105 static void UndupModule ( module_t * );
106 static int CallEntry ( module_t * );
107 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
108 static void CloseModule ( module_handle_t );
109 static void * GetSymbol ( module_handle_t, const char * );
110 static void CacheLoad ( vlc_object_t * );
111 static int CacheLoadConfig ( module_t *, FILE * );
112 static void CacheSave ( vlc_object_t * );
113 static void CacheSaveConfig ( module_t *, FILE * );
114 static char * CacheName ( void );
115 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
116 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
118 #if defined(HAVE_DL_WINDOWS)
119 static char * GetWindowsError ( void );
123 static void module_LoadMain( vlc_object_t *p_this );
125 /* Sub-version number
126 * (only used to avoid breakage in dev version when cache structure changes) */
127 #define CACHE_SUBVERSION_NUM 3
129 /*****************************************************************************
130 * module_InitBank: create the module bank.
131 *****************************************************************************
132 * This function creates a module bank structure which will be filled later
133 * on with all the modules found.
134 *****************************************************************************/
135 void __module_InitBank( vlc_object_t *p_this )
137 module_bank_t *p_bank = NULL;
139 libvlc_global_data_t *p_libvlc_global = vlc_global();
141 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
142 var_Get( p_libvlc_global, "libvlc", &lockval );
143 vlc_mutex_lock( lockval.p_address );
144 if( p_libvlc_global->p_module_bank )
146 p_libvlc_global->p_module_bank->i_usage++;
147 vlc_mutex_unlock( lockval.p_address );
148 var_Destroy( p_libvlc_global, "libvlc" );
151 vlc_mutex_unlock( lockval.p_address );
152 var_Destroy( p_libvlc_global, "libvlc" );
154 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
157 p_bank->psz_object_name = "module bank";
159 p_bank->i_cache = p_bank->i_loaded_cache = 0;
160 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
161 p_bank->b_cache = p_bank->b_cache_dirty =
162 p_bank->b_cache_delete = VLC_FALSE;
164 /* Everything worked, attach the object */
165 p_libvlc_global->p_module_bank = p_bank;
166 vlc_object_attach( p_bank, p_libvlc_global );
168 module_LoadMain( p_this );
172 /*****************************************************************************
173 * module_EndBank: empty the module bank.
174 *****************************************************************************
175 * This function unloads all unused plugin modules and empties the module
176 * bank in case of success.
177 *****************************************************************************/
178 void __module_EndBank( vlc_object_t *p_this )
180 module_t * p_next = NULL;
182 libvlc_global_data_t *p_libvlc_global = vlc_global();
184 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
185 var_Get( p_libvlc_global, "libvlc", &lockval );
186 vlc_mutex_lock( lockval.p_address );
187 if( !p_libvlc_global->p_module_bank )
189 vlc_mutex_unlock( lockval.p_address );
190 var_Destroy( p_libvlc_global, "libvlc" );
193 if( --p_libvlc_global->p_module_bank->i_usage )
195 vlc_mutex_unlock( lockval.p_address );
196 var_Destroy( p_libvlc_global, "libvlc" );
199 vlc_mutex_unlock( lockval.p_address );
200 var_Destroy( p_libvlc_global, "libvlc" );
202 config_AutoSaveConfigFile( p_this );
204 #ifdef HAVE_DYNAMIC_PLUGINS
205 # define p_bank p_libvlc_global->p_module_bank
206 if( p_bank->b_cache ) CacheSave( p_this );
207 while( p_bank->i_loaded_cache-- )
209 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
211 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
212 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
213 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
214 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
215 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
218 if( p_bank->pp_loaded_cache )
220 free( p_bank->pp_loaded_cache );
221 p_bank->pp_loaded_cache = NULL;
223 while( p_bank->i_cache-- )
225 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
226 free( p_bank->pp_cache[p_bank->i_cache] );
227 p_bank->pp_cache[p_bank->i_cache] = NULL;
229 if( p_bank->pp_cache )
231 free( p_bank->pp_cache );
232 p_bank->pp_cache = NULL;
237 vlc_object_detach( p_libvlc_global->p_module_bank );
239 while( p_libvlc_global->p_module_bank->i_children )
241 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
243 if( DeleteModule( p_next, VLC_TRUE ) )
245 /* Module deletion failed */
246 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
247 p_next->psz_object_name );
249 /* We just free the module by hand. Niahahahahaha. */
250 vlc_object_detach( p_next );
251 vlc_object_destroy( p_next );
255 vlc_object_destroy( p_libvlc_global->p_module_bank );
256 p_libvlc_global->p_module_bank = NULL;
259 /*****************************************************************************
260 * module_LoadMain: load the main program info into the module bank.
261 *****************************************************************************
262 * This function fills the module bank structure with the main module infos.
263 * This is very useful as it will allow us to consider the main program just
264 * as another module, and for instance the configuration options of main will
265 * be available in the module bank structure just as for every other module.
266 *****************************************************************************/
267 static void module_LoadMain( vlc_object_t *p_this )
270 libvlc_global_data_t *p_libvlc_global = vlc_global();
272 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
273 var_Get( p_libvlc_global, "libvlc", &lockval );
274 vlc_mutex_lock( lockval.p_address );
275 if( p_libvlc_global->p_module_bank->b_main )
277 vlc_mutex_unlock( lockval.p_address );
278 var_Destroy( p_libvlc_global, "libvlc" );
281 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
282 vlc_mutex_unlock( lockval.p_address );
283 var_Destroy( p_libvlc_global, "libvlc" );
285 AllocateBuiltinModule( p_this, vlc_entry__main );
288 /*****************************************************************************
289 * module_LoadBuiltins: load all modules which we built with.
290 *****************************************************************************
291 * This function fills the module bank structure with the builtin modules.
292 *****************************************************************************/
293 void __module_LoadBuiltins( vlc_object_t * p_this )
296 libvlc_global_data_t *p_libvlc_global = vlc_global();
298 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
299 var_Get( p_libvlc_global, "libvlc", &lockval );
300 vlc_mutex_lock( lockval.p_address );
301 if( p_libvlc_global->p_module_bank->b_builtins )
303 vlc_mutex_unlock( lockval.p_address );
304 var_Destroy( p_libvlc_global, "libvlc" );
307 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
308 vlc_mutex_unlock( lockval.p_address );
309 var_Destroy( p_libvlc_global, "libvlc" );
311 msg_Dbg( p_this, "checking builtin modules" );
312 ALLOCATE_ALL_BUILTINS();
315 /*****************************************************************************
316 * module_LoadPlugins: load all plugin modules we can find.
317 *****************************************************************************
318 * This function fills the module bank structure with the plugin modules.
319 *****************************************************************************/
320 void __module_LoadPlugins( vlc_object_t * p_this )
322 #ifdef HAVE_DYNAMIC_PLUGINS
324 libvlc_global_data_t *p_libvlc_global = vlc_global();
326 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
327 var_Get( p_libvlc_global, "libvlc", &lockval );
328 vlc_mutex_lock( lockval.p_address );
329 if( p_libvlc_global->p_module_bank->b_plugins )
331 vlc_mutex_unlock( lockval.p_address );
332 var_Destroy( p_libvlc_global, "libvlc" );
335 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
336 vlc_mutex_unlock( lockval.p_address );
337 var_Destroy( p_libvlc_global, "libvlc" );
339 msg_Dbg( p_this, "checking plugin modules" );
341 if( config_GetInt( p_this, "plugins-cache" ) )
342 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
344 if( p_libvlc_global->p_module_bank->b_cache ||
345 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
347 AllocateAllPlugins( p_this );
351 /*****************************************************************************
352 * module_IsCapable: checks whether a module implements a capability.
353 *****************************************************************************/
354 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
356 return !strcmp( m->psz_capability, cap );
359 /*****************************************************************************
360 * module_GetObjName: internal name of a module.
361 *****************************************************************************/
362 const char *module_GetObjName( const module_t *m )
364 return m->psz_object_name;
367 /*****************************************************************************
368 * module_GetName: human-friendly name of a module.
369 *****************************************************************************/
370 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
372 if( long_name && ( m->psz_longname != NULL) )
373 return m->psz_longname;
375 return m->psz_shortname ?: m->psz_object_name;
378 const char *module_GetHelp( const module_t *m )
383 /*****************************************************************************
384 * module_Need: return the best module function, given a capability list.
385 *****************************************************************************
386 * This function returns the module that best fits the asked capabilities.
387 *****************************************************************************/
388 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
389 const char *psz_name, vlc_bool_t b_strict )
391 typedef struct module_list_t module_list_t;
398 module_list_t *p_next;
401 module_list_t *p_list, *p_first, *p_tmp;
404 int i_which_module, i_index = 0;
405 vlc_bool_t b_intf = VLC_FALSE;
410 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
411 vlc_bool_t b_force_backup = p_this->b_force;
414 /* Deal with variables */
415 if( psz_name && psz_name[0] == '$' )
418 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
419 var_Get( p_this, psz_name + 1, &val );
420 psz_var = val.psz_string;
424 /* Count how many different shortcuts were asked for */
425 if( psz_name && *psz_name )
427 char *psz_parser, *psz_last_shortcut;
429 /* If the user wants none, give him none. */
430 if( !strcmp( psz_name, "none" ) )
437 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
439 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
441 if( *psz_parser == ',' )
445 psz_last_shortcut = psz_parser + 1;
449 /* Check if the user wants to override the "strict" mode */
450 if( psz_last_shortcut )
452 if( !strcmp(psz_last_shortcut, "none") )
457 else if( !strcmp(psz_last_shortcut, "any") )
459 b_strict = VLC_FALSE;
465 /* Sort the modules and test them */
466 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
467 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
469 unsigned i_cpu = vlc_CPU();
471 /* Parse the module list for capabilities and probe each of them */
472 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
474 int i_shortcut_bonus = 0;
476 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
478 /* Test that this module can do what we need */
479 if( !module_IsCapable( p_module, psz_capability ) )
481 /* Don't recurse through the sub-modules because vlc_list_find()
482 * will list them anyway. */
486 /* Test if we have the required CPU */
487 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 )
496 const char *psz_name = psz_shortcuts;
498 /* Let's drop modules with a <= 0 score (unless they are
499 * explicitly requested) */
500 b_trash = p_module->i_score <= 0;
502 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
504 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
507 if( ( c = strchr( psz_name, '@' ) )
508 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
510 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
515 i_shortcut_bonus = i_short * 10000;
520 /* Go to the next shortcut... This is so lame! */
521 psz_name += strlen( psz_name ) + 1;
524 /* If we are in "strict" mode and we couldn't
525 * find the module in the list of provided shortcuts,
526 * then kick the bastard out of here!!! */
530 /* If we didn't require a shortcut, trash <= 0 scored plugins */
531 else if( p_module->i_score <= 0 )
538 /* Special case: test if we requested a particular intf plugin */
539 if( !i_shortcuts && p_module->psz_program
540 && !strcmp( psz_capability, "interface" )
541 && !strcmp( p_module->psz_program,
542 p_this->p_libvlc->psz_object_name ) )
546 /* Remove previous non-matching plugins */
553 /* This one doesn't match */
557 /* Store this new module */
558 p_list[ i_index ].p_module = p_module;
559 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
560 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
562 /* Add it to the modules-to-probe list */
565 p_list[ 0 ].p_next = NULL;
570 /* Ok, so at school you learned that quicksort is quick, and
571 * bubble sort sucks raw eggs. But that's when dealing with
572 * thousands of items. Here we have barely 50. */
573 module_list_t *p_newlist = p_first;
575 if( p_first->i_score < p_list[ i_index ].i_score )
577 p_list[ i_index ].p_next = p_first;
578 p_first = &p_list[ i_index ];
582 while( p_newlist->p_next != NULL &&
583 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
585 p_newlist = p_newlist->p_next;
588 p_list[ i_index ].p_next = p_newlist->p_next;
589 p_newlist->p_next = &p_list[ i_index ];
596 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
597 i_index, i_index == 1 ? "" : "s" );
599 /* Lock all candidate modules */
601 while( p_tmp != NULL )
603 vlc_object_yield( p_tmp->p_module );
604 p_tmp = p_tmp->p_next;
607 /* We can release the list, interesting modules were yielded */
608 vlc_list_release( p_all );
610 /* Parse the linked list and use the first successful module */
612 while( p_tmp != NULL )
614 #ifdef HAVE_DYNAMIC_PLUGINS
615 /* Make sure the module is loaded in mem */
616 module_t *p_module = p_tmp->p_module;
617 if( p_module->b_submodule )
618 p_module = (module_t *)p_module->p_parent;
620 if( !p_module->b_builtin && !p_module->b_loaded )
622 module_t *p_new_module =
623 AllocatePlugin( p_this, p_module->psz_filename );
626 CacheMerge( p_this, p_module, p_new_module );
627 vlc_object_attach( p_new_module, p_module );
628 DeleteModule( p_new_module, VLC_TRUE );
633 p_this->b_force = p_tmp->b_force;
634 if( p_tmp->p_module->pf_activate
635 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
640 vlc_object_release( p_tmp->p_module );
641 p_tmp = p_tmp->p_next;
644 /* Store the locked module value */
647 p_module = p_tmp->p_module;
648 p_tmp = p_tmp->p_next;
655 /* Unlock the remaining modules */
656 while( p_tmp != NULL )
658 vlc_object_release( p_tmp->p_module );
659 p_tmp = p_tmp->p_next;
663 p_this->b_force = b_force_backup;
665 if( p_module != NULL )
667 msg_Dbg( p_this, "using %s module \"%s\"",
668 psz_capability, p_module->psz_object_name );
670 else if( p_first == NULL )
672 if( !strcmp( psz_capability, "access_demux" ) )
674 msg_Warn( p_this, "no %s module matched \"%s\"",
675 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
679 msg_Err( p_this, "no %s module matched \"%s\"",
680 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
682 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
683 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
686 else if( psz_name != NULL && *psz_name )
688 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
689 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
692 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
694 if( p_module && !p_this->psz_object_name )
696 /* This assumes that p_this is the object which will be using the
697 * module. That's not always the case ... but it is in most cases.
700 p_this->psz_object_name = strdup( psz_alias );
702 p_this->psz_object_name = strdup( p_module->psz_object_name );
705 free( psz_shortcuts );
708 /* Don't forget that the module is still locked */
712 /*****************************************************************************
713 * module_Unneed: decrease the usage count of a module.
714 *****************************************************************************
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 *****************************************************************************/
718 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
720 /* Use the close method */
721 if( p_module->pf_deactivate )
723 p_module->pf_deactivate( p_this );
726 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
728 vlc_object_release( p_module );
733 /*****************************************************************************
734 * module_FindName: get a pointer to a module_t given it's name.
735 *****************************************************************************/
736 module_t *__module_FindName( vlc_object_t *p_this, const char * psz_name )
740 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
741 for( i = 0 ; i < p_list->i_count; i++)
743 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
744 const char *psz_module_name = p_module->psz_object_name;
745 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
747 /* We can release the list, and return yes */
748 vlc_object_yield( p_module );
749 vlc_list_release( p_list );
753 vlc_list_release( p_list );
757 /*****************************************************************************
758 * module_Exists: tell if a module exists.
759 *****************************************************************************
760 * This function is a boolean function that tells if a module exist or not.
761 *****************************************************************************/
762 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
764 module_t *p_module = __module_FindName( p_this, psz_name );
767 vlc_object_release( p_module );
776 /*****************************************************************************
777 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
778 * names of the modules that have a certain capability.
779 * Free after uses both the string and the table.
780 *****************************************************************************/
781 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
782 const char * psz_capability,
783 char ***pppsz_longname )
789 /* Do it in two passes */
790 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
791 for( i = 0 ; i < p_list->i_count; i++)
793 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
794 const char *psz_module_capability = p_module->psz_capability;
795 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
798 psz_ret = malloc( sizeof(char*) * (count+1) );
800 *pppsz_longname = malloc( sizeof(char*) * (count+1) );
802 for( i = 0 ; i < p_list->i_count; i++)
804 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
805 const char *psz_module_capability = p_module->psz_capability;
806 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
808 int k = -1; /* hack to handle submodules properly */
809 if( p_module->b_submodule )
811 while( p_module->pp_shortcuts[++k] != NULL );
814 psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
815 :p_module->psz_object_name );
817 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
821 psz_ret[count] = NULL;
823 vlc_list_release( p_list );
829 /*****************************************************************************
830 * Following functions are local.
831 *****************************************************************************/
833 /*****************************************************************************
834 * AllocateAllPlugins: load all plugin modules we can find.
835 *****************************************************************************/
836 #ifdef HAVE_DYNAMIC_PLUGINS
837 static void AllocateAllPlugins( vlc_object_t *p_this )
839 /* Yes, there are two NULLs because we replace one with "plugin-path". */
840 #if defined( WIN32 ) || defined( UNDER_CE )
841 const char *path[] = { "modules", "", "plugins", NULL, NULL };
843 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
846 const char *const *ppsz_path;
848 /* If the user provided a plugin path, we add it to the list */
849 char *userpath = config_GetPsz( p_this, "plugin-path" );
850 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
852 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
856 if (!**ppsz_path) continue;
858 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
860 /* Handle relative as well as absolute paths */
862 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
863 (*ppsz_path)[1] != ':' )
865 if( (*ppsz_path)[0] != '/' )
868 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
869 vlc_global()->psz_vlcpath, *ppsz_path) )
874 psz_fullpath = strdup( *ppsz_path );
876 if( psz_fullpath == NULL )
879 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
881 /* Don't go deeper than 5 subdirectories */
882 AllocatePluginDir( p_this, psz_fullpath, 5 );
884 free( psz_fullpath );
887 /* Free plugin-path */
891 /*****************************************************************************
892 * AllocatePluginDir: recursively parse a directory to look for plugins
893 *****************************************************************************/
894 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
897 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
903 #if defined( UNDER_CE ) || defined( _MSC_VER )
905 wchar_t psz_wpath[MAX_PATH + 256];
906 wchar_t psz_wdir[MAX_PATH];
908 char psz_path[MAX_PATH + 256];
909 WIN32_FIND_DATA finddata;
915 struct dirent * file;
919 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
924 #if defined( UNDER_CE ) || defined( _MSC_VER )
926 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
928 rc = GetFileAttributes( psz_wdir );
929 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
931 /* Parse all files in the directory */
932 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
934 rc = GetFileAttributes( psz_dir );
935 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
938 /* Parse all files in the directory */
939 sprintf( psz_path, "%s\\*", psz_dir );
942 handle = FindFirstFile( psz_wpath, &finddata );
944 handle = FindFirstFile( psz_path, &finddata );
946 if( handle == INVALID_HANDLE_VALUE )
948 /* Empty directory */
952 /* Parse the directory and try to load all files it contains. */
956 unsigned int i_len = wcslen( finddata.cFileName );
957 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
958 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
960 unsigned int i_len = strlen( finddata.cFileName );
961 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
965 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
966 || !strcmp( finddata.cFileName, ".." ) )
968 if( !FindNextFile( handle, &finddata ) ) break;
973 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
975 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
978 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
980 else if( i_len > strlen( LIBEXT )
981 /* We only load files ending with LIBEXT */
982 && !strncasecmp( psz_path + strlen( psz_path)
984 LIBEXT, strlen( LIBEXT ) ) )
986 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
987 int64_t i_time = 0, i_size = 0;
990 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
993 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
997 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
999 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1000 i_size = attrbuf.nFileSizeHigh;
1002 i_size |= attrbuf.nFileSizeLow;
1004 psz_file = psz_path;
1006 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1009 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1011 /* Close the directory */
1012 FindClose( handle );
1015 dir = opendir( psz_dir );
1021 i_dirlen = strlen( psz_dir );
1023 /* Parse the directory and try to load all files it contains. */
1024 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1026 struct stat statbuf;
1030 /* Skip ".", ".." */
1031 if( !*file->d_name || !strcmp( file->d_name, "." )
1032 || !strcmp( file->d_name, ".." ) )
1037 i_len = strlen( file->d_name );
1038 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1040 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
1042 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
1045 i_stat = stat( psz_file, &statbuf );
1046 if( !i_stat && statbuf.st_mode & S_IFDIR )
1048 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1050 else if( i_len > strlen( LIBEXT )
1051 /* We only load files ending with LIBEXT */
1052 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1053 LIBEXT, strlen( LIBEXT ) ) )
1055 int64_t i_time = 0, i_size = 0;
1059 i_time = statbuf.st_mtime;
1060 i_size = statbuf.st_size;
1063 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1069 /* Close the directory */
1075 /*****************************************************************************
1076 * AllocatePluginFile: load a module into memory and initialize it.
1077 *****************************************************************************
1078 * This function loads a dynamically loadable module and allocates a structure
1079 * for its information data. The module can then be handled by module_Need
1080 * and module_Unneed. It can be removed by DeleteModule.
1081 *****************************************************************************/
1082 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1083 int64_t i_file_time, int64_t i_file_size )
1085 module_t * p_module = NULL;
1086 module_cache_t *p_cache_entry = NULL;
1089 * Check our plugins cache first then load plugin if needed
1092 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1094 if( !p_cache_entry )
1096 p_module = AllocatePlugin( p_this, psz_file );
1100 /* If junk dll, don't try to load it */
1101 if( p_cache_entry->b_junk )
1107 module_config_t *p_item = NULL, *p_end = NULL;
1109 p_module = p_cache_entry->p_module;
1110 p_module->b_loaded = VLC_FALSE;
1112 /* For now we force loading if the module's config contains
1113 * callbacks or actions.
1114 * Could be optimized by adding an API call.*/
1115 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1116 p_item < p_end; p_item++ )
1118 if( p_item->pf_callback || p_item->i_action )
1120 p_module = AllocatePlugin( p_this, psz_file );
1124 if( p_module == p_cache_entry->p_module )
1125 p_cache_entry->b_used = VLC_TRUE;
1131 libvlc_global_data_t *p_libvlc_global = vlc_global();
1133 /* Everything worked fine !
1134 * The module is ready to be added to the list. */
1135 p_module->b_builtin = VLC_FALSE;
1137 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1138 p_module->psz_object_name, p_module->psz_longname ); */
1140 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1142 if( !p_libvlc_global->p_module_bank->b_cache )
1145 #define p_bank p_libvlc_global->p_module_bank
1146 /* Add entry to cache */
1148 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1149 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1150 if( !p_bank->pp_cache[p_bank->i_cache] )
1152 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1153 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1154 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1155 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1156 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1157 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1161 return p_module ? 0 : -1;
1164 /*****************************************************************************
1165 * AllocatePlugin: load a module into memory and initialize it.
1166 *****************************************************************************
1167 * This function loads a dynamically loadable module and allocates a structure
1168 * for its information data. The module can then be handled by module_Need
1169 * and module_Unneed. It can be removed by DeleteModule.
1170 *****************************************************************************/
1171 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1173 module_t * p_module = NULL;
1174 module_handle_t handle;
1176 if( LoadModule( p_this, psz_file, &handle ) )
1179 /* Now that we have successfully loaded the module, we can
1180 * allocate a structure for it */
1181 p_module = vlc_module_create( p_this );
1182 if( p_module == NULL )
1184 msg_Err( p_this, "out of memory" );
1185 CloseModule( handle );
1189 /* We need to fill these since they may be needed by CallEntry() */
1190 p_module->psz_filename = psz_file;
1191 p_module->handle = handle;
1192 p_module->b_loaded = VLC_TRUE;
1194 /* Initialize the module: fill p_module, default config */
1195 if( CallEntry( p_module ) != 0 )
1197 /* We couldn't call module_init() */
1198 vlc_object_destroy( p_module );
1199 CloseModule( handle );
1203 DupModule( p_module );
1204 p_module->psz_filename = strdup( p_module->psz_filename );
1206 /* Everything worked fine ! The module is ready to be added to the list. */
1207 p_module->b_builtin = VLC_FALSE;
1212 /*****************************************************************************
1213 * DupModule: make a plugin module standalone.
1214 *****************************************************************************
1215 * This function duplicates all strings in the module, so that the dynamic
1216 * object can be unloaded. It acts recursively on submodules.
1217 *****************************************************************************/
1218 static void DupModule( module_t *p_module )
1220 const char **pp_shortcut;
1223 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1225 *pp_shortcut = strdup( *pp_shortcut );
1228 /* We strdup() these entries so that they are still valid when the
1229 * module is unloaded. */
1230 p_module->psz_object_name = strdup( p_module->psz_object_name );
1231 p_module->psz_capability = strdup( p_module->psz_capability );
1232 p_module->psz_shortname = p_module->psz_shortname ?
1233 strdup( p_module->psz_shortname ) : NULL;
1234 p_module->psz_longname = strdup( p_module->psz_longname );
1235 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1238 if( p_module->psz_program != NULL )
1240 p_module->psz_program = strdup( p_module->psz_program );
1243 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1245 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1249 /*****************************************************************************
1250 * UndupModule: free a duplicated module.
1251 *****************************************************************************
1252 * This function frees the allocations done in DupModule().
1253 *****************************************************************************/
1254 static void UndupModule( module_t *p_module )
1256 const char **pp_shortcut;
1259 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1261 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1264 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1266 free( (void*)*pp_shortcut );
1269 free( (void*)p_module->psz_object_name );
1270 free( p_module->psz_capability );
1271 free( (void*)p_module->psz_shortname );
1272 free( (void*)p_module->psz_longname );
1273 free( (void*)p_module->psz_help );
1274 free( (void*)p_module->psz_program );
1277 #endif /* HAVE_DYNAMIC_PLUGINS */
1279 /*****************************************************************************
1280 * AllocateBuiltinModule: initialize a builtin module.
1281 *****************************************************************************
1282 * This function registers a builtin module and allocates a structure
1283 * for its information data. The module can then be handled by module_Need
1284 * and module_Unneed. It can be removed by DeleteModule.
1285 *****************************************************************************/
1286 static int AllocateBuiltinModule( vlc_object_t * p_this,
1287 int ( *pf_entry ) ( module_t * ) )
1289 module_t * p_module;
1291 /* Now that we have successfully loaded the module, we can
1292 * allocate a structure for it */
1293 p_module = vlc_module_create( p_this );
1294 if( p_module == NULL )
1296 msg_Err( p_this, "out of memory" );
1300 /* Initialize the module : fill p_module->psz_object_name, etc. */
1301 if( pf_entry( p_module ) != 0 )
1303 /* With a well-written module we shouldn't have to print an
1304 * additional error message here, but just make sure. */
1305 msg_Err( p_this, "failed calling entry point in builtin module" );
1306 vlc_object_destroy( p_module );
1310 /* Everything worked fine ! The module is ready to be added to the list. */
1311 p_module->b_builtin = VLC_TRUE;
1313 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1314 p_module->psz_object_name, p_module->psz_longname ); */
1316 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1321 /*****************************************************************************
1322 * DeleteModule: delete a module and its structure.
1323 *****************************************************************************
1324 * This function can only be called if the module isn't being used.
1325 *****************************************************************************/
1326 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1328 if( !p_module ) return VLC_EGENERIC;
1330 vlc_object_detach( p_module );
1332 /* We free the structures that we strdup()ed in Allocate*Module(). */
1333 #ifdef HAVE_DYNAMIC_PLUGINS
1334 if( !p_module->b_builtin )
1336 if( p_module->b_loaded && p_module->b_unloadable )
1338 CloseModule( p_module->handle );
1340 UndupModule( p_module );
1341 free( p_module->psz_filename );
1345 /* Free and detach the object's children */
1346 while( p_module->i_children )
1348 vlc_object_t *p_this = p_module->pp_children[0];
1349 vlc_object_detach( p_this );
1350 vlc_object_destroy( p_this );
1353 config_Free( p_module );
1354 vlc_object_destroy( p_module );
1359 #ifdef HAVE_DYNAMIC_PLUGINS
1360 /*****************************************************************************
1361 * CallEntry: call an entry point.
1362 *****************************************************************************
1363 * This function calls a symbol given its name and a module structure. The
1364 * symbol MUST refer to a function returning int and taking a module_t* as
1366 *****************************************************************************/
1367 static int CallEntry( module_t * p_module )
1369 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
1370 int (* pf_symbol) ( module_t * p_module );
1372 /* Try to resolve the symbol */
1373 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1375 if( pf_symbol == NULL )
1377 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1378 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1379 psz_name, p_module->psz_filename );
1380 #elif defined(HAVE_DL_WINDOWS)
1381 char *psz_error = GetWindowsError();
1382 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1383 psz_name, p_module->psz_filename, psz_error );
1385 #elif defined(HAVE_DL_DLOPEN)
1386 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1387 psz_name, p_module->psz_filename, dlerror() );
1388 #elif defined(HAVE_DL_SHL_LOAD)
1389 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%m)",
1390 psz_name, p_module->psz_filename );
1392 # error "Something is wrong in modules.c"
1397 /* We can now try to call the symbol */
1398 if( pf_symbol( p_module ) != 0 )
1400 /* With a well-written module we shouldn't have to print an
1401 * additional error message here, but just make sure. */
1402 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1403 psz_name, p_module->psz_filename );
1407 /* Everything worked fine, we can return */
1411 /*****************************************************************************
1412 * LoadModule: loads a dynamic library
1413 *****************************************************************************
1414 * This function loads a dynamically linked library using a system dependant
1415 * method. Will return 0 on success as well as the module handle.
1416 *****************************************************************************/
1417 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1418 module_handle_t *p_handle )
1420 module_handle_t handle;
1422 #if defined(HAVE_DL_DYLD)
1423 NSObjectFileImage image;
1424 NSObjectFileImageReturnCode ret;
1426 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1428 if( ret != NSObjectFileImageSuccess )
1430 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1434 /* Open the dynamic module */
1435 handle = NSLinkModule( image, psz_file,
1436 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1440 NSLinkEditErrors errors;
1441 const char *psz_file, *psz_err;
1443 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1444 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1445 NSDestroyObjectFileImage( image );
1449 /* Destroy our image, we won't need it */
1450 NSDestroyObjectFileImage( image );
1452 #elif defined(HAVE_DL_BEOS)
1453 handle = load_add_on( psz_file );
1456 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1460 #elif defined(HAVE_DL_WINDOWS)
1463 wchar_t psz_wfile[MAX_PATH];
1464 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1465 handle = LoadLibrary( psz_wfile );
1468 handle = LoadLibrary( psz_file );
1470 if( handle == NULL )
1472 char *psz_err = GetWindowsError();
1473 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1478 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1479 /* static is OK, we are called atomically */
1480 handle = dlopen( psz_file, RTLD_NOW );
1481 if( handle == NULL )
1483 msg_Warn( p_this, "cannot load module `%s' (%s)",
1484 psz_file, dlerror() );
1488 #elif defined(HAVE_DL_DLOPEN)
1489 # if defined(DL_LAZY)
1490 handle = dlopen( psz_file, DL_LAZY );
1492 handle = dlopen( psz_file, 0 );
1494 if( handle == NULL )
1496 msg_Warn( p_this, "cannot load module `%s' (%s)",
1497 psz_file, dlerror() );
1501 #elif defined(HAVE_DL_SHL_LOAD)
1502 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1503 if( handle == NULL )
1505 msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
1510 # error "Something is wrong in modules.c"
1518 /*****************************************************************************
1519 * CloseModule: unload a dynamic library
1520 *****************************************************************************
1521 * This function unloads a previously opened dynamically linked library
1522 * using a system dependant method. No return value is taken in consideration,
1523 * since some libraries sometimes refuse to close properly.
1524 *****************************************************************************/
1525 static void CloseModule( module_handle_t handle )
1527 #if defined(HAVE_DL_DYLD)
1528 NSUnLinkModule( handle, FALSE );
1530 #elif defined(HAVE_DL_BEOS)
1531 unload_add_on( handle );
1533 #elif defined(HAVE_DL_WINDOWS)
1534 FreeLibrary( handle );
1536 #elif defined(HAVE_DL_DLOPEN)
1541 #elif defined(HAVE_DL_SHL_LOAD)
1542 shl_unload( handle );
1548 /*****************************************************************************
1549 * GetSymbol: get a symbol from a dynamic library
1550 *****************************************************************************
1551 * This function queries a loaded library for a symbol specified in a
1552 * string, and returns a pointer to it. We don't check for dlerror() or
1553 * similar functions, since we want a non-NULL symbol anyway.
1554 *****************************************************************************/
1555 static void * _module_getsymbol( module_handle_t, const char * );
1557 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1559 void * p_symbol = _module_getsymbol( handle, psz_function );
1561 /* MacOS X dl library expects symbols to begin with "_". So do
1562 * some other operating systems. That's really lame, but hey, what
1564 if( p_symbol == NULL )
1566 char psz_call[strlen( psz_function ) + 2];
1568 psz_call[ 0 ] = '_';
1569 memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 );
1570 p_symbol = _module_getsymbol( handle, psz_call );
1576 static void * _module_getsymbol( module_handle_t handle,
1577 const char * psz_function )
1579 #if defined(HAVE_DL_DYLD)
1580 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1581 return NSAddressOfSymbol( sym );
1583 #elif defined(HAVE_DL_BEOS)
1585 if( B_OK == get_image_symbol( handle, psz_function,
1586 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1595 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1596 wchar_t psz_real[256];
1597 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1599 return (void *)GetProcAddress( handle, psz_real );
1601 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1602 return (void *)GetProcAddress( handle, (char *)psz_function );
1604 #elif defined(HAVE_DL_DLOPEN)
1605 return dlsym( handle, psz_function );
1607 #elif defined(HAVE_DL_SHL_LOAD)
1609 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1615 #if defined(HAVE_DL_WINDOWS)
1616 static char * GetWindowsError( void )
1618 #if defined(UNDER_CE)
1619 wchar_t psz_tmp[MAX_PATH];
1620 char * psz_buffer = malloc( MAX_PATH );
1622 char * psz_tmp = malloc( MAX_PATH );
1624 int i = 0, i_error = GetLastError();
1626 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1627 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1628 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1630 /* Go to the end of the string */
1631 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1638 #if defined(UNDER_CE)
1639 swprintf( psz_tmp + i, L" (error %i)", i_error );
1640 psz_tmp[ 255 ] = L'\0';
1642 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1643 psz_tmp[ 255 ] = '\0';
1647 #if defined(UNDER_CE)
1648 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1654 #endif /* HAVE_DL_WINDOWS */
1656 /*****************************************************************************
1657 * LoadPluginsCache: loads the plugins cache file
1658 *****************************************************************************
1659 * This function will load the plugin cache if present and valid. This cache
1660 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1661 * actually load the dynamically loadable module.
1662 * This allows us to only fully load plugins when they are actually used.
1663 *****************************************************************************/
1664 static void CacheLoad( vlc_object_t *p_this )
1666 char *psz_filename, *psz_cachedir;
1668 int i, j, i_size, i_read;
1669 char p_cachestring[sizeof("cache " COPYRIGHT_MESSAGE)];
1670 char p_cachelang[6], p_lang[6];
1672 module_cache_t **pp_cache = 0;
1673 int32_t i_file_size, i_marker;
1674 libvlc_global_data_t *p_libvlc_global = vlc_global();
1676 psz_cachedir = p_this->p_libvlc->psz_cachedir;
1677 if( !psz_cachedir ) /* XXX: this should never happen */
1679 msg_Err( p_this, "Unable to get cache directory" );
1683 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s",
1684 psz_cachedir, CacheName() );
1687 msg_Err( p_this, "out of memory" );
1691 if( p_libvlc_global->p_module_bank->b_cache_delete )
1693 #if !defined( UNDER_CE )
1694 unlink( psz_filename );
1696 wchar_t psz_wf[MAX_PATH];
1697 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1698 DeleteFile( psz_wf );
1700 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1701 free( psz_filename );
1705 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1707 file = utf8_fopen( psz_filename, "rb" );
1710 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1712 free( psz_filename );
1715 free( psz_filename );
1717 /* Check the file size */
1718 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
1719 if( i_read != sizeof(i_file_size) )
1721 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1727 fseek( file, 0, SEEK_END );
1728 if( ftell( file ) != i_file_size )
1730 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1731 "(corrupted size)" );
1735 fseek( file, sizeof(i_file_size), SEEK_SET );
1737 /* Check the file is a plugins cache */
1738 i_size = sizeof("cache " COPYRIGHT_MESSAGE) - 1;
1739 i_read = fread( p_cachestring, 1, i_size, file );
1740 if( i_read != i_size ||
1741 memcmp( p_cachestring, "cache " COPYRIGHT_MESSAGE, i_size ) )
1743 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1748 /* Check Sub-version number */
1749 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1750 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1752 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1753 "(corrupted header)" );
1758 /* Check the language hasn't changed */
1759 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1760 i_read = fread( p_cachelang, 1, i_size, file );
1761 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1763 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1764 "(language changed)" );
1769 /* Check header marker */
1770 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1771 if( i_read != sizeof(i_marker) ||
1772 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1774 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1775 "(corrupted header)" );
1780 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1781 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
1783 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1784 "(file too short)" );
1790 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
1791 malloc( i_cache * sizeof(void *) );
1793 #define LOAD_IMMEDIATE(a) \
1794 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1795 #define LOAD_STRING(a) \
1798 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1799 || ( i_size > 16384 ) ) \
1802 char *psz = malloc( i_size ); \
1803 if( fread( psz, i_size, 1, file ) != 1 ) { \
1807 if( psz[i_size-1] ) { \
1815 for( i = 0; i < i_cache; i++ )
1820 pp_cache[i] = malloc( sizeof(module_cache_t) );
1821 p_libvlc_global->p_module_bank->i_loaded_cache++;
1823 /* Load common info */
1824 LOAD_STRING( pp_cache[i]->psz_file );
1825 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1826 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1827 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1828 pp_cache[i]->b_used = VLC_FALSE;
1830 if( pp_cache[i]->b_junk ) continue;
1832 pp_cache[i]->p_module = vlc_module_create( p_this );
1834 /* Load additional infos */
1835 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1836 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1837 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1838 LOAD_STRING( pp_cache[i]->p_module->psz_help );
1839 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1840 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1842 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1844 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1845 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1846 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1847 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1848 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1849 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1852 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1855 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1857 LOAD_IMMEDIATE( i_submodules );
1859 while( i_submodules-- )
1861 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
1862 LOAD_STRING( p_module->psz_object_name );
1863 LOAD_STRING( p_module->psz_shortname );
1864 LOAD_STRING( p_module->psz_longname );
1865 LOAD_STRING( p_module->psz_help );
1866 LOAD_STRING( p_module->psz_program );
1867 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1869 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1871 LOAD_STRING( p_module->psz_capability );
1872 LOAD_IMMEDIATE( p_module->i_score );
1873 LOAD_IMMEDIATE( p_module->i_cpu );
1874 LOAD_IMMEDIATE( p_module->b_unloadable );
1875 LOAD_IMMEDIATE( p_module->b_reentrant );
1884 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1887 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1894 int CacheLoadConfig( module_t *p_module, FILE *file )
1899 /* Calculate the structure length */
1900 LOAD_IMMEDIATE( p_module->i_config_items );
1901 LOAD_IMMEDIATE( p_module->i_bool_items );
1903 LOAD_IMMEDIATE( i_lines );
1905 /* Allocate memory */
1908 p_module->p_config =
1909 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1910 if( p_module->p_config == NULL )
1912 p_module->confsize = 0;
1913 msg_Err( p_module, "config error: can't duplicate p_config" );
1917 p_module->confsize = i_lines;
1919 /* Do the duplication job */
1920 for (size_t i = 0; i < i_lines; i++ )
1922 LOAD_IMMEDIATE( p_module->p_config[i] );
1924 LOAD_STRING( p_module->p_config[i].psz_type );
1925 LOAD_STRING( p_module->p_config[i].psz_name );
1926 LOAD_STRING( p_module->p_config[i].psz_text );
1927 LOAD_STRING( p_module->p_config[i].psz_longtext );
1928 LOAD_STRING( p_module->p_config[i].psz_current );
1930 if (IsConfigStringType (p_module->p_config[i].i_type))
1932 LOAD_STRING (p_module->p_config[i].orig.psz);
1933 p_module->p_config[i].value.psz =
1934 (p_module->p_config[i].orig.psz != NULL)
1935 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1936 p_module->p_config[i].saved.psz = NULL;
1940 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1941 sizeof (p_module->p_config[i].value));
1942 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1943 sizeof (p_module->p_config[i].saved));
1946 p_module->p_config[i].b_dirty = VLC_FALSE;
1948 p_module->p_config[i].p_lock = &p_module->object_lock;
1950 if( p_module->p_config[i].i_list )
1952 if( p_module->p_config[i].ppsz_list )
1955 p_module->p_config[i].ppsz_list =
1956 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1957 if( p_module->p_config[i].ppsz_list )
1959 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1960 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1961 p_module->p_config[i].ppsz_list[j] = NULL;
1964 if( p_module->p_config[i].ppsz_list_text )
1967 p_module->p_config[i].ppsz_list_text =
1968 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1969 if( p_module->p_config[i].ppsz_list_text )
1971 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1972 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1973 p_module->p_config[i].ppsz_list_text[j] = NULL;
1976 if( p_module->p_config[i].pi_list )
1978 p_module->p_config[i].pi_list =
1979 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1980 if( p_module->p_config[i].pi_list )
1982 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1983 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1988 if( p_module->p_config[i].i_action )
1990 p_module->p_config[i].ppf_action =
1991 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1992 p_module->p_config[i].ppsz_action_text =
1993 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1995 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1997 p_module->p_config[i].ppf_action[j] = 0;
1998 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
2002 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
2009 return VLC_EGENERIC;
2012 /*****************************************************************************
2013 * SavePluginsCache: saves the plugins cache to a file
2014 *****************************************************************************/
2015 static void CacheSave( vlc_object_t *p_this )
2017 static char const psz_tag[] =
2018 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
2019 "# This file is a cache directory tag created by VLC.\r\n"
2020 "# For information about cache directory tags, see:\r\n"
2021 "# http://www.brynosaurus.com/cachedir/\r\n";
2026 module_cache_t **pp_cache;
2027 int32_t i_file_size = 0;
2028 libvlc_global_data_t *p_libvlc_global = vlc_global();
2030 psz_cachedir = p_this->p_libvlc->psz_cachedir;
2031 if( !psz_cachedir ) /* XXX: this should never happen */
2033 msg_Err( p_this, "Unable to get cache directory" );
2037 char psz_filename[sizeof(DIR_SEP) + 32 + strlen(psz_cachedir)];
2038 config_CreateDir( p_this, psz_cachedir );
2040 sprintf( psz_filename, "%s"DIR_SEP"CACHEDIR.TAG", psz_cachedir );
2041 file = utf8_fopen( psz_filename, "wb" );
2044 fwrite( psz_tag, 1, strlen(psz_tag), file );
2048 sprintf( psz_filename, "%s"DIR_SEP"%s", psz_cachedir, CacheName() );
2050 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2052 file = utf8_fopen( psz_filename, "wb" );
2055 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2057 free( psz_filename );
2061 /* Empty space for file size */
2062 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2064 /* Contains version number */
2065 fprintf( file, "%s", "cache " COPYRIGHT_MESSAGE );
2067 /* Sub-version number (to avoid breakage in the dev version when cache
2068 * structure changes) */
2069 i_file_size = CACHE_SUBVERSION_NUM;
2070 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2073 fprintf( file, "%5.5s", _("C") );
2076 i_file_size = ftell( file );
2077 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2079 i_cache = p_libvlc_global->p_module_bank->i_cache;
2080 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
2082 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2084 #define SAVE_IMMEDIATE(a) \
2085 fwrite( &a, sizeof(char), sizeof(a), file )
2086 #define SAVE_STRING(a) \
2087 { i_size = a ? strlen( a ) + 1 : 0; \
2088 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2089 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2092 for( i = 0; i < i_cache; i++ )
2095 uint32_t i_submodule;
2097 /* Save common info */
2098 SAVE_STRING( pp_cache[i]->psz_file );
2099 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2100 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2101 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2103 if( pp_cache[i]->b_junk ) continue;
2105 /* Save additional infos */
2106 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2107 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2108 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2109 SAVE_STRING( pp_cache[i]->p_module->psz_help );
2110 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2111 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2113 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2115 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2116 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2117 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2118 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2119 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2120 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2123 CacheSaveConfig( pp_cache[i]->p_module, file );
2125 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2127 i_submodule = pp_cache[i]->p_module->i_children;
2128 SAVE_IMMEDIATE( i_submodule );
2129 for( i_submodule = 0;
2130 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2133 module_t *p_module =
2134 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2136 SAVE_STRING( p_module->psz_object_name );
2137 SAVE_STRING( p_module->psz_shortname );
2138 SAVE_STRING( p_module->psz_longname );
2139 SAVE_STRING( p_module->psz_help );
2140 SAVE_STRING( p_module->psz_program );
2141 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2143 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2145 SAVE_STRING( p_module->psz_capability );
2146 SAVE_IMMEDIATE( p_module->i_score );
2147 SAVE_IMMEDIATE( p_module->i_cpu );
2148 SAVE_IMMEDIATE( p_module->b_unloadable );
2149 SAVE_IMMEDIATE( p_module->b_reentrant );
2153 /* Fill-up file size */
2154 i_file_size = ftell( file );
2155 fseek( file, 0, SEEK_SET );
2156 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2163 void CacheSaveConfig( module_t *p_module, FILE *file )
2165 uint32_t i_lines = p_module->confsize;
2168 SAVE_IMMEDIATE( p_module->i_config_items );
2169 SAVE_IMMEDIATE( p_module->i_bool_items );
2170 SAVE_IMMEDIATE( i_lines );
2172 for (size_t i = 0; i < i_lines ; i++)
2174 SAVE_IMMEDIATE( p_module->p_config[i] );
2176 SAVE_STRING( p_module->p_config[i].psz_type );
2177 SAVE_STRING( p_module->p_config[i].psz_name );
2178 SAVE_STRING( p_module->p_config[i].psz_text );
2179 SAVE_STRING( p_module->p_config[i].psz_longtext );
2180 SAVE_STRING( p_module->p_config[i].psz_current );
2181 if (IsConfigStringType (p_module->p_config[i].i_type))
2182 SAVE_STRING( p_module->p_config[i].orig.psz );
2184 if( p_module->p_config[i].i_list )
2186 if( p_module->p_config[i].ppsz_list )
2188 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2189 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2192 if( p_module->p_config[i].ppsz_list_text )
2194 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2195 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2197 if( p_module->p_config[i].pi_list )
2199 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2200 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2204 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2205 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2207 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2211 /*****************************************************************************
2212 * CacheName: Return the cache file name for this platform.
2213 *****************************************************************************/
2214 static char *CacheName( void )
2216 static char psz_cachename[32];
2218 /* Code int size, pointer size and endianness in the filename */
2219 int32_t x = 0xbe00001e;
2220 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2221 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2222 return psz_cachename;
2225 /*****************************************************************************
2226 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2227 *****************************************************************************/
2228 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2229 module_t *p_module )
2234 p_cache->pf_activate = p_module->pf_activate;
2235 p_cache->pf_deactivate = p_module->pf_deactivate;
2236 p_cache->handle = p_module->handle;
2238 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2240 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2241 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2242 p_cchild->pf_activate = p_child->pf_activate;
2243 p_cchild->pf_deactivate = p_child->pf_deactivate;
2246 p_cache->b_loaded = VLC_TRUE;
2247 p_module->b_loaded = VLC_FALSE;
2250 /*****************************************************************************
2251 * CacheFind: finds the cache entry corresponding to a file
2252 *****************************************************************************/
2253 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2254 int64_t i_time, int64_t i_size )
2256 module_cache_t **pp_cache;
2258 libvlc_global_data_t *p_libvlc_global = vlc_global();
2260 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
2261 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
2263 for( i = 0; i < i_cache; i++ )
2265 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2266 pp_cache[i]->i_time == i_time &&
2267 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2273 #endif /* HAVE_DYNAMIC_PLUGINS */