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