1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.81 2002/08/07 00:29:37 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"
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 # include "modules_plugin.h"
80 #if !defined( _MSC_VER )
81 # include "modules_builtin.h"
83 # include "modules_builtin_msvc.h"
86 /*****************************************************************************
88 *****************************************************************************/
89 #ifdef HAVE_DYNAMIC_PLUGINS
90 static void AllocateAllPlugins ( vlc_object_t * );
91 static void AllocatePluginDir ( vlc_object_t *, const char *, int );
92 static int AllocatePluginFile ( vlc_object_t *, char * );
94 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
95 static int DeleteModule ( module_t * );
96 static int LockModule ( module_t * );
97 static int UnlockModule ( module_t * );
98 #ifdef HAVE_DYNAMIC_PLUGINS
99 static int HideModule ( module_t * );
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";
118 p_bank->first = NULL;
120 vlc_mutex_init( p_this, &p_bank->lock );
123 * Store the symbols to be exported
125 #ifdef HAVE_DYNAMIC_PLUGINS
126 STORE_SYMBOLS( &p_bank->symbols );
129 /* Everything worked, attach the object */
130 p_this->p_vlc->p_module_bank = p_bank;
131 vlc_object_attach( p_bank, p_this->p_vlc );
136 /*****************************************************************************
137 * module_ResetBank: reset the module bank.
138 *****************************************************************************
139 * This function resets the module bank by unloading all unused plugin
141 *****************************************************************************/
142 void __module_ResetBank( vlc_object_t *p_this )
144 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
148 /*****************************************************************************
149 * module_EndBank: empty the module bank.
150 *****************************************************************************
151 * This function unloads all unused plugin modules and empties the module
152 * bank in case of success.
153 *****************************************************************************/
154 void __module_EndBank( vlc_object_t *p_this )
158 vlc_object_detach_all( p_this->p_vlc->p_module_bank );
160 while( p_this->p_vlc->p_module_bank->first != NULL )
162 if( DeleteModule( p_this->p_vlc->p_module_bank->first ) )
164 /* Module deletion failed */
165 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
166 p_this->p_vlc->p_module_bank->first->psz_object_name );
168 /* We just free the module by hand. Niahahahahaha. */
169 p_next = p_this->p_vlc->p_module_bank->first->next;
170 free( p_this->p_vlc->p_module_bank->first );
171 p_this->p_vlc->p_module_bank->first = p_next;
175 /* Destroy the lock */
176 vlc_mutex_destroy( &p_this->p_vlc->p_module_bank->lock );
178 vlc_object_destroy( p_this->p_vlc->p_module_bank );
183 /*****************************************************************************
184 * module_LoadMain: load the main program info into the module bank.
185 *****************************************************************************
186 * This function fills the module bank structure with the main module infos.
187 * This is very useful as it will allow us to consider the main program just
188 * as another module, and for instance the configuration options of main will
189 * be available in the module bank structure just as for every other module.
190 *****************************************************************************/
191 void __module_LoadMain( vlc_object_t *p_this )
193 AllocateBuiltinModule( p_this, vlc_entry__main );
196 /*****************************************************************************
197 * module_LoadBuiltins: load all modules which we built with.
198 *****************************************************************************
199 * This function fills the module bank structure with the builtin modules.
200 *****************************************************************************/
201 void __module_LoadBuiltins( vlc_object_t * p_this )
203 msg_Dbg( p_this, "checking builtin modules" );
204 ALLOCATE_ALL_BUILTINS();
207 /*****************************************************************************
208 * module_LoadPlugins: load all plugin modules we can find.
209 *****************************************************************************
210 * This function fills the module bank structure with the plugin modules.
211 *****************************************************************************/
212 void __module_LoadPlugins( vlc_object_t * p_this )
214 #ifdef HAVE_DYNAMIC_PLUGINS
215 msg_Dbg( p_this, "checking plugin modules" );
216 AllocateAllPlugins( p_this );
220 /*****************************************************************************
221 * module_ManageBank: manage the module bank.
222 *****************************************************************************
223 * This function parses the module bank and hides modules that have been
224 * unused for a while.
225 *****************************************************************************/
226 void __module_ManageBank( vlc_object_t *p_this )
228 #ifdef HAVE_DYNAMIC_PLUGINS
231 /* We take the global lock */
232 vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
234 /* Parse the module list to see if any modules need to be unloaded */
235 for( p_module = p_this->p_vlc->p_module_bank->first ;
237 p_module = p_module->next )
239 /* If the module is unused and if it is a plugin module... */
240 if( p_module->i_usage == 0 && !p_module->b_builtin )
242 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
244 p_module->i_unused_delay++;
248 msg_Dbg( p_this, "hiding unused plugin module \"%s\"",
249 p_module->psz_object_name );
250 HideModule( p_module );
252 /* Break here, so that we only hide one module at a time */
258 /* We release the global lock */
259 vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
260 #endif /* HAVE_DYNAMIC_PLUGINS */
265 /*****************************************************************************
266 * module_Need: return the best module function, given a capability list.
267 *****************************************************************************
268 * This function returns the module that best fits the asked capabilities.
269 *****************************************************************************/
270 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
271 const char *psz_name )
273 typedef struct module_list_t module_list_t;
279 module_list_t *p_next;
282 module_list_t *p_list, *p_first, *p_tmp;
285 vlc_bool_t b_intf = VLC_FALSE;
290 char *psz_shortcuts = NULL, *psz_var = NULL;
292 msg_Dbg( p_this, "looking for %s module", psz_capability );
294 /* Deal with variables */
295 if( psz_name && psz_name[0] == '$' )
297 psz_var = config_GetPsz( p_this, psz_name + 1 );
301 /* Count how many different shortcuts were asked for */
302 if( psz_name && *psz_name )
306 /* If the user wants none, give him none. */
307 if( !strcmp( psz_name, "none" ) )
309 if( psz_var ) free( psz_var );
314 psz_shortcuts = strdup( psz_name );
316 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
318 if( *psz_parser == ',' )
326 /* We take the global lock */
327 vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
329 /* Sort the modules and test them */
330 p_list = malloc( p_this->p_vlc->p_module_bank->i_count
331 * sizeof( module_list_t ) );
334 /* Parse the module list for capabilities and probe each of them */
335 for( p_module = p_this->p_vlc->p_module_bank->first ;
337 p_module = p_module->next )
339 module_t * p_submodule = NULL;
340 int i_shortcut_bonus = 0, i_submodule;
342 /* Test that this module can do what we need */
343 if( strcmp( p_module->psz_capability, psz_capability ) )
345 for( i_submodule = 0;
346 i_submodule < p_module->i_children;
349 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
350 ->psz_capability, psz_capability ) )
353 (module_t*)p_module->pp_children[ i_submodule ];
354 p_submodule->next = p_module->next;
359 if( p_submodule == NULL )
364 p_module = p_submodule;
367 /* Test if we have the required CPU */
368 if( (p_module->i_cpu & p_this->p_vlc->i_cpu) != p_module->i_cpu )
373 /* If we required a shortcut, check this plugin provides it. */
376 vlc_bool_t b_trash = VLC_TRUE;
377 int i_dummy, i_short = i_shortcuts;
378 char *psz_name = psz_shortcuts;
383 b_trash && p_module->pp_shortcuts[i_dummy];
386 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
387 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
392 i_shortcut_bonus = i_short * 10000;
396 /* Go to the next shortcut... This is so lame! */
410 /* If we didn't require a shortcut, trash zero-scored plugins */
411 else if( !p_module->i_score )
416 /* Special case: test if we requested a particular intf plugin */
417 if( p_module->psz_program
418 && !strcmp( p_module->psz_program,
419 p_this->p_vlc->psz_object_name ) )
423 /* Remove previous non-matching plugins */
430 /* This one doesn't match */
434 /* Store this new module */
435 p_list[ i_index ].p_module = p_module;
436 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
438 /* Add it to the modules-to-probe list */
441 p_list[ 0 ].p_next = NULL;
446 /* Ok, so at school you learned that quicksort is quick, and
447 * bubble sort sucks raw eggs. But that's when dealing with
448 * thousands of items. Here we have barely 50. */
449 module_list_t *p_newlist = p_first;
451 if( p_first->i_score < p_list[ i_index ].i_score )
453 p_list[ i_index ].p_next = p_first;
454 p_first = &p_list[ i_index ];
458 while( p_newlist->p_next != NULL &&
459 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
461 p_newlist = p_newlist->p_next;
464 p_list[ i_index ].p_next = p_newlist->p_next;
465 p_newlist->p_next = &p_list[ i_index ];
472 msg_Dbg( p_this, "probing %i candidate%s",
473 i_index, i_index == 1 ? "" : "s" );
475 /* Lock all selected modules */
477 while( p_tmp != NULL )
479 LockModule( p_tmp->p_module );
480 p_tmp = p_tmp->p_next;
483 /* We can release the global lock, module refcounts were incremented */
484 vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
486 /* Parse the linked list and use the first successful module */
488 while( p_tmp != NULL )
490 if( p_tmp->p_module->pf_activate
491 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
496 UnlockModule( p_tmp->p_module );
497 p_tmp = p_tmp->p_next;
500 /* Store the locked module value */
503 p_module = p_tmp->p_module;
504 p_tmp = p_tmp->p_next;
511 /* Unlock the remaining modules */
512 while( p_tmp != NULL )
514 UnlockModule( p_tmp->p_module );
515 p_tmp = p_tmp->p_next;
520 if( p_module != NULL )
522 msg_Info( p_module, "using %s module \"%s\"",
523 psz_capability, p_module->psz_object_name );
525 else if( p_first == NULL )
527 msg_Err( p_this, "no %s module matched \"%s\"",
528 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
530 else if( psz_name != NULL && *psz_name )
532 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
533 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
538 free( psz_shortcuts );
546 /* Don't forget that the module is still locked */
550 /*****************************************************************************
551 * module_Unneed: decrease the usage count of a module.
552 *****************************************************************************
553 * This function must be called by the thread that called module_Need, to
554 * decrease the reference count and allow for hiding of modules.
555 *****************************************************************************/
556 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
558 /* Use the close method */
559 if( p_module->pf_deactivate )
561 p_module->pf_deactivate( p_this );
564 /* We take the global lock */
565 vlc_mutex_lock( &p_module->p_vlc->p_module_bank->lock );
567 /* Just unlock the module - we can't do anything if it fails,
568 * so there is no need to check the return value. */
569 UnlockModule( p_module );
571 msg_Info( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
573 /* We release the global lock */
574 vlc_mutex_unlock( &p_module->p_vlc->p_module_bank->lock );
579 /*****************************************************************************
580 * Following functions are local.
581 *****************************************************************************/
583 /*****************************************************************************
584 * AllocateAllPlugins: load all plugin modules we can find.
585 *****************************************************************************/
586 #ifdef HAVE_DYNAMIC_PLUGINS
587 static void AllocateAllPlugins( vlc_object_t *p_this )
589 /* Yes, there are two NULLs because we replace one with "plugin-path". */
590 char * path[] = { "modules", PLUGIN_PATH, NULL, NULL };
592 char ** ppsz_path = path;
594 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
595 char * psz_vlcpath = system_GetProgramPath();
596 int i_vlclen = strlen( psz_vlcpath );
597 vlc_bool_t b_notinroot;
600 /* If the user provided a plugin path, we add it to the list */
601 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
604 for( ; *ppsz_path != NULL ; ppsz_path++ )
606 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
607 /* Store strlen(*ppsz_path) for later use. */
608 int i_dirlen = strlen( *ppsz_path );
610 b_notinroot = VLC_FALSE;
611 /* Under BeOS, we need to add beos_GetProgramPath() to access
612 * files under the current directory */
613 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
615 i_dirlen += i_vlclen + 2;
616 b_notinroot = VLC_TRUE;
618 psz_fullpath = malloc( i_dirlen );
619 if( psz_fullpath == NULL )
623 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
628 psz_fullpath = *ppsz_path;
631 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
633 /* Don't go deeper than 5 subdirectories */
634 AllocatePluginDir( p_this, psz_fullpath, 5 );
636 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
639 free( psz_fullpath );
645 /*****************************************************************************
646 * AllocatePluginDir: recursively parse a directory to look for plugins
647 *****************************************************************************/
648 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
651 #define PLUGIN_EXT ".so"
656 struct dirent * file;
663 dir = opendir( psz_dir );
670 i_dirlen = strlen( psz_dir );
672 /* Parse the directory and try to load all files it contains. */
673 while( (file = readdir( dir )) )
676 int i_len = strlen( file->d_name );
678 /* Skip ".", ".." and anything starting with "." */
679 if( !*file->d_name || *file->d_name == '.' )
684 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
685 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
687 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
689 AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
691 else if( i_len > strlen( PLUGIN_EXT )
692 /* We only load files ending with ".so" */
693 && !strncmp( file->d_name + i_len - strlen( PLUGIN_EXT ),
694 PLUGIN_EXT, strlen( PLUGIN_EXT ) ) )
696 AllocatePluginFile( p_this, psz_file );
702 /* Close the directory */
706 /*****************************************************************************
707 * AllocatePluginFile: load a module into memory and initialize it.
708 *****************************************************************************
709 * This function loads a dynamically loadable module and allocates a structure
710 * for its information data. The module can then be handled by module_Need,
711 * module_Unneed and HideModule. It can be removed by DeleteModule.
712 *****************************************************************************/
713 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
716 module_handle_t handle;
718 /* Try to dynamically load the module. */
719 if( module_load( psz_file, &handle ) )
721 char psz_buffer[256];
723 /* The plugin module couldn't be opened */
724 msg_Warn( p_this, "cannot open `%s' (%s)",
725 psz_file, module_error( psz_buffer ) );
729 /* Now that we have successfully loaded the module, we can
730 * allocate a structure for it */
731 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
732 if( p_module == NULL )
734 msg_Err( p_this, "out of memory" );
735 module_unload( handle );
739 /* We need to fill these since they may be needed by CallEntry() */
740 p_module->psz_filename = psz_file;
741 p_module->handle = handle;
742 p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
744 /* Initialize the module: fill p_module->psz_object_name, default config */
745 if( CallEntry( p_module ) != 0 )
747 /* We couldn't call module_init() */
748 vlc_object_destroy( p_module );
749 module_unload( handle );
753 DupModule( p_module );
754 p_module->psz_filename = strdup( p_module->psz_filename );
755 p_module->psz_longname = strdup( p_module->psz_longname );
757 /* Everything worked fine ! The module is ready to be added to the list. */
758 p_module->i_usage = 0;
759 p_module->i_unused_delay = 0;
761 p_module->b_builtin = VLC_FALSE;
763 /* Link module into the linked list */
764 if( p_this->p_vlc->p_module_bank->first != NULL )
766 p_this->p_vlc->p_module_bank->first->prev = p_module;
768 p_module->next = p_this->p_vlc->p_module_bank->first;
769 p_module->prev = NULL;
770 p_this->p_vlc->p_module_bank->first = p_module;
771 p_this->p_vlc->p_module_bank->i_count++;
773 //msg_Dbg( p_this, "plugin \"%s\", %s",
774 // p_module->psz_object_name, p_module->psz_longname );
776 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
781 /*****************************************************************************
782 * DupModule: make a plugin module standalone.
783 *****************************************************************************
784 * This function duplicates all strings in the module, so that the dynamic
785 * object can be unloaded. It acts recursively on submodules.
786 *****************************************************************************/
787 static void DupModule( module_t *p_module )
792 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
794 *pp_shortcut = strdup( *pp_shortcut );
797 /* We strdup() these entries so that they are still valid when the
798 * module is unloaded. */
799 p_module->psz_object_name = strdup( p_module->psz_object_name );
800 p_module->psz_capability = strdup( p_module->psz_capability );
802 if( p_module->psz_program != NULL )
804 p_module->psz_program = strdup( p_module->psz_program );
807 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
809 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
813 /*****************************************************************************
814 * UndupModule: free a duplicated module.
815 *****************************************************************************
816 * This function frees the allocations done in DupModule().
817 *****************************************************************************/
818 static void UndupModule( module_t *p_module )
823 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
825 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
828 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
830 free( *pp_shortcut );
833 free( p_module->psz_object_name );
834 free( p_module->psz_capability );
836 if( p_module->psz_program != NULL )
838 free( p_module->psz_program );
842 #endif /* HAVE_DYNAMIC_PLUGINS */
844 /*****************************************************************************
845 * AllocateBuiltinModule: initialize a builtin module.
846 *****************************************************************************
847 * This function registers a builtin module and allocates a structure
848 * for its information data. The module can then be handled by module_Need,
849 * module_Unneed and HideModule. It can be removed by DeleteModule.
850 *****************************************************************************/
851 static int AllocateBuiltinModule( vlc_object_t * p_this,
852 int ( *pf_entry ) ( module_t * ) )
856 /* Now that we have successfully loaded the module, we can
857 * allocate a structure for it */
858 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
859 if( p_module == NULL )
861 msg_Err( p_this, "out of memory" );
865 /* Initialize the module : fill p_module->psz_object_name, etc. */
866 if( pf_entry( p_module ) != 0 )
868 /* With a well-written module we shouldn't have to print an
869 * additional error message here, but just make sure. */
870 msg_Err( p_this, "failed calling entry point in builtin module" );
871 vlc_object_destroy( p_module );
875 /* Everything worked fine ! The module is ready to be added to the list. */
876 p_module->i_usage = 0;
877 p_module->i_unused_delay = 0;
879 p_module->b_builtin = VLC_TRUE;
881 /* Link module into the linked list */
882 if( p_this->p_vlc->p_module_bank->first != NULL )
884 p_this->p_vlc->p_module_bank->first->prev = p_module;
886 p_module->next = p_this->p_vlc->p_module_bank->first;
887 p_module->prev = NULL;
888 p_this->p_vlc->p_module_bank->first = p_module;
889 p_this->p_vlc->p_module_bank->i_count++;
891 //msg_Dbg( p_this, "builtin \"%s\", %s",
892 // p_module->psz_object_name, p_module->psz_longname );
894 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
899 /*****************************************************************************
900 * DeleteModule: delete a module and its structure.
901 *****************************************************************************
902 * This function can only be called if i_usage <= 0.
903 *****************************************************************************/
904 static int DeleteModule( module_t * p_module )
906 /* If the module is not in use but is still in memory, we first have
907 * to hide it and remove it from memory before we can free the
909 if( p_module->b_builtin )
911 if( p_module->i_usage != 0 )
913 msg_Err( p_module, "trying to free builtin module \"%s\" with "
914 "usage %i", p_module->psz_object_name, p_module->i_usage );
918 #ifdef HAVE_DYNAMIC_PLUGINS
921 if( p_module->i_usage >= 1 )
923 msg_Err( p_module, "trying to free module \"%s\" which is "
924 "still in use", p_module->psz_object_name );
928 /* Two possibilities here: i_usage == -1 and the module is already
929 * unloaded, we can continue, or i_usage == 0, and we have to hide
930 * the module before going on. */
931 if( p_module->i_usage == 0 )
933 if( HideModule( p_module ) != 0 )
941 vlc_object_detach_all( p_module );
943 /* Unlink the module from the linked list. */
944 if( p_module->prev != NULL )
946 p_module->prev->next = p_module->next;
950 p_module->p_vlc->p_module_bank->first = p_module->next;
953 if( p_module->next != NULL )
955 p_module->next->prev = p_module->prev;
958 p_module->p_vlc->p_module_bank->i_count--;
960 /* We free the structures that we strdup()ed in Allocate*Module(). */
961 #ifdef HAVE_DYNAMIC_PLUGINS
962 if( !p_module->b_builtin )
964 UndupModule( p_module );
965 free( p_module->psz_filename );
966 free( p_module->psz_longname );
970 /* Free and detach the object's children */
971 while( p_module->i_children )
973 vlc_object_t *p_this = p_module->pp_children[0];
974 vlc_object_detach_all( p_this );
975 vlc_object_destroy( p_this );
978 config_Free( p_module );
979 vlc_object_destroy( p_module );
984 /*****************************************************************************
985 * LockModule: increase the usage count of a module and load it if needed.
986 *****************************************************************************
987 * This function has to be called before a thread starts using a module. If
988 * the module is already loaded, we just increase its usage count. If it isn't
989 * loaded, we have to dynamically open it and initialize it.
990 * If you successfully call LockModule() at any moment, be careful to call
991 * UnlockModule() when you don't need it anymore.
992 *****************************************************************************/
993 static int LockModule( module_t * p_module )
995 if( p_module->i_usage >= 0 )
997 /* This module is already loaded and activated, we can return */
1002 if( p_module->b_builtin )
1004 /* A builtin module should always have a refcount >= 0 ! */
1005 msg_Err( p_module, "builtin module \"%s\" has refcount %i",
1006 p_module->psz_object_name, p_module->i_usage );
1010 #ifdef HAVE_DYNAMIC_PLUGINS
1011 if( p_module->i_usage != -1 )
1013 /* This shouldn't happen. Ever. We have serious problems here. */
1014 msg_Err( p_module, "plugin module \"%s\" has refcount %i",
1015 p_module->psz_object_name, p_module->i_usage );
1019 /* i_usage == -1, which means that the module isn't in memory */
1020 if( module_load( p_module->psz_filename, &p_module->handle ) )
1022 char psz_buffer[256];
1024 /* The plugin module couldn't be opened */
1025 msg_Err( p_module, "cannot open `%s' (%s)",
1026 p_module->psz_filename, module_error(psz_buffer) );
1030 /* FIXME: what to do if the guy modified the plugin while it was
1031 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1032 * to be a bit more clever here. */
1034 /* Everything worked fine ! The module is ready to be used */
1035 p_module->i_usage = 1;
1036 #endif /* HAVE_DYNAMIC_PLUGINS */
1041 /*****************************************************************************
1042 * UnlockModule: decrease the usage count of a module.
1043 *****************************************************************************
1044 * We decrease the usage count of a module so that we know when a module
1045 * becomes unused and can be hidden.
1046 *****************************************************************************/
1047 static int UnlockModule( module_t * p_module )
1049 if( p_module->i_usage <= 0 )
1051 /* This shouldn't happen. Ever. We have serious problems here. */
1052 msg_Err( p_module, "trying to call module_Unneed() on \"%s\" "
1053 "which is not in use", p_module->psz_object_name );
1057 /* This module is still in use, we can return */
1058 p_module->i_usage--;
1059 p_module->i_unused_delay = 0;
1064 #ifdef HAVE_DYNAMIC_PLUGINS
1065 /*****************************************************************************
1066 * HideModule: remove a module from memory but keep its structure.
1067 *****************************************************************************
1068 * This function can only be called if i_usage == 0. It will make a call
1069 * to the module's inner module_deactivate() symbol, and then unload it
1070 * from memory. A call to module_Need() will automagically load it again.
1071 *****************************************************************************/
1072 static int HideModule( module_t * p_module )
1074 if( p_module->b_builtin )
1076 /* A builtin module should never be hidden. */
1077 msg_Err( p_module, "trying to hide builtin module \"%s\"",
1078 p_module->psz_object_name );
1082 if( p_module->i_usage >= 1 )
1084 msg_Err( p_module, "trying to hide module \"%s\" which is still "
1085 "in use", p_module->psz_object_name );
1089 if( p_module->i_usage <= -1 )
1091 msg_Err( p_module, "trying to hide module \"%s\" which is already "
1092 "hidden", p_module->psz_object_name );
1096 /* Everything worked fine, we can safely unload the module. */
1097 module_unload( p_module->handle );
1098 p_module->i_usage = -1;
1103 /*****************************************************************************
1104 * CallEntry: call an entry point.
1105 *****************************************************************************
1106 * This function calls a symbol given its name and a module structure. The
1107 * symbol MUST refer to a function returning int and taking a module_t* as
1109 *****************************************************************************/
1110 static int CallEntry( module_t * p_module )
1112 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1113 int (* pf_symbol) ( module_t * p_module );
1115 /* Try to resolve the symbol */
1116 pf_symbol = module_getsymbol( p_module->handle, psz_name );
1118 if( pf_symbol == NULL )
1120 char psz_buffer[256];
1122 /* We couldn't load the symbol */
1123 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1124 psz_name, p_module->psz_filename,
1125 module_error( psz_buffer ) );
1129 /* We can now try to call the symbol */
1130 if( pf_symbol( p_module ) != 0 )
1132 /* With a well-written module we shouldn't have to print an
1133 * additional error message here, but just make sure. */
1134 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1135 psz_name, p_module->psz_filename );
1139 /* Everything worked fine, we can return */
1142 #endif /* HAVE_DYNAMIC_PLUGINS */