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