modules.c 76 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
Antoine Cellerier's avatar
Antoine Cellerier 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

Clément Stenac's avatar
Clément Stenac committed
82
#include "vlc_interface.h"
83 84
#include "vlc_playlist.h"

Clément Stenac's avatar
Clément Stenac 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"
Clément Stenac's avatar
Clément Stenac committed
89

Clément Stenac's avatar
Clément Stenac committed
90 91
#include "vlc_vout.h"
#include "vlc_vout_synchro.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
92

Clément Stenac's avatar
Clément Stenac committed
93
#include "vlc_aout.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
94

Clément Stenac's avatar
Clément Stenac committed
95
#include "vlc_sout.h"
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg 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"
Gildas Bazin's avatar
Gildas Bazin committed
100
#include "vlc_xml.h"
Christophe Mutricy's avatar
Christophe Mutricy committed
101
#include "vlc_url.h"
102

Gildas Bazin's avatar
 
Gildas Bazin committed
103
#include "iso_lang.h"
Clément Stenac's avatar
Clément Stenac committed
104
#include "vlc_charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
105

106 107
#include "vlc_block.h"

108 109
#include "vlc_vlm.h"

Clément Stenac's avatar
Clément Stenac committed
110
#include "vlc_image.h"
111
#include "vlc_osd.h"
Clément Stenac's avatar
Clément Stenac committed
112

113
#include "vlc_update.h"
114
#include "vlc_strings.h"
Clément Stenac's avatar
Clément Stenac committed
115
#include "vlc_streaming.h"
116

117 118
#include "modules/modules.h"
#include "modules/builtin.h"
Clément Stenac's avatar
Clément Stenac 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.
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin 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();
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 );
Gildas Bazin's avatar
 
Gildas Bazin 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();
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();
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();
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();
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 );
}

394 395 396 397 398 399 400 401 402 403 404
/*****************************************************************************
 * 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
405
const char *module_GetName( const module_t *m, vlc_bool_t long_name )
406 407 408 409 410 411 412
{
    if( long_name && ( m->psz_longname != NULL) )
        return m->psz_longname;
    
    return m->psz_shortname ?: m->psz_object_name;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
413 414 415 416 417
const char *module_GetHelp( const module_t *m )
{
    return m->psz_help;
}

Sam Hocevar's avatar
 
Sam Hocevar committed
418
/*****************************************************************************
419
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
420
 *****************************************************************************
421
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
422
 *****************************************************************************/
423
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
424
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
425
{
426
    typedef struct module_list_t module_list_t;
427

428
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
429
    {
430
        module_t *p_module;
431
        int i_score;
432
        vlc_bool_t b_force;
433 434 435 436
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
437
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
438

Gildas Bazin's avatar
 
Gildas Bazin committed
439
    int i_which_module, i_index = 0;
440
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
 
Sam Hocevar committed
441 442

    module_t *p_module;
443

444
    int   i_shortcuts = 0;
445
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
446
    vlc_bool_t b_force_backup = p_this->b_force;
Sam Hocevar's avatar
 
Sam Hocevar committed
447

448

449 450 451
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
452 453 454 455
        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;
456
        psz_name = psz_var;
457 458
    }

459
    /* Count how many different shortcuts were asked for */
460
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
461
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
462
        char *psz_parser, *psz_last_shortcut;
463

464 465 466
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
467
            free( psz_var );
468 469 470
            return NULL;
        }

