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