1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28 * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strdup() */
38 #include <vlc/input.h>
42 #elif defined( UNDER_CE )
43 # include <windows.h> /* GetFileAttributes() */
45 # include "../extras/dirent.h"
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
51 #ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
58 #define HAVE_DYNAMIC_PLUGINS
59 #if defined(HAVE_DL_DYLD)
60 # if defined(HAVE_MACH_O_DYLD_H)
61 # include <mach-o/dyld.h>
63 #elif defined(HAVE_DL_BEOS)
64 # if defined(HAVE_IMAGE_H)
67 #elif defined(HAVE_DL_WINDOWS)
69 #elif defined(HAVE_DL_DLOPEN)
70 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
73 # if defined(HAVE_SYS_DL_H)
76 #elif defined(HAVE_DL_SHL_LOAD)
77 # if defined(HAVE_DL_H)
81 # undef HAVE_DYNAMIC_PLUGINS
84 #include "vlc_error.h"
86 #include "vlc_interface.h"
87 #include "intf_eject.h"
89 #include "vlc_playlist.h"
91 #include "vlc_video.h"
92 #include "video_output.h"
93 #include "vout_synchro.h"
96 #include "audio_output.h"
97 #include "aout_internal.h"
99 #include "stream_output.h"
101 #include "vlc_httpd.h"
105 #include "iso_lang.h"
108 #include "vlc_block.h"
112 #include "vlc_image.h"
114 #ifdef HAVE_DYNAMIC_PLUGINS
115 # include "modules_plugin.h"
118 #if defined( UNDER_CE )
119 # include "modules_builtin_evc.h"
120 #elif defined( _MSC_VER )
121 # include "modules_builtin_msvc.h"
123 # include "modules_builtin.h"
127 #if defined( WIN32) || defined( UNDER_CE )
128 /* Avoid name collisions */
129 # define LoadModule(a,b,c) _LoadModule(a,b,c)
132 /*****************************************************************************
134 *****************************************************************************/
135 #ifdef HAVE_DYNAMIC_PLUGINS
136 static void AllocateAllPlugins ( vlc_object_t * );
137 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
138 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
139 static module_t * AllocatePlugin( vlc_object_t *, char * );
141 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
142 static int DeleteModule ( module_t * );
143 #ifdef HAVE_DYNAMIC_PLUGINS
144 static void DupModule ( module_t * );
145 static void UndupModule ( module_t * );
146 static int CallEntry ( module_t * );
147 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
148 static void CloseModule ( module_handle_t );
149 static void * GetSymbol ( module_handle_t, const char * );
150 static void CacheLoad ( vlc_object_t * );
151 static int CacheLoadConfig ( module_t *, FILE * );
152 static void CacheSave ( vlc_object_t * );
153 static void CacheSaveConfig ( module_t *, FILE * );
154 static char * CacheName ( void );
155 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
156 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
158 #if defined(HAVE_DL_WINDOWS)
159 static char * GetWindowsError ( void );
163 /*****************************************************************************
164 * module_InitBank: create the module bank.
165 *****************************************************************************
166 * This function creates a module bank structure which will be filled later
167 * on with all the modules found.
168 *****************************************************************************/
169 void __module_InitBank( vlc_object_t *p_this )
171 module_bank_t *p_bank;
174 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
175 var_Get( p_this->p_libvlc, "libvlc", &lockval );
176 vlc_mutex_lock( lockval.p_address );
177 if( p_this->p_libvlc->p_module_bank )
179 p_this->p_libvlc->p_module_bank->i_usage++;
180 vlc_mutex_unlock( lockval.p_address );
181 var_Destroy( p_this->p_libvlc, "libvlc" );
184 vlc_mutex_unlock( lockval.p_address );
185 var_Destroy( p_this->p_libvlc, "libvlc" );
187 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
188 p_bank->psz_object_name = "module bank";
190 p_bank->i_cache = p_bank->i_loaded_cache = 0;
191 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
192 p_bank->b_cache = p_bank->b_cache_dirty =
193 p_bank->b_cache_delete = VLC_FALSE;
196 * Store the symbols to be exported
198 #ifdef HAVE_DYNAMIC_PLUGINS
199 STORE_SYMBOLS( &p_bank->symbols );
202 /* Everything worked, attach the object */
203 p_this->p_libvlc->p_module_bank = p_bank;
204 vlc_object_attach( p_bank, p_this->p_libvlc );
206 module_LoadMain( p_this );
211 /*****************************************************************************
212 * module_ResetBank: reset the module bank.
213 *****************************************************************************
214 * This function resets the module bank by unloading all unused plugin
216 *****************************************************************************/
217 void __module_ResetBank( vlc_object_t *p_this )
219 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
223 /*****************************************************************************
224 * module_EndBank: empty the module bank.
225 *****************************************************************************
226 * This function unloads all unused plugin modules and empties the module
227 * bank in case of success.
228 *****************************************************************************/
229 void __module_EndBank( vlc_object_t *p_this )
234 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
235 var_Get( p_this->p_libvlc, "libvlc", &lockval );
236 vlc_mutex_lock( lockval.p_address );
237 if( !p_this->p_libvlc->p_module_bank )
239 vlc_mutex_unlock( lockval.p_address );
240 var_Destroy( p_this->p_libvlc, "libvlc" );
243 if( --p_this->p_libvlc->p_module_bank->i_usage )
245 vlc_mutex_unlock( lockval.p_address );
246 var_Destroy( p_this->p_libvlc, "libvlc" );
249 vlc_mutex_unlock( lockval.p_address );
250 var_Destroy( p_this->p_libvlc, "libvlc" );
252 #ifdef HAVE_DYNAMIC_PLUGINS
253 #define p_bank p_this->p_libvlc->p_module_bank
254 if( p_bank->b_cache ) CacheSave( p_this );
255 while( p_bank->i_loaded_cache-- )
257 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
258 free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
259 if( !p_bank->i_loaded_cache ) free( p_bank->pp_loaded_cache );
261 while( p_bank->i_cache-- )
263 free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
264 free( p_bank->pp_cache[p_bank->i_cache] );
265 if( !p_bank->i_cache ) free( p_bank->pp_cache );
270 vlc_object_detach( p_this->p_libvlc->p_module_bank );
272 while( p_this->p_libvlc->p_module_bank->i_children )
274 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
276 if( DeleteModule( p_next ) )
278 /* Module deletion failed */
279 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
280 p_next->psz_object_name );
282 /* We just free the module by hand. Niahahahahaha. */
283 vlc_object_detach( p_next );
284 vlc_object_destroy( p_next );
288 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
289 p_this->p_libvlc->p_module_bank = NULL;
294 /*****************************************************************************
295 * module_LoadMain: load the main program info into the module bank.
296 *****************************************************************************
297 * This function fills the module bank structure with the main module infos.
298 * This is very useful as it will allow us to consider the main program just
299 * as another module, and for instance the configuration options of main will
300 * be available in the module bank structure just as for every other module.
301 *****************************************************************************/
302 void __module_LoadMain( vlc_object_t *p_this )
306 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
307 var_Get( p_this->p_libvlc, "libvlc", &lockval );
308 vlc_mutex_lock( lockval.p_address );
309 if( p_this->p_libvlc->p_module_bank->b_main )
311 vlc_mutex_unlock( lockval.p_address );
312 var_Destroy( p_this->p_libvlc, "libvlc" );
315 p_this->p_libvlc->p_module_bank->b_main = VLC_TRUE;
316 vlc_mutex_unlock( lockval.p_address );
317 var_Destroy( p_this->p_libvlc, "libvlc" );
319 AllocateBuiltinModule( p_this, vlc_entry__main );
322 /*****************************************************************************
323 * module_LoadBuiltins: load all modules which we built with.
324 *****************************************************************************
325 * This function fills the module bank structure with the builtin modules.
326 *****************************************************************************/
327 void __module_LoadBuiltins( vlc_object_t * p_this )
331 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
332 var_Get( p_this->p_libvlc, "libvlc", &lockval );
333 vlc_mutex_lock( lockval.p_address );
334 if( p_this->p_libvlc->p_module_bank->b_builtins )
336 vlc_mutex_unlock( lockval.p_address );
337 var_Destroy( p_this->p_libvlc, "libvlc" );
340 p_this->p_libvlc->p_module_bank->b_builtins = VLC_TRUE;
341 vlc_mutex_unlock( lockval.p_address );
342 var_Destroy( p_this->p_libvlc, "libvlc" );
344 msg_Dbg( p_this, "checking builtin modules" );
345 ALLOCATE_ALL_BUILTINS();
348 /*****************************************************************************
349 * module_LoadPlugins: load all plugin modules we can find.
350 *****************************************************************************
351 * This function fills the module bank structure with the plugin modules.
352 *****************************************************************************/
353 void __module_LoadPlugins( vlc_object_t * p_this )
355 #ifdef HAVE_DYNAMIC_PLUGINS
358 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
359 var_Get( p_this->p_libvlc, "libvlc", &lockval );
360 vlc_mutex_lock( lockval.p_address );
361 if( p_this->p_libvlc->p_module_bank->b_plugins )
363 vlc_mutex_unlock( lockval.p_address );
364 var_Destroy( p_this->p_libvlc, "libvlc" );
367 p_this->p_libvlc->p_module_bank->b_plugins = VLC_TRUE;
368 vlc_mutex_unlock( lockval.p_address );
369 var_Destroy( p_this->p_libvlc, "libvlc" );
371 msg_Dbg( p_this, "checking plugin modules" );
373 if( config_GetInt( p_this, "plugins-cache" ) )
374 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
376 if( p_this->p_libvlc->p_module_bank->b_cache ||
377 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
379 AllocateAllPlugins( p_this );
383 /*****************************************************************************
384 * module_Need: return the best module function, given a capability list.
385 *****************************************************************************
386 * This function returns the module that best fits the asked capabilities.
387 *****************************************************************************/
388 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
389 const char *psz_name, vlc_bool_t b_strict )
391 typedef struct module_list_t module_list_t;
398 module_list_t *p_next;
401 module_list_t *p_list, *p_first, *p_tmp;
404 int i_which_module, i_index = 0;
405 vlc_bool_t b_intf = VLC_FALSE;
410 char *psz_shortcuts = NULL, *psz_var = NULL;
411 vlc_bool_t b_force_backup = p_this->b_force;
413 msg_Dbg( p_this, "looking for %s module", psz_capability );
415 /* Deal with variables */
416 if( psz_name && psz_name[0] == '$' )
419 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
420 var_Get( p_this, psz_name + 1, &val );
421 psz_var = val.psz_string;
425 /* Count how many different shortcuts were asked for */
426 if( psz_name && *psz_name )
428 char *psz_parser, *psz_last_shortcut;
430 /* If the user wants none, give him none. */
431 if( !strcmp( psz_name, "none" ) )
433 if( psz_var ) free( psz_var );
438 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
440 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
442 if( *psz_parser == ',' )
446 psz_last_shortcut = psz_parser + 1;
450 /* Check if the user wants to override the "strict" mode */
451 if( psz_last_shortcut )
453 if( !strcmp(psz_last_shortcut, "none") )
458 else if( !strcmp(psz_last_shortcut, "any") )
460 b_strict = VLC_FALSE;
466 /* Sort the modules and test them */
467 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
468 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
471 /* Parse the module list for capabilities and probe each of them */
472 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
474 int i_shortcut_bonus = 0;
476 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
478 /* Test that this module can do what we need */
479 if( strcmp( p_module->psz_capability, psz_capability ) )
481 /* Don't recurse through the sub-modules because vlc_list_find()
482 * will list them anyway. */
486 /* Test if we have the required CPU */
487 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
492 /* If we required a shortcut, check this plugin provides it. */
493 if( i_shortcuts > 0 )
496 int i_dummy, i_short = i_shortcuts;
497 char *psz_name = psz_shortcuts;
499 /* Let's drop modules with a <= 0 score (unless they are
500 * explicitly requested) */
501 b_trash = p_module->i_score <= 0;
505 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
507 if( !strcasecmp( psz_name,
508 p_module->pp_shortcuts[i_dummy] ) )
512 i_shortcut_bonus = i_short * 10000;
517 if( i_shortcut_bonus )
519 /* We found it... remember ? */
523 /* Go to the next shortcut... This is so lame! */
532 /* If we are in "strict" mode and we couldn't
533 * find the module in the list of provided shortcuts,
534 * then kick the bastard out of here!!! */
535 if( i_short == 0 && b_strict )
545 /* If we didn't require a shortcut, trash <= 0 scored plugins */
546 else if( p_module->i_score <= 0 )
551 /* Special case: test if we requested a particular intf plugin */
552 if( !i_shortcuts && p_module->psz_program
553 && !strcmp( psz_capability, "interface" )
554 && !strcmp( p_module->psz_program,
555 p_this->p_vlc->psz_object_name ) )
559 /* Remove previous non-matching plugins */
566 /* This one doesn't match */
570 /* Store this new module */
571 p_list[ i_index ].p_module = p_module;
572 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
573 p_list[ i_index ].b_force = !!i_shortcut_bonus;
575 /* Add it to the modules-to-probe list */
578 p_list[ 0 ].p_next = NULL;
583 /* Ok, so at school you learned that quicksort is quick, and
584 * bubble sort sucks raw eggs. But that's when dealing with
585 * thousands of items. Here we have barely 50. */
586 module_list_t *p_newlist = p_first;
588 if( p_first->i_score < p_list[ i_index ].i_score )
590 p_list[ i_index ].p_next = p_first;
591 p_first = &p_list[ i_index ];
595 while( p_newlist->p_next != NULL &&
596 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
598 p_newlist = p_newlist->p_next;
601 p_list[ i_index ].p_next = p_newlist->p_next;
602 p_newlist->p_next = &p_list[ i_index ];
609 msg_Dbg( p_this, "probing %i candidate%s",
610 i_index, i_index == 1 ? "" : "s" );
612 /* Lock all candidate modules */
614 while( p_tmp != NULL )
616 vlc_object_yield( p_tmp->p_module );
617 p_tmp = p_tmp->p_next;
620 /* We can release the list, interesting modules were yielded */
621 vlc_list_release( p_all );
623 /* Parse the linked list and use the first successful module */
625 while( p_tmp != NULL )
627 #ifdef HAVE_DYNAMIC_PLUGINS
628 /* Make sure the module is loaded in mem */
629 module_t *p_module = p_tmp->p_module->b_submodule ?
630 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
631 if( !p_module->b_builtin && !p_module->b_loaded )
633 module_t *p_new_module =
634 AllocatePlugin( p_this, p_module->psz_filename );
637 CacheMerge( p_this, p_module, p_new_module );
638 vlc_object_attach( p_new_module, p_module );
639 DeleteModule( p_new_module );
644 p_this->b_force = p_tmp->b_force;
645 if( p_tmp->p_module->pf_activate
646 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
651 vlc_object_release( p_tmp->p_module );
652 p_tmp = p_tmp->p_next;
655 /* Store the locked module value */
658 p_module = p_tmp->p_module;
659 p_tmp = p_tmp->p_next;
666 /* Unlock the remaining modules */
667 while( p_tmp != NULL )
669 vlc_object_release( p_tmp->p_module );
670 p_tmp = p_tmp->p_next;
674 p_this->b_force = b_force_backup;
676 if( p_module != NULL )
678 msg_Dbg( p_module, "using %s module \"%s\"",
679 psz_capability, p_module->psz_object_name );
681 else if( p_first == NULL )
683 if( !strcmp( psz_capability, "access_demux" ) )
685 msg_Warn( p_this, "no %s module matched \"%s\"",
686 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
690 msg_Err( p_this, "no %s module matched \"%s\"",
691 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
694 else if( psz_name != NULL && *psz_name )
696 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
697 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
702 free( psz_shortcuts );
710 /* Don't forget that the module is still locked */
714 /*****************************************************************************
715 * module_Unneed: decrease the usage count of a module.
716 *****************************************************************************
717 * This function must be called by the thread that called module_Need, to
718 * decrease the reference count and allow for hiding of modules.
719 *****************************************************************************/
720 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
722 /* Use the close method */
723 if( p_module->pf_deactivate )
725 p_module->pf_deactivate( p_this );
728 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
730 vlc_object_release( p_module );
735 /*****************************************************************************
736 * Following functions are local.
737 *****************************************************************************/
739 /*****************************************************************************
740 * AllocateAllPlugins: load all plugin modules we can find.
741 *****************************************************************************/
742 #ifdef HAVE_DYNAMIC_PLUGINS
743 static void AllocateAllPlugins( vlc_object_t *p_this )
745 /* Yes, there are two NULLs because we replace one with "plugin-path". */
746 #if defined( WIN32 ) || defined( UNDER_CE )
747 char *path[] = { "modules", "", "plugins", 0, 0 };
749 char *path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
752 char **ppsz_path = path;
755 /* If the user provided a plugin path, we add it to the list */
756 path[ sizeof(path)/sizeof(char*) - 2 ] =
757 config_GetPsz( p_this, "plugin-path" );
759 for( ; *ppsz_path != NULL ; ppsz_path++ )
761 if( !(*ppsz_path)[0] ) continue;
763 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
765 /* Handle relative as well as absolute paths */
767 if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
768 (*ppsz_path)[1] != ':' )
770 if( (*ppsz_path)[0] != '/' )
773 int i_dirlen = strlen( *ppsz_path );
774 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
776 psz_fullpath = malloc( i_dirlen );
777 if( psz_fullpath == NULL )
782 sprintf( psz_fullpath, "%s\\%s",
783 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
785 sprintf( psz_fullpath, "%s/%s",
786 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
792 psz_fullpath = strdup( *ppsz_path );
795 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
797 /* Don't go deeper than 5 subdirectories */
798 AllocatePluginDir( p_this, psz_fullpath, 5 );
800 free( psz_fullpath );
803 /* Free plugin-path */
804 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
805 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
806 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
809 /*****************************************************************************
810 * AllocatePluginDir: recursively parse a directory to look for plugins
811 *****************************************************************************/
812 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
815 #if defined( UNDER_CE ) || defined( _MSC_VER )
817 wchar_t psz_wpath[MAX_PATH + 256];
818 wchar_t psz_wdir[MAX_PATH];
820 char psz_path[MAX_PATH + 256];
821 WIN32_FIND_DATA finddata;
827 struct dirent * file;
831 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
836 #if defined( UNDER_CE ) || defined( _MSC_VER )
838 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
840 rc = GetFileAttributes( psz_wdir );
841 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
843 /* Parse all files in the directory */
844 swprintf( psz_wpath, L"%ls\\*", psz_wdir );
846 rc = GetFileAttributes( psz_dir );
847 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
850 /* Parse all files in the directory */
851 sprintf( psz_path, "%s\\*", psz_dir );
854 handle = FindFirstFile( psz_wpath, &finddata );
856 handle = FindFirstFile( psz_path, &finddata );
858 if( handle == INVALID_HANDLE_VALUE )
860 /* Empty directory */
864 /* Parse the directory and try to load all files it contains. */
868 unsigned int i_len = wcslen( finddata.cFileName );
869 swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
870 sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
872 unsigned int i_len = strlen( finddata.cFileName );
873 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
876 /* Skip ".", ".." and anything starting with "." */
877 if( !*finddata.cFileName || *finddata.cFileName == '.' )
879 if( !FindNextFile( handle, &finddata ) ) break;
884 if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
886 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
889 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
891 else if( i_len > strlen( LIBEXT )
892 /* We only load files ending with LIBEXT */
893 && !strncasecmp( psz_path + strlen( psz_path)
895 LIBEXT, strlen( LIBEXT ) ) )
897 WIN32_FILE_ATTRIBUTE_DATA attrbuf;
898 int64_t i_time = 0, i_size = 0;
901 if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
904 if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
908 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
910 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
911 i_size = attrbuf.nFileSizeHigh;
913 i_size |= attrbuf.nFileSizeLow;
917 AllocatePluginFile( p_this, psz_file, i_time, i_size );
920 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
922 /* Close the directory */
926 dir = opendir( psz_dir );
932 i_dirlen = strlen( psz_dir );
934 /* Parse the directory and try to load all files it contains. */
935 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
941 /* Skip ".", ".." and anything starting with "." */
942 if( !*file->d_name || *file->d_name == '.' )
947 i_len = strlen( file->d_name );
948 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
950 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
952 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
955 i_stat = stat( psz_file, &statbuf );
956 if( !i_stat && statbuf.st_mode & S_IFDIR )
958 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
960 else if( i_len > strlen( LIBEXT )
961 /* We only load files ending with LIBEXT */
962 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
963 LIBEXT, strlen( LIBEXT ) ) )
965 int64_t i_time = 0, i_size = 0;
969 i_time = statbuf.st_mtime;
970 i_size = statbuf.st_size;
973 AllocatePluginFile( p_this, psz_file, i_time, i_size );
979 /* Close the directory */
985 /*****************************************************************************
986 * AllocatePluginFile: load a module into memory and initialize it.
987 *****************************************************************************
988 * This function loads a dynamically loadable module and allocates a structure
989 * for its information data. The module can then be handled by module_Need
990 * and module_Unneed. It can be removed by DeleteModule.
991 *****************************************************************************/
992 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
993 int64_t i_file_time, int64_t i_file_size )
996 module_cache_t *p_cache_entry = NULL;
999 * Check our plugins cache first then load plugin if needed
1002 CacheFind( p_this, psz_file, i_file_time, i_file_size );
1004 if( !p_cache_entry )
1006 p_module = AllocatePlugin( p_this, psz_file );
1010 /* If junk dll, don't try to load it */
1011 if( p_cache_entry->b_junk )
1017 module_config_t *p_item;
1019 p_module = p_cache_entry->p_module;
1020 p_module->b_loaded = VLC_FALSE;
1022 /* For now we force loading if the module's config contains
1023 * callbacks or actions.
1024 * Could be optimized by adding an API call.*/
1025 for( p_item = p_module->p_config;
1026 p_item->i_type != CONFIG_HINT_END; p_item++ )
1028 if( p_item->pf_callback || p_item->i_action )
1029 p_module = AllocatePlugin( p_this, psz_file );
1036 /* Everything worked fine !
1037 * The module is ready to be added to the list. */
1038 p_module->b_builtin = VLC_FALSE;
1040 /* msg_Dbg( p_this, "plugin \"%s\", %s",
1041 p_module->psz_object_name, p_module->psz_longname ); */
1043 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1046 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
1048 /* Add entry to cache */
1049 #define p_bank p_this->p_libvlc->p_module_bank
1051 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1052 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1053 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1054 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1055 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1056 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1057 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1060 return p_module ? 0 : -1;
1063 /*****************************************************************************
1064 * AllocatePlugin: load a module into memory and initialize it.
1065 *****************************************************************************
1066 * This function loads a dynamically loadable module and allocates a structure
1067 * for its information data. The module can then be handled by module_Need
1068 * and module_Unneed. It can be removed by DeleteModule.
1069 *****************************************************************************/
1070 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1072 module_t * p_module;
1073 module_handle_t handle;
1075 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
1077 /* Now that we have successfully loaded the module, we can
1078 * allocate a structure for it */
1079 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1080 if( p_module == NULL )
1082 msg_Err( p_this, "out of memory" );
1083 CloseModule( handle );
1087 /* We need to fill these since they may be needed by CallEntry() */
1088 p_module->psz_filename = psz_file;
1089 p_module->handle = handle;
1090 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1091 p_module->b_loaded = VLC_TRUE;
1093 /* Initialize the module: fill p_module, default config */
1094 if( CallEntry( p_module ) != 0 )
1096 /* We couldn't call module_init() */
1097 vlc_object_destroy( p_module );
1098 CloseModule( handle );
1102 DupModule( p_module );
1103 p_module->psz_filename = strdup( p_module->psz_filename );
1105 /* Everything worked fine ! The module is ready to be added to the list. */
1106 p_module->b_builtin = VLC_FALSE;
1111 /*****************************************************************************
1112 * DupModule: make a plugin module standalone.
1113 *****************************************************************************
1114 * This function duplicates all strings in the module, so that the dynamic
1115 * object can be unloaded. It acts recursively on submodules.
1116 *****************************************************************************/
1117 static void DupModule( module_t *p_module )
1122 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1124 *pp_shortcut = strdup( *pp_shortcut );
1127 /* We strdup() these entries so that they are still valid when the
1128 * module is unloaded. */
1129 p_module->psz_object_name = strdup( p_module->psz_object_name );
1130 p_module->psz_capability = strdup( p_module->psz_capability );
1131 p_module->psz_shortname = strdup( p_module->psz_shortname );
1132 p_module->psz_longname = strdup( p_module->psz_longname );
1134 if( p_module->psz_program != NULL )
1136 p_module->psz_program = strdup( p_module->psz_program );
1139 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1141 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1145 /*****************************************************************************
1146 * UndupModule: free a duplicated module.
1147 *****************************************************************************
1148 * This function frees the allocations done in DupModule().
1149 *****************************************************************************/
1150 static void UndupModule( module_t *p_module )
1155 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1157 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1160 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1162 free( *pp_shortcut );
1165 free( p_module->psz_object_name );
1166 free( p_module->psz_capability );
1167 free( p_module->psz_shortname );
1168 free( p_module->psz_longname );
1170 if( p_module->psz_program != NULL )
1172 free( p_module->psz_program );
1176 #endif /* HAVE_DYNAMIC_PLUGINS */
1178 /*****************************************************************************
1179 * AllocateBuiltinModule: initialize a builtin module.
1180 *****************************************************************************
1181 * This function registers a builtin module and allocates a structure
1182 * for its information data. The module can then be handled by module_Need
1183 * and module_Unneed. It can be removed by DeleteModule.
1184 *****************************************************************************/
1185 static int AllocateBuiltinModule( vlc_object_t * p_this,
1186 int ( *pf_entry ) ( module_t * ) )
1188 module_t * p_module;
1190 /* Now that we have successfully loaded the module, we can
1191 * allocate a structure for it */
1192 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1193 if( p_module == NULL )
1195 msg_Err( p_this, "out of memory" );
1199 /* Initialize the module : fill p_module->psz_object_name, etc. */
1200 if( pf_entry( p_module ) != 0 )
1202 /* With a well-written module we shouldn't have to print an
1203 * additional error message here, but just make sure. */
1204 msg_Err( p_this, "failed calling entry point in builtin module" );
1205 vlc_object_destroy( p_module );
1209 /* Everything worked fine ! The module is ready to be added to the list. */
1210 p_module->b_builtin = VLC_TRUE;
1212 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1213 p_module->psz_object_name, p_module->psz_longname ); */
1215 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1220 /*****************************************************************************
1221 * DeleteModule: delete a module and its structure.
1222 *****************************************************************************
1223 * This function can only be called if the module isn't being used.
1224 *****************************************************************************/
1225 static int DeleteModule( module_t * p_module )
1227 vlc_object_detach( p_module );
1229 /* We free the structures that we strdup()ed in Allocate*Module(). */
1230 #ifdef HAVE_DYNAMIC_PLUGINS
1231 if( !p_module->b_builtin )
1233 if( p_module->b_loaded && p_module->b_unloadable )
1235 CloseModule( p_module->handle );
1237 UndupModule( p_module );
1238 free( p_module->psz_filename );
1242 /* Free and detach the object's children */
1243 while( p_module->i_children )
1245 vlc_object_t *p_this = p_module->pp_children[0];
1246 vlc_object_detach( p_this );
1247 vlc_object_destroy( p_this );
1250 config_Free( p_module );
1251 vlc_object_destroy( p_module );
1256 #ifdef HAVE_DYNAMIC_PLUGINS
1257 /*****************************************************************************
1258 * CallEntry: call an entry point.
1259 *****************************************************************************
1260 * This function calls a symbol given its name and a module structure. The
1261 * symbol MUST refer to a function returning int and taking a module_t* as
1263 *****************************************************************************/
1264 static int CallEntry( module_t * p_module )
1266 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1267 int (* pf_symbol) ( module_t * p_module );
1269 /* Try to resolve the symbol */
1270 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1272 if( pf_symbol == NULL )
1274 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1275 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1276 psz_name, p_module->psz_filename );
1277 #elif defined(HAVE_DL_WINDOWS)
1278 char *psz_error = GetWindowsError();
1279 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1280 psz_name, p_module->psz_filename, psz_error );
1282 #elif defined(HAVE_DL_DLOPEN)
1283 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1284 psz_name, p_module->psz_filename, dlerror() );
1285 #elif defined(HAVE_DL_SHL_LOAD)
1286 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1287 psz_name, p_module->psz_filename, strerror(errno) );
1289 # error "Something is wrong in modules.c"
1294 /* We can now try to call the symbol */
1295 if( pf_symbol( p_module ) != 0 )
1297 /* With a well-written module we shouldn't have to print an
1298 * additional error message here, but just make sure. */
1299 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1300 psz_name, p_module->psz_filename );
1304 /* Everything worked fine, we can return */
1308 /*****************************************************************************
1309 * LoadModule: loads a dynamic library
1310 *****************************************************************************
1311 * This function loads a dynamically linked library using a system dependant
1312 * method. Will return 0 on success as well as the module handle.
1313 *****************************************************************************/
1314 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1315 module_handle_t *p_handle )
1317 module_handle_t handle;
1319 #if defined(HAVE_DL_DYLD)
1320 NSObjectFileImage image;
1321 NSObjectFileImageReturnCode ret;
1323 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1325 if( ret != NSObjectFileImageSuccess )
1327 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1331 /* Open the dynamic module */
1332 handle = NSLinkModule( image, psz_file,
1333 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1337 NSLinkEditErrors errors;
1338 const char *psz_file, *psz_err;
1340 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1341 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1342 NSDestroyObjectFileImage( image );
1346 /* Destroy our image, we won't need it */
1347 NSDestroyObjectFileImage( image );
1349 #elif defined(HAVE_DL_BEOS)
1350 handle = load_add_on( psz_file );
1353 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1357 #elif defined(HAVE_DL_WINDOWS)
1360 wchar_t psz_wfile[MAX_PATH];
1361 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1362 handle = LoadLibrary( psz_wfile );
1365 handle = LoadLibrary( psz_file );
1367 if( handle == NULL )
1369 char *psz_err = GetWindowsError();
1370 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1375 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1376 /* static is OK, we are called atomically */
1377 static vlc_bool_t b_kde = VLC_FALSE;
1379 # if defined(SYS_LINUX)
1380 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1381 * are going to get namespace collisions when two modules have common
1382 * public symbols, but ALSA is being a pest here. */
1383 if( strstr( psz_file, "alsa_plugin" ) )
1385 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1386 if( handle == NULL )
1388 msg_Warn( p_this, "cannot load module `%s' (%s)",
1389 psz_file, dlerror() );
1394 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1395 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1396 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1397 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1398 if( !b_kde && !strstr( psz_file, "kde" ) )
1400 dlopen( "libstdc++.so.6", RTLD_NOW )
1401 || dlopen( "libstdc++.so.5", RTLD_NOW )
1402 || dlopen( "libstdc++.so.4", RTLD_NOW )
1403 || dlopen( "libstdc++.so.3", RTLD_NOW );
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[256];
1547 char * psz_buffer = malloc( 256 );
1549 char * psz_tmp = malloc( 256 );
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, 256, NULL );
1557 /* Go to the end of the string */
1558 #if defined(UNDER_CE)
1559 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1561 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1569 #if defined(UNDER_CE)
1570 swprintf( psz_tmp + i, L" (error %i)", i_error );
1571 psz_tmp[ 255 ] = L'\0';
1573 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1574 psz_tmp[ 255 ] = '\0';
1578 #if defined(UNDER_CE)
1579 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1580 psz_buffer, 256, NULL, NULL );
1586 #endif /* HAVE_DL_WINDOWS */
1588 /*****************************************************************************
1589 * LoadPluginsCache: loads the plugins cache file
1590 *****************************************************************************
1591 * This function will load the plugin cache if present and valid. This cache
1592 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1593 * actually load the dynamically loadable module.
1594 * This allows us to only fully load plugins when they are actually used.
1595 *****************************************************************************/
1596 static void CacheLoad( vlc_object_t *p_this )
1598 char *psz_filename, *psz_homedir;
1600 int i, j, i_size, i_read;
1601 char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1602 char p_cachelang[6], p_lang[6];
1604 module_cache_t **pp_cache = 0;
1605 int32_t i_file_size, i_marker;
1607 psz_homedir = p_this->p_vlc->psz_homedir;
1610 msg_Err( p_this, "psz_homedir is null" );
1614 i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1615 PLUGINSCACHE_DIR, CacheName() );
1618 msg_Err( p_this, "out of memory" );
1622 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1624 #if !defined( UNDER_CE )
1625 unlink( psz_filename );
1627 wchar_t psz_wf[MAX_PATH];
1628 MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1629 DeleteFile( psz_wf );
1631 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1635 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1637 file = fopen( psz_filename, "rb" );
1640 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1642 free( psz_filename );
1645 free( psz_filename );
1647 /* Check the file size */
1648 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1649 if( i_read != sizeof(i_file_size) )
1651 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1657 fseek( file, 0, SEEK_END );
1658 if( ftell( file ) != i_file_size )
1660 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1661 "(corrupted size)" );
1665 fseek( file, sizeof(i_file_size), SEEK_SET );
1667 /* Check the file is a plugins cache */
1668 i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1669 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1670 if( i_read != i_size ||
1671 memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1673 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
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->p_module_bank->i_loaded_cache = 0;
1701 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1702 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1703 malloc( i_cache * sizeof(void *) );
1705 #define LOAD_IMMEDIATE(a) \
1706 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1707 #define LOAD_STRING(a) \
1708 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1709 != sizeof(i_size) ) goto error; \
1711 a = malloc( i_size ); \
1712 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1718 for( i = 0; i < i_cache; i++ )
1723 pp_cache[i] = malloc( sizeof(module_cache_t) );
1724 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1726 /* Load common info */
1727 LOAD_STRING( pp_cache[i]->psz_file );
1728 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1729 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1730 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1732 if( pp_cache[i]->b_junk ) continue;
1734 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1736 /* Load additional infos */
1737 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1738 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1739 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1740 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1741 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1743 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1745 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1746 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1747 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1748 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1749 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1750 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1753 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1756 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1758 LOAD_IMMEDIATE( i_submodules );
1760 while( i_submodules-- )
1762 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1763 vlc_object_attach( p_module, pp_cache[i]->p_module );
1764 p_module->b_submodule = VLC_TRUE;
1766 LOAD_STRING( p_module->psz_object_name );
1767 LOAD_STRING( p_module->psz_shortname );
1768 LOAD_STRING( p_module->psz_longname );
1769 LOAD_STRING( p_module->psz_program );
1770 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1772 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1774 LOAD_STRING( p_module->psz_capability );
1775 LOAD_IMMEDIATE( p_module->i_score );
1776 LOAD_IMMEDIATE( p_module->i_cpu );
1777 LOAD_IMMEDIATE( p_module->b_unloadable );
1778 LOAD_IMMEDIATE( p_module->b_reentrant );
1787 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1790 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1796 int CacheLoadConfig( module_t *p_module, FILE *file )
1801 /* Calculate the structure length */
1802 LOAD_IMMEDIATE( p_module->i_config_items );
1803 LOAD_IMMEDIATE( p_module->i_bool_items );
1805 LOAD_IMMEDIATE( i_lines );
1807 /* Allocate memory */
1808 p_module->p_config =
1809 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1810 if( p_module->p_config == NULL )
1812 msg_Err( p_module, "config error: can't duplicate p_config" );
1816 /* Do the duplication job */
1817 for( i = 0; i < i_lines ; i++ )
1819 LOAD_IMMEDIATE( p_module->p_config[i] );
1821 LOAD_STRING( p_module->p_config[i].psz_type );
1822 LOAD_STRING( p_module->p_config[i].psz_name );
1823 LOAD_STRING( p_module->p_config[i].psz_text );
1824 LOAD_STRING( p_module->p_config[i].psz_longtext );
1825 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1827 p_module->p_config[i].psz_value =
1828 p_module->p_config[i].psz_value_orig ?
1829 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1830 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1831 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1833 p_module->p_config[i].p_lock = &p_module->object_lock;
1835 if( p_module->p_config[i].i_list )
1837 if( p_module->p_config[i].ppsz_list )
1839 p_module->p_config[i].ppsz_list =
1840 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1841 if( p_module->p_config[i].ppsz_list )
1843 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1844 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1845 p_module->p_config[i].ppsz_list[j] = NULL;
1848 if( p_module->p_config[i].ppsz_list_text )
1850 p_module->p_config[i].ppsz_list_text =
1851 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1852 if( p_module->p_config[i].ppsz_list_text )
1854 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1855 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1856 p_module->p_config[i].ppsz_list_text[j] = NULL;
1859 if( p_module->p_config[i].pi_list )
1861 p_module->p_config[i].pi_list =
1862 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1863 if( p_module->p_config[i].pi_list )
1865 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1866 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1871 if( p_module->p_config[i].i_action )
1873 p_module->p_config[i].ppf_action =
1874 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1875 p_module->p_config[i].ppsz_action_text =
1876 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1878 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1880 p_module->p_config[i].ppf_action[j] = 0;
1881 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1885 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1888 p_module->p_config[i].i_type = CONFIG_HINT_END;
1894 return VLC_EGENERIC;
1897 /*****************************************************************************
1898 * SavePluginsCache: saves the plugins cache to a file
1899 *****************************************************************************/
1900 static void CacheSave( vlc_object_t *p_this )
1902 static char const psz_tag[] =
1903 "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1904 "# This file is a cache directory tag created by VLC.\r\n"
1905 "# For information about cache directory tags, see:\r\n"
1906 "# http://www.brynosaurus.com/cachedir/\r\n";
1908 char *psz_filename, *psz_homedir;
1911 module_cache_t **pp_cache;
1912 int32_t i_file_size = 0;
1914 psz_homedir = p_this->p_vlc->psz_homedir;
1917 msg_Err( p_this, "psz_homedir is null" );
1921 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1922 strlen(psz_homedir) + strlen(CacheName()) );
1926 msg_Err( p_this, "out of memory" );
1930 sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
1932 config_CreateDir( p_this, psz_filename );
1934 strcat( psz_filename, "/" PLUGINSCACHE_DIR );
1936 config_CreateDir( p_this, psz_filename );
1938 strcat( psz_filename, "/CACHEDIR.TAG" );
1940 file = fopen( psz_filename, "wb" );
1943 fwrite( psz_tag, 1, strlen(psz_tag), file );
1947 sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1948 PLUGINSCACHE_DIR, CacheName() );
1950 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1952 file = fopen( psz_filename, "wb" );
1955 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1957 free( psz_filename );
1960 free( psz_filename );
1962 /* Empty space for file size */
1963 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1965 /* Contains version number */
1966 fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
1969 fprintf( file, "%5.5s", _("C") );
1972 i_file_size = ftell( file );
1973 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1975 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1976 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1978 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1980 #define SAVE_IMMEDIATE(a) \
1981 fwrite( &a, sizeof(char), sizeof(a), file )
1982 #define SAVE_STRING(a) \
1983 { i_size = a ? strlen( a ) + 1 : 0; \
1984 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1985 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1988 for( i = 0; i < i_cache; i++ )
1991 int32_t i_submodule;
1993 /* Save common info */
1994 SAVE_STRING( pp_cache[i]->psz_file );
1995 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1996 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1997 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1999 if( pp_cache[i]->b_junk ) continue;
2001 /* Save additional infos */
2002 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2003 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2004 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2005 SAVE_STRING( pp_cache[i]->p_module->psz_program );
2006 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2008 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2010 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2011 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2012 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2013 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2014 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2015 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2018 CacheSaveConfig( pp_cache[i]->p_module, file );
2020 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2022 i_submodule = pp_cache[i]->p_module->i_children;
2023 SAVE_IMMEDIATE( i_submodule );
2024 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
2027 module_t *p_module =
2028 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2030 SAVE_STRING( p_module->psz_object_name );
2031 SAVE_STRING( p_module->psz_shortname );
2032 SAVE_STRING( p_module->psz_longname );
2033 SAVE_STRING( p_module->psz_program );
2034 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2036 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
2038 SAVE_STRING( p_module->psz_capability );
2039 SAVE_IMMEDIATE( p_module->i_score );
2040 SAVE_IMMEDIATE( p_module->i_cpu );
2041 SAVE_IMMEDIATE( p_module->b_unloadable );
2042 SAVE_IMMEDIATE( p_module->b_reentrant );
2046 /* Fill-up file size */
2047 i_file_size = ftell( file );
2048 fseek( file, 0, SEEK_SET );
2049 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2056 void CacheSaveConfig( module_t *p_module, FILE *file )
2058 int i, j, i_lines = 0;
2059 module_config_t *p_item;
2062 SAVE_IMMEDIATE( p_module->i_config_items );
2063 SAVE_IMMEDIATE( p_module->i_bool_items );
2065 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
2066 p_item++ ) i_lines++;
2068 SAVE_IMMEDIATE( i_lines );
2070 for( i = 0; i < i_lines ; i++ )
2072 SAVE_IMMEDIATE( p_module->p_config[i] );
2074 SAVE_STRING( p_module->p_config[i].psz_type );
2075 SAVE_STRING( p_module->p_config[i].psz_name );
2076 SAVE_STRING( p_module->p_config[i].psz_text );
2077 SAVE_STRING( p_module->p_config[i].psz_longtext );
2078 SAVE_STRING( p_module->p_config[i].psz_value_orig );
2080 if( p_module->p_config[i].i_list )
2082 if( p_module->p_config[i].ppsz_list )
2084 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2085 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
2088 if( p_module->p_config[i].ppsz_list_text )
2090 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2091 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
2093 if( p_module->p_config[i].pi_list )
2095 for( j = 0; j < p_module->p_config[i].i_list; j++ )
2096 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
2100 for( j = 0; j < p_module->p_config[i].i_action; j++ )
2101 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
2103 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
2107 /*****************************************************************************
2108 * CacheName: Return the cache file name for this platform.
2109 *****************************************************************************/
2110 static char *CacheName( void )
2112 static char psz_cachename[32];
2113 static vlc_bool_t b_initialised = VLC_FALSE;
2115 if( !b_initialised )
2117 /* Code int size, pointer size and endianness in the filename */
2118 int32_t x = 0xbe00001e;
2119 sprintf( psz_cachename, "plugins-%.2x%.2x%.2x.dat", sizeof(int),
2120 sizeof(void *), (unsigned int)((unsigned char *)&x)[0] );
2121 b_initialised = VLC_TRUE;
2124 return psz_cachename;
2127 /*****************************************************************************
2128 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
2129 *****************************************************************************/
2130 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
2131 module_t *p_module )
2135 p_cache->pf_activate = p_module->pf_activate;
2136 p_cache->pf_deactivate = p_module->pf_deactivate;
2137 p_cache->p_symbols = p_module->p_symbols;
2138 p_cache->handle = p_module->handle;
2140 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2142 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2143 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2144 p_cchild->pf_activate = p_child->pf_activate;
2145 p_cchild->pf_deactivate = p_child->pf_deactivate;
2146 p_cchild->p_symbols = p_child->p_symbols;
2149 p_cache->b_loaded = VLC_TRUE;
2150 p_module->b_loaded = VLC_FALSE;
2153 /*****************************************************************************
2154 * FindPluginCache: finds the cache entry corresponding to a file
2155 *****************************************************************************/
2156 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2157 int64_t i_time, int64_t i_size )
2159 module_cache_t **pp_cache;
2162 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2163 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2165 for( i = 0; i < i_cache; i++ )
2167 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2168 pp_cache[i]->i_time == i_time &&
2169 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2175 #endif /* HAVE_DYNAMIC_PLUGINS */