1 /*****************************************************************************
2 * modules.c : Built-in and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.50 2002/01/24 13:32:53 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() */
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
56 #include "interface.h"
57 #include "intf_playlist.h"
58 #include "intf_eject.h"
60 #include "stream_control.h"
61 #include "input_ext-intf.h"
62 #include "input_ext-dec.h"
63 #include "input_ext-plugins.h"
66 #include "video_output.h"
68 #include "audio_output.h"
72 #ifdef HAVE_DYNAMIC_PLUGINS
73 # include "modules_plugin.h"
75 #include "modules_builtin.h"
77 /*****************************************************************************
79 *****************************************************************************/
80 #ifdef HAVE_DYNAMIC_PLUGINS
81 static void AllocateAllPlugins ( void );
82 static int AllocatePluginModule ( char * );
84 static void AllocateAllBuiltins ( void );
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 and fills it with the
104 * built-in modules, as well as all the plugin modules it can find.
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 );
120 * Check all the built-in modules
122 intf_WarnMsg( 2, "module: checking built-in modules" );
123 AllocateAllBuiltins();
126 * Check all the plugin modules we can find
128 #ifdef HAVE_DYNAMIC_PLUGINS
129 intf_WarnMsg( 2, "module: checking plugin modules" );
130 AllocateAllPlugins();
133 intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
134 p_module_bank->i_count );
139 /*****************************************************************************
140 * module_EndBank: empty the module bank.
141 *****************************************************************************
142 * This function unloads all unused plugin modules and empties the module
143 * bank in case of success.
144 *****************************************************************************/
145 void module_EndBank( void )
149 while( p_module_bank->first != NULL )
151 if( DeleteModule( p_module_bank->first ) )
153 /* Module deletion failed */
154 intf_ErrMsg( "module error: `%s' can't be removed, trying harder",
155 p_module_bank->first->psz_name );
157 /* We just free the module by hand. Niahahahahaha. */
158 p_next = p_module_bank->first->next;
159 free(p_module_bank->first);
160 p_module_bank->first = p_next;
164 /* Destroy the lock */
165 vlc_mutex_destroy( &p_module_bank->lock );
170 /*****************************************************************************
171 * module_ResetBank: reset the module bank.
172 *****************************************************************************
173 * This function resets the module bank by unloading all unused plugin
175 *****************************************************************************/
176 void module_ResetBank( void )
178 intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
182 /*****************************************************************************
183 * module_ManageBank: manage the module bank.
184 *****************************************************************************
185 * This function parses the module bank and hides modules that have been
186 * unused for a while.
187 *****************************************************************************/
188 void module_ManageBank( void )
190 #ifdef HAVE_DYNAMIC_PLUGINS
193 /* We take the global lock */
194 vlc_mutex_lock( &p_module_bank->lock );
196 /* Parse the module list to see if any modules need to be unloaded */
197 for( p_module = p_module_bank->first ;
199 p_module = p_module->next )
201 /* If the module is unused and if it is a plugin module... */
202 if( p_module->i_usage == 0 && !p_module->b_builtin )
204 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
206 p_module->i_unused_delay++;
210 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
211 p_module->psz_name );
212 HideModule( p_module );
214 /* Break here, so that we only hide one module at a time */
220 /* We release the global lock */
221 vlc_mutex_unlock( &p_module_bank->lock );
222 #endif /* HAVE_DYNAMIC_PLUGINS */
227 int module_NeedMemcpy( memcpy_module_t *p_memcpy )
229 p_memcpy->p_module = module_Need( MODULE_CAPABILITY_MEMCPY, NULL, NULL );
231 if( p_memcpy->p_module == NULL )
236 p_memcpy->pf_memcpy = p_memcpy->p_module->p_functions->memcpy.functions.memcpy.fast_memcpy;
241 void module_UnneedMemcpy( memcpy_module_t *p_memcpy )
243 module_Unneed( p_memcpy->p_module );
247 int module_NeedIntf( intf_module_t *p_intf )
249 p_intf->p_module = module_Need( MODULE_CAPABILITY_INTF, NULL );
251 if( p_intf->p_module == NULL )
256 p_intf->pf_open = p_intf->p_module->p_functions->intf.functions.intf.pf_open;
257 p_intf->pf_run = p_intf->p_module->p_functions->intf.functions.intf.pf_run;
258 p_intf->pf_close = p_intf->p_module->p_functions->intf.functions.intf.pf_close;
264 /*****************************************************************************
265 * module_Need: return the best module function, given a capability list.
266 *****************************************************************************
267 * This function returns the module that best fits the asked capabilities.
268 *****************************************************************************/
269 module_t * module_Need( int i_capability, char *psz_name, probedata_t *p_data )
273 /* We take the global lock */
274 vlc_mutex_lock( &p_module_bank->lock );
276 if( psz_name != NULL && *psz_name )
278 /* A module name was requested. Use the first matching one. */
279 char *psz_realname = NULL;
283 psz_realname = strdup( psz_name );
287 p = strchr( psz_realname, ':' );
292 psz_name = psz_realname;
295 for( p_module = p_module_bank->first;
297 p_module = p_module->next )
299 /* Test that this module can do everything we need */
300 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
305 /* Test if we have the required CPU */
306 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
307 != p_module->i_cpu_capabilities )
312 /* Test if this plugin exports the required shortcut */
314 !b_ok && p_module->pp_shortcuts[i_index];
317 b_ok = !strcmp( psz_name, 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_name );
339 free( psz_realname );
344 /* No module name was requested. Sort the modules and test them */
345 typedef struct module_list_s
347 struct module_s *p_module;
348 struct module_list_s* p_next;
353 struct module_list_s *p_list = malloc( p_module_bank->i_count
354 * sizeof( module_list_t ) );
355 struct module_list_s *p_first = NULL;
357 /* Parse the module list for capabilities and probe each of them */
358 for( p_module = p_module_bank->first ;
360 p_module = p_module->next )
362 /* Test that this module can do everything we need */
363 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
368 /* Test if we have the required CPU */
369 if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
370 != p_module->i_cpu_capabilities )
375 /* Test if we requested a particular intf plugin */
377 if( i_capability == MODULE_CAPABILITY_INTF
378 && p_module->psz_program != NULL
379 && strcmp( p_module->psz_program, p_main->psz_arg0 ) )
385 /* Store this new module */
386 p_list[ i_index ].p_module = p_module;
390 p_list[ i_index ].p_next = NULL;
395 /* Ok, so at school you learned that quicksort is quick, and
396 * bubble sort sucks raw eggs. But that's when dealing with
397 * thousands of items. Here we have barely 50. */
398 struct module_list_s *p_newlist = p_first;
400 if( p_first->p_module->pi_score[i_capability]
401 < p_module->pi_score[i_capability] )
403 p_list[ i_index ].p_next = p_first;
404 p_first = &p_list[ i_index ];
408 while( p_newlist->p_next != NULL
410 ->p_module->pi_score[i_capability]
411 >= p_module->pi_score[i_capability] )
413 p_newlist = p_newlist->p_next;
416 p_list[ i_index ].p_next = p_newlist->p_next;
417 p_newlist->p_next = &p_list[ i_index ];
424 /* Parse the linked list and use the first successful module */
425 while( p_first != NULL )
427 LockModule( p_first->p_module );
429 /* Test the requested capability */
430 i_score += ((function_list_t *)p_first->p_module->p_functions)
431 [i_capability].pf_probe( p_data );
433 /* If the high score was broken, we have a new champion */
439 UnlockModule( p_first->p_module );
441 p_first = p_first->p_next;
444 p_module = (p_first == NULL) ? NULL : p_first->p_module;
448 /* We can release the global lock, module refcount was incremented */
449 vlc_mutex_unlock( &p_module_bank->lock );
451 if( p_module != NULL )
453 intf_WarnMsg( 1, "module: locking %s module `%s'",
454 GetCapabilityName( i_capability ),
455 p_module->psz_name );
458 /* Don't forget that the module is still locked if bestmodule != NULL */
462 /*****************************************************************************
463 * module_Unneed: decrease the usage count of a module.
464 *****************************************************************************
465 * This function must be called by the thread that called module_Need, to
466 * decrease the reference count and allow for hiding of modules.
467 *****************************************************************************/
468 void module_Unneed( module_t * p_module )
470 /* We take the global lock */
471 vlc_mutex_lock( &p_module_bank->lock );
473 /* Just unlock the module - we can't do anything if it fails,
474 * so there is no need to check the return value. */
475 UnlockModule( p_module );
477 intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
479 /* We release the global lock */
480 vlc_mutex_unlock( &p_module_bank->lock );
485 /*****************************************************************************
486 * Following functions are local.
487 *****************************************************************************/
489 /*****************************************************************************
490 * AllocateAllPlugins: load all plugin modules we can find.
491 *****************************************************************************/
492 #ifdef HAVE_DYNAMIC_PLUGINS
493 static void AllocateAllPlugins( void )
495 static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
497 char ** ppsz_path = path;
500 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
501 char * psz_vlcpath = system_GetProgramPath();
502 int i_vlclen = strlen( psz_vlcpath );
503 boolean_t b_notinroot;
506 struct dirent * file;
508 for( ; *ppsz_path != NULL ; ppsz_path++ )
510 /* Store strlen(*ppsz_path) for later use. */
511 int i_dirlen = strlen( *ppsz_path );
513 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
515 /* Under BeOS, we need to add beos_GetProgramPath() to access
516 * files under the current directory */
517 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
519 i_dirlen += i_vlclen + 2;
522 psz_fullpath = malloc( i_dirlen );
523 if( psz_fullpath == NULL )
527 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
532 psz_fullpath = *ppsz_path;
535 intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
537 if( (dir = opendir( psz_fullpath )) )
539 /* Parse the directory and try to load all files it contains. */
540 while( (file = readdir( dir )) )
542 int i_filelen = strlen( file->d_name );
544 /* We only load files ending with ".so" */
546 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
548 psz_file = malloc( i_dirlen + i_filelen + 2 );
549 if( psz_file == NULL )
553 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
555 /* We created a nice filename -- now we just try to load
556 * it as a plugin module. */
557 AllocatePluginModule( psz_file );
559 /* We don't care if the allocation succeeded */
564 /* Close the directory if successfully opened */
568 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
571 free( psz_fullpath );
577 /*****************************************************************************
578 * AllocatePluginModule: load a module into memory and initialize it.
579 *****************************************************************************
580 * This function loads a dynamically loadable module and allocates a structure
581 * for its information data. The module can then be handled by module_Need,
582 * module_Unneed and HideModule. It can be removed by DeleteModule.
583 *****************************************************************************/
584 static int AllocatePluginModule( char * psz_filename )
587 module_t * p_module, * p_othermodule;
588 module_handle_t handle;
590 /* Try to dynamically load the module. */
591 if( module_load( psz_filename, &handle ) )
593 /* The plugin module couldn't be opened */
594 intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
595 psz_filename, module_error() );
599 /* Now that we have successfully loaded the module, we can
600 * allocate a structure for it */
601 p_module = malloc( sizeof( module_t ) );
602 if( p_module == NULL )
604 intf_ErrMsg( "module error: can't allocate p_module" );
605 module_unload( handle );
609 /* We need to fill these since they may be needed by CallSymbol() */
610 p_module->is.plugin.psz_filename = psz_filename;
611 p_module->is.plugin.handle = handle;
612 p_module->p_symbols = &symbols;
614 /* Initialize the module : fill p_module->psz_name, etc. */
615 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
617 /* We couldn't call InitModule() */
619 module_unload( handle );
623 /* Check that we don't already have a module with this name */
624 for( p_othermodule = p_module_bank->first ;
625 p_othermodule != NULL ;
626 p_othermodule = p_othermodule->next )
628 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
630 intf_WarnMsg( 5, "module warning: cannot load %s, a module named "
631 "`%s' already exists",
632 psz_filename, p_module->psz_name );
634 module_unload( handle );
639 /* Activate the module : fill the capability structure, etc. */
640 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
642 /* We couldn't call ActivateModule() */
644 module_unload( handle );
648 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
650 *pp_shortcut = strdup( *pp_shortcut );
653 /* We strdup() these entries so that they are still valid when the
654 * module is unloaded. */
655 p_module->is.plugin.psz_filename =
656 strdup( p_module->is.plugin.psz_filename );
657 p_module->psz_name = strdup( p_module->psz_name );
658 p_module->psz_longname = strdup( p_module->psz_longname );
660 if( p_module->is.plugin.psz_filename == NULL
661 || p_module->psz_name == NULL
662 || p_module->psz_longname == NULL )
664 intf_ErrMsg( "module error: can't duplicate strings" );
666 free( p_module->is.plugin.psz_filename );
667 free( p_module->psz_name );
668 free( p_module->psz_longname );
669 free( p_module->psz_program );
672 module_unload( handle );
676 if( p_module->psz_program != NULL )
678 p_module->psz_program = strdup( p_module->psz_program );
681 /* Everything worked fine ! The module is ready to be added to the list. */
682 p_module->i_usage = 0;
683 p_module->i_unused_delay = 0;
685 p_module->b_builtin = 0;
687 /* Link module into the linked list */
688 if( p_module_bank->first != NULL )
690 p_module_bank->first->prev = p_module;
692 p_module->next = p_module_bank->first;
693 p_module->prev = NULL;
694 p_module_bank->first = p_module;
695 p_module_bank->i_count++;
697 /* Immediate message so that a slow module doesn't make the user wait */
698 intf_WarnMsgImm( 2, "module: new plugin module `%s', %s",
699 p_module->psz_name, p_module->psz_longname );
703 #endif /* HAVE_DYNAMIC_PLUGINS */
705 /*****************************************************************************
706 * AllocateAllBuiltins: load all modules we were built with.
707 *****************************************************************************/
708 static void AllocateAllBuiltins( void )
710 ALLOCATE_ALL_BUILTINS();
713 /*****************************************************************************
714 * AllocateBuiltinModule: initialize a built-in module.
715 *****************************************************************************
716 * This function registers a built-in module and allocates a structure
717 * for its information data. The module can then be handled by module_Need,
718 * module_Unneed and HideModule. It can be removed by DeleteModule.
719 *****************************************************************************/
720 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
721 int ( *pf_activate ) ( module_t * ),
722 int ( *pf_deactivate ) ( module_t * ) )
724 module_t * p_module, * p_othermodule;
726 /* Now that we have successfully loaded the module, we can
727 * allocate a structure for it */
728 p_module = malloc( sizeof( module_t ) );
729 if( p_module == NULL )
731 intf_ErrMsg( "module error: can't allocate p_module" );
735 /* Initialize the module : fill p_module->psz_name, etc. */
736 if( pf_init( 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 init in builtin module" );
745 /* Check that we don't already have a module with this name */
746 for( p_othermodule = p_module_bank->first ;
747 p_othermodule != NULL ;
748 p_othermodule = p_othermodule->next )
750 if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
752 intf_WarnMsg( 5, "module warning: cannot load builtin `%s', a "
753 "module named `%s' already exists",
754 p_module->psz_name, p_module->psz_name );
760 if( pf_activate( p_module ) != 0 )
762 /* With a well-written module we shouldn't have to print an
763 * additional error message here, but just make sure. */
764 intf_ErrMsg( "module error: failed calling activate "
765 "in builtin module" );
770 /* Everything worked fine ! The module is ready to be added to the list. */
771 p_module->i_usage = 0;
772 p_module->i_unused_delay = 0;
774 p_module->b_builtin = 1;
775 p_module->is.builtin.pf_deactivate = pf_deactivate;
777 /* Link module into the linked list */
778 if( p_module_bank->first != NULL )
780 p_module_bank->first->prev = p_module;
782 p_module->next = p_module_bank->first;
783 p_module->prev = NULL;
784 p_module_bank->first = p_module;
785 p_module_bank->i_count++;
787 /* Immediate message so that a slow module doesn't make the user wait */
788 intf_WarnMsgImm( 2, "module: new builtin module `%s', %s",
789 p_module->psz_name, p_module->psz_longname );
794 /*****************************************************************************
795 * DeleteModule: delete a module and its structure.
796 *****************************************************************************
797 * This function can only be called if i_usage <= 0.
798 *****************************************************************************/
799 static int DeleteModule( module_t * p_module )
801 /* If the module is not in use but is still in memory, we first have
802 * to hide it and remove it from memory before we can free the
804 if( p_module->b_builtin )
806 if( p_module->i_usage != 0 )
808 intf_ErrMsg( "module error: trying to free builtin module `%s' with"
809 " usage %i", p_module->psz_name, p_module->i_usage );
814 /* We deactivate the module now. */
815 p_module->is.builtin.pf_deactivate( p_module );
818 #ifdef HAVE_DYNAMIC_PLUGINS
821 if( p_module->i_usage >= 1 )
823 intf_ErrMsg( "module error: trying to free module `%s' which is"
824 " still in use", p_module->psz_name );
828 /* Two possibilities here: i_usage == -1 and the module is already
829 * unloaded, we can continue, or i_usage == 0, and we have to hide
830 * the module before going on. */
831 if( p_module->i_usage == 0 )
833 if( HideModule( p_module ) != 0 )
841 /* Unlink the module from the linked list. */
842 if( p_module->prev != NULL )
844 p_module->prev->next = p_module->next;
848 p_module_bank->first = p_module->next;
851 if( p_module->next != NULL )
853 p_module->next->prev = p_module->prev;
856 p_module_bank->i_count--;
858 /* We free the structures that we strdup()ed in Allocate*Module(). */
859 #ifdef HAVE_DYNAMIC_PLUGINS
860 if( !p_module->b_builtin )
862 char **pp_shortcut = p_module->pp_shortcuts;
864 for( ; *pp_shortcut ; pp_shortcut++ )
866 free( *pp_shortcut );
869 free( p_module->is.plugin.psz_filename );
870 free( p_module->psz_name );
871 free( p_module->psz_longname );
872 free( p_module->psz_program );
881 /*****************************************************************************
882 * LockModule: increase the usage count of a module and load it if needed.
883 *****************************************************************************
884 * This function has to be called before a thread starts using a module. If
885 * the module is already loaded, we just increase its usage count. If it isn't
886 * loaded, we have to dynamically open it and initialize it.
887 * If you successfully call LockModule() at any moment, be careful to call
888 * UnlockModule() when you don't need it anymore.
889 *****************************************************************************/
890 static int LockModule( module_t * p_module )
892 if( p_module->i_usage >= 0 )
894 /* This module is already loaded and activated, we can return */
899 if( p_module->b_builtin )
901 /* A built-in module should always have a refcount >= 0 ! */
902 intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
903 p_module->psz_name, p_module->i_usage );
907 #ifdef HAVE_DYNAMIC_PLUGINS
908 if( p_module->i_usage != -1 )
910 /* This shouldn't happen. Ever. We have serious problems here. */
911 intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
912 p_module->psz_name, p_module->i_usage );
916 /* i_usage == -1, which means that the module isn't in memory */
917 if( module_load( p_module->is.plugin.psz_filename,
918 &p_module->is.plugin.handle ) )
920 /* The plugin module couldn't be opened */
921 intf_ErrMsg( "module error: cannot open %s (%s)",
922 p_module->is.plugin.psz_filename, module_error() );
926 /* FIXME: what to do if the guy modified the plugin while it was
927 * unloaded ? It makes XMMS crash nastily, perhaps we should try
928 * to be a bit more clever here. */
930 /* Activate the module : fill the capability structure, etc. */
931 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
933 /* We couldn't call ActivateModule() -- looks nasty, but
934 * we can't do much about it. Just try to unload module. */
935 module_unload( p_module->is.plugin.handle );
936 p_module->i_usage = -1;
940 /* Everything worked fine ! The module is ready to be used */
941 p_module->i_usage = 1;
942 #endif /* HAVE_DYNAMIC_PLUGINS */
947 /*****************************************************************************
948 * UnlockModule: decrease the usage count of a module.
949 *****************************************************************************
950 * We decrease the usage count of a module so that we know when a module
951 * becomes unused and can be hidden.
952 *****************************************************************************/
953 static int UnlockModule( module_t * p_module )
955 if( p_module->i_usage <= 0 )
957 /* This shouldn't happen. Ever. We have serious problems here. */
958 intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
959 " which isn't even in use", p_module->psz_name );
963 /* This module is still in use, we can return */
965 p_module->i_unused_delay = 0;
970 #ifdef HAVE_DYNAMIC_PLUGINS
971 /*****************************************************************************
972 * HideModule: remove a module from memory but keep its structure.
973 *****************************************************************************
974 * This function can only be called if i_usage == 0. It will make a call
975 * to the module's inner DeactivateModule() symbol, and then unload it
976 * from memory. A call to module_Need() will automagically load it again.
977 *****************************************************************************/
978 static int HideModule( module_t * p_module )
980 if( p_module->b_builtin )
982 /* A built-in module should never be hidden. */
983 intf_ErrMsg( "module error: trying to hide built-in module `%s'",
984 p_module->psz_name );
988 if( p_module->i_usage >= 1 )
990 intf_ErrMsg( "module error: trying to hide module `%s' which is still"
991 " in use", p_module->psz_name );
995 if( p_module->i_usage <= -1 )
997 intf_ErrMsg( "module error: trying to hide module `%s' which is already"
998 " hidden", p_module->psz_name );
1002 /* Deactivate the module : free the capability structure, etc. */
1003 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1005 /* We couldn't call DeactivateModule() -- looks nasty, but
1006 * we can't do much about it. Just try to unload module anyway. */
1007 module_unload( p_module->is.plugin.handle );
1008 p_module->i_usage = -1;
1012 /* Everything worked fine, we can safely unload the module. */
1013 module_unload( p_module->is.plugin.handle );
1014 p_module->i_usage = -1;
1019 /*****************************************************************************
1020 * CallSymbol: calls a module symbol.
1021 *****************************************************************************
1022 * This function calls a symbol given its name and a module structure. The
1023 * symbol MUST refer to a function returning int and taking a module_t* as
1025 *****************************************************************************/
1026 static int CallSymbol( module_t * p_module, char * psz_name )
1028 int (* pf_symbol) ( module_t * p_module );
1030 /* Try to resolve the symbol */
1031 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1033 if( pf_symbol == NULL )
1035 /* We couldn't load the symbol */
1036 intf_WarnMsg( 1, "module warning: "
1037 "cannot find symbol %s in module %s (%s)",
1038 psz_name, p_module->is.plugin.psz_filename,
1043 /* We can now try to call the symbol */
1044 if( pf_symbol( p_module ) != 0 )
1046 /* With a well-written module we shouldn't have to print an
1047 * additional error message here, but just make sure. */
1048 intf_ErrMsg( "module error: failed calling symbol %s in module %s",
1049 psz_name, p_module->is.plugin.psz_filename );
1053 /* Everything worked fine, we can return */
1056 #endif /* HAVE_DYNAMIC_PLUGINS */