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