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