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