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