modules.c 44 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1
/*****************************************************************************
2
 * modules.c : Builtin and plugin modules management functions
Sam Hocevar's avatar
 
Sam Hocevar committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Sam Hocevar's avatar
 
Sam Hocevar committed
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
Sam Hocevar's avatar
   
Sam Hocevar committed
8
 *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
Sam Hocevar's avatar
   
Sam Hocevar committed
9
 *          Hans-Peter Jansen <hpj@urpla.net>
10
 *          Gildas Bazin <gbazin@videolan.org>
Sam Hocevar's avatar
 
Sam Hocevar committed
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
 
Sam Hocevar committed
25
26
 *****************************************************************************/

27
#include <vlc/vlc.h>
28
#include "libvlc.h"
29

Sam Hocevar's avatar
   
Sam Hocevar committed
30
31
32
33
34
35
/* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
 * is set to 64. Don't try to be cleverer. */
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
36
37
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
   
Sam Hocevar committed
38
#include <string.h>                                              /* strdup() */
Sam Hocevar's avatar
   
Sam Hocevar committed
39

40
41
42
43
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
#endif

44
45
46
47
48
49
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
50
51
52
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
53

54
55
56
#if !defined(HAVE_DYNAMIC_PLUGINS)
    /* no support for plugins */
#elif defined(HAVE_DL_DYLD)
Sam Hocevar's avatar
Sam Hocevar committed
57
58
59
60
61
62
63
64
#   if defined(HAVE_MACH_O_DYLD_H)
#       include <mach-o/dyld.h>
#   endif
#elif defined(HAVE_DL_BEOS)
#   if defined(HAVE_IMAGE_H)
#       include <image.h>
#   endif
#elif defined(HAVE_DL_WINDOWS)
Sam Hocevar's avatar
Sam Hocevar committed
65
#   include <windows.h>
Sam Hocevar's avatar
Sam Hocevar committed
66
67
#elif defined(HAVE_DL_DLOPEN)
#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
Sam Hocevar's avatar
Sam Hocevar committed
68
69
#       include <dlfcn.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
70
#   if defined(HAVE_SYS_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
71
72
#       include <sys/dl.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
73
74
#elif defined(HAVE_DL_SHL_LOAD)
#   if defined(HAVE_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
75
76
#       include <dl.h>
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
77
78
#endif

79
#include "config/config.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
80

zorglub's avatar
zorglub committed
81
#include "vlc_charset.h"
gbazin's avatar
   
gbazin committed
82

83
84
#include "modules/modules.h"
#include "modules/builtin.h"
zorglub's avatar
zorglub committed
85

Sam Hocevar's avatar
   
Sam Hocevar committed
86
87
88
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
89
#ifdef HAVE_DYNAMIC_PLUGINS
90
91
92
93
static void AllocateAllPlugins  ( vlc_object_t * );
static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
static module_t * AllocatePlugin( vlc_object_t *, char * );
Sam Hocevar's avatar
   
Sam Hocevar committed
94
#endif
95
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
96
static int  DeleteModule ( module_t *, vlc_bool_t );
Sam Hocevar's avatar
   
Sam Hocevar committed
97
#ifdef HAVE_DYNAMIC_PLUGINS
98
99
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
100
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
101

102
103
static void module_LoadMain( vlc_object_t *p_this );

Sam Hocevar's avatar
   
Sam Hocevar committed
104
105
106
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
gbazin's avatar
   
gbazin committed
107
108
 * This function creates a module bank structure which will be filled later
 * on with all the modules found.
Sam Hocevar's avatar
   
Sam Hocevar committed
109
 *****************************************************************************/
110
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
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
117
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    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
125
126
        return;
    }
    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
131
    if( !p_bank )
        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;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
135
    p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
136
137
    p_bank->b_cache = p_bank->b_cache_dirty =
        p_bank->b_cache_delete = VLC_FALSE;
138
139

    /* Everything worked, attach the object */
140
141
    p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_libvlc_global );
142

143
    module_LoadMain( p_this );
gbazin's avatar
   
gbazin committed
144
145
}

Sam Hocevar's avatar
   
