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 *****************************************************************************/
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 "vlc_interaction.h"
83 #include "intf_eject.h"
85 #include "vlc_playlist.h"
87 #include "vlc_video.h"
88 #include "video_output.h"
89 #include "vout_synchro.h"
92 #include "audio_output.h"
93 #include "aout_internal.h"
95 #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 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
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, "removing 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 #ifndef HAVE_SHARED_LIBVLC
1094 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1096 p_module->b_loaded = VLC_TRUE;
1098 /* Initialize the module: fill p_module, default config */
1099 if( CallEntry( p_module ) != 0 )
1101 /* We couldn't call module_init() */
1102 vlc_object_destroy( p_module );
1103 CloseModule( handle );
1107 DupModule( p_module );
1108 p_module->psz_filename = strdup( p_module->psz_filename );
1110 /* Everything worked fine ! The module is ready to be added to the list. */
1111 p_module->b_builtin = VLC_FALSE;
1116 /*****************************************************************************
1117 * DupModule: make a plugin module standalone.
1118 *****************************************************************************
1119 * This function duplicates all strings in the module, so that the dynamic
1120 * object can be unloaded. It acts recursively on submodules.
1121 *****************************************************************************/
1122 static void DupModule( module_t *p_module )
1127 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1129 *pp_shortcut = strdup( *pp_shortcut );
1132 /* We strdup() these entries so that they are still valid when the
1133 * module is unloaded. */
1134 p_module->psz_object_name = strdup( p_module->psz_object_name );
1135 p_module->psz_capability = strdup( p_module->psz_capability );
1136 p_module->psz_shortname = p_module->psz_shortname ?
1137 strdup( p_module->psz_shortname ) : NULL;
1138 p_module->psz_longname = strdup( p_module->psz_longname );
1140 if( p_module->psz_program != NULL )
1142 p_module->psz_program = strdup( p_module->psz_program );
1145 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1147 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1151 /*****************************************************************************
1152 * UndupModule: free a duplicated module.
1153 *****************************************************************************
1154 * This function frees the allocations done in DupModule().
1155 *****************************************************************************/
1156 static void UndupModule( module_t *p_module )
1161 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1163 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1166 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1168 free( *pp_shortcut );
1171 free( p_module->psz_object_name );
1172 free( p_module->psz_capability );
1173 if( p_module->psz_shortname ) free( p_module->psz_shortname );
1174 free( p_module->psz_longname );
1176 if( p_module->psz_program != NULL )
1178 free( p_module->psz_program );
1182 #endif /* HAVE_DYNAMIC_PLUGINS */
1184 /*****************************************************************************
1185 * AllocateBuiltinModule: initialize a builtin module.
1186 *****************************************************************************
1187 * This function registers a builtin module and allocates a structure
1188 * for its information data. The module can then be handled by module_Need
1189 * and module_Unneed. It can be removed by DeleteModule.
1190 *****************************************************************************/
1191 static int AllocateBuiltinModule( vlc_object_t * p_this,
1192 int ( *pf_entry ) ( module_t * ) )
1194 module_t * p_module;
1196 /* Now that we have successfully loaded the module, we can
1197 * allocate a structure for it */
1198 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1199 if( p_module == NULL )
1201 msg_Err( p_this, "out of memory" );
1205 /* Initialize the module : fill p_module->psz_object_name, etc. */
1206 if( pf_entry( p_module ) != 0 )
1208 /* With a well-written module we shouldn't have to print an
1209 * additional error message here, but just make sure. */
1210 msg_Err( p_this, "failed calling entry point in builtin module" );
1211 vlc_object_destroy( p_module );
1215 /* Everything worked fine ! The module is ready to be added to the list. */
1216 p_module->b_builtin = VLC_TRUE;
1218 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1219 p_module->psz_object_name, p_module->psz_longname ); */
1221 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1226 /*****************************************************************************
1227 * DeleteModule: delete a module and its structure.
1228 *****************************************************************************
1229 * This function can only be called if the module isn't being used.
1230 *****************************************************************************/
1231 static int DeleteModule( module_t * p_module )
1233 vlc_object_detach( p_module );
1235 /* We free the structures that we strdup()ed in Allocate*Module(). */
1236 #ifdef HAVE_DYNAMIC_PLUGINS
1237 if( !p_module->b_builtin )
1239 if( p_module->b_loaded && p_module->b_unloadable )
1241 CloseModule( p_module->handle );
1243 UndupModule( p_module );
1244 free( p_module->psz_filename );
1248 /* Free and detach the object's children */
1249 while( p_module->i_children )
1251 vlc_object_t *p_this = p_module->pp_children[0];
1252 vlc_object_detach( p_this );
1253 vlc_object_destroy( p_this );
1256 config_Free( p_module );
1257 vlc_object_destroy( p_module );
1262 #ifdef HAVE_DYNAMIC_PLUGINS
1263 /*****************************************************************************
1264 * CallEntry: call an entry point.
1265 *****************************************************************************
1266 * This function calls a symbol given its name and a module structure. The
1267 * symbol MUST refer to a function returning int and taking a module_t* as
1269 *****************************************************************************/
1270 static int CallEntry( module_t * p_module )
1272 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1273 int (* pf_symbol) ( module_t * p_module );
1275 /* Try to resolve the symbol */
1276 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1278 if( pf_symbol == NULL )
1280 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1281 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1282 psz_name, p_module->psz_filename );
1283 #elif defined(HAVE_DL_WINDOWS)
1284 char *psz_error = GetWindowsError();
1285 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1286 psz_name, p_module->psz_filename, psz_error );
1288 #elif defined(HAVE_DL_DLOPEN)
1289 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1290 psz_name, p_module->psz_filename, dlerror() );
1291 #elif defined(HAVE_DL_SHL_LOAD)
1292 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1293 psz_name, p_module->psz_filename, strerror(errno) );
1295 # error "Something is wrong in modules.c"
1300 /* We can now try to call the symbol */
1301 if( pf_symbol( p_module ) != 0 )
1303 /* With a well-written module we shouldn't have to print an
1304 * additional error message here, but just make sure. */
1305 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1306 psz_name, p_module->psz_filename );
1310 /* Everything worked fine, we can return */
1314 /*****************************************************************************
1315 * LoadModule: loads a dynamic library
1316 *****************************************************************************
1317 * This function loads a dynamically linked library using a system dependant
1318 * method. Will return 0 on success as well as the module handle.
1319 *****************************************************************************/
1320 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1321 module_handle_t *p_handle )
1323 module_handle_t handle;
1325 #if defined(HAVE_DL_DYLD)
1326 NSObjectFileImage image;
1327 NSObjectFileImageReturnCode ret;
1329 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1331 if( ret != NSObjectFileImageSuccess )
1333 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1337 /* Open the dynamic module */
1338 handle = NSLinkModule( image, psz_file,
1339 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1343 NSLinkEditErrors errors;
1344 const char *psz_file, *psz_err;
1346 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1347 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1348 NSDestroyObjectFileImage( image );
1352 /* Destroy our image, we won't need it */
1353 NSDestroyObjectFileImage( image );
1355 #elif defined(HAVE_DL_BEOS)
1356 handle = load_add_on( psz_file );
1359 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1363 #elif defined(HAVE_DL_WINDOWS)
1366 wchar_t psz_wfile[MAX_PATH];
1367 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1368 handle = LoadLibrary( psz_wfile );
1371 handle = LoadLibrary( psz_file );
1373 if( handle == NULL )
1375 char *psz_err = GetWindowsError();
1376 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1381 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1382 /* static is OK, we are called atomically */
1384 # if defined(SYS_LINUX)
1385 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1386 * are going to get namespace collisions when two modules have common
1387 * public symbols, but ALSA is being a pest here. */
1388 if( strstr( psz_file, "alsa_plugin" ) )
1390 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1391 if( handle == NULL )
1393 msg_Warn( p_this, "cannot load module `%s' (%s)",
1394 psz_file, dlerror() );
1400 handle = dlopen( psz_file, RTLD_NOW );
1401 if( handle == NULL )
1403 msg_Warn( p_this, "cannot load module `%s' (%s)",
1404 psz_file, dlerror() );
1408 #elif defined(HAVE_DL_DLOPEN)
1409 # if defined(DL_LAZY)
1410 handle = dlopen( psz_file, DL_LAZY );
1412 handle = dlopen( psz_file, 0 );
1414 if( handle == NULL )
1416 msg_Warn( p_this, "cannot load module `%s' (%s)",
1417 psz_file, dlerror() );
1421 #elif defined(HAVE_DL_SHL_LOAD)
1422 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1423 if( handle == NULL )
1425 msg_Warn( p_this, "cannot load module `%s' (%s)",
1426 psz_file, strerror(errno) );
1431 # error "Something is wrong in modules.c"
1439 /*****************************************************************************
1440 * CloseModule: unload a dynamic library
1441 *****************************************************************************
1442 * This function unloads a previously opened dynamically linked library
1443 * using a system dependant method. No return value is taken in consideration,
1444 * since some libraries sometimes refuse to close properly.
1445 *****************************************************************************/
1446 static void CloseModule( module_handle_t handle )
1448 #if defined(HAVE_DL_DYLD)
1449 NSUnLinkModule( handle, FALSE );
1451 #elif defined(HAVE_DL_BEOS)
1452 unload_add_on( handle );
1454 #elif defined(HAVE_DL_WINDOWS)
1455 FreeLibrary( handle );
1457 #elif defined(HAVE_DL_DLOPEN)
1460 #elif defined(HAVE_DL_SHL_LOAD)
1461 shl_unload( handle );
1467 /*****************************************************************************
1468 * GetSymbol: get a symbol from a dynamic library
1469 *****************************************************************************
1470 * This function queries a loaded library for a symbol specified in a
1471 * string, and returns a pointer to it. We don't check for dlerror() or
1472 * similar functions, since we want a non-NULL symbol anyway.
1473 *****************************************************************************/
1474 static void * _module_getsymbol( module_handle_t, const char * );
1476 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1478 void * p_symbol = _module_getsymbol( handle, psz_function );
1480 /* MacOS X dl library expects symbols to begin with "_". So do
1481 * some other operating systems. That's really lame, but hey, what
1483 if( p_symbol == NULL )
1485 char *psz_call = malloc( strlen( psz_function ) + 2 );
1487 strcpy( psz_call + 1, psz_function );
1488 psz_call[ 0 ] = '_';
1489 p_symbol = _module_getsymbol( handle, psz_call );
1496 static void * _module_getsymbol( module_handle_t handle,
1497 const char * psz_function )
1499 #if defined(HAVE_DL_DYLD)
1500 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1501 return NSAddressOfSymbol( sym );
1503 #elif defined(HAVE_DL_BEOS)
1505 if( B_OK == get_image_symbol( handle, psz_function,
1506 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1515 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1516 wchar_t psz_real[256];
1517 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1519 return (void *)GetProcAddress( handle, psz_real );
1521 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1522 return (void *)GetProcAddress( handle, (char *)psz_function );
1524 #elif defined(HAVE_DL_DLOPEN)
1525 return dlsym( handle, psz_function );
1527 #elif defined(HAVE_DL_SHL_LOAD)
1529 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1535 #if defined(HAVE_DL_WINDOWS)
1536 static char * GetWindowsError( void )
1538 #if defined(UNDER_CE)
1539 wchar_t psz_tmp[MAX_PATH];
1540 char * psz_buffer = malloc( MAX_PATH );
1542 char * psz_tmp = malloc( MAX_PATH );
1544 int i = 0, i_error = GetLastError();
1546 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1547 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1548 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1550 /* Go to the end of the string */
1551 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1558 #if defined(UNDER_CE)
1559 swprintf( psz_tmp + i, L" (error %i)", i_error );
1560 psz_tmp[ 255 ] = L'\0';
1562 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1563 psz_tmp[ 255 ] = '\0';
1567 #if defined(UNDER_CE)
1568 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1574 #endif /* HAVE_DL_WINDOWS */
1576 /*****************************************************************************
1577 * LoadPluginsCache: loads the plugins cache file
1578 *****************************************************************************
1579 * This function will load the plugin cache if present and valid. This cache
1580 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1581 * actually load the dynamically loadable module.
1582 * This allows us to only fully load plugins when they are actually used.
1583 *****************************************************************************/
1584 static void CacheLoad( vlc_object_t *p_this )
1586 char *psz_filename, *psz_homedir;
1588 int i, j, i_size, i_read;
1589 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1590 char p_cachelang[6], p_lang[6];
1592 module_cache_t **pp_cache = 0;
1593 int32_t i_file_size, i_marker;
1595 psz_homedir = p_this->p_vlc->psz_homedir;
1598 msg_Err( p_this, "psz_homedir is null" );
1602 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1603 PLUGINSCACHE_DIR, CacheName() );
1606 msg_Err( p_this, "out of memory" );
1610 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1612 #if !defined( UNDER_CE )
1613 unlink( psz_filename );
1615 wchar_t psz_wf[MAX_PATH];
1616 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1617 DeleteFile( psz_wf );
1619 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1620 free( psz_filename );
1624 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1626 file = fopen( psz_filename, "rb" );
1629 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1631 free( psz_filename );
1634 free( psz_filename );
1636 /* Check the file size */
1637 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1638 if( i_read != sizeof(i_file_size) )
1640 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1646 fseek( file, 0, SEEK_END );
1647 if( ftell( file ) != i_file_size )
1649 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1650 "(corrupted size)" );
1654 fseek( file, sizeof(i_file_size), SEEK_SET );
1656 /* Check the file is a plugins cache */
1657 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1658 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1659 if( i_read != i_size ||
1660 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1662 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1667 /* Check Sub-version number */
1668 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1669 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1671 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1672 "(corrupted header)" );
1677 /* Check the language hasn't changed */
1678 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1679 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1680 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1682 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1683 "(language changed)" );
1688 /* Check header marker */
1689 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1690 if( i_read != sizeof(i_marker) ||
1691 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1693 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1694 "(corrupted header)" );
1699 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1700 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1702 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1703 malloc( i_cache * sizeof(void *) );
1705 #define LOAD_IMMEDIATE(a) \
1706 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1707 #define LOAD_STRING(a) \
1708 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1709 != sizeof(i_size) ) goto error; \
1710 if( i_size && i_size < 16384 ) { \
1711 a = malloc( i_size ); \
1712 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1714 if( a[i_size-1] ) { \
1721 for( i = 0; i < i_cache; i++ )
1726 pp_cache[i] = malloc( sizeof(module_cache_t) );
1727 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1729 /* Load common info */
1730 LOAD_STRING( pp_cache[i]->psz_file );
1731 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1732 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1733 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1735 if( pp_cache[i]->b_junk ) continue;
1737 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1739 /* Load additional infos */
1740 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1741 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1742 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1743 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1744 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1746 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1748 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1749 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1750 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1751 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1752 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1753 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1756 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1759 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1761 LOAD_IMMEDIATE( i_submodules );
1763 while( i_submodules-- )
1765 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1766 vlc_object_attach( p_module, pp_cache[i]->p_module );
1767 p_module->b_submodule = VLC_TRUE;
1769 LOAD_STRING( p_module->psz_object_name );
1770 LOAD_STRING( p_module->psz_shortname );
1771 LOAD_STRING( p_module->psz_longname );
1772 LOAD_STRING( p_module->psz_program );
1773 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1775 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1777 LOAD_STRING( p_module->psz_capability );
1778 LOAD_IMMEDIATE( p_module->i_score );
1779 LOAD_IMMEDIATE( p_module->i_cpu );
1780 LOAD_IMMEDIATE( p_module->b_unloadable );
1781 LOAD_IMMEDIATE( p_module->b_reentrant );
1790 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1793 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1799 int CacheLoadConfig( module_t *p_module, FILE *file )
1804 /* Calculate the structure length */
1805 LOAD_IMMEDIATE( p_module->i_config_items );
1806 LOAD_IMMEDIATE( p_module->i_bool_items );
1808 LOAD_IMMEDIATE( i_lines );
1810 /* Allocate memory */
1811 p_module->p_config =
1812 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1813 if( p_module->p_config == NULL )
1815 msg_Err( p_module, "config error: can't duplicate p_config" );
1819 /* Do the duplication job */
1820 for( i = 0; i < i_lines ; i++ )
1822 LOAD_IMMEDIATE( p_module->p_config[i] );
1824 LOAD_STRING( p_module->p_config[i].psz_type );
1825 LOAD_STRING( p_module->p_config[i].psz_name );
1826 LOAD_STRING( p_module->p_config[i].psz_text );
1827 LOAD_STRING( p_module->p_config[i].psz_longtext );
1828 LOAD_STRING( p_module->p_config[i].psz_current );
1829 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1831 p_module->p_config[i].psz_value =
1832 p_module->p_config[i].psz_value_orig ?
1833 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1834 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1835 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1836 p_module->p_config[i].i_value_saved = p_module->p_config[i].i_value;
1837 p_module->p_config[i].f_value_saved = p_module->p_config[i].f_value;
1838 p_module->p_config[i].psz_value_saved = 0;
1839 p_module->p_config[i].b_dirty = VLC_FALSE;
1841 p_module->p_config[i].p_lock = &p_module->object_lock;
1843 if( p_module->p_config[i].i_list )
1845 if( p_module->p_config[i].ppsz_list )
1847 p_module->p_config[i].ppsz_list =
1848 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1849 if( p_module->p_config[i].ppsz_list )
1851 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1852 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1853 p_module->p_config[i].ppsz_list[j] = NULL;
1856 if( p_module->p_config[i].ppsz_list_text )
1858 p_module->p_config[i].ppsz_list_text =
1859 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1860 if( p_module->p_config[i].ppsz_list_text )
1862 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1863 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1864 p_module->p_config[i].ppsz_list_text[j] = NULL;
1867 if( p_module->p_config[i].pi_list )
1869 p_module->p_config[i].pi_list =
1870 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1871 if( p_module->p_config[i].pi_list )
1873 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1874 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1879 if( p_module->p_config[i].i_action )
1881 p_module->p_config[i].ppf_action =
1882 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1883 p_module->p_config[i].ppsz_action_text =
1884 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1886 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1888 p_module->p_config[i].ppf_action[j] = 0;
1889 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1893 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1896 p_module->p_config[i].i_type = CONFIG_HINT_END;
1902 return VLC_EGENERIC;
1905 /*****************************************************************************
1906 * SavePluginsCache: saves the plugins cache to a file
1907 *****************************************************************************/
1908 static void CacheSave( vlc_object_t *p_this )
1910 static char const psz_tag[] =
1911 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1912 "# This file is a cache directory tag created by VLC.\r\n"
1913 "# For information about cache directory tags, see:\r\n"
1914 "# http://www.brynosaurus.com/cachedir/\r\n";
1916 char *psz_filename, *psz_homedir;
1919 module_cache_t **pp_cache;
1920 int32_t i_file_size = 0;
1922 psz_homedir = p_this->p_vlc->psz_homedir;
1925 msg_Err( p_this, "psz_homedir is null" );
1929 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1930 strlen(psz_homedir) + strlen(CacheName()) );
1934 msg_Err( p_this, "out of memory" );
1938 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1940 config_CreateDir( p_this, psz_filename );
1942 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1944 config_CreateDir( p_this, psz_filename );
1946 strcat( psz_filename, "/CACHEDIR.TAG" );
1948 file = fopen( psz_filename, "wb" );
1951 fwrite( psz_tag, 1, strlen(psz_tag), file );
1955 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1956 PLUGINSCACHE_DIR, CacheName() );
1958 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1960 file = fopen( psz_filename, "wb" );
1963 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1965 free( psz_filename );
1968 free( psz_filename );
1970 /* Empty space for file size */
1971 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1973 /* Contains version number */
1974 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1976 /* Sub-version number (to avoid breakage in the dev version when cache
1977 * structure changes) */
1978 i_file_size = CACHE_SUBVERSION_NUM;
1979 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1982 fprintf( file, "%5.5s", _("C") );
1985 i_file_size = ftell( file );
1986 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1988 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1989 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1991 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1993 #define SAVE_IMMEDIATE(a) \
1994 fwrite( &a, sizeof(char), sizeof(a), file )
1995 #define SAVE_STRING(a) \
1996 { i_size = a ? strlen( a ) + 1 : 0; \
1997 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1998 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2001 for( i = 0; i < i_cache; i++ )
2004 int32_t i_submodule;
2006 /* Save common info */
2007 SAVE_STRING( pp_cache[i]->psz_file );
2008 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2009 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2010 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2012 if( pp_cache[i]->b_junk ) continue;
2014 /* Save additional infos */
2015 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2016 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2017 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2018 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2019 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2021 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2023 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2024 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2025 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2026 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2027 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2028 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2031 CacheSaveConfig( pp_cache[i]->p_module, file );
2033 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2035 i_submodule = pp_cache[i]->p_module->i_children;
2036 SAVE_IMMEDIATE( i_submodule );
2037 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2040 module_t *p_module =
2041 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2043 SAVE_STRING( p_module->psz_object_name );
2044 SAVE_STRING( p_module->psz_shortname );
2045 SAVE_STRING( p_module->psz_longname );
2046 SAVE_STRING( p_module->psz_program );
2047 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2049 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2051 SAVE_STRING( p_module->psz_capability );
2052 SAVE_IMMEDIATE( p_module->i_score );
2053 SAVE_IMMEDIATE( p_module->i_cpu );
2054 SAVE_IMMEDIATE( p_module->b_unloadable );
2055 SAVE_IMMEDIATE( p_module->b_reentrant );
2059 /* Fill-up file size */
2060 i_file_size = ftell( file );
2061 fseek( file, 0, SEEK_SET );
2062 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2069 void CacheSaveConfig( module_t *p_module, FILE *file )
2071 int i, j, i_lines = 0;
2072 module_config_t *p_item;
2075 SAVE_IMMEDIATE( p_module->i_config_items );
2076 SAVE_IMMEDIATE( p_module->i_bool_items );
2078 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2079 p_item++ ) i_lines++;
2081 SAVE_IMMEDIATE( i_lines );
2083 for( i = 0; i < i_lines ; i++ )
2085 SAVE_IMMEDIATE( p_module->p_config[i] );
2087 SAVE_STRING( p_module->p_config[i].psz_type );
2088 SAVE_STRING( p_module->p_config[i].psz_name );
2089 SAVE_STRING( p_module->p_config[i].psz_text );
2090 SAVE_STRING( p_module->p_config[i].psz_longtext );
2091 SAVE_STRING( p_module->p_config[i].psz_current );
2092 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2094 if( p_module->p_config[i].i_list )
2096 if( p_module->p_config[i].ppsz_list )
2098 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2099 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2102 if( p_module->p_config[i].ppsz_list_text )
2104 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2105 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2107 if( p_module->p_config[i].pi_list )
2109 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2110 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2114 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2115 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2117 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2121 /*****************************************************************************
2122 * CacheName: Return the cache file name for this platform.
2123 *****************************************************************************/
2124 static char *CacheName( void )
2126 static char psz_cachename[32];
2128 /* Code int size, pointer size and endianness in the filename */
2129 int32_t x = 0xbe00001e;
2130 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2131 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2132 return psz_cachename;
2135 /*****************************************************************************
2136 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2137 *****************************************************************************/
2138 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2139 module_t *p_module )
2143 p_cache->pf_activate = p_module->pf_activate;
2144 p_cache->pf_deactivate = p_module->pf_deactivate;
2145 #ifndef HAVE_SHARED_LIBVLC
2146 p_cache->p_symbols = p_module->p_symbols;
2148 p_cache->handle = p_module->handle;
2150 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2152 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2153 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2154 p_cchild->pf_activate = p_child->pf_activate;
2155 p_cchild->pf_deactivate = p_child->pf_deactivate;
2156 #ifndef HAVE_SHARED_LIBVLC
2157 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 */