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