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