]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* ./include/*, ./src/*: separated WIN32 #tests and UNDER_CE #tests, because
[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.103 2002/11/11 14:39:12 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 #ifdef HAVE_DIRENT_H
39 #   include <dirent.h>
40 #else
41 #   include "../extras/dirent.h"
42 #endif
43
44 #ifdef HAVE_SYS_TYPES_H
45 #   include <sys/types.h>
46 #endif
47 #ifdef HAVE_SYS_STAT_H
48 #   include <sys/stat.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #   include <unistd.h>
52 #endif
53
54 #if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
55 #   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
56 #   define HAVE_DYNAMIC_PLUGINS
57 #elif defined(HAVE_IMAGE_H)                                          /* BeOS */
58 #   include <image.h>
59 #   define HAVE_DYNAMIC_PLUGINS
60 #elif defined(UNDER_CE)
61 #   define HAVE_DYNAMIC_PLUGINS
62 #elif defined(WIN32)
63 #   define HAVE_DYNAMIC_PLUGINS
64 #else
65 #   undef HAVE_DYNAMIC_PLUGINS
66 #endif
67
68 #include "error.h"
69 #include "netutils.h"
70
71 #include "interface.h"
72 #include "vlc_playlist.h"
73 #include "intf_eject.h"
74
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
79
80 #include "video.h"
81 #include "video_output.h"
82
83 #include "audio_output.h"
84 #include "aout_internal.h"
85
86 #include "stream_output.h"
87
88 #include "iso_lang.h"
89
90 #ifdef HAVE_DYNAMIC_PLUGINS
91 #   include "modules_plugin.h"
92 #endif
93
94 #if !defined( _MSC_VER )
95 #    include "modules_builtin.h"
96 #else
97 #    include "modules_builtin_msvc.h"
98 #endif
99
100 /*****************************************************************************
101  * Local prototypes
102  *****************************************************************************/
103 #ifdef HAVE_DYNAMIC_PLUGINS
104 static void AllocateAllPlugins   ( vlc_object_t * );
105 static void AllocatePluginDir    ( vlc_object_t *, const char *, int );
106 static int  AllocatePluginFile   ( vlc_object_t *, char * );
107 #endif
108 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
109 static int  DeleteModule ( module_t * );
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 static void DupModule    ( module_t * );
112 static void UndupModule  ( module_t * );
113 static int  CallEntry    ( module_t * );
114 #endif
115
116 /*****************************************************************************
117  * module_InitBank: create the module bank.
118  *****************************************************************************
119  * This function creates a module bank structure which will be filled later
120  * on with all the modules found.
121  *****************************************************************************/
122 void __module_InitBank( vlc_object_t *p_this )
123 {
124     module_bank_t *p_bank;
125
126     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
127     p_bank->psz_object_name = "module bank";
128
129     /*
130      * Store the symbols to be exported
131      */
132 #ifdef HAVE_DYNAMIC_PLUGINS
133     STORE_SYMBOLS( &p_bank->symbols );
134 #endif
135
136     /* Everything worked, attach the object */
137     p_this->p_libvlc->p_module_bank = p_bank;
138     vlc_object_attach( p_bank, p_this->p_libvlc );
139
140     return;
141 }
142
143 /*****************************************************************************
144  * module_ResetBank: reset the module bank.
145  *****************************************************************************
146  * This function resets the module bank by unloading all unused plugin
147  * modules.
148  *****************************************************************************/
149 void __module_ResetBank( vlc_object_t *p_this )
150 {
151     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
152     return;
153 }
154
155 /*****************************************************************************
156  * module_EndBank: empty the module bank.
157  *****************************************************************************
158  * This function unloads all unused plugin modules and empties the module
159  * bank in case of success.
160  *****************************************************************************/
161 void __module_EndBank( vlc_object_t *p_this )
162 {
163     module_t * p_next;
164
165     vlc_object_detach( p_this->p_libvlc->p_module_bank );
166
167     while( p_this->p_libvlc->p_module_bank->i_children )
168     {
169         p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
170
171         if( DeleteModule( p_next ) )
172         {
173             /* Module deletion failed */
174             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
175                      p_next->psz_object_name );
176
177             /* We just free the module by hand. Niahahahahaha. */
178             vlc_object_detach( p_next );
179             vlc_object_destroy( p_next );
180         }
181     }
182
183     vlc_object_destroy( p_this->p_libvlc->p_module_bank );
184
185     return;
186 }
187
188 /*****************************************************************************
189  * module_LoadMain: load the main program info into the module bank.
190  *****************************************************************************
191  * This function fills the module bank structure with the main module infos.
192  * This is very useful as it will allow us to consider the main program just
193  * as another module, and for instance the configuration options of main will
194  * be available in the module bank structure just as for every other module.
195  *****************************************************************************/
196 void __module_LoadMain( vlc_object_t *p_this )
197 {
198     AllocateBuiltinModule( p_this, vlc_entry__main );
199 }
200
201 /*****************************************************************************
202  * module_LoadBuiltins: load all modules which we built with.
203  *****************************************************************************
204  * This function fills the module bank structure with the builtin modules.
205  *****************************************************************************/
206 void __module_LoadBuiltins( vlc_object_t * p_this )
207 {
208     msg_Dbg( p_this, "checking builtin modules" );
209     ALLOCATE_ALL_BUILTINS();
210 }
211
212 /*****************************************************************************
213  * module_LoadPlugins: load all plugin modules we can find.
214  *****************************************************************************
215  * This function fills the module bank structure with the plugin modules.
216  *****************************************************************************/
217 void __module_LoadPlugins( vlc_object_t * p_this )
218 {
219 #ifdef HAVE_DYNAMIC_PLUGINS
220     msg_Dbg( p_this, "checking plugin modules" );
221     AllocateAllPlugins( p_this );
222 #endif
223 }
224
225 /*****************************************************************************
226  * module_Need: return the best module function, given a capability list.
227  *****************************************************************************
228  * This function returns the module that best fits the asked capabilities.
229  *****************************************************************************/
230 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
231                           const char *psz_name )
232 {
233     typedef struct module_list_t module_list_t;
234
235     struct module_list_t
236     {
237         module_t *p_module;
238         int i_score;
239         module_list_t *p_next;
240     };
241
242     module_list_t *p_list, *p_first, *p_tmp;
243     vlc_list_t *p_all;
244
245     int i_index = 0;
246     vlc_bool_t b_intf = VLC_FALSE;
247
248     module_t *p_module;
249     module_t **pp_parser;
250
251     int   i_shortcuts = 0;
252     char *psz_shortcuts = NULL, *psz_var = NULL;
253
254     msg_Dbg( p_this, "looking for %s module", psz_capability );
255
256     /* Deal with variables */
257     if( psz_name && psz_name[0] == '$' )
258     {
259         psz_var = config_GetPsz( p_this, psz_name + 1 );
260         psz_name = psz_var;
261     }
262
263     /* Count how many different shortcuts were asked for */
264     if( psz_name && *psz_name )
265     {
266         char *psz_parser;
267
268         /* If the user wants none, give him none. */
269         if( !strcmp( psz_name, "none" ) )
270         {
271             if( psz_var ) free( psz_var );
272             return NULL;
273         }
274
275         i_shortcuts++;
276         psz_shortcuts = strdup( psz_name );
277
278         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
279         {
280             if( *psz_parser == ',' )
281             {
282                  *psz_parser = '\0';
283                  i_shortcuts++;
284             }
285         }
286     }
287
288     /* Sort the modules and test them */
289     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
290     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
291     p_first = NULL;
292
293     /* Parse the module list for capabilities and probe each of them */
294     for( pp_parser = (module_t**)p_all->pp_objects ; *pp_parser ; pp_parser++ )
295     {
296         module_t * p_submodule = NULL;
297         int i_shortcut_bonus = 0, i_submodule;
298
299         p_module = *pp_parser;
300
301         /* Test that this module can do what we need */
302         if( strcmp( p_module->psz_capability, psz_capability ) )
303         {
304             for( i_submodule = 0;
305                  i_submodule < p_module->i_children;
306                  i_submodule++ )
307             {
308                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
309                                            ->psz_capability, psz_capability ) )
310                 {
311                     p_submodule =
312                             (module_t*)p_module->pp_children[ i_submodule ];
313                     break;
314                 }
315             }
316
317             if( p_submodule == NULL )
318             {
319                 continue;
320             }
321
322             p_module = p_submodule;
323         }
324
325         /* Test if we have the required CPU */
326         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
327         {
328             continue;
329         }
330
331         /* If we required a shortcut, check this plugin provides it. */
332         if( i_shortcuts )
333         {
334             vlc_bool_t b_trash = VLC_TRUE;
335             int i_dummy, i_short = i_shortcuts;
336             char *psz_name = psz_shortcuts;
337
338             while( i_short )
339             {
340                 for( i_dummy = 0;
341                      b_trash && p_module->pp_shortcuts[i_dummy];
342                      i_dummy++ )
343                 {
344                     b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
345                         && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
346                 }
347
348                 if( !b_trash )
349                 {
350                     i_shortcut_bonus = i_short * 10000;
351                     break;
352                 }
353
354                 /* Go to the next shortcut... This is so lame! */
355                 while( *psz_name )
356                 {
357                     psz_name++;
358                 }
359                 psz_name++;
360                 i_short--;
361             }
362
363             if( b_trash )
364             {
365                 continue;
366             }
367         }
368         /* If we didn't require a shortcut, trash zero-scored plugins */
369         else if( !p_module->i_score )
370         {
371             continue;
372         }
373
374         /* Special case: test if we requested a particular intf plugin */
375         if( p_module->psz_program
376              && !strcmp( p_module->psz_program,
377                          p_this->p_vlc->psz_object_name ) )
378         {
379             if( !b_intf ) 
380             {
381                 /* Remove previous non-matching plugins */
382                 i_index = 0;
383                 b_intf = VLC_TRUE;
384             }
385         }
386         else if( b_intf )
387         {
388             /* This one doesn't match */
389             continue;
390         }
391
392         /* Store this new module */
393         p_list[ i_index ].p_module = p_module;
394         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
395
396         /* Add it to the modules-to-probe list */
397         if( i_index == 0 )
398         {
399             p_list[ 0 ].p_next = NULL;
400             p_first = p_list;
401         }
402         else
403         {
404             /* Ok, so at school you learned that quicksort is quick, and
405              * bubble sort sucks raw eggs. But that's when dealing with
406              * thousands of items. Here we have barely 50. */
407             module_list_t *p_newlist = p_first;
408
409             if( p_first->i_score < p_list[ i_index ].i_score )
410             {
411                 p_list[ i_index ].p_next = p_first;
412                 p_first = &p_list[ i_index ];
413             }
414             else
415             {
416                 while( p_newlist->p_next != NULL &&
417                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
418                 {
419                     p_newlist = p_newlist->p_next;
420                 }
421
422                 p_list[ i_index ].p_next = p_newlist->p_next;
423                 p_newlist->p_next = &p_list[ i_index ];
424             }
425         }
426
427         i_index++;
428     }
429
430     msg_Dbg( p_this, "probing %i candidate%s",
431                      i_index, i_index == 1 ? "" : "s" );
432
433     /* Lock all candidate modules */
434     p_tmp = p_first;
435     while( p_tmp != NULL )
436     {
437         vlc_object_yield( p_tmp->p_module );
438         p_tmp = p_tmp->p_next;
439     }
440
441     /* We can release the list, interesting modules were yielded */
442     vlc_list_release( p_all );
443
444     /* Parse the linked list and use the first successful module */
445     p_tmp = p_first;
446     while( p_tmp != NULL )
447     {
448         if( p_tmp->p_module->pf_activate
449              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
450         {
451             break;
452         }
453
454         vlc_object_release( p_tmp->p_module );
455         p_tmp = p_tmp->p_next;
456     }
457
458     /* Store the locked module value */
459     if( p_tmp != NULL )
460     {
461         p_module = p_tmp->p_module;
462         p_tmp = p_tmp->p_next;
463     }
464     else
465     {
466         p_module = NULL;
467     }
468
469     /* Unlock the remaining modules */
470     while( p_tmp != NULL )
471     {
472         vlc_object_release( p_tmp->p_module );
473         p_tmp = p_tmp->p_next;
474     }
475
476     free( p_list );
477
478     if( p_module != NULL )
479     {
480         msg_Dbg( p_module, "using %s module \"%s\"",
481                  psz_capability, p_module->psz_object_name );
482     }
483     else if( p_first == NULL )
484     {
485         msg_Err( p_this, "no %s module matched \"%s\"",
486                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
487     }
488     else if( psz_name != NULL && *psz_name )
489     {
490         msg_Err( p_this, "no %s module matching \"%s\" could be loaded",
491                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
492     }
493
494     if( psz_shortcuts )
495     {
496         free( psz_shortcuts );
497     }
498
499     if( psz_var )
500     {
501         free( psz_var );
502     }
503
504     /* Don't forget that the module is still locked */
505     return p_module;
506 }
507
508 /*****************************************************************************
509  * module_Unneed: decrease the usage count of a module.
510  *****************************************************************************
511  * This function must be called by the thread that called module_Need, to
512  * decrease the reference count and allow for hiding of modules.
513  *****************************************************************************/
514 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
515 {
516     /* Use the close method */
517     if( p_module->pf_deactivate )
518     {
519         p_module->pf_deactivate( p_this );
520     }
521
522     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
523
524     vlc_object_release( p_module );
525
526     return;
527 }
528
529 /*****************************************************************************
530  * Following functions are local.
531  *****************************************************************************/
532
533 /*****************************************************************************
534  * AllocateAllPlugins: load all plugin modules we can find.
535  *****************************************************************************/
536 #ifdef HAVE_DYNAMIC_PLUGINS
537 static void AllocateAllPlugins( vlc_object_t *p_this )
538 {
539     /* Yes, there are two NULLs because we replace one with "plugin-path". */
540     char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
541                                NULL };
542
543     char **         ppsz_path = path;
544     char *          psz_fullpath;
545 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
546     char *          psz_vlcpath = system_GetProgramPath();
547     int             i_vlclen = strlen( psz_vlcpath );
548     vlc_bool_t      b_notinroot;
549 #endif
550
551     /* If the user provided a plugin path, we add it to the list */
552     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
553                                                             "plugin-path" );
554
555     for( ; *ppsz_path != NULL ; ppsz_path++ )
556     {
557 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
558         /* Store strlen(*ppsz_path) for later use. */
559         int i_dirlen = strlen( *ppsz_path );
560
561         b_notinroot = VLC_FALSE;
562         /* Under BeOS, we need to add beos_GetProgramPath() to access
563          * files under the current directory */
564         if( ( i_dirlen > 1 ) && strncmp( *ppsz_path, "/", 1 ) )
565         {
566             i_dirlen += i_vlclen + 2;
567             b_notinroot = VLC_TRUE;
568
569             psz_fullpath = malloc( i_dirlen );
570             if( psz_fullpath == NULL )
571             {
572                 continue;
573             }
574             sprintf( psz_fullpath, "%s/%s", psz_vlcpath, *ppsz_path );
575         }
576         else
577 #endif
578         {
579             psz_fullpath = *ppsz_path;
580         }
581
582         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
583
584         /* Don't go deeper than 5 subdirectories */
585         AllocatePluginDir( p_this, psz_fullpath, 5 );
586
587 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
588         if( b_notinroot )
589         {
590             free( psz_fullpath );
591         }
592 #endif
593     }
594
595     /* Free plugin-path */
596     free( path[ sizeof(path)/sizeof(char*) - 2 ] );
597     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
598 }
599
600 /*****************************************************************************
601  * AllocatePluginDir: recursively parse a directory to look for plugins
602  *****************************************************************************/
603 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
604                                int i_maxdepth )
605 {
606     int    i_dirlen;
607     DIR *  dir;
608     char * psz_file;
609
610     struct dirent * file;
611
612     if( i_maxdepth < 0 )
613     {
614         return;
615     }
616
617     dir = opendir( psz_dir );
618
619     if( !dir )
620     {
621         return;
622     }
623
624     i_dirlen = strlen( psz_dir );
625
626     /* Parse the directory and try to load all files it contains. */
627     while( (file = readdir( dir )) )
628     {
629 #ifndef UNDER_CE
630         struct stat statbuf;
631 #endif
632         unsigned int i_len;
633
634         /* Skip ".", ".." and anything starting with "." */
635         if( !*file->d_name || *file->d_name == '.' )
636         {
637             continue;
638         }
639
640         i_len = strlen( file->d_name );
641
642         psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
643         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
644
645 #ifdef UNDER_CE
646         if( GetFileAttributes( psz_file ) & FILE_ATTRIBUTE_DIRECTORY )
647 #else
648         if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
649 #endif
650         {
651             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
652         }
653         else if( i_len > strlen( LIBEXT )
654                   /* We only load files ending with LIBEXT */
655                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
656                                    LIBEXT, strlen( LIBEXT ) ) )
657         {
658             AllocatePluginFile( p_this, psz_file );
659         }
660
661         free( psz_file );
662     }
663
664     /* Close the directory */
665     closedir( dir );
666 }
667
668 /*****************************************************************************
669  * AllocatePluginFile: load a module into memory and initialize it.
670  *****************************************************************************
671  * This function loads a dynamically loadable module and allocates a structure
672  * for its information data. The module can then be handled by module_Need
673  * and module_Unneed. It can be removed by DeleteModule.
674  *****************************************************************************/
675 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
676 {
677     module_t * p_module;
678     module_handle_t handle;
679
680     /* Try to dynamically load the module. */
681     if( module_load( psz_file, &handle ) )
682     {
683         char psz_buffer[256];
684
685         /* The plugin module couldn't be opened */
686         msg_Warn( p_this, "cannot open `%s' (%s)",
687                   psz_file, module_error( psz_buffer ) );
688         return -1;
689     }
690
691     /* Now that we have successfully loaded the module, we can
692      * allocate a structure for it */ 
693     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
694     if( p_module == NULL )
695     {
696         msg_Err( p_this, "out of memory" );
697         module_unload( handle );
698         return -1;
699     }
700
701     /* We need to fill these since they may be needed by CallEntry() */
702     p_module->psz_filename = psz_file;
703     p_module->handle = handle;
704     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
705
706     /* Initialize the module: fill p_module->psz_object_name, default config */
707     if( CallEntry( p_module ) != 0 )
708     {
709         /* We couldn't call module_init() */
710         vlc_object_destroy( p_module );
711         module_unload( handle );
712         return -1;
713     }
714
715     DupModule( p_module );
716     p_module->psz_filename = strdup( p_module->psz_filename );
717     p_module->psz_longname = strdup( p_module->psz_longname );
718
719     /* Everything worked fine ! The module is ready to be added to the list. */
720     p_module->b_builtin = VLC_FALSE;
721
722     /* msg_Dbg( p_this, "plugin \"%s\", %s",
723                 p_module->psz_object_name, p_module->psz_longname ); */
724
725     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
726
727     return 0;
728 }
729
730 /*****************************************************************************
731  * DupModule: make a plugin module standalone.
732  *****************************************************************************
733  * This function duplicates all strings in the module, so that the dynamic
734  * object can be unloaded. It acts recursively on submodules.
735  *****************************************************************************/
736 static void DupModule( module_t *p_module )
737 {
738     char **pp_shortcut;
739     int i_submodule;
740
741     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
742     {
743         *pp_shortcut = strdup( *pp_shortcut );
744     }
745
746     /* We strdup() these entries so that they are still valid when the
747      * module is unloaded. */
748     p_module->psz_object_name = strdup( p_module->psz_object_name );
749     p_module->psz_capability = strdup( p_module->psz_capability );
750
751     if( p_module->psz_program != NULL )
752     {
753         p_module->psz_program = strdup( p_module->psz_program );
754     }
755
756     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
757     {
758         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
759     }
760 }
761
762 /*****************************************************************************
763  * UndupModule: free a duplicated module.
764  *****************************************************************************
765  * This function frees the allocations done in DupModule().
766  *****************************************************************************/
767 static void UndupModule( module_t *p_module )
768 {
769     char **pp_shortcut;
770     int i_submodule;
771
772     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
773     {
774         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
775     }
776
777     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
778     {
779         free( *pp_shortcut );
780     }
781
782     free( p_module->psz_object_name );
783     free( p_module->psz_capability );
784
785     if( p_module->psz_program != NULL )
786     {
787         free( p_module->psz_program );
788     }
789 }
790
791 #endif /* HAVE_DYNAMIC_PLUGINS */
792
793 /*****************************************************************************
794  * AllocateBuiltinModule: initialize a builtin module.
795  *****************************************************************************
796  * This function registers a builtin module and allocates a structure
797  * for its information data. The module can then be handled by module_Need
798  * and module_Unneed. It can be removed by DeleteModule.
799  *****************************************************************************/
800 static int AllocateBuiltinModule( vlc_object_t * p_this,
801                                   int ( *pf_entry ) ( module_t * ) )
802 {
803     module_t * p_module;
804
805     /* Now that we have successfully loaded the module, we can
806      * allocate a structure for it */ 
807     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
808     if( p_module == NULL )
809     {
810         msg_Err( p_this, "out of memory" );
811         return -1;
812     }
813
814     /* Initialize the module : fill p_module->psz_object_name, etc. */
815     if( pf_entry( p_module ) != 0 )
816     {
817         /* With a well-written module we shouldn't have to print an
818          * additional error message here, but just make sure. */
819         msg_Err( p_this, "failed calling entry point in builtin module" );
820         vlc_object_destroy( p_module );
821         return -1;
822     }
823
824     /* Everything worked fine ! The module is ready to be added to the list. */
825     p_module->b_builtin = VLC_TRUE;
826
827     /* msg_Dbg( p_this, "builtin \"%s\", %s",
828                 p_module->psz_object_name, p_module->psz_longname ); */
829
830     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
831
832     return 0;
833 }
834
835 /*****************************************************************************
836  * DeleteModule: delete a module and its structure.
837  *****************************************************************************
838  * This function can only be called if the module isn't being used.
839  *****************************************************************************/
840 static int DeleteModule( module_t * p_module )
841 {
842     vlc_object_detach( p_module );
843
844     /* We free the structures that we strdup()ed in Allocate*Module(). */
845 #ifdef HAVE_DYNAMIC_PLUGINS
846     if( !p_module->b_builtin )
847     {
848         if( p_module->b_unloadable )
849         {
850             module_unload( p_module->handle );
851         }
852         UndupModule( p_module );
853         free( p_module->psz_filename );
854         free( p_module->psz_longname );
855     }
856 #endif
857
858     /* Free and detach the object's children */
859     while( p_module->i_children )
860     {
861         vlc_object_t *p_this = p_module->pp_children[0];
862         vlc_object_detach( p_this );
863         vlc_object_destroy( p_this );
864     }
865
866     config_Free( p_module );
867     vlc_object_destroy( p_module );
868
869     return 0;
870 }
871
872 #ifdef HAVE_DYNAMIC_PLUGINS
873 /*****************************************************************************
874  * CallEntry: call an entry point.
875  *****************************************************************************
876  * This function calls a symbol given its name and a module structure. The
877  * symbol MUST refer to a function returning int and taking a module_t* as
878  * an argument.
879  *****************************************************************************/
880 static int CallEntry( module_t * p_module )
881 {
882     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
883     int (* pf_symbol) ( module_t * p_module );
884
885     /* Try to resolve the symbol */
886     pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle, 
887                                                         psz_name );
888
889     if( pf_symbol == NULL )
890     {
891         char psz_buffer[256];
892
893         /* We couldn't load the symbol */
894         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
895                             psz_name, p_module->psz_filename,
896                             module_error( psz_buffer ) );
897         return -1;
898     }
899
900     /* We can now try to call the symbol */
901     if( pf_symbol( p_module ) != 0 )
902     {
903         /* With a well-written module we shouldn't have to print an
904          * additional error message here, but just make sure. */
905         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
906                            psz_name, p_module->psz_filename );
907         return -1;
908     }
909
910     /* Everything worked fine, we can return */
911     return 0;
912 }
913 #endif /* HAVE_DYNAMIC_PLUGINS */
914