1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.71 2002/07/04 18:11:57 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() */
40 #if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
41 # include <dlfcn.h> /* dlopen(), dlsym(), dlclose() */
42 # define HAVE_DYNAMIC_PLUGINS
43 #elif defined(HAVE_IMAGE_H) /* BeOS */
45 # define HAVE_DYNAMIC_PLUGINS
47 # define HAVE_DYNAMIC_PLUGINS
49 # undef HAVE_DYNAMIC_PLUGINS
55 #include "interface.h"
56 #include "vlc_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"
75 #if !defined( _MSC_VER )
76 # include "modules_builtin.h"
78 # include "modules_builtin_msvc.h"
81 /*****************************************************************************
83 *****************************************************************************/
84 DECLARE_MODULE_CAPABILITY_TABLE;
86 /*****************************************************************************
88 *****************************************************************************/
89 #ifdef HAVE_DYNAMIC_PLUGINS
90 static void AllocateAllPlugins ( vlc_object_t * );
91 static int AllocatePluginModule ( vlc_object_t *, char * );
93 static int AllocateBuiltinModule( vlc_object_t *,
94 int ( * ) ( module_t * ),
95 int ( * ) ( module_t * ),
96 int ( * ) ( module_t * ) );
97 static int DeleteModule ( module_t * );
98 static int LockModule ( module_t * );
99 static int UnlockModule ( module_t * );
100 #ifdef HAVE_DYNAMIC_PLUGINS
101 static int HideModule ( module_t * );
102 static int CallSymbol ( module_t *, char * );
105 #ifdef HAVE_DYNAMIC_PLUGINS
106 static module_symbols_t symbols;
109 /*****************************************************************************
110 * module_InitBank: create the module bank.
111 *****************************************************************************
112 * This function creates a module bank structure which will be filled later
113 * on with all the modules found.
114 *****************************************************************************/
115 void __module_InitBank( vlc_object_t *p_this )
117 p_this->p_vlc->module_bank.first = NULL;
118 p_this->p_vlc->module_bank.i_count = 0;
119 vlc_mutex_init( p_this, &p_this->p_vlc->module_bank.lock );
122 * Store the symbols to be exported
124 #ifdef HAVE_DYNAMIC_PLUGINS
125 STORE_SYMBOLS( &symbols );
131 /*****************************************************************************
132 * module_ResetBank: reset the module bank.
133 *****************************************************************************
134 * This function resets the module bank by unloading all unused plugin
136 *****************************************************************************/
137 void __module_ResetBank( vlc_object_t *p_this )
139 msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
143 /*****************************************************************************
144 * module_EndBank: empty the module bank.
145 *****************************************************************************
146 * This function unloads all unused plugin modules and empties the module
147 * bank in case of success.
148 *****************************************************************************/
149 void __module_EndBank( vlc_object_t *p_this )
153 while( p_this->p_vlc->module_bank.first != NULL )
155 if( DeleteModule( p_this->p_vlc->module_bank.first ) )
157 /* Module deletion failed */
158 msg_Err( p_this, "`%s' can't be removed, trying harder",
159 p_this->p_vlc->module_bank.first->psz_object_name );
161 /* We just free the module by hand. Niahahahahaha. */
162 p_next = p_this->p_vlc->module_bank.first->next;
163 free( p_this->p_vlc->module_bank.first );
164 p_this->p_vlc->module_bank.first = p_next;
168 /* Destroy the lock */
169 vlc_mutex_destroy( &p_this->p_vlc->module_bank.lock );
174 /*****************************************************************************
175 * module_LoadMain: load the main program info into the module bank.
176 *****************************************************************************
177 * This function fills the module bank structure with the main module infos.
178 * This is very useful as it will allow us to consider the main program just
179 * as another module, and for instance the configuration options of main will
180 * be available in the module bank structure just as for every other module.
181 *****************************************************************************/
182 void __module_LoadMain( vlc_object_t *p_this )
184 AllocateBuiltinModule( p_this, InitModule__MODULE_main,
185 ActivateModule__MODULE_main,
186 DeactivateModule__MODULE_main );
189 /*****************************************************************************
190 * module_LoadBuiltins: load all modules which we built with.
191 *****************************************************************************
192 * This function fills the module bank structure with the builtin modules.
193 *****************************************************************************/
194 void __module_LoadBuiltins( vlc_object_t * p_this )
196 msg_Dbg( p_this, "checking builtin modules" );
197 ALLOCATE_ALL_BUILTINS();
200 /*****************************************************************************
201 * module_LoadPlugins: load all plugin modules we can find.
202 *****************************************************************************
203 * This function fills the module bank structure with the plugin modules.
204 *****************************************************************************/
205 void __module_LoadPlugins( vlc_object_t * p_this )
207 #ifdef HAVE_DYNAMIC_PLUGINS
208 msg_Dbg( p_this, "checking plugin modules" );
209 AllocateAllPlugins( p_this );
213 /*****************************************************************************
214 * module_ManageBank: manage the module bank.
215 *****************************************************************************
216 * This function parses the module bank and hides modules that have been
217 * unused for a while.
218 *****************************************************************************/
219 void __module_ManageBank( vlc_object_t *p_this )
221 #ifdef HAVE_DYNAMIC_PLUGINS
224 /* We take the global lock */
225 vlc_mutex_lock( &p_this->p_vlc->module_bank.lock );
227 /* Parse the module list to see if any modules need to be unloaded */
228 for( p_module = p_this->p_vlc->module_bank.first ;
230 p_module = p_module->next )
232 /* If the module is unused and if it is a plugin module... */
233 if( p_module->i_usage == 0 && !p_module->b_builtin )
235 if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
237 p_module->i_unused_delay++;
241 msg_Dbg( p_this, "hiding unused plugin module `%s'",
242 p_module->psz_object_name );
243 HideModule( p_module );
245 /* Break here, so that we only hide one module at a time */
251 /* We release the global lock */
252 vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
253 #endif /* HAVE_DYNAMIC_PLUGINS */
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( vlc_object_t *p_this, int i_capability,
264 const char *psz_name, void *p_data )
266 typedef struct module_list_s module_list_t;
272 module_list_t *p_next;
275 module_list_t *p_list, *p_first, *p_tmp;
277 int i_ret, i_index = 0;
278 vlc_bool_t b_intf = VLC_FALSE;
283 char *psz_shortcuts = NULL;
285 msg_Dbg( p_this, "looking for %s module",
286 MODULE_CAPABILITY( i_capability ) );
288 /* We take the global lock */
289 vlc_mutex_lock( &p_this->p_vlc->module_bank.lock );
291 /* Count how many different shortcuts were asked for */
292 if( psz_name && *psz_name )
297 psz_shortcuts = strdup( psz_name );
299 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
301 if( *psz_parser == ',' )
309 /* Sort the modules and test them */
310 p_list = malloc( p_this->p_vlc->module_bank.i_count
311 * sizeof( module_list_t ) );
314 /* Parse the module list for capabilities and probe each of them */
315 for( p_module = p_this->p_vlc->module_bank.first ;
317 p_module = p_module->next )
319 int i_shortcut_bonus = 0;
321 /* Test that this module can do everything we need */
322 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
327 /* Test if we have the required CPU */
328 if( (p_module->i_cpu_capabilities
329 & p_this->p_vlc->i_cpu_capabilities)
330 != p_module->i_cpu_capabilities )
335 /* If we required a shortcut, check this plugin provides it. */
338 vlc_bool_t b_trash = VLC_TRUE;
339 int i_dummy, i_short = i_shortcuts;
340 char *psz_name = psz_shortcuts;
345 b_trash && p_module->pp_shortcuts[i_dummy];
348 b_trash = strcmp( psz_name,
349 p_module->pp_shortcuts[i_dummy] );
354 i_shortcut_bonus = i_short * 10000;
371 /* If we didn't require a shortcut, trash zero-scored plugins */
372 else if( !p_module->pi_score[i_capability] )
377 /* Special case: test if we requested a particular intf plugin */
378 if( i_capability == MODULE_CAPABILITY_INTF )
380 if( p_module->psz_program != NULL
381 && !strcmp( p_module->psz_program,
382 p_this->p_vlc->psz_object_name ) )
386 /* Remove previous non-matching plugins */
395 /* This one doesn't match */
401 /* Store this new module */
402 p_list[ i_index ].p_module = p_module;
403 p_list[ i_index ].i_score = p_module->pi_score[i_capability]
406 /* Add it to the modules-to-probe list */
409 p_list[ 0 ].p_next = NULL;
414 /* Ok, so at school you learned that quicksort is quick, and
415 * bubble sort sucks raw eggs. But that's when dealing with
416 * thousands of items. Here we have barely 50. */
417 module_list_t *p_newlist = p_first;
419 if( p_first->i_score < p_list[ i_index ].i_score )
421 p_list[ i_index ].p_next = p_first;
422 p_first = &p_list[ i_index ];
426 while( p_newlist->p_next != NULL &&
427 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
429 p_newlist = p_newlist->p_next;
432 p_list[ i_index ].p_next = p_newlist->p_next;
433 p_newlist->p_next = &p_list[ i_index ];
440 /* Lock all selected modules */
442 while( p_tmp != NULL )
444 LockModule( p_tmp->p_module );
445 p_tmp = p_tmp->p_next;
448 /* We can release the global lock, module refcounts were incremented */
449 vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
451 /* Parse the linked list and use the first successful module */
453 while( p_tmp != NULL )
455 /* Test the requested capability */
456 switch( i_capability )
458 case MODULE_CAPABILITY_ACCESS:
459 i_ret = p_tmp->p_module->p_functions->access.functions.
460 access.pf_open( (input_thread_t *)p_data );
463 case MODULE_CAPABILITY_DEMUX:
464 i_ret = p_tmp->p_module->p_functions->demux.functions.
465 demux.pf_init( (input_thread_t *)p_data );
468 case MODULE_CAPABILITY_NETWORK:
469 i_ret = p_tmp->p_module->p_functions->network.functions.
470 network.pf_open( p_this, (network_socket_t *)p_data );
473 case MODULE_CAPABILITY_DECODER:
474 i_ret = p_tmp->p_module->p_functions->dec.functions.
475 dec.pf_probe( (u8 *)p_data );
478 case MODULE_CAPABILITY_INTF:
479 i_ret = p_tmp->p_module->p_functions->intf.functions.
480 intf.pf_open( (intf_thread_t *)p_data );
483 case MODULE_CAPABILITY_AOUT:
484 i_ret = p_tmp->p_module->p_functions->aout.functions.
485 aout.pf_open( (aout_thread_t *)p_data );
488 case MODULE_CAPABILITY_VOUT:
489 case MODULE_CAPABILITY_VOUT_FILTER:
490 i_ret = p_tmp->p_module->p_functions->vout.functions.
491 vout.pf_create( (vout_thread_t *)p_data );
494 case MODULE_CAPABILITY_CHROMA:
495 i_ret = p_tmp->p_module->p_functions->chroma.functions.
496 chroma.pf_init( (vout_thread_t *)p_data );
499 case MODULE_CAPABILITY_IDCT:
500 case MODULE_CAPABILITY_IMDCT:
501 case MODULE_CAPABILITY_MOTION:
502 case MODULE_CAPABILITY_DOWNMIX:
503 case MODULE_CAPABILITY_MEMCPY:
504 /* This one always works */
509 msg_Err( p_this, "unknown module type %i", i_capability );
514 /* If the high score was broken, we have a new champion */
521 UnlockModule( p_tmp->p_module );
524 p_tmp = p_tmp->p_next;
527 /* Store the locked module value */
530 p_module = p_tmp->p_module;
531 p_tmp = p_tmp->p_next;
538 /* Unlock the remaining modules */
539 while( p_tmp != NULL )
541 UnlockModule( p_tmp->p_module );
542 p_tmp = p_tmp->p_next;
547 if( p_module != NULL )
549 msg_Info( p_module, "found and locked %s module `%s'",
550 MODULE_CAPABILITY( i_capability ),
551 p_module->psz_object_name );
553 else if( p_first == NULL )
555 msg_Err( p_this, "no available %s module matched `%s'",
556 MODULE_CAPABILITY( i_capability ), psz_name );
558 else if( psz_name != NULL && *psz_name )
560 msg_Err( p_this, "could not load any %s module matching `%s'",
561 MODULE_CAPABILITY( i_capability ), psz_name );
566 free( psz_shortcuts );
569 /* Don't forget that the module is still locked */
573 /*****************************************************************************
574 * module_Unneed: decrease the usage count of a module.
575 *****************************************************************************
576 * This function must be called by the thread that called module_Need, to
577 * decrease the reference count and allow for hiding of modules.
578 *****************************************************************************/
579 void module_Unneed( module_t * p_module )
581 /* We take the global lock */
582 vlc_mutex_lock( &p_module->p_vlc->module_bank.lock );
584 /* Just unlock the module - we can't do anything if it fails,
585 * so there is no need to check the return value. */
586 UnlockModule( p_module );
588 msg_Info( p_module, "unlocking module `%s'", p_module->psz_object_name );
590 /* We release the global lock */
591 vlc_mutex_unlock( &p_module->p_vlc->module_bank.lock );
596 /*****************************************************************************
597 * Following functions are local.
598 *****************************************************************************/
600 /*****************************************************************************
601 * AllocateAllPlugins: load all plugin modules we can find.
602 *****************************************************************************/
603 #ifdef HAVE_DYNAMIC_PLUGINS
604 static void AllocateAllPlugins( vlc_object_t *p_this )
606 /* Yes, there are two NULLs because we replace one with "plugin-path". */
607 char * path[] = { "plugins", PLUGIN_PATH, NULL, NULL };
609 char ** ppsz_path = path;
612 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
613 char * psz_vlcpath = system_GetProgramPath();
614 int i_vlclen = strlen( psz_vlcpath );
615 vlc_bool_t b_notinroot;
618 struct dirent * file;
620 /* If the user provided a plugin path, we add it to the list */
621 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
624 for( ; *ppsz_path != NULL ; ppsz_path++ )
626 /* Store strlen(*ppsz_path) for later use. */
627 int i_dirlen = strlen( *ppsz_path );
629 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
630 b_notinroot = VLC_FALSE;
631 /* Under BeOS, we need to add beos_GetProgramPath() to access
632 * files under the current directory */
633 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
635 i_dirlen += i_vlclen + 2;
636 b_notinroot = VLC_TRUE;
638 psz_fullpath = malloc( i_dirlen );
639 if( psz_fullpath == NULL )
643 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
648 psz_fullpath = *ppsz_path;
651 msg_Dbg( p_this, "browsing `%s'", psz_fullpath );
653 if( (dir = opendir( psz_fullpath )) )
655 /* Parse the directory and try to load all files it contains. */
656 while( (file = readdir( dir )) )
658 int i_filelen = strlen( file->d_name );
660 /* We only load files ending with ".so" */
662 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
664 psz_file = malloc( i_dirlen + i_filelen + 2 );
665 if( psz_file == NULL )
669 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
671 /* We created a nice filename -- now we just try to load
672 * it as a plugin module. */
673 AllocatePluginModule( p_this, psz_file );
675 /* We don't care if the allocation succeeded */
680 /* Close the directory if successfully opened */
684 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
687 free( psz_fullpath );
693 /*****************************************************************************
694 * AllocatePluginModule: load a module into memory and initialize it.
695 *****************************************************************************
696 * This function loads a dynamically loadable module and allocates a structure
697 * for its information data. The module can then be handled by module_Need,
698 * module_Unneed and HideModule. It can be removed by DeleteModule.
699 *****************************************************************************/
700 static int AllocatePluginModule( vlc_object_t * p_this, char * psz_filename )
703 module_t * p_module, * p_othermodule;
704 module_handle_t handle;
706 /* Try to dynamically load the module. */
707 if( module_load( psz_filename, &handle ) )
709 char psz_buffer[256];
711 /* The plugin module couldn't be opened */
712 msg_Warn( p_this, "cannot open %s (%s)",
713 psz_filename, module_error( psz_buffer ) );
717 /* Now that we have successfully loaded the module, we can
718 * allocate a structure for it */
719 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
720 if( p_module == NULL )
722 msg_Err( p_this, "out of memory" );
723 module_unload( handle );
727 /* We need to fill these since they may be needed by CallSymbol() */
728 p_module->is.plugin.psz_filename = psz_filename;
729 p_module->is.plugin.handle = handle;
730 p_module->p_symbols = &symbols;
732 /* Initialize the module: fill p_module->psz_object_name, default config */
733 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
735 /* We couldn't call InitModule() */
736 vlc_object_destroy( p_module );
737 module_unload( handle );
741 /* Check that we don't already have a module with this name */
742 for( p_othermodule = p_this->p_vlc->module_bank.first ;
743 p_othermodule != NULL ;
744 p_othermodule = p_othermodule->next )
746 if( !strcmp( p_othermodule->psz_object_name,
747 p_module->psz_object_name ) )
750 "cannot load %s, a module named `%s' already exists",
751 psz_filename, p_module->psz_object_name );
752 config_Free( p_module );
753 vlc_object_destroy( p_module );
754 module_unload( handle );
759 /* Activate the module : fill the capability structure, etc. */
760 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
762 /* We couldn't call ActivateModule() */
763 config_Free( p_module );
764 vlc_object_destroy( p_module );
765 module_unload( handle );
769 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
771 *pp_shortcut = strdup( *pp_shortcut );
774 /* We strdup() these entries so that they are still valid when the
775 * module is unloaded. */
776 p_module->is.plugin.psz_filename =
777 strdup( p_module->is.plugin.psz_filename );
778 p_module->psz_object_name = strdup( p_module->psz_object_name );
779 p_module->psz_longname = strdup( p_module->psz_longname );
781 if( p_module->is.plugin.psz_filename == NULL
782 || p_module->psz_object_name == NULL
783 || p_module->psz_longname == NULL )
785 msg_Err( p_this, "out of memory" );
787 free( p_module->is.plugin.psz_filename );
788 free( p_module->psz_object_name );
789 free( p_module->psz_longname );
790 free( p_module->psz_program );
792 config_Free( p_module );
793 vlc_object_destroy( p_module );
794 module_unload( handle );
798 if( p_module->psz_program != NULL )
800 p_module->psz_program = strdup( p_module->psz_program );
803 /* Everything worked fine ! The module is ready to be added to the list. */
804 p_module->i_usage = 0;
805 p_module->i_unused_delay = 0;
807 p_module->b_builtin = VLC_FALSE;
809 /* Link module into the linked list */
810 if( p_this->p_vlc->module_bank.first != NULL )
812 p_this->p_vlc->module_bank.first->prev = p_module;
814 p_module->next = p_this->p_vlc->module_bank.first;
815 p_module->prev = NULL;
816 p_this->p_vlc->module_bank.first = p_module;
817 p_this->p_vlc->module_bank.i_count++;
819 msg_Dbg( p_this, "plugin `%s', %s",
820 p_module->psz_object_name, p_module->psz_longname );
822 vlc_object_attach( p_module, p_this );
826 #endif /* HAVE_DYNAMIC_PLUGINS */
828 /*****************************************************************************
829 * AllocateBuiltinModule: initialize a builtin module.
830 *****************************************************************************
831 * This function registers a builtin module and allocates a structure
832 * for its information data. The module can then be handled by module_Need,
833 * module_Unneed and HideModule. It can be removed by DeleteModule.
834 *****************************************************************************/
835 static int AllocateBuiltinModule( vlc_object_t * p_this,
836 int ( *pf_init ) ( module_t * ),
837 int ( *pf_activate ) ( module_t * ),
838 int ( *pf_deactivate ) ( module_t * ) )
840 module_t * p_module, * p_othermodule;
842 /* Now that we have successfully loaded the module, we can
843 * allocate a structure for it */
844 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
845 if( p_module == NULL )
847 msg_Err( p_this, "out of memory" );
851 /* Initialize the module : fill p_module->psz_object_name, etc. */
852 if( pf_init( p_module ) != 0 )
854 /* With a well-written module we shouldn't have to print an
855 * additional error message here, but just make sure. */
856 msg_Err( p_this, "failed calling init in builtin module" );
857 vlc_object_destroy( p_module );
861 /* Check that we don't already have a module with this name */
862 for( p_othermodule = p_this->p_vlc->module_bank.first ;
863 p_othermodule != NULL ;
864 p_othermodule = p_othermodule->next )
866 if( !strcmp( p_othermodule->psz_object_name,
867 p_module->psz_object_name ) )
869 msg_Warn( p_this, "cannot load builtin `%s', "
870 "a module named `%s' already exists",
871 p_module->psz_object_name, p_module->psz_object_name );
872 config_Free( p_module );
873 vlc_object_destroy( p_module );
878 if( pf_activate( p_module ) != 0 )
880 /* With a well-written module we shouldn't have to print an
881 * additional error message here, but just make sure. */
882 msg_Err( p_this, "failed calling activate in builtin module" );
883 config_Free( p_module );
884 vlc_object_destroy( p_module );
888 /* Everything worked fine ! The module is ready to be added to the list. */
889 p_module->i_usage = 0;
890 p_module->i_unused_delay = 0;
892 p_module->b_builtin = VLC_TRUE;
893 p_module->is.builtin.pf_deactivate = pf_deactivate;
895 /* Link module into the linked list */
896 if( p_this->p_vlc->module_bank.first != NULL )
898 p_this->p_vlc->module_bank.first->prev = p_module;
900 p_module->next = p_this->p_vlc->module_bank.first;
901 p_module->prev = NULL;
902 p_this->p_vlc->module_bank.first = p_module;
903 p_this->p_vlc->module_bank.i_count++;
905 msg_Dbg( p_this, "builtin `%s', %s",
906 p_module->psz_object_name, p_module->psz_longname );
908 vlc_object_attach( p_module, p_this );
913 /*****************************************************************************
914 * DeleteModule: delete a module and its structure.
915 *****************************************************************************
916 * This function can only be called if i_usage <= 0.
917 *****************************************************************************/
918 static int DeleteModule( module_t * p_module )
920 /* If the module is not in use but is still in memory, we first have
921 * to hide it and remove it from memory before we can free the
923 if( p_module->b_builtin )
925 if( p_module->i_usage != 0 )
927 msg_Err( p_module, "trying to free builtin module `%s' with "
928 "usage %i", p_module->psz_object_name, p_module->i_usage );
933 /* We deactivate the module now. */
934 p_module->is.builtin.pf_deactivate( p_module );
937 #ifdef HAVE_DYNAMIC_PLUGINS
940 if( p_module->i_usage >= 1 )
942 msg_Err( p_module, "trying to free module `%s' which is "
943 "still in use", p_module->psz_object_name );
947 /* Two possibilities here: i_usage == -1 and the module is already
948 * unloaded, we can continue, or i_usage == 0, and we have to hide
949 * the module before going on. */
950 if( p_module->i_usage == 0 )
952 if( HideModule( p_module ) != 0 )
960 vlc_object_detach_all( p_module );
962 /* Unlink the module from the linked list. */
963 if( p_module->prev != NULL )
965 p_module->prev->next = p_module->next;
969 p_module->p_vlc->module_bank.first = p_module->next;
972 if( p_module->next != NULL )
974 p_module->next->prev = p_module->prev;
977 p_module->p_vlc->module_bank.i_count--;
979 /* We free the structures that we strdup()ed in Allocate*Module(). */
980 #ifdef HAVE_DYNAMIC_PLUGINS
981 if( !p_module->b_builtin )
983 char **pp_shortcut = p_module->pp_shortcuts;
985 for( ; *pp_shortcut ; pp_shortcut++ )
987 free( *pp_shortcut );
990 free( p_module->is.plugin.psz_filename );
991 free( p_module->psz_object_name );
992 free( p_module->psz_longname );
993 free( p_module->psz_program );
997 config_Free( p_module );
998 vlc_object_destroy( p_module );
1003 /*****************************************************************************
1004 * LockModule: increase the usage count of a module and load it if needed.
1005 *****************************************************************************
1006 * This function has to be called before a thread starts using a module. If
1007 * the module is already loaded, we just increase its usage count. If it isn't
1008 * loaded, we have to dynamically open it and initialize it.
1009 * If you successfully call LockModule() at any moment, be careful to call
1010 * UnlockModule() when you don't need it anymore.
1011 *****************************************************************************/
1012 static int LockModule( module_t * p_module )
1014 if( p_module->i_usage >= 0 )
1016 /* This module is already loaded and activated, we can return */
1017 p_module->i_usage++;
1021 if( p_module->b_builtin )
1023 /* A builtin module should always have a refcount >= 0 ! */
1024 msg_Err( p_module, "builtin module `%s' has refcount %i",
1025 p_module->psz_object_name, p_module->i_usage );
1029 #ifdef HAVE_DYNAMIC_PLUGINS
1030 if( p_module->i_usage != -1 )
1032 /* This shouldn't happen. Ever. We have serious problems here. */
1033 msg_Err( p_module, "plugin module `%s' has refcount %i",
1034 p_module->psz_object_name, p_module->i_usage );
1038 /* i_usage == -1, which means that the module isn't in memory */
1039 if( module_load( p_module->is.plugin.psz_filename,
1040 &p_module->is.plugin.handle ) )
1042 char psz_buffer[256];
1044 /* The plugin module couldn't be opened */
1045 msg_Err( p_module, "cannot open %s (%s)",
1046 p_module->is.plugin.psz_filename, module_error(psz_buffer) );
1050 /* FIXME: what to do if the guy modified the plugin while it was
1051 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1052 * to be a bit more clever here. */
1054 /* Activate the module : fill the capability structure, etc. */
1055 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
1057 /* We couldn't call ActivateModule() -- looks nasty, but
1058 * we can't do much about it. Just try to unload module. */
1059 module_unload( p_module->is.plugin.handle );
1060 p_module->i_usage = -1;
1064 /* Everything worked fine ! The module is ready to be used */
1065 p_module->i_usage = 1;
1066 #endif /* HAVE_DYNAMIC_PLUGINS */
1071 /*****************************************************************************
1072 * UnlockModule: decrease the usage count of a module.
1073 *****************************************************************************
1074 * We decrease the usage count of a module so that we know when a module
1075 * becomes unused and can be hidden.
1076 *****************************************************************************/
1077 static int UnlockModule( module_t * p_module )
1079 if( p_module->i_usage <= 0 )
1081 /* This shouldn't happen. Ever. We have serious problems here. */
1082 msg_Err( p_module, "trying to call module_Unneed() on `%s' "
1083 "which is not in use", p_module->psz_object_name );
1087 /* This module is still in use, we can return */
1088 p_module->i_usage--;
1089 p_module->i_unused_delay = 0;
1094 #ifdef HAVE_DYNAMIC_PLUGINS
1095 /*****************************************************************************
1096 * HideModule: remove a module from memory but keep its structure.
1097 *****************************************************************************
1098 * This function can only be called if i_usage == 0. It will make a call
1099 * to the module's inner DeactivateModule() symbol, and then unload it
1100 * from memory. A call to module_Need() will automagically load it again.
1101 *****************************************************************************/
1102 static int HideModule( module_t * p_module )
1104 if( p_module->b_builtin )
1106 /* A builtin module should never be hidden. */
1107 msg_Err( p_module, "trying to hide builtin module `%s'",
1108 p_module->psz_object_name );
1112 if( p_module->i_usage >= 1 )
1114 msg_Err( p_module, "trying to hide module `%s' which is still "
1115 "in use", p_module->psz_object_name );
1119 if( p_module->i_usage <= -1 )
1121 msg_Err( p_module, "trying to hide module `%s' which is already "
1122 "hidden", p_module->psz_object_name );
1126 /* Deactivate the module : free the capability structure, etc. */
1127 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1129 /* We couldn't call DeactivateModule() -- looks nasty, but
1130 * we can't do much about it. Just try to unload module anyway. */
1131 module_unload( p_module->is.plugin.handle );
1132 p_module->i_usage = -1;
1136 /* Everything worked fine, we can safely unload the module. */
1137 module_unload( p_module->is.plugin.handle );
1138 p_module->i_usage = -1;
1143 /*****************************************************************************
1144 * CallSymbol: calls a module symbol.
1145 *****************************************************************************
1146 * This function calls a symbol given its name and a module structure. The
1147 * symbol MUST refer to a function returning int and taking a module_t* as
1149 *****************************************************************************/
1150 static int CallSymbol( module_t * p_module, char * psz_name )
1152 int (* pf_symbol) ( module_t * p_module );
1154 /* Try to resolve the symbol */
1155 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1157 if( pf_symbol == NULL )
1159 char psz_buffer[256];
1161 /* We couldn't load the symbol */
1162 msg_Warn( p_module, "cannot find symbol %s in module %s (%s)",
1163 psz_name, p_module->is.plugin.psz_filename,
1164 module_error( psz_buffer ) );
1168 /* We can now try to call the symbol */
1169 if( pf_symbol( p_module ) != 0 )
1171 /* With a well-written module we shouldn't have to print an
1172 * additional error message here, but just make sure. */
1173 msg_Err( p_module, "failed calling symbol %s in module %s",
1174 psz_name, p_module->is.plugin.psz_filename );
1178 /* Everything worked fine, we can return */
1181 #endif /* HAVE_DYNAMIC_PLUGINS */