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