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