1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.54 2002/02/24 20:51:10 gbazin 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() */
36 #include <videolan/vlc.h>
38 #if !defined( _MSC_VER )
42 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
43 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
44 # define HAVE_DYNAMIC_PLUGINS
45 #elif defined(HAVE_IMAGE_H) /* BeOS */
47 # define HAVE_DYNAMIC_PLUGINS
48 #elif defined(WIN32) && defined( __MINGW32__ )
49 # define HAVE_DYNAMIC_PLUGINS
51 # undef HAVE_DYNAMIC_PLUGINS
57 #include "interface.h"
58 #include "intf_playlist.h"
59 #include "intf_eject.h"
61 #include "stream_control.h"
62 #include "input_ext-intf.h"
63 #include "input_ext-dec.h"
64 #include "input_ext-plugins.h"
67 #include "video_output.h"
69 #include "audio_output.h"
73 #ifdef HAVE_DYNAMIC_PLUGINS
74 # include "modules_plugin.h"
76 #include "modules_builtin.h"
78 /*****************************************************************************
80 *****************************************************************************/
81 #ifdef HAVE_DYNAMIC_PLUGINS
82 static void AllocateAllPlugins ( void );
83 static int AllocatePluginModule ( char * );
85 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
86 int ( * ) ( module_t * ),
87 int ( * ) ( module_t * ) );
88 static int DeleteModule ( module_t * );
89 static int LockModule ( module_t * );
90 static int UnlockModule ( module_t * );
91 #ifdef HAVE_DYNAMIC_PLUGINS
92 static int HideModule ( module_t * );
93 static int CallSymbol ( module_t *, char * );
96 #ifdef HAVE_DYNAMIC_PLUGINS
97 static module_symbols_t symbols;
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( void )
108 p_module_bank->first = NULL;
109 p_module_bank->i_count = 0;
110 vlc_mutex_init( &p_module_bank->lock );
113 * Store the symbols to be exported
115 #ifdef HAVE_DYNAMIC_PLUGINS
116 STORE_SYMBOLS( &symbols );
122 /*****************************************************************************
123 * module_LoadMain: load the main program info into the module bank.
124 *****************************************************************************
125 * This function fills the module bank structure with the main module infos.
126 * This is very useful as it will allow us to consider the main program just
127 * as another module, and for instance the configuration options of main will
128 * be available in the module bank structure just as for every other module.
129 *****************************************************************************/
130 void module_LoadMain( void )
132 AllocateBuiltinModule( InitModule__MODULE_main,
133 ActivateModule__MODULE_main,
134 DeactivateModule__MODULE_main );
137 /*****************************************************************************
138 * module_LoadBuiltins: load all modules which we built with.
139 *****************************************************************************
140 * This function fills the module bank structure with the built-in modules.
141 *****************************************************************************/
142 void module_LoadBuiltins( void )
144 intf_WarnMsg( 2, "module: checking built-in modules" );
145 ALLOCATE_ALL_BUILTINS();
148 /*****************************************************************************
149 * module_LoadPlugins: load all plugin modules we can find.
150 *****************************************************************************
151 * This function fills the module bank structure with the plugin modules.
152 *****************************************************************************/
153 void module_LoadPlugins( void )
155 #ifdef HAVE_DYNAMIC_PLUGINS
156 intf_WarnMsg( 2, "module: checking plugin modules" );
157 AllocateAllPlugins();
161 /*****************************************************************************
162 * module_EndBank: empty the module bank.
163 *****************************************************************************
164 * This function unloads all unused plugin modules and empties the module
165 * bank in case of success.
166 *****************************************************************************/
167 void module_EndBank( void )
171 while( p_module_bank->first != NULL )
173 if( DeleteModule( p_module_bank->first ) )
175 /* Module deletion failed */
176 intf_ErrMsg( "module error: `%s' can't be removed, trying harder",
177 p_module_bank->first->psz_name );
179 /* We just free the module by hand. Niahahahahaha. */
180 p_next = p_module_bank->first->next;
181 free(p_module_bank->first);
182 p_module_bank->first = p_next;
186 /* Destroy the lock */
187 vlc_mutex_destroy( &p_module_bank->lock );
192 /*****************************************************************************
193 * module_ResetBank: reset the module bank.
194 *****************************************************************************
195 * This function resets the module bank by unloading all unused plugin
197 *****************************************************************************/
198 void module_ResetBank( void )
200 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
204 /*****************************************************************************
205 * module_ManageBank: manage the module bank.
206 *****************************************************************************
207 * This function parses the module bank and hides modules that have been
208 * unused for a while.
209 *****************************************************************************/
210 void module_ManageBank( void )
212 #ifdef HAVE_DYNAMIC_PLUGINS
215 /* We take the global lock */
216 vlc_mutex_lock( &p_module_bank->lock );
218 /* Parse the module list to see if any modules need to be unloaded */
219 for( p_module = p_module_bank->first ;
221 p_module = p_module->next )
223 /* If the module is unused and if it is a plugin module... */
224 if( p_module->i_usage == 0 && !p_module->b_builtin )
226 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
228 p_module->i_unused_delay++;
232 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
233 p_module->psz_name );
234 HideModule( p_module );
236 /* Break here, so that we only hide one module at a time */
242 /* We release the global lock */
243 vlc_mutex_unlock( &p_module_bank->lock );
244 #endif /* HAVE_DYNAMIC_PLUGINS */
249 /*****************************************************************************
250 * module_Need: return the best module function, given a capability list.
251 *****************************************************************************
252 * This function returns the module that best fits the asked capabilities.
253 *****************************************************************************/
254 module_t * module_Need( int i_capability, char *psz_name, void *p_data )
256 typedef struct module_list_s
258 struct module_s *p_module;
259 struct module_list_s* p_next;
261 struct module_list_s *p_list, *p_first;
263 int i_ret, i_index = 0;
266 char *psz_realname = NULL;
268 /* We take the global lock */
269 vlc_mutex_lock( &p_module_bank->lock );
271 if( psz_name != NULL && *psz_name )
273 /* A module name was requested. */
274 psz_realname = strdup( psz_name );
278 p = strchr( psz_realname, ':' );
283 psz_name = psz_realname;
287 /* Sort the modules and test them */
288 p_list = malloc( p_module_bank->i_count * sizeof( module_list_t ) );
291 /* Parse the module list for capabilities and probe each of them */
292 for( p_module = p_module_bank->first ;
294 p_module = p_module->next )
296 /* Test that this module can do everything we need */
297 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
302 /* Test if we have the required CPU */
303 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
304 != p_module->i_cpu_capabilities )
309 /* Test if this plugin exports the required shortcut */
310 if( psz_name != NULL && *psz_name )
316 !b_ok && p_module->pp_shortcuts[i_dummy];
319 b_ok = !strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
328 /* Test if we requested a particular intf plugin */
330 if( i_capability == MODULE_CAPABILITY_INTF
331 && p_module->psz_program != NULL
332 && strcmp( p_module->psz_program, p_main->psz_arg0 ) )
338 /* Store this new module */
339 p_list[ i_index ].p_module = p_module;
342 LockModule( p_module );
346 p_list[ i_index ].p_next = NULL;
351 /* Ok, so at school you learned that quicksort is quick, and
352 * bubble sort sucks raw eggs. But that's when dealing with
353 * thousands of items. Here we have barely 50. */
354 struct module_list_s *p_newlist = p_first;
356 if( p_first->p_module->pi_score[i_capability]
357 < p_module->pi_score[i_capability] )
359 p_list[ i_index ].p_next = p_first;
360 p_first = &p_list[ i_index ];
364 while( p_newlist->p_next != NULL
365 && p_newlist->p_next->p_module->pi_score[i_capability]
366 >= p_module->pi_score[i_capability] )
368 p_newlist = p_newlist->p_next;
371 p_list[ i_index ].p_next = p_newlist->p_next;
372 p_newlist->p_next = &p_list[ i_index ];
379 /* We can release the global lock, module refcount were incremented */
380 vlc_mutex_unlock( &p_module_bank->lock );
382 /* Parse the linked list and use the first successful module */
383 while( p_first != NULL )
385 /* Test the requested capability */
386 switch( i_capability )
388 case MODULE_CAPABILITY_INPUT:
389 i_ret = p_first->p_module->p_functions->input.functions.
390 input.pf_probe( (input_thread_t *)p_data );
393 case MODULE_CAPABILITY_DECODER:
394 i_ret = p_first->p_module->p_functions->dec.functions.
395 dec.pf_probe( (u8 *)p_data );
398 case MODULE_CAPABILITY_INTF:
399 i_ret = p_first->p_module->p_functions->intf.functions.
400 intf.pf_open( (intf_thread_t *)p_data );
403 case MODULE_CAPABILITY_AOUT:
404 i_ret = p_first->p_module->p_functions->aout.functions.
405 aout.pf_open( (aout_thread_t *)p_data );
408 case MODULE_CAPABILITY_VOUT:
409 i_ret = p_first->p_module->p_functions->vout.functions.
410 vout.pf_create( (vout_thread_t *)p_data );
413 case MODULE_CAPABILITY_CHROMA:
414 i_ret = p_first->p_module->p_functions->chroma.functions.
415 chroma.pf_init( (vout_thread_t *)p_data );
418 case MODULE_CAPABILITY_IDCT:
419 case MODULE_CAPABILITY_IMDCT:
420 case MODULE_CAPABILITY_MOTION:
421 case MODULE_CAPABILITY_DOWNMIX:
422 case MODULE_CAPABILITY_MEMCPY:
423 /* This one always works */
428 intf_ErrMsg( "module error: unknown module type %i",
434 /* If the high score was broken, we have a new champion */
441 UnlockModule( p_first->p_module );
444 p_first = p_first->p_next;
447 /* Store the locked module value */
448 if( p_first != NULL )
450 p_module = p_first->p_module;
451 p_first = p_first->p_next;
458 /* Unlock the remaining modules */
459 while( p_first != NULL )
461 UnlockModule( p_first->p_module );
462 p_first = p_first->p_next;
467 if( p_module != NULL )
469 intf_WarnMsg( 1, "module: locking %s module `%s'",
470 GetCapabilityName( i_capability ),
471 p_module->psz_name );
473 else if( psz_name != NULL && *psz_name )
475 intf_ErrMsg( "module error: requested %s module `%s' unavailable",
476 GetCapabilityName( i_capability ), psz_name );
481 free( psz_realname );
484 /* Don't forget that the module is still locked */
488 /*****************************************************************************
489 * module_Unneed: decrease the usage count of a module.
490 *****************************************************************************
491 * This function must be called by the thread that called module_Need, to
492 * decrease the reference count and allow for hiding of modules.
493 *****************************************************************************/
494 void module_Unneed( module_t * p_module )
496 /* We take the global lock */
497 vlc_mutex_lock( &p_module_bank->lock );
499 /* Just unlock the module - we can't do anything if it fails,
500 * so there is no need to check the return value. */
501 UnlockModule( p_module );
503 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
505 /* We release the global lock */
506 vlc_mutex_unlock( &p_module_bank->lock );
511 /*****************************************************************************
512 * Following functions are local.
513 *****************************************************************************/
515 /*****************************************************************************
516 * AllocateAllPlugins: load all plugin modules we can find.
517 *****************************************************************************/
518 #ifdef HAVE_DYNAMIC_PLUGINS
519 static void AllocateAllPlugins( void )
521 static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
523 char ** ppsz_path = path;
526 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
527 char * psz_vlcpath = system_GetProgramPath();
528 int i_vlclen = strlen( psz_vlcpath );
529 boolean_t b_notinroot;
532 struct dirent * file;
534 for( ; *ppsz_path != NULL ; ppsz_path++ )
536 /* Store strlen(*ppsz_path) for later use. */
537 int i_dirlen = strlen( *ppsz_path );
539 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
541 /* Under BeOS, we need to add beos_GetProgramPath() to access
542 * files under the current directory */
543 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
545 i_dirlen += i_vlclen + 2;
548 psz_fullpath = malloc( i_dirlen );
549 if( psz_fullpath == NULL )
553 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
558 psz_fullpath = *ppsz_path;
561 intf_WarnMsg( 1, "module: browsing `%s'", psz_fullpath );
563 if( (dir = opendir( psz_fullpath )) )
565 /* Parse the directory and try to load all files it contains. */
566 while( (file = readdir( dir )) )
568 int i_filelen = strlen( file->d_name );
570 /* We only load files ending with ".so" */
572 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
574 psz_file = malloc( i_dirlen + i_filelen + 2 );
575 if( psz_file == NULL )
579 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
581 /* We created a nice filename -- now we just try to load
582 * it as a plugin module. */
583 AllocatePluginModule( psz_file );
585 /* We don't care if the allocation succeeded */
590 /* Close the directory if successfully opened */
594 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
597 free( psz_fullpath );
603 /*****************************************************************************
604 * AllocatePluginModule: load a module into memory and initialize it.
605 *****************************************************************************
606 * This function loads a dynamically loadable module and allocates a structure
607 * for its information data. The module can then be handled by module_Need,
608 * module_Unneed and HideModule. It can be removed by DeleteModule.
609 *****************************************************************************/
610 static int AllocatePluginModule( char * psz_filename )
613 module_t * p_module, * p_othermodule;
614 module_handle_t handle;
616 /* Try to dynamically load the module. */
617 if( module_load( psz_filename, &handle ) )
619 /* The plugin module couldn't be opened */
620 intf_WarnMsg( 1, "module warning: cannot open %s (%s)",
621 psz_filename, module_error() );
625 /* Now that we have successfully loaded the module, we can
626 * allocate a structure for it */
627 p_module = malloc( sizeof( module_t ) );
628 if( p_module == NULL )
630 intf_ErrMsg( "module error: can't allocate p_module" );
631 module_unload( handle );
635 /* We need to fill these since they may be needed by CallSymbol() */
636 p_module->is.plugin.psz_filename = psz_filename;
637 p_module->is.plugin.handle = handle;
638 p_module->p_symbols = &symbols;
640 /* Initialize the module : fill p_module->psz_name, default config, etc. */
641 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
643 /* We couldn't call InitModule() */
645 module_unload( handle );
649 /* Check that we don't already have a module with this name */
650 for( p_othermodule = p_module_bank->first ;
651 p_othermodule != NULL ;
652 p_othermodule = p_othermodule->next )
654 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
656 intf_WarnMsg( 5, "module warning: cannot load %s, a module named "
657 "`%s' already exists",
658 psz_filename, p_module->psz_name );
660 module_unload( handle );
665 /* Activate the module : fill the capability structure, etc. */
666 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
668 /* We couldn't call ActivateModule() */
670 module_unload( handle );
674 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
676 *pp_shortcut = strdup( *pp_shortcut );
679 /* We strdup() these entries so that they are still valid when the
680 * module is unloaded. */
681 p_module->is.plugin.psz_filename =
682 strdup( p_module->is.plugin.psz_filename );
683 p_module->psz_name = strdup( p_module->psz_name );
684 p_module->psz_longname = strdup( p_module->psz_longname );
686 if( p_module->is.plugin.psz_filename == NULL
687 || p_module->psz_name == NULL
688 || p_module->psz_longname == NULL )
690 intf_ErrMsg( "module error: can't duplicate strings" );
692 free( p_module->is.plugin.psz_filename );
693 free( p_module->psz_name );
694 free( p_module->psz_longname );
695 free( p_module->psz_program );
698 module_unload( handle );
702 if( p_module->psz_program != NULL )
704 p_module->psz_program = strdup( p_module->psz_program );
707 /* Everything worked fine ! The module is ready to be added to the list. */
708 p_module->i_usage = 0;
709 p_module->i_unused_delay = 0;
711 p_module->b_builtin = 0;
713 /* Link module into the linked list */
714 if( p_module_bank->first != NULL )
716 p_module_bank->first->prev = p_module;
718 p_module->next = p_module_bank->first;
719 p_module->prev = NULL;
720 p_module_bank->first = p_module;
721 p_module_bank->i_count++;
723 /* Immediate message so that a slow module doesn't make the user wait */
724 intf_WarnMsg( 2, "module: new plugin module `%s', %s",
725 p_module->psz_name, p_module->psz_longname );
729 #endif /* HAVE_DYNAMIC_PLUGINS */
731 /*****************************************************************************
732 * AllocateBuiltinModule: initialize a built-in module.
733 *****************************************************************************
734 * This function registers a built-in module and allocates a structure
735 * for its information data. The module can then be handled by module_Need,
736 * module_Unneed and HideModule. It can be removed by DeleteModule.
737 *****************************************************************************/
738 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
739 int ( *pf_activate ) ( module_t * ),
740 int ( *pf_deactivate ) ( module_t * ) )
742 module_t * p_module, * p_othermodule;
744 /* Now that we have successfully loaded the module, we can
745 * allocate a structure for it */
746 p_module = malloc( sizeof( module_t ) );
747 if( p_module == NULL )
749 intf_ErrMsg( "module error: can't allocate p_module" );
753 /* Initialize the module : fill p_module->psz_name, etc. */
754 if( pf_init( p_module ) != 0 )
756 /* With a well-written module we shouldn't have to print an
757 * additional error message here, but just make sure. */
758 intf_ErrMsg( "module error: failed calling init in builtin module" );
763 /* Check that we don't already have a module with this name */
764 for( p_othermodule = p_module_bank->first ;
765 p_othermodule != NULL ;
766 p_othermodule = p_othermodule->next )
768 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
770 intf_WarnMsg( 5, "module warning: cannot load builtin `%s', a "
771 "module named `%s' already exists",
772 p_module->psz_name, p_module->psz_name );
778 if( pf_activate( p_module ) != 0 )
780 /* With a well-written module we shouldn't have to print an
781 * additional error message here, but just make sure. */
782 intf_ErrMsg( "module error: failed calling activate "
783 "in builtin module" );
788 /* Everything worked fine ! The module is ready to be added to the list. */
789 p_module->i_usage = 0;
790 p_module->i_unused_delay = 0;
792 p_module->b_builtin = 1;
793 p_module->is.builtin.pf_deactivate = pf_deactivate;
795 /* Link module into the linked list */
796 if( p_module_bank->first != NULL )
798 p_module_bank->first->prev = p_module;
800 p_module->next = p_module_bank->first;
801 p_module->prev = NULL;
802 p_module_bank->first = p_module;
803 p_module_bank->i_count++;
805 /* Immediate message so that a slow module doesn't make the user wait */
806 intf_WarnMsg( 2, "module: new builtin module `%s', %s",
807 p_module->psz_name, p_module->psz_longname );
812 /*****************************************************************************
813 * DeleteModule: delete a module and its structure.
814 *****************************************************************************
815 * This function can only be called if i_usage <= 0.
816 *****************************************************************************/
817 static int DeleteModule( module_t * p_module )
819 /* If the module is not in use but is still in memory, we first have
820 * to hide it and remove it from memory before we can free the
822 if( p_module->b_builtin )
824 if( p_module->i_usage != 0 )
826 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
827 " usage %i", p_module->psz_name, p_module->i_usage );
832 /* We deactivate the module now. */
833 p_module->is.builtin.pf_deactivate( p_module );
836 #ifdef HAVE_DYNAMIC_PLUGINS
839 if( p_module->i_usage >= 1 )
841 intf_ErrMsg( "module error: trying to free module `%s' which is"
842 " still in use", p_module->psz_name );
846 /* Two possibilities here: i_usage == -1 and the module is already
847 * unloaded, we can continue, or i_usage == 0, and we have to hide
848 * the module before going on. */
849 if( p_module->i_usage == 0 )
851 if( HideModule( p_module ) != 0 )
859 /* Unlink the module from the linked list. */
860 if( p_module->prev != NULL )
862 p_module->prev->next = p_module->next;
866 p_module_bank->first = p_module->next;
869 if( p_module->next != NULL )
871 p_module->next->prev = p_module->prev;
874 p_module_bank->i_count--;
876 /* We free the structures that we strdup()ed in Allocate*Module(). */
877 #ifdef HAVE_DYNAMIC_PLUGINS
878 if( !p_module->b_builtin )
880 char **pp_shortcut = p_module->pp_shortcuts;
882 for( ; *pp_shortcut ; pp_shortcut++ )
884 free( *pp_shortcut );
887 free( p_module->is.plugin.psz_filename );
888 free( p_module->psz_name );
889 free( p_module->psz_longname );
890 free( p_module->psz_program );
899 /*****************************************************************************
900 * LockModule: increase the usage count of a module and load it if needed.
901 *****************************************************************************
902 * This function has to be called before a thread starts using a module. If
903 * the module is already loaded, we just increase its usage count. If it isn't
904 * loaded, we have to dynamically open it and initialize it.
905 * If you successfully call LockModule() at any moment, be careful to call
906 * UnlockModule() when you don't need it anymore.
907 *****************************************************************************/
908 static int LockModule( module_t * p_module )
910 if( p_module->i_usage >= 0 )
912 /* This module is already loaded and activated, we can return */
917 if( p_module->b_builtin )
919 /* A built-in module should always have a refcount >= 0 ! */
920 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
921 p_module->psz_name, p_module->i_usage );
925 #ifdef HAVE_DYNAMIC_PLUGINS
926 if( p_module->i_usage != -1 )
928 /* This shouldn't happen. Ever. We have serious problems here. */
929 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
930 p_module->psz_name, p_module->i_usage );
934 /* i_usage == -1, which means that the module isn't in memory */
935 if( module_load( p_module->is.plugin.psz_filename,
936 &p_module->is.plugin.handle ) )
938 /* The plugin module couldn't be opened */
939 intf_ErrMsg( "module error: cannot open %s (%s)",
940 p_module->is.plugin.psz_filename, module_error() );
944 /* FIXME: what to do if the guy modified the plugin while it was
945 * unloaded ? It makes XMMS crash nastily, perhaps we should try
946 * to be a bit more clever here. */
948 /* Activate the module : fill the capability structure, etc. */
949 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
951 /* We couldn't call ActivateModule() -- looks nasty, but
952 * we can't do much about it. Just try to unload module. */
953 module_unload( p_module->is.plugin.handle );
954 p_module->i_usage = -1;
958 /* Everything worked fine ! The module is ready to be used */
959 p_module->i_usage = 1;
960 #endif /* HAVE_DYNAMIC_PLUGINS */
965 /*****************************************************************************
966 * UnlockModule: decrease the usage count of a module.
967 *****************************************************************************
968 * We decrease the usage count of a module so that we know when a module
969 * becomes unused and can be hidden.
970 *****************************************************************************/
971 static int UnlockModule( module_t * p_module )
973 if( p_module->i_usage <= 0 )
975 /* This shouldn't happen. Ever. We have serious problems here. */
976 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
977 " which isn't even in use", p_module->psz_name );
981 /* This module is still in use, we can return */
983 p_module->i_unused_delay = 0;
988 #ifdef HAVE_DYNAMIC_PLUGINS
989 /*****************************************************************************
990 * HideModule: remove a module from memory but keep its structure.
991 *****************************************************************************
992 * This function can only be called if i_usage == 0. It will make a call
993 * to the module's inner DeactivateModule() symbol, and then unload it
994 * from memory. A call to module_Need() will automagically load it again.
995 *****************************************************************************/
996 static int HideModule( module_t * p_module )
998 if( p_module->b_builtin )
1000 /* A built-in module should never be hidden. */
1001 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
1002 p_module->psz_name );
1006 if( p_module->i_usage >= 1 )
1008 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
1009 " in use", p_module->psz_name );
1013 if( p_module->i_usage <= -1 )
1015 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
1016 " hidden", p_module->psz_name );
1020 /* Deactivate the module : free the capability structure, etc. */
1021 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1023 /* We couldn't call DeactivateModule() -- looks nasty, but
1024 * we can't do much about it. Just try to unload module anyway. */
1025 module_unload( p_module->is.plugin.handle );
1026 p_module->i_usage = -1;
1030 /* Everything worked fine, we can safely unload the module. */
1031 module_unload( p_module->is.plugin.handle );
1032 p_module->i_usage = -1;
1037 /*****************************************************************************
1038 * CallSymbol: calls a module symbol.
1039 *****************************************************************************
1040 * This function calls a symbol given its name and a module structure. The
1041 * symbol MUST refer to a function returning int and taking a module_t* as
1043 *****************************************************************************/
1044 static int CallSymbol( module_t * p_module, char * psz_name )
1046 int (* pf_symbol) ( module_t * p_module );
1048 /* Try to resolve the symbol */
1049 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1051 if( pf_symbol == NULL )
1053 /* We couldn't load the symbol */
1054 intf_WarnMsg( 1, "module warning: "
1055 "cannot find symbol %s in module %s (%s)",
1056 psz_name, p_module->is.plugin.psz_filename,
1061 /* We can now try to call the symbol */
1062 if( pf_symbol( p_module ) != 0 )
1064 /* With a well-written module we shouldn't have to print an
1065 * additional error message here, but just make sure. */
1066 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
1067 psz_name, p_module->is.plugin.psz_filename );
1071 /* Everything worked fine, we can return */
1074 #endif /* HAVE_DYNAMIC_PLUGINS */