1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.103 2002/11/11 14:39:12 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
60 #elif defined(UNDER_CE)
61 # define HAVE_DYNAMIC_PLUGINS
63 # define HAVE_DYNAMIC_PLUGINS
65 # undef HAVE_DYNAMIC_PLUGINS
71 #include "interface.h"
72 #include "vlc_playlist.h"
73 #include "intf_eject.h"
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
81 #include "video_output.h"
83 #include "audio_output.h"
84 #include "aout_internal.h"
86 #include "stream_output.h"
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 # include "modules_plugin.h"
94 #if !defined( _MSC_VER )
95 # include "modules_builtin.h"
97 # include "modules_builtin_msvc.h"
100 /*****************************************************************************
102 *****************************************************************************/
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static void AllocateAllPlugins ( vlc_object_t * );
105 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
106 static int AllocatePluginFile ( vlc_object_t *, char * );
108 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
109 static int DeleteModule ( module_t * );
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 static void DupModule ( module_t * );
112 static void UndupModule ( module_t * );
113 static int CallEntry ( module_t * );
116 /*****************************************************************************
117 * module_InitBank: create the module bank.
118 *****************************************************************************
119 * This function creates a module bank structure which will be filled later
120 * on with all the modules found.
121 *****************************************************************************/
122 void __module_InitBank( vlc_object_t *p_this )
124 module_bank_t *p_bank;
126 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
127 p_bank->psz_object_name = "module bank";
130 * Store the symbols to be exported
132 #ifdef HAVE_DYNAMIC_PLUGINS
133 STORE_SYMBOLS( &p_bank->symbols );
136 /* Everything worked, attach the object */
137 p_this->p_libvlc->p_module_bank = p_bank;
138 vlc_object_attach( p_bank, p_this->p_libvlc );
143 /*****************************************************************************
144 * module_ResetBank: reset the module bank.
145 *****************************************************************************
146 * This function resets the module bank by unloading all unused plugin
148 *****************************************************************************/
149 void __module_ResetBank( vlc_object_t *p_this )
151 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
155 /*****************************************************************************
156 * module_EndBank: empty the module bank.
157 *****************************************************************************
158 * This function unloads all unused plugin modules and empties the module
159 * bank in case of success.
160 *****************************************************************************/
161 void __module_EndBank( vlc_object_t *p_this )
165 vlc_object_detach( p_this->p_libvlc->p_module_bank );
167 while( p_this->p_libvlc->p_module_bank->i_children )
169 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
171 if( DeleteModule( p_next ) )
173 /* Module deletion failed */
174 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
175 p_next->psz_object_name );
177 /* We just free the module by hand. Niahahahahaha. */
178 vlc_object_detach( p_next );
179 vlc_object_destroy( p_next );
183 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
188 /*****************************************************************************
189 * module_LoadMain: load the main program info into the module bank.
190 *****************************************************************************
191 * This function fills the module bank structure with the main module infos.
192 * This is very useful as it will allow us to consider the main program just
193 * as another module, and for instance the configuration options of main will
194 * be available in the module bank structure just as for every other module.
195 *****************************************************************************/
196 void __module_LoadMain( vlc_object_t *p_this )
198 AllocateBuiltinModule( p_this, vlc_entry__main );
201 /*****************************************************************************
202 * module_LoadBuiltins: load all modules which we built with.
203 *****************************************************************************
204 * This function fills the module bank structure with the builtin modules.
205 *****************************************************************************/
206 void __module_LoadBuiltins( vlc_object_t * p_this )
208 msg_Dbg( p_this, "checking builtin modules" );
209 ALLOCATE_ALL_BUILTINS();
212 /*****************************************************************************
213 * module_LoadPlugins: load all plugin modules we can find.
214 *****************************************************************************
215 * This function fills the module bank structure with the plugin modules.
216 *****************************************************************************/
217 void __module_LoadPlugins( vlc_object_t * p_this )
219 #ifdef HAVE_DYNAMIC_PLUGINS
220 msg_Dbg( p_this, "checking plugin modules" );
221 AllocateAllPlugins( p_this );
225 /*****************************************************************************
226 * module_Need: return the best module function, given a capability list.
227 *****************************************************************************
228 * This function returns the module that best fits the asked capabilities.
229 *****************************************************************************/
230 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
231 const char *psz_name )
233 typedef struct module_list_t module_list_t;
239 module_list_t *p_next;
242 module_list_t *p_list, *p_first, *p_tmp;
246 vlc_bool_t b_intf = VLC_FALSE;
249 module_t **pp_parser;
252 char *psz_shortcuts = NULL, *psz_var = NULL;
254 msg_Dbg( p_this, "looking for %s module", psz_capability );
256 /* Deal with variables */
257 if( psz_name && psz_name[0] == '$' )
259 psz_var = config_GetPsz( p_this, psz_name + 1 );
263 /* Count how many different shortcuts were asked for */
264 if( psz_name && *psz_name )
268 /* If the user wants none, give him none. */
269 if( !strcmp( psz_name, "none" ) )
271 if( psz_var ) free( psz_var );
276 psz_shortcuts = strdup( psz_name );
278 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
280 if( *psz_parser == ',' )
288 /* Sort the modules and test them */
289 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
290 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
293 /* Parse the module list for capabilities and probe each of them */
294 for( pp_parser = (module_t**)p_all->pp_objects ; *pp_parser ; pp_parser++ )
296 module_t * p_submodule = NULL;
297 int i_shortcut_bonus = 0, i_submodule;
299 p_module = *pp_parser;
301 /* Test that this module can do what we need */
302 if( strcmp( p_module->psz_capability, psz_capability ) )
304 for( i_submodule = 0;
305 i_submodule < p_module->i_children;
308 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
309 ->psz_capability, psz_capability ) )
312 (module_t*)p_module->pp_children[ i_submodule ];
317 if( p_submodule == NULL )
322 p_module = p_submodule;
325 /* Test if we have the required CPU */
326 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
331 /* If we required a shortcut, check this plugin provides it. */
334 vlc_bool_t b_trash = VLC_TRUE;
335 int i_dummy, i_short = i_shortcuts;
336 char *psz_name = psz_shortcuts;
341 b_trash && p_module->pp_shortcuts[i_dummy];
344 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
345 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
350 i_shortcut_bonus = i_short * 10000;
354 /* Go to the next shortcut... This is so lame! */
368 /* If we didn't require a shortcut, trash zero-scored plugins */
369 else if( !p_module->i_score )
374 /* Special case: test if we requested a particular intf plugin */
375 if( p_module->psz_program
376 && !strcmp( p_module->psz_program,
377 p_this->p_vlc->psz_object_name ) )
381 /* Remove previous non-matching plugins */
388 /* This one doesn't match */
392 /* Store this new module */
393 p_list[ i_index ].p_module = p_module;
394 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
396 /* Add it to the modules-to-probe list */
399 p_list[ 0 ].p_next = NULL;
404 /* Ok, so at school you learned that quicksort is quick, and
405 * bubble sort sucks raw eggs. But that's when dealing with
406 * thousands of items. Here we have barely 50. */
407 module_list_t *p_newlist = p_first;
409 if( p_first->i_score < p_list[ i_index ].i_score )
411 p_list[ i_index ].p_next = p_first;
412 p_first = &p_list[ i_index ];
416 while( p_newlist->p_next != NULL &&
417 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
419 p_newlist = p_newlist->p_next;
422 p_list[ i_index ].p_next = p_newlist->p_next;
423 p_newlist->p_next = &p_list[ i_index ];
430 msg_Dbg( p_this, "probing %i candidate%s",
431 i_index, i_index == 1 ? "" : "s" );
433 /* Lock all candidate modules */
435 while( p_tmp != NULL )
437 vlc_object_yield( p_tmp->p_module );
438 p_tmp = p_tmp->p_next;
441 /* We can release the list, interesting modules were yielded */
442 vlc_list_release( p_all );
444 /* Parse the linked list and use the first successful module */
446 while( p_tmp != NULL )
448 if( p_tmp->p_module->pf_activate
449 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
454 vlc_object_release( p_tmp->p_module );
455 p_tmp = p_tmp->p_next;
458 /* Store the locked module value */
461 p_module = p_tmp->p_module;
462 p_tmp = p_tmp->p_next;
469 /* Unlock the remaining modules */
470 while( p_tmp != NULL )
472 vlc_object_release( p_tmp->p_module );
473 p_tmp = p_tmp->p_next;
478 if( p_module != NULL )
480 msg_Dbg( p_module, "using %s module \"%s\"",
481 psz_capability, p_module->psz_object_name );
483 else if( p_first == NULL )
485 msg_Err( p_this, "no %s module matched \"%s\"",
486 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
488 else if( psz_name != NULL && *psz_name )
490 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
491 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
496 free( psz_shortcuts );
504 /* Don't forget that the module is still locked */
508 /*****************************************************************************
509 * module_Unneed: decrease the usage count of a module.
510 *****************************************************************************
511 * This function must be called by the thread that called module_Need, to
512 * decrease the reference count and allow for hiding of modules.
513 *****************************************************************************/
514 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
516 /* Use the close method */
517 if( p_module->pf_deactivate )
519 p_module->pf_deactivate( p_this );
522 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
524 vlc_object_release( p_module );
529 /*****************************************************************************
530 * Following functions are local.
531 *****************************************************************************/
533 /*****************************************************************************
534 * AllocateAllPlugins: load all plugin modules we can find.
535 *****************************************************************************/
536 #ifdef HAVE_DYNAMIC_PLUGINS
537 static void AllocateAllPlugins( vlc_object_t *p_this )
539 /* Yes, there are two NULLs because we replace one with "plugin-path". */
540 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
543 char ** ppsz_path = path;
545 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
546 char * psz_vlcpath = system_GetProgramPath();
547 int i_vlclen = strlen( psz_vlcpath );
548 vlc_bool_t b_notinroot;
551 /* If the user provided a plugin path, we add it to the list */
552 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
555 for( ; *ppsz_path != NULL ; ppsz_path++ )
557 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
558 /* Store strlen(*ppsz_path) for later use. */
559 int i_dirlen = strlen( *ppsz_path );
561 b_notinroot = VLC_FALSE;
562 /* Under BeOS, we need to add beos_GetProgramPath() to access
563 * files under the current directory */
564 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
566 i_dirlen += i_vlclen + 2;
567 b_notinroot = VLC_TRUE;
569 psz_fullpath = malloc( i_dirlen );
570 if( psz_fullpath == NULL )
574 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
579 psz_fullpath = *ppsz_path;
582 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
584 /* Don't go deeper than 5 subdirectories */
585 AllocatePluginDir( p_this, psz_fullpath, 5 );
587 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
590 free( psz_fullpath );
595 /* Free plugin-path */
596 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
597 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
600 /*****************************************************************************
601 * AllocatePluginDir: recursively parse a directory to look for plugins
602 *****************************************************************************/
603 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
610 struct dirent * file;
617 dir = opendir( psz_dir );
624 i_dirlen = strlen( psz_dir );
626 /* Parse the directory and try to load all files it contains. */
627 while( (file = readdir( dir )) )
634 /* Skip ".", ".." and anything starting with "." */
635 if( !*file->d_name || *file->d_name == '.' )
640 i_len = strlen( file->d_name );
642 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
643 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
646 if( GetFileAttributes( psz_file ) & FILE_ATTRIBUTE_DIRECTORY )
648 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
651 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
653 else if( i_len > strlen( LIBEXT )
654 /* We only load files ending with LIBEXT */
655 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
656 LIBEXT, strlen( LIBEXT ) ) )
658 AllocatePluginFile( p_this, psz_file );
664 /* Close the directory */
668 /*****************************************************************************
669 * AllocatePluginFile: load a module into memory and initialize it.
670 *****************************************************************************
671 * This function loads a dynamically loadable module and allocates a structure
672 * for its information data. The module can then be handled by module_Need
673 * and module_Unneed. It can be removed by DeleteModule.
674 *****************************************************************************/
675 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
678 module_handle_t handle;
680 /* Try to dynamically load the module. */
681 if( module_load( psz_file, &handle ) )
683 char psz_buffer[256];
685 /* The plugin module couldn't be opened */
686 msg_Warn( p_this, "cannot open `%s' (%s)",
687 psz_file, module_error( psz_buffer ) );
691 /* Now that we have successfully loaded the module, we can
692 * allocate a structure for it */
693 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
694 if( p_module == NULL )
696 msg_Err( p_this, "out of memory" );
697 module_unload( handle );
701 /* We need to fill these since they may be needed by CallEntry() */
702 p_module->psz_filename = psz_file;
703 p_module->handle = handle;
704 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
706 /* Initialize the module: fill p_module->psz_object_name, default config */
707 if( CallEntry( p_module ) != 0 )
709 /* We couldn't call module_init() */
710 vlc_object_destroy( p_module );
711 module_unload( handle );
715 DupModule( p_module );
716 p_module->psz_filename = strdup( p_module->psz_filename );
717 p_module->psz_longname = strdup( p_module->psz_longname );
719 /* Everything worked fine ! The module is ready to be added to the list. */
720 p_module->b_builtin = VLC_FALSE;
722 /* msg_Dbg( p_this, "plugin \"%s\", %s",
723 p_module->psz_object_name, p_module->psz_longname ); */
725 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
730 /*****************************************************************************
731 * DupModule: make a plugin module standalone.
732 *****************************************************************************
733 * This function duplicates all strings in the module, so that the dynamic
734 * object can be unloaded. It acts recursively on submodules.
735 *****************************************************************************/
736 static void DupModule( module_t *p_module )
741 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
743 *pp_shortcut = strdup( *pp_shortcut );
746 /* We strdup() these entries so that they are still valid when the
747 * module is unloaded. */
748 p_module->psz_object_name = strdup( p_module->psz_object_name );
749 p_module->psz_capability = strdup( p_module->psz_capability );
751 if( p_module->psz_program != NULL )
753 p_module->psz_program = strdup( p_module->psz_program );
756 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
758 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
762 /*****************************************************************************
763 * UndupModule: free a duplicated module.
764 *****************************************************************************
765 * This function frees the allocations done in DupModule().
766 *****************************************************************************/
767 static void UndupModule( module_t *p_module )
772 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
774 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
777 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
779 free( *pp_shortcut );
782 free( p_module->psz_object_name );
783 free( p_module->psz_capability );
785 if( p_module->psz_program != NULL )
787 free( p_module->psz_program );
791 #endif /* HAVE_DYNAMIC_PLUGINS */
793 /*****************************************************************************
794 * AllocateBuiltinModule: initialize a builtin module.
795 *****************************************************************************
796 * This function registers a builtin module and allocates a structure
797 * for its information data. The module can then be handled by module_Need
798 * and module_Unneed. It can be removed by DeleteModule.
799 *****************************************************************************/
800 static int AllocateBuiltinModule( vlc_object_t * p_this,
801 int ( *pf_entry ) ( module_t * ) )
805 /* Now that we have successfully loaded the module, we can
806 * allocate a structure for it */
807 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
808 if( p_module == NULL )
810 msg_Err( p_this, "out of memory" );
814 /* Initialize the module : fill p_module->psz_object_name, etc. */
815 if( pf_entry( p_module ) != 0 )
817 /* With a well-written module we shouldn't have to print an
818 * additional error message here, but just make sure. */
819 msg_Err( p_this, "failed calling entry point in builtin module" );
820 vlc_object_destroy( p_module );
824 /* Everything worked fine ! The module is ready to be added to the list. */
825 p_module->b_builtin = VLC_TRUE;
827 /* msg_Dbg( p_this, "builtin \"%s\", %s",
828 p_module->psz_object_name, p_module->psz_longname ); */
830 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
835 /*****************************************************************************
836 * DeleteModule: delete a module and its structure.
837 *****************************************************************************
838 * This function can only be called if the module isn't being used.
839 *****************************************************************************/
840 static int DeleteModule( module_t * p_module )
842 vlc_object_detach( p_module );
844 /* We free the structures that we strdup()ed in Allocate*Module(). */
845 #ifdef HAVE_DYNAMIC_PLUGINS
846 if( !p_module->b_builtin )
848 if( p_module->b_unloadable )
850 module_unload( p_module->handle );
852 UndupModule( p_module );
853 free( p_module->psz_filename );
854 free( p_module->psz_longname );
858 /* Free and detach the object's children */
859 while( p_module->i_children )
861 vlc_object_t *p_this = p_module->pp_children[0];
862 vlc_object_detach( p_this );
863 vlc_object_destroy( p_this );
866 config_Free( p_module );
867 vlc_object_destroy( p_module );
872 #ifdef HAVE_DYNAMIC_PLUGINS
873 /*****************************************************************************
874 * CallEntry: call an entry point.
875 *****************************************************************************
876 * This function calls a symbol given its name and a module structure. The
877 * symbol MUST refer to a function returning int and taking a module_t* as
879 *****************************************************************************/
880 static int CallEntry( module_t * p_module )
882 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
883 int (* pf_symbol) ( module_t * p_module );
885 /* Try to resolve the symbol */
886 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
889 if( pf_symbol == NULL )
891 char psz_buffer[256];
893 /* We couldn't load the symbol */
894 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
895 psz_name, p_module->psz_filename,
896 module_error( psz_buffer ) );
900 /* We can now try to call the symbol */
901 if( pf_symbol( p_module ) != 0 )
903 /* With a well-written module we shouldn't have to print an
904 * additional error message here, but just make sure. */
905 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
906 psz_name, p_module->psz_filename );
910 /* Everything worked fine, we can return */
913 #endif /* HAVE_DYNAMIC_PLUGINS */