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