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