1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.49 2002/01/21 00:52:07 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
26 * is set to 64. Don't try to be cleverer. */
27 #ifdef _FILE_OFFSET_BITS
28 #undef _FILE_OFFSET_BITS
31 #include <stdlib.h> /* free(), strtol() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strdup() */
35 #include <videolan/vlc.h>
37 #if !defined( _MSC_VER )
41 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
42 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
43 # define HAVE_DYNAMIC_PLUGINS
44 #elif defined(HAVE_IMAGE_H) /* BeOS */
46 # define HAVE_DYNAMIC_PLUGINS
47 #elif defined(WIN32) && defined( __MINGW32__ )
48 # define HAVE_DYNAMIC_PLUGINS
50 # undef HAVE_DYNAMIC_PLUGINS
55 #include "interface.h"
56 #include "intf_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"
74 #include "modules_builtin.h"
76 /*****************************************************************************
78 *****************************************************************************/
79 #ifdef HAVE_DYNAMIC_PLUGINS
80 static void AllocateAllPlugins ( void );
81 static int AllocatePluginModule ( char * );
83 static void AllocateAllBuiltins ( void );
84 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
85 int ( * ) ( module_t * ),
86 int ( * ) ( module_t * ) );
87 static int DeleteModule ( module_t * );
88 static int LockModule ( module_t * );
89 static int UnlockModule ( module_t * );
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 static int HideModule ( module_t * );
92 static int CallSymbol ( module_t *, char * );
95 #ifdef HAVE_DYNAMIC_PLUGINS
96 static module_symbols_t symbols;
99 /*****************************************************************************
100 * module_InitBank: create the module bank.
101 *****************************************************************************
102 * This function creates a module bank structure and fills it with the
103 * built-in modules, as well as all the plugin modules it can find.
104 *****************************************************************************/
105 void module_InitBank( void )
107 p_module_bank->first = NULL;
108 p_module_bank->i_count = 0;
109 vlc_mutex_init( &p_module_bank->lock );
112 * Store the symbols to be exported
114 #ifdef HAVE_DYNAMIC_PLUGINS
115 STORE_SYMBOLS( &symbols );
119 * Check all the built-in modules
121 intf_WarnMsg( 2, "module: checking built-in modules" );
122 AllocateAllBuiltins();
125 * Check all the plugin modules we can find
127 #ifdef HAVE_DYNAMIC_PLUGINS
128 intf_WarnMsg( 2, "module: checking plugin modules" );
129 AllocateAllPlugins();
132 intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
133 p_module_bank->i_count );
138 /*****************************************************************************
139 * module_EndBank: empty the module bank.
140 *****************************************************************************
141 * This function unloads all unused plugin modules and empties the module
142 * bank in case of success.
143 *****************************************************************************/
144 void module_EndBank( void )
148 while( p_module_bank->first != NULL )
150 if( DeleteModule( p_module_bank->first ) )
152 /* Module deletion failed */
153 intf_ErrMsg( "module error: `%s' can't be removed, trying harder",
154 p_module_bank->first->psz_name );
156 /* We just free the module by hand. Niahahahahaha. */
157 p_next = p_module_bank->first->next;
158 free(p_module_bank->first);
159 p_module_bank->first = p_next;
163 /* Destroy the lock */
164 vlc_mutex_destroy( &p_module_bank->lock );
169 /*****************************************************************************
170 * module_ResetBank: reset the module bank.
171 *****************************************************************************
172 * This function resets the module bank by unloading all unused plugin
174 *****************************************************************************/
175 void module_ResetBank( void )
177 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
181 /*****************************************************************************
182 * module_ManageBank: manage the module bank.
183 *****************************************************************************
184 * This function parses the module bank and hides modules that have been
185 * unused for a while.
186 *****************************************************************************/
187 void module_ManageBank( void )
189 #ifdef HAVE_DYNAMIC_PLUGINS
192 /* We take the global lock */
193 vlc_mutex_lock( &p_module_bank->lock );
195 /* Parse the module list to see if any modules need to be unloaded */
196 for( p_module = p_module_bank->first ;
198 p_module = p_module->next )
200 /* If the module is unused and if it is a plugin module... */
201 if( p_module->i_usage == 0 && !p_module->b_builtin )
203 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
205 p_module->i_unused_delay++;
209 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
210 p_module->psz_name );
211 HideModule( p_module );
213 /* Break here, so that we only hide one module at a time */
219 /* We release the global lock */
220 vlc_mutex_unlock( &p_module_bank->lock );
221 #endif /* HAVE_DYNAMIC_PLUGINS */
226 int module_NeedMemcpy( memcpy_module_t *p_memcpy )
228 p_memcpy->p_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL, NULL );
230 if( p_memcpy->p_module == NULL )
235 p_memcpy->pf_memcpy = p_memcpy->p_module->p_functions->memcpy.functions.memcpy.fast_memcpy;
240 void module_UnneedMemcpy( memcpy_module_t *p_memcpy )
242 module_Unneed( p_memcpy->p_module );
246 int module_NeedIntf( intf_module_t *p_intf )
248 p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
250 if( p_intf->p_module == NULL )
255 p_intf->pf_open = p_intf->p_module->p_functions->intf.functions.intf.pf_open;
256 p_intf->pf_run = p_intf->p_module->p_functions->intf.functions.intf.pf_run;
257 p_intf->pf_close = p_intf->p_module->p_functions->intf.functions.intf.pf_close;
263 /*****************************************************************************
264 * module_Need: return the best module function, given a capability list.
265 *****************************************************************************
266 * This function returns the module that best fits the asked capabilities.
267 *****************************************************************************/
268 module_t * module_Need( int i_capability, char *psz_name, probedata_t *p_data )
272 /* We take the global lock */
273 vlc_mutex_lock( &p_module_bank->lock );
275 if( psz_name != NULL && *psz_name )
277 #define MAX_PLUGIN_NAME 128
278 /* A module name was requested. Use the first matching one. */
279 char psz_realname[ MAX_PLUGIN_NAME + 1 ];
284 i_index < MAX_PLUGIN_NAME
285 && psz_name[ i_index ]
286 && psz_name[ i_index ] != ':';
289 psz_realname[ i_index ] = psz_name[ i_index ];
292 psz_realname[ i_index ] = '\0';
294 for( p_module = p_module_bank->first;
296 p_module = p_module->next )
298 /* Test that this module can do everything we need */
299 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
304 /* Test if we have the required CPU */
305 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
306 != p_module->i_cpu_capabilities )
311 /* Test if this plugin exports the required shortcut */
313 !b_ok && p_module->pp_shortcuts[i_index];
316 b_ok = !strcmp( psz_realname,
317 p_module->pp_shortcuts[i_index] );
329 LockModule( p_module );
333 intf_ErrMsg( "module error: requested %s module `%s' not found",
334 GetCapabilityName( i_capability ), psz_realname );
339 /* No module name was requested. Sort the modules and test them */
340 typedef struct module_list_s
342 struct module_s *p_module;
343 struct module_list_s* p_next;
348 struct module_list_s *p_list = malloc( p_module_bank->i_count
349 * sizeof( module_list_t ) );
350 struct module_list_s *p_first = NULL;
352 /* Parse the module list for capabilities and probe each of them */
353 for( p_module = p_module_bank->first ;
355 p_module = p_module->next )
357 /* Test that this module can do everything we need */
358 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
363 /* Test if we have the required CPU */
364 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
365 != p_module->i_cpu_capabilities )
370 /* Test if we requested a particular intf plugin */
372 if( i_capability == MODULE_CAPABILITY_INTF
373 && p_module->psz_program != NULL
374 && strcmp( p_module->psz_program, p_main->psz_arg0 ) )
380 /* Store this new module */
381 p_list[ i_index ].p_module = p_module;
385 p_list[ i_index ].p_next = NULL;
390 /* Ok, so at school you learned that quicksort is quick, and
391 * bubble sort sucks raw eggs. But that's when dealing with
392 * thousands of items. Here we have barely 50. */
393 struct module_list_s *p_newlist = p_first;
395 if( p_first->p_module->pi_score[i_capability]
396 < p_module->pi_score[i_capability] )
398 p_list[ i_index ].p_next = p_first;
399 p_first = &p_list[ i_index ];
403 while( p_newlist->p_next != NULL
405 ->p_module->pi_score[i_capability]
406 >= p_module->pi_score[i_capability] )
408 p_newlist = p_newlist->p_next;
411 p_list[ i_index ].p_next = p_newlist->p_next;
412 p_newlist->p_next = &p_list[ i_index ];
419 /* Parse the linked list and use the first successful module */
420 while( p_first != NULL )
422 LockModule( p_first->p_module );
424 /* Test the requested capability */
425 i_score += ((function_list_t *)p_first->p_module->p_functions)
426 [i_capability].pf_probe( p_data );
428 /* If the high score was broken, we have a new champion */
434 UnlockModule( p_first->p_module );
436 p_first = p_first->p_next;
439 p_module = (p_first == NULL) ? NULL : p_first->p_module;
443 /* We can release the global lock, module refcount was incremented */
444 vlc_mutex_unlock( &p_module_bank->lock );
446 if( p_module != NULL )
448 intf_WarnMsg( 1, "module: locking %s module `%s'",
449 GetCapabilityName( i_capability ),
450 p_module->psz_name );
453 /* Don't forget that the module is still locked if bestmodule != NULL */
457 /*****************************************************************************
458 * module_Unneed: decrease the usage count of a module.
459 *****************************************************************************
460 * This function must be called by the thread that called module_Need, to
461 * decrease the reference count and allow for hiding of modules.
462 *****************************************************************************/
463 void module_Unneed( module_t * p_module )
465 /* We take the global lock */
466 vlc_mutex_lock( &p_module_bank->lock );
468 /* Just unlock the module - we can't do anything if it fails,
469 * so there is no need to check the return value. */
470 UnlockModule( p_module );
472 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
474 /* We release the global lock */
475 vlc_mutex_unlock( &p_module_bank->lock );
480 /*****************************************************************************
481 * Following functions are local.
482 *****************************************************************************/
484 /*****************************************************************************
485 * AllocateAllPlugins: load all plugin modules we can find.
486 *****************************************************************************/
487 #ifdef HAVE_DYNAMIC_PLUGINS
488 static void AllocateAllPlugins( void )
490 static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
492 char ** ppsz_path = path;
495 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
496 char * psz_vlcpath = system_GetProgramPath();
497 int i_vlclen = strlen( psz_vlcpath );
498 boolean_t b_notinroot;
501 struct dirent * file;
503 for( ; *ppsz_path != NULL ; ppsz_path++ )
505 /* Store strlen(*ppsz_path) for later use. */
506 int i_dirlen = strlen( *ppsz_path );
508 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
510 /* Under BeOS, we need to add beos_GetProgramPath() to access
511 * files under the current directory */
512 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
514 i_dirlen += i_vlclen + 2;
517 psz_fullpath = malloc( i_dirlen );
518 if( psz_fullpath == NULL )
522 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
527 psz_fullpath = *ppsz_path;
530 intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
532 if( (dir = opendir( psz_fullpath )) )
534 /* Parse the directory and try to load all files it contains. */
535 while( (file = readdir( dir )) )
537 int i_filelen = strlen( file->d_name );
539 /* We only load files ending with ".so" */
541 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
543 psz_file = malloc( i_dirlen + i_filelen + 2 );
544 if( psz_file == NULL )
548 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
550 /* We created a nice filename -- now we just try to load
551 * it as a plugin module. */
552 AllocatePluginModule( psz_file );
554 /* We don't care if the allocation succeeded */
559 /* Close the directory if successfully opened */
563 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
566 free( psz_fullpath );
572 /*****************************************************************************
573 * AllocatePluginModule: load a module into memory and initialize it.
574 *****************************************************************************
575 * This function loads a dynamically loadable module and allocates a structure
576 * for its information data. The module can then be handled by module_Need,
577 * module_Unneed and HideModule. It can be removed by DeleteModule.
578 *****************************************************************************/
579 static int AllocatePluginModule( char * psz_filename )
582 module_t * p_module, * p_othermodule;
583 module_handle_t handle;
585 /* Try to dynamically load the module. */
586 if( module_load( psz_filename, &handle ) )
588 /* The plugin module couldn't be opened */
589 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
590 psz_filename, module_error() );
594 /* Now that we have successfully loaded the module, we can
595 * allocate a structure for it */
596 p_module = malloc( sizeof( module_t ) );
597 if( p_module == NULL )
599 intf_ErrMsg( "module error: can't allocate p_module" );
600 module_unload( handle );
604 /* We need to fill these since they may be needed by CallSymbol() */
605 p_module->is.plugin.psz_filename = psz_filename;
606 p_module->is.plugin.handle = handle;
607 p_module->p_symbols = &symbols;
609 /* Initialize the module : fill p_module->psz_name, etc. */
610 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
612 /* We couldn't call InitModule() */
614 module_unload( handle );
618 /* Check that we don't already have a module with this name */
619 for( p_othermodule = p_module_bank->first ;
620 p_othermodule != NULL ;
621 p_othermodule = p_othermodule->next )
623 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
625 intf_WarnMsg( 5, "module warning: cannot load %s, a module named "
626 "`%s' already exists",
627 psz_filename, p_module->psz_name );
629 module_unload( handle );
634 /* Activate the module : fill the capability structure, etc. */
635 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
637 /* We couldn't call ActivateModule() */
639 module_unload( handle );
643 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
645 *pp_shortcut = strdup( *pp_shortcut );
648 /* We strdup() these entries so that they are still valid when the
649 * module is unloaded. */
650 p_module->is.plugin.psz_filename =
651 strdup( p_module->is.plugin.psz_filename );
652 p_module->psz_name = strdup( p_module->psz_name );
653 p_module->psz_longname = strdup( p_module->psz_longname );
655 if( p_module->is.plugin.psz_filename == NULL
656 || p_module->psz_name == NULL
657 || p_module->psz_longname == NULL )
659 intf_ErrMsg( "module error: can't duplicate strings" );
661 free( p_module->is.plugin.psz_filename );
662 free( p_module->psz_name );
663 free( p_module->psz_longname );
664 free( p_module->psz_program );
667 module_unload( handle );
671 if( p_module->psz_program != NULL )
673 p_module->psz_program = strdup( p_module->psz_program );
676 /* Everything worked fine ! The module is ready to be added to the list. */
677 p_module->i_usage = 0;
678 p_module->i_unused_delay = 0;
680 p_module->b_builtin = 0;
682 /* Link module into the linked list */
683 if( p_module_bank->first != NULL )
685 p_module_bank->first->prev = p_module;
687 p_module->next = p_module_bank->first;
688 p_module->prev = NULL;
689 p_module_bank->first = p_module;
690 p_module_bank->i_count++;
692 /* Immediate message so that a slow module doesn't make the user wait */
693 intf_WarnMsgImm( 2, "module: new plugin module `%s', %s",
694 p_module->psz_name, p_module->psz_longname );
698 #endif /* HAVE_DYNAMIC_PLUGINS */
700 /*****************************************************************************
701 * AllocateAllBuiltins: load all modules we were built with.
702 *****************************************************************************/
703 static void AllocateAllBuiltins( void )
705 ALLOCATE_ALL_BUILTINS();
708 /*****************************************************************************
709 * AllocateBuiltinModule: initialize a built-in module.
710 *****************************************************************************
711 * This function registers a built-in module and allocates a structure
712 * for its information data. The module can then be handled by module_Need,
713 * module_Unneed and HideModule. It can be removed by DeleteModule.
714 *****************************************************************************/
715 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
716 int ( *pf_activate ) ( module_t * ),
717 int ( *pf_deactivate ) ( module_t * ) )
719 module_t * p_module, * p_othermodule;
721 /* Now that we have successfully loaded the module, we can
722 * allocate a structure for it */
723 p_module = malloc( sizeof( module_t ) );
724 if( p_module == NULL )
726 intf_ErrMsg( "module error: can't allocate p_module" );
730 /* Initialize the module : fill p_module->psz_name, etc. */
731 if( pf_init( p_module ) != 0 )
733 /* With a well-written module we shouldn't have to print an
734 * additional error message here, but just make sure. */
735 intf_ErrMsg( "module error: failed calling init in builtin module" );
740 /* Check that we don't already have a module with this name */
741 for( p_othermodule = p_module_bank->first ;
742 p_othermodule != NULL ;
743 p_othermodule = p_othermodule->next )
745 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
747 intf_WarnMsg( 5, "module warning: cannot load builtin `%s', a "
748 "module named `%s' already exists",
749 p_module->psz_name, p_module->psz_name );
755 if( pf_activate( p_module ) != 0 )
757 /* With a well-written module we shouldn't have to print an
758 * additional error message here, but just make sure. */
759 intf_ErrMsg( "module error: failed calling activate "
760 "in builtin module" );
765 /* Everything worked fine ! The module is ready to be added to the list. */
766 p_module->i_usage = 0;
767 p_module->i_unused_delay = 0;
769 p_module->b_builtin = 1;
770 p_module->is.builtin.pf_deactivate = pf_deactivate;
772 /* Link module into the linked list */
773 if( p_module_bank->first != NULL )
775 p_module_bank->first->prev = p_module;
777 p_module->next = p_module_bank->first;
778 p_module->prev = NULL;
779 p_module_bank->first = p_module;
780 p_module_bank->i_count++;
782 /* Immediate message so that a slow module doesn't make the user wait */
783 intf_WarnMsgImm( 2, "module: new builtin module `%s', %s",
784 p_module->psz_name, p_module->psz_longname );
789 /*****************************************************************************
790 * DeleteModule: delete a module and its structure.
791 *****************************************************************************
792 * This function can only be called if i_usage <= 0.
793 *****************************************************************************/
794 static int DeleteModule( module_t * p_module )
796 /* If the module is not in use but is still in memory, we first have
797 * to hide it and remove it from memory before we can free the
799 if( p_module->b_builtin )
801 if( p_module->i_usage != 0 )
803 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
804 " usage %i", p_module->psz_name, p_module->i_usage );
809 /* We deactivate the module now. */
810 p_module->is.builtin.pf_deactivate( p_module );
813 #ifdef HAVE_DYNAMIC_PLUGINS
816 if( p_module->i_usage >= 1 )
818 intf_ErrMsg( "module error: trying to free module `%s' which is"
819 " still in use", p_module->psz_name );
823 /* Two possibilities here: i_usage == -1 and the module is already
824 * unloaded, we can continue, or i_usage == 0, and we have to hide
825 * the module before going on. */
826 if( p_module->i_usage == 0 )
828 if( HideModule( p_module ) != 0 )
836 /* Unlink the module from the linked list. */
837 if( p_module->prev != NULL )
839 p_module->prev->next = p_module->next;
843 p_module_bank->first = p_module->next;
846 if( p_module->next != NULL )
848 p_module->next->prev = p_module->prev;
851 p_module_bank->i_count--;
853 /* We free the structures that we strdup()ed in Allocate*Module(). */
854 #ifdef HAVE_DYNAMIC_PLUGINS
855 if( !p_module->b_builtin )
857 char **pp_shortcut = p_module->pp_shortcuts;
859 for( ; *pp_shortcut ; pp_shortcut++ )
861 free( *pp_shortcut );
864 free( p_module->is.plugin.psz_filename );
865 free( p_module->psz_name );
866 free( p_module->psz_longname );
867 free( p_module->psz_program );
876 /*****************************************************************************
877 * LockModule: increase the usage count of a module and load it if needed.
878 *****************************************************************************
879 * This function has to be called before a thread starts using a module. If
880 * the module is already loaded, we just increase its usage count. If it isn't
881 * loaded, we have to dynamically open it and initialize it.
882 * If you successfully call LockModule() at any moment, be careful to call
883 * UnlockModule() when you don't need it anymore.
884 *****************************************************************************/
885 static int LockModule( module_t * p_module )
887 if( p_module->i_usage >= 0 )
889 /* This module is already loaded and activated, we can return */
894 if( p_module->b_builtin )
896 /* A built-in module should always have a refcount >= 0 ! */
897 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
898 p_module->psz_name, p_module->i_usage );
902 #ifdef HAVE_DYNAMIC_PLUGINS
903 if( p_module->i_usage != -1 )
905 /* This shouldn't happen. Ever. We have serious problems here. */
906 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
907 p_module->psz_name, p_module->i_usage );
911 /* i_usage == -1, which means that the module isn't in memory */
912 if( module_load( p_module->is.plugin.psz_filename,
913 &p_module->is.plugin.handle ) )
915 /* The plugin module couldn't be opened */
916 intf_ErrMsg( "module error: cannot open %s (%s)",
917 p_module->is.plugin.psz_filename, module_error() );
921 /* FIXME: what to do if the guy modified the plugin while it was
922 * unloaded ? It makes XMMS crash nastily, perhaps we should try
923 * to be a bit more clever here. */
925 /* Activate the module : fill the capability structure, etc. */
926 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
928 /* We couldn't call ActivateModule() -- looks nasty, but
929 * we can't do much about it. Just try to unload module. */
930 module_unload( p_module->is.plugin.handle );
931 p_module->i_usage = -1;
935 /* Everything worked fine ! The module is ready to be used */
936 p_module->i_usage = 1;
937 #endif /* HAVE_DYNAMIC_PLUGINS */
942 /*****************************************************************************
943 * UnlockModule: decrease the usage count of a module.
944 *****************************************************************************
945 * We decrease the usage count of a module so that we know when a module
946 * becomes unused and can be hidden.
947 *****************************************************************************/
948 static int UnlockModule( module_t * p_module )
950 if( p_module->i_usage <= 0 )
952 /* This shouldn't happen. Ever. We have serious problems here. */
953 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
954 " which isn't even in use", p_module->psz_name );
958 /* This module is still in use, we can return */
960 p_module->i_unused_delay = 0;
965 #ifdef HAVE_DYNAMIC_PLUGINS
966 /*****************************************************************************
967 * HideModule: remove a module from memory but keep its structure.
968 *****************************************************************************
969 * This function can only be called if i_usage == 0. It will make a call
970 * to the module's inner DeactivateModule() symbol, and then unload it
971 * from memory. A call to module_Need() will automagically load it again.
972 *****************************************************************************/
973 static int HideModule( module_t * p_module )
975 if( p_module->b_builtin )
977 /* A built-in module should never be hidden. */
978 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
979 p_module->psz_name );
983 if( p_module->i_usage >= 1 )
985 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
986 " in use", p_module->psz_name );
990 if( p_module->i_usage <= -1 )
992 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
993 " hidden", p_module->psz_name );
997 /* Deactivate the module : free the capability structure, etc. */
998 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1000 /* We couldn't call DeactivateModule() -- looks nasty, but
1001 * we can't do much about it. Just try to unload module anyway. */
1002 module_unload( p_module->is.plugin.handle );
1003 p_module->i_usage = -1;
1007 /* Everything worked fine, we can safely unload the module. */
1008 module_unload( p_module->is.plugin.handle );
1009 p_module->i_usage = -1;
1014 /*****************************************************************************
1015 * CallSymbol: calls a module symbol.
1016 *****************************************************************************
1017 * This function calls a symbol given its name and a module structure. The
1018 * symbol MUST refer to a function returning int and taking a module_t* as
1020 *****************************************************************************/
1021 static int CallSymbol( module_t * p_module, char * psz_name )
1023 int (* pf_symbol) ( module_t * p_module );
1025 /* Try to resolve the symbol */
1026 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1028 if( pf_symbol == NULL )
1030 /* We couldn't load the symbol */
1031 intf_WarnMsg( 1, "module warning: "
1032 "cannot find symbol %s in module %s (%s)",
1033 psz_name, p_module->is.plugin.psz_filename,
1038 /* We can now try to call the symbol */
1039 if( pf_symbol( p_module ) != 0 )
1041 /* With a well-written module we shouldn't have to print an
1042 * additional error message here, but just make sure. */
1043 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
1044 psz_name, p_module->is.plugin.psz_filename );
1048 /* Everything worked fine, we can return */
1051 #endif /* HAVE_DYNAMIC_PLUGINS */