modules.c 73.2 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 "modules/configuration.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
80
#include "libvlc.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
81

zorglub's avatar
zorglub committed
82
#include "vlc_interface.h"
83
84
#include "vlc_playlist.h"

zorglub's avatar
zorglub committed
85
86
87
#include "vlc_stream.h"
#include "vlc_access.h"
#include "vlc_demux.h"
Laurent Aimar's avatar
Laurent Aimar committed
88
#include "vlc_codec.h"
zorglub's avatar
zorglub committed
89

zorglub's avatar
zorglub committed
90
91
#include "vlc_vout.h"
#include "vlc_vout_synchro.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
92

zorglub's avatar
zorglub committed
93
#include "vlc_aout.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
94

zorglub's avatar
zorglub committed
95
#include "vlc_sout.h"
sigmunau's avatar
sigmunau committed
96
#include "vlc_httpd.h"
97
#include "vlc_acl.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
98
#include "vlc_tls.h"
99
#include "vlc_md5.h"
gbazin's avatar
gbazin committed
100
#include "vlc_xml.h"
Christophe Mutricy's avatar
Christophe Mutricy committed
101
#include "vlc_url.h"
102

gbazin's avatar
   
gbazin committed
103
#include "iso_lang.h"
zorglub's avatar
zorglub committed
104
#include "vlc_charset.h"
gbazin's avatar
   
gbazin committed
105

106
107
#include "vlc_block.h"

108
109
#include "vlc_vlm.h"

zorglub's avatar
zorglub committed
110
#include "vlc_image.h"
111
#include "vlc_osd.h"
zorglub's avatar
zorglub committed
112

113
#include "vlc_update.h"
114
#include "vlc_strings.h"
zorglub's avatar
zorglub committed
115
#include "vlc_streaming.h"
116

117
118
#include "modules/modules.h"
#include "modules/builtin.h"
zorglub's avatar
zorglub committed
119
120

#include "vlc_network.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
121

122
#if defined( WIN32 ) || defined( UNDER_CE )
123
    /* Avoid name collisions */
Steve Lhomme's avatar
Steve Lhomme committed
124
#   define LoadModule(a,b,c) LoadVlcModule(a,b,c)
125
126
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
127
128
129
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
130
#ifdef HAVE_DYNAMIC_PLUGINS
131
132
133
134
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
135
#endif
136
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
137
static int  DeleteModule ( module_t *, vlc_bool_t );
Sam Hocevar's avatar
   
Sam Hocevar committed
138
#ifdef HAVE_DYNAMIC_PLUGINS
139
140
141
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
static int    CallEntry        ( module_t * );
142
static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
143
144
static void   CloseModule      ( module_handle_t );
static void * GetSymbol        ( module_handle_t, const char * );
145
static void   CacheLoad        ( vlc_object_t * );
146
static int    CacheLoadConfig  ( module_t *, FILE * );
147
148
static void   CacheSave        ( vlc_object_t * );
static void   CacheSaveConfig  ( module_t *, FILE * );
Sam Hocevar's avatar
Sam Hocevar committed
149
static char * CacheName        ( void );
150
151
152
static void   CacheMerge       ( vlc_object_t *, module_t *, module_t * );
static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );

Sam Hocevar's avatar
Sam Hocevar committed
153
#if defined(HAVE_DL_WINDOWS)
154
155
static char * GetWindowsError  ( void );
#endif
156
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
157

158
159
static void module_LoadMain( vlc_object_t *p_this );

160
161
/* Sub-version number
 * (only used to avoid breakage in dev version when cache structure changes) */
162
#define CACHE_SUBVERSION_NUM 3
163

Sam Hocevar's avatar
   
Sam Hocevar committed
164
165
166
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
gbazin's avatar
   
gbazin committed
167
168
 * 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
169
 *****************************************************************************/
170
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
171
{
172
    module_bank_t *p_bank = NULL;
173
    vlc_value_t  lockval;
174
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
175

176
177
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
178
    vlc_mutex_lock( lockval.p_address );
179
    if( p_libvlc_global->p_module_bank )
180
    {
181
        p_libvlc_global->p_module_bank->i_usage++;
182
        vlc_mutex_unlock( lockval.p_address );
183
        var_Destroy( p_libvlc_global, "libvlc" );
184
185
186
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
187
    var_Destroy( p_libvlc_global, "libvlc" );
188
189

    p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
190
191
    if( !p_bank )
        return;
192
    p_bank->psz_object_name = "module bank";
193
    p_bank->i_usage = 1;
194
    p_bank->i_cache = p_bank->i_loaded_cache = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
    p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
196
197
    p_bank->b_cache = p_bank->b_cache_dirty =
        p_bank->b_cache_delete = VLC_FALSE;
198
199

    /* Everything worked, attach the object */
200
201
    p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_libvlc_global );
202

203
    module_LoadMain( p_this );
gbazin's avatar
   
gbazin committed
204
205
}

