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() */
39 #include <vlc/input.h>
45 #ifdef HAVE_SYS_TYPES_H
46 # include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
55 #if !defined(HAVE_DYNAMIC_PLUGINS)
56 /* no support for plugins */
57 #elif defined(HAVE_DL_DYLD)
58 # if defined(HAVE_MACH_O_DYLD_H)
59 # include <mach-o/dyld.h>
61 #elif defined(HAVE_DL_BEOS)
62 # if defined(HAVE_IMAGE_H)
65 #elif defined(HAVE_DL_WINDOWS)
67 #elif defined(HAVE_DL_DLOPEN)
68 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
71 # if defined(HAVE_SYS_DL_H)
74 #elif defined(HAVE_DL_SHL_LOAD)
75 # if defined(HAVE_DL_H)
80 #include "vlc_error.h"
82 #include "vlc_interface.h"
83 #include "vlc_interaction.h"
84 #include "intf_eject.h"
86 #include "vlc_playlist.h"
88 #include "vlc_video.h"
89 #include "video_output.h"
90 #include "vout_synchro.h"
93 #include "audio_output.h"
94 #include "aout_internal.h"
96 #include "stream_output.h"
97 #include "vlc_httpd.h"
104 #include "iso_lang.h"
107 #include "vlc_block.h"
111 #include "vlc_image.h"
114 #include "vlc_update.h"
115 #include "vlc_strings.h"
117 #if defined( _MSC_VER ) && defined( UNDER_CE )
118 # include "modules_builtin_evc.h"
119 #elif defined( _MSC_VER )
120 # include "modules_builtin_msvc.h"
122 # include "modules_builtin.h"
126 #if defined( WIN32 ) || defined( UNDER_CE )
127 /* Avoid name collisions */
128 # define LoadModule(a,b,c) LoadVlcModule(a,b,c)
131 /*****************************************************************************
133 *****************************************************************************/
134 #ifdef HAVE_DYNAMIC_PLUGINS
135 static void AllocateAllPlugins ( vlc_object_t * );
136 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
137 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
138 static module_t * AllocatePlugin( vlc_object_t *, char * );
140 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
141 static int DeleteModule ( module_t * );
142 #ifdef HAVE_DYNAMIC_PLUGINS
143 static void DupModule ( module_t * );
144 static void UndupModule ( module_t * );
145 static int CallEntry ( module_t * );
146 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
147 static void CloseModule ( module_handle_t );
148 static void * GetSymbol ( module_handle_t, const char * );
149 static void CacheLoad ( vlc_object_t * );
150 static int CacheLoadConfig ( module_t *, FILE * );
151 static void CacheSave ( vlc_object_t * );
152 static void CacheSaveConfig ( module_t *, FILE * );
153 static char * CacheName ( void );
154 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
155 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
157 #if defined(HAVE_DL_WINDOWS)
158 static char * GetWindowsError ( void );
163 /* Sub-version number
164 * (only used to avoid breakage in dev version when cache structure changes) */
165 #define CACHE_SUBVERSION_NUM 1
167 /*****************************************************************************
168 * module_InitBank: create the module bank.
169 *****************************************************************************
170 * This function creates a module bank structure which will be filled later
171 * on with all the modules found.
172 *****************************************************************************/
173 void __module_InitBank( vlc_object_t *p_this )
175 module_bank_t *p_bank;
178 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
179 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
180 vlc_mutex_lock( lockval.p_address );
181 if( p_this->p_libvlc_global->p_module_bank )
183 p_this->p_libvlc_global->p_module_bank->i_usage++;
184 vlc_mutex_unlock( lockval.p_address );
185 var_Destroy( p_this->p_libvlc_global, "libvlc" );
188 vlc_mutex_unlock( lockval.p_address );
189 var_Destroy( p_this->p_libvlc_global, "libvlc" );
191 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
192 p_bank->psz_object_name = "module bank";
194 p_bank->i_cache = p_bank->i_loaded_cache = 0;
195 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
196 p_bank->b_cache = p_bank->b_cache_dirty =
197 p_bank->b_cache_delete = VLC_FALSE;
200 * Store the symbols to be exported
202 #if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
203 STORE_SYMBOLS( &p_bank->symbols );
206 /* Everything worked, attach the object */
207 p_this->p_libvlc_global->p_module_bank = p_bank;
208 vlc_object_attach( p_bank, p_this->p_libvlc_global );
210 module_LoadMain( p_this );
215 /*****************************************************************************
216 * module_ResetBank: reset the module bank.
217 *****************************************************************************
218 * This function resets the module bank by unloading all unused plugin
220 *****************************************************************************/
221 void __module_ResetBank( vlc_object_t *p_this )
223 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
227 /*****************************************************************************
228 * module_EndBank: empty the module bank.
229 *****************************************************************************
230 * This function unloads all unused plugin modules and empties the module
231 * bank in case of success.
232 *****************************************************************************/
233 void __module_EndBank( vlc_object_t *p_this )
238 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
239 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
240 vlc_mutex_lock( lockval.p_address );
241 if( !p_this->p_libvlc_global->p_module_bank )
243 vlc_mutex_unlock( lockval.p_address );
244 var_Destroy( p_this->p_libvlc_global, "libvlc" );
247 if( --p_this->p_libvlc_global->p_module_bank->i_usage )
249 vlc_mutex_unlock( lockval.p_address );
250 var_Destroy( p_this->p_libvlc_global, "libvlc" );
253 vlc_mutex_unlock( lockval.p_address );
254 var_Destroy( p_this->p_libvlc_global, "libvlc" );
256 config_AutoSaveConfigFile( p_this );
258 #ifdef HAVE_DYNAMIC_PLUGINS
259 #define p_bank p_this->p_libvlc_global->p_module_bank
260 if( p_bank->b_cache ) CacheSave( p_this );
261 while( p_bank->i_loaded_cache-- )
263 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
264 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
266 if( p_bank->pp_loaded_cache )
267 free( p_bank->pp_loaded_cache );
269 while( p_bank->i_cache-- )
271 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
272 free( p_bank->pp_cache[p_bank->i_cache] );
274 if( p_bank->pp_cache )
275 free( p_bank->pp_cache );
279 vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
281 while( p_this->p_libvlc_global->p_module_bank->i_children )
283 p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
285 if( DeleteModule( p_next ) )
287 /* Module deletion failed */
288 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
289 p_next->psz_object_name );
291 /* We just free the module by hand. Niahahahahaha. */
292 vlc_object_detach( p_next );
293 vlc_object_destroy( p_next );
297 vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
298 p_this->p_libvlc_global->p_module_bank = NULL;
303 /*****************************************************************************
304 * module_LoadMain: load the main program info into the module bank.
305 *****************************************************************************
306 * This function fills the module bank structure with the main module infos.
307 * This is very useful as it will allow us to consider the main program just
308 * as another module, and for instance the configuration options of main will
309 * be available in the module bank structure just as for every other module.
310 *****************************************************************************/
311 void __module_LoadMain( vlc_object_t *p_this )
315 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
316 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
317 vlc_mutex_lock( lockval.p_address );
318 if( p_this->p_libvlc_global->p_module_bank->b_main )
320 vlc_mutex_unlock( lockval.p_address );
321 var_Destroy( p_this->p_libvlc_global, "libvlc" );
324 p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
325 vlc_mutex_unlock( lockval.p_address );
326 var_Destroy( p_this->p_libvlc_global, "libvlc" );
328 AllocateBuiltinModule( p_this, vlc_entry__main );
331 /*****************************************************************************
332 * module_LoadBuiltins: load all modules which we built with.
333 *****************************************************************************
334 * This function fills the module bank structure with the builtin modules.
335 *****************************************************************************/
336 void __module_LoadBuiltins( vlc_object_t * p_this )
340 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
341 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
342 vlc_mutex_lock( lockval.p_address );
343 if( p_this->p_libvlc_global->p_module_bank->b_builtins )
345 vlc_mutex_unlock( lockval.p_address );
346 var_Destroy( p_this->p_libvlc_global, "libvlc" );
349 p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
350 vlc_mutex_unlock( lockval.p_address );
351 var_Destroy( p_this->p_libvlc_global, "libvlc" );
353 msg_Dbg( p_this, "checking builtin modules" );
354 ALLOCATE_ALL_BUILTINS();
357 /*****************************************************************************
358 * module_LoadPlugins: load all plugin modules we can find.
359 *****************************************************************************
360 * This function fills the module bank structure with the plugin modules.
361 *****************************************************************************/
362 void __module_LoadPlugins( vlc_object_t * p_this )
364 #ifdef HAVE_DYNAMIC_PLUGINS
367 var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
368 var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
369 vlc_mutex_lock( lockval.p_address );
370 if( p_this->p_libvlc_global->p_module_bank->b_plugins )
372 vlc_mutex_unlock( lockval.p_address );
373 var_Destroy( p_this->p_libvlc_global, "libvlc" );
376 p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
377 vlc_mutex_unlock( lockval.p_address );
378 var_Destroy( p_this->p_libvlc_global, "libvlc" );
380 msg_Dbg( p_this, "checking plugin modules" );
382 if( config_GetInt( p_this, "plugins-cache" ) )
383 p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
385 if( p_this->p_libvlc_global->p_module_bank->b_cache ||
386 p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
388 AllocateAllPlugins( p_this );
392 /*****************************************************************************
393 * module_Need: return the best module function, given a capability list.
394 *****************************************************************************
395 * This function returns the module that best fits the asked capabilities.
396 *****************************************************************************/
397 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
398 const char *psz_name, vlc_bool_t b_strict )
400 typedef struct module_list_t module_list_t;
407 module_list_t *p_next;
410 module_list_t *p_list, *p_first, *p_tmp;
413 int i_which_module, i_index = 0;
414 vlc_bool_t b_intf = VLC_FALSE;
419 char *psz_shortcuts = NULL, *psz_var = NULL;
420 vlc_bool_t b_force_backup = p_this->b_force;
423 /* Deal with variables */
424 if( psz_name && psz_name[0] == '$' )
427 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
428 var_Get( p_this, psz_name + 1, &val );
429 psz_var = val.psz_string;
433 /* Count how many different shortcuts were asked for */
434 if( psz_name && *psz_name )
436 char *psz_parser, *psz_last_shortcut;
438 /* If the user wants none, give him none. */
439 if( !strcmp( psz_name, "none" ) )
441 if( psz_var ) free( psz_var );
446 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
448 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
450 if( *psz_parser == ',' )
454 psz_last_shortcut = psz_parser + 1;
458 /* Check if the user wants to override the "strict" mode */
459 if( psz_last_shortcut )
461 if( !strcmp(psz_last_shortcut, "none") )
466 else if( !strcmp(psz_last_shortcut, "any") )
468 b_strict = VLC_FALSE;
474 /* Sort the modules and test them */
475 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
476 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
479 /* Parse the module list for capabilities and probe each of them */
480 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
482 int i_shortcut_bonus = 0;
484 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
486 /* Test that this module can do what we need */
487 if( strcmp( p_module->psz_capability, psz_capability ) )
489 /* Don't recurse through the sub-modules because vlc_list_find()
490 * will list them anyway. */
494 /* Test if we have the required CPU */
495 if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
500 /* If we required a shortcut, check this plugin provides it. */
501 if( i_shortcuts > 0 )
504 int i_dummy, i_short = i_shortcuts;
505 char *psz_name = psz_shortcuts;
507 /* Let's drop modules with a <= 0 score (unless they are
508 * explicitly requested) */
509 b_trash = p_module->i_score <= 0;
513 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
515 if( !strcasecmp( psz_name,
516 p_module->pp_shortcuts[i_dummy] ) )
520 i_shortcut_bonus = i_short * 10000;
525 if( i_shortcut_bonus )
527 /* We found it... remember ? */
531 /* Go to the next shortcut... This is so lame! */
540 /* If we are in "strict" mode and we couldn't
541 * find the module in the list of provided shortcuts,
542 * then kick the bastard out of here!!! */
543 if( i_short == 0 && b_strict )
553 /* If we didn't require a shortcut, trash <= 0 scored plugins */
554 else if( p_module->i_score <= 0 )
559 /* Special case: test if we requested a particular intf plugin */
560 if( !i_shortcuts && p_module->psz_program
561 && !strcmp( psz_capability, "interface" )
562 && !strcmp( p_module->psz_program,
563 p_this->p_libvlc->psz_object_name ) )
567 /* Remove previous non-matching plugins */
574 /* This one doesn't match */
578 /* Store this new module */
579 p_list[ i_index ].p_module = p_module;
580 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
581 p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
583 /* Add it to the modules-to-probe list */
586 p_list[ 0 ].p_next = NULL;
591 /* Ok, so at school you learned that quicksort is quick, and
592 * bubble sort sucks raw eggs. But that's when dealing with
593 * thousands of items. Here we have barely 50. */
594 module_list_t *p_newlist = p_first;
596 if( p_first->i_score < p_list[ i_index ].i_score )
598 p_list[ i_index ].p_next = p_first;
599 p_first = &p_list[ i_index ];
603 while( p_newlist->p_next != NULL &&
604 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
606 p_newlist = p_newlist->p_next;
609 p_list[ i_index ].p_next = p_newlist->p_next;
610 p_newlist->p_next = &p_list[ i_index ];
617 msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
618 i_index, i_index == 1 ? "" : "s" );
620 /* Lock all candidate modules */
622 while( p_tmp != NULL )
624 vlc_object_yield( p_tmp->p_module );
625 p_tmp = p_tmp->p_next;
628 /* We can release the list, interesting modules were yielded */
629 vlc_list_release( p_all );
631 /* Parse the linked list and use the first successful module */
633 while( p_tmp != NULL )
635 #ifdef HAVE_DYNAMIC_PLUGINS
636 /* Make sure the module is loaded in mem */
637 module_t *p_module = p_tmp->p_module->b_submodule ?
638 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
639 if( !p_module->b_builtin && !p_module->b_loaded )
641 module_t *p_new_module =
642 AllocatePlugin( p_this, p_module->psz_filename );
645 CacheMerge( p_this, p_module, p_new_module );
646 vlc_object_attach( p_new_module, p_module );
647 DeleteModule( p_new_module );
652 p_this->b_force = p_tmp->b_force;
653 if( p_tmp->p_module->pf_activate
654 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
659 vlc_object_release( p_tmp->p_module );
660 p_tmp = p_tmp->p_next;
663 /* Store the locked module value */
666 p_module = p_tmp->p_module;
667 p_tmp = p_tmp->p_next;
674 /* Unlock the remaining modules */
675 while( p_tmp != NULL )
677 vlc_object_release( p_tmp->p_module );
678 p_tmp = p_tmp->p_next;
682 p_this->b_force = b_force_backup;
684 if( p_module != NULL )
686 msg_Dbg( p_this, "using %s module \"%s\"",
687 psz_capability, p_module->psz_object_name );
689 else if( p_first == NULL )
691 if( !strcmp( psz_capability, "access_demux" ) )
693 msg_Warn( p_this, "no %s module matched \"%s\"",
694 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
698 msg_Err( p_this, "no %s module matched \"%s\"",
699 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
702 else if( psz_name != NULL && *psz_name )
704 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
705 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
710 free( psz_shortcuts );
718 /* Don't forget that the module is still locked */
722 /*****************************************************************************
723 * module_Unneed: decrease the usage count of a module.
724 *****************************************************************************
725 * This function must be called by the thread that called module_Need, to
726 * decrease the reference count and allow for hiding of modules.
727 *****************************************************************************/
728 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
730 /* Use the close method */
731 if( p_module->pf_deactivate )
733 p_module->pf_deactivate( p_this );
736 msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
738 vlc_object_release( p_module );
743 /*****************************************************************************
744 * Following functions are local.
745 *****************************************************************************/
747 /*****************************************************************************
748 * AllocateAllPlugins: load all plugin modules we can find.
749 *****************************************************************************/
750 #ifdef HAVE_DYNAMIC_PLUGINS
751 static void AllocateAllPlugins( vlc_object_t *p_this )
753 /* Yes, there are two NULLs because we replace one with "plugin-path". */
754 #if defined( WIN32 ) || defined( UNDER_CE )
755 char *path[] = { "modules", "", "plugins", 0, 0 };
757 char *path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
760 char **ppsz_path = path;
763 /* If the user provided a plugin path, we add it to the list */
764 path[ sizeof(path)/sizeof(char*) - 2 ] =
765 config_GetPsz( p_this, "plugin-path" );
767 for( ; *ppsz_path != NULL ; ppsz_path++ )
769 if( !(*ppsz_path)[0] ) 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 int i_dirlen = strlen( *ppsz_path );
782 i_dirlen += strlen( p_this->p_libvlc_global->psz_vlcpath ) + 2;
784 psz_fullpath = malloc( i_dirlen );
785 if( psz_fullpath == NULL )
790 sprintf( psz_fullpath, "%s\\%s",
791 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path );
793 sprintf( psz_fullpath, "%s/%s",
794 p_this->p_libvlc_global->psz_vlcpath, *ppsz_path );
800 psz_fullpath = strdup( *ppsz_path );
803 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
805 /* Don't go deeper than 5 subdirectories */
806 AllocatePluginDir( p_this, psz_fullpath, 5 );
808 free( psz_fullpath );
811 /* Free plugin-path */
812 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
813 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
814 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
817 /*****************************************************************************
818 * AllocatePluginDir: recursively parse a directory to look for plugins
819 *****************************************************************************/
820 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
823 #if defined( UNDER_CE ) || defined( _MSC_VER )
825 wchar_t psz_wpath[MAX_PATH + 256];
826 wchar_t psz_wdir[MAX_PATH];
828 char psz_path[MAX_PATH + 256];
829 WIN32_FIND_DATA finddata;
835 struct dirent * file;
839 if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
844 #if defined( UNDER_CE ) || defined( _MSC_VER )
846 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
848 rc = GetFileAttributes( psz_wdir );
849 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
851 /* Parse all files in the directory */
852 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
854 rc = GetFileAttributes( psz_dir );
855 if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
858 /* Parse all files in the directory */
859 sprintf( psz_path, "%s\\*", psz_dir );
862 handle = FindFirstFile( psz_wpath, &finddata );
864 handle = FindFirstFile( psz_path, &finddata );
866 if( handle == INVALID_HANDLE_VALUE )
868 /* Empty directory */
872 /* Parse the directory and try to load all files it contains. */
876 unsigned int i_len = wcslen( finddata.cFileName );
877 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
878 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
880 unsigned int i_len = strlen( finddata.cFileName );
881 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
885 if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
886 || !strcmp( finddata.cFileName, ".." ) )
888 if( !FindNextFile( handle, &finddata ) ) break;
893 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
895 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
898 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
900 else if( i_len > strlen( LIBEXT )
901 /* We only load files ending with LIBEXT */
902 && !strncasecmp( psz_path + strlen( psz_path)
904 LIBEXT, strlen( LIBEXT ) ) )
906 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
907 int64_t i_time = 0, i_size = 0;
910 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
913 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
917 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
919 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
920 i_size = attrbuf.nFileSizeHigh;
922 i_size |= attrbuf.nFileSizeLow;
926 AllocatePluginFile( p_this, psz_file, i_time, i_size );
929 while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
931 /* Close the directory */
935 dir = opendir( psz_dir );
941 i_dirlen = strlen( psz_dir );
943 /* Parse the directory and try to load all files it contains. */
944 while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
951 if( !*file->d_name || !strcmp( file->d_name, "." )
952 || !strcmp( file->d_name, ".." ) )
957 i_len = strlen( file->d_name );
958 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
960 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
962 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
965 i_stat = stat( psz_file, &statbuf );
966 if( !i_stat && statbuf.st_mode & S_IFDIR )
968 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
970 else if( i_len > strlen( LIBEXT )
971 /* We only load files ending with LIBEXT */
972 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
973 LIBEXT, strlen( LIBEXT ) ) )
975 int64_t i_time = 0, i_size = 0;
979 i_time = statbuf.st_mtime;
980 i_size = statbuf.st_size;
983 AllocatePluginFile( p_this, psz_file, i_time, i_size );
989 /* Close the directory */
995 /*****************************************************************************
996 * AllocatePluginFile: load a module into memory and initialize it.
997 *****************************************************************************
998 * This function loads a dynamically loadable module and allocates a structure
999 * for its information data. The module can then be handled by module_Need
1000 * and module_Unneed. It can be removed by DeleteModule.
1001 *****************************************************************************/
1002 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1003 int64_t i_file_time, int64_t i_file_size )
1005 module_t * p_module;
1006 module_cache_t *p_cache_entry = NULL;
1009 * Check our plugins cache first then load plugin if needed
1012 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1014 if( !p_cache_entry )
1016 p_module = AllocatePlugin( p_this, psz_file );
1020 /* If junk dll, don't try to load it */
1021 if( p_cache_entry->b_junk )
1027 module_config_t *p_item;
1029 p_module = p_cache_entry->p_module;
1030 p_module->b_loaded = VLC_FALSE;
1032 /* For now we force loading if the module's config contains
1033 * callbacks or actions.
1034 * Could be optimized by adding an API call.*/
1035 for( p_item = p_module->p_config;
1036 p_item->i_type != CONFIG_HINT_END; p_item++ )
1038 if( p_item->pf_callback || p_item->i_action )
1039 p_module = AllocatePlugin( p_this, psz_file );
1046 /* Everything worked fine !
1047 * The module is ready to be added to the list. */
1048 p_module->b_builtin = VLC_FALSE;
1050 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1051 p_module->psz_object_name, p_module->psz_longname ); */
1053 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1056 if( !p_this->p_libvlc_global->p_module_bank->b_cache ) return 0;
1058 /* Add entry to cache */
1059 #define p_bank p_this->p_libvlc_global->p_module_bank
1061 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1062 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1063 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1064 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1065 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1066 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1067 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1070 return p_module ? 0 : -1;
1073 /*****************************************************************************
1074 * AllocatePlugin: load a module into memory and initialize it.
1075 *****************************************************************************
1076 * This function loads a dynamically loadable module and allocates a structure
1077 * for its information data. The module can then be handled by module_Need
1078 * and module_Unneed. It can be removed by DeleteModule.
1079 *****************************************************************************/
1080 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1082 module_t * p_module;
1083 module_handle_t handle;
1085 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1087 /* Now that we have successfully loaded the module, we can
1088 * allocate a structure for it */
1089 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1090 if( p_module == NULL )
1092 msg_Err( p_this, "out of memory" );
1093 CloseModule( handle );
1097 /* We need to fill these since they may be needed by CallEntry() */
1098 p_module->psz_filename = psz_file;
1099 p_module->handle = handle;
1100 #ifndef HAVE_SHARED_LIBVLC
1101 p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1103 p_module->b_loaded = VLC_TRUE;
1105 /* Initialize the module: fill p_module, default config */
1106 if( CallEntry( p_module ) != 0 )
1108 /* We couldn't call module_init() */
1109 vlc_object_destroy( p_module );
1110 CloseModule( handle );
1114 DupModule( p_module );
1115 p_module->psz_filename = strdup( p_module->psz_filename );
1117 /* Everything worked fine ! The module is ready to be added to the list. */
1118 p_module->b_builtin = VLC_FALSE;
1123 /*****************************************************************************
1124 * DupModule: make a plugin module standalone.
1125 *****************************************************************************
1126 * This function duplicates all strings in the module, so that the dynamic
1127 * object can be unloaded. It acts recursively on submodules.
1128 *****************************************************************************/
1129 static void DupModule( module_t *p_module )
1134 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1136 *pp_shortcut = strdup( *pp_shortcut );
1139 /* We strdup() these entries so that they are still valid when the
1140 * module is unloaded. */
1141 p_module->psz_object_name = strdup( p_module->psz_object_name );
1142 p_module->psz_capability = strdup( p_module->psz_capability );
1143 p_module->psz_shortname = p_module->psz_shortname ?
1144 strdup( p_module->psz_shortname ) : NULL;
1145 p_module->psz_longname = strdup( p_module->psz_longname );
1147 if( p_module->psz_program != NULL )
1149 p_module->psz_program = strdup( p_module->psz_program );
1152 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1154 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1158 /*****************************************************************************
1159 * UndupModule: free a duplicated module.
1160 *****************************************************************************
1161 * This function frees the allocations done in DupModule().
1162 *****************************************************************************/
1163 static void UndupModule( module_t *p_module )
1168 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1170 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1173 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1175 free( *pp_shortcut );
1178 free( p_module->psz_object_name );
1179 free( p_module->psz_capability );
1180 if( p_module->psz_shortname ) free( p_module->psz_shortname );
1181 free( p_module->psz_longname );
1183 if( p_module->psz_program != NULL )
1185 free( p_module->psz_program );
1189 #endif /* HAVE_DYNAMIC_PLUGINS */
1191 /*****************************************************************************
1192 * AllocateBuiltinModule: initialize a builtin module.
1193 *****************************************************************************
1194 * This function registers a builtin module and allocates a structure
1195 * for its information data. The module can then be handled by module_Need
1196 * and module_Unneed. It can be removed by DeleteModule.
1197 *****************************************************************************/
1198 static int AllocateBuiltinModule( vlc_object_t * p_this,
1199 int ( *pf_entry ) ( module_t * ) )
1201 module_t * p_module;
1203 /* Now that we have successfully loaded the module, we can
1204 * allocate a structure for it */
1205 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1206 if( p_module == NULL )
1208 msg_Err( p_this, "out of memory" );
1212 /* Initialize the module : fill p_module->psz_object_name, etc. */
1213 if( pf_entry( p_module ) != 0 )
1215 /* With a well-written module we shouldn't have to print an
1216 * additional error message here, but just make sure. */
1217 msg_Err( p_this, "failed calling entry point in builtin module" );
1218 vlc_object_destroy( p_module );
1222 /* Everything worked fine ! The module is ready to be added to the list. */
1223 p_module->b_builtin = VLC_TRUE;
1225 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1226 p_module->psz_object_name, p_module->psz_longname ); */
1228 vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1233 /*****************************************************************************
1234 * DeleteModule: delete a module and its structure.
1235 *****************************************************************************
1236 * This function can only be called if the module isn't being used.
1237 *****************************************************************************/
1238 static int DeleteModule( module_t * p_module )
1240 vlc_object_detach( p_module );
1242 /* We free the structures that we strdup()ed in Allocate*Module(). */
1243 #ifdef HAVE_DYNAMIC_PLUGINS
1244 if( !p_module->b_builtin )
1246 if( p_module->b_loaded && p_module->b_unloadable )
1248 CloseModule( p_module->handle );
1250 UndupModule( p_module );
1251 free( p_module->psz_filename );
1255 /* Free and detach the object's children */
1256 while( p_module->i_children )
1258 vlc_object_t *p_this = p_module->pp_children[0];
1259 vlc_object_detach( p_this );
1260 vlc_object_destroy( p_this );
1263 config_Free( p_module );
1264 vlc_object_destroy( p_module );
1269 #ifdef HAVE_DYNAMIC_PLUGINS
1270 /*****************************************************************************
1271 * CallEntry: call an entry point.
1272 *****************************************************************************
1273 * This function calls a symbol given its name and a module structure. The
1274 * symbol MUST refer to a function returning int and taking a module_t* as
1276 *****************************************************************************/
1277 static int CallEntry( module_t * p_module )
1279 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1280 int (* pf_symbol) ( module_t * p_module );
1282 /* Try to resolve the symbol */
1283 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1285 if( pf_symbol == NULL )
1287 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1288 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1289 psz_name, p_module->psz_filename );
1290 #elif defined(HAVE_DL_WINDOWS)
1291 char *psz_error = GetWindowsError();
1292 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1293 psz_name, p_module->psz_filename, psz_error );
1295 #elif defined(HAVE_DL_DLOPEN)
1296 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1297 psz_name, p_module->psz_filename, dlerror() );
1298 #elif defined(HAVE_DL_SHL_LOAD)
1299 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1300 psz_name, p_module->psz_filename, strerror(errno) );
1302 # error "Something is wrong in modules.c"
1307 /* We can now try to call the symbol */
1308 if( pf_symbol( p_module ) != 0 )
1310 /* With a well-written module we shouldn't have to print an
1311 * additional error message here, but just make sure. */
1312 msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1313 psz_name, p_module->psz_filename );
1317 /* Everything worked fine, we can return */
1321 /*****************************************************************************
1322 * LoadModule: loads a dynamic library
1323 *****************************************************************************
1324 * This function loads a dynamically linked library using a system dependant
1325 * method. Will return 0 on success as well as the module handle.
1326 *****************************************************************************/
1327 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1328 module_handle_t *p_handle )
1330 module_handle_t handle;
1332 #if defined(HAVE_DL_DYLD)
1333 NSObjectFileImage image;
1334 NSObjectFileImageReturnCode ret;
1336 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1338 if( ret != NSObjectFileImageSuccess )
1340 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1344 /* Open the dynamic module */
1345 handle = NSLinkModule( image, psz_file,
1346 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1350 NSLinkEditErrors errors;
1351 const char *psz_file, *psz_err;
1353 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1354 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1355 NSDestroyObjectFileImage( image );
1359 /* Destroy our image, we won't need it */
1360 NSDestroyObjectFileImage( image );
1362 #elif defined(HAVE_DL_BEOS)
1363 handle = load_add_on( psz_file );
1366 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1370 #elif defined(HAVE_DL_WINDOWS)
1373 wchar_t psz_wfile[MAX_PATH];
1374 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1375 handle = LoadLibrary( psz_wfile );
1378 handle = LoadLibrary( psz_file );
1380 if( handle == NULL )
1382 char *psz_err = GetWindowsError();
1383 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1388 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1389 /* static is OK, we are called atomically */
1391 # if defined(SYS_LINUX)
1392 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1393 * are going to get namespace collisions when two modules have common
1394 * public symbols, but ALSA is being a pest here. */
1395 if( strstr( psz_file, "alsa_plugin" ) )
1397 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1398 if( handle == NULL )
1400 msg_Warn( p_this, "cannot load module `%s' (%s)",
1401 psz_file, dlerror() );
1407 handle = dlopen( psz_file, RTLD_NOW );
1408 if( handle == NULL )
1410 msg_Warn( p_this, "cannot load module `%s' (%s)",
1411 psz_file, dlerror() );
1415 #elif defined(HAVE_DL_DLOPEN)
1416 # if defined(DL_LAZY)
1417 handle = dlopen( psz_file, DL_LAZY );
1419 handle = dlopen( psz_file, 0 );
1421 if( handle == NULL )
1423 msg_Warn( p_this, "cannot load module `%s' (%s)",
1424 psz_file, dlerror() );
1428 #elif defined(HAVE_DL_SHL_LOAD)
1429 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1430 if( handle == NULL )
1432 msg_Warn( p_this, "cannot load module `%s' (%s)",
1433 psz_file, strerror(errno) );
1438 # error "Something is wrong in modules.c"
1446 /*****************************************************************************
1447 * CloseModule: unload a dynamic library
1448 *****************************************************************************
1449 * This function unloads a previously opened dynamically linked library
1450 * using a system dependant method. No return value is taken in consideration,
1451 * since some libraries sometimes refuse to close properly.
1452 *****************************************************************************/
1453 static void CloseModule( module_handle_t handle )
1455 #if defined(HAVE_DL_DYLD)
1456 NSUnLinkModule( handle, FALSE );
1458 #elif defined(HAVE_DL_BEOS)
1459 unload_add_on( handle );
1461 #elif defined(HAVE_DL_WINDOWS)
1462 FreeLibrary( handle );
1464 #elif defined(HAVE_DL_DLOPEN)
1467 #elif defined(HAVE_DL_SHL_LOAD)
1468 shl_unload( handle );
1474 /*****************************************************************************
1475 * GetSymbol: get a symbol from a dynamic library
1476 *****************************************************************************
1477 * This function queries a loaded library for a symbol specified in a
1478 * string, and returns a pointer to it. We don't check for dlerror() or
1479 * similar functions, since we want a non-NULL symbol anyway.
1480 *****************************************************************************/
1481 static void * _module_getsymbol( module_handle_t, const char * );
1483 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1485 void * p_symbol = _module_getsymbol( handle, psz_function );
1487 /* MacOS X dl library expects symbols to begin with "_". So do
1488 * some other operating systems. That's really lame, but hey, what
1490 if( p_symbol == NULL )
1492 char *psz_call = malloc( strlen( psz_function ) + 2 );
1494 strcpy( psz_call + 1, psz_function );
1495 psz_call[ 0 ] = '_';
1496 p_symbol = _module_getsymbol( handle, psz_call );
1503 static void * _module_getsymbol( module_handle_t handle,
1504 const char * psz_function )
1506 #if defined(HAVE_DL_DYLD)
1507 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1508 return NSAddressOfSymbol( sym );
1510 #elif defined(HAVE_DL_BEOS)
1512 if( B_OK == get_image_symbol( handle, psz_function,
1513 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1522 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1523 wchar_t psz_real[256];
1524 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1526 return (void *)GetProcAddress( handle, psz_real );
1528 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1529 return (void *)GetProcAddress( handle, (char *)psz_function );
1531 #elif defined(HAVE_DL_DLOPEN)
1532 return dlsym( handle, psz_function );
1534 #elif defined(HAVE_DL_SHL_LOAD)
1536 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1542 #if defined(HAVE_DL_WINDOWS)
1543 static char * GetWindowsError( void )
1545 #if defined(UNDER_CE)
1546 wchar_t psz_tmp[MAX_PATH];
1547 char * psz_buffer = malloc( MAX_PATH );
1549 char * psz_tmp = malloc( MAX_PATH );
1551 int i = 0, i_error = GetLastError();
1553 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1554 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1555 (LPTSTR)psz_tmp, MAX_PATH, NULL );
1557 /* Go to the end of the string */
1558 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1565 #if defined(UNDER_CE)
1566 swprintf( psz_tmp + i, L" (error %i)", i_error );
1567 psz_tmp[ 255 ] = L'\0';
1569 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1570 psz_tmp[ 255 ] = '\0';
1574 #if defined(UNDER_CE)
1575 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1581 #endif /* HAVE_DL_WINDOWS */
1583 /*****************************************************************************
1584 * LoadPluginsCache: loads the plugins cache file
1585 *****************************************************************************
1586 * This function will load the plugin cache if present and valid. This cache
1587 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1588 * actually load the dynamically loadable module.
1589 * This allows us to only fully load plugins when they are actually used.
1590 *****************************************************************************/
1591 static void CacheLoad( vlc_object_t *p_this )
1593 char *psz_filename, *psz_homedir;
1595 int i, j, i_size, i_read;
1596 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1597 char p_cachelang[6], p_lang[6];
1599 module_cache_t **pp_cache = 0;
1600 int32_t i_file_size, i_marker;
1602 psz_homedir = p_this->p_libvlc->psz_homedir;
1605 msg_Err( p_this, "psz_homedir is null" );
1609 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1610 PLUGINSCACHE_DIR, CacheName() );
1613 msg_Err( p_this, "out of memory" );
1617 if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1619 #if !defined( UNDER_CE )
1620 unlink( psz_filename );
1622 wchar_t psz_wf[MAX_PATH];
1623 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1624 DeleteFile( psz_wf );
1626 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1627 free( psz_filename );
1631 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1633 file = utf8_fopen( psz_filename, "rb" );
1636 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1638 free( psz_filename );
1641 free( psz_filename );
1643 /* Check the file size */
1644 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1645 if( i_read != sizeof(i_file_size) )
1647 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1653 fseek( file, 0, SEEK_END );
1654 if( ftell( file ) != i_file_size )
1656 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1657 "(corrupted size)" );
1661 fseek( file, sizeof(i_file_size), SEEK_SET );
1663 /* Check the file is a plugins cache */
1664 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1665 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1666 if( i_read != i_size ||
1667 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1669 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1674 /* Check Sub-version number */
1675 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1676 if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1678 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1679 "(corrupted header)" );
1684 /* Check the language hasn't changed */
1685 sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1686 i_read = fread( p_cachelang, sizeof(char), i_size, file );
1687 if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1689 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1690 "(language changed)" );
1695 /* Check header marker */
1696 i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1697 if( i_read != sizeof(i_marker) ||
1698 i_marker != ftell( file ) - (int)sizeof(i_marker) )
1700 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1701 "(corrupted header)" );
1706 p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1707 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1709 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1710 malloc( i_cache * sizeof(void *) );
1712 #define LOAD_IMMEDIATE(a) \
1713 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1714 #define LOAD_STRING(a) \
1715 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1716 != sizeof(i_size) ) goto error; \
1717 if( i_size && i_size < 16384 ) { \
1718 a = malloc( i_size ); \
1719 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1721 if( a[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;
1806 int CacheLoadConfig( module_t *p_module, FILE *file )
1811 /* Calculate the structure length */
1812 LOAD_IMMEDIATE( p_module->i_config_items );
1813 LOAD_IMMEDIATE( p_module->i_bool_items );
1815 LOAD_IMMEDIATE( i_lines );
1817 /* Allocate memory */
1818 p_module->p_config =
1819 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1820 if( p_module->p_config == NULL )
1822 msg_Err( p_module, "config error: can't duplicate p_config" );
1826 /* Do the duplication job */
1827 for( i = 0; i < i_lines ; i++ )
1829 LOAD_IMMEDIATE( p_module->p_config[i] );
1831 LOAD_STRING( p_module->p_config[i].psz_type );
1832 LOAD_STRING( p_module->p_config[i].psz_name );
1833 LOAD_STRING( p_module->p_config[i].psz_text );
1834 LOAD_STRING( p_module->p_config[i].psz_longtext );
1835 LOAD_STRING( p_module->p_config[i].psz_current );
1836 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1838 p_module->p_config[i].psz_value =
1839 p_module->p_config[i].psz_value_orig ?
1840 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1841 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1842 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1843 p_module->p_config[i].i_value_saved = p_module->p_config[i].i_value;
1844 p_module->p_config[i].f_value_saved = p_module->p_config[i].f_value;
1845 p_module->p_config[i].psz_value_saved = 0;
1846 p_module->p_config[i].b_dirty = VLC_FALSE;
1848 p_module->p_config[i].p_lock = &p_module->object_lock;
1850 if( p_module->p_config[i].i_list )
1852 if( p_module->p_config[i].ppsz_list )
1854 p_module->p_config[i].ppsz_list =
1855 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1856 if( p_module->p_config[i].ppsz_list )
1858 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1859 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1860 p_module->p_config[i].ppsz_list[j] = NULL;
1863 if( p_module->p_config[i].ppsz_list_text )
1865 p_module->p_config[i].ppsz_list_text =
1866 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1867 if( p_module->p_config[i].ppsz_list_text )
1869 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1870 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1871 p_module->p_config[i].ppsz_list_text[j] = NULL;
1874 if( p_module->p_config[i].pi_list )
1876 p_module->p_config[i].pi_list =
1877 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1878 if( p_module->p_config[i].pi_list )
1880 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1881 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1886 if( p_module->p_config[i].i_action )
1888 p_module->p_config[i].ppf_action =
1889 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1890 p_module->p_config[i].ppsz_action_text =
1891 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1893 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1895 p_module->p_config[i].ppf_action[j] = 0;
1896 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1900 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1903 p_module->p_config[i].i_type = CONFIG_HINT_END;
1909 return VLC_EGENERIC;
1912 /*****************************************************************************
1913 * SavePluginsCache: saves the plugins cache to a file
1914 *****************************************************************************/
1915 static void CacheSave( vlc_object_t *p_this )
1917 static char const psz_tag[] =
1918 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1919 "# This file is a cache directory tag created by VLC.\r\n"
1920 "# For information about cache directory tags, see:\r\n"
1921 "# http://www.brynosaurus.com/cachedir/\r\n";
1923 char *psz_filename, *psz_homedir;
1926 module_cache_t **pp_cache;
1927 int32_t i_file_size = 0;
1929 psz_homedir = p_this->p_libvlc->psz_homedir;
1932 msg_Err( p_this, "psz_homedir is null" );
1936 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1937 strlen(psz_homedir) + strlen(CacheName()) );
1941 msg_Err( p_this, "out of memory" );
1945 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1947 config_CreateDir( p_this, psz_filename );
1949 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1951 config_CreateDir( p_this, psz_filename );
1953 strcat( psz_filename, "/CACHEDIR.TAG" );
1955 file = utf8_fopen( psz_filename, "wb" );
1958 fwrite( psz_tag, 1, strlen(psz_tag), file );
1962 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1963 PLUGINSCACHE_DIR, CacheName() );
1965 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1967 file = utf8_fopen( psz_filename, "wb" );
1970 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1972 free( psz_filename );
1975 free( psz_filename );
1977 /* Empty space for file size */
1978 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1980 /* Contains version number */
1981 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1983 /* Sub-version number (to avoid breakage in the dev version when cache
1984 * structure changes) */
1985 i_file_size = CACHE_SUBVERSION_NUM;
1986 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1989 fprintf( file, "%5.5s", _("C") );
1992 i_file_size = ftell( file );
1993 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1995 i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
1996 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
1998 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2000 #define SAVE_IMMEDIATE(a) \
2001 fwrite( &a, sizeof(char), sizeof(a), file )
2002 #define SAVE_STRING(a) \
2003 { i_size = a ? strlen( a ) + 1 : 0; \
2004 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2005 if( a ) fwrite( a, sizeof(char), i_size, file ); \
2008 for( i = 0; i < i_cache; i++ )
2011 int32_t i_submodule;
2013 /* Save common info */
2014 SAVE_STRING( pp_cache[i]->psz_file );
2015 SAVE_IMMEDIATE( pp_cache[i]->i_time );
2016 SAVE_IMMEDIATE( pp_cache[i]->i_size );
2017 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2019 if( pp_cache[i]->b_junk ) continue;
2021 /* Save additional infos */
2022 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2023 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2024 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2025 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2026 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2028 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2030 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2031 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2032 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2033 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2034 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2035 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2038 CacheSaveConfig( pp_cache[i]->p_module, file );
2040 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2042 i_submodule = pp_cache[i]->p_module->i_children;
2043 SAVE_IMMEDIATE( i_submodule );
2044 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2047 module_t *p_module =
2048 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2050 SAVE_STRING( p_module->psz_object_name );
2051 SAVE_STRING( p_module->psz_shortname );
2052 SAVE_STRING( p_module->psz_longname );
2053 SAVE_STRING( p_module->psz_program );
2054 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2056 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2058 SAVE_STRING( p_module->psz_capability );
2059 SAVE_IMMEDIATE( p_module->i_score );
2060 SAVE_IMMEDIATE( p_module->i_cpu );
2061 SAVE_IMMEDIATE( p_module->b_unloadable );
2062 SAVE_IMMEDIATE( p_module->b_reentrant );
2066 /* Fill-up file size */
2067 i_file_size = ftell( file );
2068 fseek( file, 0, SEEK_SET );
2069 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2076 void CacheSaveConfig( module_t *p_module, FILE *file )
2078 int i, j, i_lines = 0;
2079 module_config_t *p_item;
2082 SAVE_IMMEDIATE( p_module->i_config_items );
2083 SAVE_IMMEDIATE( p_module->i_bool_items );
2085 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2086 p_item++ ) i_lines++;
2088 SAVE_IMMEDIATE( i_lines );
2090 for( i = 0; i < i_lines ; i++ )
2092 SAVE_IMMEDIATE( p_module->p_config[i] );
2094 SAVE_STRING( p_module->p_config[i].psz_type );
2095 SAVE_STRING( p_module->p_config[i].psz_name );
2096 SAVE_STRING( p_module->p_config[i].psz_text );
2097 SAVE_STRING( p_module->p_config[i].psz_longtext );
2098 SAVE_STRING( p_module->p_config[i].psz_current );
2099 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2101 if( p_module->p_config[i].i_list )
2103 if( p_module->p_config[i].ppsz_list )
2105 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2106 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2109 if( p_module->p_config[i].ppsz_list_text )
2111 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2112 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2114 if( p_module->p_config[i].pi_list )
2116 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2117 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2121 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2122 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2124 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2128 /*****************************************************************************
2129 * CacheName: Return the cache file name for this platform.
2130 *****************************************************************************/
2131 static char *CacheName( void )
2133 static char psz_cachename[32];
2135 /* Code int size, pointer size and endianness in the filename */
2136 int32_t x = 0xbe00001e;
2137 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2138 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2139 return psz_cachename;
2142 /*****************************************************************************
2143 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2144 *****************************************************************************/
2145 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2146 module_t *p_module )
2150 p_cache->pf_activate = p_module->pf_activate;
2151 p_cache->pf_deactivate = p_module->pf_deactivate;
2152 #ifndef HAVE_SHARED_LIBVLC
2153 p_cache->p_symbols = p_module->p_symbols;
2155 p_cache->handle = p_module->handle;
2157 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2159 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2160 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2161 p_cchild->pf_activate = p_child->pf_activate;
2162 p_cchild->pf_deactivate = p_child->pf_deactivate;
2163 #ifndef HAVE_SHARED_LIBVLC
2164 p_cchild->p_symbols = p_child->p_symbols;
2168 p_cache->b_loaded = VLC_TRUE;
2169 p_module->b_loaded = VLC_FALSE;
2172 /*****************************************************************************
2173 * FindPluginCache: finds the cache entry corresponding to a file
2174 *****************************************************************************/
2175 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2176 int64_t i_time, int64_t i_size )
2178 module_cache_t **pp_cache;
2181 pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2182 i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2184 for( i = 0; i < i_cache; i++ )
2186 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2187 pp_cache[i]->i_time == i_time &&
2188 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2194 #endif /* HAVE_DYNAMIC_PLUGINS */