modules.c 43.5 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 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc/vlc.h>
32
#include "libvlc.h"
33

Sam Hocevar's avatar
 
Sam Hocevar committed
34 35 36 37 38 39
/* 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
40 41
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
 
Sam Hocevar committed
42
#include <string.h>                                              /* strdup() */
43
#include <assert.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
44

45 46 47 48
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
#endif

49 50 51 52 53 54
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
55 56 57
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
58

59 60 61
#if !defined(HAVE_DYNAMIC_PLUGINS)
    /* no support for plugins */
#elif defined(HAVE_DL_DYLD)
Sam Hocevar's avatar
Sam Hocevar committed
62 63 64 65 66 67 68 69
#   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
70
#   include <windows.h>
Sam Hocevar's avatar
Sam Hocevar committed
71 72
#elif defined(HAVE_DL_DLOPEN)
#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
Sam Hocevar's avatar
Sam Hocevar committed
73 74
#       include <dlfcn.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
75
#   if defined(HAVE_SYS_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
76 77
#       include <sys/dl.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
78 79
#elif defined(HAVE_DL_SHL_LOAD)
#   if defined(HAVE_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
80 81
#       include <dl.h>
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
82 83
#endif

84
#include "config/configuration.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
85

Clément Stenac's avatar
Clément Stenac committed
86
#include "vlc_charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
87

88 89
#include "modules/modules.h"
#include "modules/builtin.h"
Clément Stenac's avatar
Clément Stenac committed
90

Sam Hocevar's avatar
 
Sam Hocevar committed
91 92 93
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
94
#ifdef HAVE_DYNAMIC_PLUGINS
95 96 97 98
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
99
#endif
100
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
101
static int  DeleteModule ( module_t *, vlc_bool_t );
Sam Hocevar's avatar
 
Sam Hocevar committed
102
#ifdef HAVE_DYNAMIC_PLUGINS
103 104
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
105
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
106

107 108
static void module_LoadMain( vlc_object_t *p_this );

109 110 111 112
/**
 * Init bank
 *
 * Creates a module bank structure which will be filled later
Gildas Bazin's avatar
 
Gildas Bazin committed
113
 * on with all the modules found.
114 115 116
 * \param p_this vlc object structure
 * \return nothing
 */
117
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
118
{
119
    module_bank_t *p_bank = NULL;
120
    vlc_value_t  lockval;
121
    libvlc_global_data_t *p_libvlc_global = vlc_global();
122

123 124
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
125
    vlc_mutex_lock( lockval.p_address );
126
    if( p_libvlc_global->p_module_bank )
127
    {
128
        p_libvlc_global->p_module_bank->i_usage++;
129
        vlc_mutex_unlock( lockval.p_address );
130
        var_Destroy( p_libvlc_global, "libvlc" );
131 132 133
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
134
    var_Destroy( p_libvlc_global, "libvlc" );
135 136

    p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
137 138
    if( !p_bank )
        return;
139
    p_bank->psz_object_name = "module bank";
140
    p_bank->i_usage = 1;
141
    p_bank->i_cache = p_bank->i_loaded_cache = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
142
    p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
143 144
    p_bank->b_cache = p_bank->b_cache_dirty =
        p_bank->b_cache_delete = VLC_FALSE;
145 146

    /* Everything worked, attach the object */
147 148
    p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_libvlc_global );
149

150
    module_LoadMain( p_this );
Gildas Bazin's avatar
 
Gildas Bazin committed
151 152
}

Sam Hocevar's avatar
 
Sam Hocevar committed
153

154 155 156 157
/**
 * End bank
 *
 * Unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
158
 * bank in case of success.
159 160 161
 * \param p_this vlc object structure
 * \return nothing
 */
162
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
163
{
164
    module_t * p_next = NULL;
165
    vlc_value_t lockval;
166
    libvlc_global_data_t *p_libvlc_global = vlc_global();
167

168 169
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
170
    vlc_mutex_lock( lockval.p_address );
171
    if( !p_libvlc_global->p_module_bank )
172 173
    {
        vlc_mutex_unlock( lockval.p_address );
174
        var_Destroy( p_libvlc_global, "libvlc" );
175 176
        return;
    }
177
    if( --p_libvlc_global->p_module_bank->i_usage )
178 179
    {
        vlc_mutex_unlock( lockval.p_address );
180
        var_Destroy( p_libvlc_global, "libvlc" );
181 182 183
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
184
    var_Destroy( p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
 
Sam Hocevar committed
185

186
    /* Save the configuration */
187 188
    config_AutoSaveConfigFile( p_this );

189
#ifdef HAVE_DYNAMIC_PLUGINS
190
# define p_bank p_libvlc_global->p_module_bank
191 192 193
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
194 195
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
196 197 198
            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 );
199 200 201 202
            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;
        }
203
    }
