]> git.sesse.net Git - vlc/blob - src/misc/modules.c
0e24d5318a4ee23e589accd4d61d0aaa6eb59637
[vlc] / src / misc / modules.c
1 /*****************************************************************************
2  * modules.c : Builtin and plugin modules management functions
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: modules.c,v 1.71 2002/07/04 18:11:57 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
9  *          Hans-Peter Jansen <hpj@urpla.net>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
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 #include <vlc/vlc.h>
37
38 #include <dirent.h>
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)
47 #   define HAVE_DYNAMIC_PLUGINS
48 #else
49 #   undef HAVE_DYNAMIC_PLUGINS
50 #endif
51
52
53 #include "netutils.h"
54
55 #include "interface.h"
56 #include "vlc_playlist.h"
57 #include "intf_eject.h"
58
59 #include "stream_control.h"
60 #include "input_ext-intf.h"
61 #include "input_ext-dec.h"
62 #include "input_ext-plugins.h"
63
64 #include "video.h"
65 #include "video_output.h"
66
67 #include "audio_output.h"
68
69 #include "iso_lang.h"
70
71 #ifdef HAVE_DYNAMIC_PLUGINS
72 #   include "modules_plugin.h"
73 #endif
74
75 #if !defined( _MSC_VER )
76 #    include "modules_builtin.h"
77 #else
78 #    include "modules_builtin_msvc.h"
79 #endif
80
81 /*****************************************************************************
82  * Static variables
83  *****************************************************************************/
84 DECLARE_MODULE_CAPABILITY_TABLE;
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 #ifdef HAVE_DYNAMIC_PLUGINS
90 static void AllocateAllPlugins   ( vlc_object_t * );
91 static int  AllocatePluginModule ( vlc_object_t *, char * );
92 #endif
93 static int  AllocateBuiltinModule( vlc_object_t *,
94                                    int ( * ) ( module_t * ),
95                                    int ( * ) ( module_t * ),
96                                    int ( * ) ( module_t * ) );
97 static int  DeleteModule ( module_t * );
98 static int  LockModule   ( module_t * );
99 static int  UnlockModule ( module_t * );
100 #ifdef HAVE_DYNAMIC_PLUGINS
101 static int  HideModule   ( module_t * );
102 static int  CallSymbol   ( module_t *, char * );
103 #endif
104
105 #ifdef HAVE_DYNAMIC_PLUGINS
106 static module_symbols_t symbols;
107 #endif
108
109 /*****************************************************************************
110  * module_InitBank: create the module bank.
111  *****************************************************************************
112  * This function creates a module bank structure which will be filled later
113  * on with all the modules found.
114  *****************************************************************************/
115 void __module_InitBank( vlc_object_t *p_this )
116 {
117     p_this->p_vlc->module_bank.first = NULL;
118     p_this->p_vlc->module_bank.i_count = 0;
119     vlc_mutex_init( p_this, &p_this->p_vlc->module_bank.lock );
120
121     /*
122      * Store the symbols to be exported
123      */
124 #ifdef HAVE_DYNAMIC_PLUGINS
125     STORE_SYMBOLS( &symbols );
126 #endif
127
128     return;
129 }
130
131 /*****************************************************************************
132  * module_ResetBank: reset the module bank.
133  *****************************************************************************
134  * This function resets the module bank by unloading all unused plugin
135  * modules.
136  *****************************************************************************/
137 void __module_ResetBank( vlc_object_t *p_this )
138 {
139     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
140     return;
141 }
142
143 /*****************************************************************************
144  * module_EndBank: empty the module bank.
145  *****************************************************************************
146  * This function unloads all unused plugin modules and empties the module
147  * bank in case of success.
148  *****************************************************************************/
149 void __module_EndBank( vlc_object_t *p_this )
150 {
151     module_t * p_next;
152
153     while( p_this->p_vlc->module_bank.first != NULL )
154     {
155         if( DeleteModule( p_this->p_vlc->module_bank.first ) )
156         {
157             /* Module deletion failed */
158             msg_Err( p_this, "`%s' can't be removed, trying harder",
159                      p_this->p_vlc->module_bank.first->psz_object_name );
160
161             /* We just free the module by hand. Niahahahahaha. */
162             p_next = p_this->p_vlc->module_bank.first->next;
163             free( p_this->p_vlc->module_bank.first );
164             p_this->p_vlc->module_bank.first = p_next;
165         }
166     }
167
168     /* Destroy the lock */
169     vlc_mutex_destroy( &p_this->p_vlc->module_bank.lock );
170
171     return;
172 }
173
174 /*****************************************************************************
175  * module_LoadMain: load the main program info into the module bank.
176  *****************************************************************************
177  * This function fills the module bank structure with the main module infos.
178  * This is very useful as it will allow us to consider the main program just
179  * as another module, and for instance the configuration options of main will
180  * be available in the module bank structure just as for every other module.
181  *****************************************************************************/
182 void __module_LoadMain( vlc_object_t *p_this )
183 {
184     AllocateBuiltinModule( p_this, InitModule__MODULE_main,
185                                    ActivateModule__MODULE_main,
186                                    DeactivateModule__MODULE_main );
187 }
188
189 /*****************************************************************************
190  * module_LoadBuiltins: load all modules which we built with.
191  *****************************************************************************
192  * This function fills the module bank structure with the builtin modules.
193  *****************************************************************************/
194 void __module_LoadBuiltins( vlc_object_t * p_this )
195 {
196     msg_Dbg( p_this, "checking builtin modules" );
197     ALLOCATE_ALL_BUILTINS();
198 }
199
200 /*****************************************************************************
201  * module_LoadPlugins: load all plugin modules we can find.
202  *****************************************************************************
203  * This function fills the module bank structure with the plugin modules.
204  *****************************************************************************/
205 void __module_LoadPlugins( vlc_object_t * p_this )
206 {
207 #ifdef HAVE_DYNAMIC_PLUGINS
208     msg_Dbg( p_this, "checking plugin modules" );
209     AllocateAllPlugins( p_this );
210 #endif
211 }
212
213 /*****************************************************************************
214  * module_ManageBank: manage the module bank.
215  *****************************************************************************
216  * This function parses the module bank and hides modules that have been
217  * unused for a while.
218  *****************************************************************************/
219 void __module_ManageBank( vlc_object_t *p_this )
220 {
221 #ifdef HAVE_DYNAMIC_PLUGINS
222     module_t * p_module;
223
224     /* We take the global lock */
225     vlc_mutex_lock( &p_this->p_vlc->module_bank.lock );
226
227     /* Parse the module list to see if any modules need to be unloaded */
228     for( p_module = p_this->p_vlc->module_bank.first ;
229          p_module != NULL ;
230          p_module = p_module->next )
231     {
232         /* If the module is unused and if it is a plugin module... */
233         if( p_module->i_usage == 0 && !p_module->b_builtin )
234         {
235             if( p_module->i_unused_delay < MODULE_HIDE_DELAY )
236             {
237                 p_module->i_unused_delay++;
238             }
239             else
240             {
241                 msg_Dbg( p_this, "hiding unused plugin module `%s'",
242                                  p_module->psz_object_name );
243                 HideModule( p_module );
244
245                 /* Break here, so that we only hide one module at a time */
246                 break;
247             }
248         }
249     }
250
251     /* We release the global lock */
252     vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
253 #endif /* HAVE_DYNAMIC_PLUGINS */
254
255     return;
256 }
257
258 /*****************************************************************************
259  * module_Need: return the best module function, given a capability list.
260  *****************************************************************************
261  * This function returns the module that best fits the asked capabilities.
262  *****************************************************************************/
263 module_t * __module_Need( vlc_object_t *p_this, int i_capability,
264                           const char *psz_name, void *p_data )
265 {
266     typedef struct module_list_s module_list_t;
267
268     struct module_list_s
269     {
270         module_t *p_module;
271         int i_score;
272         module_list_t *p_next;
273     };
274
275     module_list_t *p_list, *p_first, *p_tmp;
276
277     int i_ret, i_index = 0;
278     vlc_bool_t b_intf = VLC_FALSE;
279
280     module_t *p_module;
281
282     int   i_shortcuts = 0;
283     char *psz_shortcuts = NULL;
284
285     msg_Dbg( p_this, "looking for %s module",
286                      MODULE_CAPABILITY( i_capability ) );
287
288     /* We take the global lock */
289     vlc_mutex_lock( &p_this->p_vlc->module_bank.lock );
290
291     /* Count how many different shortcuts were asked for */
292     if( psz_name && *psz_name )
293     {
294         char *psz_parser;
295
296         i_shortcuts++;
297         psz_shortcuts = strdup( psz_name );
298
299         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
300         {
301             if( *psz_parser == ',' )
302             {
303                  *psz_parser = '\0';
304                  i_shortcuts++;
305             }
306         }
307     }
308
309     /* Sort the modules and test them */
310     p_list = malloc( p_this->p_vlc->module_bank.i_count
311                       * sizeof( module_list_t ) );
312     p_first = NULL;
313
314     /* Parse the module list for capabilities and probe each of them */
315     for( p_module = p_this->p_vlc->module_bank.first ;
316          p_module != NULL ;
317          p_module = p_module->next )
318     {
319         int i_shortcut_bonus = 0;
320
321         /* Test that this module can do everything we need */
322         if( !(p_module->i_capabilities & ( 1 << i_capability )) )
323         {
324             continue;
325         }
326
327         /* Test if we have the required CPU */
328         if( (p_module->i_cpu_capabilities
329                 & p_this->p_vlc->i_cpu_capabilities)
330               != p_module->i_cpu_capabilities )
331         {
332             continue;
333         }
334
335         /* If we required a shortcut, check this plugin provides it. */
336         if( i_shortcuts )
337         {
338             vlc_bool_t b_trash = VLC_TRUE;
339             int i_dummy, i_short = i_shortcuts;
340             char *psz_name = psz_shortcuts;
341
342             while( i_short )
343             {
344                 for( i_dummy = 0;
345                      b_trash && p_module->pp_shortcuts[i_dummy];
346                      i_dummy++ )
347                 {
348                     b_trash = strcmp( psz_name,
349                                       p_module->pp_shortcuts[i_dummy] );
350                 }
351
352                 if( !b_trash )
353                 {
354                     i_shortcut_bonus = i_short * 10000;
355                     break;
356                 }
357
358                 while( *psz_name )
359                 {
360                     psz_name++;
361                 }
362                 psz_name++;
363                 i_short--;
364             }
365
366             if( b_trash )
367             {
368                 continue;
369             }
370         }
371         /* If we didn't require a shortcut, trash zero-scored plugins */
372         else if( !p_module->pi_score[i_capability] )
373         {
374             continue;
375         }
376
377         /* Special case: test if we requested a particular intf plugin */
378         if( i_capability == MODULE_CAPABILITY_INTF )
379         {
380             if( p_module->psz_program != NULL
381                  && !strcmp( p_module->psz_program,
382                              p_this->p_vlc->psz_object_name ) )
383             {
384                 if( !b_intf ) 
385                 {
386                     /* Remove previous non-matching plugins */
387                     i_index = 0;
388                     b_intf = VLC_TRUE;
389                 }
390             }
391             else
392             {
393                 if( b_intf )
394                 {
395                     /* This one doesn't match */
396                     continue;
397                 }
398             }
399         }
400
401         /* Store this new module */
402         p_list[ i_index ].p_module = p_module;
403         p_list[ i_index ].i_score = p_module->pi_score[i_capability]
404                                      + i_shortcut_bonus;
405
406         /* Add it to the modules-to-probe list */
407         if( i_index == 0 )
408         {
409             p_list[ 0 ].p_next = NULL;
410             p_first = p_list;
411         }
412         else
413         {
414             /* Ok, so at school you learned that quicksort is quick, and
415              * bubble sort sucks raw eggs. But that's when dealing with
416              * thousands of items. Here we have barely 50. */
417             module_list_t *p_newlist = p_first;
418
419             if( p_first->i_score < p_list[ i_index ].i_score )
420             {
421                 p_list[ i_index ].p_next = p_first;
422                 p_first = &p_list[ i_index ];
423             }
424             else
425             {
426                 while( p_newlist->p_next != NULL &&
427                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
428                 {
429                     p_newlist = p_newlist->p_next;
430                 }
431
432                 p_list[ i_index ].p_next = p_newlist->p_next;
433                 p_newlist->p_next = &p_list[ i_index ];
434             }
435         }
436
437         i_index++;
438     }
439
440     /* Lock all selected modules */
441     p_tmp = p_first;
442     while( p_tmp != NULL )
443     {
444         LockModule( p_tmp->p_module );
445         p_tmp = p_tmp->p_next;
446     }
447
448     /* We can release the global lock, module refcounts were incremented */
449     vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
450
451     /* Parse the linked list and use the first successful module */
452     p_tmp = p_first;
453     while( p_tmp != NULL )
454     {
455         /* Test the requested capability */
456         switch( i_capability )
457         {
458             case MODULE_CAPABILITY_ACCESS:
459                 i_ret = p_tmp->p_module->p_functions->access.functions.
460                               access.pf_open( (input_thread_t *)p_data );
461                 break;
462
463             case MODULE_CAPABILITY_DEMUX:
464                 i_ret = p_tmp->p_module->p_functions->demux.functions.
465                               demux.pf_init( (input_thread_t *)p_data );
466                 break;
467
468             case MODULE_CAPABILITY_NETWORK:
469                 i_ret = p_tmp->p_module->p_functions->network.functions.
470                          network.pf_open( p_this, (network_socket_t *)p_data );
471                 break;
472
473             case MODULE_CAPABILITY_DECODER:
474                 i_ret = p_tmp->p_module->p_functions->dec.functions.
475                               dec.pf_probe( (u8 *)p_data );
476                 break;
477
478             case MODULE_CAPABILITY_INTF:
479                 i_ret = p_tmp->p_module->p_functions->intf.functions.
480                               intf.pf_open( (intf_thread_t *)p_data );
481                 break;
482
483             case MODULE_CAPABILITY_AOUT:
484                 i_ret = p_tmp->p_module->p_functions->aout.functions.
485                               aout.pf_open( (aout_thread_t *)p_data );
486                 break;
487
488             case MODULE_CAPABILITY_VOUT:
489             case MODULE_CAPABILITY_VOUT_FILTER:
490                 i_ret = p_tmp->p_module->p_functions->vout.functions.
491                               vout.pf_create( (vout_thread_t *)p_data );
492                 break;
493
494             case MODULE_CAPABILITY_CHROMA:
495                 i_ret = p_tmp->p_module->p_functions->chroma.functions.
496                               chroma.pf_init( (vout_thread_t *)p_data );
497                 break;
498
499             case MODULE_CAPABILITY_IDCT:
500             case MODULE_CAPABILITY_IMDCT:
501             case MODULE_CAPABILITY_MOTION:
502             case MODULE_CAPABILITY_DOWNMIX:
503             case MODULE_CAPABILITY_MEMCPY:
504                 /* This one always works */
505                 i_ret = 0;
506                 break;
507
508             default:
509                 msg_Err( p_this, "unknown module type %i", i_capability );
510                 i_ret = -1;
511                 break;
512         }
513
514         /* If the high score was broken, we have a new champion */
515         if( i_ret == 0 )
516         {
517             break;
518         }
519         else
520         {
521             UnlockModule( p_tmp->p_module );
522         }
523
524         p_tmp = p_tmp->p_next;
525     }
526
527     /* Store the locked module value */
528     if( p_tmp != NULL )
529     {
530         p_module = p_tmp->p_module;
531         p_tmp = p_tmp->p_next;
532     }
533     else
534     {
535         p_module = NULL;
536     }
537
538     /* Unlock the remaining modules */
539     while( p_tmp != NULL )
540     {
541         UnlockModule( p_tmp->p_module );
542         p_tmp = p_tmp->p_next;
543     }
544
545     free( p_list );
546
547     if( p_module != NULL )
548     {
549         msg_Info( p_module, "found and locked %s module `%s'",
550                   MODULE_CAPABILITY( i_capability ),
551                   p_module->psz_object_name );
552     }
553     else if( p_first == NULL )
554     {
555         msg_Err( p_this, "no available %s module matched `%s'",
556                  MODULE_CAPABILITY( i_capability ), psz_name );
557     }
558     else if( psz_name != NULL && *psz_name )
559     {
560         msg_Err( p_this, "could not load any %s module matching `%s'",
561                  MODULE_CAPABILITY( i_capability ), psz_name );
562     }
563
564     if( psz_shortcuts )
565     {
566         free( psz_shortcuts );
567     }
568
569     /* Don't forget that the module is still locked */
570     return p_module;
571 }
572
573 /*****************************************************************************
574  * module_Unneed: decrease the usage count of a module.
575  *****************************************************************************
576  * This function must be called by the thread that called module_Need, to
577  * decrease the reference count and allow for hiding of modules.
578  *****************************************************************************/
579 void module_Unneed( module_t * p_module )
580 {
581     /* We take the global lock */
582     vlc_mutex_lock( &p_module->p_vlc->module_bank.lock );
583
584     /* Just unlock the module - we can't do anything if it fails,
585      * so there is no need to check the return value. */
586     UnlockModule( p_module );
587
588     msg_Info( p_module, "unlocking module `%s'", p_module->psz_object_name );
589
590     /* We release the global lock */
591     vlc_mutex_unlock( &p_module->p_vlc->module_bank.lock );
592
593     return;
594 }
595
596 /*****************************************************************************
597  * Following functions are local.
598  *****************************************************************************/
599
600 /*****************************************************************************
601  * AllocateAllPlugins: load all plugin modules we can find.
602  *****************************************************************************/
603 #ifdef HAVE_DYNAMIC_PLUGINS
604 static void AllocateAllPlugins( vlc_object_t *p_this )
605 {
606     /* Yes, there are two NULLs because we replace one with "plugin-path". */
607     char *          path[] = { "plugins", PLUGIN_PATH, NULL, NULL };
608
609     char **         ppsz_path = path;
610     char *          psz_fullpath;
611     char *          psz_file;
612 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
613     char *          psz_vlcpath = system_GetProgramPath();
614     int             i_vlclen = strlen( psz_vlcpath );
615     vlc_bool_t      b_notinroot;
616 #endif
617     DIR *           dir;
618     struct dirent * file;
619
620     /* If the user provided a plugin path, we add it to the list */
621     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
622                                                             "plugin-path" );
623
624     for( ; *ppsz_path != NULL ; ppsz_path++ )
625     {
626         /* Store strlen(*ppsz_path) for later use. */
627         int i_dirlen = strlen( *ppsz_path );
628
629 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
630         b_notinroot = VLC_FALSE;
631         /* Under BeOS, we need to add beos_GetProgramPath() to access
632          * files under the current directory */
633         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
634         {
635             i_dirlen += i_vlclen + 2;
636             b_notinroot = VLC_TRUE;
637
638             psz_fullpath = malloc( i_dirlen );
639             if( psz_fullpath == NULL )
640             {
641                 continue;
642             }
643             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
644         }
645         else
646 #endif
647         {
648             psz_fullpath = *ppsz_path;
649         }
650
651         msg_Dbg( p_this, "browsing `%s'", psz_fullpath );
652
653         if( (dir = opendir( psz_fullpath )) )
654         {
655             /* Parse the directory and try to load all files it contains. */
656             while( (file = readdir( dir )) )
657             {
658                 int i_filelen = strlen( file->d_name );
659
660                 /* We only load files ending with ".so" */
661                 if( i_filelen > 3
662                         && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
663                 {
664                     psz_file = malloc( i_dirlen + i_filelen + 2 );
665                     if( psz_file == NULL )
666                     {
667                         continue;
668                     }
669                     sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
670
671                     /* We created a nice filename -- now we just try to load
672                      * it as a plugin module. */
673                     AllocatePluginModule( p_this, psz_file );
674
675                     /* We don't care if the allocation succeeded */
676                     free( psz_file );
677                 }
678             }
679
680             /* Close the directory if successfully opened */
681             closedir( dir );
682         }
683
684 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
685         if( b_notinroot )
686         {
687             free( psz_fullpath );
688         }
689 #endif
690     }
691 }
692
693 /*****************************************************************************
694  * AllocatePluginModule: load a module into memory and initialize it.
695  *****************************************************************************
696  * This function loads a dynamically loadable module and allocates a structure
697  * for its information data. The module can then be handled by module_Need,
698  * module_Unneed and HideModule. It can be removed by DeleteModule.
699  *****************************************************************************/
700 static int AllocatePluginModule( vlc_object_t * p_this, char * psz_filename )
701 {
702     char **pp_shortcut;
703     module_t * p_module, * p_othermodule;
704     module_handle_t handle;
705
706     /* Try to dynamically load the module. */
707     if( module_load( psz_filename, &handle ) )
708     {
709         char psz_buffer[256];
710
711         /* The plugin module couldn't be opened */
712         msg_Warn( p_this, "cannot open %s (%s)",
713                   psz_filename, module_error( psz_buffer ) );
714         return -1;
715     }
716
717     /* Now that we have successfully loaded the module, we can
718      * allocate a structure for it */ 
719     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
720     if( p_module == NULL )
721     {
722         msg_Err( p_this, "out of memory" );
723         module_unload( handle );
724         return -1;
725     }
726
727     /* We need to fill these since they may be needed by CallSymbol() */
728     p_module->is.plugin.psz_filename = psz_filename;
729     p_module->is.plugin.handle = handle;
730     p_module->p_symbols = &symbols;
731
732     /* Initialize the module: fill p_module->psz_object_name, default config */
733     if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
734     {
735         /* We couldn't call InitModule() */
736         vlc_object_destroy( p_module );
737         module_unload( handle );
738         return -1;
739     }
740
741     /* Check that we don't already have a module with this name */
742     for( p_othermodule = p_this->p_vlc->module_bank.first ;
743          p_othermodule != NULL ;
744          p_othermodule = p_othermodule->next )
745     {
746         if( !strcmp( p_othermodule->psz_object_name,
747                      p_module->psz_object_name ) )
748         {
749             msg_Warn( p_this,
750                       "cannot load %s, a module named `%s' already exists",
751                       psz_filename, p_module->psz_object_name );
752             config_Free( p_module );
753             vlc_object_destroy( p_module );
754             module_unload( handle );
755             return -1;
756         }
757     }
758
759     /* Activate the module : fill the capability structure, etc. */
760     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
761     {
762         /* We couldn't call ActivateModule() */
763         config_Free( p_module );
764         vlc_object_destroy( p_module );
765         module_unload( handle );
766         return -1;
767     }
768
769     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
770     {
771         *pp_shortcut = strdup( *pp_shortcut );
772     }
773
774     /* We strdup() these entries so that they are still valid when the
775      * module is unloaded. */
776     p_module->is.plugin.psz_filename =
777             strdup( p_module->is.plugin.psz_filename );
778     p_module->psz_object_name = strdup( p_module->psz_object_name );
779     p_module->psz_longname = strdup( p_module->psz_longname );
780
781     if( p_module->is.plugin.psz_filename == NULL 
782             || p_module->psz_object_name == NULL
783             || p_module->psz_longname == NULL )
784     {
785         msg_Err( p_this, "out of memory" );
786
787         free( p_module->is.plugin.psz_filename );
788         free( p_module->psz_object_name );
789         free( p_module->psz_longname );
790         free( p_module->psz_program );
791
792         config_Free( p_module );
793         vlc_object_destroy( p_module );
794         module_unload( handle );
795         return -1;
796     }
797
798     if( p_module->psz_program != NULL )
799     {
800         p_module->psz_program = strdup( p_module->psz_program );
801     }
802
803     /* Everything worked fine ! The module is ready to be added to the list. */
804     p_module->i_usage = 0;
805     p_module->i_unused_delay = 0;
806
807     p_module->b_builtin = VLC_FALSE;
808
809     /* Link module into the linked list */
810     if( p_this->p_vlc->module_bank.first != NULL )
811     {
812         p_this->p_vlc->module_bank.first->prev = p_module;
813     }
814     p_module->next = p_this->p_vlc->module_bank.first;
815     p_module->prev = NULL;
816     p_this->p_vlc->module_bank.first = p_module;
817     p_this->p_vlc->module_bank.i_count++;
818
819     msg_Dbg( p_this, "plugin `%s', %s",
820              p_module->psz_object_name, p_module->psz_longname );
821
822     vlc_object_attach( p_module, p_this );
823
824     return 0;
825 }
826 #endif /* HAVE_DYNAMIC_PLUGINS */
827
828 /*****************************************************************************
829  * AllocateBuiltinModule: initialize a builtin module.
830  *****************************************************************************
831  * This function registers a builtin module and allocates a structure
832  * for its information data. The module can then be handled by module_Need,
833  * module_Unneed and HideModule. It can be removed by DeleteModule.
834  *****************************************************************************/
835 static int AllocateBuiltinModule( vlc_object_t * p_this,
836                                   int ( *pf_init ) ( module_t * ),
837                                   int ( *pf_activate ) ( module_t * ),
838                                   int ( *pf_deactivate ) ( module_t * ) )
839 {
840     module_t * p_module, * p_othermodule;
841
842     /* Now that we have successfully loaded the module, we can
843      * allocate a structure for it */ 
844     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
845     if( p_module == NULL )
846     {
847         msg_Err( p_this, "out of memory" );
848         return -1;
849     }
850
851     /* Initialize the module : fill p_module->psz_object_name, etc. */
852     if( pf_init( p_module ) != 0 )
853     {
854         /* With a well-written module we shouldn't have to print an
855          * additional error message here, but just make sure. */
856         msg_Err( p_this, "failed calling init in builtin module" );
857         vlc_object_destroy( p_module );
858         return -1;
859     }
860
861     /* Check that we don't already have a module with this name */
862     for( p_othermodule = p_this->p_vlc->module_bank.first ;
863          p_othermodule != NULL ;
864          p_othermodule = p_othermodule->next )
865     {
866         if( !strcmp( p_othermodule->psz_object_name,
867                      p_module->psz_object_name ) )
868         {
869             msg_Warn( p_this, "cannot load builtin `%s', "
870                       "a module named `%s' already exists",
871                       p_module->psz_object_name, p_module->psz_object_name );
872             config_Free( p_module );
873             vlc_object_destroy( p_module );
874             return -1;
875         }
876     }
877
878     if( pf_activate( p_module ) != 0 )
879     {
880         /* With a well-written module we shouldn't have to print an
881          * additional error message here, but just make sure. */
882         msg_Err( p_this, "failed calling activate in builtin module" );
883         config_Free( p_module );
884         vlc_object_destroy( p_module );
885         return -1;
886     }
887
888     /* Everything worked fine ! The module is ready to be added to the list. */
889     p_module->i_usage = 0;
890     p_module->i_unused_delay = 0;
891
892     p_module->b_builtin = VLC_TRUE;
893     p_module->is.builtin.pf_deactivate = pf_deactivate;
894
895     /* Link module into the linked list */
896     if( p_this->p_vlc->module_bank.first != NULL )
897     {
898         p_this->p_vlc->module_bank.first->prev = p_module;
899     }
900     p_module->next = p_this->p_vlc->module_bank.first;
901     p_module->prev = NULL;
902     p_this->p_vlc->module_bank.first = p_module;
903     p_this->p_vlc->module_bank.i_count++;
904
905     msg_Dbg( p_this, "builtin `%s', %s",
906              p_module->psz_object_name, p_module->psz_longname );
907
908     vlc_object_attach( p_module, p_this );
909
910     return 0;
911 }
912
913 /*****************************************************************************
914  * DeleteModule: delete a module and its structure.
915  *****************************************************************************
916  * This function can only be called if i_usage <= 0.
917  *****************************************************************************/
918 static int DeleteModule( module_t * p_module )
919 {
920     /* If the module is not in use but is still in memory, we first have
921      * to hide it and remove it from memory before we can free the
922      * data structure. */
923     if( p_module->b_builtin )
924     {
925         if( p_module->i_usage != 0 )
926         {
927             msg_Err( p_module, "trying to free builtin module `%s' with "
928                      "usage %i", p_module->psz_object_name, p_module->i_usage );
929             return -1;
930         }
931         else
932         {
933             /* We deactivate the module now. */
934             p_module->is.builtin.pf_deactivate( p_module );
935         }
936     }
937 #ifdef HAVE_DYNAMIC_PLUGINS
938     else
939     {
940         if( p_module->i_usage >= 1 )
941         {
942             msg_Err( p_module, "trying to free module `%s' which is "
943                                "still in use", p_module->psz_object_name );
944             return -1;
945         }
946
947         /* Two possibilities here: i_usage == -1 and the module is already
948          * unloaded, we can continue, or i_usage == 0, and we have to hide
949          * the module before going on. */
950         if( p_module->i_usage == 0 )
951         {
952             if( HideModule( p_module ) != 0 )
953             {
954                 return -1;
955             }
956         }
957     }
958 #endif
959
960     vlc_object_detach_all( p_module );
961
962     /* Unlink the module from the linked list. */
963     if( p_module->prev != NULL )
964     {
965         p_module->prev->next = p_module->next;
966     }
967     else
968     {
969         p_module->p_vlc->module_bank.first = p_module->next;
970     }
971
972     if( p_module->next != NULL )
973     {
974         p_module->next->prev = p_module->prev;
975     }
976
977     p_module->p_vlc->module_bank.i_count--;
978
979     /* We free the structures that we strdup()ed in Allocate*Module(). */
980 #ifdef HAVE_DYNAMIC_PLUGINS
981     if( !p_module->b_builtin )
982     {
983         char **pp_shortcut = p_module->pp_shortcuts;
984
985         for( ; *pp_shortcut ; pp_shortcut++ )
986         {
987             free( *pp_shortcut );
988         }
989
990         free( p_module->is.plugin.psz_filename );
991         free( p_module->psz_object_name );
992         free( p_module->psz_longname );
993         free( p_module->psz_program );
994     }
995 #endif
996
997     config_Free( p_module );
998     vlc_object_destroy( p_module );
999
1000     return 0;
1001 }
1002
1003 /*****************************************************************************
1004  * LockModule: increase the usage count of a module and load it if needed.
1005  *****************************************************************************
1006  * This function has to be called before a thread starts using a module. If
1007  * the module is already loaded, we just increase its usage count. If it isn't
1008  * loaded, we have to dynamically open it and initialize it.
1009  * If you successfully call LockModule() at any moment, be careful to call
1010  * UnlockModule() when you don't need it anymore.
1011  *****************************************************************************/
1012 static int LockModule( module_t * p_module )
1013 {
1014     if( p_module->i_usage >= 0 )
1015     {
1016         /* This module is already loaded and activated, we can return */
1017         p_module->i_usage++;
1018         return 0;
1019     }
1020
1021     if( p_module->b_builtin )
1022     {
1023         /* A builtin module should always have a refcount >= 0 ! */
1024         msg_Err( p_module, "builtin module `%s' has refcount %i",
1025                            p_module->psz_object_name, p_module->i_usage );
1026         return -1;
1027     }
1028
1029 #ifdef HAVE_DYNAMIC_PLUGINS
1030     if( p_module->i_usage != -1 )
1031     {
1032         /* This shouldn't happen. Ever. We have serious problems here. */
1033         msg_Err( p_module, "plugin module `%s' has refcount %i",
1034                            p_module->psz_object_name, p_module->i_usage );
1035         return -1;
1036     }
1037
1038     /* i_usage == -1, which means that the module isn't in memory */
1039     if( module_load( p_module->is.plugin.psz_filename,
1040                      &p_module->is.plugin.handle ) )
1041     {
1042         char psz_buffer[256];
1043
1044         /* The plugin module couldn't be opened */
1045         msg_Err( p_module, "cannot open %s (%s)",
1046                  p_module->is.plugin.psz_filename, module_error(psz_buffer) );
1047         return -1;
1048     }
1049
1050     /* FIXME: what to do if the guy modified the plugin while it was
1051      * unloaded ? It makes XMMS crash nastily, perhaps we should try
1052      * to be a bit more clever here. */
1053
1054     /* Activate the module : fill the capability structure, etc. */
1055     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
1056     {
1057         /* We couldn't call ActivateModule() -- looks nasty, but
1058          * we can't do much about it. Just try to unload module. */
1059         module_unload( p_module->is.plugin.handle );
1060         p_module->i_usage = -1;
1061         return -1;
1062     }
1063
1064     /* Everything worked fine ! The module is ready to be used */
1065     p_module->i_usage = 1;
1066 #endif /* HAVE_DYNAMIC_PLUGINS */
1067
1068     return 0;
1069 }
1070
1071 /*****************************************************************************
1072  * UnlockModule: decrease the usage count of a module.
1073  *****************************************************************************
1074  * We decrease the usage count of a module so that we know when a module
1075  * becomes unused and can be hidden.
1076  *****************************************************************************/
1077 static int UnlockModule( module_t * p_module )
1078 {
1079     if( p_module->i_usage <= 0 )
1080     {
1081         /* This shouldn't happen. Ever. We have serious problems here. */
1082         msg_Err( p_module, "trying to call module_Unneed() on `%s' "
1083                            "which is not in use", p_module->psz_object_name );
1084         return -1;
1085     }
1086
1087     /* This module is still in use, we can return */
1088     p_module->i_usage--;
1089     p_module->i_unused_delay = 0;
1090
1091     return 0;
1092 }
1093
1094 #ifdef HAVE_DYNAMIC_PLUGINS
1095 /*****************************************************************************
1096  * HideModule: remove a module from memory but keep its structure.
1097  *****************************************************************************
1098  * This function can only be called if i_usage == 0. It will make a call
1099  * to the module's inner DeactivateModule() symbol, and then unload it
1100  * from memory. A call to module_Need() will automagically load it again.
1101  *****************************************************************************/
1102 static int HideModule( module_t * p_module )
1103 {
1104     if( p_module->b_builtin )
1105     {
1106         /* A builtin module should never be hidden. */
1107         msg_Err( p_module, "trying to hide builtin module `%s'",
1108                            p_module->psz_object_name );
1109         return -1;
1110     }
1111
1112     if( p_module->i_usage >= 1 )
1113     {
1114         msg_Err( p_module, "trying to hide module `%s' which is still "
1115                            "in use", p_module->psz_object_name );
1116         return -1;
1117     }
1118
1119     if( p_module->i_usage <= -1 )
1120     {
1121         msg_Err( p_module, "trying to hide module `%s' which is already "
1122                            "hidden", p_module->psz_object_name );
1123         return -1;
1124     }
1125
1126     /* Deactivate the module : free the capability structure, etc. */
1127     if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1128     {
1129         /* We couldn't call DeactivateModule() -- looks nasty, but
1130          * we can't do much about it. Just try to unload module anyway. */
1131         module_unload( p_module->is.plugin.handle );
1132         p_module->i_usage = -1;
1133         return -1;
1134     }
1135
1136     /* Everything worked fine, we can safely unload the module. */
1137     module_unload( p_module->is.plugin.handle );
1138     p_module->i_usage = -1;
1139
1140     return 0;
1141 }
1142
1143 /*****************************************************************************
1144  * CallSymbol: calls a module symbol.
1145  *****************************************************************************
1146  * This function calls a symbol given its name and a module structure. The
1147  * symbol MUST refer to a function returning int and taking a module_t* as
1148  * an argument.
1149  *****************************************************************************/
1150 static int CallSymbol( module_t * p_module, char * psz_name )
1151 {
1152     int (* pf_symbol) ( module_t * p_module );
1153
1154     /* Try to resolve the symbol */
1155     pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1156
1157     if( pf_symbol == NULL )
1158     {
1159         char psz_buffer[256];
1160
1161         /* We couldn't load the symbol */
1162         msg_Warn( p_module, "cannot find symbol %s in module %s (%s)",
1163                             psz_name, p_module->is.plugin.psz_filename,
1164                             module_error( psz_buffer ) );
1165         return -1;
1166     }
1167
1168     /* We can now try to call the symbol */
1169     if( pf_symbol( p_module ) != 0 )
1170     {
1171         /* With a well-written module we shouldn't have to print an
1172          * additional error message here, but just make sure. */
1173         msg_Err( p_module, "failed calling symbol %s in module %s",
1174                            psz_name, p_module->is.plugin.psz_filename );
1175         return -1;
1176     }
1177
1178     /* Everything worked fine, we can return */
1179     return 0;
1180 }
1181 #endif /* HAVE_DYNAMIC_PLUGINS */
1182