]> git.sesse.net Git - vlc/blob - src/misc/modules.c
dbaad51f8477feac41ca4832131b944a9a5d8685
[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.24 2001/04/15 10:54:46 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                 /* Break here, so that we only hide one module at a time */
281                 break;
282             }
283         }
284     }
285
286     /* We release the global lock */
287     vlc_mutex_unlock( &p_bank->lock );
288 #endif /* HAVE_DYNAMIC_PLUGINS */
289
290     return;
291 }
292
293 /*****************************************************************************
294  * module_Need: return the best module function, given a capability list.
295  *****************************************************************************
296  * This function returns the module that best fits the asked capabilities.
297  *****************************************************************************/
298 module_t * module_Need( module_bank_t *p_bank,
299                         int i_capabilities, void *p_data )
300 {
301     module_t * p_module;
302     module_t * p_bestmodule = NULL;
303     int i_score, i_totalscore, i_bestscore = 0;
304     int i_index;
305
306     /* We take the global lock */
307     vlc_mutex_lock( &p_bank->lock );
308
309     /* Parse the module list for capabilities and probe each of them */
310     for( p_module = p_bank->first ;
311          p_module != NULL ;
312          p_module = p_module->next )
313     {
314         /* Test that this module can do everything we need */
315         if( ( p_module->i_capabilities & i_capabilities ) == i_capabilities )
316         {
317             i_totalscore = 0;
318
319             LockModule( p_module );
320
321             /* Parse all the requested capabilities and test them */
322             for( i_index = 0 ; (1 << i_index) <= i_capabilities ; i_index++ )
323             {
324                 if( ( (1 << i_index) & i_capabilities ) )
325                 {
326                     i_score = ( (function_list_t *)p_module->p_functions)
327                                                   [i_index].pf_probe( p_data );
328
329                     if( i_score )
330                     {
331                         i_totalscore += i_score;
332                     }
333                     else
334                     {
335                         break;
336                     }
337                 }
338             }
339
340             /* If the high score was broken, we have a new champion */
341             if( i_totalscore > i_bestscore )
342             {
343                 /* Keep the current module locked, but release the previous */
344                 if( p_bestmodule != NULL )
345                 {
346                     UnlockModule( p_bestmodule );
347                 }
348
349                 /* This is the new best module */
350                 i_bestscore = i_totalscore;
351                 p_bestmodule = p_module;
352             }
353             else
354             {
355                 /* This module wasn't interesting, unlock it and forget it */
356                 UnlockModule( p_module );
357             }
358         }
359     }
360
361     /* We can release the global lock, module refcount was incremented */
362     vlc_mutex_unlock( &p_bank->lock );
363
364     if( p_bestmodule != NULL )
365     {
366         intf_WarnMsg( 3, "module: locking module `%s'",
367                       p_bestmodule->psz_name );
368     }
369
370     /* Don't forget that the module is still locked if bestmodule != NULL */
371     return( p_bestmodule );
372 }
373
374 /*****************************************************************************
375  * module_Unneed: decrease the usage count of a module.
376  *****************************************************************************
377  * This function must be called by the thread that called module_Need, to
378  * decrease the reference count and allow for hiding of modules.
379  *****************************************************************************/
380 void module_Unneed( module_bank_t * p_bank, module_t * p_module )
381 {
382     /* We take the global lock */
383     vlc_mutex_lock( &p_bank->lock );
384
385     /* Just unlock the module - we can't do anything if it fails,
386      * so there is no need to check the return value. */
387     UnlockModule( p_module );
388
389     intf_WarnMsg( 3, "module: unlocking module `%s'", p_module->psz_name );
390
391     /* We release the global lock */
392     vlc_mutex_unlock( &p_bank->lock );
393
394     return;
395 }
396
397 /*****************************************************************************
398  * Following functions are local.
399  *****************************************************************************/
400
401 #ifdef HAVE_DYNAMIC_PLUGINS
402 /*****************************************************************************
403  * AllocatePluginModule: load a module into memory and initialize it.
404  *****************************************************************************
405  * This function loads a dynamically loadable module and allocates a structure
406  * for its information data. The module can then be handled by module_Need,
407  * module_Unneed and HideModule. It can be removed by FreeModule.
408  *****************************************************************************/
409 static int AllocatePluginModule( module_bank_t * p_bank, char * psz_filename )
410 {
411     module_t * p_module, * p_othermodule;
412     module_handle_t handle;
413
414     /* Try to dynamically load the module. */
415     if( module_load( psz_filename, &handle ) )
416     {
417         /* The plugin module couldn't be opened */
418         intf_WarnMsgImm( 3, "module warning: cannot open %s (%s)",
419                          psz_filename, module_error() );
420         return( -1 );
421     }
422
423     /* Now that we have successfully loaded the module, we can
424      * allocate a structure for it */ 
425     p_module = malloc( sizeof( module_t ) );
426     if( p_module == NULL )
427     {
428         intf_ErrMsg( "module error: can't allocate p_module" );
429         module_unload( handle );
430         return( -1 );
431     }
432
433     /* We need to fill these since they may be needed by CallSymbol() */
434     p_module->is.plugin.psz_filename = psz_filename;
435     p_module->is.plugin.handle = handle;
436
437     /* Initialize the module : fill p_module->psz_name, etc. */
438     if( CallSymbol( p_module, "InitModule" ) != 0 )
439     {
440         /* We couldn't call InitModule() */
441         free( p_module );
442         module_unload( handle );
443         return( -1 );
444     }
445
446     /* Check that version numbers match */
447     if( strcmp( VERSION, p_module->psz_version ) )
448     {
449         free( p_module );
450         module_unload( handle );
451         return( -1 );
452     }
453
454     /* Check that we don't already have a module with this name */
455     for( p_othermodule = p_bank->first ;
456          p_othermodule != NULL ;
457          p_othermodule = p_othermodule->next )
458     {
459         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
460         {
461             free( p_module );
462             module_unload( handle );
463             return( -1 );
464         }
465     }
466
467     /* Activate the module : fill the capability structure, etc. */
468     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
469     {
470         /* We couldn't call ActivateModule() */
471         free( p_module );
472         module_unload( handle );
473         return( -1 );
474     }
475
476     /* We strdup() these entries so that they are still valid when the
477      * module is unloaded. */
478     p_module->is.plugin.psz_filename =
479             strdup( p_module->is.plugin.psz_filename );
480     p_module->psz_name = strdup( p_module->psz_name );
481     p_module->psz_longname = strdup( p_module->psz_longname );
482     p_module->psz_version = strdup( p_module->psz_version );
483     if( p_module->is.plugin.psz_filename == NULL 
484             || p_module->psz_name == NULL
485             || p_module->psz_longname == NULL
486             || p_module->psz_version == NULL )
487     {
488         intf_ErrMsg( "module error: can't duplicate strings" );
489         free( p_module->is.plugin.psz_filename );
490         free( p_module->psz_name );
491         free( p_module->psz_longname );
492         free( p_module->psz_version );
493         free( p_module );
494         module_unload( handle );
495         return( -1 );
496     }
497
498     /* Everything worked fine ! The module is ready to be added to the list. */
499     p_module->i_usage = 0;
500     p_module->i_unused_delay = 0;
501
502     p_module->b_builtin = 0;
503
504     /* Link module into the linked list */
505     if( p_bank->first != NULL )
506     {
507         p_bank->first->prev = p_module;
508     }
509     p_module->next = p_bank->first;
510     p_module->prev = NULL;
511     p_bank->first = p_module;
512
513     /* Immediate message so that a slow module doesn't make the user wait */
514     intf_WarnMsgImm( 2, "module: plugin module `%s', %s",
515                      p_module->psz_name, p_module->psz_longname );
516
517     return( 0 );
518 }
519 #endif /* HAVE_DYNAMIC_PLUGINS */
520
521 /*****************************************************************************
522  * AllocateBuiltinModule: initialize a built-in module.
523  *****************************************************************************
524  * This function registers a built-in module and allocates a structure
525  * for its information data. The module can then be handled by module_Need,
526  * module_Unneed and HideModule. It can be removed by FreeModule.
527  *****************************************************************************/
528 static int AllocateBuiltinModule( module_bank_t * p_bank,
529                                   int ( *pf_init ) ( module_t * ),
530                                   int ( *pf_activate ) ( module_t * ),
531                                   int ( *pf_deactivate ) ( module_t * ) )
532 {
533     module_t * p_module, * p_othermodule;
534
535     /* Now that we have successfully loaded the module, we can
536      * allocate a structure for it */ 
537     p_module = malloc( sizeof( module_t ) );
538     if( p_module == NULL )
539     {
540         intf_ErrMsg( "module error: can't allocate p_module" );
541         return( -1 );
542     }
543
544     /* Initialize the module : fill p_module->psz_name, etc. */
545     if( pf_init( p_module ) != 0 )
546     {
547         /* With a well-written module we shouldn't have to print an
548          * additional error message here, but just make sure. */
549         intf_ErrMsg( "module error: failed calling init in builtin module" );
550         free( p_module );
551         return( -1 );
552     }
553
554     /* Check that version numbers match */
555     if( strcmp( VERSION, p_module->psz_version ) )
556     {
557         free( p_module );
558         return( -1 );
559     }
560
561     /* Check that we don't already have a module with this name */
562     for( p_othermodule = p_bank->first ;
563          p_othermodule != NULL ;
564          p_othermodule = p_othermodule->next )
565     {
566         if( !strcmp( p_othermodule->psz_name, p_module->psz_name ) )
567         {
568             free( p_module );
569             return( -1 );
570         }
571     }
572
573     if( pf_activate( p_module ) != 0 )
574     {
575         /* With a well-written module we shouldn't have to print an
576          * additional error message here, but just make sure. */
577         intf_ErrMsg( "module error: failed calling activate "
578                      "in builtin module" );
579         free( p_module );
580         return( -1 );
581     }
582
583     /* We strdup() these entries so that they are still valid when the
584      * module is unloaded. */
585     p_module->psz_name = strdup( p_module->psz_name );
586     p_module->psz_longname = strdup( p_module->psz_longname );
587     p_module->psz_version = strdup( p_module->psz_version );
588     if( p_module->psz_name == NULL 
589             || p_module->psz_longname == NULL
590             || p_module->psz_version == NULL )
591     {
592         intf_ErrMsg( "module error: can't duplicate strings" );
593         free( p_module->psz_name );
594         free( p_module->psz_longname );
595         free( p_module->psz_version );
596         free( p_module );
597         return( -1 );
598     }
599
600     /* Everything worked fine ! The module is ready to be added to the list. */
601     p_module->i_usage = 0;
602     p_module->i_unused_delay = 0;
603
604     p_module->b_builtin = 1;
605     p_module->is.builtin.pf_deactivate = pf_deactivate;
606
607     /* Link module into the linked list */
608     if( p_bank->first != NULL )
609     {
610         p_bank->first->prev = p_module;
611     }
612     p_module->next = p_bank->first;
613     p_module->prev = NULL;
614     p_bank->first = p_module;
615
616     /* Immediate message so that a slow module doesn't make the user wait */
617     intf_WarnMsgImm( 2, "module: builtin module `%s', %s",
618                      p_module->psz_name, p_module->psz_longname );
619
620     return( 0 );
621 }
622
623 /*****************************************************************************
624  * FreeModule: delete a module and its structure.
625  *****************************************************************************
626  * This function can only be called if i_usage <= 0.
627  *****************************************************************************/
628 static int FreeModule( module_bank_t * p_bank, module_t * p_module )
629 {
630     /* If the module is not in use but is still in memory, we first have
631      * to hide it and remove it from memory before we can free the
632      * data structure. */
633     if( p_module->b_builtin )
634     {
635         if( p_module->i_usage != 0 )
636         {
637             intf_ErrMsg( "module error: trying to free builtin module `%s' with"
638                          " usage %i", p_module->psz_name, p_module->i_usage );
639             return( -1 );
640         }
641         else
642         {
643             /* We deactivate the module now. */
644             p_module->is.builtin.pf_deactivate( p_module );
645         }
646     }
647 #ifdef HAVE_DYNAMIC_PLUGINS
648     else
649     {
650         if( p_module->i_usage >= 1 )
651         {
652             intf_ErrMsg( "module error: trying to free module `%s' which is"
653                          " still in use", p_module->psz_name );
654             return( -1 );
655         }
656
657         /* Two possibilities here: i_usage == -1 and the module is already
658          * unloaded, we can continue, or i_usage == 0, and we have to hide
659          * the module before going on. */
660         if( p_module->i_usage == 0 )
661         {
662             if( HideModule( p_module ) != 0 )
663             {
664                 return( -1 );
665             }
666         }
667     }
668 #endif
669
670     /* Unlink the module from the linked list. */
671     if( p_module == p_bank->first )
672     {
673         p_bank->first = p_module->next;
674     }
675
676     if( p_module->prev != NULL )
677     {
678         p_module->prev->next = p_module->next;
679     }
680
681     if( p_module->next != NULL )
682     {
683         p_module->next->prev = p_module->prev;
684     }
685
686     /* We free the structures that we strdup()ed in Allocate*Module(). */
687 #ifdef HAVE_DYNAMIC_PLUGINS
688     if( !p_module->b_builtin )
689     {
690         free( p_module->is.plugin.psz_filename );
691     }
692 #endif
693     free( p_module->psz_name );
694     free( p_module->psz_longname );
695     free( p_module->psz_version );
696
697     free( p_module );
698
699     return( 0 );
700 }
701
702 /*****************************************************************************
703  * LockModule: increase the usage count of a module and load it if needed.
704  *****************************************************************************
705  * This function has to be called before a thread starts using a module. If
706  * the module is already loaded, we just increase its usage count. If it isn't
707  * loaded, we have to dynamically open it and initialize it.
708  * If you successfully call LockModule() at any moment, be careful to call
709  * UnlockModule() when you don't need it anymore.
710  *****************************************************************************/
711 static int LockModule( module_t * p_module )
712 {
713     if( p_module->i_usage >= 0 )
714     {
715         /* This module is already loaded and activated, we can return */
716         p_module->i_usage++;
717         return( 0 );
718     }
719
720     if( p_module->b_builtin )
721     {
722         /* A built-in module should always have a refcount >= 0 ! */
723         intf_ErrMsg( "module error: built-in module `%s' has refcount %i",
724                      p_module->psz_name, p_module->i_usage );
725         return( -1 );
726     }
727
728 #ifdef HAVE_DYNAMIC_PLUGINS
729     if( p_module->i_usage != -1 )
730     {
731         /* This shouldn't happen. Ever. We have serious problems here. */
732         intf_ErrMsg( "module error: plugin module `%s' has refcount %i",
733                      p_module->psz_name, p_module->i_usage );
734         return( -1 );
735     }
736
737     /* i_usage == -1, which means that the module isn't in memory */
738     if( module_load( p_module->is.plugin.psz_filename,
739                      &p_module->is.plugin.handle ) )
740     {
741         /* The plugin module couldn't be opened */
742         intf_ErrMsg( "module error: cannot open %s (%s)",
743                      p_module->is.plugin.psz_filename, module_error() );
744         return( -1 );
745     }
746
747     /* FIXME: what to do if the guy modified the plugin while it was
748      * unloaded ? It makes XMMS crash nastily, perhaps we should try
749      * to be a bit more clever here. */
750
751     /* Activate the module : fill the capability structure, etc. */
752     if( CallSymbol( p_module, "ActivateModule" ) != 0 )
753     {
754         /* We couldn't call ActivateModule() -- looks nasty, but
755          * we can't do much about it. Just try to unload module. */
756         module_unload( p_module->is.plugin.handle );
757         p_module->i_usage = -1;
758         return( -1 );
759     }
760
761     /* Everything worked fine ! The module is ready to be used */
762     p_module->i_usage = 1;
763 #endif /* HAVE_DYNAMIC_PLUGINS */
764
765     return( 0 );
766 }
767
768 /*****************************************************************************
769  * UnlockModule: decrease the usage count of a module.
770  *****************************************************************************
771  * We decrease the usage count of a module so that we know when a module
772  * becomes unused and can be hidden.
773  *****************************************************************************/
774 static int UnlockModule( module_t * p_module )
775 {
776     if( p_module->i_usage <= 0 )
777     {
778         /* This shouldn't happen. Ever. We have serious problems here. */
779         intf_ErrMsg( "module error: trying to call module_Unneed() on `%s'"
780                      " which isn't even in use", p_module->psz_name );
781         return( -1 );
782     }
783
784     /* This module is still in use, we can return */
785     p_module->i_usage--;
786     p_module->i_unused_delay = 0;
787
788     return( 0 );
789 }
790
791 #ifdef HAVE_DYNAMIC_PLUGINS
792 /*****************************************************************************
793  * HideModule: remove a module from memory but keep its structure.
794  *****************************************************************************
795  * This function can only be called if i_usage == 0. It will make a call
796  * to the module's inner DeactivateModule() symbol, and then unload it
797  * from memory. A call to module_Need() will automagically load it again.
798  *****************************************************************************/
799 static int HideModule( module_t * p_module )
800 {
801     if( p_module->b_builtin )
802     {
803         /* A built-in module should never be hidden. */
804         intf_ErrMsg( "module error: trying to hide built-in module `%s'",
805                      p_module->psz_name );
806         return( -1 );
807     }
808
809     if( p_module->i_usage >= 1 )
810     {
811         intf_ErrMsg( "module error: trying to hide module `%s' which is still"
812                      " in use", p_module->psz_name );
813         return( -1 );
814     }
815
816     if( p_module->i_usage <= -1 )
817     {
818         intf_ErrMsg( "module error: trying to hide module `%s' which is already"
819                      " hidden", p_module->psz_name );
820         return( -1 );
821     }
822
823     /* Deactivate the module : free the capability structure, etc. */
824     if( CallSymbol( p_module, "DeactivateModule" ) != 0 )
825     {
826         /* We couldn't call DeactivateModule() -- looks nasty, but
827          * we can't do much about it. Just try to unload module anyway. */
828         module_unload( p_module->is.plugin.handle );
829         p_module->i_usage = -1;
830         return( -1 );
831     }
832
833     /* Everything worked fine, we can safely unload the module. */
834     module_unload( p_module->is.plugin.handle );
835     p_module->i_usage = -1;
836
837     return( 0 );
838 }
839
840 /*****************************************************************************
841  * CallSymbol: calls a module symbol.
842  *****************************************************************************
843  * This function calls a symbol given its name and a module structure. The
844  * symbol MUST refer to a function returning int and taking a module_t* as
845  * an argument.
846  *****************************************************************************/
847 static int CallSymbol( module_t * p_module, char * psz_name )
848 {
849     typedef int ( symbol_t ) ( module_t * p_module );
850     symbol_t * p_symbol;
851
852     /* Try to resolve the symbol */
853     p_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
854
855     if( !p_symbol )
856     {
857         /* We couldn't load the symbol */
858         intf_WarnMsg( 3, "module warning: "
859                          "cannot find symbol %s in module %s (%s)",
860                          psz_name, p_module->is.plugin.psz_filename,
861                          module_error() );
862         return( -1 );
863     }
864
865     /* We can now try to call the symbol */
866     if( p_symbol( p_module ) != 0 )
867     {
868         /* With a well-written module we shouldn't have to print an
869          * additional error message here, but just make sure. */
870         intf_ErrMsg( "module error: failed calling symbol %s in module %s",
871                      psz_name, p_module->is.plugin.psz_filename );
872         return( -1 );
873     }
874
875     /* Everything worked fine, we can return */
876     return( 0 );
877 }
878 #endif /* HAVE_DYNAMIC_PLUGINS */
879