1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.90 2002/08/21 09:26:53 xav 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() */
39 #include <sys/types.h>
45 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
46 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
47 # define HAVE_DYNAMIC_PLUGINS
48 #elif defined(HAVE_IMAGE_H) /* BeOS */
50 # define HAVE_DYNAMIC_PLUGINS
52 # define HAVE_DYNAMIC_PLUGINS
54 # undef HAVE_DYNAMIC_PLUGINS
60 #include "interface.h"
61 #include "vlc_playlist.h"
62 #include "intf_eject.h"
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
66 #include "input_ext-dec.h"
67 #include "input_ext-plugins.h"
70 #include "video_output.h"
72 #include "audio_output.h"
73 #include "aout_internal.h"
75 #include "stream_output.h"
79 #ifdef HAVE_DYNAMIC_PLUGINS
80 # include "modules_plugin.h"
83 #if !defined( _MSC_VER )
84 # include "modules_builtin.h"
86 # include "modules_builtin_msvc.h"
89 /*****************************************************************************
91 *****************************************************************************/
92 #ifdef HAVE_DYNAMIC_PLUGINS
93 static void AllocateAllPlugins ( vlc_object_t * );
94 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
95 static int AllocatePluginFile ( vlc_object_t *, char * );
97 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
98 static int DeleteModule ( module_t * );
99 #ifdef HAVE_DYNAMIC_PLUGINS
100 static void DupModule ( module_t * );
101 static void UndupModule ( module_t * );
102 static int CallEntry ( module_t * );
105 /*****************************************************************************
106 * module_InitBank: create the module bank.
107 *****************************************************************************
108 * This function creates a module bank structure which will be filled later
109 * on with all the modules found.
110 *****************************************************************************/
111 void __module_InitBank( vlc_object_t *p_this )
113 module_bank_t *p_bank;
115 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
116 p_bank->psz_object_name = "module bank";
119 * Store the symbols to be exported
121 #ifdef HAVE_DYNAMIC_PLUGINS
122 STORE_SYMBOLS( &p_bank->symbols );
125 /* Everything worked, attach the object */
126 p_this->p_vlc->p_module_bank = p_bank;
127 vlc_object_attach( p_bank, p_this->p_vlc );
132 /*****************************************************************************
133 * module_ResetBank: reset the module bank.
134 *****************************************************************************
135 * This function resets the module bank by unloading all unused plugin
137 *****************************************************************************/
138 void __module_ResetBank( vlc_object_t *p_this )
140 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
144 /*****************************************************************************
145 * module_EndBank: empty the module bank.
146 *****************************************************************************
147 * This function unloads all unused plugin modules and empties the module
148 * bank in case of success.
149 *****************************************************************************/
150 void __module_EndBank( vlc_object_t *p_this )
154 vlc_object_detach( p_this->p_vlc->p_module_bank );
156 while( p_this->p_vlc->p_module_bank->i_children )
158 p_next = (module_t *)p_this->p_vlc->p_module_bank->pp_children[0];
160 if( DeleteModule( p_next ) )
162 /* Module deletion failed */
163 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
164 p_next->psz_object_name );
166 /* We just free the module by hand. Niahahahahaha. */
167 vlc_object_detach( p_next );
168 vlc_object_destroy( p_next );
172 vlc_object_destroy( p_this->p_vlc->p_module_bank );
177 /*****************************************************************************
178 * module_LoadMain: load the main program info into the module bank.
179 *****************************************************************************
180 * This function fills the module bank structure with the main module infos.
181 * This is very useful as it will allow us to consider the main program just
182 * as another module, and for instance the configuration options of main will
183 * be available in the module bank structure just as for every other module.
184 *****************************************************************************/
185 void __module_LoadMain( vlc_object_t *p_this )
187 AllocateBuiltinModule( p_this, vlc_entry__main );
190 /*****************************************************************************
191 * module_LoadBuiltins: load all modules which we built with.
192 *****************************************************************************
193 * This function fills the module bank structure with the builtin modules.
194 *****************************************************************************/
195 void __module_LoadBuiltins( vlc_object_t * p_this )
197 msg_Dbg( p_this, "checking builtin modules" );
198 ALLOCATE_ALL_BUILTINS();
201 /*****************************************************************************
202 * module_LoadPlugins: load all plugin modules we can find.
203 *****************************************************************************
204 * This function fills the module bank structure with the plugin modules.
205 *****************************************************************************/
206 void __module_LoadPlugins( vlc_object_t * p_this )
208 #ifdef HAVE_DYNAMIC_PLUGINS
209 msg_Dbg( p_this, "checking plugin modules" );
210 AllocateAllPlugins( p_this );
214 /*****************************************************************************
215 * module_Need: return the best module function, given a capability list.
216 *****************************************************************************
217 * This function returns the module that best fits the asked capabilities.
218 *****************************************************************************/
219 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
220 const char *psz_name )
222 typedef struct module_list_t module_list_t;
228 module_list_t *p_next;
231 module_list_t *p_list, *p_first, *p_tmp;
235 vlc_bool_t b_intf = VLC_FALSE;
238 module_t **pp_parser;
241 char *psz_shortcuts = NULL, *psz_var = NULL;
243 msg_Dbg( p_this, "looking for %s module", psz_capability );
245 /* Deal with variables */
246 if( psz_name && psz_name[0] == '$' )
248 psz_var = config_GetPsz( p_this, psz_name + 1 );
252 /* Count how many different shortcuts were asked for */
253 if( psz_name && *psz_name )
257 /* If the user wants none, give him none. */
258 if( !strcmp( psz_name, "none" ) )
260 if( psz_var ) free( psz_var );
265 psz_shortcuts = strdup( psz_name );
267 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
269 if( *psz_parser == ',' )
277 /* Sort the modules and test them */
278 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
279 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
282 /* Parse the module list for capabilities and probe each of them */
283 for( pp_parser = (module_t**)p_all->pp_objects ; *pp_parser ; pp_parser++ )
285 module_t * p_submodule = NULL;
286 int i_shortcut_bonus = 0, i_submodule;
288 p_module = *pp_parser;
290 /* Test that this module can do what we need */
291 if( strcmp( p_module->psz_capability, psz_capability ) )
293 for( i_submodule = 0;
294 i_submodule < p_module->i_children;
297 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
298 ->psz_capability, psz_capability ) )
301 (module_t*)p_module->pp_children[ i_submodule ];
306 if( p_submodule == NULL )
311 p_module = p_submodule;
314 /* Test if we have the required CPU */
315 if( (p_module->i_cpu & p_this->p_vlc->i_cpu) != p_module->i_cpu )
320 /* If we required a shortcut, check this plugin provides it. */
323 vlc_bool_t b_trash = VLC_TRUE;
324 int i_dummy, i_short = i_shortcuts;
325 char *psz_name = psz_shortcuts;
330 b_trash && p_module->pp_shortcuts[i_dummy];
333 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
334 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
339 i_shortcut_bonus = i_short * 10000;
343 /* Go to the next shortcut... This is so lame! */
357 /* If we didn't require a shortcut, trash zero-scored plugins */
358 else if( !p_module->i_score )
363 /* Special case: test if we requested a particular intf plugin */
364 if( p_module->psz_program
365 && !strcmp( p_module->psz_program,
366 p_this->p_vlc->psz_object_name ) )
370 /* Remove previous non-matching plugins */
377 /* This one doesn't match */
381 /* Store this new module */
382 p_list[ i_index ].p_module = p_module;
383 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
385 /* Add it to the modules-to-probe list */
388 p_list[ 0 ].p_next = NULL;
393 /* Ok, so at school you learned that quicksort is quick, and
394 * bubble sort sucks raw eggs. But that's when dealing with
395 * thousands of items. Here we have barely 50. */
396 module_list_t *p_newlist = p_first;
398 if( p_first->i_score < p_list[ i_index ].i_score )
400 p_list[ i_index ].p_next = p_first;
401 p_first = &p_list[ i_index ];
405 while( p_newlist->p_next != NULL &&
406 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
408 p_newlist = p_newlist->p_next;
411 p_list[ i_index ].p_next = p_newlist->p_next;
412 p_newlist->p_next = &p_list[ i_index ];
419 msg_Dbg( p_this, "probing %i candidate%s",
420 i_index, i_index == 1 ? "" : "s" );
422 /* Lock all candidate modules */
424 while( p_tmp != NULL )
426 vlc_object_yield( p_tmp->p_module );
427 p_tmp = p_tmp->p_next;
430 /* We can release the list, interesting modules were yielded */
431 vlc_list_release( p_all );
433 /* Parse the linked list and use the first successful module */
435 while( p_tmp != NULL )
437 if( p_tmp->p_module->pf_activate
438 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
443 vlc_object_release( p_tmp->p_module );
444 p_tmp = p_tmp->p_next;
447 /* Store the locked module value */
450 p_module = p_tmp->p_module;
451 p_tmp = p_tmp->p_next;
458 /* Unlock the remaining modules */
459 while( p_tmp != NULL )
461 vlc_object_release( p_tmp->p_module );
462 p_tmp = p_tmp->p_next;
467 if( p_module != NULL )
469 msg_Info( p_module, "using %s module \"%s\"",
470 psz_capability, p_module->psz_object_name );
472 else if( p_first == NULL )
474 msg_Err( p_this, "no %s module matched \"%s\"",
475 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
477 else if( psz_name != NULL && *psz_name )
479 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
480 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
485 free( psz_shortcuts );
493 /* Don't forget that the module is still locked */
497 /*****************************************************************************
498 * module_Unneed: decrease the usage count of a module.
499 *****************************************************************************
500 * This function must be called by the thread that called module_Need, to
501 * decrease the reference count and allow for hiding of modules.
502 *****************************************************************************/
503 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
505 /* Use the close method */
506 if( p_module->pf_deactivate )
508 p_module->pf_deactivate( p_this );
511 msg_Info( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
513 vlc_object_release( p_module );
518 /*****************************************************************************
519 * Following functions are local.
520 *****************************************************************************/
522 /*****************************************************************************
523 * AllocateAllPlugins: load all plugin modules we can find.
524 *****************************************************************************/
525 #ifdef HAVE_DYNAMIC_PLUGINS
526 static void AllocateAllPlugins( vlc_object_t *p_this )
528 /* Yes, there are two NULLs because we replace one with "plugin-path". */
529 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
532 char ** ppsz_path = path;
534 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
535 char * psz_vlcpath = system_GetProgramPath();
536 int i_vlclen = strlen( psz_vlcpath );
537 vlc_bool_t b_notinroot;
540 /* If the user provided a plugin path, we add it to the list */
541 path[ sizeof(path)/sizeof(char*) - 1 ] = config_GetPsz( p_this,
544 for( ; *ppsz_path != NULL ; ppsz_path++ )
546 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
547 /* Store strlen(*ppsz_path) for later use. */
548 int i_dirlen = strlen( *ppsz_path );
550 b_notinroot = VLC_FALSE;
551 /* Under BeOS, we need to add beos_GetProgramPath() to access
552 * files under the current directory */
553 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
555 i_dirlen += i_vlclen + 2;
556 b_notinroot = VLC_TRUE;
558 psz_fullpath = malloc( i_dirlen );
559 if( psz_fullpath == NULL )
563 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
568 psz_fullpath = *ppsz_path;
571 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
573 /* Don't go deeper than 5 subdirectories */
574 AllocatePluginDir( p_this, psz_fullpath, 5 );
576 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
579 free( psz_fullpath );
585 /*****************************************************************************
586 * AllocatePluginDir: recursively parse a directory to look for plugins
587 *****************************************************************************/
588 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
591 #define PLUGIN_EXT ".so"
596 struct dirent * file;
603 dir = opendir( psz_dir );
610 i_dirlen = strlen( psz_dir );
612 /* Parse the directory and try to load all files it contains. */
613 while( (file = readdir( dir )) )
616 int i_len = strlen( file->d_name );
618 /* Skip ".", ".." and anything starting with "." */
619 if( !*file->d_name || *file->d_name == '.' )
624 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
625 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
627 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
629 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
631 else if( i_len > strlen( PLUGIN_EXT )
632 /* We only load files ending with ".so" */
633 && !strncmp( file->d_name + i_len - strlen( PLUGIN_EXT ),
634 PLUGIN_EXT, strlen( PLUGIN_EXT ) ) )
636 AllocatePluginFile( p_this, psz_file );
642 /* Close the directory */
646 /*****************************************************************************
647 * AllocatePluginFile: load a module into memory and initialize it.
648 *****************************************************************************
649 * This function loads a dynamically loadable module and allocates a structure
650 * for its information data. The module can then be handled by module_Need
651 * and module_Unneed. It can be removed by DeleteModule.
652 *****************************************************************************/
653 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
656 module_handle_t handle;
658 /* Try to dynamically load the module. */
659 if( module_load( psz_file, &handle ) )
661 char psz_buffer[256];
663 /* The plugin module couldn't be opened */
664 msg_Warn( p_this, "cannot open `%s' (%s)",
665 psz_file, module_error( psz_buffer ) );
669 /* Now that we have successfully loaded the module, we can
670 * allocate a structure for it */
671 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
672 if( p_module == NULL )
674 msg_Err( p_this, "out of memory" );
675 module_unload( handle );
679 /* We need to fill these since they may be needed by CallEntry() */
680 p_module->psz_filename = psz_file;
681 p_module->handle = handle;
682 p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
684 /* Initialize the module: fill p_module->psz_object_name, default config */
685 if( CallEntry( p_module ) != 0 )
687 /* We couldn't call module_init() */
688 vlc_object_destroy( p_module );
689 module_unload( handle );
693 DupModule( p_module );
694 p_module->psz_filename = strdup( p_module->psz_filename );
695 p_module->psz_longname = strdup( p_module->psz_longname );
697 /* Everything worked fine ! The module is ready to be added to the list. */
698 p_module->b_builtin = VLC_FALSE;
700 /* msg_Dbg( p_this, "plugin \"%s\", %s",
701 p_module->psz_object_name, p_module->psz_longname ); */
703 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
708 /*****************************************************************************
709 * DupModule: make a plugin module standalone.
710 *****************************************************************************
711 * This function duplicates all strings in the module, so that the dynamic
712 * object can be unloaded. It acts recursively on submodules.
713 *****************************************************************************/
714 static void DupModule( module_t *p_module )
719 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
721 *pp_shortcut = strdup( *pp_shortcut );
724 /* We strdup() these entries so that they are still valid when the
725 * module is unloaded. */
726 p_module->psz_object_name = strdup( p_module->psz_object_name );
727 p_module->psz_capability = strdup( p_module->psz_capability );
729 if( p_module->psz_program != NULL )
731 p_module->psz_program = strdup( p_module->psz_program );
734 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
736 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
740 /*****************************************************************************
741 * UndupModule: free a duplicated module.
742 *****************************************************************************
743 * This function frees the allocations done in DupModule().
744 *****************************************************************************/
745 static void UndupModule( module_t *p_module )
750 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
752 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
755 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
757 free( *pp_shortcut );
760 free( p_module->psz_object_name );
761 free( p_module->psz_capability );
763 if( p_module->psz_program != NULL )
765 free( p_module->psz_program );
769 #endif /* HAVE_DYNAMIC_PLUGINS */
771 /*****************************************************************************
772 * AllocateBuiltinModule: initialize a builtin module.
773 *****************************************************************************
774 * This function registers a builtin module and allocates a structure
775 * for its information data. The module can then be handled by module_Need
776 * and module_Unneed. It can be removed by DeleteModule.
777 *****************************************************************************/
778 static int AllocateBuiltinModule( vlc_object_t * p_this,
779 int ( *pf_entry ) ( module_t * ) )
783 /* Now that we have successfully loaded the module, we can
784 * allocate a structure for it */
785 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
786 if( p_module == NULL )
788 msg_Err( p_this, "out of memory" );
792 /* Initialize the module : fill p_module->psz_object_name, etc. */
793 if( pf_entry( p_module ) != 0 )
795 /* With a well-written module we shouldn't have to print an
796 * additional error message here, but just make sure. */
797 msg_Err( p_this, "failed calling entry point in builtin module" );
798 vlc_object_destroy( p_module );
802 /* Everything worked fine ! The module is ready to be added to the list. */
803 p_module->b_builtin = VLC_TRUE;
805 /* msg_Dbg( p_this, "builtin \"%s\", %s",
806 p_module->psz_object_name, p_module->psz_longname ); */
808 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
813 /*****************************************************************************
814 * DeleteModule: delete a module and its structure.
815 *****************************************************************************
816 * This function can only be called if the module isn't being used.
817 *****************************************************************************/
818 static int DeleteModule( module_t * p_module )
820 vlc_object_detach( p_module );
822 /* We free the structures that we strdup()ed in Allocate*Module(). */
823 #ifdef HAVE_DYNAMIC_PLUGINS
824 if( !p_module->b_builtin )
826 module_unload( p_module->handle );
827 UndupModule( p_module );
828 free( p_module->psz_filename );
829 free( p_module->psz_longname );
833 /* Free and detach the object's children */
834 while( p_module->i_children )
836 vlc_object_t *p_this = p_module->pp_children[0];
837 vlc_object_detach( p_this );
838 vlc_object_destroy( p_this );
841 config_Free( p_module );
842 vlc_object_destroy( p_module );
847 #ifdef HAVE_DYNAMIC_PLUGINS
848 /*****************************************************************************
849 * CallEntry: call an entry point.
850 *****************************************************************************
851 * This function calls a symbol given its name and a module structure. The
852 * symbol MUST refer to a function returning int and taking a module_t* as
854 *****************************************************************************/
855 static int CallEntry( module_t * p_module )
857 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
858 int (* pf_symbol) ( module_t * p_module );
860 /* Try to resolve the symbol */
861 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
864 if( pf_symbol == NULL )
866 char psz_buffer[256];
868 /* We couldn't load the symbol */
869 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
870 psz_name, p_module->psz_filename,
871 module_error( psz_buffer ) );
875 /* We can now try to call the symbol */
876 if( pf_symbol( p_module ) != 0 )
878 /* With a well-written module we shouldn't have to print an
879 * additional error message here, but just make sure. */
880 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
881 psz_name, p_module->psz_filename );
885 /* Everything worked fine, we can return */
888 #endif /* HAVE_DYNAMIC_PLUGINS */