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