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