]> git.sesse.net Git - vlc/blob - src/misc/modules.c
This is the first part of the new configuration architecture for vlc.
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Built-in and plugin modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: modules.c,v 1.54 2002/02/24 20:51:10 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
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
30 #endif
31
32 #include <stdlib.h>                                      /* free(), strtol() */
33 #include <stdio.h>                                              /* sprintf() */
34 #include <string.h>                                              /* strdup() */
35
36 #include <videolan/vlc.h>
37
38 #if !defined( _MSC_VER )
39 #include <dirent.h>
40 #endif
41
42 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
43 #   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
44 #   define HAVE_DYNAMIC_PLUGINS
45 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
46 #   include <image.h>
47 #   define HAVE_DYNAMIC_PLUGINS
48 #elif defined(WIN32) && defined( __MINGW32__ )
49 #   define HAVE_DYNAMIC_PLUGINS
50 #else
51 #   undef HAVE_DYNAMIC_PLUGINS
52 #endif
53
54
55 #include "netutils.h"
56
57 #include "interface.h"
58 #include "intf_playlist.h"
59 #include "intf_eject.h"
60
61 #include "stream_control.h"
62 #include "input_ext-intf.h"
63 #include "input_ext-dec.h"
64 #include "input_ext-plugins.h"
65
66 #include "video.h"
67 #include "video_output.h"
68
69 #include "audio_output.h"
70
71 #include "iso_lang.h"
72
73 #ifdef HAVE_DYNAMIC_PLUGINS
74 #   include "modules_plugin.h"
75 #endif
76 #include "modules_builtin.h"
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 #ifdef HAVE_DYNAMIC_PLUGINS
82 static void AllocateAllPlugins   ( void );
83 static int  AllocatePluginModule ( char * );
84 #endif
85 static int  AllocateBuiltinModule( int ( * ) ( module_t * ),
86                                    int ( * ) ( module_t * ),
87                                    int ( * ) ( module_t * ) );
88 static int  DeleteModule ( module_t * );
89 static int  LockModule   ( module_t * );
90 static int  UnlockModule ( module_t * );
91 #ifdef HAVE_DYNAMIC_PLUGINS
92 static int  HideModule   ( module_t * );
93 static int  CallSymbol   ( module_t *, char * );
94 #endif
95
96 #ifdef HAVE_DYNAMIC_PLUGINS
97 static module_symbols_t symbols;
98 #endif
99
100 /*****************************************************************************
101  * module_InitBank: create the module bank.
102  *****************************************************************************
103  * This function creates a module bank structure which will be filled later
104  * on with all the modules found.
105  *****************************************************************************/
106 void module_InitBank( void )
107 {
108     p_module_bank->first = NULL;
109     p_module_bank->i_count = 0;
110     vlc_mutex_init( &p_module_bank->lock );
111
112     /*
113      * Store the symbols to be exported
114      */
115 #ifdef HAVE_DYNAMIC_PLUGINS
116     STORE_SYMBOLS( &symbols );
117 #endif
118
119     return;
120 }
121
122 /*****************************************************************************
123  * module_LoadMain: load the main program info into the module bank.
124  *****************************************************************************
125  * This function fills the module bank structure with the main module infos.
126  * This is very useful as it will allow us to consider the main program just
127  * as another module, and for instance the configuration options of main will
128  * be available in the module bank structure just as for every other module.
129  *****************************************************************************/
130 void module_LoadMain( void )
131 {
132     AllocateBuiltinModule( InitModule__MODULE_main,
133                            ActivateModule__MODULE_main,
134                            DeactivateModule__MODULE_main );
135 }
136
137 /*****************************************************************************
138  * module_LoadBuiltins: load all modules which we built with.
139  *****************************************************************************
140  * This function fills the module bank structure with the built-in modules.
141  *****************************************************************************/
142 void module_LoadBuiltins( void )
143 {
144     intf_WarnMsg( 2, "module: checking built-in modules" );
145     ALLOCATE_ALL_BUILTINS();
146 }
147
148 /*****************************************************************************
149  * module_LoadPlugins: load all plugin modules we can find.
150  *****************************************************************************
151  * This function fills the module bank structure with the plugin modules.
152  *****************************************************************************/
153 void module_LoadPlugins( void )
154 {
155 #ifdef HAVE_DYNAMIC_PLUGINS
156     intf_WarnMsg( 2, "module: checking plugin modules" );
157     AllocateAllPlugins();
158 #endif
159 }
160
161 /*****************************************************************************
162  * module_EndBank: empty the module bank.
163  *****************************************************************************
164  * This function unloads all unused plugin modules and empties the module
165  * bank in case of success.
166  *****************************************************************************/
167 void module_EndBank( void )
168 {
169     module_t * p_next;
170
171     while( p_module_bank->first != NULL )
172     {
173         if( DeleteModule( p_module_bank->first ) )
174         {
175             /* Module deletion failed */
176             intf_ErrMsg( "module error: `%s' can't be removed, trying harder",
177                          p_module_bank->first->psz_name );
178
179             /* We just free the module by hand. Niahahahahaha. */
180             p_next = p_module_bank->first->next;
181             free(p_module_bank->first);
182             p_module_bank->first = p_next;
183         }
184     }
185
186     /* Destroy the lock */
187     vlc_mutex_destroy( &p_module_bank->lock );
188
189     return;
190 }
191
192 /*****************************************************************************
193  * module_ResetBank: reset the module bank.
194  *****************************************************************************
195  * This function resets the module bank by unloading all unused plugin
196  * modules.
197  *****************************************************************************/
198 void module_ResetBank( void )
199 {
200     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
201     return;
202 }
203
204 /*****************************************************************************
205  * module_ManageBank: manage the module bank.
206  *****************************************************************************
207  * This function parses the module bank and hides modules that have been
208  * unused for a while.
209  *****************************************************************************/
210 void module_ManageBank( void )
211 {
212 #ifdef HAVE_DYNAMIC_PLUGINS
213     module_t * p_module;
214
215     /* We take the global lock */
216     vlc_mutex_lock( &p_module_bank->lock );
217
218     /* Parse the module list to see if any modules need to be unloaded */
219     for( p_module = p_module_bank->first ;
220          p_module != NULL ;
221          p_module = p_module->next )
222     {
223         /* If the module is unused and if it is a plugin module... */
224         if( p_module->i_usage == 0 && !p_module->b_builtin )
225         {
226             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
227             {
228                 p_module->i_unused_delay++;
229             }
230             else
231             {
232                 intf_WarnMsg( 3, "module: hiding unused plugin module `%s'",
233                               p_module->psz_name );
234                 HideModule( p_module );
235
236                 /* Break here, so that we only hide one module at a time */
237                 break;
238             }
239         }
240     }
241
242     /* We release the global lock */
243     vlc_mutex_unlock( &p_module_bank->lock );
244 #endif /* HAVE_DYNAMIC_PLUGINS */
245
246     return;
247 }
248
249 /*****************************************************************************
250  * module_Need: return the best module function, given a capability list.
251  *****************************************************************************
252  * This function returns the module that best fits the asked capabilities.
253  *****************************************************************************/
254 module_t * module_Need( int i_capability, char *psz_name, void *p_data )
255 {
256     typedef struct module_list_s
257     {
258         struct module_s *p_module;
259         struct module_list_s* p_next;
260     } module_list_t;
261     struct module_list_s *p_list, *p_first;
262
263     int i_ret, i_index = 0;
264
265     module_t *p_module;
266     char     *psz_realname = NULL;
267
268     /* We take the global lock */
269     vlc_mutex_lock( &p_module_bank->lock );
270
271     if( psz_name != NULL && *psz_name )
272     {
273         /* A module name was requested. */
274         psz_realname = strdup( psz_name );
275         if( psz_realname )
276         {
277             char *p;
278             p = strchr( psz_realname, ':' );
279             if( p )
280             {
281                 *p = '\0';
282             }
283             psz_name = psz_realname;
284         }
285     }
286
287     /* Sort the modules and test them */
288     p_list = malloc( p_module_bank->i_count * sizeof( module_list_t ) );
289     p_first = NULL;
290
291     /* Parse the module list for capabilities and probe each of them */
292     for( p_module = p_module_bank->first ;
293          p_module != NULL ;
294          p_module = p_module->next )
295     {
296         /* Test that this module can do everything we need */
297         if( !(p_module->i_capabilities & ( 1 << i_capability )) )
298         {
299             continue;
300         }
301
302         /* Test if we have the required CPU */
303         if( (p_module->i_cpu_capabilities & p_main->i_cpu_capabilities)
304               != p_module->i_cpu_capabilities )
305         {
306             continue;
307         }
308
309         /* Test if this plugin exports the required shortcut */
310         if( psz_name != NULL && *psz_name )
311         {
312             boolean_t b_ok = 0;
313             int i_dummy;
314
315             for( i_dummy = 0;
316                  !b_ok && p_module->pp_shortcuts[i_dummy];
317                  i_dummy++ )
318             {
319                 b_ok = !strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
320             }
321
322             if( !b_ok )
323             {
324                 continue;
325             }
326         }
327
328         /* Test if we requested a particular intf plugin */
329 #if 0
330         if( i_capability == MODULE_CAPABILITY_INTF
331              && p_module->psz_program != NULL
332              && strcmp( p_module->psz_program, p_main->psz_arg0 ) )
333         {
334             continue;
335         }
336 #endif
337
338         /* Store this new module */
339         p_list[ i_index ].p_module = p_module;
340
341         /* Lock it */
342         LockModule( p_module );
343
344         if( i_index == 0 )
345         {
346             p_list[ i_index ].p_next = NULL;
347             p_first = p_list;
348         }
349         else
350         {
351             /* Ok, so at school you learned that quicksort is quick, and
352              * bubble sort sucks raw eggs. But that's when dealing with
353              * thousands of items. Here we have barely 50. */
354             struct module_list_s *p_newlist = p_first;
355
356             if( p_first->p_module->pi_score[i_capability]
357                  < p_module->pi_score[i_capability] )
358             {
359                 p_list[ i_index ].p_next = p_first;
360                 p_first = &p_list[ i_index ];
361             }
362             else
363             {
364                 while( p_newlist->p_next != NULL
365                         && p_newlist->p_next->p_module->pi_score[i_capability]
366                             >= p_module->pi_score[i_capability] )
367                 {
368                     p_newlist = p_newlist->p_next;
369                 }
370
371                 p_list[ i_index ].p_next = p_newlist->p_next;
372                 p_newlist->p_next = &p_list[ i_index ];
373             }
374         }
375
376         i_index++;
377     }
378
379     /* We can release the global lock, module refcount were incremented */
380     vlc_mutex_unlock( &p_module_bank->lock );
381
382     /* Parse the linked list and use the first successful module */
383     while( p_first != NULL )
384     {
385         /* Test the requested capability */
386         switch( i_capability )
387         {
388             case MODULE_CAPABILITY_INPUT:
389                 i_ret = p_first->p_module->p_functions->input.functions.
390                               input.pf_probe( (input_thread_t *)p_data );
391                 break;
392
393             case MODULE_CAPABILITY_DECODER:
394                 i_ret = p_first->p_module->p_functions->dec.functions.
395                               dec.pf_probe( (u8 *)p_data );
396                 break;
397
398             case MODULE_CAPABILITY_INTF:
399                 i_ret = p_first->p_module->p_functions->intf.functions.
400                               intf.pf_open( (intf_thread_t *)p_data );
401                 break;
402
403             case MODULE_CAPABILITY_AOUT:
404                 i_ret = p_first->p_module->p_functions->aout.functions.
405                               aout.pf_open( (aout_thread_t *)p_data );
406                 break;
407
408             case MODULE_CAPABILITY_VOUT:
409                 i_ret = p_first->p_module->p_functions->vout.functions.
410                               vout.pf_create( (vout_thread_t *)p_data );
411                 break;
412
413             case MODULE_CAPABILITY_CHROMA:
414                 i_ret = p_first->p_module->p_functions->chroma.functions.
415                               chroma.pf_init( (vout_thread_t *)p_data );
416                 break;
417
418             case MODULE_CAPABILITY_IDCT:
419             case MODULE_CAPABILITY_IMDCT:
420             case MODULE_CAPABILITY_MOTION:
421             case MODULE_CAPABILITY_DOWNMIX:
422             case MODULE_CAPABILITY_MEMCPY:
423                 /* This one always works */
424                 i_ret = 0;
425                 break;
426
427             default:
428                 intf_ErrMsg( "module error: unknown module type %i",
429                              i_capability );
430                 i_ret = -1;
431                 break;
432         }
433
434         /* If the high score was broken, we have a new champion */
435         if( i_ret == 0 )
436         {
437             break;
438         }
439         else
440         {
441             UnlockModule( p_first->p_module );
442         }
443
444         p_first = p_first->p_next;
445     }
446
447     /* Store the locked module value */
448     if( p_first != NULL )
449     {
450         p_module = p_first->p_module;
451         p_first = p_first->p_next;
452     }
453     else
454     {
455         p_module = NULL;
456     }
457
458     /* Unlock the remaining modules */
459     while( p_first != NULL )
460     {
461         UnlockModule( p_first->p_module );
462         p_first = p_first->p_next;
463     }
464
465     free( p_list );
466
467     if( p_module != NULL )
468     {
469         intf_WarnMsg( 1, "module: locking %s module `%s'",
470                          GetCapabilityName( i_capability ),
471                          p_module->psz_name );
472     }
473     else if( psz_name != NULL && *psz_name )
474     {
475         intf_ErrMsg( "module error: requested %s module `%s' unavailable",
476                      GetCapabilityName( i_capability ), psz_name );
477     }
478
479     if( psz_realname )
480     {
481         free( psz_realname );
482     }
483
484     /* Don't forget that the module is still locked */
485     return( p_module );
486 }
487
488 /*****************************************************************************
489  * module_Unneed: decrease the usage count of a module.
490  *****************************************************************************
491  * This function must be called by the thread that called module_Need, to
492  * decrease the reference count and allow for hiding of modules.
493  *****************************************************************************/
494 void module_Unneed( module_t * p_module )
495 {
496     /* We take the global lock */
497     vlc_mutex_lock( &p_module_bank->lock );
498
499     /* Just unlock the module - we can't do anything if it fails,
500      * so there is no need to check the return value. */
501     UnlockModule( p_module );
502
503     intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
504
505     /* We release the global lock */
506     vlc_mutex_unlock( &p_module_bank->lock );
507
508     return;
509 }
510
511 /*****************************************************************************
512  * Following functions are local.
513  *****************************************************************************/
514
515 /*****************************************************************************
516  * AllocateAllPlugins: load all plugin modules we can find.
517  *****************************************************************************/
518 #ifdef HAVE_DYNAMIC_PLUGINS
519 static void AllocateAllPlugins( void )
520 {
521     static char * path[] = { ".", "plugins", PLUGIN_PATH, NULL, NULL };
522
523     char **         ppsz_path = path;
524     char *          psz_fullpath;
525     char *          psz_file;
526 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
527     char *          psz_vlcpath = system_GetProgramPath();
528     int             i_vlclen = strlen( psz_vlcpath );
529     boolean_t       b_notinroot;
530 #endif
531     DIR *           dir;
532     struct dirent * file;
533
534     for( ; *ppsz_path != NULL ; ppsz_path++ )
535     {
536         /* Store strlen(*ppsz_path) for later use. */
537         int i_dirlen = strlen( *ppsz_path );
538
539 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
540         b_notinroot = 0;
541         /* Under BeOS, we need to add beos_GetProgramPath() to access
542          * files under the current directory */
543         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
544         {
545             i_dirlen += i_vlclen + 2;
546             b_notinroot = 1;
547
548             psz_fullpath = malloc( i_dirlen );
549             if( psz_fullpath == NULL )
550             {
551                 continue;
552             }
553             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
554         }
555         else
556 #endif
557         {
558             psz_fullpath = *ppsz_path;
559         }
560
561         intf_WarnMsg( 1, "module: browsing `%s'", psz_fullpath );
562
563         if( (dir = opendir( psz_fullpath )) )
564         {
565             /* Parse the directory and try to load all files it contains. */
566             while( (file = readdir( dir )) )
567             {
568                 int i_filelen = strlen( file->d_name );
569
570                 /* We only load files ending with ".so" */
571                 if( i_filelen > 3
572                         && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
573                 {
574                     psz_file = malloc( i_dirlen + i_filelen + 2 );
575                     if( psz_file == NULL )
576                     {
577                         continue;
578                     }
579                     sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
580
581                     /* We created a nice filename -- now we just try to load
582                      * it as a plugin module. */
583                     AllocatePluginModule( psz_file );
584
585                     /* We don't care if the allocation succeeded */
586                     free( psz_file );
587                 }
588             }
589
590             /* Close the directory if successfully opened */
591             closedir( dir );
592         }
593
594 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
595         if( b_notinroot )
596         {
597             free( psz_fullpath );
598         }
599 #endif
600     }
601 }
602
603 /*****************************************************************************
604  * AllocatePluginModule: load a module into memory and initialize it.
605  *****************************************************************************
606  * This function loads a dynamically loadable module and allocates a structure
607  * for its information data. The module can then be handled by module_Need,
608  * module_Unneed and HideModule. It can be removed by DeleteModule.
609  *****************************************************************************/
610 static int AllocatePluginModule( char * psz_filename )
611 {
612     char **pp_shortcut;
613     module_t * p_module, * p_othermodule;
614     module_handle_t handle;
615
616     /* Try to dynamically load the module. */
617     if( module_load( psz_filename, &handle ) )
618     {
619         /* The plugin module couldn't be opened */
620         intf_WarnMsg( 1, "module warning: cannot open %s (%s)",
621                          psz_filename, module_error() );
622         return( -1 );
623     }
624
625     /* Now that we have successfully loaded the module, we can
626      * allocate a structure for it */ 
627     p_module = malloc( sizeof( module_t ) );
628     if( p_module == NULL )
629     {
630         intf_ErrMsg( "module error: can't allocate p_module" );
631         module_unload( handle );
632         return( -1 );
633     }
634
635     /* We need to fill these since they may be needed by CallSymbol() */
636     p_module->is.plugin.psz_filename = psz_filename;
637     p_module->is.plugin.handle = handle;
638     p_module->p_symbols = &symbols;
639
640     /* Initialize the module : fill p_module->psz_name, default config, etc. */
641     if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
642     {
643         /* We couldn't call InitModule() */
644         free( p_module );
645         module_unload( handle );
646         return( -1 );
647     }
648
649     /* Check that we don't already have a module with this name */
650     for( p_othermodule = p_module_bank->first ;
651          p_othermodule != NULL ;
652          p_othermodule = p_othermodule->next )
653     {
654         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
655         {
656             intf_WarnMsg( 5, "module warning: cannot load %s, a module named "
657                              "`%s' already exists",
658                              psz_filename, p_module->psz_name );
659             free( p_module );
660             module_unload( handle );
661             return( -1 );
662         }
663     }
664
665     /* Activate the module : fill the capability structure, etc. */
666     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
667     {
668         /* We couldn't call ActivateModule() */
669         free( p_module );
670         module_unload( handle );
671         return( -1 );
672     }
673
674     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
675     {
676         *pp_shortcut = strdup( *pp_shortcut );
677     }
678
679     /* We strdup() these entries so that they are still valid when the
680      * module is unloaded. */
681     p_module->is.plugin.psz_filename =
682             strdup( p_module->is.plugin.psz_filename );
683     p_module->psz_name = strdup( p_module->psz_name );
684     p_module->psz_longname = strdup( p_module->psz_longname );
685
686     if( p_module->is.plugin.psz_filename == NULL 
687             || p_module->psz_name == NULL
688             || p_module->psz_longname == NULL )
689     {
690         intf_ErrMsg( "module error: can't duplicate strings" );
691
692         free( p_module->is.plugin.psz_filename );
693         free( p_module->psz_name );
694         free( p_module->psz_longname );
695         free( p_module->psz_program );
696
697         free( p_module );
698         module_unload( handle );
699         return( -1 );
700     }
701
702     if( p_module->psz_program != NULL )
703     {
704         p_module->psz_program = strdup( p_module->psz_program );
705     }
706
707     /* Everything worked fine ! The module is ready to be added to the list. */
708     p_module->i_usage = 0;
709     p_module->i_unused_delay = 0;
710
711     p_module->b_builtin = 0;
712
713     /* Link module into the linked list */
714     if( p_module_bank->first != NULL )
715     {
716         p_module_bank->first->prev = p_module;
717     }
718     p_module->next = p_module_bank->first;
719     p_module->prev = NULL;
720     p_module_bank->first = p_module;
721     p_module_bank->i_count++;
722
723     /* Immediate message so that a slow module doesn't make the user wait */
724     intf_WarnMsg( 2, "module: new plugin module `%s', %s",
725                      p_module->psz_name, p_module->psz_longname );
726
727     return( 0 );
728 }
729 #endif /* HAVE_DYNAMIC_PLUGINS */
730
731 /*****************************************************************************
732  * AllocateBuiltinModule: initialize a built-in module.
733  *****************************************************************************
734  * This function registers a built-in module and allocates a structure
735  * for its information data. The module can then be handled by module_Need,
736  * module_Unneed and HideModule. It can be removed by DeleteModule.
737  *****************************************************************************/
738 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
739                                   int ( *pf_activate ) ( module_t * ),
740                                   int ( *pf_deactivate ) ( module_t * ) )
741 {
742     module_t * p_module, * p_othermodule;
743
744     /* Now that we have successfully loaded the module, we can
745      * allocate a structure for it */ 
746     p_module = malloc( sizeof( module_t ) );
747     if( p_module == NULL )
748     {
749         intf_ErrMsg( "module error: can't allocate p_module" );
750         return( -1 );
751     }
752
753     /* Initialize the module : fill p_module->psz_name, etc. */
754     if( pf_init( p_module ) != 0 )
755     {
756         /* With a well-written module we shouldn't have to print an
757          * additional error message here, but just make sure. */
758         intf_ErrMsg( "module error: failed calling init in builtin module" );
759         free( p_module );
760         return( -1 );
761     }
762
763     /* Check that we don't already have a module with this name */
764     for( p_othermodule = p_module_bank->first ;
765          p_othermodule != NULL ;
766          p_othermodule = p_othermodule->next )
767     {
768         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
769         {
770             intf_WarnMsg( 5, "module warning: cannot load builtin `%s', a "
771                              "module named `%s' already exists",
772                              p_module->psz_name, p_module->psz_name );
773             free( p_module );
774             return( -1 );
775         }
776     }
777
778     if( pf_activate( p_module ) != 0 )
779     {
780         /* With a well-written module we shouldn't have to print an
781          * additional error message here, but just make sure. */
782         intf_ErrMsg( "module error: failed calling activate "
783                      "in builtin module" );
784         free( p_module );
785         return( -1 );
786     }
787
788     /* Everything worked fine ! The module is ready to be added to the list. */
789     p_module->i_usage = 0;
790     p_module->i_unused_delay = 0;
791
792     p_module->b_builtin = 1;
793     p_module->is.builtin.pf_deactivate = pf_deactivate;
794
795     /* Link module into the linked list */
796     if( p_module_bank->first != NULL )
797     {
798         p_module_bank->first->prev = p_module;
799     }
800     p_module->next = p_module_bank->first;
801     p_module->prev = NULL;
802     p_module_bank->first = p_module;
803     p_module_bank->i_count++;
804
805     /* Immediate message so that a slow module doesn't make the user wait */
806     intf_WarnMsg( 2, "module: new builtin module `%s', %s",
807                      p_module->psz_name, p_module->psz_longname );
808
809     return( 0 );
810 }
811
812 /*****************************************************************************
813  * DeleteModule: delete a module and its structure.
814  *****************************************************************************
815  * This function can only be called if i_usage <= 0.
816  *****************************************************************************/
817 static int DeleteModule( module_t * p_module )
818 {
819     /* If the module is not in use but is still in memory, we first have
820      * to hide it and remove it from memory before we can free the
821      * data structure. */
822     if( p_module->b_builtin )
823     {
824         if( p_module->i_usage != 0 )
825         {
826             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
827                          " usage %i", p_module->psz_name, p_module->i_usage );
828             return( -1 );
829         }
830         else
831         {
832             /* We deactivate the module now. */
833             p_module->is.builtin.pf_deactivate( p_module );
834         }
835     }
836 #ifdef HAVE_DYNAMIC_PLUGINS
837     else
838     {
839         if( p_module->i_usage >= 1 )
840         {
841             intf_ErrMsg( "module error: trying to free module `%s' which is"
842                          " still in use", p_module->psz_name );
843             return( -1 );
844         }
845
846         /* Two possibilities here: i_usage == -1 and the module is already
847          * unloaded, we can continue, or i_usage == 0, and we have to hide
848          * the module before going on. */
849         if( p_module->i_usage == 0 )
850         {
851             if( HideModule( p_module ) != 0 )
852             {
853                 return( -1 );
854             }
855         }
856     }
857 #endif
858
859     /* Unlink the module from the linked list. */
860     if( p_module->prev != NULL )
861     {
862         p_module->prev->next = p_module->next;
863     }
864     else
865     {
866         p_module_bank->first = p_module->next;
867     }
868
869     if( p_module->next != NULL )
870     {
871         p_module->next->prev = p_module->prev;
872     }
873
874     p_module_bank->i_count--;
875
876     /* We free the structures that we strdup()ed in Allocate*Module(). */
877 #ifdef HAVE_DYNAMIC_PLUGINS
878     if( !p_module->b_builtin )
879     {
880         char **pp_shortcut = p_module->pp_shortcuts;
881
882         for( ; *pp_shortcut ; pp_shortcut++ )
883         {
884             free( *pp_shortcut );
885         }
886
887         free( p_module->is.plugin.psz_filename );
888         free( p_module->psz_name );
889         free( p_module->psz_longname );
890         free( p_module->psz_program );
891     }
892 #endif
893
894     free( p_module );
895
896     return( 0 );
897 }
898
899 /*****************************************************************************
900  * LockModule: increase the usage count of a module and load it if needed.
901  *****************************************************************************
902  * This function has to be called before a thread starts using a module. If
903  * the module is already loaded, we just increase its usage count. If it isn't
904  * loaded, we have to dynamically open it and initialize it.
905  * If you successfully call LockModule() at any moment, be careful to call
906  * UnlockModule() when you don't need it anymore.
907  *****************************************************************************/
908 static int LockModule( module_t * p_module )
909 {
910     if( p_module->i_usage >= 0 )
911     {
912         /* This module is already loaded and activated, we can return */
913         p_module->i_usage++;
914         return( 0 );
915     }
916
917     if( p_module->b_builtin )
918     {
919         /* A built-in module should always have a refcount >= 0 ! */
920         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
921                      p_module->psz_name, p_module->i_usage );
922         return( -1 );
923     }
924
925 #ifdef HAVE_DYNAMIC_PLUGINS
926     if( p_module->i_usage != -1 )
927     {
928         /* This shouldn't happen. Ever. We have serious problems here. */
929         intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
930                      p_module->psz_name, p_module->i_usage );
931         return( -1 );
932     }
933
934     /* i_usage == -1, which means that the module isn't in memory */
935     if( module_load( p_module->is.plugin.psz_filename,
936                      &p_module->is.plugin.handle ) )
937     {
938         /* The plugin module couldn't be opened */
939         intf_ErrMsg( "module error: cannot open %s (%s)",
940                      p_module->is.plugin.psz_filename, module_error() );
941         return( -1 );
942     }
943
944     /* FIXME: what to do if the guy modified the plugin while it was
945      * unloaded ? It makes XMMS crash nastily, perhaps we should try
946      * to be a bit more clever here. */
947
948     /* Activate the module : fill the capability structure, etc. */
949     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
950     {
951         /* We couldn't call ActivateModule() -- looks nasty, but
952          * we can't do much about it. Just try to unload module. */
953         module_unload( p_module->is.plugin.handle );
954         p_module->i_usage = -1;
955         return( -1 );
956     }
957
958     /* Everything worked fine ! The module is ready to be used */
959     p_module->i_usage = 1;
960 #endif /* HAVE_DYNAMIC_PLUGINS */
961
962     return( 0 );
963 }
964
965 /*****************************************************************************
966  * UnlockModule: decrease the usage count of a module.
967  *****************************************************************************
968  * We decrease the usage count of a module so that we know when a module
969  * becomes unused and can be hidden.
970  *****************************************************************************/
971 static int UnlockModule( module_t * p_module )
972 {
973     if( p_module->i_usage <= 0 )
974     {
975         /* This shouldn't happen. Ever. We have serious problems here. */
976         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
977                      " which isn't even in use", p_module->psz_name );
978         return( -1 );
979     }
980
981     /* This module is still in use, we can return */
982     p_module->i_usage--;
983     p_module->i_unused_delay = 0;
984
985     return( 0 );
986 }
987
988 #ifdef HAVE_DYNAMIC_PLUGINS
989 /*****************************************************************************
990  * HideModule: remove a module from memory but keep its structure.
991  *****************************************************************************
992  * This function can only be called if i_usage == 0. It will make a call
993  * to the module's inner DeactivateModule() symbol, and then unload it
994  * from memory. A call to module_Need() will automagically load it again.
995  *****************************************************************************/
996 static int HideModule( module_t * p_module )
997 {
998     if( p_module->b_builtin )
999     {
1000         /* A built-in module should never be hidden. */
1001         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
1002                      p_module->psz_name );
1003         return( -1 );
1004     }
1005
1006     if( p_module->i_usage >= 1 )
1007     {
1008         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
1009                      " in use", p_module->psz_name );
1010         return( -1 );
1011     }
1012
1013     if( p_module->i_usage <= -1 )
1014     {
1015         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
1016                      " hidden", p_module->psz_name );
1017         return( -1 );
1018     }
1019
1020     /* Deactivate the module : free the capability structure, etc. */
1021     if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1022     {
1023         /* We couldn't call DeactivateModule() -- looks nasty, but
1024          * we can't do much about it. Just try to unload module anyway. */
1025         module_unload( p_module->is.plugin.handle );
1026         p_module->i_usage = -1;
1027         return( -1 );
1028     }
1029
1030     /* Everything worked fine, we can safely unload the module. */
1031     module_unload( p_module->is.plugin.handle );
1032     p_module->i_usage = -1;
1033
1034     return( 0 );
1035 }
1036
1037 /*****************************************************************************
1038  * CallSymbol: calls a module symbol.
1039  *****************************************************************************
1040  * This function calls a symbol given its name and a module structure. The
1041  * symbol MUST refer to a function returning int and taking a module_t* as
1042  * an argument.
1043  *****************************************************************************/
1044 static int CallSymbol( module_t * p_module, char * psz_name )
1045 {
1046     int (* pf_symbol) ( module_t * p_module );
1047
1048     /* Try to resolve the symbol */
1049     pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1050
1051     if( pf_symbol == NULL )
1052     {
1053         /* We couldn't load the symbol */
1054         intf_WarnMsg( 1, "module warning: "
1055                          "cannot find symbol %s in module %s (%s)",
1056                          psz_name, p_module->is.plugin.psz_filename,
1057                          module_error() );
1058         return( -1 );
1059     }
1060
1061     /* We can now try to call the symbol */
1062     if( pf_symbol( p_module ) != 0 )
1063     {
1064         /* With a well-written module we shouldn't have to print an
1065          * additional error message here, but just make sure. */
1066         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
1067                      psz_name, p_module->is.plugin.psz_filename );
1068         return( -1 );
1069     }
1070
1071     /* Everything worked fine, we can return */
1072     return( 0 );
1073 }
1074 #endif /* HAVE_DYNAMIC_PLUGINS */
1075