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() */
39 #include <vlc/input.h>
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
55 #if !defined(HAVE_DYNAMIC_PLUGINS)
56 /* no support for plugins */
57 #elif defined(HAVE_DL_DYLD)
58 # if defined(HAVE_MACH_O_DYLD_H)
59 # include <mach-o/dyld.h>
61 #elif defined(HAVE_DL_BEOS)
62 # if defined(HAVE_IMAGE_H)
65 #elif defined(HAVE_DL_WINDOWS)
67 #elif defined(HAVE_DL_DLOPEN)
68 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
71 # if defined(HAVE_SYS_DL_H)
74 #elif defined(HAVE_DL_SHL_LOAD)
75 # if defined(HAVE_DL_H)
80 #include "vlc_error.h"
82 #include "vlc_interface.h"
83 #include "vlc_interaction.h"
84 #include "intf_eject.h"
86 #include "vlc_playlist.h"
88 #include "vlc_video.h"
89 #include "video_output.h"
90 #include "vout_synchro.h"
93 #include "audio_output.h"
94 #include "aout_internal.h"
96 #include "stream_output.h"
97 #include "vlc_httpd.h"
104 #include "iso_lang.h"
107 #include "vlc_block.h"
111 #include "vlc_image.h"
114 #include "vlc_update.h"
115 #include "vlc_strings.h"
116 #include "vlc_streaming.h"
118 #if defined( _MSC_VER ) && defined( UNDER_CE )
119 # include "modules_builtin_evc.h"
120 #elif defined( _MSC_VER )
121 # include "modules_builtin_msvc.h"
123 # include "modules_builtin.h"
127 #if defined( WIN32 ) || defined( UNDER_CE )
128 /* Avoid name collisions */
129 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
132 /*****************************************************************************
134 *****************************************************************************/
135 #ifdef HAVE_DYNAMIC_PLUGINS
136 static void AllocateAllPlugins ( vlc_object_t * );
137 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
138 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
139 static module_t * AllocatePlugin( vlc_object_t *, char * );
141 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
142 static int DeleteModule ( module_t * );
143 #ifdef HAVE_DYNAMIC_PLUGINS
144 static void DupModule ( module_t * );
145 static void UndupModule ( module_t * );
146 static int CallEntry ( module_t * );
147 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
148 static void CloseModule ( module_handle_t );
149 static void * GetSymbol ( module_handle_t, const char * );
150 static void CacheLoad ( vlc_object_t * );
151 static int CacheLoadConfig ( module_t *, FILE * );
152 static void CacheSave ( vlc_object_t * );
153 static void CacheSaveConfig ( module_t *, FILE * );
154 static char * CacheName ( void );
155 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
156 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
158 #if defined(HAVE_DL_WINDOWS)
159 static char * GetWindowsError ( void );
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;
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) );
193 p_bank->psz_object_name = "module bank";
195 p_bank->i_cache = p_bank->i_loaded_cache = 0;
196 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
197 p_bank->b_cache = p_bank->b_cache_dirty =
198 p_bank->b_cache_delete = VLC_FALSE;
201 * Store the symbols to be exported
203 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
204 STORE_SYMBOLS( &p_bank->symbols );
207 /* Everything worked, attach the object */
208 p_this->p_libvlc_global->p_module_bank = p_bank;
209 vlc_object_attach( p_bank, p_this->p_libvlc_global );
211 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 )
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 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
265 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
267 if( p_bank->pp_loaded_cache )
268 free( p_bank->pp_loaded_cache );
270 while( p_bank->i_cache-- )
272 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
273 free( p_bank->pp_cache[p_bank->i_cache] );
275 if( p_bank->pp_cache )
276 free( p_bank->pp_cache );
280 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
282 while( p_this->p_libvlc_global->p_module_bank->i_children )
284 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
286 if( DeleteModule( p_next ) )
288 /* Module deletion failed */
289 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
290 p_next->psz_object_name );
292 /* We just free the module by hand. Niahahahahaha. */
293 vlc_object_detach( p_next );
294 vlc_object_destroy( p_next );
298 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
299 p_this->p_libvlc_global->p_module_bank = NULL;
304 /*****************************************************************************
305 * module_LoadMain: load the main program info into the module bank.
306 *****************************************************************************
307 * This function fills the module bank structure with the main module infos.
308 * This is very useful as it will allow us to consider the main program just
309 * as another module, and for instance the configuration options of main will
310 * be available in the module bank structure just as for every other module.
311 *****************************************************************************/
312 void __module_LoadMain( vlc_object_t *p_this )
316 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
317 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
318 vlc_mutex_lock( lockval.p_address );
319 if( p_this->p_libvlc_global->p_module_bank->b_main )
321 vlc_mutex_unlock( lockval.p_address );
322 var_Destroy( p_this->p_libvlc_global, "libvlc" );
325 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
326 vlc_mutex_unlock( lockval.p_address );
327 var_Destroy( p_this->p_libvlc_global, "libvlc" );
329 AllocateBuiltinModule( p_this, vlc_entry__main );
332 /*****************************************************************************
333 * module_LoadBuiltins: load all modules which we built with.
334 *****************************************************************************
335 * This function fills the module bank structure with the builtin modules.
336 *****************************************************************************/
337 void __module_LoadBuiltins( vlc_object_t * p_this )
341 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
342 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
343 vlc_mutex_lock( lockval.p_address );
344 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
346 vlc_mutex_unlock( lockval.p_address );
347 var_Destroy( p_this->p_libvlc_global, "libvlc" );
350 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
351 vlc_mutex_unlock( lockval.p_address );
352 var_Destroy( p_this->p_libvlc_global, "libvlc" );
354 msg_Dbg( p_this, "checking builtin modules" );
355 ALLOCATE_ALL_BUILTINS();
358 /*****************************************************************************
359 * module_LoadPlugins: load all plugin modules we can find.
360 *****************************************************************************
361 * This function fills the module bank structure with the plugin modules.
362 *****************************************************************************/
363 void __module_LoadPlugins( vlc_object_t * p_this )
365 #ifdef HAVE_DYNAMIC_PLUGINS
368 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
369 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
370 vlc_mutex_lock( lockval.p_address );
371 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
373 vlc_mutex_unlock( lockval.p_address );
374 var_Destroy( p_this->p_libvlc_global, "libvlc" );
377 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
378 vlc_mutex_unlock( lockval.p_address );
379 var_Destroy( p_this->p_libvlc_global, "libvlc" );
381 msg_Dbg( p_this, "checking plugin modules" );
383 if( config_GetInt( p_this, "plugins-cache" ) )
384 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
386 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
387 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
389 AllocateAllPlugins( p_this );
393 /*****************************************************************************
394 * module_Need: return the best module function, given a capability list.
395 *****************************************************************************
396 * This function returns the module that best fits the asked capabilities.
397 *****************************************************************************/
398 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
399 const char *psz_name, vlc_bool_t b_strict )
401 typedef struct module_list_t module_list_t;
408 module_list_t *p_next;
411 module_list_t *p_list, *p_first, *p_tmp;
414 int i_which_module, i_index = 0;
415 vlc_bool_t b_intf = VLC_FALSE;
420 char *psz_shortcuts = NULL, *psz_var = NULL;
421 vlc_bool_t b_force_backup = p_this->b_force;
424 /* Deal with variables */
425 if( psz_name && psz_name[0] == '$' )
428 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
429 var_Get( p_this, psz_name + 1, &val );
430 psz_var = val.psz_string;
434 /* Count how many different shortcuts were asked for */
435 if( psz_name && *psz_name )
437 char *psz_parser, *psz_last_shortcut;
439 /* If the user wants none, give him none. */
440 if( !strcmp( psz_name, "none" ) )
442 if( psz_var ) free( psz_var );
447 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
449 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
451 if( *psz_parser == ',' )
455 psz_last_shortcut = psz_parser + 1;
459 /* Check if the user wants to override the "strict" mode */
460 if( psz_last_shortcut )
462 if( !strcmp(psz_last_shortcut, "none") )
467 else if( !strcmp(psz_last_shortcut, "any") )
469 b_strict = VLC_FALSE;
475 /* Sort the modules and test them */
476 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
477 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
480 /* Parse the module list for capabilities and probe each of them */
481 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
483 int i_shortcut_bonus = 0;
485 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
487 /* Test that this module can do what we need */
488 if( strcmp( p_module->psz_capability, psz_capability ) )
490 /* Don't recurse through the sub-modules because vlc_list_find()
491 * will list them anyway. */
495 /* Test if we have the required CPU */
496 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
501 /* If we required a shortcut, check this plugin provides it. */
502 if( i_shortcuts > 0 )
505 int i_dummy, i_short = i_shortcuts;
506 char *psz_name = psz_shortcuts;
508 /* Let's drop modules with a <= 0 score (unless they are
509 * explicitly requested) */
510 b_trash = p_module->i_score <= 0;
514 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
516 if( !strcasecmp( psz_name,
517 p_module->pp_shortcuts[i_dummy] ) )
521 i_shortcut_bonus = i_short * 10000;
526 if( i_shortcut_bonus )
528 /* We found it... remember ? */
532 /* Go to the next shortcut... This is so lame! */
541 /* If we are in "strict" mode and we couldn't
542 * find the module in the list of provided shortcuts,
543 * then kick the bastard out of here!!! */
544 if( i_short == 0 && b_strict )
554 /* If we didn't require a shortcut, trash <= 0 scored plugins */
555 else if( p_module->i_score <= 0 )
560 /* Special case: test if we requested a particular intf plugin */
561 if( !i_shortcuts && p_module->psz_program
562 && !strcmp( psz_capability, "interface" )
563 && !strcmp( p_module->psz_program,
564 p_this->p_libvlc->psz_object_name ) )
568 /* Remove previous non-matching plugins */
575 /* This one doesn't match */
579 /* Store this new module */
580 p_list[ i_index ].p_module = p_module;
581 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
582 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
584 /* Add it to the modules-to-probe list */
587 p_list[ 0 ].p_next = NULL;
592 /* Ok, so at school you learned that quicksort is quick, and
593 * bubble sort sucks raw eggs. But that's when dealing with
594 * thousands of items. Here we have barely 50. */
595 module_list_t *p_newlist = p_first;
597 if( p_first->i_score < p_list[ i_index ].i_score )
599 p_list[ i_index ].p_next = p_first;
600 p_first = &p_list[ i_index ];
604 while( p_newlist->p_next != NULL &&
605 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
607 p_newlist = p_newlist->p_next;
610 p_list[ i_index ].p_next = p_newlist->p_next;
611 p_newlist->p_next = &p_list[ i_index ];
618 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
619 i_index, i_index == 1 ? "" : "s" );
621 /* Lock all candidate modules */
623 while( p_tmp != NULL )
625 vlc_object_yield( p_tmp->p_module );
626 p_tmp = p_tmp->p_next;
629 /* We can release the list, interesting modules were yielded */
630 vlc_list_release( p_all );
632 /* Parse the linked list and use the first successful module */
634 while( p_tmp != NULL )
636 #ifdef HAVE_DYNAMIC_PLUGINS
637 /* Make sure the module is loaded in mem */
638 module_t *p_module = p_tmp->p_module->b_submodule ?
639 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
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 );
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" );
703 else if( psz_name != NULL && *psz_name )
705 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
706 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
711 free( psz_shortcuts );
719 /* Don't forget that the module is still locked */
723 /*****************************************************************************
724 * module_Unneed: decrease the usage count of a module.
725 *****************************************************************************
726 * This function must be called by the thread that called module_Need, to
727 * decrease the reference count and allow for hiding of modules.
728 *****************************************************************************/
729 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
731 /* Use the close method */
732 if( p_module->pf_deactivate )
734 p_module->pf_deactivate( p_this );
737 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
739 vlc_object_release( p_module );
744 /*****************************************************************************
745 * Following functions are local.
746 *****************************************************************************/
748 /*****************************************************************************
749 * AllocateAllPlugins: load all plugin modules we can find.
750 *****************************************************************************/
751 #ifdef HAVE_DYNAMIC_PLUGINS
752 static void AllocateAllPlugins( vlc_object_t *p_this )
754 /* Yes, there are two NULLs because we replace one with "plugin-path". */
755 #if defined( WIN32 ) || defined( UNDER_CE )
756 const char *path[] = { "modules", "", "plugins", NULL, NULL };
758 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
761 const char *const *ppsz_path;
763 /* If the user provided a plugin path, we add it to the list */
764 char *userpath = config_GetPsz( p_this, "plugin-path" );
765 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
767 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
771 if (!**ppsz_path) continue;
773 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
775 /* Handle relative as well as absolute paths */
777 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
778 (*ppsz_path)[1] != ':' )
780 if( (*ppsz_path)[0] != '/' )
783 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
784 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
789 psz_fullpath = strdup( *ppsz_path );
791 if( psz_fullpath == NULL )
794 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
796 /* Don't go deeper than 5 subdirectories */
797 AllocatePluginDir( p_this, psz_fullpath, 5 );
799 free( psz_fullpath );
802 /* Free plugin-path */
803 if( userpath != NULL )
807 /*****************************************************************************
808 * AllocatePluginDir: recursively parse a directory to look for plugins
809 *****************************************************************************/
810 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
813 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
819 #if defined( UNDER_CE ) || defined( _MSC_VER )
821 wchar_t psz_wpath[MAX_PATH + 256];
822 wchar_t psz_wdir[MAX_PATH];
824 char psz_path[MAX_PATH + 256];
825 WIN32_FIND_DATA finddata;
831 struct dirent * file;
835 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
840 #if defined( UNDER_CE ) || defined( _MSC_VER )
842 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
844 rc = GetFileAttributes( psz_wdir );
845 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
847 /* Parse all files in the directory */
848 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
850 rc = GetFileAttributes( psz_dir );
851 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
854 /* Parse all files in the directory */
855 sprintf( psz_path, "%s\\*", psz_dir );
858 handle = FindFirstFile( psz_wpath, &finddata );
860 handle = FindFirstFile( psz_path, &finddata );
862 if( handle == INVALID_HANDLE_VALUE )
864 /* Empty directory */
868 /* Parse the directory and try to load all files it contains. */
872 unsigned int i_len = wcslen( finddata.cFileName );
873 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
874 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
876 unsigned int i_len = strlen( finddata.cFileName );
877 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
881 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
882 || !strcmp( finddata.cFileName, ".." ) )
884 if( !FindNextFile( handle, &finddata ) ) break;
889 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
891 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
894 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
896 else if( i_len > strlen( LIBEXT )
897 /* We only load files ending with LIBEXT */
898 && !strncasecmp( psz_path + strlen( psz_path)
900 LIBEXT, strlen( LIBEXT ) ) )
902 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
903 int64_t i_time = 0, i_size = 0;
906 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
909 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
913 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
915 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
916 i_size = attrbuf.nFileSizeHigh;
918 i_size |= attrbuf.nFileSizeLow;
922 AllocatePluginFile( p_this, psz_file, i_time, i_size );
925 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
927 /* Close the directory */
931 dir = opendir( psz_dir );
937 i_dirlen = strlen( psz_dir );
939 /* Parse the directory and try to load all files it contains. */
940 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
947 if( !*file->d_name || !strcmp( file->d_name, "." )
948 || !strcmp( file->d_name, ".." ) )
953 i_len = strlen( file->d_name );
954 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
956 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
958 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
961 i_stat = stat( psz_file, &statbuf );
962 if( !i_stat && statbuf.st_mode & S_IFDIR )
964 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
966 else if( i_len > strlen( LIBEXT )
967 /* We only load files ending with LIBEXT */
968 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
969 LIBEXT, strlen( LIBEXT ) ) )
971 int64_t i_time = 0, i_size = 0;
975 i_time = statbuf.st_mtime;
976 i_size = statbuf.st_size;
979 AllocatePluginFile( p_this, psz_file, i_time, i_size );
985 /* Close the directory */
991 /*****************************************************************************
992 * AllocatePluginFile: load a module into memory and initialize it.
993 *****************************************************************************
994 * This function loads a dynamically loadable module and allocates a structure
995 * for its information data. The module can then be handled by module_Need
996 * and module_Unneed. It can be removed by DeleteModule.
997 *****************************************************************************/
998 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
999 int64_t i_file_time, int64_t i_file_size )
1001 module_t * p_module;
1002 module_cache_t *p_cache_entry = NULL;
1005 * Check our plugins cache first then load plugin if needed
1008 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1010 if( !p_cache_entry )
1012 p_module = AllocatePlugin( p_this, psz_file );
1016 /* If junk dll, don't try to load it */
1017 if( p_cache_entry->b_junk )
1023 module_config_t *p_item, *p_end;
1025 p_module = p_cache_entry->p_module;
1026 p_module->b_loaded = VLC_FALSE;
1028 /* For now we force loading if the module's config contains
1029 * callbacks or actions.
1030 * Could be optimized by adding an API call.*/
1031 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1032 p_item < p_end; p_item++ )
1034 if( p_item->pf_callback || p_item->i_action )
1035 p_module = AllocatePlugin( p_this, psz_file );
1042 /* Everything worked fine !
1043 * The module is ready to be added to the list. */
1044 p_module->b_builtin = VLC_FALSE;
1046 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1047 p_module->psz_object_name, p_module->psz_longname ); */
1049 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1052 if( !p_this->p_libvlc_global->p_module_bank->b_cache ) return 0;
1054 /* Add entry to cache */
1055 #define p_bank p_this->p_libvlc_global->p_module_bank
1057 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1058 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1059 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1060 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1061 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1062 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1063 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1066 return p_module ? 0 : -1;
1069 /*****************************************************************************
1070 * AllocatePlugin: load a module into memory and initialize it.
1071 *****************************************************************************
1072 * This function loads a dynamically loadable module and allocates a structure
1073 * for its information data. The module can then be handled by module_Need
1074 * and module_Unneed. It can be removed by DeleteModule.
1075 *****************************************************************************/
1076 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1078 module_t * p_module;
1079 module_handle_t handle;
1081 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1083 /* Now that we have successfully loaded the module, we can
1084 * allocate a structure for it */
1085 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1086 if( p_module == NULL )
1088 msg_Err( p_this, "out of memory" );
1089 CloseModule( handle );
1093 /* We need to fill these since they may be needed by CallEntry() */
1094 p_module->psz_filename = psz_file;
1095 p_module->handle = handle;
1096 #ifndef HAVE_SHARED_LIBVLC
1097 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1099 p_module->b_loaded = VLC_TRUE;
1101 /* Initialize the module: fill p_module, default config */
1102 if( CallEntry( p_module ) != 0 )
1104 /* We couldn't call module_init() */
1105 vlc_object_destroy( p_module );
1106 CloseModule( handle );
1110 DupModule( p_module );
1111 p_module->psz_filename = strdup( p_module->psz_filename );
1113 /* Everything worked fine ! The module is ready to be added to the list. */
1114 p_module->b_builtin = VLC_FALSE;
1119 /*****************************************************************************
1120 * DupModule: make a plugin module standalone.
1121 *****************************************************************************
1122 * This function duplicates all strings in the module, so that the dynamic
1123 * object can be unloaded. It acts recursively on submodules.
1124 *****************************************************************************/
1125 static void DupModule( module_t *p_module )
1127 const char **pp_shortcut;
1130 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1132 *pp_shortcut = strdup( *pp_shortcut );
1135 /* We strdup() these entries so that they are still valid when the
1136 * module is unloaded. */
1137 p_module->psz_object_name = strdup( p_module->psz_object_name );
1138 p_module->psz_capability = strdup( p_module->psz_capability );
1139 p_module->psz_shortname = p_module->psz_shortname ?
1140 strdup( p_module->psz_shortname ) : NULL;
1141 p_module->psz_longname = strdup( p_module->psz_longname );
1143 if( p_module->psz_program != NULL )
1145 p_module->psz_program = strdup( p_module->psz_program );
1148 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1150 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1154 /*****************************************************************************
1155 * UndupModule: free a duplicated module.
1156 *****************************************************************************
1157 * This function frees the allocations done in DupModule().
1158 *****************************************************************************/
1159 static void UndupModule( module_t *p_module )
1161 const char **pp_shortcut;
1164 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1166 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1169 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1171 free( (void *)*pp_shortcut );
1174 free( (void *)p_module->psz_object_name );
1175 free( (void *)p_module->psz_capability );
1176 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1177 free( (void *)p_module->psz_longname );
1179 if( p_module->psz_program != NULL )
1181 free( (void *)p_module->psz_program );
1185 #endif /* HAVE_DYNAMIC_PLUGINS */
1187 /*****************************************************************************
1188 * AllocateBuiltinModule: initialize a builtin module.
1189 *****************************************************************************
1190 * This function registers a builtin module and allocates a structure
1191 * for its information data. The module can then be handled by module_Need
1192 * and module_Unneed. It can be removed by DeleteModule.
1193 *****************************************************************************/
1194 static int AllocateBuiltinModule( vlc_object_t * p_this,
1195 int ( *pf_entry ) ( module_t * ) )
1197 module_t * p_module;
1199 /* Now that we have successfully loaded the module, we can
1200 * allocate a structure for it */
1201 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1202 if( p_module == NULL )
1204 msg_Err( p_this, "out of memory" );
1208 /* Initialize the module : fill p_module->psz_object_name, etc. */
1209 if( pf_entry( p_module ) != 0 )
1211 /* With a well-written module we shouldn't have to print an
1212 * additional error message here, but just make sure. */
1213 msg_Err( p_this, "failed calling entry point in builtin module" );
1214 vlc_object_destroy( p_module );
1218 /* Everything worked fine ! The module is ready to be added to the list. */
1219 p_module->b_builtin = VLC_TRUE;
1221 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1222 p_module->psz_object_name, p_module->psz_longname ); */
1224 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1229 /*****************************************************************************
1230 * DeleteModule: delete a module and its structure.
1231 *****************************************************************************
1232 * This function can only be called if the module isn't being used.
1233 *****************************************************************************/
1234 static int DeleteModule( module_t * p_module )
1236 vlc_object_detach( p_module );
1238 /* We free the structures that we strdup()ed in Allocate*Module(). */
1239 #ifdef HAVE_DYNAMIC_PLUGINS
1240 if( !p_module->b_builtin )
1242 if( p_module->b_loaded && p_module->b_unloadable )
1244 CloseModule( p_module->handle );
1246 UndupModule( p_module );
1247 free( p_module->psz_filename );
1251 /* Free and detach the object's children */
1252 while( p_module->i_children )
1254 vlc_object_t *p_this = p_module->pp_children[0];
1255 vlc_object_detach( p_this );
1256 vlc_object_destroy( p_this );
1259 config_Free( p_module );
1260 vlc_object_destroy( p_module );
1265 #ifdef HAVE_DYNAMIC_PLUGINS
1266 /*****************************************************************************
1267 * CallEntry: call an entry point.
1268 *****************************************************************************
1269 * This function calls a symbol given its name and a module structure. The
1270 * symbol MUST refer to a function returning int and taking a module_t* as
1272 *****************************************************************************/
1273 static int CallEntry( module_t * p_module )
1275 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1276 int (* pf_symbol) ( module_t * p_module );
1278 /* Try to resolve the symbol */
1279 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1281 if( pf_symbol == NULL )
1283 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1284 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1285 psz_name, p_module->psz_filename );
1286 #elif defined(HAVE_DL_WINDOWS)
1287 char *psz_error = GetWindowsError();
1288 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1289 psz_name, p_module->psz_filename, psz_error );
1291 #elif defined(HAVE_DL_DLOPEN)
1292 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1293 psz_name, p_module->psz_filename, dlerror() );
1294 #elif defined(HAVE_DL_SHL_LOAD)
1295 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1296 psz_name, p_module->psz_filename, strerror(errno) );
1298 # error "Something is wrong in modules.c"
1303 /* We can now try to call the symbol */
1304 if( pf_symbol( p_module ) != 0 )
1306 /* With a well-written module we shouldn't have to print an
1307 * additional error message here, but just make sure. */
1308 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1309 psz_name, p_module->psz_filename );
1313 /* Everything worked fine, we can return */
1317 /*****************************************************************************
1318 * LoadModule: loads a dynamic library
1319 *****************************************************************************
1320 * This function loads a dynamically linked library using a system dependant
1321 * method. Will return 0 on success as well as the module handle.
1322 *****************************************************************************/
1323 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1324 module_handle_t *p_handle )
1326 module_handle_t handle;
1328 #if defined(HAVE_DL_DYLD)
1329 NSObjectFileImage image;
1330 NSObjectFileImageReturnCode ret;
1332 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1334 if( ret != NSObjectFileImageSuccess )
1336 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1340 /* Open the dynamic module */
1341 handle = NSLinkModule( image, psz_file,
1342 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1346 NSLinkEditErrors errors;
1347 const char *psz_file, *psz_err;
1349 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1350 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1351 NSDestroyObjectFileImage( image );
1355 /* Destroy our image, we won't need it */
1356 NSDestroyObjectFileImage( image );
1358 #elif defined(HAVE_DL_BEOS)
1359 handle = load_add_on( psz_file );
1362 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1366 #elif defined(HAVE_DL_WINDOWS)
1369 wchar_t psz_wfile[MAX_PATH];
1370 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1371 handle = LoadLibrary( psz_wfile );
1374 handle = LoadLibrary( psz_file );
1376 if( handle == NULL )
1378 char *psz_err = GetWindowsError();
1379 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1384 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1385 /* static is OK, we are called atomically */
1387 # if defined(SYS_LINUX)
1388 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1389 * are going to get namespace collisions when two modules have common
1390 * public symbols, but ALSA is being a pest here. */
1391 if( strstr( psz_file, "alsa_plugin" ) )
1393 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1394 if( handle == NULL )
1396 msg_Warn( p_this, "cannot load module `%s' (%s)",
1397 psz_file, dlerror() );
1403 handle = dlopen( psz_file, RTLD_NOW );
1404 if( handle == NULL )
1406 msg_Warn( p_this, "cannot load module `%s' (%s)",
1407 psz_file, dlerror() );
1411 #elif defined(HAVE_DL_DLOPEN)
1412 # if defined(DL_LAZY)
1413 handle = dlopen( psz_file, DL_LAZY );
1415 handle = dlopen( psz_file, 0 );
1417 if( handle == NULL )
1419 msg_Warn( p_this, "cannot load module `%s' (%s)",
1420 psz_file, dlerror() );
1424 #elif defined(HAVE_DL_SHL_LOAD)
1425 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1426 if( handle == NULL )
1428 msg_Warn( p_this, "cannot load module `%s' (%s)",
1429 psz_file, strerror(errno) );
1434 # error "Something is wrong in modules.c"
1442 /*****************************************************************************
1443 * CloseModule: unload a dynamic library
1444 *****************************************************************************
1445 * This function unloads a previously opened dynamically linked library
1446 * using a system dependant method. No return value is taken in consideration,
1447 * since some libraries sometimes refuse to close properly.
1448 *****************************************************************************/
1449 static void CloseModule( module_handle_t handle )
1451 #if defined(HAVE_DL_DYLD)
1452 NSUnLinkModule( handle, FALSE );
1454 #elif defined(HAVE_DL_BEOS)
1455 unload_add_on( handle );
1457 #elif defined(HAVE_DL_WINDOWS)
1458 FreeLibrary( handle );
1460 #elif defined(HAVE_DL_DLOPEN)
1463 #elif defined(HAVE_DL_SHL_LOAD)
1464 shl_unload( handle );
1470 /*****************************************************************************
1471 * GetSymbol: get a symbol from a dynamic library
1472 *****************************************************************************
1473 * This function queries a loaded library for a symbol specified in a
1474 * string, and returns a pointer to it. We don't check for dlerror() or
1475 * similar functions, since we want a non-NULL symbol anyway.
1476 *****************************************************************************/
1477 static void * _module_getsymbol( module_handle_t, const char * );
1479 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1481 void * p_symbol = _module_getsymbol( handle, psz_function );
1483 /* MacOS X dl library expects symbols to begin with "_". So do
1484 * some other operating systems. That's really lame, but hey, what
1486 if( p_symbol == NULL )
1488 char *psz_call = malloc( strlen( psz_function ) + 2 );
1490 strcpy( psz_call + 1, psz_function );
1491 psz_call[ 0 ] = '_';
1492 p_symbol = _module_getsymbol( handle, psz_call );
1499 static void * _module_getsymbol( module_handle_t handle,
1500 const char * psz_function )
1502 #if defined(HAVE_DL_DYLD)
1503 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1504 return NSAddressOfSymbol( sym );
1506 #elif defined(HAVE_DL_BEOS)
1508 if( B_OK == get_image_symbol( handle, psz_function,
1509 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1518 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1519 wchar_t psz_real[256];
1520 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1522 return (void *)GetProcAddress( handle, psz_real );
1524 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1525 return (void *)GetProcAddress( handle, (char *)psz_function );
1527 #elif defined(HAVE_DL_DLOPEN)
1528 return dlsym( handle, psz_function );
1530 #elif defined(HAVE_DL_SHL_LOAD)
1532 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1538 #if defined(HAVE_DL_WINDOWS)
1539 static char * GetWindowsError( void )
1541 #if defined(UNDER_CE)
1542 wchar_t psz_tmp[MAX_PATH];
1543 char * psz_buffer = malloc( MAX_PATH );
1545 char * psz_tmp = malloc( MAX_PATH );
1547 int i = 0, i_error = GetLastError();
1549 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1550 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1551 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1553 /* Go to the end of the string */
1554 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1561 #if defined(UNDER_CE)
1562 swprintf( psz_tmp + i, L" (error %i)", i_error );
1563 psz_tmp[ 255 ] = L'\0';
1565 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1566 psz_tmp[ 255 ] = '\0';
1570 #if defined(UNDER_CE)
1571 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1577 #endif /* HAVE_DL_WINDOWS */
1579 /*****************************************************************************
1580 * LoadPluginsCache: loads the plugins cache file
1581 *****************************************************************************
1582 * This function will load the plugin cache if present and valid. This cache
1583 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1584 * actually load the dynamically loadable module.
1585 * This allows us to only fully load plugins when they are actually used.
1586 *****************************************************************************/
1587 static void CacheLoad( vlc_object_t *p_this )
1589 char *psz_filename, *psz_homedir;
1591 int i, j, i_size, i_read;
1592 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1593 char p_cachelang[6], p_lang[6];
1595 module_cache_t **pp_cache = 0;
1596 int32_t i_file_size, i_marker;
1598 psz_homedir = p_this->p_libvlc->psz_homedir;
1601 msg_Err( p_this, "psz_homedir is null" );
1605 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1606 PLUGINSCACHE_DIR, CacheName() );
1609 msg_Err( p_this, "out of memory" );
1613 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1615 #if !defined( UNDER_CE )
1616 unlink( psz_filename );
1618 wchar_t psz_wf[MAX_PATH];
1619 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1620 DeleteFile( psz_wf );
1622 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1623 free( psz_filename );
1627 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1629 file = utf8_fopen( psz_filename, "rb" );
1632 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1634 free( psz_filename );
1637 free( psz_filename );
1639 /* Check the file size */
1640 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1641 if( i_read != sizeof(i_file_size) )
1643 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1649 fseek( file, 0, SEEK_END );
1650 if( ftell( file ) != i_file_size )
1652 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1653 "(corrupted size)" );
1657 fseek( file, sizeof(i_file_size), SEEK_SET );
1659 /* Check the file is a plugins cache */
1660 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1661 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1662 if( i_read != i_size ||
1663 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1665 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1670 /* Check Sub-version number */
1671 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1672 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1674 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1675 "(corrupted header)" );
1680 /* Check the language hasn't changed */
1681 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1682 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1683 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1685 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1686 "(language changed)" );
1691 /* Check header marker */
1692 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1693 if( i_read != sizeof(i_marker) ||
1694 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1696 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1697 "(corrupted header)" );
1702 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1703 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1705 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1706 malloc( i_cache * sizeof(void *) );
1708 #define LOAD_IMMEDIATE(a) \
1709 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1710 #define LOAD_STRING(a) \
1713 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1714 || ( i_size > 16384 ) ) \
1717 char *psz = malloc( i_size ); \
1718 if( fread( psz, i_size, 1, file ) != 1 ) { \
1722 if( psz[i_size-1] ) { \
1730 for( i = 0; i < i_cache; i++ )
1735 pp_cache[i] = malloc( sizeof(module_cache_t) );
1736 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1738 /* Load common info */
1739 LOAD_STRING( pp_cache[i]->psz_file );
1740 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1741 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1742 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1744 if( pp_cache[i]->b_junk ) continue;
1746 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1748 /* Load additional infos */
1749 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1750 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1751 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1752 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1753 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1755 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1757 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1758 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1759 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1760 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1761 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1762 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1765 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1768 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1770 LOAD_IMMEDIATE( i_submodules );
1772 while( i_submodules-- )
1774 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1775 vlc_object_attach( p_module, pp_cache[i]->p_module );
1776 p_module->b_submodule = VLC_TRUE;
1778 LOAD_STRING( p_module->psz_object_name );
1779 LOAD_STRING( p_module->psz_shortname );
1780 LOAD_STRING( p_module->psz_longname );
1781 LOAD_STRING( p_module->psz_program );
1782 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1784 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1786 LOAD_STRING( p_module->psz_capability );
1787 LOAD_IMMEDIATE( p_module->i_score );
1788 LOAD_IMMEDIATE( p_module->i_cpu );
1789 LOAD_IMMEDIATE( p_module->b_unloadable );
1790 LOAD_IMMEDIATE( p_module->b_reentrant );
1799 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1802 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1809 int CacheLoadConfig( module_t *p_module, FILE *file )
1814 /* Calculate the structure length */
1815 LOAD_IMMEDIATE( p_module->i_config_items );
1816 LOAD_IMMEDIATE( p_module->i_bool_items );
1818 LOAD_IMMEDIATE( i_lines );
1820 /* Allocate memory */
1823 p_module->p_config =
1824 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1825 if( p_module->p_config == NULL )
1827 p_module->confsize = 0;
1828 msg_Err( p_module, "config error: can't duplicate p_config" );
1832 p_module->confsize = i_lines;
1834 /* Do the duplication job */
1835 for (size_t i = 0; i < i_lines; i++ )
1837 LOAD_IMMEDIATE( p_module->p_config[i] );
1839 LOAD_STRING( p_module->p_config[i].psz_type );
1840 LOAD_STRING( p_module->p_config[i].psz_name );
1841 LOAD_STRING( p_module->p_config[i].psz_text );
1842 LOAD_STRING( p_module->p_config[i].psz_longtext );
1843 LOAD_STRING( p_module->p_config[i].psz_current );
1845 if (IsConfigStringType (p_module->p_config[i].i_type))
1847 LOAD_STRING (p_module->p_config[i].orig.psz);
1848 p_module->p_config[i].value.psz =
1849 (p_module->p_config[i].orig.psz != NULL)
1850 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1851 p_module->p_config[i].saved.psz = NULL;
1855 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1856 sizeof (p_module->p_config[i].value));
1857 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1858 sizeof (p_module->p_config[i].saved));
1861 p_module->p_config[i].b_dirty = VLC_FALSE;
1863 p_module->p_config[i].p_lock = &p_module->object_lock;
1865 if( p_module->p_config[i].i_list )
1867 if( p_module->p_config[i].ppsz_list )
1870 p_module->p_config[i].ppsz_list =
1871 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1872 if( p_module->p_config[i].ppsz_list )
1874 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1875 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1876 p_module->p_config[i].ppsz_list[j] = NULL;
1879 if( p_module->p_config[i].ppsz_list_text )
1882 p_module->p_config[i].ppsz_list_text =
1883 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1884 if( p_module->p_config[i].ppsz_list_text )
1886 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1887 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1888 p_module->p_config[i].ppsz_list_text[j] = NULL;
1891 if( p_module->p_config[i].pi_list )
1893 p_module->p_config[i].pi_list =
1894 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1895 if( p_module->p_config[i].pi_list )
1897 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1898 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1903 if( p_module->p_config[i].i_action )
1905 p_module->p_config[i].ppf_action =
1906 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1907 p_module->p_config[i].ppsz_action_text =
1908 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1910 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1912 p_module->p_config[i].ppf_action[j] = 0;
1913 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1917 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1924 return VLC_EGENERIC;
1927 /*****************************************************************************
1928 * SavePluginsCache: saves the plugins cache to a file
1929 *****************************************************************************/
1930 static void CacheSave( vlc_object_t *p_this )
1932 static char const psz_tag[] =
1933 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1934 "# This file is a cache directory tag created by VLC.\r\n"
1935 "# For information about cache directory tags, see:\r\n"
1936 "# http://www.brynosaurus.com/cachedir/\r\n";
1938 char *psz_filename, *psz_homedir;
1941 module_cache_t **pp_cache;
1942 int32_t i_file_size = 0;
1944 psz_homedir = p_this->p_libvlc->psz_homedir;
1947 msg_Err( p_this, "psz_homedir is null" );
1951 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1952 strlen(psz_homedir) + strlen(CacheName()) );
1956 msg_Err( p_this, "out of memory" );
1960 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1962 config_CreateDir( p_this, psz_filename );
1964 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1966 config_CreateDir( p_this, psz_filename );
1968 strcat( psz_filename, "/CACHEDIR.TAG" );
1970 file = utf8_fopen( psz_filename, "wb" );
1973 fwrite( psz_tag, 1, strlen(psz_tag), file );
1977 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1978 PLUGINSCACHE_DIR, CacheName() );
1980 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1982 file = utf8_fopen( psz_filename, "wb" );
1985 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1987 free( psz_filename );
1990 free( psz_filename );
1992 /* Empty space for file size */
1993 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1995 /* Contains version number */
1996 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1998 /* Sub-version number (to avoid breakage in the dev version when cache
1999 * structure changes) */
2000 i_file_size = CACHE_SUBVERSION_NUM;
2001 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2004 fprintf( file, "%5.5s", _("C") );
2007 i_file_size = ftell( file );
2008 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2010 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2011 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2013 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2015 #define SAVE_IMMEDIATE(a) \
2016 fwrite( &a, sizeof(char), sizeof(a), file )
2017 #define SAVE_STRING(a) \
2018 { i_size = a ? strlen( a ) + 1 : 0; \
2019 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2020 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2023 for( i = 0; i < i_cache; i++ )
2026 uint32_t i_submodule;
2028 /* Save common info */
2029 SAVE_STRING( pp_cache[i]->psz_file );
2030 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2031 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2032 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2034 if( pp_cache[i]->b_junk ) continue;
2036 /* Save additional infos */
2037 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2038 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2039 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2040 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2041 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2043 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2045 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2046 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2047 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2048 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2049 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2050 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2053 CacheSaveConfig( pp_cache[i]->p_module, file );
2055 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2057 i_submodule = pp_cache[i]->p_module->i_children;
2058 SAVE_IMMEDIATE( i_submodule );
2059 for( i_submodule = 0;
2060 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2063 module_t *p_module =
2064 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2066 SAVE_STRING( p_module->psz_object_name );
2067 SAVE_STRING( p_module->psz_shortname );
2068 SAVE_STRING( p_module->psz_longname );
2069 SAVE_STRING( p_module->psz_program );
2070 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2072 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2074 SAVE_STRING( p_module->psz_capability );
2075 SAVE_IMMEDIATE( p_module->i_score );
2076 SAVE_IMMEDIATE( p_module->i_cpu );
2077 SAVE_IMMEDIATE( p_module->b_unloadable );
2078 SAVE_IMMEDIATE( p_module->b_reentrant );
2082 /* Fill-up file size */
2083 i_file_size = ftell( file );
2084 fseek( file, 0, SEEK_SET );
2085 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2092 void CacheSaveConfig( module_t *p_module, FILE *file )
2094 uint32_t i_lines = p_module->confsize;
2097 SAVE_IMMEDIATE( p_module->i_config_items );
2098 SAVE_IMMEDIATE( p_module->i_bool_items );
2099 SAVE_IMMEDIATE( i_lines );
2101 for (size_t i = 0; i < i_lines ; i++)
2103 SAVE_IMMEDIATE( p_module->p_config[i] );
2105 SAVE_STRING( p_module->p_config[i].psz_type );
2106 SAVE_STRING( p_module->p_config[i].psz_name );
2107 SAVE_STRING( p_module->p_config[i].psz_text );
2108 SAVE_STRING( p_module->p_config[i].psz_longtext );
2109 SAVE_STRING( p_module->p_config[i].psz_current );
2110 if (IsConfigStringType (p_module->p_config[i].i_type))
2111 SAVE_STRING( p_module->p_config[i].orig.psz );
2113 if( p_module->p_config[i].i_list )
2115 if( p_module->p_config[i].ppsz_list )
2117 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2118 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2121 if( p_module->p_config[i].ppsz_list_text )
2123 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2124 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2126 if( p_module->p_config[i].pi_list )
2128 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2129 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2133 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2134 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2136 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2140 /*****************************************************************************
2141 * CacheName: Return the cache file name for this platform.
2142 *****************************************************************************/
2143 static char *CacheName( void )
2145 static char psz_cachename[32];
2147 /* Code int size, pointer size and endianness in the filename */
2148 int32_t x = 0xbe00001e;
2149 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2150 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2151 return psz_cachename;
2154 /*****************************************************************************
2155 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2156 *****************************************************************************/
2157 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2158 module_t *p_module )
2162 p_cache->pf_activate = p_module->pf_activate;
2163 p_cache->pf_deactivate = p_module->pf_deactivate;
2164 #ifndef HAVE_SHARED_LIBVLC
2165 p_cache->p_symbols = p_module->p_symbols;
2167 p_cache->handle = p_module->handle;
2169 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2171 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2172 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2173 p_cchild->pf_activate = p_child->pf_activate;
2174 p_cchild->pf_deactivate = p_child->pf_deactivate;
2175 #ifndef HAVE_SHARED_LIBVLC
2176 p_cchild->p_symbols = p_child->p_symbols;
2180 p_cache->b_loaded = VLC_TRUE;
2181 p_module->b_loaded = VLC_FALSE;
2184 /*****************************************************************************
2185 * FindPluginCache: finds the cache entry corresponding to a file
2186 *****************************************************************************/
2187 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2188 int64_t i_time, int64_t i_size )
2190 module_cache_t **pp_cache;
2193 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2194 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2196 for( i = 0; i < i_cache; i++ )
2198 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2199 pp_cache[i]->i_time == i_time &&
2200 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2206 #endif /* HAVE_DYNAMIC_PLUGINS */