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