471
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
472
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
473 474

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
475
        {
476
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
477
            {
478 479
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
480
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
481
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
482
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484 485 486

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
487 488 489 490 491 492 493 494 495 496
            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--;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
497
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
498
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
499

Sam Hocevar's avatar
 
Sam Hocevar committed
500
    /* Sort the modules and test them */
501 502
    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
503
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
504
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
 
Sam Hocevar committed
505

Sam Hocevar's avatar
 
Sam Hocevar committed
506
    /* Parse the module list for capabilities and probe each of them */
507
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
508
    {
509
        int i_shortcut_bonus = 0;
510

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

513
        /* Test that this module can do what we need */
514
        if( !module_IsCapable( p_module, psz_capability ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
515
        {
516 517 518
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
519 520
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
521
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
522
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
 
Sam Hocevar committed
523
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
524
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
525
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
526

527
        /* If we required a shortcut, check this plugin provides it. */
528
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
529
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
530
            vlc_bool_t b_trash;
531
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
 
Sam Hocevar committed
532

533
            /* Let's drop modules with a <= 0 score (unless they are
Gildas Bazin's avatar
 
Gildas Bazin committed
534
             * explicitly requested) */
535
            b_trash = p_module->i_score <= 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
536

537
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
 
Sam Hocevar committed
538
            {
539
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
540
                {
541 542 543 544 545
                    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] ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
546 547
                    {
                        /* Found it */
548 549
                        if( c && c[1] )
                            psz_alias = c+1;
Gildas Bazin's avatar
 
Gildas Bazin committed
550
                        i_shortcut_bonus = i_short * 10000;
551
                        goto found_shortcut;
Gildas Bazin's avatar
 
Gildas Bazin committed
552
                    }
553 554
                }

555
                /* Go to the next shortcut... This is so lame! */
556
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
557
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
558

559 560 561
            /* 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!!! */
562
            if( b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
563
                continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
564
        }
565 566
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
567 568 569
        {
            continue;
        }
570

571 572
found_shortcut:

Sam Hocevar's avatar
 
Sam Hocevar committed
573
        /* Special case: test if we requested a particular intf plugin */
Gildas Bazin's avatar
 
Gildas Bazin committed
574
        if( !i_shortcuts && p_module->psz_program
Sam Hocevar's avatar
Sam Hocevar committed
575
             && !strcmp( psz_capability, "interface" )
576
             && !strcmp( p_module->psz_program,
577
                         p_this->p_libvlc->psz_object_name ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
578
        {
579
            if( !b_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
580
            {
581 582 583
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
584
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
585
        }
586 587 588 589 590
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
591

Sam Hocevar's avatar
 
Sam Hocevar committed
592 593
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
594
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
595
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
 
Sam Hocevar committed
596

Sam Hocevar's avatar
 
Sam Hocevar committed
597
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
 
Sam Hocevar committed
598 599
        if( i_index == 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
600
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
601 602 603 604 605 606 607
            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. */
608
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
609

610
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
611
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
612 613
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
 
Sam Hocevar committed
614 615
            }
            else
616
            {
617 618
                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
619
                {
Sam Hocevar's avatar
 
Sam Hocevar committed
620
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
621 622
                }

Sam Hocevar's avatar
 
Sam Hocevar committed
623 624 625
                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
626 627
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
628 629
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
630

Clément Stenac's avatar
Clément Stenac committed
631 632
    msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
                                            i_index, i_index == 1 ? "" : "s" );
633

634
    /* Lock all candidate modules */
635 636
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
637
    {
638
        vlc_object_yield( p_tmp->p_module );
639
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
640 641
    }

642
    /* We can release the list, interesting modules were yielded */
643
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
644

Sam Hocevar's avatar
 
Sam Hocevar committed
645
    /* Parse the linked list and use the first successful module */
646 647
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
648
    {
649 650
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
651 652 653 654
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

655 656 657 658 659 660 661 662
        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 );
663
                DeleteModule( p_new_module, VLC_TRUE );
664 665 666 667
            }
        }
#endif

668
        p_this->b_force = p_tmp->b_force;
669 670
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
 
Sam Hocevar committed
671 672
        {
            break;
673
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
674

675
        vlc_object_release( p_tmp->p_module );
676
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
677
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
678

Sam Hocevar's avatar
 
Sam Hocevar committed
679
    /* Store the locked module value */
680
    if( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
681
    {
682 683
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
684
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
685 686 687 688
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
689

Sam Hocevar's avatar
 
Sam Hocevar committed
690
    /* Unlock the remaining modules */
691
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
692
    {
693
        vlc_object_release( p_tmp->p_module );
694
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
695
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
696

Sam Hocevar's avatar
 
Sam Hocevar committed
697
    free( p_list );
698
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
699

Sam Hocevar's avatar
 
Sam Hocevar committed
700
    if( p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
701
    {
702
        msg_Dbg( p_this, "using %s module \"%s\"",
703
                 psz_capability, p_module->psz_object_name );
704 705 706
    }
    else if( p_first == NULL )
    {
707 708 709
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
710
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
711 712
        }
        else
Clément Stenac's avatar
Clément Stenac committed
713
        {
714 715
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
716 717 718

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
719
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
720
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
721 722
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
723 724
        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
725
    }
726 727
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
728

729 730
    if( p_module && !p_this->psz_object_name )
    {
731 732 733
        /* 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.
         */
734 735 736 737 738
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
739

740 741
    free( psz_shortcuts );
    free( psz_var );
742

Sam Hocevar's avatar
 
Sam Hocevar committed
743
    /* Don't forget that the module is still locked */
744
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
745 746 747 748 749
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
750 751
 * 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
752
 *****************************************************************************/
753
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
754
{
755 756 757 758 759 760
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

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

763
    vlc_object_release( p_module );
764 765

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
766 767
}

768
/*****************************************************************************
769
 * module_FindName: get a pointer to a module_t given it's name.