modules.c 71.6 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-2004 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
#include <vlc/vlc.h>

Sam Hocevar's avatar
 
Sam Hocevar committed
29 30 31 32 33 34
/* 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
35 36
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
 
Sam Hocevar committed
37
#include <string.h>                                              /* strdup() */
Sam Hocevar's avatar
 
Sam Hocevar committed
38

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

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

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

Clément Stenac's avatar
Clément Stenac committed
78
#include "misc/configuration.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
79

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

Clément Stenac's avatar
Clément Stenac committed
83 84 85 86
#include "vlc_stream.h"
#include "vlc_access.h"
#include "vlc_demux.h"

Clément Stenac's avatar
Clément Stenac committed
87 88
#include "vlc_vout.h"
#include "vlc_vout_synchro.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
89

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

Clément Stenac's avatar
Clément Stenac committed
92
#include "vlc_sout.h"
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
93
#include "vlc_httpd.h"
94
#include "vlc_acl.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
#include "vlc_tls.h"
96
#include "vlc_md5.h"
Gildas Bazin's avatar
Gildas Bazin committed
97
#include "vlc_xml.h"
Christophe Mutricy's avatar
Christophe Mutricy committed
98
#include "vlc_url.h"
99

Gildas Bazin's avatar
 
Gildas Bazin committed
100
#include "iso_lang.h"
Clément Stenac's avatar
Clément Stenac committed
101
#include "vlc_charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
102

103 104
#include "vlc_block.h"

105 106
#include "vlc_vlm.h"

Clément Stenac's avatar
Clément Stenac committed
107
#include "vlc_image.h"
108
#include "vlc_osd.h"
Clément Stenac's avatar
Clément Stenac committed
109

110
#include "vlc_update.h"
111
#include "vlc_strings.h"
Clément Stenac's avatar
Clément Stenac committed
112
#include "vlc_streaming.h"
113

Clément Stenac's avatar
Clément Stenac committed
114 115
#include "modules.h"

Gildas Bazin's avatar
Gildas Bazin committed
116
#if defined( _MSC_VER ) && defined( UNDER_CE )
117 118
#    include "modules_builtin_evc.h"
#elif defined( _MSC_VER )
Gildas Bazin's avatar
 
Gildas Bazin committed
119
#    include "modules_builtin_msvc.h"
120 121
#else
#    include "modules_builtin.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
122
#endif
Clément Stenac's avatar
Clément Stenac committed
123
#include "vlc_network.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
124

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

Sam Hocevar's avatar
 
Sam Hocevar committed
130 131 132
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
133
#ifdef HAVE_DYNAMIC_PLUGINS
134 135 136 137
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
138
#endif
139
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
Sam Hocevar's avatar
 
Sam Hocevar committed
140
static int  DeleteModule ( module_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
141
#ifdef HAVE_DYNAMIC_PLUGINS
142 143 144
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
static int    CallEntry        ( module_t * );
145
static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
146 147
static void   CloseModule      ( module_handle_t );
static void * GetSymbol        ( module_handle_t, const char * );
148
static void   CacheLoad        ( vlc_object_t * );
149
static int    CacheLoadConfig  ( module_t *, FILE * );
150 151
static void   CacheSave        ( vlc_object_t * );
static void   CacheSaveConfig  ( module_t *, FILE * );
Sam Hocevar's avatar
Sam Hocevar committed
152
static char * CacheName        ( void );
153 154 155
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
156
#if defined(HAVE_DL_WINDOWS)
157 158
static char * GetWindowsError  ( void );
#endif
159
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
160

161 162
static void module_LoadMain( vlc_object_t *p_this );

163 164 165 166 167

/* Sub-version number
 * (only used to avoid breakage in dev version when cache structure changes) */
#define CACHE_SUBVERSION_NUM 1

Sam Hocevar's avatar
 
Sam Hocevar committed
168 169 170
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
171 172
 * 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
173
 *****************************************************************************/
174
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
175
{
176
    module_bank_t *p_bank = NULL;
177 178
    vlc_value_t  lockval;

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
202 203 204
    /*
     * Store the symbols to be exported
     */
205
#if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
206
    STORE_SYMBOLS( &p_bank->symbols );
207
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
208

209
    /* Everything worked, attach the object */
210 211
    p_this->p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_this->p_libvlc_global );
212

213
    module_LoadMain( p_this );
Gildas Bazin's avatar
 
Gildas Bazin committed
214 215 216
}