Sam Hocevar committed
146

Sam Hocevar's avatar
 
Sam Hocevar committed
147
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
148
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
149
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
150
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
151
152
 * bank in case of success.
 *****************************************************************************/
153
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
154
{
155
    module_t * p_next = NULL;
156
    vlc_value_t lockval;
157
    libvlc_global_data_t *p_libvlc_global = vlc_global();
158

159
160
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    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
167
        return;
    }
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
173
174
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
175
    var_Destroy( p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
   
Sam Hocevar committed
176

177
178
    config_AutoSaveConfigFile( p_this );

179
#ifdef HAVE_DYNAMIC_PLUGINS
180
# define p_bank p_libvlc_global->p_module_bank
181
182
183
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
184
185
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
186
187
            DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module,
                          p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
188
189
190
191
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
            p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
        }
192
    }
193
    if( p_bank->pp_loaded_cache )
194
    {
195
        free( p_bank->pp_loaded_cache );
196
197
        p_bank->pp_loaded_cache = NULL;
    }
198
199
200
201
    while( p_bank->i_cache-- )
    {
        free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
        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
208
        p_bank->pp_cache = NULL;
    }
209
# undef p_bank
210
211
#endif

212
    vlc_object_detach( p_libvlc_global->p_module_bank );
213

214
    while( p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
   
Sam Hocevar committed
215
    {
216
        p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
217

218
        if( DeleteModule( p_next, VLC_TRUE ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
219
220
        {
            /* Module deletion failed */
221
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
222
                     p_next->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
223
224

            /* We just free the module by hand. Niahahahahaha. */
225
226
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
   
Sam Hocevar committed
227
228
229
        }
    }

230
231
    vlc_object_destroy( p_libvlc_global->p_module_bank );
    p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
232
233
234
}

/*****************************************************************************
235
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
236
 *****************************************************************************
237
238
239
240
 * This function fills the module bank structure with the main module infos.
 * This is very useful as it will allow us to consider the main program just
 * as another module, and for instance the configuration options of main will
 * be available in the module bank structure just as for every other module.
Sam Hocevar's avatar
 
Sam Hocevar committed
241
 *****************************************************************************/
242
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
243
{
244
    vlc_value_t lockval;
245
    libvlc_global_data_t *p_libvlc_global = vlc_global();
246

247
248
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    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
255
        return;
    }
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
265
266
267
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
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
274
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    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
281
        return;
    }
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
287
288
289
290
291
292
293
294
    msg_Dbg( p_this, "checking builtin modules" );
    ALLOCATE_ALL_BUILTINS();
}

/*****************************************************************************
 * module_LoadPlugins: load all plugin modules we can find.
 *****************************************************************************
 * This function fills the module bank structure with the plugin modules.
 *****************************************************************************/
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
302
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    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
309
        return;
    }
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
320
    if( p_libvlc_global->p_module_bank->b_cache ||
        p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
321

322
323
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
324
325
}

326
327
328
329
330
331
332
333
/*****************************************************************************
 * module_IsCapable: checks whether a module implements a capability.
 *****************************************************************************/
vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
{
    return !strcmp( m->psz_capability, cap );
}

334
335
336
337
338
339
340
341
342
343
344
/*****************************************************************************
 * module_GetObjName: internal name of a module.
 *****************************************************************************/
const char *module_GetObjName( const module_t *m )
{
    return m->psz_object_name;
}

/*****************************************************************************
 * module_GetName: human-friendly name of a module.
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
345
const char *module_GetName( const module_t *m, vlc_bool_t long_name )
346
347
348
{
    if( long_name && ( m->psz_longname != NULL) )
        return m->psz_longname;
349
 
350
351
352
    return m->psz_shortname ?: m->psz_object_name;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
353
354
355
356
357
const char *module_GetHelp( const module_t *m )
{
    return m->psz_help;
}

Sam Hocevar's avatar
 
Sam Hocevar committed
358
/*****************************************************************************
359
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
360
 *****************************************************************************
361
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
362
 *****************************************************************************/
363
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
gbazin's avatar
   
