]> git.sesse.net Git - vlc/blob - src/modules/modules.c
- fix symbol list
[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
798     assert (psize != NULL);
799     *psize = size;
800
801     module_config_t *config = malloc (size * sizeof (*config));
802     if (config)
803         memcpy (config, module->p_config, size * sizeof (*config));
804
805     return config;
806 }
807
808 void module_PutConfig (module_config_t *config)
809 {
810     free (config);
811 }
812
813 /*****************************************************************************
814  * Following functions are local.
815  *****************************************************************************/
816
817 /*****************************************************************************
818  * AllocateAllPlugins: load all plugin modules we can find.
819  *****************************************************************************/
820 #ifdef HAVE_DYNAMIC_PLUGINS
821 static void AllocateAllPlugins( vlc_object_t *p_this )
822 {
823     /* Yes, there are two NULLs because we replace one with "plugin-path". */
824 #if defined( WIN32 ) || defined( UNDER_CE )
825     const char *path[] = { "modules", "", "plugins", NULL, NULL };
826 #else
827     const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
828 #endif
829
830     const char *const *ppsz_path;
831
832     /* If the user provided a plugin path, we add it to the list */
833     char *userpath = config_GetPsz( p_this, "plugin-path" );
834     path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
835
836     for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
837     {
838         char *psz_fullpath;
839
840         if (!**ppsz_path) continue;
841
842 #if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
843
844         /* Handle relative as well as absolute paths */
845 #ifdef WIN32
846         if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
847             (*ppsz_path)[1] != ':' )
848 #else
849         if( (*ppsz_path)[0] != '/' )
850 #endif
851         {
852             if( 0>= asprintf( &psz_fullpath, "%s"DIR_SEP"%s",
853                               vlc_global()->psz_vlcpath, *ppsz_path) )
854                 psz_fullpath = NULL;
855         }
856         else
857 #endif
858             psz_fullpath = strdup( *ppsz_path );
859
860         if( psz_fullpath == NULL )
861             continue;
862
863         msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
864
865         /* Don't go deeper than 5 subdirectories */
866         AllocatePluginDir( p_this, psz_fullpath, 5 );
867
868         free( psz_fullpath );
869     }
870
871     /* Free plugin-path */
872     free( userpath );
873 }
874
875 /*****************************************************************************
876  * AllocatePluginDir: recursively parse a directory to look for plugins
877  *****************************************************************************/
878 static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
879                                int i_maxdepth )
880 {
881 /* FIXME: Needs to be ported to wide char on ALL Windows builds */
882 #ifdef WIN32
883 # undef opendir
884 # undef closedir
885 # undef readdir
886 #endif
887 #if defined( UNDER_CE ) || defined( _MSC_VER )
888 #ifdef UNDER_CE
889     wchar_t psz_wpath[MAX_PATH + 256];
890     wchar_t psz_wdir[MAX_PATH];
891 #endif
892     char psz_path[MAX_PATH + 256];
893     WIN32_FIND_DATA finddata;
894     HANDLE handle;
895     int rc;
896 #else
897     int    i_dirlen;
898     DIR *  dir;
899     struct dirent * file;
900 #endif
901     char * psz_file;
902
903     if( p_this->p_libvlc->b_die || i_maxdepth < 0 )
904     {
905         return;
906     }
907
908 #if defined( UNDER_CE ) || defined( _MSC_VER )
909 #ifdef UNDER_CE
910     MultiByteToWideChar( CP_ACP, 0, psz_dir, -1, psz_wdir, MAX_PATH );
911
912     rc = GetFileAttributes( psz_wdir );
913     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
914
915     /* Parse all files in the directory */
916     swprintf( psz_wpath, L"%ls\\*", psz_wdir );
917 #else
918     rc = GetFileAttributes( psz_dir );
919     if( rc<0 || !(rc&FILE_ATTRIBUTE_DIRECTORY) ) return; /* Not a directory */
920 #endif
921
922     /* Parse all files in the directory */
923     sprintf( psz_path, "%s\\*", psz_dir );
924
925 #ifdef UNDER_CE
926     handle = FindFirstFile( psz_wpath, &finddata );
927 #else
928     handle = FindFirstFile( psz_path, &finddata );
929 #endif
930     if( handle == INVALID_HANDLE_VALUE )
931     {
932         /* Empty directory */
933         return;
934     }
935
936     /* Parse the directory and try to load all files it contains. */
937     do
938     {
939 #ifdef UNDER_CE
940         unsigned int i_len = wcslen( finddata.cFileName );
941         swprintf( psz_wpath, L"%ls\\%ls", psz_wdir, finddata.cFileName );
942         sprintf( psz_path, "%s\\%ls", psz_dir, finddata.cFileName );
943 #else
944         unsigned int i_len = strlen( finddata.cFileName );
945         sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
946 #endif
947
948         /* Skip ".", ".." */
949         if( !*finddata.cFileName || !strcmp( finddata.cFileName, "." )
950          || !strcmp( finddata.cFileName, ".." ) )
951         {
952             if( !FindNextFile( handle, &finddata ) ) break;
953             continue;
954         }
955
956 #ifdef UNDER_CE
957         if( GetFileAttributes( psz_wpath ) & FILE_ATTRIBUTE_DIRECTORY )
958 #else
959         if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
960 #endif
961         {
962             AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
963         }
964         else if( i_len > strlen( LIBEXT )
965                   /* We only load files ending with LIBEXT */
966                   && !strncasecmp( psz_path + strlen( psz_path)
967                                    - strlen( LIBEXT ),
968                                    LIBEXT, strlen( LIBEXT ) ) )
969         {
970             WIN32_FILE_ATTRIBUTE_DATA attrbuf;
971             int64_t i_time = 0, i_size = 0;
972
973 #ifdef UNDER_CE
974             if( GetFileAttributesEx( psz_wpath, GetFileExInfoStandard,
975                                      &attrbuf ) )
976 #else
977             if( GetFileAttributesEx( psz_path, GetFileExInfoStandard,
978                                      &attrbuf ) )
979 #endif
980             {
981                 i_time = attrbuf.ftLastWriteTime.dwHighDateTime;
982                 i_time <<= 32;
983                 i_time |= attrbuf.ftLastWriteTime.dwLowDateTime;
984                 i_size = attrbuf.nFileSizeHigh;
985                 i_size <<= 32;
986                 i_size |= attrbuf.nFileSizeLow;
987             }
988             psz_file = psz_path;
989
990             AllocatePluginFile( p_this, psz_file, i_time, i_size );
991         }
992     }
993     while( !p_this->p_libvlc->b_die && FindNextFile( handle, &finddata ) );
994
995     /* Close the directory */
996     FindClose( handle );
997
998 #else
999     dir = opendir( psz_dir );
1000     if( !dir )
1001     {
1002         return;
1003     }
1004
1005     i_dirlen = strlen( psz_dir );
1006
1007     /* Parse the directory and try to load all files it contains. */
1008     while( !p_this->p_libvlc->b_die && (file = readdir( dir )) )
1009     {
1010         struct stat statbuf;
1011         unsigned int i_len;
1012         int i_stat;
1013
1014         /* Skip ".", ".." */
1015         if( !*file->d_name || !strcmp( file->d_name, "." )
1016          || !strcmp( file->d_name, ".." ) )
1017         {
1018             continue;
1019         }
1020
1021         i_len = strlen( file->d_name );
1022         psz_file = malloc( i_dirlen + 1 + i_len + 1 );
1023         sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
1024
1025         i_stat = stat( psz_file, &statbuf );
1026         if( !i_stat && statbuf.st_mode & S_IFDIR )
1027         {
1028             AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
1029         }
1030         else if( i_len > strlen( LIBEXT )
1031                   /* We only load files ending with LIBEXT */
1032                   && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
1033                                    LIBEXT, strlen( LIBEXT ) ) )
1034         {
1035             int64_t i_time = 0, i_size = 0;
1036
1037             if( !i_stat )
1038             {
1039                 i_time = statbuf.st_mtime;
1040                 i_size = statbuf.st_size;
1041             }
1042
1043             AllocatePluginFile( p_this, psz_file, i_time, i_size );
1044         }
1045
1046         free( psz_file );
1047     }
1048
1049     /* Close the directory */
1050     closedir( dir );
1051
1052 #endif
1053 }
1054
1055 /*****************************************************************************
1056  * AllocatePluginFile: load a module into memory and initialize it.
1057  *****************************************************************************
1058  * This function loads a dynamically loadable module and allocates a structure
1059  * for its information data. The module can then be handled by module_Need
1060  * and module_Unneed. It can be removed by DeleteModule.
1061  *****************************************************************************/
1062 static int AllocatePluginFile( vlc_object_t * p_this, char * psz_file,
1063                                int64_t i_file_time, int64_t i_file_size )
1064 {
1065     module_t * p_module = NULL;
1066     module_cache_t *p_cache_entry = NULL;
1067
1068     /*
1069      * Check our plugins cache first then load plugin if needed
1070      */
1071     p_cache_entry =
1072         CacheFind( p_this, psz_file, i_file_time, i_file_size );
1073
1074     if( !p_cache_entry )
1075     {
1076         p_module = AllocatePlugin( p_this, psz_file );
1077     }
1078     else
1079     {
1080         /* If junk dll, don't try to load it */
1081         if( p_cache_entry->b_junk )
1082         {
1083             p_module = NULL;
1084         }
1085         else
1086         {
1087             module_config_t *p_item = NULL, *p_end = NULL;
1088
1089             p_module = p_cache_entry->p_module;
1090             p_module->b_loaded = VLC_FALSE;
1091
1092             /* For now we force loading if the module's config contains
1093              * callbacks or actions.
1094              * Could be optimized by adding an API call.*/
1095             for( p_item = p_module->p_config, p_end = p_item + p_module->confsize;
1096                  p_item < p_end; p_item++ )
1097             {
1098                 if( p_item->pf_callback || p_item->i_action )
1099                 {
1100                     p_module = AllocatePlugin( p_this, psz_file );
1101                     break;
1102                 }
1103             }
1104             if( p_module == p_cache_entry->p_module )
1105                 p_cache_entry->b_used = VLC_TRUE;
1106         }
1107     }
1108
1109     if( p_module )
1110     {
1111         libvlc_global_data_t *p_libvlc_global = vlc_global();
1112
1113         /* Everything worked fine !
1114          * The module is ready to be added to the list. */
1115         p_module->b_builtin = VLC_FALSE;
1116
1117         /* msg_Dbg( p_this, "plugin \"%s\", %s",
1118                     p_module->psz_object_name, p_module->psz_longname ); */
1119
1120         vlc_object_attach( p_module, p_libvlc_global->p_module_bank );
1121
1122         if( !p_libvlc_global->p_module_bank->b_cache )
1123             return 0;
1124
1125 #define p_bank p_libvlc_global->p_module_bank
1126         /* Add entry to cache */
1127         p_bank->pp_cache =
1128             realloc( p_bank->pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
1129         p_bank->pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
1130         if( !p_bank->pp_cache[p_bank->i_cache] )
1131             return -1;
1132         p_bank->pp_cache[p_bank->i_cache]->psz_file = strdup( psz_file );
1133         p_bank->pp_cache[p_bank->i_cache]->i_time = i_file_time;
1134         p_bank->pp_cache[p_bank->i_cache]->i_size = i_file_size;
1135         p_bank->pp_cache[p_bank->i_cache]->b_junk = p_module ? 0 : 1;
1136         p_bank->pp_cache[p_bank->i_cache]->b_used = VLC_TRUE;
1137         p_bank->pp_cache[p_bank->i_cache]->p_module = p_module;
1138         p_bank->i_cache++;
1139     }
1140
1141     return p_module ? 0 : -1;
1142 }
1143
1144 /*****************************************************************************
1145  * AllocatePlugin: load a module into memory and initialize it.
1146  *****************************************************************************
1147  * This function loads a dynamically loadable module and allocates a structure
1148  * for its information data. The module can then be handled by module_Need
1149  * and module_Unneed. It can be removed by DeleteModule.
1150  *****************************************************************************/
1151 static module_t * AllocatePlugin( vlc_object_t * p_this, char * psz_file )
1152 {
1153     module_t * p_module = NULL;
1154     module_handle_t handle;
1155
1156     if( module_Load( p_this, psz_file, &handle ) )
1157         return NULL;
1158
1159     /* Now that we have successfully loaded the module, we can
1160      * allocate a structure for it */
1161     p_module = vlc_module_create( p_this );
1162     if( p_module == NULL )
1163     {
1164         msg_Err( p_this, "out of memory" );
1165         module_Unload( handle );
1166         return NULL;
1167     }
1168
1169     /* We need to fill these since they may be needed by module_Call() */
1170     p_module->psz_filename = psz_file;
1171     p_module->handle = handle;
1172     p_module->b_loaded = VLC_TRUE;
1173
1174     /* Initialize the module: fill p_module, default config */
1175     if( module_Call( p_module ) != 0 )
1176     {
1177         /* We couldn't call module_init() */
1178         vlc_object_destroy( p_module );
1179         module_Unload( handle );
1180         return NULL;
1181     }
1182
1183     DupModule( p_module );
1184     p_module->psz_filename = strdup( p_module->psz_filename );
1185
1186     /* Everything worked fine ! The module is ready to be added to the list. */
1187     p_module->b_builtin = VLC_FALSE;
1188
1189     return p_module;
1190 }
1191
1192 /*****************************************************************************
1193  * DupModule: make a plugin module standalone.
1194  *****************************************************************************
1195  * This function duplicates all strings in the module, so that the dynamic
1196  * object can be unloaded. It acts recursively on submodules.
1197  *****************************************************************************/
1198 static void DupModule( module_t *p_module )
1199 {
1200     const char **pp_shortcut;
1201     int i_submodule;
1202
1203     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1204     {
1205         *pp_shortcut = strdup( *pp_shortcut );
1206     }
1207
1208     /* We strdup() these entries so that they are still valid when the
1209      * module is unloaded. */
1210     p_module->psz_object_name = strdup( p_module->psz_object_name );
1211     p_module->psz_capability = strdup( p_module->psz_capability );
1212     p_module->psz_shortname = p_module->psz_shortname ?
1213                                  strdup( p_module->psz_shortname ) : NULL;
1214     p_module->psz_longname = strdup( p_module->psz_longname );
1215     p_module->psz_help = p_module->psz_help ? strdup( p_module->psz_help )
1216                                             : NULL;
1217
1218     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1219     {
1220         DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1221     }
1222 }
1223
1224 /*****************************************************************************
1225  * UndupModule: free a duplicated module.
1226  *****************************************************************************
1227  * This function frees the allocations done in DupModule().
1228  *****************************************************************************/
1229 static void UndupModule( module_t *p_module )
1230 {
1231     const char **pp_shortcut;
1232     int i_submodule;
1233
1234     for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
1235     {
1236         UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
1237     }
1238
1239     for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
1240     {
1241         free( (void*)*pp_shortcut );
1242     }
1243
1244     free( (void*)p_module->psz_object_name );
1245     free( p_module->psz_capability );
1246     free( (void*)p_module->psz_shortname );
1247     free( (void*)p_module->psz_longname );
1248     free( (void*)p_module->psz_help );
1249 }
1250
1251 #endif /* HAVE_DYNAMIC_PLUGINS */
1252
1253 /*****************************************************************************
1254  * AllocateBuiltinModule: initialize a builtin module.
1255  *****************************************************************************
1256  * This function registers a builtin module and allocates a structure
1257  * for its information data. The module can then be handled by module_Need
1258  * and module_Unneed. It can be removed by DeleteModule.
1259  *****************************************************************************/
1260 static int AllocateBuiltinModule( vlc_object_t * p_this,
1261                                   int ( *pf_entry ) ( module_t * ) )
1262 {
1263     module_t * p_module;
1264
1265     /* Now that we have successfully loaded the module, we can
1266      * allocate a structure for it */
1267     p_module = vlc_module_create( p_this );
1268     if( p_module == NULL )
1269     {
1270         msg_Err( p_this, "out of memory" );
1271         return -1;
1272     }
1273
1274     /* Initialize the module : fill p_module->psz_object_name, etc. */
1275     if( pf_entry( p_module ) != 0 )
1276     {
1277         /* With a well-written module we shouldn't have to print an
1278          * additional error message here, but just make sure. */
1279         msg_Err( p_this, "failed calling entry point in builtin module" );
1280         vlc_object_destroy( p_module );
1281         return -1;
1282     }
1283
1284     /* Everything worked fine ! The module is ready to be added to the list. */
1285     p_module->b_builtin = VLC_TRUE;
1286
1287     /* msg_Dbg( p_this, "builtin \"%s\", %s",
1288                 p_module->psz_object_name, p_module->psz_longname ); */
1289
1290     vlc_object_attach( p_module, vlc_global()->p_module_bank );
1291
1292     return 0;
1293 }
1294
1295 /*****************************************************************************
1296  * DeleteModule: delete a module and its structure.
1297  *****************************************************************************
1298  * This function can only be called if the module isn't being used.
1299  *****************************************************************************/
1300 static int DeleteModule( module_t * p_module, vlc_bool_t b_detach )
1301 {
1302     if( !p_module ) return VLC_EGENERIC;
1303     if( b_detach )
1304         vlc_object_detach( p_module );
1305
1306     /* We free the structures that we strdup()ed in Allocate*Module(). */
1307 #ifdef HAVE_DYNAMIC_PLUGINS
1308     if( !p_module->b_builtin )
1309     {
1310         if( p_module->b_loaded && p_module->b_unloadable )
1311         {
1312             module_Unload( p_module->handle );
1313         }
1314         UndupModule( p_module );
1315         free( p_module->psz_filename );
1316     }
1317 #endif
1318
1319     /* Free and detach the object's children */
1320     while( p_module->i_children )
1321     {
1322         vlc_object_t *p_this = p_module->pp_children[0];
1323         vlc_object_detach( p_this );
1324         vlc_object_destroy( p_this );
1325     }
1326
1327     config_Free( p_module );
1328     vlc_object_destroy( p_module );
1329     p_module = NULL;
1330     return 0;
1331 }