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;
332 module_list_t *p_next;
335 module_list_t *p_list, *p_first, *p_tmp;
338 int i_which_module, i_index = 0;
339 vlc_bool_t b_intf = VLC_FALSE;
344 char *psz_shortcuts = NULL, *psz_var = NULL;
346 msg_Dbg( p_this, "looking for %s module", psz_capability );
348 /* Deal with variables */
349 if( psz_name && psz_name[0] == '$' )
352 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
353 var_Get( p_this, psz_name + 1, &val );
354 psz_var = val.psz_string;
358 /* Count how many different shortcuts were asked for */
359 if( psz_name && *psz_name )
361 char *psz_parser, *psz_last_shortcut;
363 /* If the user wants none, give him none. */
364 if( !strcmp( psz_name, "none" ) )
366 if( psz_var ) free( psz_var );
371 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
373 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
375 if( *psz_parser == ',' )
379 psz_last_shortcut = psz_parser + 1;
383 /* Check if the user wants to override the "strict" mode */
384 if( psz_last_shortcut )
386 if( !strcmp(psz_last_shortcut, "none") )
391 else if( !strcmp(psz_last_shortcut, "any") )
393 b_strict = VLC_FALSE;
399 /* Sort the modules and test them */
400 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
401 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
404 /* Parse the module list for capabilities and probe each of them */
405 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
407 int i_shortcut_bonus = 0;
409 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
411 /* Test that this module can do what we need */
412 if( strcmp( p_module->psz_capability, psz_capability ) )
414 /* Don't recurse through the sub-modules because vlc_list_find()
415 * will list them anyway. */
419 /* Test if we have the required CPU */
420 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
425 /* If we required a shortcut, check this plugin provides it. */
426 if( i_shortcuts > 0 )
429 int i_dummy, i_short = i_shortcuts;
430 char *psz_name = psz_shortcuts;
432 /* Let's drop modules with a 0 score (unless they are
433 * explicitly requested) */
434 b_trash = !p_module->i_score;
438 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
440 if( !strcasecmp( psz_name,
441 p_module->pp_shortcuts[i_dummy] ) )
445 i_shortcut_bonus = i_short * 10000;
450 if( i_shortcut_bonus )
452 /* We found it... remember ? */
456 /* Go to the next shortcut... This is so lame! */
465 /* If we are in "strict" mode and we couldn't
466 * find the module in the list of provided shortcuts,
467 * then kick the bastard out of here!!! */
468 if( i_short == 0 && b_strict )
478 /* If we didn't require a shortcut, trash zero-scored plugins */
479 else if( !p_module->i_score )
484 /* Special case: test if we requested a particular intf plugin */
485 if( !i_shortcuts && p_module->psz_program
486 && !strcmp( p_module->psz_program,
487 p_this->p_vlc->psz_object_name ) )
491 /* Remove previous non-matching plugins */
498 /* This one doesn't match */
502 /* Store this new module */
503 p_list[ i_index ].p_module = p_module;
504 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
505 p_list[ i_index ].b_force = !!i_shortcut_bonus;
507 /* Add it to the modules-to-probe list */
510 p_list[ 0 ].p_next = NULL;
515 /* Ok, so at school you learned that quicksort is quick, and
516 * bubble sort sucks raw eggs. But that's when dealing with
517 * thousands of items. Here we have barely 50. */
518 module_list_t *p_newlist = p_first;
520 if( p_first->i_score < p_list[ i_index ].i_score )
522 p_list[ i_index ].p_next = p_first;
523 p_first = &p_list[ i_index ];
527 while( p_newlist->p_next != NULL &&
528 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
530 p_newlist = p_newlist->p_next;
533 p_list[ i_index ].p_next = p_newlist->p_next;
534 p_newlist->p_next = &p_list[ i_index ];
541 msg_Dbg( p_this, "probing %i candidate%s",
542 i_index, i_index == 1 ? "" : "s" );
544 /* Lock all candidate modules */
546 while( p_tmp != NULL )
548 vlc_object_yield( p_tmp->p_module );
549 p_tmp = p_tmp->p_next;
552 /* We can release the list, interesting modules were yielded */
553 vlc_list_release( p_all );
555 /* Parse the linked list and use the first successful module */
557 while( p_tmp != NULL )
559 #ifdef HAVE_DYNAMIC_PLUGINS
560 /* Make sure the module is loaded in mem */
561 module_t *p_module = p_tmp->p_module->b_submodule ?
562 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
563 if( !p_module->b_builtin && !p_module->b_loaded )
565 module_t *p_new_module =
566 AllocatePlugin( p_this, p_module->psz_filename );
569 CacheMerge( p_this, p_module, p_new_module );
570 vlc_object_attach( p_new_module, p_module );
571 DeleteModule( p_new_module );
576 p_this->b_force = p_tmp->b_force;
577 if( p_tmp->p_module->pf_activate
578 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
583 vlc_object_release( p_tmp->p_module );
584 p_tmp = p_tmp->p_next;
587 /* Store the locked module value */
590 p_module = p_tmp->p_module;
591 p_tmp = p_tmp->p_next;
598 /* Unlock the remaining modules */
599 while( p_tmp != NULL )
601 vlc_object_release( p_tmp->p_module );
602 p_tmp = p_tmp->p_next;
606 p_this->b_force = VLC_FALSE;
608 if( p_module != NULL )
610 msg_Dbg( p_module, "using %s module \"%s\"",
611 psz_capability, p_module->psz_object_name );
613 else if( p_first == NULL )
615 msg_Err( p_this, "no %s module matched \"%s\"",
616 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
618 else if( psz_name != NULL && *psz_name )
620 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
621 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
626 free( psz_shortcuts );
634 /* Don't forget that the module is still locked */
638 /*****************************************************************************
639 * module_Unneed: decrease the usage count of a module.
640 *****************************************************************************
641 * This function must be called by the thread that called module_Need, to
642 * decrease the reference count and allow for hiding of modules.
643 *****************************************************************************/
644 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
646 /* Use the close method */
647 if( p_module->pf_deactivate )
649 p_module->pf_deactivate( p_this );
652 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
654 vlc_object_release( p_module );
659 /*****************************************************************************
660 * Following functions are local.
661 *****************************************************************************/
663 /*****************************************************************************
664 * AllocateAllPlugins: load all plugin modules we can find.
665 *****************************************************************************/
666 #ifdef HAVE_DYNAMIC_PLUGINS
667 static void AllocateAllPlugins( vlc_object_t *p_this )
669 /* Yes, there are two NULLs because we replace one with "plugin-path". */
670 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
673 char ** ppsz_path = path;
676 /* If the user provided a plugin path, we add it to the list */
677 path[ sizeof(path)/sizeof(char*) - 2 ] =
678 config_GetPsz( p_this, "plugin-path" );
680 for( ; *ppsz_path != NULL ; ppsz_path++ )
682 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
683 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
685 /* Handle relative as well as absolute paths */
687 if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
689 if( (*ppsz_path)[0] != '/' )
692 int i_dirlen = strlen( *ppsz_path );
693 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
695 psz_fullpath = malloc( i_dirlen );
696 if( psz_fullpath == NULL )
701 sprintf( psz_fullpath, "%s\\%s",
702 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
704 sprintf( psz_fullpath, "%s/%s",
705 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
711 psz_fullpath = strdup( *ppsz_path );
714 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
716 /* Don't go deeper than 5 subdirectories */
717 AllocatePluginDir( p_this, psz_fullpath, 5 );
719 free( psz_fullpath );
722 /* Free plugin-path */
723 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
724 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
725 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
728 /*****************************************************************************
729 * AllocatePluginDir: recursively parse a directory to look for plugins
730 *****************************************************************************/
731 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
734 #if defined( UNDER_CE ) || defined( _MSC_VER )
736 wchar_t psz_path[MAX_PATH + 256];
737 wchar_t psz_wdir[MAX_PATH];
739 char psz_path[MAX_PATH + 256];
741 WIN32_FIND_DATA finddata;
747 struct dirent * file;
751 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
756 #if defined( UNDER_CE ) || defined( _MSC_VER )
757 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
759 rc = GetFileAttributes( psz_wdir );
760 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
762 /* Not a directory */
766 /* Parse all files in the directory */
768 swprintf( psz_path, L"%s\\*.*", psz_dir );
770 sprintf( psz_path, "%s\\*.*", psz_dir );
772 handle = FindFirstFile( psz_path, &finddata );
773 if( handle == INVALID_HANDLE_VALUE )
775 /* Empty directory */
779 /* Parse the directory and try to load all files it contains. */
783 unsigned int i_len = wcslen( finddata.cFileName );
784 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
786 unsigned int i_len = strlen( finddata.cFileName );
787 /* Skip ".", ".." and anything starting with "." */
788 if( !*finddata.cFileName || *finddata.cFileName == '.' )
790 if( !FindNextFile( handle, &finddata ) ) break;
793 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
796 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
798 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
800 else if( i_len > strlen( LIBEXT )
804 /* We only load files ending with LIBEXT */
805 && !strncasecmp( psz_path + strlen( psz_path)
807 LIBEXT, strlen( LIBEXT ) ) )
811 char psz_filename[MAX_PATH];
812 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_path, -1,
813 psz_filename, MAX_PATH, NULL, NULL );
814 psz_file = psz_filename;
819 AllocatePluginFile( p_this, psz_file, 0, 0 );
822 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
824 /* Close the directory */
828 dir = opendir( psz_dir );
834 i_dirlen = strlen( psz_dir );
836 /* Parse the directory and try to load all files it contains. */
837 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
843 /* Skip ".", ".." and anything starting with "." */
844 if( !*file->d_name || *file->d_name == '.' )
849 i_len = strlen( file->d_name );
850 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
852 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
854 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
857 i_stat = stat( psz_file, &statbuf );
858 if( !i_stat && statbuf.st_mode & S_IFDIR )
860 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
862 else if( i_len > strlen( LIBEXT )
863 /* We only load files ending with LIBEXT */
864 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
865 LIBEXT, strlen( LIBEXT ) ) )
867 int64_t i_time = 0, i_size = 0;
871 i_time = statbuf.st_mtime;
872 i_size = statbuf.st_size;
875 AllocatePluginFile( p_this, psz_file, i_time, i_size );
881 /* Close the directory */
887 /*****************************************************************************
888 * AllocatePluginFile: load a module into memory and initialize it.
889 *****************************************************************************
890 * This function loads a dynamically loadable module and allocates a structure
891 * for its information data. The module can then be handled by module_Need
892 * and module_Unneed. It can be removed by DeleteModule.
893 *****************************************************************************/
894 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
895 int64_t i_file_time, int64_t i_file_size )
898 module_cache_t *p_cache_entry = NULL;
901 * Check our plugins cache first then load plugin if needed
904 CacheFind( p_this, psz_file, i_file_time, i_file_size );
908 p_module = AllocatePlugin( p_this, psz_file );
912 /* If junk dll, don't try to load it */
913 if( p_cache_entry->b_junk )
919 module_config_t *p_item;
921 p_module = p_cache_entry->p_module;
922 p_module->b_loaded = VLC_FALSE;
924 /* For now we force loading if the module's config contains
925 * callbacks or actions.
926 * Could be optimized by adding an API call.*/
927 for( p_item = p_module->p_config;
928 p_item->i_type != CONFIG_HINT_END; p_item++ )
930 if( p_item->pf_callback || p_item->i_action )
931 p_module = AllocatePlugin( p_this, psz_file );
938 /* Everything worked fine !
939 * The module is ready to be added to the list. */
940 p_module->b_builtin = VLC_FALSE;
942 /* msg_Dbg( p_this, "plugin \"%s\", %s",
943 p_module->psz_object_name, p_module->psz_longname ); */
945 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
948 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
950 /* Add entry to cache */
951 #define p_bank p_this->p_libvlc->p_module_bank
953 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
954 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
955 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
956 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
957 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
958 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
959 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
962 return p_module ? 0 : -1;
965 /*****************************************************************************
966 * AllocatePlugin: load a module into memory and initialize it.
967 *****************************************************************************
968 * This function loads a dynamically loadable module and allocates a structure
969 * for its information data. The module can then be handled by module_Need
970 * and module_Unneed. It can be removed by DeleteModule.
971 *****************************************************************************/
972 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
975 module_handle_t handle;
977 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
979 /* Now that we have successfully loaded the module, we can
980 * allocate a structure for it */
981 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
982 if( p_module == NULL )
984 msg_Err( p_this, "out of memory" );
985 CloseModule( handle );
989 /* We need to fill these since they may be needed by CallEntry() */
990 p_module->psz_filename = psz_file;
991 p_module->handle = handle;
992 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
993 p_module->b_loaded = VLC_TRUE;
995 /* Initialize the module: fill p_module, default config */
996 if( CallEntry( p_module ) != 0 )
998 /* We couldn't call module_init() */
999 vlc_object_destroy( p_module );
1000 CloseModule( handle );
1004 DupModule( p_module );
1005 p_module->psz_filename = strdup( p_module->psz_filename );
1006 p_module->psz_longname = strdup( p_module->psz_longname );
1008 /* Everything worked fine ! The module is ready to be added to the list. */
1009 p_module->b_builtin = VLC_FALSE;
1014 /*****************************************************************************
1015 * DupModule: make a plugin module standalone.
1016 *****************************************************************************
1017 * This function duplicates all strings in the module, so that the dynamic
1018 * object can be unloaded. It acts recursively on submodules.
1019 *****************************************************************************/
1020 static void DupModule( module_t *p_module )
1025 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1027 *pp_shortcut = strdup( *pp_shortcut );
1030 /* We strdup() these entries so that they are still valid when the
1031 * module is unloaded. */
1032 p_module->psz_object_name = strdup( p_module->psz_object_name );
1033 p_module->psz_capability = strdup( p_module->psz_capability );
1035 if( p_module->psz_program != NULL )
1037 p_module->psz_program = strdup( p_module->psz_program );
1040 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1042 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1046 /*****************************************************************************
1047 * UndupModule: free a duplicated module.
1048 *****************************************************************************
1049 * This function frees the allocations done in DupModule().
1050 *****************************************************************************/
1051 static void UndupModule( module_t *p_module )
1056 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1058 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1061 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1063 free( *pp_shortcut );
1066 free( p_module->psz_object_name );
1067 free( p_module->psz_capability );
1069 if( p_module->psz_program != NULL )
1071 free( p_module->psz_program );
1075 #endif /* HAVE_DYNAMIC_PLUGINS */
1077 /*****************************************************************************
1078 * AllocateBuiltinModule: initialize a builtin module.
1079 *****************************************************************************
1080 * This function registers a builtin module and allocates a structure
1081 * for its information data. The module can then be handled by module_Need
1082 * and module_Unneed. It can be removed by DeleteModule.
1083 *****************************************************************************/
1084 static int AllocateBuiltinModule( vlc_object_t * p_this,
1085 int ( *pf_entry ) ( module_t * ) )
1087 module_t * p_module;
1089 /* Now that we have successfully loaded the module, we can
1090 * allocate a structure for it */
1091 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1092 if( p_module == NULL )
1094 msg_Err( p_this, "out of memory" );
1098 /* Initialize the module : fill p_module->psz_object_name, etc. */
1099 if( pf_entry( p_module ) != 0 )
1101 /* With a well-written module we shouldn't have to print an
1102 * additional error message here, but just make sure. */
1103 msg_Err( p_this, "failed calling entry point in builtin module" );
1104 vlc_object_destroy( p_module );
1108 /* Everything worked fine ! The module is ready to be added to the list. */
1109 p_module->b_builtin = VLC_TRUE;
1111 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1112 p_module->psz_object_name, p_module->psz_longname ); */
1114 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1119 /*****************************************************************************
1120 * DeleteModule: delete a module and its structure.
1121 *****************************************************************************
1122 * This function can only be called if the module isn't being used.
1123 *****************************************************************************/
1124 static int DeleteModule( module_t * p_module )
1126 vlc_object_detach( p_module );
1128 /* We free the structures that we strdup()ed in Allocate*Module(). */
1129 #ifdef HAVE_DYNAMIC_PLUGINS
1130 if( !p_module->b_builtin )
1132 if( p_module->b_loaded && p_module->b_unloadable )
1134 CloseModule( p_module->handle );
1136 UndupModule( p_module );
1137 free( p_module->psz_filename );
1138 free( p_module->psz_longname );
1142 /* Free and detach the object's children */
1143 while( p_module->i_children )
1145 vlc_object_t *p_this = p_module->pp_children[0];
1146 vlc_object_detach( p_this );
1147 vlc_object_destroy( p_this );
1150 config_Free( p_module );
1151 vlc_object_destroy( p_module );
1156 #ifdef HAVE_DYNAMIC_PLUGINS
1157 /*****************************************************************************
1158 * CallEntry: call an entry point.
1159 *****************************************************************************
1160 * This function calls a symbol given its name and a module structure. The
1161 * symbol MUST refer to a function returning int and taking a module_t* as
1163 *****************************************************************************/
1164 static int CallEntry( module_t * p_module )
1166 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1167 int (* pf_symbol) ( module_t * p_module );
1169 /* Try to resolve the symbol */
1170 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1172 if( pf_symbol == NULL )
1174 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1175 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1176 psz_name, p_module->psz_filename );
1177 #elif defined(HAVE_DL_WINDOWS)
1178 char *psz_error = GetWindowsError();
1179 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1180 psz_name, p_module->psz_filename, psz_error );
1182 #elif defined(HAVE_DL_DLOPEN)
1183 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1184 psz_name, p_module->psz_filename, dlerror() );
1185 #elif defined(HAVE_DL_SHL_LOAD)
1186 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1187 psz_name, p_module->psz_filename, strerror(errno) );
1189 # error "Something is wrong in modules.c"
1194 /* We can now try to call the symbol */
1195 if( pf_symbol( p_module ) != 0 )
1197 /* With a well-written module we shouldn't have to print an
1198 * additional error message here, but just make sure. */
1199 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1200 psz_name, p_module->psz_filename );
1204 /* Everything worked fine, we can return */
1208 /*****************************************************************************
1209 * LoadModule: loads a dynamic library
1210 *****************************************************************************
1211 * This function loads a dynamically linked library using a system dependant
1212 * method. Will return 0 on success as well as the module handle.
1213 *****************************************************************************/
1214 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1215 module_handle_t *p_handle )
1217 module_handle_t handle;
1219 #if defined(HAVE_DL_DYLD)
1220 NSObjectFileImage image;
1221 NSObjectFileImageReturnCode ret;
1223 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1225 if( ret != NSObjectFileImageSuccess )
1227 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1231 /* Open the dynamic module */
1232 handle = NSLinkModule( image, psz_file,
1233 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1237 NSLinkEditErrors errors;
1238 const char *psz_file, *psz_err;
1240 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1241 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1242 NSDestroyObjectFileImage( image );
1246 /* Destroy our image, we won't need it */
1247 NSDestroyObjectFileImage( image );
1249 #elif defined(HAVE_DL_BEOS)
1250 handle = load_add_on( psz_file );
1253 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1257 #elif defined(HAVE_DL_WINDOWS)
1258 handle = LoadLibrary( psz_file );
1259 if( handle == NULL )
1261 char *psz_err = GetWindowsError();
1262 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1266 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1267 /* static is OK, we are called atomically */
1268 static vlc_bool_t b_kde = VLC_FALSE;
1270 # if defined(SYS_LINUX)
1271 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1272 * are going to get namespace collisions when two modules have common
1273 * public symbols, but ALSA is being a pest here. */
1274 if( strstr( psz_file, "alsa_plugin" ) )
1276 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1277 if( handle == NULL )
1279 msg_Warn( p_this, "cannot load module `%s' (%s)",
1280 psz_file, dlerror() );
1285 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1286 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1287 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1288 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1289 if( !b_kde && !strstr( psz_file, "kde" ) )
1291 dlopen( "libstdc++.so.6", RTLD_NOW )
1292 || dlopen( "libstdc++.so.5", RTLD_NOW )
1293 || dlopen( "libstdc++.so.4", RTLD_NOW )
1294 || dlopen( "libstdc++.so.3", RTLD_NOW );
1298 handle = dlopen( psz_file, RTLD_NOW );
1299 if( handle == NULL )
1301 msg_Warn( p_this, "cannot load module `%s' (%s)",
1302 psz_file, dlerror() );
1306 #elif defined(HAVE_DL_DLOPEN)
1307 # if defined(DL_LAZY)
1308 handle = dlopen( psz_file, DL_LAZY );
1310 handle = dlopen( psz_file, 0 );
1312 if( handle == NULL )
1314 msg_Warn( p_this, "cannot load module `%s' (%s)",
1315 psz_file, dlerror() );
1319 #elif defined(HAVE_DL_SHL_LOAD)
1320 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1321 if( handle == NULL )
1323 msg_Warn( p_this, "cannot load module `%s' (%s)",
1324 psz_file, strerror(errno) );
1329 # error "Something is wrong in modules.c"
1337 /*****************************************************************************
1338 * CloseModule: unload a dynamic library
1339 *****************************************************************************
1340 * This function unloads a previously opened dynamically linked library
1341 * using a system dependant method. No return value is taken in consideration,
1342 * since some libraries sometimes refuse to close properly.
1343 *****************************************************************************/
1344 static void CloseModule( module_handle_t handle )
1346 #if defined(HAVE_DL_DYLD)
1347 NSUnLinkModule( handle, FALSE );
1349 #elif defined(HAVE_DL_BEOS)
1350 unload_add_on( handle );
1352 #elif defined(HAVE_DL_WINDOWS)
1353 FreeLibrary( handle );
1355 #elif defined(HAVE_DL_DLOPEN)
1358 #elif defined(HAVE_DL_SHL_LOAD)
1359 shl_unload( handle );
1365 /*****************************************************************************
1366 * GetSymbol: get a symbol from a dynamic library
1367 *****************************************************************************
1368 * This function queries a loaded library for a symbol specified in a
1369 * string, and returns a pointer to it. We don't check for dlerror() or
1370 * similar functions, since we want a non-NULL symbol anyway.
1371 *****************************************************************************/
1372 static void * _module_getsymbol( module_handle_t, const char * );
1374 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1376 void * p_symbol = _module_getsymbol( handle, psz_function );
1378 /* MacOS X dl library expects symbols to begin with "_". So do
1379 * some other operating systems. That's really lame, but hey, what
1381 if( p_symbol == NULL )
1383 char *psz_call = malloc( strlen( psz_function ) + 2 );
1385 strcpy( psz_call + 1, psz_function );
1386 psz_call[ 0 ] = '_';
1387 p_symbol = _module_getsymbol( handle, psz_call );
1394 static void * _module_getsymbol( module_handle_t handle,
1395 const char * psz_function )
1397 #if defined(HAVE_DL_DYLD)
1398 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1399 return NSAddressOfSymbol( sym );
1401 #elif defined(HAVE_DL_BEOS)
1403 if( B_OK == get_image_symbol( handle, psz_function,
1404 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1413 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1414 wchar_t psz_real[256];
1415 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1417 return (void *)GetProcAddress( handle, psz_real );
1419 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1420 return (void *)GetProcAddress( handle, (char *)psz_function );
1422 #elif defined(HAVE_DL_DLOPEN)
1423 return dlsym( handle, psz_function );
1425 #elif defined(HAVE_DL_SHL_LOAD)
1427 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1433 #if defined(HAVE_DL_WINDOWS)
1434 static char * GetWindowsError( void )
1436 #if defined(UNDER_CE)
1437 wchar_t psz_tmp[256];
1438 char * psz_buffer = malloc( 256 );
1440 char * psz_tmp = malloc( 256 );
1442 int i = 0, i_error = GetLastError();
1444 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1445 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1446 (LPTSTR) psz_tmp, 256, NULL );
1448 /* Go to the end of the string */
1449 #if defined(UNDER_CE)
1450 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1452 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1460 #if defined(UNDER_CE)
1461 swprintf( psz_tmp + i, L" (error %i)", i_error );
1462 psz_tmp[ 255 ] = L'\0';
1464 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1465 psz_tmp[ 255 ] = '\0';
1469 #if defined(UNDER_CE)
1470 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1471 psz_buffer, 256, NULL, NULL );
1477 #endif /* HAVE_DL_WINDOWS */
1479 /*****************************************************************************
1480 * LoadPluginsCache: loads the plugins cache file
1481 *****************************************************************************
1482 * This function will load the plugin cache if present and valid. This cache
1483 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1484 * actually load the dynamically loadable module.
1485 * This allows us to only fully load plugins when they are actually used.
1486 *****************************************************************************/
1487 static void CacheLoad( vlc_object_t *p_this )
1489 char *psz_filename, *psz_homedir;
1491 int i, j, i_size, i_read;
1492 char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1494 module_cache_t **pp_cache = 0;
1495 int32_t i_file_size;
1497 psz_homedir = p_this->p_vlc->psz_homedir;
1500 msg_Err( p_this, "psz_homedir is null" );
1504 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1505 strlen(psz_homedir) );
1508 sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1513 msg_Err( p_this, "out of memory" );
1517 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1519 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1520 unlink( psz_filename );
1524 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1526 file = fopen( psz_filename, "r" );
1529 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1531 free( psz_filename );
1534 free( psz_filename );
1536 /* Check the file size */
1537 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1538 if( i_read != sizeof(i_file_size) )
1540 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1546 fseek( file, 0, SEEK_END );
1547 if( ftell( file ) != i_file_size )
1549 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1550 "(corrupted size)" );
1554 fseek( file, sizeof(i_file_size), SEEK_SET );
1556 /* Check the file is a plugins cache */
1557 i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1558 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1559 if( i_read != i_size ||
1560 memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1562 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1567 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1568 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1569 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1570 malloc( i_cache * sizeof(void *) );
1572 #define LOAD_IMMEDIATE(a) \
1573 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1574 #define LOAD_STRING(a) \
1575 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1576 != sizeof(i_size) ) goto error; \
1578 a = malloc( i_size ); \
1579 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1585 for( i = 0; i < i_cache; i++ )
1590 pp_cache[i] = malloc( sizeof(module_cache_t) );
1591 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1593 /* Save common info */
1594 LOAD_STRING( pp_cache[i]->psz_file );
1595 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1596 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1597 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1599 if( pp_cache[i]->b_junk ) continue;
1601 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1603 /* Save additional infos */
1604 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1605 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1606 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1607 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1608 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1610 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1612 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1613 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1614 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1615 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1616 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1617 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1620 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1623 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1625 LOAD_IMMEDIATE( i_submodules );
1627 while( i_submodules-- )
1629 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1630 vlc_object_attach( p_module, pp_cache[i]->p_module );
1631 p_module->b_submodule = VLC_TRUE;
1633 LOAD_STRING( p_module->psz_object_name );
1634 LOAD_STRING( p_module->psz_shortname );
1635 LOAD_STRING( p_module->psz_longname );
1636 LOAD_STRING( p_module->psz_program );
1637 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1639 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1641 LOAD_STRING( p_module->psz_capability );
1642 LOAD_IMMEDIATE( p_module->i_score );
1643 LOAD_IMMEDIATE( p_module->i_cpu );
1644 LOAD_IMMEDIATE( p_module->b_unloadable );
1645 LOAD_IMMEDIATE( p_module->b_reentrant );
1654 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1657 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1663 int CacheLoadConfig( module_t *p_module, FILE *file )
1668 /* Calculate the structure length */
1669 LOAD_IMMEDIATE( p_module->i_config_items );
1670 LOAD_IMMEDIATE( p_module->i_bool_items );
1672 LOAD_IMMEDIATE( i_lines );
1674 /* Allocate memory */
1675 p_module->p_config =
1676 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1677 if( p_module->p_config == NULL )
1679 msg_Err( p_module, "config error: can't duplicate p_config" );
1683 /* Do the duplication job */
1684 for( i = 0; i < i_lines ; i++ )
1686 LOAD_IMMEDIATE( p_module->p_config[i] );
1688 LOAD_STRING( p_module->p_config[i].psz_type );
1689 LOAD_STRING( p_module->p_config[i].psz_name );
1690 LOAD_STRING( p_module->p_config[i].psz_text );
1691 LOAD_STRING( p_module->p_config[i].psz_longtext );
1692 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1694 p_module->p_config[i].psz_value =
1695 p_module->p_config[i].psz_value_orig ?
1696 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1697 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1698 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1700 p_module->p_config[i].p_lock = &p_module->object_lock;
1702 if( p_module->p_config[i].i_list )
1704 if( p_module->p_config[i].ppsz_list )
1706 p_module->p_config[i].ppsz_list =
1707 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1708 if( p_module->p_config[i].ppsz_list )
1710 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1711 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1712 p_module->p_config[i].ppsz_list[j] = NULL;
1715 if( p_module->p_config[i].ppsz_list_text )
1717 p_module->p_config[i].ppsz_list_text =
1718 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1719 if( p_module->p_config[i].ppsz_list_text )
1721 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1722 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1723 p_module->p_config[i].ppsz_list_text[j] = NULL;
1726 if( p_module->p_config[i].pi_list )
1728 p_module->p_config[i].pi_list =
1729 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1730 if( p_module->p_config[i].pi_list )
1732 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1733 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1738 if( p_module->p_config[i].i_action )
1740 p_module->p_config[i].ppf_action =
1741 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1742 p_module->p_config[i].ppsz_action_text =
1743 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1745 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1747 p_module->p_config[i].ppf_action[j] = 0;
1748 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1752 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1755 p_module->p_config[i].i_type = CONFIG_HINT_END;
1761 return VLC_EGENERIC;
1764 /*****************************************************************************
1765 * SavePluginsCache: saves the plugins cache to a file
1766 *****************************************************************************/
1767 static void CacheSave( vlc_object_t *p_this )
1769 char *psz_filename, *psz_homedir;
1772 module_cache_t **pp_cache;
1773 int32_t i_file_size = 0;
1775 psz_homedir = p_this->p_vlc->psz_homedir;
1778 msg_Err( p_this, "psz_homedir is null" );
1782 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1783 strlen(psz_homedir) );
1786 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1790 msg_Err( p_this, "out of memory" );
1794 config_CreateDir( p_this, psz_filename );
1796 strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1798 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1800 file = fopen( psz_filename, "w" );
1803 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1805 free( psz_filename );
1808 free( psz_filename );
1810 /* Empty space for file size */
1811 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1813 /* Contains version number */
1814 fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1816 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1817 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1819 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1821 #define SAVE_IMMEDIATE(a) \
1822 fwrite( &a, sizeof(char), sizeof(a), file )
1823 #define SAVE_STRING(a) \
1824 { i_size = a ? strlen( a ) + 1 : 0; \
1825 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1826 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1829 for( i = 0; i < i_cache; i++ )
1832 int32_t i_submodule;
1834 /* Save common info */
1835 SAVE_STRING( pp_cache[i]->psz_file );
1836 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1837 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1838 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1840 if( pp_cache[i]->b_junk ) continue;
1842 /* Save additional infos */
1843 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1844 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1845 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1846 SAVE_STRING( pp_cache[i]->p_module->psz_program );
1847 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1849 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1851 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1852 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1853 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1854 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1855 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1856 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1859 CacheSaveConfig( pp_cache[i]->p_module, file );
1861 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1863 i_submodule = pp_cache[i]->p_module->i_children;
1864 SAVE_IMMEDIATE( i_submodule );
1865 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1868 module_t *p_module =
1869 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1871 SAVE_STRING( p_module->psz_object_name );
1872 SAVE_STRING( p_module->psz_shortname );
1873 SAVE_STRING( p_module->psz_longname );
1874 SAVE_STRING( p_module->psz_program );
1875 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1877 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1879 SAVE_STRING( p_module->psz_capability );
1880 SAVE_IMMEDIATE( p_module->i_score );
1881 SAVE_IMMEDIATE( p_module->i_cpu );
1882 SAVE_IMMEDIATE( p_module->b_unloadable );
1883 SAVE_IMMEDIATE( p_module->b_reentrant );
1887 /* Fill-up file size */
1888 i_file_size = ftell( file );
1889 fseek( file, 0, SEEK_SET );
1890 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1897 void CacheSaveConfig( module_t *p_module, FILE *file )
1899 int i, j, i_lines = 0;
1900 module_config_t *p_item;
1903 SAVE_IMMEDIATE( p_module->i_config_items );
1904 SAVE_IMMEDIATE( p_module->i_bool_items );
1906 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1907 p_item++ ) i_lines++;
1909 SAVE_IMMEDIATE( i_lines );
1911 for( i = 0; i < i_lines ; i++ )
1913 SAVE_IMMEDIATE( p_module->p_config[i] );
1915 SAVE_STRING( p_module->p_config[i].psz_type );
1916 SAVE_STRING( p_module->p_config[i].psz_name );
1917 SAVE_STRING( p_module->p_config[i].psz_text );
1918 SAVE_STRING( p_module->p_config[i].psz_longtext );
1919 SAVE_STRING( p_module->p_config[i].psz_value_orig );
1921 if( p_module->p_config[i].i_list )
1923 if( p_module->p_config[i].ppsz_list )
1925 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1926 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1929 if( p_module->p_config[i].ppsz_list_text )
1931 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1932 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1934 if( p_module->p_config[i].pi_list )
1936 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1937 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1941 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1942 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1944 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
1948 /*****************************************************************************
1949 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1950 *****************************************************************************/
1951 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1952 module_t *p_module )
1956 p_cache->pf_activate = p_module->pf_activate;
1957 p_cache->pf_deactivate = p_module->pf_deactivate;
1958 p_cache->p_symbols = p_module->p_symbols;
1959 p_cache->handle = p_module->handle;
1961 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1963 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1964 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1965 p_cchild->pf_activate = p_child->pf_activate;
1966 p_cchild->pf_deactivate = p_child->pf_deactivate;
1967 p_cchild->p_symbols = p_child->p_symbols;
1970 p_cache->b_loaded = VLC_TRUE;
1971 p_module->b_loaded = VLC_FALSE;
1974 /*****************************************************************************
1975 * FindPluginCache: finds the cache entry corresponding to a file
1976 *****************************************************************************/
1977 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
1978 int64_t i_time, int64_t i_size )
1980 module_cache_t **pp_cache;
1983 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
1984 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
1986 for( i = 0; i < i_cache; i++ )
1988 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
1989 pp_cache[i]->i_time == i_time &&
1990 pp_cache[i]->i_size == i_size ) return pp_cache[i];
1996 #endif /* HAVE_DYNAMIC_PLUGINS */