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