]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* ./src/misc/win32_specific.c: under Win32 we retrieve the executable's
[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.112 2003/02/17 05:50:31 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_which_module, i_index = 0;
256     vlc_bool_t b_intf = VLC_FALSE;
257
258     module_t *p_module;
259
260     int   i_shortcuts = 0;
261     char *psz_shortcuts = NULL, *psz_var = NULL;
262
263     msg_Dbg( p_this, "looking for %s module", psz_capability );
264
265     /* Deal with variables */
266     if( psz_name && psz_name[0] == '$' )
267     {
268         psz_var = config_GetPsz( p_this, psz_name + 1 );
269         psz_name = psz_var;
270     }
271
272     /* Count how many different shortcuts were asked for */
273     if( psz_name && *psz_name )
274     {
275         char *psz_parser;
276
277         /* If the user wants none, give him none. */
278         if( !strcmp( psz_name, "none" ) )
279         {
280             if( psz_var ) free( psz_var );
281             return NULL;
282         }
283
284         i_shortcuts++;
285         psz_shortcuts = strdup( psz_name );
286
287         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
288         {
289             if( *psz_parser == ',' )
290             {
291                  *psz_parser = '\0';
292                  i_shortcuts++;
293             }
294         }
295     }
296
297     /* Sort the modules and test them */
298     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
299     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
300     p_first = NULL;
301
302     /* Parse the module list for capabilities and probe each of them */
303     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
304     {
305         module_t * p_submodule = NULL;
306         int i_shortcut_bonus = 0, i_submodule;
307
308         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
309
310         /* Test that this module can do what we need */
311         if( strcmp( p_module->psz_capability, psz_capability ) )
312         {
313             for( i_submodule = 0;
314                  i_submodule < p_module->i_children;
315                  i_submodule++ )
316             {
317                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
318                                            ->psz_capability, psz_capability ) )
319                 {
320                     p_submodule =
321                             (module_t*)p_module->pp_children[ i_submodule ];
322                     break;
323                 }
324             }
325
326             if( p_submodule == NULL )
327             {
328                 continue;
329             }
330
331             p_module = p_submodule;
332         }
333
334         /* Test if we have the required CPU */
335         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
336         {
337             continue;
338         }
339
340         /* If we required a shortcut, check this plugin provides it. */
341         if( i_shortcuts )
342         {
343             vlc_bool_t b_trash = VLC_TRUE;
344             int i_dummy, i_short = i_shortcuts;
345             char *psz_name = psz_shortcuts;
346
347             while( i_short )
348             {
349                 for( i_dummy = 0;
350                      b_trash && p_module->pp_shortcuts[i_dummy];
351                      i_dummy++ )
352                 {
353                     b_trash = ( strcmp(psz_name, "any") || !p_module->i_score )
354                         && strcmp( psz_name, p_module->pp_shortcuts[i_dummy] );
355                 }
356
357                 if( !b_trash )
358                 {
359                     i_shortcut_bonus = i_short * 10000;
360                     break;
361                 }
362
363                 /* Go to the next shortcut... This is so lame! */
364                 while( *psz_name )
365                 {
366                     psz_name++;
367                 }
368                 psz_name++;
369                 i_short--;
370             }
371
372             if( b_trash )
373             {
374                 continue;
375             }
376         }
377         /* If we didn't require a shortcut, trash zero-scored plugins */
378         else if( !p_module->i_score )
379         {
380             continue;
381         }
382
383         /* Special case: test if we requested a particular intf plugin */
384         if( p_module->psz_program
385              && !strcmp( p_module->psz_program,
386                          p_this->p_vlc->psz_object_name ) )
387         {
388             if( !b_intf )
389             {
390                 /* Remove previous non-matching plugins */
391                 i_index = 0;
392                 b_intf = VLC_TRUE;
393             }
394         }
395         else if( b_intf )
396         {
397             /* This one doesn't match */
398             continue;
399         }
400
401         /* Store this new module */
402         p_list[ i_index ].p_module = p_module;
403         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
404
405         /* Add it to the modules-to-probe list */
406         if( i_index == 0 )
407         {
408             p_list[ 0 ].p_next = NULL;
409             p_first = p_list;
410         }
411         else
412         {
413             /* Ok, so at school you learned that quicksort is quick, and
414              * bubble sort sucks raw eggs. But that's when dealing with
415              * thousands of items. Here we have barely 50. */
416             module_list_t *p_newlist = p_first;
417
418             if( p_first->i_score < p_list[ i_index ].i_score )
419             {
420                 p_list[ i_index ].p_next = p_first;
421                 p_first = &p_list[ i_index ];
422             }
423             else
424             {
425                 while( p_newlist->p_next != NULL &&
426                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
427                 {
428                     p_newlist = p_newlist->p_next;
429                 }
430
431                 p_list[ i_index ].p_next = p_newlist->p_next;
432                 p_newlist->p_next = &p_list[ i_index ];
433             }
434         }
435
436         i_index++;
437     }
438
439     msg_Dbg( p_this, "probing %i candidate%s",
440                      i_index, i_index == 1 ? "" : "s" );
441
442     /* Lock all candidate modules */
443     p_tmp = p_first;
444     while( p_tmp != NULL )
445     {
446         vlc_object_yield( p_tmp->p_module );
447         p_tmp = p_tmp->p_next;
448     }
449
450     /* We can release the list, interesting modules were yielded */
451     vlc_list_release( p_all );
452
453     /* Parse the linked list and use the first successful module */
454     p_tmp = p_first;
455     while( p_tmp != NULL )
456     {
457         if( p_tmp->p_module->pf_activate
458              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
459         {
460             break;
461         }
462
463         vlc_object_release( p_tmp->p_module );
464         p_tmp = p_tmp->p_next;
465     }
466
467     /* Store the locked module value */
468     if( p_tmp != NULL )
469     {
470         p_module = p_tmp->p_module;
471         p_tmp = p_tmp->p_next;
472     }
473     else
474     {
475         p_module = NULL;
476     }
477
478     /* Unlock the remaining modules */
479     while( p_tmp != NULL )
480     {
481         vlc_object_release( p_tmp->p_module );
482         p_tmp = p_tmp->p_next;
483     }
484
485     free( p_list );
486
487     if( p_module != NULL )
488     {
489         msg_Dbg( p_module, "using %s module \"%s\"",
490                  psz_capability, p_module->psz_object_name );
491     }
492     else if( p_first == NULL )
493     {
494         msg_Err( p_this, "no %s module matched \"%s\"",
495                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
496     }
497     else if( psz_name != NULL && *psz_name )
498     {
499         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
500                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
501     }
502
503     if( psz_shortcuts )
504     {
505         free( psz_shortcuts );
506     }
507
508     if( psz_var )
509     {
510         free( psz_var );
511     }
512
513     /* Don't forget that the module is still locked */
514     return p_module;
515 }
516
517 /*****************************************************************************
518  * module_Unneed: decrease the usage count of a module.
519  *****************************************************************************
520  * This function must be called by the thread that called module_Need, to
521  * decrease the reference count and allow for hiding of modules.
522  *****************************************************************************/
523 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
524 {
525     /* Use the close method */
526     if( p_module->pf_deactivate )
527     {
528         p_module->pf_deactivate( p_this );
529     }
530
531     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
532
533     vlc_object_release( p_module );
534
535     return;
536 }
537
538 /*****************************************************************************
539  * Following functions are local.
540  *****************************************************************************/
541
542 /*****************************************************************************
543  * AllocateAllPlugins: load all plugin modules we can find.
544  *****************************************************************************/
545 #ifdef HAVE_DYNAMIC_PLUGINS
546 static void AllocateAllPlugins( vlc_object_t *p_this )
547 {
548     /* Yes, there are two NULLs because we replace one with "plugin-path". */
549     char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
550                                NULL };
551
552     char **         ppsz_path = path;
553     char *          psz_fullpath;
554 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
555      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
556     int             i_vlclen = strlen( p_this->p_libvlc->psz_vlcpath );
557     vlc_bool_t      b_notinroot;
558 #endif
559
560 #if defined( UNDER_CE )
561     wchar_t         psz_dir[MAX_PATH];
562 #endif
563
564     /* If the user provided a plugin path, we add it to the list */
565     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
566                                                             "plugin-path" );
567
568     for( ; *ppsz_path != NULL ; ppsz_path++ )
569     {
570 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
571      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
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 #ifdef WIN32
579         if( i_dirlen < 3 || ppsz_path[3] != '\\' )
580 #else
581         if( ppsz_path[0] != '/' )
582 #endif
583         {
584             i_dirlen += i_vlclen + 2;
585             b_notinroot = VLC_TRUE;
586
587             psz_fullpath = malloc( i_dirlen );
588             if( psz_fullpath == NULL )
589             {
590                 continue;
591             }
592             sprintf( psz_fullpath, "%s/%s",
593                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
594         }
595         else
596 #endif
597         {
598             psz_fullpath = *ppsz_path;
599         }
600
601         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
602
603         /* Don't go deeper than 5 subdirectories */
604 #if defined( UNDER_CE )
605         MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
606         AllocatePluginDir( p_this, psz_dir, 5 );
607 #else
608         AllocatePluginDir( p_this, psz_fullpath, 5 );
609 #endif
610
611 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
612         if( b_notinroot )
613         {
614             free( psz_fullpath );
615         }
616 #endif
617     }
618
619     /* Free plugin-path */
620     free( path[ sizeof(path)/sizeof(char*) - 2 ] );
621     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
622 }
623
624 /*****************************************************************************
625  * AllocatePluginDir: recursively parse a directory to look for plugins
626  *****************************************************************************/
627 static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
628                                int i_maxdepth )
629 {
630 #ifdef UNDER_CE
631     MYCHAR psz_path[MAX_PATH + 256];
632     WIN32_FIND_DATA finddata;
633     HANDLE handle;
634     unsigned int rc;
635 #else
636     int    i_dirlen;
637     DIR *  dir;
638     char * psz_file;
639     struct dirent * file;
640 #endif
641
642     if( i_maxdepth < 0 )
643     {
644         return;
645     }
646
647 #ifdef UNDER_CE
648     rc = GetFileAttributes( psz_dir );
649     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
650     {
651         /* Not a directory */
652         return;
653     }
654
655     /* Parse all files in the directory */
656     swprintf( psz_path, L"%s\\*.*", psz_dir );
657     handle = FindFirstFile( psz_path, &finddata );
658     if( handle == INVALID_HANDLE_VALUE )
659     {
660         /* Empty directory */
661         return;
662     }
663
664     /* Parse the directory and try to load all files it contains. */
665     do
666     {
667         unsigned int i_len = wcslen( finddata.cFileName );
668
669         swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
670
671         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
672         {
673             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
674         }
675         else if( i_len > strlen( LIBEXT ) )
676         {
677             AllocatePluginFile( p_this, psz_path );
678         }
679     }
680     while( FindNextFile( handle, &finddata ) );
681
682     /* Close the directory */
683     FindClose( handle );
684
685 #else
686     dir = opendir( psz_dir );
687     if( !dir )
688     {
689         return;
690     }
691
692     i_dirlen = strlen( psz_dir );
693
694     /* Parse the directory and try to load all files it contains. */
695     while( (file = readdir( dir )) )
696     {
697         struct stat statbuf;
698         unsigned int i_len;
699
700         /* Skip ".", ".." and anything starting with "." */
701         if( !*file->d_name || *file->d_name == '.' )
702         {
703             continue;
704         }
705
706         i_len = strlen( file->d_name );
707         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
708         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
709
710         if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
711         {
712             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
713         }
714         else if( i_len > strlen( LIBEXT )
715                   /* We only load files ending with LIBEXT */
716                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
717                                    LIBEXT, strlen( LIBEXT ) ) )
718         {
719             AllocatePluginFile( p_this, psz_file );
720         }
721
722         free( psz_file );
723     }
724
725     /* Close the directory */
726     closedir( dir );
727
728 #endif
729 }
730
731 /*****************************************************************************
732  * AllocatePluginFile: load a module into memory and initialize it.
733  *****************************************************************************
734  * This function loads a dynamically loadable module and allocates a structure
735  * for its information data. The module can then be handled by module_Need
736  * and module_Unneed. It can be removed by DeleteModule.
737  *****************************************************************************/
738 static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
739 {
740     module_t * p_module;
741     module_handle_t handle;
742
743 #ifdef UNDER_CE
744     char psz_filename[MAX_PATH];
745     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
746                          psz_filename, MAX_PATH, NULL, NULL );
747 #else
748     char * psz_filename = psz_file;
749 #endif
750
751     /* Try to dynamically load the module. */
752     if( module_load( psz_file, &handle ) )
753     {
754         char psz_buffer[256];
755
756         /* The plugin module couldn't be opened */
757         msg_Warn( p_this, "cannot open `%s' (%s)",
758                   psz_filename, module_error( psz_buffer ) );
759         return -1;
760     }
761
762     /* Now that we have successfully loaded the module, we can
763      * allocate a structure for it */
764     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
765     if( p_module == NULL )
766     {
767         msg_Err( p_this, "out of memory" );
768         module_unload( handle );
769         return -1;
770     }
771
772     /* We need to fill these since they may be needed by CallEntry() */
773     p_module->psz_filename = psz_filename;
774     p_module->handle = handle;
775     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
776
777     /* Initialize the module: fill p_module->psz_object_name, default config */
778     if( CallEntry( p_module ) != 0 )
779     {
780         /* We couldn't call module_init() */
781         vlc_object_destroy( p_module );
782         module_unload( handle );
783         return -1;
784     }
785
786     DupModule( p_module );
787     p_module->psz_filename = strdup( p_module->psz_filename );
788     p_module->psz_longname = strdup( p_module->psz_longname );
789
790     /* Everything worked fine ! The module is ready to be added to the list. */
791     p_module->b_builtin = VLC_FALSE;
792
793     /* msg_Dbg( p_this, "plugin \"%s\", %s",
794                 p_module->psz_object_name, p_module->psz_longname ); */
795
796     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
797
798     return 0;
799 }
800
801 /*****************************************************************************
802  * DupModule: make a plugin module standalone.
803  *****************************************************************************
804  * This function duplicates all strings in the module, so that the dynamic
805  * object can be unloaded. It acts recursively on submodules.
806  *****************************************************************************/
807 static void DupModule( module_t *p_module )
808 {
809     char **pp_shortcut;
810     int i_submodule;
811
812     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
813     {
814         *pp_shortcut = strdup( *pp_shortcut );
815     }
816
817     /* We strdup() these entries so that they are still valid when the
818      * module is unloaded. */
819     p_module->psz_object_name = strdup( p_module->psz_object_name );
820     p_module->psz_capability = strdup( p_module->psz_capability );
821
822     if( p_module->psz_program != NULL )
823     {
824         p_module->psz_program = strdup( p_module->psz_program );
825     }
826
827     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
828     {
829         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
830     }
831 }
832
833 /*****************************************************************************
834  * UndupModule: free a duplicated module.
835  *****************************************************************************
836  * This function frees the allocations done in DupModule().
837  *****************************************************************************/
838 static void UndupModule( module_t *p_module )
839 {
840     char **pp_shortcut;
841     int i_submodule;
842
843     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
844     {
845         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
846     }
847
848     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
849     {
850         free( *pp_shortcut );
851     }
852
853     free( p_module->psz_object_name );
854     free( p_module->psz_capability );
855
856     if( p_module->psz_program != NULL )
857     {
858         free( p_module->psz_program );
859     }
860 }
861
862 #endif /* HAVE_DYNAMIC_PLUGINS */
863
864 /*****************************************************************************
865  * AllocateBuiltinModule: initialize a builtin module.
866  *****************************************************************************
867  * This function registers a builtin module and allocates a structure
868  * for its information data. The module can then be handled by module_Need
869  * and module_Unneed. It can be removed by DeleteModule.
870  *****************************************************************************/
871 static int AllocateBuiltinModule( vlc_object_t * p_this,
872                                   int ( *pf_entry ) ( module_t * ) )
873 {
874     module_t * p_module;
875
876     /* Now that we have successfully loaded the module, we can
877      * allocate a structure for it */
878     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
879     if( p_module == NULL )
880     {
881         msg_Err( p_this, "out of memory" );
882         return -1;
883     }
884
885     /* Initialize the module : fill p_module->psz_object_name, etc. */
886     if( pf_entry( p_module ) != 0 )
887     {
888         /* With a well-written module we shouldn't have to print an
889          * additional error message here, but just make sure. */
890         msg_Err( p_this, "failed calling entry point in builtin module" );
891         vlc_object_destroy( p_module );
892         return -1;
893     }
894
895     /* Everything worked fine ! The module is ready to be added to the list. */
896     p_module->b_builtin = VLC_TRUE;
897
898     /* msg_Dbg( p_this, "builtin \"%s\", %s",
899                 p_module->psz_object_name, p_module->psz_longname ); */
900
901     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
902
903     return 0;
904 }
905
906 /*****************************************************************************
907  * DeleteModule: delete a module and its structure.
908  *****************************************************************************
909  * This function can only be called if the module isn't being used.
910  *****************************************************************************/
911 static int DeleteModule( module_t * p_module )
912 {
913     vlc_object_detach( p_module );
914
915     /* We free the structures that we strdup()ed in Allocate*Module(). */
916 #ifdef HAVE_DYNAMIC_PLUGINS
917     if( !p_module->b_builtin )
918     {
919         if( p_module->b_unloadable )
920         {
921             module_unload( p_module->handle );
922         }
923         UndupModule( p_module );
924         free( p_module->psz_filename );
925         free( p_module->psz_longname );
926     }
927 #endif
928
929     /* Free and detach the object's children */
930     while( p_module->i_children )
931     {
932         vlc_object_t *p_this = p_module->pp_children[0];
933         vlc_object_detach( p_this );
934         vlc_object_destroy( p_this );
935     }
936
937     config_Free( p_module );
938     vlc_object_destroy( p_module );
939
940     return 0;
941 }
942
943 #ifdef HAVE_DYNAMIC_PLUGINS
944 /*****************************************************************************
945  * CallEntry: call an entry point.
946  *****************************************************************************
947  * This function calls a symbol given its name and a module structure. The
948  * symbol MUST refer to a function returning int and taking a module_t* as
949  * an argument.
950  *****************************************************************************/
951 static int CallEntry( module_t * p_module )
952 {
953     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
954     int (* pf_symbol) ( module_t * p_module );
955
956     /* Try to resolve the symbol */
957     pf_symbol = (int (*)(module_t *)) module_getsymbol( p_module->handle,
958                                                         psz_name );
959
960     if( pf_symbol == NULL )
961     {
962         char psz_buffer[256];
963
964         /* We couldn't load the symbol */
965         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
966                             psz_name, p_module->psz_filename,
967                             module_error( psz_buffer ) );
968         return -1;
969     }
970
971     /* We can now try to call the symbol */
972     if( pf_symbol( p_module ) != 0 )
973     {
974         /* With a well-written module we shouldn't have to print an
975          * additional error message here, but just make sure. */
976         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
977                            psz_name, p_module->psz_filename );
978         return -1;
979     }
980
981     /* Everything worked fine, we can return */
982     return 0;
983 }
984 #endif /* HAVE_DYNAMIC_PLUGINS */