1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.130 2003/08/23 22:49:50 fenrir 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() */
40 #elif defined( UNDER_CE )
41 # include <windows.h> /* GetFileAttributes() */
43 # include "../extras/dirent.h"
46 #ifdef HAVE_SYS_TYPES_H
47 # include <sys/types.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
56 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
57 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
58 # define HAVE_DYNAMIC_PLUGINS
59 #elif defined(HAVE_IMAGE_H) /* BeOS */
61 # define HAVE_DYNAMIC_PLUGINS
62 #elif defined(UNDER_CE)
63 # define HAVE_DYNAMIC_PLUGINS
65 # define HAVE_DYNAMIC_PLUGINS
67 # undef HAVE_DYNAMIC_PLUGINS
70 #include "vlc_error.h"
72 #include "vlc_interface.h"
73 #include "vlc_playlist.h"
74 #include "intf_eject.h"
76 #include "stream_control.h"
77 #include "input_ext-intf.h"
78 #include "input_ext-dec.h"
79 #include "input_ext-plugins.h"
82 #include "vlc_video.h"
83 #include "video_output.h"
84 #include "vout_synchro.h"
86 #include "audio_output.h"
87 #include "aout_internal.h"
89 #include "stream_output.h"
90 /*#include "announce.h"*/
96 #include "vlc_block.h"
98 #if defined( UNDER_CE )
99 # define MYCHAR wchar_t
104 #ifdef HAVE_DYNAMIC_PLUGINS
105 # include "modules_plugin.h"
108 #if defined( UNDER_CE )
109 # include "modules_builtin_evc.h"
110 #elif defined( _MSC_VER )
111 # include "modules_builtin_msvc.h"
113 # include "modules_builtin.h"
116 /*****************************************************************************
118 *****************************************************************************/
119 #ifdef HAVE_DYNAMIC_PLUGINS
120 static void AllocateAllPlugins ( vlc_object_t * );
121 static void AllocatePluginDir ( vlc_object_t *, const MYCHAR *, int );
122 static int AllocatePluginFile ( vlc_object_t *, MYCHAR * );
124 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
125 static int DeleteModule ( module_t * );
126 #ifdef HAVE_DYNAMIC_PLUGINS
127 static void DupModule ( module_t * );
128 static void UndupModule ( module_t * );
129 static int CallEntry ( module_t * );
132 /*****************************************************************************
133 * module_InitBank: create the module bank.
134 *****************************************************************************
135 * This function creates a module bank structure which will be filled later
136 * on with all the modules found.
137 *****************************************************************************/
138 void __module_InitBank( vlc_object_t *p_this )
140 module_bank_t *p_bank;
142 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
143 p_bank->psz_object_name = "module bank";
146 * Store the symbols to be exported
148 #ifdef HAVE_DYNAMIC_PLUGINS
149 STORE_SYMBOLS( &p_bank->symbols );
152 /* Everything worked, attach the object */
153 p_this->p_libvlc->p_module_bank = p_bank;
154 vlc_object_attach( p_bank, p_this->p_libvlc );
159 /*****************************************************************************
160 * module_ResetBank: reset the module bank.
161 *****************************************************************************
162 * This function resets the module bank by unloading all unused plugin
164 *****************************************************************************/
165 void __module_ResetBank( vlc_object_t *p_this )
167 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
171 /*****************************************************************************
172 * module_EndBank: empty the module bank.
173 *****************************************************************************
174 * This function unloads all unused plugin modules and empties the module
175 * bank in case of success.
176 *****************************************************************************/
177 void __module_EndBank( vlc_object_t *p_this )
181 vlc_object_detach( p_this->p_libvlc->p_module_bank );
183 while( p_this->p_libvlc->p_module_bank->i_children )
185 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
187 if( DeleteModule( p_next ) )
189 /* Module deletion failed */
190 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
191 p_next->psz_object_name );
193 /* We just free the module by hand. Niahahahahaha. */
194 vlc_object_detach( p_next );
195 vlc_object_destroy( p_next );
199 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
204 /*****************************************************************************
205 * module_LoadMain: load the main program info into the module bank.
206 *****************************************************************************
207 * This function fills the module bank structure with the main module infos.
208 * This is very useful as it will allow us to consider the main program just
209 * as another module, and for instance the configuration options of main will
210 * be available in the module bank structure just as for every other module.
211 *****************************************************************************/
212 void __module_LoadMain( vlc_object_t *p_this )
214 AllocateBuiltinModule( p_this, vlc_entry__main );
217 /*****************************************************************************
218 * module_LoadBuiltins: load all modules which we built with.
219 *****************************************************************************
220 * This function fills the module bank structure with the builtin modules.
221 *****************************************************************************/
222 void __module_LoadBuiltins( vlc_object_t * p_this )
224 msg_Dbg( p_this, "checking builtin modules" );
225 ALLOCATE_ALL_BUILTINS();
228 /*****************************************************************************
229 * module_LoadPlugins: load all plugin modules we can find.
230 *****************************************************************************
231 * This function fills the module bank structure with the plugin modules.
232 *****************************************************************************/
233 void __module_LoadPlugins( vlc_object_t * p_this )
235 #ifdef HAVE_DYNAMIC_PLUGINS
236 msg_Dbg( p_this, "checking plugin modules" );
237 AllocateAllPlugins( p_this );
241 /*****************************************************************************
242 * module_Need: return the best module function, given a capability list.
243 *****************************************************************************
244 * This function returns the module that best fits the asked capabilities.
245 *****************************************************************************/
246 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
247 const char *psz_name )
249 typedef struct module_list_t module_list_t;
255 module_list_t *p_next;
258 module_list_t *p_list, *p_first, *p_tmp;
261 int i_which_module, i_index = 0;
262 vlc_bool_t b_intf = VLC_FALSE;
267 char *psz_shortcuts = NULL, *psz_var = NULL;
269 msg_Dbg( p_this, "looking for %s module", psz_capability );
271 /* Deal with variables */
272 if( psz_name && psz_name[0] == '$' )
274 psz_var = config_GetPsz( p_this, psz_name + 1 );
278 /* Count how many different shortcuts were asked for */
279 if( psz_name && *psz_name )
283 /* If the user wants none, give him none. */
284 if( !strcmp( psz_name, "none" ) )
286 if( psz_var ) free( psz_var );
291 psz_shortcuts = strdup( psz_name );
293 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
295 if( *psz_parser == ',' )
303 /* Sort the modules and test them */
304 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
305 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
308 /* Parse the module list for capabilities and probe each of them */
309 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
311 module_t * p_submodule = NULL;
312 int i_shortcut_bonus = 0, i_submodule;
314 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
316 /* Test that this module can do what we need */
317 if( strcmp( p_module->psz_capability, psz_capability ) )
319 for( i_submodule = 0;
320 i_submodule < p_module->i_children;
323 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
324 ->psz_capability, psz_capability ) )
327 (module_t*)p_module->pp_children[ i_submodule ];
332 if( p_submodule == NULL )
337 p_module = p_submodule;
340 /* Test if we have the required CPU */
341 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
346 /* If we required a shortcut, check this plugin provides it. */
350 int i_dummy, i_short = i_shortcuts;
351 char *psz_name = psz_shortcuts;
353 /* Let's drop modules with a 0 score (unless they are
354 * explicitly requested) */
355 b_trash = !p_module->i_score;
359 /* If the last given shortcut is "none" and we couldn't
360 * find the module in the list of provided shortcuts,
361 * then kick the bastard out of here!!! */
362 if( (i_short == 1) && !strcmp(psz_name, "none") )
368 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
370 if( !strcmp( psz_name,
371 p_module->pp_shortcuts[i_dummy] ) )
375 i_shortcut_bonus = i_short * 10000;
380 if( i_shortcut_bonus )
382 /* We found it... remember ? */
386 /* Go to the next shortcut... This is so lame! */
400 /* If we didn't require a shortcut, trash zero-scored plugins */
401 else if( !p_module->i_score )
406 /* Special case: test if we requested a particular intf plugin */
407 if( !i_shortcuts && p_module->psz_program
408 && !strcmp( p_module->psz_program,
409 p_this->p_vlc->psz_object_name ) )
413 /* Remove previous non-matching plugins */
420 /* This one doesn't match */
424 /* Store this new module */
425 p_list[ i_index ].p_module = p_module;
426 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
428 /* Add it to the modules-to-probe list */
431 p_list[ 0 ].p_next = NULL;
436 /* Ok, so at school you learned that quicksort is quick, and
437 * bubble sort sucks raw eggs. But that's when dealing with
438 * thousands of items. Here we have barely 50. */
439 module_list_t *p_newlist = p_first;
441 if( p_first->i_score < p_list[ i_index ].i_score )
443 p_list[ i_index ].p_next = p_first;
444 p_first = &p_list[ i_index ];
448 while( p_newlist->p_next != NULL &&
449 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
451 p_newlist = p_newlist->p_next;
454 p_list[ i_index ].p_next = p_newlist->p_next;
455 p_newlist->p_next = &p_list[ i_index ];
462 msg_Dbg( p_this, "probing %i candidate%s",
463 i_index, i_index == 1 ? "" : "s" );
465 /* Lock all candidate modules */
467 while( p_tmp != NULL )
469 vlc_object_yield( p_tmp->p_module );
470 p_tmp = p_tmp->p_next;
473 /* We can release the list, interesting modules were yielded */
474 vlc_list_release( p_all );
476 /* Parse the linked list and use the first successful module */
478 while( p_tmp != NULL )
480 if( p_tmp->p_module->pf_activate
481 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
486 vlc_object_release( p_tmp->p_module );
487 p_tmp = p_tmp->p_next;
490 /* Store the locked module value */
493 p_module = p_tmp->p_module;
494 p_tmp = p_tmp->p_next;
501 /* Unlock the remaining modules */
502 while( p_tmp != NULL )
504 vlc_object_release( p_tmp->p_module );
505 p_tmp = p_tmp->p_next;
510 if( p_module != NULL )
512 msg_Dbg( p_module, "using %s module \"%s\"",
513 psz_capability, p_module->psz_object_name );
515 else if( p_first == NULL )
517 msg_Err( p_this, "no %s module matched \"%s\"",
518 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
520 else if( psz_name != NULL && *psz_name )
522 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
523 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
528 free( psz_shortcuts );
536 /* Don't forget that the module is still locked */
540 /*****************************************************************************
541 * module_Unneed: decrease the usage count of a module.
542 *****************************************************************************
543 * This function must be called by the thread that called module_Need, to
544 * decrease the reference count and allow for hiding of modules.
545 *****************************************************************************/
546 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
548 /* Use the close method */
549 if( p_module->pf_deactivate )
551 p_module->pf_deactivate( p_this );
554 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
556 vlc_object_release( p_module );
561 /*****************************************************************************
562 * Following functions are local.
563 *****************************************************************************/
565 /*****************************************************************************
566 * AllocateAllPlugins: load all plugin modules we can find.
567 *****************************************************************************/
568 #ifdef HAVE_DYNAMIC_PLUGINS
569 static void AllocateAllPlugins( vlc_object_t *p_this )
571 /* Yes, there are two NULLs because we replace one with "plugin-path". */
572 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
575 char ** ppsz_path = path;
577 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
578 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
579 int i_vlclen = strlen( p_this->p_libvlc->psz_vlcpath );
580 vlc_bool_t b_notinroot;
583 #if defined( UNDER_CE )
584 wchar_t psz_dir[MAX_PATH];
587 /* If the user provided a plugin path, we add it to the list */
588 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
591 #if defined( WIN32 ) && !defined( UNDER_CE ) && !defined( _MSC_VER )
592 /* If there is no 'plugins' nor 'modules' subdirectory, the user may have
593 * screwed up the unzipping stage, so we look into '.' instead */
594 if( !opendir( "plugins" ) && !opendir( "modules" )
595 && !strcmp( *ppsz_path, "modules" ) )
601 for( ; *ppsz_path != NULL ; ppsz_path++ )
603 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
604 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
605 /* Store strlen(*ppsz_path) for later use. */
606 int i_dirlen = strlen( *ppsz_path );
608 b_notinroot = VLC_FALSE;
609 /* Under BeOS, we need to add beos_GetProgramPath() to access
610 * files under the current directory */
612 if( i_dirlen < 3 || (*ppsz_path)[3] != '\\' )
614 if( (*ppsz_path)[0] != '/' )
617 i_dirlen += i_vlclen + 2;
618 b_notinroot = VLC_TRUE;
620 psz_fullpath = malloc( i_dirlen );
621 if( psz_fullpath == NULL )
626 sprintf( psz_fullpath, "%s\\%s",
627 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
629 sprintf( psz_fullpath, "%s/%s",
630 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
636 psz_fullpath = *ppsz_path;
639 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
641 /* Don't go deeper than 5 subdirectories */
642 #if defined( UNDER_CE )
643 MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
644 AllocatePluginDir( p_this, psz_dir, 5 );
646 AllocatePluginDir( p_this, psz_fullpath, 5 );
649 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
652 free( psz_fullpath );
657 /* Free plugin-path */
658 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
659 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
660 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
663 /*****************************************************************************
664 * AllocatePluginDir: recursively parse a directory to look for plugins
665 *****************************************************************************/
666 static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
669 #if defined( UNDER_CE ) || defined( _MSC_VER )
671 MYCHAR psz_path[MAX_PATH + 256];
673 char psz_path[MAX_PATH + 256];
675 WIN32_FIND_DATA finddata;
682 struct dirent * file;
690 #if defined( UNDER_CE ) || defined( _MSC_VER )
691 rc = GetFileAttributes( psz_dir );
692 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
694 /* Not a directory */
698 /* Parse all files in the directory */
700 swprintf( psz_path, L"%s\\*.*", psz_dir );
702 sprintf( psz_path, "%s\\*.*", psz_dir );
704 handle = FindFirstFile( psz_path, &finddata );
705 if( handle == INVALID_HANDLE_VALUE )
707 /* Empty directory */
711 /* Parse the directory and try to load all files it contains. */
715 unsigned int i_len = wcslen( finddata.cFileName );
716 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
718 unsigned int i_len = strlen( finddata.cFileName );
719 /* Skip ".", ".." and anything starting with "." */
720 if( !*finddata.cFileName || *finddata.cFileName == '.' )
722 if( !FindNextFile( handle, &finddata ) ) break;
725 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
728 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
730 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
732 else if( i_len > strlen( LIBEXT )
736 /* We only load files ending with LIBEXT */
737 && !strncasecmp( psz_path + strlen( psz_path)
739 LIBEXT, strlen( LIBEXT ) ) )
742 AllocatePluginFile( p_this, psz_path );
745 while( FindNextFile( handle, &finddata ) );
747 /* Close the directory */
751 dir = opendir( psz_dir );
757 i_dirlen = strlen( psz_dir );
759 /* Parse the directory and try to load all files it contains. */
760 while( (file = readdir( dir )) )
765 /* Skip ".", ".." and anything starting with "." */
766 if( !*file->d_name || *file->d_name == '.' )
771 i_len = strlen( file->d_name );
772 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
774 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
776 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
779 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
781 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
783 else if( i_len > strlen( LIBEXT )
784 /* We only load files ending with LIBEXT */
785 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
786 LIBEXT, strlen( LIBEXT ) ) )
788 AllocatePluginFile( p_this, psz_file );
794 /* Close the directory */
800 /*****************************************************************************
801 * AllocatePluginFile: load a module into memory and initialize it.
802 *****************************************************************************
803 * This function loads a dynamically loadable module and allocates a structure
804 * for its information data. The module can then be handled by module_Need
805 * and module_Unneed. It can be removed by DeleteModule.
806 *****************************************************************************/
807 static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
810 module_handle_t handle;
813 char psz_filename[MAX_PATH];
814 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
815 psz_filename, MAX_PATH, NULL, NULL );
817 char * psz_filename = psz_file;
820 /* Try to dynamically load the module. */
821 if( module_load( psz_file, &handle ) )
823 char psz_buffer[256];
825 /* The plugin module couldn't be opened */
826 msg_Warn( p_this, "cannot open `%s' (%s)",
827 psz_filename, module_error( psz_buffer ) );
831 /* Now that we have successfully loaded the module, we can
832 * allocate a structure for it */
833 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
834 if( p_module == NULL )
836 msg_Err( p_this, "out of memory" );
837 module_unload( handle );
841 /* We need to fill these since they may be needed by CallEntry() */
842 p_module->psz_filename = psz_filename;
843 p_module->handle = handle;
844 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
846 /* Initialize the module: fill p_module->psz_object_name, default config */
847 if( CallEntry( p_module ) != 0 )
849 /* We couldn't call module_init() */
850 vlc_object_destroy( p_module );
851 module_unload( handle );
855 DupModule( p_module );
856 p_module->psz_filename = strdup( p_module->psz_filename );
857 p_module->psz_longname = strdup( p_module->psz_longname );
859 /* Everything worked fine ! The module is ready to be added to the list. */
860 p_module->b_builtin = VLC_FALSE;
862 /* msg_Dbg( p_this, "plugin \"%s\", %s",
863 p_module->psz_object_name, p_module->psz_longname ); */
865 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
870 /*****************************************************************************
871 * DupModule: make a plugin module standalone.
872 *****************************************************************************
873 * This function duplicates all strings in the module, so that the dynamic
874 * object can be unloaded. It acts recursively on submodules.
875 *****************************************************************************/
876 static void DupModule( module_t *p_module )
881 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
883 *pp_shortcut = strdup( *pp_shortcut );
886 /* We strdup() these entries so that they are still valid when the
887 * module is unloaded. */
888 p_module->psz_object_name = strdup( p_module->psz_object_name );
889 p_module->psz_capability = strdup( p_module->psz_capability );
891 if( p_module->psz_program != NULL )
893 p_module->psz_program = strdup( p_module->psz_program );
896 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
898 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
902 /*****************************************************************************
903 * UndupModule: free a duplicated module.
904 *****************************************************************************
905 * This function frees the allocations done in DupModule().
906 *****************************************************************************/
907 static void UndupModule( module_t *p_module )
912 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
914 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
917 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
919 free( *pp_shortcut );
922 free( p_module->psz_object_name );
923 free( p_module->psz_capability );
925 if( p_module->psz_program != NULL )
927 free( p_module->psz_program );
931 #endif /* HAVE_DYNAMIC_PLUGINS */
933 /*****************************************************************************
934 * AllocateBuiltinModule: initialize a builtin module.
935 *****************************************************************************
936 * This function registers a builtin module and allocates a structure
937 * for its information data. The module can then be handled by module_Need
938 * and module_Unneed. It can be removed by DeleteModule.
939 *****************************************************************************/
940 static int AllocateBuiltinModule( vlc_object_t * p_this,
941 int ( *pf_entry ) ( module_t * ) )
945 /* Now that we have successfully loaded the module, we can
946 * allocate a structure for it */
947 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
948 if( p_module == NULL )
950 msg_Err( p_this, "out of memory" );
954 /* Initialize the module : fill p_module->psz_object_name, etc. */
955 if( pf_entry( p_module ) != 0 )
957 /* With a well-written module we shouldn't have to print an
958 * additional error message here, but just make sure. */
959 msg_Err( p_this, "failed calling entry point in builtin module" );
960 vlc_object_destroy( p_module );
964 /* Everything worked fine ! The module is ready to be added to the list. */
965 p_module->b_builtin = VLC_TRUE;
967 /* msg_Dbg( p_this, "builtin \"%s\", %s",
968 p_module->psz_object_name, p_module->psz_longname ); */
970 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
975 /*****************************************************************************
976 * DeleteModule: delete a module and its structure.
977 *****************************************************************************
978 * This function can only be called if the module isn't being used.
979 *****************************************************************************/
980 static int DeleteModule( module_t * p_module )
982 vlc_object_detach( p_module );
984 /* We free the structures that we strdup()ed in Allocate*Module(). */
985 #ifdef HAVE_DYNAMIC_PLUGINS
986 if( !p_module->b_builtin )
988 if( p_module->b_unloadable )
990 module_unload( p_module->handle );
992 UndupModule( p_module );
993 free( p_module->psz_filename );
994 free( p_module->psz_longname );
998 /* Free and detach the object's children */
999 while( p_module->i_children )
1001 vlc_object_t *p_this = p_module->pp_children[0];
1002 vlc_object_detach( p_this );
1003 vlc_object_destroy( p_this );
1006 config_Free( p_module );
1007 vlc_object_destroy( p_module );
1012 #ifdef HAVE_DYNAMIC_PLUGINS
1013 /*****************************************************************************
1014 * CallEntry: call an entry point.
1015 *****************************************************************************
1016 * This function calls a symbol given its name and a module structure. The
1017 * symbol MUST refer to a function returning int and taking a module_t* as
1019 *****************************************************************************/
1020 static int CallEntry( module_t * p_module )
1022 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1023 int (* pf_symbol) ( module_t * p_module );
1025 /* Try to resolve the symbol */
1026 pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
1029 if( pf_symbol == NULL )
1031 char psz_buffer[256];
1033 /* We couldn't load the symbol */
1034 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1035 psz_name, p_module->psz_filename,
1036 module_error( psz_buffer ) );
1040 /* We can now try to call the symbol */
1041 if( pf_symbol( p_module ) != 0 )
1043 /* With a well-written module we shouldn't have to print an
1044 * additional error message here, but just make sure. */
1045 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1046 psz_name, p_module->psz_filename );
1050 /* Everything worked fine, we can return */
1053 #endif /* HAVE_DYNAMIC_PLUGINS */