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