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