1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.70 2002/07/03 19:40:49 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 if( psz_name && *psz_name )
295 psz_shortcuts = strdup( psz_name );
297 for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
299 if( *psz_parser == ',' )
307 /* Sort the modules and test them */
308 p_list = malloc( p_this->p_vlc->module_bank.i_count
309 * sizeof( module_list_t ) );
312 /* Parse the module list for capabilities and probe each of them */
313 for( p_module = p_this->p_vlc->module_bank.first ;
315 p_module = p_module->next )
317 int i_shortcut_bonus = 0;
319 /* Test that this module can do everything we need */
320 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
325 /* Test if we have the required CPU */
326 if( (p_module->i_cpu_capabilities
327 & p_this->p_vlc->i_cpu_capabilities)
328 != p_module->i_cpu_capabilities )
333 /* If we required a shortcut, check this plugin provides it. */
336 vlc_bool_t b_trash = VLC_TRUE;
337 int i_dummy, i_short = i_shortcuts;
338 char *psz_name = psz_shortcuts;
343 b_trash && p_module->pp_shortcuts[i_dummy];
346 b_trash = strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
351 i_shortcut_bonus = i_short * 10000;
368 /* If we didn't require a shortcut, trash zero-scored plugins */
369 else if( !p_module->pi_score[i_capability] )
374 /* Special case: test if we requested a particular intf plugin */
375 if( i_capability == MODULE_CAPABILITY_INTF )
377 if( p_module->psz_program != NULL
378 && !strcmp( p_module->psz_program,
379 p_this->p_vlc->psz_object_name ) )
383 /* Remove previous non-matching plugins */
392 /* This one doesn't match */
398 /* Store this new module */
399 p_list[ i_index ].p_module = p_module;
400 p_list[ i_index ].i_score = p_module->pi_score[i_capability]
403 /* Add it to the modules-to-probe list */
406 p_list[ 0 ].p_next = NULL;
411 /* Ok, so at school you learned that quicksort is quick, and
412 * bubble sort sucks raw eggs. But that's when dealing with
413 * thousands of items. Here we have barely 50. */
414 module_list_t *p_newlist = p_first;
416 if( p_first->i_score < p_list[ i_index ].i_score )
418 p_list[ i_index ].p_next = p_first;
419 p_first = &p_list[ i_index ];
423 while( p_newlist->p_next != NULL &&
424 p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
426 p_newlist = p_newlist->p_next;
429 p_list[ i_index ].p_next = p_newlist->p_next;
430 p_newlist->p_next = &p_list[ i_index ];
437 /* Lock all selected modules */
439 while( p_tmp != NULL )
441 LockModule( p_tmp->p_module );
442 p_tmp = p_tmp->p_next;
445 /* We can release the global lock, module refcounts were incremented */
446 vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
448 /* Parse the linked list and use the first successful module */
450 while( p_tmp != NULL )
452 /* Test the requested capability */
453 switch( i_capability )
455 case MODULE_CAPABILITY_ACCESS:
456 i_ret = p_tmp->p_module->p_functions->access.functions.
457 access.pf_open( (input_thread_t *)p_data );
460 case MODULE_CAPABILITY_DEMUX:
461 i_ret = p_tmp->p_module->p_functions->demux.functions.
462 demux.pf_init( (input_thread_t *)p_data );
465 case MODULE_CAPABILITY_NETWORK:
466 i_ret = p_tmp->p_module->p_functions->network.functions.
467 network.pf_open( p_this, (network_socket_t *)p_data );
470 case MODULE_CAPABILITY_DECODER:
471 i_ret = p_tmp->p_module->p_functions->dec.functions.
472 dec.pf_probe( (u8 *)p_data );
475 case MODULE_CAPABILITY_INTF:
476 i_ret = p_tmp->p_module->p_functions->intf.functions.
477 intf.pf_open( (intf_thread_t *)p_data );
480 case MODULE_CAPABILITY_AOUT:
481 i_ret = p_tmp->p_module->p_functions->aout.functions.
482 aout.pf_open( (aout_thread_t *)p_data );
485 case MODULE_CAPABILITY_VOUT:
486 case MODULE_CAPABILITY_VOUT_FILTER:
487 i_ret = p_tmp->p_module->p_functions->vout.functions.
488 vout.pf_create( (vout_thread_t *)p_data );
491 case MODULE_CAPABILITY_CHROMA:
492 i_ret = p_tmp->p_module->p_functions->chroma.functions.
493 chroma.pf_init( (vout_thread_t *)p_data );
496 case MODULE_CAPABILITY_IDCT:
497 case MODULE_CAPABILITY_IMDCT:
498 case MODULE_CAPABILITY_MOTION:
499 case MODULE_CAPABILITY_DOWNMIX:
500 case MODULE_CAPABILITY_MEMCPY:
501 /* This one always works */
506 msg_Err( p_this, "unknown module type %i", i_capability );
511 /* If the high score was broken, we have a new champion */
518 UnlockModule( p_tmp->p_module );
521 p_tmp = p_tmp->p_next;
524 /* Store the locked module value */
527 p_module = p_tmp->p_module;
528 p_tmp = p_tmp->p_next;
535 /* Unlock the remaining modules */
536 while( p_tmp != NULL )
538 UnlockModule( p_tmp->p_module );
539 p_tmp = p_tmp->p_next;
544 if( p_module != NULL )
546 msg_Info( p_module, "found and locked %s module `%s'",
547 MODULE_CAPABILITY( i_capability ),
548 p_module->psz_object_name );
550 else if( p_first == NULL )
552 msg_Err( p_this, "no available %s module matched `%s'",
553 MODULE_CAPABILITY( i_capability ), psz_name );
555 else if( psz_name != NULL && *psz_name )
557 msg_Err( p_this, "could not load any %s module matching `%s'",
558 MODULE_CAPABILITY( i_capability ), psz_name );
563 free( psz_shortcuts );
566 /* Don't forget that the module is still locked */
570 /*****************************************************************************
571 * module_Unneed: decrease the usage count of a module.
572 *****************************************************************************
573 * This function must be called by the thread that called module_Need, to
574 * decrease the reference count and allow for hiding of modules.
575 *****************************************************************************/
576 void module_Unneed( module_t * p_module )
578 /* We take the global lock */
579 vlc_mutex_lock( &p_module->p_vlc->module_bank.lock );
581 /* Just unlock the module - we can't do anything if it fails,
582 * so there is no need to check the return value. */
583 UnlockModule( p_module );
585 msg_Info( p_module, "unlocking module `%s'", p_module->psz_object_name );
587 /* We release the global lock */
588 vlc_mutex_unlock( &p_module->p_vlc->module_bank.lock );
593 /*****************************************************************************
594 * Following functions are local.
595 *****************************************************************************/
597 /*****************************************************************************
598 * AllocateAllPlugins: load all plugin modules we can find.
599 *****************************************************************************/
600 #ifdef HAVE_DYNAMIC_PLUGINS
601 static void AllocateAllPlugins( vlc_object_t *p_this )
603 /* Yes, there are two NULLs because we replace one with "plugin-path". */
604 char * path[] = { "plugins", PLUGIN_PATH, NULL, NULL };
606 char ** ppsz_path = path;
609 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
610 char * psz_vlcpath = system_GetProgramPath();
611 int i_vlclen = strlen( psz_vlcpath );
612 vlc_bool_t b_notinroot;
615 struct dirent * file;
617 /* If the user provided a plugin path, we add it to the list */
618 path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
621 for( ; *ppsz_path != NULL ; ppsz_path++ )
623 /* Store strlen(*ppsz_path) for later use. */
624 int i_dirlen = strlen( *ppsz_path );
626 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
627 b_notinroot = VLC_FALSE;
628 /* Under BeOS, we need to add beos_GetProgramPath() to access
629 * files under the current directory */
630 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
632 i_dirlen += i_vlclen + 2;
633 b_notinroot = VLC_TRUE;
635 psz_fullpath = malloc( i_dirlen );
636 if( psz_fullpath == NULL )
640 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
645 psz_fullpath = *ppsz_path;
648 msg_Dbg( p_this, "browsing `%s'", psz_fullpath );
650 if( (dir = opendir( psz_fullpath )) )
652 /* Parse the directory and try to load all files it contains. */
653 while( (file = readdir( dir )) )
655 int i_filelen = strlen( file->d_name );
657 /* We only load files ending with ".so" */
659 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
661 psz_file = malloc( i_dirlen + i_filelen + 2 );
662 if( psz_file == NULL )
666 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
668 /* We created a nice filename -- now we just try to load
669 * it as a plugin module. */
670 AllocatePluginModule( p_this, psz_file );
672 /* We don't care if the allocation succeeded */
677 /* Close the directory if successfully opened */
681 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
684 free( psz_fullpath );
690 /*****************************************************************************
691 * AllocatePluginModule: load a module into memory and initialize it.
692 *****************************************************************************
693 * This function loads a dynamically loadable module and allocates a structure
694 * for its information data. The module can then be handled by module_Need,
695 * module_Unneed and HideModule. It can be removed by DeleteModule.
696 *****************************************************************************/
697 static int AllocatePluginModule( vlc_object_t * p_this, char * psz_filename )
700 module_t * p_module, * p_othermodule;
701 module_handle_t handle;
703 /* Try to dynamically load the module. */
704 if( module_load( psz_filename, &handle ) )
706 char psz_buffer[256];
708 /* The plugin module couldn't be opened */
709 msg_Warn( p_this, "cannot open %s (%s)",
710 psz_filename, module_error( psz_buffer ) );
714 /* Now that we have successfully loaded the module, we can
715 * allocate a structure for it */
716 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
717 if( p_module == NULL )
719 msg_Err( p_this, "out of memory" );
720 module_unload( handle );
724 /* We need to fill these since they may be needed by CallSymbol() */
725 p_module->is.plugin.psz_filename = psz_filename;
726 p_module->is.plugin.handle = handle;
727 p_module->p_symbols = &symbols;
729 /* Initialize the module: fill p_module->psz_object_name, default config */
730 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
732 /* We couldn't call InitModule() */
733 vlc_object_destroy( p_module );
734 module_unload( handle );
738 /* Check that we don't already have a module with this name */
739 for( p_othermodule = p_this->p_vlc->module_bank.first ;
740 p_othermodule != NULL ;
741 p_othermodule = p_othermodule->next )
743 if( !strcmp( p_othermodule->psz_object_name,
744 p_module->psz_object_name ) )
747 "cannot load %s, a module named `%s' already exists",
748 psz_filename, p_module->psz_object_name );
749 config_Free( p_module );
750 vlc_object_destroy( p_module );
751 module_unload( handle );
756 /* Activate the module : fill the capability structure, etc. */
757 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
759 /* We couldn't call ActivateModule() */
760 config_Free( p_module );
761 vlc_object_destroy( p_module );
762 module_unload( handle );
766 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
768 *pp_shortcut = strdup( *pp_shortcut );
771 /* We strdup() these entries so that they are still valid when the
772 * module is unloaded. */
773 p_module->is.plugin.psz_filename =
774 strdup( p_module->is.plugin.psz_filename );
775 p_module->psz_object_name = strdup( p_module->psz_object_name );
776 p_module->psz_longname = strdup( p_module->psz_longname );
778 if( p_module->is.plugin.psz_filename == NULL
779 || p_module->psz_object_name == NULL
780 || p_module->psz_longname == NULL )
782 msg_Err( p_this, "out of memory" );
784 free( p_module->is.plugin.psz_filename );
785 free( p_module->psz_object_name );
786 free( p_module->psz_longname );
787 free( p_module->psz_program );
789 config_Free( p_module );
790 vlc_object_destroy( p_module );
791 module_unload( handle );
795 if( p_module->psz_program != NULL )
797 p_module->psz_program = strdup( p_module->psz_program );
800 /* Everything worked fine ! The module is ready to be added to the list. */
801 p_module->i_usage = 0;
802 p_module->i_unused_delay = 0;
804 p_module->b_builtin = VLC_FALSE;
806 /* Link module into the linked list */
807 if( p_this->p_vlc->module_bank.first != NULL )
809 p_this->p_vlc->module_bank.first->prev = p_module;
811 p_module->next = p_this->p_vlc->module_bank.first;
812 p_module->prev = NULL;
813 p_this->p_vlc->module_bank.first = p_module;
814 p_this->p_vlc->module_bank.i_count++;
816 msg_Dbg( p_this, "plugin `%s', %s",
817 p_module->psz_object_name, p_module->psz_longname );
819 vlc_object_attach( p_module, p_this );
823 #endif /* HAVE_DYNAMIC_PLUGINS */
825 /*****************************************************************************
826 * AllocateBuiltinModule: initialize a builtin module.
827 *****************************************************************************
828 * This function registers a builtin module and allocates a structure
829 * for its information data. The module can then be handled by module_Need,
830 * module_Unneed and HideModule. It can be removed by DeleteModule.
831 *****************************************************************************/
832 static int AllocateBuiltinModule( vlc_object_t * p_this,
833 int ( *pf_init ) ( module_t * ),
834 int ( *pf_activate ) ( module_t * ),
835 int ( *pf_deactivate ) ( module_t * ) )
837 module_t * p_module, * p_othermodule;
839 /* Now that we have successfully loaded the module, we can
840 * allocate a structure for it */
841 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
842 if( p_module == NULL )
844 msg_Err( p_this, "out of memory" );
848 /* Initialize the module : fill p_module->psz_object_name, etc. */
849 if( pf_init( p_module ) != 0 )
851 /* With a well-written module we shouldn't have to print an
852 * additional error message here, but just make sure. */
853 msg_Err( p_this, "failed calling init in builtin module" );
854 vlc_object_destroy( p_module );
858 /* Check that we don't already have a module with this name */
859 for( p_othermodule = p_this->p_vlc->module_bank.first ;
860 p_othermodule != NULL ;
861 p_othermodule = p_othermodule->next )
863 if( !strcmp( p_othermodule->psz_object_name,
864 p_module->psz_object_name ) )
866 msg_Warn( p_this, "cannot load builtin `%s', "
867 "a module named `%s' already exists",
868 p_module->psz_object_name, p_module->psz_object_name );
869 config_Free( p_module );
870 vlc_object_destroy( p_module );
875 if( pf_activate( p_module ) != 0 )
877 /* With a well-written module we shouldn't have to print an
878 * additional error message here, but just make sure. */
879 msg_Err( p_this, "failed calling activate in builtin module" );
880 config_Free( p_module );
881 vlc_object_destroy( p_module );
885 /* Everything worked fine ! The module is ready to be added to the list. */
886 p_module->i_usage = 0;
887 p_module->i_unused_delay = 0;
889 p_module->b_builtin = VLC_TRUE;
890 p_module->is.builtin.pf_deactivate = pf_deactivate;
892 /* Link module into the linked list */
893 if( p_this->p_vlc->module_bank.first != NULL )
895 p_this->p_vlc->module_bank.first->prev = p_module;
897 p_module->next = p_this->p_vlc->module_bank.first;
898 p_module->prev = NULL;
899 p_this->p_vlc->module_bank.first = p_module;
900 p_this->p_vlc->module_bank.i_count++;
902 msg_Dbg( p_this, "builtin `%s', %s",
903 p_module->psz_object_name, p_module->psz_longname );
905 vlc_object_attach( p_module, p_this );
910 /*****************************************************************************
911 * DeleteModule: delete a module and its structure.
912 *****************************************************************************
913 * This function can only be called if i_usage <= 0.
914 *****************************************************************************/
915 static int DeleteModule( module_t * p_module )
917 /* If the module is not in use but is still in memory, we first have
918 * to hide it and remove it from memory before we can free the
920 if( p_module->b_builtin )
922 if( p_module->i_usage != 0 )
924 msg_Err( p_module, "trying to free builtin module `%s' with "
925 "usage %i", p_module->psz_object_name, p_module->i_usage );
930 /* We deactivate the module now. */
931 p_module->is.builtin.pf_deactivate( p_module );
934 #ifdef HAVE_DYNAMIC_PLUGINS
937 if( p_module->i_usage >= 1 )
939 msg_Err( p_module, "trying to free module `%s' which is "
940 "still in use", p_module->psz_object_name );
944 /* Two possibilities here: i_usage == -1 and the module is already
945 * unloaded, we can continue, or i_usage == 0, and we have to hide
946 * the module before going on. */
947 if( p_module->i_usage == 0 )
949 if( HideModule( p_module ) != 0 )
957 vlc_object_detach_all( p_module );
959 /* Unlink the module from the linked list. */
960 if( p_module->prev != NULL )
962 p_module->prev->next = p_module->next;
966 p_module->p_vlc->module_bank.first = p_module->next;
969 if( p_module->next != NULL )
971 p_module->next->prev = p_module->prev;
974 p_module->p_vlc->module_bank.i_count--;
976 /* We free the structures that we strdup()ed in Allocate*Module(). */
977 #ifdef HAVE_DYNAMIC_PLUGINS
978 if( !p_module->b_builtin )
980 char **pp_shortcut = p_module->pp_shortcuts;
982 for( ; *pp_shortcut ; pp_shortcut++ )
984 free( *pp_shortcut );
987 free( p_module->is.plugin.psz_filename );
988 free( p_module->psz_object_name );
989 free( p_module->psz_longname );
990 free( p_module->psz_program );
994 config_Free( p_module );
995 vlc_object_destroy( p_module );
1000 /*****************************************************************************
1001 * LockModule: increase the usage count of a module and load it if needed.
1002 *****************************************************************************
1003 * This function has to be called before a thread starts using a module. If
1004 * the module is already loaded, we just increase its usage count. If it isn't
1005 * loaded, we have to dynamically open it and initialize it.
1006 * If you successfully call LockModule() at any moment, be careful to call
1007 * UnlockModule() when you don't need it anymore.
1008 *****************************************************************************/
1009 static int LockModule( module_t * p_module )
1011 if( p_module->i_usage >= 0 )
1013 /* This module is already loaded and activated, we can return */
1014 p_module->i_usage++;
1018 if( p_module->b_builtin )
1020 /* A builtin module should always have a refcount >= 0 ! */
1021 msg_Err( p_module, "builtin module `%s' has refcount %i",
1022 p_module->psz_object_name, p_module->i_usage );
1026 #ifdef HAVE_DYNAMIC_PLUGINS
1027 if( p_module->i_usage != -1 )
1029 /* This shouldn't happen. Ever. We have serious problems here. */
1030 msg_Err( p_module, "plugin module `%s' has refcount %i",
1031 p_module->psz_object_name, p_module->i_usage );
1035 /* i_usage == -1, which means that the module isn't in memory */
1036 if( module_load( p_module->is.plugin.psz_filename,
1037 &p_module->is.plugin.handle ) )
1039 char psz_buffer[256];
1041 /* The plugin module couldn't be opened */
1042 msg_Err( p_module, "cannot open %s (%s)",
1043 p_module->is.plugin.psz_filename, module_error(psz_buffer) );
1047 /* FIXME: what to do if the guy modified the plugin while it was
1048 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1049 * to be a bit more clever here. */
1051 /* Activate the module : fill the capability structure, etc. */
1052 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
1054 /* We couldn't call ActivateModule() -- looks nasty, but
1055 * we can't do much about it. Just try to unload module. */
1056 module_unload( p_module->is.plugin.handle );
1057 p_module->i_usage = -1;
1061 /* Everything worked fine ! The module is ready to be used */
1062 p_module->i_usage = 1;
1063 #endif /* HAVE_DYNAMIC_PLUGINS */
1068 /*****************************************************************************
1069 * UnlockModule: decrease the usage count of a module.
1070 *****************************************************************************
1071 * We decrease the usage count of a module so that we know when a module
1072 * becomes unused and can be hidden.
1073 *****************************************************************************/
1074 static int UnlockModule( module_t * p_module )
1076 if( p_module->i_usage <= 0 )
1078 /* This shouldn't happen. Ever. We have serious problems here. */
1079 msg_Err( p_module, "trying to call module_Unneed() on `%s' "
1080 "which is not in use", p_module->psz_object_name );
1084 /* This module is still in use, we can return */
1085 p_module->i_usage--;
1086 p_module->i_unused_delay = 0;
1091 #ifdef HAVE_DYNAMIC_PLUGINS
1092 /*****************************************************************************
1093 * HideModule: remove a module from memory but keep its structure.
1094 *****************************************************************************
1095 * This function can only be called if i_usage == 0. It will make a call
1096 * to the module's inner DeactivateModule() symbol, and then unload it
1097 * from memory. A call to module_Need() will automagically load it again.
1098 *****************************************************************************/
1099 static int HideModule( module_t * p_module )
1101 if( p_module->b_builtin )
1103 /* A builtin module should never be hidden. */
1104 msg_Err( p_module, "trying to hide builtin module `%s'",
1105 p_module->psz_object_name );
1109 if( p_module->i_usage >= 1 )
1111 msg_Err( p_module, "trying to hide module `%s' which is still "
1112 "in use", p_module->psz_object_name );
1116 if( p_module->i_usage <= -1 )
1118 msg_Err( p_module, "trying to hide module `%s' which is already "
1119 "hidden", p_module->psz_object_name );
1123 /* Deactivate the module : free the capability structure, etc. */
1124 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1126 /* We couldn't call DeactivateModule() -- looks nasty, but
1127 * we can't do much about it. Just try to unload module anyway. */
1128 module_unload( p_module->is.plugin.handle );
1129 p_module->i_usage = -1;
1133 /* Everything worked fine, we can safely unload the module. */
1134 module_unload( p_module->is.plugin.handle );
1135 p_module->i_usage = -1;
1140 /*****************************************************************************
1141 * CallSymbol: calls a module symbol.
1142 *****************************************************************************
1143 * This function calls a symbol given its name and a module structure. The
1144 * symbol MUST refer to a function returning int and taking a module_t* as
1146 *****************************************************************************/
1147 static int CallSymbol( module_t * p_module, char * psz_name )
1149 int (* pf_symbol) ( module_t * p_module );
1151 /* Try to resolve the symbol */
1152 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1154 if( pf_symbol == NULL )
1156 char psz_buffer[256];
1158 /* We couldn't load the symbol */
1159 msg_Warn( p_module, "cannot find symbol %s in module %s (%s)",
1160 psz_name, p_module->is.plugin.psz_filename,
1161 module_error( psz_buffer ) );
1165 /* We can now try to call the symbol */
1166 if( pf_symbol( p_module ) != 0 )
1168 /* With a well-written module we shouldn't have to print an
1169 * additional error message here, but just make sure. */
1170 msg_Err( p_module, "failed calling symbol %s in module %s",
1171 psz_name, p_module->is.plugin.psz_filename );
1175 /* Everything worked fine, we can return */
1178 #endif /* HAVE_DYNAMIC_PLUGINS */