/*****************************************************************************
217
 * module_ResetBank: reset the module bank.
Gildas Bazin's avatar
 
Gildas Bazin committed
218
 *****************************************************************************
219 220
 * This function resets the module bank by unloading all unused plugin
 * modules.
Gildas Bazin's avatar
 
Gildas Bazin committed
221
 *****************************************************************************/
222
void __module_ResetBank( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
223
{
224 225
    msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
226 227
}

Sam Hocevar's avatar
 
Sam Hocevar committed
228
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
229
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
230
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
231
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
232 233
 * bank in case of success.
 *****************************************************************************/
234
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
235
{
236
    module_t * p_next = NULL;
237
    vlc_value_t lockval;
238

239 240
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
241
    vlc_mutex_lock( lockval.p_address );
242
    if( !p_this->p_libvlc_global->p_module_bank )
243 244
    {
        vlc_mutex_unlock( lockval.p_address );
245
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
246 247
        return;
    }
248
    if( --p_this->p_libvlc_global->p_module_bank->i_usage )
249 250
    {
        vlc_mutex_unlock( lockval.p_address );
251
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
252 253 254
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
255
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
 
Sam Hocevar committed
256

257 258
    config_AutoSaveConfigFile( p_this );

259
#ifdef HAVE_DYNAMIC_PLUGINS
260
#define p_bank p_this->p_libvlc_global->p_module_bank
261 262 263
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
264 265 266 267 268 269 270
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
            DeleteModule (p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module);
            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;
        }
271
    }
272
    if( p_bank->pp_loaded_cache )
273
    {
274
        free( p_bank->pp_loaded_cache );
275 276
        p_bank->pp_loaded_cache = NULL;
    }
277 278 279 280
    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] );
281
        p_bank->pp_cache[p_bank->i_cache] = NULL;
282
    }
283
    if( p_bank->pp_cache )
284
    {
285
        free( p_bank->pp_cache );
286 287
        p_bank->pp_cache = NULL;
    }
288
#undef p_bank
289 290
#endif

291
    vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
292

