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