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