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