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