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