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