1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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 1
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" ) )
453 if( psz_var ) free( psz_var );
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( psz_alias && !p_this->psz_object_name )
715 /* This assumes that p_this is the object which will be using the
716 * module. That's not always the case ... but it is in most cases.
718 p_this->psz_object_name = strdup( psz_alias );
722 free( psz_shortcuts );
730 /* Don't forget that the module is still locked */
734 /*****************************************************************************
735 * module_Unneed: decrease the usage count of a module.
736 *****************************************************************************
737 * This function must be called by the thread that called module_Need, to
738 * decrease the reference count and allow for hiding of modules.
739 *****************************************************************************/
740 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
742 /* Use the close method */
743 if( p_module->pf_deactivate )
745 p_module->pf_deactivate( p_this );
748 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
750 vlc_object_release( p_module );
755 /*****************************************************************************
756 * module_Exists: tell if a module exists.
757 *****************************************************************************
758 * This function is a boolean function that tells if a module exist or not.
759 *****************************************************************************/
761 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
765 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
766 for( i = 0 ; i < p_list->i_count; i++)
769 ((module_t *) p_list->p_values[i].p_object)->psz_shortname ,
772 /* We can release the list, and return yes */
773 vlc_list_release( p_list ); return VLC_TRUE;
776 vlc_list_release( p_list ); return VLC_FALSE;
780 /*****************************************************************************
781 * Following functions are local.
782 *****************************************************************************/
784 /*****************************************************************************
785 * AllocateAllPlugins: load all plugin modules we can find.
786 *****************************************************************************/
787 #ifdef HAVE_DYNAMIC_PLUGINS
788 static void AllocateAllPlugins( vlc_object_t *p_this )
790 /* Yes, there are two NULLs because we replace one with "plugin-path". */
791 #if defined( WIN32 ) || defined( UNDER_CE )
792 const char *path[] = { "modules", "", "plugins", NULL, NULL };
794 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
797 const char *const *ppsz_path;
799 /* If the user provided a plugin path, we add it to the list */
800 char *userpath = config_GetPsz( p_this, "plugin-path" );
801 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
803 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
807 if (!**ppsz_path) continue;
809 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
811 /* Handle relative as well as absolute paths */
813 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
814 (*ppsz_path)[1] != ':' )
816 if( (*ppsz_path)[0] != '/' )
819 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
820 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
825 psz_fullpath = strdup( *ppsz_path );
827 if( psz_fullpath == NULL )
830 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
832 /* Don't go deeper than 5 subdirectories */
833 AllocatePluginDir( p_this, psz_fullpath, 5 );
835 free( psz_fullpath );
838 /* Free plugin-path */
839 if( userpath != NULL )
843 /*****************************************************************************
844 * AllocatePluginDir: recursively parse a directory to look for plugins
845 *****************************************************************************/
846 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
849 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
855 #if defined( UNDER_CE ) || defined( _MSC_VER )
857 wchar_t psz_wpath[MAX_PATH + 256];
858 wchar_t psz_wdir[MAX_PATH];
860 char psz_path[MAX_PATH + 256];
861 WIN32_FIND_DATA finddata;
867 struct dirent * file;
871 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
876 #if defined( UNDER_CE ) || defined( _MSC_VER )
878 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
880 rc = GetFileAttributes( psz_wdir );
881 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
883 /* Parse all files in the directory */
884 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
886 rc = GetFileAttributes( psz_dir );
887 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
890 /* Parse all files in the directory */
891 sprintf( psz_path, "%s\\*", psz_dir );
894 handle = FindFirstFile( psz_wpath, &finddata );
896 handle = FindFirstFile( psz_path, &finddata );
898 if( handle == INVALID_HANDLE_VALUE )
900 /* Empty directory */
904 /* Parse the directory and try to load all files it contains. */
908 unsigned int i_len = wcslen( finddata.cFileName );
909 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
910 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
912 unsigned int i_len = strlen( finddata.cFileName );
913 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
917 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
918 || !strcmp( finddata.cFileName, ".." ) )
920 if( !FindNextFile( handle, &finddata ) ) break;
925 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
927 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
930 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
932 else if( i_len > strlen( LIBEXT )
933 /* We only load files ending with LIBEXT */
934 && !strncasecmp( psz_path + strlen( psz_path)
936 LIBEXT, strlen( LIBEXT ) ) )
938 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
939 int64_t i_time = 0, i_size = 0;
942 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
945 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
949 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
951 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
952 i_size = attrbuf.nFileSizeHigh;
954 i_size |= attrbuf.nFileSizeLow;
958 AllocatePluginFile( p_this, psz_file, i_time, i_size );
961 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
963 /* Close the directory */
967 dir = opendir( psz_dir );
973 i_dirlen = strlen( psz_dir );
975 /* Parse the directory and try to load all files it contains. */
976 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
983 if( !*file->d_name || !strcmp( file->d_name, "." )
984 || !strcmp( file->d_name, ".." ) )
989 i_len = strlen( file->d_name );
990 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
992 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
994 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
997 i_stat = stat( psz_file, &statbuf );
998 if( !i_stat && statbuf.st_mode & S_IFDIR )
1000 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1002 else if( i_len > strlen( LIBEXT )
1003 /* We only load files ending with LIBEXT */
1004 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1005 LIBEXT, strlen( LIBEXT ) ) )
1007 int64_t i_time = 0, i_size = 0;
1011 i_time = statbuf.st_mtime;
1012 i_size = statbuf.st_size;
1015 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1021 /* Close the directory */
1027 /*****************************************************************************
1028 * AllocatePluginFile: load a module into memory and initialize it.
1029 *****************************************************************************
1030 * This function loads a dynamically loadable module and allocates a structure
1031 * for its information data. The module can then be handled by module_Need
1032 * and module_Unneed. It can be removed by DeleteModule.
1033 *****************************************************************************/
1034 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1035 int64_t i_file_time, int64_t i_file_size )
1037 module_t * p_module = NULL;
1038 module_cache_t *p_cache_entry = NULL;
1041 * Check our plugins cache first then load plugin if needed
1044 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1046 if( !p_cache_entry )
1048 p_module = AllocatePlugin( p_this, psz_file );
1052 /* If junk dll, don't try to load it */
1053 if( p_cache_entry->b_junk )
1059 module_config_t *p_item = NULL, *p_end = NULL;
1061 p_module = p_cache_entry->p_module;
1062 p_module->b_loaded = VLC_FALSE;
1064 /* For now we force loading if the module's config contains
1065 * callbacks or actions.
1066 * Could be optimized by adding an API call.*/
1067 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1068 p_item < p_end; p_item++ )
1070 if( p_item->pf_callback || p_item->i_action )
1072 p_module = AllocatePlugin( p_this, psz_file );
1076 if( p_module == p_cache_entry->p_module )
1077 p_cache_entry->b_used = VLC_TRUE;
1083 /* Everything worked fine !
1084 * The module is ready to be added to the list. */
1085 p_module->b_builtin = VLC_FALSE;
1087 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1088 p_module->psz_object_name, p_module->psz_longname ); */
1090 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1092 if( !p_this->p_libvlc_global->p_module_bank->b_cache )
1095 /* Add entry to cache */
1096 #define p_bank p_this->p_libvlc_global->p_module_bank
1098 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1099 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1100 if( !p_bank->pp_cache[p_bank->i_cache] )
1102 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1103 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1104 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1105 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1106 p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1107 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1111 return p_module ? 0 : -1;
1114 /*****************************************************************************
1115 * AllocatePlugin: load a module into memory and initialize it.
1116 *****************************************************************************
1117 * This function loads a dynamically loadable module and allocates a structure
1118 * for its information data. The module can then be handled by module_Need
1119 * and module_Unneed. It can be removed by DeleteModule.
1120 *****************************************************************************/
1121 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1123 module_t * p_module = NULL;
1124 module_handle_t handle;
1126 if( LoadModule( p_this, psz_file, &handle ) )
1129 /* Now that we have successfully loaded the module, we can
1130 * allocate a structure for it */
1131 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1132 if( p_module == NULL )
1134 msg_Err( p_this, "out of memory" );
1135 CloseModule( handle );
1139 /* We need to fill these since they may be needed by CallEntry() */
1140 p_module->psz_filename = psz_file;
1141 p_module->handle = handle;
1142 #ifndef HAVE_SHARED_LIBVLC
1143 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1145 p_module->b_loaded = VLC_TRUE;
1147 /* Initialize the module: fill p_module, default config */
1148 if( CallEntry( p_module ) != 0 )
1150 /* We couldn't call module_init() */
1151 vlc_object_destroy( p_module );
1152 CloseModule( handle );
1156 DupModule( p_module );
1157 p_module->psz_filename = strdup( p_module->psz_filename );
1159 /* Everything worked fine ! The module is ready to be added to the list. */
1160 p_module->b_builtin = VLC_FALSE;
1165 /*****************************************************************************
1166 * DupModule: make a plugin module standalone.
1167 *****************************************************************************
1168 * This function duplicates all strings in the module, so that the dynamic
1169 * object can be unloaded. It acts recursively on submodules.
1170 *****************************************************************************/
1171 static void DupModule( module_t *p_module )
1173 const char **pp_shortcut;
1176 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1178 *pp_shortcut = strdup( *pp_shortcut );
1181 /* We strdup() these entries so that they are still valid when the
1182 * module is unloaded. */
1183 p_module->psz_object_name = strdup( p_module->psz_object_name );
1184 p_module->psz_capability = strdup( p_module->psz_capability );
1185 p_module->psz_shortname = p_module->psz_shortname ?
1186 strdup( p_module->psz_shortname ) : NULL;
1187 p_module->psz_longname = strdup( p_module->psz_longname );
1189 if( p_module->psz_program != NULL )
1191 p_module->psz_program = strdup( p_module->psz_program );
1194 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1196 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1200 /*****************************************************************************
1201 * UndupModule: free a duplicated module.
1202 *****************************************************************************
1203 * This function frees the allocations done in DupModule().
1204 *****************************************************************************/
1205 static void UndupModule( module_t *p_module )
1207 const char **pp_shortcut;
1210 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1212 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1215 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1217 free( (void *)*pp_shortcut );
1220 free( (void *)p_module->psz_object_name );
1221 free( (void *)p_module->psz_capability );
1222 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1223 free( (void *)p_module->psz_longname );
1225 if( p_module->psz_program != NULL )
1227 free( (void *)p_module->psz_program );
1231 #endif /* HAVE_DYNAMIC_PLUGINS */
1233 /*****************************************************************************
1234 * AllocateBuiltinModule: initialize a builtin module.
1235 *****************************************************************************
1236 * This function registers a builtin module and allocates a structure
1237 * for its information data. The module can then be handled by module_Need
1238 * and module_Unneed. It can be removed by DeleteModule.
1239 *****************************************************************************/
1240 static int AllocateBuiltinModule( vlc_object_t * p_this,
1241 int ( *pf_entry ) ( module_t * ) )
1243 module_t * p_module;
1245 /* Now that we have successfully loaded the module, we can
1246 * allocate a structure for it */
1247 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1248 if( p_module == NULL )
1250 msg_Err( p_this, "out of memory" );
1254 /* Initialize the module : fill p_module->psz_object_name, etc. */
1255 if( pf_entry( p_module ) != 0 )
1257 /* With a well-written module we shouldn't have to print an
1258 * additional error message here, but just make sure. */
1259 msg_Err( p_this, "failed calling entry point in builtin module" );
1260 vlc_object_destroy( p_module );
1264 /* Everything worked fine ! The module is ready to be added to the list. */
1265 p_module->b_builtin = VLC_TRUE;
1267 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1268 p_module->psz_object_name, p_module->psz_longname ); */
1270 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1275 /*****************************************************************************
1276 * DeleteModule: delete a module and its structure.
1277 *****************************************************************************
1278 * This function can only be called if the module isn't being used.
1279 *****************************************************************************/
1280 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1282 if( !p_module ) return VLC_EGENERIC;
1284 vlc_object_detach( p_module );
1286 /* We free the structures that we strdup()ed in Allocate*Module(). */
1287 #ifdef HAVE_DYNAMIC_PLUGINS
1288 if( !p_module->b_builtin )
1290 if( p_module->b_loaded && p_module->b_unloadable )
1292 CloseModule( p_module->handle );
1294 UndupModule( p_module );
1295 free( p_module->psz_filename );
1299 /* Free and detach the object's children */
1300 while( p_module->i_children )
1302 vlc_object_t *p_this = p_module->pp_children[0];
1303 vlc_object_detach( p_this );
1304 vlc_object_destroy( p_this );
1307 config_Free( p_module );
1308 vlc_object_destroy( p_module );
1313 #ifdef HAVE_DYNAMIC_PLUGINS
1314 /*****************************************************************************
1315 * CallEntry: call an entry point.
1316 *****************************************************************************
1317 * This function calls a symbol given its name and a module structure. The
1318 * symbol MUST refer to a function returning int and taking a module_t* as
1320 *****************************************************************************/
1321 static int CallEntry( module_t * p_module )
1323 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1324 int (* pf_symbol) ( module_t * p_module );
1326 /* Try to resolve the symbol */
1327 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1329 if( pf_symbol == NULL )
1331 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1332 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1333 psz_name, p_module->psz_filename );
1334 #elif defined(HAVE_DL_WINDOWS)
1335 char *psz_error = GetWindowsError();
1336 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1337 psz_name, p_module->psz_filename, psz_error );
1339 #elif defined(HAVE_DL_DLOPEN)
1340 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1341 psz_name, p_module->psz_filename, dlerror() );
1342 #elif defined(HAVE_DL_SHL_LOAD)
1343 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1344 psz_name, p_module->psz_filename, strerror(errno) );
1346 # error "Something is wrong in modules.c"
1351 /* We can now try to call the symbol */
1352 if( pf_symbol( p_module ) != 0 )
1354 /* With a well-written module we shouldn't have to print an
1355 * additional error message here, but just make sure. */
1356 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1357 psz_name, p_module->psz_filename );
1361 /* Everything worked fine, we can return */
1365 /*****************************************************************************
1366 * LoadModule: loads a dynamic library
1367 *****************************************************************************
1368 * This function loads a dynamically linked library using a system dependant
1369 * method. Will return 0 on success as well as the module handle.
1370 *****************************************************************************/
1371 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1372 module_handle_t *p_handle )
1374 module_handle_t handle;
1376 #if defined(HAVE_DL_DYLD)
1377 NSObjectFileImage image;
1378 NSObjectFileImageReturnCode ret;
1380 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1382 if( ret != NSObjectFileImageSuccess )
1384 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1388 /* Open the dynamic module */
1389 handle = NSLinkModule( image, psz_file,
1390 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1394 NSLinkEditErrors errors;
1395 const char *psz_file, *psz_err;
1397 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1398 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1399 NSDestroyObjectFileImage( image );
1403 /* Destroy our image, we won't need it */
1404 NSDestroyObjectFileImage( image );
1406 #elif defined(HAVE_DL_BEOS)
1407 handle = load_add_on( psz_file );
1410 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1414 #elif defined(HAVE_DL_WINDOWS)
1417 wchar_t psz_wfile[MAX_PATH];
1418 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1419 handle = LoadLibrary( psz_wfile );
1422 handle = LoadLibrary( psz_file );
1424 if( handle == NULL )
1426 char *psz_err = GetWindowsError();
1427 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1432 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1433 /* static is OK, we are called atomically */
1435 # if defined(SYS_LINUX)
1436 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1437 * are going to get namespace collisions when two modules have common
1438 * public symbols, but ALSA is being a pest here. */
1439 if( strstr( psz_file, "alsa_plugin" ) )
1441 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1442 if( handle == NULL )
1444 msg_Warn( p_this, "cannot load module `%s' (%s)",
1445 psz_file, dlerror() );
1451 handle = dlopen( psz_file, RTLD_NOW );
1452 if( handle == NULL )
1454 msg_Warn( p_this, "cannot load module `%s' (%s)",
1455 psz_file, dlerror() );
1459 #elif defined(HAVE_DL_DLOPEN)
1460 # if defined(DL_LAZY)
1461 handle = dlopen( psz_file, DL_LAZY );
1463 handle = dlopen( psz_file, 0 );
1465 if( handle == NULL )
1467 msg_Warn( p_this, "cannot load module `%s' (%s)",
1468 psz_file, dlerror() );
1472 #elif defined(HAVE_DL_SHL_LOAD)
1473 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1474 if( handle == NULL )
1476 msg_Warn( p_this, "cannot load module `%s' (%s)",
1477 psz_file, strerror(errno) );
1482 # error "Something is wrong in modules.c"
1490 /*****************************************************************************
1491 * CloseModule: unload a dynamic library
1492 *****************************************************************************
1493 * This function unloads a previously opened dynamically linked library
1494 * using a system dependant method. No return value is taken in consideration,
1495 * since some libraries sometimes refuse to close properly.
1496 *****************************************************************************/
1497 static void CloseModule( module_handle_t handle )
1499 #if defined(HAVE_DL_DYLD)
1500 NSUnLinkModule( handle, FALSE );
1502 #elif defined(HAVE_DL_BEOS)
1503 unload_add_on( handle );
1505 #elif defined(HAVE_DL_WINDOWS)
1506 FreeLibrary( handle );
1508 #elif defined(HAVE_DL_DLOPEN)
1511 #elif defined(HAVE_DL_SHL_LOAD)
1512 shl_unload( handle );
1518 /*****************************************************************************
1519 * GetSymbol: get a symbol from a dynamic library
1520 *****************************************************************************
1521 * This function queries a loaded library for a symbol specified in a
1522 * string, and returns a pointer to it. We don't check for dlerror() or
1523 * similar functions, since we want a non-NULL symbol anyway.
1524 *****************************************************************************/
1525 static void * _module_getsymbol( module_handle_t, const char * );
1527 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1529 void * p_symbol = _module_getsymbol( handle, psz_function );
1531 /* MacOS X dl library expects symbols to begin with "_". So do
1532 * some other operating systems. That's really lame, but hey, what
1534 if( p_symbol == NULL )
1536 char *psz_call = malloc( strlen( psz_function ) + 2 );
1538 strcpy( psz_call + 1, psz_function );
1539 psz_call[ 0 ] = '_';
1540 p_symbol = _module_getsymbol( handle, psz_call );
1547 static void * _module_getsymbol( module_handle_t handle,
1548 const char * psz_function )
1550 #if defined(HAVE_DL_DYLD)
1551 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1552 return NSAddressOfSymbol( sym );
1554 #elif defined(HAVE_DL_BEOS)
1556 if( B_OK == get_image_symbol( handle, psz_function,
1557 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1566 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1567 wchar_t psz_real[256];
1568 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1570 return (void *)GetProcAddress( handle, psz_real );
1572 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1573 return (void *)GetProcAddress( handle, (char *)psz_function );
1575 #elif defined(HAVE_DL_DLOPEN)
1576 return dlsym( handle, psz_function );
1578 #elif defined(HAVE_DL_SHL_LOAD)
1580 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1586 #if defined(HAVE_DL_WINDOWS)
1587 static char * GetWindowsError( void )
1589 #if defined(UNDER_CE)
1590 wchar_t psz_tmp[MAX_PATH];
1591 char * psz_buffer = malloc( MAX_PATH );
1593 char * psz_tmp = malloc( MAX_PATH );
1595 int i = 0, i_error = GetLastError();
1597 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1598 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1599 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1601 /* Go to the end of the string */
1602 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1609 #if defined(UNDER_CE)
1610 swprintf( psz_tmp + i, L" (error %i)", i_error );
1611 psz_tmp[ 255 ] = L'\0';
1613 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1614 psz_tmp[ 255 ] = '\0';
1618 #if defined(UNDER_CE)
1619 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1625 #endif /* HAVE_DL_WINDOWS */
1627 /*****************************************************************************
1628 * LoadPluginsCache: loads the plugins cache file
1629 *****************************************************************************
1630 * This function will load the plugin cache if present and valid. This cache
1631 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1632 * actually load the dynamically loadable module.
1633 * This allows us to only fully load plugins when they are actually used.
1634 *****************************************************************************/
1635 static void CacheLoad( vlc_object_t *p_this )
1637 char *psz_filename, *psz_homedir;
1639 int i, j, i_size, i_read;
1640 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1641 char p_cachelang[6], p_lang[6];
1643 module_cache_t **pp_cache = 0;
1644 int32_t i_file_size, i_marker;
1646 psz_homedir = p_this->p_libvlc->psz_homedir;
1649 msg_Err( p_this, "psz_homedir is null" );
1653 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1654 PLUGINSCACHE_DIR, CacheName() );
1657 msg_Err( p_this, "out of memory" );
1661 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1663 #if !defined( UNDER_CE )
1664 unlink( psz_filename );
1666 wchar_t psz_wf[MAX_PATH];
1667 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1668 DeleteFile( psz_wf );
1670 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1671 free( psz_filename );
1675 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1677 file = utf8_fopen( psz_filename, "rb" );
1680 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1682 free( psz_filename );
1685 free( psz_filename );
1687 /* Check the file size */
1688 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1689 if( i_read != sizeof(i_file_size) )
1691 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1697 fseek( file, 0, SEEK_END );
1698 if( ftell( file ) != i_file_size )
1700 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1701 "(corrupted size)" );
1705 fseek( file, sizeof(i_file_size), SEEK_SET );
1707 /* Check the file is a plugins cache */
1708 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1709 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1710 if( i_read != i_size ||
1711 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1713 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1718 /* Check Sub-version number */
1719 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1720 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1722 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1723 "(corrupted header)" );
1728 /* Check the language hasn't changed */
1729 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1730 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1731 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1733 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1734 "(language changed)" );
1739 /* Check header marker */
1740 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1741 if( i_read != sizeof(i_marker) ||
1742 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1744 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1745 "(corrupted header)" );
1750 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1751 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1753 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1754 malloc( i_cache * sizeof(void *) );
1756 #define LOAD_IMMEDIATE(a) \
1757 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1758 #define LOAD_STRING(a) \
1761 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1762 || ( i_size > 16384 ) ) \
1765 char *psz = malloc( i_size ); \
1766 if( fread( psz, i_size, 1, file ) != 1 ) { \
1770 if( psz[i_size-1] ) { \
1778 for( i = 0; i < i_cache; i++ )
1783 pp_cache[i] = malloc( sizeof(module_cache_t) );
1784 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1786 /* Load common info */
1787 LOAD_STRING( pp_cache[i]->psz_file );
1788 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1789 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1790 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1791 pp_cache[i]->b_used = VLC_FALSE;
1793 if( pp_cache[i]->b_junk ) continue;
1795 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1797 /* Load additional infos */
1798 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1799 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1800 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1801 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1802 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1804 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1806 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1807 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1808 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1809 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1810 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1811 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1814 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1817 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1819 LOAD_IMMEDIATE( i_submodules );
1821 while( i_submodules-- )
1823 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1824 vlc_object_attach( p_module, pp_cache[i]->p_module );
1825 p_module->b_submodule = VLC_TRUE;
1827 LOAD_STRING( p_module->psz_object_name );
1828 LOAD_STRING( p_module->psz_shortname );
1829 LOAD_STRING( p_module->psz_longname );
1830 LOAD_STRING( p_module->psz_program );
1831 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1833 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1835 LOAD_STRING( p_module->psz_capability );
1836 LOAD_IMMEDIATE( p_module->i_score );
1837 LOAD_IMMEDIATE( p_module->i_cpu );
1838 LOAD_IMMEDIATE( p_module->b_unloadable );
1839 LOAD_IMMEDIATE( p_module->b_reentrant );
1848 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1851 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1858 int CacheLoadConfig( module_t *p_module, FILE *file )
1863 /* Calculate the structure length */
1864 LOAD_IMMEDIATE( p_module->i_config_items );
1865 LOAD_IMMEDIATE( p_module->i_bool_items );
1867 LOAD_IMMEDIATE( i_lines );
1869 /* Allocate memory */
1872 p_module->p_config =
1873 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1874 if( p_module->p_config == NULL )
1876 p_module->confsize = 0;
1877 msg_Err( p_module, "config error: can't duplicate p_config" );
1881 p_module->confsize = i_lines;
1883 /* Do the duplication job */
1884 for (size_t i = 0; i < i_lines; i++ )
1886 LOAD_IMMEDIATE( p_module->p_config[i] );
1888 LOAD_STRING( p_module->p_config[i].psz_type );
1889 LOAD_STRING( p_module->p_config[i].psz_name );
1890 LOAD_STRING( p_module->p_config[i].psz_text );
1891 LOAD_STRING( p_module->p_config[i].psz_longtext );
1892 LOAD_STRING( p_module->p_config[i].psz_current );
1894 if (IsConfigStringType (p_module->p_config[i].i_type))
1896 LOAD_STRING (p_module->p_config[i].orig.psz);
1897 p_module->p_config[i].value.psz =
1898 (p_module->p_config[i].orig.psz != NULL)
1899 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1900 p_module->p_config[i].saved.psz = NULL;
1904 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1905 sizeof (p_module->p_config[i].value));
1906 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1907 sizeof (p_module->p_config[i].saved));
1910 p_module->p_config[i].b_dirty = VLC_FALSE;
1912 p_module->p_config[i].p_lock = &p_module->object_lock;
1914 if( p_module->p_config[i].i_list )
1916 if( p_module->p_config[i].ppsz_list )
1919 p_module->p_config[i].ppsz_list =
1920 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1921 if( p_module->p_config[i].ppsz_list )
1923 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1924 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1925 p_module->p_config[i].ppsz_list[j] = NULL;
1928 if( p_module->p_config[i].ppsz_list_text )
1931 p_module->p_config[i].ppsz_list_text =
1932 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1933 if( p_module->p_config[i].ppsz_list_text )
1935 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1936 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1937 p_module->p_config[i].ppsz_list_text[j] = NULL;
1940 if( p_module->p_config[i].pi_list )
1942 p_module->p_config[i].pi_list =
1943 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1944 if( p_module->p_config[i].pi_list )
1946 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1947 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1952 if( p_module->p_config[i].i_action )
1954 p_module->p_config[i].ppf_action =
1955 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1956 p_module->p_config[i].ppsz_action_text =
1957 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1959 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1961 p_module->p_config[i].ppf_action[j] = 0;
1962 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1966 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1973 return VLC_EGENERIC;
1976 /*****************************************************************************
1977 * SavePluginsCache: saves the plugins cache to a file
1978 *****************************************************************************/
1979 static void CacheSave( vlc_object_t *p_this )
1981 static char const psz_tag[] =
1982 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1983 "# This file is a cache directory tag created by VLC.\r\n"
1984 "# For information about cache directory tags, see:\r\n"
1985 "# http://www.brynosaurus.com/cachedir/\r\n";
1987 char *psz_filename, *psz_homedir;
1990 module_cache_t **pp_cache;
1991 int32_t i_file_size = 0;
1993 psz_homedir = p_this->p_libvlc->psz_homedir;
1996 msg_Err( p_this, "psz_homedir is null" );
2000 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
2001 strlen(psz_homedir) + strlen(CacheName()) );
2005 msg_Err( p_this, "out of memory" );
2009 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
2011 config_CreateDir( p_this, psz_filename );
2013 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
2015 config_CreateDir( p_this, psz_filename );
2017 strcat( psz_filename, "/CACHEDIR.TAG" );
2019 file = utf8_fopen( psz_filename, "wb" );
2022 fwrite( psz_tag, 1, strlen(psz_tag), file );
2026 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
2027 PLUGINSCACHE_DIR, CacheName() );
2029 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2031 file = utf8_fopen( psz_filename, "wb" );
2034 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2036 free( psz_filename );
2039 free( psz_filename );
2041 /* Empty space for file size */
2042 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2044 /* Contains version number */
2045 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2047 /* Sub-version number (to avoid breakage in the dev version when cache
2048 * structure changes) */
2049 i_file_size = CACHE_SUBVERSION_NUM;
2050 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2053 fprintf( file, "%5.5s", _("C") );
2056 i_file_size = ftell( file );
2057 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2059 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2060 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2062 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2064 #define SAVE_IMMEDIATE(a) \
2065 fwrite( &a, sizeof(char), sizeof(a), file )
2066 #define SAVE_STRING(a) \
2067 { i_size = a ? strlen( a ) + 1 : 0; \
2068 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2069 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2072 for( i = 0; i < i_cache; i++ )
2075 uint32_t i_submodule;
2077 /* Save common info */
2078 SAVE_STRING( pp_cache[i]->psz_file );
2079 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2080 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2081 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2083 if( pp_cache[i]->b_junk ) continue;
2085 /* Save additional infos */
2086 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2087 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2088 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2089 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2090 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2092 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2094 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2095 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2096 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2097 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2098 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2099 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2102 CacheSaveConfig( pp_cache[i]->p_module, file );
2104 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2106 i_submodule = pp_cache[i]->p_module->i_children;
2107 SAVE_IMMEDIATE( i_submodule );
2108 for( i_submodule = 0;
2109 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2112 module_t *p_module =
2113 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2115 SAVE_STRING( p_module->psz_object_name );
2116 SAVE_STRING( p_module->psz_shortname );
2117 SAVE_STRING( p_module->psz_longname );
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 )
2211 p_cache->pf_activate = p_module->pf_activate;
2212 p_cache->pf_deactivate = p_module->pf_deactivate;
2213 #ifndef HAVE_SHARED_LIBVLC
2214 p_cache->p_symbols = p_module->p_symbols;
2216 p_cache->handle = p_module->handle;
2218 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2220 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2221 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2222 p_cchild->pf_activate = p_child->pf_activate;
2223 p_cchild->pf_deactivate = p_child->pf_deactivate;
2224 #ifndef HAVE_SHARED_LIBVLC
2225 p_cchild->p_symbols = p_child->p_symbols;
2229 p_cache->b_loaded = VLC_TRUE;
2230 p_module->b_loaded = VLC_FALSE;
2233 /*****************************************************************************
2234 * CacheFind: finds the cache entry corresponding to a file
2235 *****************************************************************************/
2236 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2237 int64_t i_time, int64_t i_size )
2239 module_cache_t **pp_cache;
2242 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2243 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2245 for( i = 0; i < i_cache; i++ )
2247 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2248 pp_cache[i]->i_time == i_time &&
2249 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2255 #endif /* HAVE_DYNAMIC_PLUGINS */