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 #include "vlc_update.h"
114 #if defined( _MSC_VER ) && defined( UNDER_CE )
115 # include "modules_builtin_evc.h"
116 #elif defined( _MSC_VER )
117 # include "modules_builtin_msvc.h"
119 # include "modules_builtin.h"
123 #if defined( WIN32 ) || defined( UNDER_CE )
124 /* Avoid name collisions */
125 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
128 /*****************************************************************************
130 *****************************************************************************/
131 #ifdef HAVE_DYNAMIC_PLUGINS
132 static void AllocateAllPlugins ( vlc_object_t * );
133 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
134 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
135 static module_t * AllocatePlugin( vlc_object_t *, char * );
137 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
138 static int DeleteModule ( module_t * );
139 #ifdef HAVE_DYNAMIC_PLUGINS
140 static void DupModule ( module_t * );
141 static void UndupModule ( module_t * );
142 static int CallEntry ( module_t * );
143 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
144 static void CloseModule ( module_handle_t );
145 static void * GetSymbol ( module_handle_t, const char * );
146 static void CacheLoad ( vlc_object_t * );
147 static int CacheLoadConfig ( module_t *, FILE * );
148 static void CacheSave ( vlc_object_t * );
149 static void CacheSaveConfig ( module_t *, FILE * );
150 static char * CacheName ( void );
151 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
152 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
154 #if defined(HAVE_DL_WINDOWS)
155 static char * GetWindowsError ( void );
160 /* Sub-version number
161 * (only used to avoid breakage in dev version when cache structure changes) */
162 #define CACHE_SUBVERSION_NUM 1
164 /*****************************************************************************
165 * module_InitBank: create the module bank.
166 *****************************************************************************
167 * This function creates a module bank structure which will be filled later
168 * on with all the modules found.
169 *****************************************************************************/
170 void __module_InitBank( vlc_object_t *p_this )
172 module_bank_t *p_bank;
175 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
176 var_Get( p_this->p_libvlc, "libvlc", &lockval );
177 vlc_mutex_lock( lockval.p_address );
178 if( p_this->p_libvlc->p_module_bank )
180 p_this->p_libvlc->p_module_bank->i_usage++;
181 vlc_mutex_unlock( lockval.p_address );
182 var_Destroy( p_this->p_libvlc, "libvlc" );
185 vlc_mutex_unlock( lockval.p_address );
186 var_Destroy( p_this->p_libvlc, "libvlc" );
188 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
189 p_bank->psz_object_name = "module bank";
191 p_bank->i_cache = p_bank->i_loaded_cache = 0;
192 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
193 p_bank->b_cache = p_bank->b_cache_dirty =
194 p_bank->b_cache_delete = VLC_FALSE;
197 * Store the symbols to be exported
199 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
200 STORE_SYMBOLS( &p_bank->symbols );
203 /* Everything worked, attach the object */
204 p_this->p_libvlc->p_module_bank = p_bank;
205 vlc_object_attach( p_bank, p_this->p_libvlc );
207 module_LoadMain( p_this );
212 /*****************************************************************************
213 * module_ResetBank: reset the module bank.
214 *****************************************************************************
215 * This function resets the module bank by unloading all unused plugin
217 *****************************************************************************/
218 void __module_ResetBank( vlc_object_t *p_this )
220 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
224 /*****************************************************************************
225 * module_EndBank: empty the module bank.
226 *****************************************************************************
227 * This function unloads all unused plugin modules and empties the module
228 * bank in case of success.
229 *****************************************************************************/
230 void __module_EndBank( vlc_object_t *p_this )
235 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
236 var_Get( p_this->p_libvlc, "libvlc", &lockval );
237 vlc_mutex_lock( lockval.p_address );
238 if( !p_this->p_libvlc->p_module_bank )
240 vlc_mutex_unlock( lockval.p_address );
241 var_Destroy( p_this->p_libvlc, "libvlc" );
244 if( --p_this->p_libvlc->p_module_bank->i_usage )
246 vlc_mutex_unlock( lockval.p_address );
247 var_Destroy( p_this->p_libvlc, "libvlc" );
250 vlc_mutex_unlock( lockval.p_address );
251 var_Destroy( p_this->p_libvlc, "libvlc" );
253 config_AutoSaveConfigFile( p_this );
255 #ifdef HAVE_DYNAMIC_PLUGINS
256 #define p_bank p_this->p_libvlc->p_module_bank
257 if( p_bank->b_cache ) CacheSave( p_this );
258 while( p_bank->i_loaded_cache-- )
260 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
261 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
263 if( p_bank->pp_loaded_cache )
264 free( p_bank->pp_loaded_cache );
266 while( p_bank->i_cache-- )
268 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
269 free( p_bank->pp_cache[p_bank->i_cache] );
271 if( p_bank->pp_cache )
272 free( p_bank->pp_cache );
276 vlc_object_detach( p_this->p_libvlc->p_module_bank );
278 while( p_this->p_libvlc->p_module_bank->i_children )
280 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
282 if( DeleteModule( p_next ) )
284 /* Module deletion failed */
285 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
286 p_next->psz_object_name );
288 /* We just free the module by hand. Niahahahahaha. */
289 vlc_object_detach( p_next );
290 vlc_object_destroy( p_next );
294 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
295 p_this->p_libvlc->p_module_bank = NULL;
300 /*****************************************************************************
301 * module_LoadMain: load the main program info into the module bank.
302 *****************************************************************************
303 * This function fills the module bank structure with the main module infos.
304 * This is very useful as it will allow us to consider the main program just
305 * as another module, and for instance the configuration options of main will
306 * be available in the module bank structure just as for every other module.
307 *****************************************************************************/
308 void __module_LoadMain( vlc_object_t *p_this )
312 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
313 var_Get( p_this->p_libvlc, "libvlc", &lockval );
314 vlc_mutex_lock( lockval.p_address );
315 if( p_this->p_libvlc->p_module_bank->b_main )
317 vlc_mutex_unlock( lockval.p_address );
318 var_Destroy( p_this->p_libvlc, "libvlc" );
321 p_this->p_libvlc->p_module_bank->b_main = VLC_TRUE;
322 vlc_mutex_unlock( lockval.p_address );
323 var_Destroy( p_this->p_libvlc, "libvlc" );
325 AllocateBuiltinModule( p_this, vlc_entry__main );
328 /*****************************************************************************
329 * module_LoadBuiltins: load all modules which we built with.
330 *****************************************************************************
331 * This function fills the module bank structure with the builtin modules.
332 *****************************************************************************/
333 void __module_LoadBuiltins( vlc_object_t * p_this )
337 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
338 var_Get( p_this->p_libvlc, "libvlc", &lockval );
339 vlc_mutex_lock( lockval.p_address );
340 if( p_this->p_libvlc->p_module_bank->b_builtins )
342 vlc_mutex_unlock( lockval.p_address );
343 var_Destroy( p_this->p_libvlc, "libvlc" );
346 p_this->p_libvlc->p_module_bank->b_builtins = VLC_TRUE;
347 vlc_mutex_unlock( lockval.p_address );
348 var_Destroy( p_this->p_libvlc, "libvlc" );
350 msg_Dbg( p_this, "checking builtin modules" );
351 ALLOCATE_ALL_BUILTINS();
354 /*****************************************************************************
355 * module_LoadPlugins: load all plugin modules we can find.
356 *****************************************************************************
357 * This function fills the module bank structure with the plugin modules.
358 *****************************************************************************/
359 void __module_LoadPlugins( vlc_object_t * p_this )
361 #ifdef HAVE_DYNAMIC_PLUGINS
364 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
365 var_Get( p_this->p_libvlc, "libvlc", &lockval );
366 vlc_mutex_lock( lockval.p_address );
367 if( p_this->p_libvlc->p_module_bank->b_plugins )
369 vlc_mutex_unlock( lockval.p_address );
370 var_Destroy( p_this->p_libvlc, "libvlc" );
373 p_this->p_libvlc->p_module_bank->b_plugins = VLC_TRUE;
374 vlc_mutex_unlock( lockval.p_address );
375 var_Destroy( p_this->p_libvlc, "libvlc" );
377 msg_Dbg( p_this, "checking plugin modules" );
379 if( config_GetInt( p_this, "plugins-cache" ) )
380 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
382 if( p_this->p_libvlc->p_module_bank->b_cache ||
383 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
385 AllocateAllPlugins( p_this );
389 /*****************************************************************************
390 * module_Need: return the best module function, given a capability list.
391 *****************************************************************************
392 * This function returns the module that best fits the asked capabilities.
393 *****************************************************************************/
394 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
395 const char *psz_name, vlc_bool_t b_strict )
397 typedef struct module_list_t module_list_t;
404 module_list_t *p_next;
407 module_list_t *p_list, *p_first, *p_tmp;
410 int i_which_module, i_index = 0;
411 vlc_bool_t b_intf = VLC_FALSE;
416 char *psz_shortcuts = NULL, *psz_var = NULL;
417 vlc_bool_t b_force_backup = p_this->b_force;
420 /* Deal with variables */
421 if( psz_name && psz_name[0] == '$' )
424 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
425 var_Get( p_this, psz_name + 1, &val );
426 psz_var = val.psz_string;
430 /* Count how many different shortcuts were asked for */
431 if( psz_name && *psz_name )
433 char *psz_parser, *psz_last_shortcut;
435 /* If the user wants none, give him none. */
436 if( !strcmp( psz_name, "none" ) )
438 if( psz_var ) free( psz_var );
443 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
445 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
447 if( *psz_parser == ',' )
451 psz_last_shortcut = psz_parser + 1;
455 /* Check if the user wants to override the "strict" mode */
456 if( psz_last_shortcut )
458 if( !strcmp(psz_last_shortcut, "none") )
463 else if( !strcmp(psz_last_shortcut, "any") )
465 b_strict = VLC_FALSE;
471 /* Sort the modules and test them */
472 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
473 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
476 /* Parse the module list for capabilities and probe each of them */
477 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
479 int i_shortcut_bonus = 0;
481 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
483 /* Test that this module can do what we need */
484 if( strcmp( p_module->psz_capability, psz_capability ) )
486 /* Don't recurse through the sub-modules because vlc_list_find()
487 * will list them anyway. */
491 /* Test if we have the required CPU */
492 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
497 /* If we required a shortcut, check this plugin provides it. */
498 if( i_shortcuts > 0 )
501 int i_dummy, i_short = i_shortcuts;
502 char *psz_name = psz_shortcuts;
504 /* Let's drop modules with a <= 0 score (unless they are
505 * explicitly requested) */
506 b_trash = p_module->i_score <= 0;
510 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
512 if( !strcasecmp( psz_name,
513 p_module->pp_shortcuts[i_dummy] ) )
517 i_shortcut_bonus = i_short * 10000;
522 if( i_shortcut_bonus )
524 /* We found it... remember ? */
528 /* Go to the next shortcut... This is so lame! */
537 /* If we are in "strict" mode and we couldn't
538 * find the module in the list of provided shortcuts,
539 * then kick the bastard out of here!!! */
540 if( i_short == 0 && b_strict )
550 /* If we didn't require a shortcut, trash <= 0 scored plugins */
551 else if( p_module->i_score <= 0 )
556 /* Special case: test if we requested a particular intf plugin */
557 if( !i_shortcuts && p_module->psz_program
558 && !strcmp( psz_capability, "interface" )
559 && !strcmp( p_module->psz_program,
560 p_this->p_vlc->psz_object_name ) )
564 /* Remove previous non-matching plugins */
571 /* This one doesn't match */
575 /* Store this new module */
576 p_list[ i_index ].p_module = p_module;
577 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
578 p_list[ i_index ].b_force = !!i_shortcut_bonus;
580 /* Add it to the modules-to-probe list */
583 p_list[ 0 ].p_next = NULL;
588 /* Ok, so at school you learned that quicksort is quick, and
589 * bubble sort sucks raw eggs. But that's when dealing with
590 * thousands of items. Here we have barely 50. */
591 module_list_t *p_newlist = p_first;
593 if( p_first->i_score < p_list[ i_index ].i_score )
595 p_list[ i_index ].p_next = p_first;
596 p_first = &p_list[ i_index ];
600 while( p_newlist->p_next != NULL &&
601 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
603 p_newlist = p_newlist->p_next;
606 p_list[ i_index ].p_next = p_newlist->p_next;
607 p_newlist->p_next = &p_list[ i_index ];
614 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
615 i_index, i_index == 1 ? "" : "s" );
617 /* Lock all candidate modules */
619 while( p_tmp != NULL )
621 vlc_object_yield( p_tmp->p_module );
622 p_tmp = p_tmp->p_next;
625 /* We can release the list, interesting modules were yielded */
626 vlc_list_release( p_all );
628 /* Parse the linked list and use the first successful module */
630 while( p_tmp != NULL )
632 #ifdef HAVE_DYNAMIC_PLUGINS
633 /* Make sure the module is loaded in mem */
634 module_t *p_module = p_tmp->p_module->b_submodule ?
635 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
636 if( !p_module->b_builtin && !p_module->b_loaded )
638 module_t *p_new_module =
639 AllocatePlugin( p_this, p_module->psz_filename );
642 CacheMerge( p_this, p_module, p_new_module );
643 vlc_object_attach( p_new_module, p_module );
644 DeleteModule( p_new_module );
649 p_this->b_force = p_tmp->b_force;
650 if( p_tmp->p_module->pf_activate
651 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
656 vlc_object_release( p_tmp->p_module );
657 p_tmp = p_tmp->p_next;
660 /* Store the locked module value */
663 p_module = p_tmp->p_module;
664 p_tmp = p_tmp->p_next;
671 /* Unlock the remaining modules */
672 while( p_tmp != NULL )
674 vlc_object_release( p_tmp->p_module );
675 p_tmp = p_tmp->p_next;
679 p_this->b_force = b_force_backup;
681 if( p_module != NULL )
683 msg_Dbg( p_this, "using %s module \"%s\"",
684 psz_capability, p_module->psz_object_name );
686 else if( p_first == NULL )
688 if( !strcmp( psz_capability, "access_demux" ) )
690 msg_Warn( p_this, "no %s module matched \"%s\"",
691 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
695 msg_Err( p_this, "no %s module matched \"%s\"",
696 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
699 else if( psz_name != NULL && *psz_name )
701 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
702 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
707 free( psz_shortcuts );
715 /* Don't forget that the module is still locked */
719 /*****************************************************************************
720 * module_Unneed: decrease the usage count of a module.
721 *****************************************************************************
722 * This function must be called by the thread that called module_Need, to
723 * decrease the reference count and allow for hiding of modules.
724 *****************************************************************************/
725 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
727 /* Use the close method */
728 if( p_module->pf_deactivate )
730 p_module->pf_deactivate( p_this );
733 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
735 vlc_object_release( p_module );
740 /*****************************************************************************
741 * Following functions are local.
742 *****************************************************************************/
744 /*****************************************************************************
745 * AllocateAllPlugins: load all plugin modules we can find.
746 *****************************************************************************/
747 #ifdef HAVE_DYNAMIC_PLUGINS
748 static void AllocateAllPlugins( vlc_object_t *p_this )
750 /* Yes, there are two NULLs because we replace one with "plugin-path". */
751 #if defined( WIN32 ) || defined( UNDER_CE )
752 char *path[] = { "modules", "", "plugins", 0, 0 };
754 char *path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
757 char **ppsz_path = path;
760 /* If the user provided a plugin path, we add it to the list */
761 path[ sizeof(path)/sizeof(char*) - 2 ] =
762 config_GetPsz( p_this, "plugin-path" );
764 for( ; *ppsz_path != NULL ; ppsz_path++ )
766 if( !(*ppsz_path)[0] ) continue;
768 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
770 /* Handle relative as well as absolute paths */
772 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
773 (*ppsz_path)[1] != ':' )
775 if( (*ppsz_path)[0] != '/' )
778 int i_dirlen = strlen( *ppsz_path );
779 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
781 psz_fullpath = malloc( i_dirlen );
782 if( psz_fullpath == NULL )
787 sprintf( psz_fullpath, "%s\\%s",
788 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
790 sprintf( psz_fullpath, "%s/%s",
791 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
797 psz_fullpath = strdup( *ppsz_path );
800 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
802 /* Don't go deeper than 5 subdirectories */
803 AllocatePluginDir( p_this, psz_fullpath, 5 );
805 free( psz_fullpath );
808 /* Free plugin-path */
809 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
810 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
811 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
814 /*****************************************************************************
815 * AllocatePluginDir: recursively parse a directory to look for plugins
816 *****************************************************************************/
817 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
820 #if defined( UNDER_CE ) || defined( _MSC_VER )
822 wchar_t psz_wpath[MAX_PATH + 256];
823 wchar_t psz_wdir[MAX_PATH];
825 char psz_path[MAX_PATH + 256];
826 WIN32_FIND_DATA finddata;
832 struct dirent * file;
836 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
841 #if defined( UNDER_CE ) || defined( _MSC_VER )
843 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
845 rc = GetFileAttributes( psz_wdir );
846 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
848 /* Parse all files in the directory */
849 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
851 rc = GetFileAttributes( psz_dir );
852 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
855 /* Parse all files in the directory */
856 sprintf( psz_path, "%s\\*", psz_dir );
859 handle = FindFirstFile( psz_wpath, &finddata );
861 handle = FindFirstFile( psz_path, &finddata );
863 if( handle == INVALID_HANDLE_VALUE )
865 /* Empty directory */
869 /* Parse the directory and try to load all files it contains. */
873 unsigned int i_len = wcslen( finddata.cFileName );
874 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
875 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
877 unsigned int i_len = strlen( finddata.cFileName );
878 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
881 /* Skip ".", ".." and anything starting with "." */
882 if( !*finddata.cFileName || *finddata.cFileName == '.' )
884 if( !FindNextFile( handle, &finddata ) ) break;
889 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
891 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
894 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
896 else if( i_len > strlen( LIBEXT )
897 /* We only load files ending with LIBEXT */
898 && !strncasecmp( psz_path + strlen( psz_path)
900 LIBEXT, strlen( LIBEXT ) ) )
902 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
903 int64_t i_time = 0, i_size = 0;
906 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
909 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
913 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
915 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
916 i_size = attrbuf.nFileSizeHigh;
918 i_size |= attrbuf.nFileSizeLow;
922 AllocatePluginFile( p_this, psz_file, i_time, i_size );
925 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
927 /* Close the directory */
931 dir = opendir( psz_dir );
937 i_dirlen = strlen( psz_dir );
939 /* Parse the directory and try to load all files it contains. */
940 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
946 /* Skip ".", ".." and anything starting with "." */
947 if( !*file->d_name || *file->d_name == '.' )
952 i_len = strlen( file->d_name );
953 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
955 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
957 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
960 i_stat = stat( psz_file, &statbuf );
961 if( !i_stat && statbuf.st_mode & S_IFDIR )
963 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
965 else if( i_len > strlen( LIBEXT )
966 /* We only load files ending with LIBEXT */
967 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
968 LIBEXT, strlen( LIBEXT ) ) )
970 int64_t i_time = 0, i_size = 0;
974 i_time = statbuf.st_mtime;
975 i_size = statbuf.st_size;
978 AllocatePluginFile( p_this, psz_file, i_time, i_size );
984 /* Close the directory */
990 /*****************************************************************************
991 * AllocatePluginFile: load a module into memory and initialize it.
992 *****************************************************************************
993 * This function loads a dynamically loadable module and allocates a structure
994 * for its information data. The module can then be handled by module_Need
995 * and module_Unneed. It can be removed by DeleteModule.
996 *****************************************************************************/
997 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
998 int64_t i_file_time, int64_t i_file_size )
1000 module_t * p_module;
1001 module_cache_t *p_cache_entry = NULL;
1004 * Check our plugins cache first then load plugin if needed
1007 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1009 if( !p_cache_entry )
1011 p_module = AllocatePlugin( p_this, psz_file );
1015 /* If junk dll, don't try to load it */
1016 if( p_cache_entry->b_junk )
1022 module_config_t *p_item;
1024 p_module = p_cache_entry->p_module;
1025 p_module->b_loaded = VLC_FALSE;
1027 /* For now we force loading if the module's config contains
1028 * callbacks or actions.
1029 * Could be optimized by adding an API call.*/
1030 for( p_item = p_module->p_config;
1031 p_item->i_type != CONFIG_HINT_END; p_item++ )
1033 if( p_item->pf_callback || p_item->i_action )
1034 p_module = AllocatePlugin( p_this, psz_file );
1041 /* Everything worked fine !
1042 * The module is ready to be added to the list. */
1043 p_module->b_builtin = VLC_FALSE;
1045 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1046 p_module->psz_object_name, p_module->psz_longname ); */
1048 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1051 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
1053 /* Add entry to cache */
1054 #define p_bank p_this->p_libvlc->p_module_bank
1056 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1057 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1058 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1059 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1060 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1061 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1062 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1065 return p_module ? 0 : -1;
1068 /*****************************************************************************
1069 * AllocatePlugin: load a module into memory and initialize it.
1070 *****************************************************************************
1071 * This function loads a dynamically loadable module and allocates a structure
1072 * for its information data. The module can then be handled by module_Need
1073 * and module_Unneed. It can be removed by DeleteModule.
1074 *****************************************************************************/
1075 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1077 module_t * p_module;
1078 module_handle_t handle;
1080 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1082 /* Now that we have successfully loaded the module, we can
1083 * allocate a structure for it */
1084 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1085 if( p_module == NULL )
1087 msg_Err( p_this, "out of memory" );
1088 CloseModule( handle );
1092 /* We need to fill these since they may be needed by CallEntry() */
1093 p_module->psz_filename = psz_file;
1094 p_module->handle = handle;
1095 #ifndef HAVE_SHARED_LIBVLC
1096 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1098 p_module->b_loaded = VLC_TRUE;
1100 /* Initialize the module: fill p_module, default config */
1101 if( CallEntry( p_module ) != 0 )
1103 /* We couldn't call module_init() */
1104 vlc_object_destroy( p_module );
1105 CloseModule( handle );
1109 DupModule( p_module );
1110 p_module->psz_filename = strdup( p_module->psz_filename );
1112 /* Everything worked fine ! The module is ready to be added to the list. */
1113 p_module->b_builtin = VLC_FALSE;
1118 /*****************************************************************************
1119 * DupModule: make a plugin module standalone.
1120 *****************************************************************************
1121 * This function duplicates all strings in the module, so that the dynamic
1122 * object can be unloaded. It acts recursively on submodules.
1123 *****************************************************************************/
1124 static void DupModule( module_t *p_module )
1129 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1131 *pp_shortcut = strdup( *pp_shortcut );
1134 /* We strdup() these entries so that they are still valid when the
1135 * module is unloaded. */
1136 p_module->psz_object_name = strdup( p_module->psz_object_name );
1137 p_module->psz_capability = strdup( p_module->psz_capability );
1138 p_module->psz_shortname = p_module->psz_shortname ?
1139 strdup( p_module->psz_shortname ) : NULL;
1140 p_module->psz_longname = strdup( p_module->psz_longname );
1142 if( p_module->psz_program != NULL )
1144 p_module->psz_program = strdup( p_module->psz_program );
1147 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1149 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1153 /*****************************************************************************
1154 * UndupModule: free a duplicated module.
1155 *****************************************************************************
1156 * This function frees the allocations done in DupModule().
1157 *****************************************************************************/
1158 static void UndupModule( module_t *p_module )
1163 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1165 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1168 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1170 free( *pp_shortcut );
1173 free( p_module->psz_object_name );
1174 free( p_module->psz_capability );
1175 if( p_module->psz_shortname ) free( p_module->psz_shortname );
1176 free( p_module->psz_longname );
1178 if( p_module->psz_program != NULL )
1180 free( p_module->psz_program );
1184 #endif /* HAVE_DYNAMIC_PLUGINS */
1186 /*****************************************************************************
1187 * AllocateBuiltinModule: initialize a builtin module.
1188 *****************************************************************************
1189 * This function registers a builtin module and allocates a structure
1190 * for its information data. The module can then be handled by module_Need
1191 * and module_Unneed. It can be removed by DeleteModule.
1192 *****************************************************************************/
1193 static int AllocateBuiltinModule( vlc_object_t * p_this,
1194 int ( *pf_entry ) ( module_t * ) )
1196 module_t * p_module;
1198 /* Now that we have successfully loaded the module, we can
1199 * allocate a structure for it */
1200 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1201 if( p_module == NULL )
1203 msg_Err( p_this, "out of memory" );
1207 /* Initialize the module : fill p_module->psz_object_name, etc. */
1208 if( pf_entry( p_module ) != 0 )
1210 /* With a well-written module we shouldn't have to print an
1211 * additional error message here, but just make sure. */
1212 msg_Err( p_this, "failed calling entry point in builtin module" );
1213 vlc_object_destroy( p_module );
1217 /* Everything worked fine ! The module is ready to be added to the list. */
1218 p_module->b_builtin = VLC_TRUE;
1220 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1221 p_module->psz_object_name, p_module->psz_longname ); */
1223 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1228 /*****************************************************************************
1229 * DeleteModule: delete a module and its structure.
1230 *****************************************************************************
1231 * This function can only be called if the module isn't being used.
1232 *****************************************************************************/
1233 static int DeleteModule( module_t * p_module )
1235 vlc_object_detach( p_module );
1237 /* We free the structures that we strdup()ed in Allocate*Module(). */
1238 #ifdef HAVE_DYNAMIC_PLUGINS
1239 if( !p_module->b_builtin )
1241 if( p_module->b_loaded && p_module->b_unloadable )
1243 CloseModule( p_module->handle );
1245 UndupModule( p_module );
1246 free( p_module->psz_filename );
1250 /* Free and detach the object's children */
1251 while( p_module->i_children )
1253 vlc_object_t *p_this = p_module->pp_children[0];
1254 vlc_object_detach( p_this );
1255 vlc_object_destroy( p_this );
1258 config_Free( p_module );
1259 vlc_object_destroy( p_module );
1264 #ifdef HAVE_DYNAMIC_PLUGINS
1265 /*****************************************************************************
1266 * CallEntry: call an entry point.
1267 *****************************************************************************
1268 * This function calls a symbol given its name and a module structure. The
1269 * symbol MUST refer to a function returning int and taking a module_t* as
1271 *****************************************************************************/
1272 static int CallEntry( module_t * p_module )
1274 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1275 int (* pf_symbol) ( module_t * p_module );
1277 /* Try to resolve the symbol */
1278 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1280 if( pf_symbol == NULL )
1282 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1283 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1284 psz_name, p_module->psz_filename );
1285 #elif defined(HAVE_DL_WINDOWS)
1286 char *psz_error = GetWindowsError();
1287 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1288 psz_name, p_module->psz_filename, psz_error );
1290 #elif defined(HAVE_DL_DLOPEN)
1291 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1292 psz_name, p_module->psz_filename, dlerror() );
1293 #elif defined(HAVE_DL_SHL_LOAD)
1294 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1295 psz_name, p_module->psz_filename, strerror(errno) );
1297 # error "Something is wrong in modules.c"
1302 /* We can now try to call the symbol */
1303 if( pf_symbol( p_module ) != 0 )
1305 /* With a well-written module we shouldn't have to print an
1306 * additional error message here, but just make sure. */
1307 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1308 psz_name, p_module->psz_filename );
1312 /* Everything worked fine, we can return */
1316 /*****************************************************************************
1317 * LoadModule: loads a dynamic library
1318 *****************************************************************************
1319 * This function loads a dynamically linked library using a system dependant
1320 * method. Will return 0 on success as well as the module handle.
1321 *****************************************************************************/
1322 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1323 module_handle_t *p_handle )
1325 module_handle_t handle;
1327 #if defined(HAVE_DL_DYLD)
1328 NSObjectFileImage image;
1329 NSObjectFileImageReturnCode ret;
1331 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1333 if( ret != NSObjectFileImageSuccess )
1335 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1339 /* Open the dynamic module */
1340 handle = NSLinkModule( image, psz_file,
1341 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1345 NSLinkEditErrors errors;
1346 const char *psz_file, *psz_err;
1348 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1349 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1350 NSDestroyObjectFileImage( image );
1354 /* Destroy our image, we won't need it */
1355 NSDestroyObjectFileImage( image );
1357 #elif defined(HAVE_DL_BEOS)
1358 handle = load_add_on( psz_file );
1361 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1365 #elif defined(HAVE_DL_WINDOWS)
1368 wchar_t psz_wfile[MAX_PATH];
1369 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1370 handle = LoadLibrary( psz_wfile );
1373 handle = LoadLibrary( psz_file );
1375 if( handle == NULL )
1377 char *psz_err = GetWindowsError();
1378 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1383 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1384 /* static is OK, we are called atomically */
1386 # if defined(SYS_LINUX)
1387 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1388 * are going to get namespace collisions when two modules have common
1389 * public symbols, but ALSA is being a pest here. */
1390 if( strstr( psz_file, "alsa_plugin" ) )
1392 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1393 if( handle == NULL )
1395 msg_Warn( p_this, "cannot load module `%s' (%s)",
1396 psz_file, dlerror() );
1402 handle = dlopen( psz_file, RTLD_NOW );
1403 if( handle == NULL )
1405 msg_Warn( p_this, "cannot load module `%s' (%s)",
1406 psz_file, dlerror() );
1410 #elif defined(HAVE_DL_DLOPEN)
1411 # if defined(DL_LAZY)
1412 handle = dlopen( psz_file, DL_LAZY );
1414 handle = dlopen( psz_file, 0 );
1416 if( handle == NULL )
1418 msg_Warn( p_this, "cannot load module `%s' (%s)",
1419 psz_file, dlerror() );
1423 #elif defined(HAVE_DL_SHL_LOAD)
1424 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1425 if( handle == NULL )
1427 msg_Warn( p_this, "cannot load module `%s' (%s)",
1428 psz_file, strerror(errno) );
1433 # error "Something is wrong in modules.c"
1441 /*****************************************************************************
1442 * CloseModule: unload a dynamic library
1443 *****************************************************************************
1444 * This function unloads a previously opened dynamically linked library
1445 * using a system dependant method. No return value is taken in consideration,
1446 * since some libraries sometimes refuse to close properly.
1447 *****************************************************************************/
1448 static void CloseModule( module_handle_t handle )
1450 #if defined(HAVE_DL_DYLD)
1451 NSUnLinkModule( handle, FALSE );
1453 #elif defined(HAVE_DL_BEOS)
1454 unload_add_on( handle );
1456 #elif defined(HAVE_DL_WINDOWS)
1457 FreeLibrary( handle );
1459 #elif defined(HAVE_DL_DLOPEN)
1462 #elif defined(HAVE_DL_SHL_LOAD)
1463 shl_unload( handle );
1469 /*****************************************************************************
1470 * GetSymbol: get a symbol from a dynamic library
1471 *****************************************************************************
1472 * This function queries a loaded library for a symbol specified in a
1473 * string, and returns a pointer to it. We don't check for dlerror() or
1474 * similar functions, since we want a non-NULL symbol anyway.
1475 *****************************************************************************/
1476 static void * _module_getsymbol( module_handle_t, const char * );
1478 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1480 void * p_symbol = _module_getsymbol( handle, psz_function );
1482 /* MacOS X dl library expects symbols to begin with "_". So do
1483 * some other operating systems. That's really lame, but hey, what
1485 if( p_symbol == NULL )
1487 char *psz_call = malloc( strlen( psz_function ) + 2 );
1489 strcpy( psz_call + 1, psz_function );
1490 psz_call[ 0 ] = '_';
1491 p_symbol = _module_getsymbol( handle, psz_call );
1498 static void * _module_getsymbol( module_handle_t handle,
1499 const char * psz_function )
1501 #if defined(HAVE_DL_DYLD)
1502 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1503 return NSAddressOfSymbol( sym );
1505 #elif defined(HAVE_DL_BEOS)
1507 if( B_OK == get_image_symbol( handle, psz_function,
1508 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1517 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1518 wchar_t psz_real[256];
1519 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1521 return (void *)GetProcAddress( handle, psz_real );
1523 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1524 return (void *)GetProcAddress( handle, (char *)psz_function );
1526 #elif defined(HAVE_DL_DLOPEN)
1527 return dlsym( handle, psz_function );
1529 #elif defined(HAVE_DL_SHL_LOAD)
1531 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1537 #if defined(HAVE_DL_WINDOWS)
1538 static char * GetWindowsError( void )
1540 #if defined(UNDER_CE)
1541 wchar_t psz_tmp[MAX_PATH];
1542 char * psz_buffer = malloc( MAX_PATH );
1544 char * psz_tmp = malloc( MAX_PATH );
1546 int i = 0, i_error = GetLastError();
1548 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1549 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1550 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1552 /* Go to the end of the string */
1553 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1560 #if defined(UNDER_CE)
1561 swprintf( psz_tmp + i, L" (error %i)", i_error );
1562 psz_tmp[ 255 ] = L'\0';
1564 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1565 psz_tmp[ 255 ] = '\0';
1569 #if defined(UNDER_CE)
1570 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1576 #endif /* HAVE_DL_WINDOWS */
1578 /*****************************************************************************
1579 * LoadPluginsCache: loads the plugins cache file
1580 *****************************************************************************
1581 * This function will load the plugin cache if present and valid. This cache
1582 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1583 * actually load the dynamically loadable module.
1584 * This allows us to only fully load plugins when they are actually used.
1585 *****************************************************************************/
1586 static void CacheLoad( vlc_object_t *p_this )
1588 char *psz_filename, *psz_homedir;
1590 int i, j, i_size, i_read;
1591 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1592 char p_cachelang[6], p_lang[6];
1594 module_cache_t **pp_cache = 0;
1595 int32_t i_file_size, i_marker;
1597 psz_homedir = p_this->p_vlc->psz_homedir;
1600 msg_Err( p_this, "psz_homedir is null" );
1604 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1605 PLUGINSCACHE_DIR, CacheName() );
1608 msg_Err( p_this, "out of memory" );
1612 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1614 #if !defined( UNDER_CE )
1615 unlink( psz_filename );
1617 wchar_t psz_wf[MAX_PATH];
1618 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1619 DeleteFile( psz_wf );
1621 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1622 free( psz_filename );
1626 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1628 file = fopen( psz_filename, "rb" );
1631 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1633 free( psz_filename );
1636 free( psz_filename );
1638 /* Check the file size */
1639 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1640 if( i_read != sizeof(i_file_size) )
1642 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1648 fseek( file, 0, SEEK_END );
1649 if( ftell( file ) != i_file_size )
1651 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1652 "(corrupted size)" );
1656 fseek( file, sizeof(i_file_size), SEEK_SET );
1658 /* Check the file is a plugins cache */
1659 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1660 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1661 if( i_read != i_size ||
1662 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1664 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1669 /* Check Sub-version number */
1670 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1671 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1673 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1674 "(corrupted header)" );
1679 /* Check the language hasn't changed */
1680 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1681 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1682 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1684 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1685 "(language changed)" );
1690 /* Check header marker */
1691 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1692 if( i_read != sizeof(i_marker) ||
1693 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1695 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1696 "(corrupted header)" );
1701 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1702 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1704 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1705 malloc( i_cache * sizeof(void *) );
1707 #define LOAD_IMMEDIATE(a) \
1708 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1709 #define LOAD_STRING(a) \
1710 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1711 != sizeof(i_size) ) goto error; \
1712 if( i_size && i_size < 16384 ) { \
1713 a = malloc( i_size ); \
1714 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1716 if( a[i_size-1] ) { \
1723 for( i = 0; i < i_cache; i++ )
1728 pp_cache[i] = malloc( sizeof(module_cache_t) );
1729 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1731 /* Load common info */
1732 LOAD_STRING( pp_cache[i]->psz_file );
1733 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1734 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1735 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1737 if( pp_cache[i]->b_junk ) continue;
1739 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1741 /* Load additional infos */
1742 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1743 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1744 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1745 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1746 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1748 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1750 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1751 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1752 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1753 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1754 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1755 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1758 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1761 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1763 LOAD_IMMEDIATE( i_submodules );
1765 while( i_submodules-- )
1767 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1768 vlc_object_attach( p_module, pp_cache[i]->p_module );
1769 p_module->b_submodule = VLC_TRUE;
1771 LOAD_STRING( p_module->psz_object_name );
1772 LOAD_STRING( p_module->psz_shortname );
1773 LOAD_STRING( p_module->psz_longname );
1774 LOAD_STRING( p_module->psz_program );
1775 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1777 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1779 LOAD_STRING( p_module->psz_capability );
1780 LOAD_IMMEDIATE( p_module->i_score );
1781 LOAD_IMMEDIATE( p_module->i_cpu );
1782 LOAD_IMMEDIATE( p_module->b_unloadable );
1783 LOAD_IMMEDIATE( p_module->b_reentrant );
1792 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1795 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1801 int CacheLoadConfig( module_t *p_module, FILE *file )
1806 /* Calculate the structure length */
1807 LOAD_IMMEDIATE( p_module->i_config_items );
1808 LOAD_IMMEDIATE( p_module->i_bool_items );
1810 LOAD_IMMEDIATE( i_lines );
1812 /* Allocate memory */
1813 p_module->p_config =
1814 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1815 if( p_module->p_config == NULL )
1817 msg_Err( p_module, "config error: can't duplicate p_config" );
1821 /* Do the duplication job */
1822 for( i = 0; i < i_lines ; i++ )
1824 LOAD_IMMEDIATE( p_module->p_config[i] );
1826 LOAD_STRING( p_module->p_config[i].psz_type );
1827 LOAD_STRING( p_module->p_config[i].psz_name );
1828 LOAD_STRING( p_module->p_config[i].psz_text );
1829 LOAD_STRING( p_module->p_config[i].psz_longtext );
1830 LOAD_STRING( p_module->p_config[i].psz_current );
1831 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1833 p_module->p_config[i].psz_value =
1834 p_module->p_config[i].psz_value_orig ?
1835 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1836 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1837 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1838 p_module->p_config[i].i_value_saved = p_module->p_config[i].i_value;
1839 p_module->p_config[i].f_value_saved = p_module->p_config[i].f_value;
1840 p_module->p_config[i].psz_value_saved = 0;
1841 p_module->p_config[i].b_dirty = VLC_FALSE;
1843 p_module->p_config[i].p_lock = &p_module->object_lock;
1845 if( p_module->p_config[i].i_list )
1847 if( p_module->p_config[i].ppsz_list )
1849 p_module->p_config[i].ppsz_list =
1850 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1851 if( p_module->p_config[i].ppsz_list )
1853 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1854 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1855 p_module->p_config[i].ppsz_list[j] = NULL;
1858 if( p_module->p_config[i].ppsz_list_text )
1860 p_module->p_config[i].ppsz_list_text =
1861 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1862 if( p_module->p_config[i].ppsz_list_text )
1864 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1865 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1866 p_module->p_config[i].ppsz_list_text[j] = NULL;
1869 if( p_module->p_config[i].pi_list )
1871 p_module->p_config[i].pi_list =
1872 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1873 if( p_module->p_config[i].pi_list )
1875 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1876 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1881 if( p_module->p_config[i].i_action )
1883 p_module->p_config[i].ppf_action =
1884 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1885 p_module->p_config[i].ppsz_action_text =
1886 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1888 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1890 p_module->p_config[i].ppf_action[j] = 0;
1891 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1895 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1898 p_module->p_config[i].i_type = CONFIG_HINT_END;
1904 return VLC_EGENERIC;
1907 /*****************************************************************************
1908 * SavePluginsCache: saves the plugins cache to a file
1909 *****************************************************************************/
1910 static void CacheSave( vlc_object_t *p_this )
1912 static char const psz_tag[] =
1913 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1914 "# This file is a cache directory tag created by VLC.\r\n"
1915 "# For information about cache directory tags, see:\r\n"
1916 "# http://www.brynosaurus.com/cachedir/\r\n";
1918 char *psz_filename, *psz_homedir;
1921 module_cache_t **pp_cache;
1922 int32_t i_file_size = 0;
1924 psz_homedir = p_this->p_vlc->psz_homedir;
1927 msg_Err( p_this, "psz_homedir is null" );
1931 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1932 strlen(psz_homedir) + strlen(CacheName()) );
1936 msg_Err( p_this, "out of memory" );
1940 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1942 config_CreateDir( p_this, psz_filename );
1944 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1946 config_CreateDir( p_this, psz_filename );
1948 strcat( psz_filename, "/CACHEDIR.TAG" );
1950 file = fopen( psz_filename, "wb" );
1953 fwrite( psz_tag, 1, strlen(psz_tag), file );
1957 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1958 PLUGINSCACHE_DIR, CacheName() );
1960 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1962 file = fopen( psz_filename, "wb" );
1965 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1967 free( psz_filename );
1970 free( psz_filename );
1972 /* Empty space for file size */
1973 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1975 /* Contains version number */
1976 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1978 /* Sub-version number (to avoid breakage in the dev version when cache
1979 * structure changes) */
1980 i_file_size = CACHE_SUBVERSION_NUM;
1981 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1984 fprintf( file, "%5.5s", _("C") );
1987 i_file_size = ftell( file );
1988 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1990 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1991 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1993 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1995 #define SAVE_IMMEDIATE(a) \
1996 fwrite( &a, sizeof(char), sizeof(a), file )
1997 #define SAVE_STRING(a) \
1998 { i_size = a ? strlen( a ) + 1 : 0; \
1999 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2000 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2003 for( i = 0; i < i_cache; i++ )
2006 int32_t i_submodule;
2008 /* Save common info */
2009 SAVE_STRING( pp_cache[i]->psz_file );
2010 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2011 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2012 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2014 if( pp_cache[i]->b_junk ) continue;
2016 /* Save additional infos */
2017 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2018 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2019 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2020 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2021 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2023 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2025 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2026 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2027 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2028 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2029 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2030 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2033 CacheSaveConfig( pp_cache[i]->p_module, file );
2035 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2037 i_submodule = pp_cache[i]->p_module->i_children;
2038 SAVE_IMMEDIATE( i_submodule );
2039 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2042 module_t *p_module =
2043 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2045 SAVE_STRING( p_module->psz_object_name );
2046 SAVE_STRING( p_module->psz_shortname );
2047 SAVE_STRING( p_module->psz_longname );
2048 SAVE_STRING( p_module->psz_program );
2049 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2051 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2053 SAVE_STRING( p_module->psz_capability );
2054 SAVE_IMMEDIATE( p_module->i_score );
2055 SAVE_IMMEDIATE( p_module->i_cpu );
2056 SAVE_IMMEDIATE( p_module->b_unloadable );
2057 SAVE_IMMEDIATE( p_module->b_reentrant );
2061 /* Fill-up file size */
2062 i_file_size = ftell( file );
2063 fseek( file, 0, SEEK_SET );
2064 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2071 void CacheSaveConfig( module_t *p_module, FILE *file )
2073 int i, j, i_lines = 0;
2074 module_config_t *p_item;
2077 SAVE_IMMEDIATE( p_module->i_config_items );
2078 SAVE_IMMEDIATE( p_module->i_bool_items );
2080 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2081 p_item++ ) i_lines++;
2083 SAVE_IMMEDIATE( i_lines );
2085 for( i = 0; i < i_lines ; i++ )
2087 SAVE_IMMEDIATE( p_module->p_config[i] );
2089 SAVE_STRING( p_module->p_config[i].psz_type );
2090 SAVE_STRING( p_module->p_config[i].psz_name );
2091 SAVE_STRING( p_module->p_config[i].psz_text );
2092 SAVE_STRING( p_module->p_config[i].psz_longtext );
2093 SAVE_STRING( p_module->p_config[i].psz_current );
2094 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2096 if( p_module->p_config[i].i_list )
2098 if( p_module->p_config[i].ppsz_list )
2100 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2101 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2104 if( p_module->p_config[i].ppsz_list_text )
2106 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2107 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2109 if( p_module->p_config[i].pi_list )
2111 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2112 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2116 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2117 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2119 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2123 /*****************************************************************************
2124 * CacheName: Return the cache file name for this platform.
2125 *****************************************************************************/
2126 static char *CacheName( void )
2128 static char psz_cachename[32];
2130 /* Code int size, pointer size and endianness in the filename */
2131 int32_t x = 0xbe00001e;
2132 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2133 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2134 return psz_cachename;
2137 /*****************************************************************************
2138 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2139 *****************************************************************************/
2140 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2141 module_t *p_module )
2145 p_cache->pf_activate = p_module->pf_activate;
2146 p_cache->pf_deactivate = p_module->pf_deactivate;
2147 #ifndef HAVE_SHARED_LIBVLC
2148 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 #ifndef HAVE_SHARED_LIBVLC
2159 p_cchild->p_symbols = p_child->p_symbols;
2163 p_cache->b_loaded = VLC_TRUE;
2164 p_module->b_loaded = VLC_FALSE;
2167 /*****************************************************************************
2168 * FindPluginCache: finds the cache entry corresponding to a file
2169 *****************************************************************************/
2170 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2171 int64_t i_time, int64_t i_size )
2173 module_cache_t **pp_cache;
2176 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2177 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2179 for( i = 0; i < i_cache; i++ )
2181 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2182 pp_cache[i]->i_time == i_time &&
2183 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2189 #endif /* HAVE_DYNAMIC_PLUGINS */