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