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 *****************************************************************************/
29 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
30 * is set to 64. Don't try to be cleverer. */
31 #ifdef _FILE_OFFSET_BITS
32 #undef _FILE_OFFSET_BITS
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* sprintf() */
37 #include <string.h> /* strdup() */
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
53 #if !defined(HAVE_DYNAMIC_PLUGINS)
54 /* no support for plugins */
55 #elif defined(HAVE_DL_DYLD)
56 # if defined(HAVE_MACH_O_DYLD_H)
57 # include <mach-o/dyld.h>
59 #elif defined(HAVE_DL_BEOS)
60 # if defined(HAVE_IMAGE_H)
63 #elif defined(HAVE_DL_WINDOWS)
65 #elif defined(HAVE_DL_DLOPEN)
66 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
69 # if defined(HAVE_SYS_DL_H)
72 #elif defined(HAVE_DL_SHL_LOAD)
73 # if defined(HAVE_DL_H)
78 #include "misc/configuration.h"
80 #include "vlc_interface.h"
81 #include "vlc_playlist.h"
83 #include "vlc_stream.h"
84 #include "vlc_access.h"
85 #include "vlc_demux.h"
88 #include "vlc_vout_synchro.h"
93 #include "vlc_httpd.h"
100 #include "iso_lang.h"
101 #include "vlc_charset.h"
103 #include "vlc_block.h"
107 #include "vlc_image.h"
110 #include "vlc_update.h"
111 #include "vlc_strings.h"
112 #include "vlc_streaming.h"
116 #if defined( _MSC_VER ) && defined( UNDER_CE )
117 # include "modules_builtin_evc.h"
118 #elif defined( _MSC_VER )
119 # include "modules_builtin_msvc.h"
121 # include "modules_builtin.h"
123 #include "vlc_network.h"
125 #if defined( WIN32 ) || defined( UNDER_CE )
126 /* Avoid name collisions */
127 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
130 /*****************************************************************************
132 *****************************************************************************/
133 #ifdef HAVE_DYNAMIC_PLUGINS
134 static void AllocateAllPlugins ( vlc_object_t * );
135 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
136 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
137 static module_t * AllocatePlugin( vlc_object_t *, char * );
139 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
140 static int DeleteModule ( module_t *, vlc_bool_t );
141 #ifdef HAVE_DYNAMIC_PLUGINS
142 static void DupModule ( module_t * );
143 static void UndupModule ( module_t * );
144 static int CallEntry ( module_t * );
145 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
146 static void CloseModule ( module_handle_t );
147 static void * GetSymbol ( module_handle_t, const char * );
148 static void CacheLoad ( vlc_object_t * );
149 static int CacheLoadConfig ( module_t *, FILE * );
150 static void CacheSave ( vlc_object_t * );
151 static void CacheSaveConfig ( module_t *, FILE * );
152 static char * CacheName ( void );
153 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
154 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
156 #if defined(HAVE_DL_WINDOWS)
157 static char * GetWindowsError ( void );
161 static void module_LoadMain( vlc_object_t *p_this );
164 /* Sub-version number
165 * (only used to avoid breakage in dev version when cache structure changes) */
166 #define CACHE_SUBVERSION_NUM 2
168 /*****************************************************************************
169 * module_InitBank: create the module bank.
170 *****************************************************************************
171 * This function creates a module bank structure which will be filled later
172 * on with all the modules found.
173 *****************************************************************************/
174 void __module_InitBank( vlc_object_t *p_this )
176 module_bank_t *p_bank = NULL;
179 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
180 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
181 vlc_mutex_lock( lockval.p_address );
182 if( p_this->p_libvlc_global->p_module_bank )
184 p_this->p_libvlc_global->p_module_bank->i_usage++;
185 vlc_mutex_unlock( lockval.p_address );
186 var_Destroy( p_this->p_libvlc_global, "libvlc" );
189 vlc_mutex_unlock( lockval.p_address );
190 var_Destroy( p_this->p_libvlc_global, "libvlc" );
192 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
195 p_bank->psz_object_name = "module bank";
197 p_bank->i_cache = p_bank->i_loaded_cache = 0;
198 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
199 p_bank->b_cache = p_bank->b_cache_dirty =
200 p_bank->b_cache_delete = VLC_FALSE;
203 * Store the symbols to be exported
205 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
206 STORE_SYMBOLS( &p_bank->symbols );
209 /* Everything worked, attach the object */
210 p_this->p_libvlc_global->p_module_bank = p_bank;
211 vlc_object_attach( p_bank, p_this->p_libvlc_global );
213 module_LoadMain( p_this );
216 /*****************************************************************************
217 * module_ResetBank: reset the module bank.
218 *****************************************************************************
219 * This function resets the module bank by unloading all unused plugin
221 *****************************************************************************/
222 void __module_ResetBank( vlc_object_t *p_this )
224 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
228 /*****************************************************************************
229 * module_EndBank: empty the module bank.
230 *****************************************************************************
231 * This function unloads all unused plugin modules and empties the module
232 * bank in case of success.
233 *****************************************************************************/
234 void __module_EndBank( vlc_object_t *p_this )
236 module_t * p_next = NULL;
239 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
240 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
241 vlc_mutex_lock( lockval.p_address );
242 if( !p_this->p_libvlc_global->p_module_bank )
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc_global, "libvlc" );
248 if( --p_this->p_libvlc_global->p_module_bank->i_usage )
250 vlc_mutex_unlock( lockval.p_address );
251 var_Destroy( p_this->p_libvlc_global, "libvlc" );
254 vlc_mutex_unlock( lockval.p_address );
255 var_Destroy( p_this->p_libvlc_global, "libvlc" );
257 config_AutoSaveConfigFile( p_this );
259 #ifdef HAVE_DYNAMIC_PLUGINS
260 #define p_bank p_this->p_libvlc_global->p_module_bank
261 if( p_bank->b_cache ) CacheSave( p_this );
262 while( p_bank->i_loaded_cache-- )
264 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
266 DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module, p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
267 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
268 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
269 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
272 if( p_bank->pp_loaded_cache )
274 free( p_bank->pp_loaded_cache );
275 p_bank->pp_loaded_cache = NULL;
277 while( p_bank->i_cache-- )
279 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
280 free( p_bank->pp_cache[p_bank->i_cache] );
281 p_bank->pp_cache[p_bank->i_cache] = NULL;
283 if( p_bank->pp_cache )
285 free( p_bank->pp_cache );
286 p_bank->pp_cache = NULL;
291 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
293 while( p_this->p_libvlc_global->p_module_bank->i_children )
295 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
297 if( DeleteModule( p_next, VLC_TRUE ) )
299 /* Module deletion failed */
300 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
301 p_next->psz_object_name );
303 /* We just free the module by hand. Niahahahahaha. */
304 vlc_object_detach( p_next );
305 vlc_object_destroy( p_next );
309 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
310 p_this->p_libvlc_global->p_module_bank = NULL;
315 /*****************************************************************************
316 * module_LoadMain: load the main program info into the module bank.
317 *****************************************************************************
318 * This function fills the module bank structure with the main module infos.
319 * This is very useful as it will allow us to consider the main program just
320 * as another module, and for instance the configuration options of main will
321 * be available in the module bank structure just as for every other module.
322 *****************************************************************************/
323 static void module_LoadMain( vlc_object_t *p_this )
327 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
328 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
329 vlc_mutex_lock( lockval.p_address );
330 if( p_this->p_libvlc_global->p_module_bank->b_main )
332 vlc_mutex_unlock( lockval.p_address );
333 var_Destroy( p_this->p_libvlc_global, "libvlc" );
336 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
337 vlc_mutex_unlock( lockval.p_address );
338 var_Destroy( p_this->p_libvlc_global, "libvlc" );
340 AllocateBuiltinModule( p_this, vlc_entry__main );
343 /*****************************************************************************
344 * module_LoadBuiltins: load all modules which we built with.
345 *****************************************************************************
346 * This function fills the module bank structure with the builtin modules.
347 *****************************************************************************/
348 void __module_LoadBuiltins( vlc_object_t * p_this )
352 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
353 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
354 vlc_mutex_lock( lockval.p_address );
355 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
357 vlc_mutex_unlock( lockval.p_address );
358 var_Destroy( p_this->p_libvlc_global, "libvlc" );
361 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
362 vlc_mutex_unlock( lockval.p_address );
363 var_Destroy( p_this->p_libvlc_global, "libvlc" );
365 msg_Dbg( p_this, "checking builtin modules" );
366 ALLOCATE_ALL_BUILTINS();
369 /*****************************************************************************
370 * module_LoadPlugins: load all plugin modules we can find.
371 *****************************************************************************
372 * This function fills the module bank structure with the plugin modules.
373 *****************************************************************************/
374 void __module_LoadPlugins( vlc_object_t * p_this )
376 #ifdef HAVE_DYNAMIC_PLUGINS
379 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
380 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
381 vlc_mutex_lock( lockval.p_address );
382 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
384 vlc_mutex_unlock( lockval.p_address );
385 var_Destroy( p_this->p_libvlc_global, "libvlc" );
388 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
389 vlc_mutex_unlock( lockval.p_address );
390 var_Destroy( p_this->p_libvlc_global, "libvlc" );
392 msg_Dbg( p_this, "checking plugin modules" );
394 if( config_GetInt( p_this, "plugins-cache" ) )
395 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
397 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
398 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
400 AllocateAllPlugins( p_this );
404 /*****************************************************************************
405 * module_Need: return the best module function, given a capability list.
406 *****************************************************************************
407 * This function returns the module that best fits the asked capabilities.
408 *****************************************************************************/
409 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
410 const char *psz_name, vlc_bool_t b_strict )
412 typedef struct module_list_t module_list_t;
419 module_list_t *p_next;
422 module_list_t *p_list, *p_first, *p_tmp;
425 int i_which_module, i_index = 0;
426 vlc_bool_t b_intf = VLC_FALSE;
431 char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
432 vlc_bool_t b_force_backup = p_this->b_force;
435 /* Deal with variables */
436 if( psz_name && psz_name[0] == '$' )
439 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
440 var_Get( p_this, psz_name + 1, &val );
441 psz_var = val.psz_string;
445 /* Count how many different shortcuts were asked for */
446 if( psz_name && *psz_name )
448 char *psz_parser, *psz_last_shortcut;
450 /* If the user wants none, give him none. */
451 if( !strcmp( psz_name, "none" ) )
458 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
460 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
462 if( *psz_parser == ',' )
466 psz_last_shortcut = psz_parser + 1;
470 /* Check if the user wants to override the "strict" mode */
471 if( psz_last_shortcut )
473 if( !strcmp(psz_last_shortcut, "none") )
478 else if( !strcmp(psz_last_shortcut, "any") )
480 b_strict = VLC_FALSE;
486 /* Sort the modules and test them */
487 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
488 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
491 /* Parse the module list for capabilities and probe each of them */
492 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
494 int i_shortcut_bonus = 0;
496 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
498 /* Test that this module can do what we need */
499 if( strcmp( p_module->psz_capability, psz_capability ) )
501 /* Don't recurse through the sub-modules because vlc_list_find()
502 * will list them anyway. */
506 /* Test if we have the required CPU */
507 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
512 /* If we required a shortcut, check this plugin provides it. */
513 if( i_shortcuts > 0 )
516 const char *psz_name = psz_shortcuts;
518 /* Let's drop modules with a <= 0 score (unless they are
519 * explicitly requested) */
520 b_trash = p_module->i_score <= 0;
522 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
524 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
527 if( ( c = strchr( psz_name, '@' ) )
528 ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
530 : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
535 i_shortcut_bonus = i_short * 10000;
540 /* Go to the next shortcut... This is so lame! */
541 psz_name += strlen( psz_name ) + 1;
544 /* If we are in "strict" mode and we couldn't
545 * find the module in the list of provided shortcuts,
546 * then kick the bastard out of here!!! */
550 /* If we didn't require a shortcut, trash <= 0 scored plugins */
551 else if( p_module->i_score <= 0 )
558 /* Special case: test if we requested a particular intf plugin */
559 if( !i_shortcuts && p_module->psz_program
560 && !strcmp( psz_capability, "interface" )
561 && !strcmp( p_module->psz_program,
562 p_this->p_libvlc->psz_object_name ) )
566 /* Remove previous non-matching plugins */
573 /* This one doesn't match */
577 /* Store this new module */
578 p_list[ i_index ].p_module = p_module;
579 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
580 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
582 /* Add it to the modules-to-probe list */
585 p_list[ 0 ].p_next = NULL;
590 /* Ok, so at school you learned that quicksort is quick, and
591 * bubble sort sucks raw eggs. But that's when dealing with
592 * thousands of items. Here we have barely 50. */
593 module_list_t *p_newlist = p_first;
595 if( p_first->i_score < p_list[ i_index ].i_score )
597 p_list[ i_index ].p_next = p_first;
598 p_first = &p_list[ i_index ];
602 while( p_newlist->p_next != NULL &&
603 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
605 p_newlist = p_newlist->p_next;
608 p_list[ i_index ].p_next = p_newlist->p_next;
609 p_newlist->p_next = &p_list[ i_index ];
616 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
617 i_index, i_index == 1 ? "" : "s" );
619 /* Lock all candidate modules */
621 while( p_tmp != NULL )
623 vlc_object_yield( p_tmp->p_module );
624 p_tmp = p_tmp->p_next;
627 /* We can release the list, interesting modules were yielded */
628 vlc_list_release( p_all );
630 /* Parse the linked list and use the first successful module */
632 while( p_tmp != NULL )
634 #ifdef HAVE_DYNAMIC_PLUGINS
635 /* Make sure the module is loaded in mem */
636 module_t *p_module = p_tmp->p_module;
637 if( p_module->b_submodule )
638 p_module = (module_t *)p_module->p_parent;
640 if( !p_module->b_builtin && !p_module->b_loaded )
642 module_t *p_new_module =
643 AllocatePlugin( p_this, p_module->psz_filename );
646 CacheMerge( p_this, p_module, p_new_module );
647 vlc_object_attach( p_new_module, p_module );
648 DeleteModule( p_new_module, VLC_TRUE );
653 p_this->b_force = p_tmp->b_force;
654 if( p_tmp->p_module->pf_activate
655 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
660 vlc_object_release( p_tmp->p_module );
661 p_tmp = p_tmp->p_next;
664 /* Store the locked module value */
667 p_module = p_tmp->p_module;
668 p_tmp = p_tmp->p_next;
675 /* Unlock the remaining modules */
676 while( p_tmp != NULL )
678 vlc_object_release( p_tmp->p_module );
679 p_tmp = p_tmp->p_next;
683 p_this->b_force = b_force_backup;
685 if( p_module != NULL )
687 msg_Dbg( p_this, "using %s module \"%s\"",
688 psz_capability, p_module->psz_object_name );
690 else if( p_first == NULL )
692 if( !strcmp( psz_capability, "access_demux" ) )
694 msg_Warn( p_this, "no %s module matched \"%s\"",
695 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
699 msg_Err( p_this, "no %s module matched \"%s\"",
700 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
702 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
703 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
706 else if( psz_name != NULL && *psz_name )
708 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
709 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
712 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
714 if( p_module && !p_this->psz_object_name )
716 /* This assumes that p_this is the object which will be using the
717 * module. That's not always the case ... but it is in most cases.
720 p_this->psz_object_name = strdup( psz_alias );
722 p_this->psz_object_name = strdup( p_module->psz_object_name );
725 free( psz_shortcuts );
728 /* Don't forget that the module is still locked */
732 /*****************************************************************************
733 * module_Unneed: decrease the usage count of a module.
734 *****************************************************************************
735 * This function must be called by the thread that called module_Need, to
736 * decrease the reference count and allow for hiding of modules.
737 *****************************************************************************/
738 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
740 /* Use the close method */
741 if( p_module->pf_deactivate )
743 p_module->pf_deactivate( p_this );
746 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
748 vlc_object_release( p_module );
753 /*****************************************************************************
754 * module_Exists: tell if a module exists.
755 *****************************************************************************
756 * This function is a boolean function that tells if a module exist or not.
757 *****************************************************************************/
759 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
763 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
764 for( i = 0 ; i < p_list->i_count; i++)
766 const char *psz_module_name =
767 ((module_t *) p_list->p_values[i].p_object)->psz_shortname;
768 if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
770 /* We can release the list, and return yes */
771 vlc_list_release( p_list ); return VLC_TRUE;
774 vlc_list_release( p_list ); return VLC_FALSE;
778 /*****************************************************************************
779 * Following functions are local.
780 *****************************************************************************/
782 /*****************************************************************************
783 * AllocateAllPlugins: load all plugin modules we can find.
784 *****************************************************************************/
785 #ifdef HAVE_DYNAMIC_PLUGINS
786 static void AllocateAllPlugins( vlc_object_t *p_this )
788 /* Yes, there are two NULLs because we replace one with "plugin-path". */
789 #if defined( WIN32 ) || defined( UNDER_CE )
790 const char *path[] = { "modules", "", "plugins", NULL, NULL };
792 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
795 const char *const *ppsz_path;
797 /* If the user provided a plugin path, we add it to the list */
798 char *userpath = config_GetPsz( p_this, "plugin-path" );
799 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
801 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
805 if (!**ppsz_path) continue;
807 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
809 /* Handle relative as well as absolute paths */
811 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
812 (*ppsz_path)[1] != ':' )
814 if( (*ppsz_path)[0] != '/' )
817 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
818 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
823 psz_fullpath = strdup( *ppsz_path );
825 if( psz_fullpath == NULL )
828 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
830 /* Don't go deeper than 5 subdirectories */
831 AllocatePluginDir( p_this, psz_fullpath, 5 );
833 free( psz_fullpath );
836 /* Free plugin-path */
840 /*****************************************************************************
841 * AllocatePluginDir: recursively parse a directory to look for plugins
842 *****************************************************************************/
843 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
846 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
852 #if defined( UNDER_CE ) || defined( _MSC_VER )
854 wchar_t psz_wpath[MAX_PATH + 256];
855 wchar_t psz_wdir[MAX_PATH];
857 char psz_path[MAX_PATH + 256];
858 WIN32_FIND_DATA finddata;
864 struct dirent * file;
868 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
873 #if defined( UNDER_CE ) || defined( _MSC_VER )
875 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
877 rc = GetFileAttributes( psz_wdir );
878 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
880 /* Parse all files in the directory */
881 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
883 rc = GetFileAttributes( psz_dir );
884 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
887 /* Parse all files in the directory */
888 sprintf( psz_path, "%s\\*", psz_dir );
891 handle = FindFirstFile( psz_wpath, &finddata );
893 handle = FindFirstFile( psz_path, &finddata );
895 if( handle == INVALID_HANDLE_VALUE )
897 /* Empty directory */
901 /* Parse the directory and try to load all files it contains. */
905 unsigned int i_len = wcslen( finddata.cFileName );
906 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
907 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
909 unsigned int i_len = strlen( finddata.cFileName );
910 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
914 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
915 || !strcmp( finddata.cFileName, ".." ) )
917 if( !FindNextFile( handle, &finddata ) ) break;
922 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
924 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
927 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
929 else if( i_len > strlen( LIBEXT )
930 /* We only load files ending with LIBEXT */
931 && !strncasecmp( psz_path + strlen( psz_path)
933 LIBEXT, strlen( LIBEXT ) ) )
935 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
936 int64_t i_time = 0, i_size = 0;
939 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
942 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
946 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
948 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
949 i_size = attrbuf.nFileSizeHigh;
951 i_size |= attrbuf.nFileSizeLow;
955 AllocatePluginFile( p_this, psz_file, i_time, i_size );
958 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
960 /* Close the directory */
964 dir = opendir( psz_dir );
970 i_dirlen = strlen( psz_dir );
972 /* Parse the directory and try to load all files it contains. */
973 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
980 if( !*file->d_name || !strcmp( file->d_name, "." )
981 || !strcmp( file->d_name, ".." ) )
986 i_len = strlen( file->d_name );
987 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
989 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
991 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
994 i_stat = stat( psz_file, &statbuf );
995 if( !i_stat && statbuf.st_mode & S_IFDIR )
997 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
999 else if( i_len > strlen( LIBEXT )
1000 /* We only load files ending with LIBEXT */
1001 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1002 LIBEXT, strlen( LIBEXT ) ) )
1004 int64_t i_time = 0, i_size = 0;
1008 i_time = statbuf.st_mtime;
1009 i_size = statbuf.st_size;
1012 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1018 /* Close the directory */
1024 /*****************************************************************************
1025 * AllocatePluginFile: load a module into memory and initialize it.
1026 *****************************************************************************
1027 * This function loads a dynamically loadable module and allocates a structure
1028 * for its information data. The module can then be handled by module_Need
1029 * and module_Unneed. It can be removed by DeleteModule.
1030 *****************************************************************************/
1031 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1032 int64_t i_file_time, int64_t i_file_size )
1034 module_t * p_module = NULL;
1035 module_cache_t *p_cache_entry = NULL;
1038 * Check our plugins cache first then load plugin if needed
1041 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1043 if( !p_cache_entry )
1045 p_module = AllocatePlugin( p_this, psz_file );
1049 /* If junk dll, don't try to load it */
1050 if( p_cache_entry->b_junk )
1056 module_config_t *p_item = NULL, *p_end = NULL;
1058 p_module = p_cache_entry->p_module;
1059 p_module->b_loaded = VLC_FALSE;
1061 /* For now we force loading if the module's config contains
1062 * callbacks or actions.
1063 * Could be optimized by adding an API call.*/
1064 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1065 p_item < p_end; p_item++ )
1067 if( p_item->pf_callback || p_item->i_action )
1069 p_module = AllocatePlugin( p_this, psz_file );
1073 if( p_module == p_cache_entry->p_module )
1074 p_cache_entry->b_used = VLC_TRUE;
1080 /* Everything worked fine !
1081 * The module is ready to be added to the list. */
1082 p_module->b_builtin = VLC_FALSE;
1084 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1085 p_module->psz_object_name, p_module->psz_longname ); */
1087 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1089 if( !p_this->p_libvlc_global->p_module_bank->b_cache )
1092 /* Add entry to cache */
1093 #define p_bank p_this->p_libvlc_global->p_module_bank
1095 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1096 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1097 if( !p_bank->pp_cache[p_bank->i_cache] )
1099 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1100 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1101 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1102 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1103 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1104 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1108 return p_module ? 0 : -1;
1111 /*****************************************************************************
1112 * AllocatePlugin: load a module into memory and initialize it.
1113 *****************************************************************************
1114 * This function loads a dynamically loadable module and allocates a structure
1115 * for its information data. The module can then be handled by module_Need
1116 * and module_Unneed. It can be removed by DeleteModule.
1117 *****************************************************************************/
1118 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1120 module_t * p_module = NULL;
1121 module_handle_t handle;
1123 if( LoadModule( p_this, psz_file, &handle ) )
1126 /* Now that we have successfully loaded the module, we can
1127 * allocate a structure for it */
1128 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1129 if( p_module == NULL )
1131 msg_Err( p_this, "out of memory" );
1132 CloseModule( handle );
1136 /* We need to fill these since they may be needed by CallEntry() */
1137 p_module->psz_filename = psz_file;
1138 p_module->handle = handle;
1139 #ifndef HAVE_SHARED_LIBVLC
1140 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1142 p_module->b_loaded = VLC_TRUE;
1144 /* Initialize the module: fill p_module, default config */
1145 if( CallEntry( p_module ) != 0 )
1147 /* We couldn't call module_init() */
1148 vlc_object_destroy( p_module );
1149 CloseModule( handle );
1153 DupModule( p_module );
1154 p_module->psz_filename = strdup( p_module->psz_filename );
1156 /* Everything worked fine ! The module is ready to be added to the list. */
1157 p_module->b_builtin = VLC_FALSE;
1162 /*****************************************************************************
1163 * DupModule: make a plugin module standalone.
1164 *****************************************************************************
1165 * This function duplicates all strings in the module, so that the dynamic
1166 * object can be unloaded. It acts recursively on submodules.
1167 *****************************************************************************/
1168 static void DupModule( module_t *p_module )
1170 const char **pp_shortcut;
1173 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1175 *pp_shortcut = strdup( *pp_shortcut );
1178 /* We strdup() these entries so that they are still valid when the
1179 * module is unloaded. */
1180 p_module->psz_object_name = strdup( p_module->psz_object_name );
1181 p_module->psz_capability = strdup( p_module->psz_capability );
1182 p_module->psz_shortname = p_module->psz_shortname ?
1183 strdup( p_module->psz_shortname ) : NULL;
1184 p_module->psz_longname = strdup( p_module->psz_longname );
1185 p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1188 if( p_module->psz_program != NULL )
1190 p_module->psz_program = strdup( p_module->psz_program );
1193 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1195 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1199 /*****************************************************************************
1200 * UndupModule: free a duplicated module.
1201 *****************************************************************************
1202 * This function frees the allocations done in DupModule().
1203 *****************************************************************************/
1204 static void UndupModule( module_t *p_module )
1206 const char **pp_shortcut;
1209 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1211 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1214 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1216 free( (void*)*pp_shortcut );
1219 free( (void*)p_module->psz_object_name );
1220 free( (void*)p_module->psz_capability );
1221 free( (void*)p_module->psz_shortname );
1222 free( (void*)p_module->psz_longname );
1223 free( (void*)p_module->psz_help );
1224 free( (void*)p_module->psz_program );
1227 #endif /* HAVE_DYNAMIC_PLUGINS */
1229 /*****************************************************************************
1230 * AllocateBuiltinModule: initialize a builtin module.
1231 *****************************************************************************
1232 * This function registers a builtin module and allocates a structure
1233 * for its information data. The module can then be handled by module_Need
1234 * and module_Unneed. It can be removed by DeleteModule.
1235 *****************************************************************************/
1236 static int AllocateBuiltinModule( vlc_object_t * p_this,
1237 int ( *pf_entry ) ( module_t * ) )
1239 module_t * p_module;
1241 /* Now that we have successfully loaded the module, we can
1242 * allocate a structure for it */
1243 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1244 if( p_module == NULL )
1246 msg_Err( p_this, "out of memory" );
1250 /* Initialize the module : fill p_module->psz_object_name, etc. */
1251 if( pf_entry( p_module ) != 0 )
1253 /* With a well-written module we shouldn't have to print an
1254 * additional error message here, but just make sure. */
1255 msg_Err( p_this, "failed calling entry point in builtin module" );
1256 vlc_object_destroy( p_module );
1260 /* Everything worked fine ! The module is ready to be added to the list. */
1261 p_module->b_builtin = VLC_TRUE;
1263 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1264 p_module->psz_object_name, p_module->psz_longname ); */
1266 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1271 /*****************************************************************************
1272 * DeleteModule: delete a module and its structure.
1273 *****************************************************************************
1274 * This function can only be called if the module isn't being used.
1275 *****************************************************************************/
1276 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1278 if( !p_module ) return VLC_EGENERIC;
1280 vlc_object_detach( p_module );
1282 /* We free the structures that we strdup()ed in Allocate*Module(). */
1283 #ifdef HAVE_DYNAMIC_PLUGINS
1284 if( !p_module->b_builtin )
1286 if( p_module->b_loaded && p_module->b_unloadable )
1288 CloseModule( p_module->handle );
1290 UndupModule( p_module );
1291 free( p_module->psz_filename );
1295 /* Free and detach the object's children */
1296 while( p_module->i_children )
1298 vlc_object_t *p_this = p_module->pp_children[0];
1299 vlc_object_detach( p_this );
1300 vlc_object_destroy( p_this );
1303 config_Free( p_module );
1304 vlc_object_destroy( p_module );
1309 #ifdef HAVE_DYNAMIC_PLUGINS
1310 /*****************************************************************************
1311 * CallEntry: call an entry point.
1312 *****************************************************************************
1313 * This function calls a symbol given its name and a module structure. The
1314 * symbol MUST refer to a function returning int and taking a module_t* as
1316 *****************************************************************************/
1317 static int CallEntry( module_t * p_module )
1319 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1320 int (* pf_symbol) ( module_t * p_module );
1322 /* Try to resolve the symbol */
1323 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1325 if( pf_symbol == NULL )
1327 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1328 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1329 psz_name, p_module->psz_filename );
1330 #elif defined(HAVE_DL_WINDOWS)
1331 char *psz_error = GetWindowsError();
1332 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1333 psz_name, p_module->psz_filename, psz_error );
1335 #elif defined(HAVE_DL_DLOPEN)
1336 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1337 psz_name, p_module->psz_filename, dlerror() );
1338 #elif defined(HAVE_DL_SHL_LOAD)
1339 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1340 psz_name, p_module->psz_filename, strerror(errno) );
1342 # error "Something is wrong in modules.c"
1347 /* We can now try to call the symbol */
1348 if( pf_symbol( p_module ) != 0 )
1350 /* With a well-written module we shouldn't have to print an
1351 * additional error message here, but just make sure. */
1352 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1353 psz_name, p_module->psz_filename );
1357 /* Everything worked fine, we can return */
1361 /*****************************************************************************
1362 * LoadModule: loads a dynamic library
1363 *****************************************************************************
1364 * This function loads a dynamically linked library using a system dependant
1365 * method. Will return 0 on success as well as the module handle.
1366 *****************************************************************************/
1367 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1368 module_handle_t *p_handle )
1370 module_handle_t handle;
1372 #if defined(HAVE_DL_DYLD)
1373 NSObjectFileImage image;
1374 NSObjectFileImageReturnCode ret;
1376 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1378 if( ret != NSObjectFileImageSuccess )
1380 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1384 /* Open the dynamic module */
1385 handle = NSLinkModule( image, psz_file,
1386 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1390 NSLinkEditErrors errors;
1391 const char *psz_file, *psz_err;
1393 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1394 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1395 NSDestroyObjectFileImage( image );
1399 /* Destroy our image, we won't need it */
1400 NSDestroyObjectFileImage( image );
1402 #elif defined(HAVE_DL_BEOS)
1403 handle = load_add_on( psz_file );
1406 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1410 #elif defined(HAVE_DL_WINDOWS)
1413 wchar_t psz_wfile[MAX_PATH];
1414 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1415 handle = LoadLibrary( psz_wfile );
1418 handle = LoadLibrary( psz_file );
1420 if( handle == NULL )
1422 char *psz_err = GetWindowsError();
1423 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1428 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1429 /* static is OK, we are called atomically */
1431 # if defined(SYS_LINUX)
1432 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1433 * are going to get namespace collisions when two modules have common
1434 * public symbols, but ALSA is being a pest here. */
1435 if( strstr( psz_file, "alsa_plugin" ) )
1437 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1438 if( handle == NULL )
1440 msg_Warn( p_this, "cannot load module `%s' (%s)",
1441 psz_file, dlerror() );
1447 handle = dlopen( psz_file, RTLD_NOW );
1448 if( handle == NULL )
1450 msg_Warn( p_this, "cannot load module `%s' (%s)",
1451 psz_file, dlerror() );
1455 #elif defined(HAVE_DL_DLOPEN)
1456 # if defined(DL_LAZY)
1457 handle = dlopen( psz_file, DL_LAZY );
1459 handle = dlopen( psz_file, 0 );
1461 if( handle == NULL )
1463 msg_Warn( p_this, "cannot load module `%s' (%s)",
1464 psz_file, dlerror() );
1468 #elif defined(HAVE_DL_SHL_LOAD)
1469 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1470 if( handle == NULL )
1472 msg_Warn( p_this, "cannot load module `%s' (%s)",
1473 psz_file, strerror(errno) );
1478 # error "Something is wrong in modules.c"
1486 /*****************************************************************************
1487 * CloseModule: unload a dynamic library
1488 *****************************************************************************
1489 * This function unloads a previously opened dynamically linked library
1490 * using a system dependant method. No return value is taken in consideration,
1491 * since some libraries sometimes refuse to close properly.
1492 *****************************************************************************/
1493 static void CloseModule( module_handle_t handle )
1495 #if defined(HAVE_DL_DYLD)
1496 NSUnLinkModule( handle, FALSE );
1498 #elif defined(HAVE_DL_BEOS)
1499 unload_add_on( handle );
1501 #elif defined(HAVE_DL_WINDOWS)
1502 FreeLibrary( handle );
1504 #elif defined(HAVE_DL_DLOPEN)
1507 #elif defined(HAVE_DL_SHL_LOAD)
1508 shl_unload( handle );
1514 /*****************************************************************************
1515 * GetSymbol: get a symbol from a dynamic library
1516 *****************************************************************************
1517 * This function queries a loaded library for a symbol specified in a
1518 * string, and returns a pointer to it. We don't check for dlerror() or
1519 * similar functions, since we want a non-NULL symbol anyway.
1520 *****************************************************************************/
1521 static void * _module_getsymbol( module_handle_t, const char * );
1523 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1525 void * p_symbol = _module_getsymbol( handle, psz_function );
1527 /* MacOS X dl library expects symbols to begin with "_". So do
1528 * some other operating systems. That's really lame, but hey, what
1530 if( p_symbol == NULL )
1532 char *psz_call = malloc( strlen( psz_function ) + 2 );
1534 strcpy( psz_call + 1, psz_function );
1535 psz_call[ 0 ] = '_';
1536 p_symbol = _module_getsymbol( handle, psz_call );
1543 static void * _module_getsymbol( module_handle_t handle,
1544 const char * psz_function )
1546 #if defined(HAVE_DL_DYLD)
1547 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1548 return NSAddressOfSymbol( sym );
1550 #elif defined(HAVE_DL_BEOS)
1552 if( B_OK == get_image_symbol( handle, psz_function,
1553 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1562 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1563 wchar_t psz_real[256];
1564 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1566 return (void *)GetProcAddress( handle, psz_real );
1568 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1569 return (void *)GetProcAddress( handle, (char *)psz_function );
1571 #elif defined(HAVE_DL_DLOPEN)
1572 return dlsym( handle, psz_function );
1574 #elif defined(HAVE_DL_SHL_LOAD)
1576 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1582 #if defined(HAVE_DL_WINDOWS)
1583 static char * GetWindowsError( void )
1585 #if defined(UNDER_CE)
1586 wchar_t psz_tmp[MAX_PATH];
1587 char * psz_buffer = malloc( MAX_PATH );
1589 char * psz_tmp = malloc( MAX_PATH );
1591 int i = 0, i_error = GetLastError();
1593 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1594 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1595 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1597 /* Go to the end of the string */
1598 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1605 #if defined(UNDER_CE)
1606 swprintf( psz_tmp + i, L" (error %i)", i_error );
1607 psz_tmp[ 255 ] = L'\0';
1609 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1610 psz_tmp[ 255 ] = '\0';
1614 #if defined(UNDER_CE)
1615 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1621 #endif /* HAVE_DL_WINDOWS */
1623 /*****************************************************************************
1624 * LoadPluginsCache: loads the plugins cache file
1625 *****************************************************************************
1626 * This function will load the plugin cache if present and valid. This cache
1627 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1628 * actually load the dynamically loadable module.
1629 * This allows us to only fully load plugins when they are actually used.
1630 *****************************************************************************/
1631 static void CacheLoad( vlc_object_t *p_this )
1633 char *psz_filename, *psz_homedir;
1635 int i, j, i_size, i_read;
1636 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1637 char p_cachelang[6], p_lang[6];
1639 module_cache_t **pp_cache = 0;
1640 int32_t i_file_size, i_marker;
1642 psz_homedir = p_this->p_libvlc->psz_homedir;
1645 msg_Err( p_this, "psz_homedir is null" );
1649 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1650 PLUGINSCACHE_DIR, CacheName() );
1653 msg_Err( p_this, "out of memory" );
1657 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1659 #if !defined( UNDER_CE )
1660 unlink( psz_filename );
1662 wchar_t psz_wf[MAX_PATH];
1663 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1664 DeleteFile( psz_wf );
1666 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1667 free( psz_filename );
1671 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1673 file = utf8_fopen( psz_filename, "rb" );
1676 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1678 free( psz_filename );
1681 free( psz_filename );
1683 /* Check the file size */
1684 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1685 if( i_read != sizeof(i_file_size) )
1687 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1693 fseek( file, 0, SEEK_END );
1694 if( ftell( file ) != i_file_size )
1696 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1697 "(corrupted size)" );
1701 fseek( file, sizeof(i_file_size), SEEK_SET );
1703 /* Check the file is a plugins cache */
1704 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1705 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1706 if( i_read != i_size ||
1707 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1709 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1714 /* Check Sub-version number */
1715 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1716 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1718 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1719 "(corrupted header)" );
1724 /* Check the language hasn't changed */
1725 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1726 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1727 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1729 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1730 "(language changed)" );
1735 /* Check header marker */
1736 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1737 if( i_read != sizeof(i_marker) ||
1738 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1740 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1741 "(corrupted header)" );
1746 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1747 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1749 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1750 malloc( i_cache * sizeof(void *) );
1752 #define LOAD_IMMEDIATE(a) \
1753 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1754 #define LOAD_STRING(a) \
1757 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1758 || ( i_size > 16384 ) ) \
1761 char *psz = malloc( i_size ); \
1762 if( fread( psz, i_size, 1, file ) != 1 ) { \
1766 if( psz[i_size-1] ) { \
1774 for( i = 0; i < i_cache; i++ )
1779 pp_cache[i] = malloc( sizeof(module_cache_t) );
1780 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1782 /* Load common info */
1783 LOAD_STRING( pp_cache[i]->psz_file );
1784 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1785 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1786 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1787 pp_cache[i]->b_used = VLC_FALSE;
1789 if( pp_cache[i]->b_junk ) continue;
1791 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1793 /* Load additional infos */
1794 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1795 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1796 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1797 LOAD_STRING( pp_cache[i]->p_module->psz_help );
1798 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1799 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1801 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1803 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1804 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1805 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1806 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1807 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1808 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1811 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1814 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1816 LOAD_IMMEDIATE( i_submodules );
1818 while( i_submodules-- )
1820 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1821 vlc_object_attach( p_module, pp_cache[i]->p_module );
1822 p_module->b_submodule = VLC_TRUE;
1824 LOAD_STRING( p_module->psz_object_name );
1825 LOAD_STRING( p_module->psz_shortname );
1826 LOAD_STRING( p_module->psz_longname );
1827 LOAD_STRING( p_module->psz_help );
1828 LOAD_STRING( p_module->psz_program );
1829 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1831 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1833 LOAD_STRING( p_module->psz_capability );
1834 LOAD_IMMEDIATE( p_module->i_score );
1835 LOAD_IMMEDIATE( p_module->i_cpu );
1836 LOAD_IMMEDIATE( p_module->b_unloadable );
1837 LOAD_IMMEDIATE( p_module->b_reentrant );
1846 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1849 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1856 int CacheLoadConfig( module_t *p_module, FILE *file )
1861 /* Calculate the structure length */
1862 LOAD_IMMEDIATE( p_module->i_config_items );
1863 LOAD_IMMEDIATE( p_module->i_bool_items );
1865 LOAD_IMMEDIATE( i_lines );
1867 /* Allocate memory */
1870 p_module->p_config =
1871 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1872 if( p_module->p_config == NULL )
1874 p_module->confsize = 0;
1875 msg_Err( p_module, "config error: can't duplicate p_config" );
1879 p_module->confsize = i_lines;
1881 /* Do the duplication job */
1882 for (size_t i = 0; i < i_lines; i++ )
1884 LOAD_IMMEDIATE( p_module->p_config[i] );
1886 LOAD_STRING( p_module->p_config[i].psz_type );
1887 LOAD_STRING( p_module->p_config[i].psz_name );
1888 LOAD_STRING( p_module->p_config[i].psz_text );
1889 LOAD_STRING( p_module->p_config[i].psz_longtext );
1890 LOAD_STRING( p_module->p_config[i].psz_current );
1892 if (IsConfigStringType (p_module->p_config[i].i_type))
1894 LOAD_STRING (p_module->p_config[i].orig.psz);
1895 p_module->p_config[i].value.psz =
1896 (p_module->p_config[i].orig.psz != NULL)
1897 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1898 p_module->p_config[i].saved.psz = NULL;
1902 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1903 sizeof (p_module->p_config[i].value));
1904 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1905 sizeof (p_module->p_config[i].saved));
1908 p_module->p_config[i].b_dirty = VLC_FALSE;
1910 p_module->p_config[i].p_lock = &p_module->object_lock;
1912 if( p_module->p_config[i].i_list )
1914 if( p_module->p_config[i].ppsz_list )
1917 p_module->p_config[i].ppsz_list =
1918 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1919 if( p_module->p_config[i].ppsz_list )
1921 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1922 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1923 p_module->p_config[i].ppsz_list[j] = NULL;
1926 if( p_module->p_config[i].ppsz_list_text )
1929 p_module->p_config[i].ppsz_list_text =
1930 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1931 if( p_module->p_config[i].ppsz_list_text )
1933 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1934 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1935 p_module->p_config[i].ppsz_list_text[j] = NULL;
1938 if( p_module->p_config[i].pi_list )
1940 p_module->p_config[i].pi_list =
1941 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1942 if( p_module->p_config[i].pi_list )
1944 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1945 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1950 if( p_module->p_config[i].i_action )
1952 p_module->p_config[i].ppf_action =
1953 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1954 p_module->p_config[i].ppsz_action_text =
1955 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1957 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1959 p_module->p_config[i].ppf_action[j] = 0;
1960 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1964 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1971 return VLC_EGENERIC;
1974 /*****************************************************************************
1975 * SavePluginsCache: saves the plugins cache to a file
1976 *****************************************************************************/
1977 static void CacheSave( vlc_object_t *p_this )
1979 static char const psz_tag[] =
1980 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1981 "# This file is a cache directory tag created by VLC.\r\n"
1982 "# For information about cache directory tags, see:\r\n"
1983 "# http://www.brynosaurus.com/cachedir/\r\n";
1985 char *psz_filename, *psz_homedir;
1988 module_cache_t **pp_cache;
1989 int32_t i_file_size = 0;
1991 psz_homedir = p_this->p_libvlc->psz_homedir;
1994 msg_Err( p_this, "psz_homedir is null" );
1998 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1999 strlen(psz_homedir) + strlen(CacheName()) );
2003 msg_Err( p_this, "out of memory" );
2007 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
2009 config_CreateDir( p_this, psz_filename );
2011 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
2013 config_CreateDir( p_this, psz_filename );
2015 strcat( psz_filename, "/CACHEDIR.TAG" );
2017 file = utf8_fopen( psz_filename, "wb" );
2020 fwrite( psz_tag, 1, strlen(psz_tag), file );
2024 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
2025 PLUGINSCACHE_DIR, CacheName() );
2027 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2029 file = utf8_fopen( psz_filename, "wb" );
2032 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2034 free( psz_filename );
2037 free( psz_filename );
2039 /* Empty space for file size */
2040 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2042 /* Contains version number */
2043 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2045 /* Sub-version number (to avoid breakage in the dev version when cache
2046 * structure changes) */
2047 i_file_size = CACHE_SUBVERSION_NUM;
2048 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2051 fprintf( file, "%5.5s", _("C") );
2054 i_file_size = ftell( file );
2055 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2057 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2058 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2060 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2062 #define SAVE_IMMEDIATE(a) \
2063 fwrite( &a, sizeof(char), sizeof(a), file )
2064 #define SAVE_STRING(a) \
2065 { i_size = a ? strlen( a ) + 1 : 0; \
2066 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2067 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2070 for( i = 0; i < i_cache; i++ )
2073 uint32_t i_submodule;
2075 /* Save common info */
2076 SAVE_STRING( pp_cache[i]->psz_file );
2077 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2078 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2079 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2081 if( pp_cache[i]->b_junk ) continue;
2083 /* Save additional infos */
2084 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2085 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2086 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2087 SAVE_STRING( pp_cache[i]->p_module->psz_help );
2088 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2089 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2091 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2093 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2094 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2095 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2096 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2097 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2098 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2101 CacheSaveConfig( pp_cache[i]->p_module, file );
2103 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2105 i_submodule = pp_cache[i]->p_module->i_children;
2106 SAVE_IMMEDIATE( i_submodule );
2107 for( i_submodule = 0;
2108 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2111 module_t *p_module =
2112 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2114 SAVE_STRING( p_module->psz_object_name );
2115 SAVE_STRING( p_module->psz_shortname );
2116 SAVE_STRING( p_module->psz_longname );
2117 SAVE_STRING( p_module->psz_help );
2118 SAVE_STRING( p_module->psz_program );
2119 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2121 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2123 SAVE_STRING( p_module->psz_capability );
2124 SAVE_IMMEDIATE( p_module->i_score );
2125 SAVE_IMMEDIATE( p_module->i_cpu );
2126 SAVE_IMMEDIATE( p_module->b_unloadable );
2127 SAVE_IMMEDIATE( p_module->b_reentrant );
2131 /* Fill-up file size */
2132 i_file_size = ftell( file );
2133 fseek( file, 0, SEEK_SET );
2134 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2141 void CacheSaveConfig( module_t *p_module, FILE *file )
2143 uint32_t i_lines = p_module->confsize;
2146 SAVE_IMMEDIATE( p_module->i_config_items );
2147 SAVE_IMMEDIATE( p_module->i_bool_items );
2148 SAVE_IMMEDIATE( i_lines );
2150 for (size_t i = 0; i < i_lines ; i++)
2152 SAVE_IMMEDIATE( p_module->p_config[i] );
2154 SAVE_STRING( p_module->p_config[i].psz_type );
2155 SAVE_STRING( p_module->p_config[i].psz_name );
2156 SAVE_STRING( p_module->p_config[i].psz_text );
2157 SAVE_STRING( p_module->p_config[i].psz_longtext );
2158 SAVE_STRING( p_module->p_config[i].psz_current );
2159 if (IsConfigStringType (p_module->p_config[i].i_type))
2160 SAVE_STRING( p_module->p_config[i].orig.psz );
2162 if( p_module->p_config[i].i_list )
2164 if( p_module->p_config[i].ppsz_list )
2166 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2167 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2170 if( p_module->p_config[i].ppsz_list_text )
2172 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2173 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2175 if( p_module->p_config[i].pi_list )
2177 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2178 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2182 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2183 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2185 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2189 /*****************************************************************************
2190 * CacheName: Return the cache file name for this platform.
2191 *****************************************************************************/
2192 static char *CacheName( void )
2194 static char psz_cachename[32];
2196 /* Code int size, pointer size and endianness in the filename */
2197 int32_t x = 0xbe00001e;
2198 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2199 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2200 return psz_cachename;
2203 /*****************************************************************************
2204 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2205 *****************************************************************************/
2206 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2207 module_t *p_module )
2212 p_cache->pf_activate = p_module->pf_activate;
2213 p_cache->pf_deactivate = p_module->pf_deactivate;
2214 #ifndef HAVE_SHARED_LIBVLC
2215 p_cache->p_symbols = p_module->p_symbols;
2217 p_cache->handle = p_module->handle;
2219 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2221 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2222 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2223 p_cchild->pf_activate = p_child->pf_activate;
2224 p_cchild->pf_deactivate = p_child->pf_deactivate;
2225 #ifndef HAVE_SHARED_LIBVLC
2226 p_cchild->p_symbols = p_child->p_symbols;
2230 p_cache->b_loaded = VLC_TRUE;
2231 p_module->b_loaded = VLC_FALSE;
2234 /*****************************************************************************
2235 * CacheFind: finds the cache entry corresponding to a file
2236 *****************************************************************************/
2237 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2238 int64_t i_time, int64_t i_size )
2240 module_cache_t **pp_cache;
2243 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2244 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2246 for( i = 0; i < i_cache; i++ )
2248 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2249 pp_cache[i]->i_time == i_time &&
2250 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2256 #endif /* HAVE_DYNAMIC_PLUGINS */