204
    if( p_bank->pp_loaded_cache )
205
    {
206
        free( p_bank->pp_loaded_cache );
207 208
        p_bank->pp_loaded_cache = NULL;
    }
209 210 211 212
    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] );
213
        p_bank->pp_cache[p_bank->i_cache] = NULL;
214
    }
215
    if( p_bank->pp_cache )
216
    {
217
        free( p_bank->pp_cache );
218 219
        p_bank->pp_cache = NULL;
    }
220
# undef p_bank
221 222
#endif

223
    vlc_object_detach( p_libvlc_global->p_module_bank );
224

225
    while( p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
 
Sam Hocevar committed
226
    {
227
        p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
228

229
        if( DeleteModule( p_next, VLC_TRUE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
230 231
        {
            /* Module deletion failed */
232
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
233
                     p_next->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
234 235

            /* We just free the module by hand. Niahahahahaha. */
236
            vlc_object_detach( p_next );
237
            vlc_object_release( p_next );
Sam Hocevar's avatar
 
Sam Hocevar committed
238 239 240
        }
    }

241
    vlc_object_release( p_libvlc_global->p_module_bank );
242
    p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
243 244
}

245 246 247 248
/**
 * Load the main program info into the module bank.
 *
 * Fills the module bank structure with the main module infos.
249 250 251
 * 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.
252 253 254
 * \param p_this vlc object structure
 * \return nothing
 */
255
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
256
{
257
    vlc_value_t lockval;
258
    libvlc_global_data_t *p_libvlc_global = vlc_global();
259

260 261
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
262
    vlc_mutex_lock( lockval.p_address );
263
    if( p_libvlc_global->p_module_bank->b_main )
264 265
    {
        vlc_mutex_unlock( lockval.p_address );
266
        var_Destroy( p_libvlc_global, "libvlc" );
267 268
        return;
    }
269
    p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
270
    vlc_mutex_unlock( lockval.p_address );
271
    var_Destroy( p_libvlc_global, "libvlc" );
272

273
    AllocateBuiltinModule( p_this, vlc_entry__main );
274 275
}

276 277 278 279 280 281 282
/**
 * Load all modules which we built with.
 *
 * Fills the module bank structure with the builtin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
283
void __module_LoadBuiltins( vlc_object_t * p_this )
284
{
285
    vlc_value_t lockval;
286
    libvlc_global_data_t *p_libvlc_global = vlc_global();
287

288 289
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
290
    vlc_mutex_lock( lockval.p_address );
291
    if( p_libvlc_global->p_module_bank->b_builtins )
292 293
    {
        vlc_mutex_unlock( lockval.p_address );
294
        var_Destroy( p_libvlc_global, "libvlc" );
295 296
        return;
    }
297
    p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
298
    vlc_mutex_unlock( lockval.p_address );
299
    var_Destroy( p_libvlc_global, "libvlc" );
300

301 302 303 304
    msg_Dbg( p_this, "checking builtin modules" );
    ALLOCATE_ALL_BUILTINS();
}

305 306 307 308 309 310 311 312
/**
 * Load all plugins
 *
 * Load all plugin modules we can find.
 * Fills the module bank structure with the plugin modules.
 * \param p_this vlc object structure
 * \return nothing
 */
313
void __module_LoadPlugins( vlc_object_t * p_this )
314 315
{
#ifdef HAVE_DYNAMIC_PLUGINS
316
    vlc_value_t lockval;
317
    libvlc_global_data_t *p_libvlc_global = vlc_global();
318

319 320
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
321
    vlc_mutex_lock( lockval.p_address );
322
    if( p_libvlc_global->p_module_bank->b_plugins )
323 324
    {
        vlc_mutex_unlock( lockval.p_address );
325
        var_Destroy( p_libvlc_global, "libvlc" );
326 327
        return;
    }
328
    p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
329
    vlc_mutex_unlock( lockval.p_address );
330
    var_Destroy( p_libvlc_global, "libvlc" );
331

332
    msg_Dbg( p_this, "checking plugin modules" );
333 334

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

337 338
    if( p_libvlc_global->p_module_bank->b_cache ||
        p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
339

340 341
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
342 343
}

344 345 346 347 348 349 350
/**
 * Checks whether a module implements a capability.
 *
 * \param m the module
 * \param cap the capability to check
 * \return TRUE if the module have the capability
 */
351 352 353 354 355
vlc_bool_t module_IsCapable( const module_t *m, const char *cap )
{
    return !strcmp( m->psz_capability, cap );
}

356 357 358 359 360 361
/**
 * Get the internal name of a module
 *
 * \param m the module
 * \return the module name
 */
362 363 364 365 366
const char *module_GetObjName( const module_t *m )
{
    return m->psz_object_name;
}

367 368 369 370 371 372 373
/**
 * Get the human-friendly name of a module.
 *
 * \param m the module
 * \param long_name TRUE to have the long name of the module
 * \return the short or long name of the module
 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
374
const char *module_GetName( const module_t *m, vlc_bool_t long_name )
375 376 377
{
    if( long_name && ( m->psz_longname != NULL) )
        return m->psz_longname;
378

379 380 381
    return m->psz_shortname ?: m->psz_object_name;
}

382 383 384 385 386 387
/**
 * Get the help for a module
 *
 * \param m the module
 * \return the help
 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
388 389 390 391 392
const char *module_GetHelp( const module_t *m )
{
    return m->psz_help;
}

393 394 395 396 397 398 399 400 401 402
/**
 * module Need
 *
 * Return the best module function, given a capability list.
 * \param p_this the vlc object
 * \param psz_capability list of capabilities needed
 * \param psz_name name of the module asked
 * \param b_strict TRUE yto use the strict mode
 * \return the module or NULL in case of a failure
 */
403
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
404
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
405
{
406
    typedef struct module_list_t module_list_t;
407

408
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
409
    {
410
        module_t *p_module;
411
        int i_score;
412
        vlc_bool_t b_force;
413 414 415 416
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
417
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
418

Gildas Bazin's avatar
 
Gildas Bazin committed
419
    int i_which_module, i_index = 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
420 421

    module_t *p_module;
422

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

427

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

438
    /* Count how many different shortcuts were asked for */
439
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
440
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
441
        char *psz_parser, *psz_last_shortcut;
442

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

450
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
451
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
452 453

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
454
        {
455
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
456
            {
457 458
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
459
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
460
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
461
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
462 463 464 465

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

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

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

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

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

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

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

512
            /* Let's drop modules with a <= 0 score (unless they are
Gildas Bazin's avatar
 
Gildas Bazin committed
513
             * explicitly requested) */
514
            b_trash = p_module->i_score <= 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
515

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

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

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

550 551
found_shortcut:

Sam Hocevar's avatar
 
Sam Hocevar committed
552 553
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
554
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
555
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
 
Sam Hocevar committed
556

Sam Hocevar's avatar
 
Sam Hocevar committed
557
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
 
Sam Hocevar committed
558 559
        if( i_index == 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
560
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
561 562 563 564 565 566 567
            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. */
568
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
569

570
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
571
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
572 573
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
 
Sam Hocevar committed
574 575
            }
            else
576
            {
577 578
                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
579
                {
Sam Hocevar's avatar
 
Sam Hocevar committed
580
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
581 582
                }

Sam Hocevar's avatar
 
Sam Hocevar committed
583 584 585
                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
586 587
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
588 589
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
590

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

594
    /* Lock all candidate modules */
595 596
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
597
    {
598
        vlc_object_yield( p_tmp->p_module );
599
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
600 601
    }

602
    /* We can release the list, interesting modules were yielded */
603
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
604

Sam Hocevar's avatar
 
Sam Hocevar committed
605
    /* Parse the linked list and use the first successful module */
606 607
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
608
    {
609 610
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
611 612 613 614
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

615 616 617 618 619 620 621 622
        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 );
623
                DeleteModule( p_new_module, VLC_TRUE );
624 625 626 627
            }
        }
#endif

628
        p_this->b_force = p_tmp->b_force;
629 630
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
 
Sam Hocevar committed
631 632
        {
            break;
633
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
634

635
        vlc_object_release( p_tmp->p_module );
636
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
637
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
638

Sam Hocevar's avatar
 
Sam Hocevar committed
639
    /* Store the locked module value */
640
    if( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
641
    {
642 643
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
644
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
645 646 647 648
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
649

Sam Hocevar's avatar
 
Sam Hocevar committed
650
    /* Unlock the remaining modules */
651
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
652
    {
653
        vlc_object_release( p_tmp->p_module );
654
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
655
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
656

Sam Hocevar's avatar
 
Sam Hocevar committed
657
    free( p_list );
658
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
659

Sam Hocevar's avatar
 
Sam Hocevar committed
660
    if( p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
661
    {
662
        msg_Dbg( p_this, "using %s module \"%s\"",
663
                 psz_capability, p_module->psz_object_name );
664 665 666
    }
    else if( p_first == NULL )
    {
667 668 669
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
670
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
671 672
        }
        else
Clément Stenac's avatar
Clément Stenac committed
673
        {
674 675
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
676 677 678

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
679
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
680
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
681 682
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
683 684
        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
685
    }
686 687
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
688

689 690
    if( p_module && !p_this->psz_object_name )
    {
691 692 693
        /* 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.
         */
694 695 696 697 698
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
699

700 701
    free( psz_shortcuts );
    free( psz_var );
702

Sam Hocevar's avatar
 
Sam Hocevar committed
703
    /* Don't forget that the module is still locked */
704
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
705 706
}

707 708 709
/**
 * Module unneed
 *
710 711
 * This function must be called by the thread that called module_Need, to
 * decrease the reference count and allow for hiding of modules.
712 713 714 715
 * \param p_this vlc object structure
 * \param p_module the module structure
 * \return nothing
 */
716
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
717
{
718 719 720 721 722 723
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

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

726
    vlc_object_release( p_module );
727 728

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
729 730
}

731 732 733 734 735 736 737
/**
 * Get a pointer to a module_t given it's name.
 *
 * \param p_this vlc object structure
 * \param psz_name the name of the module
 * \return a pointer to the module or NULL in case of a failure
 */
738
module_t *__module_Find( vlc_object_t *p_this, const char * psz_name )
739 740 741 742 743 744
{
    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++)
    {
745 746
        module_t *p_module = ((module_t *) p_list->p_values[i].p_object);
        const char *psz_module_name = p_module->psz_object_name;
747
        if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
748 749
        {
            /* We can release the list, and return yes */
750
            vlc_object_yield( p_module );
751
            vlc_list_release( p_list );
752
            return p_module;
753 754
        }
    }
755 756 757 758
    vlc_list_release( p_list );
    return NULL;
}

759

760 761 762 763 764 765 766
/**
 * Release a module_t pointer from module_Find().
 *
 * \param module the module to release
 * \return nothing
 */
void module_Put( module_t *module )
767
{
768
    vlc_object_release( module );
769 770 771
}


772 773 774 775 776 777 778 779
/**
 * Tell if a module exists and release it in thic case
 *
 * \param p_this vlc object structure
 * \param psz_name th name of the module
 * \return TRUE if the module exists
 */
vlc_bool_t __module_Exists( vlc_object_t *p_this, const char * psz_name )
780
{
781
    module_t *p_module = __module_Find( p_this, psz_name );
782 783
    if( p_module )
    {
784
        module_Put( p_module );
785 786 787 788 789 790
        return VLC_TRUE;
    }
    else
    {
        return VLC_FALSE;
    }
791 792
}

793 794 795 796 797
/**
 * GetModuleNamesForCapability
 *
 * Return a NULL terminated array with the names of the modules
 * that have a certain capability.
798
 * Free after uses both the string and the table.
799 800 801 802 803 804
 * \param p_this vlc object structure
 * \param psz_capability the capability asked
 * \param pppsz_longname an pointer to an array of string to contain
    the long names of the modules. If set to NULL the function don't use it.
 * \return the NULL terminated array
 */
805
char ** __module_GetModulesNamesForCapability( vlc_object_t *p_this,
806
                                               const char *psz_capability,
807
                                               char ***pppsz_longname )
808 809
{
    vlc_list_t *p_list;
810
    int i, j, count = 0;
811
    char **psz_ret;
812

813
    /* Do it in two passes : count the number of modules before */
814 815 816 817 818 819 820 821
    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++;
    }
822

823
    psz_ret = malloc( sizeof(char*) * (count+1) );
824 825
    if( pppsz_longname )
        *pppsz_longname = malloc( sizeof(char*) * (count+1) );
826 827