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