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