]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* ./src/misc/modules.c: module_Need accepts a comma-separated list of
[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.70 2002/07/03 19:40:49 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 = 1;
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     if( psz_name && *psz_name )
292     {
293         char *psz_parser;
294
295         psz_shortcuts = strdup( psz_name );
296
297         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
298         {
299             if( *psz_parser == ',' )
300             {
301                  *psz_parser = '\0';
302                  i_shortcuts++;
303             }
304         }
305     }
306
307     /* Sort the modules and test them */
308     p_list = malloc( p_this->p_vlc->module_bank.i_count
309                       * sizeof( module_list_t ) );
310     p_first = NULL;
311
312     /* Parse the module list for capabilities and probe each of them */
313     for( p_module = p_this->p_vlc->module_bank.first ;
314          p_module != NULL ;
315          p_module = p_module->next )
316     {
317         int i_shortcut_bonus = 0;
318
319         /* Test that this module can do everything we need */
320         if( !(p_module->i_capabilities & ( 1 << i_capability )) )
321         {
322             continue;
323         }
324
325         /* Test if we have the required CPU */
326         if( (p_module->i_cpu_capabilities
327                 & p_this->p_vlc->i_cpu_capabilities)
328               != p_module->i_cpu_capabilities )
329         {
330             continue;
331         }
332
333         /* If we required a shortcut, check this plugin provides it. */
334         if( psz_shortcuts )
335         {
336             vlc_bool_t b_trash = VLC_TRUE;
337             int i_dummy, i_short = i_shortcuts;
338             char *psz_name = psz_shortcuts;
339
340             while( i_short )
341             {
342                 for( i_dummy = 0;
343                      b_trash && p_module->pp_shortcuts[i_dummy];
344                      i_dummy++ )
345                 {
346                     b_trash = strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
347                 }
348
349                 if( !b_trash )
350                 {
351                     i_shortcut_bonus = i_short * 10000;
352                     break;
353                 }
354
355                 while( *psz_name )
356                 {
357                     psz_name++;
358                 }
359                 psz_name++;
360                 i_short--;
361             }
362
363             if( b_trash )
364             {
365                 continue;
366             }
367         }
368         /* If we didn't require a shortcut, trash zero-scored plugins */
369         else if( !p_module->pi_score[i_capability] )
370         {
371             continue;
372         }
373
374         /* Special case: test if we requested a particular intf plugin */
375         if( i_capability == MODULE_CAPABILITY_INTF )
376         {
377             if( p_module->psz_program != NULL
378                  && !strcmp( p_module->psz_program,
379                              p_this->p_vlc->psz_object_name ) )
380             {
381                 if( !b_intf ) 
382                 {
383                     /* Remove previous non-matching plugins */
384                     i_index = 0;
385                     b_intf = VLC_TRUE;
386                 }
387             }
388             else
389             {
390                 if( b_intf )
391                 {
392                     /* This one doesn't match */
393                     continue;
394                 }
395             }
396         }
397
398         /* Store this new module */
399         p_list[ i_index ].p_module = p_module;
400         p_list[ i_index ].i_score = p_module->pi_score[i_capability]
401                                      + i_shortcut_bonus;
402
403         /* Add it to the modules-to-probe list */
404         if( i_index == 0 )
405         {
406             p_list[ 0 ].p_next = NULL;
407             p_first = p_list;
408         }
409         else
410         {
411             /* Ok, so at school you learned that quicksort is quick, and
412              * bubble sort sucks raw eggs. But that's when dealing with
413              * thousands of items. Here we have barely 50. */
414             module_list_t *p_newlist = p_first;
415
416             if( p_first->i_score < p_list[ i_index ].i_score )
417             {
418                 p_list[ i_index ].p_next = p_first;
419                 p_first = &p_list[ i_index ];
420             }
421             else
422             {
423                 while( p_newlist->p_next != NULL &&
424                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
425                 {
426                     p_newlist = p_newlist->p_next;
427                 }
428
429                 p_list[ i_index ].p_next = p_newlist->p_next;
430                 p_newlist->p_next = &p_list[ i_index ];
431             }
432         }
433
434         i_index++;
435     }
436
437     /* Lock all selected modules */
438     p_tmp = p_first;
439     while( p_tmp != NULL )
440     {
441         LockModule( p_tmp->p_module );
442         p_tmp = p_tmp->p_next;
443     }
444
445     /* We can release the global lock, module refcounts were incremented */
446     vlc_mutex_unlock( &p_this->p_vlc->module_bank.lock );
447
448     /* Parse the linked list and use the first successful module */
449     p_tmp = p_first;
450     while( p_tmp != NULL )
451     {
452         /* Test the requested capability */
453         switch( i_capability )
454         {
455             case MODULE_CAPABILITY_ACCESS:
456                 i_ret = p_tmp->p_module->p_functions->access.functions.
457                               access.pf_open( (input_thread_t *)p_data );
458                 break;
459
460             case MODULE_CAPABILITY_DEMUX:
461                 i_ret = p_tmp->p_module->p_functions->demux.functions.
462                               demux.pf_init( (input_thread_t *)p_data );
463                 break;
464
465             case MODULE_CAPABILITY_NETWORK:
466                 i_ret = p_tmp->p_module->p_functions->network.functions.
467                          network.pf_open( p_this, (network_socket_t *)p_data );
468                 break;
469
470             case MODULE_CAPABILITY_DECODER:
471                 i_ret = p_tmp->p_module->p_functions->dec.functions.
472                               dec.pf_probe( (u8 *)p_data );
473                 break;
474
475             case MODULE_CAPABILITY_INTF:
476                 i_ret = p_tmp->p_module->p_functions->intf.functions.
477                               intf.pf_open( (intf_thread_t *)p_data );
478                 break;
479
480             case MODULE_CAPABILITY_AOUT:
481                 i_ret = p_tmp->p_module->p_functions->aout.functions.
482                               aout.pf_open( (aout_thread_t *)p_data );
483                 break;
484
485             case MODULE_CAPABILITY_VOUT:
486             case MODULE_CAPABILITY_VOUT_FILTER:
487                 i_ret = p_tmp->p_module->p_functions->vout.functions.
488                               vout.pf_create( (vout_thread_t *)p_data );
489                 break;
490
491             case MODULE_CAPABILITY_CHROMA:
492                 i_ret = p_tmp->p_module->p_functions->chroma.functions.
493                               chroma.pf_init( (vout_thread_t *)p_data );
494                 break;
495
496             case MODULE_CAPABILITY_IDCT:
497             case MODULE_CAPABILITY_IMDCT:
498             case MODULE_CAPABILITY_MOTION:
499             case MODULE_CAPABILITY_DOWNMIX:
500             case MODULE_CAPABILITY_MEMCPY:
501                 /* This one always works */
502                 i_ret = 0;
503                 break;
504
505             default:
506                 msg_Err( p_this, "unknown module type %i", i_capability );
507                 i_ret = -1;
508                 break;
509         }
510
511         /* If the high score was broken, we have a new champion */
512         if( i_ret == 0 )
513         {
514             break;
515         }
516         else
517         {
518             UnlockModule( p_tmp->p_module );
519         }
520
521         p_tmp = p_tmp->p_next;
522     }
523
524     /* Store the locked module value */
525     if( p_tmp != NULL )
526     {
527         p_module = p_tmp->p_module;
528         p_tmp = p_tmp->p_next;
529     }
530     else
531     {
532         p_module = NULL;
533     }
534
535     /* Unlock the remaining modules */
536     while( p_tmp != NULL )
537     {
538         UnlockModule( p_tmp->p_module );
539         p_tmp = p_tmp->p_next;
540     }
541
542     free( p_list );
543
544     if( p_module != NULL )
545     {
546         msg_Info( p_module, "found and locked %s module `%s'",
547                   MODULE_CAPABILITY( i_capability ),
548                   p_module->psz_object_name );
549     }
550     else if( p_first == NULL )
551     {
552         msg_Err( p_this, "no available %s module matched `%s'",
553                  MODULE_CAPABILITY( i_capability ), psz_name );
554     }
555     else if( psz_name != NULL && *psz_name )
556     {
557         msg_Err( p_this, "could not load any %s module matching `%s'",
558                  MODULE_CAPABILITY( i_capability ), psz_name );
559     }
560
561     if( psz_shortcuts )
562     {
563         free( psz_shortcuts );
564     }
565
566     /* Don't forget that the module is still locked */
567     return p_module;
568 }
569
570 /*****************************************************************************
571  * module_Unneed: decrease the usage count of a module.
572  *****************************************************************************
573  * This function must be called by the thread that called module_Need, to
574  * decrease the reference count and allow for hiding of modules.
575  *****************************************************************************/
576 void module_Unneed( module_t * p_module )
577 {
578     /* We take the global lock */
579     vlc_mutex_lock( &p_module->p_vlc->module_bank.lock );
580
581     /* Just unlock the module - we can't do anything if it fails,
582      * so there is no need to check the return value. */
583     UnlockModule( p_module );
584
585     msg_Info( p_module, "unlocking module `%s'", p_module->psz_object_name );
586
587     /* We release the global lock */
588     vlc_mutex_unlock( &p_module->p_vlc->module_bank.lock );
589
590     return;
591 }
592
593 /*****************************************************************************
594  * Following functions are local.
595  *****************************************************************************/
596
597 /*****************************************************************************
598  * AllocateAllPlugins: load all plugin modules we can find.
599  *****************************************************************************/
600 #ifdef HAVE_DYNAMIC_PLUGINS
601 static void AllocateAllPlugins( vlc_object_t *p_this )
602 {
603     /* Yes, there are two NULLs because we replace one with "plugin-path". */
604     char *          path[] = { "plugins", PLUGIN_PATH, NULL, NULL };
605
606     char **         ppsz_path = path;
607     char *          psz_fullpath;
608     char *          psz_file;
609 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
610     char *          psz_vlcpath = system_GetProgramPath();
611     int             i_vlclen = strlen( psz_vlcpath );
612     vlc_bool_t      b_notinroot;
613 #endif
614     DIR *           dir;
615     struct dirent * file;
616
617     /* If the user provided a plugin path, we add it to the list */
618     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
619                                                             "plugin-path" );
620
621     for( ; *ppsz_path != NULL ; ppsz_path++ )
622     {
623         /* Store strlen(*ppsz_path) for later use. */
624         int i_dirlen = strlen( *ppsz_path );
625
626 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
627         b_notinroot = VLC_FALSE;
628         /* Under BeOS, we need to add beos_GetProgramPath() to access
629          * files under the current directory */
630         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
631         {
632             i_dirlen += i_vlclen + 2;
633             b_notinroot = VLC_TRUE;
634
635             psz_fullpath = malloc( i_dirlen );
636             if( psz_fullpath == NULL )
637             {
638                 continue;
639             }
640             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
641         }
642         else
643 #endif
644         {
645             psz_fullpath = *ppsz_path;
646         }
647
648         msg_Dbg( p_this, "browsing `%s'", psz_fullpath );
649
650         if( (dir = opendir( psz_fullpath )) )
651         {
652             /* Parse the directory and try to load all files it contains. */
653             while( (file = readdir( dir )) )
654             {
655                 int i_filelen = strlen( file->d_name );
656
657                 /* We only load files ending with ".so" */
658                 if( i_filelen > 3
659                         && !strncmp( file->d_name + i_filelen - 3, ".so", 3 ) )
660                 {
661                     psz_file = malloc( i_dirlen + i_filelen + 2 );
662                     if( psz_file == NULL )
663                     {
664                         continue;
665                     }
666                     sprintf( psz_file, "%s/%s", psz_fullpath, file->d_name );
667
668                     /* We created a nice filename -- now we just try to load
669                      * it as a plugin module. */
670                     AllocatePluginModule( p_this, psz_file );
671
672                     /* We don't care if the allocation succeeded */
673                     free( psz_file );
674                 }
675             }
676
677             /* Close the directory if successfully opened */
678             closedir( dir );
679         }
680
681 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
682         if( b_notinroot )
683         {
684             free( psz_fullpath );
685         }
686 #endif
687     }
688 }
689
690 /*****************************************************************************
691  * AllocatePluginModule: load a module into memory and initialize it.
692  *****************************************************************************
693  * This function loads a dynamically loadable module and allocates a structure
694  * for its information data. The module can then be handled by module_Need,
695  * module_Unneed and HideModule. It can be removed by DeleteModule.
696  *****************************************************************************/
697 static int AllocatePluginModule( vlc_object_t * p_this, char * psz_filename )
698 {
699     char **pp_shortcut;
700     module_t * p_module, * p_othermodule;
701     module_handle_t handle;
702
703     /* Try to dynamically load the module. */
704     if( module_load( psz_filename, &handle ) )
705     {
706         char psz_buffer[256];
707
708         /* The plugin module couldn't be opened */
709         msg_Warn( p_this, "cannot open %s (%s)",
710                   psz_filename, module_error( psz_buffer ) );
711         return -1;
712     }
713
714     /* Now that we have successfully loaded the module, we can
715      * allocate a structure for it */ 
716     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
717     if( p_module == NULL )
718     {
719         msg_Err( p_this, "out of memory" );
720         module_unload( handle );
721         return -1;
722     }
723
724     /* We need to fill these since they may be needed by CallSymbol() */
725     p_module->is.plugin.psz_filename = psz_filename;
726     p_module->is.plugin.handle = handle;
727     p_module->p_symbols = &symbols;
728
729     /* Initialize the module: fill p_module->psz_object_name, default config */
730     if( CallSymbol( p_module, "InitModule" MODULE_SUFFIX ) != 0 )
731     {
732         /* We couldn't call InitModule() */
733         vlc_object_destroy( p_module );
734         module_unload( handle );
735         return -1;
736     }
737
738     /* Check that we don't already have a module with this name */
739     for( p_othermodule = p_this->p_vlc->module_bank.first ;
740          p_othermodule != NULL ;
741          p_othermodule = p_othermodule->next )
742     {
743         if( !strcmp( p_othermodule->psz_object_name,
744                      p_module->psz_object_name ) )
745         {
746             msg_Warn( p_this,
747                       "cannot load %s, a module named `%s' already exists",
748                       psz_filename, p_module->psz_object_name );
749             config_Free( p_module );
750             vlc_object_destroy( p_module );
751             module_unload( handle );
752             return -1;
753         }
754     }
755
756     /* Activate the module : fill the capability structure, etc. */
757     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
758     {
759         /* We couldn't call ActivateModule() */
760         config_Free( p_module );
761         vlc_object_destroy( p_module );
762         module_unload( handle );
763         return -1;
764     }
765
766     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
767     {
768         *pp_shortcut = strdup( *pp_shortcut );
769     }
770
771     /* We strdup() these entries so that they are still valid when the
772      * module is unloaded. */
773     p_module->is.plugin.psz_filename =
774             strdup( p_module->is.plugin.psz_filename );
775     p_module->psz_object_name = strdup( p_module->psz_object_name );
776     p_module->psz_longname = strdup( p_module->psz_longname );
777
778     if( p_module->is.plugin.psz_filename == NULL 
779             || p_module->psz_object_name == NULL
780             || p_module->psz_longname == NULL )
781     {
782         msg_Err( p_this, "out of memory" );
783
784         free( p_module->is.plugin.psz_filename );
785         free( p_module->psz_object_name );
786         free( p_module->psz_longname );
787         free( p_module->psz_program );
788
789         config_Free( p_module );
790         vlc_object_destroy( p_module );
791         module_unload( handle );
792         return -1;
793     }
794
795     if( p_module->psz_program != NULL )
796     {
797         p_module->psz_program = strdup( p_module->psz_program );
798     }
799
800     /* Everything worked fine ! The module is ready to be added to the list. */
801     p_module->i_usage = 0;
802     p_module->i_unused_delay = 0;
803
804     p_module->b_builtin = VLC_FALSE;
805
806     /* Link module into the linked list */
807     if( p_this->p_vlc->module_bank.first != NULL )
808     {
809         p_this->p_vlc->module_bank.first->prev = p_module;
810     }
811     p_module->next = p_this->p_vlc->module_bank.first;
812     p_module->prev = NULL;
813     p_this->p_vlc->module_bank.first = p_module;
814     p_this->p_vlc->module_bank.i_count++;
815
816     msg_Dbg( p_this, "plugin `%s', %s",
817              p_module->psz_object_name, p_module->psz_longname );
818
819     vlc_object_attach( p_module, p_this );
820
821     return 0;
822 }
823 #endif /* HAVE_DYNAMIC_PLUGINS */
824
825 /*****************************************************************************
826  * AllocateBuiltinModule: initialize a builtin module.
827  *****************************************************************************
828  * This function registers a builtin module and allocates a structure
829  * for its information data. The module can then be handled by module_Need,
830  * module_Unneed and HideModule. It can be removed by DeleteModule.
831  *****************************************************************************/
832 static int AllocateBuiltinModule( vlc_object_t * p_this,
833                                   int ( *pf_init ) ( module_t * ),
834                                   int ( *pf_activate ) ( module_t * ),
835                                   int ( *pf_deactivate ) ( module_t * ) )
836 {
837     module_t * p_module, * p_othermodule;
838
839     /* Now that we have successfully loaded the module, we can
840      * allocate a structure for it */ 
841     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
842     if( p_module == NULL )
843     {
844         msg_Err( p_this, "out of memory" );
845         return -1;
846     }
847
848     /* Initialize the module : fill p_module->psz_object_name, etc. */
849     if( pf_init( p_module ) != 0 )
850     {
851         /* With a well-written module we shouldn't have to print an
852          * additional error message here, but just make sure. */
853         msg_Err( p_this, "failed calling init in builtin module" );
854         vlc_object_destroy( p_module );
855         return -1;
856     }
857
858     /* Check that we don't already have a module with this name */
859     for( p_othermodule = p_this->p_vlc->module_bank.first ;
860          p_othermodule != NULL ;
861          p_othermodule = p_othermodule->next )
862     {
863         if( !strcmp( p_othermodule->psz_object_name,
864                      p_module->psz_object_name ) )
865         {
866             msg_Warn( p_this, "cannot load builtin `%s', "
867                       "a module named `%s' already exists",
868                       p_module->psz_object_name, p_module->psz_object_name );
869             config_Free( p_module );
870             vlc_object_destroy( p_module );
871             return -1;
872         }
873     }
874
875     if( pf_activate( p_module ) != 0 )
876     {
877         /* With a well-written module we shouldn't have to print an
878          * additional error message here, but just make sure. */
879         msg_Err( p_this, "failed calling activate in builtin module" );
880         config_Free( p_module );
881         vlc_object_destroy( p_module );
882         return -1;
883     }
884
885     /* Everything worked fine ! The module is ready to be added to the list. */
886     p_module->i_usage = 0;
887     p_module->i_unused_delay = 0;
888
889     p_module->b_builtin = VLC_TRUE;
890     p_module->is.builtin.pf_deactivate = pf_deactivate;
891
892     /* Link module into the linked list */
893     if( p_this->p_vlc->module_bank.first != NULL )
894     {
895         p_this->p_vlc->module_bank.first->prev = p_module;
896     }
897     p_module->next = p_this->p_vlc->module_bank.first;
898     p_module->prev = NULL;
899     p_this->p_vlc->module_bank.first = p_module;
900     p_this->p_vlc->module_bank.i_count++;
901
902     msg_Dbg( p_this, "builtin `%s', %s",
903              p_module->psz_object_name, p_module->psz_longname );
904
905     vlc_object_attach( p_module, p_this );
906
907     return 0;
908 }
909
910 /*****************************************************************************
911  * DeleteModule: delete a module and its structure.
912  *****************************************************************************
913  * This function can only be called if i_usage <= 0.
914  *****************************************************************************/
915 static int DeleteModule( module_t * p_module )
916 {
917     /* If the module is not in use but is still in memory, we first have
918      * to hide it and remove it from memory before we can free the
919      * data structure. */
920     if( p_module->b_builtin )
921     {
922         if( p_module->i_usage != 0 )
923         {
924             msg_Err( p_module, "trying to free builtin module `%s' with "
925                      "usage %i", p_module->psz_object_name, p_module->i_usage );
926             return -1;
927         }
928         else
929         {
930             /* We deactivate the module now. */
931             p_module->is.builtin.pf_deactivate( p_module );
932         }
933     }
934 #ifdef HAVE_DYNAMIC_PLUGINS
935     else
936     {
937         if( p_module->i_usage >= 1 )
938         {
939             msg_Err( p_module, "trying to free module `%s' which is "
940                                "still in use", p_module->psz_object_name );
941             return -1;
942         }
943
944         /* Two possibilities here: i_usage == -1 and the module is already
945          * unloaded, we can continue, or i_usage == 0, and we have to hide
946          * the module before going on. */
947         if( p_module->i_usage == 0 )
948         {
949             if( HideModule( p_module ) != 0 )
950             {
951                 return -1;
952             }
953         }
954     }
955 #endif
956
957     vlc_object_detach_all( p_module );
958
959     /* Unlink the module from the linked list. */
960     if( p_module->prev != NULL )
961     {
962         p_module->prev->next = p_module->next;
963     }
964     else
965     {
966         p_module->p_vlc->module_bank.first = p_module->next;
967     }
968
969     if( p_module->next != NULL )
970     {
971         p_module->next->prev = p_module->prev;
972     }
973
974     p_module->p_vlc->module_bank.i_count--;
975
976     /* We free the structures that we strdup()ed in Allocate*Module(). */
977 #ifdef HAVE_DYNAMIC_PLUGINS
978     if( !p_module->b_builtin )
979     {
980         char **pp_shortcut = p_module->pp_shortcuts;
981
982         for( ; *pp_shortcut ; pp_shortcut++ )
983         {
984             free( *pp_shortcut );
985         }
986
987         free( p_module->is.plugin.psz_filename );
988         free( p_module->psz_object_name );
989         free( p_module->psz_longname );
990         free( p_module->psz_program );
991     }
992 #endif
993
994     config_Free( p_module );
995     vlc_object_destroy( p_module );
996
997     return 0;
998 }
999
1000 /*****************************************************************************
1001  * LockModule: increase the usage count of a module and load it if needed.
1002  *****************************************************************************
1003  * This function has to be called before a thread starts using a module. If
1004  * the module is already loaded, we just increase its usage count. If it isn't
1005  * loaded, we have to dynamically open it and initialize it.
1006  * If you successfully call LockModule() at any moment, be careful to call
1007  * UnlockModule() when you don't need it anymore.
1008  *****************************************************************************/
1009 static int LockModule( module_t * p_module )
1010 {
1011     if( p_module->i_usage >= 0 )
1012     {
1013         /* This module is already loaded and activated, we can return */
1014         p_module->i_usage++;
1015         return 0;
1016     }
1017
1018     if( p_module->b_builtin )
1019     {
1020         /* A builtin module should always have a refcount >= 0 ! */
1021         msg_Err( p_module, "builtin module `%s' has refcount %i",
1022                            p_module->psz_object_name, p_module->i_usage );
1023         return -1;
1024     }
1025
1026 #ifdef HAVE_DYNAMIC_PLUGINS
1027     if( p_module->i_usage != -1 )
1028     {
1029         /* This shouldn't happen. Ever. We have serious problems here. */
1030         msg_Err( p_module, "plugin module `%s' has refcount %i",
1031                            p_module->psz_object_name, p_module->i_usage );
1032         return -1;
1033     }
1034
1035     /* i_usage == -1, which means that the module isn't in memory */
1036     if( module_load( p_module->is.plugin.psz_filename,
1037                      &p_module->is.plugin.handle ) )
1038     {
1039         char psz_buffer[256];
1040
1041         /* The plugin module couldn't be opened */
1042         msg_Err( p_module, "cannot open %s (%s)",
1043                  p_module->is.plugin.psz_filename, module_error(psz_buffer) );
1044         return -1;
1045     }
1046
1047     /* FIXME: what to do if the guy modified the plugin while it was
1048      * unloaded ? It makes XMMS crash nastily, perhaps we should try
1049      * to be a bit more clever here. */
1050
1051     /* Activate the module : fill the capability structure, etc. */
1052     if( CallSymbol( p_module, "ActivateModule" MODULE_SUFFIX ) != 0 )
1053     {
1054         /* We couldn't call ActivateModule() -- looks nasty, but
1055          * we can't do much about it. Just try to unload module. */
1056         module_unload( p_module->is.plugin.handle );
1057         p_module->i_usage = -1;
1058         return -1;
1059     }
1060
1061     /* Everything worked fine ! The module is ready to be used */
1062     p_module->i_usage = 1;
1063 #endif /* HAVE_DYNAMIC_PLUGINS */
1064
1065     return 0;
1066 }
1067
1068 /*****************************************************************************
1069  * UnlockModule: decrease the usage count of a module.
1070  *****************************************************************************
1071  * We decrease the usage count of a module so that we know when a module
1072  * becomes unused and can be hidden.
1073  *****************************************************************************/
1074 static int UnlockModule( module_t * p_module )
1075 {
1076     if( p_module->i_usage <= 0 )
1077     {
1078         /* This shouldn't happen. Ever. We have serious problems here. */
1079         msg_Err( p_module, "trying to call module_Unneed() on `%s' "
1080                            "which is not in use", p_module->psz_object_name );
1081         return -1;
1082     }
1083
1084     /* This module is still in use, we can return */
1085     p_module->i_usage--;
1086     p_module->i_unused_delay = 0;
1087
1088     return 0;
1089 }
1090
1091 #ifdef HAVE_DYNAMIC_PLUGINS
1092 /*****************************************************************************
1093  * HideModule: remove a module from memory but keep its structure.
1094  *****************************************************************************
1095  * This function can only be called if i_usage == 0. It will make a call
1096  * to the module's inner DeactivateModule() symbol, and then unload it
1097  * from memory. A call to module_Need() will automagically load it again.
1098  *****************************************************************************/
1099 static int HideModule( module_t * p_module )
1100 {
1101     if( p_module->b_builtin )
1102     {
1103         /* A builtin module should never be hidden. */
1104         msg_Err( p_module, "trying to hide builtin module `%s'",
1105                            p_module->psz_object_name );
1106         return -1;
1107     }
1108
1109     if( p_module->i_usage >= 1 )
1110     {
1111         msg_Err( p_module, "trying to hide module `%s' which is still "
1112                            "in use", p_module->psz_object_name );
1113         return -1;
1114     }
1115
1116     if( p_module->i_usage <= -1 )
1117     {
1118         msg_Err( p_module, "trying to hide module `%s' which is already "
1119                            "hidden", p_module->psz_object_name );
1120         return -1;
1121     }
1122
1123     /* Deactivate the module : free the capability structure, etc. */
1124     if( CallSymbol( p_module, "DeactivateModule" MODULE_SUFFIX ) != 0 )
1125     {
1126         /* We couldn't call DeactivateModule() -- looks nasty, but
1127          * we can't do much about it. Just try to unload module anyway. */
1128         module_unload( p_module->is.plugin.handle );
1129         p_module->i_usage = -1;
1130         return -1;
1131     }
1132
1133     /* Everything worked fine, we can safely unload the module. */
1134     module_unload( p_module->is.plugin.handle );
1135     p_module->i_usage = -1;
1136
1137     return 0;
1138 }
1139
1140 /*****************************************************************************
1141  * CallSymbol: calls a module symbol.
1142  *****************************************************************************
1143  * This function calls a symbol given its name and a module structure. The
1144  * symbol MUST refer to a function returning int and taking a module_t* as
1145  * an argument.
1146  *****************************************************************************/
1147 static int CallSymbol( module_t * p_module, char * psz_name )
1148 {
1149     int (* pf_symbol) ( module_t * p_module );
1150
1151     /* Try to resolve the symbol */
1152     pf_symbol = module_getsymbol( p_module->is.plugin.handle, psz_name );
1153
1154     if( pf_symbol == NULL )
1155     {
1156         char psz_buffer[256];
1157
1158         /* We couldn't load the symbol */
1159         msg_Warn( p_module, "cannot find symbol %s in module %s (%s)",
1160                             psz_name, p_module->is.plugin.psz_filename,
1161                             module_error( psz_buffer ) );
1162         return -1;
1163     }
1164
1165     /* We can now try to call the symbol */
1166     if( pf_symbol( p_module ) != 0 )
1167     {
1168         /* With a well-written module we shouldn't have to print an
1169          * additional error message here, but just make sure. */
1170         msg_Err( p_module, "failed calling symbol %s in module %s",
1171                            psz_name, p_module->is.plugin.psz_filename );
1172         return -1;
1173     }
1174
1175     /* Everything worked fine, we can return */
1176     return 0;
1177 }
1178 #endif /* HAVE_DYNAMIC_PLUGINS */
1179