1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.80 2002/08/06 00:26:48 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 * );
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 AllocatePluginDir( p_this, psz_fullpath );
635 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
638 free( psz_fullpath );
644 /*****************************************************************************
645 * AllocatePluginDir: recursively parse a directory to look for plugins
646 *****************************************************************************/
647 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir )
649 #define PLUGIN_EXT ".so"
654 struct dirent * file;
656 dir = opendir( psz_dir );
663 i_dirlen = strlen( psz_dir );
665 /* Parse the directory and try to load all files it contains. */
666 while( (file = readdir( dir )) )
669 int i_len = strlen( file->d_name );
671 /* Skip ".", ".." and anything starting with "." */
672 if( !*file->d_name || *file->d_name == '.' )
677 psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
678 sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
680 if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
682 AllocatePluginDir( p_this, psz_file );
684 else if( i_len > strlen( PLUGIN_EXT )
685 /* We only load files ending with ".so" */
686 && !strncmp( file->d_name + i_len - strlen( PLUGIN_EXT ),
687 PLUGIN_EXT, strlen( PLUGIN_EXT ) ) )
689 AllocatePluginFile( p_this, psz_file );
695 /* Close the directory */
699 /*****************************************************************************
700 * AllocatePluginFile: load a module into memory and initialize it.
701 *****************************************************************************
702 * This function loads a dynamically loadable module and allocates a structure
703 * for its information data. The module can then be handled by module_Need,
704 * module_Unneed and HideModule. It can be removed by DeleteModule.
705 *****************************************************************************/
706 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
709 module_handle_t handle;
711 /* Try to dynamically load the module. */
712 if( module_load( psz_file, &handle ) )
714 char psz_buffer[256];
716 /* The plugin module couldn't be opened */
717 msg_Warn( p_this, "cannot open `%s' (%s)",
718 psz_file, module_error( psz_buffer ) );
722 /* Now that we have successfully loaded the module, we can
723 * allocate a structure for it */
724 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
725 if( p_module == NULL )
727 msg_Err( p_this, "out of memory" );
728 module_unload( handle );
732 /* We need to fill these since they may be needed by CallEntry() */
733 p_module->psz_filename = psz_file;
734 p_module->handle = handle;
735 p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
737 /* Initialize the module: fill p_module->psz_object_name, default config */
738 if( CallEntry( p_module ) != 0 )
740 /* We couldn't call module_init() */
741 vlc_object_destroy( p_module );
742 module_unload( handle );
746 DupModule( p_module );
747 p_module->psz_filename = strdup( p_module->psz_filename );
748 p_module->psz_longname = strdup( p_module->psz_longname );
750 /* Everything worked fine ! The module is ready to be added to the list. */
751 p_module->i_usage = 0;
752 p_module->i_unused_delay = 0;
754 p_module->b_builtin = VLC_FALSE;
756 /* Link module into the linked list */
757 if( p_this->p_vlc->p_module_bank->first != NULL )
759 p_this->p_vlc->p_module_bank->first->prev = p_module;
761 p_module->next = p_this->p_vlc->p_module_bank->first;
762 p_module->prev = NULL;
763 p_this->p_vlc->p_module_bank->first = p_module;
764 p_this->p_vlc->p_module_bank->i_count++;
766 msg_Dbg( p_this, "plugin \"%s\", %s",
767 p_module->psz_object_name, p_module->psz_longname );
769 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
774 /*****************************************************************************
775 * DupModule: make a plugin module standalone.
776 *****************************************************************************
777 * This function duplicates all strings in the module, so that the dynamic
778 * object can be unloaded. It acts recursively on submodules.
779 *****************************************************************************/
780 static void DupModule( module_t *p_module )
785 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
787 *pp_shortcut = strdup( *pp_shortcut );
790 /* We strdup() these entries so that they are still valid when the
791 * module is unloaded. */
792 p_module->psz_object_name = strdup( p_module->psz_object_name );
793 p_module->psz_capability = strdup( p_module->psz_capability );
795 if( p_module->psz_program != NULL )
797 p_module->psz_program = strdup( p_module->psz_program );
800 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
802 DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
806 /*****************************************************************************
807 * UndupModule: free a duplicated module.
808 *****************************************************************************
809 * This function frees the allocations done in DupModule().
810 *****************************************************************************/
811 static void UndupModule( module_t *p_module )
816 for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
818 UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
821 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
823 free( *pp_shortcut );
826 free( p_module->psz_object_name );
827 free( p_module->psz_capability );
829 if( p_module->psz_program != NULL )
831 free( p_module->psz_program );
835 #endif /* HAVE_DYNAMIC_PLUGINS */
837 /*****************************************************************************
838 * AllocateBuiltinModule: initialize a builtin module.
839 *****************************************************************************
840 * This function registers a builtin module and allocates a structure
841 * for its information data. The module can then be handled by module_Need,
842 * module_Unneed and HideModule. It can be removed by DeleteModule.
843 *****************************************************************************/
844 static int AllocateBuiltinModule( vlc_object_t * p_this,
845 int ( *pf_entry ) ( module_t * ) )
849 /* Now that we have successfully loaded the module, we can
850 * allocate a structure for it */
851 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
852 if( p_module == NULL )
854 msg_Err( p_this, "out of memory" );
858 /* Initialize the module : fill p_module->psz_object_name, etc. */
859 if( pf_entry( p_module ) != 0 )
861 /* With a well-written module we shouldn't have to print an
862 * additional error message here, but just make sure. */
863 msg_Err( p_this, "failed calling entry point in builtin module" );
864 vlc_object_destroy( p_module );
868 /* Everything worked fine ! The module is ready to be added to the list. */
869 p_module->i_usage = 0;
870 p_module->i_unused_delay = 0;
872 p_module->b_builtin = VLC_TRUE;
874 /* Link module into the linked list */
875 if( p_this->p_vlc->p_module_bank->first != NULL )
877 p_this->p_vlc->p_module_bank->first->prev = p_module;
879 p_module->next = p_this->p_vlc->p_module_bank->first;
880 p_module->prev = NULL;
881 p_this->p_vlc->p_module_bank->first = p_module;
882 p_this->p_vlc->p_module_bank->i_count++;
884 msg_Dbg( p_this, "builtin \"%s\", %s",
885 p_module->psz_object_name, p_module->psz_longname );
887 vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
892 /*****************************************************************************
893 * DeleteModule: delete a module and its structure.
894 *****************************************************************************
895 * This function can only be called if i_usage <= 0.
896 *****************************************************************************/
897 static int DeleteModule( module_t * p_module )
899 /* If the module is not in use but is still in memory, we first have
900 * to hide it and remove it from memory before we can free the
902 if( p_module->b_builtin )
904 if( p_module->i_usage != 0 )
906 msg_Err( p_module, "trying to free builtin module \"%s\" with "
907 "usage %i", p_module->psz_object_name, p_module->i_usage );
911 #ifdef HAVE_DYNAMIC_PLUGINS
914 if( p_module->i_usage >= 1 )
916 msg_Err( p_module, "trying to free module \"%s\" which is "
917 "still in use", p_module->psz_object_name );
921 /* Two possibilities here: i_usage == -1 and the module is already
922 * unloaded, we can continue, or i_usage == 0, and we have to hide
923 * the module before going on. */
924 if( p_module->i_usage == 0 )
926 if( HideModule( p_module ) != 0 )
934 vlc_object_detach_all( p_module );
936 /* Unlink the module from the linked list. */
937 if( p_module->prev != NULL )
939 p_module->prev->next = p_module->next;
943 p_module->p_vlc->p_module_bank->first = p_module->next;
946 if( p_module->next != NULL )
948 p_module->next->prev = p_module->prev;
951 p_module->p_vlc->p_module_bank->i_count--;
953 /* We free the structures that we strdup()ed in Allocate*Module(). */
954 #ifdef HAVE_DYNAMIC_PLUGINS
955 if( !p_module->b_builtin )
957 UndupModule( p_module );
958 free( p_module->psz_filename );
959 free( p_module->psz_longname );
963 /* Free and detach the object's children */
964 while( p_module->i_children )
966 vlc_object_t *p_this = p_module->pp_children[0];
967 vlc_object_detach_all( p_this );
968 vlc_object_destroy( p_this );
971 config_Free( p_module );
972 vlc_object_destroy( p_module );
977 /*****************************************************************************
978 * LockModule: increase the usage count of a module and load it if needed.
979 *****************************************************************************
980 * This function has to be called before a thread starts using a module. If
981 * the module is already loaded, we just increase its usage count. If it isn't
982 * loaded, we have to dynamically open it and initialize it.
983 * If you successfully call LockModule() at any moment, be careful to call
984 * UnlockModule() when you don't need it anymore.
985 *****************************************************************************/
986 static int LockModule( module_t * p_module )
988 if( p_module->i_usage >= 0 )
990 /* This module is already loaded and activated, we can return */
995 if( p_module->b_builtin )
997 /* A builtin module should always have a refcount >= 0 ! */
998 msg_Err( p_module, "builtin module \"%s\" has refcount %i",
999 p_module->psz_object_name, p_module->i_usage );
1003 #ifdef HAVE_DYNAMIC_PLUGINS
1004 if( p_module->i_usage != -1 )
1006 /* This shouldn't happen. Ever. We have serious problems here. */
1007 msg_Err( p_module, "plugin module \"%s\" has refcount %i",
1008 p_module->psz_object_name, p_module->i_usage );
1012 /* i_usage == -1, which means that the module isn't in memory */
1013 if( module_load( p_module->psz_filename, &p_module->handle ) )
1015 char psz_buffer[256];
1017 /* The plugin module couldn't be opened */
1018 msg_Err( p_module, "cannot open `%s' (%s)",
1019 p_module->psz_filename, module_error(psz_buffer) );
1023 /* FIXME: what to do if the guy modified the plugin while it was
1024 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1025 * to be a bit more clever here. */
1027 /* Everything worked fine ! The module is ready to be used */
1028 p_module->i_usage = 1;
1029 #endif /* HAVE_DYNAMIC_PLUGINS */
1034 /*****************************************************************************
1035 * UnlockModule: decrease the usage count of a module.
1036 *****************************************************************************
1037 * We decrease the usage count of a module so that we know when a module
1038 * becomes unused and can be hidden.
1039 *****************************************************************************/
1040 static int UnlockModule( module_t * p_module )
1042 if( p_module->i_usage <= 0 )
1044 /* This shouldn't happen. Ever. We have serious problems here. */
1045 msg_Err( p_module, "trying to call module_Unneed() on \"%s\" "
1046 "which is not in use", p_module->psz_object_name );
1050 /* This module is still in use, we can return */
1051 p_module->i_usage--;
1052 p_module->i_unused_delay = 0;
1057 #ifdef HAVE_DYNAMIC_PLUGINS
1058 /*****************************************************************************
1059 * HideModule: remove a module from memory but keep its structure.
1060 *****************************************************************************
1061 * This function can only be called if i_usage == 0. It will make a call
1062 * to the module's inner module_deactivate() symbol, and then unload it
1063 * from memory. A call to module_Need() will automagically load it again.
1064 *****************************************************************************/
1065 static int HideModule( module_t * p_module )
1067 if( p_module->b_builtin )
1069 /* A builtin module should never be hidden. */
1070 msg_Err( p_module, "trying to hide builtin module \"%s\"",
1071 p_module->psz_object_name );
1075 if( p_module->i_usage >= 1 )
1077 msg_Err( p_module, "trying to hide module \"%s\" which is still "
1078 "in use", p_module->psz_object_name );
1082 if( p_module->i_usage <= -1 )
1084 msg_Err( p_module, "trying to hide module \"%s\" which is already "
1085 "hidden", p_module->psz_object_name );
1089 /* Everything worked fine, we can safely unload the module. */
1090 module_unload( p_module->handle );
1091 p_module->i_usage = -1;
1096 /*****************************************************************************
1097 * CallEntry: call an entry point.
1098 *****************************************************************************
1099 * This function calls a symbol given its name and a module structure. The
1100 * symbol MUST refer to a function returning int and taking a module_t* as
1102 *****************************************************************************/
1103 static int CallEntry( module_t * p_module )
1105 static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1106 int (* pf_symbol) ( module_t * p_module );
1108 /* Try to resolve the symbol */
1109 pf_symbol = module_getsymbol( p_module->handle, psz_name );
1111 if( pf_symbol == NULL )
1113 char psz_buffer[256];
1115 /* We couldn't load the symbol */
1116 msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1117 psz_name, p_module->psz_filename,
1118 module_error( psz_buffer ) );
1122 /* We can now try to call the symbol */
1123 if( pf_symbol( p_module ) != 0 )
1125 /* With a well-written module we shouldn't have to print an
1126 * additional error message here, but just make sure. */
1127 msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1128 psz_name, p_module->psz_filename );
1132 /* Everything worked fine, we can return */
1135 #endif /* HAVE_DYNAMIC_PLUGINS */