1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28 * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strdup() */
38 #include <vlc/input.h>
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #define HAVE_DYNAMIC_PLUGINS
55 #if defined(HAVE_DL_DYLD)
56 # if defined(HAVE_MACH_O_DYLD_H)
57 # include <mach-o/dyld.h>
59 #elif defined(HAVE_DL_BEOS)
60 # if defined(HAVE_IMAGE_H)
63 #elif defined(HAVE_DL_WINDOWS)
65 #elif defined(HAVE_DL_DLOPEN)
66 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
69 # if defined(HAVE_SYS_DL_H)
72 #elif defined(HAVE_DL_SHL_LOAD)
73 # if defined(HAVE_DL_H)
77 # undef HAVE_DYNAMIC_PLUGINS
80 #include "vlc_error.h"
82 #include "vlc_interface.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"
97 #include "vlc_httpd.h"
101 #include "iso_lang.h"
104 #include "vlc_block.h"
108 #include "vlc_image.h"
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 # include "modules_plugin.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) _LoadModule(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 );
159 /*****************************************************************************
160 * module_InitBank: create the module bank.
161 *****************************************************************************
162 * This function creates a module bank structure which will be filled later
163 * on with all the modules found.
164 *****************************************************************************/
165 void __module_InitBank( vlc_object_t *p_this )
167 module_bank_t *p_bank;
170 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
171 var_Get( p_this->p_libvlc, "libvlc", &lockval );
172 vlc_mutex_lock( lockval.p_address );
173 if( p_this->p_libvlc->p_module_bank )
175 p_this->p_libvlc->p_module_bank->i_usage++;
176 vlc_mutex_unlock( lockval.p_address );
177 var_Destroy( p_this->p_libvlc, "libvlc" );
180 vlc_mutex_unlock( lockval.p_address );
181 var_Destroy( p_this->p_libvlc, "libvlc" );
183 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
184 p_bank->psz_object_name = "module bank";
186 p_bank->i_cache = p_bank->i_loaded_cache = 0;
187 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
188 p_bank->b_cache = p_bank->b_cache_dirty =
189 p_bank->b_cache_delete = VLC_FALSE;
192 * Store the symbols to be exported
194 #ifdef HAVE_DYNAMIC_PLUGINS
195 STORE_SYMBOLS( &p_bank->symbols );
198 /* Everything worked, attach the object */
199 p_this->p_libvlc->p_module_bank = p_bank;
200 vlc_object_attach( p_bank, p_this->p_libvlc );
202 module_LoadMain( p_this );
207 /*****************************************************************************
208 * module_ResetBank: reset the module bank.
209 *****************************************************************************
210 * This function resets the module bank by unloading all unused plugin
212 *****************************************************************************/
213 void __module_ResetBank( vlc_object_t *p_this )
215 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
219 /*****************************************************************************
220 * module_EndBank: empty the module bank.
221 *****************************************************************************
222 * This function unloads all unused plugin modules and empties the module
223 * bank in case of success.
224 *****************************************************************************/
225 void __module_EndBank( vlc_object_t *p_this )
230 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
231 var_Get( p_this->p_libvlc, "libvlc", &lockval );
232 vlc_mutex_lock( lockval.p_address );
233 if( !p_this->p_libvlc->p_module_bank )
235 vlc_mutex_unlock( lockval.p_address );
236 var_Destroy( p_this->p_libvlc, "libvlc" );
239 if( --p_this->p_libvlc->p_module_bank->i_usage )
241 vlc_mutex_unlock( lockval.p_address );
242 var_Destroy( p_this->p_libvlc, "libvlc" );
245 vlc_mutex_unlock( lockval.p_address );
246 var_Destroy( p_this->p_libvlc, "libvlc" );
248 #ifdef HAVE_DYNAMIC_PLUGINS
249 #define p_bank p_this->p_libvlc->p_module_bank
250 if( p_bank->b_cache ) CacheSave( p_this );
251 while( p_bank->i_loaded_cache-- )
253 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
254 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
255 if( !p_bank->i_loaded_cache ) free( p_bank->pp_loaded_cache );
257 while( p_bank->i_cache-- )
259 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
260 free( p_bank->pp_cache[p_bank->i_cache] );
261 if( !p_bank->i_cache ) free( p_bank->pp_cache );
266 vlc_object_detach( p_this->p_libvlc->p_module_bank );
268 while( p_this->p_libvlc->p_module_bank->i_children )
270 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
272 if( DeleteModule( p_next ) )
274 /* Module deletion failed */
275 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
276 p_next->psz_object_name );
278 /* We just free the module by hand. Niahahahahaha. */
279 vlc_object_detach( p_next );
280 vlc_object_destroy( p_next );
284 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
285 p_this->p_libvlc->p_module_bank = NULL;
290 /*****************************************************************************
291 * module_LoadMain: load the main program info into the module bank.
292 *****************************************************************************
293 * This function fills the module bank structure with the main module infos.
294 * This is very useful as it will allow us to consider the main program just
295 * as another module, and for instance the configuration options of main will
296 * be available in the module bank structure just as for every other module.
297 *****************************************************************************/
298 void __module_LoadMain( vlc_object_t *p_this )
302 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
303 var_Get( p_this->p_libvlc, "libvlc", &lockval );
304 vlc_mutex_lock( lockval.p_address );
305 if( p_this->p_libvlc->p_module_bank->b_main )
307 vlc_mutex_unlock( lockval.p_address );
308 var_Destroy( p_this->p_libvlc, "libvlc" );
311 p_this->p_libvlc->p_module_bank->b_main = VLC_TRUE;
312 vlc_mutex_unlock( lockval.p_address );
313 var_Destroy( p_this->p_libvlc, "libvlc" );
315 AllocateBuiltinModule( p_this, vlc_entry__main );
318 /*****************************************************************************
319 * module_LoadBuiltins: load all modules which we built with.
320 *****************************************************************************
321 * This function fills the module bank structure with the builtin modules.
322 *****************************************************************************/
323 void __module_LoadBuiltins( vlc_object_t * p_this )
327 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
328 var_Get( p_this->p_libvlc, "libvlc", &lockval );
329 vlc_mutex_lock( lockval.p_address );
330 if( p_this->p_libvlc->p_module_bank->b_builtins )
332 vlc_mutex_unlock( lockval.p_address );
333 var_Destroy( p_this->p_libvlc, "libvlc" );
336 p_this->p_libvlc->p_module_bank->b_builtins = VLC_TRUE;
337 vlc_mutex_unlock( lockval.p_address );
338 var_Destroy( p_this->p_libvlc, "libvlc" );
340 msg_Dbg( p_this, "checking builtin modules" );
341 ALLOCATE_ALL_BUILTINS();
344 /*****************************************************************************
345 * module_LoadPlugins: load all plugin modules we can find.
346 *****************************************************************************
347 * This function fills the module bank structure with the plugin modules.
348 *****************************************************************************/
349 void __module_LoadPlugins( vlc_object_t * p_this )
351 #ifdef HAVE_DYNAMIC_PLUGINS
354 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
355 var_Get( p_this->p_libvlc, "libvlc", &lockval );
356 vlc_mutex_lock( lockval.p_address );
357 if( p_this->p_libvlc->p_module_bank->b_plugins )
359 vlc_mutex_unlock( lockval.p_address );
360 var_Destroy( p_this->p_libvlc, "libvlc" );
363 p_this->p_libvlc->p_module_bank->b_plugins = VLC_TRUE;
364 vlc_mutex_unlock( lockval.p_address );
365 var_Destroy( p_this->p_libvlc, "libvlc" );
367 msg_Dbg( p_this, "checking plugin modules" );
369 if( config_GetInt( p_this, "plugins-cache" ) )
370 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
372 if( p_this->p_libvlc->p_module_bank->b_cache ||
373 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
375 AllocateAllPlugins( p_this );
379 /*****************************************************************************
380 * module_Need: return the best module function, given a capability list.
381 *****************************************************************************
382 * This function returns the module that best fits the asked capabilities.
383 *****************************************************************************/
384 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
385 const char *psz_name, vlc_bool_t b_strict )
387 typedef struct module_list_t module_list_t;
394 module_list_t *p_next;
397 module_list_t *p_list, *p_first, *p_tmp;
400 int i_which_module, i_index = 0;
401 vlc_bool_t b_intf = VLC_FALSE;
406 char *psz_shortcuts = NULL, *psz_var = NULL;
407 vlc_bool_t b_force_backup = p_this->b_force;
410 /* Deal with variables */
411 if( psz_name && psz_name[0] == '$' )
414 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
415 var_Get( p_this, psz_name + 1, &val );
416 psz_var = val.psz_string;
420 /* Count how many different shortcuts were asked for */
421 if( psz_name && *psz_name )
423 char *psz_parser, *psz_last_shortcut;
425 /* If the user wants none, give him none. */
426 if( !strcmp( psz_name, "none" ) )
428 if( psz_var ) free( psz_var );
433 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
435 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
437 if( *psz_parser == ',' )
441 psz_last_shortcut = psz_parser + 1;
445 /* Check if the user wants to override the "strict" mode */
446 if( psz_last_shortcut )
448 if( !strcmp(psz_last_shortcut, "none") )
453 else if( !strcmp(psz_last_shortcut, "any") )
455 b_strict = VLC_FALSE;
461 /* Sort the modules and test them */
462 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
463 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
466 /* Parse the module list for capabilities and probe each of them */
467 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
469 int i_shortcut_bonus = 0;
471 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
473 /* Test that this module can do what we need */
474 if( strcmp( p_module->psz_capability, psz_capability ) )
476 /* Don't recurse through the sub-modules because vlc_list_find()
477 * will list them anyway. */
481 /* Test if we have the required CPU */
482 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
487 /* If we required a shortcut, check this plugin provides it. */
488 if( i_shortcuts > 0 )
491 int i_dummy, i_short = i_shortcuts;
492 char *psz_name = psz_shortcuts;
494 /* Let's drop modules with a <= 0 score (unless they are
495 * explicitly requested) */
496 b_trash = p_module->i_score <= 0;
500 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
502 if( !strcasecmp( psz_name,
503 p_module->pp_shortcuts[i_dummy] ) )
507 i_shortcut_bonus = i_short * 10000;
512 if( i_shortcut_bonus )
514 /* We found it... remember ? */
518 /* Go to the next shortcut... This is so lame! */
527 /* If we are in "strict" mode and we couldn't
528 * find the module in the list of provided shortcuts,
529 * then kick the bastard out of here!!! */
530 if( i_short == 0 && b_strict )
540 /* If we didn't require a shortcut, trash <= 0 scored plugins */
541 else if( p_module->i_score <= 0 )
546 /* Special case: test if we requested a particular intf plugin */
547 if( !i_shortcuts && p_module->psz_program
548 && !strcmp( psz_capability, "interface" )
549 && !strcmp( p_module->psz_program,
550 p_this->p_vlc->psz_object_name ) )
554 /* Remove previous non-matching plugins */
561 /* This one doesn't match */
565 /* Store this new module */
566 p_list[ i_index ].p_module = p_module;
567 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
568 p_list[ i_index ].b_force = !!i_shortcut_bonus;
570 /* Add it to the modules-to-probe list */
573 p_list[ 0 ].p_next = NULL;
578 /* Ok, so at school you learned that quicksort is quick, and
579 * bubble sort sucks raw eggs. But that's when dealing with
580 * thousands of items. Here we have barely 50. */
581 module_list_t *p_newlist = p_first;
583 if( p_first->i_score < p_list[ i_index ].i_score )
585 p_list[ i_index ].p_next = p_first;
586 p_first = &p_list[ i_index ];
590 while( p_newlist->p_next != NULL &&
591 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
593 p_newlist = p_newlist->p_next;
596 p_list[ i_index ].p_next = p_newlist->p_next;
597 p_newlist->p_next = &p_list[ i_index ];
604 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
605 i_index, i_index == 1 ? "" : "s" );
607 /* Lock all candidate modules */
609 while( p_tmp != NULL )
611 vlc_object_yield( p_tmp->p_module );
612 p_tmp = p_tmp->p_next;
615 /* We can release the list, interesting modules were yielded */
616 vlc_list_release( p_all );
618 /* Parse the linked list and use the first successful module */
620 while( p_tmp != NULL )
622 #ifdef HAVE_DYNAMIC_PLUGINS
623 /* Make sure the module is loaded in mem */
624 module_t *p_module = p_tmp->p_module->b_submodule ?
625 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
626 if( !p_module->b_builtin && !p_module->b_loaded )
628 module_t *p_new_module =
629 AllocatePlugin( p_this, p_module->psz_filename );
632 CacheMerge( p_this, p_module, p_new_module );
633 vlc_object_attach( p_new_module, p_module );
634 DeleteModule( p_new_module );
639 p_this->b_force = p_tmp->b_force;
640 if( p_tmp->p_module->pf_activate
641 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
646 vlc_object_release( p_tmp->p_module );
647 p_tmp = p_tmp->p_next;
650 /* Store the locked module value */
653 p_module = p_tmp->p_module;
654 p_tmp = p_tmp->p_next;
661 /* Unlock the remaining modules */
662 while( p_tmp != NULL )
664 vlc_object_release( p_tmp->p_module );
665 p_tmp = p_tmp->p_next;
669 p_this->b_force = b_force_backup;
671 if( p_module != NULL )
673 msg_Dbg( p_module, "using %s module \"%s\"",
674 psz_capability, p_module->psz_object_name );
676 else if( p_first == NULL )
678 if( !strcmp( psz_capability, "access_demux" ) )
680 msg_Warn( p_this, "no %s module matched \"%s\"",
681 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
685 msg_Err( p_this, "no %s module matched \"%s\"",
686 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
689 else if( psz_name != NULL && *psz_name )
691 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
692 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
697 free( psz_shortcuts );
705 /* Don't forget that the module is still locked */
709 /*****************************************************************************
710 * module_Unneed: decrease the usage count of a module.
711 *****************************************************************************
712 * This function must be called by the thread that called module_Need, to
713 * decrease the reference count and allow for hiding of modules.
714 *****************************************************************************/
715 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
717 /* Use the close method */
718 if( p_module->pf_deactivate )
720 p_module->pf_deactivate( p_this );
723 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
725 vlc_object_release( p_module );
730 /*****************************************************************************
731 * Following functions are local.
732 *****************************************************************************/
734 /*****************************************************************************
735 * AllocateAllPlugins: load all plugin modules we can find.
736 *****************************************************************************/
737 #ifdef HAVE_DYNAMIC_PLUGINS
738 static void AllocateAllPlugins( vlc_object_t *p_this )
740 /* Yes, there are two NULLs because we replace one with "plugin-path". */
741 #if defined( WIN32 ) || defined( UNDER_CE )
742 char *path[] = { "modules", "", "plugins", 0, 0 };
744 char *path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
747 char **ppsz_path = path;
750 /* If the user provided a plugin path, we add it to the list */
751 path[ sizeof(path)/sizeof(char*) - 2 ] =
752 config_GetPsz( p_this, "plugin-path" );
754 for( ; *ppsz_path != NULL ; ppsz_path++ )
756 if( !(*ppsz_path)[0] ) continue;
758 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
760 /* Handle relative as well as absolute paths */
762 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
763 (*ppsz_path)[1] != ':' )
765 if( (*ppsz_path)[0] != '/' )
768 int i_dirlen = strlen( *ppsz_path );
769 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
771 psz_fullpath = malloc( i_dirlen );
772 if( psz_fullpath == NULL )
777 sprintf( psz_fullpath, "%s\\%s",
778 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
780 sprintf( psz_fullpath, "%s/%s",
781 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
787 psz_fullpath = strdup( *ppsz_path );
790 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
792 /* Don't go deeper than 5 subdirectories */
793 AllocatePluginDir( p_this, psz_fullpath, 5 );
795 free( psz_fullpath );
798 /* Free plugin-path */
799 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
800 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
801 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
804 /*****************************************************************************
805 * AllocatePluginDir: recursively parse a directory to look for plugins
806 *****************************************************************************/
807 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
810 #if defined( UNDER_CE ) || defined( _MSC_VER )
812 wchar_t psz_wpath[MAX_PATH + 256];
813 wchar_t psz_wdir[MAX_PATH];
815 char psz_path[MAX_PATH + 256];
816 WIN32_FIND_DATA finddata;
822 struct dirent * file;
826 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
831 #if defined( UNDER_CE ) || defined( _MSC_VER )
833 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
835 rc = GetFileAttributes( psz_wdir );
836 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
838 /* Parse all files in the directory */
839 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
841 rc = GetFileAttributes( psz_dir );
842 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
845 /* Parse all files in the directory */
846 sprintf( psz_path, "%s\\*", psz_dir );
849 handle = FindFirstFile( psz_wpath, &finddata );
851 handle = FindFirstFile( psz_path, &finddata );
853 if( handle == INVALID_HANDLE_VALUE )
855 /* Empty directory */
859 /* Parse the directory and try to load all files it contains. */
863 unsigned int i_len = wcslen( finddata.cFileName );
864 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
865 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
867 unsigned int i_len = strlen( finddata.cFileName );
868 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
871 /* Skip ".", ".." and anything starting with "." */
872 if( !*finddata.cFileName || *finddata.cFileName == '.' )
874 if( !FindNextFile( handle, &finddata ) ) break;
879 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
881 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
884 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
886 else if( i_len > strlen( LIBEXT )
887 /* We only load files ending with LIBEXT */
888 && !strncasecmp( psz_path + strlen( psz_path)
890 LIBEXT, strlen( LIBEXT ) ) )
892 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
893 int64_t i_time = 0, i_size = 0;
896 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
899 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
903 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
905 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
906 i_size = attrbuf.nFileSizeHigh;
908 i_size |= attrbuf.nFileSizeLow;
912 AllocatePluginFile( p_this, psz_file, i_time, i_size );
915 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
917 /* Close the directory */
921 dir = opendir( psz_dir );
927 i_dirlen = strlen( psz_dir );
929 /* Parse the directory and try to load all files it contains. */
930 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
936 /* Skip ".", ".." and anything starting with "." */
937 if( !*file->d_name || *file->d_name == '.' )
942 i_len = strlen( file->d_name );
943 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
945 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
947 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
950 i_stat = stat( psz_file, &statbuf );
951 if( !i_stat && statbuf.st_mode & S_IFDIR )
953 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
955 else if( i_len > strlen( LIBEXT )
956 /* We only load files ending with LIBEXT */
957 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
958 LIBEXT, strlen( LIBEXT ) ) )
960 int64_t i_time = 0, i_size = 0;
964 i_time = statbuf.st_mtime;
965 i_size = statbuf.st_size;
968 AllocatePluginFile( p_this, psz_file, i_time, i_size );
974 /* Close the directory */
980 /*****************************************************************************
981 * AllocatePluginFile: load a module into memory and initialize it.
982 *****************************************************************************
983 * This function loads a dynamically loadable module and allocates a structure
984 * for its information data. The module can then be handled by module_Need
985 * and module_Unneed. It can be removed by DeleteModule.
986 *****************************************************************************/
987 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
988 int64_t i_file_time, int64_t i_file_size )
991 module_cache_t *p_cache_entry = NULL;
994 * Check our plugins cache first then load plugin if needed
997 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1001 p_module = AllocatePlugin( p_this, psz_file );
1005 /* If junk dll, don't try to load it */
1006 if( p_cache_entry->b_junk )
1012 module_config_t *p_item;
1014 p_module = p_cache_entry->p_module;
1015 p_module->b_loaded = VLC_FALSE;
1017 /* For now we force loading if the module's config contains
1018 * callbacks or actions.
1019 * Could be optimized by adding an API call.*/
1020 for( p_item = p_module->p_config;
1021 p_item->i_type != CONFIG_HINT_END; p_item++ )
1023 if( p_item->pf_callback || p_item->i_action )
1024 p_module = AllocatePlugin( p_this, psz_file );
1031 /* Everything worked fine !
1032 * The module is ready to be added to the list. */
1033 p_module->b_builtin = VLC_FALSE;
1035 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1036 p_module->psz_object_name, p_module->psz_longname ); */
1038 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1041 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
1043 /* Add entry to cache */
1044 #define p_bank p_this->p_libvlc->p_module_bank
1046 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1047 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1048 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1049 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1050 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1051 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1052 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1055 return p_module ? 0 : -1;
1058 /*****************************************************************************
1059 * AllocatePlugin: load a module into memory and initialize it.
1060 *****************************************************************************
1061 * This function loads a dynamically loadable module and allocates a structure
1062 * for its information data. The module can then be handled by module_Need
1063 * and module_Unneed. It can be removed by DeleteModule.
1064 *****************************************************************************/
1065 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1067 module_t * p_module;
1068 module_handle_t handle;
1070 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1072 /* Now that we have successfully loaded the module, we can
1073 * allocate a structure for it */
1074 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1075 if( p_module == NULL )
1077 msg_Err( p_this, "out of memory" );
1078 CloseModule( handle );
1082 /* We need to fill these since they may be needed by CallEntry() */
1083 p_module->psz_filename = psz_file;
1084 p_module->handle = handle;
1085 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1086 p_module->b_loaded = VLC_TRUE;
1088 /* Initialize the module: fill p_module, default config */
1089 if( CallEntry( p_module ) != 0 )
1091 /* We couldn't call module_init() */
1092 vlc_object_destroy( p_module );
1093 CloseModule( handle );
1097 DupModule( p_module );
1098 p_module->psz_filename = strdup( p_module->psz_filename );
1100 /* Everything worked fine ! The module is ready to be added to the list. */
1101 p_module->b_builtin = VLC_FALSE;
1106 /*****************************************************************************
1107 * DupModule: make a plugin module standalone.
1108 *****************************************************************************
1109 * This function duplicates all strings in the module, so that the dynamic
1110 * object can be unloaded. It acts recursively on submodules.
1111 *****************************************************************************/
1112 static void DupModule( module_t *p_module )
1117 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1119 *pp_shortcut = strdup( *pp_shortcut );
1122 /* We strdup() these entries so that they are still valid when the
1123 * module is unloaded. */
1124 p_module->psz_object_name = strdup( p_module->psz_object_name );
1125 p_module->psz_capability = strdup( p_module->psz_capability );
1126 p_module->psz_shortname = p_module->psz_shortname ?
1127 strdup( p_module->psz_shortname ) : NULL;
1128 p_module->psz_longname = strdup( p_module->psz_longname );
1130 if( p_module->psz_program != NULL )
1132 p_module->psz_program = strdup( p_module->psz_program );
1135 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1137 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1141 /*****************************************************************************
1142 * UndupModule: free a duplicated module.
1143 *****************************************************************************
1144 * This function frees the allocations done in DupModule().
1145 *****************************************************************************/
1146 static void UndupModule( module_t *p_module )
1151 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1153 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1156 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1158 free( *pp_shortcut );
1161 free( p_module->psz_object_name );
1162 free( p_module->psz_capability );
1163 if( p_module->psz_shortname ) free( p_module->psz_shortname );
1164 free( p_module->psz_longname );
1166 if( p_module->psz_program != NULL )
1168 free( p_module->psz_program );
1172 #endif /* HAVE_DYNAMIC_PLUGINS */
1174 /*****************************************************************************
1175 * AllocateBuiltinModule: initialize a builtin module.
1176 *****************************************************************************
1177 * This function registers a builtin module and allocates a structure
1178 * for its information data. The module can then be handled by module_Need
1179 * and module_Unneed. It can be removed by DeleteModule.
1180 *****************************************************************************/
1181 static int AllocateBuiltinModule( vlc_object_t * p_this,
1182 int ( *pf_entry ) ( module_t * ) )
1184 module_t * p_module;
1186 /* Now that we have successfully loaded the module, we can
1187 * allocate a structure for it */
1188 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1189 if( p_module == NULL )
1191 msg_Err( p_this, "out of memory" );
1195 /* Initialize the module : fill p_module->psz_object_name, etc. */
1196 if( pf_entry( p_module ) != 0 )
1198 /* With a well-written module we shouldn't have to print an
1199 * additional error message here, but just make sure. */
1200 msg_Err( p_this, "failed calling entry point in builtin module" );
1201 vlc_object_destroy( p_module );
1205 /* Everything worked fine ! The module is ready to be added to the list. */
1206 p_module->b_builtin = VLC_TRUE;
1208 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1209 p_module->psz_object_name, p_module->psz_longname ); */
1211 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1216 /*****************************************************************************
1217 * DeleteModule: delete a module and its structure.
1218 *****************************************************************************
1219 * This function can only be called if the module isn't being used.
1220 *****************************************************************************/
1221 static int DeleteModule( module_t * p_module )
1223 vlc_object_detach( p_module );
1225 /* We free the structures that we strdup()ed in Allocate*Module(). */
1226 #ifdef HAVE_DYNAMIC_PLUGINS
1227 if( !p_module->b_builtin )
1229 if( p_module->b_loaded && p_module->b_unloadable )
1231 CloseModule( p_module->handle );
1233 UndupModule( p_module );
1234 free( p_module->psz_filename );
1238 /* Free and detach the object's children */
1239 while( p_module->i_children )
1241 vlc_object_t *p_this = p_module->pp_children[0];
1242 vlc_object_detach( p_this );
1243 vlc_object_destroy( p_this );
1246 config_Free( p_module );
1247 vlc_object_destroy( p_module );
1252 #ifdef HAVE_DYNAMIC_PLUGINS
1253 /*****************************************************************************
1254 * CallEntry: call an entry point.
1255 *****************************************************************************
1256 * This function calls a symbol given its name and a module structure. The
1257 * symbol MUST refer to a function returning int and taking a module_t* as
1259 *****************************************************************************/
1260 static int CallEntry( module_t * p_module )
1262 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1263 int (* pf_symbol) ( module_t * p_module );
1265 /* Try to resolve the symbol */
1266 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1268 if( pf_symbol == NULL )
1270 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1271 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1272 psz_name, p_module->psz_filename );
1273 #elif defined(HAVE_DL_WINDOWS)
1274 char *psz_error = GetWindowsError();
1275 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1276 psz_name, p_module->psz_filename, psz_error );
1278 #elif defined(HAVE_DL_DLOPEN)
1279 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1280 psz_name, p_module->psz_filename, dlerror() );
1281 #elif defined(HAVE_DL_SHL_LOAD)
1282 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1283 psz_name, p_module->psz_filename, strerror(errno) );
1285 # error "Something is wrong in modules.c"
1290 /* We can now try to call the symbol */
1291 if( pf_symbol( p_module ) != 0 )
1293 /* With a well-written module we shouldn't have to print an
1294 * additional error message here, but just make sure. */
1295 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1296 psz_name, p_module->psz_filename );
1300 /* Everything worked fine, we can return */
1304 /*****************************************************************************
1305 * LoadModule: loads a dynamic library
1306 *****************************************************************************
1307 * This function loads a dynamically linked library using a system dependant
1308 * method. Will return 0 on success as well as the module handle.
1309 *****************************************************************************/
1310 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1311 module_handle_t *p_handle )
1313 module_handle_t handle;
1315 #if defined(HAVE_DL_DYLD)
1316 NSObjectFileImage image;
1317 NSObjectFileImageReturnCode ret;
1319 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1321 if( ret != NSObjectFileImageSuccess )
1323 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1327 /* Open the dynamic module */
1328 handle = NSLinkModule( image, psz_file,
1329 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1333 NSLinkEditErrors errors;
1334 const char *psz_file, *psz_err;
1336 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1337 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1338 NSDestroyObjectFileImage( image );
1342 /* Destroy our image, we won't need it */
1343 NSDestroyObjectFileImage( image );
1345 #elif defined(HAVE_DL_BEOS)
1346 handle = load_add_on( psz_file );
1349 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1353 #elif defined(HAVE_DL_WINDOWS)
1356 wchar_t psz_wfile[MAX_PATH];
1357 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1358 handle = LoadLibrary( psz_wfile );
1361 handle = LoadLibrary( psz_file );
1363 if( handle == NULL )
1365 char *psz_err = GetWindowsError();
1366 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1371 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1372 /* static is OK, we are called atomically */
1373 static vlc_bool_t b_kde = VLC_FALSE;
1375 # if defined(SYS_LINUX)
1376 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1377 * are going to get namespace collisions when two modules have common
1378 * public symbols, but ALSA is being a pest here. */
1379 if( strstr( psz_file, "alsa_plugin" ) )
1381 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1382 if( handle == NULL )
1384 msg_Warn( p_this, "cannot load module `%s' (%s)",
1385 psz_file, dlerror() );
1390 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1391 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1392 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1393 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1394 if( !b_kde && !strstr( psz_file, "kde" ) )
1396 dlopen( "libstdc++.so.6", RTLD_NOW )
1397 || dlopen( "libstdc++.so.5", RTLD_NOW )
1398 || dlopen( "libstdc++.so.4", RTLD_NOW )
1399 || dlopen( "libstdc++.so.3", RTLD_NOW );
1403 handle = dlopen( psz_file, RTLD_NOW );
1404 if( handle == NULL )
1406 msg_Warn( p_this, "cannot load module `%s' (%s)",
1407 psz_file, dlerror() );
1411 #elif defined(HAVE_DL_DLOPEN)
1412 # if defined(DL_LAZY)
1413 handle = dlopen( psz_file, DL_LAZY );
1415 handle = dlopen( psz_file, 0 );
1417 if( handle == NULL )
1419 msg_Warn( p_this, "cannot load module `%s' (%s)",
1420 psz_file, dlerror() );
1424 #elif defined(HAVE_DL_SHL_LOAD)
1425 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1426 if( handle == NULL )
1428 msg_Warn( p_this, "cannot load module `%s' (%s)",
1429 psz_file, strerror(errno) );
1434 # error "Something is wrong in modules.c"
1442 /*****************************************************************************
1443 * CloseModule: unload a dynamic library
1444 *****************************************************************************
1445 * This function unloads a previously opened dynamically linked library
1446 * using a system dependant method. No return value is taken in consideration,
1447 * since some libraries sometimes refuse to close properly.
1448 *****************************************************************************/
1449 static void CloseModule( module_handle_t handle )
1451 #if defined(HAVE_DL_DYLD)
1452 NSUnLinkModule( handle, FALSE );
1454 #elif defined(HAVE_DL_BEOS)
1455 unload_add_on( handle );
1457 #elif defined(HAVE_DL_WINDOWS)
1458 FreeLibrary( handle );
1460 #elif defined(HAVE_DL_DLOPEN)
1463 #elif defined(HAVE_DL_SHL_LOAD)
1464 shl_unload( handle );
1470 /*****************************************************************************
1471 * GetSymbol: get a symbol from a dynamic library
1472 *****************************************************************************
1473 * This function queries a loaded library for a symbol specified in a
1474 * string, and returns a pointer to it. We don't check for dlerror() or
1475 * similar functions, since we want a non-NULL symbol anyway.
1476 *****************************************************************************/
1477 static void * _module_getsymbol( module_handle_t, const char * );
1479 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1481 void * p_symbol = _module_getsymbol( handle, psz_function );
1483 /* MacOS X dl library expects symbols to begin with "_". So do
1484 * some other operating systems. That's really lame, but hey, what
1486 if( p_symbol == NULL )
1488 char *psz_call = malloc( strlen( psz_function ) + 2 );
1490 strcpy( psz_call + 1, psz_function );
1491 psz_call[ 0 ] = '_';
1492 p_symbol = _module_getsymbol( handle, psz_call );
1499 static void * _module_getsymbol( module_handle_t handle,
1500 const char * psz_function )
1502 #if defined(HAVE_DL_DYLD)
1503 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1504 return NSAddressOfSymbol( sym );
1506 #elif defined(HAVE_DL_BEOS)
1508 if( B_OK == get_image_symbol( handle, psz_function,
1509 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1518 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1519 wchar_t psz_real[256];
1520 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1522 return (void *)GetProcAddress( handle, psz_real );
1524 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1525 return (void *)GetProcAddress( handle, (char *)psz_function );
1527 #elif defined(HAVE_DL_DLOPEN)
1528 return dlsym( handle, psz_function );
1530 #elif defined(HAVE_DL_SHL_LOAD)
1532 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1538 #if defined(HAVE_DL_WINDOWS)
1539 static char * GetWindowsError( void )
1541 #if defined(UNDER_CE)
1542 wchar_t psz_tmp[MAX_PATH];
1543 char * psz_buffer = malloc( MAX_PATH );
1545 char * psz_tmp = malloc( MAX_PATH );
1547 int i = 0, i_error = GetLastError();
1549 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1550 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1551 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1553 /* Go to the end of the string */
1554 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1561 #if defined(UNDER_CE)
1562 swprintf( psz_tmp + i, L" (error %i)", i_error );
1563 psz_tmp[ 255 ] = L'\0';
1565 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1566 psz_tmp[ 255 ] = '\0';
1570 #if defined(UNDER_CE)
1571 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1577 #endif /* HAVE_DL_WINDOWS */
1579 /*****************************************************************************
1580 * LoadPluginsCache: loads the plugins cache file
1581 *****************************************************************************
1582 * This function will load the plugin cache if present and valid. This cache
1583 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1584 * actually load the dynamically loadable module.
1585 * This allows us to only fully load plugins when they are actually used.
1586 *****************************************************************************/
1587 static void CacheLoad( vlc_object_t *p_this )
1589 char *psz_filename, *psz_homedir;
1591 int i, j, i_size, i_read;
1592 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1593 char p_cachelang[6], p_lang[6];
1595 module_cache_t **pp_cache = 0;
1596 int32_t i_file_size, i_marker;
1598 psz_homedir = p_this->p_vlc->psz_homedir;
1601 msg_Err( p_this, "psz_homedir is null" );
1605 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1606 PLUGINSCACHE_DIR, CacheName() );
1609 msg_Err( p_this, "out of memory" );
1613 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1615 #if !defined( UNDER_CE )
1616 unlink( psz_filename );
1618 wchar_t psz_wf[MAX_PATH];
1619 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1620 DeleteFile( psz_wf );
1622 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
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 the language hasn't changed */
1670 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1671 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1672 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1674 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1675 "(language changed)" );
1680 /* Check header marker */
1681 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1682 if( i_read != sizeof(i_marker) ||
1683 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1685 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1686 "(corrupted header)" );
1691 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1692 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1693 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1694 malloc( i_cache * sizeof(void *) );
1696 #define LOAD_IMMEDIATE(a) \
1697 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1698 #define LOAD_STRING(a) \
1699 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1700 != sizeof(i_size) ) goto error; \
1702 a = malloc( i_size ); \
1703 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1709 for( i = 0; i < i_cache; i++ )
1714 pp_cache[i] = malloc( sizeof(module_cache_t) );
1715 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1717 /* Load common info */
1718 LOAD_STRING( pp_cache[i]->psz_file );
1719 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1720 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1721 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1723 if( pp_cache[i]->b_junk ) continue;
1725 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1727 /* Load additional infos */
1728 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1729 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1730 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1731 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1732 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1734 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1736 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1737 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1738 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1739 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1740 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1741 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1744 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1747 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1749 LOAD_IMMEDIATE( i_submodules );
1751 while( i_submodules-- )
1753 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1754 vlc_object_attach( p_module, pp_cache[i]->p_module );
1755 p_module->b_submodule = VLC_TRUE;
1757 LOAD_STRING( p_module->psz_object_name );
1758 LOAD_STRING( p_module->psz_shortname );
1759 LOAD_STRING( p_module->psz_longname );
1760 LOAD_STRING( p_module->psz_program );
1761 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1763 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1765 LOAD_STRING( p_module->psz_capability );
1766 LOAD_IMMEDIATE( p_module->i_score );
1767 LOAD_IMMEDIATE( p_module->i_cpu );
1768 LOAD_IMMEDIATE( p_module->b_unloadable );
1769 LOAD_IMMEDIATE( p_module->b_reentrant );
1778 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1781 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1787 int CacheLoadConfig( module_t *p_module, FILE *file )
1792 /* Calculate the structure length */
1793 LOAD_IMMEDIATE( p_module->i_config_items );
1794 LOAD_IMMEDIATE( p_module->i_bool_items );
1796 LOAD_IMMEDIATE( i_lines );
1798 /* Allocate memory */
1799 p_module->p_config =
1800 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1801 if( p_module->p_config == NULL )
1803 msg_Err( p_module, "config error: can't duplicate p_config" );
1807 /* Do the duplication job */
1808 for( i = 0; i < i_lines ; i++ )
1810 LOAD_IMMEDIATE( p_module->p_config[i] );
1812 LOAD_STRING( p_module->p_config[i].psz_type );
1813 LOAD_STRING( p_module->p_config[i].psz_name );
1814 LOAD_STRING( p_module->p_config[i].psz_text );
1815 LOAD_STRING( p_module->p_config[i].psz_longtext );
1816 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1818 p_module->p_config[i].psz_value =
1819 p_module->p_config[i].psz_value_orig ?
1820 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1821 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1822 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1824 p_module->p_config[i].p_lock = &p_module->object_lock;
1826 if( p_module->p_config[i].i_list )
1828 if( p_module->p_config[i].ppsz_list )
1830 p_module->p_config[i].ppsz_list =
1831 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1832 if( p_module->p_config[i].ppsz_list )
1834 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1835 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1836 p_module->p_config[i].ppsz_list[j] = NULL;
1839 if( p_module->p_config[i].ppsz_list_text )
1841 p_module->p_config[i].ppsz_list_text =
1842 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1843 if( p_module->p_config[i].ppsz_list_text )
1845 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1846 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1847 p_module->p_config[i].ppsz_list_text[j] = NULL;
1850 if( p_module->p_config[i].pi_list )
1852 p_module->p_config[i].pi_list =
1853 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1854 if( p_module->p_config[i].pi_list )
1856 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1857 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1862 if( p_module->p_config[i].i_action )
1864 p_module->p_config[i].ppf_action =
1865 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1866 p_module->p_config[i].ppsz_action_text =
1867 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1869 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1871 p_module->p_config[i].ppf_action[j] = 0;
1872 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1876 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1879 p_module->p_config[i].i_type = CONFIG_HINT_END;
1885 return VLC_EGENERIC;
1888 /*****************************************************************************
1889 * SavePluginsCache: saves the plugins cache to a file
1890 *****************************************************************************/
1891 static void CacheSave( vlc_object_t *p_this )
1893 static char const psz_tag[] =
1894 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1895 "# This file is a cache directory tag created by VLC.\r\n"
1896 "# For information about cache directory tags, see:\r\n"
1897 "# http://www.brynosaurus.com/cachedir/\r\n";
1899 char *psz_filename, *psz_homedir;
1902 module_cache_t **pp_cache;
1903 int32_t i_file_size = 0;
1905 psz_homedir = p_this->p_vlc->psz_homedir;
1908 msg_Err( p_this, "psz_homedir is null" );
1912 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1913 strlen(psz_homedir) + strlen(CacheName()) );
1917 msg_Err( p_this, "out of memory" );
1921 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1923 config_CreateDir( p_this, psz_filename );
1925 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1927 config_CreateDir( p_this, psz_filename );
1929 strcat( psz_filename, "/CACHEDIR.TAG" );
1931 file = fopen( psz_filename, "wb" );
1934 fwrite( psz_tag, 1, strlen(psz_tag), file );
1938 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1939 PLUGINSCACHE_DIR, CacheName() );
1941 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1943 file = fopen( psz_filename, "wb" );
1946 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1948 free( psz_filename );
1951 free( psz_filename );
1953 /* Empty space for file size */
1954 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1956 /* Contains version number */
1957 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1960 fprintf( file, "%5.5s", _("C") );
1963 i_file_size = ftell( file );
1964 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1966 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1967 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1969 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1971 #define SAVE_IMMEDIATE(a) \
1972 fwrite( &a, sizeof(char), sizeof(a), file )
1973 #define SAVE_STRING(a) \
1974 { i_size = a ? strlen( a ) + 1 : 0; \
1975 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1976 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1979 for( i = 0; i < i_cache; i++ )
1982 int32_t i_submodule;
1984 /* Save common info */
1985 SAVE_STRING( pp_cache[i]->psz_file );
1986 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1987 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1988 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1990 if( pp_cache[i]->b_junk ) continue;
1992 /* Save additional infos */
1993 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1994 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1995 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1996 SAVE_STRING( pp_cache[i]->p_module->psz_program );
1997 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1999 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2001 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2002 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2003 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2004 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2005 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2006 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2009 CacheSaveConfig( pp_cache[i]->p_module, file );
2011 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2013 i_submodule = pp_cache[i]->p_module->i_children;
2014 SAVE_IMMEDIATE( i_submodule );
2015 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2018 module_t *p_module =
2019 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2021 SAVE_STRING( p_module->psz_object_name );
2022 SAVE_STRING( p_module->psz_shortname );
2023 SAVE_STRING( p_module->psz_longname );
2024 SAVE_STRING( p_module->psz_program );
2025 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2027 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2029 SAVE_STRING( p_module->psz_capability );
2030 SAVE_IMMEDIATE( p_module->i_score );
2031 SAVE_IMMEDIATE( p_module->i_cpu );
2032 SAVE_IMMEDIATE( p_module->b_unloadable );
2033 SAVE_IMMEDIATE( p_module->b_reentrant );
2037 /* Fill-up file size */
2038 i_file_size = ftell( file );
2039 fseek( file, 0, SEEK_SET );
2040 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2047 void CacheSaveConfig( module_t *p_module, FILE *file )
2049 int i, j, i_lines = 0;
2050 module_config_t *p_item;
2053 SAVE_IMMEDIATE( p_module->i_config_items );
2054 SAVE_IMMEDIATE( p_module->i_bool_items );
2056 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2057 p_item++ ) i_lines++;
2059 SAVE_IMMEDIATE( i_lines );
2061 for( i = 0; i < i_lines ; i++ )
2063 SAVE_IMMEDIATE( p_module->p_config[i] );
2065 SAVE_STRING( p_module->p_config[i].psz_type );
2066 SAVE_STRING( p_module->p_config[i].psz_name );
2067 SAVE_STRING( p_module->p_config[i].psz_text );
2068 SAVE_STRING( p_module->p_config[i].psz_longtext );
2069 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2071 if( p_module->p_config[i].i_list )
2073 if( p_module->p_config[i].ppsz_list )
2075 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2076 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2079 if( p_module->p_config[i].ppsz_list_text )
2081 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2082 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2084 if( p_module->p_config[i].pi_list )
2086 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2087 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2091 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2092 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2094 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2098 /*****************************************************************************
2099 * CacheName: Return the cache file name for this platform.
2100 *****************************************************************************/
2101 static char *CacheName( void )
2103 static char psz_cachename[32];
2104 static vlc_bool_t b_initialised = VLC_FALSE;
2106 if( !b_initialised )
2108 /* Code int size, pointer size and endianness in the filename */
2109 int32_t x = 0xbe00001e;
2110 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2111 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2112 b_initialised = VLC_TRUE;
2115 return psz_cachename;
2118 /*****************************************************************************
2119 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2120 *****************************************************************************/
2121 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2122 module_t *p_module )
2126 p_cache->pf_activate = p_module->pf_activate;
2127 p_cache->pf_deactivate = p_module->pf_deactivate;
2128 p_cache->p_symbols = p_module->p_symbols;
2129 p_cache->handle = p_module->handle;
2131 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2133 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2134 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2135 p_cchild->pf_activate = p_child->pf_activate;
2136 p_cchild->pf_deactivate = p_child->pf_deactivate;
2137 p_cchild->p_symbols = p_child->p_symbols;
2140 p_cache->b_loaded = VLC_TRUE;
2141 p_module->b_loaded = VLC_FALSE;
2144 /*****************************************************************************
2145 * FindPluginCache: finds the cache entry corresponding to a file
2146 *****************************************************************************/
2147 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2148 int64_t i_time, int64_t i_size )
2150 module_cache_t **pp_cache;
2153 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2154 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2156 for( i = 0; i < i_cache; i++ )
2158 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2159 pp_cache[i]->i_time == i_time &&
2160 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2166 #endif /* HAVE_DYNAMIC_PLUGINS */