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