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