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;
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) );
193 p_bank->psz_object_name = "module bank";
195 p_bank->i_cache = p_bank->i_loaded_cache = 0;
196 p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
197 p_bank->b_cache = p_bank->b_cache_dirty =
198 p_bank->b_cache_delete = VLC_FALSE;
201 * Store the symbols to be exported
203 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
204 STORE_SYMBOLS( &p_bank->symbols );
207 /* Everything worked, attach the object */
208 p_this->p_libvlc_global->p_module_bank = p_bank;
209 vlc_object_attach( p_bank, p_this->p_libvlc_global );
211 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 )
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 DeleteModule (p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module);
265 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
266 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
268 if( p_bank->pp_loaded_cache )
269 free( p_bank->pp_loaded_cache );
271 while( p_bank->i_cache-- )
273 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
274 free( p_bank->pp_cache[p_bank->i_cache] );
276 if( p_bank->pp_cache )
277 free( p_bank->pp_cache );
281 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
283 while( p_this->p_libvlc_global->p_module_bank->i_children )
285 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
287 if( DeleteModule( p_next ) )
289 /* Module deletion failed */
290 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
291 p_next->psz_object_name );
293 /* We just free the module by hand. Niahahahahaha. */
294 vlc_object_detach( p_next );
295 vlc_object_destroy( p_next );
299 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
300 p_this->p_libvlc_global->p_module_bank = NULL;
305 /*****************************************************************************
306 * module_LoadMain: load the main program info into the module bank.
307 *****************************************************************************
308 * This function fills the module bank structure with the main module infos.
309 * This is very useful as it will allow us to consider the main program just
310 * as another module, and for instance the configuration options of main will
311 * be available in the module bank structure just as for every other module.
312 *****************************************************************************/
313 static void module_LoadMain( vlc_object_t *p_this )
317 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
318 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
319 vlc_mutex_lock( lockval.p_address );
320 if( p_this->p_libvlc_global->p_module_bank->b_main )
322 vlc_mutex_unlock( lockval.p_address );
323 var_Destroy( p_this->p_libvlc_global, "libvlc" );
326 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
327 vlc_mutex_unlock( lockval.p_address );
328 var_Destroy( p_this->p_libvlc_global, "libvlc" );
330 AllocateBuiltinModule( p_this, vlc_entry__main );
333 /*****************************************************************************
334 * module_LoadBuiltins: load all modules which we built with.
335 *****************************************************************************
336 * This function fills the module bank structure with the builtin modules.
337 *****************************************************************************/
338 void __module_LoadBuiltins( vlc_object_t * p_this )
342 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
343 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
344 vlc_mutex_lock( lockval.p_address );
345 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
347 vlc_mutex_unlock( lockval.p_address );
348 var_Destroy( p_this->p_libvlc_global, "libvlc" );
351 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
352 vlc_mutex_unlock( lockval.p_address );
353 var_Destroy( p_this->p_libvlc_global, "libvlc" );
355 msg_Dbg( p_this, "checking builtin modules" );
356 ALLOCATE_ALL_BUILTINS();
359 /*****************************************************************************
360 * module_LoadPlugins: load all plugin modules we can find.
361 *****************************************************************************
362 * This function fills the module bank structure with the plugin modules.
363 *****************************************************************************/
364 void __module_LoadPlugins( vlc_object_t * p_this )
366 #ifdef HAVE_DYNAMIC_PLUGINS
369 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
370 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
371 vlc_mutex_lock( lockval.p_address );
372 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
374 vlc_mutex_unlock( lockval.p_address );
375 var_Destroy( p_this->p_libvlc_global, "libvlc" );
378 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
379 vlc_mutex_unlock( lockval.p_address );
380 var_Destroy( p_this->p_libvlc_global, "libvlc" );
382 msg_Dbg( p_this, "checking plugin modules" );
384 if( config_GetInt( p_this, "plugins-cache" ) )
385 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
387 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
388 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
390 AllocateAllPlugins( p_this );
394 /*****************************************************************************
395 * module_Need: return the best module function, given a capability list.
396 *****************************************************************************
397 * This function returns the module that best fits the asked capabilities.
398 *****************************************************************************/
399 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
400 const char *psz_name, vlc_bool_t b_strict )
402 typedef struct module_list_t module_list_t;
409 module_list_t *p_next;
412 module_list_t *p_list, *p_first, *p_tmp;
415 int i_which_module, i_index = 0;
416 vlc_bool_t b_intf = VLC_FALSE;
421 char *psz_shortcuts = NULL, *psz_var = NULL;
422 vlc_bool_t b_force_backup = p_this->b_force;
425 /* Deal with variables */
426 if( psz_name && psz_name[0] == '$' )
429 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
430 var_Get( p_this, psz_name + 1, &val );
431 psz_var = val.psz_string;
435 /* Count how many different shortcuts were asked for */
436 if( psz_name && *psz_name )
438 char *psz_parser, *psz_last_shortcut;
440 /* If the user wants none, give him none. */
441 if( !strcmp( psz_name, "none" ) )
443 if( psz_var ) free( psz_var );
448 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
450 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
452 if( *psz_parser == ',' )
456 psz_last_shortcut = psz_parser + 1;
460 /* Check if the user wants to override the "strict" mode */
461 if( psz_last_shortcut )
463 if( !strcmp(psz_last_shortcut, "none") )
468 else if( !strcmp(psz_last_shortcut, "any") )
470 b_strict = VLC_FALSE;
476 /* Sort the modules and test them */
477 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
478 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
481 /* Parse the module list for capabilities and probe each of them */
482 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
484 int i_shortcut_bonus = 0;
486 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
488 /* Test that this module can do what we need */
489 if( strcmp( p_module->psz_capability, psz_capability ) )
491 /* Don't recurse through the sub-modules because vlc_list_find()
492 * will list them anyway. */
496 /* Test if we have the required CPU */
497 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
502 /* If we required a shortcut, check this plugin provides it. */
503 if( i_shortcuts > 0 )
506 int i_dummy, i_short = i_shortcuts;
507 char *psz_name = psz_shortcuts;
509 /* Let's drop modules with a <= 0 score (unless they are
510 * explicitly requested) */
511 b_trash = p_module->i_score <= 0;
515 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
517 if( !strcasecmp( psz_name,
518 p_module->pp_shortcuts[i_dummy] ) )
522 i_shortcut_bonus = i_short * 10000;
527 if( i_shortcut_bonus )
529 /* We found it... remember ? */
533 /* Go to the next shortcut... This is so lame! */
542 /* If we are in "strict" mode and we couldn't
543 * find the module in the list of provided shortcuts,
544 * then kick the bastard out of here!!! */
545 if( i_short == 0 && b_strict )
555 /* If we didn't require a shortcut, trash <= 0 scored plugins */
556 else if( p_module->i_score <= 0 )
561 /* Special case: test if we requested a particular intf plugin */
562 if( !i_shortcuts && p_module->psz_program
563 && !strcmp( psz_capability, "interface" )
564 && !strcmp( p_module->psz_program,
565 p_this->p_libvlc->psz_object_name ) )
569 /* Remove previous non-matching plugins */
576 /* This one doesn't match */
580 /* Store this new module */
581 p_list[ i_index ].p_module = p_module;
582 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
583 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
585 /* Add it to the modules-to-probe list */
588 p_list[ 0 ].p_next = NULL;
593 /* Ok, so at school you learned that quicksort is quick, and
594 * bubble sort sucks raw eggs. But that's when dealing with
595 * thousands of items. Here we have barely 50. */
596 module_list_t *p_newlist = p_first;
598 if( p_first->i_score < p_list[ i_index ].i_score )
600 p_list[ i_index ].p_next = p_first;
601 p_first = &p_list[ i_index ];
605 while( p_newlist->p_next != NULL &&
606 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
608 p_newlist = p_newlist->p_next;
611 p_list[ i_index ].p_next = p_newlist->p_next;
612 p_newlist->p_next = &p_list[ i_index ];
619 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
620 i_index, i_index == 1 ? "" : "s" );
622 /* Lock all candidate modules */
624 while( p_tmp != NULL )
626 vlc_object_yield( p_tmp->p_module );
627 p_tmp = p_tmp->p_next;
630 /* We can release the list, interesting modules were yielded */
631 vlc_list_release( p_all );
633 /* Parse the linked list and use the first successful module */
635 while( p_tmp != NULL )
637 #ifdef HAVE_DYNAMIC_PLUGINS
638 /* Make sure the module is loaded in mem */
639 module_t *p_module = p_tmp->p_module->b_submodule ?
640 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
641 if( !p_module->b_builtin && !p_module->b_loaded )
643 module_t *p_new_module =
644 AllocatePlugin( p_this, p_module->psz_filename );
647 CacheMerge( p_this, p_module, p_new_module );
648 vlc_object_attach( p_new_module, p_module );
649 DeleteModule( p_new_module );
654 p_this->b_force = p_tmp->b_force;
655 if( p_tmp->p_module->pf_activate
656 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
661 vlc_object_release( p_tmp->p_module );
662 p_tmp = p_tmp->p_next;
665 /* Store the locked module value */
668 p_module = p_tmp->p_module;
669 p_tmp = p_tmp->p_next;
676 /* Unlock the remaining modules */
677 while( p_tmp != NULL )
679 vlc_object_release( p_tmp->p_module );
680 p_tmp = p_tmp->p_next;
684 p_this->b_force = b_force_backup;
686 if( p_module != NULL )
688 msg_Dbg( p_this, "using %s module \"%s\"",
689 psz_capability, p_module->psz_object_name );
691 else if( p_first == NULL )
693 if( !strcmp( psz_capability, "access_demux" ) )
695 msg_Warn( p_this, "no %s module matched \"%s\"",
696 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
700 msg_Err( p_this, "no %s module matched \"%s\"",
701 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
704 else if( psz_name != NULL && *psz_name )
706 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
707 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
712 free( psz_shortcuts );
720 /* Don't forget that the module is still locked */
724 /*****************************************************************************
725 * module_Unneed: decrease the usage count of a module.
726 *****************************************************************************
727 * This function must be called by the thread that called module_Need, to
728 * decrease the reference count and allow for hiding of modules.
729 *****************************************************************************/
730 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
732 /* Use the close method */
733 if( p_module->pf_deactivate )
735 p_module->pf_deactivate( p_this );
738 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
740 vlc_object_release( p_module );
745 /*****************************************************************************
746 * Following functions are local.
747 *****************************************************************************/
749 /*****************************************************************************
750 * AllocateAllPlugins: load all plugin modules we can find.
751 *****************************************************************************/
752 #ifdef HAVE_DYNAMIC_PLUGINS
753 static void AllocateAllPlugins( vlc_object_t *p_this )
755 /* Yes, there are two NULLs because we replace one with "plugin-path". */
756 #if defined( WIN32 ) || defined( UNDER_CE )
757 const char *path[] = { "modules", "", "plugins", NULL, NULL };
759 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
762 const char *const *ppsz_path;
764 /* If the user provided a plugin path, we add it to the list */
765 char *userpath = config_GetPsz( p_this, "plugin-path" );
766 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
768 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
772 if (!**ppsz_path) continue;
774 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
776 /* Handle relative as well as absolute paths */
778 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
779 (*ppsz_path)[1] != ':' )
781 if( (*ppsz_path)[0] != '/' )
784 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
785 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
790 psz_fullpath = strdup( *ppsz_path );
792 if( psz_fullpath == NULL )
795 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
797 /* Don't go deeper than 5 subdirectories */
798 AllocatePluginDir( p_this, psz_fullpath, 5 );
800 free( psz_fullpath );
803 /* Free plugin-path */
804 if( userpath != NULL )
808 /*****************************************************************************
809 * AllocatePluginDir: recursively parse a directory to look for plugins
810 *****************************************************************************/
811 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
814 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
820 #if defined( UNDER_CE ) || defined( _MSC_VER )
822 wchar_t psz_wpath[MAX_PATH + 256];
823 wchar_t psz_wdir[MAX_PATH];
825 char psz_path[MAX_PATH + 256];
826 WIN32_FIND_DATA finddata;
832 struct dirent * file;
836 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
841 #if defined( UNDER_CE ) || defined( _MSC_VER )
843 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
845 rc = GetFileAttributes( psz_wdir );
846 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
848 /* Parse all files in the directory */
849 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
851 rc = GetFileAttributes( psz_dir );
852 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
855 /* Parse all files in the directory */
856 sprintf( psz_path, "%s\\*", psz_dir );
859 handle = FindFirstFile( psz_wpath, &finddata );
861 handle = FindFirstFile( psz_path, &finddata );
863 if( handle == INVALID_HANDLE_VALUE )
865 /* Empty directory */
869 /* Parse the directory and try to load all files it contains. */
873 unsigned int i_len = wcslen( finddata.cFileName );
874 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
875 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
877 unsigned int i_len = strlen( finddata.cFileName );
878 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
882 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
883 || !strcmp( finddata.cFileName, ".." ) )
885 if( !FindNextFile( handle, &finddata ) ) break;
890 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
892 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
895 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
897 else if( i_len > strlen( LIBEXT )
898 /* We only load files ending with LIBEXT */
899 && !strncasecmp( psz_path + strlen( psz_path)
901 LIBEXT, strlen( LIBEXT ) ) )
903 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
904 int64_t i_time = 0, i_size = 0;
907 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
910 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
914 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
916 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
917 i_size = attrbuf.nFileSizeHigh;
919 i_size |= attrbuf.nFileSizeLow;
923 AllocatePluginFile( p_this, psz_file, i_time, i_size );
926 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
928 /* Close the directory */
932 dir = opendir( psz_dir );
938 i_dirlen = strlen( psz_dir );
940 /* Parse the directory and try to load all files it contains. */
941 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
948 if( !*file->d_name || !strcmp( file->d_name, "." )
949 || !strcmp( file->d_name, ".." ) )
954 i_len = strlen( file->d_name );
955 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
957 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
959 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
962 i_stat = stat( psz_file, &statbuf );
963 if( !i_stat && statbuf.st_mode & S_IFDIR )
965 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
967 else if( i_len > strlen( LIBEXT )
968 /* We only load files ending with LIBEXT */
969 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
970 LIBEXT, strlen( LIBEXT ) ) )
972 int64_t i_time = 0, i_size = 0;
976 i_time = statbuf.st_mtime;
977 i_size = statbuf.st_size;
980 AllocatePluginFile( p_this, psz_file, i_time, i_size );
986 /* Close the directory */
992 /*****************************************************************************
993 * AllocatePluginFile: load a module into memory and initialize it.
994 *****************************************************************************
995 * This function loads a dynamically loadable module and allocates a structure
996 * for its information data. The module can then be handled by module_Need
997 * and module_Unneed. It can be removed by DeleteModule.
998 *****************************************************************************/
999 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1000 int64_t i_file_time, int64_t i_file_size )
1002 module_t * p_module;
1003 module_cache_t *p_cache_entry = NULL;
1006 * Check our plugins cache first then load plugin if needed
1009 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1011 if( !p_cache_entry )
1013 p_module = AllocatePlugin( p_this, psz_file );
1017 /* If junk dll, don't try to load it */
1018 if( p_cache_entry->b_junk )
1024 module_config_t *p_item, *p_end;
1026 p_module = p_cache_entry->p_module;
1027 p_module->b_loaded = VLC_FALSE;
1029 /* For now we force loading if the module's config contains
1030 * callbacks or actions.
1031 * Could be optimized by adding an API call.*/
1032 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1033 p_item < p_end; p_item++ )
1035 if( p_item->pf_callback || p_item->i_action )
1036 p_module = AllocatePlugin( p_this, psz_file );
1043 /* Everything worked fine !
1044 * The module is ready to be added to the list. */
1045 p_module->b_builtin = VLC_FALSE;
1047 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1048 p_module->psz_object_name, p_module->psz_longname ); */
1050 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1053 if( !p_this->p_libvlc_global->p_module_bank->b_cache ) return 0;
1055 /* Add entry to cache */
1056 #define p_bank p_this->p_libvlc_global->p_module_bank
1058 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1059 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1060 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1061 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1062 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1063 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1064 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1067 return p_module ? 0 : -1;
1070 /*****************************************************************************
1071 * AllocatePlugin: load a module into memory and initialize it.
1072 *****************************************************************************
1073 * This function loads a dynamically loadable module and allocates a structure
1074 * for its information data. The module can then be handled by module_Need
1075 * and module_Unneed. It can be removed by DeleteModule.
1076 *****************************************************************************/
1077 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1079 module_t * p_module;
1080 module_handle_t handle;
1082 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1084 /* Now that we have successfully loaded the module, we can
1085 * allocate a structure for it */
1086 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1087 if( p_module == NULL )
1089 msg_Err( p_this, "out of memory" );
1090 CloseModule( handle );
1094 /* We need to fill these since they may be needed by CallEntry() */
1095 p_module->psz_filename = psz_file;
1096 p_module->handle = handle;
1097 #ifndef HAVE_SHARED_LIBVLC
1098 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1100 p_module->b_loaded = VLC_TRUE;
1102 /* Initialize the module: fill p_module, default config */
1103 if( CallEntry( p_module ) != 0 )
1105 /* We couldn't call module_init() */
1106 vlc_object_destroy( p_module );
1107 CloseModule( handle );
1111 DupModule( p_module );
1112 p_module->psz_filename = strdup( p_module->psz_filename );
1114 /* Everything worked fine ! The module is ready to be added to the list. */
1115 p_module->b_builtin = VLC_FALSE;
1120 /*****************************************************************************
1121 * DupModule: make a plugin module standalone.
1122 *****************************************************************************
1123 * This function duplicates all strings in the module, so that the dynamic
1124 * object can be unloaded. It acts recursively on submodules.
1125 *****************************************************************************/
1126 static void DupModule( module_t *p_module )
1128 const char **pp_shortcut;
1131 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1133 *pp_shortcut = strdup( *pp_shortcut );
1136 /* We strdup() these entries so that they are still valid when the
1137 * module is unloaded. */
1138 p_module->psz_object_name = strdup( p_module->psz_object_name );
1139 p_module->psz_capability = strdup( p_module->psz_capability );
1140 p_module->psz_shortname = p_module->psz_shortname ?
1141 strdup( p_module->psz_shortname ) : NULL;
1142 p_module->psz_longname = strdup( p_module->psz_longname );
1144 if( p_module->psz_program != NULL )
1146 p_module->psz_program = strdup( p_module->psz_program );
1149 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1151 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1155 /*****************************************************************************
1156 * UndupModule: free a duplicated module.
1157 *****************************************************************************
1158 * This function frees the allocations done in DupModule().
1159 *****************************************************************************/
1160 static void UndupModule( module_t *p_module )
1162 const char **pp_shortcut;
1165 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1167 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1170 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1172 free( (void *)*pp_shortcut );
1175 free( (void *)p_module->psz_object_name );
1176 free( (void *)p_module->psz_capability );
1177 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1178 free( (void *)p_module->psz_longname );
1180 if( p_module->psz_program != NULL )
1182 free( (void *)p_module->psz_program );
1186 #endif /* HAVE_DYNAMIC_PLUGINS */
1188 /*****************************************************************************
1189 * AllocateBuiltinModule: initialize a builtin module.
1190 *****************************************************************************
1191 * This function registers a builtin module and allocates a structure
1192 * for its information data. The module can then be handled by module_Need
1193 * and module_Unneed. It can be removed by DeleteModule.
1194 *****************************************************************************/
1195 static int AllocateBuiltinModule( vlc_object_t * p_this,
1196 int ( *pf_entry ) ( module_t * ) )
1198 module_t * p_module;
1200 /* Now that we have successfully loaded the module, we can
1201 * allocate a structure for it */
1202 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1203 if( p_module == NULL )
1205 msg_Err( p_this, "out of memory" );
1209 /* Initialize the module : fill p_module->psz_object_name, etc. */
1210 if( pf_entry( p_module ) != 0 )
1212 /* With a well-written module we shouldn't have to print an
1213 * additional error message here, but just make sure. */
1214 msg_Err( p_this, "failed calling entry point in builtin module" );
1215 vlc_object_destroy( p_module );
1219 /* Everything worked fine ! The module is ready to be added to the list. */
1220 p_module->b_builtin = VLC_TRUE;
1222 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1223 p_module->psz_object_name, p_module->psz_longname ); */
1225 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1230 /*****************************************************************************
1231 * DeleteModule: delete a module and its structure.
1232 *****************************************************************************
1233 * This function can only be called if the module isn't being used.
1234 *****************************************************************************/
1235 static int DeleteModule( module_t * p_module )
1237 vlc_object_detach( p_module );
1239 /* We free the structures that we strdup()ed in Allocate*Module(). */
1240 #ifdef HAVE_DYNAMIC_PLUGINS
1241 if( !p_module->b_builtin )
1243 if( p_module->b_loaded && p_module->b_unloadable )
1245 CloseModule( p_module->handle );
1247 UndupModule( p_module );
1248 free( p_module->psz_filename );
1252 /* Free and detach the object's children */
1253 while( p_module->i_children )
1255 vlc_object_t *p_this = p_module->pp_children[0];
1256 vlc_object_detach( p_this );
1257 vlc_object_destroy( p_this );
1260 config_Free( p_module );
1261 vlc_object_destroy( p_module );
1266 #ifdef HAVE_DYNAMIC_PLUGINS
1267 /*****************************************************************************
1268 * CallEntry: call an entry point.
1269 *****************************************************************************
1270 * This function calls a symbol given its name and a module structure. The
1271 * symbol MUST refer to a function returning int and taking a module_t* as
1273 *****************************************************************************/
1274 static int CallEntry( module_t * p_module )
1276 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1277 int (* pf_symbol) ( module_t * p_module );
1279 /* Try to resolve the symbol */
1280 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1282 if( pf_symbol == NULL )
1284 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1285 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1286 psz_name, p_module->psz_filename );
1287 #elif defined(HAVE_DL_WINDOWS)
1288 char *psz_error = GetWindowsError();
1289 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1290 psz_name, p_module->psz_filename, psz_error );
1292 #elif defined(HAVE_DL_DLOPEN)
1293 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1294 psz_name, p_module->psz_filename, dlerror() );
1295 #elif defined(HAVE_DL_SHL_LOAD)
1296 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1297 psz_name, p_module->psz_filename, strerror(errno) );
1299 # error "Something is wrong in modules.c"
1304 /* We can now try to call the symbol */
1305 if( pf_symbol( p_module ) != 0 )
1307 /* With a well-written module we shouldn't have to print an
1308 * additional error message here, but just make sure. */
1309 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1310 psz_name, p_module->psz_filename );
1314 /* Everything worked fine, we can return */
1318 /*****************************************************************************
1319 * LoadModule: loads a dynamic library
1320 *****************************************************************************
1321 * This function loads a dynamically linked library using a system dependant
1322 * method. Will return 0 on success as well as the module handle.
1323 *****************************************************************************/
1324 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1325 module_handle_t *p_handle )
1327 module_handle_t handle;
1329 #if defined(HAVE_DL_DYLD)
1330 NSObjectFileImage image;
1331 NSObjectFileImageReturnCode ret;
1333 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1335 if( ret != NSObjectFileImageSuccess )
1337 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1341 /* Open the dynamic module */
1342 handle = NSLinkModule( image, psz_file,
1343 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1347 NSLinkEditErrors errors;
1348 const char *psz_file, *psz_err;
1350 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1351 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1352 NSDestroyObjectFileImage( image );
1356 /* Destroy our image, we won't need it */
1357 NSDestroyObjectFileImage( image );
1359 #elif defined(HAVE_DL_BEOS)
1360 handle = load_add_on( psz_file );
1363 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1367 #elif defined(HAVE_DL_WINDOWS)
1370 wchar_t psz_wfile[MAX_PATH];
1371 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1372 handle = LoadLibrary( psz_wfile );
1375 handle = LoadLibrary( psz_file );
1377 if( handle == NULL )
1379 char *psz_err = GetWindowsError();
1380 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1385 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1386 /* static is OK, we are called atomically */
1388 # if defined(SYS_LINUX)
1389 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1390 * are going to get namespace collisions when two modules have common
1391 * public symbols, but ALSA is being a pest here. */
1392 if( strstr( psz_file, "alsa_plugin" ) )
1394 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1395 if( handle == NULL )
1397 msg_Warn( p_this, "cannot load module `%s' (%s)",
1398 psz_file, dlerror() );
1404 handle = dlopen( psz_file, RTLD_NOW );
1405 if( handle == NULL )
1407 msg_Warn( p_this, "cannot load module `%s' (%s)",
1408 psz_file, dlerror() );
1412 #elif defined(HAVE_DL_DLOPEN)
1413 # if defined(DL_LAZY)
1414 handle = dlopen( psz_file, DL_LAZY );
1416 handle = dlopen( psz_file, 0 );
1418 if( handle == NULL )
1420 msg_Warn( p_this, "cannot load module `%s' (%s)",
1421 psz_file, dlerror() );
1425 #elif defined(HAVE_DL_SHL_LOAD)
1426 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1427 if( handle == NULL )
1429 msg_Warn( p_this, "cannot load module `%s' (%s)",
1430 psz_file, strerror(errno) );
1435 # error "Something is wrong in modules.c"
1443 /*****************************************************************************
1444 * CloseModule: unload a dynamic library
1445 *****************************************************************************
1446 * This function unloads a previously opened dynamically linked library
1447 * using a system dependant method. No return value is taken in consideration,
1448 * since some libraries sometimes refuse to close properly.
1449 *****************************************************************************/
1450 static void CloseModule( module_handle_t handle )
1452 #if defined(HAVE_DL_DYLD)
1453 NSUnLinkModule( handle, FALSE );
1455 #elif defined(HAVE_DL_BEOS)
1456 unload_add_on( handle );
1458 #elif defined(HAVE_DL_WINDOWS)
1459 FreeLibrary( handle );
1461 #elif defined(HAVE_DL_DLOPEN)
1464 #elif defined(HAVE_DL_SHL_LOAD)
1465 shl_unload( handle );
1471 /*****************************************************************************
1472 * GetSymbol: get a symbol from a dynamic library
1473 *****************************************************************************
1474 * This function queries a loaded library for a symbol specified in a
1475 * string, and returns a pointer to it. We don't check for dlerror() or
1476 * similar functions, since we want a non-NULL symbol anyway.
1477 *****************************************************************************/
1478 static void * _module_getsymbol( module_handle_t, const char * );
1480 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1482 void * p_symbol = _module_getsymbol( handle, psz_function );
1484 /* MacOS X dl library expects symbols to begin with "_". So do
1485 * some other operating systems. That's really lame, but hey, what
1487 if( p_symbol == NULL )
1489 char *psz_call = malloc( strlen( psz_function ) + 2 );
1491 strcpy( psz_call + 1, psz_function );
1492 psz_call[ 0 ] = '_';
1493 p_symbol = _module_getsymbol( handle, psz_call );
1500 static void * _module_getsymbol( module_handle_t handle,
1501 const char * psz_function )
1503 #if defined(HAVE_DL_DYLD)
1504 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1505 return NSAddressOfSymbol( sym );
1507 #elif defined(HAVE_DL_BEOS)
1509 if( B_OK == get_image_symbol( handle, psz_function,
1510 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1519 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1520 wchar_t psz_real[256];
1521 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1523 return (void *)GetProcAddress( handle, psz_real );
1525 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1526 return (void *)GetProcAddress( handle, (char *)psz_function );
1528 #elif defined(HAVE_DL_DLOPEN)
1529 return dlsym( handle, psz_function );
1531 #elif defined(HAVE_DL_SHL_LOAD)
1533 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1539 #if defined(HAVE_DL_WINDOWS)
1540 static char * GetWindowsError( void )
1542 #if defined(UNDER_CE)
1543 wchar_t psz_tmp[MAX_PATH];
1544 char * psz_buffer = malloc( MAX_PATH );
1546 char * psz_tmp = malloc( MAX_PATH );
1548 int i = 0, i_error = GetLastError();
1550 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1551 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1552 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1554 /* Go to the end of the string */
1555 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1562 #if defined(UNDER_CE)
1563 swprintf( psz_tmp + i, L" (error %i)", i_error );
1564 psz_tmp[ 255 ] = L'\0';
1566 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1567 psz_tmp[ 255 ] = '\0';
1571 #if defined(UNDER_CE)
1572 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1578 #endif /* HAVE_DL_WINDOWS */
1580 /*****************************************************************************
1581 * LoadPluginsCache: loads the plugins cache file
1582 *****************************************************************************
1583 * This function will load the plugin cache if present and valid. This cache
1584 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1585 * actually load the dynamically loadable module.
1586 * This allows us to only fully load plugins when they are actually used.
1587 *****************************************************************************/
1588 static void CacheLoad( vlc_object_t *p_this )
1590 char *psz_filename, *psz_homedir;
1592 int i, j, i_size, i_read;
1593 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1594 char p_cachelang[6], p_lang[6];
1596 module_cache_t **pp_cache = 0;
1597 int32_t i_file_size, i_marker;
1599 psz_homedir = p_this->p_libvlc->psz_homedir;
1602 msg_Err( p_this, "psz_homedir is null" );
1606 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1607 PLUGINSCACHE_DIR, CacheName() );
1610 msg_Err( p_this, "out of memory" );
1614 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1616 #if !defined( UNDER_CE )
1617 unlink( psz_filename );
1619 wchar_t psz_wf[MAX_PATH];
1620 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1621 DeleteFile( psz_wf );
1623 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1624 free( psz_filename );
1628 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1630 file = utf8_fopen( psz_filename, "rb" );
1633 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1635 free( psz_filename );
1638 free( psz_filename );
1640 /* Check the file size */
1641 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1642 if( i_read != sizeof(i_file_size) )
1644 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1650 fseek( file, 0, SEEK_END );
1651 if( ftell( file ) != i_file_size )
1653 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1654 "(corrupted size)" );
1658 fseek( file, sizeof(i_file_size), SEEK_SET );
1660 /* Check the file is a plugins cache */
1661 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1662 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1663 if( i_read != i_size ||
1664 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1666 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1671 /* Check Sub-version number */
1672 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1673 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1675 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1676 "(corrupted header)" );
1681 /* Check the language hasn't changed */
1682 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1683 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1684 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1686 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1687 "(language changed)" );
1692 /* Check header marker */
1693 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1694 if( i_read != sizeof(i_marker) ||
1695 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1697 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1698 "(corrupted header)" );
1703 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1704 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1706 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1707 malloc( i_cache * sizeof(void *) );
1709 #define LOAD_IMMEDIATE(a) \
1710 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1711 #define LOAD_STRING(a) \
1714 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1715 || ( i_size > 16384 ) ) \
1718 char *psz = malloc( i_size ); \
1719 if( fread( psz, i_size, 1, file ) != 1 ) { \
1723 if( psz[i_size-1] ) { \
1731 for( i = 0; i < i_cache; i++ )
1736 pp_cache[i] = malloc( sizeof(module_cache_t) );
1737 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1739 /* Load common info */
1740 LOAD_STRING( pp_cache[i]->psz_file );
1741 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1742 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1743 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1745 if( pp_cache[i]->b_junk ) continue;
1747 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1749 /* Load additional infos */
1750 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1751 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1752 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1753 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1754 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1756 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1758 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1759 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1760 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1761 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1762 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1763 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1766 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1769 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1771 LOAD_IMMEDIATE( i_submodules );
1773 while( i_submodules-- )
1775 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1776 vlc_object_attach( p_module, pp_cache[i]->p_module );
1777 p_module->b_submodule = VLC_TRUE;
1779 LOAD_STRING( p_module->psz_object_name );
1780 LOAD_STRING( p_module->psz_shortname );
1781 LOAD_STRING( p_module->psz_longname );
1782 LOAD_STRING( p_module->psz_program );
1783 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1785 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1787 LOAD_STRING( p_module->psz_capability );
1788 LOAD_IMMEDIATE( p_module->i_score );
1789 LOAD_IMMEDIATE( p_module->i_cpu );
1790 LOAD_IMMEDIATE( p_module->b_unloadable );
1791 LOAD_IMMEDIATE( p_module->b_reentrant );
1800 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1803 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1810 int CacheLoadConfig( module_t *p_module, FILE *file )
1815 /* Calculate the structure length */
1816 LOAD_IMMEDIATE( p_module->i_config_items );
1817 LOAD_IMMEDIATE( p_module->i_bool_items );
1819 LOAD_IMMEDIATE( i_lines );
1821 /* Allocate memory */
1824 p_module->p_config =
1825 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1826 if( p_module->p_config == NULL )
1828 p_module->confsize = 0;
1829 msg_Err( p_module, "config error: can't duplicate p_config" );
1833 p_module->confsize = i_lines;
1835 /* Do the duplication job */
1836 for (size_t i = 0; i < i_lines; i++ )
1838 LOAD_IMMEDIATE( p_module->p_config[i] );
1840 LOAD_STRING( p_module->p_config[i].psz_type );
1841 LOAD_STRING( p_module->p_config[i].psz_name );
1842 LOAD_STRING( p_module->p_config[i].psz_text );
1843 LOAD_STRING( p_module->p_config[i].psz_longtext );
1844 LOAD_STRING( p_module->p_config[i].psz_current );
1846 if (IsConfigStringType (p_module->p_config[i].i_type))
1848 LOAD_STRING (p_module->p_config[i].orig.psz);
1849 p_module->p_config[i].value.psz =
1850 (p_module->p_config[i].orig.psz != NULL)
1851 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1852 p_module->p_config[i].saved.psz = NULL;
1856 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1857 sizeof (p_module->p_config[i].value));
1858 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1859 sizeof (p_module->p_config[i].saved));
1862 p_module->p_config[i].b_dirty = VLC_FALSE;
1864 p_module->p_config[i].p_lock = &p_module->object_lock;
1866 if( p_module->p_config[i].i_list )
1868 if( p_module->p_config[i].ppsz_list )
1871 p_module->p_config[i].ppsz_list =
1872 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1873 if( p_module->p_config[i].ppsz_list )
1875 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1876 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1877 p_module->p_config[i].ppsz_list[j] = NULL;
1880 if( p_module->p_config[i].ppsz_list_text )
1883 p_module->p_config[i].ppsz_list_text =
1884 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1885 if( p_module->p_config[i].ppsz_list_text )
1887 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1888 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1889 p_module->p_config[i].ppsz_list_text[j] = NULL;
1892 if( p_module->p_config[i].pi_list )
1894 p_module->p_config[i].pi_list =
1895 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1896 if( p_module->p_config[i].pi_list )
1898 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1899 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1904 if( p_module->p_config[i].i_action )
1906 p_module->p_config[i].ppf_action =
1907 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1908 p_module->p_config[i].ppsz_action_text =
1909 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1911 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1913 p_module->p_config[i].ppf_action[j] = 0;
1914 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1918 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1925 return VLC_EGENERIC;
1928 /*****************************************************************************
1929 * SavePluginsCache: saves the plugins cache to a file
1930 *****************************************************************************/
1931 static void CacheSave( vlc_object_t *p_this )
1933 static char const psz_tag[] =
1934 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1935 "# This file is a cache directory tag created by VLC.\r\n"
1936 "# For information about cache directory tags, see:\r\n"
1937 "# http://www.brynosaurus.com/cachedir/\r\n";
1939 char *psz_filename, *psz_homedir;
1942 module_cache_t **pp_cache;
1943 int32_t i_file_size = 0;
1945 psz_homedir = p_this->p_libvlc->psz_homedir;
1948 msg_Err( p_this, "psz_homedir is null" );
1952 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1953 strlen(psz_homedir) + strlen(CacheName()) );
1957 msg_Err( p_this, "out of memory" );
1961 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1963 config_CreateDir( p_this, psz_filename );
1965 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1967 config_CreateDir( p_this, psz_filename );
1969 strcat( psz_filename, "/CACHEDIR.TAG" );
1971 file = utf8_fopen( psz_filename, "wb" );
1974 fwrite( psz_tag, 1, strlen(psz_tag), file );
1978 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1979 PLUGINSCACHE_DIR, CacheName() );
1981 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1983 file = utf8_fopen( psz_filename, "wb" );
1986 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1988 free( psz_filename );
1991 free( psz_filename );
1993 /* Empty space for file size */
1994 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1996 /* Contains version number */
1997 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1999 /* Sub-version number (to avoid breakage in the dev version when cache
2000 * structure changes) */
2001 i_file_size = CACHE_SUBVERSION_NUM;
2002 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2005 fprintf( file, "%5.5s", _("C") );
2008 i_file_size = ftell( file );
2009 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2011 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2012 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2014 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2016 #define SAVE_IMMEDIATE(a) \
2017 fwrite( &a, sizeof(char), sizeof(a), file )
2018 #define SAVE_STRING(a) \
2019 { i_size = a ? strlen( a ) + 1 : 0; \
2020 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2021 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2024 for( i = 0; i < i_cache; i++ )
2027 uint32_t i_submodule;
2029 /* Save common info */
2030 SAVE_STRING( pp_cache[i]->psz_file );
2031 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2032 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2033 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2035 if( pp_cache[i]->b_junk ) continue;
2037 /* Save additional infos */
2038 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2039 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2040 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2041 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2042 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2044 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2046 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2047 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2048 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2049 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2050 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2051 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2054 CacheSaveConfig( pp_cache[i]->p_module, file );
2056 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2058 i_submodule = pp_cache[i]->p_module->i_children;
2059 SAVE_IMMEDIATE( i_submodule );
2060 for( i_submodule = 0;
2061 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2064 module_t *p_module =
2065 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2067 SAVE_STRING( p_module->psz_object_name );
2068 SAVE_STRING( p_module->psz_shortname );
2069 SAVE_STRING( p_module->psz_longname );
2070 SAVE_STRING( p_module->psz_program );
2071 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2073 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2075 SAVE_STRING( p_module->psz_capability );
2076 SAVE_IMMEDIATE( p_module->i_score );
2077 SAVE_IMMEDIATE( p_module->i_cpu );
2078 SAVE_IMMEDIATE( p_module->b_unloadable );
2079 SAVE_IMMEDIATE( p_module->b_reentrant );
2083 /* Fill-up file size */
2084 i_file_size = ftell( file );
2085 fseek( file, 0, SEEK_SET );
2086 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2093 void CacheSaveConfig( module_t *p_module, FILE *file )
2095 uint32_t i_lines = p_module->confsize;
2098 SAVE_IMMEDIATE( p_module->i_config_items );
2099 SAVE_IMMEDIATE( p_module->i_bool_items );
2100 SAVE_IMMEDIATE( i_lines );
2102 for (size_t i = 0; i < i_lines ; i++)
2104 SAVE_IMMEDIATE( p_module->p_config[i] );
2106 SAVE_STRING( p_module->p_config[i].psz_type );
2107 SAVE_STRING( p_module->p_config[i].psz_name );
2108 SAVE_STRING( p_module->p_config[i].psz_text );
2109 SAVE_STRING( p_module->p_config[i].psz_longtext );
2110 SAVE_STRING( p_module->p_config[i].psz_current );
2111 if (IsConfigStringType (p_module->p_config[i].i_type))
2112 SAVE_STRING( p_module->p_config[i].orig.psz );
2114 if( p_module->p_config[i].i_list )
2116 if( p_module->p_config[i].ppsz_list )
2118 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2119 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2122 if( p_module->p_config[i].ppsz_list_text )
2124 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2125 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2127 if( p_module->p_config[i].pi_list )
2129 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2130 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2134 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2135 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2137 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2141 /*****************************************************************************
2142 * CacheName: Return the cache file name for this platform.
2143 *****************************************************************************/
2144 static char *CacheName( void )
2146 static char psz_cachename[32];
2148 /* Code int size, pointer size and endianness in the filename */
2149 int32_t x = 0xbe00001e;
2150 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2151 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2152 return psz_cachename;
2155 /*****************************************************************************
2156 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2157 *****************************************************************************/
2158 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2159 module_t *p_module )
2163 p_cache->pf_activate = p_module->pf_activate;
2164 p_cache->pf_deactivate = p_module->pf_deactivate;
2165 #ifndef HAVE_SHARED_LIBVLC
2166 p_cache->p_symbols = p_module->p_symbols;
2168 p_cache->handle = p_module->handle;
2170 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2172 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2173 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2174 p_cchild->pf_activate = p_child->pf_activate;
2175 p_cchild->pf_deactivate = p_child->pf_deactivate;
2176 #ifndef HAVE_SHARED_LIBVLC
2177 p_cchild->p_symbols = p_child->p_symbols;
2181 p_cache->b_loaded = VLC_TRUE;
2182 p_module->b_loaded = VLC_FALSE;
2185 /*****************************************************************************
2186 * FindPluginCache: finds the cache entry corresponding to a file
2187 *****************************************************************************/
2188 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2189 int64_t i_time, int64_t i_size )
2191 module_cache_t **pp_cache;
2194 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2195 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2197 for( i = 0; i < i_cache; i++ )
2199 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2200 pp_cache[i]->i_time == i_time &&
2201 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2207 #endif /* HAVE_DYNAMIC_PLUGINS */