1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.46 2001/12/30 07:09:56 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"
58 #include "stream_control.h"
59 #include "input_ext-intf.h"
60 #include "input_ext-dec.h"
61 #include "input_ext-plugins.h"
64 #include "video_output.h"
66 #include "audio_output.h"
70 #ifdef HAVE_DYNAMIC_PLUGINS
71 # include "modules_plugin.h"
73 #include "modules_builtin.h"
75 /*****************************************************************************
77 *****************************************************************************/
78 #ifdef HAVE_DYNAMIC_PLUGINS
79 static void AllocateAllPlugins ( void );
80 static int AllocatePluginModule ( char * );
82 static void AllocateAllBuiltins ( void );
83 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
84 int ( * ) ( module_t * ),
85 int ( * ) ( module_t * ) );
86 static int DeleteModule ( module_t * );
87 static int LockModule ( module_t * );
88 static int UnlockModule ( module_t * );
89 #ifdef HAVE_DYNAMIC_PLUGINS
90 static int HideModule ( module_t * );
91 static int CallSymbol ( module_t *, char * );
94 static module_symbols_t symbols;
96 /*****************************************************************************
97 * module_InitBank: create the module bank.
98 *****************************************************************************
99 * This function creates a module bank structure and fills it with the
100 * built-in modules, as well as all the plugin modules it can find.
101 *****************************************************************************/
102 void module_InitBank( void )
104 p_module_bank->first = NULL;
105 p_module_bank->i_count = 0;
106 vlc_mutex_init( &p_module_bank->lock );
109 * Store the symbols to be exported
111 STORE_SYMBOLS( &symbols );
114 * Check all the built-in modules
116 intf_WarnMsg( 2, "module: checking built-in modules" );
117 AllocateAllBuiltins();
120 * Check all the plugin modules we can find
122 #ifdef HAVE_DYNAMIC_PLUGINS
123 intf_WarnMsg( 2, "module: checking plugin modules" );
124 AllocateAllPlugins();
127 intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
128 p_module_bank->i_count );
133 /*****************************************************************************
134 * module_EndBank: empty the module bank.
135 *****************************************************************************
136 * This function unloads all unused plugin modules and empties the module
137 * bank in case of success.
138 *****************************************************************************/
139 void module_EndBank( void )
143 while( p_module_bank->first != NULL )
145 if( DeleteModule( p_module_bank->first ) )
147 /* Module deletion failed */
148 intf_ErrMsg( "module error: `%s' can't be removed, trying harder",
149 p_module_bank->first->psz_name );
151 /* We just free the module by hand. Niahahahahaha. */
152 p_next = p_module_bank->first->next;
153 free(p_module_bank->first);
154 p_module_bank->first = p_next;
158 /* Destroy the lock */
159 vlc_mutex_destroy( &p_module_bank->lock );
164 /*****************************************************************************
165 * module_ResetBank: reset the module bank.
166 *****************************************************************************
167 * This function resets the module bank by unloading all unused plugin
169 *****************************************************************************/
170 void module_ResetBank( void )
172 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
176 /*****************************************************************************
177 * module_ManageBank: manage the module bank.
178 *****************************************************************************
179 * This function parses the module bank and hides modules that have been
180 * unused for a while.
181 *****************************************************************************/
182 void module_ManageBank( void )
184 #ifdef HAVE_DYNAMIC_PLUGINS
187 /* We take the global lock */
188 vlc_mutex_lock( &p_module_bank->lock );
190 /* Parse the module list to see if any modules need to be unloaded */
191 for( p_module = p_module_bank->first ;
193 p_module = p_module->next )
195 /* If the module is unused and if it is a plugin module... */
196 if( p_module->i_usage == 0 && !p_module->b_builtin )
198 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
200 p_module->i_unused_delay++;
204 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
205 p_module->psz_name );
206 HideModule( p_module );
208 /* Break here, so that we only hide one module at a time */
214 /* We release the global lock */
215 vlc_mutex_unlock( &p_module_bank->lock );
216 #endif /* HAVE_DYNAMIC_PLUGINS */
221 int module_NeedMemcpy( memcpy_module_t *p_memcpy )
223 p_memcpy->p_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL, NULL );
225 if( p_memcpy->p_module == NULL )
230 p_memcpy->pf_memcpy = p_memcpy->p_module->p_functions->memcpy.functions.memcpy.fast_memcpy;
235 void module_UnneedMemcpy( memcpy_module_t *p_memcpy )
237 module_Unneed( p_memcpy->p_module );
241 int module_NeedIntf( intf_module_t *p_intf )
243 p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
245 if( p_intf->p_module == NULL )
250 p_intf->pf_open = p_intf->p_module->p_functions->intf.functions.intf.pf_open;
251 p_intf->pf_run = p_intf->p_module->p_functions->intf.functions.intf.pf_run;
252 p_intf->pf_close = p_intf->p_module->p_functions->intf.functions.intf.pf_close;
258 /*****************************************************************************
259 * module_Need: return the best module function, given a capability list.
260 *****************************************************************************
261 * This function returns the module that best fits the asked capabilities.
262 *****************************************************************************/
263 module_t * module_Need( int i_capability, char *psz_name, probedata_t *p_data )
267 /* We take the global lock */
268 vlc_mutex_lock( &p_module_bank->lock );
270 if( psz_name != NULL && *psz_name )
272 /* A module name was requested. Use the first matching one. */
276 for( p_module = p_module_bank->first;
278 p_module = p_module->next )
280 /* Test that this module can do everything we need */
281 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
286 /* Test if we have the required CPU */
287 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
288 != p_module->i_cpu_capabilities )
293 /* Test if this plugin exports the required shortcut */
295 !b_ok && p_module->pp_shortcuts[i_index];
298 b_ok = !strcmp( psz_name, p_module->pp_shortcuts[i_index] );
310 LockModule( p_module );
314 intf_ErrMsg( "module error: requested %s module `%s' not found",
315 GetCapabilityName( i_capability ), psz_name );
320 /* No module name was requested. Sort the modules and test them */
321 typedef struct module_list_s
323 struct module_s *p_module;
324 struct module_list_s* p_next;
329 struct module_list_s *p_list = malloc( p_module_bank->i_count
330 * sizeof( module_list_t ) );
331 struct module_list_s *p_first = NULL;
333 /* Parse the module list for capabilities and probe each of them */
334 for( p_module = p_module_bank->first ;
336 p_module = p_module->next )
338 /* Test that this module can do everything we need */
339 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
344 /* Test if we have the required CPU */
345 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
346 != p_module->i_cpu_capabilities )
351 /* Test if we requested a particular intf plugin */
353 if( i_capability == MODULE_CAPABILITY_INTF
354 && p_module->psz_program != NULL
355 && strcmp( p_module->psz_program, p_main->psz_arg0 ) )
361 /* Store this new module */
362 p_list[ i_index ].p_module = p_module;
366 p_list[ i_index ].p_next = NULL;
371 /* Ok, so at school you learned that quicksort is quick, and
372 * bubble sort sucks raw eggs. But that's when dealing with
373 * thousands of items. Here we have barely 50. */
374 struct module_list_s *p_newlist = p_first;
376 if( p_first->p_module->pi_score[i_capability]
377 < p_module->pi_score[i_capability] )
379 p_list[ i_index ].p_next = p_first;
380 p_first = &p_list[ i_index ];
384 while( p_newlist->p_next != NULL
386 ->p_module->pi_score[i_capability]
387 >= p_module->pi_score[i_capability] )
389 p_newlist = p_newlist->p_next;
392 p_list[ i_index ].p_next = p_newlist->p_next;
393 p_newlist->p_next = &p_list[ i_index ];
400 /* Parse the linked list and use the first successful module */
401 while( p_first != NULL )
403 LockModule( p_first->p_module );
405 /* Test the requested capability */
406 i_score += ((function_list_t *)p_first->p_module->p_functions)
407 [i_capability].pf_probe( p_data );
409 /* If the high score was broken, we have a new champion */
415 UnlockModule( p_first->p_module );
417 p_first = p_first->p_next;
420 p_module = (p_first == NULL) ? NULL : p_first->p_module;
424 /* We can release the global lock, module refcount was incremented */
425 vlc_mutex_unlock( &p_module_bank->lock );
427 if( p_module != NULL )
429 intf_WarnMsg( 1, "module: locking %s module `%s'",
430 GetCapabilityName( i_capability ),
431 p_module->psz_name );
434 /* Don't forget that the module is still locked if bestmodule != NULL */
438 /*****************************************************************************
439 * module_Unneed: decrease the usage count of a module.
440 *****************************************************************************
441 * This function must be called by the thread that called module_Need, to
442 * decrease the reference count and allow for hiding of modules.
443 *****************************************************************************/
444 void module_Unneed( module_t * p_module )
446 /* We take the global lock */
447 vlc_mutex_lock( &p_module_bank->lock );
449 /* Just unlock the module - we can't do anything if it fails,
450 * so there is no need to check the return value. */
451 UnlockModule( p_module );
453 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
455 /* We release the global lock */
456 vlc_mutex_unlock( &p_module_bank->lock );
461 /*****************************************************************************
462 * Following functions are local.
463 *****************************************************************************/
465 /*****************************************************************************
466 * AllocateAllPlugins: load all plugin modules we can find.
467 *****************************************************************************/
468 #ifdef HAVE_DYNAMIC_PLUGINS
469 static void AllocateAllPlugins( void )
471 static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
473 char ** ppsz_path = path;
476 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
477 char * psz_vlcpath = system_GetProgramPath();
478 int i_vlclen = strlen( psz_vlcpath );
479 boolean_t b_notinroot;
482 struct dirent * file;
484 for( ; *ppsz_path != NULL ; ppsz_path++ )
486 /* Store strlen(*ppsz_path) for later use. */
487 int i_dirlen = strlen( *ppsz_path );
489 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
491 /* Under BeOS, we need to add beos_GetProgramPath() to access
492 * files under the current directory */
493 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
495 i_dirlen += i_vlclen + 2;
498 psz_fullpath = malloc( i_dirlen );
499 if( psz_fullpath == NULL )
503 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
508 psz_fullpath = *ppsz_path;
511 intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
513 if( (dir = opendir( psz_fullpath )) )
515 /* Parse the directory and try to load all files it contains. */
516 while( (file = readdir( dir )) )
518 int i_filelen = strlen( file->d_name );
520 /* We only load files ending with ".so" */
522 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
524 psz_file = malloc( i_dirlen + i_filelen + 2 );
525 if( psz_file == NULL )
529 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
531 /* We created a nice filename -- now we just try to load
532 * it as a plugin module. */
533 AllocatePluginModule( psz_file );
535 /* We don't care if the allocation succeeded */
540 /* Close the directory if successfully opened */
544 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
547 free( psz_fullpath );
553 /*****************************************************************************
554 * AllocatePluginModule: load a module into memory and initialize it.
555 *****************************************************************************
556 * This function loads a dynamically loadable module and allocates a structure
557 * for its information data. The module can then be handled by module_Need,
558 * module_Unneed and HideModule. It can be removed by DeleteModule.
559 *****************************************************************************/
560 static int AllocatePluginModule( char * psz_filename )
563 module_t * p_module, * p_othermodule;
564 module_handle_t handle;
566 /* Try to dynamically load the module. */
567 if( module_load( psz_filename, &handle ) )
569 /* The plugin module couldn't be opened */
570 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
571 psz_filename, module_error() );
575 /* Now that we have successfully loaded the module, we can
576 * allocate a structure for it */
577 p_module = malloc( sizeof( module_t ) );
578 if( p_module == NULL )
580 intf_ErrMsg( "module error: can't allocate p_module" );
581 module_unload( handle );
585 /* We need to fill these since they may be needed by CallSymbol() */
586 p_module->is.plugin.psz_filename = psz_filename;
587 p_module->is.plugin.handle = handle;
588 p_module->p_symbols = &symbols;
590 /* Initialize the module : fill p_module->psz_name, etc. */
591 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
593 /* We couldn't call InitModule() */
595 module_unload( handle );
599 /* Check that we don't already have a module with this name */
600 for( p_othermodule = p_module_bank->first ;
601 p_othermodule != NULL ;
602 p_othermodule = p_othermodule->next )
604 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
606 intf_WarnMsg( 5, "module warning: cannot load %s, a module named "
607 "`%s' already exists",
608 psz_filename, p_module->psz_name );
610 module_unload( handle );
615 /* Activate the module : fill the capability structure, etc. */
616 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
618 /* We couldn't call ActivateModule() */
620 module_unload( handle );
624 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
626 *pp_shortcut = strdup( *pp_shortcut );
629 /* We strdup() these entries so that they are still valid when the
630 * module is unloaded. */
631 p_module->is.plugin.psz_filename =
632 strdup( p_module->is.plugin.psz_filename );
633 p_module->psz_name = strdup( p_module->psz_name );
634 p_module->psz_longname = strdup( p_module->psz_longname );
636 if( p_module->is.plugin.psz_filename == NULL
637 || p_module->psz_name == NULL
638 || p_module->psz_longname == NULL )
640 intf_ErrMsg( "module error: can't duplicate strings" );
642 free( p_module->is.plugin.psz_filename );
643 free( p_module->psz_name );
644 free( p_module->psz_longname );
645 free( p_module->psz_program );
648 module_unload( handle );
652 if( p_module->psz_program != NULL )
654 p_module->psz_program = strdup( p_module->psz_program );
657 /* Everything worked fine ! The module is ready to be added to the list. */
658 p_module->i_usage = 0;
659 p_module->i_unused_delay = 0;
661 p_module->b_builtin = 0;
663 /* Link module into the linked list */
664 if( p_module_bank->first != NULL )
666 p_module_bank->first->prev = p_module;
668 p_module->next = p_module_bank->first;
669 p_module->prev = NULL;
670 p_module_bank->first = p_module;
671 p_module_bank->i_count++;
673 /* Immediate message so that a slow module doesn't make the user wait */
674 intf_WarnMsgImm( 2, "module: new plugin module `%s', %s",
675 p_module->psz_name, p_module->psz_longname );
679 #endif /* HAVE_DYNAMIC_PLUGINS */
681 /*****************************************************************************
682 * AllocateAllBuiltins: load all modules we were built with.
683 *****************************************************************************/
684 static void AllocateAllBuiltins( void )
686 ALLOCATE_ALL_BUILTINS();
689 /*****************************************************************************
690 * AllocateBuiltinModule: initialize a built-in module.
691 *****************************************************************************
692 * This function registers a built-in module and allocates a structure
693 * for its information data. The module can then be handled by module_Need,
694 * module_Unneed and HideModule. It can be removed by DeleteModule.
695 *****************************************************************************/
696 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
697 int ( *pf_activate ) ( module_t * ),
698 int ( *pf_deactivate ) ( module_t * ) )
700 module_t * p_module, * p_othermodule;
702 /* Now that we have successfully loaded the module, we can
703 * allocate a structure for it */
704 p_module = malloc( sizeof( module_t ) );
705 if( p_module == NULL )
707 intf_ErrMsg( "module error: can't allocate p_module" );
711 /* Initialize the module : fill p_module->psz_name, etc. */
712 if( pf_init( p_module ) != 0 )
714 /* With a well-written module we shouldn't have to print an
715 * additional error message here, but just make sure. */
716 intf_ErrMsg( "module error: failed calling init in builtin module" );
721 /* Check that we don't already have a module with this name */
722 for( p_othermodule = p_module_bank->first ;
723 p_othermodule != NULL ;
724 p_othermodule = p_othermodule->next )
726 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
728 intf_WarnMsg( 5, "module warning: cannot load builtin `%s', a "
729 "module named `%s' already exists",
730 p_module->psz_name, p_module->psz_name );
736 if( pf_activate( p_module ) != 0 )
738 /* With a well-written module we shouldn't have to print an
739 * additional error message here, but just make sure. */
740 intf_ErrMsg( "module error: failed calling activate "
741 "in builtin module" );
746 /* Everything worked fine ! The module is ready to be added to the list. */
747 p_module->i_usage = 0;
748 p_module->i_unused_delay = 0;
750 p_module->b_builtin = 1;
751 p_module->is.builtin.pf_deactivate = pf_deactivate;
753 /* Link module into the linked list */
754 if( p_module_bank->first != NULL )
756 p_module_bank->first->prev = p_module;
758 p_module->next = p_module_bank->first;
759 p_module->prev = NULL;
760 p_module_bank->first = p_module;
761 p_module_bank->i_count++;
763 /* Immediate message so that a slow module doesn't make the user wait */
764 intf_WarnMsgImm( 2, "module: new builtin module `%s', %s",
765 p_module->psz_name, p_module->psz_longname );
770 /*****************************************************************************
771 * DeleteModule: delete a module and its structure.
772 *****************************************************************************
773 * This function can only be called if i_usage <= 0.
774 *****************************************************************************/
775 static int DeleteModule( module_t * p_module )
777 /* If the module is not in use but is still in memory, we first have
778 * to hide it and remove it from memory before we can free the
780 if( p_module->b_builtin )
782 if( p_module->i_usage != 0 )
784 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
785 " usage %i", p_module->psz_name, p_module->i_usage );
790 /* We deactivate the module now. */
791 p_module->is.builtin.pf_deactivate( p_module );
794 #ifdef HAVE_DYNAMIC_PLUGINS
797 if( p_module->i_usage >= 1 )
799 intf_ErrMsg( "module error: trying to free module `%s' which is"
800 " still in use", p_module->psz_name );
804 /* Two possibilities here: i_usage == -1 and the module is already
805 * unloaded, we can continue, or i_usage == 0, and we have to hide
806 * the module before going on. */
807 if( p_module->i_usage == 0 )
809 if( HideModule( p_module ) != 0 )
817 /* Unlink the module from the linked list. */
818 if( p_module->prev != NULL )
820 p_module->prev->next = p_module->next;
824 p_module_bank->first = p_module->next;
827 if( p_module->next != NULL )
829 p_module->next->prev = p_module->prev;
832 p_module_bank->i_count--;
834 /* We free the structures that we strdup()ed in Allocate*Module(). */
835 #ifdef HAVE_DYNAMIC_PLUGINS
836 if( !p_module->b_builtin )
838 char **pp_shortcut = p_module->pp_shortcuts;
840 for( ; *pp_shortcut ; pp_shortcut++ )
842 free( *pp_shortcut );
845 free( p_module->is.plugin.psz_filename );
846 free( p_module->psz_name );
847 free( p_module->psz_longname );
848 free( p_module->psz_program );
857 /*****************************************************************************
858 * LockModule: increase the usage count of a module and load it if needed.
859 *****************************************************************************
860 * This function has to be called before a thread starts using a module. If
861 * the module is already loaded, we just increase its usage count. If it isn't
862 * loaded, we have to dynamically open it and initialize it.
863 * If you successfully call LockModule() at any moment, be careful to call
864 * UnlockModule() when you don't need it anymore.
865 *****************************************************************************/
866 static int LockModule( module_t * p_module )
868 if( p_module->i_usage >= 0 )
870 /* This module is already loaded and activated, we can return */
875 if( p_module->b_builtin )
877 /* A built-in module should always have a refcount >= 0 ! */
878 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
879 p_module->psz_name, p_module->i_usage );
883 #ifdef HAVE_DYNAMIC_PLUGINS
884 if( p_module->i_usage != -1 )
886 /* This shouldn't happen. Ever. We have serious problems here. */
887 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
888 p_module->psz_name, p_module->i_usage );
892 /* i_usage == -1, which means that the module isn't in memory */
893 if( module_load( p_module->is.plugin.psz_filename,
894 &p_module->is.plugin.handle ) )
896 /* The plugin module couldn't be opened */
897 intf_ErrMsg( "module error: cannot open %s (%s)",
898 p_module->is.plugin.psz_filename, module_error() );
902 /* FIXME: what to do if the guy modified the plugin while it was
903 * unloaded ? It makes XMMS crash nastily, perhaps we should try
904 * to be a bit more clever here. */
906 /* Activate the module : fill the capability structure, etc. */
907 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
909 /* We couldn't call ActivateModule() -- looks nasty, but
910 * we can't do much about it. Just try to unload module. */
911 module_unload( p_module->is.plugin.handle );
912 p_module->i_usage = -1;
916 /* Everything worked fine ! The module is ready to be used */
917 p_module->i_usage = 1;
918 #endif /* HAVE_DYNAMIC_PLUGINS */
923 /*****************************************************************************
924 * UnlockModule: decrease the usage count of a module.
925 *****************************************************************************
926 * We decrease the usage count of a module so that we know when a module
927 * becomes unused and can be hidden.
928 *****************************************************************************/
929 static int UnlockModule( module_t * p_module )
931 if( p_module->i_usage <= 0 )
933 /* This shouldn't happen. Ever. We have serious problems here. */
934 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
935 " which isn't even in use", p_module->psz_name );
939 /* This module is still in use, we can return */
941 p_module->i_unused_delay = 0;
946 #ifdef HAVE_DYNAMIC_PLUGINS
947 /*****************************************************************************
948 * HideModule: remove a module from memory but keep its structure.
949 *****************************************************************************
950 * This function can only be called if i_usage == 0. It will make a call
951 * to the module's inner DeactivateModule() symbol, and then unload it
952 * from memory. A call to module_Need() will automagically load it again.
953 *****************************************************************************/
954 static int HideModule( module_t * p_module )
956 if( p_module->b_builtin )
958 /* A built-in module should never be hidden. */
959 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
960 p_module->psz_name );
964 if( p_module->i_usage >= 1 )
966 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
967 " in use", p_module->psz_name );
971 if( p_module->i_usage <= -1 )
973 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
974 " hidden", p_module->psz_name );
978 /* Deactivate the module : free the capability structure, etc. */
979 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
981 /* We couldn't call DeactivateModule() -- looks nasty, but
982 * we can't do much about it. Just try to unload module anyway. */
983 module_unload( p_module->is.plugin.handle );
984 p_module->i_usage = -1;
988 /* Everything worked fine, we can safely unload the module. */
989 module_unload( p_module->is.plugin.handle );
990 p_module->i_usage = -1;
995 /*****************************************************************************
996 * CallSymbol: calls a module symbol.
997 *****************************************************************************
998 * This function calls a symbol given its name and a module structure. The
999 * symbol MUST refer to a function returning int and taking a module_t* as
1001 *****************************************************************************/
1002 static int CallSymbol( module_t * p_module, char * psz_name )
1004 int (* pf_symbol) ( module_t * p_module );
1006 /* Try to resolve the symbol */
1007 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1009 if( pf_symbol == NULL )
1011 /* We couldn't load the symbol */
1012 intf_WarnMsg( 1, "module warning: "
1013 "cannot find symbol %s in module %s (%s)",
1014 psz_name, p_module->is.plugin.psz_filename,
1019 /* We can now try to call the symbol */
1020 if( pf_symbol( p_module ) != 0 )
1022 /* With a well-written module we shouldn't have to print an
1023 * additional error message here, but just make sure. */
1024 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
1025 psz_name, p_module->is.plugin.psz_filename );
1029 /* Everything worked fine, we can return */
1032 #endif /* HAVE_DYNAMIC_PLUGINS */