293
    while( p_this->p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
 
Sam Hocevar committed
294
    {
295
        p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
296 297

        if( DeleteModule( p_next ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
298 299
        {
            /* Module deletion failed */
300
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
301
                     p_next->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
302 303

            /* We just free the module by hand. Niahahahahaha. */
304 305
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
 
Sam Hocevar committed
306 307 308
        }
    }

309 310
    vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
    p_this->p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
311 312

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
313 314 315
}

/*****************************************************************************
316
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
317
 *****************************************************************************
318 319 320 321
 * 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
322
 *****************************************************************************/
323
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
324
{
325 326
    vlc_value_t lockval;

327 328
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
329
    vlc_mutex_lock( lockval.p_address );
330
    if( p_this->p_libvlc_global->p_module_bank->b_main )
331 332
    {
        vlc_mutex_unlock( lockval.p_address );
333
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
334 335
        return;
    }
336
    p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
337
    vlc_mutex_unlock( lockval.p_address );
338
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
339

340
    AllocateBuiltinModule( p_this, vlc_entry__main );
341 342 343 344 345 346 347
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
348
void __module_LoadBuiltins( vlc_object_t * p_this )
349
{
350 351
    vlc_value_t lockval;

352 353
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
354
    vlc_mutex_lock( lockval.p_address );
355
    if( p_this->p_libvlc_global->p_module_bank->b_builtins )
356 357
    {
        vlc_mutex_unlock( lockval.p_address );
358
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
359 360
        return;
    }
361
    p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
362
    vlc_mutex_unlock( lockval.p_address );
363
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
364

365 366 367 368 369 370 371 372 373
    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.
 *****************************************************************************/
374
void __module_LoadPlugins( vlc_object_t * p_this )
375 376
{
#ifdef HAVE_DYNAMIC_PLUGINS
377 378
    vlc_value_t lockval;

379 380
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
381
    vlc_mutex_lock( lockval.p_address );
382
    if( p_this->p_libvlc_global->p_module_bank->b_plugins )
383 384
    {
        vlc_mutex_unlock( lockval.p_address );
385
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
386 387
        return;
    }
388
    p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
389
    vlc_mutex_unlock( lockval.p_address );
390
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
391

392
    msg_Dbg( p_this, "checking plugin modules" );
393 394

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

397 398
    if( p_this->p_libvlc_global->p_module_bank->b_cache ||
        p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
399

400 401
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
402 403 404
}

/*****************************************************************************
405
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
406
 *****************************************************************************
407
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
408
 *****************************************************************************/
409
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
410
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
411
{
412
    typedef struct module_list_t module_list_t;
413

414
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
415
    {
416
        module_t *p_module;
417
        int i_score;
418
        vlc_bool_t b_force;
419 420 421 422
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
423
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
424

Gildas Bazin's avatar
 
Gildas Bazin committed
425
    int i_which_module, i_index = 0;
426
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
 
Sam Hocevar committed
427 428

    module_t *p_module;
429

430
    int   i_shortcuts = 0;
431
    char *psz_shortcuts = NULL, *psz_var = NULL;
432
    vlc_bool_t b_force_backup = p_this->b_force;
Sam Hocevar's avatar
 
Sam Hocevar committed
433

434

435 436 437
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
438 439 440 441
        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;
442
        psz_name = psz_var;
443 444
    }

445
    /* Count how many different shortcuts were asked for */
446
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
447
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
448
        char *psz_parser, *psz_last_shortcut;
449

450 451 452
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
453
            if( psz_var ) free( psz_var );
454 455 456
            return NULL;
        }

457
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
458
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
459 460

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
461
        {
462
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
463
            {
464 465
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
466
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
467
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
468
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
469 470 471 472

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
473 474 475 476 477 478 479 480 481 482
            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
483
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
484
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
485

Sam Hocevar's avatar
 
Sam Hocevar committed
486
    /* Sort the modules and test them */
487 488
    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
489
    p_first = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
490

Sam Hocevar's avatar
 
Sam Hocevar committed
491
    /* Parse the module list for capabilities and probe each of them */
492
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
493
    {
494
        int i_shortcut_bonus = 0;
495

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

498 499
        /* Test that this module can do what we need */
        if( strcmp( p_module->psz_capability, psz_capability ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
500
        {
501 502 503
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
504 505
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
506
        /* Test if we have the required CPU */
507
        if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
 
Sam Hocevar committed
508
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
509
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
510
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
511

512
        /* If we required a shortcut, check this plugin provides it. */
513
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
514
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
515
            vlc_bool_t b_trash;
516
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
 
Sam Hocevar committed
517

518
            /* Let's drop modules with a <= 0 score (unless they are
Gildas Bazin's avatar
 
Gildas Bazin committed
519
             * explicitly requested) */
520
            b_trash = p_module->i_score <= 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
521

522
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
 
Sam Hocevar committed
523
            {
524
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
525
                {
526
                    if( !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
527 528 529
                    {
                        /* Found it */
                        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
        /* Special case: test if we requested a particular intf plugin */
Gildas Bazin's avatar
 
Gildas Bazin committed
553
        if( !i_shortcuts && p_module->psz_program
Sam Hocevar's avatar
Sam Hocevar committed
554
             && !strcmp( psz_capability, "interface" )
555
             && !strcmp( p_module->psz_program,
556
                         p_this->p_libvlc->psz_object_name ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
557
        {
558
            if( !b_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
559
            {
560 561 562
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
563
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
564
        }
565 566 567 568 569
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
570

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

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
602 603 604
                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
605 606
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
607 608
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
609

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

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

621
    /* We can release the list, interesting modules were yielded */
622
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
623

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

634 635 636 637 638 639 640 641 642 643 644 645 646
        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 );
                DeleteModule( p_new_module );
            }
        }
#endif

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

654
        vlc_object_release( p_tmp->p_module );
655
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
656
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
657

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
676
    free( p_list );
677
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
678

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

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

708
    if( psz_shortcuts )
Sam Hocevar's avatar
 
Sam Hocevar committed
709
    {
710
        free( psz_shortcuts );
Sam Hocevar's avatar
 
Sam Hocevar committed
711
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
712

713
    if( psz_var )
714
    {
715
        free( psz_var );
716 717
    }

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

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

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

738
    vlc_object_release( p_module );
739 740

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
741 742 743 744 745 746
}

/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

Sam Hocevar's avatar
 
Sam Hocevar committed
747 748 749
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
750
#ifdef HAVE_DYNAMIC_PLUGINS
751
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
752
{
753
    /* Yes, there are two NULLs because we replace one with "plugin-path". */
754
#if defined( WIN32 ) || defined( UNDER_CE )
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
755
    const char *path[] = { "modules", "", "plugins", NULL, NULL };
756
#else
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
757
    const char *path[] = { "modules", PLUGIN_PATH, "plugins", NULL, NULL };
758
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
759

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

762
    /* 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
763 764
    char *userpath = config_GetPsz( p_this, "plugin-path" );
    path[sizeof(path)/sizeof(path[0]) - 2] = userpath;
765

Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
766
    for (ppsz_path = path; *ppsz_path != NULL; ppsz_path++)
Sam Hocevar's avatar
 
Sam Hocevar committed
767
    {
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
768 769 770
        char *psz_fullpath;

        if (!**ppsz_path) continue;
771

772
#if defined( SYS_BEOS ) || defined( __APPLE__ ) || defined( WIN32 )
Sam Hocevar's avatar
 
Sam Hocevar committed
773

774
        /* Handle relative as well as absolute paths */
775
#ifdef WIN32
776 777
        if( (*ppsz_path)[0] != '\\' && (*ppsz_path)[0] != '/' &&
            (*ppsz_path)[1] != ':' )
778
#else
779
        if( (*ppsz_path)[0] != '/' )
780
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
781
        {
782 783
            if( 0>= asprintf(&psz_fullpath, "%s"DIR_SEP"%s",
                          p_this->p_libvlc_global->psz_vlcpath, *ppsz_path) )
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
784
                psz_fullpath = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
785 786 787
        }
        else
#endif
788
            psz_fullpath = strdup( *ppsz_path );
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
789 790 791

        if( psz_fullpath == NULL )
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
792

793
        msg_Dbg( p_this, "recursively browsing `%s'", psz_fullpath );
Sam Hocevar's avatar
 
Sam Hocevar committed
794

795 796
        /* Don't go deeper than 5 subdirectories */
        AllocatePluginDir( p_this, psz_fullpath, 5 );
Sam Hocevar's avatar
 
Sam Hocevar committed
797

798
        free( psz_fullpath );
Sam Hocevar's avatar
 
Sam Hocevar committed
799
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
800 801

    /* Free plugin-path */
Rémi Denis-Courmont's avatar
Cleanup  
Rémi Denis-Courmont committed
802 803
    if( userpath != NULL )
        free( userpath );
Sam Hocevar's avatar
 
Sam Hocevar committed
804 805
}

Sam Hocevar's avatar
 
Sam Hocevar committed
806
/*****************************************************************************
807 808
 * AllocatePluginDir: recursively parse a directory to look for plugins
 *****************************************************************************/
809
static void AllocatePluginDir( vlc_object_t *p_this, const char *psz_dir,
810
                               int i_maxdepth )
811
{
812 813 814 815 816 817
/* FIXME: Needs to be ported to wide char on ALL Windows builds */
#ifdef WIN32
# undef opendir
# undef closedir
# undef readdir
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
818
#if defined( UNDER_CE ) || defined( _MSC_VER )
819
#ifdef UNDER_CE
820
    wchar_t psz_wpath[MAX_PATH + 256];
821
    wchar_t psz_wdir[MAX_PATH];
Gildas Bazin's avatar
 
Gildas Bazin committed
822
#endif
823
    char psz_path[MAX_PATH + 256];
824 825
    WIN32_FIND_DATA finddata;
    HANDLE handle;
826
    int rc;
827
#else
828 829
    int    i_dirlen;
    DIR *  dir;
830
    struct dirent * file;
831
#endif
832
    char * psz_file;
833