]> git.sesse.net Git - vlc/blob - src/modules/modules.c
Remove /* inside comment
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include "libvlc.h"
34
35 /* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
36  * is set to 64. Don't try to be cleverer. */
37 #ifdef _FILE_OFFSET_BITS
38 #undef _FILE_OFFSET_BITS
39 #endif
40
41 #include <stdlib.h>                                      /* free(), strtol() */
42 #include <stdio.h>                                              /* sprintf() */
43 #include <string.h>                                              /* strdup() */
44 #include <assert.h>
45
46 #ifdef HAVE_DIRENT_H
47 #   include <dirent.h>
48 #endif
49
50 #ifdef HAVE_SYS_TYPES_H
51 #   include <sys/types.h>
52 #endif
53 #ifdef HAVE_SYS_STAT_H
54 #   include <sys/stat.h>
55 #endif
56 #ifdef HAVE_UNISTD_H
57 #   include <unistd.h>
58 #endif
59
60 #if !defined(HAVE_DYNAMIC_PLUGINS)
61     /* no support for plugins */
62 #elif defined(HAVE_DL_DYLD)
63 #   if defined(HAVE_MACH_O_DYLD_H)
64 #       include <mach-o/dyld.h>
65 #   endif
66 #elif defined(HAVE_DL_BEOS)
67 #   if defined(HAVE_IMAGE_H)
68 #       include <image.h>
69 #   endif
70 #elif defined(HAVE_DL_WINDOWS)
71 #   include <windows.h>
72 #elif defined(HAVE_DL_DLOPEN)
73 #   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
74 #       include <dlfcn.h>
75 #   endif
76 #   if defined(HAVE_SYS_DL_H)
77 #       include <sys/dl.h>
78 #   endif
79 #elif defined(HAVE_DL_SHL_LOAD)
80 #   if defined(HAVE_DL_H)
81 #       include <dl.h>
82 #   endif
83 #endif
84
85 #include "config/configuration.h"
86
87 #include "vlc_charset.h"
88 #include "vlc_arrays.h"
89
90 #include "modules/modules.h"
91 #include "modules/builtin.h"
92
93 module_bank_t *p_module_bank;
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98 #ifdef HAVE_DYNAMIC_PLUGINS
99 static void AllocateAllPlugins  ( vlc_object_t * );
100 static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
101 static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
102 static module_t * AllocatePlugin( vlc_object_t *, char * );
103 #endif
104 static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
105 static void DeleteModule ( module_t *, bool );
106 #ifdef HAVE_DYNAMIC_PLUGINS
107 static void   DupModule        ( module_t * );
108 static void   UndupModule      ( module_t * );
109 #endif
110
111 /**
112  * Init bank
113  *
114  * Creates a module bank structure which will be filled later
115  * on with all the modules found.
116  * \param p_this vlc object structure
117  * \return nothing
118  */
119 void __module_InitBank( vlc_object_t *p_this )
120 {
121     module_bank_t *p_bank = NULL;
122
123     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
124
125     if( p_module_bank == NULL )
126     {
127         p_bank = vlc_custom_create( p_this, sizeof(module_bank_t),
128                                     VLC_OBJECT_GENERIC, "module bank");
129         p_bank->i_usage = 1;
130         p_bank->i_cache = p_bank->i_loaded_cache = 0;
131         p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
132         p_bank->b_cache = p_bank->b_cache_dirty =
133         p_bank->b_cache_delete = false;
134
135         /* Everything worked, attach the object */
136         p_module_bank = p_bank;
137
138         /* Fills the module bank structure with the main module infos.
139          * This is very useful as it will allow us to consider the main
140          * library just as another module, and for instance the configuration
141          * options of main will be available in the module bank structure just
142          * as for every other module. */
143         AllocateBuiltinModule( p_this, vlc_entry__main );
144     }
145     else
146         p_module_bank->i_usage++;
147
148     vlc_mutex_unlock( lock );
149 }
150
151
152 /**
153  * End bank
154  *
155  * Unloads all unused plugin modules and empties the module
156  * bank in case of success.
157  * \param p_this vlc object structure
158  * \return nothing
159  */
160 void __module_EndBank( vlc_object_t *p_this )
161 {
162     module_t * p_next = NULL;
163
164     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
165     if( !p_module_bank )
166     {
167         vlc_mutex_unlock( lock );
168         return;
169     }
170     if( --p_module_bank->i_usage )
171     {
172         vlc_mutex_unlock( lock );
173         return;
174     }
175     vlc_mutex_unlock( lock );
176
177     /* Save the configuration */
178     config_AutoSaveConfigFile( p_this );
179
180 #ifdef HAVE_DYNAMIC_PLUGINS
181 # define p_bank p_module_bank
182     if( p_bank->b_cache ) CacheSave( p_this );
183     while( p_bank->i_loaded_cache-- )
184     {
185         if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
186         {
187             DeleteModule(
188                     p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
189                     p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
190             free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
191             free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
192             p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
193         }
194     }
195     if( p_bank->pp_loaded_cache )
196     {
197         free( p_bank->pp_loaded_cache );
198         p_bank->pp_loaded_cache = NULL;
199     }
200     while( p_bank->i_cache-- )
201     {
202         free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
203         free( p_bank->pp_cache[p_bank->i_cache] );
204         p_bank->pp_cache[p_bank->i_cache] = NULL;
205     }
206     if( p_bank->pp_cache )
207     {
208         free( p_bank->pp_cache );
209         p_bank->pp_cache = NULL;
210     }
211 # undef p_bank
212 #endif
213
214     while( vlc_internals( p_module_bank )->i_children )
215     {
216         p_next = (module_t *)vlc_internals( p_module_bank )->pp_children[0];
217         DeleteModule( p_next, true );
218     }
219
220     vlc_object_release( p_module_bank );
221     p_module_bank = NULL;
222 }
223
224 /**
225  * Load all modules which we built with.
226  *
227  * Fills the module bank structure with the builtin modules.
228  * \param p_this vlc object structure
229  * \return nothing
230  */
231 void __module_LoadBuiltins( vlc_object_t * p_this )
232 {
233     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
234     if( p_module_bank->b_builtins )
235     {
236         vlc_mutex_unlock( lock );
237         return;
238     }
239     p_module_bank->b_builtins = true;
240     vlc_mutex_unlock( lock );
241
242     msg_Dbg( p_this, "checking builtin modules" );
243     ALLOCATE_ALL_BUILTINS();
244 }
245
246 /**
247  * Load all plugins
248  *
249  * Load all plugin modules we can find.
250  * Fills the module bank structure with the plugin modules.
251  * \param p_this vlc object structure
252  * \return nothing
253  */
254 void __module_LoadPlugins( vlc_object_t * p_this )
255 {
256 #ifdef HAVE_DYNAMIC_PLUGINS
257     vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
258     if( p_module_bank->b_plugins )
259     {
260         vlc_mutex_unlock( lock );
261         return;
262     }
263     p_module_bank->b_plugins = true;
264     vlc_mutex_unlock( lock );
265
266     msg_Dbg( p_this, "checking plugin modules" );
267
268     if( config_GetInt( p_this, "plugins-cache" ) )
269         p_module_bank->b_cache = true;
270
271     if( p_module_bank->b_cache ||
272         p_module_bank->b_cache_delete ) CacheLoad( p_this );
273
274     AllocateAllPlugins( p_this );
275 #endif
276 }
277
278 /**
279  * Checks whether a module implements a capability.
280  *
281  * \param m the module
282  * \param cap the capability to check
283  * \return TRUE if the module have the capability
284  */
285 bool module_IsCapable( const module_t *m, const char *cap )
286 {
287     return !strcmp( m->psz_capability, cap );
288 }
289
290 /**
291  * Get the internal name of a module
292  *
293  * \param m the module
294  * \return the module name
295  */
296 const char *module_GetObjName( const module_t *m )
297 {
298     return m->psz_object_name;
299 }
300
301 /**
302  * Get the human-friendly name of a module.
303  *
304  * \param m the module
305  * \param long_name TRUE to have the long name of the module
306  * \return the short or long name of the module
307  */
308 const char *module_GetName( const module_t *m, bool long_name )
309 {
310     if( long_name && ( m->psz_longname != NULL) )
311         return m->psz_longname;
312
313     return m->psz_shortname ?: m->psz_object_name;
314 }
315
316 /**
317  * Get the help for a module
318  *
319  * \param m the module
320  * \return the help
321  */
322 const char *module_GetHelp( const module_t *m )
323 {
324     return m->psz_help;
325 }
326
327 /**
328  * module Need
329  *
330  * Return the best module function, given a capability list.
331  * \param p_this the vlc object
332  * \param psz_capability list of capabilities needed
333  * \param psz_name name of the module asked
334  * \param b_strict TRUE yto use the strict mode
335  * \return the module or NULL in case of a failure
336  */
337 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
338                           const char *psz_name, bool b_strict )
339 {
340     typedef struct module_list_t module_list_t;
341
342     stats_TimerStart( p_this, "module_Need()", STATS_TIMER_MODULE_NEED );
343
344     struct module_list_t
345     {
346         module_t *p_module;
347         int i_score;
348         bool b_force;
349         module_list_t *p_next;
350     };
351
352     module_list_t *p_list, *p_first, *p_tmp;
353     vlc_list_t *p_all;
354
355     int i_which_module, i_index = 0;
356
357     module_t *p_module;
358
359     int   i_shortcuts = 0;
360     char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
361     bool b_force_backup = p_this->b_force;
362
363
364     /* Deal with variables */
365     if( psz_name && psz_name[0] == '$' )
366     {
367         psz_name = psz_var = var_CreateGetString( p_this, psz_name + 1 );
368     }
369
370     /* Count how many different shortcuts were asked for */
371     if( psz_name && *psz_name )
372     {
373         char *psz_parser, *psz_last_shortcut;
374
375         /* If the user wants none, give him none. */
376         if( !strcmp( psz_name, "none" ) )
377         {
378             free( psz_var );
379             stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
380             stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
381             stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
382             return NULL;
383         }
384
385         i_shortcuts++;
386         psz_shortcuts = psz_last_shortcut = strdup( psz_name );
387
388         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
389         {
390             if( *psz_parser == ',' )
391             {
392                  *psz_parser = '\0';
393                  i_shortcuts++;
394                  psz_last_shortcut = psz_parser + 1;
395             }
396         }
397
398         /* Check if the user wants to override the "strict" mode */
399         if( psz_last_shortcut )
400         {
401             if( !strcmp(psz_last_shortcut, "none") )
402             {
403                 b_strict = true;
404                 i_shortcuts--;
405             }
406             else if( !strcmp(psz_last_shortcut, "any") )
407             {
408                 b_strict = false;
409                 i_shortcuts--;
410             }
411         }
412     }
413
414     /* Sort the modules and test them */
415     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
416     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
417     p_first = NULL;
418     unsigned i_cpu = vlc_CPU();
419
420     /* Parse the module list for capabilities and probe each of them */
421     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
422     {
423         int i_shortcut_bonus = 0;
424
425         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
426
427         /* Test that this module can do what we need */
428         if( !module_IsCapable( p_module, psz_capability ) )
429         {
430             /* Don't recurse through the sub-modules because vlc_list_find()
431              * will list them anyway. */
432             continue;
433         }
434
435         /* Test if we have the required CPU */
436         if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
437         {
438             continue;
439         }
440
441         /* If we required a shortcut, check this plugin provides it. */
442         if( i_shortcuts > 0 )
443         {
444             bool b_trash;
445             const char *psz_name = psz_shortcuts;
446
447             /* Let's drop modules with a <= 0 score (unless they are
448              * explicitly requested) */
449             b_trash = p_module->i_score <= 0;
450
451             for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
452             {
453                 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
454                 {
455                     char *c;
456                     if( ( c = strchr( psz_name, '@' ) )
457                         ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
458                                         c-psz_name )
459                         : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
460                     {
461                         /* Found it */
462                         if( c && c[1] )
463                             psz_alias = c+1;
464                         i_shortcut_bonus = i_short * 10000;
465                         goto found_shortcut;
466                     }
467                 }
468
469                 /* Go to the next shortcut... This is so lame! */
470                 psz_name += strlen( psz_name ) + 1;
471             }
472
473             /* If we are in "strict" mode and we couldn't
474              * find the module in the list of provided shortcuts,
475              * then kick the bastard out of here!!! */
476             if( b_strict )
477                 continue;
478         }
479         /* If we didn't require a shortcut, trash <= 0 scored plugins */
480         else if( p_module->i_score <= 0 )
481         {
482             continue;
483         }
484
485 found_shortcut:
486
487         /* Store this new module */
488         p_list[ i_index ].p_module = p_module;
489         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
490         p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
491
492         /* Add it to the modules-to-probe list */
493         if( i_index == 0 )
494         {
495             p_list[ 0 ].p_next = NULL;
496             p_first = p_list;
497         }
498         else
499         {
500             /* Ok, so at school you learned that quicksort is quick, and
501              * bubble sort sucks raw eggs. But that's when dealing with
502              * thousands of items. Here we have barely 50. */
503             module_list_t *p_newlist = p_first;
504
505             if( p_first->i_score < p_list[ i_index ].i_score )
506             {
507                 p_list[ i_index ].p_next = p_first;
508                 p_first = &p_list[ i_index ];
509             }
510             else
511             {
512                 while( p_newlist->p_next != NULL &&
513                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
514                 {
515                     p_newlist = p_newlist->p_next;
516                 }
517
518                 p_list[ i_index ].p_next = p_newlist->p_next;
519                 p_newlist->p_next = &p_list[ i_index ];
520             }
521         }
522
523         i_index++;
524     }
525
526     msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
527                                             i_index, i_index == 1 ? "" : "s" );
528
529     /* Lock all candidate modules */
530     p_tmp = p_first;
531     while( p_tmp != NULL )
532     {
533         vlc_object_yield( p_tmp->p_module );
534         p_tmp = p_tmp->p_next;
535     }
536
537     /* We can release the list, interesting modules were yielded */
538     vlc_list_release( p_all );
539
540     /* Parse the linked list and use the first successful module */
541     p_tmp = p_first;
542     while( p_tmp != NULL )
543     {
544 #ifdef HAVE_DYNAMIC_PLUGINS
545         /* Make sure the module is loaded in mem */
546         module_t *p_module = p_tmp->p_module;
547         if( p_module->b_submodule )
548             p_module = (module_t *)p_module->p_parent;
549
550         if( !p_module->b_builtin && !p_module->b_loaded )
551         {
552             module_t *p_new_module =
553                 AllocatePlugin( p_this, p_module->psz_filename );
554             if( p_new_module )
555             {
556                 CacheMerge( p_this, p_module, p_new_module );
557                 vlc_object_attach( p_new_module, p_module );
558                 DeleteModule( p_new_module, true );
559             }
560         }
561 #endif
562
563         p_this->b_force = p_tmp->b_force;
564         if( p_tmp->p_module->pf_activate
565              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
566         {
567             break;
568         }
569
570         vlc_object_release( p_tmp->p_module );
571         p_tmp = p_tmp->p_next;
572     }
573
574     /* Store the locked module value */
575     if( p_tmp != NULL )
576     {
577         p_module = p_tmp->p_module;
578         p_tmp = p_tmp->p_next;
579     }
580     else
581     {
582         p_module = NULL;
583     }
584
585     /* Unlock the remaining modules */
586     while( p_tmp != NULL )
587     {
588         vlc_object_release( p_tmp->p_module );
589         p_tmp = p_tmp->p_next;
590     }
591
592     free( p_list );
593     p_this->b_force = b_force_backup;
594
595     if( p_module != NULL )
596     {
597         msg_Dbg( p_this, "using %s module \"%s\"",
598                  psz_capability, p_module->psz_object_name );
599     }
600     else if( p_first == NULL )
601     {
602         if( !strcmp( psz_capability, "access_demux" ) )
603         {
604             msg_Warn( p_this, "no %s module matched \"%s\"",
605                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
606         }
607         else
608         {
609             msg_Err( p_this, "no %s module matched \"%s\"",
610                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
611
612             msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
613                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
614         }
615     }
616     else if( psz_name != NULL && *psz_name )
617     {
618         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
619                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
620     }
621     else
622         msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
623
624     if( p_module && !p_this->psz_object_name )
625     {
626         /* This assumes that p_this is the object which will be using the
627          * module. That's not always the case ... but it is in most cases.
628          */
629         if( psz_alias )
630             p_this->psz_object_name = strdup( psz_alias );
631         else
632             p_this->psz_object_name = strdup( p_module->psz_object_name );
633     }
634
635     free( psz_shortcuts );
636     free( psz_var );
637
638     stats_TimerStop( p_this, STATS_TIMER_MODULE_NEED );
639     stats_TimerDump( p_this, STATS_TIMER_MODULE_NEED );
640     stats_TimerClean( p_this, STATS_TIMER_MODULE_NEED );
641
642     /* Don't forget that the module is still locked */
643     return p_module;
644 }
645
646 /**
647  * Module unneed
648  *
649  * This function must be called by the thread that called module_Need, to
650  * decrease the reference count and allow for hiding of modules.
651  * \param p_this vlc object structure
652  * \param p_module the module structure
653  * \return nothing
654  */
655 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
656 {
657     /* Use the close method */
658     if( p_module->pf_deactivate )
659     {
660         p_module->pf_deactivate( p_this );
661     }
662
663     msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
664
665     vlc_object_release( p_module );
666 }
667
668 /**
669  * Get a pointer to a module_t given it's name.
670  *
671  * \param p_this vlc object structure
672  * \param psz_name the name of the module
673  * \return a pointer to the module or NULL in case of a failure
674  */
675 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
676 {
677     vlc_list_t *p_list;
678     int i;
679     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
680     for( i = 0 ; i < p_list->i_count; i++)
681     {
682         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
683         const char *psz_module_name = p_module->psz_object_name;
684         if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
685         {
686             /* We can release the list, and return yes */
687             vlc_object_yield( p_module );
688             vlc_list_release( p_list );
689             return p_module;
690         }
691     }
692     vlc_list_release( p_list );
693     return NULL;
694 }
695
696
697 /**
698  * Release a module_t pointer from module_Find().
699  *
700  * \param module the module to release
701  * \return nothing
702  */
703 void module_Put( module_t *module )
704 {
705     vlc_object_release( module );
706 }
707
708
709 /**
710  * Tell if a module exists and release it in thic case
711  *
712  * \param p_this vlc object structure
713  * \param psz_name th name of the module
714  * \return TRUE if the module exists
715  */
716 bool __module_Exists( vlc_object_t *p_this, const char * psz_name )
717 {
718     module_t *p_module = __module_Find( p_this, psz_name );
719     if( p_module )
720     {
721         module_Put( p_module );
722         return true;
723     }
724     else
725     {
726         return false;
727     }
728 }
729
730 /**
731  * GetModuleNamesForCapability
732  *
733  * Return a NULL terminated array with the names of the modules
734  * that have a certain capability.
735  * Free after uses both the string and the table.
736  * \param p_this vlc object structure
737  * \param psz_capability the capability asked
738  * \param pppsz_longname an pointer to an array of string to contain
739     the long names of the modules. If set to NULL the function don't use it.
740  * \return the NULL terminated array
741  */
742 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
743                                                const char *psz_capability,
744                                                char ***pppsz_longname )
745 {
746     vlc_list_t *p_list;
747     int i, j, count = 0;
748     char **psz_ret;
749
750     /* Do it in two passes : count the number of modules before */
751     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
752     for( i = 0 ; i < p_list->i_count; i++)
753     {
754         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
755         const char *psz_module_capability = p_module->psz_capability;
756         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
757             count++;
758     }
759
760     psz_ret = malloc( sizeof(char*) * (count+1) );
761     if( pppsz_longname )
762         *pppsz_longname = malloc( sizeof(char*) * (count+1) );
763     if( !psz_ret || ( pppsz_longname && *pppsz_longname == NULL ) )
764     {
765         free( psz_ret );
766         free( *pppsz_longname );
767         *pppsz_longname = NULL;
768         vlc_list_release( p_list );
769         return NULL;
770     }
771
772     j = 0;
773     for( i = 0 ; i < p_list->i_count; i++)
774     {
775         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
776         const char *psz_module_capability = p_module->psz_capability;
777         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
778         {
779             int k = -1; /* hack to handle submodules properly */
780             if( p_module->b_submodule )
781             {
782                 while( p_module->pp_shortcuts[++k] != NULL );
783                 k--;
784             }
785             psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
786                                      :p_module->psz_object_name );
787             if( pppsz_longname )
788                 (*pppsz_longname)[j] = strdup( module_GetName( p_module, true ) );
789             j++;
790         }
791     }
792     psz_ret[count] = NULL;
793
794     vlc_list_release( p_list );
795
796     return psz_ret;
797 }
798
799 /**
800  * Get the configuration of a module
801  *
802  * \param module the module
803  * \param psize the size of the configuration returned
804  * \return the configuration as an array
805  */
806 module_config_t *module_GetConfig( const module_t *module, unsigned *restrict psize )
807 {
808     unsigned i,j;
809     unsigned size = module->confsize;
810     module_config_t *config = malloc( size * sizeof( *config ) );
811
812     assert( psize != NULL );
813     *psize = 0;
814
815     if( !config )
816         return NULL;
817
818     for( i = 0, j = 0; i < size; i++ )
819     {
820         const module_config_t *item = module->p_config + i;
821         if( item->b_internal /* internal option */
822          || item->b_unsaveable /* non-modifiable option */
823          || item->b_removed /* removed option */ )
824             continue;
825
826         memcpy( config + j, item, sizeof( *config ) );
827         j++;
828     }
829     *psize = j;
830
831     return config;
832 }
833
834 /**
835  * Release the configuration
836  *
837  * \param the configuration
838  * \return nothing
839  */
840 void module_PutConfig( module_config_t *config )
841 {
842     free( config );
843 }
844
845 /*****************************************************************************
846  * Following functions are local.
847  *****************************************************************************/
848
849  /*****************************************************************************
850  * copy_next_paths_token: from a PATH_SEP_CHAR (a ':' or a ';') separated paths
851  * return first path.
852  *****************************************************************************/
853 static char * copy_next_paths_token( char * paths, char ** remaining_paths )
854 {
855     char * path;
856     int i, done;
857     bool escaped = false;
858
859     assert( paths );
860
861     /* Alloc a buffer to store the path */
862     path = malloc( strlen( paths ) + 1 );
863     if( !path ) return NULL;
864
865     /* Look for PATH_SEP_CHAR (a ':' or a ';') */
866     for( i = 0, done = 0 ; paths[i]; i++ )
867     {
868         /* Take care of \\ and \: or \; escapement */
869         if( escaped )
870         {
871             escaped = false;
872             path[done++] = paths[i];
873         }
874 #ifdef WIN32
875         else if( paths[i] == '/' )
876             escaped = true;
877 #else
878         else if( paths[i] == '\\' )
879             escaped = true;
880 #endif
881         else if( paths[i] == PATH_SEP_CHAR )
882             break;
883         else
884             path[done++] = paths[i];
885     }
886     path[done++] = 0;
887
888     /* Return the remaining paths */
889     if( remaining_paths ) {
890         *remaining_paths = paths[i] ? &paths[i]+1 : NULL;
891     }
892
893     return path;
894 }
895
896 /*****************************************************************************
897  * AllocateAllPlugins: load all plugin modules we can find.
898  *****************************************************************************/
899 #ifdef HAVE_DYNAMIC_PLUGINS
900 static void AllocateAllPlugins( vlc_object_t *p_this )
901 {
902     const char *vlcpath = vlc_global()->psz_vlcpath;
903     int count,i;
904     char * path;
905     vlc_array_t *arraypaths = vlc_array_new();
906
907     /* Contruct the special search path for system that have a relocatable
908      * executable. Set it to <vlc path>/modules and <vlc path>/plugins. */
909
910     if( vlcpath && asprintf( &path, "%s" DIR_SEP "modules", vlcpath ) != -1 )
911         vlc_array_append( arraypaths, path );
912     if( vlcpath && asprintf( &path, "%s" DIR_SEP "plugins", vlcpath ) != -1 )
913         vlc_array_append( arraypaths, path );
914 #ifndef WIN32
915     vlc_array_append( arraypaths, strdup( PLUGIN_PATH ) );
916 #endif
917
918     /* If the user provided a plugin path, we add it to the list */
919     char *userpaths = config_GetPsz( p_this, "plugin-path" );
920     char *paths_iter;
921
922     for( paths_iter = userpaths; paths_iter; )
923     {
924         path = copy_next_paths_token( paths_iter, &paths_iter );
925         if( path )
926             vlc_array_append( arraypaths, path );
927     }
928
929     count = vlc_array_count( arraypaths );
930     for( i = 0 ; i < count ; i++ )
931     {
932         path = vlc_array_item_at_index( arraypaths, i );
933         if( !path )
934             continue;
935
936         msg_Dbg( p_this, "recursively browsing `%s'", path );
937
938         /* Don't go deeper than 5 subdirectories */
939         AllocatePluginDir( p_this, path, 5 );
940
941         free( path );
942     }
943
944     vlc_array_destroy( arraypaths );
945     free( userpaths );
946 }
947
948 /*****************************************************************************
949  * AllocatePluginDir: recursively parse a directory to look for plugins
950  *****************************************************************************/
951 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
952                                int i_maxdepth )
953 {
954 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
955 #ifdef WIN32
956 # undef opendir
957 # undef closedir
958 # undef readdir
959 #endif
960 #if defined( UNDER_CE ) || defined( _MSC_VER )
961 #ifdef UNDER_CE
962     wchar_t psz_wpath[MAX_PATH + 256];
963     wchar_t psz_wdir[MAX_PATH];
964 #endif
965     char psz_path[MAX_PATH + 256];
966     WIN32_FIND_DATA finddata;
967     HANDLE handle;
968     int rc;
969 #else
970     int    i_dirlen;
971     DIR *  dir;
972     struct dirent * file;
973 #endif
974     char * psz_file;
975
976     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
977     {
978         return;
979     }
980
981 #if defined( UNDER_CE ) || defined( _MSC_VER )
982 #ifdef UNDER_CE
983     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
984
985     rc = GetFileAttributes( psz_wdir );
986     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
987
988     /* Parse all files in the directory */
989     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
990 #else
991     rc = GetFileAttributes( psz_dir );
992     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
993 #endif
994
995     /* Parse all files in the directory */
996     sprintf( psz_path, "%s\\*", psz_dir );
997
998 #ifdef UNDER_CE
999     handle = FindFirstFile( psz_wpath, &finddata );
1000 #else
1001     handle = FindFirstFile( psz_path, &finddata );
1002 #endif
1003     if( handle == INVALID_HANDLE_VALUE )
1004     {
1005         /* Empty directory */
1006         return;
1007     }
1008
1009     /* Parse the directory and try to load all files it contains. */
1010     do
1011     {
1012 #ifdef UNDER_CE
1013         unsigned int i_len = wcslen( finddata.cFileName );
1014         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
1015         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
1016 #else
1017         unsigned int i_len = strlen( finddata.cFileName );
1018         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
1019 #endif
1020
1021         /* Skip ".", ".." */
1022         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
1023          || !strcmp( finddata.cFileName, ".." ) )
1024         {
1025             if( !FindNextFile( handle, &finddata ) ) break;
1026             continue;
1027         }
1028
1029 #ifdef UNDER_CE
1030         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
1031 #else
1032         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
1033 #endif
1034         {
1035             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
1036         }
1037         else if( i_len > strlen( LIBEXT )
1038                   /* We only load files ending with LIBEXT */
1039                   && !strncasecmp( psz_path + strlen( psz_path)
1040                                    - strlen( LIBEXT ),
1041                                    LIBEXT, strlen( LIBEXT ) ) )
1042         {
1043             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
1044             int64_t i_time = 0, i_size = 0;
1045
1046 #ifdef UNDER_CE
1047             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
1048                                      &attrbuf ) )
1049 #else
1050             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
1051                                      &attrbuf ) )
1052 #endif
1053             {
1054                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
1055                 i_time <<= 32;
1056                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
1057                 i_size = attrbuf.nFileSizeHigh;
1058                 i_size <<= 32;
1059                 i_size |= attrbuf.nFileSizeLow;
1060             }
1061             psz_file = psz_path;
1062
1063             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1064         }
1065     }
1066     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1067
1068     /* Close the directory */
1069     FindClose( handle );
1070
1071 #else
1072     dir = opendir( psz_dir );
1073     if( !dir )
1074     {
1075         return;
1076     }
1077
1078     i_dirlen = strlen( psz_dir );
1079
1080     /* Parse the directory and try to load all files it contains. */
1081     while( !p_this->p_libvlc->b_die && ( file = readdir( dir ) ) )
1082     {
1083         struct stat statbuf;
1084         unsigned int i_len;
1085         int i_stat;
1086
1087         /* Skip ".", ".." */
1088         if( !*file->d_name || !strcmp( file->d_name, "." )
1089          || !strcmp( file->d_name, ".." ) )
1090         {
1091             continue;
1092         }
1093
1094         i_len = strlen( file->d_name );
1095         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1096         sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1097
1098         i_stat = stat( psz_file, &statbuf );
1099         if( !i_stat && statbuf.st_mode & S_IFDIR )
1100         {
1101             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1102         }
1103         else if( i_len > strlen( LIBEXT )
1104                   /* We only load files ending with LIBEXT */
1105                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1106                                    LIBEXT, strlen( LIBEXT ) ) )
1107         {
1108             int64_t i_time = 0, i_size = 0;
1109
1110             if( !i_stat )
1111             {
1112                 i_time = statbuf.st_mtime;
1113                 i_size = statbuf.st_size;
1114             }
1115
1116             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1117         }
1118
1119         free( psz_file );
1120     }
1121
1122     /* Close the directory */
1123     closedir( dir );
1124
1125 #endif
1126 }
1127
1128 /*****************************************************************************
1129  * AllocatePluginFile: load a module into memory and initialize it.
1130  *****************************************************************************
1131  * This function loads a dynamically loadable module and allocates a structure
1132  * for its information data. The module can then be handled by module_Need
1133  * and module_Unneed. It can be removed by DeleteModule.
1134  *****************************************************************************/
1135 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1136                                int64_t i_file_time, int64_t i_file_size )
1137 {
1138     module_t * p_module = NULL;
1139     module_cache_t *p_cache_entry = NULL;
1140
1141     /*
1142      * Check our plugins cache first then load plugin if needed
1143      */
1144     p_cache_entry =
1145         CacheFind( psz_file, i_file_time, i_file_size );
1146
1147     if( !p_cache_entry )
1148     {
1149         p_module = AllocatePlugin( p_this, psz_file );
1150     }
1151     else
1152     {
1153         /* If junk dll, don't try to load it */
1154         if( p_cache_entry->b_junk )
1155         {
1156             p_module = NULL;
1157         }
1158         else
1159         {
1160             module_config_t *p_item = NULL, *p_end = NULL;
1161
1162             p_module = p_cache_entry->p_module;
1163             p_module->b_loaded = false;
1164
1165             /* For now we force loading if the module's config contains
1166              * callbacks or actions.
1167              * Could be optimized by adding an API call.*/
1168             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1169                  p_item < p_end; p_item++ )
1170             {
1171                 if( p_item->pf_callback || p_item->i_action )
1172                 {
1173                     p_module = AllocatePlugin( p_this, psz_file );
1174                     break;
1175                 }
1176             }
1177             if( p_module == p_cache_entry->p_module )
1178                 p_cache_entry->b_used = true;
1179         }
1180     }
1181
1182     if( p_module )
1183     {
1184         /* Everything worked fine !
1185          * The module is ready to be added to the list. */
1186         p_module->b_builtin = false;
1187
1188         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1189                     p_module->psz_object_name, p_module->psz_longname ); */
1190
1191         vlc_object_attach( p_module, p_module_bank );
1192
1193         if( !p_module_bank->b_cache )
1194             return 0;
1195
1196 #define p_bank p_module_bank
1197         /* Add entry to cache */
1198         p_bank->pp_cache =
1199             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1200         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1201         if( !p_bank->pp_cache[p_bank->i_cache] )
1202             return -1;
1203         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1204         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1205         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1206         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1207         p_bank->pp_cache[p_bank->i_cache]->b_used = true;
1208         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1209         p_bank->i_cache++;
1210 #undef p_bank
1211     }
1212
1213     return p_module ? 0 : -1;
1214 }
1215
1216 /*****************************************************************************
1217  * AllocatePlugin: load a module into memory and initialize it.
1218  *****************************************************************************
1219  * This function loads a dynamically loadable module and allocates a structure
1220  * for its information data. The module can then be handled by module_Need
1221  * and module_Unneed. It can be removed by DeleteModule.
1222  *****************************************************************************/
1223 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1224 {
1225     module_t * p_module = NULL;
1226     module_handle_t handle;
1227
1228     if( module_Load( p_this, psz_file, &handle ) )
1229         return NULL;
1230
1231     /* Now that we have successfully loaded the module, we can
1232      * allocate a structure for it */
1233     p_module = vlc_module_create( p_this );
1234     if( p_module == NULL )
1235     {
1236         module_Unload( handle );
1237         return NULL;
1238     }
1239
1240     /* We need to fill these since they may be needed by module_Call() */
1241     p_module->psz_filename = psz_file;
1242     p_module->handle = handle;
1243     p_module->b_loaded = true;
1244
1245     /* Initialize the module: fill p_module, default config */
1246     if( module_Call( VLC_OBJECT(p_module), p_module ) != 0 )
1247     {
1248         /* We couldn't call module_init() */
1249         vlc_object_release( p_module );
1250         module_Unload( handle );
1251         return NULL;
1252     }
1253
1254     DupModule( p_module );
1255     p_module->psz_filename = strdup( p_module->psz_filename );
1256
1257     /* Everything worked fine ! The module is ready to be added to the list. */
1258     p_module->b_builtin = false;
1259
1260     return p_module;
1261 }
1262
1263 /*****************************************************************************
1264  * DupModule: make a plugin module standalone.
1265  *****************************************************************************
1266  * This function duplicates all strings in the module, so that the dynamic
1267  * object can be unloaded. It acts recursively on submodules.
1268  *****************************************************************************/
1269 static void DupModule( module_t *p_module )
1270 {
1271     char **pp_shortcut;
1272     int i_submodule;
1273
1274     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1275     {
1276         *pp_shortcut = strdup( *pp_shortcut );
1277     }
1278
1279     /* We strdup() these entries so that they are still valid when the
1280      * module is unloaded. */
1281     p_module->psz_capability = strdup( p_module->psz_capability );
1282     p_module->psz_shortname = p_module->psz_shortname ?
1283                                  strdup( p_module->psz_shortname ) : NULL;
1284     p_module->psz_longname = strdup( p_module->psz_longname );
1285     p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1286                                             : NULL;
1287
1288     for( i_submodule = 0; i_submodule < vlc_internals( p_module )->i_children; i_submodule++ )
1289     {
1290         DupModule( (module_t*)vlc_internals( p_module )->pp_children[ i_submodule ] );
1291     }
1292 }
1293
1294 /*****************************************************************************
1295  * UndupModule: free a duplicated module.
1296  *****************************************************************************
1297  * This function frees the allocations done in DupModule().
1298  *****************************************************************************/
1299 static void UndupModule( module_t *p_module )
1300 {
1301     char **pp_shortcut;
1302     int i_submodule;
1303
1304     for( i_submodule = 0; i_submodule < vlc_internals( p_module )->i_children; i_submodule++ )
1305     {
1306         UndupModule( (module_t*)vlc_internals( p_module )->pp_children[ i_submodule ] );
1307     }
1308
1309     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1310     {
1311         free( *pp_shortcut );
1312     }
1313
1314     FREENULL( p_module->psz_object_name );
1315     free( p_module->psz_capability );
1316     free( p_module->psz_shortname );
1317     free( p_module->psz_longname );
1318     free( p_module->psz_help );
1319 }
1320
1321 #endif /* HAVE_DYNAMIC_PLUGINS */
1322
1323 /*****************************************************************************
1324  * AllocateBuiltinModule: initialize a builtin module.
1325  *****************************************************************************
1326  * This function registers a builtin module and allocates a structure
1327  * for its information data. The module can then be handled by module_Need
1328  * and module_Unneed. It can be removed by DeleteModule.
1329  *****************************************************************************/
1330 static int AllocateBuiltinModule( vlc_object_t * p_this,
1331                                   int ( *pf_entry ) ( module_t * ) )
1332 {
1333     module_t * p_module;
1334
1335     /* Now that we have successfully loaded the module, we can
1336      * allocate a structure for it */
1337     p_module = vlc_module_create( p_this );
1338     if( p_module == NULL )
1339         return -1;
1340
1341     /* Initialize the module : fill p_module->psz_object_name, etc. */
1342     if( pf_entry( p_module ) != 0 )
1343     {
1344         /* With a well-written module we shouldn't have to print an
1345          * additional error message here, but just make sure. */
1346         msg_Err( p_this, "failed calling entry point in builtin module" );
1347         vlc_object_release( p_module );
1348         return -1;
1349     }
1350
1351     /* Everything worked fine ! The module is ready to be added to the list. */
1352     p_module->b_builtin = true;
1353
1354     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1355                 p_module->psz_object_name, p_module->psz_longname ); */
1356
1357     vlc_object_attach( p_module, p_module_bank );
1358
1359     return 0;
1360 }
1361
1362 /*****************************************************************************
1363  * DeleteModule: delete a module and its structure.
1364  *****************************************************************************
1365  * This function can only be called if the module isn't being used.
1366  *****************************************************************************/
1367 static void DeleteModule( module_t * p_module, bool b_detach )
1368 {
1369     assert( p_module );
1370
1371     if( b_detach )
1372         vlc_object_detach( p_module );
1373
1374     /* We free the structures that we strdup()ed in Allocate*Module(). */
1375 #ifdef HAVE_DYNAMIC_PLUGINS
1376     if( !p_module->b_builtin )
1377     {
1378         if( p_module->b_loaded && p_module->b_unloadable )
1379         {
1380             module_Unload( p_module->handle );
1381         }
1382         UndupModule( p_module );
1383         free( p_module->psz_filename );
1384     }
1385 #endif
1386
1387     /* Free and detach the object's children */
1388     while( vlc_internals( p_module )->i_children )
1389     {
1390         vlc_object_t *p_this = vlc_internals( p_module )->pp_children[0];
1391         vlc_object_detach( p_this );
1392         vlc_object_release( p_this );
1393     }
1394
1395     config_Free( p_module );
1396     vlc_object_release( p_module );
1397 }