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