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