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_interface.h"
83 #include "vlc_playlist.h"
85 #include "vlc_stream.h"
86 #include "vlc_access.h"
87 #include "vlc_demux.h"
88 #include "vlc_codec.h"
91 #include "vlc_vout_synchro.h"
96 #include "vlc_httpd.h"
103 #include "iso_lang.h"
104 #include "vlc_charset.h"
106 #include "vlc_block.h"
110 #include "vlc_image.h"
113 #include "vlc_update.h"
114 #include "vlc_strings.h"
115 #include "vlc_streaming.h"
117 #include "modules/modules.h"
118 #include "modules/builtin.h"
120 #include "vlc_network.h"
122 #if defined( WIN32 ) || defined( UNDER_CE )
123 /* Avoid name collisions */
124 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
127 /*****************************************************************************
129 *****************************************************************************/
130 #ifdef HAVE_DYNAMIC_PLUGINS
131 static void AllocateAllPlugins ( vlc_object_t * );
132 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
133 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
134 static module_t * AllocatePlugin( vlc_object_t *, char * );
136 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
137 static int DeleteModule ( module_t *, vlc_bool_t );
138 #ifdef HAVE_DYNAMIC_PLUGINS
139 static void DupModule ( module_t * );
140 static void UndupModule ( module_t * );
141 static int CallEntry ( module_t * );
142 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
143 static void CloseModule ( module_handle_t );
144 static void * GetSymbol ( module_handle_t, const char * );
145 static void CacheLoad ( vlc_object_t * );
146 static int CacheLoadConfig ( module_t *, FILE * );
147 static void CacheSave ( vlc_object_t * );
148 static void CacheSaveConfig ( module_t *, FILE * );
149 static char * CacheName ( void );
150 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
151 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
153 #if defined(HAVE_DL_WINDOWS)
154 static char * GetWindowsError ( void );
158 static void module_LoadMain( vlc_object_t *p_this );
160 /* Sub-version number
161 * (only used to avoid breakage in dev version when cache structure changes) */
162 #define CACHE_SUBVERSION_NUM 3
164 /*****************************************************************************
165 * module_InitBank: create the module bank.
166 *****************************************************************************
167 * This function creates a module bank structure which will be filled later
168 * on with all the modules found.
169 *****************************************************************************/
170 void __module_InitBank( vlc_object_t *p_this )
172 module_bank_t *p_bank = NULL;
174 libvlc_global_data_t *p_libvlc_global = vlc_global();
176 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
177 var_Get( p_libvlc_global, "libvlc", &lockval );
178 vlc_mutex_lock( lockval.p_address );
179 if( p_libvlc_global->p_module_bank )
181 p_libvlc_global->p_module_bank->i_usage++;
182 vlc_mutex_unlock( lockval.p_address );
183 var_Destroy( p_libvlc_global, "libvlc" );
186 vlc_mutex_unlock( lockval.p_address );
187 var_Destroy( p_libvlc_global, "libvlc" );
189 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
192 p_bank->psz_object_name = "module bank";
194 p_bank->i_cache = p_bank->i_loaded_cache = 0;
195 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
196 p_bank->b_cache = p_bank->b_cache_dirty =
197 p_bank->b_cache_delete = VLC_FALSE;
199 /* Everything worked, attach the object */
200 p_libvlc_global->p_module_bank = p_bank;
201 vlc_object_attach( p_bank, p_libvlc_global );
203 module_LoadMain( p_this );
207 /*****************************************************************************
208 * module_EndBank: empty the module bank.
209 *****************************************************************************
210 * This function unloads all unused plugin modules and empties the module
211 * bank in case of success.
212 *****************************************************************************/
213 void __module_EndBank( vlc_object_t *p_this )
215 module_t * p_next = NULL;
217 libvlc_global_data_t *p_libvlc_global = vlc_global();
219 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
220 var_Get( p_libvlc_global, "libvlc", &lockval );
221 vlc_mutex_lock( lockval.p_address );
222 if( !p_libvlc_global->p_module_bank )
224 vlc_mutex_unlock( lockval.p_address );
225 var_Destroy( p_libvlc_global, "libvlc" );
228 if( --p_libvlc_global->p_module_bank->i_usage )
230 vlc_mutex_unlock( lockval.p_address );
231 var_Destroy( p_libvlc_global, "libvlc" );
234 vlc_mutex_unlock( lockval.p_address );
235 var_Destroy( p_libvlc_global, "libvlc" );
237 config_AutoSaveConfigFile( p_this );
239 #ifdef HAVE_DYNAMIC_PLUGINS
240 # define p_bank p_libvlc_global->p_module_bank
241 if( p_bank->b_cache ) CacheSave( p_this );
242 while( p_bank->i_loaded_cache-- )
244 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
246 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
247 p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
248 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
249 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
250 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
253 if( p_bank->pp_loaded_cache )
255 free( p_bank->pp_loaded_cache );
256 p_bank->pp_loaded_cache = NULL;
258 while( p_bank->i_cache-- )
260 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
261 free( p_bank->pp_cache[p_bank->i_cache] );
262 p_bank->pp_cache[p_bank->i_cache] = NULL;
264 if( p_bank->pp_cache )
266 free( p_bank->pp_cache );
267 p_bank->pp_cache = NULL;
272 vlc_object_detach( p_libvlc_global->p_module_bank );
274 while( p_libvlc_global->p_module_bank->i_children )
276 p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
278 if( DeleteModule( p_next, VLC_TRUE ) )
280 /* Module deletion failed */
281 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
282 p_next->psz_object_name );
284 /* We just free the module by hand. Niahahahahaha. */
285 vlc_object_detach( p_next );
286 vlc_object_destroy( p_next );
290 vlc_object_destroy( p_libvlc_global->p_module_bank );
291 p_libvlc_global->p_module_bank = NULL;
294 /*****************************************************************************
295 * module_LoadMain: load the main program info into the module bank.
296 *****************************************************************************
297 * This function fills the module bank structure with the main module infos.
298 * This is very useful as it will allow us to consider the main program just
299 * as another module, and for instance the configuration options of main will
300 * be available in the module bank structure just as for every other module.
301 *****************************************************************************/
302 static void module_LoadMain( vlc_object_t *p_this )
305 libvlc_global_data_t *p_libvlc_global = vlc_global();
307 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
308 var_Get( p_libvlc_global, "libvlc", &lockval );
309 vlc_mutex_lock( lockval.p_address );
310 if( p_libvlc_global->p_module_bank->b_main )
312 vlc_mutex_unlock( lockval.p_address );
313 var_Destroy( p_libvlc_global, "libvlc" );
316 p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
317 vlc_mutex_unlock( lockval.p_address );
318 var_Destroy( p_libvlc_global, "libvlc" );
320 AllocateBuiltinModule( p_this, vlc_entry__main );
323 /*****************************************************************************
324 * module_LoadBuiltins: load all modules which we built with.
325 *****************************************************************************
326 * This function fills the module bank structure with the builtin modules.
327 *****************************************************************************/
328 void __module_LoadBuiltins( vlc_object_t * p_this )
331 libvlc_global_data_t *p_libvlc_global = vlc_global();
333 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
334 var_Get( p_libvlc_global, "libvlc", &lockval );
335 vlc_mutex_lock( lockval.p_address );
336 if( p_libvlc_global->p_module_bank->b_builtins )
338 vlc_mutex_unlock( lockval.p_address );
339 var_Destroy( p_libvlc_global, "libvlc" );
342 p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
343 vlc_mutex_unlock( lockval.p_address );
344 var_Destroy( p_libvlc_global, "libvlc" );
346 msg_Dbg( p_this, "checking builtin modules" );
347 ALLOCATE_ALL_BUILTINS();
350 /*****************************************************************************
351 * module_LoadPlugins: load all plugin modules we can find.
352 *****************************************************************************
353 * This function fills the module bank structure with the plugin modules.
354 *****************************************************************************/
355 void __module_LoadPlugins( vlc_object_t * p_this )
357 #ifdef HAVE_DYNAMIC_PLUGINS
359 libvlc_global_data_t *p_libvlc_global = vlc_global();
361 var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
362 var_Get( p_libvlc_global, "libvlc", &lockval );
363 vlc_mutex_lock( lockval.p_address );
364 if( p_libvlc_global->p_module_bank->b_plugins )
366 vlc_mutex_unlock( lockval.p_address );
367 var_Destroy( p_libvlc_global, "libvlc" );
370 p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
371 vlc_mutex_unlock( lockval.p_address );
372 var_Destroy( p_libvlc_global, "libvlc" );
374 msg_Dbg( p_this, "checking plugin modules" );
376 if( config_GetInt( p_this, "plugins-cache" ) )
377 p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
379 if( p_libvlc_global->p_module_bank->b_cache ||
380 p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
382 AllocateAllPlugins( p_this );
386 /*****************************************************************************
387 * module_IsCapable: checks whether a module implements a capability.
388 *****************************************************************************/
389 vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
391 return !strcmp( m->psz_capability, cap );
394 /*****************************************************************************
395 * module_GetObjName: internal name of a module.
396 *****************************************************************************/
397 const char *module_GetObjName( const module_t *m )
399 return m->psz_object_name;
402 /*****************************************************************************
403 * module_GetName: human-friendly name of a module.
404 *****************************************************************************/
405 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
407 if( long_name && ( m->psz_longname != NULL) )
408 return m->psz_longname;
410 return m->psz_shortname ?: m->psz_object_name;
413 const char *module_GetHelp( const module_t *m )
418 /*****************************************************************************
419 * module_Need: return the best module function, given a capability list.
420 *****************************************************************************
421 * This function returns the module that best fits the asked capabilities.
422 *****************************************************************************/
423 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
424 const char *psz_name, vlc_bool_t b_strict )
426 typedef struct module_list_t module_list_t;
433 module_list_t *p_next;
436 module_list_t *p_list, *p_first, *p_tmp;
439 int i_which_module, i_index = 0;
440 vlc_bool_t b_intf = VLC_FALSE;
445 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
446 vlc_bool_t b_force_backup = p_this->b_force;
449 /* Deal with variables */
450 if( psz_name && psz_name[0] == '$' )
453 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
454 var_Get( p_this, psz_name + 1, &val );
455 psz_var = val.psz_string;
459 /* Count how many different shortcuts were asked for */
460 if( psz_name && *psz_name )
462 char *psz_parser, *psz_last_shortcut;
464 /* If the user wants none, give him none. */
465 if( !strcmp( psz_name, "none" ) )
472 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
474 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
476 if( *psz_parser == ',' )
480 psz_last_shortcut = psz_parser + 1;
484 /* Check if the user wants to override the "strict" mode */
485 if( psz_last_shortcut )
487 if( !strcmp(psz_last_shortcut, "none") )
492 else if( !strcmp(psz_last_shortcut, "any") )
494 b_strict = VLC_FALSE;
500 /* Sort the modules and test them */
501 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
502 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
504 unsigned i_cpu = vlc_CPU();
506 /* Parse the module list for capabilities and probe each of them */
507 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
509 int i_shortcut_bonus = 0;
511 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
513 /* Test that this module can do what we need */
514 if( !module_IsCapable( p_module, psz_capability ) )
516 /* Don't recurse through the sub-modules because vlc_list_find()
517 * will list them anyway. */
521 /* Test if we have the required CPU */
522 if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
527 /* If we required a shortcut, check this plugin provides it. */
528 if( i_shortcuts > 0 )
531 const char *psz_name = psz_shortcuts;
533 /* Let's drop modules with a <= 0 score (unless they are
534 * explicitly requested) */
535 b_trash = p_module->i_score <= 0;
537 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
539 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
542 if( ( c = strchr( psz_name, '@' ) )
543 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
545 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
550 i_shortcut_bonus = i_short * 10000;
555 /* Go to the next shortcut... This is so lame! */
556 psz_name += strlen( psz_name ) + 1;
559 /* If we are in "strict" mode and we couldn't
560 * find the module in the list of provided shortcuts,
561 * then kick the bastard out of here!!! */
565 /* If we didn't require a shortcut, trash <= 0 scored plugins */
566 else if( p_module->i_score <= 0 )
573 /* Special case: test if we requested a particular intf plugin */
574 if( !i_shortcuts && p_module->psz_program
575 && !strcmp( psz_capability, "interface" )
576 && !strcmp( p_module->psz_program,
577 p_this->p_libvlc->psz_object_name ) )
581 /* Remove previous non-matching plugins */
588 /* This one doesn't match */
592 /* Store this new module */
593 p_list[ i_index ].p_module = p_module;
594 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
595 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
597 /* Add it to the modules-to-probe list */
600 p_list[ 0 ].p_next = NULL;
605 /* Ok, so at school you learned that quicksort is quick, and
606 * bubble sort sucks raw eggs. But that's when dealing with
607 * thousands of items. Here we have barely 50. */
608 module_list_t *p_newlist = p_first;
610 if( p_first->i_score < p_list[ i_index ].i_score )
612 p_list[ i_index ].p_next = p_first;
613 p_first = &p_list[ i_index ];
617 while( p_newlist->p_next != NULL &&
618 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
620 p_newlist = p_newlist->p_next;
623 p_list[ i_index ].p_next = p_newlist->p_next;
624 p_newlist->p_next = &p_list[ i_index ];
631 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
632 i_index, i_index == 1 ? "" : "s" );
634 /* Lock all candidate modules */
636 while( p_tmp != NULL )
638 vlc_object_yield( p_tmp->p_module );
639 p_tmp = p_tmp->p_next;
642 /* We can release the list, interesting modules were yielded */
643 vlc_list_release( p_all );
645 /* Parse the linked list and use the first successful module */
647 while( p_tmp != NULL )
649 #ifdef HAVE_DYNAMIC_PLUGINS
650 /* Make sure the module is loaded in mem */
651 module_t *p_module = p_tmp->p_module;
652 if( p_module->b_submodule )
653 p_module = (module_t *)p_module->p_parent;
655 if( !p_module->b_builtin && !p_module->b_loaded )
657 module_t *p_new_module =
658 AllocatePlugin( p_this, p_module->psz_filename );
661 CacheMerge( p_this, p_module, p_new_module );
662 vlc_object_attach( p_new_module, p_module );
663 DeleteModule( p_new_module, VLC_TRUE );
668 p_this->b_force = p_tmp->b_force;
669 if( p_tmp->p_module->pf_activate
670 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
675 vlc_object_release( p_tmp->p_module );
676 p_tmp = p_tmp->p_next;
679 /* Store the locked module value */
682 p_module = p_tmp->p_module;
683 p_tmp = p_tmp->p_next;
690 /* Unlock the remaining modules */
691 while( p_tmp != NULL )
693 vlc_object_release( p_tmp->p_module );
694 p_tmp = p_tmp->p_next;
698 p_this->b_force = b_force_backup;
700 if( p_module != NULL )
702 msg_Dbg( p_this, "using %s module \"%s\"",
703 psz_capability, p_module->psz_object_name );
705 else if( p_first == NULL )
707 if( !strcmp( psz_capability, "access_demux" ) )
709 msg_Warn( p_this, "no %s module matched \"%s\"",
710 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
714 msg_Err( p_this, "no %s module matched \"%s\"",
715 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
717 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
718 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
721 else if( psz_name != NULL && *psz_name )
723 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
724 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
727 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
729 if( p_module && !p_this->psz_object_name )
731 /* This assumes that p_this is the object which will be using the
732 * module. That's not always the case ... but it is in most cases.
735 p_this->psz_object_name = strdup( psz_alias );
737 p_this->psz_object_name = strdup( p_module->psz_object_name );
740 free( psz_shortcuts );
743 /* Don't forget that the module is still locked */
747 /*****************************************************************************
748 * module_Unneed: decrease the usage count of a module.
749 *****************************************************************************
750 * This function must be called by the thread that called module_Need, to
751 * decrease the reference count and allow for hiding of modules.
752 *****************************************************************************/
753 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
755 /* Use the close method */
756 if( p_module->pf_deactivate )
758 p_module->pf_deactivate( p_this );
761 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
763 vlc_object_release( p_module );
768 /*****************************************************************************
769 * module_FindName: get a pointer to a module_t given it's name.
770 *****************************************************************************/
771 module_t *__module_FindName( vlc_object_t *p_this, const char * psz_name )
775 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
776 for( i = 0 ; i < p_list->i_count; i++)
778 module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
779 const char *psz_module_name = p_module->psz_object_name;
780 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
782 /* We can release the list, and return yes */
783 vlc_list_release( p_list );
784 vlc_object_yield( p_module );
788 vlc_list_release( p_list );
792 /*****************************************************************************
793 * module_Exists: tell if a module exists.
794 *****************************************************************************
795 * This function is a boolean function that tells if a module exist or not.
796 *****************************************************************************/
797 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
799 module_t *p_module = __module_FindName( p_this, psz_name );
802 vlc_object_release( p_module );
812 /*****************************************************************************
813 * Following functions are local.
814 *****************************************************************************/
816 /*****************************************************************************
817 * AllocateAllPlugins: load all plugin modules we can find.
818 *****************************************************************************/
819 #ifdef HAVE_DYNAMIC_PLUGINS
820 static void AllocateAllPlugins( vlc_object_t *p_this )
822 /* Yes, there are two NULLs because we replace one with "plugin-path". */
823 #if defined( WIN32 ) || defined( UNDER_CE )
824 const char *path[] = { "modules", "", "plugins", NULL, NULL };
826 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
829 const char *const *ppsz_path;
831 /* If the user provided a plugin path, we add it to the list */
832 char *userpath = config_GetPsz( p_this, "plugin-path" );
833 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
835 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
839 if (!**ppsz_path) continue;
841 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
843 /* Handle relative as well as absolute paths */
845 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
846 (*ppsz_path)[1] != ':' )
848 if( (*ppsz_path)[0] != '/' )
851 if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
852 vlc_global()->psz_vlcpath, *ppsz_path) )
857 psz_fullpath = strdup( *ppsz_path );
859 if( psz_fullpath == NULL )
862 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
864 /* Don't go deeper than 5 subdirectories */
865 AllocatePluginDir( p_this, psz_fullpath, 5 );
867 free( psz_fullpath );
870 /* Free plugin-path */
874 /*****************************************************************************
875 * AllocatePluginDir: recursively parse a directory to look for plugins
876 *****************************************************************************/
877 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
880 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
886 #if defined( UNDER_CE ) || defined( _MSC_VER )
888 wchar_t psz_wpath[MAX_PATH + 256];
889 wchar_t psz_wdir[MAX_PATH];
891 char psz_path[MAX_PATH + 256];
892 WIN32_FIND_DATA finddata;
898 struct dirent * file;
902 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
907 #if defined( UNDER_CE ) || defined( _MSC_VER )
909 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
911 rc = GetFileAttributes( psz_wdir );
912 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
914 /* Parse all files in the directory */
915 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
917 rc = GetFileAttributes( psz_dir );
918 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
921 /* Parse all files in the directory */
922 sprintf( psz_path, "%s\\*", psz_dir );
925 handle = FindFirstFile( psz_wpath, &finddata );
927 handle = FindFirstFile( psz_path, &finddata );
929 if( handle == INVALID_HANDLE_VALUE )
931 /* Empty directory */
935 /* Parse the directory and try to load all files it contains. */
939 unsigned int i_len = wcslen( finddata.cFileName );
940 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
941 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
943 unsigned int i_len = strlen( finddata.cFileName );
944 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
948 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
949 || !strcmp( finddata.cFileName, ".." ) )
951 if( !FindNextFile( handle, &finddata ) ) break;
956 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
958 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
961 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
963 else if( i_len > strlen( LIBEXT )
964 /* We only load files ending with LIBEXT */
965 && !strncasecmp( psz_path + strlen( psz_path)
967 LIBEXT, strlen( LIBEXT ) ) )
969 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
970 int64_t i_time = 0, i_size = 0;
973 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
976 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
980 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
982 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
983 i_size = attrbuf.nFileSizeHigh;
985 i_size |= attrbuf.nFileSizeLow;
989 AllocatePluginFile( p_this, psz_file, i_time, i_size );
992 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
994 /* Close the directory */
998 dir = opendir( psz_dir );
1004 i_dirlen = strlen( psz_dir );
1006 /* Parse the directory and try to load all files it contains. */
1007 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1009 struct stat statbuf;
1013 /* Skip ".", ".." */
1014 if( !*file->d_name || !strcmp( file->d_name, "." )
1015 || !strcmp( file->d_name, ".." ) )
1020 i_len = strlen( file->d_name );
1021 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1023 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
1025 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
1028 i_stat = stat( psz_file, &statbuf );
1029 if( !i_stat && statbuf.st_mode & S_IFDIR )
1031 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1033 else if( i_len > strlen( LIBEXT )
1034 /* We only load files ending with LIBEXT */
1035 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1036 LIBEXT, strlen( LIBEXT ) ) )
1038 int64_t i_time = 0, i_size = 0;
1042 i_time = statbuf.st_mtime;
1043 i_size = statbuf.st_size;
1046 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1052 /* Close the directory */
1058 /*****************************************************************************
1059 * AllocatePluginFile: load a module into memory and initialize it.
1060 *****************************************************************************
1061 * This function loads a dynamically loadable module and allocates a structure
1062 * for its information data. The module can then be handled by module_Need
1063 * and module_Unneed. It can be removed by DeleteModule.
1064 *****************************************************************************/
1065 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1066 int64_t i_file_time, int64_t i_file_size )
1068 module_t * p_module = NULL;
1069 module_cache_t *p_cache_entry = NULL;
1072 * Check our plugins cache first then load plugin if needed
1075 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1077 if( !p_cache_entry )
1079 p_module = AllocatePlugin( p_this, psz_file );
1083 /* If junk dll, don't try to load it */
1084 if( p_cache_entry->b_junk )
1090 module_config_t *p_item = NULL, *p_end = NULL;
1092 p_module = p_cache_entry->p_module;
1093 p_module->b_loaded = VLC_FALSE;
1095 /* For now we force loading if the module's config contains
1096 * callbacks or actions.
1097 * Could be optimized by adding an API call.*/
1098 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1099 p_item < p_end; p_item++ )
1101 if( p_item->pf_callback || p_item->i_action )
1103 p_module = AllocatePlugin( p_this, psz_file );
1107 if( p_module == p_cache_entry->p_module )
1108 p_cache_entry->b_used = VLC_TRUE;
1114 libvlc_global_data_t *p_libvlc_global = vlc_global();
1116 /* Everything worked fine !
1117 * The module is ready to be added to the list. */
1118 p_module->b_builtin = VLC_FALSE;
1120 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1121 p_module->psz_object_name, p_module->psz_longname ); */
1123 vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1125 if( !p_libvlc_global->p_module_bank->b_cache )
1128 #define p_bank p_libvlc_global->p_module_bank
1129 /* Add entry to cache */
1131 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1132 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1133 if( !p_bank->pp_cache[p_bank->i_cache] )
1135 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1136 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1137 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1138 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1139 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1140 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1144 return p_module ? 0 : -1;
1147 /*****************************************************************************
1148 * AllocatePlugin: load a module into memory and initialize it.
1149 *****************************************************************************
1150 * This function loads a dynamically loadable module and allocates a structure
1151 * for its information data. The module can then be handled by module_Need
1152 * and module_Unneed. It can be removed by DeleteModule.
1153 *****************************************************************************/
1154 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1156 module_t * p_module = NULL;
1157 module_handle_t handle;
1159 if( LoadModule( p_this, psz_file, &handle ) )
1162 /* Now that we have successfully loaded the module, we can
1163 * allocate a structure for it */
1164 p_module = vlc_module_create( p_this );
1165 if( p_module == NULL )
1167 msg_Err( p_this, "out of memory" );
1168 CloseModule( handle );
1172 /* We need to fill these since they may be needed by CallEntry() */
1173 p_module->psz_filename = psz_file;
1174 p_module->handle = handle;
1175 p_module->b_loaded = VLC_TRUE;
1177 /* Initialize the module: fill p_module, default config */
1178 if( CallEntry( p_module ) != 0 )
1180 /* We couldn't call module_init() */
1181 vlc_object_destroy( p_module );
1182 CloseModule( handle );
1186 DupModule( p_module );
1187 p_module->psz_filename = strdup( p_module->psz_filename );
1189 /* Everything worked fine ! The module is ready to be added to the list. */
1190 p_module->b_builtin = VLC_FALSE;
1195 /*****************************************************************************
1196 * DupModule: make a plugin module standalone.
1197 *****************************************************************************
1198 * This function duplicates all strings in the module, so that the dynamic
1199 * object can be unloaded. It acts recursively on submodules.
1200 *****************************************************************************/
1201 static void DupModule( module_t *p_module )
1203 const char **pp_shortcut;
1206 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1208 *pp_shortcut = strdup( *pp_shortcut );
1211 /* We strdup() these entries so that they are still valid when the
1212 * module is unloaded. */
1213 p_module->psz_object_name = strdup( p_module->psz_object_name );
1214 p_module->psz_capability = strdup( p_module->psz_capability );
1215 p_module->psz_shortname = p_module->psz_shortname ?
1216 strdup( p_module->psz_shortname ) : NULL;
1217 p_module->psz_longname = strdup( p_module->psz_longname );
1218 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1221 if( p_module->psz_program != NULL )
1223 p_module->psz_program = strdup( p_module->psz_program );
1226 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1228 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1232 /*****************************************************************************
1233 * UndupModule: free a duplicated module.
1234 *****************************************************************************
1235 * This function frees the allocations done in DupModule().
1236 *****************************************************************************/
1237 static void UndupModule( module_t *p_module )
1239 const char **pp_shortcut;
1242 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1244 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1247 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1249 free( (void*)*pp_shortcut );
1252 free( (void*)p_module->psz_object_name );
1253 free( p_module->psz_capability );
1254 free( (void*)p_module->psz_shortname );
1255 free( (void*)p_module->psz_longname );
1256 free( (void*)p_module->psz_help );
1257 free( (void*)p_module->psz_program );
1260 #endif /* HAVE_DYNAMIC_PLUGINS */
1262 /*****************************************************************************
1263 * AllocateBuiltinModule: initialize a builtin module.
1264 *****************************************************************************
1265 * This function registers a builtin module and allocates a structure
1266 * for its information data. The module can then be handled by module_Need
1267 * and module_Unneed. It can be removed by DeleteModule.
1268 *****************************************************************************/
1269 static int AllocateBuiltinModule( vlc_object_t * p_this,
1270 int ( *pf_entry ) ( module_t * ) )
1272 module_t * p_module;
1274 /* Now that we have successfully loaded the module, we can
1275 * allocate a structure for it */
1276 p_module = vlc_module_create( p_this );
1277 if( p_module == NULL )
1279 msg_Err( p_this, "out of memory" );
1283 /* Initialize the module : fill p_module->psz_object_name, etc. */
1284 if( pf_entry( p_module ) != 0 )
1286 /* With a well-written module we shouldn't have to print an
1287 * additional error message here, but just make sure. */
1288 msg_Err( p_this, "failed calling entry point in builtin module" );
1289 vlc_object_destroy( p_module );
1293 /* Everything worked fine ! The module is ready to be added to the list. */
1294 p_module->b_builtin = VLC_TRUE;
1296 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1297 p_module->psz_object_name, p_module->psz_longname ); */
1299 vlc_object_attach( p_module, vlc_global()->p_module_bank );
1304 /*****************************************************************************
1305 * DeleteModule: delete a module and its structure.
1306 *****************************************************************************
1307 * This function can only be called if the module isn't being used.
1308 *****************************************************************************/
1309 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1311 if( !p_module ) return VLC_EGENERIC;
1313 vlc_object_detach( p_module );
1315 /* We free the structures that we strdup()ed in Allocate*Module(). */
1316 #ifdef HAVE_DYNAMIC_PLUGINS
1317 if( !p_module->b_builtin )
1319 if( p_module->b_loaded && p_module->b_unloadable )
1321 CloseModule( p_module->handle );
1323 UndupModule( p_module );
1324 free( p_module->psz_filename );
1328 /* Free and detach the object's children */
1329 while( p_module->i_children )
1331 vlc_object_t *p_this = p_module->pp_children[0];
1332 vlc_object_detach( p_this );
1333 vlc_object_destroy( p_this );
1336 config_Free( p_module );
1337 vlc_object_destroy( p_module );
1342 #ifdef HAVE_DYNAMIC_PLUGINS
1343 /*****************************************************************************
1344 * CallEntry: call an entry point.
1345 *****************************************************************************
1346 * This function calls a symbol given its name and a module structure. The
1347 * symbol MUST refer to a function returning int and taking a module_t* as
1349 *****************************************************************************/
1350 static int CallEntry( module_t * p_module )
1352 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
1353 int (* pf_symbol) ( module_t * p_module );
1355 /* Try to resolve the symbol */
1356 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1358 if( pf_symbol == NULL )
1360 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1361 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1362 psz_name, p_module->psz_filename );
1363 #elif defined(HAVE_DL_WINDOWS)
1364 char *psz_error = GetWindowsError();
1365 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1366 psz_name, p_module->psz_filename, psz_error );
1368 #elif defined(HAVE_DL_DLOPEN)
1369 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1370 psz_name, p_module->psz_filename, dlerror() );
1371 #elif defined(HAVE_DL_SHL_LOAD)
1372 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1373 psz_name, p_module->psz_filename, strerror(errno) );
1375 # error "Something is wrong in modules.c"
1380 /* We can now try to call the symbol */
1381 if( pf_symbol( p_module ) != 0 )
1383 /* With a well-written module we shouldn't have to print an
1384 * additional error message here, but just make sure. */
1385 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1386 psz_name, p_module->psz_filename );
1390 /* Everything worked fine, we can return */
1394 /*****************************************************************************
1395 * LoadModule: loads a dynamic library
1396 *****************************************************************************
1397 * This function loads a dynamically linked library using a system dependant
1398 * method. Will return 0 on success as well as the module handle.
1399 *****************************************************************************/
1400 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1401 module_handle_t *p_handle )
1403 module_handle_t handle;
1405 #if defined(HAVE_DL_DYLD)
1406 NSObjectFileImage image;
1407 NSObjectFileImageReturnCode ret;
1409 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1411 if( ret != NSObjectFileImageSuccess )
1413 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1417 /* Open the dynamic module */
1418 handle = NSLinkModule( image, psz_file,
1419 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1423 NSLinkEditErrors errors;
1424 const char *psz_file, *psz_err;
1426 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1427 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1428 NSDestroyObjectFileImage( image );
1432 /* Destroy our image, we won't need it */
1433 NSDestroyObjectFileImage( image );
1435 #elif defined(HAVE_DL_BEOS)
1436 handle = load_add_on( psz_file );
1439 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1443 #elif defined(HAVE_DL_WINDOWS)
1446 wchar_t psz_wfile[MAX_PATH];
1447 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1448 handle = LoadLibrary( psz_wfile );
1451 handle = LoadLibrary( psz_file );
1453 if( handle == NULL )
1455 char *psz_err = GetWindowsError();
1456 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1461 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1462 /* static is OK, we are called atomically */
1464 # if defined(SYS_LINUX)
1465 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1466 * are going to get namespace collisions when two modules have common
1467 * public symbols, but ALSA is being a pest here. */
1468 if( strstr( psz_file, "alsa_plugin" ) )
1470 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1471 if( handle == NULL )
1473 msg_Warn( p_this, "cannot load module `%s' (%s)",
1474 psz_file, dlerror() );
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' (%s)",
1506 psz_file, strerror(errno) );
1511 # error "Something is wrong in modules.c"
1519 /*****************************************************************************
1520 * CloseModule: unload a dynamic library
1521 *****************************************************************************
1522 * This function unloads a previously opened dynamically linked library
1523 * using a system dependant method. No return value is taken in consideration,
1524 * since some libraries sometimes refuse to close properly.
1525 *****************************************************************************/
1526 static void CloseModule( module_handle_t handle )
1528 #if defined(HAVE_DL_DYLD)
1529 NSUnLinkModule( handle, FALSE );
1531 #elif defined(HAVE_DL_BEOS)
1532 unload_add_on( handle );
1534 #elif defined(HAVE_DL_WINDOWS)
1535 FreeLibrary( handle );
1537 #elif defined(HAVE_DL_DLOPEN)
1542 #elif defined(HAVE_DL_SHL_LOAD)
1543 shl_unload( handle );
1549 /*****************************************************************************
1550 * GetSymbol: get a symbol from a dynamic library
1551 *****************************************************************************
1552 * This function queries a loaded library for a symbol specified in a
1553 * string, and returns a pointer to it. We don't check for dlerror() or
1554 * similar functions, since we want a non-NULL symbol anyway.
1555 *****************************************************************************/
1556 static void * _module_getsymbol( module_handle_t, const char * );
1558 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1560 void * p_symbol = _module_getsymbol( handle, psz_function );
1562 /* MacOS X dl library expects symbols to begin with "_". So do
1563 * some other operating systems. That's really lame, but hey, what
1565 if( p_symbol == NULL )
1567 char psz_call[strlen( psz_function ) + 2];
1569 psz_call[ 0 ] = '_';
1570 memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 );
1571 p_symbol = _module_getsymbol( handle, psz_call );
1577 static void * _module_getsymbol( module_handle_t handle,
1578 const char * psz_function )
1580 #if defined(HAVE_DL_DYLD)
1581 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1582 return NSAddressOfSymbol( sym );
1584 #elif defined(HAVE_DL_BEOS)
1586 if( B_OK == get_image_symbol( handle, psz_function,
1587 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1596 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1597 wchar_t psz_real[256];
1598 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1600 return (void *)GetProcAddress( handle, psz_real );
1602 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1603 return (void *)GetProcAddress( handle, (char *)psz_function );
1605 #elif defined(HAVE_DL_DLOPEN)
1606 return dlsym( handle, psz_function );
1608 #elif defined(HAVE_DL_SHL_LOAD)
1610 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1616 #if defined(HAVE_DL_WINDOWS)
1617 static char * GetWindowsError( void )
1619 #if defined(UNDER_CE)
1620 wchar_t psz_tmp[MAX_PATH];
1621 char * psz_buffer = malloc( MAX_PATH );
1623 char * psz_tmp = malloc( MAX_PATH );
1625 int i = 0, i_error = GetLastError();
1627 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1628 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1629 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1631 /* Go to the end of the string */
1632 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1639 #if defined(UNDER_CE)
1640 swprintf( psz_tmp + i, L" (error %i)", i_error );
1641 psz_tmp[ 255 ] = L'\0';
1643 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1644 psz_tmp[ 255 ] = '\0';
1648 #if defined(UNDER_CE)
1649 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1655 #endif /* HAVE_DL_WINDOWS */
1657 /*****************************************************************************
1658 * LoadPluginsCache: loads the plugins cache file
1659 *****************************************************************************
1660 * This function will load the plugin cache if present and valid. This cache
1661 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1662 * actually load the dynamically loadable module.
1663 * This allows us to only fully load plugins when they are actually used.
1664 *****************************************************************************/
1665 static void CacheLoad( vlc_object_t *p_this )
1667 char *psz_filename, *psz_homedir;
1669 int i, j, i_size, i_read;
1670 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1671 char p_cachelang[6], p_lang[6];
1673 module_cache_t **pp_cache = 0;
1674 int32_t i_file_size, i_marker;
1675 libvlc_global_data_t *p_libvlc_global = vlc_global();
1677 psz_homedir = p_this->p_libvlc->psz_homedir;
1680 msg_Err( p_this, "psz_homedir is null" );
1684 i_size = asprintf( &psz_filename, "%s"DIR_SEP"%s"DIR_SEP"%s"DIR_SEP"%s",
1685 psz_homedir, CONFIG_DIR, PLUGINSCACHE_DIR, CacheName() );
1688 msg_Err( p_this, "out of memory" );
1692 if( p_libvlc_global->p_module_bank->b_cache_delete )
1694 #if !defined( UNDER_CE )
1695 unlink( psz_filename );
1697 wchar_t psz_wf[MAX_PATH];
1698 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1699 DeleteFile( psz_wf );
1701 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1702 free( psz_filename );
1706 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1708 file = utf8_fopen( psz_filename, "rb" );
1711 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1713 free( psz_filename );
1716 free( psz_filename );
1718 /* Check the file size */
1719 i_read = fread( &i_file_size, 1, sizeof(i_file_size), file );
1720 if( i_read != sizeof(i_file_size) )
1722 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1728 fseek( file, 0, SEEK_END );
1729 if( ftell( file ) != i_file_size )
1731 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1732 "(corrupted size)" );
1736 fseek( file, sizeof(i_file_size), SEEK_SET );
1738 /* Check the file is a plugins cache */
1739 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1740 i_read = fread( p_cachestring, 1, i_size, file );
1741 if( i_read != i_size ||
1742 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1744 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1749 /* Check Sub-version number */
1750 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1751 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1753 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1754 "(corrupted header)" );
1759 /* Check the language hasn't changed */
1760 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1761 i_read = fread( p_cachelang, 1, i_size, file );
1762 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1764 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1765 "(language changed)" );
1770 /* Check header marker */
1771 i_read = fread( &i_marker, 1, sizeof(i_marker), file );
1772 if( i_read != sizeof(i_marker) ||
1773 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1775 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1776 "(corrupted header)" );
1781 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1782 if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
1784 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1785 "(file too short)" );
1791 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache =
1792 malloc( i_cache * sizeof(void *) );
1794 #define LOAD_IMMEDIATE(a) \
1795 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1796 #define LOAD_STRING(a) \
1799 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1800 || ( i_size > 16384 ) ) \
1803 char *psz = malloc( i_size ); \
1804 if( fread( psz, i_size, 1, file ) != 1 ) { \
1808 if( psz[i_size-1] ) { \
1816 for( i = 0; i < i_cache; i++ )
1821 pp_cache[i] = malloc( sizeof(module_cache_t) );
1822 p_libvlc_global->p_module_bank->i_loaded_cache++;
1824 /* Load common info */
1825 LOAD_STRING( pp_cache[i]->psz_file );
1826 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1827 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1828 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1829 pp_cache[i]->b_used = VLC_FALSE;
1831 if( pp_cache[i]->b_junk ) continue;
1833 pp_cache[i]->p_module = vlc_module_create( p_this );
1835 /* Load additional infos */
1836 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1837 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1838 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1839 LOAD_STRING( pp_cache[i]->p_module->psz_help );
1840 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1841 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1843 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1845 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1846 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1847 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1848 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1849 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1850 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1853 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1856 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1858 LOAD_IMMEDIATE( i_submodules );
1860 while( i_submodules-- )
1862 module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
1863 LOAD_STRING( p_module->psz_object_name );
1864 LOAD_STRING( p_module->psz_shortname );
1865 LOAD_STRING( p_module->psz_longname );
1866 LOAD_STRING( p_module->psz_help );
1867 LOAD_STRING( p_module->psz_program );
1868 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1870 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1872 LOAD_STRING( p_module->psz_capability );
1873 LOAD_IMMEDIATE( p_module->i_score );
1874 LOAD_IMMEDIATE( p_module->i_cpu );
1875 LOAD_IMMEDIATE( p_module->b_unloadable );
1876 LOAD_IMMEDIATE( p_module->b_reentrant );
1885 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1888 p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1895 int CacheLoadConfig( module_t *p_module, FILE *file )
1900 /* Calculate the structure length */
1901 LOAD_IMMEDIATE( p_module->i_config_items );
1902 LOAD_IMMEDIATE( p_module->i_bool_items );
1904 LOAD_IMMEDIATE( i_lines );
1906 /* Allocate memory */
1909 p_module->p_config =
1910 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1911 if( p_module->p_config == NULL )
1913 p_module->confsize = 0;
1914 msg_Err( p_module, "config error: can't duplicate p_config" );
1918 p_module->confsize = i_lines;
1920 /* Do the duplication job */
1921 for (size_t i = 0; i < i_lines; i++ )
1923 LOAD_IMMEDIATE( p_module->p_config[i] );
1925 LOAD_STRING( p_module->p_config[i].psz_type );
1926 LOAD_STRING( p_module->p_config[i].psz_name );
1927 LOAD_STRING( p_module->p_config[i].psz_text );
1928 LOAD_STRING( p_module->p_config[i].psz_longtext );
1929 LOAD_STRING( p_module->p_config[i].psz_current );
1931 if (IsConfigStringType (p_module->p_config[i].i_type))
1933 LOAD_STRING (p_module->p_config[i].orig.psz);
1934 p_module->p_config[i].value.psz =
1935 (p_module->p_config[i].orig.psz != NULL)
1936 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1937 p_module->p_config[i].saved.psz = NULL;
1941 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1942 sizeof (p_module->p_config[i].value));
1943 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1944 sizeof (p_module->p_config[i].saved));
1947 p_module->p_config[i].b_dirty = VLC_FALSE;
1949 p_module->p_config[i].p_lock = &p_module->object_lock;
1951 if( p_module->p_config[i].i_list )
1953 if( p_module->p_config[i].ppsz_list )
1956 p_module->p_config[i].ppsz_list =
1957 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1958 if( p_module->p_config[i].ppsz_list )
1960 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1961 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1962 p_module->p_config[i].ppsz_list[j] = NULL;
1965 if( p_module->p_config[i].ppsz_list_text )
1968 p_module->p_config[i].ppsz_list_text =
1969 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1970 if( p_module->p_config[i].ppsz_list_text )
1972 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1973 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1974 p_module->p_config[i].ppsz_list_text[j] = NULL;
1977 if( p_module->p_config[i].pi_list )
1979 p_module->p_config[i].pi_list =
1980 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1981 if( p_module->p_config[i].pi_list )
1983 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1984 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1989 if( p_module->p_config[i].i_action )
1991 p_module->p_config[i].ppf_action =
1992 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1993 p_module->p_config[i].ppsz_action_text =
1994 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1996 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1998 p_module->p_config[i].ppf_action[j] = 0;
1999 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
2003 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
2010 return VLC_EGENERIC;
2013 /*****************************************************************************
2014 * SavePluginsCache: saves the plugins cache to a file
2015 *****************************************************************************/
2016 static void CacheSave( vlc_object_t *p_this )
2018 static char const psz_tag[] =
2019 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
2020 "# This file is a cache directory tag created by VLC.\r\n"
2021 "# For information about cache directory tags, see:\r\n"
2022 "# http://www.brynosaurus.com/cachedir/\r\n";
2024 char *psz_filename, *psz_homedir;
2027 module_cache_t **pp_cache;
2028 int32_t i_file_size = 0;
2029 libvlc_global_data_t *p_libvlc_global = vlc_global();
2031 psz_homedir = p_this->p_libvlc->psz_homedir;
2034 msg_Err( p_this, "psz_homedir is null" );
2038 (char *)malloc( sizeof(DIR_SEP CONFIG_DIR DIR_SEP PLUGINSCACHE_DIR DIR_SEP ) +
2039 strlen(psz_homedir) + strlen(CacheName()) );
2043 msg_Err( p_this, "out of memory" );
2047 sprintf( psz_filename, "%s"DIR_SEP"%s", psz_homedir, CONFIG_DIR );
2049 config_CreateDir( p_this, psz_filename );
2051 strcat( psz_filename, DIR_SEP PLUGINSCACHE_DIR );
2053 config_CreateDir( p_this, psz_filename );
2055 strcat( psz_filename, DIR_SEP"CACHEDIR.TAG" );
2057 file = utf8_fopen( psz_filename, "wb" );
2060 fwrite( psz_tag, 1, strlen(psz_tag), file );
2064 sprintf( psz_filename, "%s"DIR_SEP"%s"DIR_SEP"%s"DIR_SEP"%s", psz_homedir, CONFIG_DIR,
2065 PLUGINSCACHE_DIR, CacheName() );
2067 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2069 file = utf8_fopen( psz_filename, "wb" );
2072 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2074 free( psz_filename );
2077 free( psz_filename );
2079 /* Empty space for file size */
2080 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2082 /* Contains version number */
2083 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2085 /* Sub-version number (to avoid breakage in the dev version when cache
2086 * structure changes) */
2087 i_file_size = CACHE_SUBVERSION_NUM;
2088 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2091 fprintf( file, "%5.5s", _("C") );
2094 i_file_size = ftell( file );
2095 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2097 i_cache = p_libvlc_global->p_module_bank->i_cache;
2098 pp_cache = p_libvlc_global->p_module_bank->pp_cache;
2100 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2102 #define SAVE_IMMEDIATE(a) \
2103 fwrite( &a, sizeof(char), sizeof(a), file )
2104 #define SAVE_STRING(a) \
2105 { i_size = a ? strlen( a ) + 1 : 0; \
2106 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2107 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2110 for( i = 0; i < i_cache; i++ )
2113 uint32_t i_submodule;
2115 /* Save common info */
2116 SAVE_STRING( pp_cache[i]->psz_file );
2117 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2118 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2119 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2121 if( pp_cache[i]->b_junk ) continue;
2123 /* Save additional infos */
2124 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2125 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2126 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2127 SAVE_STRING( pp_cache[i]->p_module->psz_help );
2128 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2129 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2131 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2133 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2134 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2135 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2136 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2137 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2138 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2141 CacheSaveConfig( pp_cache[i]->p_module, file );
2143 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2145 i_submodule = pp_cache[i]->p_module->i_children;
2146 SAVE_IMMEDIATE( i_submodule );
2147 for( i_submodule = 0;
2148 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2151 module_t *p_module =
2152 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2154 SAVE_STRING( p_module->psz_object_name );
2155 SAVE_STRING( p_module->psz_shortname );
2156 SAVE_STRING( p_module->psz_longname );
2157 SAVE_STRING( p_module->psz_help );
2158 SAVE_STRING( p_module->psz_program );
2159 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2161 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2163 SAVE_STRING( p_module->psz_capability );
2164 SAVE_IMMEDIATE( p_module->i_score );
2165 SAVE_IMMEDIATE( p_module->i_cpu );
2166 SAVE_IMMEDIATE( p_module->b_unloadable );
2167 SAVE_IMMEDIATE( p_module->b_reentrant );
2171 /* Fill-up file size */
2172 i_file_size = ftell( file );
2173 fseek( file, 0, SEEK_SET );
2174 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2181 void CacheSaveConfig( module_t *p_module, FILE *file )
2183 uint32_t i_lines = p_module->confsize;
2186 SAVE_IMMEDIATE( p_module->i_config_items );
2187 SAVE_IMMEDIATE( p_module->i_bool_items );
2188 SAVE_IMMEDIATE( i_lines );
2190 for (size_t i = 0; i < i_lines ; i++)
2192 SAVE_IMMEDIATE( p_module->p_config[i] );
2194 SAVE_STRING( p_module->p_config[i].psz_type );
2195 SAVE_STRING( p_module->p_config[i].psz_name );
2196 SAVE_STRING( p_module->p_config[i].psz_text );
2197 SAVE_STRING( p_module->p_config[i].psz_longtext );
2198 SAVE_STRING( p_module->p_config[i].psz_current );
2199 if (IsConfigStringType (p_module->p_config[i].i_type))
2200 SAVE_STRING( p_module->p_config[i].orig.psz );
2202 if( p_module->p_config[i].i_list )
2204 if( p_module->p_config[i].ppsz_list )
2206 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2207 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2210 if( p_module->p_config[i].ppsz_list_text )
2212 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2213 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2215 if( p_module->p_config[i].pi_list )
2217 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2218 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2222 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2223 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2225 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2229 /*****************************************************************************
2230 * CacheName: Return the cache file name for this platform.
2231 *****************************************************************************/
2232 static char *CacheName( void )
2234 static char psz_cachename[32];
2236 /* Code int size, pointer size and endianness in the filename */
2237 int32_t x = 0xbe00001e;
2238 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2239 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2240 return psz_cachename;
2243 /*****************************************************************************
2244 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2245 *****************************************************************************/
2246 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2247 module_t *p_module )
2252 p_cache->pf_activate = p_module->pf_activate;
2253 p_cache->pf_deactivate = p_module->pf_deactivate;
2254 p_cache->handle = p_module->handle;
2256 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2258 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2259 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2260 p_cchild->pf_activate = p_child->pf_activate;
2261 p_cchild->pf_deactivate = p_child->pf_deactivate;
2264 p_cache->b_loaded = VLC_TRUE;
2265 p_module->b_loaded = VLC_FALSE;
2268 /*****************************************************************************
2269 * CacheFind: finds the cache entry corresponding to a file
2270 *****************************************************************************/
2271 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2272 int64_t i_time, int64_t i_size )
2274 module_cache_t **pp_cache;
2276 libvlc_global_data_t *p_libvlc_global = vlc_global();
2278 pp_cache = p_libvlc_global->p_module_bank->pp_loaded_cache;
2279 i_cache = p_libvlc_global->p_module_bank->i_loaded_cache;
2281 for( i = 0; i < i_cache; i++ )
2283 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2284 pp_cache[i]->i_time == i_time &&
2285 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2291 #endif /* HAVE_DYNAMIC_PLUGINS */