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