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