Sam Hocevar's avatar
   
Sam Hocevar committed
206

Sam Hocevar's avatar
 
Sam Hocevar committed
207
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
208
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
209
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
210
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
211
212
 * bank in case of success.
 *****************************************************************************/
213
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
214
{
215
    module_t * p_next = NULL;
216
    vlc_value_t lockval;
217
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
218

219
220
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
221
    vlc_mutex_lock( lockval.p_address );
222
    if( !p_libvlc_global->p_module_bank )
223
224
    {
        vlc_mutex_unlock( lockval.p_address );
225
        var_Destroy( p_libvlc_global, "libvlc" );
226
227
        return;
    }
228
    if( --p_libvlc_global->p_module_bank->i_usage )
229
230
    {
        vlc_mutex_unlock( lockval.p_address );
231
        var_Destroy( p_libvlc_global, "libvlc" );
232
233
234
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
235
    var_Destroy( p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
   
Sam Hocevar committed
236

237
238
    config_AutoSaveConfigFile( p_this );

239
#ifdef HAVE_DYNAMIC_PLUGINS
240
# define p_bank p_libvlc_global->p_module_bank
241
242
243
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
244
245
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
246
247
            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 );
248
249
250
251
            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;
        }
252
    }
253
    if( p_bank->pp_loaded_cache )
254
    {
255
        free( p_bank->pp_loaded_cache );
256
257
        p_bank->pp_loaded_cache = NULL;
    }
258
259
260
261
    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] );
262
        p_bank->pp_cache[p_bank->i_cache] = NULL;
263
    }
264
    if( p_bank->pp_cache )
265
    {
266
        free( p_bank->pp_cache );
267
268
        p_bank->pp_cache = NULL;
    }
269
# undef p_bank
270
271
#endif

272
    vlc_object_detach( p_libvlc_global->p_module_bank );
273

