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