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