1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.102 2002/11/10 18:04:24 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
27 * is set to 64. Don't try to be cleverer. */
28 #ifdef _FILE_OFFSET_BITS
29 #undef _FILE_OFFSET_BITS
32 #include <stdlib.h> /* free(), strtol() */
33 #include <stdio.h> /* sprintf() */
34 #include <string.h> /* strdup() */
41 # include "../extras/dirent.h"
44 #ifdef HAVE_SYS_TYPES_H
45 # include <sys/types.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
54 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
55 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
56 # define HAVE_DYNAMIC_PLUGINS
57 #elif defined(HAVE_IMAGE_H) /* BeOS */
59 # define HAVE_DYNAMIC_PLUGINS
61 # define HAVE_DYNAMIC_PLUGINS
63 # undef HAVE_DYNAMIC_PLUGINS
69 #include "interface.h"
70 #include "vlc_playlist.h"
71 #include "intf_eject.h"
73 #include "stream_control.h"
74 #include "input_ext-intf.h"
75 #include "input_ext-dec.h"
76 #include "input_ext-plugins.h"
79 #include "video_output.h"
81 #include "audio_output.h"
82 #include "aout_internal.h"
84 #include "stream_output.h"
88 #ifdef HAVE_DYNAMIC_PLUGINS
89 # include "modules_plugin.h"
92 #if !defined( _MSC_VER )
93 # include "modules_builtin.h"
95 # include "modules_builtin_msvc.h"
98 /*****************************************************************************
100 *****************************************************************************/
101 #ifdef HAVE_DYNAMIC_PLUGINS
102 static void AllocateAllPlugins ( vlc_object_t * );
103 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
104 static int AllocatePluginFile ( vlc_object_t *, char * );
106 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
107 static int DeleteModule ( module_t * );
108 #ifdef HAVE_DYNAMIC_PLUGINS
109 static void DupModule ( module_t * );
110 static void UndupModule ( module_t * );
111 static int CallEntry ( module_t * );
114 /*****************************************************************************
115 * module_InitBank: create the module bank.
116 *****************************************************************************
117 * This function creates a module bank structure which will be filled later
118 * on with all the modules found.
119 *****************************************************************************/
120 void __module_InitBank( vlc_object_t *p_this )
122 module_bank_t *p_bank;
124 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
125 p_bank->psz_object_name = "module bank";
128 * Store the symbols to be exported
130 #ifdef HAVE_DYNAMIC_PLUGINS
131 STORE_SYMBOLS( &p_bank->symbols );
134 /* Everything worked, attach the object */
135 p_this->p_libvlc->p_module_bank = p_bank;
136 vlc_object_attach( p_bank, p_this->p_libvlc );
141 /*****************************************************************************
142 * module_ResetBank: reset the module bank.
143 *****************************************************************************
144 * This function resets the module bank by unloading all unused plugin
146 *****************************************************************************/
147 void __module_ResetBank( vlc_object_t *p_this )
149 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
153 /*****************************************************************************
154 * module_EndBank: empty the module bank.
155 *****************************************************************************
156 * This function unloads all unused plugin modules and empties the module
157 * bank in case of success.
158 *****************************************************************************/
159 void __module_EndBank( vlc_object_t *p_this )
163 vlc_object_detach( p_this->p_libvlc->p_module_bank );
165 while( p_this->p_libvlc->p_module_bank->i_children )
167 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
169 if( DeleteModule( p_next ) )
171 /* Module deletion failed */
172 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
173 p_next->psz_object_name );
175 /* We just free the module by hand. Niahahahahaha. */
176 vlc_object_detach( p_next );
177 vlc_object_destroy( p_next );
181 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
186 /*****************************************************************************
187 * module_LoadMain: load the main program info into the module bank.
188 *****************************************************************************
189 * This function fills the module bank structure with the main module infos.
190 * This is very useful as it will allow us to consider the main program just
191 * as another module, and for instance the configuration options of main will
192 * be available in the module bank structure just as for every other module.
193 *****************************************************************************/
194 void __module_LoadMain( vlc_object_t *p_this )
196 AllocateBuiltinModule( p_this, vlc_entry__main );
199 /*****************************************************************************
200 * module_LoadBuiltins: load all modules which we built with.
201 *****************************************************************************
202 * This function fills the module bank structure with the builtin modules.
203 *****************************************************************************/
204 void __module_LoadBuiltins( vlc_object_t * p_this )
206 msg_Dbg( p_this, "checking builtin modules" );
207 ALLOCATE_ALL_BUILTINS();
210 /*****************************************************************************
211 * module_LoadPlugins: load all plugin modules we can find.
212 *****************************************************************************
213 * This function fills the module bank structure with the plugin modules.
214 *****************************************************************************/
215 void __module_LoadPlugins( vlc_object_t * p_this )
217 #ifdef HAVE_DYNAMIC_PLUGINS
218 msg_Dbg( p_this, "checking plugin modules" );
219 AllocateAllPlugins( p_this );
223 /*****************************************************************************
224 * module_Need: return the best module function, given a capability list.
225 *****************************************************************************
226 * This function returns the module that best fits the asked capabilities.
227 *****************************************************************************/
228 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
229 const char *psz_name )
231 typedef struct module_list_t module_list_t;
237 module_list_t *p_next;
240 module_list_t *p_list, *p_first, *p_tmp;
244 vlc_bool_t b_intf = VLC_FALSE;
247 module_t **pp_parser;
250 char *psz_shortcuts = NULL, *psz_var = NULL;
252 msg_Dbg( p_this, "looking for %s module", psz_capability );
254 /* Deal with variables */
255 if( psz_name && psz_name[0] == '$' )
257 psz_var = config_GetPsz( p_this, psz_name + 1 );
261 /* Count how many different shortcuts were asked for */
262 if( psz_name && *psz_name )
266 /* If the user wants none, give him none. */
267 if( !strcmp( psz_name, "none" ) )
269 if( psz_var ) free( psz_var );
274 psz_shortcuts = strdup( psz_name );
276 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
278 if( *psz_parser == ',' )
286 /* Sort the modules and test them */
287 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
288 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
291 /* Parse the module list for capabilities and probe each of them */
292 for( pp_parser = (module_t**)p_all->pp_objects ; *pp_parser ; pp_parser++ )
294 module_t * p_submodule = NULL;
295 int i_shortcut_bonus = 0, i_submodule;
297 p_module = *pp_parser;
299 /* Test that this module can do what we need */
300 if( strcmp( p_module->psz_capability, psz_capability ) )
302 for( i_submodule = 0;
303 i_submodule < p_module->i_children;
306 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
307 ->psz_capability, psz_capability ) )
310 (module_t*)p_module->pp_children[ i_submodule ];
315 if( p_submodule == NULL )
320 p_module = p_submodule;
323 /* Test if we have the required CPU */
324 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
329 /* If we required a shortcut, check this plugin provides it. */
332 vlc_bool_t b_trash = VLC_TRUE;
333 int i_dummy, i_short = i_shortcuts;
334 char *psz_name = psz_shortcuts;
339 b_trash && p_module->pp_shortcuts[i_dummy];
342 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
343 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
348 i_shortcut_bonus = i_short * 10000;
352 /* Go to the next shortcut... This is so lame! */
366 /* If we didn't require a shortcut, trash zero-scored plugins */
367 else if( !p_module->i_score )
372 /* Special case: test if we requested a particular intf plugin */
373 if( p_module->psz_program
374 && !strcmp( p_module->psz_program,
375 p_this->p_vlc->psz_object_name ) )
379 /* Remove previous non-matching plugins */
386 /* This one doesn't match */
390 /* Store this new module */
391 p_list[ i_index ].p_module = p_module;
392 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
394 /* Add it to the modules-to-probe list */
397 p_list[ 0 ].p_next = NULL;
402 /* Ok, so at school you learned that quicksort is quick, and
403 * bubble sort sucks raw eggs. But that's when dealing with
404 * thousands of items. Here we have barely 50. */
405 module_list_t *p_newlist = p_first;
407 if( p_first->i_score < p_list[ i_index ].i_score )
409 p_list[ i_index ].p_next = p_first;
410 p_first = &p_list[ i_index ];
414 while( p_newlist->p_next != NULL &&
415 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
417 p_newlist = p_newlist->p_next;
420 p_list[ i_index ].p_next = p_newlist->p_next;
421 p_newlist->p_next = &p_list[ i_index ];
428 msg_Dbg( p_this, "probing %i candidate%s",
429 i_index, i_index == 1 ? "" : "s" );
431 /* Lock all candidate modules */
433 while( p_tmp != NULL )
435 vlc_object_yield( p_tmp->p_module );
436 p_tmp = p_tmp->p_next;
439 /* We can release the list, interesting modules were yielded */
440 vlc_list_release( p_all );
442 /* Parse the linked list and use the first successful module */
444 while( p_tmp != NULL )
446 if( p_tmp->p_module->pf_activate
447 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
452 vlc_object_release( p_tmp->p_module );
453 p_tmp = p_tmp->p_next;
456 /* Store the locked module value */
459 p_module = p_tmp->p_module;
460 p_tmp = p_tmp->p_next;
467 /* Unlock the remaining modules */
468 while( p_tmp != NULL )
470 vlc_object_release( p_tmp->p_module );
471 p_tmp = p_tmp->p_next;
476 if( p_module != NULL )
478 msg_Dbg( p_module, "using %s module \"%s\"",
479 psz_capability, p_module->psz_object_name );
481 else if( p_first == NULL )
483 msg_Err( p_this, "no %s module matched \"%s\"",
484 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
486 else if( psz_name != NULL && *psz_name )
488 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
489 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
494 free( psz_shortcuts );
502 /* Don't forget that the module is still locked */
506 /*****************************************************************************
507 * module_Unneed: decrease the usage count of a module.
508 *****************************************************************************
509 * This function must be called by the thread that called module_Need, to
510 * decrease the reference count and allow for hiding of modules.
511 *****************************************************************************/
512 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
514 /* Use the close method */
515 if( p_module->pf_deactivate )
517 p_module->pf_deactivate( p_this );
520 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
522 vlc_object_release( p_module );
527 /*****************************************************************************
528 * Following functions are local.
529 *****************************************************************************/
531 /*****************************************************************************
532 * AllocateAllPlugins: load all plugin modules we can find.
533 *****************************************************************************/
534 #ifdef HAVE_DYNAMIC_PLUGINS
535 static void AllocateAllPlugins( vlc_object_t *p_this )
537 /* Yes, there are two NULLs because we replace one with "plugin-path". */
538 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
541 char ** ppsz_path = path;
543 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
544 char * psz_vlcpath = system_GetProgramPath();
545 int i_vlclen = strlen( psz_vlcpath );
546 vlc_bool_t b_notinroot;
549 /* If the user provided a plugin path, we add it to the list */
550 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
553 for( ; *ppsz_path != NULL ; ppsz_path++ )
555 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
556 /* Store strlen(*ppsz_path) for later use. */
557 int i_dirlen = strlen( *ppsz_path );
559 b_notinroot = VLC_FALSE;
560 /* Under BeOS, we need to add beos_GetProgramPath() to access
561 * files under the current directory */
562 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
564 i_dirlen += i_vlclen + 2;
565 b_notinroot = VLC_TRUE;
567 psz_fullpath = malloc( i_dirlen );
568 if( psz_fullpath == NULL )
572 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
577 psz_fullpath = *ppsz_path;
580 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
582 /* Don't go deeper than 5 subdirectories */
583 AllocatePluginDir( p_this, psz_fullpath, 5 );
585 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
588 free( psz_fullpath );
593 /* Free plugin-path */
594 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
595 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
598 /*****************************************************************************
599 * AllocatePluginDir: recursively parse a directory to look for plugins
600 *****************************************************************************/
601 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
608 struct dirent * file;
615 dir = opendir( psz_dir );
622 i_dirlen = strlen( psz_dir );
624 /* Parse the directory and try to load all files it contains. */
625 while( (file = readdir( dir )) )
632 /* Skip ".", ".." and anything starting with "." */
633 if( !*file->d_name || *file->d_name == '.' )
638 i_len = strlen( file->d_name );
640 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
641 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
644 if( GetFileAttributes( psz_file ) & FILE_ATTRIBUTE_DIRECTORY )
646 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
649 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
651 else if( i_len > strlen( LIBEXT )
652 /* We only load files ending with LIBEXT */
653 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
654 LIBEXT, strlen( LIBEXT ) ) )
656 AllocatePluginFile( p_this, psz_file );
662 /* Close the directory */
666 /*****************************************************************************
667 * AllocatePluginFile: load a module into memory and initialize it.
668 *****************************************************************************
669 * This function loads a dynamically loadable module and allocates a structure
670 * for its information data. The module can then be handled by module_Need
671 * and module_Unneed. It can be removed by DeleteModule.
672 *****************************************************************************/
673 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
676 module_handle_t handle;
678 /* Try to dynamically load the module. */
679 if( module_load( psz_file, &handle ) )
681 char psz_buffer[256];
683 /* The plugin module couldn't be opened */
684 msg_Warn( p_this, "cannot open `%s' (%s)",
685 psz_file, module_error( psz_buffer ) );
689 /* Now that we have successfully loaded the module, we can
690 * allocate a structure for it */
691 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
692 if( p_module == NULL )
694 msg_Err( p_this, "out of memory" );
695 module_unload( handle );
699 /* We need to fill these since they may be needed by CallEntry() */
700 p_module->psz_filename = psz_file;
701 p_module->handle = handle;
702 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
704 /* Initialize the module: fill p_module->psz_object_name, default config */
705 if( CallEntry( p_module ) != 0 )
707 /* We couldn't call module_init() */
708 vlc_object_destroy( p_module );
709 module_unload( handle );
713 DupModule( p_module );
714 p_module->psz_filename = strdup( p_module->psz_filename );
715 p_module->psz_longname = strdup( p_module->psz_longname );
717 /* Everything worked fine ! The module is ready to be added to the list. */
718 p_module->b_builtin = VLC_FALSE;
720 /* msg_Dbg( p_this, "plugin \"%s\", %s",
721 p_module->psz_object_name, p_module->psz_longname ); */
723 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
728 /*****************************************************************************
729 * DupModule: make a plugin module standalone.
730 *****************************************************************************
731 * This function duplicates all strings in the module, so that the dynamic
732 * object can be unloaded. It acts recursively on submodules.
733 *****************************************************************************/
734 static void DupModule( module_t *p_module )
739 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
741 *pp_shortcut = strdup( *pp_shortcut );
744 /* We strdup() these entries so that they are still valid when the
745 * module is unloaded. */
746 p_module->psz_object_name = strdup( p_module->psz_object_name );
747 p_module->psz_capability = strdup( p_module->psz_capability );
749 if( p_module->psz_program != NULL )
751 p_module->psz_program = strdup( p_module->psz_program );
754 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
756 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
760 /*****************************************************************************
761 * UndupModule: free a duplicated module.
762 *****************************************************************************
763 * This function frees the allocations done in DupModule().
764 *****************************************************************************/
765 static void UndupModule( module_t *p_module )
770 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
772 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
775 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
777 free( *pp_shortcut );
780 free( p_module->psz_object_name );
781 free( p_module->psz_capability );
783 if( p_module->psz_program != NULL )
785 free( p_module->psz_program );
789 #endif /* HAVE_DYNAMIC_PLUGINS */
791 /*****************************************************************************
792 * AllocateBuiltinModule: initialize a builtin module.
793 *****************************************************************************
794 * This function registers a builtin module and allocates a structure
795 * for its information data. The module can then be handled by module_Need
796 * and module_Unneed. It can be removed by DeleteModule.
797 *****************************************************************************/
798 static int AllocateBuiltinModule( vlc_object_t * p_this,
799 int ( *pf_entry ) ( module_t * ) )
803 /* Now that we have successfully loaded the module, we can
804 * allocate a structure for it */
805 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
806 if( p_module == NULL )
808 msg_Err( p_this, "out of memory" );
812 /* Initialize the module : fill p_module->psz_object_name, etc. */
813 if( pf_entry( p_module ) != 0 )
815 /* With a well-written module we shouldn't have to print an
816 * additional error message here, but just make sure. */
817 msg_Err( p_this, "failed calling entry point in builtin module" );
818 vlc_object_destroy( p_module );
822 /* Everything worked fine ! The module is ready to be added to the list. */
823 p_module->b_builtin = VLC_TRUE;
825 /* msg_Dbg( p_this, "builtin \"%s\", %s",
826 p_module->psz_object_name, p_module->psz_longname ); */
828 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
833 /*****************************************************************************
834 * DeleteModule: delete a module and its structure.
835 *****************************************************************************
836 * This function can only be called if the module isn't being used.
837 *****************************************************************************/
838 static int DeleteModule( module_t * p_module )
840 vlc_object_detach( p_module );
842 /* We free the structures that we strdup()ed in Allocate*Module(). */
843 #ifdef HAVE_DYNAMIC_PLUGINS
844 if( !p_module->b_builtin )
846 if( p_module->b_unloadable )
848 module_unload( p_module->handle );
850 UndupModule( p_module );
851 free( p_module->psz_filename );
852 free( p_module->psz_longname );
856 /* Free and detach the object's children */
857 while( p_module->i_children )
859 vlc_object_t *p_this = p_module->pp_children[0];
860 vlc_object_detach( p_this );
861 vlc_object_destroy( p_this );
864 config_Free( p_module );
865 vlc_object_destroy( p_module );
870 #ifdef HAVE_DYNAMIC_PLUGINS
871 /*****************************************************************************
872 * CallEntry: call an entry point.
873 *****************************************************************************
874 * This function calls a symbol given its name and a module structure. The
875 * symbol MUST refer to a function returning int and taking a module_t* as
877 *****************************************************************************/
878 static int CallEntry( module_t * p_module )
880 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
881 int (* pf_symbol) ( module_t * p_module );
883 /* Try to resolve the symbol */
884 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
887 if( pf_symbol == NULL )
889 char psz_buffer[256];
891 /* We couldn't load the symbol */
892 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
893 psz_name, p_module->psz_filename,
894 module_error( psz_buffer ) );
898 /* We can now try to call the symbol */
899 if( pf_symbol( p_module ) != 0 )
901 /* With a well-written module we shouldn't have to print an
902 * additional error message here, but just make sure. */
903 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
904 psz_name, p_module->psz_filename );
908 /* Everything worked fine, we can return */
911 #endif /* HAVE_DYNAMIC_PLUGINS */