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