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