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