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