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