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 "modules/configuration.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 )
788 /* Do it in two passes */
789 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
790 for( i = 0 ; i < p_list->i_count; i++)
792 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
793 const char *psz_module_capability = p_module->psz_capability;
794 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
797 psz_ret = malloc( sizeof(char**) * (count+1) );
798 for( i = 0 ; i < p_list->i_count; i++)
800 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
801 const char *psz_module_capability = p_module->psz_capability;
802 if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
803 psz_ret[i] = strdup( p_module->psz_object_name );
805 psz_ret[count] = NULL;
807 vlc_list_release( p_list );
813 /*****************************************************************************
814 * Following functions are local.
815 *****************************************************************************/
817 /*****************************************************************************
818 * AllocateAllPlugins: load all plugin modules we can find.
819 *****************************************************************************/
820 #ifdef HAVE_DYNAMIC_PLUGINS
821 static void AllocateAllPlugins( vlc_object_t *p_this )
823 /* Yes, there are two NULLs because we replace one with "plugin-path". */
824 #if defined( WIN32 ) || defined( UNDER_CE )
825 const char *path[] = { "modules", "", "plugins", NULL, NULL };
827 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
830 const char *const *ppsz_path;
832 /* If the user provided a plugin path, we add it to the list */
833 char *userpath = config_GetPsz( p_this, "plugin-path" );
834 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
836 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
840 if (!**ppsz_path) continue;
842 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
844 /* Handle relative as well as absolute paths */
846 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
847 (*ppsz_path)[1] != ':' )
849 if( (*ppsz_path)[0] != '/' )
852 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
853 vlc_global()->psz_vlcpath, *ppsz_path) )
858 psz_fullpath = strdup( *ppsz_path );
860 if( psz_fullpath == NULL )
863 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
865 /* Don't go deeper than 5 subdirectories */
866 AllocatePluginDir( p_this, psz_fullpath, 5 );
868 free( psz_fullpath );
871 /* Free plugin-path */
875 /*****************************************************************************
876 * AllocatePluginDir: recursively parse a directory to look for plugins
877 *****************************************************************************/
878 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
881 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
887 #if defined( UNDER_CE ) || defined( _MSC_VER )
889 wchar_t psz_wpath[MAX_PATH + 256];
890 wchar_t psz_wdir[MAX_PATH];
892 char psz_path[MAX_PATH + 256];
893 WIN32_FIND_DATA finddata;
899 struct dirent * file;
903 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
908 #if defined( UNDER_CE ) || defined( _MSC_VER )
910 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
912 rc = GetFileAttributes( psz_wdir );
913 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
915 /* Parse all files in the directory */
916 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
918 rc = GetFileAttributes( psz_dir );
919 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
922 /* Parse all files in the directory */
923 sprintf( psz_path, "%s\\*", psz_dir );
926 handle = FindFirstFile( psz_wpath, &finddata );
928 handle = FindFirstFile( psz_path, &finddata );
930 if( handle == INVALID_HANDLE_VALUE )
932 /* Empty directory */
936 /* Parse the directory and try to load all files it contains. */
940 unsigned int i_len = wcslen( finddata.cFileName );
941 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
942 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
944 unsigned int i_len = strlen( finddata.cFileName );
945 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
949 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
950 || !strcmp( finddata.cFileName, ".." ) )
952 if( !FindNextFile( handle, &finddata ) ) break;
957 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
959 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
962 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
964 else if( i_len > strlen( LIBEXT )
965 /* We only load files ending with LIBEXT */
966 && !strncasecmp( psz_path + strlen( psz_path)
968 LIBEXT, strlen( LIBEXT ) ) )
970 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
971 int64_t i_time = 0, i_size = 0;
974 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
977 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
981 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
983 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
984 i_size = attrbuf.nFileSizeHigh;
986 i_size |= attrbuf.nFileSizeLow;
990 AllocatePluginFile( p_this, psz_file, i_time, i_size );
993 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
995 /* Close the directory */
999 dir = opendir( psz_dir );
1005 i_dirlen = strlen( psz_dir );
1007 /* Parse the directory and try to load all files it contains. */
1008 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1010 struct stat statbuf;
1014 /* Skip ".", ".." */
1015 if( !*file->d_name || !strcmp( file->d_name, "." )
1016 || !strcmp( file->d_name, ".." ) )
1021 i_len = strlen( file->d_name );
1022 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1024 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
1026 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
1029 i_stat = stat( psz_file, &statbuf );
1030 if( !i_stat && statbuf.st_mode & S_IFDIR )
1032 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1034 else if( i_len > strlen( LIBEXT )
1035 /* We only load files ending with LIBEXT */
1036 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1037 LIBEXT, strlen( LIBEXT ) ) )
1039 int64_t i_time = 0, i_size = 0;
1043 i_time = statbuf.st_mtime;
1044 i_size = statbuf.st_size;
1047 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1053 /* Close the directory */
1059 /*****************************************************************************
1060 * AllocatePluginFile: load a module into memory and initialize it.
1061 *****************************************************************************
1062 * This function loads a dynamically loadable module and allocates a structure
1063 * for its information data. The module can then be handled by module_Need
1064 * and module_Unneed. It can be removed by DeleteModule.
1065 *****************************************************************************/
1066 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1067 int64_t i_file_time, int64_t i_file_size )
1069 module_t * p_module = NULL;
1070 module_cache_t *p_cache_entry = NULL;
1073 * Check our plugins cache first then load plugin if needed
1076 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1078 if( !p_cache_entry )
1080 p_module = AllocatePlugin( p_this, psz_file );
1084 /* If junk dll, don't try to load it */
1085 if( p_cache_entry->b_junk )
1091 module_config_t *p_item = NULL, *p_end = NULL;
1093 p_module = p_cache_entry->p_module;
1094 p_module->b_loaded = VLC_FALSE;
1096 /* For now we force loading if the module's config contains
1097 * callbacks or actions.
1098 * Could be optimized by adding an API call.*/
1099 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1100 p_item < p_end; p_item++ )
1102 if( p_item->pf_callback || p_item->i_action )
1104 p_module = AllocatePlugin( p_this, psz_file );
1108 if( p_module == p_cache_entry->p_module )
1109 p_cache_entry->b_used = VLC_TRUE;
1115 libvlc_global_data_t *p_libvlc_global = vlc_global();
1117 /* Everything worked fine !
1118 * The module is ready to be added to the list. */
1119 p_module->b_builtin = VLC_FALSE;
1121 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1122 p_module->psz_object_name, p_module->psz_longname ); */
1124 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1126 if( !p_libvlc_global->p_module_bank->b_cache )
1129 #define p_bank p_libvlc_global->p_module_bank
1130 /* Add entry to cache */
1132 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1133 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1134 if( !p_bank->pp_cache[p_bank->i_cache] )
1136 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1137 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1138 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1139 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1140 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1141 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1145 return p_module ? 0 : -1;
1148 /*****************************************************************************
1149 * AllocatePlugin: load a module into memory and initialize it.
1150 *****************************************************************************
1151 * This function loads a dynamically loadable module and allocates a structure
1152 * for its information data. The module can then be handled by module_Need
1153 * and module_Unneed. It can be removed by DeleteModule.
1154 *****************************************************************************/
1155 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1157 module_t * p_module = NULL;
1158 module_handle_t handle;
1160 if( LoadModule( p_this, psz_file, &handle ) )
1163 /* Now that we have successfully loaded the module, we can
1164 * allocate a structure for it */
1165 p_module = vlc_module_create( p_this );
1166 if( p_module == NULL )
1168 msg_Err( p_this, "out of memory" );
1169 CloseModule( handle );
1173 /* We need to fill these since they may be needed by CallEntry() */
1174 p_module->psz_filename = psz_file;
1175 p_module->handle = handle;
1176 p_module->b_loaded = VLC_TRUE;
1178 /* Initialize the module: fill p_module, default config */
1179 if( CallEntry( p_module ) != 0 )
1181 /* We couldn't call module_init() */
1182 vlc_object_destroy( p_module );
1183 CloseModule( handle );
1187 DupModule( p_module );
1188 p_module->psz_filename = strdup( p_module->psz_filename );
1190 /* Everything worked fine ! The module is ready to be added to the list. */
1191 p_module->b_builtin = VLC_FALSE;
1196 /*****************************************************************************
1197 * DupModule: make a plugin module standalone.
1198 *****************************************************************************
1199 * This function duplicates all strings in the module, so that the dynamic
1200 * object can be unloaded. It acts recursively on submodules.
1201 *****************************************************************************/
1202 static void DupModule( module_t *p_module )
1204 const char **pp_shortcut;
1207 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1209 *pp_shortcut = strdup( *pp_shortcut );
1212 /* We strdup() these entries so that they are still valid when the
1213 * module is unloaded. */
1214 p_module->psz_object_name = strdup( p_module->psz_object_name );
1215 p_module->psz_capability = strdup( p_module->psz_capability );
1216 p_module->psz_shortname = p_module->psz_shortname ?
1217 strdup( p_module->psz_shortname ) : NULL;
1218 p_module->psz_longname = strdup( p_module->psz_longname );
1219 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1222 if( p_module->psz_program != NULL )
1224 p_module->psz_program = strdup( p_module->psz_program );
1227 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1229 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1233 /*****************************************************************************
1234 * UndupModule: free a duplicated module.
1235 *****************************************************************************
1236 * This function frees the allocations done in DupModule().
1237 *****************************************************************************/
1238 static void UndupModule( module_t *p_module )
1240 const char **pp_shortcut;
1243 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1245 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1248 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1250 free( (void*)*pp_shortcut );
1253 free( (void*)p_module->psz_object_name );
1254 free( p_module->psz_capability );
1255 free( (void*)p_module->psz_shortname );
1256 free( (void*)p_module->psz_longname );
1257 free( (void*)p_module->psz_help );
1258 free( (void*)p_module->psz_program );
1261 #endif /* HAVE_DYNAMIC_PLUGINS */
1263 /*****************************************************************************
1264 * AllocateBuiltinModule: initialize a builtin module.
1265 *****************************************************************************
1266 * This function registers a builtin module and allocates a structure
1267 * for its information data. The module can then be handled by module_Need
1268 * and module_Unneed. It can be removed by DeleteModule.
1269 *****************************************************************************/
1270 static int AllocateBuiltinModule( vlc_object_t * p_this,
1271 int ( *pf_entry ) ( module_t * ) )
1273 module_t * p_module;
1275 /* Now that we have successfully loaded the module, we can
1276 * allocate a structure for it */
1277 p_module = vlc_module_create( p_this );
1278 if( p_module == NULL )
1280 msg_Err( p_this, "out of memory" );
1284 /* Initialize the module : fill p_module->psz_object_name, etc. */
1285 if( pf_entry( p_module ) != 0 )
1287 /* With a well-written module we shouldn't have to print an
1288 * additional error message here, but just make sure. */
1289 msg_Err( p_this, "failed calling entry point in builtin module" );
1290 vlc_object_destroy( p_module );
1294 /* Everything worked fine ! The module is ready to be added to the list. */
1295 p_module->b_builtin = VLC_TRUE;
1297 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1298 p_module->psz_object_name, p_module->psz_longname ); */
1300 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1305 /*****************************************************************************
1306 * DeleteModule: delete a module and its structure.
1307 *****************************************************************************
1308 * This function can only be called if the module isn't being used.
1309 *****************************************************************************/
1310 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1312 if( !p_module ) return VLC_EGENERIC;
1314 vlc_object_detach( p_module );
1316 /* We free the structures that we strdup()ed in Allocate*Module(). */
1317 #ifdef HAVE_DYNAMIC_PLUGINS
1318 if( !p_module->b_builtin )
1320 if( p_module->b_loaded && p_module->b_unloadable )
1322 CloseModule( p_module->handle );
1324 UndupModule( p_module );
1325 free( p_module->psz_filename );
1329 /* Free and detach the object's children */
1330 while( p_module->i_children )
1332 vlc_object_t *p_this = p_module->pp_children[0];
1333 vlc_object_detach( p_this );
1334 vlc_object_destroy( p_this );
1337 config_Free( p_module );
1338 vlc_object_destroy( p_module );
1343 #ifdef HAVE_DYNAMIC_PLUGINS
1344 /*****************************************************************************
1345 * CallEntry: call an entry point.
1346 *****************************************************************************
1347 * This function calls a symbol given its name and a module structure. The
1348 * symbol MUST refer to a function returning int and taking a module_t* as
1350 *****************************************************************************/
1351 static int CallEntry( module_t * p_module )
1353 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
1354 int (* pf_symbol) ( module_t * p_module );
1356 /* Try to resolve the symbol */
1357 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1359 if( pf_symbol == NULL )
1361 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1362 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1363 psz_name, p_module->psz_filename );
1364 #elif defined(HAVE_DL_WINDOWS)
1365 char *psz_error = GetWindowsError();
1366 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1367 psz_name, p_module->psz_filename, psz_error );
1369 #elif defined(HAVE_DL_DLOPEN)
1370 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1371 psz_name, p_module->psz_filename, dlerror() );
1372 #elif defined(HAVE_DL_SHL_LOAD)
1373 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1374 psz_name, p_module->psz_filename, strerror(errno) );
1376 # error "Something is wrong in modules.c"
1381 /* We can now try to call the symbol */
1382 if( pf_symbol( p_module ) != 0 )
1384 /* With a well-written module we shouldn't have to print an
1385 * additional error message here, but just make sure. */
1386 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1387 psz_name, p_module->psz_filename );
1391 /* Everything worked fine, we can return */
1395 /*****************************************************************************
1396 * LoadModule: loads a dynamic library
1397 *****************************************************************************
1398 * This function loads a dynamically linked library using a system dependant
1399 * method. Will return 0 on success as well as the module handle.
1400 *****************************************************************************/
1401 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1402 module_handle_t *p_handle )
1404 module_handle_t handle;
1406 #if defined(HAVE_DL_DYLD)
1407 NSObjectFileImage image;
1408 NSObjectFileImageReturnCode ret;
1410 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1412 if( ret != NSObjectFileImageSuccess )
1414 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1418 /* Open the dynamic module */
1419 handle = NSLinkModule( image, psz_file,
1420 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1424 NSLinkEditErrors errors;
1425 const char *psz_file, *psz_err;
1427 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1428 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1429 NSDestroyObjectFileImage( image );
1433 /* Destroy our image, we won't need it */
1434 NSDestroyObjectFileImage( image );
1436 #elif defined(HAVE_DL_BEOS)
1437 handle = load_add_on( psz_file );
1440 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1444 #elif defined(HAVE_DL_WINDOWS)
1447 wchar_t psz_wfile[MAX_PATH];
1448 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1449 handle = LoadLibrary( psz_wfile );
1452 handle = LoadLibrary( psz_file );
1454 if( handle == NULL )
1456 char *psz_err = GetWindowsError();
1457 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1462 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1463 /* static is OK, we are called atomically */
1465 # if defined(SYS_LINUX)
1466 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1467 * are going to get namespace collisions when two modules have common
1468 * public symbols, but ALSA is being a pest here. */
1469 if( strstr( psz_file, "alsa_plugin" ) )
1471 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1472 if( handle == NULL )
1474 msg_Warn( p_this, "cannot load module `%s' (%s)",
1475 psz_file, dlerror() );
1481 handle = dlopen( psz_file, RTLD_NOW );
1482 if( handle == NULL )
1484 msg_Warn( p_this, "cannot load module `%s' (%s)",
1485 psz_file, dlerror() );
1489 #elif defined(HAVE_DL_DLOPEN)
1490 # if defined(DL_LAZY)
1491 handle = dlopen( psz_file, DL_LAZY );
1493 handle = dlopen( psz_file, 0 );
1495 if( handle == NULL )
1497 msg_Warn( p_this, "cannot load module `%s' (%s)",
1498 psz_file, dlerror() );
1502 #elif defined(HAVE_DL_SHL_LOAD)
1503 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1504 if( handle == NULL )
1506 msg_Warn( p_this, "cannot load module `%s' (%s)",
1507 psz_file, strerror(errno) );
1512 # error "Something is wrong in modules.c"
1520 /*****************************************************************************
1521 * CloseModule: unload a dynamic library
1522 *****************************************************************************
1523 * This function unloads a previously opened dynamically linked library
1524 * using a system dependant method. No return value is taken in consideration,
1525 * since some libraries sometimes refuse to close properly.
1526 *****************************************************************************/
1527 static void CloseModule( module_handle_t handle )
1529 #if defined(HAVE_DL_DYLD)
1530 NSUnLinkModule( handle, FALSE );
1532 #elif defined(HAVE_DL_BEOS)
1533 unload_add_on( handle );
1535 #elif defined(HAVE_DL_WINDOWS)
1536 FreeLibrary( handle );
1538 #elif defined(HAVE_DL_DLOPEN)
1543 #elif defined(HAVE_DL_SHL_LOAD)
1544 shl_unload( handle );
1550 /*****************************************************************************
1551 * GetSymbol: get a symbol from a dynamic library
1552 *****************************************************************************
1553 * This function queries a loaded library for a symbol specified in a
1554 * string, and returns a pointer to it. We don't check for dlerror() or
1555 * similar functions, since we want a non-NULL symbol anyway.
1556 *****************************************************************************/
1557 static void * _module_getsymbol( module_handle_t, const char * );
1559 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1561 void * p_symbol = _module_getsymbol( handle, psz_function );
1563 /* MacOS X dl library expects symbols to begin with "_". So do
1564 * some other operating systems. That's really lame, but hey, what
1566 if( p_symbol == NULL )
1568 char psz_call[strlen( psz_function ) + 2];
1570 psz_call[ 0 ] = '_';
1571 memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 );
1572 p_symbol = _module_getsymbol( handle, psz_call );
1578 static void * _module_getsymbol( module_handle_t handle,
1579 const char * psz_function )
1581 #if defined(HAVE_DL_DYLD)
1582 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1583 return NSAddressOfSymbol( sym );
1585 #elif defined(HAVE_DL_BEOS)
1587 if( B_OK == get_image_symbol( handle, psz_function,
1588 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1597 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1598 wchar_t psz_real[256];
1599 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1601 return (void *)GetProcAddress( handle, psz_real );
1603 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1604 return (void *)GetProcAddress( handle, (char *)psz_function );
1606 #elif defined(HAVE_DL_DLOPEN)
1607 return dlsym( handle, psz_function );
1609 #elif defined(HAVE_DL_SHL_LOAD)
1611 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1617 #if defined(HAVE_DL_WINDOWS)
1618 static char * GetWindowsError( void )
1620 #if defined(UNDER_CE)
1621 wchar_t psz_tmp[MAX_PATH];
1622 char * psz_buffer = malloc( MAX_PATH );
1624 char * psz_tmp = malloc( MAX_PATH );
1626 int i = 0, i_error = GetLastError();
1628 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1629 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1630 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1632 /* Go to the end of the string */
1633 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1640 #if defined(UNDER_CE)
1641 swprintf( psz_tmp + i, L" (error %i)", i_error );
1642 psz_tmp[ 255 ] = L'\0';
1644 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1645 psz_tmp[ 255 ] = '\0';
1649 #if defined(UNDER_CE)
1650 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1656 #endif /* HAVE_DL_WINDOWS */
1658 /*****************************************************************************
1659 * LoadPluginsCache: loads the plugins cache file
1660 *****************************************************************************
1661 * This function will load the plugin cache if present and valid. This cache
1662 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1663 * actually load the dynamically loadable module.
1664 * This allows us to only fully load plugins when they are actually used.
1665 *****************************************************************************/
1666 static void CacheLoad( vlc_object_t *p_this )
1668 char *psz_filename, *psz_homedir;
1670 int i, j, i_size, i_read;
1671 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1672 char p_cachelang[6], p_lang[6];
1674 module_cache_t **pp_cache = 0;
1675 int32_t i_file_size, i_marker;
1676 libvlc_global_data_t *p_libvlc_global = vlc_global();
1678 psz_homedir = p_this->p_libvlc->psz_homedir;
1681 msg_Err( p_this, "psz_homedir is null" );
1685 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s"DIR_SEP"%s"DIR_SEP"%s",
1686 psz_homedir, CONFIG_DIR, PLUGINSCACHE_DIR, CacheName() );
1689 msg_Err( p_this, "out of memory" );
1693 if( p_libvlc_global->p_module_bank->b_cache_delete )
1695 #if !defined( UNDER_CE )
1696 unlink( psz_filename );
1698 wchar_t psz_wf[MAX_PATH];
1699 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1700 DeleteFile( psz_wf );
1702 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1703 free( psz_filename );
1707 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1709 file = utf8_fopen( psz_filename, "rb" );
1712 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1714 free( psz_filename );
1717 free( psz_filename );
1719 /* Check the file size */
1720 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
1721 if( i_read != sizeof(i_file_size) )
1723 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1729 fseek( file, 0, SEEK_END );
1730 if( ftell( file ) != i_file_size )
1732 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1733 "(corrupted size)" );
1737 fseek( file, sizeof(i_file_size), SEEK_SET );
1739 /* Check the file is a plugins cache */
1740 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1741 i_read = fread( p_cachestring, 1, i_size, file );
1742 if( i_read != i_size ||
1743 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1745 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1750 /* Check Sub-version number */
1751 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1752 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1754 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1755 "(corrupted header)" );
1760 /* Check the language hasn't changed */
1761 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1762 i_read = fread( p_cachelang, 1, i_size, file );
1763 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1765 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1766 "(language changed)" );
1771 /* Check header marker */
1772 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1773 if( i_read != sizeof(i_marker) ||
1774 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1776 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1777 "(corrupted header)" );
1782 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1783 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
1785 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1786 "(file too short)" );
1792 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
1793 malloc( i_cache * sizeof(void *) );
1795 #define LOAD_IMMEDIATE(a) \
1796 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1797 #define LOAD_STRING(a) \
1800 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1801 || ( i_size > 16384 ) ) \
1804 char *psz = malloc( i_size ); \
1805 if( fread( psz, i_size, 1, file ) != 1 ) { \
1809 if( psz[i_size-1] ) { \
1817 for( i = 0; i < i_cache; i++ )
1822 pp_cache[i] = malloc( sizeof(module_cache_t) );
1823 p_libvlc_global->p_module_bank->i_loaded_cache++;
1825 /* Load common info */
1826 LOAD_STRING( pp_cache[i]->psz_file );
1827 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1828 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1829 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1830 pp_cache[i]->b_used = VLC_FALSE;
1832 if( pp_cache[i]->b_junk ) continue;
1834 pp_cache[i]->p_module = vlc_module_create( p_this );
1836 /* Load additional infos */
1837 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1838 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1839 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1840 LOAD_STRING( pp_cache[i]->p_module->psz_help );
1841 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1842 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1844 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1846 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1847 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1848 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1849 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1850 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1851 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1854 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1857 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1859 LOAD_IMMEDIATE( i_submodules );
1861 while( i_submodules-- )
1863 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
1864 LOAD_STRING( p_module->psz_object_name );
1865 LOAD_STRING( p_module->psz_shortname );
1866 LOAD_STRING( p_module->psz_longname );
1867 LOAD_STRING( p_module->psz_help );
1868 LOAD_STRING( p_module->psz_program );
1869 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1871 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1873 LOAD_STRING( p_module->psz_capability );
1874 LOAD_IMMEDIATE( p_module->i_score );
1875 LOAD_IMMEDIATE( p_module->i_cpu );
1876 LOAD_IMMEDIATE( p_module->b_unloadable );
1877 LOAD_IMMEDIATE( p_module->b_reentrant );
1886 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1889 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1896 int CacheLoadConfig( module_t *p_module, FILE *file )
1901 /* Calculate the structure length */
1902 LOAD_IMMEDIATE( p_module->i_config_items );
1903 LOAD_IMMEDIATE( p_module->i_bool_items );
1905 LOAD_IMMEDIATE( i_lines );
1907 /* Allocate memory */
1910 p_module->p_config =
1911 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1912 if( p_module->p_config == NULL )
1914 p_module->confsize = 0;
1915 msg_Err( p_module, "config error: can't duplicate p_config" );
1919 p_module->confsize = i_lines;
1921 /* Do the duplication job */
1922 for (size_t i = 0; i < i_lines; i++ )
1924 LOAD_IMMEDIATE( p_module->p_config[i] );
1926 LOAD_STRING( p_module->p_config[i].psz_type );
1927 LOAD_STRING( p_module->p_config[i].psz_name );
1928 LOAD_STRING( p_module->p_config[i].psz_text );
1929 LOAD_STRING( p_module->p_config[i].psz_longtext );
1930 LOAD_STRING( p_module->p_config[i].psz_current );
1932 if (IsConfigStringType (p_module->p_config[i].i_type))
1934 LOAD_STRING (p_module->p_config[i].orig.psz);
1935 p_module->p_config[i].value.psz =
1936 (p_module->p_config[i].orig.psz != NULL)
1937 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1938 p_module->p_config[i].saved.psz = NULL;
1942 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1943 sizeof (p_module->p_config[i].value));
1944 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1945 sizeof (p_module->p_config[i].saved));
1948 p_module->p_config[i].b_dirty = VLC_FALSE;
1950 p_module->p_config[i].p_lock = &p_module->object_lock;
1952 if( p_module->p_config[i].i_list )
1954 if( p_module->p_config[i].ppsz_list )
1957 p_module->p_config[i].ppsz_list =
1958 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1959 if( p_module->p_config[i].ppsz_list )
1961 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1962 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1963 p_module->p_config[i].ppsz_list[j] = NULL;
1966 if( p_module->p_config[i].ppsz_list_text )
1969 p_module->p_config[i].ppsz_list_text =
1970 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1971 if( p_module->p_config[i].ppsz_list_text )
1973 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1974 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1975 p_module->p_config[i].ppsz_list_text[j] = NULL;
1978 if( p_module->p_config[i].pi_list )
1980 p_module->p_config[i].pi_list =
1981 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1982 if( p_module->p_config[i].pi_list )
1984 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1985 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1990 if( p_module->p_config[i].i_action )
1992 p_module->p_config[i].ppf_action =
1993 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1994 p_module->p_config[i].ppsz_action_text =
1995 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1997 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1999 p_module->p_config[i].ppf_action[j] = 0;
2000 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
2004 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
2011 return VLC_EGENERIC;
2014 /*****************************************************************************
2015 * SavePluginsCache: saves the plugins cache to a file
2016 *****************************************************************************/
2017 static void CacheSave( vlc_object_t *p_this )
2019 static char const psz_tag[] =
2020 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
2021 "# This file is a cache directory tag created by VLC.\r\n"
2022 "# For information about cache directory tags, see:\r\n"
2023 "# http://www.brynosaurus.com/cachedir/\r\n";
2025 char *psz_filename, *psz_homedir;
2028 module_cache_t **pp_cache;
2029 int32_t i_file_size = 0;
2030 libvlc_global_data_t *p_libvlc_global = vlc_global();
2032 psz_homedir = p_this->p_libvlc->psz_homedir;
2035 msg_Err( p_this, "psz_homedir is null" );
2039 (char *)malloc( sizeof(DIR_SEP CONFIG_DIR DIR_SEP PLUGINSCACHE_DIR DIR_SEP ) +
2040 strlen(psz_homedir) + strlen(CacheName()) );
2044 msg_Err( p_this, "out of memory" );
2048 sprintf( psz_filename, "%s"DIR_SEP"%s", psz_homedir, CONFIG_DIR );
2050 config_CreateDir( p_this, psz_filename );
2052 strcat( psz_filename, DIR_SEP PLUGINSCACHE_DIR );
2054 config_CreateDir( p_this, psz_filename );
2056 strcat( psz_filename, DIR_SEP"CACHEDIR.TAG" );
2058 file = utf8_fopen( psz_filename, "wb" );
2061 fwrite( psz_tag, 1, strlen(psz_tag), file );
2065 sprintf( psz_filename, "%s"DIR_SEP"%s"DIR_SEP"%s"DIR_SEP"%s", psz_homedir, CONFIG_DIR,
2066 PLUGINSCACHE_DIR, CacheName() );
2068 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2070 file = utf8_fopen( psz_filename, "wb" );
2073 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2075 free( psz_filename );
2078 free( psz_filename );
2080 /* Empty space for file size */
2081 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2083 /* Contains version number */
2084 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2086 /* Sub-version number (to avoid breakage in the dev version when cache
2087 * structure changes) */
2088 i_file_size = CACHE_SUBVERSION_NUM;
2089 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2092 fprintf( file, "%5.5s", _("C") );
2095 i_file_size = ftell( file );
2096 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2098 i_cache = p_libvlc_global->p_module_bank->i_cache;
2099 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
2101 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2103 #define SAVE_IMMEDIATE(a) \
2104 fwrite( &a, sizeof(char), sizeof(a), file )
2105 #define SAVE_STRING(a) \
2106 { i_size = a ? strlen( a ) + 1 : 0; \
2107 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2108 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2111 for( i = 0; i < i_cache; i++ )
2114 uint32_t i_submodule;
2116 /* Save common info */
2117 SAVE_STRING( pp_cache[i]->psz_file );
2118 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2119 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2120 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2122 if( pp_cache[i]->b_junk ) continue;
2124 /* Save additional infos */
2125 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2126 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2127 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2128 SAVE_STRING( pp_cache[i]->p_module->psz_help );
2129 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2130 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2132 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2134 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2135 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2136 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2137 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2138 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2139 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2142 CacheSaveConfig( pp_cache[i]->p_module, file );
2144 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2146 i_submodule = pp_cache[i]->p_module->i_children;
2147 SAVE_IMMEDIATE( i_submodule );
2148 for( i_submodule = 0;
2149 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2152 module_t *p_module =
2153 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2155 SAVE_STRING( p_module->psz_object_name );
2156 SAVE_STRING( p_module->psz_shortname );
2157 SAVE_STRING( p_module->psz_longname );
2158 SAVE_STRING( p_module->psz_help );
2159 SAVE_STRING( p_module->psz_program );
2160 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2162 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2164 SAVE_STRING( p_module->psz_capability );
2165 SAVE_IMMEDIATE( p_module->i_score );
2166 SAVE_IMMEDIATE( p_module->i_cpu );
2167 SAVE_IMMEDIATE( p_module->b_unloadable );
2168 SAVE_IMMEDIATE( p_module->b_reentrant );
2172 /* Fill-up file size */
2173 i_file_size = ftell( file );
2174 fseek( file, 0, SEEK_SET );
2175 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2182 void CacheSaveConfig( module_t *p_module, FILE *file )
2184 uint32_t i_lines = p_module->confsize;
2187 SAVE_IMMEDIATE( p_module->i_config_items );
2188 SAVE_IMMEDIATE( p_module->i_bool_items );
2189 SAVE_IMMEDIATE( i_lines );
2191 for (size_t i = 0; i < i_lines ; i++)
2193 SAVE_IMMEDIATE( p_module->p_config[i] );
2195 SAVE_STRING( p_module->p_config[i].psz_type );
2196 SAVE_STRING( p_module->p_config[i].psz_name );
2197 SAVE_STRING( p_module->p_config[i].psz_text );
2198 SAVE_STRING( p_module->p_config[i].psz_longtext );
2199 SAVE_STRING( p_module->p_config[i].psz_current );
2200 if (IsConfigStringType (p_module->p_config[i].i_type))
2201 SAVE_STRING( p_module->p_config[i].orig.psz );
2203 if( p_module->p_config[i].i_list )
2205 if( p_module->p_config[i].ppsz_list )
2207 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2208 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2211 if( p_module->p_config[i].ppsz_list_text )
2213 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2214 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2216 if( p_module->p_config[i].pi_list )
2218 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2219 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2223 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2224 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2226 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2230 /*****************************************************************************
2231 * CacheName: Return the cache file name for this platform.
2232 *****************************************************************************/
2233 static char *CacheName( void )
2235 static char psz_cachename[32];
2237 /* Code int size, pointer size and endianness in the filename */
2238 int32_t x = 0xbe00001e;
2239 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2240 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2241 return psz_cachename;
2244 /*****************************************************************************
2245 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2246 *****************************************************************************/
2247 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2248 module_t *p_module )
2253 p_cache->pf_activate = p_module->pf_activate;
2254 p_cache->pf_deactivate = p_module->pf_deactivate;
2255 p_cache->handle = p_module->handle;
2257 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2259 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2260 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2261 p_cchild->pf_activate = p_child->pf_activate;
2262 p_cchild->pf_deactivate = p_child->pf_deactivate;
2265 p_cache->b_loaded = VLC_TRUE;
2266 p_module->b_loaded = VLC_FALSE;
2269 /*****************************************************************************
2270 * CacheFind: finds the cache entry corresponding to a file
2271 *****************************************************************************/
2272 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2273 int64_t i_time, int64_t i_size )
2275 module_cache_t **pp_cache;
2277 libvlc_global_data_t *p_libvlc_global = vlc_global();
2279 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
2280 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
2282 for( i = 0; i < i_cache; i++ )
2284 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2285 pp_cache[i]->i_time == i_time &&
2286 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2292 #endif /* HAVE_DYNAMIC_PLUGINS */