1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: modules.c,v 1.143 2004/01/06 12:02:06 zorglub Exp $
7 * Authors: Sam 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 #define HAVE_DYNAMIC_PLUGINS
57 #if defined(HAVE_DL_DYLD)
58 # if defined(HAVE_MACH_O_DYLD_H)
59 # include <mach-o/dyld.h>
61 #elif defined(HAVE_DL_BEOS)
62 # if defined(HAVE_IMAGE_H)
65 #elif defined(HAVE_DL_WINDOWS)
67 #elif defined(HAVE_DL_DLOPEN)
68 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
71 # if defined(HAVE_SYS_DL_H)
74 #elif defined(HAVE_DL_SHL_LOAD)
75 # if defined(HAVE_DL_H)
79 # undef HAVE_DYNAMIC_PLUGINS
82 #include "vlc_error.h"
84 #include "vlc_interface.h"
85 #include "vlc_playlist.h"
86 #include "intf_eject.h"
88 #include "stream_control.h"
89 #include "input_ext-intf.h"
90 #include "input_ext-dec.h"
91 #include "input_ext-plugins.h"
94 #include "vlc_video.h"
95 #include "video_output.h"
96 #include "vout_synchro.h"
98 #include "audio_output.h"
99 #include "aout_internal.h"
101 #include "stream_output.h"
102 /*#include "announce.h"*/
105 #include "iso_lang.h"
108 #include "vlc_block.h"
110 #if defined( UNDER_CE )
111 # define MYCHAR wchar_t
116 #ifdef HAVE_DYNAMIC_PLUGINS
117 # include "modules_plugin.h"
120 #if defined( UNDER_CE )
121 # include "modules_builtin_evc.h"
122 #elif defined( _MSC_VER )
123 # include "modules_builtin_msvc.h"
125 # include "modules_builtin.h"
129 /*****************************************************************************
131 *****************************************************************************/
132 #ifdef HAVE_DYNAMIC_PLUGINS
133 static void AllocateAllPlugins ( vlc_object_t * );
134 static void AllocatePluginDir ( vlc_object_t *, const MYCHAR *, int );
135 static int AllocatePluginFile ( vlc_object_t *, MYCHAR * );
137 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
138 static int DeleteModule ( module_t * );
139 #ifdef HAVE_DYNAMIC_PLUGINS
140 static void DupModule ( module_t * );
141 static void UndupModule ( module_t * );
142 static int CallEntry ( module_t * );
143 static void CloseModule ( module_handle_t );
144 static void * GetSymbol ( module_handle_t, const char * );
145 #if defined(HAVE_DL_WINDOWS)
146 static char * GetWindowsError ( void );
150 /*****************************************************************************
151 * module_InitBank: create the module bank.
152 *****************************************************************************
153 * This function creates a module bank structure which will be filled later
154 * on with all the modules found.
155 *****************************************************************************/
156 void __module_InitBank( vlc_object_t *p_this )
158 module_bank_t *p_bank;
160 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
161 p_bank->psz_object_name = "module bank";
164 * Store the symbols to be exported
166 #ifdef HAVE_DYNAMIC_PLUGINS
167 STORE_SYMBOLS( &p_bank->symbols );
170 /* Everything worked, attach the object */
171 p_this->p_libvlc->p_module_bank = p_bank;
172 vlc_object_attach( p_bank, p_this->p_libvlc );
177 /*****************************************************************************
178 * module_ResetBank: reset the module bank.
179 *****************************************************************************
180 * This function resets the module bank by unloading all unused plugin
182 *****************************************************************************/
183 void __module_ResetBank( vlc_object_t *p_this )
185 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
189 /*****************************************************************************
190 * module_EndBank: empty the module bank.
191 *****************************************************************************
192 * This function unloads all unused plugin modules and empties the module
193 * bank in case of success.
194 *****************************************************************************/
195 void __module_EndBank( vlc_object_t *p_this )
199 vlc_object_detach( p_this->p_libvlc->p_module_bank );
201 while( p_this->p_libvlc->p_module_bank->i_children )
203 p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
205 if( DeleteModule( p_next ) )
207 /* Module deletion failed */
208 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
209 p_next->psz_object_name );
211 /* We just free the module by hand. Niahahahahaha. */
212 vlc_object_detach( p_next );
213 vlc_object_destroy( p_next );
217 vlc_object_destroy( p_this->p_libvlc->p_module_bank );
222 /*****************************************************************************
223 * module_LoadMain: load the main program info into the module bank.
224 *****************************************************************************
225 * This function fills the module bank structure with the main module infos.
226 * This is very useful as it will allow us to consider the main program just
227 * as another module, and for instance the configuration options of main will
228 * be available in the module bank structure just as for every other module.
229 *****************************************************************************/
230 void __module_LoadMain( vlc_object_t *p_this )
232 AllocateBuiltinModule( p_this, vlc_entry__main );
235 /*****************************************************************************
236 * module_LoadBuiltins: load all modules which we built with.
237 *****************************************************************************
238 * This function fills the module bank structure with the builtin modules.
239 *****************************************************************************/
240 void __module_LoadBuiltins( vlc_object_t * p_this )
242 msg_Dbg( p_this, "checking builtin modules" );
243 ALLOCATE_ALL_BUILTINS();
246 /*****************************************************************************
247 * module_LoadPlugins: load all plugin modules we can find.
248 *****************************************************************************
249 * This function fills the module bank structure with the plugin modules.
250 *****************************************************************************/
251 void __module_LoadPlugins( vlc_object_t * p_this )
253 #ifdef HAVE_DYNAMIC_PLUGINS
254 msg_Dbg( p_this, "checking plugin modules" );
255 AllocateAllPlugins( p_this );
259 /*****************************************************************************
260 * module_Need: return the best module function, given a capability list.
261 *****************************************************************************
262 * This function returns the module that best fits the asked capabilities.
263 *****************************************************************************/
264 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
265 const char *psz_name )
267 typedef struct module_list_t module_list_t;
273 module_list_t *p_next;
276 module_list_t *p_list, *p_first, *p_tmp;
279 int i_which_module, i_index = 0;
280 vlc_bool_t b_intf = VLC_FALSE;
285 char *psz_shortcuts = NULL, *psz_var = NULL;
287 msg_Dbg( p_this, "looking for %s module", psz_capability );
289 /* Deal with variables */
290 if( psz_name && psz_name[0] == '$' )
293 var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
294 var_Get( p_this, psz_name + 1, &val );
295 psz_var = val.psz_string;
299 /* Count how many different shortcuts were asked for */
300 if( psz_name && *psz_name )
304 /* If the user wants none, give him none. */
305 if( !strcmp( psz_name, "none" ) )
307 if( psz_var ) free( psz_var );
312 psz_shortcuts = strdup( psz_name );
314 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
316 if( *psz_parser == ',' )
324 /* Sort the modules and test them */
325 p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
326 p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
329 /* Parse the module list for capabilities and probe each of them */
330 for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
332 module_t * p_submodule = NULL;
333 int i_shortcut_bonus = 0, i_submodule;
335 p_module = (module_t *)p_all->p_values[i_which_module].p_object;
337 /* Test that this module can do what we need */
338 if( strcmp( p_module->psz_capability, psz_capability ) )
340 for( i_submodule = 0;
341 i_submodule < p_module->i_children;
344 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
345 ->psz_capability, psz_capability ) )
348 (module_t*)p_module->pp_children[ i_submodule ];
353 if( p_submodule == NULL )
358 p_module = p_submodule;
361 /* Test if we have the required CPU */
362 if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
367 /* If we required a shortcut, check this plugin provides it. */
371 int i_dummy, i_short = i_shortcuts;
372 char *psz_name = psz_shortcuts;
374 /* Let's drop modules with a 0 score (unless they are
375 * explicitly requested) */
376 b_trash = !p_module->i_score;
380 /* If the last given shortcut is "none" and we couldn't
381 * find the module in the list of provided shortcuts,
382 * then kick the bastard out of here!!! */
383 if( (i_short == 1) && !strcmp(psz_name, "none") )
389 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
391 if( !strcasecmp( psz_name,
392 p_module->pp_shortcuts[i_dummy] ) )
396 i_shortcut_bonus = i_short * 10000;
401 if( i_shortcut_bonus )
403 /* We found it... remember ? */
407 /* Go to the next shortcut... This is so lame! */
421 /* If we didn't require a shortcut, trash zero-scored plugins */
422 else if( !p_module->i_score )
427 /* Special case: test if we requested a particular intf plugin */
428 if( !i_shortcuts && p_module->psz_program
429 && !strcmp( p_module->psz_program,
430 p_this->p_vlc->psz_object_name ) )
434 /* Remove previous non-matching plugins */
441 /* This one doesn't match */
445 /* Store this new module */
446 p_list[ i_index ].p_module = p_module;
447 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
449 /* Add it to the modules-to-probe list */
452 p_list[ 0 ].p_next = NULL;
457 /* Ok, so at school you learned that quicksort is quick, and
458 * bubble sort sucks raw eggs. But that's when dealing with
459 * thousands of items. Here we have barely 50. */
460 module_list_t *p_newlist = p_first;
462 if( p_first->i_score < p_list[ i_index ].i_score )
464 p_list[ i_index ].p_next = p_first;
465 p_first = &p_list[ i_index ];
469 while( p_newlist->p_next != NULL &&
470 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
472 p_newlist = p_newlist->p_next;
475 p_list[ i_index ].p_next = p_newlist->p_next;
476 p_newlist->p_next = &p_list[ i_index ];
483 msg_Dbg( p_this, "probing %i candidate%s",
484 i_index, i_index == 1 ? "" : "s" );
486 /* Lock all candidate modules */
488 while( p_tmp != NULL )
490 vlc_object_yield( p_tmp->p_module );
491 p_tmp = p_tmp->p_next;
494 /* We can release the list, interesting modules were yielded */
495 vlc_list_release( p_all );
497 /* Parse the linked list and use the first successful module */
499 while( p_tmp != NULL )
501 if( p_tmp->p_module->pf_activate
502 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
507 vlc_object_release( p_tmp->p_module );
508 p_tmp = p_tmp->p_next;
511 /* Store the locked module value */
514 p_module = p_tmp->p_module;
515 p_tmp = p_tmp->p_next;
522 /* Unlock the remaining modules */
523 while( p_tmp != NULL )
525 vlc_object_release( p_tmp->p_module );
526 p_tmp = p_tmp->p_next;
531 if( p_module != NULL )
533 msg_Dbg( p_module, "using %s module \"%s\"",
534 psz_capability, p_module->psz_object_name );
536 else if( p_first == NULL )
538 msg_Err( p_this, "no %s module matched \"%s\"",
539 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
541 else if( psz_name != NULL && *psz_name )
543 msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
544 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
549 free( psz_shortcuts );
557 /* Don't forget that the module is still locked */
561 /*****************************************************************************
562 * module_Unneed: decrease the usage count of a module.
563 *****************************************************************************
564 * This function must be called by the thread that called module_Need, to
565 * decrease the reference count and allow for hiding of modules.
566 *****************************************************************************/
567 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
569 /* Use the close method */
570 if( p_module->pf_deactivate )
572 p_module->pf_deactivate( p_this );
575 msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
577 vlc_object_release( p_module );
582 /*****************************************************************************
583 * Following functions are local.
584 *****************************************************************************/
586 /*****************************************************************************
587 * AllocateAllPlugins: load all plugin modules we can find.
588 *****************************************************************************/
589 #ifdef HAVE_DYNAMIC_PLUGINS
590 static void AllocateAllPlugins( vlc_object_t *p_this )
592 /* Yes, there are two NULLs because we replace one with "plugin-path". */
593 char * path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
596 char ** ppsz_path = path;
599 #if defined( UNDER_CE )
600 wchar_t psz_dir[MAX_PATH];
603 /* If the user provided a plugin path, we add it to the list */
604 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
607 for( ; *ppsz_path != NULL ; ppsz_path++ )
609 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
610 || ( defined( WIN32 ) && !defined( UNDER_CE ) )
612 /* Handle relative as well as absolute paths */
614 if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
616 if( (*ppsz_path)[0] != '/' )
619 int i_dirlen = strlen( *ppsz_path );
620 i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
622 psz_fullpath = malloc( i_dirlen );
623 if( psz_fullpath == NULL )
628 sprintf( psz_fullpath, "%s\\%s",
629 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
631 sprintf( psz_fullpath, "%s/%s",
632 p_this->p_libvlc->psz_vlcpath, *ppsz_path );
638 psz_fullpath = strdup( *ppsz_path );
641 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
643 /* Don't go deeper than 5 subdirectories */
644 #if defined( UNDER_CE )
645 MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
646 AllocatePluginDir( p_this, psz_dir, 5 );
648 AllocatePluginDir( p_this, psz_fullpath, 5 );
651 free( psz_fullpath );
654 /* Free plugin-path */
655 if( path[ sizeof(path)/sizeof(char*) - 2 ] )
656 free( path[ sizeof(path)/sizeof(char*) - 2 ] );
657 path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
660 /*****************************************************************************
661 * AllocatePluginDir: recursively parse a directory to look for plugins
662 *****************************************************************************/
663 static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
666 #if defined( UNDER_CE ) || defined( _MSC_VER )
668 MYCHAR psz_path[MAX_PATH + 256];
670 char psz_path[MAX_PATH + 256];
672 WIN32_FIND_DATA finddata;
679 struct dirent * file;
682 if( p_this->p_vlc->b_die || i_maxdepth < 0 )
687 #if defined( UNDER_CE ) || defined( _MSC_VER )
688 rc = GetFileAttributes( psz_dir );
689 if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
691 /* Not a directory */
695 /* Parse all files in the directory */
697 swprintf( psz_path, L"%s\\*.*", psz_dir );
699 sprintf( psz_path, "%s\\*.*", psz_dir );
701 handle = FindFirstFile( psz_path, &finddata );
702 if( handle == INVALID_HANDLE_VALUE )
704 /* Empty directory */
708 /* Parse the directory and try to load all files it contains. */
712 unsigned int i_len = wcslen( finddata.cFileName );
713 swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
715 unsigned int i_len = strlen( finddata.cFileName );
716 /* Skip ".", ".." and anything starting with "." */
717 if( !*finddata.cFileName || *finddata.cFileName == '.' )
719 if( !FindNextFile( handle, &finddata ) ) break;
722 sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
725 if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
727 AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
729 else if( i_len > strlen( LIBEXT )
733 /* We only load files ending with LIBEXT */
734 && !strncasecmp( psz_path + strlen( psz_path)
736 LIBEXT, strlen( LIBEXT ) ) )
739 AllocatePluginFile( p_this, psz_path );
742 while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
744 /* Close the directory */
748 dir = opendir( psz_dir );
754 i_dirlen = strlen( psz_dir );
756 /* Parse the directory and try to load all files it contains. */
757 while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
762 /* Skip ".", ".." and anything starting with "." */
763 if( !*file->d_name || *file->d_name == '.' )
768 i_len = strlen( file->d_name );
769 psz_file = malloc( i_dirlen + 1 + i_len + 1 );
771 sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
773 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
776 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
778 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
780 else if( i_len > strlen( LIBEXT )
781 /* We only load files ending with LIBEXT */
782 && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
783 LIBEXT, strlen( LIBEXT ) ) )
785 AllocatePluginFile( p_this, psz_file );
791 /* Close the directory */
797 /*****************************************************************************
798 * AllocatePluginFile: load a module into memory and initialize it.
799 *****************************************************************************
800 * This function loads a dynamically loadable module and allocates a structure
801 * for its information data. The module can then be handled by module_Need
802 * and module_Unneed. It can be removed by DeleteModule.
803 *****************************************************************************/
804 static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
807 module_handle_t handle;
810 * Try to dynamically load the module. This part is very platform-
811 * specific, and error messages should be as verbose as possible.
814 #if defined(HAVE_DL_DYLD)
815 NSObjectFileImage image;
816 NSObjectFileImageReturnCode ret;
818 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
820 if( ret != NSObjectFileImageSuccess )
822 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
826 /* Open the dynamic module */
827 handle = NSLinkModule( image, psz_file,
828 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
832 NSLinkEditErrors errors;
833 const char *psz_file, *psz_err;
835 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
836 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
837 NSDestroyObjectFileImage( image );
841 /* Destroy our image, we won't need it */
842 NSDestroyObjectFileImage( image );
844 #elif defined(HAVE_DL_BEOS)
845 handle = load_add_on( psz_file );
848 msg_Warn( p_this, "cannot load module `%s'", psz_file );
852 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
853 char psz_filename[MAX_PATH];
854 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
855 psz_filename, MAX_PATH, NULL, NULL );
856 handle = LoadLibrary( psz_filename );
859 char *psz_error = GetWindowsError();
860 msg_Warn( p_this, "cannot load module `%s' (%s)",
861 psz_filename, psz_error );
865 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
866 handle = LoadLibrary( psz_file );
869 char *psz_error = GetWindowsError();
870 msg_Warn( p_this, "cannot load module `%s' (%s)",
871 psz_file, psz_error );
875 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
876 /* static is OK, we are called atomically */
877 static vlc_bool_t b_kde = VLC_FALSE;
879 # if defined(SYS_LINUX)
880 /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
881 * are going to get namespace collisions when two modules have common
882 * public symbols, but ALSA is being a pest here. */
883 if( strstr( psz_file, "alsa_plugin" ) )
885 handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
888 msg_Warn( p_this, "cannot load module `%s' (%s)",
889 psz_file, dlerror() );
894 /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
895 * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
896 * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
897 /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
898 if( !b_kde && !strstr( psz_file, "kde" ) )
900 dlopen( "libstdc++.so.6", RTLD_NOW )
901 || dlopen( "libstdc++.so.5", RTLD_NOW )
902 || dlopen( "libstdc++.so.4", RTLD_NOW )
903 || dlopen( "libstdc++.so.3", RTLD_NOW );
907 handle = dlopen( psz_file, RTLD_NOW );
910 msg_Warn( p_this, "cannot load module `%s' (%s)",
911 psz_file, dlerror() );
915 #elif defined(HAVE_DL_DLOPEN)
916 # if defined(DL_LAZY)
917 handle = dlopen( psz_file, DL_LAZY );
919 handle = dlopen( psz_file, 0 );
923 msg_Warn( p_this, "cannot load module `%s' (%s)",
924 psz_file, dlerror() );
928 #elif defined(HAVE_DL_SHL_LOAD)
929 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
932 msg_Warn( p_this, "cannot load module `%s' (%s)",
933 psz_file, strerror(errno) );
938 # error "Something is wrong in modules.c"
942 /* Now that we have successfully loaded the module, we can
943 * allocate a structure for it */
944 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
945 if( p_module == NULL )
947 msg_Err( p_this, "out of memory" );
948 CloseModule( handle );
952 /* We need to fill these since they may be needed by CallEntry() */
954 p_module->psz_filename = psz_filename;
956 p_module->psz_filename = psz_file;
958 p_module->handle = handle;
959 p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
961 /* Initialize the module: fill p_module->psz_object_name, default config */
962 if( CallEntry( p_module ) != 0 )
964 /* We couldn't call module_init() */
965 vlc_object_destroy( p_module );
966 CloseModule( handle );
970 DupModule( p_module );
971 p_module->psz_filename = strdup( p_module->psz_filename );
972 p_module->psz_longname = strdup( p_module->psz_longname );
974 /* Everything worked fine ! The module is ready to be added to the list. */
975 p_module->b_builtin = VLC_FALSE;
977 /* msg_Dbg( p_this, "plugin \"%s\", %s",
978 p_module->psz_object_name, p_module->psz_longname ); */
980 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
985 /*****************************************************************************
986 * DupModule: make a plugin module standalone.
987 *****************************************************************************
988 * This function duplicates all strings in the module, so that the dynamic
989 * object can be unloaded. It acts recursively on submodules.
990 *****************************************************************************/
991 static void DupModule( module_t *p_module )
996 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
998 *pp_shortcut = strdup( *pp_shortcut );
1001 /* We strdup() these entries so that they are still valid when the
1002 * module is unloaded. */
1003 p_module->psz_object_name = strdup( p_module->psz_object_name );
1004 p_module->psz_capability = strdup( p_module->psz_capability );
1006 if( p_module->psz_program != NULL )
1008 p_module->psz_program = strdup( p_module->psz_program );
1011 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1013 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1017 /*****************************************************************************
1018 * UndupModule: free a duplicated module.
1019 *****************************************************************************
1020 * This function frees the allocations done in DupModule().
1021 *****************************************************************************/
1022 static void UndupModule( module_t *p_module )
1027 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1029 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1032 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1034 free( *pp_shortcut );
1037 free( p_module->psz_object_name );
1038 free( p_module->psz_capability );
1040 if( p_module->psz_program != NULL )
1042 free( p_module->psz_program );
1046 #endif /* HAVE_DYNAMIC_PLUGINS */
1048 /*****************************************************************************
1049 * AllocateBuiltinModule: initialize a builtin module.
1050 *****************************************************************************
1051 * This function registers a builtin module and allocates a structure
1052 * for its information data. The module can then be handled by module_Need
1053 * and module_Unneed. It can be removed by DeleteModule.
1054 *****************************************************************************/
1055 static int AllocateBuiltinModule( vlc_object_t * p_this,
1056 int ( *pf_entry ) ( module_t * ) )
1058 module_t * p_module;
1060 /* Now that we have successfully loaded the module, we can
1061 * allocate a structure for it */
1062 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1063 if( p_module == NULL )
1065 msg_Err( p_this, "out of memory" );
1069 /* Initialize the module : fill p_module->psz_object_name, etc. */
1070 if( pf_entry( p_module ) != 0 )
1072 /* With a well-written module we shouldn't have to print an
1073 * additional error message here, but just make sure. */
1074 msg_Err( p_this, "failed calling entry point in builtin module" );
1075 vlc_object_destroy( p_module );
1079 /* Everything worked fine ! The module is ready to be added to the list. */
1080 p_module->b_builtin = VLC_TRUE;
1082 /* msg_Dbg( p_this, "builtin \"%s\", %s",
1083 p_module->psz_object_name, p_module->psz_longname ); */
1085 vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1090 /*****************************************************************************
1091 * DeleteModule: delete a module and its structure.
1092 *****************************************************************************
1093 * This function can only be called if the module isn't being used.
1094 *****************************************************************************/
1095 static int DeleteModule( module_t * p_module )
1097 vlc_object_detach( p_module );
1099 /* We free the structures that we strdup()ed in Allocate*Module(). */
1100 #ifdef HAVE_DYNAMIC_PLUGINS
1101 if( !p_module->b_builtin )
1103 if( p_module->b_unloadable )
1105 CloseModule( p_module->handle );
1107 UndupModule( p_module );
1108 free( p_module->psz_filename );
1109 free( p_module->psz_longname );
1113 /* Free and detach the object's children */
1114 while( p_module->i_children )
1116 vlc_object_t *p_this = p_module->pp_children[0];
1117 vlc_object_detach( p_this );
1118 vlc_object_destroy( p_this );
1121 config_Free( p_module );
1122 vlc_object_destroy( p_module );
1127 #ifdef HAVE_DYNAMIC_PLUGINS
1128 /*****************************************************************************
1129 * CallEntry: call an entry point.
1130 *****************************************************************************
1131 * This function calls a symbol given its name and a module structure. The
1132 * symbol MUST refer to a function returning int and taking a module_t* as
1134 *****************************************************************************/
1135 static int CallEntry( module_t * p_module )
1137 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1138 int (* pf_symbol) ( module_t * p_module );
1140 /* Try to resolve the symbol */
1141 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1143 if( pf_symbol == NULL )
1145 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1146 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1147 psz_name, p_module->psz_filename );
1148 #elif defined(HAVE_DL_WINDOWS)
1149 char *psz_error = GetWindowsError();
1150 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1151 psz_name, p_module->psz_filename, psz_error );
1153 #elif defined(HAVE_DL_DLOPEN)
1154 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1155 psz_name, p_module->psz_filename, dlerror() );
1156 #elif defined(HAVE_DL_SHL_LOAD)
1157 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1158 psz_name, p_module->psz_filename, strerror(errno) );
1160 # error "Something is wrong in modules.c"
1165 /* We can now try to call the symbol */
1166 if( pf_symbol( p_module ) != 0 )
1168 /* With a well-written module we shouldn't have to print an
1169 * additional error message here, but just make sure. */
1170 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1171 psz_name, p_module->psz_filename );
1175 /* Everything worked fine, we can return */
1179 /*****************************************************************************
1180 * CloseModule: unload a dynamic library
1181 *****************************************************************************
1182 * This function unloads a previously opened dynamically linked library
1183 * using a system dependant method. No return value is taken in consideration,
1184 * since some libraries sometimes refuse to close properly.
1185 *****************************************************************************/
1186 static void CloseModule( module_handle_t handle )
1188 #if defined(HAVE_DL_DYLD)
1189 NSUnLinkModule( handle, FALSE );
1191 #elif defined(HAVE_DL_BEOS)
1192 unload_add_on( handle );
1194 #elif defined(HAVE_DL_WINDOWS)
1195 FreeLibrary( handle );
1197 #elif defined(HAVE_DL_DLOPEN)
1200 #elif defined(HAVE_DL_SHL_LOAD)
1201 shl_unload( handle );
1207 /*****************************************************************************
1208 * GetSymbol: get a symbol from a dynamic library
1209 *****************************************************************************
1210 * This function queries a loaded library for a symbol specified in a
1211 * string, and returns a pointer to it. We don't check for dlerror() or
1212 * similar functions, since we want a non-NULL symbol anyway.
1213 *****************************************************************************/
1214 static void * _module_getsymbol( module_handle_t, const char * );
1216 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1218 void * p_symbol = _module_getsymbol( handle, psz_function );
1220 /* MacOS X dl library expects symbols to begin with "_". So do
1221 * some other operating systems. That's really lame, but hey, what
1223 if( p_symbol == NULL )
1225 char *psz_call = malloc( strlen( psz_function ) + 2 );
1227 strcpy( psz_call + 1, psz_function );
1228 psz_call[ 0 ] = '_';
1229 p_symbol = _module_getsymbol( handle, psz_call );
1236 static void * _module_getsymbol( module_handle_t handle,
1237 const char * psz_function )
1239 #if defined(HAVE_DL_DYLD)
1240 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1241 return NSAddressOfSymbol( sym );
1243 #elif defined(HAVE_DL_BEOS)
1245 if( B_OK == get_image_symbol( handle, psz_function,
1246 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1255 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1256 wchar_t psz_real[256];
1257 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1259 return (void *)GetProcAddress( handle, psz_real );
1261 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1262 return (void *)GetProcAddress( handle, (MYCHAR*)psz_function );
1264 #elif defined(HAVE_DL_DLOPEN)
1265 return dlsym( handle, psz_function );
1267 #elif defined(HAVE_DL_SHL_LOAD)
1269 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1275 #if defined(HAVE_DL_WINDOWS)
1276 static char * GetWindowsError( void )
1278 #if defined(UNDER_CE)
1279 wchar_t psz_tmp[256];
1280 char * psz_buffer = malloc( 256 );
1282 char * psz_tmp = malloc( 256 );
1284 int i = 0, i_error = GetLastError();
1286 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1287 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1288 (LPTSTR) psz_tmp, 256, NULL );
1290 /* Go to the end of the string */
1291 #if defined(UNDER_CE)
1292 while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1294 while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1302 #if defined(UNDER_CE)
1303 swprintf( psz_tmp + i, L" (error %i)", i_error );
1304 psz_tmp[ 255 ] = L'\0';
1306 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1307 psz_tmp[ 255 ] = '\0';
1311 #if defined(UNDER_CE)
1312 WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1313 psz_buffer, 256, NULL, NULL );
1319 #endif /* HAVE_DL_WINDOWS */
1321 #endif /* HAVE_DYNAMIC_PLUGINS */