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