1 /*****************************************************************************
2 * modules.c : Builtin and plugin modules management functions
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: modules.c,v 1.63 2002/06/01 12:32:01 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"
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,
264 int i_capability, char *psz_name, void *p_data )
266 typedef struct module_list_s module_list_t;
271 module_list_t *p_next;
274 module_list_t *p_list, *p_first, *p_tmp;
276 int i_ret, i_index = 0;
277 vlc_bool_t b_intf = 0;
280 char *psz_realname = NULL;
282 msg_Info( p_this, "looking for %s module",
283 MODULE_CAPABILITY( i_capability ) );
285 /* We take the global lock */
286 vlc_mutex_lock( &p_this->p_vlc->module_bank.lock );
288 if( psz_name != NULL && *psz_name )
290 /* A module name was requested. */
291 psz_realname = strdup( psz_name );
295 p = strchr( psz_realname, ':' );
300 psz_name = psz_realname;
304 /* Sort the modules and test them */
305 p_list = malloc( p_this->p_vlc->module_bank.i_count
306 * sizeof( module_list_t ) );
309 /* Parse the module list for capabilities and probe each of them */
310 for( p_module = p_this->p_vlc->module_bank.first ;
312 p_module = p_module->next )
314 /* Test that this module can do everything we need */
315 if( !(p_module->i_capabilities & ( 1 << i_capability )) )
320 /* Test if we have the required CPU */
321 if( (p_module->i_cpu_capabilities
322 & p_this->p_vlc->i_cpu_capabilities)
323 != p_module->i_cpu_capabilities )
328 /* If we required a shortcut, check this plugin provides it. */
329 if( psz_name != NULL && *psz_name )
331 vlc_bool_t b_trash = 1;
335 b_trash && p_module->pp_shortcuts[i_dummy];
338 b_trash = strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
346 /* If we didn't require a shortcut, trash zero-scored plugins */
347 else if( !p_module->pi_score[i_capability] )
352 /* Special case: test if we requested a particular intf plugin */
353 if( i_capability == MODULE_CAPABILITY_INTF )
355 if( p_module->psz_program != NULL
356 && !strcmp( p_module->psz_program,
357 p_this->p_vlc->psz_object_name ) )
361 /* Remove previous non-matching plugins */
370 /* This one doesn't match */
376 /* Store this new module */
377 p_list[ i_index ].p_module = p_module;
379 /* Add it to the modules-to-probe list */
382 p_list[ 0 ].p_next = NULL;
387 /* Ok, so at school you learned that quicksort is quick, and
388 * bubble sort sucks raw eggs. But that's when dealing with
389 * thousands of items. Here we have barely 50. */
390 module_list_t *p_newlist = p_first;
392 if( p_first->p_module->pi_score[i_capability]
393 < p_module->pi_score[i_capability] )
395 p_list[ i_index ].p_next = p_first;
396 p_first = &p_list[ i_index ];
400 while( p_newlist->p_next != NULL
401 && p_newlist->p_next->p_module->pi_score[i_capability]
402 >= p_module->pi_score[i_capability] )
404 p_newlist = p_newlist->p_next;
407 p_list[ i_index ].p_next = p_newlist->p_next;
408 p_newlist->p_next = &p_list[ i_index ];
415 /* Lock all selected modules */
417 while( p_tmp != NULL )
419 LockModule( p_tmp->p_module );
420 p_tmp = p_tmp->p_next;
423 /* We can release the global lock, module refcounts were incremented */
424 vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
426 /* Parse the linked list and use the first successful module */
428 while( p_tmp != NULL )
430 /* Test the requested capability */
431 switch( i_capability )
433 case MODULE_CAPABILITY_ACCESS:
434 i_ret = p_tmp->p_module->p_functions->access.functions.
435 access.pf_open( (input_thread_t *)p_data );
438 case MODULE_CAPABILITY_DEMUX:
439 i_ret = p_tmp->p_module->p_functions->demux.functions.
440 demux.pf_init( (input_thread_t *)p_data );
443 case MODULE_CAPABILITY_NETWORK:
444 i_ret = p_tmp->p_module->p_functions->network.functions.
445 network.pf_open( p_this, (network_socket_t *)p_data );
448 case MODULE_CAPABILITY_DECODER:
449 i_ret = p_tmp->p_module->p_functions->dec.functions.
450 dec.pf_probe( (u8 *)p_data );
453 case MODULE_CAPABILITY_INTF:
454 i_ret = p_tmp->p_module->p_functions->intf.functions.
455 intf.pf_open( (intf_thread_t *)p_data );
458 case MODULE_CAPABILITY_AOUT:
459 i_ret = p_tmp->p_module->p_functions->aout.functions.
460 aout.pf_open( (aout_thread_t *)p_data );
463 case MODULE_CAPABILITY_VOUT:
464 i_ret = p_tmp->p_module->p_functions->vout.functions.
465 vout.pf_create( (vout_thread_t *)p_data );
468 case MODULE_CAPABILITY_CHROMA:
469 i_ret = p_tmp->p_module->p_functions->chroma.functions.
470 chroma.pf_init( (vout_thread_t *)p_data );
473 case MODULE_CAPABILITY_IDCT:
474 case MODULE_CAPABILITY_IMDCT:
475 case MODULE_CAPABILITY_MOTION:
476 case MODULE_CAPABILITY_DOWNMIX:
477 case MODULE_CAPABILITY_MEMCPY:
478 /* This one always works */
483 msg_Err( p_this, "unknown module type %i", i_capability );
488 /* If the high score was broken, we have a new champion */
495 UnlockModule( p_tmp->p_module );
498 p_tmp = p_tmp->p_next;
501 /* Store the locked module value */
504 p_module = p_tmp->p_module;
505 p_tmp = p_tmp->p_next;
512 /* Unlock the remaining modules */
513 while( p_tmp != NULL )
515 UnlockModule( p_tmp->p_module );
516 p_tmp = p_tmp->p_next;
521 if( p_module != NULL )
523 msg_Info( p_module, "found and locked %s module `%s'",
524 MODULE_CAPABILITY( i_capability ),
525 p_module->psz_object_name );
527 else if( p_first == NULL )
529 msg_Err( p_this, "no %s module named `%s'",
530 MODULE_CAPABILITY( i_capability ), psz_name );
532 else if( psz_name != NULL && *psz_name )
534 msg_Err( p_this, "could not load %s module `%s'",
535 MODULE_CAPABILITY( i_capability ), psz_name );
540 free( psz_realname );
543 /* Don't forget that the module is still locked */
547 /*****************************************************************************
548 * module_Unneed: decrease the usage count of a module.
549 *****************************************************************************
550 * This function must be called by the thread that called module_Need, to
551 * decrease the reference count and allow for hiding of modules.
552 *****************************************************************************/
553 void module_Unneed( module_t * p_module )
555 /* We take the global lock */
556 vlc_mutex_lock( &p_module->p_vlc->module_bank.lock );
558 /* Just unlock the module - we can't do anything if it fails,
559 * so there is no need to check the return value. */
560 UnlockModule( p_module );
562 msg_Info( p_module, "unlocking module `%s'", p_module->psz_object_name );
564 /* We release the global lock */
565 vlc_mutex_unlock( &p_module->p_vlc->module_bank.lock );
570 /*****************************************************************************
571 * Following functions are local.
572 *****************************************************************************/
574 /*****************************************************************************
575 * AllocateAllPlugins: load all plugin modules we can find.
576 *****************************************************************************/
577 #ifdef HAVE_DYNAMIC_PLUGINS
578 static void AllocateAllPlugins( vlc_object_t *p_this )
580 static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
582 char ** ppsz_path = path;
585 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
586 char * psz_vlcpath = system_GetProgramPath();
587 int i_vlclen = strlen( psz_vlcpath );
588 vlc_bool_t b_notinroot;
591 struct dirent * file;
593 for( ; *ppsz_path != NULL ; ppsz_path++ )
595 /* Store strlen(*ppsz_path) for later use. */
596 int i_dirlen = strlen( *ppsz_path );
598 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
600 /* Under BeOS, we need to add beos_GetProgramPath() to access
601 * files under the current directory */
602 if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
604 i_dirlen += i_vlclen + 2;
607 psz_fullpath = malloc( i_dirlen );
608 if( psz_fullpath == NULL )
612 sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
617 psz_fullpath = *ppsz_path;
620 msg_Dbg( p_this, "browsing `%s'", psz_fullpath );
622 if( (dir = opendir( psz_fullpath )) )
624 /* Parse the directory and try to load all files it contains. */
625 while( (file = readdir( dir )) )
627 int i_filelen = strlen( file->d_name );
629 /* We only load files ending with ".so" */
631 && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
633 psz_file = malloc( i_dirlen + i_filelen + 2 );
634 if( psz_file == NULL )
638 sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
640 /* We created a nice filename -- now we just try to load
641 * it as a plugin module. */
642 AllocatePluginModule( p_this, psz_file );
644 /* We don't care if the allocation succeeded */
649 /* Close the directory if successfully opened */
653 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
656 free( psz_fullpath );
662 /*****************************************************************************
663 * AllocatePluginModule: load a module into memory and initialize it.
664 *****************************************************************************
665 * This function loads a dynamically loadable module and allocates a structure
666 * for its information data. The module can then be handled by module_Need,
667 * module_Unneed and HideModule. It can be removed by DeleteModule.
668 *****************************************************************************/
669 static int AllocatePluginModule( vlc_object_t * p_this, char * psz_filename )
672 module_t * p_module, * p_othermodule;
673 module_handle_t handle;
675 /* Try to dynamically load the module. */
676 if( module_load( psz_filename, &handle ) )
678 char psz_buffer[256];
680 /* The plugin module couldn't be opened */
681 msg_Warn( p_this, "cannot open %s (%s)",
682 psz_filename, module_error( psz_buffer ) );
686 /* Now that we have successfully loaded the module, we can
687 * allocate a structure for it */
688 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
689 if( p_module == NULL )
691 msg_Err( p_this, "out of memory" );
692 module_unload( handle );
696 vlc_object_attach( p_module, p_this );
698 /* We need to fill these since they may be needed by CallSymbol() */
699 p_module->is.plugin.psz_filename = psz_filename;
700 p_module->is.plugin.handle = handle;
701 p_module->p_symbols = &symbols;
703 /* Initialize the module: fill p_module->psz_object_name, default config */
704 if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
706 /* We couldn't call InitModule() */
707 vlc_object_destroy( p_module );
708 module_unload( handle );
712 /* Check that we don't already have a module with this name */
713 for( p_othermodule = p_this->p_vlc->module_bank.first ;
714 p_othermodule != NULL ;
715 p_othermodule = p_othermodule->next )
717 if( !strcmp( p_othermodule->psz_object_name,
718 p_module->psz_object_name ) )
721 "cannot load %s, a module named `%s' already exists",
722 psz_filename, p_module->psz_object_name );
723 config_Free( p_module );
724 vlc_object_destroy( p_module );
725 module_unload( handle );
730 /* Activate the module : fill the capability structure, etc. */
731 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
733 /* We couldn't call ActivateModule() */
734 config_Free( p_module );
735 vlc_object_destroy( p_module );
736 module_unload( handle );
740 for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
742 *pp_shortcut = strdup( *pp_shortcut );
745 /* We strdup() these entries so that they are still valid when the
746 * module is unloaded. */
747 p_module->is.plugin.psz_filename =
748 strdup( p_module->is.plugin.psz_filename );
749 p_module->psz_object_name = strdup( p_module->psz_object_name );
750 p_module->psz_longname = strdup( p_module->psz_longname );
752 if( p_module->is.plugin.psz_filename == NULL
753 || p_module->psz_object_name == NULL
754 || p_module->psz_longname == NULL )
756 msg_Err( p_this, "out of memory" );
758 free( p_module->is.plugin.psz_filename );
759 free( p_module->psz_object_name );
760 free( p_module->psz_longname );
761 free( p_module->psz_program );
763 config_Free( p_module );
764 vlc_object_destroy( p_module );
765 module_unload( handle );
769 if( p_module->psz_program != NULL )
771 p_module->psz_program = strdup( p_module->psz_program );
774 /* Everything worked fine ! The module is ready to be added to the list. */
775 p_module->i_usage = 0;
776 p_module->i_unused_delay = 0;
778 p_module->b_builtin = 0;
780 /* Link module into the linked list */
781 if( p_this->p_vlc->module_bank.first != NULL )
783 p_this->p_vlc->module_bank.first->prev = p_module;
785 p_module->next = p_this->p_vlc->module_bank.first;
786 p_module->prev = NULL;
787 p_this->p_vlc->module_bank.first = p_module;
788 p_this->p_vlc->module_bank.i_count++;
790 msg_Dbg( p_this, "plugin `%s', %s",
791 p_module->psz_object_name, p_module->psz_longname );
795 #endif /* HAVE_DYNAMIC_PLUGINS */
797 /*****************************************************************************
798 * AllocateBuiltinModule: initialize a builtin module.
799 *****************************************************************************
800 * This function registers a builtin module and allocates a structure
801 * for its information data. The module can then be handled by module_Need,
802 * module_Unneed and HideModule. It can be removed by DeleteModule.
803 *****************************************************************************/
804 static int AllocateBuiltinModule( vlc_object_t * p_this,
805 int ( *pf_init ) ( module_t * ),
806 int ( *pf_activate ) ( module_t * ),
807 int ( *pf_deactivate ) ( module_t * ) )
809 module_t * p_module, * p_othermodule;
811 /* Now that we have successfully loaded the module, we can
812 * allocate a structure for it */
813 p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
814 if( p_module == NULL )
816 msg_Err( p_this, "out of memory" );
820 /* Initialize the module : fill p_module->psz_object_name, etc. */
821 if( pf_init( p_module ) != 0 )
823 /* With a well-written module we shouldn't have to print an
824 * additional error message here, but just make sure. */
825 msg_Err( p_this, "failed calling init in builtin module" );
826 vlc_object_destroy( p_module );
830 /* Check that we don't already have a module with this name */
831 for( p_othermodule = p_this->p_vlc->module_bank.first ;
832 p_othermodule != NULL ;
833 p_othermodule = p_othermodule->next )
835 if( !strcmp( p_othermodule->psz_object_name,
836 p_module->psz_object_name ) )
838 msg_Warn( p_this, "cannot load builtin `%s', "
839 "a module named `%s' already exists",
840 p_module->psz_object_name, p_module->psz_object_name );
841 config_Free( p_module );
842 vlc_object_destroy( p_module );
847 if( pf_activate( p_module ) != 0 )
849 /* With a well-written module we shouldn't have to print an
850 * additional error message here, but just make sure. */
851 msg_Err( p_this, "failed calling activate in builtin module" );
852 config_Free( p_module );
853 vlc_object_destroy( p_module );
857 /* Everything worked fine ! The module is ready to be added to the list. */
858 p_module->i_usage = 0;
859 p_module->i_unused_delay = 0;
861 p_module->b_builtin = 1;
862 p_module->is.builtin.pf_deactivate = pf_deactivate;
864 /* Link module into the linked list */
865 if( p_this->p_vlc->module_bank.first != NULL )
867 p_this->p_vlc->module_bank.first->prev = p_module;
869 p_module->next = p_this->p_vlc->module_bank.first;
870 p_module->prev = NULL;
871 p_this->p_vlc->module_bank.first = p_module;
872 p_this->p_vlc->module_bank.i_count++;
874 msg_Dbg( p_this, "builtin `%s', %s",
875 p_module->psz_object_name, p_module->psz_longname );
877 vlc_object_attach( p_module, p_this );
882 /*****************************************************************************
883 * DeleteModule: delete a module and its structure.
884 *****************************************************************************
885 * This function can only be called if i_usage <= 0.
886 *****************************************************************************/
887 static int DeleteModule( module_t * p_module )
889 /* If the module is not in use but is still in memory, we first have
890 * to hide it and remove it from memory before we can free the
892 if( p_module->b_builtin )
894 if( p_module->i_usage != 0 )
896 msg_Err( p_module, "trying to free builtin module `%s' with "
897 "usage %i", p_module->psz_object_name, p_module->i_usage );
902 /* We deactivate the module now. */
903 p_module->is.builtin.pf_deactivate( p_module );
906 #ifdef HAVE_DYNAMIC_PLUGINS
909 if( p_module->i_usage >= 1 )
911 msg_Err( p_module, "trying to free module `%s' which is "
912 "still in use", p_module->psz_object_name );
916 /* Two possibilities here: i_usage == -1 and the module is already
917 * unloaded, we can continue, or i_usage == 0, and we have to hide
918 * the module before going on. */
919 if( p_module->i_usage == 0 )
921 if( HideModule( p_module ) != 0 )
929 vlc_object_unlink_all( p_module );
931 /* Unlink the module from the linked list. */
932 if( p_module->prev != NULL )
934 p_module->prev->next = p_module->next;
938 p_module->p_vlc->module_bank.first = p_module->next;
941 if( p_module->next != NULL )
943 p_module->next->prev = p_module->prev;
946 p_module->p_vlc->module_bank.i_count--;
948 /* We free the structures that we strdup()ed in Allocate*Module(). */
949 #ifdef HAVE_DYNAMIC_PLUGINS
950 if( !p_module->b_builtin )
952 char **pp_shortcut = p_module->pp_shortcuts;
954 for( ; *pp_shortcut ; pp_shortcut++ )
956 free( *pp_shortcut );
959 free( p_module->is.plugin.psz_filename );
960 free( p_module->psz_object_name );
961 free( p_module->psz_longname );
962 free( p_module->psz_program );
966 config_Free( p_module );
967 vlc_object_destroy( p_module );
972 /*****************************************************************************
973 * LockModule: increase the usage count of a module and load it if needed.
974 *****************************************************************************
975 * This function has to be called before a thread starts using a module. If
976 * the module is already loaded, we just increase its usage count. If it isn't
977 * loaded, we have to dynamically open it and initialize it.
978 * If you successfully call LockModule() at any moment, be careful to call
979 * UnlockModule() when you don't need it anymore.
980 *****************************************************************************/
981 static int LockModule( module_t * p_module )
983 if( p_module->i_usage >= 0 )
985 /* This module is already loaded and activated, we can return */
990 if( p_module->b_builtin )
992 /* A builtin module should always have a refcount >= 0 ! */
993 msg_Err( p_module, "builtin module `%s' has refcount %i",
994 p_module->psz_object_name, p_module->i_usage );
998 #ifdef HAVE_DYNAMIC_PLUGINS
999 if( p_module->i_usage != -1 )
1001 /* This shouldn't happen. Ever. We have serious problems here. */
1002 msg_Err( p_module, "plugin module `%s' has refcount %i",
1003 p_module->psz_object_name, p_module->i_usage );
1007 /* i_usage == -1, which means that the module isn't in memory */
1008 if( module_load( p_module->is.plugin.psz_filename,
1009 &p_module->is.plugin.handle ) )
1011 char psz_buffer[256];
1013 /* The plugin module couldn't be opened */
1014 msg_Err( p_module, "cannot open %s (%s)",
1015 p_module->is.plugin.psz_filename, module_error(psz_buffer) );
1019 /* FIXME: what to do if the guy modified the plugin while it was
1020 * unloaded ? It makes XMMS crash nastily, perhaps we should try
1021 * to be a bit more clever here. */
1023 /* Activate the module : fill the capability structure, etc. */
1024 if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
1026 /* We couldn't call ActivateModule() -- looks nasty, but
1027 * we can't do much about it. Just try to unload module. */
1028 module_unload( p_module->is.plugin.handle );
1029 p_module->i_usage = -1;
1033 /* Everything worked fine ! The module is ready to be used */
1034 p_module->i_usage = 1;
1035 #endif /* HAVE_DYNAMIC_PLUGINS */
1040 /*****************************************************************************
1041 * UnlockModule: decrease the usage count of a module.
1042 *****************************************************************************
1043 * We decrease the usage count of a module so that we know when a module
1044 * becomes unused and can be hidden.
1045 *****************************************************************************/
1046 static int UnlockModule( module_t * p_module )
1048 if( p_module->i_usage <= 0 )
1050 /* This shouldn't happen. Ever. We have serious problems here. */
1051 msg_Err( p_module, "trying to call module_Unneed() on `%s' "
1052 "which is not in use", p_module->psz_object_name );
1056 /* This module is still in use, we can return */
1057 p_module->i_usage--;
1058 p_module->i_unused_delay = 0;
1063 #ifdef HAVE_DYNAMIC_PLUGINS
1064 /*****************************************************************************
1065 * HideModule: remove a module from memory but keep its structure.
1066 *****************************************************************************
1067 * This function can only be called if i_usage == 0. It will make a call
1068 * to the module's inner DeactivateModule() symbol, and then unload it
1069 * from memory. A call to module_Need() will automagically load it again.
1070 *****************************************************************************/
1071 static int HideModule( module_t * p_module )
1073 if( p_module->b_builtin )
1075 /* A builtin module should never be hidden. */
1076 msg_Err( p_module, "trying to hide builtin module `%s'",
1077 p_module->psz_object_name );
1081 if( p_module->i_usage >= 1 )
1083 msg_Err( p_module, "trying to hide module `%s' which is still "
1084 "in use", p_module->psz_object_name );
1088 if( p_module->i_usage <= -1 )
1090 msg_Err( p_module, "trying to hide module `%s' which is already "
1091 "hidden", p_module->psz_object_name );
1095 /* Deactivate the module : free the capability structure, etc. */
1096 if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1098 /* We couldn't call DeactivateModule() -- looks nasty, but
1099 * we can't do much about it. Just try to unload module anyway. */
1100 module_unload( p_module->is.plugin.handle );
1101 p_module->i_usage = -1;
1105 /* Everything worked fine, we can safely unload the module. */
1106 module_unload( p_module->is.plugin.handle );
1107 p_module->i_usage = -1;
1112 /*****************************************************************************
1113 * CallSymbol: calls a module symbol.
1114 *****************************************************************************
1115 * This function calls a symbol given its name and a module structure. The
1116 * symbol MUST refer to a function returning int and taking a module_t* as
1118 *****************************************************************************/
1119 static int CallSymbol( module_t * p_module, char * psz_name )
1121 int (* pf_symbol) ( module_t * p_module );
1123 /* Try to resolve the symbol */
1124 pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1126 if( pf_symbol == NULL )
1128 char psz_buffer[256];
1130 /* We couldn't load the symbol */
1131 msg_Warn( p_module, "cannot find symbol %s in module %s (%s)",
1132 psz_name, p_module->is.plugin.psz_filename,
1133 module_error( psz_buffer ) );
1137 /* We can now try to call the symbol */
1138 if( pf_symbol( p_module ) != 0 )
1140 /* With a well-written module we shouldn't have to print an
1141 * additional error message here, but just make sure. */
1142 msg_Err( p_module, "failed calling symbol %s in module %s",
1143 psz_name, p_module->is.plugin.psz_filename );
1147 /* Everything worked fine, we can return */
1150 #endif /* HAVE_DYNAMIC_PLUGINS */