]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* ALL: the build mechanism now uses automake. See HACKING for more details.
[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.93 2002/09/30 11:05:42 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     int    i_dirlen;
592     DIR *  dir;
593     char * psz_file;
594
595     struct dirent * file;
596
597     if( i_maxdepth < 0 )
598     {
599         return;
600     }
601
602     dir = opendir( psz_dir );
603
604     if( !dir )
605     {
606         return;
607     }
608
609     i_dirlen = strlen( psz_dir );
610
611     /* Parse the directory and try to load all files it contains. */
612     while( (file = readdir( dir )) )
613     {
614         struct stat statbuf;
615         int i_len;
616
617         /* Skip ".", ".." and anything starting with "." */
618         if( !*file->d_name || *file->d_name == '.' )
619         {
620             continue;
621         }
622
623         i_len = strlen( file->d_name );
624
625         psz_file = malloc( i_dirlen + 1 /* / */ + i_len + 1 /* \0 */ );
626         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
627
628         if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
629         {
630             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
631         }
632         else if( i_len > strlen( LIBEXT )
633                   /* We only load files ending with LIBEXT */
634                   && !strncmp( file->d_name + i_len - strlen( LIBEXT ),
635                                LIBEXT, strlen( LIBEXT ) ) )
636         {
637             AllocatePluginFile( p_this, psz_file );
638         }
639
640         free( psz_file );
641     }
642
643     /* Close the directory */
644     closedir( dir );
645 }
646
647 /*****************************************************************************
648  * AllocatePluginFile: load a module into memory and initialize it.
649  *****************************************************************************
650  * This function loads a dynamically loadable module and allocates a structure
651  * for its information data. The module can then be handled by module_Need
652  * and module_Unneed. It can be removed by DeleteModule.
653  *****************************************************************************/
654 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file )
655 {
656     module_t * p_module;
657     module_handle_t handle;
658
659     /* Try to dynamically load the module. */
660     if( module_load( psz_file, &handle ) )
661     {
662         char psz_buffer[256];
663
664         /* The plugin module couldn't be opened */
665         msg_Warn( p_this, "cannot open `%s' (%s)",
666                   psz_file, module_error( psz_buffer ) );
667         return -1;
668     }
669
670     /* Now that we have successfully loaded the module, we can
671      * allocate a structure for it */ 
672     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
673     if( p_module == NULL )
674     {
675         msg_Err( p_this, "out of memory" );
676         module_unload( handle );
677         return -1;
678     }
679
680     /* We need to fill these since they may be needed by CallEntry() */
681     p_module->psz_filename = psz_file;
682     p_module->handle = handle;
683     p_module->p_symbols = &p_this->p_vlc->p_module_bank->symbols;
684
685     /* Initialize the module: fill p_module->psz_object_name, default config */
686     if( CallEntry( p_module ) != 0 )
687     {
688         /* We couldn't call module_init() */
689         vlc_object_destroy( p_module );
690         module_unload( handle );
691         return -1;
692     }
693
694     DupModule( p_module );
695     p_module->psz_filename = strdup( p_module->psz_filename );
696     p_module->psz_longname = strdup( p_module->psz_longname );
697
698     /* Everything worked fine ! The module is ready to be added to the list. */
699     p_module->b_builtin = VLC_FALSE;
700
701     /* msg_Dbg( p_this, "plugin \"%s\", %s",
702                 p_module->psz_object_name, p_module->psz_longname ); */
703
704     vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
705
706     return 0;
707 }
708
709 /*****************************************************************************
710  * DupModule: make a plugin module standalone.
711  *****************************************************************************
712  * This function duplicates all strings in the module, so that the dynamic
713  * object can be unloaded. It acts recursively on submodules.
714  *****************************************************************************/
715 static void DupModule( module_t *p_module )
716 {
717     char **pp_shortcut;
718     int i_submodule;
719
720     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
721     {
722         *pp_shortcut = strdup( *pp_shortcut );
723     }
724
725     /* We strdup() these entries so that they are still valid when the
726      * module is unloaded. */
727     p_module->psz_object_name = strdup( p_module->psz_object_name );
728     p_module->psz_capability = strdup( p_module->psz_capability );
729
730     if( p_module->psz_program != NULL )
731     {
732         p_module->psz_program = strdup( p_module->psz_program );
733     }
734
735     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
736     {
737         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
738     }
739 }
740
741 /*****************************************************************************
742  * UndupModule: free a duplicated module.
743  *****************************************************************************
744  * This function frees the allocations done in DupModule().
745  *****************************************************************************/
746 static void UndupModule( module_t *p_module )
747 {
748     char **pp_shortcut;
749     int i_submodule;
750
751     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
752     {
753         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
754     }
755
756     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
757     {
758         free( *pp_shortcut );
759     }
760
761     free( p_module->psz_object_name );
762     free( p_module->psz_capability );
763
764     if( p_module->psz_program != NULL )
765     {
766         free( p_module->psz_program );
767     }
768 }
769
770 #endif /* HAVE_DYNAMIC_PLUGINS */
771
772 /*****************************************************************************
773  * AllocateBuiltinModule: initialize a builtin module.
774  *****************************************************************************
775  * This function registers a builtin module and allocates a structure
776  * for its information data. The module can then be handled by module_Need
777  * and module_Unneed. It can be removed by DeleteModule.
778  *****************************************************************************/
779 static int AllocateBuiltinModule( vlc_object_t * p_this,
780                                   int ( *pf_entry ) ( module_t * ) )
781 {
782     module_t * p_module;
783
784     /* Now that we have successfully loaded the module, we can
785      * allocate a structure for it */ 
786     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
787     if( p_module == NULL )
788     {
789         msg_Err( p_this, "out of memory" );
790         return -1;
791     }
792
793     /* Initialize the module : fill p_module->psz_object_name, etc. */
794     if( pf_entry( p_module ) != 0 )
795     {
796         /* With a well-written module we shouldn't have to print an
797          * additional error message here, but just make sure. */
798         msg_Err( p_this, "failed calling entry point in builtin module" );
799         vlc_object_destroy( p_module );
800         return -1;
801     }
802
803     /* Everything worked fine ! The module is ready to be added to the list. */
804     p_module->b_builtin = VLC_TRUE;
805
806     /* msg_Dbg( p_this, "builtin \"%s\", %s",
807                 p_module->psz_object_name, p_module->psz_longname ); */
808
809     vlc_object_attach( p_module, p_this->p_vlc->p_module_bank );
810
811     return 0;
812 }
813
814 /*****************************************************************************
815  * DeleteModule: delete a module and its structure.
816  *****************************************************************************
817  * This function can only be called if the module isn't being used.
818  *****************************************************************************/
819 static int DeleteModule( module_t * p_module )
820 {
821     vlc_object_detach( p_module );
822
823     /* We free the structures that we strdup()ed in Allocate*Module(). */
824 #ifdef HAVE_DYNAMIC_PLUGINS
825     if( !p_module->b_builtin )
826     {
827         if( p_module->b_unloadable )
828         {
829             module_unload( p_module->handle );
830         }
831         UndupModule( p_module );
832         free( p_module->psz_filename );
833         free( p_module->psz_longname );
834     }
835 #endif
836
837     /* Free and detach the object's children */
838     while( p_module->i_children )
839     {
840         vlc_object_t *p_this = p_module->pp_children[0];
841         vlc_object_detach( p_this );
842         vlc_object_destroy( p_this );
843     }
844
845     config_Free( p_module );
846     vlc_object_destroy( p_module );
847
848     return 0;
849 }
850
851 #ifdef HAVE_DYNAMIC_PLUGINS
852 /*****************************************************************************
853  * CallEntry: call an entry point.
854  *****************************************************************************
855  * This function calls a symbol given its name and a module structure. The
856  * symbol MUST refer to a function returning int and taking a module_t* as
857  * an argument.
858  *****************************************************************************/
859 static int CallEntry( module_t * p_module )
860 {
861     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
862     int (* pf_symbol) ( module_t * p_module );
863
864     /* Try to resolve the symbol */
865     pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle, 
866                                                         psz_name );
867
868     if( pf_symbol == NULL )
869     {
870         char psz_buffer[256];
871
872         /* We couldn't load the symbol */
873         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
874                             psz_name, p_module->psz_filename,
875                             module_error( psz_buffer ) );
876         return -1;
877     }
878
879     /* We can now try to call the symbol */
880     if( pf_symbol( p_module ) != 0 )
881     {
882         /* With a well-written module we shouldn't have to print an
883          * additional error message here, but just make sure. */
884         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
885                            psz_name, p_module->psz_filename );
886         return -1;
887     }
888
889     /* Everything worked fine, we can return */
890     return 0;
891 }
892 #endif /* HAVE_DYNAMIC_PLUGINS */
893