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