]> git.sesse.net Git - vlc/blob - src/misc/modules.c
* src/misc/*, src/extras/libc.c: bunch of WinCE fixes.
[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  *          Gildas Bazin <gbazin@videolan.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
28  * is set to 64. Don't try to be cleverer. */
29 #ifdef _FILE_OFFSET_BITS
30 #undef _FILE_OFFSET_BITS
31 #endif
32
33 #include <stdlib.h>                                      /* free(), strtol() */
34 #include <stdio.h>                                              /* sprintf() */
35 #include <string.h>                                              /* strdup() */
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #ifdef HAVE_DIRENT_H
41 #   include <dirent.h>
42 #elif defined( UNDER_CE )
43 #   include <windows.h>                               /* GetFileAttributes() */
44 #else
45 #   include "../extras/dirent.h"
46 #endif
47
48 #ifdef HAVE_SYS_TYPES_H
49 #   include <sys/types.h>
50 #endif
51 #ifdef HAVE_SYS_STAT_H
52 #   include <sys/stat.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 #   include <unistd.h>
56 #endif
57
58 #define HAVE_DYNAMIC_PLUGINS
59 #if defined(HAVE_DL_DYLD)
60 #   if defined(HAVE_MACH_O_DYLD_H)
61 #       include <mach-o/dyld.h>
62 #   endif
63 #elif defined(HAVE_DL_BEOS)
64 #   if defined(HAVE_IMAGE_H)
65 #       include <image.h>
66 #   endif
67 #elif defined(HAVE_DL_WINDOWS)
68 #   include <windows.h>
69 #elif defined(HAVE_DL_DLOPEN)
70 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
71 #       include <dlfcn.h>
72 #   endif
73 #   if defined(HAVE_SYS_DL_H)
74 #       include <sys/dl.h>
75 #   endif
76 #elif defined(HAVE_DL_SHL_LOAD)
77 #   if defined(HAVE_DL_H)
78 #       include <dl.h>
79 #   endif
80 #else
81 #   undef HAVE_DYNAMIC_PLUGINS
82 #endif
83
84 #include "vlc_error.h"
85
86 #include "vlc_interface.h"
87 #include "intf_eject.h"
88
89 #include "vlc_playlist.h"
90
91 #include "vlc_video.h"
92 #include "video_output.h"
93 #include "vout_synchro.h"
94 #include "vlc_spu.h"
95
96 #include "audio_output.h"
97 #include "aout_internal.h"
98
99 #include "stream_output.h"
100 #include "osd.h"
101 #include "vlc_httpd.h"
102
103 #include "iso_lang.h"
104 #include "charset.h"
105
106 #include "vlc_block.h"
107
108 #include "vlc_vlm.h"
109
110 #ifdef HAVE_DYNAMIC_PLUGINS
111 #   include "modules_plugin.h"
112 #endif
113
114 #if defined( UNDER_CE )
115 #    include "modules_builtin_evc.h"
116 #elif defined( _MSC_VER )
117 #    include "modules_builtin_msvc.h"
118 #else
119 #    include "modules_builtin.h"
120 #endif
121 #include "network.h"
122
123 #if defined( WIN32) || defined( UNDER_CE )
124     /* Avoid name collisions */
125 #   define LoadModule(a,b,c) _LoadModule(a,b,c)
126 #endif
127
128 /*****************************************************************************
129  * Local prototypes
130  *****************************************************************************/
131 #ifdef HAVE_DYNAMIC_PLUGINS
132 static void AllocateAllPlugins  ( vlc_object_t * );
133 static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
134 static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
135 static module_t * AllocatePlugin( vlc_object_t *, char * );
136 #endif
137 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
138 static int  DeleteModule ( module_t * );
139 #ifdef HAVE_DYNAMIC_PLUGINS
140 static void   DupModule        ( module_t * );
141 static void   UndupModule      ( module_t * );
142 static int    CallEntry        ( module_t * );
143 static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
144 static void   CloseModule      ( module_handle_t );
145 static void * GetSymbol        ( module_handle_t, const char * );
146 static void   CacheLoad        ( vlc_object_t * );
147 static int    CacheLoadConfig  ( module_t *, FILE * );
148 static void   CacheSave        ( vlc_object_t * );
149 static void   CacheSaveConfig  ( module_t *, FILE * );
150 static void   CacheMerge       ( vlc_object_t *, module_t *, module_t * );
151 static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );
152
153 #if defined(HAVE_DL_WINDOWS)
154 static char * GetWindowsError  ( void );
155 #endif
156 #endif
157
158 /*****************************************************************************
159  * module_InitBank: create the module bank.
160  *****************************************************************************
161  * This function creates a module bank structure which will be filled later
162  * on with all the modules found.
163  *****************************************************************************/
164 void __module_InitBank( vlc_object_t *p_this )
165 {
166     module_bank_t *p_bank;
167     vlc_value_t  lockval;
168
169     var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
170     var_Get( p_this->p_libvlc, "libvlc", &lockval );
171     vlc_mutex_lock( lockval.p_address );
172     if( p_this->p_libvlc->p_module_bank )
173     {
174         p_this->p_libvlc->p_module_bank->i_usage++;
175         vlc_mutex_unlock( lockval.p_address );
176         var_Destroy( p_this->p_libvlc, "libvlc" );
177         return;
178     }
179     vlc_mutex_unlock( lockval.p_address );
180     var_Destroy( p_this->p_libvlc, "libvlc" );
181
182     p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
183     p_bank->psz_object_name = "module bank";
184     p_bank->i_usage = 1;
185     p_bank->i_cache = p_bank->i_loaded_cache = 0;
186     p_bank->pp_cache = p_bank->pp_loaded_cache = 0;
187     p_bank->b_cache = p_bank->b_cache_dirty =
188         p_bank->b_cache_delete = VLC_FALSE;
189
190     /*
191      * Store the symbols to be exported
192      */
193 #ifdef HAVE_DYNAMIC_PLUGINS
194     STORE_SYMBOLS( &p_bank->symbols );
195 #endif
196
197     /* Everything worked, attach the object */
198     p_this->p_libvlc->p_module_bank = p_bank;
199     vlc_object_attach( p_bank, p_this->p_libvlc );
200
201     module_LoadMain( p_this );
202
203     return;
204 }
205
206 /*****************************************************************************
207  * module_ResetBank: reset the module bank.
208  *****************************************************************************
209  * This function resets the module bank by unloading all unused plugin
210  * modules.
211  *****************************************************************************/
212 void __module_ResetBank( vlc_object_t *p_this )
213 {
214     msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
215     return;
216 }
217
218 /*****************************************************************************
219  * module_EndBank: empty the module bank.
220  *****************************************************************************
221  * This function unloads all unused plugin modules and empties the module
222  * bank in case of success.
223  *****************************************************************************/
224 void __module_EndBank( vlc_object_t *p_this )
225 {
226     module_t * p_next;
227     vlc_value_t  lockval;
228
229     var_Create( p_this->p_libvlc, "libvlc", VLC_VAR_MUTEX );
230     var_Get( p_this->p_libvlc, "libvlc", &lockval );
231     vlc_mutex_lock( lockval.p_address );
232     if( !p_this->p_libvlc->p_module_bank )
233     {
234         vlc_mutex_unlock( lockval.p_address );
235         var_Destroy( p_this->p_libvlc, "libvlc" );
236         return;
237     }
238     if( --p_this->p_libvlc->p_module_bank->i_usage )
239     {
240         vlc_mutex_unlock( lockval.p_address );
241         var_Destroy( p_this->p_libvlc, "libvlc" );
242         return;
243     }
244     vlc_mutex_unlock( lockval.p_address );
245     var_Destroy( p_this->p_libvlc, "libvlc" );
246
247 #ifdef HAVE_DYNAMIC_PLUGINS
248     if( p_this->p_libvlc->p_module_bank->b_cache ) CacheSave( p_this );
249 #endif
250
251     vlc_object_detach( p_this->p_libvlc->p_module_bank );
252
253     while( p_this->p_libvlc->p_module_bank->i_children )
254     {
255         p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
256
257         if( DeleteModule( p_next ) )
258         {
259             /* Module deletion failed */
260             msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
261                      p_next->psz_object_name );
262
263             /* We just free the module by hand. Niahahahahaha. */
264             vlc_object_detach( p_next );
265             vlc_object_destroy( p_next );
266         }
267     }
268
269     vlc_object_destroy( p_this->p_libvlc->p_module_bank );
270
271     return;
272 }
273
274 /*****************************************************************************
275  * module_LoadMain: load the main program info into the module bank.
276  *****************************************************************************
277  * This function fills the module bank structure with the main module infos.
278  * This is very useful as it will allow us to consider the main program just
279  * as another module, and for instance the configuration options of main will
280  * be available in the module bank structure just as for every other module.
281  *****************************************************************************/
282 void __module_LoadMain( vlc_object_t *p_this )
283 {
284     AllocateBuiltinModule( p_this, vlc_entry__main );
285 }
286
287 /*****************************************************************************
288  * module_LoadBuiltins: load all modules which we built with.
289  *****************************************************************************
290  * This function fills the module bank structure with the builtin modules.
291  *****************************************************************************/
292 void __module_LoadBuiltins( vlc_object_t * p_this )
293 {
294     msg_Dbg( p_this, "checking builtin modules" );
295     ALLOCATE_ALL_BUILTINS();
296 }
297
298 /*****************************************************************************
299  * module_LoadPlugins: load all plugin modules we can find.
300  *****************************************************************************
301  * This function fills the module bank structure with the plugin modules.
302  *****************************************************************************/
303 void __module_LoadPlugins( vlc_object_t * p_this )
304 {
305 #ifdef HAVE_DYNAMIC_PLUGINS
306     msg_Dbg( p_this, "checking plugin modules" );
307
308     if( config_GetInt( p_this, "plugins-cache" ) )
309         p_this->p_libvlc->p_module_bank->b_cache = VLC_TRUE;
310
311     if( p_this->p_libvlc->p_module_bank->b_cache ||
312         p_this->p_libvlc->p_module_bank->b_cache_delete ) CacheLoad( p_this );
313
314     AllocateAllPlugins( p_this );
315 #endif
316 }
317
318 /*****************************************************************************
319  * module_Need: return the best module function, given a capability list.
320  *****************************************************************************
321  * This function returns the module that best fits the asked capabilities.
322  *****************************************************************************/
323 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
324                           const char *psz_name, vlc_bool_t b_strict )
325 {
326     typedef struct module_list_t module_list_t;
327
328     struct module_list_t
329     {
330         module_t *p_module;
331         int i_score;
332         vlc_bool_t b_force;
333         module_list_t *p_next;
334     };
335
336     module_list_t *p_list, *p_first, *p_tmp;
337     vlc_list_t *p_all;
338
339     int i_which_module, i_index = 0;
340     vlc_bool_t b_intf = VLC_FALSE;
341
342     module_t *p_module;
343
344     int   i_shortcuts = 0;
345     char *psz_shortcuts = NULL, *psz_var = NULL;
346
347     msg_Dbg( p_this, "looking for %s module", psz_capability );
348
349     /* Deal with variables */
350     if( psz_name && psz_name[0] == '$' )
351     {
352         vlc_value_t val;
353         var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
354         var_Get( p_this, psz_name + 1, &val );
355         psz_var = val.psz_string;
356         psz_name = psz_var;
357     }
358
359     /* Count how many different shortcuts were asked for */
360     if( psz_name && *psz_name )
361     {
362         char *psz_parser, *psz_last_shortcut;
363
364         /* If the user wants none, give him none. */
365         if( !strcmp( psz_name, "none" ) )
366         {
367             if( psz_var ) free( psz_var );
368             return NULL;
369         }
370
371         i_shortcuts++;
372         psz_shortcuts = psz_last_shortcut = strdup( psz_name );
373
374         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
375         {
376             if( *psz_parser == ',' )
377             {
378                  *psz_parser = '\0';
379                  i_shortcuts++;
380                  psz_last_shortcut = psz_parser + 1;
381             }
382         }
383
384         /* Check if the user wants to override the "strict" mode */
385         if( psz_last_shortcut )
386         {
387             if( !strcmp(psz_last_shortcut, "none") )
388             {
389                 b_strict = VLC_TRUE;
390                 i_shortcuts--;
391             }
392             else if( !strcmp(psz_last_shortcut, "any") )
393             {
394                 b_strict = VLC_FALSE;
395                 i_shortcuts--;
396             }
397         }
398     }
399
400     /* Sort the modules and test them */
401     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
402     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
403     p_first = NULL;
404
405     /* Parse the module list for capabilities and probe each of them */
406     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
407     {
408         int i_shortcut_bonus = 0;
409
410         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
411
412         /* Test that this module can do what we need */
413         if( strcmp( p_module->psz_capability, psz_capability ) )
414         {
415             /* Don't recurse through the sub-modules because vlc_list_find()
416              * will list them anyway. */
417             continue;
418         }
419
420         /* Test if we have the required CPU */
421         if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
422         {
423             continue;
424         }
425
426         /* If we required a shortcut, check this plugin provides it. */
427         if( i_shortcuts > 0 )
428         {
429             vlc_bool_t b_trash;
430             int i_dummy, i_short = i_shortcuts;
431             char *psz_name = psz_shortcuts;
432
433             /* Let's drop modules with a <= 0 score (unless they are
434              * explicitly requested) */
435             b_trash = p_module->i_score <= 0;
436
437             while( i_short > 0 )
438             {
439                 for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
440                 {
441                     if( !strcasecmp( psz_name,
442                                      p_module->pp_shortcuts[i_dummy] ) )
443                     {
444                         /* Found it */
445                         b_trash = VLC_FALSE;
446                         i_shortcut_bonus = i_short * 10000;
447                         break;
448                     }
449                 }
450
451                 if( i_shortcut_bonus )
452                 {
453                     /* We found it... remember ? */
454                     break;
455                 }
456
457                 /* Go to the next shortcut... This is so lame! */
458                 while( *psz_name )
459                 {
460                     psz_name++;
461                 }
462                 psz_name++;
463                 i_short--;
464             }
465
466             /* If we are in "strict" mode and we couldn't
467              * find the module in the list of provided shortcuts,
468              * then kick the bastard out of here!!! */
469             if( i_short == 0 && b_strict )
470             {
471                 b_trash = VLC_TRUE;
472             }
473
474             if( b_trash )
475             {
476                 continue;
477             }
478         }
479         /* If we didn't require a shortcut, trash <= 0 scored plugins */
480         else if( p_module->i_score <= 0 )
481         {
482             continue;
483         }
484
485         /* Special case: test if we requested a particular intf plugin */
486         if( !i_shortcuts && p_module->psz_program
487              && !strcmp( p_module->psz_program,
488                          p_this->p_vlc->psz_object_name ) )
489         {
490             if( !b_intf )
491             {
492                 /* Remove previous non-matching plugins */
493                 i_index = 0;
494                 b_intf = VLC_TRUE;
495             }
496         }
497         else if( b_intf )
498         {
499             /* This one doesn't match */
500             continue;
501         }
502
503         /* Store this new module */
504         p_list[ i_index ].p_module = p_module;
505         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
506         p_list[ i_index ].b_force = !!i_shortcut_bonus;
507
508         /* Add it to the modules-to-probe list */
509         if( i_index == 0 )
510         {
511             p_list[ 0 ].p_next = NULL;
512             p_first = p_list;
513         }
514         else
515         {
516             /* Ok, so at school you learned that quicksort is quick, and
517              * bubble sort sucks raw eggs. But that's when dealing with
518              * thousands of items. Here we have barely 50. */
519             module_list_t *p_newlist = p_first;
520
521             if( p_first->i_score < p_list[ i_index ].i_score )
522             {
523                 p_list[ i_index ].p_next = p_first;
524                 p_first = &p_list[ i_index ];
525             }
526             else
527             {
528                 while( p_newlist->p_next != NULL &&
529                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
530                 {
531                     p_newlist = p_newlist->p_next;
532                 }
533
534                 p_list[ i_index ].p_next = p_newlist->p_next;
535                 p_newlist->p_next = &p_list[ i_index ];
536             }
537         }
538
539         i_index++;
540     }
541
542     msg_Dbg( p_this, "probing %i candidate%s",
543                      i_index, i_index == 1 ? "" : "s" );
544
545     /* Lock all candidate modules */
546     p_tmp = p_first;
547     while( p_tmp != NULL )
548     {
549         vlc_object_yield( p_tmp->p_module );
550         p_tmp = p_tmp->p_next;
551     }
552
553     /* We can release the list, interesting modules were yielded */
554     vlc_list_release( p_all );
555
556     /* Parse the linked list and use the first successful module */
557     p_tmp = p_first;
558     while( p_tmp != NULL )
559     {
560 #ifdef HAVE_DYNAMIC_PLUGINS
561         /* Make sure the module is loaded in mem */
562         module_t *p_module = p_tmp->p_module->b_submodule ?
563             (module_t *)p_tmp->p_module->p_parent : p_tmp->p_module;
564         if( !p_module->b_builtin && !p_module->b_loaded )
565         {
566             module_t *p_new_module =
567                 AllocatePlugin( p_this, p_module->psz_filename );
568             if( p_new_module )
569             {
570                 CacheMerge( p_this, p_module, p_new_module );
571                 vlc_object_attach( p_new_module, p_module );
572                 DeleteModule( p_new_module );
573             }
574         }
575 #endif
576
577         p_this->b_force = p_tmp->b_force;
578         if( p_tmp->p_module->pf_activate
579              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
580         {
581             break;
582         }
583
584         vlc_object_release( p_tmp->p_module );
585         p_tmp = p_tmp->p_next;
586     }
587
588     /* Store the locked module value */
589     if( p_tmp != NULL )
590     {
591         p_module = p_tmp->p_module;
592         p_tmp = p_tmp->p_next;
593     }
594     else
595     {
596         p_module = NULL;
597     }
598
599     /* Unlock the remaining modules */
600     while( p_tmp != NULL )
601     {
602         vlc_object_release( p_tmp->p_module );
603         p_tmp = p_tmp->p_next;
604     }
605
606     free( p_list );
607     p_this->b_force = VLC_FALSE;
608
609     if( p_module != NULL )
610     {
611         msg_Dbg( p_module, "using %s module \"%s\"",
612                  psz_capability, p_module->psz_object_name );
613     }
614     else if( p_first == NULL )
615     {
616         if( !strcmp( psz_capability, "access_demux" ) )
617         {
618             msg_Warn( p_this, "no %s module matched \"%s\"",
619                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
620         }
621         else
622         {  
623             msg_Err( p_this, "no %s module matched \"%s\"",
624                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
625         }
626     }
627     else if( psz_name != NULL && *psz_name )
628     {
629         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
630                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
631     }
632
633     if( psz_shortcuts )
634     {
635         free( psz_shortcuts );
636     }
637
638     if( psz_var )
639     {
640         free( psz_var );
641     }
642
643     /* Don't forget that the module is still locked */
644     return p_module;
645 }
646
647 /*****************************************************************************
648  * module_Unneed: decrease the usage count of a module.
649  *****************************************************************************
650  * This function must be called by the thread that called module_Need, to
651  * decrease the reference count and allow for hiding of modules.
652  *****************************************************************************/
653 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
654 {
655     /* Use the close method */
656     if( p_module->pf_deactivate )
657     {
658         p_module->pf_deactivate( p_this );
659     }
660
661     msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
662
663     vlc_object_release( p_module );
664
665     return;
666 }
667
668 /*****************************************************************************
669  * Following functions are local.
670  *****************************************************************************/
671
672 /*****************************************************************************
673  * AllocateAllPlugins: load all plugin modules we can find.
674  *****************************************************************************/
675 #ifdef HAVE_DYNAMIC_PLUGINS
676 static void AllocateAllPlugins( vlc_object_t *p_this )
677 {
678     /* Yes, there are two NULLs because we replace one with "plugin-path". */
679 #ifdef WIN32
680     char *          path[] = { "modules", "", "plugins", 0, 0 };
681 #else
682     char *          path[] = { "modules", PLUGIN_PATH, "plugins", 0, 0 };
683 #endif
684
685     char **         ppsz_path = path;
686     char *          psz_fullpath;
687
688     /* If the user provided a plugin path, we add it to the list */
689     path[ sizeof(path)/sizeof(char*) - 2 ] =
690         config_GetPsz( p_this, "plugin-path" );
691
692     for( ; *ppsz_path != NULL ; ppsz_path++ )
693     {
694         if( !(*ppsz_path)[0] ) continue;
695
696 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
697
698         /* Handle relative as well as absolute paths */
699 #ifdef WIN32
700         if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
701             (*ppsz_path)[1] != ':' )
702 #else
703         if( (*ppsz_path)[0] != '/' )
704 #endif
705         {
706             int i_dirlen = strlen( *ppsz_path );
707             i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
708
709             psz_fullpath = malloc( i_dirlen );
710             if( psz_fullpath == NULL )
711             {
712                 continue;
713             }
714 #ifdef WIN32
715             sprintf( psz_fullpath, "%s\\%s",
716                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
717 #else
718             sprintf( psz_fullpath, "%s/%s",
719                      p_this->p_libvlc->psz_vlcpath, *ppsz_path );
720 #endif
721         }
722         else
723 #endif
724         {
725             psz_fullpath = strdup( *ppsz_path );
726         }
727
728         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
729
730         /* Don't go deeper than 5 subdirectories */
731         AllocatePluginDir( p_this, psz_fullpath, 5 );
732
733         free( psz_fullpath );
734     }
735
736     /* Free plugin-path */
737     if( path[ sizeof(path)/sizeof(char*) - 2 ] )
738         free( path[ sizeof(path)/sizeof(char*) - 2 ] );
739     path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
740 }
741
742 /*****************************************************************************
743  * AllocatePluginDir: recursively parse a directory to look for plugins
744  *****************************************************************************/
745 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
746                                int i_maxdepth )
747 {
748 #if defined( UNDER_CE ) || defined( _MSC_VER )
749 #ifdef UNDER_CE
750     wchar_t psz_wpath[MAX_PATH + 256];
751     wchar_t psz_wdir[MAX_PATH];
752 #endif
753     char psz_path[MAX_PATH + 256];
754     WIN32_FIND_DATA finddata;
755     HANDLE handle;
756     unsigned int rc;
757 #else
758     int    i_dirlen;
759     DIR *  dir;
760     struct dirent * file;
761 #endif
762     char * psz_file;
763
764     if( p_this->p_vlc->b_die || i_maxdepth < 0 )
765     {
766         return;
767     }
768
769 #if defined( UNDER_CE ) || defined( _MSC_VER )
770 #ifdef UNDER_CE
771     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
772
773     rc = GetFileAttributes( psz_wdir );
774     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
775
776     /* Parse all files in the directory */
777     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
778 #else
779     rc = GetFileAttributes( psz_dir );
780     if( !(rc & FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
781 #endif
782
783     /* Parse all files in the directory */
784     sprintf( psz_path, "%s\\*", psz_dir );
785
786 #ifdef UNDER_CE
787     handle = FindFirstFile( psz_wpath, &finddata );
788 #else
789     handle = FindFirstFile( psz_path, &finddata );
790 #endif
791     if( handle == INVALID_HANDLE_VALUE )
792     {
793         /* Empty directory */
794         return;
795     }
796
797     /* Parse the directory and try to load all files it contains. */
798     do
799     {
800 #ifdef UNDER_CE
801         unsigned int i_len = wcslen( finddata.cFileName );
802         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
803         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
804 #else
805         unsigned int i_len = strlen( finddata.cFileName );
806         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
807 #endif
808
809         /* Skip ".", ".." and anything starting with "." */
810         if( !*finddata.cFileName || *finddata.cFileName == '.' )
811         {
812             if( !FindNextFile( handle, &finddata ) ) break;
813             continue;
814         }
815
816 #ifdef UNDER_CE
817         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
818 #else
819         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
820 #endif
821         {
822             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
823         }
824         else if( i_len > strlen( LIBEXT )
825                   /* We only load files ending with LIBEXT */
826                   && !strncasecmp( psz_path + strlen( psz_path)
827                                    - strlen( LIBEXT ),
828                                    LIBEXT, strlen( LIBEXT ) ) )
829         {
830             psz_file = psz_path;
831
832             AllocatePluginFile( p_this, psz_file, 0, 0 );
833         }
834     }
835     while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
836
837     /* Close the directory */
838     FindClose( handle );
839
840 #else
841     dir = opendir( psz_dir );
842     if( !dir )
843     {
844         return;
845     }
846
847     i_dirlen = strlen( psz_dir );
848
849     /* Parse the directory and try to load all files it contains. */
850     while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
851     {
852         struct stat statbuf;
853         unsigned int i_len;
854         int i_stat;
855
856         /* Skip ".", ".." and anything starting with "." */
857         if( !*file->d_name || *file->d_name == '.' )
858         {
859             continue;
860         }
861
862         i_len = strlen( file->d_name );
863         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
864 #ifdef WIN32
865         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
866 #else
867         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
868 #endif
869
870         i_stat = stat( psz_file, &statbuf );
871         if( !i_stat && statbuf.st_mode & S_IFDIR )
872         {
873             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
874         }
875         else if( i_len > strlen( LIBEXT )
876                   /* We only load files ending with LIBEXT */
877                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
878                                    LIBEXT, strlen( LIBEXT ) ) )
879         {
880             int64_t i_time = 0, i_size = 0;
881
882             if( !i_stat )
883             {
884                 i_time = statbuf.st_mtime;
885                 i_size = statbuf.st_size;
886             }
887
888             AllocatePluginFile( p_this, psz_file, i_time, i_size );
889         }
890
891         free( psz_file );
892     }
893
894     /* Close the directory */
895     closedir( dir );
896
897 #endif
898 }
899
900 /*****************************************************************************
901  * AllocatePluginFile: load a module into memory and initialize it.
902  *****************************************************************************
903  * This function loads a dynamically loadable module and allocates a structure
904  * for its information data. The module can then be handled by module_Need
905  * and module_Unneed. It can be removed by DeleteModule.
906  *****************************************************************************/
907 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
908                                int64_t i_file_time, int64_t i_file_size )
909 {
910     module_t * p_module;
911     module_cache_t *p_cache_entry = NULL;
912
913     /*
914      * Check our plugins cache first then load plugin if needed
915      */
916     p_cache_entry =
917         CacheFind( p_this, psz_file, i_file_time, i_file_size );
918
919     if( !p_cache_entry )
920     {
921         p_module = AllocatePlugin( p_this, psz_file );
922     }
923     else
924     {
925         /* If junk dll, don't try to load it */
926         if( p_cache_entry->b_junk )
927         {
928             p_module = NULL;
929         }
930         else
931         {
932             module_config_t *p_item;
933
934             p_module = p_cache_entry->p_module;
935             p_module->b_loaded = VLC_FALSE;
936
937             /* For now we force loading if the module's config contains
938              * callbacks or actions.
939              * Could be optimized by adding an API call.*/
940             for( p_item = p_module->p_config;
941                  p_item->i_type != CONFIG_HINT_END; p_item++ )
942             {
943                 if( p_item->pf_callback || p_item->i_action )
944                     p_module = AllocatePlugin( p_this, psz_file );
945             }
946         }
947     }
948
949     if( p_module )
950     {
951         /* Everything worked fine !
952          * The module is ready to be added to the list. */
953         p_module->b_builtin = VLC_FALSE;
954
955         /* msg_Dbg( p_this, "plugin \"%s\", %s",
956                     p_module->psz_object_name, p_module->psz_longname ); */
957
958         vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
959     }
960
961     if( !p_this->p_libvlc->p_module_bank->b_cache ) return 0;
962
963     /* Add entry to cache */
964 #define p_bank p_this->p_libvlc->p_module_bank
965     p_bank->pp_cache =
966         realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
967     p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
968     p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
969     p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
970     p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
971     p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
972     p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
973     p_bank->i_cache++;
974
975     return p_module ? 0 : -1;
976 }
977
978 /*****************************************************************************
979  * AllocatePlugin: load a module into memory and initialize it.
980  *****************************************************************************
981  * This function loads a dynamically loadable module and allocates a structure
982  * for its information data. The module can then be handled by module_Need
983  * and module_Unneed. It can be removed by DeleteModule.
984  *****************************************************************************/
985 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
986 {
987     module_t * p_module;
988     module_handle_t handle;
989
990     if( LoadModule( p_this, psz_file, &handle ) ) return NULL;
991
992     /* Now that we have successfully loaded the module, we can
993      * allocate a structure for it */
994     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
995     if( p_module == NULL )
996     {
997         msg_Err( p_this, "out of memory" );
998         CloseModule( handle );
999         return NULL;
1000     }
1001
1002     /* We need to fill these since they may be needed by CallEntry() */
1003     p_module->psz_filename = psz_file;
1004     p_module->handle = handle;
1005     p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
1006     p_module->b_loaded = VLC_TRUE;
1007
1008     /* Initialize the module: fill p_module, default config */
1009     if( CallEntry( p_module ) != 0 )
1010     {
1011         /* We couldn't call module_init() */
1012         vlc_object_destroy( p_module );
1013         CloseModule( handle );
1014         return NULL;
1015     }
1016
1017     DupModule( p_module );
1018     p_module->psz_filename = strdup( p_module->psz_filename );
1019     p_module->psz_longname = strdup( p_module->psz_longname );
1020
1021     /* Everything worked fine ! The module is ready to be added to the list. */
1022     p_module->b_builtin = VLC_FALSE;
1023
1024     return p_module;
1025 }
1026
1027 /*****************************************************************************
1028  * DupModule: make a plugin module standalone.
1029  *****************************************************************************
1030  * This function duplicates all strings in the module, so that the dynamic
1031  * object can be unloaded. It acts recursively on submodules.
1032  *****************************************************************************/
1033 static void DupModule( module_t *p_module )
1034 {
1035     char **pp_shortcut;
1036     int i_submodule;
1037
1038     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1039     {
1040         *pp_shortcut = strdup( *pp_shortcut );
1041     }
1042
1043     /* We strdup() these entries so that they are still valid when the
1044      * module is unloaded. */
1045     p_module->psz_object_name = strdup( p_module->psz_object_name );
1046     p_module->psz_capability = strdup( p_module->psz_capability );
1047
1048     if( p_module->psz_program != NULL )
1049     {
1050         p_module->psz_program = strdup( p_module->psz_program );
1051     }
1052
1053     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1054     {
1055         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1056     }
1057 }
1058
1059 /*****************************************************************************
1060  * UndupModule: free a duplicated module.
1061  *****************************************************************************
1062  * This function frees the allocations done in DupModule().
1063  *****************************************************************************/
1064 static void UndupModule( module_t *p_module )
1065 {
1066     char **pp_shortcut;
1067     int i_submodule;
1068
1069     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1070     {
1071         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1072     }
1073
1074     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1075     {
1076         free( *pp_shortcut );
1077     }
1078
1079     free( p_module->psz_object_name );
1080     free( p_module->psz_capability );
1081
1082     if( p_module->psz_program != NULL )
1083     {
1084         free( p_module->psz_program );
1085     }
1086 }
1087
1088 #endif /* HAVE_DYNAMIC_PLUGINS */
1089
1090 /*****************************************************************************
1091  * AllocateBuiltinModule: initialize a builtin module.
1092  *****************************************************************************
1093  * This function registers a builtin module and allocates a structure
1094  * for its information data. The module can then be handled by module_Need
1095  * and module_Unneed. It can be removed by DeleteModule.
1096  *****************************************************************************/
1097 static int AllocateBuiltinModule( vlc_object_t * p_this,
1098                                   int ( *pf_entry ) ( module_t * ) )
1099 {
1100     module_t * p_module;
1101
1102     /* Now that we have successfully loaded the module, we can
1103      * allocate a structure for it */
1104     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1105     if( p_module == NULL )
1106     {
1107         msg_Err( p_this, "out of memory" );
1108         return -1;
1109     }
1110
1111     /* Initialize the module : fill p_module->psz_object_name, etc. */
1112     if( pf_entry( p_module ) != 0 )
1113     {
1114         /* With a well-written module we shouldn't have to print an
1115          * additional error message here, but just make sure. */
1116         msg_Err( p_this, "failed calling entry point in builtin module" );
1117         vlc_object_destroy( p_module );
1118         return -1;
1119     }
1120
1121     /* Everything worked fine ! The module is ready to be added to the list. */
1122     p_module->b_builtin = VLC_TRUE;
1123
1124     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1125                 p_module->psz_object_name, p_module->psz_longname ); */
1126
1127     vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1128
1129     return 0;
1130 }
1131
1132 /*****************************************************************************
1133  * DeleteModule: delete a module and its structure.
1134  *****************************************************************************
1135  * This function can only be called if the module isn't being used.
1136  *****************************************************************************/
1137 static int DeleteModule( module_t * p_module )
1138 {
1139     vlc_object_detach( p_module );
1140
1141     /* We free the structures that we strdup()ed in Allocate*Module(). */
1142 #ifdef HAVE_DYNAMIC_PLUGINS
1143     if( !p_module->b_builtin )
1144     {
1145         if( p_module->b_loaded && p_module->b_unloadable )
1146         {
1147             CloseModule( p_module->handle );
1148         }
1149         UndupModule( p_module );
1150         free( p_module->psz_filename );
1151         free( p_module->psz_longname );
1152     }
1153 #endif
1154
1155     /* Free and detach the object's children */
1156     while( p_module->i_children )
1157     {
1158         vlc_object_t *p_this = p_module->pp_children[0];
1159         vlc_object_detach( p_this );
1160         vlc_object_destroy( p_this );
1161     }
1162
1163     config_Free( p_module );
1164     vlc_object_destroy( p_module );
1165
1166     return 0;
1167 }
1168
1169 #ifdef HAVE_DYNAMIC_PLUGINS
1170 /*****************************************************************************
1171  * CallEntry: call an entry point.
1172  *****************************************************************************
1173  * This function calls a symbol given its name and a module structure. The
1174  * symbol MUST refer to a function returning int and taking a module_t* as
1175  * an argument.
1176  *****************************************************************************/
1177 static int CallEntry( module_t * p_module )
1178 {
1179     static char *psz_name = "vlc_entry" MODULE_SUFFIX;
1180     int (* pf_symbol) ( module_t * p_module );
1181
1182     /* Try to resolve the symbol */
1183     pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1184
1185     if( pf_symbol == NULL )
1186     {
1187 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1188         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1189                             psz_name, p_module->psz_filename );
1190 #elif defined(HAVE_DL_WINDOWS)
1191         char *psz_error = GetWindowsError();
1192         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1193                             psz_name, p_module->psz_filename, psz_error );
1194         free( psz_error );
1195 #elif defined(HAVE_DL_DLOPEN)
1196         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1197                             psz_name, p_module->psz_filename, dlerror() );
1198 #elif defined(HAVE_DL_SHL_LOAD)
1199         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1200                             psz_name, p_module->psz_filename, strerror(errno) );
1201 #else
1202 #   error "Something is wrong in modules.c"
1203 #endif
1204         return -1;
1205     }
1206
1207     /* We can now try to call the symbol */
1208     if( pf_symbol( p_module ) != 0 )
1209     {
1210         /* With a well-written module we shouldn't have to print an
1211          * additional error message here, but just make sure. */
1212         msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
1213                            psz_name, p_module->psz_filename );
1214         return -1;
1215     }
1216
1217     /* Everything worked fine, we can return */
1218     return 0;
1219 }
1220
1221 /*****************************************************************************
1222  * LoadModule: loads a dynamic library
1223  *****************************************************************************
1224  * This function loads a dynamically linked library using a system dependant
1225  * method. Will return 0 on success as well as the module handle.
1226  *****************************************************************************/
1227 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1228                        module_handle_t *p_handle )
1229 {
1230     module_handle_t handle;
1231
1232 #if defined(HAVE_DL_DYLD)
1233     NSObjectFileImage image;
1234     NSObjectFileImageReturnCode ret;
1235
1236     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1237
1238     if( ret != NSObjectFileImageSuccess )
1239     {
1240         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1241         return -1;
1242     }
1243
1244     /* Open the dynamic module */
1245     handle = NSLinkModule( image, psz_file,
1246                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1247
1248     if( !handle )
1249     {
1250         NSLinkEditErrors errors;
1251         const char *psz_file, *psz_err;
1252         int i_errnum;
1253         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1254         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1255         NSDestroyObjectFileImage( image );
1256         return -1;
1257     }
1258
1259     /* Destroy our image, we won't need it */
1260     NSDestroyObjectFileImage( image );
1261
1262 #elif defined(HAVE_DL_BEOS)
1263     handle = load_add_on( psz_file );
1264     if( handle < 0 )
1265     {
1266         msg_Warn( p_this, "cannot load module `%s'", psz_file );
1267         return -1;
1268     }
1269
1270 #elif defined(HAVE_DL_WINDOWS)
1271 #ifdef UNDER_CE
1272     {
1273         wchar_t psz_wfile[MAX_PATH];
1274         MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1275         handle = LoadLibrary( psz_wfile );
1276     }
1277 #else
1278     handle = LoadLibrary( psz_file );
1279 #endif
1280     if( handle == NULL )
1281     {
1282         char *psz_err = GetWindowsError();
1283         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1284         free( psz_err );
1285         return -1;
1286     }
1287
1288 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1289     /* static is OK, we are called atomically */
1290     static vlc_bool_t b_kde = VLC_FALSE;
1291
1292 #   if defined(SYS_LINUX)
1293     /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1294      * are going to get namespace collisions when two modules have common
1295      * public symbols, but ALSA is being a pest here. */
1296     if( strstr( psz_file, "alsa_plugin" ) )
1297     {
1298         handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1299         if( handle == NULL )
1300         {
1301             msg_Warn( p_this, "cannot load module `%s' (%s)",
1302                               psz_file, dlerror() );
1303             return -1;
1304         }
1305     }
1306 #   endif
1307     /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
1308      * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
1309      * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
1310     /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
1311     if( !b_kde && !strstr( psz_file, "kde" ) )
1312     {
1313         dlopen( "libstdc++.so.6", RTLD_NOW )
1314          || dlopen( "libstdc++.so.5", RTLD_NOW )
1315          || dlopen( "libstdc++.so.4", RTLD_NOW )
1316          || dlopen( "libstdc++.so.3", RTLD_NOW );
1317         b_kde = VLC_TRUE;
1318     }
1319
1320     handle = dlopen( psz_file, RTLD_NOW );
1321     if( handle == NULL )
1322     {
1323         msg_Warn( p_this, "cannot load module `%s' (%s)",
1324                           psz_file, dlerror() );
1325         return -1;
1326     }
1327
1328 #elif defined(HAVE_DL_DLOPEN)
1329 #   if defined(DL_LAZY)
1330     handle = dlopen( psz_file, DL_LAZY );
1331 #   else
1332     handle = dlopen( psz_file, 0 );
1333 #   endif
1334     if( handle == NULL )
1335     {
1336         msg_Warn( p_this, "cannot load module `%s' (%s)",
1337                           psz_file, dlerror() );
1338         return -1;
1339     }
1340
1341 #elif defined(HAVE_DL_SHL_LOAD)
1342     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1343     if( handle == NULL )
1344     {
1345         msg_Warn( p_this, "cannot load module `%s' (%s)",
1346                           psz_file, strerror(errno) );
1347         return -1;
1348     }
1349
1350 #else
1351 #   error "Something is wrong in modules.c"
1352
1353 #endif
1354
1355     *p_handle = handle;
1356     return 0;
1357 }
1358
1359 /*****************************************************************************
1360  * CloseModule: unload a dynamic library
1361  *****************************************************************************
1362  * This function unloads a previously opened dynamically linked library
1363  * using a system dependant method. No return value is taken in consideration,
1364  * since some libraries sometimes refuse to close properly.
1365  *****************************************************************************/
1366 static void CloseModule( module_handle_t handle )
1367 {
1368 #if defined(HAVE_DL_DYLD)
1369     NSUnLinkModule( handle, FALSE );
1370
1371 #elif defined(HAVE_DL_BEOS)
1372     unload_add_on( handle );
1373
1374 #elif defined(HAVE_DL_WINDOWS)
1375     FreeLibrary( handle );
1376
1377 #elif defined(HAVE_DL_DLOPEN)
1378     dlclose( handle );
1379
1380 #elif defined(HAVE_DL_SHL_LOAD)
1381     shl_unload( handle );
1382
1383 #endif
1384     return;
1385 }
1386
1387 /*****************************************************************************
1388  * GetSymbol: get a symbol from a dynamic library
1389  *****************************************************************************
1390  * This function queries a loaded library for a symbol specified in a
1391  * string, and returns a pointer to it. We don't check for dlerror() or
1392  * similar functions, since we want a non-NULL symbol anyway.
1393  *****************************************************************************/
1394 static void * _module_getsymbol( module_handle_t, const char * );
1395
1396 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1397 {
1398     void * p_symbol = _module_getsymbol( handle, psz_function );
1399
1400     /* MacOS X dl library expects symbols to begin with "_". So do
1401      * some other operating systems. That's really lame, but hey, what
1402      * can we do ? */
1403     if( p_symbol == NULL )
1404     {
1405         char *psz_call = malloc( strlen( psz_function ) + 2 );
1406
1407         strcpy( psz_call + 1, psz_function );
1408         psz_call[ 0 ] = '_';
1409         p_symbol = _module_getsymbol( handle, psz_call );
1410         free( psz_call );
1411     }
1412
1413     return p_symbol;
1414 }
1415
1416 static void * _module_getsymbol( module_handle_t handle,
1417                                  const char * psz_function )
1418 {
1419 #if defined(HAVE_DL_DYLD)
1420     NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1421     return NSAddressOfSymbol( sym );
1422
1423 #elif defined(HAVE_DL_BEOS)
1424     void * p_symbol;
1425     if( B_OK == get_image_symbol( handle, psz_function,
1426                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1427     {
1428         return p_symbol;
1429     }
1430     else
1431     {
1432         return NULL;
1433     }
1434
1435 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1436     wchar_t psz_real[256];
1437     MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1438
1439     return (void *)GetProcAddress( handle, psz_real );
1440
1441 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1442     return (void *)GetProcAddress( handle, (char *)psz_function );
1443
1444 #elif defined(HAVE_DL_DLOPEN)
1445     return dlsym( handle, psz_function );
1446
1447 #elif defined(HAVE_DL_SHL_LOAD)
1448     void *p_sym;
1449     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1450     return p_sym;
1451
1452 #endif
1453 }
1454
1455 #if defined(HAVE_DL_WINDOWS)
1456 static char * GetWindowsError( void )
1457 {
1458 #if defined(UNDER_CE)
1459     wchar_t psz_tmp[256];
1460     char * psz_buffer = malloc( 256 );
1461 #else
1462     char * psz_tmp = malloc( 256 );
1463 #endif
1464     int i = 0, i_error = GetLastError();
1465
1466     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1467                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1468                    (LPTSTR) psz_tmp, 256, NULL );
1469
1470     /* Go to the end of the string */
1471 #if defined(UNDER_CE)
1472     while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
1473 #else
1474     while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
1475 #endif
1476     {
1477         i++;
1478     }
1479
1480     if( psz_tmp[i] )
1481     {
1482 #if defined(UNDER_CE)
1483         swprintf( psz_tmp + i, L" (error %i)", i_error );
1484         psz_tmp[ 255 ] = L'\0';
1485 #else
1486         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1487         psz_tmp[ 255 ] = '\0';
1488 #endif
1489     }
1490
1491 #if defined(UNDER_CE)
1492     WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
1493                          psz_buffer, 256, NULL, NULL );
1494     return psz_buffer;
1495 #else
1496     return psz_tmp;
1497 #endif
1498 }
1499 #endif /* HAVE_DL_WINDOWS */
1500
1501 /*****************************************************************************
1502  * LoadPluginsCache: loads the plugins cache file
1503  *****************************************************************************
1504  * This function will load the plugin cache if present and valid. This cache
1505  * will in turn be queried by AllocateAllPlugins() to see if it needs to
1506  * actually load the dynamically loadable module.
1507  * This allows us to only fully load plugins when they are actually used.
1508  *****************************************************************************/
1509 static void CacheLoad( vlc_object_t *p_this )
1510 {
1511     char *psz_filename, *psz_homedir;
1512     FILE *file;
1513     int i, j, i_size, i_read;
1514     char p_cachestring[sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE)];
1515     int i_cache;
1516     module_cache_t **pp_cache = 0;
1517     int32_t i_file_size;
1518
1519     psz_homedir = p_this->p_vlc->psz_homedir;
1520     if( !psz_homedir )
1521     {
1522         msg_Err( p_this, "psz_homedir is null" );
1523         return;
1524     }
1525     psz_filename =
1526         (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1527                         strlen(psz_homedir) );
1528
1529     if( psz_filename )
1530         sprintf( psz_filename, "%s/" CONFIG_DIR "/" PLUGINSCACHE_FILE,
1531                  psz_homedir );
1532
1533     if( !psz_filename )
1534     {
1535         msg_Err( p_this, "out of memory" );
1536         return;
1537     }
1538
1539     if( p_this->p_libvlc->p_module_bank->b_cache_delete )
1540     {
1541         msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1542 #if !defined( UNDER_CE )
1543         unlink( psz_filename );
1544 #else
1545         msg_Err( p_this, "FIXME, unlink not implemented" );
1546 #endif
1547         return;
1548     }
1549
1550     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1551
1552     file = fopen( psz_filename, "rb" );
1553     if( !file )
1554     {
1555         msg_Warn( p_this, "could not open plugins cache file %s for reading",
1556                   psz_filename );
1557         free( psz_filename );
1558         return;
1559     }
1560     free( psz_filename );
1561
1562     /* Check the file size */
1563     i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1564     if( i_read != sizeof(i_file_size) )
1565     {
1566         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1567                   "(too short)" );
1568         fclose( file );
1569         return;
1570     }
1571
1572     fseek( file, 0, SEEK_END );
1573     if( ftell( file ) != i_file_size )
1574     {
1575         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1576                   "(corrupted size)" );
1577         fclose( file );
1578         return;
1579     }
1580     fseek( file, sizeof(i_file_size), SEEK_SET );
1581
1582     /* Check the file is a plugins cache */
1583     i_size = sizeof(PLUGINSCACHE_FILE COPYRIGHT_MESSAGE) - 1;
1584     i_read = fread( p_cachestring, sizeof(char), i_size, file );
1585     if( i_read != i_size ||
1586         memcmp( p_cachestring, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE, i_size ) )
1587     {
1588         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1589         fclose( file );
1590         return;
1591     }
1592
1593     p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1594     fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1595     pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache =
1596         malloc( i_cache * sizeof(void *) );
1597
1598 #define LOAD_IMMEDIATE(a) \
1599     if( fread( &a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1600 #define LOAD_STRING(a) \
1601     { if( fread( &i_size, sizeof(char), sizeof(i_size), file ) \
1602           != sizeof(i_size) ) goto error; \
1603       if( i_size ) { \
1604           a = malloc( i_size ); \
1605           if( fread( a, sizeof(char), i_size, file ) != (size_t)i_size ) \
1606               goto error; \
1607       } else a = 0; \
1608     } while(0)
1609
1610
1611     for( i = 0; i < i_cache; i++ )
1612     {
1613         int16_t i_size;
1614         int i_submodules;
1615
1616         pp_cache[i] = malloc( sizeof(module_cache_t) );
1617         p_this->p_libvlc->p_module_bank->i_loaded_cache++;
1618
1619         /* Save common info */
1620         LOAD_STRING( pp_cache[i]->psz_file );
1621         LOAD_IMMEDIATE( pp_cache[i]->i_time );
1622         LOAD_IMMEDIATE( pp_cache[i]->i_size );
1623         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1624
1625         if( pp_cache[i]->b_junk ) continue;
1626
1627         pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1628
1629         /* Save additional infos */
1630         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1631         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1632         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1633         LOAD_STRING( pp_cache[i]->p_module->psz_program );
1634         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1635         {
1636             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1637         }
1638         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1639         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1640         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1641         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1642         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1643         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1644
1645         /* Config stuff */
1646         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1647             goto error;
1648
1649         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1650
1651         LOAD_IMMEDIATE( i_submodules );
1652
1653         while( i_submodules-- )
1654         {
1655             module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1656             vlc_object_attach( p_module, pp_cache[i]->p_module );
1657             p_module->b_submodule = VLC_TRUE; 
1658
1659             LOAD_STRING( p_module->psz_object_name );
1660             LOAD_STRING( p_module->psz_shortname );
1661             LOAD_STRING( p_module->psz_longname );
1662             LOAD_STRING( p_module->psz_program );
1663             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1664             {
1665                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1666             }
1667             LOAD_STRING( p_module->psz_capability );
1668             LOAD_IMMEDIATE( p_module->i_score );
1669             LOAD_IMMEDIATE( p_module->i_cpu );
1670             LOAD_IMMEDIATE( p_module->b_unloadable );
1671             LOAD_IMMEDIATE( p_module->b_reentrant );
1672         }
1673     }
1674
1675     fclose( file );
1676     return;
1677
1678  error:
1679
1680     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1681
1682     /* TODO: cleanup */
1683     p_this->p_libvlc->p_module_bank->i_loaded_cache = 0;
1684
1685     fclose( file );
1686     return;
1687 }
1688
1689 int CacheLoadConfig( module_t *p_module, FILE *file )
1690 {
1691     int i, j, i_lines;
1692     int16_t i_size;
1693
1694     /* Calculate the structure length */
1695     LOAD_IMMEDIATE( p_module->i_config_items );
1696     LOAD_IMMEDIATE( p_module->i_bool_items );
1697
1698     LOAD_IMMEDIATE( i_lines );
1699
1700     /* Allocate memory */
1701     p_module->p_config =
1702         (module_config_t *)malloc( sizeof(module_config_t) * (i_lines + 1));
1703     if( p_module->p_config == NULL )
1704     {
1705         msg_Err( p_module, "config error: can't duplicate p_config" );
1706         return VLC_ENOMEM;
1707     }
1708
1709     /* Do the duplication job */
1710     for( i = 0; i < i_lines ; i++ )
1711     {
1712         LOAD_IMMEDIATE( p_module->p_config[i] );
1713
1714         LOAD_STRING( p_module->p_config[i].psz_type );
1715         LOAD_STRING( p_module->p_config[i].psz_name );
1716         LOAD_STRING( p_module->p_config[i].psz_text );
1717         LOAD_STRING( p_module->p_config[i].psz_longtext );
1718         LOAD_STRING( p_module->p_config[i].psz_value_orig );
1719
1720         p_module->p_config[i].psz_value =
1721             p_module->p_config[i].psz_value_orig ?
1722                 strdup( p_module->p_config[i].psz_value_orig ) : 0;
1723         p_module->p_config[i].i_value = p_module->p_config[i].i_value_orig;
1724         p_module->p_config[i].f_value = p_module->p_config[i].f_value_orig;
1725
1726         p_module->p_config[i].p_lock = &p_module->object_lock;
1727
1728         if( p_module->p_config[i].i_list )
1729         {
1730             if( p_module->p_config[i].ppsz_list )
1731             {
1732                 p_module->p_config[i].ppsz_list =
1733                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1734                 if( p_module->p_config[i].ppsz_list )
1735                 {
1736                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1737                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1738                     p_module->p_config[i].ppsz_list[j] = NULL;
1739                 }
1740             }
1741             if( p_module->p_config[i].ppsz_list_text )
1742             {
1743                 p_module->p_config[i].ppsz_list_text =
1744                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1745                 if( p_module->p_config[i].ppsz_list_text )
1746                 {
1747                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
1748                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1749                   p_module->p_config[i].ppsz_list_text[j] = NULL;
1750                 }
1751             }
1752             if( p_module->p_config[i].pi_list )
1753             {
1754                 p_module->p_config[i].pi_list =
1755                     malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1756                 if( p_module->p_config[i].pi_list )
1757                 {
1758                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1759                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1760                 }
1761             }
1762         }
1763
1764         if( p_module->p_config[i].i_action )
1765         {
1766             p_module->p_config[i].ppf_action =
1767                 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1768             p_module->p_config[i].ppsz_action_text =
1769                 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1770
1771             for( j = 0; j < p_module->p_config[i].i_action; j++ )
1772             {
1773                 p_module->p_config[i].ppf_action[j] = 0;
1774                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1775             }
1776         }
1777
1778         LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1779     }
1780
1781     p_module->p_config[i].i_type = CONFIG_HINT_END;
1782
1783     return VLC_SUCCESS;
1784
1785  error:
1786
1787     return VLC_EGENERIC;
1788 }
1789
1790 /*****************************************************************************
1791  * SavePluginsCache: saves the plugins cache to a file
1792  *****************************************************************************/
1793 static void CacheSave( vlc_object_t *p_this )
1794 {
1795     char *psz_filename, *psz_homedir;
1796     FILE *file;
1797     int i, j, i_cache;
1798     module_cache_t **pp_cache;
1799     int32_t i_file_size = 0;
1800
1801     psz_homedir = p_this->p_vlc->psz_homedir;
1802     if( !psz_homedir )
1803     {
1804         msg_Err( p_this, "psz_homedir is null" );
1805         return;
1806     }
1807     psz_filename =
1808        (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_FILE) +
1809                        strlen(psz_homedir) );
1810
1811     if( psz_filename )
1812         sprintf( psz_filename, "%s/" CONFIG_DIR, psz_homedir );
1813
1814     if( !psz_filename )
1815     {
1816         msg_Err( p_this, "out of memory" );
1817         return;
1818     }
1819
1820     config_CreateDir( p_this, psz_filename );
1821
1822     strcat( psz_filename, "/" PLUGINSCACHE_FILE );
1823
1824     msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
1825
1826     file = fopen( psz_filename, "wb" );
1827     if( !file )
1828     {
1829         msg_Warn( p_this, "could not open plugins cache file %s for writing",
1830                   psz_filename );
1831         free( psz_filename );
1832         return;
1833     }
1834     free( psz_filename );
1835
1836     /* Empty space for file size */
1837     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1838
1839     /* Contains version number */
1840     fprintf( file, PLUGINSCACHE_FILE COPYRIGHT_MESSAGE );
1841
1842     i_cache = p_this->p_libvlc->p_module_bank->i_cache;
1843     pp_cache = p_this->p_libvlc->p_module_bank->pp_cache;
1844
1845     fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
1846
1847 #define SAVE_IMMEDIATE(a) \
1848     fwrite( &a, sizeof(char), sizeof(a), file )
1849 #define SAVE_STRING(a) \
1850     { i_size = a ? strlen( a ) + 1 : 0; \
1851       fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
1852       if( a ) fwrite( a, sizeof(char), i_size, file ); \
1853     } while(0)
1854
1855     for( i = 0; i < i_cache; i++ )
1856     {
1857         int16_t i_size;
1858         int32_t i_submodule;
1859
1860         /* Save common info */
1861         SAVE_STRING( pp_cache[i]->psz_file );
1862         SAVE_IMMEDIATE( pp_cache[i]->i_time );
1863         SAVE_IMMEDIATE( pp_cache[i]->i_size );
1864         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
1865
1866         if( pp_cache[i]->b_junk ) continue;
1867
1868         /* Save additional infos */
1869         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
1870         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
1871         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
1872         SAVE_STRING( pp_cache[i]->p_module->psz_program );
1873         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1874         {
1875             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1876         }
1877         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
1878         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
1879         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1880         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1881         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1882         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1883
1884         /* Config stuff */
1885         CacheSaveConfig( pp_cache[i]->p_module, file );
1886
1887         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
1888
1889         i_submodule = pp_cache[i]->p_module->i_children;
1890         SAVE_IMMEDIATE( i_submodule );
1891         for( i_submodule = 0; i_submodule < pp_cache[i]->p_module->i_children;
1892              i_submodule++ )
1893         {
1894             module_t *p_module =
1895                 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
1896
1897             SAVE_STRING( p_module->psz_object_name );
1898             SAVE_STRING( p_module->psz_shortname );
1899             SAVE_STRING( p_module->psz_longname );
1900             SAVE_STRING( p_module->psz_program );
1901             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1902             {
1903                 SAVE_STRING( p_module->pp_shortcuts[j] ); // FIX
1904             }
1905             SAVE_STRING( p_module->psz_capability );
1906             SAVE_IMMEDIATE( p_module->i_score );
1907             SAVE_IMMEDIATE( p_module->i_cpu );
1908             SAVE_IMMEDIATE( p_module->b_unloadable );
1909             SAVE_IMMEDIATE( p_module->b_reentrant );
1910         }
1911     }
1912
1913     /* Fill-up file size */
1914     i_file_size = ftell( file );
1915     fseek( file, 0, SEEK_SET );
1916     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1917
1918     fclose( file );
1919
1920     return;
1921 }
1922
1923 void CacheSaveConfig( module_t *p_module, FILE *file )
1924 {
1925     int i, j, i_lines = 0;
1926     module_config_t *p_item;
1927     int16_t i_size;
1928
1929     SAVE_IMMEDIATE( p_module->i_config_items );
1930     SAVE_IMMEDIATE( p_module->i_bool_items );
1931
1932     for( p_item = p_module->p_config; p_item->i_type != CONFIG_HINT_END;
1933          p_item++ ) i_lines++;
1934
1935     SAVE_IMMEDIATE( i_lines );
1936
1937     for( i = 0; i < i_lines ; i++ )
1938     {
1939         SAVE_IMMEDIATE( p_module->p_config[i] );
1940
1941         SAVE_STRING( p_module->p_config[i].psz_type );
1942         SAVE_STRING( p_module->p_config[i].psz_name );
1943         SAVE_STRING( p_module->p_config[i].psz_text );
1944         SAVE_STRING( p_module->p_config[i].psz_longtext );
1945         SAVE_STRING( p_module->p_config[i].psz_value_orig );
1946
1947         if( p_module->p_config[i].i_list )
1948         {
1949             if( p_module->p_config[i].ppsz_list )
1950             {
1951                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1952                     SAVE_STRING( p_module->p_config[i].ppsz_list[j] );
1953             }
1954
1955             if( p_module->p_config[i].ppsz_list_text )
1956             {
1957                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1958                     SAVE_STRING( p_module->p_config[i].ppsz_list_text[j] );
1959             }
1960             if( p_module->p_config[i].pi_list )
1961             {
1962                 for( j = 0; j < p_module->p_config[i].i_list; j++ )
1963                     SAVE_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1964             }
1965         }
1966
1967         for( j = 0; j < p_module->p_config[i].i_action; j++ )
1968             SAVE_STRING( p_module->p_config[i].ppsz_action_text[j] );
1969
1970         SAVE_IMMEDIATE( p_module->p_config[i].pf_callback );
1971     }
1972 }
1973
1974 /*****************************************************************************
1975  * CacheMerge: Merge a cache module descriptor with a full module descriptor.
1976  *****************************************************************************/
1977 static void CacheMerge( vlc_object_t *p_this, module_t *p_cache,
1978                         module_t *p_module )
1979 {
1980     int i_submodule;
1981
1982     p_cache->pf_activate = p_module->pf_activate;
1983     p_cache->pf_deactivate = p_module->pf_deactivate;
1984     p_cache->p_symbols = p_module->p_symbols;
1985     p_cache->handle = p_module->handle;
1986
1987     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1988     {
1989         module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
1990         module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
1991         p_cchild->pf_activate = p_child->pf_activate;
1992         p_cchild->pf_deactivate = p_child->pf_deactivate;
1993         p_cchild->p_symbols = p_child->p_symbols;
1994     }
1995
1996     p_cache->b_loaded = VLC_TRUE;
1997     p_module->b_loaded = VLC_FALSE;
1998 }
1999
2000 /*****************************************************************************
2001  * FindPluginCache: finds the cache entry corresponding to a file
2002  *****************************************************************************/
2003 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2004                                   int64_t i_time, int64_t i_size )
2005 {
2006     module_cache_t **pp_cache;
2007     int i_cache, i;
2008
2009     pp_cache = p_this->p_libvlc->p_module_bank->pp_loaded_cache;
2010     i_cache = p_this->p_libvlc->p_module_bank->i_loaded_cache;
2011
2012     for( i = 0; i < i_cache; i++ )
2013     {
2014         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2015             pp_cache[i]->i_time == i_time &&
2016             pp_cache[i]->i_size == i_size ) return pp_cache[i];
2017     }
2018
2019     return NULL;
2020 }
2021
2022 #endif /* HAVE_DYNAMIC_PLUGINS */