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