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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28 * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strdup() */
38 #include <vlc/input.h>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if !defined(HAVE_DYNAMIC_PLUGINS)
55 /* no support for plugins */
56 #elif defined(HAVE_DL_DYLD)
57 # if defined(HAVE_MACH_O_DYLD_H)
58 # include <mach-o/dyld.h>
60 #elif defined(HAVE_DL_BEOS)
61 # if defined(HAVE_IMAGE_H)
64 #elif defined(HAVE_DL_WINDOWS)
66 #elif defined(HAVE_DL_DLOPEN)
67 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
70 # if defined(HAVE_SYS_DL_H)
73 #elif defined(HAVE_DL_SHL_LOAD)
74 # if defined(HAVE_DL_H)
79 #include "vlc_error.h"
81 #include "vlc_interface.h"
82 #include "intf_eject.h"
84 #include "vlc_playlist.h"
86 #include "vlc_video.h"
87 #include "video_output.h"
88 #include "vout_synchro.h"
91 #include "audio_output.h"
92 #include "aout_internal.h"
94 #include "stream_output.h"
96 #include "vlc_httpd.h"
102 #include "iso_lang.h"
105 #include "vlc_block.h"
109 #include "vlc_image.h"
112 #if defined( _MSC_VER ) && defined( UNDER_CE )
113 # include "modules_builtin_evc.h"
114 #elif defined( _MSC_VER )
115 # include "modules_builtin_msvc.h"
117 # include "modules_builtin.h"
121 #if defined( WIN32 ) || defined( UNDER_CE )
122 /* Avoid name collisions */
123 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
126 /*****************************************************************************
128 *****************************************************************************/
129 #ifdef HAVE_DYNAMIC_PLUGINS
130 static void AllocateAllPlugins ( vlc_object_t * );
131 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
132 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
133 static module_t * AllocatePlugin( vlc_object_t *, char * );
135 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
136 static int DeleteModule ( module_t * );
137 #ifdef HAVE_DYNAMIC_PLUGINS
138 static void DupModule ( module_t * );
139 static void UndupModule ( module_t * );
140 static int CallEntry ( module_t * );
141 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
142 static void CloseModule ( module_handle_t );
143 static void * GetSymbol ( module_handle_t, const char * );
144 static void CacheLoad ( vlc_object_t * );
145 static int CacheLoadConfig ( module_t *, FILE * );
146 static void CacheSave ( vlc_object_t * );
147 static void CacheSaveConfig ( module_t *, FILE * );
148 static char * CacheName ( void );
149 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
150 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
152 #if defined(HAVE_DL_WINDOWS)
153 static char * GetWindowsError ( void );
158 /* Sub-version number
159 * (only used to avoid breakage in dev version when cache structure changes) */
160 #define CACHE_SUBVERSION_NUM 1
162 /*****************************************************************************
163 * module_InitBank: create the module bank.
164 *****************************************************************************
165 * This function creates a module bank structure which will be filled later
166 * on with all the modules found.
167 *****************************************************************************/
168 void __module_InitBank( vlc_object_t *p_this )
170 module_bank_t *p_bank;
173 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
174 var_Get( p_this->p_libvlc, "libvlc", &lockval );
175 vlc_mutex_lock( lockval.p_address );
176 if( p_this->p_libvlc->p_module_bank )
178 p_this->p_libvlc->p_module_bank->i_usage++;
179 vlc_mutex_unlock( lockval.p_address );
180 var_Destroy( p_this->p_libvlc, "libvlc" );
183 vlc_mutex_unlock( lockval.p_address );
184 var_Destroy( p_this->p_libvlc, "libvlc" );
186 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
187 p_bank->psz_object_name = "module bank";
189 p_bank->i_cache = p_bank->i_loaded_cache = 0;
190 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
191 p_bank->b_cache = p_bank->b_cache_dirty =
192 p_bank->b_cache_delete = VLC_FALSE;
195 * Store the symbols to be exported
197 #ifdef HAVE_DYNAMIC_PLUGINS
198 STORE_SYMBOLS( &p_bank->symbols );
201 /* Everything worked, attach the object */
202 p_this->p_libvlc->p_module_bank = p_bank;
203 vlc_object_attach( p_bank, p_this->p_libvlc );
205 module_LoadMain( p_this );
210 /*****************************************************************************
211 * module_ResetBank: reset the module bank.
212 *****************************************************************************
213 * This function resets the module bank by unloading all unused plugin
215 *****************************************************************************/
216 void __module_ResetBank( vlc_object_t *p_this )
218 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
222 /*****************************************************************************
223 * module_EndBank: empty the module bank.
224 *****************************************************************************
225 * This function unloads all unused plugin modules and empties the module
226 * bank in case of success.
227 *****************************************************************************/
228 void __module_EndBank( vlc_object_t *p_this )
233 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
234 var_Get( p_this->p_libvlc, "libvlc", &lockval );
235 vlc_mutex_lock( lockval.p_address );
236 if( !p_this->p_libvlc->p_module_bank )
238 vlc_mutex_unlock( lockval.p_address );
239 var_Destroy( p_this->p_libvlc, "libvlc" );
242 if( --p_this->p_libvlc->p_module_bank->i_usage )
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc, "libvlc" );
248 vlc_mutex_unlock( lockval.p_address );
249 var_Destroy( p_this->p_libvlc, "libvlc" );
251 config_AutoSaveConfigFile( p_this );
253 #ifdef HAVE_DYNAMIC_PLUGINS
254 #define p_bank p_this->p_libvlc->p_module_bank
255 if( p_bank->b_cache ) CacheSave( p_this );
256 while( p_bank->i_loaded_cache-- )
258 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
259 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
261 if( p_bank->pp_loaded_cache )
262 free( p_bank->pp_loaded_cache );
264 while( p_bank->i_cache-- )
266 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
267 free( p_bank->pp_cache[p_bank->i_cache] );
269 if( p_bank->pp_cache )
270 free( p_bank->pp_cache );
274 vlc_object_detach( p_this->p_libvlc->p_module_bank );
276 while( p_this->p_libvlc->p_module_bank->i_children )
278 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
280 if( DeleteModule( p_next ) )
282 /* Module deletion failed */
283 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
284 p_next->psz_object_name );
286 /* We just free the module by hand. Niahahahahaha. */
287 vlc_object_detach( p_next );
288 vlc_object_destroy( p_next );
292 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
293 p_this->p_libvlc->p_module_bank = NULL;
298 /*****************************************************************************
299 * module_LoadMain: load the main program info into the module bank.
300 *****************************************************************************
301 * This function fills the module bank structure with the main module infos.
302 * This is very useful as it will allow us to consider the main program just
303 * as another module, and for instance the configuration options of main will
304 * be available in the module bank structure just as for every other module.
305 *****************************************************************************/
306 void __module_LoadMain( vlc_object_t *p_this )
310 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
311 var_Get( p_this->p_libvlc, "libvlc", &lockval );
312 vlc_mutex_lock( lockval.p_address );
313 if( p_this->p_libvlc->p_module_bank->b_main )
315 vlc_mutex_unlock( lockval.p_address );
316 var_Destroy( p_this->p_libvlc, "libvlc" );
319 p_this->p_libvlc->p_module_bank->b_main = VLC_TRUE;
320 vlc_mutex_unlock( lockval.p_address );
321 var_Destroy( p_this->p_libvlc, "libvlc" );
323 AllocateBuiltinModule( p_this, vlc_entry__main );
326 /*****************************************************************************
327 * module_LoadBuiltins: load all modules which we built with.
328 *****************************************************************************
329 * This function fills the module bank structure with the builtin modules.
330 *****************************************************************************/
331 void __module_LoadBuiltins( vlc_object_t * p_this )
335 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
336 var_Get( p_this->p_libvlc, "libvlc", &lockval );
337 vlc_mutex_lock( lockval.p_address );
338 if( p_this->p_libvlc->p_module_bank->b_builtins )
340 vlc_mutex_unlock( lockval.p_address );
341 var_Destroy( p_this->p_libvlc, "libvlc" );
344 p_this->p_libvlc->p_module_bank->b_builtins = VLC_TRUE;
345 vlc_mutex_unlock( lockval.p_address );
346 var_Destroy( p_this->p_libvlc, "libvlc" );
348 msg_Dbg( p_this, "checking builtin modules" );
349 ALLOCATE_ALL_BUILTINS();
352 /*****************************************************************************
353 * module_LoadPlugins: load all plugin modules we can find.
354 *****************************************************************************
355 * This function fills the module bank structure with the plugin modules.
356 *****************************************************************************/
357 void __module_LoadPlugins( vlc_object_t * p_this )
359 #ifdef HAVE_DYNAMIC_PLUGINS
362 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
363 var_Get( p_this->p_libvlc, "libvlc", &lockval );
364 vlc_mutex_lock( lockval.p_address );
365 if( p_this->p_libvlc->p_module_bank->b_plugins )
367 vlc_mutex_unlock( lockval.p_address );
368 var_Destroy( p_this->p_libvlc, "libvlc" );
371 p_this->p_libvlc->p_module_bank->b_plugins = VLC_TRUE;
372 vlc_mutex_unlock( lockval.p_address );
373 var_Destroy( p_this->p_libvlc, "libvlc" );
375 msg_Dbg( p_this, "checking plugin modules" );
377 if( config_GetInt( p_this, "plugins-cache" ) )
378 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
380 if( p_this->p_libvlc->p_module_bank->b_cache ||
381 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
383 AllocateAllPlugins( p_this );
387 /*****************************************************************************
388 * module_Need: return the best module function, given a capability list.
389 *****************************************************************************
390 * This function returns the module that best fits the asked capabilities.
391 *****************************************************************************/
392 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
393 const char *psz_name, vlc_bool_t b_strict )
395 typedef struct module_list_t module_list_t;
402 module_list_t *p_next;
405 module_list_t *p_list, *p_first, *p_tmp;
408 int i_which_module, i_index = 0;
409 vlc_bool_t b_intf = VLC_FALSE;
414 char *psz_shortcuts = NULL, *psz_var = NULL;
415 vlc_bool_t b_force_backup = p_this->b_force;
418 /* Deal with variables */
419 if( psz_name && psz_name[0] == '$' )
422 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
423 var_Get( p_this, psz_name + 1, &val );
424 psz_var = val.psz_string;
428 /* Count how many different shortcuts were asked for */
429 if( psz_name && *psz_name )
431 char *psz_parser, *psz_last_shortcut;
433 /* If the user wants none, give him none. */
434 if( !strcmp( psz_name, "none" ) )
436 if( psz_var ) free( psz_var );
441 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
443 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
445 if( *psz_parser == ',' )
449 psz_last_shortcut = psz_parser + 1;
453 /* Check if the user wants to override the "strict" mode */
454 if( psz_last_shortcut )
456 if( !strcmp(psz_last_shortcut, "none") )
461 else if( !strcmp(psz_last_shortcut, "any") )
463 b_strict = VLC_FALSE;
469 /* Sort the modules and test them */
470 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
471 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
474 /* Parse the module list for capabilities and probe each of them */
475 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
477 int i_shortcut_bonus = 0;
479 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
481 /* Test that this module can do what we need */
482 if( strcmp( p_module->psz_capability, psz_capability ) )
484 /* Don't recurse through the sub-modules because vlc_list_find()
485 * will list them anyway. */
489 /* Test if we have the required CPU */
490 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
495 /* If we required a shortcut, check this plugin provides it. */
496 if( i_shortcuts > 0 )
499 int i_dummy, i_short = i_shortcuts;
500 char *psz_name = psz_shortcuts;
502 /* Let's drop modules with a <= 0 score (unless they are
503 * explicitly requested) */
504 b_trash = p_module->i_score <= 0;
508 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
510 if( !strcasecmp( psz_name,
511 p_module->pp_shortcuts[i_dummy] ) )
515 i_shortcut_bonus = i_short * 10000;
520 if( i_shortcut_bonus )
522 /* We found it... remember ? */
526 /* Go to the next shortcut... This is so lame! */
535 /* If we are in "strict" mode and we couldn't
536 * find the module in the list of provided shortcuts,
537 * then kick the bastard out of here!!! */
538 if( i_short == 0 && b_strict )
548 /* If we didn't require a shortcut, trash <= 0 scored plugins */
549 else if( p_module->i_score <= 0 )
554 /* Special case: test if we requested a particular intf plugin */
555 if( !i_shortcuts && p_module->psz_program
556 && !strcmp( psz_capability, "interface" )
557 && !strcmp( p_module->psz_program,
558 p_this->p_vlc->psz_object_name ) )
562 /* Remove previous non-matching plugins */
569 /* This one doesn't match */
573 /* Store this new module */
574 p_list[ i_index ].p_module = p_module;
575 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
576 p_list[ i_index ].b_force = !!i_shortcut_bonus;
578 /* Add it to the modules-to-probe list */
581 p_list[ 0 ].p_next = NULL;
586 /* Ok, so at school you learned that quicksort is quick, and
587 * bubble sort sucks raw eggs. But that's when dealing with
588 * thousands of items. Here we have barely 50. */
589 module_list_t *p_newlist = p_first;
591 if( p_first->i_score < p_list[ i_index ].i_score )
593 p_list[ i_index ].p_next = p_first;
594 p_first = &p_list[ i_index ];
598 while( p_newlist->p_next != NULL &&
599 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
601 p_newlist = p_newlist->p_next;
604 p_list[ i_index ].p_next = p_newlist->p_next;
605 p_newlist->p_next = &p_list[ i_index ];
612 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
613 i_index, i_index == 1 ? "" : "s" );
615 /* Lock all candidate modules */
617 while( p_tmp != NULL )
619 vlc_object_yield( p_tmp->p_module );
620 p_tmp = p_tmp->p_next;
623 /* We can release the list, interesting modules were yielded */
624 vlc_list_release( p_all );
626 /* Parse the linked list and use the first successful module */
628 while( p_tmp != NULL )
630 #ifdef HAVE_DYNAMIC_PLUGINS
631 /* Make sure the module is loaded in mem */
632 module_t *p_module = p_tmp->p_module->b_submodule ?
633 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
634 if( !p_module->b_builtin && !p_module->b_loaded )
636 module_t *p_new_module =
637 AllocatePlugin( p_this, p_module->psz_filename );
640 CacheMerge( p_this, p_module, p_new_module );
641 vlc_object_attach( p_new_module, p_module );
642 DeleteModule( p_new_module );
647 p_this->b_force = p_tmp->b_force;
648 if( p_tmp->p_module->pf_activate
649 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
654 vlc_object_release( p_tmp->p_module );
655 p_tmp = p_tmp->p_next;
658 /* Store the locked module value */
661 p_module = p_tmp->p_module;
662 p_tmp = p_tmp->p_next;
669 /* Unlock the remaining modules */
670 while( p_tmp != NULL )
672 vlc_object_release( p_tmp->p_module );
673 p_tmp = p_tmp->p_next;
677 p_this->b_force = b_force_backup;
679 if( p_module != NULL )
681 msg_Dbg( p_module, "using %s module \"%s\"",
682 psz_capability, p_module->psz_object_name );
684 else if( p_first == NULL )
686 if( !strcmp( psz_capability, "access_demux" ) )
688 msg_Warn( p_this, "no %s module matched \"%s\"",
689 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
693 msg_Err( p_this, "no %s module matched \"%s\"",
694 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
697 else if( psz_name != NULL && *psz_name )
699 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
700 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
705 free( psz_shortcuts );
713 /* Don't forget that the module is still locked */
717 /*****************************************************************************
718 * module_Unneed: decrease the usage count of a module.
719 *****************************************************************************
720 * This function must be called by the thread that called module_Need, to
721 * decrease the reference count and allow for hiding of modules.
722 *****************************************************************************/
723 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
725 /* Use the close method */
726 if( p_module->pf_deactivate )
728 p_module->pf_deactivate( p_this );
731 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
733 vlc_object_release( p_module );
738 /*****************************************************************************
739 * Following functions are local.
740 *****************************************************************************/
742 /*****************************************************************************
743 * AllocateAllPlugins: load all plugin modules we can find.
744 *****************************************************************************/
745 #ifdef HAVE_DYNAMIC_PLUGINS
746 static void AllocateAllPlugins( vlc_object_t *p_this )
748 /* Yes, there are two NULLs because we replace one with "plugin-path". */
749 #if defined( WIN32 ) || defined( UNDER_CE )
750 char *path[] = { "modules", "", "plugins", 0, 0 };
752 char *path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
755 char **ppsz_path = path;
758 /* If the user provided a plugin path, we add it to the list */
759 path[ sizeof(path)/sizeof(char*) - 2 ] =
760 config_GetPsz( p_this, "plugin-path" );
762 for( ; *ppsz_path != NULL ; ppsz_path++ )
764 if( !(*ppsz_path)[0] ) continue;
766 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
768 /* Handle relative as well as absolute paths */
770 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
771 (*ppsz_path)[1] != ':' )
773 if( (*ppsz_path)[0] != '/' )
776 int i_dirlen = strlen( *ppsz_path );
777 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
779 psz_fullpath = malloc( i_dirlen );
780 if( psz_fullpath == NULL )
785 sprintf( psz_fullpath, "%s\\%s",
786 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
788 sprintf( psz_fullpath, "%s/%s",
789 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
795 psz_fullpath = strdup( *ppsz_path );
798 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
800 /* Don't go deeper than 5 subdirectories */
801 AllocatePluginDir( p_this, psz_fullpath, 5 );
803 free( psz_fullpath );
806 /* Free plugin-path */
807 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
808 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
809 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
812 /*****************************************************************************
813 * AllocatePluginDir: recursively parse a directory to look for plugins
814 *****************************************************************************/
815 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
818 #if defined( UNDER_CE ) || defined( _MSC_VER )
820 wchar_t psz_wpath[MAX_PATH + 256];
821 wchar_t psz_wdir[MAX_PATH];
823 char psz_path[MAX_PATH + 256];
824 WIN32_FIND_DATA finddata;
830 struct dirent * file;
834 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
839 #if defined( UNDER_CE ) || defined( _MSC_VER )
841 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
843 rc = GetFileAttributes( psz_wdir );
844 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
846 /* Parse all files in the directory */
847 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
849 rc = GetFileAttributes( psz_dir );
850 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
853 /* Parse all files in the directory */
854 sprintf( psz_path, "%s\\*", psz_dir );
857 handle = FindFirstFile( psz_wpath, &finddata );
859 handle = FindFirstFile( psz_path, &finddata );
861 if( handle == INVALID_HANDLE_VALUE )
863 /* Empty directory */
867 /* Parse the directory and try to load all files it contains. */
871 unsigned int i_len = wcslen( finddata.cFileName );
872 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
873 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
875 unsigned int i_len = strlen( finddata.cFileName );
876 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
879 /* Skip ".", ".." and anything starting with "." */
880 if( !*finddata.cFileName || *finddata.cFileName == '.' )
882 if( !FindNextFile( handle, &finddata ) ) break;
887 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
889 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
892 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
894 else if( i_len > strlen( LIBEXT )
895 /* We only load files ending with LIBEXT */
896 && !strncasecmp( psz_path + strlen( psz_path)
898 LIBEXT, strlen( LIBEXT ) ) )
900 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
901 int64_t i_time = 0, i_size = 0;
904 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
907 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
911 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
913 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
914 i_size = attrbuf.nFileSizeHigh;
916 i_size |= attrbuf.nFileSizeLow;
920 AllocatePluginFile( p_this, psz_file, i_time, i_size );
923 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
925 /* Close the directory */
929 dir = opendir( psz_dir );
935 i_dirlen = strlen( psz_dir );
937 /* Parse the directory and try to load all files it contains. */
938 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
944 /* Skip ".", ".." and anything starting with "." */
945 if( !*file->d_name || *file->d_name == '.' )
950 i_len = strlen( file->d_name );
951 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
953 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
955 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
958 i_stat = stat( psz_file, &statbuf );
959 if( !i_stat && statbuf.st_mode & S_IFDIR )
961 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
963 else if( i_len > strlen( LIBEXT )
964 /* We only load files ending with LIBEXT */
965 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
966 LIBEXT, strlen( LIBEXT ) ) )
968 int64_t i_time = 0, i_size = 0;
972 i_time = statbuf.st_mtime;
973 i_size = statbuf.st_size;
976 AllocatePluginFile( p_this, psz_file, i_time, i_size );
982 /* Close the directory */
988 /*****************************************************************************
989 * AllocatePluginFile: load a module into memory and initialize it.
990 *****************************************************************************
991 * This function loads a dynamically loadable module and allocates a structure
992 * for its information data. The module can then be handled by module_Need
993 * and module_Unneed. It can be removed by DeleteModule.
994 *****************************************************************************/
995 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
996 int64_t i_file_time, int64_t i_file_size )
999 module_cache_t *p_cache_entry = NULL;
1002 * Check our plugins cache first then load plugin if needed
1005 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1007 if( !p_cache_entry )
1009 p_module = AllocatePlugin( p_this, psz_file );
1013 /* If junk dll, don't try to load it */
1014 if( p_cache_entry->b_junk )
1020 module_config_t *p_item;
1022 p_module = p_cache_entry->p_module;
1023 p_module->b_loaded = VLC_FALSE;
1025 /* For now we force loading if the module's config contains
1026 * callbacks or actions.
1027 * Could be optimized by adding an API call.*/
1028 for( p_item = p_module->p_config;
1029 p_item->i_type != CONFIG_HINT_END; p_item++ )
1031 if( p_item->pf_callback || p_item->i_action )
1032 p_module = AllocatePlugin( p_this, psz_file );
1039 /* Everything worked fine !
1040 * The module is ready to be added to the list. */
1041 p_module->b_builtin = VLC_FALSE;
1043 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1044 p_module->psz_object_name, p_module->psz_longname ); */
1046 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1049 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
1051 /* Add entry to cache */
1052 #define p_bank p_this->p_libvlc->p_module_bank
1054 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1055 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1056 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1057 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1058 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1059 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1060 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1063 return p_module ? 0 : -1;
1066 /*****************************************************************************
1067 * AllocatePlugin: load a module into memory and initialize it.
1068 *****************************************************************************
1069 * This function loads a dynamically loadable module and allocates a structure
1070 * for its information data. The module can then be handled by module_Need
1071 * and module_Unneed. It can be removed by DeleteModule.
1072 *****************************************************************************/
1073 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1075 module_t * p_module;
1076 module_handle_t handle;
1078 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1080 /* Now that we have successfully loaded the module, we can
1081 * allocate a structure for it */
1082 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1083 if( p_module == NULL )
1085 msg_Err( p_this, "out of memory" );
1086 CloseModule( handle );
1090 /* We need to fill these since they may be needed by CallEntry() */
1091 p_module->psz_filename = psz_file;
1092 p_module->handle = handle;
1093 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1094 p_module->b_loaded = VLC_TRUE;
1096 /* Initialize the module: fill p_module, default config */
1097 if( CallEntry( p_module ) != 0 )
1099 /* We couldn't call module_init() */
1100 vlc_object_destroy( p_module );
1101 CloseModule( handle );
1105 DupModule( p_module );
1106 p_module->psz_filename = strdup( p_module->psz_filename );
1108 /* Everything worked fine ! The module is ready to be added to the list. */
1109 p_module->b_builtin = VLC_FALSE;
1114 /*****************************************************************************
1115 * DupModule: make a plugin module standalone.
1116 *****************************************************************************
1117 * This function duplicates all strings in the module, so that the dynamic
1118 * object can be unloaded. It acts recursively on submodules.
1119 *****************************************************************************/
1120 static void DupModule( module_t *p_module )
1125 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1127 *pp_shortcut = strdup( *pp_shortcut );
1130 /* We strdup() these entries so that they are still valid when the
1131 * module is unloaded. */
1132 p_module->psz_object_name = strdup( p_module->psz_object_name );
1133 p_module->psz_capability = strdup( p_module->psz_capability );
1134 p_module->psz_shortname = p_module->psz_shortname ?
1135 strdup( p_module->psz_shortname ) : NULL;
1136 p_module->psz_longname = strdup( p_module->psz_longname );
1138 if( p_module->psz_program != NULL )
1140 p_module->psz_program = strdup( p_module->psz_program );
1143 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1145 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1149 /*****************************************************************************
1150 * UndupModule: free a duplicated module.
1151 *****************************************************************************
1152 * This function frees the allocations done in DupModule().
1153 *****************************************************************************/
1154 static void UndupModule( module_t *p_module )
1159 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1161 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1164 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1166 free( *pp_shortcut );
1169 free( p_module->psz_object_name );
1170 free( p_module->psz_capability );
1171 if( p_module->psz_shortname ) free( p_module->psz_shortname );
1172 free( p_module->psz_longname );
1174 if( p_module->psz_program != NULL )
1176 free( p_module->psz_program );
1180 #endif /* HAVE_DYNAMIC_PLUGINS */
1182 /*****************************************************************************
1183 * AllocateBuiltinModule: initialize a builtin module.
1184 *****************************************************************************
1185 * This function registers a builtin module and allocates a structure
1186 * for its information data. The module can then be handled by module_Need
1187 * and module_Unneed. It can be removed by DeleteModule.
1188 *****************************************************************************/
1189 static int AllocateBuiltinModule( vlc_object_t * p_this,
1190 int ( *pf_entry ) ( module_t * ) )
1192 module_t * p_module;
1194 /* Now that we have successfully loaded the module, we can
1195 * allocate a structure for it */
1196 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1197 if( p_module == NULL )
1199 msg_Err( p_this, "out of memory" );
1203 /* Initialize the module : fill p_module->psz_object_name, etc. */
1204 if( pf_entry( p_module ) != 0 )
1206 /* With a well-written module we shouldn't have to print an
1207 * additional error message here, but just make sure. */
1208 msg_Err( p_this, "failed calling entry point in builtin module" );
1209 vlc_object_destroy( p_module );
1213 /* Everything worked fine ! The module is ready to be added to the list. */
1214 p_module->b_builtin = VLC_TRUE;
1216 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1217 p_module->psz_object_name, p_module->psz_longname ); */
1219 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1224 /*****************************************************************************
1225 * DeleteModule: delete a module and its structure.
1226 *****************************************************************************
1227 * This function can only be called if the module isn't being used.
1228 *****************************************************************************/
1229 static int DeleteModule( module_t * p_module )
1231 vlc_object_detach( p_module );
1233 /* We free the structures that we strdup()ed in Allocate*Module(). */
1234 #ifdef HAVE_DYNAMIC_PLUGINS
1235 if( !p_module->b_builtin )
1237 if( p_module->b_loaded && p_module->b_unloadable )
1239 CloseModule( p_module->handle );
1241 UndupModule( p_module );
1242 free( p_module->psz_filename );
1246 /* Free and detach the object's children */
1247 while( p_module->i_children )
1249 vlc_object_t *p_this = p_module->pp_children[0];
1250 vlc_object_detach( p_this );
1251 vlc_object_destroy( p_this );
1254 config_Free( p_module );
1255 vlc_object_destroy( p_module );
1260 #ifdef HAVE_DYNAMIC_PLUGINS
1261 /*****************************************************************************
1262 * CallEntry: call an entry point.
1263 *****************************************************************************
1264 * This function calls a symbol given its name and a module structure. The
1265 * symbol MUST refer to a function returning int and taking a module_t* as
1267 *****************************************************************************/
1268 static int CallEntry( module_t * p_module )
1270 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1271 int (* pf_symbol) ( module_t * p_module );
1273 /* Try to resolve the symbol */
1274 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1276 if( pf_symbol == NULL )
1278 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1279 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1280 psz_name, p_module->psz_filename );
1281 #elif defined(HAVE_DL_WINDOWS)
1282 char *psz_error = GetWindowsError();
1283 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1284 psz_name, p_module->psz_filename, psz_error );
1286 #elif defined(HAVE_DL_DLOPEN)
1287 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1288 psz_name, p_module->psz_filename, dlerror() );
1289 #elif defined(HAVE_DL_SHL_LOAD)
1290 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1291 psz_name, p_module->psz_filename, strerror(errno) );
1293 # error "Something is wrong in modules.c"
1298 /* We can now try to call the symbol */
1299 if( pf_symbol( p_module ) != 0 )
1301 /* With a well-written module we shouldn't have to print an
1302 * additional error message here, but just make sure. */
1303 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1304 psz_name, p_module->psz_filename );
1308 /* Everything worked fine, we can return */
1312 /*****************************************************************************
1313 * LoadModule: loads a dynamic library
1314 *****************************************************************************
1315 * This function loads a dynamically linked library using a system dependant
1316 * method. Will return 0 on success as well as the module handle.
1317 *****************************************************************************/
1318 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1319 module_handle_t *p_handle )
1321 module_handle_t handle;
1323 #if defined(HAVE_DL_DYLD)
1324 NSObjectFileImage image;
1325 NSObjectFileImageReturnCode ret;
1327 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1329 if( ret != NSObjectFileImageSuccess )
1331 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1335 /* Open the dynamic module */
1336 handle = NSLinkModule( image, psz_file,
1337 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1341 NSLinkEditErrors errors;
1342 const char *psz_file, *psz_err;
1344 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1345 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1346 NSDestroyObjectFileImage( image );
1350 /* Destroy our image, we won't need it */
1351 NSDestroyObjectFileImage( image );
1353 #elif defined(HAVE_DL_BEOS)
1354 handle = load_add_on( psz_file );
1357 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1361 #elif defined(HAVE_DL_WINDOWS)
1364 wchar_t psz_wfile[MAX_PATH];
1365 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1366 handle = LoadLibrary( psz_wfile );
1369 handle = LoadLibrary( psz_file );
1371 if( handle == NULL )
1373 char *psz_err = GetWindowsError();
1374 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1379 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1380 /* static is OK, we are called atomically */
1382 # if defined(SYS_LINUX)
1383 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1384 * are going to get namespace collisions when two modules have common
1385 * public symbols, but ALSA is being a pest here. */
1386 if( strstr( psz_file, "alsa_plugin" ) )
1388 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1389 if( handle == NULL )
1391 msg_Warn( p_this, "cannot load module `%s' (%s)",
1392 psz_file, dlerror() );
1398 handle = dlopen( psz_file, RTLD_NOW );
1399 if( handle == NULL )
1401 msg_Warn( p_this, "cannot load module `%s' (%s)",
1402 psz_file, dlerror() );
1406 #elif defined(HAVE_DL_DLOPEN)
1407 # if defined(DL_LAZY)
1408 handle = dlopen( psz_file, DL_LAZY );
1410 handle = dlopen( psz_file, 0 );
1412 if( handle == NULL )
1414 msg_Warn( p_this, "cannot load module `%s' (%s)",
1415 psz_file, dlerror() );
1419 #elif defined(HAVE_DL_SHL_LOAD)
1420 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1421 if( handle == NULL )
1423 msg_Warn( p_this, "cannot load module `%s' (%s)",
1424 psz_file, strerror(errno) );
1429 # error "Something is wrong in modules.c"
1437 /*****************************************************************************
1438 * CloseModule: unload a dynamic library
1439 *****************************************************************************
1440 * This function unloads a previously opened dynamically linked library
1441 * using a system dependant method. No return value is taken in consideration,
1442 * since some libraries sometimes refuse to close properly.
1443 *****************************************************************************/
1444 static void CloseModule( module_handle_t handle )
1446 #if defined(HAVE_DL_DYLD)
1447 NSUnLinkModule( handle, FALSE );
1449 #elif defined(HAVE_DL_BEOS)
1450 unload_add_on( handle );
1452 #elif defined(HAVE_DL_WINDOWS)
1453 FreeLibrary( handle );
1455 #elif defined(HAVE_DL_DLOPEN)
1458 #elif defined(HAVE_DL_SHL_LOAD)
1459 shl_unload( handle );
1465 /*****************************************************************************
1466 * GetSymbol: get a symbol from a dynamic library
1467 *****************************************************************************
1468 * This function queries a loaded library for a symbol specified in a
1469 * string, and returns a pointer to it. We don't check for dlerror() or
1470 * similar functions, since we want a non-NULL symbol anyway.
1471 *****************************************************************************/
1472 static void * _module_getsymbol( module_handle_t, const char * );
1474 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1476 void * p_symbol = _module_getsymbol( handle, psz_function );
1478 /* MacOS X dl library expects symbols to begin with "_". So do
1479 * some other operating systems. That's really lame, but hey, what
1481 if( p_symbol == NULL )
1483 char *psz_call = malloc( strlen( psz_function ) + 2 );
1485 strcpy( psz_call + 1, psz_function );
1486 psz_call[ 0 ] = '_';
1487 p_symbol = _module_getsymbol( handle, psz_call );
1494 static void * _module_getsymbol( module_handle_t handle,
1495 const char * psz_function )
1497 #if defined(HAVE_DL_DYLD)
1498 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1499 return NSAddressOfSymbol( sym );
1501 #elif defined(HAVE_DL_BEOS)
1503 if( B_OK == get_image_symbol( handle, psz_function,
1504 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1513 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1514 wchar_t psz_real[256];
1515 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1517 return (void *)GetProcAddress( handle, psz_real );
1519 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1520 return (void *)GetProcAddress( handle, (char *)psz_function );
1522 #elif defined(HAVE_DL_DLOPEN)
1523 return dlsym( handle, psz_function );
1525 #elif defined(HAVE_DL_SHL_LOAD)
1527 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1533 #if defined(HAVE_DL_WINDOWS)
1534 static char * GetWindowsError( void )
1536 #if defined(UNDER_CE)
1537 wchar_t psz_tmp[MAX_PATH];
1538 char * psz_buffer = malloc( MAX_PATH );
1540 char * psz_tmp = malloc( MAX_PATH );
1542 int i = 0, i_error = GetLastError();
1544 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1545 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1546 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1548 /* Go to the end of the string */
1549 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1556 #if defined(UNDER_CE)
1557 swprintf( psz_tmp + i, L" (error %i)", i_error );
1558 psz_tmp[ 255 ] = L'\0';
1560 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1561 psz_tmp[ 255 ] = '\0';
1565 #if defined(UNDER_CE)
1566 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1572 #endif /* HAVE_DL_WINDOWS */
1574 /*****************************************************************************
1575 * LoadPluginsCache: loads the plugins cache file
1576 *****************************************************************************
1577 * This function will load the plugin cache if present and valid. This cache
1578 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1579 * actually load the dynamically loadable module.
1580 * This allows us to only fully load plugins when they are actually used.
1581 *****************************************************************************/
1582 static void CacheLoad( vlc_object_t *p_this )
1584 char *psz_filename, *psz_homedir;
1586 int i, j, i_size, i_read;
1587 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1588 char p_cachelang[6], p_lang[6];
1590 module_cache_t **pp_cache = 0;
1591 int32_t i_file_size, i_marker;
1593 psz_homedir = p_this->p_vlc->psz_homedir;
1596 msg_Err( p_this, "psz_homedir is null" );
1600 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1601 PLUGINSCACHE_DIR, CacheName() );
1604 msg_Err( p_this, "out of memory" );
1608 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1610 #if !defined( UNDER_CE )
1611 unlink( psz_filename );
1613 wchar_t psz_wf[MAX_PATH];
1614 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1615 DeleteFile( psz_wf );
1617 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1618 free( psz_filename );
1622 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1624 file = fopen( psz_filename, "rb" );
1627 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1629 free( psz_filename );
1632 free( psz_filename );
1634 /* Check the file size */
1635 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1636 if( i_read != sizeof(i_file_size) )
1638 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1644 fseek( file, 0, SEEK_END );
1645 if( ftell( file ) != i_file_size )
1647 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1648 "(corrupted size)" );
1652 fseek( file, sizeof(i_file_size), SEEK_SET );
1654 /* Check the file is a plugins cache */
1655 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1656 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1657 if( i_read != i_size ||
1658 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1660 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1665 /* Check Sub-version number */
1666 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1667 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1669 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1670 "(corrupted header)" );
1675 /* Check the language hasn't changed */
1676 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1677 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1678 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1680 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1681 "(language changed)" );
1686 /* Check header marker */
1687 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1688 if( i_read != sizeof(i_marker) ||
1689 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1691 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1692 "(corrupted header)" );
1697 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1698 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1700 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1701 malloc( i_cache * sizeof(void *) );
1703 #define LOAD_IMMEDIATE(a) \
1704 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1705 #define LOAD_STRING(a) \
1706 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1707 != sizeof(i_size) ) goto error; \
1708 if( i_size && i_size < 16384 ) { \
1709 a = malloc( i_size ); \
1710 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1712 if( a[i_size-1] ) { \
1719 for( i = 0; i < i_cache; i++ )
1724 pp_cache[i] = malloc( sizeof(module_cache_t) );
1725 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1727 /* Load common info */
1728 LOAD_STRING( pp_cache[i]->psz_file );
1729 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1730 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1731 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1733 if( pp_cache[i]->b_junk ) continue;
1735 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1737 /* Load additional infos */
1738 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1739 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1740 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1741 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1742 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1744 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1746 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1747 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1748 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1749 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1750 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1751 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1754 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1757 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1759 LOAD_IMMEDIATE( i_submodules );
1761 while( i_submodules-- )
1763 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1764 vlc_object_attach( p_module, pp_cache[i]->p_module );
1765 p_module->b_submodule = VLC_TRUE;
1767 LOAD_STRING( p_module->psz_object_name );
1768 LOAD_STRING( p_module->psz_shortname );
1769 LOAD_STRING( p_module->psz_longname );
1770 LOAD_STRING( p_module->psz_program );
1771 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1773 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1775 LOAD_STRING( p_module->psz_capability );
1776 LOAD_IMMEDIATE( p_module->i_score );
1777 LOAD_IMMEDIATE( p_module->i_cpu );
1778 LOAD_IMMEDIATE( p_module->b_unloadable );
1779 LOAD_IMMEDIATE( p_module->b_reentrant );
1788 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1791 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1797 int CacheLoadConfig( module_t *p_module, FILE *file )
1802 /* Calculate the structure length */
1803 LOAD_IMMEDIATE( p_module->i_config_items );
1804 LOAD_IMMEDIATE( p_module->i_bool_items );
1806 LOAD_IMMEDIATE( i_lines );
1808 /* Allocate memory */
1809 p_module->p_config =
1810 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1811 if( p_module->p_config == NULL )
1813 msg_Err( p_module, "config error: can't duplicate p_config" );
1817 /* Do the duplication job */
1818 for( i = 0; i < i_lines ; i++ )
1820 LOAD_IMMEDIATE( p_module->p_config[i] );
1822 LOAD_STRING( p_module->p_config[i].psz_type );
1823 LOAD_STRING( p_module->p_config[i].psz_name );
1824 LOAD_STRING( p_module->p_config[i].psz_text );
1825 LOAD_STRING( p_module->p_config[i].psz_longtext );
1826 LOAD_STRING( p_module->p_config[i].psz_current );
1827 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1829 p_module->p_config[i].psz_value =
1830 p_module->p_config[i].psz_value_orig ?
1831 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1832 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1833 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1834 p_module->p_config[i].i_value_saved = p_module->p_config[i].i_value;
1835 p_module->p_config[i].f_value_saved = p_module->p_config[i].f_value;
1836 p_module->p_config[i].psz_value_saved = 0;
1837 p_module->p_config[i].b_dirty = VLC_FALSE;
1839 p_module->p_config[i].p_lock = &p_module->object_lock;
1841 if( p_module->p_config[i].i_list )
1843 if( p_module->p_config[i].ppsz_list )
1845 p_module->p_config[i].ppsz_list =
1846 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1847 if( p_module->p_config[i].ppsz_list )
1849 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1850 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1851 p_module->p_config[i].ppsz_list[j] = NULL;
1854 if( p_module->p_config[i].ppsz_list_text )
1856 p_module->p_config[i].ppsz_list_text =
1857 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1858 if( p_module->p_config[i].ppsz_list_text )
1860 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1861 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1862 p_module->p_config[i].ppsz_list_text[j] = NULL;
1865 if( p_module->p_config[i].pi_list )
1867 p_module->p_config[i].pi_list =
1868 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1869 if( p_module->p_config[i].pi_list )
1871 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1872 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1877 if( p_module->p_config[i].i_action )
1879 p_module->p_config[i].ppf_action =
1880 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1881 p_module->p_config[i].ppsz_action_text =
1882 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1884 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1886 p_module->p_config[i].ppf_action[j] = 0;
1887 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1891 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1894 p_module->p_config[i].i_type = CONFIG_HINT_END;
1900 return VLC_EGENERIC;
1903 /*****************************************************************************
1904 * SavePluginsCache: saves the plugins cache to a file
1905 *****************************************************************************/
1906 static void CacheSave( vlc_object_t *p_this )
1908 static char const psz_tag[] =
1909 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1910 "# This file is a cache directory tag created by VLC.\r\n"
1911 "# For information about cache directory tags, see:\r\n"
1912 "# http://www.brynosaurus.com/cachedir/\r\n";
1914 char *psz_filename, *psz_homedir;
1917 module_cache_t **pp_cache;
1918 int32_t i_file_size = 0;
1920 psz_homedir = p_this->p_vlc->psz_homedir;
1923 msg_Err( p_this, "psz_homedir is null" );
1927 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1928 strlen(psz_homedir) + strlen(CacheName()) );
1932 msg_Err( p_this, "out of memory" );
1936 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1938 config_CreateDir( p_this, psz_filename );
1940 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1942 config_CreateDir( p_this, psz_filename );
1944 strcat( psz_filename, "/CACHEDIR.TAG" );
1946 file = fopen( psz_filename, "wb" );
1949 fwrite( psz_tag, 1, strlen(psz_tag), file );
1953 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1954 PLUGINSCACHE_DIR, CacheName() );
1956 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1958 file = fopen( psz_filename, "wb" );
1961 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1963 free( psz_filename );
1966 free( psz_filename );
1968 /* Empty space for file size */
1969 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1971 /* Contains version number */
1972 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1974 /* Sub-version number (to avoid breakage in the dev version when cache
1975 * structure changes) */
1976 i_file_size = CACHE_SUBVERSION_NUM;
1977 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1980 fprintf( file, "%5.5s", _("C") );
1983 i_file_size = ftell( file );
1984 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1986 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1987 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1989 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1991 #define SAVE_IMMEDIATE(a) \
1992 fwrite( &a, sizeof(char), sizeof(a), file )
1993 #define SAVE_STRING(a) \
1994 { i_size = a ? strlen( a ) + 1 : 0; \
1995 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1996 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1999 for( i = 0; i < i_cache; i++ )
2002 int32_t i_submodule;
2004 /* Save common info */
2005 SAVE_STRING( pp_cache[i]->psz_file );
2006 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2007 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2008 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2010 if( pp_cache[i]->b_junk ) continue;
2012 /* Save additional infos */
2013 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2014 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2015 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2016 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2017 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2019 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2021 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2022 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2023 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2024 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2025 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2026 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2029 CacheSaveConfig( pp_cache[i]->p_module, file );
2031 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2033 i_submodule = pp_cache[i]->p_module->i_children;
2034 SAVE_IMMEDIATE( i_submodule );
2035 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2038 module_t *p_module =
2039 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2041 SAVE_STRING( p_module->psz_object_name );
2042 SAVE_STRING( p_module->psz_shortname );
2043 SAVE_STRING( p_module->psz_longname );
2044 SAVE_STRING( p_module->psz_program );
2045 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2047 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2049 SAVE_STRING( p_module->psz_capability );
2050 SAVE_IMMEDIATE( p_module->i_score );
2051 SAVE_IMMEDIATE( p_module->i_cpu );
2052 SAVE_IMMEDIATE( p_module->b_unloadable );
2053 SAVE_IMMEDIATE( p_module->b_reentrant );
2057 /* Fill-up file size */
2058 i_file_size = ftell( file );
2059 fseek( file, 0, SEEK_SET );
2060 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2067 void CacheSaveConfig( module_t *p_module, FILE *file )
2069 int i, j, i_lines = 0;
2070 module_config_t *p_item;
2073 SAVE_IMMEDIATE( p_module->i_config_items );
2074 SAVE_IMMEDIATE( p_module->i_bool_items );
2076 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2077 p_item++ ) i_lines++;
2079 SAVE_IMMEDIATE( i_lines );
2081 for( i = 0; i < i_lines ; i++ )
2083 SAVE_IMMEDIATE( p_module->p_config[i] );
2085 SAVE_STRING( p_module->p_config[i].psz_type );
2086 SAVE_STRING( p_module->p_config[i].psz_name );
2087 SAVE_STRING( p_module->p_config[i].psz_text );
2088 SAVE_STRING( p_module->p_config[i].psz_longtext );
2089 SAVE_STRING( p_module->p_config[i].psz_current );
2090 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2092 if( p_module->p_config[i].i_list )
2094 if( p_module->p_config[i].ppsz_list )
2096 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2097 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2100 if( p_module->p_config[i].ppsz_list_text )
2102 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2103 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2105 if( p_module->p_config[i].pi_list )
2107 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2108 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2112 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2113 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2115 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2119 /*****************************************************************************
2120 * CacheName: Return the cache file name for this platform.
2121 *****************************************************************************/
2122 static char *CacheName( void )
2124 static char psz_cachename[32];
2125 static vlc_bool_t b_initialised = VLC_FALSE;
2127 if( !b_initialised )
2129 /* Code int size, pointer size and endianness in the filename */
2130 int32_t x = 0xbe00001e;
2131 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2132 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2133 b_initialised = VLC_TRUE;
2136 return psz_cachename;
2139 /*****************************************************************************
2140 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2141 *****************************************************************************/
2142 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2143 module_t *p_module )
2147 p_cache->pf_activate = p_module->pf_activate;
2148 p_cache->pf_deactivate = p_module->pf_deactivate;
2149 p_cache->p_symbols = p_module->p_symbols;
2150 p_cache->handle = p_module->handle;
2152 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2154 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2155 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2156 p_cchild->pf_activate = p_child->pf_activate;
2157 p_cchild->pf_deactivate = p_child->pf_deactivate;
2158 p_cchild->p_symbols = p_child->p_symbols;
2161 p_cache->b_loaded = VLC_TRUE;
2162 p_module->b_loaded = VLC_FALSE;
2165 /*****************************************************************************
2166 * FindPluginCache: finds the cache entry corresponding to a file
2167 *****************************************************************************/
2168 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2169 int64_t i_time, int64_t i_size )
2171 module_cache_t **pp_cache;
2174 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2175 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2177 for( i = 0; i < i_cache; i++ )
2179 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2180 pp_cache[i]->i_time == i_time &&
2181 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2187 #endif /* HAVE_DYNAMIC_PLUGINS */