]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* configure.ac:
[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.136 2003/10/05 21:29:23 sam Exp $
6  *
7  * Authors: Sam 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 #define HAVE_DYNAMIC_PLUGINS
57 #if defined(HAVE_DL_DYLD)
58 #   if defined(HAVE_MACH_O_DYLD_H)
59 #       include <mach-o/dyld.h>
60 #   endif
61 #elif defined(HAVE_DL_BEOS)
62 #   if defined(HAVE_IMAGE_H)
63 #       include <image.h>
64 #   endif
65 #elif defined(HAVE_DL_WINDOWS)
66 #   include <windows.h>
67 #elif defined(HAVE_DL_DLOPEN)
68 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
69 #       include <dlfcn.h>
70 #   endif
71 #   if defined(HAVE_SYS_DL_H)
72 #       include <sys/dl.h>
73 #   endif
74 #elif defined(HAVE_DL_SHL_LOAD)
75 #   if defined(HAVE_DL_H)
76 #       include <dl.h>
77 #   endif
78 #else
79 #   undef HAVE_DYNAMIC_PLUGINS
80 #endif
81
82 #include "vlc_error.h"
83
84 #include "vlc_interface.h"
85 #include "vlc_playlist.h"
86 #include "intf_eject.h"
87
88 #include "stream_control.h"
89 #include "input_ext-intf.h"
90 #include "input_ext-dec.h"
91 #include "input_ext-plugins.h"
92 #include "ninput.h"
93
94 #include "vlc_video.h"
95 #include "video_output.h"
96 #include "vout_synchro.h"
97
98 #include "audio_output.h"
99 #include "aout_internal.h"
100
101 #include "stream_output.h"
102 /*#include "announce.h"*/
103 #include "osd.h"
104
105 #include "iso_lang.h"
106 #include "charset.h"
107
108 #include "vlc_block.h"
109
110 #if defined( UNDER_CE )
111 #   define MYCHAR wchar_t
112 #else
113 #   define MYCHAR char
114 #endif
115
116 #ifdef HAVE_DYNAMIC_PLUGINS
117 #   include "modules_plugin.h"
118 #endif
119
120 #if defined( UNDER_CE )
121 #    include "modules_builtin_evc.h"
122 #elif defined( _MSC_VER )
123 #    include "modules_builtin_msvc.h"
124 #else
125 #    include "modules_builtin.h"
126 #endif
127
128 /*****************************************************************************
129  * Local prototypes
130  *****************************************************************************/
131 #ifdef HAVE_DYNAMIC_PLUGINS
132 static void AllocateAllPlugins   ( vlc_object_t * );
133 static void AllocatePluginDir    ( vlc_object_t *, const MYCHAR *, int );
134 static int  AllocatePluginFile   ( vlc_object_t *, MYCHAR * );
135 #endif
136 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
137 static int  DeleteModule ( module_t * );
138 #ifdef HAVE_DYNAMIC_PLUGINS
139 static void   DupModule        ( module_t * );
140 static void   UndupModule      ( module_t * );
141 static int    CallEntry        ( module_t * );
142 static void   CloseModule      ( module_handle_t );
143 static void * GetSymbol        ( module_handle_t, const char * );
144 #if defined(HAVE_DL_WINDOWS)
145 static char * GetWindowsError  ( void );
146 #endif
147 #endif
148
149 /*****************************************************************************
150  * module_InitBank: create the module bank.
151  *****************************************************************************
152  * This function creates a module bank structure which will be filled later
153  * on with all the modules found.
154  *****************************************************************************/
155 void __module_InitBank( vlc_object_t *p_this )
156 {
157     module_bank_t *p_bank;
158
159     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
160     p_bank->psz_object_name = "module bank";
161
162     /*
163      * Store the symbols to be exported
164      */
165 #ifdef HAVE_DYNAMIC_PLUGINS
166     STORE_SYMBOLS( &p_bank->symbols );
167 #endif
168
169     /* Everything worked, attach the object */
170     p_this->p_libvlc->p_module_bank = p_bank;
171     vlc_object_attach( p_bank, p_this->p_libvlc );
172
173     return;
174 }
175
176 /*****************************************************************************
177  * module_ResetBank: reset the module bank.
178  *****************************************************************************
179  * This function resets the module bank by unloading all unused plugin
180  * modules.
181  *****************************************************************************/
182 void __module_ResetBank( vlc_object_t *p_this )
183 {
184     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
185     return;
186 }
187
188 /*****************************************************************************
189  * module_EndBank: empty the module bank.
190  *****************************************************************************
191  * This function unloads all unused plugin modules and empties the module
192  * bank in case of success.
193  *****************************************************************************/
194 void __module_EndBank( vlc_object_t *p_this )
195 {
196     module_t * p_next;
197
198     vlc_object_detach( p_this->p_libvlc->p_module_bank );
199
200     while( p_this->p_libvlc->p_module_bank->i_children )
201     {
202         p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
203
204         if( DeleteModule( p_next ) )
205         {
206             /* Module deletion failed */
207             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
208                      p_next->psz_object_name );
209
210             /* We just free the module by hand. Niahahahahaha. */
211             vlc_object_detach( p_next );
212             vlc_object_destroy( p_next );
213         }
214     }
215
216     vlc_object_destroy( p_this->p_libvlc->p_module_bank );
217
218     return;
219 }
220
221 /*****************************************************************************
222  * module_LoadMain: load the main program info into the module bank.
223  *****************************************************************************
224  * This function fills the module bank structure with the main module infos.
225  * This is very useful as it will allow us to consider the main program just
226  * as another module, and for instance the configuration options of main will
227  * be available in the module bank structure just as for every other module.
228  *****************************************************************************/
229 void __module_LoadMain( vlc_object_t *p_this )
230 {
231     AllocateBuiltinModule( p_this, vlc_entry__main );
232 }
233
234 /*****************************************************************************
235  * module_LoadBuiltins: load all modules which we built with.
236  *****************************************************************************
237  * This function fills the module bank structure with the builtin modules.
238  *****************************************************************************/
239 void __module_LoadBuiltins( vlc_object_t * p_this )
240 {
241     msg_Dbg( p_this, "checking builtin modules" );
242     ALLOCATE_ALL_BUILTINS();
243 }
244
245 /*****************************************************************************
246  * module_LoadPlugins: load all plugin modules we can find.
247  *****************************************************************************
248  * This function fills the module bank structure with the plugin modules.
249  *****************************************************************************/
250 void __module_LoadPlugins( vlc_object_t * p_this )
251 {
252 #ifdef HAVE_DYNAMIC_PLUGINS
253     msg_Dbg( p_this, "checking plugin modules" );
254     AllocateAllPlugins( p_this );
255 #endif
256 }
257
258 /*****************************************************************************
259  * module_Need: return the best module function, given a capability list.
260  *****************************************************************************
261  * This function returns the module that best fits the asked capabilities.
262  *****************************************************************************/
263 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
264                           const char *psz_name )
265 {
266     typedef struct module_list_t module_list_t;
267
268     struct module_list_t
269     {
270         module_t *p_module;
271         int i_score;
272         module_list_t *p_next;
273     };
274
275     module_list_t *p_list, *p_first, *p_tmp;
276     vlc_list_t *p_all;
277
278     int i_which_module, i_index = 0;
279     vlc_bool_t b_intf = VLC_FALSE;
280
281     module_t *p_module;
282
283     int   i_shortcuts = 0;
284     char *psz_shortcuts = NULL, *psz_var = NULL;
285
286     msg_Dbg( p_this, "looking for %s module", psz_capability );
287
288     /* Deal with variables */
289     if( psz_name && psz_name[0] == '$' )
290     {
291         psz_var = config_GetPsz( p_this, psz_name + 1 );
292         psz_name = psz_var;
293     }
294
295     /* Count how many different shortcuts were asked for */
296     if( psz_name && *psz_name )
297     {
298         char *psz_parser;
299
300         /* If the user wants none, give him none. */
301         if( !strcmp( psz_name, "none" ) )
302         {
303             if( psz_var ) free( psz_var );
304             return NULL;
305         }
306
307         i_shortcuts++;
308         psz_shortcuts = strdup( psz_name );
309
310         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
311         {
312             if( *psz_parser == ',' )
313             {
314                  *psz_parser = '\0';
315                  i_shortcuts++;
316             }
317         }
318     }
319
320     /* Sort the modules and test them */
321     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
322     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
323     p_first = NULL;
324
325     /* Parse the module list for capabilities and probe each of them */
326     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
327     {
328         module_t * p_submodule = NULL;
329         int i_shortcut_bonus = 0, i_submodule;
330
331         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
332
333         /* Test that this module can do what we need */
334         if( strcmp( p_module->psz_capability, psz_capability ) )
335         {
336             for( i_submodule = 0;
337                  i_submodule < p_module->i_children;
338                  i_submodule++ )
339             {
340                 if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
341                                            ->psz_capability, psz_capability ) )
342                 {
343                     p_submodule =
344                             (module_t*)p_module->pp_children[ i_submodule ];
345                     break;
346                 }
347             }
348
349             if( p_submodule == NULL )
350             {
351                 continue;
352             }
353
354             p_module = p_submodule;
355         }
356
357         /* Test if we have the required CPU */
358         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
359         {
360             continue;
361         }
362
363         /* If we required a shortcut, check this plugin provides it. */
364         if( i_shortcuts )
365         {
366             vlc_bool_t b_trash;
367             int i_dummy, i_short = i_shortcuts;
368             char *psz_name = psz_shortcuts;
369
370             /* Let's drop modules with a 0 score (unless they are
371              * explicitly requested) */
372             b_trash = !p_module->i_score;
373
374             while( i_short )
375             {
376                 /* If the last given shortcut is "none" and we couldn't
377                  * find the module in the list of provided shortcuts,
378                  * then kick the bastard out of here!!! */
379                 if( (i_short == 1) && !strcmp(psz_name, "none") )
380                 {
381                     b_trash = VLC_TRUE;
382                     break;
383                 }
384
385                 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
386                 {
387                     if( !strcasecmp( psz_name,
388                                      p_module->pp_shortcuts[i_dummy] ) )
389                     {
390                         /* Found it */
391                         b_trash = VLC_FALSE;
392                         i_shortcut_bonus = i_short * 10000;
393                         break;
394                     }
395                 }
396
397                 if( i_shortcut_bonus )
398                 {
399                     /* We found it... remember ? */
400                     break;
401                 }
402
403                 /* Go to the next shortcut... This is so lame! */
404                 while( *psz_name )
405                 {
406                     psz_name++;
407                 }
408                 psz_name++;
409                 i_short--;
410             }
411
412             if( b_trash )
413             {
414                 continue;
415             }
416         }
417         /* If we didn't require a shortcut, trash zero-scored plugins */
418         else if( !p_module->i_score )
419         {
420             continue;
421         }
422
423         /* Special case: test if we requested a particular intf plugin */
424         if( !i_shortcuts && p_module->psz_program
425              && !strcmp( p_module->psz_program,
426                          p_this->p_vlc->psz_object_name ) )
427         {
428             if( !b_intf )
429             {
430                 /* Remove previous non-matching plugins */
431                 i_index = 0;
432                 b_intf = VLC_TRUE;
433             }
434         }
435         else if( b_intf )
436         {
437             /* This one doesn't match */
438             continue;
439         }
440
441         /* Store this new module */
442         p_list[ i_index ].p_module = p_module;
443         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
444
445         /* Add it to the modules-to-probe list */
446         if( i_index == 0 )
447         {
448             p_list[ 0 ].p_next = NULL;
449             p_first = p_list;
450         }
451         else
452         {
453             /* Ok, so at school you learned that quicksort is quick, and
454              * bubble sort sucks raw eggs. But that's when dealing with
455              * thousands of items. Here we have barely 50. */
456             module_list_t *p_newlist = p_first;
457
458             if( p_first->i_score < p_list[ i_index ].i_score )
459             {
460                 p_list[ i_index ].p_next = p_first;
461                 p_first = &p_list[ i_index ];
462             }
463             else
464             {
465                 while( p_newlist->p_next != NULL &&
466                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
467                 {
468                     p_newlist = p_newlist->p_next;
469                 }
470
471                 p_list[ i_index ].p_next = p_newlist->p_next;
472                 p_newlist->p_next = &p_list[ i_index ];
473             }
474         }
475
476         i_index++;
477     }
478
479     msg_Dbg( p_this, "probing %i candidate%s",
480                      i_index, i_index == 1 ? "" : "s" );
481
482     /* Lock all candidate modules */
483     p_tmp = p_first;
484     while( p_tmp != NULL )
485     {
486         vlc_object_yield( p_tmp->p_module );
487         p_tmp = p_tmp->p_next;
488     }
489
490     /* We can release the list, interesting modules were yielded */
491     vlc_list_release( p_all );
492
493     /* Parse the linked list and use the first successful module */
494     p_tmp = p_first;
495     while( p_tmp != NULL )
496     {
497         if( p_tmp->p_module->pf_activate
498              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
499         {
500             break;
501         }
502
503         vlc_object_release( p_tmp->p_module );
504         p_tmp = p_tmp->p_next;
505     }
506
507     /* Store the locked module value */
508     if( p_tmp != NULL )
509     {
510         p_module = p_tmp->p_module;
511         p_tmp = p_tmp->p_next;
512     }
513     else
514     {
515         p_module = NULL;
516     }
517
518     /* Unlock the remaining modules */
519     while( p_tmp != NULL )
520     {
521         vlc_object_release( p_tmp->p_module );
522         p_tmp = p_tmp->p_next;
523     }
524
525     free( p_list );
526
527     if( p_module != NULL )
528     {
529         msg_Dbg( p_module, "using %s module \"%s\"",
530                  psz_capability, p_module->psz_object_name );
531     }
532     else if( p_first == NULL )
533     {
534         msg_Err( p_this, "no %s module matched \"%s\"",
535                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
536     }
537     else if( psz_name != NULL && *psz_name )
538     {
539         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
540                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
541     }
542
543     if( psz_shortcuts )
544     {
545         free( psz_shortcuts );
546     }
547
548     if( psz_var )
549     {
550         free( psz_var );
551     }
552
553     /* Don't forget that the module is still locked */
554     return p_module;
555 }
556
557 /*****************************************************************************
558  * module_Unneed: decrease the usage count of a module.
559  *****************************************************************************
560  * This function must be called by the thread that called module_Need, to
561  * decrease the reference count and allow for hiding of modules.
562  *****************************************************************************/
563 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
564 {
565     /* Use the close method */
566     if( p_module->pf_deactivate )
567     {
568         p_module->pf_deactivate( p_this );
569     }
570
571     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
572
573     vlc_object_release( p_module );
574
575     return;
576 }
577
578 /*****************************************************************************
579  * Following functions are local.
580  *****************************************************************************/
581
582 /*****************************************************************************
583  * AllocateAllPlugins: load all plugin modules we can find.
584  *****************************************************************************/
585 #ifdef HAVE_DYNAMIC_PLUGINS
586 static void AllocateAllPlugins( vlc_object_t *p_this )
587 {
588     /* Yes, there are two NULLs because we replace one with "plugin-path". */
589     char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
590                                NULL };
591
592     char **         ppsz_path = path;
593     char *          psz_fullpath;
594
595 #if defined( UNDER_CE )
596     wchar_t         psz_dir[MAX_PATH];
597 #endif
598
599     /* If the user provided a plugin path, we add it to the list */
600     path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
601                                                             "plugin-path" );
602
603     for( ; *ppsz_path != NULL ; ppsz_path++ )
604     {
605 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
606      || ( defined( WIN32 ) && !defined( UNDER_CE ) )
607
608         /* Handle relative as well as absolute paths */
609 #ifdef WIN32
610         if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
611 #else
612         if( (*ppsz_path)[0] != '/' )
613 #endif
614         {
615             int i_dirlen = strlen( *ppsz_path );
616             i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
617
618             psz_fullpath = malloc( i_dirlen );
619             if( psz_fullpath == NULL )
620             {
621                 continue;
622             }
623 #ifdef WIN32
624             sprintf( psz_fullpath, "%s\\%s",
625                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
626 #else
627             sprintf( psz_fullpath, "%s/%s",
628                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
629 #endif
630         }
631         else
632 #endif
633         {
634             psz_fullpath = strdup( *ppsz_path );
635         }
636
637         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
638
639         /* Don't go deeper than 5 subdirectories */
640 #if defined( UNDER_CE )
641         MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
642         AllocatePluginDir( p_this, psz_dir, 5 );
643 #else
644         AllocatePluginDir( p_this, psz_fullpath, 5 );
645 #endif
646
647         free( psz_fullpath );
648     }
649
650     /* Free plugin-path */
651     if( path[ sizeof(path)/sizeof(char*) - 2 ] )
652         free( path[ sizeof(path)/sizeof(char*) - 2 ] );
653     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
654 }
655
656 /*****************************************************************************
657  * AllocatePluginDir: recursively parse a directory to look for plugins
658  *****************************************************************************/
659 static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
660                                int i_maxdepth )
661 {
662 #if defined( UNDER_CE ) || defined( _MSC_VER )
663 #ifdef UNDER_CE
664     MYCHAR psz_path[MAX_PATH + 256];
665 #else
666     char psz_path[MAX_PATH + 256];
667 #endif
668     WIN32_FIND_DATA finddata;
669     HANDLE handle;
670     unsigned int rc;
671 #else
672     int    i_dirlen;
673     DIR *  dir;
674     char * psz_file;
675     struct dirent * file;
676 #endif
677
678     if( i_maxdepth < 0 )
679     {
680         return;
681     }
682
683 #if defined( UNDER_CE ) || defined( _MSC_VER )
684     rc = GetFileAttributes( psz_dir );
685     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
686     {
687         /* Not a directory */
688         return;
689     }
690
691     /* Parse all files in the directory */
692 #ifdef UNDER_CE
693     swprintf( psz_path, L"%s\\*.*", psz_dir );
694 #else
695     sprintf( psz_path, "%s\\*.*", psz_dir );
696 #endif
697     handle = FindFirstFile( psz_path, &finddata );
698     if( handle == INVALID_HANDLE_VALUE )
699     {
700         /* Empty directory */
701         return;
702     }
703
704     /* Parse the directory and try to load all files it contains. */
705     do
706     {
707 #ifdef UNDER_CE
708         unsigned int i_len = wcslen( finddata.cFileName );
709         swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
710 #else
711         unsigned int i_len = strlen( finddata.cFileName );
712         /* Skip ".", ".." and anything starting with "." */
713         if( !*finddata.cFileName || *finddata.cFileName == '.' )
714         {
715             if( !FindNextFile( handle, &finddata ) ) break;
716             continue;
717         }
718         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
719 #endif
720
721         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
722         {
723             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
724         }
725         else if( i_len > strlen( LIBEXT )
726 #ifdef UNDER_CE
727                 )
728 #else
729                   /* We only load files ending with LIBEXT */
730                   && !strncasecmp( psz_path + strlen( psz_path)
731                                    - strlen( LIBEXT ),
732                                    LIBEXT, strlen( LIBEXT ) ) )
733 #endif
734         {
735             AllocatePluginFile( p_this, psz_path );
736         }
737     }
738     while( FindNextFile( handle, &finddata ) );
739
740     /* Close the directory */
741     FindClose( handle );
742
743 #else
744     dir = opendir( psz_dir );
745     if( !dir )
746     {
747         return;
748     }
749
750     i_dirlen = strlen( psz_dir );
751
752     /* Parse the directory and try to load all files it contains. */
753     while( (file = readdir( dir )) )
754     {
755         struct stat statbuf;
756         unsigned int i_len;
757
758         /* Skip ".", ".." and anything starting with "." */
759         if( !*file->d_name || *file->d_name == '.' )
760         {
761             continue;
762         }
763
764         i_len = strlen( file->d_name );
765         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
766 #ifdef WIN32
767         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
768 #else
769         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
770 #endif
771
772         if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
773         {
774             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
775         }
776         else if( i_len > strlen( LIBEXT )
777                   /* We only load files ending with LIBEXT */
778                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
779                                    LIBEXT, strlen( LIBEXT ) ) )
780         {
781             AllocatePluginFile( p_this, psz_file );
782         }
783
784         free( psz_file );
785     }
786
787     /* Close the directory */
788     closedir( dir );
789
790 #endif
791 }
792
793 /*****************************************************************************
794  * AllocatePluginFile: load a module into memory and initialize it.
795  *****************************************************************************
796  * This function loads a dynamically loadable module and allocates a structure
797  * for its information data. The module can then be handled by module_Need
798  * and module_Unneed. It can be removed by DeleteModule.
799  *****************************************************************************/
800 static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
801 {
802     module_t * p_module;
803     module_handle_t handle;
804
805     /*
806      * Try to dynamically load the module. This part is very platform-
807      * specific, and error messages should be as verbose as possible.
808      */
809
810 #if defined(HAVE_DL_DYLD)
811     NSObjectFileImage image;
812     NSObjectFileImageReturnCode ret;
813
814     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
815
816     if( ret != NSObjectFileImageSuccess )
817     {
818         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
819         return -1;
820     }
821
822     /* Open the dynamic module */
823     handle = NSLinkModule( image, psz_file,
824                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
825
826     if( !handle )
827     {
828         NSLinkEditErrors errors;
829         const char *psz_file, *psz_err;
830         int i_errnum;
831         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
832         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
833         NSDestroyObjectFileImage( image );
834         return -1;
835     }
836
837     /* Destroy our image, we won't need it */
838     NSDestroyObjectFileImage( image );
839
840 #elif defined(HAVE_DL_BEOS)
841     handle = load_add_on( psz_file );
842     if( handle < 0 )
843     {
844         msg_Warn( p_this, "cannot load module `%s'", psz_file );
845         return -1;
846     }
847
848 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
849     char psz_filename[MAX_PATH];
850     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
851                          psz_filename, MAX_PATH, NULL, NULL );
852     handle = LoadLibrary( psz_filename );
853     if( handle == NULL )
854     {
855         char *psz_error = GetWindowsError();
856         msg_Warn( p_this, "cannot load module `%s' (%s)",
857                           psz_filename, psz_error );
858         free( psz_error );
859     }
860
861 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
862     handle = LoadLibrary( psz_file );
863     if( handle == NULL )
864     {
865         char *psz_error = GetWindowsError();
866         msg_Warn( p_this, "cannot load module `%s' (%s)",
867                           psz_filename, psz_error );
868         free( psz_error );
869     }
870
871 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
872     /* static is OK, we are called atomically */
873     static vlc_bool_t b_kde = VLC_FALSE;
874
875 #   if defined(SYS_LINUX)
876     /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
877      * are going to get namespace collisions when two modules have common
878      * public symbols, but ALSA is being a pest here. */
879     if( strstr( psz_file, "alsa_plugin" ) )
880     {
881         handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
882         if( handle == NULL )
883         {
884             msg_Warn( p_this, "cannot load module `%s' (%s)",
885                               psz_file, dlerror() );
886             return -1;
887         }
888     }
889 #   endif
890     /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
891      * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
892      * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
893     /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
894     if( !b_kde && !strstr( psz_file, "kde" ) )
895     {
896         dlopen( "libstdc++.so.6", RTLD_NOW )
897          || dlopen( "libstdc++.so.5", RTLD_NOW )
898          || dlopen( "libstdc++.so.4", RTLD_NOW )
899          || dlopen( "libstdc++.so.3", RTLD_NOW );
900         b_kde = VLC_TRUE;
901     }
902
903     handle = dlopen( psz_file, RTLD_NOW );
904     if( handle == NULL )
905     {
906         msg_Warn( p_this, "cannot load module `%s' (%s)",
907                           psz_file, dlerror() );
908         return -1;
909     }
910
911 #elif defined(HAVE_DL_DLOPEN)
912 #   if defined(DL_LAZY)
913     handle = dlopen( psz_file, DL_LAZY );
914 #   else
915     handle = dlopen( psz_file, 0 );
916 #   endif
917     if( handle == NULL )
918     {
919         msg_Warn( p_this, "cannot load module `%s' (%s)",
920                           psz_file, dlerror() );
921         return -1;
922     }
923
924 #elif defined(HAVE_DL_SHL_LOAD)
925     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
926     if( handle == NULL )
927     {
928         msg_Warn( p_this, "cannot load module `%s' (%s)",
929                           psz_file, strerror(errno) );
930         return -1;
931     }
932
933 #else
934 #   error "Something is wrong in modules.c"
935
936 #endif
937
938     /* Now that we have successfully loaded the module, we can
939      * allocate a structure for it */
940     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
941     if( p_module == NULL )
942     {
943         msg_Err( p_this, "out of memory" );
944         CloseModule( handle );
945         return -1;
946     }
947
948     /* We need to fill these since they may be needed by CallEntry() */
949 #ifdef UNDER_CE
950     p_module->psz_filename = psz_filename;
951 #else
952     p_module->psz_filename = psz_file;
953 #endif
954     p_module->handle = handle;
955     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
956
957     /* Initialize the module: fill p_module->psz_object_name, default config */
958     if( CallEntry( p_module ) != 0 )
959     {
960         /* We couldn't call module_init() */
961         vlc_object_destroy( p_module );
962         CloseModule( handle );
963         return -1;
964     }
965
966     DupModule( p_module );
967     p_module->psz_filename = strdup( p_module->psz_filename );
968     p_module->psz_longname = strdup( p_module->psz_longname );
969
970     /* Everything worked fine ! The module is ready to be added to the list. */
971     p_module->b_builtin = VLC_FALSE;
972
973     /* msg_Dbg( p_this, "plugin \"%s\", %s",
974                 p_module->psz_object_name, p_module->psz_longname ); */
975
976     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
977
978     return 0;
979 }
980
981 /*****************************************************************************
982  * DupModule: make a plugin module standalone.
983  *****************************************************************************
984  * This function duplicates all strings in the module, so that the dynamic
985  * object can be unloaded. It acts recursively on submodules.
986  *****************************************************************************/
987 static void DupModule( module_t *p_module )
988 {
989     char **pp_shortcut;
990     int i_submodule;
991
992     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
993     {
994         *pp_shortcut = strdup( *pp_shortcut );
995     }
996
997     /* We strdup() these entries so that they are still valid when the
998      * module is unloaded. */
999     p_module->psz_object_name = strdup( p_module->psz_object_name );
1000     p_module->psz_capability = strdup( p_module->psz_capability );
1001
1002     if( p_module->psz_program != NULL )
1003     {
1004         p_module->psz_program = strdup( p_module->psz_program );
1005     }
1006
1007     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1008     {
1009         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1010     }
1011 }
1012
1013 /*****************************************************************************
1014  * UndupModule: free a duplicated module.
1015  *****************************************************************************
1016  * This function frees the allocations done in DupModule().
1017  *****************************************************************************/
1018 static void UndupModule( module_t *p_module )
1019 {
1020     char **pp_shortcut;
1021     int i_submodule;
1022
1023     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1024     {
1025         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1026     }
1027
1028     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1029     {
1030         free( *pp_shortcut );
1031     }
1032
1033     free( p_module->psz_object_name );
1034     free( p_module->psz_capability );
1035
1036     if( p_module->psz_program != NULL )
1037     {
1038         free( p_module->psz_program );
1039     }
1040 }
1041
1042 #endif /* HAVE_DYNAMIC_PLUGINS */
1043
1044 /*****************************************************************************
1045  * AllocateBuiltinModule: initialize a builtin module.
1046  *****************************************************************************
1047  * This function registers a builtin module and allocates a structure
1048  * for its information data. The module can then be handled by module_Need
1049  * and module_Unneed. It can be removed by DeleteModule.
1050  *****************************************************************************/
1051 static int AllocateBuiltinModule( vlc_object_t * p_this,
1052                                   int ( *pf_entry ) ( module_t * ) )
1053 {
1054     module_t * p_module;
1055
1056     /* Now that we have successfully loaded the module, we can
1057      * allocate a structure for it */
1058     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1059     if( p_module == NULL )
1060     {
1061         msg_Err( p_this, "out of memory" );
1062         return -1;
1063     }
1064
1065     /* Initialize the module : fill p_module->psz_object_name, etc. */
1066     if( pf_entry( p_module ) != 0 )
1067     {
1068         /* With a well-written module we shouldn't have to print an
1069          * additional error message here, but just make sure. */
1070         msg_Err( p_this, "failed calling entry point in builtin module" );
1071         vlc_object_destroy( p_module );
1072         return -1;
1073     }
1074
1075     /* Everything worked fine ! The module is ready to be added to the list. */
1076     p_module->b_builtin = VLC_TRUE;
1077
1078     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1079                 p_module->psz_object_name, p_module->psz_longname ); */
1080
1081     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1082
1083     return 0;
1084 }
1085
1086 /*****************************************************************************
1087  * DeleteModule: delete a module and its structure.
1088  *****************************************************************************
1089  * This function can only be called if the module isn't being used.
1090  *****************************************************************************/
1091 static int DeleteModule( module_t * p_module )
1092 {
1093     vlc_object_detach( p_module );
1094
1095     /* We free the structures that we strdup()ed in Allocate*Module(). */
1096 #ifdef HAVE_DYNAMIC_PLUGINS
1097     if( !p_module->b_builtin )
1098     {
1099         if( p_module->b_unloadable )
1100         {
1101             CloseModule( p_module->handle );
1102         }
1103         UndupModule( p_module );
1104         free( p_module->psz_filename );
1105         free( p_module->psz_longname );
1106     }
1107 #endif
1108
1109     /* Free and detach the object's children */
1110     while( p_module->i_children )
1111     {
1112         vlc_object_t *p_this = p_module->pp_children[0];
1113         vlc_object_detach( p_this );
1114         vlc_object_destroy( p_this );
1115     }
1116
1117     config_Free( p_module );
1118     vlc_object_destroy( p_module );
1119
1120     return 0;
1121 }
1122
1123 #ifdef HAVE_DYNAMIC_PLUGINS
1124 /*****************************************************************************
1125  * CallEntry: call an entry point.
1126  *****************************************************************************
1127  * This function calls a symbol given its name and a module structure. The
1128  * symbol MUST refer to a function returning int and taking a module_t* as
1129  * an argument.
1130  *****************************************************************************/
1131 static int CallEntry( module_t * p_module )
1132 {
1133     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1134     int (* pf_symbol) ( module_t * p_module );
1135
1136     /* Try to resolve the symbol */
1137     pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1138
1139     if( pf_symbol == NULL )
1140     {
1141 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1142         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1143                             psz_name, p_module->psz_filename );
1144 #elif defined(HAVE_DL_WINDOWS)
1145         char *psz_error = GetWindowsError();
1146         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1147                             psz_name, p_module->psz_filename, psz_error );
1148         free( psz_error );
1149 #elif defined(HAVE_DL_DLOPEN)
1150         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1151                             psz_name, p_module->psz_filename, dlerror() );
1152 #elif defined(HAVE_DL_SHL_LOAD)
1153         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1154                             psz_name, p_module->psz_filename, strerror(errno) );
1155 #else
1156 #   error "Something is wrong in modules.c"
1157 #endif
1158     }
1159
1160     /* We can now try to call the symbol */
1161     if( pf_symbol( p_module ) != 0 )
1162     {
1163         /* With a well-written module we shouldn't have to print an
1164          * additional error message here, but just make sure. */
1165         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1166                            psz_name, p_module->psz_filename );
1167         return -1;
1168     }
1169
1170     /* Everything worked fine, we can return */
1171     return 0;
1172 }
1173
1174 /*****************************************************************************
1175  * CloseModule: unload a dynamic library
1176  *****************************************************************************
1177  * This function unloads a previously opened dynamically linked library
1178  * using a system dependant method. No return value is taken in consideration,
1179  * since some libraries sometimes refuse to close properly.
1180  *****************************************************************************/
1181 static void CloseModule( module_handle_t handle )
1182 {
1183 #if defined(HAVE_DL_DYLD)
1184     NSUnLinkModule( handle, FALSE );
1185
1186 #elif defined(HAVE_DL_BEOS)
1187     unload_add_on( handle );
1188
1189 #elif defined(HAVE_DL_WINDOWS)
1190     FreeLibrary( handle );
1191
1192 #elif defined(HAVE_DL_DLOPEN)
1193     dlclose( handle );
1194
1195 #elif defined(HAVE_DL_SHL_LOAD)
1196     shl_unload( handle );
1197
1198 #endif
1199     return;
1200 }
1201
1202 /*****************************************************************************
1203  * GetSymbol: get a symbol from a dynamic library
1204  *****************************************************************************
1205  * This function queries a loaded library for a symbol specified in a
1206  * string, and returns a pointer to it. We don't check for dlerror() or
1207  * similar functions, since we want a non-NULL symbol anyway.
1208  *****************************************************************************/
1209 static void * _module_getsymbol( module_handle_t, const char * );
1210
1211 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1212 {
1213     void * p_symbol = _module_getsymbol( handle, psz_function );
1214
1215     /* MacOS X dl library expects symbols to begin with "_". So do
1216      * some other operating systems. That's really lame, but hey, what
1217      * can we do ? */
1218     if( p_symbol == NULL )
1219     {
1220         char *psz_call = malloc( strlen( psz_function ) + 2 );
1221
1222         strcpy( psz_call + 1, psz_function );
1223         psz_call[ 0 ] = '_';
1224         p_symbol = _module_getsymbol( handle, psz_call );
1225         free( psz_call );
1226     }
1227
1228     return p_symbol;
1229 }
1230
1231 static void * _module_getsymbol( module_handle_t handle,
1232                                  const char * psz_function )
1233 {
1234 #if defined(HAVE_DL_DYLD)
1235     NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1236     return NSAddressOfSymbol( sym );
1237
1238 #elif defined(HAVE_DL_BEOS)
1239     void * p_symbol;
1240     if( B_OK == get_image_symbol( handle, psz_function,
1241                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1242     {
1243         return p_symbol;
1244     }
1245     else
1246     {
1247         return NULL;
1248     }
1249
1250 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1251     wchar_t psz_real[256];
1252     MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1253
1254     return (void *)GetProcAddress( handle, psz_real );
1255
1256 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1257     return (void *)GetProcAddress( handle, (MYCHAR*)psz_function );
1258
1259 #elif defined(HAVE_DL_DLOPEN)
1260     return dlsym( handle, psz_function );
1261
1262 #elif defined(HAVE_DL_SHL_LOAD)
1263     void *p_sym;
1264     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1265     return p_sym;
1266
1267 #endif
1268 }
1269
1270 #if defined(HAVE_DL_WINDOWS)
1271 static char * GetWindowsError( void )
1272 {
1273 #if defined(UNDER_CE)
1274     wchar_t psz_tmp[256];
1275     char * psz_buffer = malloc( 256 );
1276 #else
1277     char * psz_tmp = malloc( 256 );
1278 #endif
1279     int i = 0, i_error = GetLastError();
1280
1281     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1282                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1283                    (LPTSTR) psz_tmp, 256, NULL );
1284
1285     /* Go to the end of the string */
1286 #if defined(UNDER_CE)
1287     while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1288 #else
1289     while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1290 #endif
1291     {
1292         i++;
1293     }
1294
1295     if( psz_tmp[i] )
1296     {
1297 #if defined(UNDER_CE)
1298         swprintf( psz_tmp + i, L" (error %i)", i_error );
1299         psz_tmp[ 255 ] = L'\0';
1300 #else
1301         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1302         psz_tmp[ 255 ] = '\0';
1303 #endif
1304     }
1305
1306 #if defined(UNDER_CE)
1307     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1308                          psz_buffer, 256, NULL, NULL );
1309     return psz_buffer;
1310 #else
1311     return psz_tmp;
1312 #endif
1313 }
1314 #endif /* HAVE_DL_WINDOWS */
1315
1316 #endif /* HAVE_DYNAMIC_PLUGINS */