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