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