1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.101 2002/11/09 17:44:09 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 #include <sys/types.h>
50 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
51 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
52 # define HAVE_DYNAMIC_PLUGINS
53 #elif defined(HAVE_IMAGE_H) /* BeOS */
55 # define HAVE_DYNAMIC_PLUGINS
57 # define HAVE_DYNAMIC_PLUGINS
59 # undef HAVE_DYNAMIC_PLUGINS
65 #include "interface.h"
66 #include "vlc_playlist.h"
67 #include "intf_eject.h"
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
72 #include "input_ext-plugins.h"
75 #include "video_output.h"
77 #include "audio_output.h"
78 #include "aout_internal.h"
80 #include "stream_output.h"
84 #ifdef HAVE_DYNAMIC_PLUGINS
85 # include "modules_plugin.h"
88 #if !defined( _MSC_VER )
89 # include "modules_builtin.h"
91 # include "modules_builtin_msvc.h"
94 /*****************************************************************************
96 *****************************************************************************/
97 #ifdef HAVE_DYNAMIC_PLUGINS
98 static void AllocateAllPlugins ( vlc_object_t * );
99 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
100 static int AllocatePluginFile ( vlc_object_t *, char * );
102 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
103 static int DeleteModule ( module_t * );
104 #ifdef HAVE_DYNAMIC_PLUGINS
105 static void DupModule ( module_t * );
106 static void UndupModule ( module_t * );
107 static int CallEntry ( module_t * );
110 /*****************************************************************************
111 * module_InitBank: create the module bank.
112 *****************************************************************************
113 * This function creates a module bank structure which will be filled later
114 * on with all the modules found.
115 *****************************************************************************/
116 void __module_InitBank( vlc_object_t *p_this )
118 module_bank_t *p_bank;
120 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
121 p_bank->psz_object_name = "module bank";
124 * Store the symbols to be exported
126 #ifdef HAVE_DYNAMIC_PLUGINS
127 STORE_SYMBOLS( &p_bank->symbols );
130 /* Everything worked, attach the object */
131 p_this->p_libvlc->p_module_bank = p_bank;
132 vlc_object_attach( p_bank, p_this->p_libvlc );
137 /*****************************************************************************
138 * module_ResetBank: reset the module bank.
139 *****************************************************************************
140 * This function resets the module bank by unloading all unused plugin
142 *****************************************************************************/
143 void __module_ResetBank( vlc_object_t *p_this )
145 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
149 /*****************************************************************************
150 * module_EndBank: empty the module bank.
151 *****************************************************************************
152 * This function unloads all unused plugin modules and empties the module
153 * bank in case of success.
154 *****************************************************************************/
155 void __module_EndBank( vlc_object_t *p_this )
159 vlc_object_detach( p_this->p_libvlc->p_module_bank );
161 while( p_this->p_libvlc->p_module_bank->i_children )
163 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
165 if( DeleteModule( p_next ) )
167 /* Module deletion failed */
168 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
169 p_next->psz_object_name );
171 /* We just free the module by hand. Niahahahahaha. */
172 vlc_object_detach( p_next );
173 vlc_object_destroy( p_next );
177 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
182 /*****************************************************************************
183 * module_LoadMain: load the main program info into the module bank.
184 *****************************************************************************
185 * This function fills the module bank structure with the main module infos.
186 * This is very useful as it will allow us to consider the main program just
187 * as another module, and for instance the configuration options of main will
188 * be available in the module bank structure just as for every other module.
189 *****************************************************************************/
190 void __module_LoadMain( vlc_object_t *p_this )
192 AllocateBuiltinModule( p_this, vlc_entry__main );
195 /*****************************************************************************
196 * module_LoadBuiltins: load all modules which we built with.
197 *****************************************************************************
198 * This function fills the module bank structure with the builtin modules.
199 *****************************************************************************/
200 void __module_LoadBuiltins( vlc_object_t * p_this )
202 msg_Dbg( p_this, "checking builtin modules" );
203 ALLOCATE_ALL_BUILTINS();
206 /*****************************************************************************
207 * module_LoadPlugins: load all plugin modules we can find.
208 *****************************************************************************
209 * This function fills the module bank structure with the plugin modules.
210 *****************************************************************************/
211 void __module_LoadPlugins( vlc_object_t * p_this )
213 #ifdef HAVE_DYNAMIC_PLUGINS
214 msg_Dbg( p_this, "checking plugin modules" );
215 AllocateAllPlugins( p_this );
219 /*****************************************************************************
220 * module_Need: return the best module function, given a capability list.
221 *****************************************************************************
222 * This function returns the module that best fits the asked capabilities.
223 *****************************************************************************/
224 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
225 const char *psz_name )
227 typedef struct module_list_t module_list_t;
233 module_list_t *p_next;
236 module_list_t *p_list, *p_first, *p_tmp;
240 vlc_bool_t b_intf = VLC_FALSE;
243 module_t **pp_parser;
246 char *psz_shortcuts = NULL, *psz_var = NULL;
248 msg_Dbg( p_this, "looking for %s module", psz_capability );
250 /* Deal with variables */
251 if( psz_name && psz_name[0] == '$' )
253 psz_var = config_GetPsz( p_this, psz_name + 1 );
257 /* Count how many different shortcuts were asked for */
258 if( psz_name && *psz_name )
262 /* If the user wants none, give him none. */
263 if( !strcmp( psz_name, "none" ) )
265 if( psz_var ) free( psz_var );
270 psz_shortcuts = strdup( psz_name );
272 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
274 if( *psz_parser == ',' )
282 /* Sort the modules and test them */
283 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
284 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
287 /* Parse the module list for capabilities and probe each of them */
288 for( pp_parser = (module_t**)p_all->pp_objects ; *pp_parser ; pp_parser++ )
290 module_t * p_submodule = NULL;
291 int i_shortcut_bonus = 0, i_submodule;
293 p_module = *pp_parser;
295 /* Test that this module can do what we need */
296 if( strcmp( p_module->psz_capability, psz_capability ) )
298 for( i_submodule = 0;
299 i_submodule < p_module->i_children;
302 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
303 ->psz_capability, psz_capability ) )
306 (module_t*)p_module->pp_children[ i_submodule ];
311 if( p_submodule == NULL )
316 p_module = p_submodule;
319 /* Test if we have the required CPU */
320 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
325 /* If we required a shortcut, check this plugin provides it. */
328 vlc_bool_t b_trash = VLC_TRUE;
329 int i_dummy, i_short = i_shortcuts;
330 char *psz_name = psz_shortcuts;
335 b_trash && p_module->pp_shortcuts[i_dummy];
338 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
339 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
344 i_shortcut_bonus = i_short * 10000;
348 /* Go to the next shortcut... This is so lame! */
362 /* If we didn't require a shortcut, trash zero-scored plugins */
363 else if( !p_module->i_score )
368 /* Special case: test if we requested a particular intf plugin */
369 if( p_module->psz_program
370 && !strcmp( p_module->psz_program,
371 p_this->p_vlc->psz_object_name ) )
375 /* Remove previous non-matching plugins */
382 /* This one doesn't match */
386 /* Store this new module */
387 p_list[ i_index ].p_module = p_module;
388 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
390 /* Add it to the modules-to-probe list */
393 p_list[ 0 ].p_next = NULL;
398 /* Ok, so at school you learned that quicksort is quick, and
399 * bubble sort sucks raw eggs. But that's when dealing with
400 * thousands of items. Here we have barely 50. */
401 module_list_t *p_newlist = p_first;
403 if( p_first->i_score < p_list[ i_index ].i_score )
405 p_list[ i_index ].p_next = p_first;
406 p_first = &p_list[ i_index ];
410 while( p_newlist->p_next != NULL &&
411 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
413 p_newlist = p_newlist->p_next;
416 p_list[ i_index ].p_next = p_newlist->p_next;
417 p_newlist->p_next = &p_list[ i_index ];
424 msg_Dbg( p_this, "probing %i candidate%s",
425 i_index, i_index == 1 ? "" : "s" );
427 /* Lock all candidate modules */
429 while( p_tmp != NULL )
431 vlc_object_yield( p_tmp->p_module );
432 p_tmp = p_tmp->p_next;
435 /* We can release the list, interesting modules were yielded */
436 vlc_list_release( p_all );
438 /* Parse the linked list and use the first successful module */
440 while( p_tmp != NULL )
442 if( p_tmp->p_module->pf_activate
443 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
448 vlc_object_release( p_tmp->p_module );
449 p_tmp = p_tmp->p_next;
452 /* Store the locked module value */
455 p_module = p_tmp->p_module;
456 p_tmp = p_tmp->p_next;
463 /* Unlock the remaining modules */
464 while( p_tmp != NULL )
466 vlc_object_release( p_tmp->p_module );
467 p_tmp = p_tmp->p_next;
472 if( p_module != NULL )
474 msg_Dbg( p_module, "using %s module \"%s\"",
475 psz_capability, p_module->psz_object_name );
477 else if( p_first == NULL )
479 msg_Err( p_this, "no %s module matched \"%s\"",
480 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
482 else if( psz_name != NULL && *psz_name )
484 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
485 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
490 free( psz_shortcuts );
498 /* Don't forget that the module is still locked */
502 /*****************************************************************************
503 * module_Unneed: decrease the usage count of a module.
504 *****************************************************************************
505 * This function must be called by the thread that called module_Need, to
506 * decrease the reference count and allow for hiding of modules.
507 *****************************************************************************/
508 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
510 /* Use the close method */
511 if( p_module->pf_deactivate )
513 p_module->pf_deactivate( p_this );
516 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
518 vlc_object_release( p_module );
523 /*****************************************************************************
524 * Following functions are local.
525 *****************************************************************************/
527 /*****************************************************************************
528 * AllocateAllPlugins: load all plugin modules we can find.
529 *****************************************************************************/
530 #ifdef HAVE_DYNAMIC_PLUGINS
531 static void AllocateAllPlugins( vlc_object_t *p_this )
533 /* Yes, there are two NULLs because we replace one with "plugin-path". */
534 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
537 char ** ppsz_path = path;
539 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
540 char * psz_vlcpath = system_GetProgramPath();
541 int i_vlclen = strlen( psz_vlcpath );
542 vlc_bool_t b_notinroot;
545 /* If the user provided a plugin path, we add it to the list */
546 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
549 for( ; *ppsz_path != NULL ; ppsz_path++ )
551 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
552 /* Store strlen(*ppsz_path) for later use. */
553 int i_dirlen = strlen( *ppsz_path );
555 b_notinroot = VLC_FALSE;
556 /* Under BeOS, we need to add beos_GetProgramPath() to access
557 * files under the current directory */
558 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
560 i_dirlen += i_vlclen + 2;
561 b_notinroot = VLC_TRUE;
563 psz_fullpath = malloc( i_dirlen );
564 if( psz_fullpath == NULL )
568 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
573 psz_fullpath = *ppsz_path;
576 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
578 /* Don't go deeper than 5 subdirectories */
579 AllocatePluginDir( p_this, psz_fullpath, 5 );
581 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
584 free( psz_fullpath );
589 /* Free plugin-path */
590 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
591 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
594 /*****************************************************************************
595 * AllocatePluginDir: recursively parse a directory to look for plugins
596 *****************************************************************************/
597 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
604 struct dirent * file;
611 dir = opendir( psz_dir );
618 i_dirlen = strlen( psz_dir );
620 /* Parse the directory and try to load all files it contains. */
621 while( (file = readdir( dir )) )
626 /* Skip ".", ".." and anything starting with "." */
627 if( !*file->d_name || *file->d_name == '.' )
632 i_len = strlen( file->d_name );
634 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
635 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
637 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
639 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
641 else if( i_len > strlen( LIBEXT )
642 /* We only load files ending with LIBEXT */
643 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
644 LIBEXT, strlen( LIBEXT ) ) )
646 AllocatePluginFile( p_this, psz_file );
652 /* Close the directory */
656 /*****************************************************************************
657 * AllocatePluginFile: load a module into memory and initialize it.
658 *****************************************************************************
659 * This function loads a dynamically loadable module and allocates a structure
660 * for its information data. The module can then be handled by module_Need
661 * and module_Unneed. It can be removed by DeleteModule.
662 *****************************************************************************/
663 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
666 module_handle_t handle;
668 /* Try to dynamically load the module. */
669 if( module_load( psz_file, &handle ) )
671 char psz_buffer[256];
673 /* The plugin module couldn't be opened */
674 msg_Warn( p_this, "cannot open `%s' (%s)",
675 psz_file, module_error( psz_buffer ) );
679 /* Now that we have successfully loaded the module, we can
680 * allocate a structure for it */
681 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
682 if( p_module == NULL )
684 msg_Err( p_this, "out of memory" );
685 module_unload( handle );
689 /* We need to fill these since they may be needed by CallEntry() */
690 p_module->psz_filename = psz_file;
691 p_module->handle = handle;
692 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
694 /* Initialize the module: fill p_module->psz_object_name, default config */
695 if( CallEntry( p_module ) != 0 )
697 /* We couldn't call module_init() */
698 vlc_object_destroy( p_module );
699 module_unload( handle );
703 DupModule( p_module );
704 p_module->psz_filename = strdup( p_module->psz_filename );
705 p_module->psz_longname = strdup( p_module->psz_longname );
707 /* Everything worked fine ! The module is ready to be added to the list. */
708 p_module->b_builtin = VLC_FALSE;
710 /* msg_Dbg( p_this, "plugin \"%s\", %s",
711 p_module->psz_object_name, p_module->psz_longname ); */
713 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
718 /*****************************************************************************
719 * DupModule: make a plugin module standalone.
720 *****************************************************************************
721 * This function duplicates all strings in the module, so that the dynamic
722 * object can be unloaded. It acts recursively on submodules.
723 *****************************************************************************/
724 static void DupModule( module_t *p_module )
729 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
731 *pp_shortcut = strdup( *pp_shortcut );
734 /* We strdup() these entries so that they are still valid when the
735 * module is unloaded. */
736 p_module->psz_object_name = strdup( p_module->psz_object_name );
737 p_module->psz_capability = strdup( p_module->psz_capability );
739 if( p_module->psz_program != NULL )
741 p_module->psz_program = strdup( p_module->psz_program );
744 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
746 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
750 /*****************************************************************************
751 * UndupModule: free a duplicated module.
752 *****************************************************************************
753 * This function frees the allocations done in DupModule().
754 *****************************************************************************/
755 static void UndupModule( module_t *p_module )
760 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
762 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
765 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
767 free( *pp_shortcut );
770 free( p_module->psz_object_name );
771 free( p_module->psz_capability );
773 if( p_module->psz_program != NULL )
775 free( p_module->psz_program );
779 #endif /* HAVE_DYNAMIC_PLUGINS */
781 /*****************************************************************************
782 * AllocateBuiltinModule: initialize a builtin module.
783 *****************************************************************************
784 * This function registers a builtin module and allocates a structure
785 * for its information data. The module can then be handled by module_Need
786 * and module_Unneed. It can be removed by DeleteModule.
787 *****************************************************************************/
788 static int AllocateBuiltinModule( vlc_object_t * p_this,
789 int ( *pf_entry ) ( module_t * ) )
793 /* Now that we have successfully loaded the module, we can
794 * allocate a structure for it */
795 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
796 if( p_module == NULL )
798 msg_Err( p_this, "out of memory" );
802 /* Initialize the module : fill p_module->psz_object_name, etc. */
803 if( pf_entry( p_module ) != 0 )
805 /* With a well-written module we shouldn't have to print an
806 * additional error message here, but just make sure. */
807 msg_Err( p_this, "failed calling entry point in builtin module" );
808 vlc_object_destroy( p_module );
812 /* Everything worked fine ! The module is ready to be added to the list. */
813 p_module->b_builtin = VLC_TRUE;
815 /* msg_Dbg( p_this, "builtin \"%s\", %s",
816 p_module->psz_object_name, p_module->psz_longname ); */
818 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
823 /*****************************************************************************
824 * DeleteModule: delete a module and its structure.
825 *****************************************************************************
826 * This function can only be called if the module isn't being used.
827 *****************************************************************************/
828 static int DeleteModule( module_t * p_module )
830 vlc_object_detach( p_module );
832 /* We free the structures that we strdup()ed in Allocate*Module(). */
833 #ifdef HAVE_DYNAMIC_PLUGINS
834 if( !p_module->b_builtin )
836 if( p_module->b_unloadable )
838 module_unload( p_module->handle );
840 UndupModule( p_module );
841 free( p_module->psz_filename );
842 free( p_module->psz_longname );
846 /* Free and detach the object's children */
847 while( p_module->i_children )
849 vlc_object_t *p_this = p_module->pp_children[0];
850 vlc_object_detach( p_this );
851 vlc_object_destroy( p_this );
854 config_Free( p_module );
855 vlc_object_destroy( p_module );
860 #ifdef HAVE_DYNAMIC_PLUGINS
861 /*****************************************************************************
862 * CallEntry: call an entry point.
863 *****************************************************************************
864 * This function calls a symbol given its name and a module structure. The
865 * symbol MUST refer to a function returning int and taking a module_t* as
867 *****************************************************************************/
868 static int CallEntry( module_t * p_module )
870 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
871 int (* pf_symbol) ( module_t * p_module );
873 /* Try to resolve the symbol */
874 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
877 if( pf_symbol == NULL )
879 char psz_buffer[256];
881 /* We couldn't load the symbol */
882 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
883 psz_name, p_module->psz_filename,
884 module_error( psz_buffer ) );
888 /* We can now try to call the symbol */
889 if( pf_symbol( p_module ) != 0 )
891 /* With a well-written module we shouldn't have to print an
892 * additional error message here, but just make sure. */
893 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
894 psz_name, p_module->psz_filename );
898 /* Everything worked fine, we can return */
901 #endif /* HAVE_DYNAMIC_PLUGINS */