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"
95 #include "audio_output.h"
96 #include "aout_internal.h"
98 #include "stream_output.h"
100 #include "vlc_httpd.h"
102 #include "iso_lang.h"
105 #include "vlc_block.h"
109 #ifdef HAVE_DYNAMIC_PLUGINS
110 # include "modules_plugin.h"
113 #if defined( UNDER_CE )
114 # include "modules_builtin_evc.h"
115 #elif defined( _MSC_VER )
116 # include "modules_builtin_msvc.h"
118 # include "modules_builtin.h"
122 #if defined( WIN32) || defined( UNDER_CE )
123 /* Avoid name collisions */
124 # define LoadModule(a,b,c) _LoadModule(a,b,c)
127 /*****************************************************************************
129 *****************************************************************************/
130 #ifdef HAVE_DYNAMIC_PLUGINS
131 static void AllocateAllPlugins ( vlc_object_t * );
132 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
133 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
134 static module_t * AllocatePlugin( vlc_object_t *, char * );
136 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
137 static int DeleteModule ( module_t * );
138 #ifdef HAVE_DYNAMIC_PLUGINS
139 static void DupModule ( module_t * );
140 static void UndupModule ( module_t * );
141 static int CallEntry ( module_t * );
142 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
143 static void CloseModule ( module_handle_t );
144 static void * GetSymbol ( module_handle_t, const char * );
145 static void CacheLoad ( vlc_object_t * );
146 static int CacheLoadConfig ( module_t *, FILE * );
147 static void CacheSave ( vlc_object_t * );
148 static void CacheSaveConfig ( module_t *, FILE * );
149 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
150 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
152 #if defined(HAVE_DL_WINDOWS)
153 static char * GetWindowsError ( void );
157 /*****************************************************************************
158 * module_InitBank: create the module bank.
159 *****************************************************************************
160 * This function creates a module bank structure which will be filled later
161 * on with all the modules found.
162 *****************************************************************************/
163 void __module_InitBank( vlc_object_t *p_this )
165 module_bank_t *p_bank;
168 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
169 var_Get( p_this->p_libvlc, "libvlc", &lockval );
170 vlc_mutex_lock( lockval.p_address );
171 if( p_this->p_libvlc->p_module_bank )
173 p_this->p_libvlc->p_module_bank->i_usage++;
174 vlc_mutex_unlock( lockval.p_address );
175 var_Destroy( p_this->p_libvlc, "libvlc" );
178 vlc_mutex_unlock( lockval.p_address );
179 var_Destroy( p_this->p_libvlc, "libvlc" );
181 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
182 p_bank->psz_object_name = "module bank";
184 p_bank->i_cache = p_bank->i_loaded_cache = 0;
185 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
186 p_bank->b_cache = p_bank->b_cache_dirty =
187 p_bank->b_cache_delete = VLC_FALSE;
190 * Store the symbols to be exported
192 #ifdef HAVE_DYNAMIC_PLUGINS
193 STORE_SYMBOLS( &p_bank->symbols );
196 /* Everything worked, attach the object */
197 p_this->p_libvlc->p_module_bank = p_bank;
198 vlc_object_attach( p_bank, p_this->p_libvlc );
200 module_LoadMain( p_this );
205 /*****************************************************************************
206 * module_ResetBank: reset the module bank.
207 *****************************************************************************
208 * This function resets the module bank by unloading all unused plugin
210 *****************************************************************************/
211 void __module_ResetBank( vlc_object_t *p_this )
213 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
217 /*****************************************************************************
218 * module_EndBank: empty the module bank.
219 *****************************************************************************
220 * This function unloads all unused plugin modules and empties the module
221 * bank in case of success.
222 *****************************************************************************/
223 void __module_EndBank( vlc_object_t *p_this )
228 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
229 var_Get( p_this->p_libvlc, "libvlc", &lockval );
230 vlc_mutex_lock( lockval.p_address );
231 if( !p_this->p_libvlc->p_module_bank )
233 vlc_mutex_unlock( lockval.p_address );
234 var_Destroy( p_this->p_libvlc, "libvlc" );
237 if( --p_this->p_libvlc->p_module_bank->i_usage )
239 vlc_mutex_unlock( lockval.p_address );
240 var_Destroy( p_this->p_libvlc, "libvlc" );
243 vlc_mutex_unlock( lockval.p_address );
244 var_Destroy( p_this->p_libvlc, "libvlc" );
246 #ifdef HAVE_DYNAMIC_PLUGINS
247 if( p_this->p_libvlc->p_module_bank->b_cache ) CacheSave( p_this );
250 vlc_object_detach( p_this->p_libvlc->p_module_bank );
252 while( p_this->p_libvlc->p_module_bank->i_children )
254 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
256 if( DeleteModule( p_next ) )
258 /* Module deletion failed */
259 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
260 p_next->psz_object_name );
262 /* We just free the module by hand. Niahahahahaha. */
263 vlc_object_detach( p_next );
264 vlc_object_destroy( p_next );
268 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
273 /*****************************************************************************
274 * module_LoadMain: load the main program info into the module bank.
275 *****************************************************************************
276 * This function fills the module bank structure with the main module infos.
277 * This is very useful as it will allow us to consider the main program just
278 * as another module, and for instance the configuration options of main will
279 * be available in the module bank structure just as for every other module.
280 *****************************************************************************/
281 void __module_LoadMain( vlc_object_t *p_this )
283 AllocateBuiltinModule( p_this, vlc_entry__main );
286 /*****************************************************************************
287 * module_LoadBuiltins: load all modules which we built with.
288 *****************************************************************************
289 * This function fills the module bank structure with the builtin modules.
290 *****************************************************************************/
291 void __module_LoadBuiltins( vlc_object_t * p_this )
293 msg_Dbg( p_this, "checking builtin modules" );
294 ALLOCATE_ALL_BUILTINS();
297 /*****************************************************************************
298 * module_LoadPlugins: load all plugin modules we can find.
299 *****************************************************************************
300 * This function fills the module bank structure with the plugin modules.
301 *****************************************************************************/
302 void __module_LoadPlugins( vlc_object_t * p_this )
304 #ifdef HAVE_DYNAMIC_PLUGINS
305 msg_Dbg( p_this, "checking plugin modules" );
307 if( config_GetInt( p_this, "plugins-cache" ) )
308 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
310 if( p_this->p_libvlc->p_module_bank->b_cache ||
311 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
313 AllocateAllPlugins( p_this );
317 /*****************************************************************************
318 * module_Need: return the best module function, given a capability list.
319 *****************************************************************************
320 * This function returns the module that best fits the asked capabilities.
321 *****************************************************************************/
322 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
323 const char *psz_name, vlc_bool_t b_strict )
325 typedef struct module_list_t module_list_t;
331 module_list_t *p_next;
334 module_list_t *p_list, *p_first, *p_tmp;
337 int i_which_module, i_index = 0;
338 vlc_bool_t b_intf = VLC_FALSE;
343 char *psz_shortcuts = NULL, *psz_var = NULL;
345 msg_Dbg( p_this, "looking for %s module", psz_capability );
347 /* Deal with variables */
348 if( psz_name && psz_name[0] == '$' )
351 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
352 var_Get( p_this, psz_name + 1, &val );
353 psz_var = val.psz_string;
357 /* Count how many different shortcuts were asked for */
358 if( psz_name && *psz_name )
360 char *psz_parser, *psz_last_shortcut;
362 /* If the user wants none, give him none. */
363 if( !strcmp( psz_name, "none" ) )
365 if( psz_var ) free( psz_var );
370 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
372 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
374 if( *psz_parser == ',' )
378 psz_last_shortcut = psz_parser + 1;
382 /* Check if the user wants to override the "strict" mode */
383 if( psz_last_shortcut )
385 if( !strcmp(psz_last_shortcut, "none") )
390 else if( !strcmp(psz_last_shortcut, "any") )
392 b_strict = VLC_FALSE;
398 /* Sort the modules and test them */
399 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
400 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
403 /* Parse the module list for capabilities and probe each of them */
404 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
406 int i_shortcut_bonus = 0;
408 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
410 /* Test that this module can do what we need */
411 if( strcmp( p_module->psz_capability, psz_capability ) )
413 /* Don't recurse through the sub-modules because vlc_list_find()
414 * will list them anyway. */
418 /* Test if we have the required CPU */
419 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
424 /* If we required a shortcut, check this plugin provides it. */
425 if( i_shortcuts > 0 )
428 int i_dummy, i_short = i_shortcuts;
429 char *psz_name = psz_shortcuts;
431 /* Let's drop modules with a 0 score (unless they are
432 * explicitly requested) */
433 b_trash = !p_module->i_score;
437 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
439 if( !strcasecmp( psz_name,
440 p_module->pp_shortcuts[i_dummy] ) )
444 i_shortcut_bonus = i_short * 10000;
449 if( i_shortcut_bonus )
451 /* We found it... remember ? */
455 /* Go to the next shortcut... This is so lame! */
464 /* If we are in "strict" mode and we couldn't
465 * find the module in the list of provided shortcuts,
466 * then kick the bastard out of here!!! */
467 if( i_short == 0 && b_strict )
477 /* If we didn't require a shortcut, trash zero-scored plugins */
478 else if( !p_module->i_score )
483 /* Special case: test if we requested a particular intf plugin */
484 if( !i_shortcuts && p_module->psz_program
485 && !strcmp( p_module->psz_program,
486 p_this->p_vlc->psz_object_name ) )
490 /* Remove previous non-matching plugins */
497 /* This one doesn't match */
501 /* Store this new module */
502 p_list[ i_index ].p_module = p_module;
503 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
505 /* Add it to the modules-to-probe list */
508 p_list[ 0 ].p_next = NULL;
513 /* Ok, so at school you learned that quicksort is quick, and
514 * bubble sort sucks raw eggs. But that's when dealing with
515 * thousands of items. Here we have barely 50. */
516 module_list_t *p_newlist = p_first;
518 if( p_first->i_score < p_list[ i_index ].i_score )
520 p_list[ i_index ].p_next = p_first;
521 p_first = &p_list[ i_index ];
525 while( p_newlist->p_next != NULL &&
526 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
528 p_newlist = p_newlist->p_next;
531 p_list[ i_index ].p_next = p_newlist->p_next;
532 p_newlist->p_next = &p_list[ i_index ];
539 msg_Dbg( p_this, "probing %i candidate%s",
540 i_index, i_index == 1 ? "" : "s" );
542 /* Lock all candidate modules */
544 while( p_tmp != NULL )
546 vlc_object_yield( p_tmp->p_module );
547 p_tmp = p_tmp->p_next;
550 /* We can release the list, interesting modules were yielded */
551 vlc_list_release( p_all );
553 /* Parse the linked list and use the first successful module */
555 while( p_tmp != NULL )
557 #ifdef HAVE_DYNAMIC_PLUGINS
558 /* Make sure the module is loaded in mem */
559 module_t *p_module = p_tmp->p_module->b_submodule ?
560 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
561 if( !p_module->b_builtin && !p_module->b_loaded )
563 module_t *p_new_module =
564 AllocatePlugin( p_this, p_module->psz_filename );
567 CacheMerge( p_this, p_module, p_new_module );
568 vlc_object_attach( p_new_module, p_module );
569 DeleteModule( p_new_module );
574 if( p_tmp->p_module->pf_activate
575 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
580 vlc_object_release( p_tmp->p_module );
581 p_tmp = p_tmp->p_next;
584 /* Store the locked module value */
587 p_module = p_tmp->p_module;
588 p_tmp = p_tmp->p_next;
595 /* Unlock the remaining modules */
596 while( p_tmp != NULL )
598 vlc_object_release( p_tmp->p_module );
599 p_tmp = p_tmp->p_next;
604 if( p_module != NULL )
606 msg_Dbg( p_module, "using %s module \"%s\"",
607 psz_capability, p_module->psz_object_name );
609 else if( p_first == NULL )
611 msg_Err( p_this, "no %s module matched \"%s\"",
612 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
614 else if( psz_name != NULL && *psz_name )
616 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
617 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
622 free( psz_shortcuts );
630 /* Don't forget that the module is still locked */
634 /*****************************************************************************
635 * module_Unneed: decrease the usage count of a module.
636 *****************************************************************************
637 * This function must be called by the thread that called module_Need, to
638 * decrease the reference count and allow for hiding of modules.
639 *****************************************************************************/
640 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
642 /* Use the close method */
643 if( p_module->pf_deactivate )
645 p_module->pf_deactivate( p_this );
648 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
650 vlc_object_release( p_module );
655 /*****************************************************************************
656 * Following functions are local.
657 *****************************************************************************/
659 /*****************************************************************************
660 * AllocateAllPlugins: load all plugin modules we can find.
661 *****************************************************************************/
662 #ifdef HAVE_DYNAMIC_PLUGINS
663 static void AllocateAllPlugins( vlc_object_t *p_this )
665 /* Yes, there are two NULLs because we replace one with "plugin-path". */
666 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
669 char ** ppsz_path = path;
672 /* If the user provided a plugin path, we add it to the list */
673 path[ sizeof(path)/sizeof(char*) - 2 ] =
674 config_GetPsz( p_this, "plugin-path" );
676 for( ; *ppsz_path != NULL ; ppsz_path++ )
678 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
679 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
681 /* Handle relative as well as absolute paths */
683 if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
685 if( (*ppsz_path)[0] != '/' )
688 int i_dirlen = strlen( *ppsz_path );
689 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
691 psz_fullpath = malloc( i_dirlen );
692 if( psz_fullpath == NULL )
697 sprintf( psz_fullpath, "%s\\%s",
698 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
700 sprintf( psz_fullpath, "%s/%s",
701 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
707 psz_fullpath = strdup( *ppsz_path );
710 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
712 /* Don't go deeper than 5 subdirectories */
713 AllocatePluginDir( p_this, psz_fullpath, 5 );
715 free( psz_fullpath );
718 /* Free plugin-path */
719 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
720 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
721 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
724 /*****************************************************************************
725 * AllocatePluginDir: recursively parse a directory to look for plugins
726 *****************************************************************************/
727 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
730 #if defined( UNDER_CE ) || defined( _MSC_VER )
732 wchar_t psz_path[MAX_PATH + 256];
733 wchar_t psz_wdir[MAX_PATH];
735 char psz_path[MAX_PATH + 256];
737 WIN32_FIND_DATA finddata;
743 struct dirent * file;
747 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
752 #if defined( UNDER_CE ) || defined( _MSC_VER )
753 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
755 rc = GetFileAttributes( psz_wdir );
756 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
758 /* Not a directory */
762 /* Parse all files in the directory */
764 swprintf( psz_path, L"%s\\*.*", psz_dir );
766 sprintf( psz_path, "%s\\*.*", psz_dir );
768 handle = FindFirstFile( psz_path, &finddata );
769 if( handle == INVALID_HANDLE_VALUE )
771 /* Empty directory */
775 /* Parse the directory and try to load all files it contains. */
779 unsigned int i_len = wcslen( finddata.cFileName );
780 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
782 unsigned int i_len = strlen( finddata.cFileName );
783 /* Skip ".", ".." and anything starting with "." */
784 if( !*finddata.cFileName || *finddata.cFileName == '.' )
786 if( !FindNextFile( handle, &finddata ) ) break;
789 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
792 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
794 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
796 else if( i_len > strlen( LIBEXT )
800 /* We only load files ending with LIBEXT */
801 && !strncasecmp( psz_path + strlen( psz_path)
803 LIBEXT, strlen( LIBEXT ) ) )
807 char psz_filename[MAX_PATH];
808 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_path, -1,
809 psz_filename, MAX_PATH, NULL, NULL );
810 psz_file = psz_filename;
815 AllocatePluginFile( p_this, psz_file, 0, 0 );
818 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
820 /* Close the directory */
824 dir = opendir( psz_dir );
830 i_dirlen = strlen( psz_dir );
832 /* Parse the directory and try to load all files it contains. */
833 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
839 /* Skip ".", ".." and anything starting with "." */
840 if( !*file->d_name || *file->d_name == '.' )
845 i_len = strlen( file->d_name );
846 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
848 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
850 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
853 i_stat = stat( psz_file, &statbuf );
854 if( !i_stat && statbuf.st_mode & S_IFDIR )
856 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
858 else if( i_len > strlen( LIBEXT )
859 /* We only load files ending with LIBEXT */
860 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
861 LIBEXT, strlen( LIBEXT ) ) )
863 int64_t i_time = 0, i_size = 0;
867 i_time = statbuf.st_mtime;
868 i_size = statbuf.st_size;
871 AllocatePluginFile( p_this, psz_file, i_time, i_size );
877 /* Close the directory */
883 /*****************************************************************************
884 * AllocatePluginFile: load a module into memory and initialize it.
885 *****************************************************************************
886 * This function loads a dynamically loadable module and allocates a structure
887 * for its information data. The module can then be handled by module_Need
888 * and module_Unneed. It can be removed by DeleteModule.
889 *****************************************************************************/
890 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
891 int64_t i_file_time, int64_t i_file_size )
894 module_cache_t *p_cache_entry = NULL;
897 * Check our plugins cache first then load plugin if needed
900 CacheFind( p_this, psz_file, i_file_time, i_file_size );
904 p_module = AllocatePlugin( p_this, psz_file );
908 /* If junk dll, don't try to load it */
909 if( p_cache_entry->b_junk )
915 module_config_t *p_item;
917 p_module = p_cache_entry->p_module;
918 p_module->b_loaded = VLC_FALSE;
920 /* For now we force loading if the module's config contains
921 * callbacks or actions.
922 * Could be optimized by adding an API call.*/
923 for( p_item = p_module->p_config;
924 p_item->i_type != CONFIG_HINT_END; p_item++ )
926 if( p_item->pf_callback || p_item->i_action )
927 p_module = AllocatePlugin( p_this, psz_file );
934 /* Everything worked fine !
935 * The module is ready to be added to the list. */
936 p_module->b_builtin = VLC_FALSE;
938 /* msg_Dbg( p_this, "plugin \"%s\", %s",
939 p_module->psz_object_name, p_module->psz_longname ); */
941 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
944 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
946 /* Add entry to cache */
947 #define p_bank p_this->p_libvlc->p_module_bank
949 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
950 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
951 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
952 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
953 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
954 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
955 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
958 return p_module ? 0 : -1;
961 /*****************************************************************************
962 * AllocatePlugin: load a module into memory and initialize it.
963 *****************************************************************************
964 * This function loads a dynamically loadable module and allocates a structure
965 * for its information data. The module can then be handled by module_Need
966 * and module_Unneed. It can be removed by DeleteModule.
967 *****************************************************************************/
968 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
971 module_handle_t handle;
973 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
975 /* Now that we have successfully loaded the module, we can
976 * allocate a structure for it */
977 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
978 if( p_module == NULL )
980 msg_Err( p_this, "out of memory" );
981 CloseModule( handle );
985 /* We need to fill these since they may be needed by CallEntry() */
986 p_module->psz_filename = psz_file;
987 p_module->handle = handle;
988 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
989 p_module->b_loaded = VLC_TRUE;
991 /* Initialize the module: fill p_module, default config */
992 if( CallEntry( p_module ) != 0 )
994 /* We couldn't call module_init() */
995 vlc_object_destroy( p_module );
996 CloseModule( handle );
1000 DupModule( p_module );
1001 p_module->psz_filename = strdup( p_module->psz_filename );
1002 p_module->psz_longname = strdup( p_module->psz_longname );
1004 /* Everything worked fine ! The module is ready to be added to the list. */
1005 p_module->b_builtin = VLC_FALSE;
1010 /*****************************************************************************
1011 * DupModule: make a plugin module standalone.
1012 *****************************************************************************
1013 * This function duplicates all strings in the module, so that the dynamic
1014 * object can be unloaded. It acts recursively on submodules.
1015 *****************************************************************************/
1016 static void DupModule( module_t *p_module )
1021 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1023 *pp_shortcut = strdup( *pp_shortcut );
1026 /* We strdup() these entries so that they are still valid when the
1027 * module is unloaded. */
1028 p_module->psz_object_name = strdup( p_module->psz_object_name );
1029 p_module->psz_capability = strdup( p_module->psz_capability );
1031 if( p_module->psz_program != NULL )
1033 p_module->psz_program = strdup( p_module->psz_program );
1036 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1038 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1042 /*****************************************************************************
1043 * UndupModule: free a duplicated module.
1044 *****************************************************************************
1045 * This function frees the allocations done in DupModule().
1046 *****************************************************************************/
1047 static void UndupModule( module_t *p_module )
1052 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1054 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1057 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1059 free( *pp_shortcut );
1062 free( p_module->psz_object_name );
1063 free( p_module->psz_capability );
1065 if( p_module->psz_program != NULL )
1067 free( p_module->psz_program );
1071 #endif /* HAVE_DYNAMIC_PLUGINS */
1073 /*****************************************************************************
1074 * AllocateBuiltinModule: initialize a builtin module.
1075 *****************************************************************************
1076 * This function registers a builtin module and allocates a structure
1077 * for its information data. The module can then be handled by module_Need
1078 * and module_Unneed. It can be removed by DeleteModule.
1079 *****************************************************************************/
1080 static int AllocateBuiltinModule( vlc_object_t * p_this,
1081 int ( *pf_entry ) ( module_t * ) )
1083 module_t * p_module;
1085 /* Now that we have successfully loaded the module, we can
1086 * allocate a structure for it */
1087 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1088 if( p_module == NULL )
1090 msg_Err( p_this, "out of memory" );
1094 /* Initialize the module : fill p_module->psz_object_name, etc. */
1095 if( pf_entry( p_module ) != 0 )
1097 /* With a well-written module we shouldn't have to print an
1098 * additional error message here, but just make sure. */
1099 msg_Err( p_this, "failed calling entry point in builtin module" );
1100 vlc_object_destroy( p_module );
1104 /* Everything worked fine ! The module is ready to be added to the list. */
1105 p_module->b_builtin = VLC_TRUE;
1107 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1108 p_module->psz_object_name, p_module->psz_longname ); */
1110 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1115 /*****************************************************************************
1116 * DeleteModule: delete a module and its structure.
1117 *****************************************************************************
1118 * This function can only be called if the module isn't being used.
1119 *****************************************************************************/
1120 static int DeleteModule( module_t * p_module )
1122 vlc_object_detach( p_module );
1124 /* We free the structures that we strdup()ed in Allocate*Module(). */
1125 #ifdef HAVE_DYNAMIC_PLUGINS
1126 if( !p_module->b_builtin )
1128 if( p_module->b_loaded && p_module->b_unloadable )
1130 CloseModule( p_module->handle );
1132 UndupModule( p_module );
1133 free( p_module->psz_filename );
1134 free( p_module->psz_longname );
1138 /* Free and detach the object's children */
1139 while( p_module->i_children )
1141 vlc_object_t *p_this = p_module->pp_children[0];
1142 vlc_object_detach( p_this );
1143 vlc_object_destroy( p_this );
1146 config_Free( p_module );
1147 vlc_object_destroy( p_module );
1152 #ifdef HAVE_DYNAMIC_PLUGINS
1153 /*****************************************************************************
1154 * CallEntry: call an entry point.
1155 *****************************************************************************
1156 * This function calls a symbol given its name and a module structure. The
1157 * symbol MUST refer to a function returning int and taking a module_t* as
1159 *****************************************************************************/
1160 static int CallEntry( module_t * p_module )
1162 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1163 int (* pf_symbol) ( module_t * p_module );
1165 /* Try to resolve the symbol */
1166 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1168 if( pf_symbol == NULL )
1170 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1171 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1172 psz_name, p_module->psz_filename );
1173 #elif defined(HAVE_DL_WINDOWS)
1174 char *psz_error = GetWindowsError();
1175 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1176 psz_name, p_module->psz_filename, psz_error );
1178 #elif defined(HAVE_DL_DLOPEN)
1179 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1180 psz_name, p_module->psz_filename, dlerror() );
1181 #elif defined(HAVE_DL_SHL_LOAD)
1182 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1183 psz_name, p_module->psz_filename, strerror(errno) );
1185 # error "Something is wrong in modules.c"
1190 /* We can now try to call the symbol */
1191 if( pf_symbol( p_module ) != 0 )
1193 /* With a well-written module we shouldn't have to print an
1194 * additional error message here, but just make sure. */
1195 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1196 psz_name, p_module->psz_filename );
1200 /* Everything worked fine, we can return */
1204 /*****************************************************************************
1205 * LoadModule: loads a dynamic library
1206 *****************************************************************************
1207 * This function loads a dynamically linked library using a system dependant
1208 * method. Will return 0 on success as well as the module handle.
1209 *****************************************************************************/
1210 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1211 module_handle_t *p_handle )
1213 module_handle_t handle;
1215 #if defined(HAVE_DL_DYLD)
1216 NSObjectFileImage image;
1217 NSObjectFileImageReturnCode ret;
1219 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1221 if( ret != NSObjectFileImageSuccess )
1223 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1227 /* Open the dynamic module */
1228 handle = NSLinkModule( image, psz_file,
1229 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1233 NSLinkEditErrors errors;
1234 const char *psz_file, *psz_err;
1236 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1237 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1238 NSDestroyObjectFileImage( image );
1242 /* Destroy our image, we won't need it */
1243 NSDestroyObjectFileImage( image );
1245 #elif defined(HAVE_DL_BEOS)
1246 handle = load_add_on( psz_file );
1249 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1253 #elif defined(HAVE_DL_WINDOWS)
1254 handle = LoadLibrary( psz_file );
1255 if( handle == NULL )
1257 char *psz_err = GetWindowsError();
1258 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1262 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1263 /* static is OK, we are called atomically */
1264 static vlc_bool_t b_kde = VLC_FALSE;
1266 # if defined(SYS_LINUX)
1267 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1268 * are going to get namespace collisions when two modules have common
1269 * public symbols, but ALSA is being a pest here. */
1270 if( strstr( psz_file, "alsa_plugin" ) )
1272 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1273 if( handle == NULL )
1275 msg_Warn( p_this, "cannot load module `%s' (%s)",
1276 psz_file, dlerror() );
1281 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1282 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1283 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1284 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1285 if( !b_kde && !strstr( psz_file, "kde" ) )
1287 dlopen( "libstdc++.so.6", RTLD_NOW )
1288 || dlopen( "libstdc++.so.5", RTLD_NOW )
1289 || dlopen( "libstdc++.so.4", RTLD_NOW )
1290 || dlopen( "libstdc++.so.3", RTLD_NOW );
1294 handle = dlopen( psz_file, RTLD_NOW );
1295 if( handle == NULL )
1297 msg_Warn( p_this, "cannot load module `%s' (%s)",
1298 psz_file, dlerror() );
1302 #elif defined(HAVE_DL_DLOPEN)
1303 # if defined(DL_LAZY)
1304 handle = dlopen( psz_file, DL_LAZY );
1306 handle = dlopen( psz_file, 0 );
1308 if( handle == NULL )
1310 msg_Warn( p_this, "cannot load module `%s' (%s)",
1311 psz_file, dlerror() );
1315 #elif defined(HAVE_DL_SHL_LOAD)
1316 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1317 if( handle == NULL )
1319 msg_Warn( p_this, "cannot load module `%s' (%s)",
1320 psz_file, strerror(errno) );
1325 # error "Something is wrong in modules.c"
1333 /*****************************************************************************
1334 * CloseModule: unload a dynamic library
1335 *****************************************************************************
1336 * This function unloads a previously opened dynamically linked library
1337 * using a system dependant method. No return value is taken in consideration,
1338 * since some libraries sometimes refuse to close properly.
1339 *****************************************************************************/
1340 static void CloseModule( module_handle_t handle )
1342 #if defined(HAVE_DL_DYLD)
1343 NSUnLinkModule( handle, FALSE );
1345 #elif defined(HAVE_DL_BEOS)
1346 unload_add_on( handle );
1348 #elif defined(HAVE_DL_WINDOWS)
1349 FreeLibrary( handle );
1351 #elif defined(HAVE_DL_DLOPEN)
1354 #elif defined(HAVE_DL_SHL_LOAD)
1355 shl_unload( handle );
1361 /*****************************************************************************
1362 * GetSymbol: get a symbol from a dynamic library
1363 *****************************************************************************
1364 * This function queries a loaded library for a symbol specified in a
1365 * string, and returns a pointer to it. We don't check for dlerror() or
1366 * similar functions, since we want a non-NULL symbol anyway.
1367 *****************************************************************************/
1368 static void * _module_getsymbol( module_handle_t, const char * );
1370 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1372 void * p_symbol = _module_getsymbol( handle, psz_function );
1374 /* MacOS X dl library expects symbols to begin with "_". So do
1375 * some other operating systems. That's really lame, but hey, what
1377 if( p_symbol == NULL )
1379 char *psz_call = malloc( strlen( psz_function ) + 2 );
1381 strcpy( psz_call + 1, psz_function );
1382 psz_call[ 0 ] = '_';
1383 p_symbol = _module_getsymbol( handle, psz_call );
1390 static void * _module_getsymbol( module_handle_t handle,
1391 const char * psz_function )
1393 #if defined(HAVE_DL_DYLD)
1394 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1395 return NSAddressOfSymbol( sym );
1397 #elif defined(HAVE_DL_BEOS)
1399 if( B_OK == get_image_symbol( handle, psz_function,
1400 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1409 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1410 wchar_t psz_real[256];
1411 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1413 return (void *)GetProcAddress( handle, psz_real );
1415 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1416 return (void *)GetProcAddress( handle, (char *)psz_function );
1418 #elif defined(HAVE_DL_DLOPEN)
1419 return dlsym( handle, psz_function );
1421 #elif defined(HAVE_DL_SHL_LOAD)
1423 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1429 #if defined(HAVE_DL_WINDOWS)
1430 static char * GetWindowsError( void )
1432 #if defined(UNDER_CE)
1433 wchar_t psz_tmp[256];
1434 char * psz_buffer = malloc( 256 );
1436 char * psz_tmp = malloc( 256 );
1438 int i = 0, i_error = GetLastError();
1440 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1441 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1442 (LPTSTR) psz_tmp, 256, NULL );
1444 /* Go to the end of the string */
1445 #if defined(UNDER_CE)
1446 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1448 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1456 #if defined(UNDER_CE)
1457 swprintf( psz_tmp + i, L" (error %i)", i_error );
1458 psz_tmp[ 255 ] = L'\0';
1460 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1461 psz_tmp[ 255 ] = '\0';
1465 #if defined(UNDER_CE)
1466 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1467 psz_buffer, 256, NULL, NULL );
1473 #endif /* HAVE_DL_WINDOWS */
1475 /*****************************************************************************
1476 * LoadPluginsCache: loads the plugins cache file
1477 *****************************************************************************
1478 * This function will load the plugin cache if present and valid. This cache
1479 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1480 * actually load the dynamically loadable module.
1481 * This allows us to only fully load plugins when they are actually used.
1482 *****************************************************************************/
1483 static void CacheLoad( vlc_object_t *p_this )
1485 char *psz_filename, *psz_homedir;
1487 int i, j, i_size, i_read;
1488 char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1490 module_cache_t **pp_cache = 0;
1491 int32_t i_file_size;
1493 psz_homedir = p_this->p_vlc->psz_homedir;
1496 msg_Err( p_this, "psz_homedir is null" );
1500 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1501 strlen(psz_homedir) );
1504 sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1509 msg_Err( p_this, "out of memory" );
1513 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1515 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1516 unlink( psz_filename );
1520 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1522 file = fopen( psz_filename, "r" );
1525 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1527 free( psz_filename );
1530 free( psz_filename );
1532 /* Check the file size */
1533 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1534 if( i_read != sizeof(i_file_size) )
1536 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1542 fseek( file, 0, SEEK_END );
1543 if( ftell( file ) != i_file_size )
1545 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1546 "(corrupted size)" );
1550 fseek( file, sizeof(i_file_size), SEEK_SET );
1552 /* Check the file is a plugins cache */
1553 i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1554 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1555 if( i_read != i_size ||
1556 memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1558 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1563 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1564 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1565 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1566 malloc( i_cache * sizeof(void *) );
1568 #define LOAD_IMMEDIATE(a) \
1569 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1570 #define LOAD_STRING(a) \
1571 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1572 != sizeof(i_size) ) goto error; \
1574 a = malloc( i_size ); \
1575 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1581 for( i = 0; i < i_cache; i++ )
1586 pp_cache[i] = malloc( sizeof(module_cache_t) );
1587 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1589 /* Save common info */
1590 LOAD_STRING( pp_cache[i]->psz_file );
1591 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1592 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1593 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1595 if( pp_cache[i]->b_junk ) continue;
1597 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1599 /* Save additional infos */
1600 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1601 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1602 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1603 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1604 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1606 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1608 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1609 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1610 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1611 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1612 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1613 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1616 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1619 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1621 LOAD_IMMEDIATE( i_submodules );
1623 while( i_submodules-- )
1625 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1626 vlc_object_attach( p_module, pp_cache[i]->p_module );
1627 p_module->b_submodule = VLC_TRUE;
1629 LOAD_STRING( p_module->psz_object_name );
1630 LOAD_STRING( p_module->psz_shortname );
1631 LOAD_STRING( p_module->psz_longname );
1632 LOAD_STRING( p_module->psz_program );
1633 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1635 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1637 LOAD_STRING( p_module->psz_capability );
1638 LOAD_IMMEDIATE( p_module->i_score );
1639 LOAD_IMMEDIATE( p_module->i_cpu );
1640 LOAD_IMMEDIATE( p_module->b_unloadable );
1641 LOAD_IMMEDIATE( p_module->b_reentrant );
1650 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1653 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1659 int CacheLoadConfig( module_t *p_module, FILE *file )
1664 /* Calculate the structure length */
1665 LOAD_IMMEDIATE( p_module->i_config_items );
1666 LOAD_IMMEDIATE( p_module->i_bool_items );
1668 LOAD_IMMEDIATE( i_lines );
1670 /* Allocate memory */
1671 p_module->p_config =
1672 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1673 if( p_module->p_config == NULL )
1675 msg_Err( p_module, "config error: can't duplicate p_config" );
1679 /* Do the duplication job */
1680 for( i = 0; i < i_lines ; i++ )
1682 LOAD_IMMEDIATE( p_module->p_config[i] );
1684 LOAD_STRING( p_module->p_config[i].psz_type );
1685 LOAD_STRING( p_module->p_config[i].psz_name );
1686 LOAD_STRING( p_module->p_config[i].psz_text );
1687 LOAD_STRING( p_module->p_config[i].psz_longtext );
1688 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1690 p_module->p_config[i].psz_value =
1691 p_module->p_config[i].psz_value_orig ?
1692 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1693 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1694 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1696 p_module->p_config[i].p_lock = &p_module->object_lock;
1698 if( p_module->p_config[i].i_list )
1700 if( p_module->p_config[i].ppsz_list )
1702 p_module->p_config[i].ppsz_list =
1703 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1704 if( p_module->p_config[i].ppsz_list )
1706 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1707 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1708 p_module->p_config[i].ppsz_list[j] = NULL;
1711 if( p_module->p_config[i].ppsz_list_text )
1713 p_module->p_config[i].ppsz_list_text =
1714 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1715 if( p_module->p_config[i].ppsz_list_text )
1717 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1718 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1719 p_module->p_config[i].ppsz_list_text[j] = NULL;
1722 if( p_module->p_config[i].pi_list )
1724 p_module->p_config[i].pi_list =
1725 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1726 if( p_module->p_config[i].pi_list )
1728 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1729 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1734 if( p_module->p_config[i].i_action )
1736 p_module->p_config[i].ppf_action =
1737 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1738 p_module->p_config[i].ppsz_action_text =
1739 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1741 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1743 p_module->p_config[i].ppf_action[j] = 0;
1744 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1748 p_module->p_config[i].pf_callback = 0;
1751 p_module->p_config[i].i_type = CONFIG_HINT_END;
1757 return VLC_EGENERIC;
1760 /*****************************************************************************
1761 * SavePluginsCache: saves the plugins cache to a file
1762 *****************************************************************************/
1763 static void CacheSave( vlc_object_t *p_this )
1765 char *psz_filename, *psz_homedir;
1768 module_cache_t **pp_cache;
1769 int32_t i_file_size = 0;
1771 psz_homedir = p_this->p_vlc->psz_homedir;
1774 msg_Err( p_this, "psz_homedir is null" );
1778 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1779 strlen(psz_homedir) );
1782 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1786 msg_Err( p_this, "out of memory" );
1790 config_CreateDir( p_this, psz_filename );
1792 strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1794 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1796 file = fopen( psz_filename, "w" );
1799 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1801 free( psz_filename );
1804 free( psz_filename );
1806 /* Empty space for file size */
1807 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1809 /* Contains version number */
1810 fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1812 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1813 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1815 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1817 #define SAVE_IMMEDIATE(a) \
1818 fwrite( &a, sizeof(char), sizeof(a), file )
1819 #define SAVE_STRING(a) \
1820 { i_size = a ? strlen( a ) + 1 : 0; \
1821 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1822 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1825 for( i = 0; i < i_cache; i++ )
1828 int32_t i_submodule;
1830 /* Save common info */
1831 SAVE_STRING( pp_cache[i]->psz_file );
1832 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1833 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1834 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1836 if( pp_cache[i]->b_junk ) continue;
1838 /* Save additional infos */
1839 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1840 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1841 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1842 SAVE_STRING( pp_cache[i]->p_module->psz_program );
1843 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1845 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1847 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1848 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1849 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1850 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1851 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1852 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1855 CacheSaveConfig( pp_cache[i]->p_module, file );
1857 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1859 i_submodule = pp_cache[i]->p_module->i_children;
1860 SAVE_IMMEDIATE( i_submodule );
1861 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1864 module_t *p_module =
1865 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1867 SAVE_STRING( p_module->psz_object_name );
1868 SAVE_STRING( p_module->psz_shortname );
1869 SAVE_STRING( p_module->psz_longname );
1870 SAVE_STRING( p_module->psz_program );
1871 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1873 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1875 SAVE_STRING( p_module->psz_capability );
1876 SAVE_IMMEDIATE( p_module->i_score );
1877 SAVE_IMMEDIATE( p_module->i_cpu );
1878 SAVE_IMMEDIATE( p_module->b_unloadable );
1879 SAVE_IMMEDIATE( p_module->b_reentrant );
1883 /* Fill-up file size */
1884 i_file_size = ftell( file );
1885 fseek( file, 0, SEEK_SET );
1886 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1893 void CacheSaveConfig( module_t *p_module, FILE *file )
1895 int i, j, i_lines = 0;
1896 module_config_t *p_item;
1899 SAVE_IMMEDIATE( p_module->i_config_items );
1900 SAVE_IMMEDIATE( p_module->i_bool_items );
1902 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1903 p_item++ ) i_lines++;
1905 SAVE_IMMEDIATE( i_lines );
1907 for( i = 0; i < i_lines ; i++ )
1909 SAVE_IMMEDIATE( p_module->p_config[i] );
1911 SAVE_STRING( p_module->p_config[i].psz_type );
1912 SAVE_STRING( p_module->p_config[i].psz_name );
1913 SAVE_STRING( p_module->p_config[i].psz_text );
1914 SAVE_STRING( p_module->p_config[i].psz_longtext );
1915 SAVE_STRING( p_module->p_config[i].psz_value_orig );
1917 if( p_module->p_config[i].i_list )
1919 if( p_module->p_config[i].ppsz_list )
1921 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1922 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1925 if( p_module->p_config[i].ppsz_list_text )
1927 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1928 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1930 if( p_module->p_config[i].pi_list )
1932 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1933 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1937 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1938 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1942 /*****************************************************************************
1943 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1944 *****************************************************************************/
1945 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1946 module_t *p_module )
1950 p_cache->pf_activate = p_module->pf_activate;
1951 p_cache->pf_deactivate = p_module->pf_deactivate;
1952 p_cache->p_symbols = p_module->p_symbols;
1953 p_cache->handle = p_module->handle;
1955 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1957 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1958 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1959 p_cchild->pf_activate = p_child->pf_activate;
1960 p_cchild->pf_deactivate = p_child->pf_deactivate;
1961 p_cchild->p_symbols = p_child->p_symbols;
1964 p_cache->b_loaded = VLC_TRUE;
1965 p_module->b_loaded = VLC_FALSE;
1968 /*****************************************************************************
1969 * FindPluginCache: finds the cache entry corresponding to a file
1970 *****************************************************************************/
1971 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
1972 int64_t i_time, int64_t i_size )
1974 module_cache_t **pp_cache;
1977 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
1978 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
1980 for( i = 0; i < i_cache; i++ )
1982 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
1983 pp_cache[i]->i_time == i_time &&
1984 pp_cache[i]->i_size == i_size ) return pp_cache[i];
1990 #endif /* HAVE_DYNAMIC_PLUGINS */