]> git.sesse.net Git - vlc/blob - src/modules/modules.c
Add "safe" config item property
[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_GetObjName: internal name of a module.
336  *****************************************************************************/
337 const char *module_GetObjName( const module_t *m )
338 {
339     return m->psz_object_name;
340 }
341
342 /*****************************************************************************
343  * module_GetName: human-friendly name of a module.
344  *****************************************************************************/
345 const char *module_GetName( const module_t *m, vlc_bool_t long_name )
346 {
347     if( long_name && ( m->psz_longname != NULL) )
348         return m->psz_longname;
349  
350     return m->psz_shortname ?: m->psz_object_name;
351 }
352
353 const char *module_GetHelp( const module_t *m )
354 {
355     return m->psz_help;
356 }
357
358 /*****************************************************************************
359  * module_Need: return the best module function, given a capability list.
360  *****************************************************************************
361  * This function returns the module that best fits the asked capabilities.
362  *****************************************************************************/
363 module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
364                           const char *psz_name, vlc_bool_t b_strict )
365 {
366     typedef struct module_list_t module_list_t;
367
368     struct module_list_t
369     {
370         module_t *p_module;
371         int i_score;
372         vlc_bool_t b_force;
373         module_list_t *p_next;
374     };
375
376     module_list_t *p_list, *p_first, *p_tmp;
377     vlc_list_t *p_all;
378
379     int i_which_module, i_index = 0;
380
381     module_t *p_module;
382
383     int   i_shortcuts = 0;
384     char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
385     vlc_bool_t b_force_backup = p_this->b_force;
386
387
388     /* Deal with variables */
389     if( psz_name && psz_name[0] == '$' )
390     {
391         vlc_value_t val;
392         var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
393         var_Get( p_this, psz_name + 1, &val );
394         psz_var = val.psz_string;
395         psz_name = psz_var;
396     }
397
398     /* Count how many different shortcuts were asked for */
399     if( psz_name && *psz_name )
400     {
401         char *psz_parser, *psz_last_shortcut;
402
403         /* If the user wants none, give him none. */
404         if( !strcmp( psz_name, "none" ) )
405         {
406             free( psz_var );
407             return NULL;
408         }
409
410         i_shortcuts++;
411         psz_shortcuts = psz_last_shortcut = strdup( psz_name );
412
413         for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
414         {
415             if( *psz_parser == ',' )
416             {
417                  *psz_parser = '\0';
418                  i_shortcuts++;
419                  psz_last_shortcut = psz_parser + 1;
420             }
421         }
422
423         /* Check if the user wants to override the "strict" mode */
424         if( psz_last_shortcut )
425         {
426             if( !strcmp(psz_last_shortcut, "none") )
427             {
428                 b_strict = VLC_TRUE;
429                 i_shortcuts--;
430             }
431             else if( !strcmp(psz_last_shortcut, "any") )
432             {
433                 b_strict = VLC_FALSE;
434                 i_shortcuts--;
435             }
436         }
437     }
438
439     /* Sort the modules and test them */
440     p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
441     p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
442     p_first = NULL;
443     unsigned i_cpu = vlc_CPU();
444
445     /* Parse the module list for capabilities and probe each of them */
446     for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
447     {
448         int i_shortcut_bonus = 0;
449
450         p_module = (module_t *)p_all->p_values[i_which_module].p_object;
451
452         /* Test that this module can do what we need */
453         if( !module_IsCapable( p_module, psz_capability ) )
454         {
455             /* Don't recurse through the sub-modules because vlc_list_find()
456              * will list them anyway. */
457             continue;
458         }
459
460         /* Test if we have the required CPU */
461         if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
462         {
463             continue;
464         }
465
466         /* If we required a shortcut, check this plugin provides it. */
467         if( i_shortcuts > 0 )
468         {
469             vlc_bool_t b_trash;
470             const char *psz_name = psz_shortcuts;
471
472             /* Let's drop modules with a <= 0 score (unless they are
473              * explicitly requested) */
474             b_trash = p_module->i_score <= 0;
475
476             for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
477             {
478                 for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
479                 {
480                     char *c;
481                     if( ( c = strchr( psz_name, '@' ) )
482                         ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
483                                         c-psz_name )
484                         : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
485                     {
486                         /* Found it */
487                         if( c && c[1] )
488                             psz_alias = c+1;
489                         i_shortcut_bonus = i_short * 10000;
490                         goto found_shortcut;
491                     }
492                 }
493
494                 /* Go to the next shortcut... This is so lame! */
495                 psz_name += strlen( psz_name ) + 1;
496             }
497
498             /* If we are in "strict" mode and we couldn't
499              * find the module in the list of provided shortcuts,
500              * then kick the bastard out of here!!! */
501             if( b_strict )
502                 continue;
503         }
504         /* If we didn't require a shortcut, trash <= 0 scored plugins */
505         else if( p_module->i_score <= 0 )
506         {
507             continue;
508         }
509
510 found_shortcut:
511
512         /* Store this new module */
513         p_list[ i_index ].p_module = p_module;
514         p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
515         p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
516
517         /* Add it to the modules-to-probe list */
518         if( i_index == 0 )
519         {
520             p_list[ 0 ].p_next = NULL;
521             p_first = p_list;
522         }
523         else
524         {
525             /* Ok, so at school you learned that quicksort is quick, and
526              * bubble sort sucks raw eggs. But that's when dealing with
527              * thousands of items. Here we have barely 50. */
528             module_list_t *p_newlist = p_first;
529
530             if( p_first->i_score < p_list[ i_index ].i_score )
531             {
532                 p_list[ i_index ].p_next = p_first;
533                 p_first = &p_list[ i_index ];
534             }
535             else
536             {
537                 while( p_newlist->p_next != NULL &&
538                     p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
539                 {
540                     p_newlist = p_newlist->p_next;
541                 }
542
543                 p_list[ i_index ].p_next = p_newlist->p_next;
544                 p_newlist->p_next = &p_list[ i_index ];
545             }
546         }
547
548         i_index++;
549     }
550
551     msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
552                                             i_index, i_index == 1 ? "" : "s" );
553
554     /* Lock all candidate modules */
555     p_tmp = p_first;
556     while( p_tmp != NULL )
557     {
558         vlc_object_yield( p_tmp->p_module );
559         p_tmp = p_tmp->p_next;
560     }
561
562     /* We can release the list, interesting modules were yielded */
563     vlc_list_release( p_all );
564
565     /* Parse the linked list and use the first successful module */
566     p_tmp = p_first;
567     while( p_tmp != NULL )
568     {
569 #ifdef HAVE_DYNAMIC_PLUGINS
570         /* Make sure the module is loaded in mem */
571         module_t *p_module = p_tmp->p_module;
572         if( p_module->b_submodule )
573             p_module = (module_t *)p_module->p_parent;
574
575         if( !p_module->b_builtin && !p_module->b_loaded )
576         {
577             module_t *p_new_module =
578                 AllocatePlugin( p_this, p_module->psz_filename );
579             if( p_new_module )
580             {
581                 CacheMerge( p_this, p_module, p_new_module );
582                 vlc_object_attach( p_new_module, p_module );
583                 DeleteModule( p_new_module, VLC_TRUE );
584             }
585         }
586 #endif
587
588         p_this->b_force = p_tmp->b_force;
589         if( p_tmp->p_module->pf_activate
590              && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
591         {
592             break;
593         }
594
595         vlc_object_release( p_tmp->p_module );
596         p_tmp = p_tmp->p_next;
597     }
598
599     /* Store the locked module value */
600     if( p_tmp != NULL )
601     {
602         p_module = p_tmp->p_module;
603         p_tmp = p_tmp->p_next;
604     }
605     else
606     {
607         p_module = NULL;
608     }
609
610     /* Unlock the remaining modules */
611     while( p_tmp != NULL )
612     {
613         vlc_object_release( p_tmp->p_module );
614         p_tmp = p_tmp->p_next;
615     }
616
617     free( p_list );
618     p_this->b_force = b_force_backup;
619
620     if( p_module != NULL )
621     {
622         msg_Dbg( p_this, "using %s module \"%s\"",
623                  psz_capability, p_module->psz_object_name );
624     }
625     else if( p_first == NULL )
626     {
627         if( !strcmp( psz_capability, "access_demux" ) )
628         {
629             msg_Warn( p_this, "no %s module matched \"%s\"",
630                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
631         }
632         else
633         {
634             msg_Err( p_this, "no %s module matched \"%s\"",
635                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
636
637             msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
638                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
639         }
640     }
641     else if( psz_name != NULL && *psz_name )
642     {
643         msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
644                   psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
645     }
646     else
647         msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
648
649     if( p_module && !p_this->psz_object_name )
650     {
651         /* This assumes that p_this is the object which will be using the
652          * module. That's not always the case ... but it is in most cases.
653          */
654         if( psz_alias )
655             p_this->psz_object_name = strdup( psz_alias );
656         else
657             p_this->psz_object_name = strdup( p_module->psz_object_name );
658     }
659
660     free( psz_shortcuts );
661     free( psz_var );
662
663     /* Don't forget that the module is still locked */
664     return p_module;
665 }
666
667 /*****************************************************************************
668  * module_Unneed: decrease the usage count of a module.
669  *****************************************************************************
670  * This function must be called by the thread that called module_Need, to
671  * decrease the reference count and allow for hiding of modules.
672  *****************************************************************************/
673 void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
674 {
675     /* Use the close method */
676     if( p_module->pf_deactivate )
677     {
678         p_module->pf_deactivate( p_this );
679     }
680
681     msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
682
683     vlc_object_release( p_module );
684
685     return;
686 }
687
688 /*****************************************************************************
689  * module_Find: get a pointer to a module_t given it's name.
690  *****************************************************************************/
691 module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
692 {
693     vlc_list_t *p_list;
694     int i;
695     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
696     for( i = 0 ; i < p_list->i_count; i++)
697     {
698         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
699         const char *psz_module_name = p_module->psz_object_name;
700         if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
701         {
702             /* We can release the list, and return yes */
703             vlc_object_yield( p_module );
704             vlc_list_release( p_list );
705             return p_module;
706         }
707     }
708     vlc_list_release( p_list );
709     return NULL;
710 }
711
712
713 /*****************************************************************************
714  * module_Put: release a module_t pointer from module_Find().
715  *****************************************************************************/
716 void module_Put ( module_t *module )
717 {
718     vlc_object_release ( module );
719 }
720
721
722 /*****************************************************************************
723  * module_Exists: tell if a module exists.
724  *****************************************************************************
725  * This function is a boolean function that tells if a module exist or not.
726  *****************************************************************************/
727 vlc_bool_t __module_Exists(  vlc_object_t *p_this, const char * psz_name )
728 {
729     module_t *p_module = __module_Find( p_this, psz_name );
730     if( p_module )
731     {
732         module_Put( p_module );
733         return VLC_TRUE;
734     }
735     else
736     {
737         return VLC_FALSE;
738     }
739 }
740
741 /*****************************************************************************
742  * module_GetModuleNamesForCapability: Return a NULL terminated array with the
743  * names of the modules that have a certain capability.
744  * Free after uses both the string and the table.
745  *****************************************************************************/
746 char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
747                                                const char * psz_capability,
748                                                char ***pppsz_longname )
749 {
750     vlc_list_t *p_list;
751     int i, j, count = 0;
752     char ** psz_ret;
753
754     /* Do it in two passes */
755     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
756     for( i = 0 ; i < p_list->i_count; i++)
757     {
758         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
759         const char *psz_module_capability = p_module->psz_capability;
760         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
761             count++;
762     }
763     psz_ret = malloc( sizeof(char*) * (count+1) );
764     if( pppsz_longname )
765         *pppsz_longname = malloc( sizeof(char*) * (count+1) );
766     j = 0;
767     for( i = 0 ; i < p_list->i_count; i++)
768     {
769         module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
770         const char *psz_module_capability = p_module->psz_capability;
771         if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
772         {
773             int k = -1; /* hack to handle submodules properly */
774             if( p_module->b_submodule )
775             {
776                 while( p_module->pp_shortcuts[++k] != NULL );
777                 k--;
778             }
779             psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
780                                      :p_module->psz_object_name );
781             if( pppsz_longname )
782                 (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
783             j++;
784         }
785     }
786     psz_ret[count] = NULL;
787
788     vlc_list_release( p_list );
789
790     return psz_ret;
791 }
792
793
794 module_config_t *module_GetConfig (const module_t *module, unsigned *restrict psize)
795 {
796     unsigned size = module->confsize;
797     module_config_t *config = malloc (size * sizeof (*config));
798
799     assert (psize != NULL);
800     *psize = 0;
801
802     for (unsigned i = 0, j = 0; i < size; i++)
803     {
804         const module_config_t *item = module->p_config + i;
805         if (item->b_internal /* internal option */
806          || item->b_unsaveable /* non-modifiable option */
807          || item->b_removed /* removed option */)
808             continue;
809
810         if (config != NULL)
811             memcpy (config + j, item, sizeof (*config));
812         *psize = ++j;
813     }
814
815     return config;
816 }
817
818 void module_PutConfig (module_config_t *config)
819 {
820     free (config);
821 }
822
823 /*****************************************************************************
824  * Following functions are local.
825  *****************************************************************************/
826
827 /*****************************************************************************
828  * AllocateAllPlugins: load all plugin modules we can find.
829  *****************************************************************************/
830 #ifdef HAVE_DYNAMIC_PLUGINS
831 static void AllocateAllPlugins( vlc_object_t *p_this )
832 {
833     /* Yes, there are two NULLs because we replace one with "plugin-path". */
834 #if defined( WIN32 ) || defined( UNDER_CE )
835     const char *path[] = { "modules", "", "plugins", NULL, NULL };
836 #else
837     const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
838 #endif
839
840     const char *const *ppsz_path;
841
842     /* If the user provided a plugin path, we add it to the list */
843     char *userpath = config_GetPsz( p_this, "plugin-path" );
844     path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
845
846     for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
847     {
848         char *psz_fullpath;
849
850         if (!**ppsz_path) continue;
851
852 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
853
854         /* Handle relative as well as absolute paths */
855 #ifdef WIN32
856         if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
857             (*ppsz_path)[1] != ':' )
858 #else
859         if( (*ppsz_path)[0] != '/' )
860 #endif
861         {
862             if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
863                               vlc_global()->psz_vlcpath, *ppsz_path) )
864                 psz_fullpath = NULL;
865         }
866         else
867 #endif
868             psz_fullpath = strdup( *ppsz_path );
869
870         if( psz_fullpath == NULL )
871             continue;
872
873         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
874
875         /* Don't go deeper than 5 subdirectories */
876         AllocatePluginDir( p_this, psz_fullpath, 5 );
877
878         free( psz_fullpath );
879     }
880
881     /* Free plugin-path */
882     free( userpath );
883 }
884
885 /*****************************************************************************
886  * AllocatePluginDir: recursively parse a directory to look for plugins
887  *****************************************************************************/
888 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
889                                int i_maxdepth )
890 {
891 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
892 #ifdef WIN32
893 # undef opendir
894 # undef closedir
895 # undef readdir
896 #endif
897 #if defined( UNDER_CE ) || defined( _MSC_VER )
898 #ifdef UNDER_CE
899     wchar_t psz_wpath[MAX_PATH + 256];
900     wchar_t psz_wdir[MAX_PATH];
901 #endif
902     char psz_path[MAX_PATH + 256];
903     WIN32_FIND_DATA finddata;
904     HANDLE handle;
905     int rc;
906 #else
907     int    i_dirlen;
908     DIR *  dir;
909     struct dirent * file;
910 #endif
911     char * psz_file;
912
913     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
914     {
915         return;
916     }
917
918 #if defined( UNDER_CE ) || defined( _MSC_VER )
919 #ifdef UNDER_CE
920     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
921
922     rc = GetFileAttributes( psz_wdir );
923     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
924
925     /* Parse all files in the directory */
926     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
927 #else
928     rc = GetFileAttributes( psz_dir );
929     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
930 #endif
931
932     /* Parse all files in the directory */
933     sprintf( psz_path, "%s\\*", psz_dir );
934
935 #ifdef UNDER_CE
936     handle = FindFirstFile( psz_wpath, &finddata );
937 #else
938     handle = FindFirstFile( psz_path, &finddata );
939 #endif
940     if( handle == INVALID_HANDLE_VALUE )
941     {
942         /* Empty directory */
943         return;
944     }
945
946     /* Parse the directory and try to load all files it contains. */
947     do
948     {
949 #ifdef UNDER_CE
950         unsigned int i_len = wcslen( finddata.cFileName );
951         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
952         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
953 #else
954         unsigned int i_len = strlen( finddata.cFileName );
955         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
956 #endif
957
958         /* Skip ".", ".." */
959         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
960          || !strcmp( finddata.cFileName, ".." ) )
961         {
962             if( !FindNextFile( handle, &finddata ) ) break;
963             continue;
964         }
965
966 #ifdef UNDER_CE
967         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
968 #else
969         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
970 #endif
971         {
972             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
973         }
974         else if( i_len > strlen( LIBEXT )
975                   /* We only load files ending with LIBEXT */
976                   && !strncasecmp( psz_path + strlen( psz_path)
977                                    - strlen( LIBEXT ),
978                                    LIBEXT, strlen( LIBEXT ) ) )
979         {
980             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
981             int64_t i_time = 0, i_size = 0;
982
983 #ifdef UNDER_CE
984             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
985                                      &attrbuf ) )
986 #else
987             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
988                                      &attrbuf ) )
989 #endif
990             {
991                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
992                 i_time <<= 32;
993                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
994                 i_size = attrbuf.nFileSizeHigh;
995                 i_size <<= 32;
996                 i_size |= attrbuf.nFileSizeLow;
997             }
998             psz_file = psz_path;
999
1000             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1001         }
1002     }
1003     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
1004
1005     /* Close the directory */
1006     FindClose( handle );
1007
1008 #else
1009     dir = opendir( psz_dir );
1010     if( !dir )
1011     {
1012         return;
1013     }
1014
1015     i_dirlen = strlen( psz_dir );
1016
1017     /* Parse the directory and try to load all files it contains. */
1018     while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1019     {
1020         struct stat statbuf;
1021         unsigned int i_len;
1022         int i_stat;
1023
1024         /* Skip ".", ".." */
1025         if( !*file->d_name || !strcmp( file->d_name, "." )
1026          || !strcmp( file->d_name, ".." ) )
1027         {
1028             continue;
1029         }
1030
1031         i_len = strlen( file->d_name );
1032         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1033         sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1034
1035         i_stat = stat( psz_file, &statbuf );
1036         if( !i_stat && statbuf.st_mode & S_IFDIR )
1037         {
1038             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1039         }
1040         else if( i_len > strlen( LIBEXT )
1041                   /* We only load files ending with LIBEXT */
1042                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1043                                    LIBEXT, strlen( LIBEXT ) ) )
1044         {
1045             int64_t i_time = 0, i_size = 0;
1046
1047             if( !i_stat )
1048             {
1049                 i_time = statbuf.st_mtime;
1050                 i_size = statbuf.st_size;
1051             }
1052
1053             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1054         }
1055
1056         free( psz_file );
1057     }
1058
1059     /* Close the directory */
1060     closedir( dir );
1061
1062 #endif
1063 }
1064
1065 /*****************************************************************************
1066  * AllocatePluginFile: load a module into memory and initialize it.
1067  *****************************************************************************
1068  * This function loads a dynamically loadable module and allocates a structure
1069  * for its information data. The module can then be handled by module_Need
1070  * and module_Unneed. It can be removed by DeleteModule.
1071  *****************************************************************************/
1072 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1073                                int64_t i_file_time, int64_t i_file_size )
1074 {
1075     module_t * p_module = NULL;
1076     module_cache_t *p_cache_entry = NULL;
1077
1078     /*
1079      * Check our plugins cache first then load plugin if needed
1080      */
1081     p_cache_entry =
1082         CacheFind( p_this, psz_file, i_file_time, i_file_size );
1083
1084     if( !p_cache_entry )
1085     {
1086         p_module = AllocatePlugin( p_this, psz_file );
1087     }
1088     else
1089     {
1090         /* If junk dll, don't try to load it */
1091         if( p_cache_entry->b_junk )
1092         {
1093             p_module = NULL;
1094         }
1095         else
1096         {
1097             module_config_t *p_item = NULL, *p_end = NULL;
1098
1099             p_module = p_cache_entry->p_module;
1100             p_module->b_loaded = VLC_FALSE;
1101
1102             /* For now we force loading if the module's config contains
1103              * callbacks or actions.
1104              * Could be optimized by adding an API call.*/
1105             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1106                  p_item < p_end; p_item++ )
1107             {
1108                 if( p_item->pf_callback || p_item->i_action )
1109                 {
1110                     p_module = AllocatePlugin( p_this, psz_file );
1111                     break;
1112                 }
1113             }
1114             if( p_module == p_cache_entry->p_module )
1115                 p_cache_entry->b_used = VLC_TRUE;
1116         }
1117     }
1118
1119     if( p_module )
1120     {
1121         libvlc_global_data_t *p_libvlc_global = vlc_global();
1122
1123         /* Everything worked fine !
1124          * The module is ready to be added to the list. */
1125         p_module->b_builtin = VLC_FALSE;
1126
1127         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1128                     p_module->psz_object_name, p_module->psz_longname ); */
1129
1130         vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1131
1132         if( !p_libvlc_global->p_module_bank->b_cache )
1133             return 0;
1134
1135 #define p_bank p_libvlc_global->p_module_bank
1136         /* Add entry to cache */
1137         p_bank->pp_cache =
1138             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1139         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1140         if( !p_bank->pp_cache[p_bank->i_cache] )
1141             return -1;
1142         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1143         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1144         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1145         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1146         p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1147         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1148         p_bank->i_cache++;
1149     }
1150
1151     return p_module ? 0 : -1;
1152 }
1153
1154 /*****************************************************************************
1155  * AllocatePlugin: load a module into memory and initialize it.
1156  *****************************************************************************
1157  * This function loads a dynamically loadable module and allocates a structure
1158  * for its information data. The module can then be handled by module_Need
1159  * and module_Unneed. It can be removed by DeleteModule.
1160  *****************************************************************************/
1161 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1162 {
1163     module_t * p_module = NULL;
1164     module_handle_t handle;
1165
1166     if( module_Load( p_this, psz_file, &handle ) )
1167         return NULL;
1168
1169     /* Now that we have successfully loaded the module, we can
1170      * allocate a structure for it */
1171     p_module = vlc_module_create( p_this );
1172     if( p_module == NULL )
1173     {
1174         msg_Err( p_this, "out of memory" );
1175         module_Unload( handle );
1176         return NULL;
1177     }
1178
1179     /* We need to fill these since they may be needed by module_Call() */
1180     p_module->psz_filename = psz_file;
1181     p_module->handle = handle;
1182     p_module->b_loaded = VLC_TRUE;
1183
1184     /* Initialize the module: fill p_module, default config */
1185     if( module_Call( p_module ) != 0 )
1186     {
1187         /* We couldn't call module_init() */
1188         vlc_object_destroy( p_module );
1189         module_Unload( handle );
1190         return NULL;
1191     }
1192
1193     DupModule( p_module );
1194     p_module->psz_filename = strdup( p_module->psz_filename );
1195
1196     /* Everything worked fine ! The module is ready to be added to the list. */
1197     p_module->b_builtin = VLC_FALSE;
1198
1199     return p_module;
1200 }
1201
1202 /*****************************************************************************
1203  * DupModule: make a plugin module standalone.
1204  *****************************************************************************
1205  * This function duplicates all strings in the module, so that the dynamic
1206  * object can be unloaded. It acts recursively on submodules.
1207  *****************************************************************************/
1208 static void DupModule( module_t *p_module )
1209 {
1210     const char **pp_shortcut;
1211     int i_submodule;
1212
1213     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1214     {
1215         *pp_shortcut = strdup( *pp_shortcut );
1216     }
1217
1218     /* We strdup() these entries so that they are still valid when the
1219      * module is unloaded. */
1220     p_module->psz_object_name = strdup( p_module->psz_object_name );
1221     p_module->psz_capability = strdup( p_module->psz_capability );
1222     p_module->psz_shortname = p_module->psz_shortname ?
1223                                  strdup( p_module->psz_shortname ) : NULL;
1224     p_module->psz_longname = strdup( p_module->psz_longname );
1225     p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1226                                             : NULL;
1227
1228     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1229     {
1230         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1231     }
1232 }
1233
1234 /*****************************************************************************
1235  * UndupModule: free a duplicated module.
1236  *****************************************************************************
1237  * This function frees the allocations done in DupModule().
1238  *****************************************************************************/
1239 static void UndupModule( module_t *p_module )
1240 {
1241     const char **pp_shortcut;
1242     int i_submodule;
1243
1244     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1245     {
1246         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1247     }
1248
1249     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1250     {
1251         free( (void*)*pp_shortcut );
1252     }
1253
1254     free( (void*)p_module->psz_object_name );
1255     free( p_module->psz_capability );
1256     free( (void*)p_module->psz_shortname );
1257     free( (void*)p_module->psz_longname );
1258     free( (void*)p_module->psz_help );
1259 }
1260
1261 #endif /* HAVE_DYNAMIC_PLUGINS */
1262
1263 /*****************************************************************************
1264  * AllocateBuiltinModule: initialize a builtin module.
1265  *****************************************************************************
1266  * This function registers a builtin module and allocates a structure
1267  * for its information data. The module can then be handled by module_Need
1268  * and module_Unneed. It can be removed by DeleteModule.
1269  *****************************************************************************/
1270 static int AllocateBuiltinModule( vlc_object_t * p_this,
1271                                   int ( *pf_entry ) ( module_t * ) )
1272 {
1273     module_t * p_module;
1274
1275     /* Now that we have successfully loaded the module, we can
1276      * allocate a structure for it */
1277     p_module = vlc_module_create( p_this );
1278     if( p_module == NULL )
1279     {
1280         msg_Err( p_this, "out of memory" );
1281         return -1;
1282     }
1283
1284     /* Initialize the module : fill p_module->psz_object_name, etc. */
1285     if( pf_entry( p_module ) != 0 )
1286     {
1287         /* With a well-written module we shouldn't have to print an
1288          * additional error message here, but just make sure. */
1289         msg_Err( p_this, "failed calling entry point in builtin module" );
1290         vlc_object_destroy( p_module );
1291         return -1;
1292     }
1293
1294     /* Everything worked fine ! The module is ready to be added to the list. */
1295     p_module->b_builtin = VLC_TRUE;
1296
1297     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1298                 p_module->psz_object_name, p_module->psz_longname ); */
1299
1300     vlc_object_attach( p_module, vlc_global()->p_module_bank );
1301
1302     return 0;
1303 }
1304
1305 /*****************************************************************************
1306  * DeleteModule: delete a module and its structure.
1307  *****************************************************************************
1308  * This function can only be called if the module isn't being used.
1309  *****************************************************************************/
1310 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1311 {
1312     if( !p_module ) return VLC_EGENERIC;
1313     if( b_detach )
1314         vlc_object_detach( p_module );
1315
1316     /* We free the structures that we strdup()ed in Allocate*Module(). */
1317 #ifdef HAVE_DYNAMIC_PLUGINS
1318     if( !p_module->b_builtin )
1319     {
1320         if( p_module->b_loaded && p_module->b_unloadable )
1321         {
1322             module_Unload( p_module->handle );
1323         }
1324         UndupModule( p_module );
1325         free( p_module->psz_filename );
1326     }
1327 #endif
1328
1329     /* Free and detach the object's children */
1330     while( p_module->i_children )
1331     {
1332         vlc_object_t *p_this = p_module->pp_children[0];
1333         vlc_object_detach( p_this );
1334         vlc_object_destroy( p_this );
1335     }
1336
1337     config_Free( p_module );
1338     vlc_object_destroy( p_module );
1339     p_module = NULL;
1340     return 0;
1341 }