1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
29 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
30 * is set to 64. Don't try to be cleverer. */
31 #ifdef _FILE_OFFSET_BITS
32 #undef _FILE_OFFSET_BITS
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* sprintf() */
37 #include <string.h> /* strdup() */
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
53 #if !defined(HAVE_DYNAMIC_PLUGINS)
54 /* no support for plugins */
55 #elif 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)
78 #include "misc/configuration.h"
80 #include "vlc_interface.h"
81 #include "vlc_playlist.h"
83 #include "vlc_stream.h"
84 #include "vlc_access.h"
85 #include "vlc_demux.h"
88 #include "vlc_vout_synchro.h"
93 #include "vlc_httpd.h"
100 #include "iso_lang.h"
101 #include "vlc_charset.h"
103 #include "vlc_block.h"
107 #include "vlc_image.h"
110 #include "vlc_update.h"
111 #include "vlc_strings.h"
112 #include "vlc_streaming.h"
116 #if defined( _MSC_VER ) && defined( UNDER_CE )
117 # include "modules_builtin_evc.h"
118 #elif defined( _MSC_VER )
119 # include "modules_builtin_msvc.h"
121 # include "modules_builtin.h"
123 #include "vlc_network.h"
125 #if defined( WIN32 ) || defined( UNDER_CE )
126 /* Avoid name collisions */
127 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
130 /*****************************************************************************
132 *****************************************************************************/
133 #ifdef HAVE_DYNAMIC_PLUGINS
134 static void AllocateAllPlugins ( vlc_object_t * );
135 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
136 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
137 static module_t * AllocatePlugin( vlc_object_t *, char * );
139 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
140 static int DeleteModule ( module_t * );
141 #ifdef HAVE_DYNAMIC_PLUGINS
142 static void DupModule ( module_t * );
143 static void UndupModule ( module_t * );
144 static int CallEntry ( module_t * );
145 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
146 static void CloseModule ( module_handle_t );
147 static void * GetSymbol ( module_handle_t, const char * );
148 static void CacheLoad ( vlc_object_t * );
149 static int CacheLoadConfig ( module_t *, FILE * );
150 static void CacheSave ( vlc_object_t * );
151 static void CacheSaveConfig ( module_t *, FILE * );
152 static char * CacheName ( void );
153 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
154 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
156 #if defined(HAVE_DL_WINDOWS)
157 static char * GetWindowsError ( void );
161 static void module_LoadMain( vlc_object_t *p_this );
164 /* Sub-version number
165 * (only used to avoid breakage in dev version when cache structure changes) */
166 #define CACHE_SUBVERSION_NUM 1
168 /*****************************************************************************
169 * module_InitBank: create the module bank.
170 *****************************************************************************
171 * This function creates a module bank structure which will be filled later
172 * on with all the modules found.
173 *****************************************************************************/
174 void __module_InitBank( vlc_object_t *p_this )
176 module_bank_t *p_bank = NULL;
179 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
180 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
181 vlc_mutex_lock( lockval.p_address );
182 if( p_this->p_libvlc_global->p_module_bank )
184 p_this->p_libvlc_global->p_module_bank->i_usage++;
185 vlc_mutex_unlock( lockval.p_address );
186 var_Destroy( p_this->p_libvlc_global, "libvlc" );
189 vlc_mutex_unlock( lockval.p_address );
190 var_Destroy( p_this->p_libvlc_global, "libvlc" );
192 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
195 p_bank->psz_object_name = "module bank";
197 p_bank->i_cache = p_bank->i_loaded_cache = 0;
198 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
199 p_bank->b_cache = p_bank->b_cache_dirty =
200 p_bank->b_cache_delete = VLC_FALSE;
203 * Store the symbols to be exported
205 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
206 STORE_SYMBOLS( &p_bank->symbols );
209 /* Everything worked, attach the object */
210 p_this->p_libvlc_global->p_module_bank = p_bank;
211 vlc_object_attach( p_bank, p_this->p_libvlc_global );
213 module_LoadMain( p_this );
216 /*****************************************************************************
217 * module_ResetBank: reset the module bank.
218 *****************************************************************************
219 * This function resets the module bank by unloading all unused plugin
221 *****************************************************************************/
222 void __module_ResetBank( vlc_object_t *p_this )
224 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
228 /*****************************************************************************
229 * module_EndBank: empty the module bank.
230 *****************************************************************************
231 * This function unloads all unused plugin modules and empties the module
232 * bank in case of success.
233 *****************************************************************************/
234 void __module_EndBank( vlc_object_t *p_this )
236 module_t * p_next = NULL;
239 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
240 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
241 vlc_mutex_lock( lockval.p_address );
242 if( !p_this->p_libvlc_global->p_module_bank )
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc_global, "libvlc" );
248 if( --p_this->p_libvlc_global->p_module_bank->i_usage )
250 vlc_mutex_unlock( lockval.p_address );
251 var_Destroy( p_this->p_libvlc_global, "libvlc" );
254 vlc_mutex_unlock( lockval.p_address );
255 var_Destroy( p_this->p_libvlc_global, "libvlc" );
257 config_AutoSaveConfigFile( p_this );
259 #ifdef HAVE_DYNAMIC_PLUGINS
260 #define p_bank p_this->p_libvlc_global->p_module_bank
261 if( p_bank->b_cache ) CacheSave( p_this );
262 while( p_bank->i_loaded_cache-- )
264 if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
266 DeleteModule (p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module);
267 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
268 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
269 p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
272 if( p_bank->pp_loaded_cache )
274 free( p_bank->pp_loaded_cache );
275 p_bank->pp_loaded_cache = NULL;
277 while( p_bank->i_cache-- )
279 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
280 free( p_bank->pp_cache[p_bank->i_cache] );
281 p_bank->pp_cache[p_bank->i_cache] = NULL;
283 if( p_bank->pp_cache )
285 free( p_bank->pp_cache );
286 p_bank->pp_cache = NULL;
291 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
293 while( p_this->p_libvlc_global->p_module_bank->i_children )
295 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
297 if( DeleteModule( p_next ) )
299 /* Module deletion failed */
300 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
301 p_next->psz_object_name );
303 /* We just free the module by hand. Niahahahahaha. */
304 vlc_object_detach( p_next );
305 vlc_object_destroy( p_next );
309 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
310 p_this->p_libvlc_global->p_module_bank = NULL;
315 /*****************************************************************************
316 * module_LoadMain: load the main program info into the module bank.
317 *****************************************************************************
318 * This function fills the module bank structure with the main module infos.
319 * This is very useful as it will allow us to consider the main program just
320 * as another module, and for instance the configuration options of main will
321 * be available in the module bank structure just as for every other module.
322 *****************************************************************************/
323 static void module_LoadMain( vlc_object_t *p_this )
327 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
328 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
329 vlc_mutex_lock( lockval.p_address );
330 if( p_this->p_libvlc_global->p_module_bank->b_main )
332 vlc_mutex_unlock( lockval.p_address );
333 var_Destroy( p_this->p_libvlc_global, "libvlc" );
336 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
337 vlc_mutex_unlock( lockval.p_address );
338 var_Destroy( p_this->p_libvlc_global, "libvlc" );
340 AllocateBuiltinModule( p_this, vlc_entry__main );
343 /*****************************************************************************
344 * module_LoadBuiltins: load all modules which we built with.
345 *****************************************************************************
346 * This function fills the module bank structure with the builtin modules.
347 *****************************************************************************/
348 void __module_LoadBuiltins( vlc_object_t * p_this )
352 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
353 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
354 vlc_mutex_lock( lockval.p_address );
355 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
357 vlc_mutex_unlock( lockval.p_address );
358 var_Destroy( p_this->p_libvlc_global, "libvlc" );
361 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
362 vlc_mutex_unlock( lockval.p_address );
363 var_Destroy( p_this->p_libvlc_global, "libvlc" );
365 msg_Dbg( p_this, "checking builtin modules" );
366 ALLOCATE_ALL_BUILTINS();
369 /*****************************************************************************
370 * module_LoadPlugins: load all plugin modules we can find.
371 *****************************************************************************
372 * This function fills the module bank structure with the plugin modules.
373 *****************************************************************************/
374 void __module_LoadPlugins( vlc_object_t * p_this )
376 #ifdef HAVE_DYNAMIC_PLUGINS
379 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
380 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
381 vlc_mutex_lock( lockval.p_address );
382 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
384 vlc_mutex_unlock( lockval.p_address );
385 var_Destroy( p_this->p_libvlc_global, "libvlc" );
388 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
389 vlc_mutex_unlock( lockval.p_address );
390 var_Destroy( p_this->p_libvlc_global, "libvlc" );
392 msg_Dbg( p_this, "checking plugin modules" );
394 if( config_GetInt( p_this, "plugins-cache" ) )
395 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
397 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
398 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
400 AllocateAllPlugins( p_this );
404 /*****************************************************************************
405 * module_Need: return the best module function, given a capability list.
406 *****************************************************************************
407 * This function returns the module that best fits the asked capabilities.
408 *****************************************************************************/
409 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
410 const char *psz_name, vlc_bool_t b_strict )
412 typedef struct module_list_t module_list_t;
419 module_list_t *p_next;
422 module_list_t *p_list, *p_first, *p_tmp;
425 int i_which_module, i_index = 0;
426 vlc_bool_t b_intf = VLC_FALSE;
431 char *psz_shortcuts = NULL, *psz_var = NULL;
432 vlc_bool_t b_force_backup = p_this->b_force;
435 /* Deal with variables */
436 if( psz_name && psz_name[0] == '$' )
439 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
440 var_Get( p_this, psz_name + 1, &val );
441 psz_var = val.psz_string;
445 /* Count how many different shortcuts were asked for */
446 if( psz_name && *psz_name )
448 char *psz_parser, *psz_last_shortcut;
450 /* If the user wants none, give him none. */
451 if( !strcmp( psz_name, "none" ) )
453 if( psz_var ) free( psz_var );
458 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
460 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
462 if( *psz_parser == ',' )
466 psz_last_shortcut = psz_parser + 1;
470 /* Check if the user wants to override the "strict" mode */
471 if( psz_last_shortcut )
473 if( !strcmp(psz_last_shortcut, "none") )
478 else if( !strcmp(psz_last_shortcut, "any") )
480 b_strict = VLC_FALSE;
486 /* Sort the modules and test them */
487 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
488 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
491 /* Parse the module list for capabilities and probe each of them */
492 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
494 int i_shortcut_bonus = 0;
496 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
498 /* Test that this module can do what we need */
499 if( strcmp( p_module->psz_capability, psz_capability ) )
501 /* Don't recurse through the sub-modules because vlc_list_find()
502 * will list them anyway. */
506 /* Test if we have the required CPU */
507 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
512 /* If we required a shortcut, check this plugin provides it. */
513 if( i_shortcuts > 0 )
516 const char *psz_name = psz_shortcuts;
518 /* Let's drop modules with a <= 0 score (unless they are
519 * explicitly requested) */
520 b_trash = p_module->i_score <= 0;
522 for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
524 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
526 if( !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
529 i_shortcut_bonus = i_short * 10000;
534 /* Go to the next shortcut... This is so lame! */
535 psz_name += strlen( psz_name ) + 1;
538 /* If we are in "strict" mode and we couldn't
539 * find the module in the list of provided shortcuts,
540 * then kick the bastard out of here!!! */
544 /* If we didn't require a shortcut, trash <= 0 scored plugins */
545 else if( p_module->i_score <= 0 )
552 /* Special case: test if we requested a particular intf plugin */
553 if( !i_shortcuts && p_module->psz_program
554 && !strcmp( psz_capability, "interface" )
555 && !strcmp( p_module->psz_program,
556 p_this->p_libvlc->psz_object_name ) )
560 /* Remove previous non-matching plugins */
567 /* This one doesn't match */
571 /* Store this new module */
572 p_list[ i_index ].p_module = p_module;
573 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
574 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
576 /* Add it to the modules-to-probe list */
579 p_list[ 0 ].p_next = NULL;
584 /* Ok, so at school you learned that quicksort is quick, and
585 * bubble sort sucks raw eggs. But that's when dealing with
586 * thousands of items. Here we have barely 50. */
587 module_list_t *p_newlist = p_first;
589 if( p_first->i_score < p_list[ i_index ].i_score )
591 p_list[ i_index ].p_next = p_first;
592 p_first = &p_list[ i_index ];
596 while( p_newlist->p_next != NULL &&
597 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
599 p_newlist = p_newlist->p_next;
602 p_list[ i_index ].p_next = p_newlist->p_next;
603 p_newlist->p_next = &p_list[ i_index ];
610 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
611 i_index, i_index == 1 ? "" : "s" );
613 /* Lock all candidate modules */
615 while( p_tmp != NULL )
617 vlc_object_yield( p_tmp->p_module );
618 p_tmp = p_tmp->p_next;
621 /* We can release the list, interesting modules were yielded */
622 vlc_list_release( p_all );
624 /* Parse the linked list and use the first successful module */
626 while( p_tmp != NULL )
628 #ifdef HAVE_DYNAMIC_PLUGINS
629 /* Make sure the module is loaded in mem */
630 module_t *p_module = p_tmp->p_module;
631 if( p_module->b_submodule )
632 p_module = (module_t *)p_module->p_parent;
634 if( !p_module->b_builtin && !p_module->b_loaded )
636 module_t *p_new_module =
637 AllocatePlugin( p_this, p_module->psz_filename );
640 CacheMerge( p_this, p_module, p_new_module );
641 vlc_object_attach( p_new_module, p_module );
642 DeleteModule( p_new_module );
647 p_this->b_force = p_tmp->b_force;
648 if( p_tmp->p_module->pf_activate
649 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
654 vlc_object_release( p_tmp->p_module );
655 p_tmp = p_tmp->p_next;
658 /* Store the locked module value */
661 p_module = p_tmp->p_module;
662 p_tmp = p_tmp->p_next;
669 /* Unlock the remaining modules */
670 while( p_tmp != NULL )
672 vlc_object_release( p_tmp->p_module );
673 p_tmp = p_tmp->p_next;
677 p_this->b_force = b_force_backup;
679 if( p_module != NULL )
681 msg_Dbg( p_this, "using %s module \"%s\"",
682 psz_capability, p_module->psz_object_name );
684 else if( p_first == NULL )
686 if( !strcmp( psz_capability, "access_demux" ) )
688 msg_Warn( p_this, "no %s module matched \"%s\"",
689 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
693 msg_Err( p_this, "no %s module matched \"%s\"",
694 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
696 msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
697 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
700 else if( psz_name != NULL && *psz_name )
702 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
703 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
706 msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
710 free( psz_shortcuts );
718 /* Don't forget that the module is still locked */
722 /*****************************************************************************
723 * module_Unneed: decrease the usage count of a module.
724 *****************************************************************************
725 * This function must be called by the thread that called module_Need, to
726 * decrease the reference count and allow for hiding of modules.
727 *****************************************************************************/
728 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
730 /* Use the close method */
731 if( p_module->pf_deactivate )
733 p_module->pf_deactivate( p_this );
736 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
738 vlc_object_release( p_module );
743 /*****************************************************************************
744 * module_Exists: tell if a module exists.
745 *****************************************************************************
746 * This function is a boolean function that tells if a module exist or not.
747 *****************************************************************************/
749 vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
753 p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
754 for( i = 0 ; i < p_list->i_count; i++)
757 ((module_t *) p_list->p_values[i].p_object)->psz_shortname ,
760 /* We can release the list, and return yes */
761 vlc_list_release( p_list ); return VLC_TRUE;
764 vlc_list_release( p_list ); return VLC_FALSE;
768 /*****************************************************************************
769 * Following functions are local.
770 *****************************************************************************/
772 /*****************************************************************************
773 * AllocateAllPlugins: load all plugin modules we can find.
774 *****************************************************************************/
775 #ifdef HAVE_DYNAMIC_PLUGINS
776 static void AllocateAllPlugins( vlc_object_t *p_this )
778 /* Yes, there are two NULLs because we replace one with "plugin-path". */
779 #if defined( WIN32 ) || defined( UNDER_CE )
780 const char *path[] = { "modules", "", "plugins", NULL, NULL };
782 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
785 const char *const *ppsz_path;
787 /* If the user provided a plugin path, we add it to the list */
788 char *userpath = config_GetPsz( p_this, "plugin-path" );
789 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
791 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
795 if (!**ppsz_path) continue;
797 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
799 /* Handle relative as well as absolute paths */
801 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
802 (*ppsz_path)[1] != ':' )
804 if( (*ppsz_path)[0] != '/' )
807 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
808 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
813 psz_fullpath = strdup( *ppsz_path );
815 if( psz_fullpath == NULL )
818 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
820 /* Don't go deeper than 5 subdirectories */
821 AllocatePluginDir( p_this, psz_fullpath, 5 );
823 free( psz_fullpath );
826 /* Free plugin-path */
827 if( userpath != NULL )
831 /*****************************************************************************
832 * AllocatePluginDir: recursively parse a directory to look for plugins
833 *****************************************************************************/
834 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
837 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
843 #if defined( UNDER_CE ) || defined( _MSC_VER )
845 wchar_t psz_wpath[MAX_PATH + 256];
846 wchar_t psz_wdir[MAX_PATH];
848 char psz_path[MAX_PATH + 256];
849 WIN32_FIND_DATA finddata;
855 struct dirent * file;
859 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
864 #if defined( UNDER_CE ) || defined( _MSC_VER )
866 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
868 rc = GetFileAttributes( psz_wdir );
869 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
871 /* Parse all files in the directory */
872 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
874 rc = GetFileAttributes( psz_dir );
875 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
878 /* Parse all files in the directory */
879 sprintf( psz_path, "%s\\*", psz_dir );
882 handle = FindFirstFile( psz_wpath, &finddata );
884 handle = FindFirstFile( psz_path, &finddata );
886 if( handle == INVALID_HANDLE_VALUE )
888 /* Empty directory */
892 /* Parse the directory and try to load all files it contains. */
896 unsigned int i_len = wcslen( finddata.cFileName );
897 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
898 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
900 unsigned int i_len = strlen( finddata.cFileName );
901 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
905 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
906 || !strcmp( finddata.cFileName, ".." ) )
908 if( !FindNextFile( handle, &finddata ) ) break;
913 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
915 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
918 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
920 else if( i_len > strlen( LIBEXT )
921 /* We only load files ending with LIBEXT */
922 && !strncasecmp( psz_path + strlen( psz_path)
924 LIBEXT, strlen( LIBEXT ) ) )
926 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
927 int64_t i_time = 0, i_size = 0;
930 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
933 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
937 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
939 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
940 i_size = attrbuf.nFileSizeHigh;
942 i_size |= attrbuf.nFileSizeLow;
946 AllocatePluginFile( p_this, psz_file, i_time, i_size );
949 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
951 /* Close the directory */
955 dir = opendir( psz_dir );
961 i_dirlen = strlen( psz_dir );
963 /* Parse the directory and try to load all files it contains. */
964 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
971 if( !*file->d_name || !strcmp( file->d_name, "." )
972 || !strcmp( file->d_name, ".." ) )
977 i_len = strlen( file->d_name );
978 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
980 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
982 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
985 i_stat = stat( psz_file, &statbuf );
986 if( !i_stat && statbuf.st_mode & S_IFDIR )
988 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
990 else if( i_len > strlen( LIBEXT )
991 /* We only load files ending with LIBEXT */
992 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
993 LIBEXT, strlen( LIBEXT ) ) )
995 int64_t i_time = 0, i_size = 0;
999 i_time = statbuf.st_mtime;
1000 i_size = statbuf.st_size;
1003 AllocatePluginFile( p_this, psz_file, i_time, i_size );
1009 /* Close the directory */
1015 /*****************************************************************************
1016 * AllocatePluginFile: load a module into memory and initialize it.
1017 *****************************************************************************
1018 * This function loads a dynamically loadable module and allocates a structure
1019 * for its information data. The module can then be handled by module_Need
1020 * and module_Unneed. It can be removed by DeleteModule.
1021 *****************************************************************************/
1022 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1023 int64_t i_file_time, int64_t i_file_size )
1025 module_t * p_module = NULL;
1026 module_cache_t *p_cache_entry = NULL;
1029 * Check our plugins cache first then load plugin if needed
1032 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1034 if( !p_cache_entry )
1036 p_module = AllocatePlugin( p_this, psz_file );
1040 /* If junk dll, don't try to load it */
1041 if( p_cache_entry->b_junk )
1047 module_config_t *p_item = NULL, *p_end = NULL;
1049 p_module = p_cache_entry->p_module;
1050 p_module->b_loaded = VLC_FALSE;
1052 /* For now we force loading if the module's config contains
1053 * callbacks or actions.
1054 * Could be optimized by adding an API call.*/
1055 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1056 p_item < p_end; p_item++ )
1058 if( p_item->pf_callback || p_item->i_action )
1059 p_module = AllocatePlugin( p_this, psz_file );
1066 /* Everything worked fine !
1067 * The module is ready to be added to the list. */
1068 p_module->b_builtin = VLC_FALSE;
1070 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1071 p_module->psz_object_name, p_module->psz_longname ); */
1073 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1075 if( !p_this->p_libvlc_global->p_module_bank->b_cache )
1078 /* Add entry to cache */
1079 #define p_bank p_this->p_libvlc_global->p_module_bank
1081 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1082 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1083 if( !p_bank->pp_cache[p_bank->i_cache] )
1085 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1086 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1087 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1088 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1089 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1093 return p_module ? 0 : -1;
1096 /*****************************************************************************
1097 * AllocatePlugin: load a module into memory and initialize it.
1098 *****************************************************************************
1099 * This function loads a dynamically loadable module and allocates a structure
1100 * for its information data. The module can then be handled by module_Need
1101 * and module_Unneed. It can be removed by DeleteModule.
1102 *****************************************************************************/
1103 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1105 module_t * p_module = NULL;
1106 module_handle_t handle;
1108 if( LoadModule( p_this, psz_file, &handle ) )
1111 /* Now that we have successfully loaded the module, we can
1112 * allocate a structure for it */
1113 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1114 if( p_module == NULL )
1116 msg_Err( p_this, "out of memory" );
1117 CloseModule( handle );
1121 /* We need to fill these since they may be needed by CallEntry() */
1122 p_module->psz_filename = psz_file;
1123 p_module->handle = handle;
1124 #ifndef HAVE_SHARED_LIBVLC
1125 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1127 p_module->b_loaded = VLC_TRUE;
1129 /* Initialize the module: fill p_module, default config */
1130 if( CallEntry( p_module ) != 0 )
1132 /* We couldn't call module_init() */
1133 vlc_object_destroy( p_module );
1134 CloseModule( handle );
1138 DupModule( p_module );
1139 p_module->psz_filename = strdup( p_module->psz_filename );
1141 /* Everything worked fine ! The module is ready to be added to the list. */
1142 p_module->b_builtin = VLC_FALSE;
1147 /*****************************************************************************
1148 * DupModule: make a plugin module standalone.
1149 *****************************************************************************
1150 * This function duplicates all strings in the module, so that the dynamic
1151 * object can be unloaded. It acts recursively on submodules.
1152 *****************************************************************************/
1153 static void DupModule( module_t *p_module )
1155 const char **pp_shortcut;
1158 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1160 *pp_shortcut = strdup( *pp_shortcut );
1163 /* We strdup() these entries so that they are still valid when the
1164 * module is unloaded. */
1165 p_module->psz_object_name = strdup( p_module->psz_object_name );
1166 p_module->psz_capability = strdup( p_module->psz_capability );
1167 p_module->psz_shortname = p_module->psz_shortname ?
1168 strdup( p_module->psz_shortname ) : NULL;
1169 p_module->psz_longname = strdup( p_module->psz_longname );
1171 if( p_module->psz_program != NULL )
1173 p_module->psz_program = strdup( p_module->psz_program );
1176 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1178 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1182 /*****************************************************************************
1183 * UndupModule: free a duplicated module.
1184 *****************************************************************************
1185 * This function frees the allocations done in DupModule().
1186 *****************************************************************************/
1187 static void UndupModule( module_t *p_module )
1189 const char **pp_shortcut;
1192 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1194 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1197 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1199 free( (void *)*pp_shortcut );
1202 free( (void *)p_module->psz_object_name );
1203 free( (void *)p_module->psz_capability );
1204 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1205 free( (void *)p_module->psz_longname );
1207 if( p_module->psz_program != NULL )
1209 free( (void *)p_module->psz_program );
1213 #endif /* HAVE_DYNAMIC_PLUGINS */
1215 /*****************************************************************************
1216 * AllocateBuiltinModule: initialize a builtin module.
1217 *****************************************************************************
1218 * This function registers a builtin module and allocates a structure
1219 * for its information data. The module can then be handled by module_Need
1220 * and module_Unneed. It can be removed by DeleteModule.
1221 *****************************************************************************/
1222 static int AllocateBuiltinModule( vlc_object_t * p_this,
1223 int ( *pf_entry ) ( module_t * ) )
1225 module_t * p_module;
1227 /* Now that we have successfully loaded the module, we can
1228 * allocate a structure for it */
1229 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1230 if( p_module == NULL )
1232 msg_Err( p_this, "out of memory" );
1236 /* Initialize the module : fill p_module->psz_object_name, etc. */
1237 if( pf_entry( p_module ) != 0 )
1239 /* With a well-written module we shouldn't have to print an
1240 * additional error message here, but just make sure. */
1241 msg_Err( p_this, "failed calling entry point in builtin module" );
1242 vlc_object_destroy( p_module );
1246 /* Everything worked fine ! The module is ready to be added to the list. */
1247 p_module->b_builtin = VLC_TRUE;
1249 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1250 p_module->psz_object_name, p_module->psz_longname ); */
1252 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1257 /*****************************************************************************
1258 * DeleteModule: delete a module and its structure.
1259 *****************************************************************************
1260 * This function can only be called if the module isn't being used.
1261 *****************************************************************************/
1262 static int DeleteModule( module_t * p_module )
1264 if( !p_module ) return VLC_EGENERIC;
1265 vlc_object_detach( p_module );
1267 /* We free the structures that we strdup()ed in Allocate*Module(). */
1268 #ifdef HAVE_DYNAMIC_PLUGINS
1269 if( !p_module->b_builtin )
1271 if( p_module->b_loaded && p_module->b_unloadable )
1273 CloseModule( p_module->handle );
1275 UndupModule( p_module );
1276 free( p_module->psz_filename );
1280 /* Free and detach the object's children */
1281 while( p_module->i_children )
1283 vlc_object_t *p_this = p_module->pp_children[0];
1284 vlc_object_detach( p_this );
1285 vlc_object_destroy( p_this );
1288 config_Free( p_module );
1289 vlc_object_destroy( p_module );
1294 #ifdef HAVE_DYNAMIC_PLUGINS
1295 /*****************************************************************************
1296 * CallEntry: call an entry point.
1297 *****************************************************************************
1298 * This function calls a symbol given its name and a module structure. The
1299 * symbol MUST refer to a function returning int and taking a module_t* as
1301 *****************************************************************************/
1302 static int CallEntry( module_t * p_module )
1304 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1305 int (* pf_symbol) ( module_t * p_module );
1307 /* Try to resolve the symbol */
1308 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1310 if( pf_symbol == NULL )
1312 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1313 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1314 psz_name, p_module->psz_filename );
1315 #elif defined(HAVE_DL_WINDOWS)
1316 char *psz_error = GetWindowsError();
1317 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1318 psz_name, p_module->psz_filename, psz_error );
1320 #elif defined(HAVE_DL_DLOPEN)
1321 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1322 psz_name, p_module->psz_filename, dlerror() );
1323 #elif defined(HAVE_DL_SHL_LOAD)
1324 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1325 psz_name, p_module->psz_filename, strerror(errno) );
1327 # error "Something is wrong in modules.c"
1332 /* We can now try to call the symbol */
1333 if( pf_symbol( p_module ) != 0 )
1335 /* With a well-written module we shouldn't have to print an
1336 * additional error message here, but just make sure. */
1337 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1338 psz_name, p_module->psz_filename );
1342 /* Everything worked fine, we can return */
1346 /*****************************************************************************
1347 * LoadModule: loads a dynamic library
1348 *****************************************************************************
1349 * This function loads a dynamically linked library using a system dependant
1350 * method. Will return 0 on success as well as the module handle.
1351 *****************************************************************************/
1352 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1353 module_handle_t *p_handle )
1355 module_handle_t handle;
1357 #if defined(HAVE_DL_DYLD)
1358 NSObjectFileImage image;
1359 NSObjectFileImageReturnCode ret;
1361 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1363 if( ret != NSObjectFileImageSuccess )
1365 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1369 /* Open the dynamic module */
1370 handle = NSLinkModule( image, psz_file,
1371 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1375 NSLinkEditErrors errors;
1376 const char *psz_file, *psz_err;
1378 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1379 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1380 NSDestroyObjectFileImage( image );
1384 /* Destroy our image, we won't need it */
1385 NSDestroyObjectFileImage( image );
1387 #elif defined(HAVE_DL_BEOS)
1388 handle = load_add_on( psz_file );
1391 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1395 #elif defined(HAVE_DL_WINDOWS)
1398 wchar_t psz_wfile[MAX_PATH];
1399 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1400 handle = LoadLibrary( psz_wfile );
1403 handle = LoadLibrary( psz_file );
1405 if( handle == NULL )
1407 char *psz_err = GetWindowsError();
1408 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1413 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1414 /* static is OK, we are called atomically */
1416 # if defined(SYS_LINUX)
1417 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1418 * are going to get namespace collisions when two modules have common
1419 * public symbols, but ALSA is being a pest here. */
1420 if( strstr( psz_file, "alsa_plugin" ) )
1422 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1423 if( handle == NULL )
1425 msg_Warn( p_this, "cannot load module `%s' (%s)",
1426 psz_file, dlerror() );
1432 handle = dlopen( psz_file, RTLD_NOW );
1433 if( handle == NULL )
1435 msg_Warn( p_this, "cannot load module `%s' (%s)",
1436 psz_file, dlerror() );
1440 #elif defined(HAVE_DL_DLOPEN)
1441 # if defined(DL_LAZY)
1442 handle = dlopen( psz_file, DL_LAZY );
1444 handle = dlopen( psz_file, 0 );
1446 if( handle == NULL )
1448 msg_Warn( p_this, "cannot load module `%s' (%s)",
1449 psz_file, dlerror() );
1453 #elif defined(HAVE_DL_SHL_LOAD)
1454 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1455 if( handle == NULL )
1457 msg_Warn( p_this, "cannot load module `%s' (%s)",
1458 psz_file, strerror(errno) );
1463 # error "Something is wrong in modules.c"
1471 /*****************************************************************************
1472 * CloseModule: unload a dynamic library
1473 *****************************************************************************
1474 * This function unloads a previously opened dynamically linked library
1475 * using a system dependant method. No return value is taken in consideration,
1476 * since some libraries sometimes refuse to close properly.
1477 *****************************************************************************/
1478 static void CloseModule( module_handle_t handle )
1480 #if defined(HAVE_DL_DYLD)
1481 NSUnLinkModule( handle, FALSE );
1483 #elif defined(HAVE_DL_BEOS)
1484 unload_add_on( handle );
1486 #elif defined(HAVE_DL_WINDOWS)
1487 FreeLibrary( handle );
1489 #elif defined(HAVE_DL_DLOPEN)
1492 #elif defined(HAVE_DL_SHL_LOAD)
1493 shl_unload( handle );
1499 /*****************************************************************************
1500 * GetSymbol: get a symbol from a dynamic library
1501 *****************************************************************************
1502 * This function queries a loaded library for a symbol specified in a
1503 * string, and returns a pointer to it. We don't check for dlerror() or
1504 * similar functions, since we want a non-NULL symbol anyway.
1505 *****************************************************************************/
1506 static void * _module_getsymbol( module_handle_t, const char * );
1508 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1510 void * p_symbol = _module_getsymbol( handle, psz_function );
1512 /* MacOS X dl library expects symbols to begin with "_". So do
1513 * some other operating systems. That's really lame, but hey, what
1515 if( p_symbol == NULL )
1517 char *psz_call = malloc( strlen( psz_function ) + 2 );
1519 strcpy( psz_call + 1, psz_function );
1520 psz_call[ 0 ] = '_';
1521 p_symbol = _module_getsymbol( handle, psz_call );
1528 static void * _module_getsymbol( module_handle_t handle,
1529 const char * psz_function )
1531 #if defined(HAVE_DL_DYLD)
1532 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1533 return NSAddressOfSymbol( sym );
1535 #elif defined(HAVE_DL_BEOS)
1537 if( B_OK == get_image_symbol( handle, psz_function,
1538 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1547 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1548 wchar_t psz_real[256];
1549 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1551 return (void *)GetProcAddress( handle, psz_real );
1553 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1554 return (void *)GetProcAddress( handle, (char *)psz_function );
1556 #elif defined(HAVE_DL_DLOPEN)
1557 return dlsym( handle, psz_function );
1559 #elif defined(HAVE_DL_SHL_LOAD)
1561 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1567 #if defined(HAVE_DL_WINDOWS)
1568 static char * GetWindowsError( void )
1570 #if defined(UNDER_CE)
1571 wchar_t psz_tmp[MAX_PATH];
1572 char * psz_buffer = malloc( MAX_PATH );
1574 char * psz_tmp = malloc( MAX_PATH );
1576 int i = 0, i_error = GetLastError();
1578 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1579 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1580 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1582 /* Go to the end of the string */
1583 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1590 #if defined(UNDER_CE)
1591 swprintf( psz_tmp + i, L" (error %i)", i_error );
1592 psz_tmp[ 255 ] = L'\0';
1594 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1595 psz_tmp[ 255 ] = '\0';
1599 #if defined(UNDER_CE)
1600 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1606 #endif /* HAVE_DL_WINDOWS */
1608 /*****************************************************************************
1609 * LoadPluginsCache: loads the plugins cache file
1610 *****************************************************************************
1611 * This function will load the plugin cache if present and valid. This cache
1612 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1613 * actually load the dynamically loadable module.
1614 * This allows us to only fully load plugins when they are actually used.
1615 *****************************************************************************/
1616 static void CacheLoad( vlc_object_t *p_this )
1618 char *psz_filename, *psz_homedir;
1620 int i, j, i_size, i_read;
1621 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1622 char p_cachelang[6], p_lang[6];
1624 module_cache_t **pp_cache = 0;
1625 int32_t i_file_size, i_marker;
1627 psz_homedir = p_this->p_libvlc->psz_homedir;
1630 msg_Err( p_this, "psz_homedir is null" );
1634 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1635 PLUGINSCACHE_DIR, CacheName() );
1638 msg_Err( p_this, "out of memory" );
1642 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1644 #if !defined( UNDER_CE )
1645 unlink( psz_filename );
1647 wchar_t psz_wf[MAX_PATH];
1648 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1649 DeleteFile( psz_wf );
1651 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1652 free( psz_filename );
1656 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1658 file = utf8_fopen( psz_filename, "rb" );
1661 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1663 free( psz_filename );
1666 free( psz_filename );
1668 /* Check the file size */
1669 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1670 if( i_read != sizeof(i_file_size) )
1672 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1678 fseek( file, 0, SEEK_END );
1679 if( ftell( file ) != i_file_size )
1681 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1682 "(corrupted size)" );
1686 fseek( file, sizeof(i_file_size), SEEK_SET );
1688 /* Check the file is a plugins cache */
1689 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1690 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1691 if( i_read != i_size ||
1692 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1694 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1699 /* Check Sub-version number */
1700 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1701 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1703 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1704 "(corrupted header)" );
1709 /* Check the language hasn't changed */
1710 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1711 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1712 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1714 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1715 "(language changed)" );
1720 /* Check header marker */
1721 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1722 if( i_read != sizeof(i_marker) ||
1723 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1725 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1726 "(corrupted header)" );
1731 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1732 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1734 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1735 malloc( i_cache * sizeof(void *) );
1737 #define LOAD_IMMEDIATE(a) \
1738 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1739 #define LOAD_STRING(a) \
1742 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1743 || ( i_size > 16384 ) ) \
1746 char *psz = malloc( i_size ); \
1747 if( fread( psz, i_size, 1, file ) != 1 ) { \
1751 if( psz[i_size-1] ) { \
1759 for( i = 0; i < i_cache; i++ )
1764 pp_cache[i] = malloc( sizeof(module_cache_t) );
1765 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1767 /* Load common info */
1768 LOAD_STRING( pp_cache[i]->psz_file );
1769 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1770 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1771 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1773 if( pp_cache[i]->b_junk ) continue;
1775 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1777 /* Load additional infos */
1778 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1779 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1780 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1781 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1782 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1784 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1786 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1787 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1788 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1789 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1790 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1791 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1794 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1797 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1799 LOAD_IMMEDIATE( i_submodules );
1801 while( i_submodules-- )
1803 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1804 vlc_object_attach( p_module, pp_cache[i]->p_module );
1805 p_module->b_submodule = VLC_TRUE;
1807 LOAD_STRING( p_module->psz_object_name );
1808 LOAD_STRING( p_module->psz_shortname );
1809 LOAD_STRING( p_module->psz_longname );
1810 LOAD_STRING( p_module->psz_program );
1811 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1813 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1815 LOAD_STRING( p_module->psz_capability );
1816 LOAD_IMMEDIATE( p_module->i_score );
1817 LOAD_IMMEDIATE( p_module->i_cpu );
1818 LOAD_IMMEDIATE( p_module->b_unloadable );
1819 LOAD_IMMEDIATE( p_module->b_reentrant );
1828 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1831 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1838 int CacheLoadConfig( module_t *p_module, FILE *file )
1843 /* Calculate the structure length */
1844 LOAD_IMMEDIATE( p_module->i_config_items );
1845 LOAD_IMMEDIATE( p_module->i_bool_items );
1847 LOAD_IMMEDIATE( i_lines );
1849 /* Allocate memory */
1852 p_module->p_config =
1853 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1854 if( p_module->p_config == NULL )
1856 p_module->confsize = 0;
1857 msg_Err( p_module, "config error: can't duplicate p_config" );
1861 p_module->confsize = i_lines;
1863 /* Do the duplication job */
1864 for (size_t i = 0; i < i_lines; i++ )
1866 LOAD_IMMEDIATE( p_module->p_config[i] );
1868 LOAD_STRING( p_module->p_config[i].psz_type );
1869 LOAD_STRING( p_module->p_config[i].psz_name );
1870 LOAD_STRING( p_module->p_config[i].psz_text );
1871 LOAD_STRING( p_module->p_config[i].psz_longtext );
1872 LOAD_STRING( p_module->p_config[i].psz_current );
1874 if (IsConfigStringType (p_module->p_config[i].i_type))
1876 LOAD_STRING (p_module->p_config[i].orig.psz);
1877 p_module->p_config[i].value.psz =
1878 (p_module->p_config[i].orig.psz != NULL)
1879 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1880 p_module->p_config[i].saved.psz = NULL;
1884 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1885 sizeof (p_module->p_config[i].value));
1886 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1887 sizeof (p_module->p_config[i].saved));
1890 p_module->p_config[i].b_dirty = VLC_FALSE;
1892 p_module->p_config[i].p_lock = &p_module->object_lock;
1894 if( p_module->p_config[i].i_list )
1896 if( p_module->p_config[i].ppsz_list )
1899 p_module->p_config[i].ppsz_list =
1900 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1901 if( p_module->p_config[i].ppsz_list )
1903 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1904 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1905 p_module->p_config[i].ppsz_list[j] = NULL;
1908 if( p_module->p_config[i].ppsz_list_text )
1911 p_module->p_config[i].ppsz_list_text =
1912 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1913 if( p_module->p_config[i].ppsz_list_text )
1915 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1916 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1917 p_module->p_config[i].ppsz_list_text[j] = NULL;
1920 if( p_module->p_config[i].pi_list )
1922 p_module->p_config[i].pi_list =
1923 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1924 if( p_module->p_config[i].pi_list )
1926 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1927 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1932 if( p_module->p_config[i].i_action )
1934 p_module->p_config[i].ppf_action =
1935 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1936 p_module->p_config[i].ppsz_action_text =
1937 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1939 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1941 p_module->p_config[i].ppf_action[j] = 0;
1942 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1946 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1953 return VLC_EGENERIC;
1956 /*****************************************************************************
1957 * SavePluginsCache: saves the plugins cache to a file
1958 *****************************************************************************/
1959 static void CacheSave( vlc_object_t *p_this )
1961 static char const psz_tag[] =
1962 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1963 "# This file is a cache directory tag created by VLC.\r\n"
1964 "# For information about cache directory tags, see:\r\n"
1965 "# http://www.brynosaurus.com/cachedir/\r\n";
1967 char *psz_filename, *psz_homedir;
1970 module_cache_t **pp_cache;
1971 int32_t i_file_size = 0;
1973 psz_homedir = p_this->p_libvlc->psz_homedir;
1976 msg_Err( p_this, "psz_homedir is null" );
1980 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1981 strlen(psz_homedir) + strlen(CacheName()) );
1985 msg_Err( p_this, "out of memory" );
1989 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1991 config_CreateDir( p_this, psz_filename );
1993 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1995 config_CreateDir( p_this, psz_filename );
1997 strcat( psz_filename, "/CACHEDIR.TAG" );
1999 file = utf8_fopen( psz_filename, "wb" );
2002 fwrite( psz_tag, 1, strlen(psz_tag), file );
2006 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
2007 PLUGINSCACHE_DIR, CacheName() );
2009 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2011 file = utf8_fopen( psz_filename, "wb" );
2014 msg_Warn( p_this, "could not open plugins cache file %s for writing",
2016 free( psz_filename );
2019 free( psz_filename );
2021 /* Empty space for file size */
2022 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2024 /* Contains version number */
2025 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2027 /* Sub-version number (to avoid breakage in the dev version when cache
2028 * structure changes) */
2029 i_file_size = CACHE_SUBVERSION_NUM;
2030 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2033 fprintf( file, "%5.5s", _("C") );
2036 i_file_size = ftell( file );
2037 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2039 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2040 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2042 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2044 #define SAVE_IMMEDIATE(a) \
2045 fwrite( &a, sizeof(char), sizeof(a), file )
2046 #define SAVE_STRING(a) \
2047 { i_size = a ? strlen( a ) + 1 : 0; \
2048 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2049 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2052 for( i = 0; i < i_cache; i++ )
2055 uint32_t i_submodule;
2057 /* Save common info */
2058 SAVE_STRING( pp_cache[i]->psz_file );
2059 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2060 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2061 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2063 if( pp_cache[i]->b_junk ) continue;
2065 /* Save additional infos */
2066 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2067 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2068 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2069 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2070 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2072 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2074 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2075 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2076 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2077 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2078 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2079 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2082 CacheSaveConfig( pp_cache[i]->p_module, file );
2084 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2086 i_submodule = pp_cache[i]->p_module->i_children;
2087 SAVE_IMMEDIATE( i_submodule );
2088 for( i_submodule = 0;
2089 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2092 module_t *p_module =
2093 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2095 SAVE_STRING( p_module->psz_object_name );
2096 SAVE_STRING( p_module->psz_shortname );
2097 SAVE_STRING( p_module->psz_longname );
2098 SAVE_STRING( p_module->psz_program );
2099 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2101 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2103 SAVE_STRING( p_module->psz_capability );
2104 SAVE_IMMEDIATE( p_module->i_score );
2105 SAVE_IMMEDIATE( p_module->i_cpu );
2106 SAVE_IMMEDIATE( p_module->b_unloadable );
2107 SAVE_IMMEDIATE( p_module->b_reentrant );
2111 /* Fill-up file size */
2112 i_file_size = ftell( file );
2113 fseek( file, 0, SEEK_SET );
2114 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2121 void CacheSaveConfig( module_t *p_module, FILE *file )
2123 uint32_t i_lines = p_module->confsize;
2126 SAVE_IMMEDIATE( p_module->i_config_items );
2127 SAVE_IMMEDIATE( p_module->i_bool_items );
2128 SAVE_IMMEDIATE( i_lines );
2130 for (size_t i = 0; i < i_lines ; i++)
2132 SAVE_IMMEDIATE( p_module->p_config[i] );
2134 SAVE_STRING( p_module->p_config[i].psz_type );
2135 SAVE_STRING( p_module->p_config[i].psz_name );
2136 SAVE_STRING( p_module->p_config[i].psz_text );
2137 SAVE_STRING( p_module->p_config[i].psz_longtext );
2138 SAVE_STRING( p_module->p_config[i].psz_current );
2139 if (IsConfigStringType (p_module->p_config[i].i_type))
2140 SAVE_STRING( p_module->p_config[i].orig.psz );
2142 if( p_module->p_config[i].i_list )
2144 if( p_module->p_config[i].ppsz_list )
2146 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2147 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2150 if( p_module->p_config[i].ppsz_list_text )
2152 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2153 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2155 if( p_module->p_config[i].pi_list )
2157 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2158 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2162 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2163 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2165 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2169 /*****************************************************************************
2170 * CacheName: Return the cache file name for this platform.
2171 *****************************************************************************/
2172 static char *CacheName( void )
2174 static char psz_cachename[32];
2176 /* Code int size, pointer size and endianness in the filename */
2177 int32_t x = 0xbe00001e;
2178 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2179 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2180 return psz_cachename;
2183 /*****************************************************************************
2184 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2185 *****************************************************************************/
2186 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2187 module_t *p_module )
2191 p_cache->pf_activate = p_module->pf_activate;
2192 p_cache->pf_deactivate = p_module->pf_deactivate;
2193 #ifndef HAVE_SHARED_LIBVLC
2194 p_cache->p_symbols = p_module->p_symbols;
2196 p_cache->handle = p_module->handle;
2198 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2200 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2201 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2202 p_cchild->pf_activate = p_child->pf_activate;
2203 p_cchild->pf_deactivate = p_child->pf_deactivate;
2204 #ifndef HAVE_SHARED_LIBVLC
2205 p_cchild->p_symbols = p_child->p_symbols;
2209 p_cache->b_loaded = VLC_TRUE;
2210 p_module->b_loaded = VLC_FALSE;
2213 /*****************************************************************************
2214 * FindPluginCache: finds the cache entry corresponding to a file
2215 *****************************************************************************/
2216 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2217 int64_t i_time, int64_t i_size )
2219 module_cache_t **pp_cache;
2222 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2223 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2225 for( i = 0; i < i_cache; i++ )
2227 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2228 pp_cache[i]->i_time == i_time &&
2229 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2235 #endif /* HAVE_DYNAMIC_PLUGINS */