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