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