gbazin committed
364
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
365
{
366
    typedef struct module_list_t module_list_t;
367

368
    struct module_list_t
Sam Hocevar's avatar
   
Sam Hocevar committed
369
    {
370
        module_t *p_module;
371
        int i_score;
372
        vlc_bool_t b_force;
373
374
375
376
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
377
    vlc_list_t *p_all;
Sam Hocevar's avatar
   
Sam Hocevar committed
378

gbazin's avatar
   
gbazin committed
379
    int i_which_module, i_index = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
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;
Sam Hocevar's avatar
 
Sam Hocevar committed
386

387

388
389
390
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
gbazin's avatar
   
gbazin committed
391
392
393
394
        vlc_value_t val;
        var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
        var_Get( p_this, psz_name + 1, &val );
        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 )
Sam Hocevar's avatar
 
Sam Hocevar committed
400
    {
gbazin's avatar
   
gbazin committed
401
        char *psz_parser, *psz_last_shortcut;
402

403
404
405
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
406
            free( psz_var );
407
408
409
            return NULL;
        }

410
        i_shortcuts++;
gbazin's avatar
   
gbazin committed
411
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
412
413

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
414
        {
415
            if( *psz_parser == ',' )
Sam Hocevar's avatar
   
Sam Hocevar committed
416
            {
417
418
                 *psz_parser = '\0';
                 i_shortcuts++;
gbazin's avatar
   
gbazin committed
419
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
420
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
421
        }
gbazin's avatar
   
gbazin committed
422
423
424
425

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
426
427
428
429
430
431
432
433
434
435
            if( !strcmp(psz_last_shortcut, "none") )
            {
                b_strict = VLC_TRUE;
                i_shortcuts--;
            }
            else if( !strcmp(psz_last_shortcut, "any") )
            {
                b_strict = VLC_FALSE;
                i_shortcuts--;
            }
gbazin's avatar
   
gbazin committed
436
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
437
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
438

Sam Hocevar's avatar
   
Sam Hocevar committed
439
    /* Sort the modules and test them */
440
441
    p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
442
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
443
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
   
Sam Hocevar committed
444

Sam Hocevar's avatar
   
Sam Hocevar committed
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++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
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 ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
454
        {
455
456
457
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
458
459
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
460
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
461
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
   
Sam Hocevar committed
462
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
463
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
464
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
465

466
        /* If we required a shortcut, check this plugin provides it. */
467
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
468
        {
gbazin's avatar
   
gbazin committed
469
            vlc_bool_t b_trash;
470
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
   
Sam Hocevar committed
471

472
            /* Let's drop modules with a <= 0 score (unless they are
gbazin's avatar
   
gbazin committed
473
             * explicitly requested) */
474
            b_trash = p_module->i_score <= 0;
gbazin's avatar
   
gbazin committed
475

476
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
   
Sam Hocevar committed
477
            {
478
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
479
                {
480
481
482
483
484
                    char *c;
                    if( ( c = strchr( psz_name, '@' ) )
                        ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
                                        c-psz_name )
                        : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
gbazin's avatar
   
gbazin committed
485
486
                    {
                        /* Found it */
487
488
                        if( c && c[1] )
                            psz_alias = c+1;
gbazin's avatar
   
gbazin committed
489
                        i_shortcut_bonus = i_short * 10000;
490
                        goto found_shortcut;
gbazin's avatar
   
gbazin committed
491
                    }
492
493
                }

494
                /* Go to the next shortcut... This is so lame! */
495
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
496
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
497

498
499
500
            /* If we are in "strict" mode and we couldn't
             * find the module in the list of provided shortcuts,
             * then kick the bastard out of here!!! */
501
            if( b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
502
                continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
503
        }
504
505
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
506
507
508
        {
            continue;
        }
509

510
511
found_shortcut:

Sam Hocevar's avatar
   
Sam Hocevar committed
512
513
        /* Store this new module */
        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;
Sam Hocevar's avatar
   
Sam Hocevar committed
516

Sam Hocevar's avatar
   
Sam Hocevar committed
517
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
   
Sam Hocevar committed
518
519
        if( i_index == 0 )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
520
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
521
522
523
524
525
526
527
            p_first = p_list;
        }
        else
        {
            /* Ok, so at school you learned that quicksort is quick, and
             * bubble sort sucks raw eggs. But that's when dealing with
             * thousands of items. Here we have barely 50. */
528
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
   
Sam Hocevar committed
529

530
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
531
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
532
533
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
   
Sam Hocevar committed
534
535
            }
            else
536
            {
537
538
                while( p_newlist->p_next != NULL &&
                    p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
539
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
540
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
541
542
                }

Sam Hocevar's avatar
   
Sam Hocevar committed
543
544
545
                p_list[ i_index ].p_next = p_newlist->p_next;
                p_newlist->p_next = &p_list[ i_index ];
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
546
547
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
548
549
        i_index++;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
550

zorglub's avatar
zorglub committed
551
552
    msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
                                            i_index, i_index == 1 ? "" : "s" );
553

554
    /* Lock all candidate modules */
555
556
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
557
    {
558
        vlc_object_yield( p_tmp->p_module );
559
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
560
561
    }

562
    /* We can release the list, interesting modules were yielded */
563
    vlc_list_release( p_all );
Sam Hocevar's avatar
   
Sam Hocevar committed
564

Sam Hocevar's avatar
   
Sam Hocevar committed
565
    /* Parse the linked list and use the first successful module */
566
567
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
568
    {
569
570
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
571
572
573
574
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

575
576
577
578
579
580
581
582
        if( !p_module->b_builtin && !p_module->b_loaded )
        {
            module_t *p_new_module =
                AllocatePlugin( p_this, p_module->psz_filename );
            if( p_new_module )
            {
                CacheMerge( p_this, p_module, p_new_module );
                vlc_object_attach( p_new_module, p_module );
583
                DeleteModule( p_new_module, VLC_TRUE );
584
585
586
587
            }
        }
#endif

588
        p_this->b_force = p_tmp->b_force;
589
590
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
   
Sam Hocevar committed
591
592
        {
            break;
593
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
594

595
        vlc_object_release( p_tmp->p_module );
596
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
597
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
598

Sam Hocevar's avatar
   
Sam Hocevar committed
599
    /* Store the locked module value */
600
    if( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
601
    {
602
603
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
604
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
605
606
607
608
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
609

Sam Hocevar's avatar
   
Sam Hocevar committed
610
    /* Unlock the remaining modules */
611
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
612
    {
613
        vlc_object_release( p_tmp->p_module );
614
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
615
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
616

Sam Hocevar's avatar
   
Sam Hocevar committed
617
    free( p_list );
618
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
619

Sam Hocevar's avatar
   
Sam Hocevar committed
620
    if( p_module != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
621
    {
622
        msg_Dbg( p_this, "using %s module \"%s\"",
623
                 psz_capability, p_module->psz_object_name );
624
625
626
    }
    else if( p_first == NULL )
    {
627
628
629
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
630
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
631
632
        }
        else
zorglub's avatar
zorglub committed
633
        {
634
635
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
636
637
638

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
639
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
640
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
641
642
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
643
644
        msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
Sam Hocevar's avatar
   
Sam Hocevar committed
645
    }
646
647
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
   
Sam Hocevar committed
648

649
650
    if( p_module && !p_this->psz_object_name )
    {
651
652
653
        /* This assumes that p_this is the object which will be using the
         * module. That's not always the case ... but it is in most cases.
         */
654
655
656
657
658
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
659

660
661
    free( psz_shortcuts );
    free( psz_var );
662

Sam Hocevar's avatar
   
Sam Hocevar committed
663
    /* Don't forget that the module is still locked */
664
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
665
666
667
668
669
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
670
671
 * This function must be called by the thread that called module_Need, to
 * decrease the reference count and allow for hiding of modules.
Sam Hocevar's avatar
 
Sam Hocevar committed
672
 *****************************************************************************/
673
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
674
{
675
676
677
678
679
680
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

681
    msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
682

683
    vlc_object_release( p_module );
684
685

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
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
694
695
696
697
{
    vlc_list_t *p_list;
    int i;
    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    for( i = 0 ; i < p_list->i_count; i++)
    {
698
699
        module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
        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
709
710
711
    vlc_list_release( p_list );
    return NULL;
}

712
713

/*****************************************************************************
714
 * module_Put: release a module_t pointer from module_Find().
715
716
717
718
719
720
721
 *****************************************************************************/
void module_Put ( module_t *module )
{
    vlc_object_release ( module );
}


722
723
724
725
726
727
728
/*****************************************************************************
 * module_Exists: tell if a module exists.
 *****************************************************************************
 * This function is a boolean function that tells if a module exist or not.
 *****************************************************************************/
vlc_bool_t __module_Exists(  vlc_object_t *p_this, const char * psz_name )
{
729
    module_t *p_module = __module_Find( p_this, psz_name );
730
731
    if( p_module )
    {
732
        module_Put( p_module );
733
734
735
736
737
738
        return VLC_TRUE;
    }
    else
    {
        return VLC_FALSE;
    }
739
740
}

741
742
743
744
745
746
/*****************************************************************************
 * module_GetModuleNamesForCapability: Return a NULL terminated array with the
 * names of the modules that have a certain capability.
 * Free after uses both the string and the table.
 *****************************************************************************/
char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
747
748
                                               const char * psz_capability,
                                               char ***pppsz_longname )
749
750
{
    vlc_list_t *p_list;
751
    int i, j, count = 0;
752
753
754
755
756
757
758
759
760
761
762
    char ** psz_ret;

    /* Do it in two passes */
    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    for( i = 0 ; i < p_list->i_count; i++)
    {
        module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
        const char *psz_module_capability = p_module->psz_capability;
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
            count++;
    }
763
    psz_ret = malloc( sizeof(char*) * (count+1) );
764
765
    if( pppsz_longname )
        *pppsz_longname = malloc( sizeof(char*) * (count+1) );
766
    j = 0;
767
768
769
770
771
    for( i = 0 ; i < p_list->i_count; i++)
    {
        module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
        const char *psz_module_capability = p_module->psz_capability;
        if( psz_module_capability && !strcmp( psz_module_capability, psz_capability ) )
772
773
774
775
776
777
778
779
780
        {
            int k = -1; /* hack to handle submodules properly */
            if( p_module->b_submodule )
            {
                while( p_module->pp_shortcuts[++k] != NULL );
                k--;
            }
            psz_ret[j] = strdup( k>=0?p_module->pp_shortcuts[k]
                                     :p_module->psz_object_name );
781
782
            if( pppsz_longname )
                (*pppsz_longname)[j] = strdup( module_GetName( p_module, VLC_TRUE ) );
783
784
            j++;
        }
785
786
787
788
    }
    psz_ret[count] = NULL;

    vlc_list_release( p_list );
789

790
791
792
    return psz_ret;
}

793

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
module_config_t *module_GetConfig (const module_t *module, unsigned *restrict psize)
{
    unsigned size = module->confsize;

    assert (psize != NULL);
    *psize = size;

    module_config_t *config = malloc (size * sizeof (*config));
    if (config)
        memcpy (config, module->p_config, size * sizeof (*config));

    return config;
}

void module_PutConfig (module_config_t *config)
{
    free (config);
}

Sam Hocevar's avatar
 
Sam Hocevar committed
813
814
815
816
/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

Sam Hocevar's avatar
   
Sam Hocevar committed
817
818
819
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
820
#ifdef HAVE_DYNAMIC_PLUGINS
821
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
822
{
823
    /* Yes, there are two NULLs because we replace one with "plugin-path". */
824
#if defined( WIN32 ) || defined( UNDER_CE )
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
825
    const char *path[] = { "modules", "", "plugins", NULL, NULL };
826
#else
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
827
    const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
828
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
829

Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
830
    const char *const *ppsz_path;
Sam Hocevar's avatar
   
Sam Hocevar committed
831

832
    /* If the user provided a plugin path, we add it to the list */
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
833
834
    char *userpath = config_GetPsz( p_this, "plugin-path" );
    path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
835

Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
836
    for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
Sam Hocevar's avatar
   
Sam Hocevar committed
837
    {