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