274
    while( p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
   
Sam Hocevar committed
275
    {
276
        p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
277

278
        if( DeleteModule( p_next, VLC_TRUE ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
279
280
        {
            /* Module deletion failed */
281
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
282
                     p_next->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
283
284

            /* We just free the module by hand. Niahahahahaha. */
285
286
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
   
Sam Hocevar committed
287
288
289
        }
    }

290
291
    vlc_object_destroy( p_libvlc_global->p_module_bank );
    p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
292
293
294
}

/*****************************************************************************
295
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
296
 *****************************************************************************
297
298
299
300
 * 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
301
 *****************************************************************************/
302
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
303
{
304
    vlc_value_t lockval;
305
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
306

307
308
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
309
    vlc_mutex_lock( lockval.p_address );
310
    if( p_libvlc_global->p_module_bank->b_main )
311
312
    {
        vlc_mutex_unlock( lockval.p_address );
313
        var_Destroy( p_libvlc_global, "libvlc" );
314
315
        return;
    }
316
    p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
317
    vlc_mutex_unlock( lockval.p_address );
318
    var_Destroy( p_libvlc_global, "libvlc" );
319

320
    AllocateBuiltinModule( p_this, vlc_entry__main );
321
322
323
324
325
326
327
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
328
void __module_LoadBuiltins( vlc_object_t * p_this )
329
{
330
    vlc_value_t lockval;
331
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
332

333
334
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
335
    vlc_mutex_lock( lockval.p_address );
336
    if( p_libvlc_global->p_module_bank->b_builtins )
337
338
    {
        vlc_mutex_unlock( lockval.p_address );
339
        var_Destroy( p_libvlc_global, "libvlc" );
340
341
        return;
    }
342
    p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
343
    vlc_mutex_unlock( lockval.p_address );
344
    var_Destroy( p_libvlc_global, "libvlc" );
345

346
347
348
349
350
351
352
353
354
    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.
 *****************************************************************************/
355
void __module_LoadPlugins( vlc_object_t * p_this )
356
357
{
#ifdef HAVE_DYNAMIC_PLUGINS
358
    vlc_value_t lockval;
359
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
360

361
362
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
363
    vlc_mutex_lock( lockval.p_address );
364
    if( p_libvlc_global->p_module_bank->b_plugins )
365
366
    {
        vlc_mutex_unlock( lockval.p_address );
367
        var_Destroy( p_libvlc_global, "libvlc" );
368
369
        return;
    }
370
    p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
371
    vlc_mutex_unlock( lockval.p_address );
372
    var_Destroy( p_libvlc_global, "libvlc" );
373

374
    msg_Dbg( p_this, "checking plugin modules" );
375
376

    if( config_GetInt( p_this, "plugins-cache" ) )
377
        p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
378

379
380
    if( p_libvlc_global->p_module_bank->b_cache ||
        p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
381

382
383
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
384
385
}

386
387
388
389
390
391
392
393
/*****************************************************************************
 * 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 );
}

Sam Hocevar's avatar
 
Sam Hocevar committed
394
/*****************************************************************************
395
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
396
 *****************************************************************************
397
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
398
 *****************************************************************************/
399
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
gbazin's avatar
   
gbazin committed
400
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
401
{
402
    typedef struct module_list_t module_list_t;
403

404
    struct module_list_t
Sam Hocevar's avatar
   
Sam Hocevar committed
405
    {
406
        module_t *p_module;
407
        int i_score;
408
        vlc_bool_t b_force;
409
410
411
412
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
413
    vlc_list_t *p_all;
Sam Hocevar's avatar
   
Sam Hocevar committed
414

gbazin's avatar
   
gbazin committed
415
    int i_which_module, i_index = 0;
416
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
   
Sam Hocevar committed
417
418

    module_t *p_module;
419

420
    int   i_shortcuts = 0;
421
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
422
    vlc_bool_t b_force_backup = p_this->b_force;
Sam Hocevar's avatar
 
Sam Hocevar committed
423

424

425
426
427
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
gbazin's avatar
   
gbazin committed
428
429
430
431
        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;
432
        psz_name = psz_var;
433
434
    }

435
    /* Count how many different shortcuts were asked for */
436
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
437
    {
gbazin's avatar
   
gbazin committed
438
        char *psz_parser, *psz_last_shortcut;
439

440
441
442
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
443
            free( psz_var );
444
445
446
            return NULL;
        }

447
        i_shortcuts++;
gbazin's avatar
   
gbazin committed
448
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
449
450

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
451
        {
452
            if( *psz_parser == ',' )
Sam Hocevar's avatar
   
Sam Hocevar committed
453
            {
454
455
                 *psz_parser = '\0';
                 i_shortcuts++;
gbazin's avatar
   
gbazin committed
456
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
457
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
458
        }
gbazin's avatar
   
gbazin committed
459
460
461
462

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
463
464
465
466
467
468
469
470
471
472
            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
473
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
474
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
475

Sam Hocevar's avatar
   
Sam Hocevar committed
476
    /* Sort the modules and test them */
477
478
    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
479
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
480
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
   
Sam Hocevar committed
481

Sam Hocevar's avatar
   
Sam Hocevar committed
482
    /* Parse the module list for capabilities and probe each of them */
483
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
484
    {
485
        int i_shortcut_bonus = 0;
486

487
        p_module = (module_t *)p_all->p_values[i_which_module].p_object;
488

489
        /* Test that this module can do what we need */
490
        if( !module_IsCapable( p_module, psz_capability ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
491
        {
492
493
494
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
495
496
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
497
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
498
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
   
Sam Hocevar committed
499
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
500
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
501
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
502

503
        /* If we required a shortcut, check this plugin provides it. */
504
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
   
Sam Hocevar committed
505
        {
gbazin's avatar
   
gbazin committed
506
            vlc_bool_t b_trash;
507
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
   
Sam Hocevar committed
508

509
            /* Let's drop modules with a <= 0 score (unless they are
gbazin's avatar
   
gbazin committed
510
             * explicitly requested) */
511
            b_trash = p_module->i_score <= 0;
gbazin's avatar
   
gbazin committed
512

513
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
   
Sam Hocevar committed
514
            {
515
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
516
                {
517
518
519
520
521
                    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
522
523
                    {
                        /* Found it */
524
525
                        if( c && c[1] )
                            psz_alias = c+1;
gbazin's avatar
   
gbazin committed
526
                        i_shortcut_bonus = i_short * 10000;
527
                        goto found_shortcut;
gbazin's avatar
   
gbazin committed
528
                    }
529
530
                }

531
                /* Go to the next shortcut... This is so lame! */
532
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
   
Sam Hocevar committed
533
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
534

535
536
537
            /* 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!!! */
538
            if( b_strict )
Sam Hocevar's avatar
   
Sam Hocevar committed
539
                continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
540
        }
541
542
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
543
544
545
        {
            continue;
        }
546

547
548
found_shortcut:

Sam Hocevar's avatar
   
Sam Hocevar committed
549
        /* Special case: test if we requested a particular intf plugin */
gbazin's avatar
   
gbazin committed
550
        if( !i_shortcuts && p_module->psz_program
Sam Hocevar's avatar
Sam Hocevar committed
551
             && !strcmp( psz_capability, "interface" )
552
             && !strcmp( p_module->psz_program,
553
                         p_this->p_libvlc->psz_object_name ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
554
        {
555
            if( !b_intf )
Sam Hocevar's avatar
   
Sam Hocevar committed
556
            {
557
558
559
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
   
Sam Hocevar committed
560
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
561
        }
562
563
564
565
566
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
567

Sam Hocevar's avatar
   
Sam Hocevar committed
568
569
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
570
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
571
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
   
Sam Hocevar committed
572

Sam Hocevar's avatar
   
Sam Hocevar committed
573
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
   
Sam Hocevar committed
574
575
        if( i_index == 0 )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
576
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
577
578
579
580
581
582
583
            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. */
584
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
   
Sam Hocevar committed
585

586
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
587
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
588
589
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
   
Sam Hocevar committed
590
591
            }
            else
592
            {
593
594
                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
595
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
596
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
597
598
                }

Sam Hocevar's avatar
   
Sam Hocevar committed
599
600
601
                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
602
603
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
604
605
        i_index++;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
606

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

610
    /* Lock all candidate modules */
611
612
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
613
    {
614
        vlc_object_yield( p_tmp->p_module );
615
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
616
617
    }

618
    /* We can release the list, interesting modules were yielded */
619
    vlc_list_release( p_all );
Sam Hocevar's avatar
   
Sam Hocevar committed
620

Sam Hocevar's avatar
   
Sam Hocevar committed
621
    /* Parse the linked list and use the first successful module */
622
623
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
624
    {
625
626
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
627
628
629
630
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

631
632
633
634
635
636
637
638
        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 );
639
                DeleteModule( p_new_module, VLC_TRUE );
640
641
642
643
            }
        }
#endif

644
        p_this->b_force = p_tmp->b_force;
645
646
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
   
Sam Hocevar committed
647
648
        {
            break;
649
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
650

651
        vlc_object_release( p_tmp->p_module );
652
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
653
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
654

Sam Hocevar's avatar
   
Sam Hocevar committed
655
    /* Store the locked module value */
656
    if( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
657
    {
658
659
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
660
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
661
662
663
664
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
665

Sam Hocevar's avatar
   
Sam Hocevar committed
666
    /* Unlock the remaining modules */
667
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
668
    {
669
        vlc_object_release( p_tmp->p_module );
670
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
671
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
672

Sam Hocevar's avatar
   
Sam Hocevar committed
673
    free( p_list );
674
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
675

Sam Hocevar's avatar
   
Sam Hocevar committed
676
    if( p_module != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
677
    {
678
        msg_Dbg( p_this, "using %s module \"%s\"",
679
                 psz_capability, p_module->psz_object_name );
680
681
682
    }
    else if( p_first == NULL )
    {
683
684
685
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
686
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
687
688
        }
        else
zorglub's avatar
zorglub committed
689
        {
690
691
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
692
693
694

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
695
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
696
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
697
698
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
699
700
        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
701
    }
702
703
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
   
Sam Hocevar committed
704

705
706
    if( p_module && !p_this->psz_object_name )
    {
707
708
709
        /* 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.
         */
710
711
712
713
714
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
715

716
717
    free( psz_shortcuts );
    free( psz_var );
718

Sam Hocevar's avatar
   
Sam Hocevar committed
719
    /* Don't forget that the module is still locked */
720
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
721
722
723
724
725
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
726
727
 * 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
728
 *****************************************************************************/
729
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
730
{
731
732
733
734
735
736
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

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

739
    vlc_object_release( p_module );
740
741

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
742
743
}

744
745
746
747
748
749
750
751
752
753
754
755
756
/*****************************************************************************
 * 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 )
{
    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++)
    {
757
        const char *psz_module_name =
758
            ((module_t *) p_list->p_values[i].p_object)->psz_shortname;
759
        if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
760
761
762
763
764
765
766
767
768
        {
            /* We can release the list, and return yes */
            vlc_list_release( p_list ); return VLC_TRUE;
        }
    }
    vlc_list_release( p_list ); return VLC_FALSE;
}


Sam Hocevar's avatar
 
Sam Hocevar committed
769
770
771
772
/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

Sam Hocevar's avatar
   
Sam Hocevar committed
773
774
775
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
776
#ifdef HAVE_DYNAMIC_PLUGINS
777
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
778
{
779
    /* Yes, there are two NULLs because we replace one with "plugin-path". */
780
#if defined( WIN32 ) || defined( UNDER_CE )
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
781
    const char *path[] = { "modules", "", "plugins", NULL, NULL };
782
#else
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
783
    const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
784
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
785

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

788
    /* 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
789
790
    char *userpath = config_GetPsz( p_this, "plugin-path" );
    path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
791

Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
792
    for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
Sam Hocevar's avatar
   
Sam Hocevar committed
793
    {
Rémi Denis-Courmont's avatar
Cleanup    
Rémi Denis-Courmont committed
794
795
796
        char *psz_fullpath;

        if (!**ppsz_path) continue;
797

798
#if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
Sam Hocevar's avatar
   
Sam Hocevar committed
799

800
        /* Handle relative as well as absolute paths */
801
#ifdef WIN32