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