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