1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.79 2002/08/05 11:48:56 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() */
40 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
41 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
42 # define HAVE_DYNAMIC_PLUGINS
43 #elif defined(HAVE_IMAGE_H) /* BeOS */
45 # define HAVE_DYNAMIC_PLUGINS
47 # define HAVE_DYNAMIC_PLUGINS
49 # undef HAVE_DYNAMIC_PLUGINS
55 #include "interface.h"
56 #include "vlc_playlist.h"
57 #include "intf_eject.h"
59 #include "stream_control.h"
60 #include "input_ext-intf.h"
61 #include "input_ext-dec.h"
62 #include "input_ext-plugins.h"
65 #include "video_output.h"
67 #include "audio_output.h"
71 #ifdef HAVE_DYNAMIC_PLUGINS
72 # include "modules_plugin.h"
75 #if !defined( _MSC_VER )
76 # include "modules_builtin.h"
78 # include "modules_builtin_msvc.h"
81 /*****************************************************************************
83 *****************************************************************************/
84 #ifdef HAVE_DYNAMIC_PLUGINS
85 static void AllocateAllPlugins ( vlc_object_t * );
86 static void AllocatePluginDir ( vlc_object_t *, const char * );
87 static int AllocatePluginFile ( vlc_object_t *, char * );
89 static int AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
90 static int DeleteModule ( module_t * );
91 static int LockModule ( module_t * );
92 static int UnlockModule ( module_t * );
93 #ifdef HAVE_DYNAMIC_PLUGINS
94 static int HideModule ( module_t * );
95 static void DupModule ( module_t * );
96 static void UndupModule ( module_t * );
97 static int CallEntry ( module_t * );
100 /*****************************************************************************
101 * module_InitBank: create the module bank.
102 *****************************************************************************
103 * This function creates a module bank structure which will be filled later
104 * on with all the modules found.
105 *****************************************************************************/
106 void __module_InitBank( vlc_object_t *p_this )
108 module_bank_t *p_bank;
110 p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
111 p_bank->psz_object_name = "module bank";
113 p_bank->first = NULL;
115 vlc_mutex_init( p_this, &p_bank->lock );
118 * Store the symbols to be exported
120 #ifdef HAVE_DYNAMIC_PLUGINS
121 STORE_SYMBOLS( &p_bank->symbols );
124 /* Everything worked, attach the object */
125 p_this->p_vlc->p_module_bank = p_bank;
126 vlc_object_attach( p_bank, p_this->p_vlc );
131 /*****************************************************************************
132 * module_ResetBank: reset the module bank.
133 *****************************************************************************
134 * This function resets the module bank by unloading all unused plugin
136 *****************************************************************************/
137 void __module_ResetBank( vlc_object_t *p_this )
139 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
143 /*****************************************************************************
144 * module_EndBank: empty the module bank.
145 *****************************************************************************
146 * This function unloads all unused plugin modules and empties the module
147 * bank in case of success.
148 *****************************************************************************/
149 void __module_EndBank( vlc_object_t *p_this )
153 vlc_object_detach_all( p_this->p_vlc->p_module_bank );
155 while( p_this->p_vlc->p_module_bank->first != NULL )
157 if( DeleteModule( p_this->p_vlc->p_module_bank->first ) )
159 /* Module deletion failed */
160 msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
161 p_this->p_vlc->p_module_bank->first->psz_object_name );
163 /* We just free the module by hand. Niahahahahaha. */
164 p_next = p_this->p_vlc->p_module_bank->first->next;
165 free( p_this->p_vlc->p_module_bank->first );
166 p_this->p_vlc->p_module_bank->first = p_next;
170 /* Destroy the lock */
171 vlc_mutex_destroy( &p_this->p_vlc->p_module_bank->lock );
173 vlc_object_destroy( p_this->p_vlc->p_module_bank );
178 /*****************************************************************************
179 * module_LoadMain: load the main program info into the module bank.
180 *****************************************************************************
181 * This function fills the module bank structure with the main module infos.
182 * This is very useful as it will allow us to consider the main program just
183 * as another module, and for instance the configuration options of main will
184 * be available in the module bank structure just as for every other module.
185 *****************************************************************************/
186 void __module_LoadMain( vlc_object_t *p_this )
188 AllocateBuiltinModule( p_this, vlc_entry__main );
191 /*****************************************************************************
192 * module_LoadBuiltins: load all modules which we built with.
193 *****************************************************************************
194 * This function fills the module bank structure with the builtin modules.
195 *****************************************************************************/
196 void __module_LoadBuiltins( vlc_object_t * p_this )
198 msg_Dbg( p_this, "checking builtin modules" );
199 ALLOCATE_ALL_BUILTINS();
202 /*****************************************************************************
203 * module_LoadPlugins: load all plugin modules we can find.
204 *****************************************************************************
205 * This function fills the module bank structure with the plugin modules.
206 *****************************************************************************/
207 void __module_LoadPlugins( vlc_object_t * p_this )
209 #ifdef HAVE_DYNAMIC_PLUGINS
210 msg_Dbg( p_this, "checking plugin modules" );
211 AllocateAllPlugins( p_this );
215 /*****************************************************************************
216 * module_ManageBank: manage the module bank.
217 *****************************************************************************
218 * This function parses the module bank and hides modules that have been
219 * unused for a while.
220 *****************************************************************************/
221 void __module_ManageBank( vlc_object_t *p_this )
223 #ifdef HAVE_DYNAMIC_PLUGINS
226 /* We take the global lock */
227 vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
229 /* Parse the module list to see if any modules need to be unloaded */
230 for( p_module = p_this->p_vlc->p_module_bank->first ;
232 p_module = p_module->next )
234 /* If the module is unused and if it is a plugin module... */
235 if( p_module->i_usage == 0 && !p_module->b_builtin )
237 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
239 p_module->i_unused_delay++;
243 msg_Dbg( p_this, "hiding unused plugin module \"%s\"",
244 p_module->psz_object_name );
245 HideModule( p_module );
247 /* Break here, so that we only hide one module at a time */
253 /* We release the global lock */
254 vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
255 #endif /* HAVE_DYNAMIC_PLUGINS */
260 /*****************************************************************************
261 * module_Need: return the best module function, given a capability list.
262 *****************************************************************************
263 * This function returns the module that best fits the asked capabilities.
264 *****************************************************************************/
265 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
266 const char *psz_name )
268 typedef struct module_list_t module_list_t;
274 module_list_t *p_next;
277 module_list_t *p_list, *p_first, *p_tmp;
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] == '$' )
292 psz_var = config_GetPsz( p_this, psz_name + 1 );
296 /* Count how many different shortcuts were asked for */
297 if( psz_name && *psz_name )
301 /* If the user wants none, give him none. */
302 if( !strcmp( psz_name, "none" ) )
304 if( psz_var ) free( psz_var );
309 psz_shortcuts = strdup( psz_name );
311 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
313 if( *psz_parser == ',' )
321 /* We take the global lock */
322 vlc_mutex_lock( &p_this->p_vlc->p_module_bank->lock );
324 /* Sort the modules and test them */
325 p_list = malloc( p_this->p_vlc->p_module_bank->i_count
326 * sizeof( module_list_t ) );
329 /* Parse the module list for capabilities and probe each of them */
330 for( p_module = p_this->p_vlc->p_module_bank->first ;
332 p_module = p_module->next )
334 module_t * p_submodule = NULL;
335 int i_shortcut_bonus = 0, i_submodule;
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 ];
349 p_submodule->next = p_module->next;
354 if( p_submodule == NULL )
359 p_module = p_submodule;
362 /* Test if we have the required CPU */
363 if( (p_module->i_cpu & p_this->p_vlc->i_cpu) != p_module->i_cpu )
368 /* If we required a shortcut, check this plugin provides it. */
371 vlc_bool_t b_trash = VLC_TRUE;
372 int i_dummy, i_short = i_shortcuts;
373 char *psz_name = psz_shortcuts;
378 b_trash && p_module->pp_shortcuts[i_dummy];
381 b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
382 && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
387 i_shortcut_bonus = i_short * 10000;
391 /* Go to the next shortcut... This is so lame! */
405 /* If we didn't require a shortcut, trash zero-scored plugins */
406 else if( !p_module->i_score )
411 /* Special case: test if we requested a particular intf plugin */
412 if( p_module->psz_program
413 && !strcmp( p_module->psz_program,
414 p_this->p_vlc->psz_object_name ) )
418 /* Remove previous non-matching plugins */
425 /* This one doesn't match */
429 /* Store this new module */
430 p_list[ i_index ].p_module = p_module;
431 p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
433 /* Add it to the modules-to-probe list */
436 p_list[ 0 ].p_next = NULL;
441 /* Ok, so at school you learned that quicksort is quick, and
442 * bubble sort sucks raw eggs. But that's when dealing with
443 * thousands of items. Here we have barely 50. */
444 module_list_t *p_newlist = p_first;
446 if( p_first->i_score < p_list[ i_index ].i_score )
448 p_list[ i_index ].p_next = p_first;
449 p_first = &p_list[ i_index ];
453 while( p_newlist->p_next != NULL &&
454 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
456 p_newlist = p_newlist->p_next;
459 p_list[ i_index ].p_next = p_newlist->p_next;
460 p_newlist->p_next = &p_list[ i_index ];
467 msg_Dbg( p_this, "probing %i candidate%s",
468 i_index, i_index == 1 ? "" : "s" );
470 /* Lock all selected modules */
472 while( p_tmp != NULL )
474 LockModule( p_tmp->p_module );
475 p_tmp = p_tmp->p_next;
478 /* We can release the global lock, module refcounts were incremented */
479 vlc_mutex_unlock( &p_this->p_vlc->p_module_bank->lock );
481 /* Parse the linked list and use the first successful module */
483 while( p_tmp != NULL )
485 if( p_tmp->p_module->pf_activate
486 && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
491 UnlockModule( p_tmp->p_module );
492 p_tmp = p_tmp->p_next;
495 /* Store the locked module value */
498 p_module = p_tmp->p_module;
499 p_tmp = p_tmp->p_next;
506 /* Unlock the remaining modules */
507 while( p_tmp != NULL )
509 UnlockModule( p_tmp->p_module );
510 p_tmp = p_tmp->p_next;
515 if( p_module != NULL )
517 msg_Info( p_module, "using %s module \"%s\"",
518 psz_capability, p_module->psz_object_name );
520 else if( p_first == NULL )
522 msg_Err( p_this, "no %s module matched \"%s\"",
523 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
525 else if( psz_name != NULL && *psz_name )
527 msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
528 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
533 free( psz_shortcuts );
541 /* Don't forget that the module is still locked */
545 /*****************************************************************************
546 * module_Unneed: decrease the usage count of a module.
547 *****************************************************************************
548 * This function must be called by the thread that called module_Need, to
549 * decrease the reference count and allow for hiding of modules.
550 *****************************************************************************/
551 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
553 /* Use the close method */
554 if( p_module->pf_deactivate )
556 p_module->pf_deactivate( p_this );
559 /* We take the global lock */
560 vlc_mutex_lock( &p_module->p_vlc->p_module_bank->lock );
562 /* Just unlock the module - we can't do anything if it fails,
563 * so there is no need to check the return value. */
564 UnlockModule( p_module );
566 msg_Info( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
568 /* We release the global lock */
569 vlc_mutex_unlock( &p_module->p_vlc->p_module_bank->lock );
574 /*****************************************************************************
575 * Following functions are local.
576 *****************************************************************************/
578 /*****************************************************************************
579 * AllocateAllPlugins: load all plugin modules we can find.
580 *****************************************************************************/
581 #ifdef HAVE_DYNAMIC_PLUGINS
582 static void AllocateAllPlugins( vlc_object_t *p_this )
584 /* Yes, there are two NULLs because we replace one with "plugin-path". */
585 char * path[] = { "modules", PLUGIN_PATH, NULL, NULL };
587 char ** ppsz_path = path;
589 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
590 char * psz_vlcpath = system_GetProgramPath();
591 int i_vlclen = strlen( psz_vlcpath );
592 vlc_bool_t b_notinroot;
595 /* If the user provided a plugin path, we add it to the list */
596 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
599 for( ; *ppsz_path != NULL ; ppsz_path++ )
601 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
602 /* Store strlen(*ppsz_path) for later use. */
603 int i_dirlen = strlen( *ppsz_path );
605 b_notinroot = VLC_FALSE;
606 /* Under BeOS, we need to add beos_GetProgramPath() to access
607 * files under the current directory */
608 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
610 i_dirlen += i_vlclen + 2;
611 b_notinroot = VLC_TRUE;
613 psz_fullpath = malloc( i_dirlen );
614 if( psz_fullpath == NULL )
618 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
623 psz_fullpath = *ppsz_path;
626 msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
628 AllocatePluginDir( p_this, psz_fullpath );
630 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
633 free( psz_fullpath );
639 /*****************************************************************************
640 * AllocatePluginDir: recursively parse a directory to look for plugins
641 *****************************************************************************/
642 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir )
644 #define PLUGIN_EXT ".so"
646 int i_dirlen = strlen( psz_dir );
648 struct dirent * file;
650 if( (dir = opendir( psz_dir )) )
652 /* Parse the directory and try to load all files it contains. */
653 while( (file = readdir( dir )) )
655 int i_len = strlen( file->d_name );
657 /* Skip ".", ".." and anything starting with "." */
658 if( !*file->d_name || *file->d_name == '.' )
663 if( file->d_type == DT_DIR )
665 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
666 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
667 AllocatePluginDir( p_this, psz_file );
670 else if( i_len > strlen( PLUGIN_EXT )
671 /* We only load files ending with ".so" */
672 && !strncmp( file->d_name + i_len - strlen( PLUGIN_EXT ),
673 PLUGIN_EXT, strlen( PLUGIN_EXT ) ) )
675 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
676 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
677 AllocatePluginFile( p_this, psz_file );
682 /* Close the directory if successfully opened */
687 /*****************************************************************************
688 * AllocatePluginFile: load a module into memory and initialize it.
689 *****************************************************************************
690 * This function loads a dynamically loadable module and allocates a structure
691 * for its information data. The module can then be handled by module_Need,
692 * module_Unneed and HideModule. It can be removed by DeleteModule.
693 *****************************************************************************/
694 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
697 module_handle_t handle;
699 /* Try to dynamically load the module. */
700 if( module_load( psz_file, &handle ) )
702 char psz_buffer[256];
704 /* The plugin module couldn't be opened */
705 msg_Warn( p_this, "cannot open `%s' (%s)",
706 psz_file, module_error( psz_buffer ) );
710 /* Now that we have successfully loaded the module, we can
711 * allocate a structure for it */
712 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
713 if( p_module == NULL )
715 msg_Err( p_this, "out of memory" );
716 module_unload( handle );
720 /* We need to fill these since they may be needed by CallEntry() */
721 p_module->psz_filename = psz_file;
722 p_module->handle = handle;
723 p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
725 /* Initialize the module: fill p_module->psz_object_name, default config */
726 if( CallEntry( p_module ) != 0 )
728 /* We couldn't call module_init() */
729 vlc_object_destroy( p_module );
730 module_unload( handle );
734 DupModule( p_module );
735 p_module->psz_filename = strdup( p_module->psz_filename );
736 p_module->psz_longname = strdup( p_module->psz_longname );
738 /* Everything worked fine ! The module is ready to be added to the list. */
739 p_module->i_usage = 0;
740 p_module->i_unused_delay = 0;
742 p_module->b_builtin = VLC_FALSE;
744 /* Link module into the linked list */
745 if( p_this->p_vlc->p_module_bank->first != NULL )
747 p_this->p_vlc->p_module_bank->first->prev = p_module;
749 p_module->next = p_this->p_vlc->p_module_bank->first;
750 p_module->prev = NULL;
751 p_this->p_vlc->p_module_bank->first = p_module;
752 p_this->p_vlc->p_module_bank->i_count++;
754 msg_Dbg( p_this, "plugin \"%s\", %s",
755 p_module->psz_object_name, p_module->psz_longname );
757 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
762 /*****************************************************************************
763 * DupModule: make a plugin module standalone.
764 *****************************************************************************
765 * This function duplicates all strings in the module, so that the dynamic
766 * object can be unloaded. It acts recursively on submodules.
767 *****************************************************************************/
768 static void DupModule( module_t *p_module )
773 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
775 *pp_shortcut = strdup( *pp_shortcut );
778 /* We strdup() these entries so that they are still valid when the
779 * module is unloaded. */
780 p_module->psz_object_name = strdup( p_module->psz_object_name );
781 p_module->psz_capability = strdup( p_module->psz_capability );
783 if( p_module->psz_program != NULL )
785 p_module->psz_program = strdup( p_module->psz_program );
788 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
790 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
794 /*****************************************************************************
795 * UndupModule: free a duplicated module.
796 *****************************************************************************
797 * This function frees the allocations done in DupModule().
798 *****************************************************************************/
799 static void UndupModule( module_t *p_module )
804 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
806 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
809 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
811 free( *pp_shortcut );
814 free( p_module->psz_object_name );
815 free( p_module->psz_capability );
817 if( p_module->psz_program != NULL )
819 free( p_module->psz_program );
823 #endif /* HAVE_DYNAMIC_PLUGINS */
825 /*****************************************************************************
826 * AllocateBuiltinModule: initialize a builtin module.
827 *****************************************************************************
828 * This function registers a builtin module and allocates a structure
829 * for its information data. The module can then be handled by module_Need,
830 * module_Unneed and HideModule. It can be removed by DeleteModule.
831 *****************************************************************************/
832 static int AllocateBuiltinModule( vlc_object_t * p_this,
833 int ( *pf_entry ) ( module_t * ) )
837 /* Now that we have successfully loaded the module, we can
838 * allocate a structure for it */
839 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
840 if( p_module == NULL )
842 msg_Err( p_this, "out of memory" );
846 /* Initialize the module : fill p_module->psz_object_name, etc. */
847 if( pf_entry( p_module ) != 0 )
849 /* With a well-written module we shouldn't have to print an
850 * additional error message here, but just make sure. */
851 msg_Err( p_this, "failed calling entry point in builtin module" );
852 vlc_object_destroy( p_module );
856 /* Everything worked fine ! The module is ready to be added to the list. */
857 p_module->i_usage = 0;
858 p_module->i_unused_delay = 0;
860 p_module->b_builtin = VLC_TRUE;
862 /* Link module into the linked list */
863 if( p_this->p_vlc->p_module_bank->first != NULL )
865 p_this->p_vlc->p_module_bank->first->prev = p_module;
867 p_module->next = p_this->p_vlc->p_module_bank->first;
868 p_module->prev = NULL;
869 p_this->p_vlc->p_module_bank->first = p_module;
870 p_this->p_vlc->p_module_bank->i_count++;
872 msg_Dbg( p_this, "builtin \"%s\", %s",
873 p_module->psz_object_name, p_module->psz_longname );
875 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
880 /*****************************************************************************
881 * DeleteModule: delete a module and its structure.
882 *****************************************************************************
883 * This function can only be called if i_usage <= 0.
884 *****************************************************************************/
885 static int DeleteModule( module_t * p_module )
887 /* If the module is not in use but is still in memory, we first have
888 * to hide it and remove it from memory before we can free the
890 if( p_module->b_builtin )
892 if( p_module->i_usage != 0 )
894 msg_Err( p_module, "trying to free builtin module \"%s\" with "
895 "usage %i", p_module->psz_object_name, p_module->i_usage );
899 #ifdef HAVE_DYNAMIC_PLUGINS
902 if( p_module->i_usage >= 1 )
904 msg_Err( p_module, "trying to free module \"%s\" which is "
905 "still in use", p_module->psz_object_name );
909 /* Two possibilities here: i_usage == -1 and the module is already
910 * unloaded, we can continue, or i_usage == 0, and we have to hide
911 * the module before going on. */
912 if( p_module->i_usage == 0 )
914 if( HideModule( p_module ) != 0 )
922 vlc_object_detach_all( p_module );
924 /* Unlink the module from the linked list. */
925 if( p_module->prev != NULL )
927 p_module->prev->next = p_module->next;
931 p_module->p_vlc->p_module_bank->first = p_module->next;
934 if( p_module->next != NULL )
936 p_module->next->prev = p_module->prev;
939 p_module->p_vlc->p_module_bank->i_count--;
941 /* We free the structures that we strdup()ed in Allocate*Module(). */
942 #ifdef HAVE_DYNAMIC_PLUGINS
943 if( !p_module->b_builtin )
945 UndupModule( p_module );
946 free( p_module->psz_filename );
947 free( p_module->psz_longname );
951 /* Free and detach the object's children */
952 while( p_module->i_children )
954 vlc_object_t *p_this = p_module->pp_children[0];
955 vlc_object_detach_all( p_this );
956 vlc_object_destroy( p_this );
959 config_Free( p_module );
960 vlc_object_destroy( p_module );
965 /*****************************************************************************
966 * LockModule: increase the usage count of a module and load it if needed.
967 *****************************************************************************
968 * This function has to be called before a thread starts using a module. If
969 * the module is already loaded, we just increase its usage count. If it isn't
970 * loaded, we have to dynamically open it and initialize it.
971 * If you successfully call LockModule() at any moment, be careful to call
972 * UnlockModule() when you don't need it anymore.
973 *****************************************************************************/
974 static int LockModule( module_t * p_module )
976 if( p_module->i_usage >= 0 )
978 /* This module is already loaded and activated, we can return */
983 if( p_module->b_builtin )
985 /* A builtin module should always have a refcount >= 0 ! */
986 msg_Err( p_module, "builtin module \"%s\" has refcount %i",
987 p_module->psz_object_name, p_module->i_usage );
991 #ifdef HAVE_DYNAMIC_PLUGINS
992 if( p_module->i_usage != -1 )
994 /* This shouldn't happen. Ever. We have serious problems here. */
995 msg_Err( p_module, "plugin module \"%s\" has refcount %i",
996 p_module->psz_object_name, p_module->i_usage );
1000 /* i_usage == -1, which means that the module isn't in memory */
1001 if( module_load( p_module->psz_filename, &p_module->handle ) )
1003 char psz_buffer[256];
1005 /* The plugin module couldn't be opened */
1006 msg_Err( p_module, "cannot open `%s' (%s)",
1007 p_module->psz_filename, module_error(psz_buffer) );
1011 /* FIXME: what to do if the guy modified the plugin while it was
1012 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1013 * to be a bit more clever here. */
1015 /* Everything worked fine ! The module is ready to be used */
1016 p_module->i_usage = 1;
1017 #endif /* HAVE_DYNAMIC_PLUGINS */
1022 /*****************************************************************************
1023 * UnlockModule: decrease the usage count of a module.
1024 *****************************************************************************
1025 * We decrease the usage count of a module so that we know when a module
1026 * becomes unused and can be hidden.
1027 *****************************************************************************/
1028 static int UnlockModule( module_t * p_module )
1030 if( p_module->i_usage <= 0 )
1032 /* This shouldn't happen. Ever. We have serious problems here. */
1033 msg_Err( p_module, "trying to call module_Unneed() on \"%s\" "
1034 "which is not in use", p_module->psz_object_name );
1038 /* This module is still in use, we can return */
1039 p_module->i_usage--;
1040 p_module->i_unused_delay = 0;
1045 #ifdef HAVE_DYNAMIC_PLUGINS
1046 /*****************************************************************************
1047 * HideModule: remove a module from memory but keep its structure.
1048 *****************************************************************************
1049 * This function can only be called if i_usage == 0. It will make a call
1050 * to the module's inner module_deactivate() symbol, and then unload it
1051 * from memory. A call to module_Need() will automagically load it again.
1052 *****************************************************************************/
1053 static int HideModule( module_t * p_module )
1055 if( p_module->b_builtin )
1057 /* A builtin module should never be hidden. */
1058 msg_Err( p_module, "trying to hide builtin module \"%s\"",
1059 p_module->psz_object_name );
1063 if( p_module->i_usage >= 1 )
1065 msg_Err( p_module, "trying to hide module \"%s\" which is still "
1066 "in use", p_module->psz_object_name );
1070 if( p_module->i_usage <= -1 )
1072 msg_Err( p_module, "trying to hide module \"%s\" which is already "
1073 "hidden", p_module->psz_object_name );
1077 /* Everything worked fine, we can safely unload the module. */
1078 module_unload( p_module->handle );
1079 p_module->i_usage = -1;
1084 /*****************************************************************************
1085 * CallEntry: call an entry point.
1086 *****************************************************************************
1087 * This function calls a symbol given its name and a module structure. The
1088 * symbol MUST refer to a function returning int and taking a module_t* as
1090 *****************************************************************************/
1091 static int CallEntry( module_t * p_module )
1093 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1094 int (* pf_symbol) ( module_t * p_module );
1096 /* Try to resolve the symbol */
1097 pf_symbol = module_getsymbol( p_module->handle, psz_name );
1099 if( pf_symbol == NULL )
1101 char psz_buffer[256];
1103 /* We couldn't load the symbol */
1104 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1105 psz_name, p_module->psz_filename,
1106 module_error( psz_buffer ) );
1110 /* We can now try to call the symbol */
1111 if( pf_symbol( p_module ) != 0 )
1113 /* With a well-written module we shouldn't have to print an
1114 * additional error message here, but just make sure. */
1115 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1116 psz_name, p_module->psz_filename );
1120 /* Everything worked fine, we can return */
1123 #endif /* HAVE_DYNAMIC_PLUGINS */