1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28 * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* sprintf() */
35 #include <string.h> /* strdup() */
38 #include <vlc/input.h>
42 #elif defined( UNDER_CE )
43 # include <windows.h> /* GetFileAttributes() */
45 # include "../extras/dirent.h"
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
51 #ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
58 #define HAVE_DYNAMIC_PLUGINS
59 #if defined(HAVE_DL_DYLD)
60 # if defined(HAVE_MACH_O_DYLD_H)
61 # include <mach-o/dyld.h>
63 #elif defined(HAVE_DL_BEOS)
64 # if defined(HAVE_IMAGE_H)
67 #elif defined(HAVE_DL_WINDOWS)
69 #elif defined(HAVE_DL_DLOPEN)
70 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
73 # if defined(HAVE_SYS_DL_H)
76 #elif defined(HAVE_DL_SHL_LOAD)
77 # if defined(HAVE_DL_H)
81 # undef HAVE_DYNAMIC_PLUGINS
84 #include "vlc_error.h"
86 #include "vlc_interface.h"
87 #include "intf_eject.h"
89 #include "vlc_playlist.h"
91 #include "vlc_video.h"
92 #include "video_output.h"
93 #include "vout_synchro.h"
96 #include "audio_output.h"
97 #include "aout_internal.h"
99 #include "stream_output.h"
101 #include "vlc_httpd.h"
103 #include "iso_lang.h"
106 #include "vlc_block.h"
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 # include "modules_plugin.h"
114 #if defined( UNDER_CE )
115 # include "modules_builtin_evc.h"
116 #elif defined( _MSC_VER )
117 # include "modules_builtin_msvc.h"
119 # include "modules_builtin.h"
123 #if defined( WIN32) || defined( UNDER_CE )
124 /* Avoid name collisions */
125 # define LoadModule(a,b,c) _LoadModule(a,b,c)
128 /*****************************************************************************
130 *****************************************************************************/
131 #ifdef HAVE_DYNAMIC_PLUGINS
132 static void AllocateAllPlugins ( vlc_object_t * );
133 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
134 static int AllocatePluginFile ( vlc_object_t *, char *, int64_t, int64_t );
135 static module_t * AllocatePlugin( vlc_object_t *, char * );
137 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
138 static int DeleteModule ( module_t * );
139 #ifdef HAVE_DYNAMIC_PLUGINS
140 static void DupModule ( module_t * );
141 static void UndupModule ( module_t * );
142 static int CallEntry ( module_t * );
143 static int LoadModule ( vlc_object_t *, char *, module_handle_t * );
144 static void CloseModule ( module_handle_t );
145 static void * GetSymbol ( module_handle_t, const char * );
146 static void CacheLoad ( vlc_object_t * );
147 static int CacheLoadConfig ( module_t *, FILE * );
148 static void CacheSave ( vlc_object_t * );
149 static void CacheSaveConfig ( module_t *, FILE * );
150 static void CacheMerge ( vlc_object_t *, module_t *, module_t * );
151 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
153 #if defined(HAVE_DL_WINDOWS)
154 static char * GetWindowsError ( void );
158 /*****************************************************************************
159 * module_InitBank: create the module bank.
160 *****************************************************************************
161 * This function creates a module bank structure which will be filled later
162 * on with all the modules found.
163 *****************************************************************************/
164 void __module_InitBank( vlc_object_t *p_this )
166 module_bank_t *p_bank;
169 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
170 var_Get( p_this->p_libvlc, "libvlc", &lockval );
171 vlc_mutex_lock( lockval.p_address );
172 if( p_this->p_libvlc->p_module_bank )
174 p_this->p_libvlc->p_module_bank->i_usage++;
175 vlc_mutex_unlock( lockval.p_address );
176 var_Destroy( p_this->p_libvlc, "libvlc" );
179 vlc_mutex_unlock( lockval.p_address );
180 var_Destroy( p_this->p_libvlc, "libvlc" );
182 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
183 p_bank->psz_object_name = "module bank";
185 p_bank->i_cache = p_bank->i_loaded_cache = 0;
186 p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
187 p_bank->b_cache = p_bank->b_cache_dirty =
188 p_bank->b_cache_delete = VLC_FALSE;
191 * Store the symbols to be exported
193 #ifdef HAVE_DYNAMIC_PLUGINS
194 STORE_SYMBOLS( &p_bank->symbols );
197 /* Everything worked, attach the object */
198 p_this->p_libvlc->p_module_bank = p_bank;
199 vlc_object_attach( p_bank, p_this->p_libvlc );
201 module_LoadMain( p_this );
206 /*****************************************************************************
207 * module_ResetBank: reset the module bank.
208 *****************************************************************************
209 * This function resets the module bank by unloading all unused plugin
211 *****************************************************************************/
212 void __module_ResetBank( vlc_object_t *p_this )
214 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
218 /*****************************************************************************
219 * module_EndBank: empty the module bank.
220 *****************************************************************************
221 * This function unloads all unused plugin modules and empties the module
222 * bank in case of success.
223 *****************************************************************************/
224 void __module_EndBank( vlc_object_t *p_this )
229 var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
230 var_Get( p_this->p_libvlc, "libvlc", &lockval );
231 vlc_mutex_lock( lockval.p_address );
232 if( !p_this->p_libvlc->p_module_bank )
234 vlc_mutex_unlock( lockval.p_address );
235 var_Destroy( p_this->p_libvlc, "libvlc" );
238 if( --p_this->p_libvlc->p_module_bank->i_usage )
240 vlc_mutex_unlock( lockval.p_address );
241 var_Destroy( p_this->p_libvlc, "libvlc" );
244 vlc_mutex_unlock( lockval.p_address );
245 var_Destroy( p_this->p_libvlc, "libvlc" );
247 #ifdef HAVE_DYNAMIC_PLUGINS
248 if( p_this->p_libvlc->p_module_bank->b_cache ) CacheSave( p_this );
251 vlc_object_detach( p_this->p_libvlc->p_module_bank );
253 while( p_this->p_libvlc->p_module_bank->i_children )
255 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
257 if( DeleteModule( p_next ) )
259 /* Module deletion failed */
260 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
261 p_next->psz_object_name );
263 /* We just free the module by hand. Niahahahahaha. */
264 vlc_object_detach( p_next );
265 vlc_object_destroy( p_next );
269 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
274 /*****************************************************************************
275 * module_LoadMain: load the main program info into the module bank.
276 *****************************************************************************
277 * This function fills the module bank structure with the main module infos.
278 * This is very useful as it will allow us to consider the main program just
279 * as another module, and for instance the configuration options of main will
280 * be available in the module bank structure just as for every other module.
281 *****************************************************************************/
282 void __module_LoadMain( vlc_object_t *p_this )
284 AllocateBuiltinModule( p_this, vlc_entry__main );
287 /*****************************************************************************
288 * module_LoadBuiltins: load all modules which we built with.
289 *****************************************************************************
290 * This function fills the module bank structure with the builtin modules.
291 *****************************************************************************/
292 void __module_LoadBuiltins( vlc_object_t * p_this )
294 msg_Dbg( p_this, "checking builtin modules" );
295 ALLOCATE_ALL_BUILTINS();
298 /*****************************************************************************
299 * module_LoadPlugins: load all plugin modules we can find.
300 *****************************************************************************
301 * This function fills the module bank structure with the plugin modules.
302 *****************************************************************************/
303 void __module_LoadPlugins( vlc_object_t * p_this )
305 #ifdef HAVE_DYNAMIC_PLUGINS
306 msg_Dbg( p_this, "checking plugin modules" );
308 if( config_GetInt( p_this, "plugins-cache" ) )
309 p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
311 if( p_this->p_libvlc->p_module_bank->b_cache ||
312 p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
314 AllocateAllPlugins( p_this );
318 /*****************************************************************************
319 * module_Need: return the best module function, given a capability list.
320 *****************************************************************************
321 * This function returns the module that best fits the asked capabilities.
322 *****************************************************************************/
323 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
324 const char *psz_name, vlc_bool_t b_strict )
326 typedef struct module_list_t module_list_t;
333 module_list_t *p_next;
336 module_list_t *p_list, *p_first, *p_tmp;
339 int i_which_module, i_index = 0;
340 vlc_bool_t b_intf = VLC_FALSE;
345 char *psz_shortcuts = NULL, *psz_var = NULL;
347 msg_Dbg( p_this, "looking for %s module", psz_capability );
349 /* Deal with variables */
350 if( psz_name && psz_name[0] == '$' )
353 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
354 var_Get( p_this, psz_name + 1, &val );
355 psz_var = val.psz_string;
359 /* Count how many different shortcuts were asked for */
360 if( psz_name && *psz_name )
362 char *psz_parser, *psz_last_shortcut;
364 /* If the user wants none, give him none. */
365 if( !strcmp( psz_name, "none" ) )
367 if( psz_var ) free( psz_var );
372 psz_shortcuts = psz_last_shortcut = strdup( psz_name );
374 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
376 if( *psz_parser == ',' )
380 psz_last_shortcut = psz_parser + 1;
384 /* Check if the user wants to override the "strict" mode */
385 if( psz_last_shortcut )
387 if( !strcmp(psz_last_shortcut, "none") )
392 else if( !strcmp(psz_last_shortcut, "any") )
394 b_strict = VLC_FALSE;
400 /* Sort the modules and test them */
401 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
402 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
405 /* Parse the module list for capabilities and probe each of them */
406 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
408 int i_shortcut_bonus = 0;
410 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
412 /* Test that this module can do what we need */
413 if( strcmp( p_module->psz_capability, psz_capability ) )
415 /* Don't recurse through the sub-modules because vlc_list_find()
416 * will list them anyway. */
420 /* Test if we have the required CPU */
421 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
426 /* If we required a shortcut, check this plugin provides it. */
427 if( i_shortcuts > 0 )
430 int i_dummy, i_short = i_shortcuts;
431 char *psz_name = psz_shortcuts;
433 /* Let's drop modules with a 0 score (unless they are
434 * explicitly requested) */
435 b_trash = !p_module->i_score;
439 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
441 if( !strcasecmp( psz_name,
442 p_module->pp_shortcuts[i_dummy] ) )
446 i_shortcut_bonus = i_short * 10000;
451 if( i_shortcut_bonus )
453 /* We found it... remember ? */
457 /* Go to the next shortcut... This is so lame! */
466 /* If we are in "strict" mode and we couldn't
467 * find the module in the list of provided shortcuts,
468 * then kick the bastard out of here!!! */
469 if( i_short == 0 && b_strict )
479 /* If we didn't require a shortcut, trash zero-scored plugins */
480 else if( !p_module->i_score )
485 /* Special case: test if we requested a particular intf plugin */
486 if( !i_shortcuts && p_module->psz_program
487 && !strcmp( p_module->psz_program,
488 p_this->p_vlc->psz_object_name ) )
492 /* Remove previous non-matching plugins */
499 /* This one doesn't match */
503 /* Store this new module */
504 p_list[ i_index ].p_module = p_module;
505 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
506 p_list[ i_index ].b_force = !!i_shortcut_bonus;
508 /* Add it to the modules-to-probe list */
511 p_list[ 0 ].p_next = NULL;
516 /* Ok, so at school you learned that quicksort is quick, and
517 * bubble sort sucks raw eggs. But that's when dealing with
518 * thousands of items. Here we have barely 50. */
519 module_list_t *p_newlist = p_first;
521 if( p_first->i_score < p_list[ i_index ].i_score )
523 p_list[ i_index ].p_next = p_first;
524 p_first = &p_list[ i_index ];
528 while( p_newlist->p_next != NULL &&
529 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
531 p_newlist = p_newlist->p_next;
534 p_list[ i_index ].p_next = p_newlist->p_next;
535 p_newlist->p_next = &p_list[ i_index ];
542 msg_Dbg( p_this, "probing %i candidate%s",
543 i_index, i_index == 1 ? "" : "s" );
545 /* Lock all candidate modules */
547 while( p_tmp != NULL )
549 vlc_object_yield( p_tmp->p_module );
550 p_tmp = p_tmp->p_next;
553 /* We can release the list, interesting modules were yielded */
554 vlc_list_release( p_all );
556 /* Parse the linked list and use the first successful module */
558 while( p_tmp != NULL )
560 #ifdef HAVE_DYNAMIC_PLUGINS
561 /* Make sure the module is loaded in mem */
562 module_t *p_module = p_tmp->p_module->b_submodule ?
563 (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
564 if( !p_module->b_builtin && !p_module->b_loaded )
566 module_t *p_new_module =
567 AllocatePlugin( p_this, p_module->psz_filename );
570 CacheMerge( p_this, p_module, p_new_module );
571 vlc_object_attach( p_new_module, p_module );
572 DeleteModule( p_new_module );
577 p_this->b_force = p_tmp->b_force;
578 if( p_tmp->p_module->pf_activate
579 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
584 vlc_object_release( p_tmp->p_module );
585 p_tmp = p_tmp->p_next;
588 /* Store the locked module value */
591 p_module = p_tmp->p_module;
592 p_tmp = p_tmp->p_next;
599 /* Unlock the remaining modules */
600 while( p_tmp != NULL )
602 vlc_object_release( p_tmp->p_module );
603 p_tmp = p_tmp->p_next;
607 p_this->b_force = VLC_FALSE;
609 if( p_module != NULL )
611 msg_Dbg( p_module, "using %s module \"%s\"",
612 psz_capability, p_module->psz_object_name );
614 else if( p_first == NULL )
616 if( !strcmp( psz_capability, "access_demux" ) )
618 msg_Warn( p_this, "no %s module matched \"%s\"",
619 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
623 msg_Err( p_this, "no %s module matched \"%s\"",
624 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
627 else if( psz_name != NULL && *psz_name )
629 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
630 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
635 free( psz_shortcuts );
643 /* Don't forget that the module is still locked */
647 /*****************************************************************************
648 * module_Unneed: decrease the usage count of a module.
649 *****************************************************************************
650 * This function must be called by the thread that called module_Need, to
651 * decrease the reference count and allow for hiding of modules.
652 *****************************************************************************/
653 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
655 /* Use the close method */
656 if( p_module->pf_deactivate )
658 p_module->pf_deactivate( p_this );
661 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
663 vlc_object_release( p_module );
668 /*****************************************************************************
669 * Following functions are local.
670 *****************************************************************************/
672 /*****************************************************************************
673 * AllocateAllPlugins: load all plugin modules we can find.
674 *****************************************************************************/
675 #ifdef HAVE_DYNAMIC_PLUGINS
676 static void AllocateAllPlugins( vlc_object_t *p_this )
678 /* Yes, there are two NULLs because we replace one with "plugin-path". */
679 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
682 char ** ppsz_path = path;
685 /* If the user provided a plugin path, we add it to the list */
686 path[ sizeof(path)/sizeof(char*) - 2 ] =
687 config_GetPsz( p_this, "plugin-path" );
689 for( ; *ppsz_path != NULL ; ppsz_path++ )
691 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
692 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
694 /* Handle relative as well as absolute paths */
696 if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
698 if( (*ppsz_path)[0] != '/' )
701 int i_dirlen = strlen( *ppsz_path );
702 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
704 psz_fullpath = malloc( i_dirlen );
705 if( psz_fullpath == NULL )
710 sprintf( psz_fullpath, "%s\\%s",
711 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
713 sprintf( psz_fullpath, "%s/%s",
714 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
720 psz_fullpath = strdup( *ppsz_path );
723 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
725 /* Don't go deeper than 5 subdirectories */
726 AllocatePluginDir( p_this, psz_fullpath, 5 );
728 free( psz_fullpath );
731 /* Free plugin-path */
732 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
733 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
734 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
737 /*****************************************************************************
738 * AllocatePluginDir: recursively parse a directory to look for plugins
739 *****************************************************************************/
740 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
743 #if defined( UNDER_CE ) || defined( _MSC_VER )
745 wchar_t psz_path[MAX_PATH + 256];
746 wchar_t psz_wdir[MAX_PATH];
748 char psz_path[MAX_PATH + 256];
750 WIN32_FIND_DATA finddata;
756 struct dirent * file;
760 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
765 #if defined( UNDER_CE ) || defined( _MSC_VER )
766 MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
768 rc = GetFileAttributes( psz_wdir );
769 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
771 /* Not a directory */
775 /* Parse all files in the directory */
777 swprintf( psz_path, L"%s\\*.*", psz_dir );
779 sprintf( psz_path, "%s\\*.*", psz_dir );
781 handle = FindFirstFile( psz_path, &finddata );
782 if( handle == INVALID_HANDLE_VALUE )
784 /* Empty directory */
788 /* Parse the directory and try to load all files it contains. */
792 unsigned int i_len = wcslen( finddata.cFileName );
793 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
795 unsigned int i_len = strlen( finddata.cFileName );
796 /* Skip ".", ".." and anything starting with "." */
797 if( !*finddata.cFileName || *finddata.cFileName == '.' )
799 if( !FindNextFile( handle, &finddata ) ) break;
802 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
805 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
807 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
809 else if( i_len > strlen( LIBEXT )
813 /* We only load files ending with LIBEXT */
814 && !strncasecmp( psz_path + strlen( psz_path)
816 LIBEXT, strlen( LIBEXT ) ) )
820 char psz_filename[MAX_PATH];
821 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_path, -1,
822 psz_filename, MAX_PATH, NULL, NULL );
823 psz_file = psz_filename;
828 AllocatePluginFile( p_this, psz_file, 0, 0 );
831 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
833 /* Close the directory */
837 dir = opendir( psz_dir );
843 i_dirlen = strlen( psz_dir );
845 /* Parse the directory and try to load all files it contains. */
846 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
852 /* Skip ".", ".." and anything starting with "." */
853 if( !*file->d_name || *file->d_name == '.' )
858 i_len = strlen( file->d_name );
859 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
861 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
863 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
866 i_stat = stat( psz_file, &statbuf );
867 if( !i_stat && statbuf.st_mode & S_IFDIR )
869 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
871 else if( i_len > strlen( LIBEXT )
872 /* We only load files ending with LIBEXT */
873 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
874 LIBEXT, strlen( LIBEXT ) ) )
876 int64_t i_time = 0, i_size = 0;
880 i_time = statbuf.st_mtime;
881 i_size = statbuf.st_size;
884 AllocatePluginFile( p_this, psz_file, i_time, i_size );
890 /* Close the directory */
896 /*****************************************************************************
897 * AllocatePluginFile: load a module into memory and initialize it.
898 *****************************************************************************
899 * This function loads a dynamically loadable module and allocates a structure
900 * for its information data. The module can then be handled by module_Need
901 * and module_Unneed. It can be removed by DeleteModule.
902 *****************************************************************************/
903 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
904 int64_t i_file_time, int64_t i_file_size )
907 module_cache_t *p_cache_entry = NULL;
910 * Check our plugins cache first then load plugin if needed
913 CacheFind( p_this, psz_file, i_file_time, i_file_size );
917 p_module = AllocatePlugin( p_this, psz_file );
921 /* If junk dll, don't try to load it */
922 if( p_cache_entry->b_junk )
928 module_config_t *p_item;
930 p_module = p_cache_entry->p_module;
931 p_module->b_loaded = VLC_FALSE;
933 /* For now we force loading if the module's config contains
934 * callbacks or actions.
935 * Could be optimized by adding an API call.*/
936 for( p_item = p_module->p_config;
937 p_item->i_type != CONFIG_HINT_END; p_item++ )
939 if( p_item->pf_callback || p_item->i_action )
940 p_module = AllocatePlugin( p_this, psz_file );
947 /* Everything worked fine !
948 * The module is ready to be added to the list. */
949 p_module->b_builtin = VLC_FALSE;
951 /* msg_Dbg( p_this, "plugin \"%s\", %s",
952 p_module->psz_object_name, p_module->psz_longname ); */
954 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
957 if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
959 /* Add entry to cache */
960 #define p_bank p_this->p_libvlc->p_module_bank
962 realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
963 p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
964 p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
965 p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
966 p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
967 p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
968 p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
971 return p_module ? 0 : -1;
974 /*****************************************************************************
975 * AllocatePlugin: load a module into memory and initialize it.
976 *****************************************************************************
977 * This function loads a dynamically loadable module and allocates a structure
978 * for its information data. The module can then be handled by module_Need
979 * and module_Unneed. It can be removed by DeleteModule.
980 *****************************************************************************/
981 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
984 module_handle_t handle;
986 if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
988 /* Now that we have successfully loaded the module, we can
989 * allocate a structure for it */
990 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
991 if( p_module == NULL )
993 msg_Err( p_this, "out of memory" );
994 CloseModule( handle );
998 /* We need to fill these since they may be needed by CallEntry() */
999 p_module->psz_filename = psz_file;
1000 p_module->handle = handle;
1001 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1002 p_module->b_loaded = VLC_TRUE;
1004 /* Initialize the module: fill p_module, default config */
1005 if( CallEntry( p_module ) != 0 )
1007 /* We couldn't call module_init() */
1008 vlc_object_destroy( p_module );
1009 CloseModule( handle );
1013 DupModule( p_module );
1014 p_module->psz_filename = strdup( p_module->psz_filename );
1015 p_module->psz_longname = strdup( p_module->psz_longname );
1017 /* Everything worked fine ! The module is ready to be added to the list. */
1018 p_module->b_builtin = VLC_FALSE;
1023 /*****************************************************************************
1024 * DupModule: make a plugin module standalone.
1025 *****************************************************************************
1026 * This function duplicates all strings in the module, so that the dynamic
1027 * object can be unloaded. It acts recursively on submodules.
1028 *****************************************************************************/
1029 static void DupModule( module_t *p_module )
1034 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1036 *pp_shortcut = strdup( *pp_shortcut );
1039 /* We strdup() these entries so that they are still valid when the
1040 * module is unloaded. */
1041 p_module->psz_object_name = strdup( p_module->psz_object_name );
1042 p_module->psz_capability = strdup( p_module->psz_capability );
1044 if( p_module->psz_program != NULL )
1046 p_module->psz_program = strdup( p_module->psz_program );
1049 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1051 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1055 /*****************************************************************************
1056 * UndupModule: free a duplicated module.
1057 *****************************************************************************
1058 * This function frees the allocations done in DupModule().
1059 *****************************************************************************/
1060 static void UndupModule( module_t *p_module )
1065 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1067 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1070 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1072 free( *pp_shortcut );
1075 free( p_module->psz_object_name );
1076 free( p_module->psz_capability );
1078 if( p_module->psz_program != NULL )
1080 free( p_module->psz_program );
1084 #endif /* HAVE_DYNAMIC_PLUGINS */
1086 /*****************************************************************************
1087 * AllocateBuiltinModule: initialize a builtin module.
1088 *****************************************************************************
1089 * This function registers a builtin module and allocates a structure
1090 * for its information data. The module can then be handled by module_Need
1091 * and module_Unneed. It can be removed by DeleteModule.
1092 *****************************************************************************/
1093 static int AllocateBuiltinModule( vlc_object_t * p_this,
1094 int ( *pf_entry ) ( module_t * ) )
1096 module_t * p_module;
1098 /* Now that we have successfully loaded the module, we can
1099 * allocate a structure for it */
1100 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1101 if( p_module == NULL )
1103 msg_Err( p_this, "out of memory" );
1107 /* Initialize the module : fill p_module->psz_object_name, etc. */
1108 if( pf_entry( p_module ) != 0 )
1110 /* With a well-written module we shouldn't have to print an
1111 * additional error message here, but just make sure. */
1112 msg_Err( p_this, "failed calling entry point in builtin module" );
1113 vlc_object_destroy( p_module );
1117 /* Everything worked fine ! The module is ready to be added to the list. */
1118 p_module->b_builtin = VLC_TRUE;
1120 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1121 p_module->psz_object_name, p_module->psz_longname ); */
1123 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1128 /*****************************************************************************
1129 * DeleteModule: delete a module and its structure.
1130 *****************************************************************************
1131 * This function can only be called if the module isn't being used.
1132 *****************************************************************************/
1133 static int DeleteModule( module_t * p_module )
1135 vlc_object_detach( p_module );
1137 /* We free the structures that we strdup()ed in Allocate*Module(). */
1138 #ifdef HAVE_DYNAMIC_PLUGINS
1139 if( !p_module->b_builtin )
1141 if( p_module->b_loaded && p_module->b_unloadable )
1143 CloseModule( p_module->handle );
1145 UndupModule( p_module );
1146 free( p_module->psz_filename );
1147 free( p_module->psz_longname );
1151 /* Free and detach the object's children */
1152 while( p_module->i_children )
1154 vlc_object_t *p_this = p_module->pp_children[0];
1155 vlc_object_detach( p_this );
1156 vlc_object_destroy( p_this );
1159 config_Free( p_module );
1160 vlc_object_destroy( p_module );
1165 #ifdef HAVE_DYNAMIC_PLUGINS
1166 /*****************************************************************************
1167 * CallEntry: call an entry point.
1168 *****************************************************************************
1169 * This function calls a symbol given its name and a module structure. The
1170 * symbol MUST refer to a function returning int and taking a module_t* as
1172 *****************************************************************************/
1173 static int CallEntry( module_t * p_module )
1175 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1176 int (* pf_symbol) ( module_t * p_module );
1178 /* Try to resolve the symbol */
1179 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1181 if( pf_symbol == NULL )
1183 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1184 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1185 psz_name, p_module->psz_filename );
1186 #elif defined(HAVE_DL_WINDOWS)
1187 char *psz_error = GetWindowsError();
1188 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1189 psz_name, p_module->psz_filename, psz_error );
1191 #elif defined(HAVE_DL_DLOPEN)
1192 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1193 psz_name, p_module->psz_filename, dlerror() );
1194 #elif defined(HAVE_DL_SHL_LOAD)
1195 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1196 psz_name, p_module->psz_filename, strerror(errno) );
1198 # error "Something is wrong in modules.c"
1203 /* We can now try to call the symbol */
1204 if( pf_symbol( p_module ) != 0 )
1206 /* With a well-written module we shouldn't have to print an
1207 * additional error message here, but just make sure. */
1208 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1209 psz_name, p_module->psz_filename );
1213 /* Everything worked fine, we can return */
1217 /*****************************************************************************
1218 * LoadModule: loads a dynamic library
1219 *****************************************************************************
1220 * This function loads a dynamically linked library using a system dependant
1221 * method. Will return 0 on success as well as the module handle.
1222 *****************************************************************************/
1223 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1224 module_handle_t *p_handle )
1226 module_handle_t handle;
1228 #if defined(HAVE_DL_DYLD)
1229 NSObjectFileImage image;
1230 NSObjectFileImageReturnCode ret;
1232 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1234 if( ret != NSObjectFileImageSuccess )
1236 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1240 /* Open the dynamic module */
1241 handle = NSLinkModule( image, psz_file,
1242 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1246 NSLinkEditErrors errors;
1247 const char *psz_file, *psz_err;
1249 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1250 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1251 NSDestroyObjectFileImage( image );
1255 /* Destroy our image, we won't need it */
1256 NSDestroyObjectFileImage( image );
1258 #elif defined(HAVE_DL_BEOS)
1259 handle = load_add_on( psz_file );
1262 msg_Warn( p_this, "cannot load module `%s'", psz_file );
1266 #elif defined(HAVE_DL_WINDOWS)
1267 handle = LoadLibrary( psz_file );
1268 if( handle == NULL )
1270 char *psz_err = GetWindowsError();
1271 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1275 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1276 /* static is OK, we are called atomically */
1277 static vlc_bool_t b_kde = VLC_FALSE;
1279 # if defined(SYS_LINUX)
1280 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1281 * are going to get namespace collisions when two modules have common
1282 * public symbols, but ALSA is being a pest here. */
1283 if( strstr( psz_file, "alsa_plugin" ) )
1285 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1286 if( handle == NULL )
1288 msg_Warn( p_this, "cannot load module `%s' (%s)",
1289 psz_file, dlerror() );
1294 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1295 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1296 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1297 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1298 if( !b_kde && !strstr( psz_file, "kde" ) )
1300 dlopen( "libstdc++.so.6", RTLD_NOW )
1301 || dlopen( "libstdc++.so.5", RTLD_NOW )
1302 || dlopen( "libstdc++.so.4", RTLD_NOW )
1303 || dlopen( "libstdc++.so.3", RTLD_NOW );
1307 handle = dlopen( psz_file, RTLD_NOW );
1308 if( handle == NULL )
1310 msg_Warn( p_this, "cannot load module `%s' (%s)",
1311 psz_file, dlerror() );
1315 #elif defined(HAVE_DL_DLOPEN)
1316 # if defined(DL_LAZY)
1317 handle = dlopen( psz_file, DL_LAZY );
1319 handle = dlopen( psz_file, 0 );
1321 if( handle == NULL )
1323 msg_Warn( p_this, "cannot load module `%s' (%s)",
1324 psz_file, dlerror() );
1328 #elif defined(HAVE_DL_SHL_LOAD)
1329 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1330 if( handle == NULL )
1332 msg_Warn( p_this, "cannot load module `%s' (%s)",
1333 psz_file, strerror(errno) );
1338 # error "Something is wrong in modules.c"
1346 /*****************************************************************************
1347 * CloseModule: unload a dynamic library
1348 *****************************************************************************
1349 * This function unloads a previously opened dynamically linked library
1350 * using a system dependant method. No return value is taken in consideration,
1351 * since some libraries sometimes refuse to close properly.
1352 *****************************************************************************/
1353 static void CloseModule( module_handle_t handle )
1355 #if defined(HAVE_DL_DYLD)
1356 NSUnLinkModule( handle, FALSE );
1358 #elif defined(HAVE_DL_BEOS)
1359 unload_add_on( handle );
1361 #elif defined(HAVE_DL_WINDOWS)
1362 FreeLibrary( handle );
1364 #elif defined(HAVE_DL_DLOPEN)
1367 #elif defined(HAVE_DL_SHL_LOAD)
1368 shl_unload( handle );
1374 /*****************************************************************************
1375 * GetSymbol: get a symbol from a dynamic library
1376 *****************************************************************************
1377 * This function queries a loaded library for a symbol specified in a
1378 * string, and returns a pointer to it. We don't check for dlerror() or
1379 * similar functions, since we want a non-NULL symbol anyway.
1380 *****************************************************************************/
1381 static void * _module_getsymbol( module_handle_t, const char * );
1383 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1385 void * p_symbol = _module_getsymbol( handle, psz_function );
1387 /* MacOS X dl library expects symbols to begin with "_". So do
1388 * some other operating systems. That's really lame, but hey, what
1390 if( p_symbol == NULL )
1392 char *psz_call = malloc( strlen( psz_function ) + 2 );
1394 strcpy( psz_call + 1, psz_function );
1395 psz_call[ 0 ] = '_';
1396 p_symbol = _module_getsymbol( handle, psz_call );
1403 static void * _module_getsymbol( module_handle_t handle,
1404 const char * psz_function )
1406 #if defined(HAVE_DL_DYLD)
1407 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1408 return NSAddressOfSymbol( sym );
1410 #elif defined(HAVE_DL_BEOS)
1412 if( B_OK == get_image_symbol( handle, psz_function,
1413 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1422 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1423 wchar_t psz_real[256];
1424 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1426 return (void *)GetProcAddress( handle, psz_real );
1428 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1429 return (void *)GetProcAddress( handle, (char *)psz_function );
1431 #elif defined(HAVE_DL_DLOPEN)
1432 return dlsym( handle, psz_function );
1434 #elif defined(HAVE_DL_SHL_LOAD)
1436 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1442 #if defined(HAVE_DL_WINDOWS)
1443 static char * GetWindowsError( void )
1445 #if defined(UNDER_CE)
1446 wchar_t psz_tmp[256];
1447 char * psz_buffer = malloc( 256 );
1449 char * psz_tmp = malloc( 256 );
1451 int i = 0, i_error = GetLastError();
1453 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1454 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1455 (LPTSTR) psz_tmp, 256, NULL );
1457 /* Go to the end of the string */
1458 #if defined(UNDER_CE)
1459 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1461 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1469 #if defined(UNDER_CE)
1470 swprintf( psz_tmp + i, L" (error %i)", i_error );
1471 psz_tmp[ 255 ] = L'\0';
1473 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1474 psz_tmp[ 255 ] = '\0';
1478 #if defined(UNDER_CE)
1479 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1480 psz_buffer, 256, NULL, NULL );
1486 #endif /* HAVE_DL_WINDOWS */
1488 /*****************************************************************************
1489 * LoadPluginsCache: loads the plugins cache file
1490 *****************************************************************************
1491 * This function will load the plugin cache if present and valid. This cache
1492 * will in turn be queried by AllocateAllPlugins() to see if it needs to
1493 * actually load the dynamically loadable module.
1494 * This allows us to only fully load plugins when they are actually used.
1495 *****************************************************************************/
1496 static void CacheLoad( vlc_object_t *p_this )
1498 char *psz_filename, *psz_homedir;
1500 int i, j, i_size, i_read;
1501 char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1503 module_cache_t **pp_cache = 0;
1504 int32_t i_file_size;
1506 psz_homedir = p_this->p_vlc->psz_homedir;
1509 msg_Err( p_this, "psz_homedir is null" );
1513 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1514 strlen(psz_homedir) );
1517 sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1522 msg_Err( p_this, "out of memory" );
1526 if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1528 msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1529 unlink( psz_filename );
1533 msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1535 file = fopen( psz_filename, "r" );
1538 msg_Warn( p_this, "could not open plugins cache file %s for reading",
1540 free( psz_filename );
1543 free( psz_filename );
1545 /* Check the file size */
1546 i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1547 if( i_read != sizeof(i_file_size) )
1549 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1555 fseek( file, 0, SEEK_END );
1556 if( ftell( file ) != i_file_size )
1558 msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1559 "(corrupted size)" );
1563 fseek( file, sizeof(i_file_size), SEEK_SET );
1565 /* Check the file is a plugins cache */
1566 i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1567 i_read = fread( p_cachestring, sizeof(char), i_size, file );
1568 if( i_read != i_size ||
1569 memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1571 msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1576 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1577 fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1578 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1579 malloc( i_cache * sizeof(void *) );
1581 #define LOAD_IMMEDIATE(a) \
1582 if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1583 #define LOAD_STRING(a) \
1584 { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1585 != sizeof(i_size) ) goto error; \
1587 a = malloc( i_size ); \
1588 if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1594 for( i = 0; i < i_cache; i++ )
1599 pp_cache[i] = malloc( sizeof(module_cache_t) );
1600 p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1602 /* Save common info */
1603 LOAD_STRING( pp_cache[i]->psz_file );
1604 LOAD_IMMEDIATE( pp_cache[i]->i_time );
1605 LOAD_IMMEDIATE( pp_cache[i]->i_size );
1606 LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1608 if( pp_cache[i]->b_junk ) continue;
1610 pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1612 /* Save additional infos */
1613 LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1614 LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1615 LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1616 LOAD_STRING( pp_cache[i]->p_module->psz_program );
1617 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1619 LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1621 LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1622 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1623 LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1624 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1625 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1626 LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1629 if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1632 LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1634 LOAD_IMMEDIATE( i_submodules );
1636 while( i_submodules-- )
1638 module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1639 vlc_object_attach( p_module, pp_cache[i]->p_module );
1640 p_module->b_submodule = VLC_TRUE;
1642 LOAD_STRING( p_module->psz_object_name );
1643 LOAD_STRING( p_module->psz_shortname );
1644 LOAD_STRING( p_module->psz_longname );
1645 LOAD_STRING( p_module->psz_program );
1646 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1648 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1650 LOAD_STRING( p_module->psz_capability );
1651 LOAD_IMMEDIATE( p_module->i_score );
1652 LOAD_IMMEDIATE( p_module->i_cpu );
1653 LOAD_IMMEDIATE( p_module->b_unloadable );
1654 LOAD_IMMEDIATE( p_module->b_reentrant );
1663 msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1666 p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1672 int CacheLoadConfig( module_t *p_module, FILE *file )
1677 /* Calculate the structure length */
1678 LOAD_IMMEDIATE( p_module->i_config_items );
1679 LOAD_IMMEDIATE( p_module->i_bool_items );
1681 LOAD_IMMEDIATE( i_lines );
1683 /* Allocate memory */
1684 p_module->p_config =
1685 (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1686 if( p_module->p_config == NULL )
1688 msg_Err( p_module, "config error: can't duplicate p_config" );
1692 /* Do the duplication job */
1693 for( i = 0; i < i_lines ; i++ )
1695 LOAD_IMMEDIATE( p_module->p_config[i] );
1697 LOAD_STRING( p_module->p_config[i].psz_type );
1698 LOAD_STRING( p_module->p_config[i].psz_name );
1699 LOAD_STRING( p_module->p_config[i].psz_text );
1700 LOAD_STRING( p_module->p_config[i].psz_longtext );
1701 LOAD_STRING( p_module->p_config[i].psz_value_orig );
1703 p_module->p_config[i].psz_value =
1704 p_module->p_config[i].psz_value_orig ?
1705 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1706 p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1707 p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1709 p_module->p_config[i].p_lock = &p_module->object_lock;
1711 if( p_module->p_config[i].i_list )
1713 if( p_module->p_config[i].ppsz_list )
1715 p_module->p_config[i].ppsz_list =
1716 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1717 if( p_module->p_config[i].ppsz_list )
1719 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1720 LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1721 p_module->p_config[i].ppsz_list[j] = NULL;
1724 if( p_module->p_config[i].ppsz_list_text )
1726 p_module->p_config[i].ppsz_list_text =
1727 malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1728 if( p_module->p_config[i].ppsz_list_text )
1730 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1731 LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1732 p_module->p_config[i].ppsz_list_text[j] = NULL;
1735 if( p_module->p_config[i].pi_list )
1737 p_module->p_config[i].pi_list =
1738 malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1739 if( p_module->p_config[i].pi_list )
1741 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1742 LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1747 if( p_module->p_config[i].i_action )
1749 p_module->p_config[i].ppf_action =
1750 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1751 p_module->p_config[i].ppsz_action_text =
1752 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1754 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1756 p_module->p_config[i].ppf_action[j] = 0;
1757 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1761 LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1764 p_module->p_config[i].i_type = CONFIG_HINT_END;
1770 return VLC_EGENERIC;
1773 /*****************************************************************************
1774 * SavePluginsCache: saves the plugins cache to a file
1775 *****************************************************************************/
1776 static void CacheSave( vlc_object_t *p_this )
1778 char *psz_filename, *psz_homedir;
1781 module_cache_t **pp_cache;
1782 int32_t i_file_size = 0;
1784 psz_homedir = p_this->p_vlc->psz_homedir;
1787 msg_Err( p_this, "psz_homedir is null" );
1791 (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1792 strlen(psz_homedir) );
1795 sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1799 msg_Err( p_this, "out of memory" );
1803 config_CreateDir( p_this, psz_filename );
1805 strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1807 msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1809 file = fopen( psz_filename, "w" );
1812 msg_Warn( p_this, "could not open plugins cache file %s for writing",
1814 free( psz_filename );
1817 free( psz_filename );
1819 /* Empty space for file size */
1820 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1822 /* Contains version number */
1823 fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1825 i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1826 pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1828 fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1830 #define SAVE_IMMEDIATE(a) \
1831 fwrite( &a, sizeof(char), sizeof(a), file )
1832 #define SAVE_STRING(a) \
1833 { i_size = a ? strlen( a ) + 1 : 0; \
1834 fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1835 if( a ) fwrite( a, sizeof(char), i_size, file ); \
1838 for( i = 0; i < i_cache; i++ )
1841 int32_t i_submodule;
1843 /* Save common info */
1844 SAVE_STRING( pp_cache[i]->psz_file );
1845 SAVE_IMMEDIATE( pp_cache[i]->i_time );
1846 SAVE_IMMEDIATE( pp_cache[i]->i_size );
1847 SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1849 if( pp_cache[i]->b_junk ) continue;
1851 /* Save additional infos */
1852 SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1853 SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1854 SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1855 SAVE_STRING( pp_cache[i]->p_module->psz_program );
1856 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1858 SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1860 SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1861 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1862 SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1863 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1864 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1865 SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1868 CacheSaveConfig( pp_cache[i]->p_module, file );
1870 SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1872 i_submodule = pp_cache[i]->p_module->i_children;
1873 SAVE_IMMEDIATE( i_submodule );
1874 for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1877 module_t *p_module =
1878 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1880 SAVE_STRING( p_module->psz_object_name );
1881 SAVE_STRING( p_module->psz_shortname );
1882 SAVE_STRING( p_module->psz_longname );
1883 SAVE_STRING( p_module->psz_program );
1884 for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1886 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1888 SAVE_STRING( p_module->psz_capability );
1889 SAVE_IMMEDIATE( p_module->i_score );
1890 SAVE_IMMEDIATE( p_module->i_cpu );
1891 SAVE_IMMEDIATE( p_module->b_unloadable );
1892 SAVE_IMMEDIATE( p_module->b_reentrant );
1896 /* Fill-up file size */
1897 i_file_size = ftell( file );
1898 fseek( file, 0, SEEK_SET );
1899 fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1906 void CacheSaveConfig( module_t *p_module, FILE *file )
1908 int i, j, i_lines = 0;
1909 module_config_t *p_item;
1912 SAVE_IMMEDIATE( p_module->i_config_items );
1913 SAVE_IMMEDIATE( p_module->i_bool_items );
1915 for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1916 p_item++ ) i_lines++;
1918 SAVE_IMMEDIATE( i_lines );
1920 for( i = 0; i < i_lines ; i++ )
1922 SAVE_IMMEDIATE( p_module->p_config[i] );
1924 SAVE_STRING( p_module->p_config[i].psz_type );
1925 SAVE_STRING( p_module->p_config[i].psz_name );
1926 SAVE_STRING( p_module->p_config[i].psz_text );
1927 SAVE_STRING( p_module->p_config[i].psz_longtext );
1928 SAVE_STRING( p_module->p_config[i].psz_value_orig );
1930 if( p_module->p_config[i].i_list )
1932 if( p_module->p_config[i].ppsz_list )
1934 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1935 SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1938 if( p_module->p_config[i].ppsz_list_text )
1940 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1941 SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1943 if( p_module->p_config[i].pi_list )
1945 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1946 SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1950 for( j = 0; j < p_module->p_config[i].i_action; j++ )
1951 SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1953 SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
1957 /*****************************************************************************
1958 * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1959 *****************************************************************************/
1960 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1961 module_t *p_module )
1965 p_cache->pf_activate = p_module->pf_activate;
1966 p_cache->pf_deactivate = p_module->pf_deactivate;
1967 p_cache->p_symbols = p_module->p_symbols;
1968 p_cache->handle = p_module->handle;
1970 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1972 module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1973 module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1974 p_cchild->pf_activate = p_child->pf_activate;
1975 p_cchild->pf_deactivate = p_child->pf_deactivate;
1976 p_cchild->p_symbols = p_child->p_symbols;
1979 p_cache->b_loaded = VLC_TRUE;
1980 p_module->b_loaded = VLC_FALSE;
1983 /*****************************************************************************
1984 * FindPluginCache: finds the cache entry corresponding to a file
1985 *****************************************************************************/
1986 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
1987 int64_t i_time, int64_t i_size )
1989 module_cache_t **pp_cache;
1992 pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
1993 i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
1995 for( i = 0; i < i_cache; i++ )
1997 if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
1998 pp_cache[i]->i_time == i_time &&
1999 pp_cache[i]->i_size == i_size ) return pp_cache[i];
2005 #endif /* HAVE_DYNAMIC_PLUGINS */