]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* Fixed the BeOS compile typo.
[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.31 2001/05/30 17:03:12 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 #include "defs.h"
25
26 #include "config.h"
27
28 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
29  * is set to 64. Don't try to be cleverer. */
30 #ifdef _FILE_OFFSET_BITS
31 #undef _FILE_OFFSET_BITS
32 #endif
33
34 #include <stdlib.h>                                      /* free(), strtol() */
35 #include <stdio.h>                                              /* sprintf() */
36 #include <string.h>                                              /* strdup() */
37 #include <dirent.h>
38
39 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
40 #   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
41 #   define HAVE_DYNAMIC_PLUGINS
42 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
43 #   include <image.h>
44 #   define HAVE_DYNAMIC_PLUGINS
45 #else
46 #   undef HAVE_DYNAMIC_PLUGINS
47 #endif
48
49 #ifdef SYS_BEOS
50 #   include "beos_specific.h"
51 #endif
52
53 #ifdef SYS_DARWIN1_3
54 #   include "darwin_specific.h"
55 #endif
56
57 #include "common.h"
58 #include "threads.h"
59 #include "mtime.h"
60 #include "tests.h"
61 #include "netutils.h"
62 #include "modules.h"
63
64 #include "stream_control.h"
65 #include "input_ext-intf.h"
66
67 #include "video.h"
68 #include "video_output.h"
69
70 #include "audio_output.h"
71
72 #include "interface.h"
73 #include "intf_msg.h"
74 #include "intf_playlist.h"
75
76 #ifdef HAVE_DYNAMIC_PLUGINS
77 #   include "modules_core.h"
78 #endif
79 #include "modules_builtin.h"
80 #include "modules_export.h"
81
82 #include "main.h"
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 #ifdef HAVE_DYNAMIC_PLUGINS
88 static int AllocatePluginModule ( char * );
89 #endif
90 static int AllocateBuiltinModule( int ( * ) ( module_t * ),
91                                   int ( * ) ( module_t * ),
92                                   int ( * ) ( module_t * ) );
93 static int DeleteModule ( module_t * );
94 static int LockModule   ( module_t * );
95 static int UnlockModule ( module_t * );
96 #ifdef HAVE_DYNAMIC_PLUGINS
97 static int HideModule   ( module_t * );
98 static int CallSymbol   ( module_t *, char * );
99 #endif
100
101 static module_symbols_t symbols;
102
103 /*****************************************************************************
104  * module_InitBank: create the module bank.
105  *****************************************************************************
106  * This function creates a module bank structure and fills it with the
107  * built-in modules, as well as all the plugin modules it can find.
108  *****************************************************************************/
109 void module_InitBank( void )
110 {
111 #ifdef HAVE_DYNAMIC_PLUGINS
112     static char * path[] = { ".", "lib", PLUGIN_PATH, NULL, NULL };
113
114     char **         ppsz_path = path;
115     char *          psz_fullpath;
116     char *          psz_file;
117 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
118     char *          psz_vlcpath = system_GetProgramPath();
119     int             i_vlclen = strlen( psz_vlcpath );
120     boolean_t       b_notinroot;
121 #endif
122     DIR *           dir;
123     struct dirent * file;
124 #endif /* HAVE_DYNAMIC_PLUGINS */
125
126     p_module_bank->first = NULL;
127     vlc_mutex_init( &p_module_bank->lock );
128
129     /*
130      * Store the symbols to be exported
131      */
132     STORE_SYMBOLS( &symbols );
133
134     /*
135      * Check all the built-in modules
136      */
137     intf_WarnMsg( 2, "module: checking built-in modules" );
138
139     ALLOCATE_ALL_BUILTINS();
140
141     /*
142      * Check all the plugin modules we can find
143      */
144 #ifdef HAVE_DYNAMIC_PLUGINS
145     intf_WarnMsg( 2, "module: checking plugin modules" );
146
147     for( ; *ppsz_path != NULL ; ppsz_path++ )
148     {
149         /* Store strlen(*ppsz_path) for later use. */
150         int i_dirlen = strlen( *ppsz_path );
151
152 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
153         b_notinroot = 0;
154         /* Under BeOS, we need to add beos_GetProgramPath() to access
155          * files under the current directory */
156         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
157         {
158             i_dirlen += i_vlclen + 2;
159             b_notinroot = 1;
160
161             psz_fullpath = malloc( i_dirlen );
162             if( psz_fullpath == NULL )
163             {
164                 continue;
165             }
166             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
167         }
168         else
169 #endif
170         {
171             psz_fullpath = *ppsz_path;
172         }
173
174         intf_WarnMsgImm( 1, "module: browsing `%s'", psz_fullpath );
175
176         if( (dir = opendir( psz_fullpath )) )
177         {
178             /* Parse the directory and try to load all files it contains. */
179             while( (file = readdir( dir )) )
180             {
181                 int i_filelen = strlen( file->d_name );
182
183                 /* We only load files ending with ".so" */
184                 if( i_filelen > 3
185                         && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
186                 {
187                     psz_file = malloc( i_dirlen + i_filelen + 2 );
188                     if( psz_file == NULL )
189                     {
190                         continue;
191                     }
192                     sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
193
194                     /* We created a nice filename -- now we just try to load
195                      * it as a plugin module. */
196                     AllocatePluginModule( psz_file );
197
198                     /* We don't care if the allocation succeeded */
199                     free( psz_file );
200                 }
201             }
202
203             /* Close the directory if successfully opened */
204             closedir( dir );
205         }
206
207 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
208         if( b_notinroot )
209         {
210             free( psz_fullpath );
211         }
212 #endif
213     }
214 #endif /* HAVE_DYNAMIC_PLUGINS */
215
216     intf_WarnMsg( 3, "module: module bank initialized" );
217
218     return;
219 }
220
221 /*****************************************************************************
222  * module_EndBank: empty the module bank.
223  *****************************************************************************
224  * This function unloads all unused plugin modules and empties the module
225  * bank in case of success.
226  *****************************************************************************/
227 void module_EndBank( void )
228 {
229     module_t * p_next;
230
231     while( p_module_bank->first != NULL )
232     {
233         if( DeleteModule( p_module_bank->first ) )
234         {
235             /* Module deletion failed */
236             intf_ErrMsg( "module error: `%s' can't be removed. trying harder.",
237                          p_module_bank->first->psz_name );
238
239             /* We just free the module by hand. Niahahahahaha. */
240             p_next = p_module_bank->first->next;
241             free(p_module_bank->first);
242             p_module_bank->first = p_next;
243         }
244     }
245
246     /* Destroy the lock */
247     vlc_mutex_destroy( &p_module_bank->lock );
248
249     return;
250 }
251
252 /*****************************************************************************
253  * module_ResetBank: reset the module bank.
254  *****************************************************************************
255  * This function resets the module bank by unloading all unused plugin
256  * modules.
257  *****************************************************************************/
258 void module_ResetBank( void )
259 {
260     intf_ErrMsg( "FIXME: module_ResetBank unimplemented" );
261     return;
262 }
263
264 /*****************************************************************************
265  * module_ManageBank: manage the module bank.
266  *****************************************************************************
267  * This function parses the module bank and hides modules that have been
268  * unused for a while.
269  *****************************************************************************/
270 void module_ManageBank( void )
271 {
272 #ifdef HAVE_DYNAMIC_PLUGINS
273     module_t * p_module;
274
275     /* We take the global lock */
276     vlc_mutex_lock( &p_module_bank->lock );
277
278     /* Parse the module list to see if any modules need to be unloaded */
279     for( p_module = p_module_bank->first ;
280          p_module != NULL ;
281          p_module = p_module->next )
282     {
283         /* If the module is unused and if it is a plugin module... */
284         if( p_module->i_usage == 0 && !p_module->b_builtin )
285         {
286             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
287             {
288                 p_module->i_unused_delay++;
289             }
290             else
291             {
292                 intf_WarnMsg( 1, "module: hiding unused plugin module `%s'",
293                               p_module->psz_name );
294                 HideModule( p_module );
295
296                 /* Break here, so that we only hide one module at a time */
297                 break;
298             }
299         }
300     }
301
302     /* We release the global lock */
303     vlc_mutex_unlock( &p_module_bank->lock );
304 #endif /* HAVE_DYNAMIC_PLUGINS */
305
306     return;
307 }
308
309 /*****************************************************************************
310  * module_Need: return the best module function, given a capability list.
311  *****************************************************************************
312  * This function returns the module that best fits the asked capabilities.
313  *****************************************************************************/
314 module_t * module_Need( int i_capabilities, void *p_data )
315 {
316     module_t * p_module;
317     module_t * p_bestmodule = NULL;
318     int i_score, i_totalscore, i_bestscore = 0;
319     int i_index;
320
321     /* We take the global lock */
322     vlc_mutex_lock( &p_module_bank->lock );
323
324     /* Parse the module list for capabilities and probe each of them */
325     for( p_module = p_module_bank->first ;
326          p_module != NULL ;
327          p_module = p_module->next )
328     {
329         /* Test that this module can do everything we need */
330         if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
331         {
332             i_totalscore = 0;
333
334             LockModule( p_module );
335
336             /* Parse all the requested capabilities and test them */
337             for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
338             {
339                 if( ( (1 << i_index) & i_capabilities ) )
340                 {
341                     i_score = ( (function_list_t *)p_module->p_functions)
342                                                   [i_index].pf_probe( p_data );
343
344                     if( i_score )
345                     {
346                         i_totalscore += i_score;
347                     }
348                     else
349                     {
350                         break;
351                     }
352                 }
353             }
354
355             /* If the high score was broken, we have a new champion */
356             if( i_totalscore > i_bestscore )
357             {
358                 /* Keep the current module locked, but release the previous */
359                 if( p_bestmodule != NULL )
360                 {
361                     UnlockModule( p_bestmodule );
362                 }
363
364                 /* This is the new best module */
365                 i_bestscore = i_totalscore;
366                 p_bestmodule = p_module;
367             }
368             else
369             {
370                 /* This module wasn't interesting, unlock it and forget it */
371                 UnlockModule( p_module );
372             }
373         }
374     }
375
376     /* We can release the global lock, module refcount was incremented */
377     vlc_mutex_unlock( &p_module_bank->lock );
378
379     if( p_bestmodule != NULL )
380     {
381         intf_WarnMsg( 1, "module: locking module `%s'",
382                       p_bestmodule->psz_name );
383     }
384
385     /* Don't forget that the module is still locked if bestmodule != NULL */
386     return( p_bestmodule );
387 }
388
389 /*****************************************************************************
390  * module_Unneed: decrease the usage count of a module.
391  *****************************************************************************
392  * This function must be called by the thread that called module_Need, to
393  * decrease the reference count and allow for hiding of modules.
394  *****************************************************************************/
395 void module_Unneed( module_t * p_module )
396 {
397     /* We take the global lock */
398     vlc_mutex_lock( &p_module_bank->lock );
399
400     /* Just unlock the module - we can't do anything if it fails,
401      * so there is no need to check the return value. */
402     UnlockModule( p_module );
403
404     intf_WarnMsg( 1, "module: unlocking module `%s'", p_module->psz_name );
405
406     /* We release the global lock */
407     vlc_mutex_unlock( &p_module_bank->lock );
408
409     return;
410 }
411
412 /*****************************************************************************
413  * Following functions are local.
414  *****************************************************************************/
415
416 #ifdef HAVE_DYNAMIC_PLUGINS
417 /*****************************************************************************
418  * AllocatePluginModule: load a module into memory and initialize it.
419  *****************************************************************************
420  * This function loads a dynamically loadable module and allocates a structure
421  * for its information data. The module can then be handled by module_Need,
422  * module_Unneed and HideModule. It can be removed by DeleteModule.
423  *****************************************************************************/
424 static int AllocatePluginModule( char * psz_filename )
425 {
426     module_t * p_module, * p_othermodule;
427     module_handle_t handle;
428
429     /* Try to dynamically load the module. */
430     if( module_load( psz_filename, &handle ) )
431     {
432         /* The plugin module couldn't be opened */
433         intf_WarnMsgImm( 1, "module warning: cannot open %s (%s)",
434                          psz_filename, module_error() );
435         return( -1 );
436     }
437
438     /* Now that we have successfully loaded the module, we can
439      * allocate a structure for it */ 
440     p_module = malloc( sizeof( module_t ) );
441     if( p_module == NULL )
442     {
443         intf_ErrMsg( "module error: can't allocate p_module" );
444         module_unload( handle );
445         return( -1 );
446     }
447
448     /* We need to fill these since they may be needed by CallSymbol() */
449     p_module->is.plugin.psz_filename = psz_filename;
450     p_module->is.plugin.handle = handle;
451     p_module->p_symbols = &symbols;
452
453     /* Initialize the module : fill p_module->psz_name, etc. */
454     if( CallSymbol( p_module, "InitModule" ) != 0 )
455     {
456         /* We couldn't call InitModule() */
457         free( p_module );
458         module_unload( handle );
459         return( -1 );
460     }
461
462     /* Check that version numbers match */
463     if( strcmp( VERSION, p_module->psz_version ) )
464     {
465         free( p_module );
466         module_unload( handle );
467         return( -1 );
468     }
469
470     /* Check that we don't already have a module with this name */
471     for( p_othermodule = p_module_bank->first ;
472          p_othermodule != NULL ;
473          p_othermodule = p_othermodule->next )
474     {
475         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
476         {
477             free( p_module );
478             module_unload( handle );
479             return( -1 );
480         }
481     }
482
483     /* Activate the module : fill the capability structure, etc. */
484     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
485     {
486         /* We couldn't call ActivateModule() */
487         free( p_module );
488         module_unload( handle );
489         return( -1 );
490     }
491
492     /* We strdup() these entries so that they are still valid when the
493      * module is unloaded. */
494     p_module->is.plugin.psz_filename =
495             strdup( p_module->is.plugin.psz_filename );
496     p_module->psz_name = strdup( p_module->psz_name );
497     p_module->psz_longname = strdup( p_module->psz_longname );
498     p_module->psz_version = strdup( p_module->psz_version );
499     if( p_module->is.plugin.psz_filename == NULL 
500             || p_module->psz_name == NULL
501             || p_module->psz_longname == NULL
502             || p_module->psz_version == NULL )
503     {
504         intf_ErrMsg( "module error: can't duplicate strings" );
505         free( p_module->is.plugin.psz_filename );
506         free( p_module->psz_name );
507         free( p_module->psz_longname );
508         free( p_module->psz_version );
509         free( p_module );
510         module_unload( handle );
511         return( -1 );
512     }
513
514     /* Everything worked fine ! The module is ready to be added to the list. */
515     p_module->i_usage = 0;
516     p_module->i_unused_delay = 0;
517
518     p_module->b_builtin = 0;
519
520     /* Link module into the linked list */
521     if( p_module_bank->first != NULL )
522     {
523         p_module_bank->first->prev = p_module;
524     }
525     p_module->next = p_module_bank->first;
526     p_module->prev = NULL;
527     p_module_bank->first = p_module;
528
529     /* Immediate message so that a slow module doesn't make the user wait */
530     intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
531                      p_module->psz_name, p_module->psz_longname );
532
533     return( 0 );
534 }
535 #endif /* HAVE_DYNAMIC_PLUGINS */
536
537 /*****************************************************************************
538  * AllocateBuiltinModule: initialize a built-in module.
539  *****************************************************************************
540  * This function registers a built-in module and allocates a structure
541  * for its information data. The module can then be handled by module_Need,
542  * module_Unneed and HideModule. It can be removed by DeleteModule.
543  *****************************************************************************/
544 static int AllocateBuiltinModule( int ( *pf_init ) ( module_t * ),
545                                   int ( *pf_activate ) ( module_t * ),
546                                   int ( *pf_deactivate ) ( module_t * ) )
547 {
548     module_t * p_module, * p_othermodule;
549
550     /* Now that we have successfully loaded the module, we can
551      * allocate a structure for it */ 
552     p_module = malloc( sizeof( module_t ) );
553     if( p_module == NULL )
554     {
555         intf_ErrMsg( "module error: can't allocate p_module" );
556         return( -1 );
557     }
558
559     /* Initialize the module : fill p_module->psz_name, etc. */
560     if( pf_init( p_module ) != 0 )
561     {
562         /* With a well-written module we shouldn't have to print an
563          * additional error message here, but just make sure. */
564         intf_ErrMsg( "module error: failed calling init in builtin module" );
565         free( p_module );
566         return( -1 );
567     }
568
569     /* Check that version numbers match */
570     if( strcmp( VERSION, p_module->psz_version ) )
571     {
572         free( p_module );
573         return( -1 );
574     }
575
576     /* Check that we don't already have a module with this name */
577     for( p_othermodule = p_module_bank->first ;
578          p_othermodule != NULL ;
579          p_othermodule = p_othermodule->next )
580     {
581         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
582         {
583             free( p_module );
584             return( -1 );
585         }
586     }
587
588     if( pf_activate( p_module ) != 0 )
589     {
590         /* With a well-written module we shouldn't have to print an
591          * additional error message here, but just make sure. */
592         intf_ErrMsg( "module error: failed calling activate "
593                      "in builtin module" );
594         free( p_module );
595         return( -1 );
596     }
597
598     /* We strdup() these entries so that they are still valid when the
599      * module is unloaded. */
600     p_module->psz_name = strdup( p_module->psz_name );
601     p_module->psz_longname = strdup( p_module->psz_longname );
602     p_module->psz_version = strdup( p_module->psz_version );
603     if( p_module->psz_name == NULL 
604             || p_module->psz_longname == NULL
605             || p_module->psz_version == NULL )
606     {
607         intf_ErrMsg( "module error: can't duplicate strings" );
608         free( p_module->psz_name );
609         free( p_module->psz_longname );
610         free( p_module->psz_version );
611         free( p_module );
612         return( -1 );
613     }
614
615     /* Everything worked fine ! The module is ready to be added to the list. */
616     p_module->i_usage = 0;
617     p_module->i_unused_delay = 0;
618
619     p_module->b_builtin = 1;
620     p_module->is.builtin.pf_deactivate = pf_deactivate;
621
622     /* Link module into the linked list */
623     if( p_module_bank->first != NULL )
624     {
625         p_module_bank->first->prev = p_module;
626     }
627     p_module->next = p_module_bank->first;
628     p_module->prev = NULL;
629     p_module_bank->first = p_module;
630
631     /* Immediate message so that a slow module doesn't make the user wait */
632     intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
633                      p_module->psz_name, p_module->psz_longname );
634
635     return( 0 );
636 }
637
638 /*****************************************************************************
639  * DeleteModule: delete a module and its structure.
640  *****************************************************************************
641  * This function can only be called if i_usage <= 0.
642  *****************************************************************************/
643 static int DeleteModule( module_t * p_module )
644 {
645     /* If the module is not in use but is still in memory, we first have
646      * to hide it and remove it from memory before we can free the
647      * data structure. */
648     if( p_module->b_builtin )
649     {
650         if( p_module->i_usage != 0 )
651         {
652             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
653                          " usage %i", p_module->psz_name, p_module->i_usage );
654             return( -1 );
655         }
656         else
657         {
658             /* We deactivate the module now. */
659             p_module->is.builtin.pf_deactivate( p_module );
660         }
661     }
662 #ifdef HAVE_DYNAMIC_PLUGINS
663     else
664     {
665         if( p_module->i_usage >= 1 )
666         {
667             intf_ErrMsg( "module error: trying to free module `%s' which is"
668                          " still in use", p_module->psz_name );
669             return( -1 );
670         }
671
672         /* Two possibilities here: i_usage == -1 and the module is already
673          * unloaded, we can continue, or i_usage == 0, and we have to hide
674          * the module before going on. */
675         if( p_module->i_usage == 0 )
676         {
677             if( HideModule( p_module ) != 0 )
678             {
679                 return( -1 );
680             }
681         }
682     }
683 #endif
684
685     /* Unlink the module from the linked list. */
686     if( p_module == p_module_bank->first )
687     {
688         p_module_bank->first = p_module->next;
689     }
690
691     if( p_module->prev != NULL )
692     {
693         p_module->prev->next = p_module->next;
694     }
695
696     if( p_module->next != NULL )
697     {
698         p_module->next->prev = p_module->prev;
699     }
700
701     /* We free the structures that we strdup()ed in Allocate*Module(). */
702 #ifdef HAVE_DYNAMIC_PLUGINS
703     if( !p_module->b_builtin )
704     {
705         free( p_module->is.plugin.psz_filename );
706     }
707 #endif
708     free( p_module->psz_name );
709     free( p_module->psz_longname );
710     free( p_module->psz_version );
711
712     free( p_module );
713
714     return( 0 );
715 }
716
717 /*****************************************************************************
718  * LockModule: increase the usage count of a module and load it if needed.
719  *****************************************************************************
720  * This function has to be called before a thread starts using a module. If
721  * the module is already loaded, we just increase its usage count. If it isn't
722  * loaded, we have to dynamically open it and initialize it.
723  * If you successfully call LockModule() at any moment, be careful to call
724  * UnlockModule() when you don't need it anymore.
725  *****************************************************************************/
726 static int LockModule( module_t * p_module )
727 {
728     if( p_module->i_usage >= 0 )
729     {
730         /* This module is already loaded and activated, we can return */
731         p_module->i_usage++;
732         return( 0 );
733     }
734
735     if( p_module->b_builtin )
736     {
737         /* A built-in module should always have a refcount >= 0 ! */
738         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
739                      p_module->psz_name, p_module->i_usage );
740         return( -1 );
741     }
742
743 #ifdef HAVE_DYNAMIC_PLUGINS
744     if( p_module->i_usage != -1 )
745     {
746         /* This shouldn't happen. Ever. We have serious problems here. */
747         intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
748                      p_module->psz_name, p_module->i_usage );
749         return( -1 );
750     }
751
752     /* i_usage == -1, which means that the module isn't in memory */
753     if( module_load( p_module->is.plugin.psz_filename,
754                      &p_module->is.plugin.handle ) )
755     {
756         /* The plugin module couldn't be opened */
757         intf_ErrMsg( "module error: cannot open %s (%s)",
758                      p_module->is.plugin.psz_filename, module_error() );
759         return( -1 );
760     }
761
762     /* FIXME: what to do if the guy modified the plugin while it was
763      * unloaded ? It makes XMMS crash nastily, perhaps we should try
764      * to be a bit more clever here. */
765
766     /* Activate the module : fill the capability structure, etc. */
767     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
768     {
769         /* We couldn't call ActivateModule() -- looks nasty, but
770          * we can't do much about it. Just try to unload module. */
771         module_unload( p_module->is.plugin.handle );
772         p_module->i_usage = -1;
773         return( -1 );
774     }
775
776     /* Everything worked fine ! The module is ready to be used */
777     p_module->i_usage = 1;
778 #endif /* HAVE_DYNAMIC_PLUGINS */
779
780     return( 0 );
781 }
782
783 /*****************************************************************************
784  * UnlockModule: decrease the usage count of a module.
785  *****************************************************************************
786  * We decrease the usage count of a module so that we know when a module
787  * becomes unused and can be hidden.
788  *****************************************************************************/
789 static int UnlockModule( module_t * p_module )
790 {
791     if( p_module->i_usage <= 0 )
792     {
793         /* This shouldn't happen. Ever. We have serious problems here. */
794         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
795                      " which isn't even in use", p_module->psz_name );
796         return( -1 );
797     }
798
799     /* This module is still in use, we can return */
800     p_module->i_usage--;
801     p_module->i_unused_delay = 0;
802
803     return( 0 );
804 }
805
806 #ifdef HAVE_DYNAMIC_PLUGINS
807 /*****************************************************************************
808  * HideModule: remove a module from memory but keep its structure.
809  *****************************************************************************
810  * This function can only be called if i_usage == 0. It will make a call
811  * to the module's inner DeactivateModule() symbol, and then unload it
812  * from memory. A call to module_Need() will automagically load it again.
813  *****************************************************************************/
814 static int HideModule( module_t * p_module )
815 {
816     if( p_module->b_builtin )
817     {
818         /* A built-in module should never be hidden. */
819         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
820                      p_module->psz_name );
821         return( -1 );
822     }
823
824     if( p_module->i_usage >= 1 )
825     {
826         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
827                      " in use", p_module->psz_name );
828         return( -1 );
829     }
830
831     if( p_module->i_usage <= -1 )
832     {
833         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
834                      " hidden", p_module->psz_name );
835         return( -1 );
836     }
837
838     /* Deactivate the module : free the capability structure, etc. */
839     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
840     {
841         /* We couldn't call DeactivateModule() -- looks nasty, but
842          * we can't do much about it. Just try to unload module anyway. */
843         module_unload( p_module->is.plugin.handle );
844         p_module->i_usage = -1;
845         return( -1 );
846     }
847
848     /* Everything worked fine, we can safely unload the module. */
849     module_unload( p_module->is.plugin.handle );
850     p_module->i_usage = -1;
851
852     return( 0 );
853 }
854
855 /*****************************************************************************
856  * CallSymbol: calls a module symbol.
857  *****************************************************************************
858  * This function calls a symbol given its name and a module structure. The
859  * symbol MUST refer to a function returning int and taking a module_t* as
860  * an argument.
861  *****************************************************************************/
862 static int CallSymbol( module_t * p_module, char * psz_name )
863 {
864     int (* pf_symbol) ( module_t * p_module );
865
866     /* Try to resolve the symbol */
867     pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
868
869     if( pf_symbol == NULL )
870     {
871         /* We couldn't load the symbol */
872         intf_WarnMsg( 1, "module warning: "
873                          "cannot find symbol %s in module %s (%s)",
874                          psz_name, p_module->is.plugin.psz_filename,
875                          module_error() );
876         return( -1 );
877     }
878
879     /* We can now try to call the symbol */
880     if( pf_symbol( p_module ) != 0 )
881     {
882         /* With a well-written module we shouldn't have to print an
883          * additional error message here, but just make sure. */
884         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
885                      psz_name, p_module->is.plugin.psz_filename );
886         return( -1 );
887     }
888
889     /* Everything worked fine, we can return */
890     return( 0 );
891 }
892 #endif /* HAVE_DYNAMIC_PLUGINS */
893