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