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 );
162 /* Sub-version number
163 * (only used to avoid breakage in dev version when cache structure changes) */
164 #define CACHE_SUBVERSION_NUM 1
166 /*****************************************************************************
167 * module_InitBank: create the module bank.
168 *****************************************************************************
169 * This function creates a module bank structure which will be filled later
170 * on with all the modules found.
171 *****************************************************************************/
172 void __module_InitBank( vlc_object_t *p_this )
174 module_bank_t *p_bank;
177 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
178 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
179 vlc_mutex_lock( lockval.p_address );
180 if( p_this->p_libvlc_global->p_module_bank )
182 p_this->p_libvlc_global->p_module_bank->i_usage++;
183 vlc_mutex_unlock( lockval.p_address );
184 var_Destroy( p_this->p_libvlc_global, "libvlc" );
187 vlc_mutex_unlock( lockval.p_address );
188 var_Destroy( p_this->p_libvlc_global, "libvlc" );
190 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
191 p_bank->psz_object_name = "module bank";
193 p_bank->i_cache = p_bank->i_loaded_cache = 0;
194 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
195 p_bank->b_cache = p_bank->b_cache_dirty =
196 p_bank->b_cache_delete = VLC_FALSE;
199 * Store the symbols to be exported
201 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
202 STORE_SYMBOLS( &p_bank->symbols );
205 /* Everything worked, attach the object */
206 p_this->p_libvlc_global->p_module_bank = p_bank;
207 vlc_object_attach( p_bank, p_this->p_libvlc_global );
209 module_LoadMain( p_this );
214 /*****************************************************************************
215 * module_ResetBank: reset the module bank.
216 *****************************************************************************
217 * This function resets the module bank by unloading all unused plugin
219 *****************************************************************************/
220 void __module_ResetBank( vlc_object_t *p_this )
222 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
226 /*****************************************************************************
227 * module_EndBank: empty the module bank.
228 *****************************************************************************
229 * This function unloads all unused plugin modules and empties the module
230 * bank in case of success.
231 *****************************************************************************/
232 void __module_EndBank( vlc_object_t *p_this )
237 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
238 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
239 vlc_mutex_lock( lockval.p_address );
240 if( !p_this->p_libvlc_global->p_module_bank )
242 vlc_mutex_unlock( lockval.p_address );
243 var_Destroy( p_this->p_libvlc_global, "libvlc" );
246 if( --p_this->p_libvlc_global->p_module_bank->i_usage )
248 vlc_mutex_unlock( lockval.p_address );
249 var_Destroy( p_this->p_libvlc_global, "libvlc" );
252 vlc_mutex_unlock( lockval.p_address );
253 var_Destroy( p_this->p_libvlc_global, "libvlc" );
255 config_AutoSaveConfigFile( p_this );
257 #ifdef HAVE_DYNAMIC_PLUGINS
258 #define p_bank p_this->p_libvlc_global->p_module_bank
259 if( p_bank->b_cache ) CacheSave( p_this );
260 while( p_bank->i_loaded_cache-- )
262 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
263 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
265 if( p_bank->pp_loaded_cache )
266 free( p_bank->pp_loaded_cache );
268 while( p_bank->i_cache-- )
270 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
271 free( p_bank->pp_cache[p_bank->i_cache] );
273 if( p_bank->pp_cache )
274 free( p_bank->pp_cache );
278 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
280 while( p_this->p_libvlc_global->p_module_bank->i_children )
282 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
284 if( DeleteModule( p_next ) )
286 /* Module deletion failed */
287 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
288 p_next->psz_object_name );
290 /* We just free the module by hand. Niahahahahaha. */
291 vlc_object_detach( p_next );
292 vlc_object_destroy( p_next );
296 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
297 p_this->p_libvlc_global->p_module_bank = NULL;
302 /*****************************************************************************
303 * module_LoadMain: load the main program info into the module bank.
304 *****************************************************************************
305 * This function fills the module bank structure with the main module infos.
306 * This is very useful as it will allow us to consider the main program just
307 * as another module, and for instance the configuration options of main will
308 * be available in the module bank structure just as for every other module.
309 *****************************************************************************/
310 void __module_LoadMain( vlc_object_t *p_this )
314 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
315 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
316 vlc_mutex_lock( lockval.p_address );
317 if( p_this->p_libvlc_global->p_module_bank->b_main )
319 vlc_mutex_unlock( lockval.p_address );
320 var_Destroy( p_this->p_libvlc_global, "libvlc" );
323 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
324 vlc_mutex_unlock( lockval.p_address );
325 var_Destroy( p_this->p_libvlc_global, "libvlc" );
327 AllocateBuiltinModule( p_this, vlc_entry__main );
330 /*****************************************************************************
331 * module_LoadBuiltins: load all modules which we built with.
332 *****************************************************************************
333 * This function fills the module bank structure with the builtin modules.
334 *****************************************************************************/
335 void __module_LoadBuiltins( vlc_object_t * p_this )
339 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
340 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
341 vlc_mutex_lock( lockval.p_address );
342 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
344 vlc_mutex_unlock( lockval.p_address );
345 var_Destroy( p_this->p_libvlc_global, "libvlc" );
348 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
349 vlc_mutex_unlock( lockval.p_address );
350 var_Destroy( p_this->p_libvlc_global, "libvlc" );
352 msg_Dbg( p_this, "checking builtin modules" );
353 ALLOCATE_ALL_BUILTINS();
356 /*****************************************************************************
357 * module_LoadPlugins: load all plugin modules we can find.
358 *****************************************************************************
359 * This function fills the module bank structure with the plugin modules.
360 *****************************************************************************/
361 void __module_LoadPlugins( vlc_object_t * p_this )
363 #ifdef HAVE_DYNAMIC_PLUGINS
366 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
367 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
368 vlc_mutex_lock( lockval.p_address );
369 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
371 vlc_mutex_unlock( lockval.p_address );
372 var_Destroy( p_this->p_libvlc_global, "libvlc" );
375 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
376 vlc_mutex_unlock( lockval.p_address );
377 var_Destroy( p_this->p_libvlc_global, "libvlc" );
379 msg_Dbg( p_this, "checking plugin modules" );
381 if( config_GetInt( p_this, "plugins-cache" ) )
382 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
384 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
385 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
387 AllocateAllPlugins( p_this );
391 /*****************************************************************************
392 * module_Need: return the best module function, given a capability list.
393 *****************************************************************************
394 * This function returns the module that best fits the asked capabilities.
395 *****************************************************************************/
396 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
397 const char *psz_name, vlc_bool_t b_strict )
399 typedef struct module_list_t module_list_t;
406 module_list_t *p_next;
409 module_list_t *p_list, *p_first, *p_tmp;
412 int i_which_module, i_index = 0;
413 vlc_bool_t b_intf = VLC_FALSE;
418 char *psz_shortcuts = NULL, *psz_var = NULL;
419 vlc_bool_t b_force_backup = p_this->b_force;
422 /* Deal with variables */
423 if( psz_name && psz_name[0] == '$' )
426 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
427 var_Get( p_this, psz_name + 1, &val );
428 psz_var = val.psz_string;
432 /* Count how many different shortcuts were asked for */
433 if( psz_name && *psz_name )
435 char *psz_parser, *psz_last_shortcut;
437 /* If the user wants none, give him none. */
438 if( !strcmp( psz_name, "none" ) )
440 if( psz_var ) free( psz_var );
445 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
447 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
449 if( *psz_parser == ',' )
453 psz_last_shortcut = psz_parser + 1;
457 /* Check if the user wants to override the "strict" mode */
458 if( psz_last_shortcut )
460 if( !strcmp(psz_last_shortcut, "none") )
465 else if( !strcmp(psz_last_shortcut, "any") )
467 b_strict = VLC_FALSE;
473 /* Sort the modules and test them */
474 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
475 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
478 /* Parse the module list for capabilities and probe each of them */
479 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
481 int i_shortcut_bonus = 0;
483 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
485 /* Test that this module can do what we need */
486 if( strcmp( p_module->psz_capability, psz_capability ) )
488 /* Don't recurse through the sub-modules because vlc_list_find()
489 * will list them anyway. */
493 /* Test if we have the required CPU */
494 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
499 /* If we required a shortcut, check this plugin provides it. */
500 if( i_shortcuts > 0 )
503 int i_dummy, i_short = i_shortcuts;
504 char *psz_name = psz_shortcuts;
506 /* Let's drop modules with a <= 0 score (unless they are
507 * explicitly requested) */
508 b_trash = p_module->i_score <= 0;
512 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
514 if( !strcasecmp( psz_name,
515 p_module->pp_shortcuts[i_dummy] ) )
519 i_shortcut_bonus = i_short * 10000;
524 if( i_shortcut_bonus )
526 /* We found it... remember ? */
530 /* Go to the next shortcut... This is so lame! */
539 /* If we are in "strict" mode and we couldn't
540 * find the module in the list of provided shortcuts,
541 * then kick the bastard out of here!!! */
542 if( i_short == 0 && b_strict )
552 /* If we didn't require a shortcut, trash <= 0 scored plugins */
553 else if( p_module->i_score <= 0 )
558 /* Special case: test if we requested a particular intf plugin */
559 if( !i_shortcuts && p_module->psz_program
560 && !strcmp( psz_capability, "interface" )
561 && !strcmp( p_module->psz_program,
562 p_this->p_libvlc->psz_object_name ) )
566 /* Remove previous non-matching plugins */
573 /* This one doesn't match */
577 /* Store this new module */
578 p_list[ i_index ].p_module = p_module;
579 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
580 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
582 /* Add it to the modules-to-probe list */
585 p_list[ 0 ].p_next = NULL;
590 /* Ok, so at school you learned that quicksort is quick, and
591 * bubble sort sucks raw eggs. But that's when dealing with
592 * thousands of items. Here we have barely 50. */
593 module_list_t *p_newlist = p_first;
595 if( p_first->i_score < p_list[ i_index ].i_score )
597 p_list[ i_index ].p_next = p_first;
598 p_first = &p_list[ i_index ];
602 while( p_newlist->p_next != NULL &&
603 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
605 p_newlist = p_newlist->p_next;
608 p_list[ i_index ].p_next = p_newlist->p_next;
609 p_newlist->p_next = &p_list[ i_index ];
616 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
617 i_index, i_index == 1 ? "" : "s" );
619 /* Lock all candidate modules */
621 while( p_tmp != NULL )
623 vlc_object_yield( p_tmp->p_module );
624 p_tmp = p_tmp->p_next;
627 /* We can release the list, interesting modules were yielded */
628 vlc_list_release( p_all );
630 /* Parse the linked list and use the first successful module */
632 while( p_tmp != NULL )
634 #ifdef HAVE_DYNAMIC_PLUGINS
635 /* Make sure the module is loaded in mem */
636 module_t *p_module = p_tmp->p_module->b_submodule ?
637 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
638 if( !p_module->b_builtin && !p_module->b_loaded )
640 module_t *p_new_module =
641 AllocatePlugin( p_this, p_module->psz_filename );
644 CacheMerge( p_this, p_module, p_new_module );
645 vlc_object_attach( p_new_module, p_module );
646 DeleteModule( p_new_module );
651 p_this->b_force = p_tmp->b_force;
652 if( p_tmp->p_module->pf_activate
653 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
658 vlc_object_release( p_tmp->p_module );
659 p_tmp = p_tmp->p_next;
662 /* Store the locked module value */
665 p_module = p_tmp->p_module;
666 p_tmp = p_tmp->p_next;
673 /* Unlock the remaining modules */
674 while( p_tmp != NULL )
676 vlc_object_release( p_tmp->p_module );
677 p_tmp = p_tmp->p_next;
681 p_this->b_force = b_force_backup;
683 if( p_module != NULL )
685 msg_Dbg( p_this, "using %s module \"%s\"",
686 psz_capability, p_module->psz_object_name );
688 else if( p_first == NULL )
690 if( !strcmp( psz_capability, "access_demux" ) )
692 msg_Warn( p_this, "no %s module matched \"%s\"",
693 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
697 msg_Err( p_this, "no %s module matched \"%s\"",
698 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
701 else if( psz_name != NULL && *psz_name )
703 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
704 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
709 free( psz_shortcuts );
717 /* Don't forget that the module is still locked */
721 /*****************************************************************************
722 * module_Unneed: decrease the usage count of a module.
723 *****************************************************************************
724 * This function must be called by the thread that called module_Need, to
725 * decrease the reference count and allow for hiding of modules.
726 *****************************************************************************/
727 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
729 /* Use the close method */
730 if( p_module->pf_deactivate )
732 p_module->pf_deactivate( p_this );
735 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
737 vlc_object_release( p_module );
742 /*****************************************************************************
743 * Following functions are local.
744 *****************************************************************************/
746 /*****************************************************************************
747 * AllocateAllPlugins: load all plugin modules we can find.
748 *****************************************************************************/
749 #ifdef HAVE_DYNAMIC_PLUGINS
750 static void AllocateAllPlugins( vlc_object_t *p_this )
752 /* Yes, there are two NULLs because we replace one with "plugin-path". */
753 #if defined( WIN32 ) || defined( UNDER_CE )
754 const char *path[] = { "modules", "", "plugins", NULL, NULL };
756 const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
759 const char *const *ppsz_path;
761 /* If the user provided a plugin path, we add it to the list */
762 char *userpath = config_GetPsz( p_this, "plugin-path" );
763 path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
765 for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
769 if (!**ppsz_path) continue;
771 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
773 /* Handle relative as well as absolute paths */
775 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
776 (*ppsz_path)[1] != ':' )
778 if( (*ppsz_path)[0] != '/' )
781 if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
782 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
787 psz_fullpath = strdup( *ppsz_path );
789 if( psz_fullpath == NULL )
792 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
794 /* Don't go deeper than 5 subdirectories */
795 AllocatePluginDir( p_this, psz_fullpath, 5 );
797 free( psz_fullpath );
800 /* Free plugin-path */
801 if( userpath != NULL )
805 /*****************************************************************************
806 * AllocatePluginDir: recursively parse a directory to look for plugins
807 *****************************************************************************/
808 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
811 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
817 #if defined( UNDER_CE ) || defined( _MSC_VER )
819 wchar_t psz_wpath[MAX_PATH + 256];
820 wchar_t psz_wdir[MAX_PATH];
822 char psz_path[MAX_PATH + 256];
823 WIN32_FIND_DATA finddata;
829 struct dirent * file;
833 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
838 #if defined( UNDER_CE ) || defined( _MSC_VER )
840 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
842 rc = GetFileAttributes( psz_wdir );
843 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
845 /* Parse all files in the directory */
846 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
848 rc = GetFileAttributes( psz_dir );
849 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
852 /* Parse all files in the directory */
853 sprintf( psz_path, "%s\\*", psz_dir );
856 handle = FindFirstFile( psz_wpath, &finddata );
858 handle = FindFirstFile( psz_path, &finddata );
860 if( handle == INVALID_HANDLE_VALUE )
862 /* Empty directory */
866 /* Parse the directory and try to load all files it contains. */
870 unsigned int i_len = wcslen( finddata.cFileName );
871 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
872 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
874 unsigned int i_len = strlen( finddata.cFileName );
875 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
879 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
880 || !strcmp( finddata.cFileName, ".." ) )
882 if( !FindNextFile( handle, &finddata ) ) break;
887 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
889 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
892 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
894 else if( i_len > strlen( LIBEXT )
895 /* We only load files ending with LIBEXT */
896 && !strncasecmp( psz_path + strlen( psz_path)
898 LIBEXT, strlen( LIBEXT ) ) )
900 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
901 int64_t i_time = 0, i_size = 0;
904 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
907 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
911 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
913 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
914 i_size = attrbuf.nFileSizeHigh;
916 i_size |= attrbuf.nFileSizeLow;
920 AllocatePluginFile( p_this, psz_file, i_time, i_size );
923 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
925 /* Close the directory */
929 dir = opendir( psz_dir );
935 i_dirlen = strlen( psz_dir );
937 /* Parse the directory and try to load all files it contains. */
938 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
945 if( !*file->d_name || !strcmp( file->d_name, "." )
946 || !strcmp( file->d_name, ".." ) )
951 i_len = strlen( file->d_name );
952 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
954 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
956 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
959 i_stat = stat( psz_file, &statbuf );
960 if( !i_stat && statbuf.st_mode & S_IFDIR )
962 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
964 else if( i_len > strlen( LIBEXT )
965 /* We only load files ending with LIBEXT */
966 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
967 LIBEXT, strlen( LIBEXT ) ) )
969 int64_t i_time = 0, i_size = 0;
973 i_time = statbuf.st_mtime;
974 i_size = statbuf.st_size;
977 AllocatePluginFile( p_this, psz_file, i_time, i_size );
983 /* Close the directory */
989 /*****************************************************************************
990 * AllocatePluginFile: load a module into memory and initialize it.
991 *****************************************************************************
992 * This function loads a dynamically loadable module and allocates a structure
993 * for its information data. The module can then be handled by module_Need
994 * and module_Unneed. It can be removed by DeleteModule.
995 *****************************************************************************/
996 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
997 int64_t i_file_time, int64_t i_file_size )
1000 module_cache_t *p_cache_entry = NULL;
1003 * Check our plugins cache first then load plugin if needed
1006 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1008 if( !p_cache_entry )
1010 p_module = AllocatePlugin( p_this, psz_file );
1014 /* If junk dll, don't try to load it */
1015 if( p_cache_entry->b_junk )
1021 module_config_t *p_item, *p_end;
1023 p_module = p_cache_entry->p_module;
1024 p_module->b_loaded = VLC_FALSE;
1026 /* For now we force loading if the module's config contains
1027 * callbacks or actions.
1028 * Could be optimized by adding an API call.*/
1029 for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1030 p_item < p_end; p_item++ )
1032 if( p_item->pf_callback || p_item->i_action )
1033 p_module = AllocatePlugin( p_this, psz_file );
1040 /* Everything worked fine !
1041 * The module is ready to be added to the list. */
1042 p_module->b_builtin = VLC_FALSE;
1044 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1045 p_module->psz_object_name, p_module->psz_longname ); */
1047 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1050 if( !p_this->p_libvlc_global->p_module_bank->b_cache ) return 0;
1052 /* Add entry to cache */
1053 #define p_bank p_this->p_libvlc_global->p_module_bank
1055 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1056 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1057 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1058 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1059 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1060 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1061 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1064 return p_module ? 0 : -1;
1067 /*****************************************************************************
1068 * AllocatePlugin: load a module into memory and initialize it.
1069 *****************************************************************************
1070 * This function loads a dynamically loadable module and allocates a structure
1071 * for its information data. The module can then be handled by module_Need
1072 * and module_Unneed. It can be removed by DeleteModule.
1073 *****************************************************************************/
1074 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1076 module_t * p_module;
1077 module_handle_t handle;
1079 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1081 /* Now that we have successfully loaded the module, we can
1082 * allocate a structure for it */
1083 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1084 if( p_module == NULL )
1086 msg_Err( p_this, "out of memory" );
1087 CloseModule( handle );
1091 /* We need to fill these since they may be needed by CallEntry() */
1092 p_module->psz_filename = psz_file;
1093 p_module->handle = handle;
1094 #ifndef HAVE_SHARED_LIBVLC
1095 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1097 p_module->b_loaded = VLC_TRUE;
1099 /* Initialize the module: fill p_module, default config */
1100 if( CallEntry( p_module ) != 0 )
1102 /* We couldn't call module_init() */
1103 vlc_object_destroy( p_module );
1104 CloseModule( handle );
1108 DupModule( p_module );
1109 p_module->psz_filename = strdup( p_module->psz_filename );
1111 /* Everything worked fine ! The module is ready to be added to the list. */
1112 p_module->b_builtin = VLC_FALSE;
1117 /*****************************************************************************
1118 * DupModule: make a plugin module standalone.
1119 *****************************************************************************
1120 * This function duplicates all strings in the module, so that the dynamic
1121 * object can be unloaded. It acts recursively on submodules.
1122 *****************************************************************************/
1123 static void DupModule( module_t *p_module )
1125 const char **pp_shortcut;
1128 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1130 *pp_shortcut = strdup( *pp_shortcut );
1133 /* We strdup() these entries so that they are still valid when the
1134 * module is unloaded. */
1135 p_module->psz_object_name = strdup( p_module->psz_object_name );
1136 p_module->psz_capability = strdup( p_module->psz_capability );
1137 p_module->psz_shortname = p_module->psz_shortname ?
1138 strdup( p_module->psz_shortname ) : NULL;
1139 p_module->psz_longname = strdup( p_module->psz_longname );
1141 if( p_module->psz_program != NULL )
1143 p_module->psz_program = strdup( p_module->psz_program );
1146 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1148 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1152 /*****************************************************************************
1153 * UndupModule: free a duplicated module.
1154 *****************************************************************************
1155 * This function frees the allocations done in DupModule().
1156 *****************************************************************************/
1157 static void UndupModule( module_t *p_module )
1159 const char **pp_shortcut;
1162 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1164 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1167 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1169 free( (void *)*pp_shortcut );
1172 free( (void *)p_module->psz_object_name );
1173 free( (void *)p_module->psz_capability );
1174 if( p_module->psz_shortname ) free( (void *)p_module->psz_shortname );
1175 free( (void *)p_module->psz_longname );
1177 if( p_module->psz_program != NULL )
1179 free( (void *)p_module->psz_program );
1183 #endif /* HAVE_DYNAMIC_PLUGINS */
1185 /*****************************************************************************
1186 * AllocateBuiltinModule: initialize a builtin module.
1187 *****************************************************************************
1188 * This function registers a builtin module and allocates a structure
1189 * for its information data. The module can then be handled by module_Need
1190 * and module_Unneed. It can be removed by DeleteModule.
1191 *****************************************************************************/
1192 static int AllocateBuiltinModule( vlc_object_t * p_this,
1193 int ( *pf_entry ) ( module_t * ) )
1195 module_t * p_module;
1197 /* Now that we have successfully loaded the module, we can
1198 * allocate a structure for it */
1199 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1200 if( p_module == NULL )
1202 msg_Err( p_this, "out of memory" );
1206 /* Initialize the module : fill p_module->psz_object_name, etc. */
1207 if( pf_entry( p_module ) != 0 )
1209 /* With a well-written module we shouldn't have to print an
1210 * additional error message here, but just make sure. */
1211 msg_Err( p_this, "failed calling entry point in builtin module" );
1212 vlc_object_destroy( p_module );
1216 /* Everything worked fine ! The module is ready to be added to the list. */
1217 p_module->b_builtin = VLC_TRUE;
1219 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1220 p_module->psz_object_name, p_module->psz_longname ); */
1222 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1227 /*****************************************************************************
1228 * DeleteModule: delete a module and its structure.
1229 *****************************************************************************
1230 * This function can only be called if the module isn't being used.
1231 *****************************************************************************/
1232 static int DeleteModule( module_t * p_module )
1234 vlc_object_detach( p_module );
1236 /* We free the structures that we strdup()ed in Allocate*Module(). */
1237 #ifdef HAVE_DYNAMIC_PLUGINS
1238 if( !p_module->b_builtin )
1240 if( p_module->b_loaded && p_module->b_unloadable )
1242 CloseModule( p_module->handle );
1244 UndupModule( p_module );
1245 free( p_module->psz_filename );
1249 /* Free and detach the object's children */
1250 while( p_module->i_children )
1252 vlc_object_t *p_this = p_module->pp_children[0];
1253 vlc_object_detach( p_this );
1254 vlc_object_destroy( p_this );
1257 config_Free( p_module );
1258 vlc_object_destroy( p_module );
1263 #ifdef HAVE_DYNAMIC_PLUGINS
1264 /*****************************************************************************
1265 * CallEntry: call an entry point.
1266 *****************************************************************************
1267 * This function calls a symbol given its name and a module structure. The
1268 * symbol MUST refer to a function returning int and taking a module_t* as
1270 *****************************************************************************/
1271 static int CallEntry( module_t * p_module )
1273 static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1274 int (* pf_symbol) ( module_t * p_module );
1276 /* Try to resolve the symbol */
1277 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1279 if( pf_symbol == NULL )
1281 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1282 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1283 psz_name, p_module->psz_filename );
1284 #elif defined(HAVE_DL_WINDOWS)
1285 char *psz_error = GetWindowsError();
1286 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1287 psz_name, p_module->psz_filename, psz_error );
1289 #elif defined(HAVE_DL_DLOPEN)
1290 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1291 psz_name, p_module->psz_filename, dlerror() );
1292 #elif defined(HAVE_DL_SHL_LOAD)
1293 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1294 psz_name, p_module->psz_filename, strerror(errno) );
1296 # error "Something is wrong in modules.c"
1301 /* We can now try to call the symbol */
1302 if( pf_symbol( p_module ) != 0 )
1304 /* With a well-written module we shouldn't have to print an
1305 * additional error message here, but just make sure. */
1306 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1307 psz_name, p_module->psz_filename );
1311 /* Everything worked fine, we can return */
1315 /*****************************************************************************
1316 * LoadModule: loads a dynamic library
1317 *****************************************************************************
1318 * This function loads a dynamically linked library using a system dependant
1319 * method. Will return 0 on success as well as the module handle.
1320 *****************************************************************************/
1321 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1322 module_handle_t *p_handle )
1324 module_handle_t handle;
1326 #if defined(HAVE_DL_DYLD)
1327 NSObjectFileImage image;
1328 NSObjectFileImageReturnCode ret;
1330 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1332 if( ret != NSObjectFileImageSuccess )
1334 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1338 /* Open the dynamic module */
1339 handle = NSLinkModule( image, psz_file,
1340 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1344 NSLinkEditErrors errors;
1345 const char *psz_file, *psz_err;
1347 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1348 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1349 NSDestroyObjectFileImage( image );
1353 /* Destroy our image, we won't need it */
1354 NSDestroyObjectFileImage( image );
1356 #elif defined(HAVE_DL_BEOS)
1357 handle = load_add_on( psz_file );
1360 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1364 #elif defined(HAVE_DL_WINDOWS)
1367 wchar_t psz_wfile[MAX_PATH];
1368 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1369 handle = LoadLibrary( psz_wfile );
1372 handle = LoadLibrary( psz_file );
1374 if( handle == NULL )
1376 char *psz_err = GetWindowsError();
1377 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1382 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1383 /* static is OK, we are called atomically */
1385 # if defined(SYS_LINUX)
1386 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1387 * are going to get namespace collisions when two modules have common
1388 * public symbols, but ALSA is being a pest here. */
1389 if( strstr( psz_file, "alsa_plugin" ) )
1391 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1392 if( handle == NULL )
1394 msg_Warn( p_this, "cannot load module `%s' (%s)",
1395 psz_file, dlerror() );
1401 handle = dlopen( psz_file, RTLD_NOW );
1402 if( handle == NULL )
1404 msg_Warn( p_this, "cannot load module `%s' (%s)",
1405 psz_file, dlerror() );
1409 #elif defined(HAVE_DL_DLOPEN)
1410 # if defined(DL_LAZY)
1411 handle = dlopen( psz_file, DL_LAZY );
1413 handle = dlopen( psz_file, 0 );
1415 if( handle == NULL )
1417 msg_Warn( p_this, "cannot load module `%s' (%s)",
1418 psz_file, dlerror() );
1422 #elif defined(HAVE_DL_SHL_LOAD)
1423 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1424 if( handle == NULL )
1426 msg_Warn( p_this, "cannot load module `%s' (%s)",
1427 psz_file, strerror(errno) );
1432 # error "Something is wrong in modules.c"
1440 /*****************************************************************************
1441 * CloseModule: unload a dynamic library
1442 *****************************************************************************
1443 * This function unloads a previously opened dynamically linked library
1444 * using a system dependant method. No return value is taken in consideration,
1445 * since some libraries sometimes refuse to close properly.
1446 *****************************************************************************/
1447 static void CloseModule( module_handle_t handle )
1449 #if defined(HAVE_DL_DYLD)
1450 NSUnLinkModule( handle, FALSE );
1452 #elif defined(HAVE_DL_BEOS)
1453 unload_add_on( handle );
1455 #elif defined(HAVE_DL_WINDOWS)
1456 FreeLibrary( handle );
1458 #elif defined(HAVE_DL_DLOPEN)
1461 #elif defined(HAVE_DL_SHL_LOAD)
1462 shl_unload( handle );
1468 /*****************************************************************************
1469 * GetSymbol: get a symbol from a dynamic library
1470 *****************************************************************************
1471 * This function queries a loaded library for a symbol specified in a
1472 * string, and returns a pointer to it. We don't check for dlerror() or
1473 * similar functions, since we want a non-NULL symbol anyway.
1474 *****************************************************************************/
1475 static void * _module_getsymbol( module_handle_t, const char * );
1477 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1479 void * p_symbol = _module_getsymbol( handle, psz_function );
1481 /* MacOS X dl library expects symbols to begin with "_". So do
1482 * some other operating systems. That's really lame, but hey, what
1484 if( p_symbol == NULL )
1486 char *psz_call = malloc( strlen( psz_function ) + 2 );
1488 strcpy( psz_call + 1, psz_function );
1489 psz_call[ 0 ] = '_';
1490 p_symbol = _module_getsymbol( handle, psz_call );
1497 static void * _module_getsymbol( module_handle_t handle,
1498 const char * psz_function )
1500 #if defined(HAVE_DL_DYLD)
1501 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1502 return NSAddressOfSymbol( sym );
1504 #elif defined(HAVE_DL_BEOS)
1506 if( B_OK == get_image_symbol( handle, psz_function,
1507 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1516 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1517 wchar_t psz_real[256];
1518 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1520 return (void *)GetProcAddress( handle, psz_real );
1522 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1523 return (void *)GetProcAddress( handle, (char *)psz_function );
1525 #elif defined(HAVE_DL_DLOPEN)
1526 return dlsym( handle, psz_function );
1528 #elif defined(HAVE_DL_SHL_LOAD)
1530 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1536 #if defined(HAVE_DL_WINDOWS)
1537 static char * GetWindowsError( void )
1539 #if defined(UNDER_CE)
1540 wchar_t psz_tmp[MAX_PATH];
1541 char * psz_buffer = malloc( MAX_PATH );
1543 char * psz_tmp = malloc( MAX_PATH );
1545 int i = 0, i_error = GetLastError();
1547 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1548 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1549 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1551 /* Go to the end of the string */
1552 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1559 #if defined(UNDER_CE)
1560 swprintf( psz_tmp + i, L" (error %i)", i_error );
1561 psz_tmp[ 255 ] = L'\0';
1563 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1564 psz_tmp[ 255 ] = '\0';
1568 #if defined(UNDER_CE)
1569 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1575 #endif /* HAVE_DL_WINDOWS */
1577 /*****************************************************************************
1578 * LoadPluginsCache: loads the plugins cache file
1579 *****************************************************************************
1580 * This function will load the plugin cache if present and valid. This cache
1581 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1582 * actually load the dynamically loadable module.
1583 * This allows us to only fully load plugins when they are actually used.
1584 *****************************************************************************/
1585 static void CacheLoad( vlc_object_t *p_this )
1587 char *psz_filename, *psz_homedir;
1589 int i, j, i_size, i_read;
1590 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1591 char p_cachelang[6], p_lang[6];
1593 module_cache_t **pp_cache = 0;
1594 int32_t i_file_size, i_marker;
1596 psz_homedir = p_this->p_libvlc->psz_homedir;
1599 msg_Err( p_this, "psz_homedir is null" );
1603 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1604 PLUGINSCACHE_DIR, CacheName() );
1607 msg_Err( p_this, "out of memory" );
1611 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1613 #if !defined( UNDER_CE )
1614 unlink( psz_filename );
1616 wchar_t psz_wf[MAX_PATH];
1617 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1618 DeleteFile( psz_wf );
1620 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1621 free( psz_filename );
1625 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1627 file = utf8_fopen( psz_filename, "rb" );
1630 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1632 free( psz_filename );
1635 free( psz_filename );
1637 /* Check the file size */
1638 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1639 if( i_read != sizeof(i_file_size) )
1641 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1647 fseek( file, 0, SEEK_END );
1648 if( ftell( file ) != i_file_size )
1650 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1651 "(corrupted size)" );
1655 fseek( file, sizeof(i_file_size), SEEK_SET );
1657 /* Check the file is a plugins cache */
1658 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1659 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1660 if( i_read != i_size ||
1661 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1663 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1668 /* Check Sub-version number */
1669 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1670 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1672 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1673 "(corrupted header)" );
1678 /* Check the language hasn't changed */
1679 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1680 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1681 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1683 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1684 "(language changed)" );
1689 /* Check header marker */
1690 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1691 if( i_read != sizeof(i_marker) ||
1692 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1694 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1695 "(corrupted header)" );
1700 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1701 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1703 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1704 malloc( i_cache * sizeof(void *) );
1706 #define LOAD_IMMEDIATE(a) \
1707 if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1708 #define LOAD_STRING(a) \
1711 if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1712 || ( i_size > 16384 ) ) \
1715 char *psz = malloc( i_size ); \
1716 if( fread( psz, i_size, 1, file ) != 1 ) { \
1720 if( psz[i_size-1] ) { \
1728 for( i = 0; i < i_cache; i++ )
1733 pp_cache[i] = malloc( sizeof(module_cache_t) );
1734 p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1736 /* Load common info */
1737 LOAD_STRING( pp_cache[i]->psz_file );
1738 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1739 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1740 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1742 if( pp_cache[i]->b_junk ) continue;
1744 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1746 /* Load additional infos */
1747 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1748 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1749 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1750 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1751 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1753 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1755 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1756 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1757 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1758 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1759 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1760 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1763 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1766 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1768 LOAD_IMMEDIATE( i_submodules );
1770 while( i_submodules-- )
1772 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1773 vlc_object_attach( p_module, pp_cache[i]->p_module );
1774 p_module->b_submodule = VLC_TRUE;
1776 LOAD_STRING( p_module->psz_object_name );
1777 LOAD_STRING( p_module->psz_shortname );
1778 LOAD_STRING( p_module->psz_longname );
1779 LOAD_STRING( p_module->psz_program );
1780 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1782 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1784 LOAD_STRING( p_module->psz_capability );
1785 LOAD_IMMEDIATE( p_module->i_score );
1786 LOAD_IMMEDIATE( p_module->i_cpu );
1787 LOAD_IMMEDIATE( p_module->b_unloadable );
1788 LOAD_IMMEDIATE( p_module->b_reentrant );
1797 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1800 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1807 int CacheLoadConfig( module_t *p_module, FILE *file )
1812 /* Calculate the structure length */
1813 LOAD_IMMEDIATE( p_module->i_config_items );
1814 LOAD_IMMEDIATE( p_module->i_bool_items );
1816 LOAD_IMMEDIATE( i_lines );
1818 /* Allocate memory */
1821 p_module->p_config =
1822 (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1823 if( p_module->p_config == NULL )
1825 p_module->confsize = 0;
1826 msg_Err( p_module, "config error: can't duplicate p_config" );
1830 p_module->confsize = i_lines;
1832 /* Do the duplication job */
1833 for (size_t i = 0; i < i_lines; i++ )
1835 LOAD_IMMEDIATE( p_module->p_config[i] );
1837 LOAD_STRING( p_module->p_config[i].psz_type );
1838 LOAD_STRING( p_module->p_config[i].psz_name );
1839 LOAD_STRING( p_module->p_config[i].psz_text );
1840 LOAD_STRING( p_module->p_config[i].psz_longtext );
1841 LOAD_STRING( p_module->p_config[i].psz_current );
1843 if (IsConfigStringType (p_module->p_config[i].i_type))
1845 LOAD_STRING (p_module->p_config[i].orig.psz);
1846 p_module->p_config[i].value.psz =
1847 (p_module->p_config[i].orig.psz != NULL)
1848 ? strdup (p_module->p_config[i].orig.psz) : NULL;
1849 p_module->p_config[i].saved.psz = NULL;
1853 memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1854 sizeof (p_module->p_config[i].value));
1855 memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1856 sizeof (p_module->p_config[i].saved));
1859 p_module->p_config[i].b_dirty = VLC_FALSE;
1861 p_module->p_config[i].p_lock = &p_module->object_lock;
1863 if( p_module->p_config[i].i_list )
1865 if( p_module->p_config[i].ppsz_list )
1868 p_module->p_config[i].ppsz_list =
1869 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1870 if( p_module->p_config[i].ppsz_list )
1872 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1873 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1874 p_module->p_config[i].ppsz_list[j] = NULL;
1877 if( p_module->p_config[i].ppsz_list_text )
1880 p_module->p_config[i].ppsz_list_text =
1881 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1882 if( p_module->p_config[i].ppsz_list_text )
1884 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1885 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1886 p_module->p_config[i].ppsz_list_text[j] = NULL;
1889 if( p_module->p_config[i].pi_list )
1891 p_module->p_config[i].pi_list =
1892 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1893 if( p_module->p_config[i].pi_list )
1895 for (int j = 0; j < p_module->p_config[i].i_list; j++)
1896 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1901 if( p_module->p_config[i].i_action )
1903 p_module->p_config[i].ppf_action =
1904 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1905 p_module->p_config[i].ppsz_action_text =
1906 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1908 for (int j = 0; j < p_module->p_config[i].i_action; j++)
1910 p_module->p_config[i].ppf_action[j] = 0;
1911 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1915 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1922 return VLC_EGENERIC;
1925 /*****************************************************************************
1926 * SavePluginsCache: saves the plugins cache to a file
1927 *****************************************************************************/
1928 static void CacheSave( vlc_object_t *p_this )
1930 static char const psz_tag[] =
1931 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1932 "# This file is a cache directory tag created by VLC.\r\n"
1933 "# For information about cache directory tags, see:\r\n"
1934 "# http://www.brynosaurus.com/cachedir/\r\n";
1936 char *psz_filename, *psz_homedir;
1939 module_cache_t **pp_cache;
1940 int32_t i_file_size = 0;
1942 psz_homedir = p_this->p_libvlc->psz_homedir;
1945 msg_Err( p_this, "psz_homedir is null" );
1949 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1950 strlen(psz_homedir) + strlen(CacheName()) );
1954 msg_Err( p_this, "out of memory" );
1958 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1960 config_CreateDir( p_this, psz_filename );
1962 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1964 config_CreateDir( p_this, psz_filename );
1966 strcat( psz_filename, "/CACHEDIR.TAG" );
1968 file = utf8_fopen( psz_filename, "wb" );
1971 fwrite( psz_tag, 1, strlen(psz_tag), file );
1975 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1976 PLUGINSCACHE_DIR, CacheName() );
1978 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1980 file = utf8_fopen( psz_filename, "wb" );
1983 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1985 free( psz_filename );
1988 free( psz_filename );
1990 /* Empty space for file size */
1991 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1993 /* Contains version number */
1994 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1996 /* Sub-version number (to avoid breakage in the dev version when cache
1997 * structure changes) */
1998 i_file_size = CACHE_SUBVERSION_NUM;
1999 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2002 fprintf( file, "%5.5s", _("C") );
2005 i_file_size = ftell( file );
2006 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2008 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2009 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2011 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2013 #define SAVE_IMMEDIATE(a) \
2014 fwrite( &a, sizeof(char), sizeof(a), file )
2015 #define SAVE_STRING(a) \
2016 { i_size = a ? strlen( a ) + 1 : 0; \
2017 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2018 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2021 for( i = 0; i < i_cache; i++ )
2024 uint32_t i_submodule;
2026 /* Save common info */
2027 SAVE_STRING( pp_cache[i]->psz_file );
2028 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2029 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2030 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2032 if( pp_cache[i]->b_junk ) continue;
2034 /* Save additional infos */
2035 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2036 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2037 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2038 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2039 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2041 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2043 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2044 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2045 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2046 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2047 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2048 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2051 CacheSaveConfig( pp_cache[i]->p_module, file );
2053 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2055 i_submodule = pp_cache[i]->p_module->i_children;
2056 SAVE_IMMEDIATE( i_submodule );
2057 for( i_submodule = 0;
2058 i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2061 module_t *p_module =
2062 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2064 SAVE_STRING( p_module->psz_object_name );
2065 SAVE_STRING( p_module->psz_shortname );
2066 SAVE_STRING( p_module->psz_longname );
2067 SAVE_STRING( p_module->psz_program );
2068 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2070 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2072 SAVE_STRING( p_module->psz_capability );
2073 SAVE_IMMEDIATE( p_module->i_score );
2074 SAVE_IMMEDIATE( p_module->i_cpu );
2075 SAVE_IMMEDIATE( p_module->b_unloadable );
2076 SAVE_IMMEDIATE( p_module->b_reentrant );
2080 /* Fill-up file size */
2081 i_file_size = ftell( file );
2082 fseek( file, 0, SEEK_SET );
2083 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2090 void CacheSaveConfig( module_t *p_module, FILE *file )
2092 uint32_t i_lines = p_module->confsize;
2095 SAVE_IMMEDIATE( p_module->i_config_items );
2096 SAVE_IMMEDIATE( p_module->i_bool_items );
2097 SAVE_IMMEDIATE( i_lines );
2099 for (size_t i = 0; i < i_lines ; i++)
2101 SAVE_IMMEDIATE( p_module->p_config[i] );
2103 SAVE_STRING( p_module->p_config[i].psz_type );
2104 SAVE_STRING( p_module->p_config[i].psz_name );
2105 SAVE_STRING( p_module->p_config[i].psz_text );
2106 SAVE_STRING( p_module->p_config[i].psz_longtext );
2107 SAVE_STRING( p_module->p_config[i].psz_current );
2108 if (IsConfigStringType (p_module->p_config[i].i_type))
2109 SAVE_STRING( p_module->p_config[i].orig.psz );
2111 if( p_module->p_config[i].i_list )
2113 if( p_module->p_config[i].ppsz_list )
2115 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2116 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2119 if( p_module->p_config[i].ppsz_list_text )
2121 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2122 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2124 if( p_module->p_config[i].pi_list )
2126 for (int j = 0; j < p_module->p_config[i].i_list; j++)
2127 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2131 for (int j = 0; j < p_module->p_config[i].i_action; j++)
2132 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2134 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2138 /*****************************************************************************
2139 * CacheName: Return the cache file name for this platform.
2140 *****************************************************************************/
2141 static char *CacheName( void )
2143 static char psz_cachename[32];
2145 /* Code int size, pointer size and endianness in the filename */
2146 int32_t x = 0xbe00001e;
2147 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", (int)sizeof(int),
2148 (int)sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2149 return psz_cachename;
2152 /*****************************************************************************
2153 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2154 *****************************************************************************/
2155 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2156 module_t *p_module )
2160 p_cache->pf_activate = p_module->pf_activate;
2161 p_cache->pf_deactivate = p_module->pf_deactivate;
2162 #ifndef HAVE_SHARED_LIBVLC
2163 p_cache->p_symbols = p_module->p_symbols;
2165 p_cache->handle = p_module->handle;
2167 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2169 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2170 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2171 p_cchild->pf_activate = p_child->pf_activate;
2172 p_cchild->pf_deactivate = p_child->pf_deactivate;
2173 #ifndef HAVE_SHARED_LIBVLC
2174 p_cchild->p_symbols = p_child->p_symbols;
2178 p_cache->b_loaded = VLC_TRUE;
2179 p_module->b_loaded = VLC_FALSE;
2182 /*****************************************************************************
2183 * FindPluginCache: finds the cache entry corresponding to a file
2184 *****************************************************************************/
2185 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2186 int64_t i_time, int64_t i_size )
2188 module_cache_t **pp_cache;
2191 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2192 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2194 for( i = 0; i < i_cache; i++ )
2196 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2197 pp_cache[i]->i_time == i_time &&
2198 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2204 #endif /* HAVE_DYNAMIC_PLUGINS */