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