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() */
41 #elif defined( UNDER_CE )
42 # include <windows.h> /* GetFileAttributes() */
44 # include "../extras/dirent.h"
47 #ifdef HAVE_SYS_TYPES_H
48 # include <sys/types.h>
50 #ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h>
57 #define HAVE_DYNAMIC_PLUGINS
58 #if defined(HAVE_DL_DYLD)
59 # if defined(HAVE_MACH_O_DYLD_H)
60 # include <mach-o/dyld.h>
62 #elif defined(HAVE_DL_BEOS)
63 # if defined(HAVE_IMAGE_H)
66 #elif defined(HAVE_DL_WINDOWS)
68 #elif defined(HAVE_DL_DLOPEN)
69 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
72 # if defined(HAVE_SYS_DL_H)
75 #elif defined(HAVE_DL_SHL_LOAD)
76 # if defined(HAVE_DL_H)
80 # undef HAVE_DYNAMIC_PLUGINS
83 #include "vlc_error.h"
85 #include "vlc_interface.h"
86 #include "intf_eject.h"
88 #include "stream_control.h"
89 #include "input_ext-intf.h"
90 #include "input_ext-dec.h"
91 #include "input_ext-plugins.h"
94 #include "vlc_playlist.h"
96 #include "vlc_video.h"
97 #include "video_output.h"
98 #include "vout_synchro.h"
100 #include "audio_output.h"
101 #include "aout_internal.h"
103 #include "stream_output.h"
105 #include "vlc_httpd.h"
107 #include "iso_lang.h"
110 #include "vlc_block.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 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 );
162 /*****************************************************************************
163 * module_InitBank: create the module bank.
164 *****************************************************************************
165 * This function creates a module bank structure which will be filled later
166 * on with all the modules found.
167 *****************************************************************************/
168 void __module_InitBank( vlc_object_t *p_this )
170 module_bank_t *p_bank;
173 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
174 var_Get( p_this->p_libvlc, "libvlc", &lockval );
175 vlc_mutex_lock( lockval.p_address );
176 if( p_this->p_libvlc->p_module_bank )
178 p_this->p_libvlc->p_module_bank->i_usage++;
179 vlc_mutex_unlock( lockval.p_address );
180 var_Destroy( p_this->p_libvlc, "libvlc" );
183 vlc_mutex_unlock( lockval.p_address );
184 var_Destroy( p_this->p_libvlc, "libvlc" );
186 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
187 p_bank->psz_object_name = "module bank";
189 p_bank->i_cache = p_bank->i_loaded_cache = 0;
190 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
191 p_bank->b_cache = p_bank->b_cache_dirty =
192 p_bank->b_cache_delete = VLC_FALSE;
195 * Store the symbols to be exported
197 #ifdef HAVE_DYNAMIC_PLUGINS
198 STORE_SYMBOLS( &p_bank->symbols );
201 /* Everything worked, attach the object */
202 p_this->p_libvlc->p_module_bank = p_bank;
203 vlc_object_attach( p_bank, p_this->p_libvlc );
205 module_LoadMain( p_this );
210 /*****************************************************************************
211 * module_ResetBank: reset the module bank.
212 *****************************************************************************
213 * This function resets the module bank by unloading all unused plugin
215 *****************************************************************************/
216 void __module_ResetBank( vlc_object_t *p_this )
218 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
222 /*****************************************************************************
223 * module_EndBank: empty the module bank.
224 *****************************************************************************
225 * This function unloads all unused plugin modules and empties the module
226 * bank in case of success.
227 *****************************************************************************/
228 void __module_EndBank( vlc_object_t *p_this )
233 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
234 var_Get( p_this->p_libvlc, "libvlc", &lockval );
235 vlc_mutex_lock( lockval.p_address );
236 if( !p_this->p_libvlc->p_module_bank )
238 vlc_mutex_unlock( lockval.p_address );
239 var_Destroy( p_this->p_libvlc, "libvlc" );
242 if( --p_this->p_libvlc->p_module_bank->i_usage )
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc, "libvlc" );
248 vlc_mutex_unlock( lockval.p_address );
249 var_Destroy( p_this->p_libvlc, "libvlc" );
251 #ifdef HAVE_DYNAMIC_PLUGINS
252 if( p_this->p_libvlc->p_module_bank->b_cache ) CacheSave( p_this );
255 vlc_object_detach( p_this->p_libvlc->p_module_bank );
257 while( p_this->p_libvlc->p_module_bank->i_children )
259 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
261 if( DeleteModule( p_next ) )
263 /* Module deletion failed */
264 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
265 p_next->psz_object_name );
267 /* We just free the module by hand. Niahahahahaha. */
268 vlc_object_detach( p_next );
269 vlc_object_destroy( p_next );
273 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
278 /*****************************************************************************
279 * module_LoadMain: load the main program info into the module bank.
280 *****************************************************************************
281 * This function fills the module bank structure with the main module infos.
282 * This is very useful as it will allow us to consider the main program just
283 * as another module, and for instance the configuration options of main will
284 * be available in the module bank structure just as for every other module.
285 *****************************************************************************/
286 void __module_LoadMain( vlc_object_t *p_this )
288 AllocateBuiltinModule( p_this, vlc_entry__main );
291 /*****************************************************************************
292 * module_LoadBuiltins: load all modules which we built with.
293 *****************************************************************************
294 * This function fills the module bank structure with the builtin modules.
295 *****************************************************************************/
296 void __module_LoadBuiltins( vlc_object_t * p_this )
298 msg_Dbg( p_this, "checking builtin modules" );
299 ALLOCATE_ALL_BUILTINS();
302 /*****************************************************************************
303 * module_LoadPlugins: load all plugin modules we can find.
304 *****************************************************************************
305 * This function fills the module bank structure with the plugin modules.
306 *****************************************************************************/
307 void __module_LoadPlugins( vlc_object_t * p_this )
309 #ifdef HAVE_DYNAMIC_PLUGINS
310 msg_Dbg( p_this, "checking plugin modules" );
312 if( config_GetInt( p_this, "plugins-cache" ) )
313 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
315 if( p_this->p_libvlc->p_module_bank->b_cache ||
316 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
318 AllocateAllPlugins( p_this );
322 /*****************************************************************************
323 * module_Need: return the best module function, given a capability list.
324 *****************************************************************************
325 * This function returns the module that best fits the asked capabilities.
326 *****************************************************************************/
327 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
328 const char *psz_name, vlc_bool_t b_strict )
330 typedef struct module_list_t module_list_t;
336 module_list_t *p_next;
339 module_list_t *p_list, *p_first, *p_tmp;
342 int i_which_module, i_index = 0;
343 vlc_bool_t b_intf = VLC_FALSE;
348 char *psz_shortcuts = NULL, *psz_var = NULL;
350 msg_Dbg( p_this, "looking for %s module", psz_capability );
352 /* Deal with variables */
353 if( psz_name && psz_name[0] == '$' )
356 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
357 var_Get( p_this, psz_name + 1, &val );
358 psz_var = val.psz_string;
362 /* Count how many different shortcuts were asked for */
363 if( psz_name && *psz_name )
365 char *psz_parser, *psz_last_shortcut;
367 /* If the user wants none, give him none. */
368 if( !strcmp( psz_name, "none" ) )
370 if( psz_var ) free( psz_var );
375 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
377 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
379 if( *psz_parser == ',' )
383 psz_last_shortcut = psz_parser + 1;
387 /* Check if the user wants to override the "strict" mode */
388 if( psz_last_shortcut )
390 if( !strcmp(psz_last_shortcut, "none") )
395 else if( !strcmp(psz_last_shortcut, "any") )
397 b_strict = VLC_FALSE;
403 /* Sort the modules and test them */
404 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
405 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
408 /* Parse the module list for capabilities and probe each of them */
409 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
411 module_t * p_submodule = NULL;
412 int i_shortcut_bonus = 0, i_submodule;
414 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
416 /* Test that this module can do what we need */
417 if( strcmp( p_module->psz_capability, psz_capability ) )
419 for( i_submodule = 0;
420 i_submodule < p_module->i_children;
423 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
424 ->psz_capability, psz_capability ) )
427 (module_t*)p_module->pp_children[ i_submodule ];
432 if( p_submodule == NULL )
437 p_module = p_submodule;
440 /* Test if we have the required CPU */
441 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
446 /* If we required a shortcut, check this plugin provides it. */
447 if( i_shortcuts > 0 )
450 int i_dummy, i_short = i_shortcuts;
451 char *psz_name = psz_shortcuts;
453 /* Let's drop modules with a 0 score (unless they are
454 * explicitly requested) */
455 b_trash = !p_module->i_score;
459 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
461 if( !strcasecmp( psz_name,
462 p_module->pp_shortcuts[i_dummy] ) )
466 i_shortcut_bonus = i_short * 10000;
471 if( i_shortcut_bonus )
473 /* We found it... remember ? */
477 /* Go to the next shortcut... This is so lame! */
486 /* If we are in "strict" mode and we couldn't
487 * find the module in the list of provided shortcuts,
488 * then kick the bastard out of here!!! */
489 if( i_short == 0 && b_strict )
499 /* If we didn't require a shortcut, trash zero-scored plugins */
500 else if( !p_module->i_score )
505 /* Special case: test if we requested a particular intf plugin */
506 if( !i_shortcuts && p_module->psz_program
507 && !strcmp( p_module->psz_program,
508 p_this->p_vlc->psz_object_name ) )
512 /* Remove previous non-matching plugins */
519 /* This one doesn't match */
523 /* Store this new module */
524 p_list[ i_index ].p_module = p_module;
525 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
527 /* Add it to the modules-to-probe list */
530 p_list[ 0 ].p_next = NULL;
535 /* Ok, so at school you learned that quicksort is quick, and
536 * bubble sort sucks raw eggs. But that's when dealing with
537 * thousands of items. Here we have barely 50. */
538 module_list_t *p_newlist = p_first;
540 if( p_first->i_score < p_list[ i_index ].i_score )
542 p_list[ i_index ].p_next = p_first;
543 p_first = &p_list[ i_index ];
547 while( p_newlist->p_next != NULL &&
548 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
550 p_newlist = p_newlist->p_next;
553 p_list[ i_index ].p_next = p_newlist->p_next;
554 p_newlist->p_next = &p_list[ i_index ];
561 msg_Dbg( p_this, "probing %i candidate%s",
562 i_index, i_index == 1 ? "" : "s" );
564 /* Lock all candidate modules */
566 while( p_tmp != NULL )
568 vlc_object_yield( p_tmp->p_module );
569 p_tmp = p_tmp->p_next;
572 /* We can release the list, interesting modules were yielded */
573 vlc_list_release( p_all );
575 /* Parse the linked list and use the first successful module */
577 while( p_tmp != NULL )
579 #ifdef HAVE_DYNAMIC_PLUGINS
580 /* Make sure the module is loaded in mem */
581 module_t *p_module = p_tmp->p_module->b_submodule ?
582 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
583 if( !p_module->b_builtin && !p_module->b_loaded )
585 module_t *p_new_module =
586 AllocatePlugin( p_this, p_module->psz_filename );
589 CacheMerge( p_this, p_module, p_new_module );
590 vlc_object_attach( p_new_module, p_module );
591 DeleteModule( p_new_module );
596 if( p_tmp->p_module->pf_activate
597 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
602 vlc_object_release( p_tmp->p_module );
603 p_tmp = p_tmp->p_next;
606 /* Store the locked module value */
609 p_module = p_tmp->p_module;
610 p_tmp = p_tmp->p_next;
617 /* Unlock the remaining modules */
618 while( p_tmp != NULL )
620 vlc_object_release( p_tmp->p_module );
621 p_tmp = p_tmp->p_next;
626 if( p_module != NULL )
628 msg_Dbg( p_module, "using %s module \"%s\"",
629 psz_capability, p_module->psz_object_name );
631 else if( p_first == NULL )
633 msg_Err( p_this, "no %s module matched \"%s\"",
634 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
636 else if( psz_name != NULL && *psz_name )
638 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
639 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
644 free( psz_shortcuts );
652 /* Don't forget that the module is still locked */
656 /*****************************************************************************
657 * module_Unneed: decrease the usage count of a module.
658 *****************************************************************************
659 * This function must be called by the thread that called module_Need, to
660 * decrease the reference count and allow for hiding of modules.
661 *****************************************************************************/
662 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
664 /* Use the close method */
665 if( p_module->pf_deactivate )
667 p_module->pf_deactivate( p_this );
670 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
672 vlc_object_release( p_module );
677 /*****************************************************************************
678 * Following functions are local.
679 *****************************************************************************/
681 /*****************************************************************************
682 * AllocateAllPlugins: load all plugin modules we can find.
683 *****************************************************************************/
684 #ifdef HAVE_DYNAMIC_PLUGINS
685 static void AllocateAllPlugins( vlc_object_t *p_this )
687 /* Yes, there are two NULLs because we replace one with "plugin-path". */
688 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
691 char ** ppsz_path = path;
694 /* If the user provided a plugin path, we add it to the list */
695 path[ sizeof(path)/sizeof(char*) - 2 ] =
696 config_GetPsz( p_this, "plugin-path" );
698 for( ; *ppsz_path != NULL ; ppsz_path++ )
700 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
701 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
703 /* Handle relative as well as absolute paths */
705 if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
707 if( (*ppsz_path)[0] != '/' )
710 int i_dirlen = strlen( *ppsz_path );
711 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
713 psz_fullpath = malloc( i_dirlen );
714 if( psz_fullpath == NULL )
719 sprintf( psz_fullpath, "%s\\%s",
720 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
722 sprintf( psz_fullpath, "%s/%s",
723 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
729 psz_fullpath = strdup( *ppsz_path );
732 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
734 /* Don't go deeper than 5 subdirectories */
735 AllocatePluginDir( p_this, psz_fullpath, 5 );
737 free( psz_fullpath );
740 /* Free plugin-path */
741 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
742 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
743 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
746 /*****************************************************************************
747 * AllocatePluginDir: recursively parse a directory to look for plugins
748 *****************************************************************************/
749 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
752 #if defined( UNDER_CE ) || defined( _MSC_VER )
754 wchar_t psz_path[MAX_PATH + 256];
755 wchar_t psz_wdir[MAX_PATH];
757 char psz_path[MAX_PATH + 256];
759 WIN32_FIND_DATA finddata;
765 struct dirent * file;
769 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
774 #if defined( UNDER_CE ) || defined( _MSC_VER )
775 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
777 rc = GetFileAttributes( psz_wdir );
778 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
780 /* Not a directory */
784 /* Parse all files in the directory */
786 swprintf( psz_path, L"%s\\*.*", psz_dir );
788 sprintf( psz_path, "%s\\*.*", psz_dir );
790 handle = FindFirstFile( psz_path, &finddata );
791 if( handle == INVALID_HANDLE_VALUE )
793 /* Empty directory */
797 /* Parse the directory and try to load all files it contains. */
801 unsigned int i_len = wcslen( finddata.cFileName );
802 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
804 unsigned int i_len = strlen( finddata.cFileName );
805 /* Skip ".", ".." and anything starting with "." */
806 if( !*finddata.cFileName || *finddata.cFileName == '.' )
808 if( !FindNextFile( handle, &finddata ) ) break;
811 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
814 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
816 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
818 else if( i_len > strlen( LIBEXT )
822 /* We only load files ending with LIBEXT */
823 && !strncasecmp( psz_path + strlen( psz_path)
825 LIBEXT, strlen( LIBEXT ) ) )
829 char psz_filename[MAX_PATH];
830 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_path, -1,
831 psz_filename, MAX_PATH, NULL, NULL );
832 psz_file = psz_filename;
837 AllocatePluginFile( p_this, psz_file, 0, 0 );
840 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
842 /* Close the directory */
846 dir = opendir( psz_dir );
852 i_dirlen = strlen( psz_dir );
854 /* Parse the directory and try to load all files it contains. */
855 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
861 /* Skip ".", ".." and anything starting with "." */
862 if( !*file->d_name || *file->d_name == '.' )
867 i_len = strlen( file->d_name );
868 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
870 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
872 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
875 i_stat = stat( psz_file, &statbuf );
876 if( !i_stat && statbuf.st_mode & S_IFDIR )
878 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
880 else if( i_len > strlen( LIBEXT )
881 /* We only load files ending with LIBEXT */
882 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
883 LIBEXT, strlen( LIBEXT ) ) )
885 int64_t i_time = 0, i_size = 0;
889 i_time = statbuf.st_mtime;
890 i_size = statbuf.st_size;
893 AllocatePluginFile( p_this, psz_file, i_time, i_size );
899 /* Close the directory */
905 /*****************************************************************************
906 * AllocatePluginFile: load a module into memory and initialize it.
907 *****************************************************************************
908 * This function loads a dynamically loadable module and allocates a structure
909 * for its information data. The module can then be handled by module_Need
910 * and module_Unneed. It can be removed by DeleteModule.
911 *****************************************************************************/
912 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
913 int64_t i_file_time, int64_t i_file_size )
916 module_cache_t *p_cache_entry = NULL;
919 * Check our plugins cache first then load plugin if needed
922 CacheFind( p_this, psz_file, i_file_time, i_file_size );
926 p_module = AllocatePlugin( p_this, psz_file );
930 /* If junk dll, don't try to load it */
931 if( p_cache_entry->b_junk )
937 module_config_t *p_item;
939 p_module = p_cache_entry->p_module;
940 p_module->b_loaded = VLC_FALSE;
942 /* For now we force loading if the module's config contains
943 * callbacks or actions.
944 * Could be optimized by adding an API call.*/
945 for( p_item = p_module->p_config;
946 p_item->i_type != CONFIG_HINT_END; p_item++ )
948 if( p_item->pf_callback || p_item->i_action )
949 p_module = AllocatePlugin( p_this, psz_file );
956 /* Everything worked fine !
957 * The module is ready to be added to the list. */
958 p_module->b_builtin = VLC_FALSE;
960 /* msg_Dbg( p_this, "plugin \"%s\", %s",
961 p_module->psz_object_name, p_module->psz_longname ); */
963 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
966 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
968 /* Add entry to cache */
969 #define p_bank p_this->p_libvlc->p_module_bank
971 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
972 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
973 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
974 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
975 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
976 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
977 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
980 return p_module ? 0 : -1;
983 /*****************************************************************************
984 * AllocatePlugin: load a module into memory and initialize it.
985 *****************************************************************************
986 * This function loads a dynamically loadable module and allocates a structure
987 * for its information data. The module can then be handled by module_Need
988 * and module_Unneed. It can be removed by DeleteModule.
989 *****************************************************************************/
990 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
993 module_handle_t handle;
995 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
997 /* Now that we have successfully loaded the module, we can
998 * allocate a structure for it */
999 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1000 if( p_module == NULL )
1002 msg_Err( p_this, "out of memory" );
1003 CloseModule( handle );
1007 /* We need to fill these since they may be needed by CallEntry() */
1008 p_module->psz_filename = psz_file;
1009 p_module->handle = handle;
1010 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1011 p_module->b_loaded = VLC_TRUE;
1013 /* Initialize the module: fill p_module, default config */
1014 if( CallEntry( p_module ) != 0 )
1016 /* We couldn't call module_init() */
1017 vlc_object_destroy( p_module );
1018 CloseModule( handle );
1022 DupModule( p_module );
1023 p_module->psz_filename = strdup( p_module->psz_filename );
1024 p_module->psz_longname = strdup( p_module->psz_longname );
1026 /* Everything worked fine ! The module is ready to be added to the list. */
1027 p_module->b_builtin = VLC_FALSE;
1032 /*****************************************************************************
1033 * DupModule: make a plugin module standalone.
1034 *****************************************************************************
1035 * This function duplicates all strings in the module, so that the dynamic
1036 * object can be unloaded. It acts recursively on submodules.
1037 *****************************************************************************/
1038 static void DupModule( module_t *p_module )
1043 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1045 *pp_shortcut = strdup( *pp_shortcut );
1048 /* We strdup() these entries so that they are still valid when the
1049 * module is unloaded. */
1050 p_module->psz_object_name = strdup( p_module->psz_object_name );
1051 p_module->psz_capability = strdup( p_module->psz_capability );
1053 if( p_module->psz_program != NULL )
1055 p_module->psz_program = strdup( p_module->psz_program );
1058 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1060 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1064 /*****************************************************************************
1065 * UndupModule: free a duplicated module.
1066 *****************************************************************************
1067 * This function frees the allocations done in DupModule().
1068 *****************************************************************************/
1069 static void UndupModule( module_t *p_module )
1074 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1076 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1079 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1081 free( *pp_shortcut );
1084 free( p_module->psz_object_name );
1085 free( p_module->psz_capability );
1087 if( p_module->psz_program != NULL )
1089 free( p_module->psz_program );
1093 #endif /* HAVE_DYNAMIC_PLUGINS */
1095 /*****************************************************************************
1096 * AllocateBuiltinModule: initialize a builtin module.
1097 *****************************************************************************
1098 * This function registers a builtin module and allocates a structure
1099 * for its information data. The module can then be handled by module_Need
1100 * and module_Unneed. It can be removed by DeleteModule.
1101 *****************************************************************************/
1102 static int AllocateBuiltinModule( vlc_object_t * p_this,
1103 int ( *pf_entry ) ( module_t * ) )
1105 module_t * p_module;
1107 /* Now that we have successfully loaded the module, we can
1108 * allocate a structure for it */
1109 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1110 if( p_module == NULL )
1112 msg_Err( p_this, "out of memory" );
1116 /* Initialize the module : fill p_module->psz_object_name, etc. */
1117 if( pf_entry( p_module ) != 0 )
1119 /* With a well-written module we shouldn't have to print an
1120 * additional error message here, but just make sure. */
1121 msg_Err( p_this, "failed calling entry point in builtin module" );
1122 vlc_object_destroy( p_module );
1126 /* Everything worked fine ! The module is ready to be added to the list. */
1127 p_module->b_builtin = VLC_TRUE;
1129 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1130 p_module->psz_object_name, p_module->psz_longname ); */
1132 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1137 /*****************************************************************************
1138 * DeleteModule: delete a module and its structure.
1139 *****************************************************************************
1140 * This function can only be called if the module isn't being used.
1141 *****************************************************************************/
1142 static int DeleteModule( module_t * p_module )
1144 vlc_object_detach( p_module );
1146 /* We free the structures that we strdup()ed in Allocate*Module(). */
1147 #ifdef HAVE_DYNAMIC_PLUGINS
1148 if( !p_module->b_builtin )
1150 if( p_module->b_loaded && p_module->b_unloadable )
1152 CloseModule( p_module->handle );
1154 UndupModule( p_module );
1155 free( p_module->psz_filename );
1156 free( p_module->psz_longname );
1160 /* Free and detach the object's children */
1161 while( p_module->i_children )
1163 vlc_object_t *p_this = p_module->pp_children[0];
1164 vlc_object_detach( p_this );
1165 vlc_object_destroy( p_this );
1168 config_Free( p_module );
1169 vlc_object_destroy( p_module );
1174 #ifdef HAVE_DYNAMIC_PLUGINS
1175 /*****************************************************************************
1176 * CallEntry: call an entry point.
1177 *****************************************************************************
1178 * This function calls a symbol given its name and a module structure. The
1179 * symbol MUST refer to a function returning int and taking a module_t* as
1181 *****************************************************************************/
1182 static int CallEntry( module_t * p_module )
1184 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1185 int (* pf_symbol) ( module_t * p_module );
1187 /* Try to resolve the symbol */
1188 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1190 if( pf_symbol == NULL )
1192 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1193 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1194 psz_name, p_module->psz_filename );
1195 #elif defined(HAVE_DL_WINDOWS)
1196 char *psz_error = GetWindowsError();
1197 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1198 psz_name, p_module->psz_filename, psz_error );
1200 #elif defined(HAVE_DL_DLOPEN)
1201 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1202 psz_name, p_module->psz_filename, dlerror() );
1203 #elif defined(HAVE_DL_SHL_LOAD)
1204 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1205 psz_name, p_module->psz_filename, strerror(errno) );
1207 # error "Something is wrong in modules.c"
1212 /* We can now try to call the symbol */
1213 if( pf_symbol( 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_module, "failed calling symbol \"%s\" in file `%s'",
1218 psz_name, p_module->psz_filename );
1222 /* Everything worked fine, we can return */
1226 /*****************************************************************************
1227 * LoadModule: loads a dynamic library
1228 *****************************************************************************
1229 * This function loads a dynamically linked library using a system dependant
1230 * method. Will return 0 on success as well as the module handle.
1231 *****************************************************************************/
1232 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1233 module_handle_t *p_handle )
1235 module_handle_t handle;
1237 #if defined(HAVE_DL_DYLD)
1238 NSObjectFileImage image;
1239 NSObjectFileImageReturnCode ret;
1241 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1243 if( ret != NSObjectFileImageSuccess )
1245 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1249 /* Open the dynamic module */
1250 handle = NSLinkModule( image, psz_file,
1251 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1255 NSLinkEditErrors errors;
1256 const char *psz_file, *psz_err;
1258 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1259 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1260 NSDestroyObjectFileImage( image );
1264 /* Destroy our image, we won't need it */
1265 NSDestroyObjectFileImage( image );
1267 #elif defined(HAVE_DL_BEOS)
1268 handle = load_add_on( psz_file );
1271 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1275 #elif defined(HAVE_DL_WINDOWS)
1276 handle = LoadLibrary( psz_file );
1277 if( handle == NULL )
1279 char *psz_err = GetWindowsError();
1280 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1284 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1285 /* static is OK, we are called atomically */
1286 static vlc_bool_t b_kde = VLC_FALSE;
1288 # if defined(SYS_LINUX)
1289 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1290 * are going to get namespace collisions when two modules have common
1291 * public symbols, but ALSA is being a pest here. */
1292 if( strstr( psz_file, "alsa_plugin" ) )
1294 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1295 if( handle == NULL )
1297 msg_Warn( p_this, "cannot load module `%s' (%s)",
1298 psz_file, dlerror() );
1303 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1304 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1305 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1306 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1307 if( !b_kde && !strstr( psz_file, "kde" ) )
1309 dlopen( "libstdc++.so.6", RTLD_NOW )
1310 || dlopen( "libstdc++.so.5", RTLD_NOW )
1311 || dlopen( "libstdc++.so.4", RTLD_NOW )
1312 || dlopen( "libstdc++.so.3", RTLD_NOW );
1316 handle = dlopen( psz_file, RTLD_NOW );
1317 if( handle == NULL )
1319 msg_Warn( p_this, "cannot load module `%s' (%s)",
1320 psz_file, dlerror() );
1324 #elif defined(HAVE_DL_DLOPEN)
1325 # if defined(DL_LAZY)
1326 handle = dlopen( psz_file, DL_LAZY );
1328 handle = dlopen( psz_file, 0 );
1330 if( handle == NULL )
1332 msg_Warn( p_this, "cannot load module `%s' (%s)",
1333 psz_file, dlerror() );
1337 #elif defined(HAVE_DL_SHL_LOAD)
1338 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1339 if( handle == NULL )
1341 msg_Warn( p_this, "cannot load module `%s' (%s)",
1342 psz_file, strerror(errno) );
1347 # error "Something is wrong in modules.c"
1355 /*****************************************************************************
1356 * CloseModule: unload a dynamic library
1357 *****************************************************************************
1358 * This function unloads a previously opened dynamically linked library
1359 * using a system dependant method. No return value is taken in consideration,
1360 * since some libraries sometimes refuse to close properly.
1361 *****************************************************************************/
1362 static void CloseModule( module_handle_t handle )
1364 #if defined(HAVE_DL_DYLD)
1365 NSUnLinkModule( handle, FALSE );
1367 #elif defined(HAVE_DL_BEOS)
1368 unload_add_on( handle );
1370 #elif defined(HAVE_DL_WINDOWS)
1371 FreeLibrary( handle );
1373 #elif defined(HAVE_DL_DLOPEN)
1376 #elif defined(HAVE_DL_SHL_LOAD)
1377 shl_unload( handle );
1383 /*****************************************************************************
1384 * GetSymbol: get a symbol from a dynamic library
1385 *****************************************************************************
1386 * This function queries a loaded library for a symbol specified in a
1387 * string, and returns a pointer to it. We don't check for dlerror() or
1388 * similar functions, since we want a non-NULL symbol anyway.
1389 *****************************************************************************/
1390 static void * _module_getsymbol( module_handle_t, const char * );
1392 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1394 void * p_symbol = _module_getsymbol( handle, psz_function );
1396 /* MacOS X dl library expects symbols to begin with "_". So do
1397 * some other operating systems. That's really lame, but hey, what
1399 if( p_symbol == NULL )
1401 char *psz_call = malloc( strlen( psz_function ) + 2 );
1403 strcpy( psz_call + 1, psz_function );
1404 psz_call[ 0 ] = '_';
1405 p_symbol = _module_getsymbol( handle, psz_call );
1412 static void * _module_getsymbol( module_handle_t handle,
1413 const char * psz_function )
1415 #if defined(HAVE_DL_DYLD)
1416 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1417 return NSAddressOfSymbol( sym );
1419 #elif defined(HAVE_DL_BEOS)
1421 if( B_OK == get_image_symbol( handle, psz_function,
1422 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1431 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1432 wchar_t psz_real[256];
1433 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1435 return (void *)GetProcAddress( handle, psz_real );
1437 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1438 return (void *)GetProcAddress( handle, (char *)psz_function );
1440 #elif defined(HAVE_DL_DLOPEN)
1441 return dlsym( handle, psz_function );
1443 #elif defined(HAVE_DL_SHL_LOAD)
1445 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1451 #if defined(HAVE_DL_WINDOWS)
1452 static char * GetWindowsError( void )
1454 #if defined(UNDER_CE)
1455 wchar_t psz_tmp[256];
1456 char * psz_buffer = malloc( 256 );
1458 char * psz_tmp = malloc( 256 );
1460 int i = 0, i_error = GetLastError();
1462 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1463 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1464 (LPTSTR) psz_tmp, 256, NULL );
1466 /* Go to the end of the string */
1467 #if defined(UNDER_CE)
1468 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1470 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1478 #if defined(UNDER_CE)
1479 swprintf( psz_tmp + i, L" (error %i)", i_error );
1480 psz_tmp[ 255 ] = L'\0';
1482 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1483 psz_tmp[ 255 ] = '\0';
1487 #if defined(UNDER_CE)
1488 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1489 psz_buffer, 256, NULL, NULL );
1495 #endif /* HAVE_DL_WINDOWS */
1497 /*****************************************************************************
1498 * LoadPluginsCache: loads the plugins cache file
1499 *****************************************************************************
1500 * This function will load the plugin cache if present and valid. This cache
1501 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1502 * actually load the dynamically loadable module.
1503 * This allows us to only fully load plugins when they are actually used.
1504 *****************************************************************************/
1505 static void CacheLoad( vlc_object_t *p_this )
1507 char *psz_filename, *psz_homedir;
1509 int i, j, i_size, i_read;
1510 char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1512 module_cache_t **pp_cache = 0;
1513 int32_t i_file_size;
1515 psz_homedir = p_this->p_vlc->psz_homedir;
1518 msg_Err( p_this, "psz_homedir is null" );
1522 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1523 strlen(psz_homedir) );
1526 sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1531 msg_Err( p_this, "out of memory" );
1535 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1537 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1538 unlink( psz_filename );
1542 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1544 file = fopen( psz_filename, "r" );
1547 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1549 free( psz_filename );
1552 free( psz_filename );
1554 /* Check the file size */
1555 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1556 if( i_read != sizeof(i_file_size) )
1558 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1564 fseek( file, 0, SEEK_END );
1565 if( ftell( file ) != i_file_size )
1567 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1568 "(corrupted size)" );
1572 fseek( file, sizeof(i_file_size), SEEK_SET );
1574 /* Check the file is a plugins cache */
1575 i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1576 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1577 if( i_read != i_size ||
1578 memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1580 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1585 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1586 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1587 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1588 malloc( i_cache * sizeof(void *) );
1590 #define LOAD_IMMEDIATE(a) \
1591 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1592 #define LOAD_STRING(a) \
1593 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1594 != sizeof(i_size) ) goto error; \
1596 a = malloc( i_size ); \
1597 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1603 for( i = 0; i < i_cache; i++ )
1608 pp_cache[i] = malloc( sizeof(module_cache_t) );
1609 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1611 /* Save common info */
1612 LOAD_STRING( pp_cache[i]->psz_file );
1613 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1614 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1615 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1617 if( pp_cache[i]->b_junk ) continue;
1619 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1621 /* Save additional infos */
1622 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1623 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1624 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1625 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1626 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1628 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1630 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1631 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1632 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1633 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1634 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1635 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1638 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1641 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1643 LOAD_IMMEDIATE( i_submodules );
1645 while( i_submodules-- )
1647 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1648 vlc_object_attach( p_module, pp_cache[i]->p_module );
1649 p_module->b_submodule = VLC_TRUE;
1651 LOAD_STRING( p_module->psz_object_name );
1652 LOAD_STRING( p_module->psz_shortname );
1653 LOAD_STRING( p_module->psz_longname );
1654 LOAD_STRING( p_module->psz_program );
1655 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1657 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1659 LOAD_STRING( p_module->psz_capability );
1660 LOAD_IMMEDIATE( p_module->i_score );
1661 LOAD_IMMEDIATE( p_module->i_cpu );
1662 LOAD_IMMEDIATE( p_module->b_unloadable );
1663 LOAD_IMMEDIATE( p_module->b_reentrant );
1672 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1675 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1681 int CacheLoadConfig( module_t *p_module, FILE *file )
1686 /* Calculate the structure length */
1687 LOAD_IMMEDIATE( p_module->i_config_items );
1688 LOAD_IMMEDIATE( p_module->i_bool_items );
1690 LOAD_IMMEDIATE( i_lines );
1692 /* Allocate memory */
1693 p_module->p_config =
1694 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1695 if( p_module->p_config == NULL )
1697 msg_Err( p_module, "config error: can't duplicate p_config" );
1701 /* Do the duplication job */
1702 for( i = 0; i < i_lines ; i++ )
1704 LOAD_IMMEDIATE( p_module->p_config[i] );
1706 LOAD_STRING( p_module->p_config[i].psz_type );
1707 LOAD_STRING( p_module->p_config[i].psz_name );
1708 LOAD_STRING( p_module->p_config[i].psz_text );
1709 LOAD_STRING( p_module->p_config[i].psz_longtext );
1710 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1712 p_module->p_config[i].psz_value =
1713 p_module->p_config[i].psz_value_orig ?
1714 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1715 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1716 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1718 p_module->p_config[i].p_lock = &p_module->object_lock;
1720 if( p_module->p_config[i].i_list )
1722 if( p_module->p_config[i].ppsz_list )
1724 p_module->p_config[i].ppsz_list =
1725 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1726 if( p_module->p_config[i].ppsz_list )
1728 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1729 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1730 p_module->p_config[i].ppsz_list[j] = NULL;
1733 if( p_module->p_config[i].ppsz_list_text )
1735 p_module->p_config[i].ppsz_list_text =
1736 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1737 if( p_module->p_config[i].ppsz_list_text )
1739 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1740 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1741 p_module->p_config[i].ppsz_list_text[j] = NULL;
1744 if( p_module->p_config[i].pi_list )
1746 p_module->p_config[i].pi_list =
1747 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1748 if( p_module->p_config[i].pi_list )
1750 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1751 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1756 if( p_module->p_config[i].i_action )
1758 p_module->p_config[i].ppf_action =
1759 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1760 p_module->p_config[i].ppsz_action_text =
1761 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1763 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1765 p_module->p_config[i].ppf_action[j] = 0;
1766 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1770 p_module->p_config[i].pf_callback = 0;
1773 p_module->p_config[i].i_type = CONFIG_HINT_END;
1779 return VLC_EGENERIC;
1782 /*****************************************************************************
1783 * SavePluginsCache: saves the plugins cache to a file
1784 *****************************************************************************/
1785 static void CacheSave( vlc_object_t *p_this )
1787 char *psz_filename, *psz_homedir;
1790 module_cache_t **pp_cache;
1791 int32_t i_file_size = 0;
1793 psz_homedir = p_this->p_vlc->psz_homedir;
1796 msg_Err( p_this, "psz_homedir is null" );
1800 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1801 strlen(psz_homedir) );
1804 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1808 msg_Err( p_this, "out of memory" );
1812 config_CreateDir( p_this, psz_filename );
1814 strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1816 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1818 file = fopen( psz_filename, "w" );
1821 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1823 free( psz_filename );
1826 free( psz_filename );
1828 /* Empty space for file size */
1829 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1831 /* Contains version number */
1832 fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1834 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1835 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1837 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1839 #define SAVE_IMMEDIATE(a) \
1840 fwrite( &a, sizeof(char), sizeof(a), file )
1841 #define SAVE_STRING(a) \
1842 { i_size = a ? strlen( a ) + 1 : 0; \
1843 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1844 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1847 for( i = 0; i < i_cache; i++ )
1850 int32_t i_submodule;
1852 /* Save common info */
1853 SAVE_STRING( pp_cache[i]->psz_file );
1854 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1855 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1856 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1858 if( pp_cache[i]->b_junk ) continue;
1860 /* Save additional infos */
1861 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1862 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1863 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1864 SAVE_STRING( pp_cache[i]->p_module->psz_program );
1865 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1867 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1869 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1870 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1871 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1872 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1873 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1874 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1877 CacheSaveConfig( pp_cache[i]->p_module, file );
1879 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1881 i_submodule = pp_cache[i]->p_module->i_children;
1882 SAVE_IMMEDIATE( i_submodule );
1883 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1886 module_t *p_module =
1887 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1889 SAVE_STRING( p_module->psz_object_name );
1890 SAVE_STRING( p_module->psz_shortname );
1891 SAVE_STRING( p_module->psz_longname );
1892 SAVE_STRING( p_module->psz_program );
1893 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1895 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1897 SAVE_STRING( p_module->psz_capability );
1898 SAVE_IMMEDIATE( p_module->i_score );
1899 SAVE_IMMEDIATE( p_module->i_cpu );
1900 SAVE_IMMEDIATE( p_module->b_unloadable );
1901 SAVE_IMMEDIATE( p_module->b_reentrant );
1905 /* Fill-up file size */
1906 i_file_size = ftell( file );
1907 fseek( file, 0, SEEK_SET );
1908 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1915 void CacheSaveConfig( module_t *p_module, FILE *file )
1917 int i, j, i_lines = 0;
1918 module_config_t *p_item;
1921 SAVE_IMMEDIATE( p_module->i_config_items );
1922 SAVE_IMMEDIATE( p_module->i_bool_items );
1924 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1925 p_item++ ) i_lines++;
1927 SAVE_IMMEDIATE( i_lines );
1929 for( i = 0; i < i_lines ; i++ )
1931 SAVE_IMMEDIATE( p_module->p_config[i] );
1933 SAVE_STRING( p_module->p_config[i].psz_type );
1934 SAVE_STRING( p_module->p_config[i].psz_name );
1935 SAVE_STRING( p_module->p_config[i].psz_text );
1936 SAVE_STRING( p_module->p_config[i].psz_longtext );
1937 SAVE_STRING( p_module->p_config[i].psz_value_orig );
1939 if( p_module->p_config[i].i_list )
1941 if( p_module->p_config[i].ppsz_list )
1943 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1944 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1947 if( p_module->p_config[i].ppsz_list_text )
1949 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1950 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1952 if( p_module->p_config[i].pi_list )
1954 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1955 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1959 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1960 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1964 /*****************************************************************************
1965 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1966 *****************************************************************************/
1967 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1968 module_t *p_module )
1972 p_cache->pf_activate = p_module->pf_activate;
1973 p_cache->pf_deactivate = p_module->pf_deactivate;
1974 p_cache->p_symbols = p_module->p_symbols;
1975 p_cache->handle = p_module->handle;
1977 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1979 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1980 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1981 p_cchild->pf_activate = p_child->pf_activate;
1982 p_cchild->pf_deactivate = p_child->pf_deactivate;
1983 p_cchild->p_symbols = p_child->p_symbols;
1986 p_cache->b_loaded = VLC_TRUE;
1987 p_module->b_loaded = VLC_FALSE;
1990 /*****************************************************************************
1991 * FindPluginCache: finds the cache entry corresponding to a file
1992 *****************************************************************************/
1993 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
1994 int64_t i_time, int64_t i_size )
1996 module_cache_t **pp_cache;
1999 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2000 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2002 for( i = 0; i < i_cache; i++ )
2004 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2005 pp_cache[i]->i_time == i_time &&
2006 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2012 #endif /* HAVE_DYNAMIC_PLUGINS */