]> git.sesse.net Git - vlc/blob - src/misc/modules.c
Switch to libdca API
[vlc] / src / misc / 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 "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 2
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             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( p_module && !p_this->psz_object_name )
715     {
716         /* This assumes that p_this is the object which will be using the
717          * module. That's not always the case ... but it is in most cases.
718          */
719         if( psz_alias )
720             p_this->psz_object_name = strdup( psz_alias );
721         else
722             p_this->psz_object_name = strdup( p_module->psz_object_name );
723     }
724
725     free( psz_shortcuts );
726     free( psz_var );
727
728     /* Don't forget that the module is still locked */
729     return p_module;
730 }
731
732 /*****************************************************************************
733  * module_Unneed: decrease the usage count of a module.
734  *****************************************************************************
735  * This function must be called by the thread that called module_Need, to
736  * decrease the reference count and allow for hiding of modules.
737  *****************************************************************************/
738 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
739 {
740     /* Use the close method */
741     if( p_module->pf_deactivate )
742     {
743         p_module->pf_deactivate( p_this );
744     }
745
746     msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
747
748     vlc_object_release( p_module );
749
750     return;
751 }
752
753 /*****************************************************************************
754  * module_Exists: tell if a module exists.
755  *****************************************************************************
756  * This function is a boolean function that tells if a module exist or not.
757  *****************************************************************************/
758
759 vlc_bool_t __module_Exists(  vlc_object_t *p_this, const char * psz_name )
760 {
761     vlc_list_t *p_list;
762     int i;
763     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
764     for( i = 0 ; i < p_list->i_count; i++)
765     {
766         const char *psz_module_name =
767             ((module_t *) p_list->p_values[i].p_object)->psz_shortname;
768         if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
769         {
770             /* We can release the list, and return yes */
771             vlc_list_release( p_list ); return VLC_TRUE;
772         }
773     }
774     vlc_list_release( p_list ); return VLC_FALSE;
775 }
776
777
778 /*****************************************************************************
779  * Following functions are local.
780  *****************************************************************************/
781
782 /*****************************************************************************
783  * AllocateAllPlugins: load all plugin modules we can find.
784  *****************************************************************************/
785 #ifdef HAVE_DYNAMIC_PLUGINS
786 static void AllocateAllPlugins( vlc_object_t *p_this )
787 {
788     /* Yes, there are two NULLs because we replace one with "plugin-path". */
789 #if defined( WIN32 ) || defined( UNDER_CE )
790     const char *path[] = { "modules", "", "plugins", NULL, NULL };
791 #else
792     const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
793 #endif
794
795     const char *const *ppsz_path;
796
797     /* If the user provided a plugin path, we add it to the list */
798     char *userpath = config_GetPsz( p_this, "plugin-path" );
799     path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
800
801     for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
802     {
803         char *psz_fullpath;
804
805         if (!**ppsz_path) continue;
806
807 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
808
809         /* Handle relative as well as absolute paths */
810 #ifdef WIN32
811         if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
812             (*ppsz_path)[1] != ':' )
813 #else
814         if( (*ppsz_path)[0] != '/' )
815 #endif
816         {
817             if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
818                           p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
819                 psz_fullpath = NULL;
820         }
821         else
822 #endif
823             psz_fullpath = strdup( *ppsz_path );
824
825         if( psz_fullpath == NULL )
826             continue;
827
828         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
829
830         /* Don't go deeper than 5 subdirectories */
831         AllocatePluginDir( p_this, psz_fullpath, 5 );
832
833         free( psz_fullpath );
834     }
835
836     /* Free plugin-path */
837     free( userpath );
838 }
839
840 /*****************************************************************************
841  * AllocatePluginDir: recursively parse a directory to look for plugins
842  *****************************************************************************/
843 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
844                                int i_maxdepth )
845 {
846 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
847 #ifdef WIN32
848 # undef opendir
849 # undef closedir
850 # undef readdir
851 #endif
852 #if defined( UNDER_CE ) || defined( _MSC_VER )
853 #ifdef UNDER_CE
854     wchar_t psz_wpath[MAX_PATH + 256];
855     wchar_t psz_wdir[MAX_PATH];
856 #endif
857     char psz_path[MAX_PATH + 256];
858     WIN32_FIND_DATA finddata;
859     HANDLE handle;
860     int rc;
861 #else
862     int    i_dirlen;
863     DIR *  dir;
864     struct dirent * file;
865 #endif
866     char * psz_file;
867
868     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
869     {
870         return;
871     }
872
873 #if defined( UNDER_CE ) || defined( _MSC_VER )
874 #ifdef UNDER_CE
875     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
876
877     rc = GetFileAttributes( psz_wdir );
878     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
879
880     /* Parse all files in the directory */
881     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
882 #else
883     rc = GetFileAttributes( psz_dir );
884     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
885 #endif
886
887     /* Parse all files in the directory */
888     sprintf( psz_path, "%s\\*", psz_dir );
889
890 #ifdef UNDER_CE
891     handle = FindFirstFile( psz_wpath, &finddata );
892 #else
893     handle = FindFirstFile( psz_path, &finddata );
894 #endif
895     if( handle == INVALID_HANDLE_VALUE )
896     {
897         /* Empty directory */
898         return;
899     }
900
901     /* Parse the directory and try to load all files it contains. */
902     do
903     {
904 #ifdef UNDER_CE
905         unsigned int i_len = wcslen( finddata.cFileName );
906         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
907         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
908 #else
909         unsigned int i_len = strlen( finddata.cFileName );
910         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
911 #endif
912
913         /* Skip ".", ".." */
914         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
915          || !strcmp( finddata.cFileName, ".." ) )
916         {
917             if( !FindNextFile( handle, &finddata ) ) break;
918             continue;
919         }
920
921 #ifdef UNDER_CE
922         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
923 #else
924         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
925 #endif
926         {
927             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
928         }
929         else if( i_len > strlen( LIBEXT )
930                   /* We only load files ending with LIBEXT */
931                   && !strncasecmp( psz_path + strlen( psz_path)
932                                    - strlen( LIBEXT ),
933                                    LIBEXT, strlen( LIBEXT ) ) )
934         {
935             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
936             int64_t i_time = 0, i_size = 0;
937
938 #ifdef UNDER_CE
939             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
940                                      &attrbuf ) )
941 #else
942             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
943                                      &attrbuf ) )
944 #endif
945             {
946                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
947                 i_time <<= 32;
948                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
949                 i_size = attrbuf.nFileSizeHigh;
950                 i_size <<= 32;
951                 i_size |= attrbuf.nFileSizeLow;
952             }
953             psz_file = psz_path;
954
955             AllocatePluginFile( p_this, psz_file, i_time, i_size );
956         }
957     }
958     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
959
960     /* Close the directory */
961     FindClose( handle );
962
963 #else
964     dir = opendir( psz_dir );
965     if( !dir )
966     {
967         return;
968     }
969
970     i_dirlen = strlen( psz_dir );
971
972     /* Parse the directory and try to load all files it contains. */
973     while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
974     {
975         struct stat statbuf;
976         unsigned int i_len;
977         int i_stat;
978
979         /* Skip ".", ".." */
980         if( !*file->d_name || !strcmp( file->d_name, "." )
981          || !strcmp( file->d_name, ".." ) )
982         {
983             continue;
984         }
985
986         i_len = strlen( file->d_name );
987         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
988 #ifdef WIN32
989         sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
990 #else
991         sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
992 #endif
993
994         i_stat = stat( psz_file, &statbuf );
995         if( !i_stat && statbuf.st_mode & S_IFDIR )
996         {
997             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
998         }
999         else if( i_len > strlen( LIBEXT )
1000                   /* We only load files ending with LIBEXT */
1001                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1002                                    LIBEXT, strlen( LIBEXT ) ) )
1003         {
1004             int64_t i_time = 0, i_size = 0;
1005
1006             if( !i_stat )
1007             {
1008                 i_time = statbuf.st_mtime;
1009                 i_size = statbuf.st_size;
1010             }
1011
1012             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1013         }
1014
1015         free( psz_file );
1016     }
1017
1018     /* Close the directory */
1019     closedir( dir );
1020
1021 #endif
1022 }
1023
1024 /*****************************************************************************
1025  * AllocatePluginFile: load a module into memory and initialize it.
1026  *****************************************************************************
1027  * This function loads a dynamically loadable module and allocates a structure
1028  * for its information data. The module can then be handled by module_Need
1029  * and module_Unneed. It can be removed by DeleteModule.
1030  *****************************************************************************/
1031 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1032                                int64_t i_file_time, int64_t i_file_size )
1033 {
1034     module_t * p_module = NULL;
1035     module_cache_t *p_cache_entry = NULL;
1036
1037     /*
1038      * Check our plugins cache first then load plugin if needed
1039      */
1040     p_cache_entry =
1041         CacheFind( p_this, psz_file, i_file_time, i_file_size );
1042
1043     if( !p_cache_entry )
1044     {
1045         p_module = AllocatePlugin( p_this, psz_file );
1046     }
1047     else
1048     {
1049         /* If junk dll, don't try to load it */
1050         if( p_cache_entry->b_junk )
1051         {
1052             p_module = NULL;
1053         }
1054         else
1055         {
1056             module_config_t *p_item = NULL, *p_end = NULL;
1057
1058             p_module = p_cache_entry->p_module;
1059             p_module->b_loaded = VLC_FALSE;
1060
1061             /* For now we force loading if the module's config contains
1062              * callbacks or actions.
1063              * Could be optimized by adding an API call.*/
1064             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1065                  p_item < p_end; p_item++ )
1066             {
1067                 if( p_item->pf_callback || p_item->i_action )
1068                 {
1069                     p_module = AllocatePlugin( p_this, psz_file );
1070                     break;
1071                 }
1072             }
1073             if( p_module == p_cache_entry->p_module )
1074                 p_cache_entry->b_used = VLC_TRUE;
1075         }
1076     }
1077
1078     if( p_module )
1079     {
1080         /* Everything worked fine !
1081          * The module is ready to be added to the list. */
1082         p_module->b_builtin = VLC_FALSE;
1083
1084         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1085                     p_module->psz_object_name, p_module->psz_longname ); */
1086
1087         vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1088
1089         if( !p_this->p_libvlc_global->p_module_bank->b_cache )
1090             return 0;
1091
1092         /* Add entry to cache */
1093 #define p_bank p_this->p_libvlc_global->p_module_bank
1094         p_bank->pp_cache =
1095             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1096         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1097         if( !p_bank->pp_cache[p_bank->i_cache] )
1098             return -1;
1099         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1100         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1101         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1102         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1103         p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1104         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1105         p_bank->i_cache++;
1106     }
1107
1108     return p_module ? 0 : -1;
1109 }
1110
1111 /*****************************************************************************
1112  * AllocatePlugin: load a module into memory and initialize it.
1113  *****************************************************************************
1114  * This function loads a dynamically loadable module and allocates a structure
1115  * for its information data. The module can then be handled by module_Need
1116  * and module_Unneed. It can be removed by DeleteModule.
1117  *****************************************************************************/
1118 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1119 {
1120     module_t * p_module = NULL;
1121     module_handle_t handle;
1122
1123     if( LoadModule( p_this, psz_file, &handle ) )
1124         return NULL;
1125
1126     /* Now that we have successfully loaded the module, we can
1127      * allocate a structure for it */
1128     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1129     if( p_module == NULL )
1130     {
1131         msg_Err( p_this, "out of memory" );
1132         CloseModule( handle );
1133         return NULL;
1134     }
1135
1136     /* We need to fill these since they may be needed by CallEntry() */
1137     p_module->psz_filename = psz_file;
1138     p_module->handle = handle;
1139 #ifndef HAVE_SHARED_LIBVLC
1140     p_module->p_symbols = &p_this->p_libvlc_global->p_module_bank->symbols;
1141 #endif
1142     p_module->b_loaded = VLC_TRUE;
1143
1144     /* Initialize the module: fill p_module, default config */
1145     if( CallEntry( p_module ) != 0 )
1146     {
1147         /* We couldn't call module_init() */
1148         vlc_object_destroy( p_module );
1149         CloseModule( handle );
1150         return NULL;
1151     }
1152
1153     DupModule( p_module );
1154     p_module->psz_filename = strdup( p_module->psz_filename );
1155
1156     /* Everything worked fine ! The module is ready to be added to the list. */
1157     p_module->b_builtin = VLC_FALSE;
1158
1159     return p_module;
1160 }
1161
1162 /*****************************************************************************
1163  * DupModule: make a plugin module standalone.
1164  *****************************************************************************
1165  * This function duplicates all strings in the module, so that the dynamic
1166  * object can be unloaded. It acts recursively on submodules.
1167  *****************************************************************************/
1168 static void DupModule( module_t *p_module )
1169 {
1170     const char **pp_shortcut;
1171     int i_submodule;
1172
1173     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1174     {
1175         *pp_shortcut = strdup( *pp_shortcut );
1176     }
1177
1178     /* We strdup() these entries so that they are still valid when the
1179      * module is unloaded. */
1180     p_module->psz_object_name = strdup( p_module->psz_object_name );
1181     p_module->psz_capability = strdup( p_module->psz_capability );
1182     p_module->psz_shortname = p_module->psz_shortname ?
1183                                  strdup( p_module->psz_shortname ) : NULL;
1184     p_module->psz_longname = strdup( p_module->psz_longname );
1185     p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1186                                             : NULL;
1187
1188     if( p_module->psz_program != NULL )
1189     {
1190         p_module->psz_program = strdup( p_module->psz_program );
1191     }
1192
1193     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1194     {
1195         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1196     }
1197 }
1198
1199 /*****************************************************************************
1200  * UndupModule: free a duplicated module.
1201  *****************************************************************************
1202  * This function frees the allocations done in DupModule().
1203  *****************************************************************************/
1204 static void UndupModule( module_t *p_module )
1205 {
1206     const char **pp_shortcut;
1207     int i_submodule;
1208
1209     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1210     {
1211         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1212     }
1213
1214     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1215     {
1216         free( (void*)*pp_shortcut );
1217     }
1218
1219     free( (void*)p_module->psz_object_name );
1220     free( (void*)p_module->psz_capability );
1221     free( (void*)p_module->psz_shortname );
1222     free( (void*)p_module->psz_longname );
1223     free( (void*)p_module->psz_help );
1224     free( (void*)p_module->psz_program );
1225 }
1226
1227 #endif /* HAVE_DYNAMIC_PLUGINS */
1228
1229 /*****************************************************************************
1230  * AllocateBuiltinModule: initialize a builtin module.
1231  *****************************************************************************
1232  * This function registers a builtin module and allocates a structure
1233  * for its information data. The module can then be handled by module_Need
1234  * and module_Unneed. It can be removed by DeleteModule.
1235  *****************************************************************************/
1236 static int AllocateBuiltinModule( vlc_object_t * p_this,
1237                                   int ( *pf_entry ) ( module_t * ) )
1238 {
1239     module_t * p_module;
1240
1241     /* Now that we have successfully loaded the module, we can
1242      * allocate a structure for it */
1243     p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1244     if( p_module == NULL )
1245     {
1246         msg_Err( p_this, "out of memory" );
1247         return -1;
1248     }
1249
1250     /* Initialize the module : fill p_module->psz_object_name, etc. */
1251     if( pf_entry( p_module ) != 0 )
1252     {
1253         /* With a well-written module we shouldn't have to print an
1254          * additional error message here, but just make sure. */
1255         msg_Err( p_this, "failed calling entry point in builtin module" );
1256         vlc_object_destroy( p_module );
1257         return -1;
1258     }
1259
1260     /* Everything worked fine ! The module is ready to be added to the list. */
1261     p_module->b_builtin = VLC_TRUE;
1262
1263     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1264                 p_module->psz_object_name, p_module->psz_longname ); */
1265
1266     vlc_object_attach( p_module, p_this->p_libvlc_global->p_module_bank );
1267
1268     return 0;
1269 }
1270
1271 /*****************************************************************************
1272  * DeleteModule: delete a module and its structure.
1273  *****************************************************************************
1274  * This function can only be called if the module isn't being used.
1275  *****************************************************************************/
1276 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1277 {
1278     if( !p_module ) return VLC_EGENERIC;
1279     if( b_detach )
1280         vlc_object_detach( p_module );
1281
1282     /* We free the structures that we strdup()ed in Allocate*Module(). */
1283 #ifdef HAVE_DYNAMIC_PLUGINS
1284     if( !p_module->b_builtin )
1285     {
1286         if( p_module->b_loaded && p_module->b_unloadable )
1287         {
1288             CloseModule( p_module->handle );
1289         }
1290         UndupModule( p_module );
1291         free( p_module->psz_filename );
1292     }
1293 #endif
1294
1295     /* Free and detach the object's children */
1296     while( p_module->i_children )
1297     {
1298         vlc_object_t *p_this = p_module->pp_children[0];
1299         vlc_object_detach( p_this );
1300         vlc_object_destroy( p_this );
1301     }
1302
1303     config_Free( p_module );
1304     vlc_object_destroy( p_module );
1305     p_module = NULL;
1306     return 0;
1307 }
1308
1309 #ifdef HAVE_DYNAMIC_PLUGINS
1310 /*****************************************************************************
1311  * CallEntry: call an entry point.
1312  *****************************************************************************
1313  * This function calls a symbol given its name and a module structure. The
1314  * symbol MUST refer to a function returning int and taking a module_t* as
1315  * an argument.
1316  *****************************************************************************/
1317 static int CallEntry( module_t * p_module )
1318 {
1319     static const char * const psz_name = "vlc_entry" MODULE_SUFFIX;
1320     int (* pf_symbol) ( module_t * p_module );
1321
1322     /* Try to resolve the symbol */
1323     pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
1324
1325     if( pf_symbol == NULL )
1326     {
1327 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1328         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
1329                             psz_name, p_module->psz_filename );
1330 #elif defined(HAVE_DL_WINDOWS)
1331         char *psz_error = GetWindowsError();
1332         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1333                             psz_name, p_module->psz_filename, psz_error );
1334         free( psz_error );
1335 #elif defined(HAVE_DL_DLOPEN)
1336         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1337                             psz_name, p_module->psz_filename, dlerror() );
1338 #elif defined(HAVE_DL_SHL_LOAD)
1339         msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1340                             psz_name, p_module->psz_filename, strerror(errno) );
1341 #else
1342 #   error "Something is wrong in modules.c"
1343 #endif
1344         return -1;
1345     }
1346
1347     /* We can now try to call the symbol */
1348     if( pf_symbol( p_module ) != 0 )
1349     {
1350         /* With a well-written module we shouldn't have to print an
1351          * additional error message here, but just make sure. */
1352         msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'",
1353                            psz_name, p_module->psz_filename );
1354         return -1;
1355     }
1356
1357     /* Everything worked fine, we can return */
1358     return 0;
1359 }
1360
1361 /*****************************************************************************
1362  * LoadModule: loads a dynamic library
1363  *****************************************************************************
1364  * This function loads a dynamically linked library using a system dependant
1365  * method. Will return 0 on success as well as the module handle.
1366  *****************************************************************************/
1367 static int LoadModule( vlc_object_t *p_this, char *psz_file,
1368                        module_handle_t *p_handle )
1369 {
1370     module_handle_t handle;
1371
1372 #if defined(HAVE_DL_DYLD)
1373     NSObjectFileImage image;
1374     NSObjectFileImageReturnCode ret;
1375
1376     ret = NSCreateObjectFileImageFromFile( psz_file, &image );
1377
1378     if( ret != NSObjectFileImageSuccess )
1379     {
1380         msg_Warn( p_this, "cannot create image from `%s'", psz_file );
1381         return -1;
1382     }
1383
1384     /* Open the dynamic module */
1385     handle = NSLinkModule( image, psz_file,
1386                            NSLINKMODULE_OPTION_RETURN_ON_ERROR );
1387
1388     if( !handle )
1389     {
1390         NSLinkEditErrors errors;
1391         const char *psz_file, *psz_err;
1392         int i_errnum;
1393         NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
1394         msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
1395         NSDestroyObjectFileImage( image );
1396         return -1;
1397     }
1398
1399     /* Destroy our image, we won't need it */
1400     NSDestroyObjectFileImage( image );
1401
1402 #elif defined(HAVE_DL_BEOS)
1403     handle = load_add_on( psz_file );
1404     if( handle < 0 )
1405     {
1406         msg_Warn( p_this, "cannot load module `%s'", psz_file );
1407         return -1;
1408     }
1409
1410 #elif defined(HAVE_DL_WINDOWS)
1411 #ifdef UNDER_CE
1412     {
1413         wchar_t psz_wfile[MAX_PATH];
1414         MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
1415         handle = LoadLibrary( psz_wfile );
1416     }
1417 #else
1418     handle = LoadLibrary( psz_file );
1419 #endif
1420     if( handle == NULL )
1421     {
1422         char *psz_err = GetWindowsError();
1423         msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
1424         free( psz_err );
1425         return -1;
1426     }
1427
1428 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
1429     /* static is OK, we are called atomically */
1430
1431 #   if defined(SYS_LINUX)
1432     /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
1433      * are going to get namespace collisions when two modules have common
1434      * public symbols, but ALSA is being a pest here. */
1435     if( strstr( psz_file, "alsa_plugin" ) )
1436     {
1437         handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
1438         if( handle == NULL )
1439         {
1440             msg_Warn( p_this, "cannot load module `%s' (%s)",
1441                               psz_file, dlerror() );
1442             return -1;
1443         }
1444     }
1445 #   endif
1446
1447     handle = dlopen( psz_file, RTLD_NOW );
1448     if( handle == NULL )
1449     {
1450         msg_Warn( p_this, "cannot load module `%s' (%s)",
1451                           psz_file, dlerror() );
1452         return -1;
1453     }
1454
1455 #elif defined(HAVE_DL_DLOPEN)
1456 #   if defined(DL_LAZY)
1457     handle = dlopen( psz_file, DL_LAZY );
1458 #   else
1459     handle = dlopen( psz_file, 0 );
1460 #   endif
1461     if( handle == NULL )
1462     {
1463         msg_Warn( p_this, "cannot load module `%s' (%s)",
1464                           psz_file, dlerror() );
1465         return -1;
1466     }
1467
1468 #elif defined(HAVE_DL_SHL_LOAD)
1469     handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
1470     if( handle == NULL )
1471     {
1472         msg_Warn( p_this, "cannot load module `%s' (%s)",
1473                           psz_file, strerror(errno) );
1474         return -1;
1475     }
1476
1477 #else
1478 #   error "Something is wrong in modules.c"
1479
1480 #endif
1481
1482     *p_handle = handle;
1483     return 0;
1484 }
1485
1486 /*****************************************************************************
1487  * CloseModule: unload a dynamic library
1488  *****************************************************************************
1489  * This function unloads a previously opened dynamically linked library
1490  * using a system dependant method. No return value is taken in consideration,
1491  * since some libraries sometimes refuse to close properly.
1492  *****************************************************************************/
1493 static void CloseModule( module_handle_t handle )
1494 {
1495 #if defined(HAVE_DL_DYLD)
1496     NSUnLinkModule( handle, FALSE );
1497
1498 #elif defined(HAVE_DL_BEOS)
1499     unload_add_on( handle );
1500
1501 #elif defined(HAVE_DL_WINDOWS)
1502     FreeLibrary( handle );
1503
1504 #elif defined(HAVE_DL_DLOPEN)
1505     dlclose( handle );
1506
1507 #elif defined(HAVE_DL_SHL_LOAD)
1508     shl_unload( handle );
1509
1510 #endif
1511     return;
1512 }
1513
1514 /*****************************************************************************
1515  * GetSymbol: get a symbol from a dynamic library
1516  *****************************************************************************
1517  * This function queries a loaded library for a symbol specified in a
1518  * string, and returns a pointer to it. We don't check for dlerror() or
1519  * similar functions, since we want a non-NULL symbol anyway.
1520  *****************************************************************************/
1521 static void * _module_getsymbol( module_handle_t, const char * );
1522
1523 static void * GetSymbol( module_handle_t handle, const char * psz_function )
1524 {
1525     void * p_symbol = _module_getsymbol( handle, psz_function );
1526
1527     /* MacOS X dl library expects symbols to begin with "_". So do
1528      * some other operating systems. That's really lame, but hey, what
1529      * can we do ? */
1530     if( p_symbol == NULL )
1531     {
1532         char *psz_call = malloc( strlen( psz_function ) + 2 );
1533
1534         strcpy( psz_call + 1, psz_function );
1535         psz_call[ 0 ] = '_';
1536         p_symbol = _module_getsymbol( handle, psz_call );
1537         free( psz_call );
1538     }
1539
1540     return p_symbol;
1541 }
1542
1543 static void * _module_getsymbol( module_handle_t handle,
1544                                  const char * psz_function )
1545 {
1546 #if defined(HAVE_DL_DYLD)
1547     NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
1548     return NSAddressOfSymbol( sym );
1549
1550 #elif defined(HAVE_DL_BEOS)
1551     void * p_symbol;
1552     if( B_OK == get_image_symbol( handle, psz_function,
1553                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
1554     {
1555         return p_symbol;
1556     }
1557     else
1558     {
1559         return NULL;
1560     }
1561
1562 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1563     wchar_t psz_real[256];
1564     MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
1565
1566     return (void *)GetProcAddress( handle, psz_real );
1567
1568 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1569     return (void *)GetProcAddress( handle, (char *)psz_function );
1570
1571 #elif defined(HAVE_DL_DLOPEN)
1572     return dlsym( handle, psz_function );
1573
1574 #elif defined(HAVE_DL_SHL_LOAD)
1575     void *p_sym;
1576     shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
1577     return p_sym;
1578
1579 #endif
1580 }
1581
1582 #if defined(HAVE_DL_WINDOWS)
1583 static char * GetWindowsError( void )
1584 {
1585 #if defined(UNDER_CE)
1586     wchar_t psz_tmp[MAX_PATH];
1587     char * psz_buffer = malloc( MAX_PATH );
1588 #else
1589     char * psz_tmp = malloc( MAX_PATH );
1590 #endif
1591     int i = 0, i_error = GetLastError();
1592
1593     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1594                    NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1595                    (LPTSTR)psz_tmp, MAX_PATH, NULL );
1596
1597     /* Go to the end of the string */
1598     while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
1599     {
1600         i++;
1601     }
1602
1603     if( psz_tmp[i] )
1604     {
1605 #if defined(UNDER_CE)
1606         swprintf( psz_tmp + i, L" (error %i)", i_error );
1607         psz_tmp[ 255 ] = L'\0';
1608 #else
1609         snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
1610         psz_tmp[ 255 ] = '\0';
1611 #endif
1612     }
1613
1614 #if defined(UNDER_CE)
1615     wcstombs( psz_buffer, psz_tmp, MAX_PATH );
1616     return psz_buffer;
1617 #else
1618     return psz_tmp;
1619 #endif
1620 }
1621 #endif /* HAVE_DL_WINDOWS */
1622
1623 /*****************************************************************************
1624  * LoadPluginsCache: loads the plugins cache file
1625  *****************************************************************************
1626  * This function will load the plugin cache if present and valid. This cache
1627  * will in turn be queried by AllocateAllPlugins() to see if it needs to
1628  * actually load the dynamically loadable module.
1629  * This allows us to only fully load plugins when they are actually used.
1630  *****************************************************************************/
1631 static void CacheLoad( vlc_object_t *p_this )
1632 {
1633     char *psz_filename, *psz_homedir;
1634     FILE *file;
1635     int i, j, i_size, i_read;
1636     char p_cachestring[sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE)];
1637     char p_cachelang[6], p_lang[6];
1638     int i_cache;
1639     module_cache_t **pp_cache = 0;
1640     int32_t i_file_size, i_marker;
1641
1642     psz_homedir = p_this->p_libvlc->psz_homedir;
1643     if( !psz_homedir )
1644     {
1645         msg_Err( p_this, "psz_homedir is null" );
1646         return;
1647     }
1648
1649     i_size = asprintf( &psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
1650                        PLUGINSCACHE_DIR, CacheName() );
1651     if( i_size <= 0 )
1652     {
1653         msg_Err( p_this, "out of memory" );
1654         return;
1655     }
1656
1657     if( p_this->p_libvlc_global->p_module_bank->b_cache_delete )
1658     {
1659 #if !defined( UNDER_CE )
1660         unlink( psz_filename );
1661 #else
1662         wchar_t psz_wf[MAX_PATH];
1663         MultiByteToWideChar( CP_ACP, 0, psz_filename, -1, psz_wf, MAX_PATH );
1664         DeleteFile( psz_wf );
1665 #endif
1666         msg_Dbg( p_this, "removing plugins cache file %s", psz_filename );
1667         free( psz_filename );
1668         return;
1669     }
1670
1671     msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
1672
1673     file = utf8_fopen( psz_filename, "rb" );
1674     if( !file )
1675     {
1676         msg_Warn( p_this, "could not open plugins cache file %s for reading",
1677                   psz_filename );
1678         free( psz_filename );
1679         return;
1680     }
1681     free( psz_filename );
1682
1683     /* Check the file size */
1684     i_read = fread( &i_file_size, sizeof(char), sizeof(i_file_size), file );
1685     if( i_read != sizeof(i_file_size) )
1686     {
1687         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1688                   "(too short)" );
1689         fclose( file );
1690         return;
1691     }
1692
1693     fseek( file, 0, SEEK_END );
1694     if( ftell( file ) != i_file_size )
1695     {
1696         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1697                   "(corrupted size)" );
1698         fclose( file );
1699         return;
1700     }
1701     fseek( file, sizeof(i_file_size), SEEK_SET );
1702
1703     /* Check the file is a plugins cache */
1704     i_size = sizeof(PLUGINSCACHE_DIR COPYRIGHT_MESSAGE) - 1;
1705     i_read = fread( p_cachestring, sizeof(char), i_size, file );
1706     if( i_read != i_size ||
1707         memcmp( p_cachestring, PLUGINSCACHE_DIR COPYRIGHT_MESSAGE, i_size ) )
1708     {
1709         msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
1710         fclose( file );
1711         return;
1712     }
1713
1714     /* Check Sub-version number */
1715     i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1716     if( i_read != sizeof(i_marker) || i_marker != CACHE_SUBVERSION_NUM )
1717     {
1718         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1719                   "(corrupted header)" );
1720         fclose( file );
1721         return;
1722     }
1723
1724     /* Check the language hasn't changed */
1725     sprintf( p_lang, "%5.5s", _("C") ); i_size = 5;
1726     i_read = fread( p_cachelang, sizeof(char), i_size, file );
1727     if( i_read != i_size || memcmp( p_cachelang, p_lang, i_size ) )
1728     {
1729         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1730                   "(language changed)" );
1731         fclose( file );
1732         return;
1733     }
1734
1735     /* Check header marker */
1736     i_read = fread( &i_marker, sizeof(char), sizeof(i_marker), file );
1737     if( i_read != sizeof(i_marker) ||
1738         i_marker != ftell( file ) - (int)sizeof(i_marker) )
1739     {
1740         msg_Warn( p_this, "This doesn't look like a valid plugins cache "
1741                   "(corrupted header)" );
1742         fclose( file );
1743         return;
1744     }
1745
1746     p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1747     fread( &i_cache, sizeof(char), sizeof(i_cache), file );
1748     if( i_cache )
1749         pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache =
1750                    malloc( i_cache * sizeof(void *) );
1751
1752 #define LOAD_IMMEDIATE(a) \
1753     if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
1754 #define LOAD_STRING(a) \
1755 { \
1756     a = NULL; \
1757     if( ( fread( &i_size, sizeof(i_size), 1, file ) != 1 ) \
1758      || ( i_size > 16384 ) ) \
1759         goto error; \
1760     if( i_size ) { \
1761         char *psz = malloc( i_size ); \
1762         if( fread( psz, i_size, 1, file ) != 1 ) { \
1763             free( psz ); \
1764             goto error; \
1765         } \
1766         if( psz[i_size-1] ) { \
1767             free( psz ); \
1768             goto error; \
1769         } \
1770         a = psz; \
1771     } \
1772 }
1773
1774     for( i = 0; i < i_cache; i++ )
1775     {
1776         uint16_t i_size;
1777         int i_submodules;
1778
1779         pp_cache[i] = malloc( sizeof(module_cache_t) );
1780         p_this->p_libvlc_global->p_module_bank->i_loaded_cache++;
1781
1782         /* Load common info */
1783         LOAD_STRING( pp_cache[i]->psz_file );
1784         LOAD_IMMEDIATE( pp_cache[i]->i_time );
1785         LOAD_IMMEDIATE( pp_cache[i]->i_size );
1786         LOAD_IMMEDIATE( pp_cache[i]->b_junk );
1787         pp_cache[i]->b_used = VLC_FALSE;
1788
1789         if( pp_cache[i]->b_junk ) continue;
1790
1791         pp_cache[i]->p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
1792
1793         /* Load additional infos */
1794         LOAD_STRING( pp_cache[i]->p_module->psz_object_name );
1795         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
1796         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
1797         LOAD_STRING( pp_cache[i]->p_module->psz_help );
1798         LOAD_STRING( pp_cache[i]->p_module->psz_program );
1799         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1800         {
1801             LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
1802         }
1803         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
1804         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
1805         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
1806         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
1807         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
1808         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
1809
1810         /* Config stuff */
1811         if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS )
1812             goto error;
1813
1814         LOAD_STRING( pp_cache[i]->p_module->psz_filename );
1815
1816         LOAD_IMMEDIATE( i_submodules );
1817
1818         while( i_submodules-- )
1819         {
1820             module_t *p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE);
1821             vlc_object_attach( p_module, pp_cache[i]->p_module );
1822             p_module->b_submodule = VLC_TRUE;
1823
1824             LOAD_STRING( p_module->psz_object_name );
1825             LOAD_STRING( p_module->psz_shortname );
1826             LOAD_STRING( p_module->psz_longname );
1827             LOAD_STRING( p_module->psz_help );
1828             LOAD_STRING( p_module->psz_program );
1829             for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
1830             {
1831                 LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
1832             }
1833             LOAD_STRING( p_module->psz_capability );
1834             LOAD_IMMEDIATE( p_module->i_score );
1835             LOAD_IMMEDIATE( p_module->i_cpu );
1836             LOAD_IMMEDIATE( p_module->b_unloadable );
1837             LOAD_IMMEDIATE( p_module->b_reentrant );
1838         }
1839     }
1840
1841     fclose( file );
1842     return;
1843
1844  error:
1845
1846     msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
1847
1848     /* TODO: cleanup */
1849     p_this->p_libvlc_global->p_module_bank->i_loaded_cache = 0;
1850
1851     fclose( file );
1852     return;
1853 }
1854
1855
1856 int CacheLoadConfig( module_t *p_module, FILE *file )
1857 {
1858     uint32_t i_lines;
1859     uint16_t i_size;
1860
1861     /* Calculate the structure length */
1862     LOAD_IMMEDIATE( p_module->i_config_items );
1863     LOAD_IMMEDIATE( p_module->i_bool_items );
1864
1865     LOAD_IMMEDIATE( i_lines );
1866
1867     /* Allocate memory */
1868     if (i_lines)
1869     {
1870         p_module->p_config =
1871             (module_config_t *)calloc( i_lines, sizeof(module_config_t) );
1872         if( p_module->p_config == NULL )
1873         {
1874             p_module->confsize = 0;
1875             msg_Err( p_module, "config error: can't duplicate p_config" );
1876             return VLC_ENOMEM;
1877         }
1878     }
1879     p_module->confsize = i_lines;
1880
1881     /* Do the duplication job */
1882     for (size_t i = 0; i < i_lines; i++ )
1883     {
1884         LOAD_IMMEDIATE( p_module->p_config[i] );
1885
1886         LOAD_STRING( p_module->p_config[i].psz_type );
1887         LOAD_STRING( p_module->p_config[i].psz_name );
1888         LOAD_STRING( p_module->p_config[i].psz_text );
1889         LOAD_STRING( p_module->p_config[i].psz_longtext );
1890         LOAD_STRING( p_module->p_config[i].psz_current );
1891
1892         if (IsConfigStringType (p_module->p_config[i].i_type))
1893         {
1894             LOAD_STRING (p_module->p_config[i].orig.psz);
1895             p_module->p_config[i].value.psz =
1896                     (p_module->p_config[i].orig.psz != NULL)
1897                         ? strdup (p_module->p_config[i].orig.psz) : NULL;
1898             p_module->p_config[i].saved.psz = NULL;
1899         }
1900         else
1901         {
1902             memcpy (&p_module->p_config[i].value, &p_module->p_config[i].orig,
1903                     sizeof (p_module->p_config[i].value));
1904             memcpy (&p_module->p_config[i].saved, &p_module->p_config[i].orig,
1905                     sizeof (p_module->p_config[i].saved));
1906         }
1907
1908         p_module->p_config[i].b_dirty = VLC_FALSE;
1909
1910         p_module->p_config[i].p_lock = &p_module->object_lock;
1911
1912         if( p_module->p_config[i].i_list )
1913         {
1914             if( p_module->p_config[i].ppsz_list )
1915             {
1916                 int j;
1917                 p_module->p_config[i].ppsz_list =
1918                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1919                 if( p_module->p_config[i].ppsz_list )
1920                 {
1921                     for( j = 0; j < p_module->p_config[i].i_list; j++ )
1922                         LOAD_STRING( p_module->p_config[i].ppsz_list[j] );
1923                     p_module->p_config[i].ppsz_list[j] = NULL;
1924                 }
1925             }
1926             if( p_module->p_config[i].ppsz_list_text )
1927             {
1928                 int j;
1929                 p_module->p_config[i].ppsz_list_text =
1930                     malloc( (p_module->p_config[i].i_list+1) * sizeof(char *));
1931                 if( p_module->p_config[i].ppsz_list_text )
1932                 {
1933                   for( j = 0; j < p_module->p_config[i].i_list; j++ )
1934                       LOAD_STRING( p_module->p_config[i].ppsz_list_text[j] );
1935                   p_module->p_config[i].ppsz_list_text[j] = NULL;
1936                 }
1937             }
1938             if( p_module->p_config[i].pi_list )
1939             {
1940                 p_module->p_config[i].pi_list =
1941                     malloc( (p_module->p_config[i].i_list + 1) * sizeof(int) );
1942                 if( p_module->p_config[i].pi_list )
1943                 {
1944                     for (int j = 0; j < p_module->p_config[i].i_list; j++)
1945                         LOAD_IMMEDIATE( p_module->p_config[i].pi_list[j] );
1946                 }
1947             }
1948         }
1949
1950         if( p_module->p_config[i].i_action )
1951         {
1952             p_module->p_config[i].ppf_action =
1953                 malloc( p_module->p_config[i].i_action * sizeof(void *) );
1954             p_module->p_config[i].ppsz_action_text =
1955                 malloc( p_module->p_config[i].i_action * sizeof(char *) );
1956
1957             for (int j = 0; j < p_module->p_config[i].i_action; j++)
1958             {
1959                 p_module->p_config[i].ppf_action[j] = 0;
1960                 LOAD_STRING( p_module->p_config[i].ppsz_action_text[j] );
1961             }
1962         }
1963
1964         LOAD_IMMEDIATE( p_module->p_config[i].pf_callback );
1965     }
1966
1967     return VLC_SUCCESS;
1968
1969  error:
1970
1971     return VLC_EGENERIC;
1972 }
1973
1974 /*****************************************************************************
1975  * SavePluginsCache: saves the plugins cache to a file
1976  *****************************************************************************/
1977 static void CacheSave( vlc_object_t *p_this )
1978 {
1979     static char const psz_tag[] =
1980         "Signature: 8a477f597d28d172789f06886806bc55\r\n"
1981         "# This file is a cache directory tag created by VLC.\r\n"
1982         "# For information about cache directory tags, see:\r\n"
1983         "#   http://www.brynosaurus.com/cachedir/\r\n";
1984
1985     char *psz_filename, *psz_homedir;
1986     FILE *file;
1987     int i, j, i_cache;
1988     module_cache_t **pp_cache;
1989     int32_t i_file_size = 0;
1990
1991     psz_homedir = p_this->p_libvlc->psz_homedir;
1992     if( !psz_homedir )
1993     {
1994         msg_Err( p_this, "psz_homedir is null" );
1995         return;
1996     }
1997     psz_filename =
1998        (char *)malloc( sizeof("/" CONFIG_DIR "/" PLUGINSCACHE_DIR "/" ) +
1999                        strlen(psz_homedir) + strlen(CacheName()) );
2000
2001     if( !psz_filename )
2002     {
2003         msg_Err( p_this, "out of memory" );
2004         return;
2005     }
2006
2007     sprintf( psz_filename, "%s/%s", psz_homedir, CONFIG_DIR );
2008
2009     config_CreateDir( p_this, psz_filename );
2010
2011     strcat( psz_filename, "/" PLUGINSCACHE_DIR );
2012
2013     config_CreateDir( p_this, psz_filename );
2014
2015     strcat( psz_filename, "/CACHEDIR.TAG" );
2016
2017     file = utf8_fopen( psz_filename, "wb" );
2018     if( file )
2019     {
2020         fwrite( psz_tag, 1, strlen(psz_tag), file );
2021         fclose( file );
2022     }
2023
2024     sprintf( psz_filename, "%s/%s/%s/%s", psz_homedir, CONFIG_DIR,
2025              PLUGINSCACHE_DIR, CacheName() );
2026
2027     msg_Dbg( p_this, "saving plugins cache file %s", psz_filename );
2028
2029     file = utf8_fopen( psz_filename, "wb" );
2030     if( !file )
2031     {
2032         msg_Warn( p_this, "could not open plugins cache file %s for writing",
2033                   psz_filename );
2034         free( psz_filename );
2035         return;
2036     }
2037     free( psz_filename );
2038
2039     /* Empty space for file size */
2040     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2041
2042     /* Contains version number */
2043     fprintf( file, "%s", PLUGINSCACHE_DIR COPYRIGHT_MESSAGE );
2044
2045     /* Sub-version number (to avoid breakage in the dev version when cache
2046      * structure changes) */
2047     i_file_size = CACHE_SUBVERSION_NUM;
2048     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2049
2050     /* Language */
2051     fprintf( file, "%5.5s", _("C") );
2052
2053     /* Header marker */
2054     i_file_size = ftell( file );
2055     fwrite( &i_file_size, sizeof(char), sizeof(i_file_size), file );
2056
2057     i_cache = p_this->p_libvlc_global->p_module_bank->i_cache;
2058     pp_cache = p_this->p_libvlc_global->p_module_bank->pp_cache;
2059
2060     fwrite( &i_cache, sizeof(char), sizeof(i_cache), file );
2061
2062 #define SAVE_IMMEDIATE(a) \
2063     fwrite( &a, sizeof(char), sizeof(a), file )
2064 #define SAVE_STRING(a) \
2065     { i_size = a ? strlen( a ) + 1 : 0; \
2066       fwrite( &i_size, sizeof(char), sizeof(i_size), file ); \
2067       if( a ) fwrite( a, sizeof(char), i_size, file ); \
2068     } while(0)
2069
2070     for( i = 0; i < i_cache; i++ )
2071     {
2072         uint16_t i_size;
2073         uint32_t i_submodule;
2074
2075         /* Save common info */
2076         SAVE_STRING( pp_cache[i]->psz_file );
2077         SAVE_IMMEDIATE( pp_cache[i]->i_time );
2078         SAVE_IMMEDIATE( pp_cache[i]->i_size );
2079         SAVE_IMMEDIATE( pp_cache[i]->b_junk );
2080
2081         if( pp_cache[i]->b_junk ) continue;
2082
2083         /* Save additional infos */
2084         SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
2085         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
2086         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
2087         SAVE_STRING( pp_cache[i]->p_module->psz_help );
2088         SAVE_STRING( pp_cache[i]->p_module->psz_program );
2089         for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
2090         {
2091             SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
2092         }
2093         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
2094         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
2095         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_cpu );
2096         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
2097         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_reentrant );
2098         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_submodule );
2099
2100         /* Config stuff */
2101         CacheSaveConfig( pp_cache[i]->p_module, file );
2102
2103         SAVE_STRING( pp_cache[i]->p_module->psz_filename );
2104
2105         i_submodule = pp_cache[i]->p_module->i_children;
2106         SAVE_IMMEDIATE( i_submodule );
2107         for( i_submodule = 0;
2108              i_submodule < (unsigned)pp_cache[i]->p_module->i_children;
2109              i_submodule++ )
2110         {
2111             module_t *p_module =
2112                 (module_t *)pp_cache[i]->p_module->pp_children[i_submodule];
2113
2114             SAVE_STRING( p_module->psz_object_name );
2115             SAVE_STRING( p_module->psz_shortname );
2116             SAVE_STRING( p_module->psz_longname );
2117             SAVE_STRING( p_module->psz_help );
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     (void)p_this;
2211
2212     p_cache->pf_activate = p_module->pf_activate;
2213     p_cache->pf_deactivate = p_module->pf_deactivate;
2214 #ifndef HAVE_SHARED_LIBVLC
2215     p_cache->p_symbols = p_module->p_symbols;
2216 #endif
2217     p_cache->handle = p_module->handle;
2218
2219     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
2220     {
2221         module_t *p_child = (module_t*)p_module->pp_children[i_submodule];
2222         module_t *p_cchild = (module_t*)p_cache->pp_children[i_submodule];
2223         p_cchild->pf_activate = p_child->pf_activate;
2224         p_cchild->pf_deactivate = p_child->pf_deactivate;
2225 #ifndef HAVE_SHARED_LIBVLC
2226         p_cchild->p_symbols = p_child->p_symbols;
2227 #endif
2228     }
2229
2230     p_cache->b_loaded = VLC_TRUE;
2231     p_module->b_loaded = VLC_FALSE;
2232 }
2233
2234 /*****************************************************************************
2235  * CacheFind: finds the cache entry corresponding to a file
2236  *****************************************************************************/
2237 static module_cache_t *CacheFind( vlc_object_t *p_this, char *psz_file,
2238                                   int64_t i_time, int64_t i_size )
2239 {
2240     module_cache_t **pp_cache;
2241     int i_cache, i;
2242
2243     pp_cache = p_this->p_libvlc_global->p_module_bank->pp_loaded_cache;
2244     i_cache = p_this->p_libvlc_global->p_module_bank->i_loaded_cache;
2245
2246     for( i = 0; i < i_cache; i++ )
2247     {
2248         if( !strcmp( pp_cache[i]->psz_file, psz_file ) &&
2249             pp_cache[i]->i_time == i_time &&
2250             pp_cache[i]->i_size == i_size ) return pp_cache[i];
2251     }
2252
2253     return NULL;
2254 }
2255
2256 #endif /* HAVE_DYNAMIC_PLUGINS */