1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.93 2002/09/30 11:05:42 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() */
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*) - 2 ] = 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,
595 struct dirent * file;
602 dir = opendir( psz_dir );
609 i_dirlen = strlen( psz_dir );
611 /* Parse the directory and try to load all files it contains. */
612 while( (file = readdir( dir )) )
617 /* Skip ".", ".." and anything starting with "." */
618 if( !*file->d_name || *file->d_name == '.' )
623 i_len = strlen( file->d_name );
625 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
626 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
628 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
630 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
632 else if( i_len > strlen( LIBEXT )
633 /* We only load files ending with LIBEXT */
634 && !strncmp( file->d_name + i_len - strlen( LIBEXT ),
635 LIBEXT, strlen( LIBEXT ) ) )
637 AllocatePluginFile( p_this, psz_file );
643 /* Close the directory */
647 /*****************************************************************************
648 * AllocatePluginFile: load a module into memory and initialize it.
649 *****************************************************************************
650 * This function loads a dynamically loadable module and allocates a structure
651 * for its information data. The module can then be handled by module_Need
652 * and module_Unneed. It can be removed by DeleteModule.
653 *****************************************************************************/
654 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
657 module_handle_t handle;
659 /* Try to dynamically load the module. */
660 if( module_load( psz_file, &handle ) )
662 char psz_buffer[256];
664 /* The plugin module couldn't be opened */
665 msg_Warn( p_this, "cannot open `%s' (%s)",
666 psz_file, module_error( psz_buffer ) );
670 /* Now that we have successfully loaded the module, we can
671 * allocate a structure for it */
672 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
673 if( p_module == NULL )
675 msg_Err( p_this, "out of memory" );
676 module_unload( handle );
680 /* We need to fill these since they may be needed by CallEntry() */
681 p_module->psz_filename = psz_file;
682 p_module->handle = handle;
683 p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
685 /* Initialize the module: fill p_module->psz_object_name, default config */
686 if( CallEntry( p_module ) != 0 )
688 /* We couldn't call module_init() */
689 vlc_object_destroy( p_module );
690 module_unload( handle );
694 DupModule( p_module );
695 p_module->psz_filename = strdup( p_module->psz_filename );
696 p_module->psz_longname = strdup( p_module->psz_longname );
698 /* Everything worked fine ! The module is ready to be added to the list. */
699 p_module->b_builtin = VLC_FALSE;
701 /* msg_Dbg( p_this, "plugin \"%s\", %s",
702 p_module->psz_object_name, p_module->psz_longname ); */
704 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
709 /*****************************************************************************
710 * DupModule: make a plugin module standalone.
711 *****************************************************************************
712 * This function duplicates all strings in the module, so that the dynamic
713 * object can be unloaded. It acts recursively on submodules.
714 *****************************************************************************/
715 static void DupModule( module_t *p_module )
720 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
722 *pp_shortcut = strdup( *pp_shortcut );
725 /* We strdup() these entries so that they are still valid when the
726 * module is unloaded. */
727 p_module->psz_object_name = strdup( p_module->psz_object_name );
728 p_module->psz_capability = strdup( p_module->psz_capability );
730 if( p_module->psz_program != NULL )
732 p_module->psz_program = strdup( p_module->psz_program );
735 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
737 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
741 /*****************************************************************************
742 * UndupModule: free a duplicated module.
743 *****************************************************************************
744 * This function frees the allocations done in DupModule().
745 *****************************************************************************/
746 static void UndupModule( module_t *p_module )
751 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
753 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
756 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
758 free( *pp_shortcut );
761 free( p_module->psz_object_name );
762 free( p_module->psz_capability );
764 if( p_module->psz_program != NULL )
766 free( p_module->psz_program );
770 #endif /* HAVE_DYNAMIC_PLUGINS */
772 /*****************************************************************************
773 * AllocateBuiltinModule: initialize a builtin module.
774 *****************************************************************************
775 * This function registers a builtin module and allocates a structure
776 * for its information data. The module can then be handled by module_Need
777 * and module_Unneed. It can be removed by DeleteModule.
778 *****************************************************************************/
779 static int AllocateBuiltinModule( vlc_object_t * p_this,
780 int ( *pf_entry ) ( module_t * ) )
784 /* Now that we have successfully loaded the module, we can
785 * allocate a structure for it */
786 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
787 if( p_module == NULL )
789 msg_Err( p_this, "out of memory" );
793 /* Initialize the module : fill p_module->psz_object_name, etc. */
794 if( pf_entry( p_module ) != 0 )
796 /* With a well-written module we shouldn't have to print an
797 * additional error message here, but just make sure. */
798 msg_Err( p_this, "failed calling entry point in builtin module" );
799 vlc_object_destroy( p_module );
803 /* Everything worked fine ! The module is ready to be added to the list. */
804 p_module->b_builtin = VLC_TRUE;
806 /* msg_Dbg( p_this, "builtin \"%s\", %s",
807 p_module->psz_object_name, p_module->psz_longname ); */
809 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
814 /*****************************************************************************
815 * DeleteModule: delete a module and its structure.
816 *****************************************************************************
817 * This function can only be called if the module isn't being used.
818 *****************************************************************************/
819 static int DeleteModule( module_t * p_module )
821 vlc_object_detach( p_module );
823 /* We free the structures that we strdup()ed in Allocate*Module(). */
824 #ifdef HAVE_DYNAMIC_PLUGINS
825 if( !p_module->b_builtin )
827 if( p_module->b_unloadable )
829 module_unload( p_module->handle );
831 UndupModule( p_module );
832 free( p_module->psz_filename );
833 free( p_module->psz_longname );
837 /* Free and detach the object's children */
838 while( p_module->i_children )
840 vlc_object_t *p_this = p_module->pp_children[0];
841 vlc_object_detach( p_this );
842 vlc_object_destroy( p_this );
845 config_Free( p_module );
846 vlc_object_destroy( p_module );
851 #ifdef HAVE_DYNAMIC_PLUGINS
852 /*****************************************************************************
853 * CallEntry: call an entry point.
854 *****************************************************************************
855 * This function calls a symbol given its name and a module structure. The
856 * symbol MUST refer to a function returning int and taking a module_t* as
858 *****************************************************************************/
859 static int CallEntry( module_t * p_module )
861 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
862 int (* pf_symbol) ( module_t * p_module );
864 /* Try to resolve the symbol */
865 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
868 if( pf_symbol == NULL )
870 char psz_buffer[256];
872 /* We couldn't load the symbol */
873 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
874 psz_name, p_module->psz_filename,
875 module_error( psz_buffer ) );
879 /* We can now try to call the symbol */
880 if( pf_symbol( p_module ) != 0 )
882 /* With a well-written module we shouldn't have to print an
883 * additional error message here, but just make sure. */
884 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
885 psz_name, p_module->psz_filename );
889 /* Everything worked fine, we can return */
892 #endif /* HAVE_DYNAMIC_PLUGINS */