modules.c 41.3 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 VideoLAN
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>
Sam Hocevar's avatar
 
Sam Hocevar committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

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

36
#include <vlc/vlc.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
37

38 39
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
40 41
#elif defined( UNDER_CE )
#   include <windows.h>                               /* GetFileAttributes() */
42 43 44 45
#else
#   include "../extras/dirent.h"
#endif

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

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

82
#include "vlc_error.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
83

84
#include "vlc_interface.h"
85
#include "vlc_playlist.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
86
#include "intf_eject.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
87

Sam Hocevar's avatar
 
Sam Hocevar committed
88 89
#include "stream_control.h"
#include "input_ext-intf.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
90
#include "input_ext-dec.h"
91
#include "input_ext-plugins.h"
92
#include "ninput.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
93

94
#include "vlc_video.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
95
#include "video_output.h"
96
#include "vout_synchro.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
97 98

#include "audio_output.h"
99
#include "aout_internal.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
100

101
#include "stream_output.h"
102
#include "osd.h"
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
103
#include "vlc_httpd.h"
104

Gildas Bazin's avatar
 
Gildas Bazin committed
105
#include "iso_lang.h"
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
106
#include "charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
107

108 109
#include "vlc_block.h"

110 111
#include "vlc_vlm.h"

112 113 114 115 116 117
#if defined( UNDER_CE )
#   define MYCHAR wchar_t
#else
#   define MYCHAR char
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
118
#ifdef HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
 
Sam Hocevar committed
119
#   include "modules_plugin.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
120
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
121

122 123 124
#if defined( UNDER_CE )
#    include "modules_builtin_evc.h"
#elif defined( _MSC_VER )
Gildas Bazin's avatar
 
Gildas Bazin committed
125
#    include "modules_builtin_msvc.h"
126 127
#else
#    include "modules_builtin.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
128
#endif
Laurent Aimar's avatar
 
Laurent Aimar committed
129
#include "network.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
130 131 132 133

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
134
#ifdef HAVE_DYNAMIC_PLUGINS
135
static void AllocateAllPlugins   ( vlc_object_t * );
136 137
static void AllocatePluginDir    ( vlc_object_t *, const MYCHAR *, int );
static int  AllocatePluginFile   ( vlc_object_t *, MYCHAR * );
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 145 146
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
static int    CallEntry        ( module_t * );
static void   CloseModule      ( module_handle_t );
static void * GetSymbol        ( module_handle_t, const char * );
Sam Hocevar's avatar
Sam Hocevar committed
147
#if defined(HAVE_DL_WINDOWS)
148 149
static char * GetWindowsError  ( void );
#endif
150
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
151

Sam Hocevar's avatar
 
Sam Hocevar committed
152 153 154
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
155 156
 * 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
157
 *****************************************************************************/
158
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
159
{
160 161 162 163 164
    module_bank_t *p_bank;

    p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
    p_bank->psz_object_name = "module bank";

Sam Hocevar's avatar
 
Sam Hocevar committed
165 166 167
    /*
     * Store the symbols to be exported
     */
168
#ifdef HAVE_DYNAMIC_PLUGINS
169
    STORE_SYMBOLS( &p_bank->symbols );
170
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
171

172
    /* Everything worked, attach the object */
173 174
    p_this->p_libvlc->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_this->p_libvlc );
175

Gildas Bazin's avatar
 
Gildas Bazin committed
176 177 178 179
    return;
}

/*****************************************************************************
180
 * module_ResetBank: reset the module bank.
Gildas Bazin's avatar
 
Gildas Bazin committed
181
 *****************************************************************************
182 183
 * This function resets the module bank by unloading all unused plugin
 * modules.
Gildas Bazin's avatar
 
Gildas Bazin committed
184
 *****************************************************************************/
185
void __module_ResetBank( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
186
{
187 188
    msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
189 190
}

Sam Hocevar's avatar
 
Sam Hocevar committed
191
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
192
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
193
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
194
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
195 196
 * bank in case of success.
 *****************************************************************************/
197
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
198
{
Sam Hocevar's avatar
 
Sam Hocevar committed
199 200
    module_t * p_next;

201
    vlc_object_detach( p_this->p_libvlc->p_module_bank );
202

203
    while( p_this->p_libvlc->p_module_bank->i_children )
Sam Hocevar's avatar
 
Sam Hocevar committed
204
    {
205
        p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
206 207

        if( DeleteModule( p_next ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
208 209
        {
            /* Module deletion failed */
210
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
211
                     p_next->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
212 213

            /* We just free the module by hand. Niahahahahaha. */
214 215
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
 
Sam Hocevar committed
216 217 218
        }
    }

219
    vlc_object_destroy( p_this->p_libvlc->p_module_bank );
Sam Hocevar's avatar
 
Sam Hocevar committed
220 221

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
222 223 224
}

/*****************************************************************************
225
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
226
 *****************************************************************************
227 228 229 230
 * 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
231
 *****************************************************************************/
232
void __module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
233
{
234
    AllocateBuiltinModule( p_this, vlc_entry__main );
235 236 237 238 239 240 241
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
242
void __module_LoadBuiltins( vlc_object_t * p_this )
243 244 245 246 247 248 249 250 251 252
{
    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.
 *****************************************************************************/
253
void __module_LoadPlugins( vlc_object_t * p_this )
254 255 256 257 258
{
#ifdef HAVE_DYNAMIC_PLUGINS
    msg_Dbg( p_this, "checking plugin modules" );
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
259 260 261
}

/*****************************************************************************
262
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
263
 *****************************************************************************
264
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
265
 *****************************************************************************/
266
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
267
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
268
{
269
    typedef struct module_list_t module_list_t;
270

271
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
272
    {
273
        module_t *p_module;
274
        int i_score;
275 276 277 278
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
279
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
280

Gildas Bazin's avatar
 
Gildas Bazin committed
281
    int i_which_module, i_index = 0;
282
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
 
Sam Hocevar committed
283 284

    module_t *p_module;
285

286
    int   i_shortcuts = 0;
287
    char *psz_shortcuts = NULL, *psz_var = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
288

289
    msg_Dbg( p_this, "looking for %s module", psz_capability );
290

291 292 293
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
294 295 296 297
        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;
298
        psz_name = psz_var;
299 300
    }

301
    /* Count how many different shortcuts were asked for */
302
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
303
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
304
        char *psz_parser, *psz_last_shortcut;
305

306 307 308
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
309
            if( psz_var ) free( psz_var );
310 311 312
            return NULL;
        }

313
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
314
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
315 316

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
317
        {
318
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
319
            {
320 321
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
322
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
323
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
324
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
325 326 327 328

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
329 330 331 332 333 334 335 336 337 338
            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
339
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
340
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
341

Sam Hocevar's avatar
 
Sam Hocevar committed
342
    /* Sort the modules and test them */
343 344
    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
345
    p_first = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
346

Sam Hocevar's avatar
 
Sam Hocevar committed
347
    /* Parse the module list for capabilities and probe each of them */
348
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
349
    {
350 351
        module_t * p_submodule = NULL;
        int i_shortcut_bonus = 0, i_submodule;
352

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

355 356
        /* Test that this module can do what we need */
        if( strcmp( p_module->psz_capability, psz_capability ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
357
        {
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
            for( i_submodule = 0;
                 i_submodule < p_module->i_children;
                 i_submodule++ )
            {
                if( !strcmp( ((module_t*)p_module->pp_children[ i_submodule ])
                                           ->psz_capability, psz_capability ) )
                {
                    p_submodule =
                            (module_t*)p_module->pp_children[ i_submodule ];
                    break;
                }
            }

            if( p_submodule == NULL )
            {
                continue;
            }

            p_module = p_submodule;
Sam Hocevar's avatar
 
Sam Hocevar committed
377 378
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
379
        /* Test if we have the required CPU */
380
        if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
 
Sam Hocevar committed
381
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
382
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
383
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
384

385
        /* If we required a shortcut, check this plugin provides it. */
386
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
387
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
388
            vlc_bool_t b_trash;
389 390
            int i_dummy, i_short = i_shortcuts;
            char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
 
Sam Hocevar committed
391

Gildas Bazin's avatar
 
Gildas Bazin committed
392 393 394 395
            /* Let's drop modules with a 0 score (unless they are
             * explicitly requested) */
            b_trash = !p_module->i_score;

396
            while( i_short > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
397
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
398
                for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
399
                {
400 401
                    if( !strcasecmp( psz_name,
                                     p_module->pp_shortcuts[i_dummy] ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
402 403 404 405 406 407
                    {
                        /* Found it */
                        b_trash = VLC_FALSE;
                        i_shortcut_bonus = i_short * 10000;
                        break;
                    }
408 409
                }

Gildas Bazin's avatar
 
Gildas Bazin committed
410
                if( i_shortcut_bonus )
411
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
412
                    /* We found it... remember ? */
413 414 415
                    break;
                }

416
                /* Go to the next shortcut... This is so lame! */
417 418 419 420 421 422
                while( *psz_name )
                {
                    psz_name++;
                }
                psz_name++;
                i_short--;
Sam Hocevar's avatar
 
Sam Hocevar committed
423
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
424

425 426 427 428 429 430 431 432
            /* 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!!! */
            if( i_short == 0 && b_strict )
            {
                b_trash = VLC_TRUE;
            }

433
            if( b_trash )
434
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
435 436
                continue;
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
437
        }
438
        /* If we didn't require a shortcut, trash zero-scored plugins */
439
        else if( !p_module->i_score )
440 441 442
        {
            continue;
        }
443

Sam Hocevar's avatar
 
Sam Hocevar committed
444
        /* Special case: test if we requested a particular intf plugin */
Gildas Bazin's avatar
 
Gildas Bazin committed
445
        if( !i_shortcuts && p_module->psz_program
446 447
             && !strcmp( p_module->psz_program,
                         p_this->p_vlc->psz_object_name ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
448
        {
449
            if( !b_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
450
            {
451 452 453
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
454
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
455
        }
456 457 458 459 460
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
461

Sam Hocevar's avatar
 
Sam Hocevar committed
462 463
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
464
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
Sam Hocevar's avatar
 
Sam Hocevar committed
465

Sam Hocevar's avatar
 
Sam Hocevar committed
466
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
 
Sam Hocevar committed
467 468
        if( i_index == 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
469
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
470 471 472 473 474 475 476
            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. */
477
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
478

479
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
480
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
481 482
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
 
Sam Hocevar committed
483 484
            }
            else
485
            {
486 487
                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
488
                {
Sam Hocevar's avatar
 
Sam Hocevar committed
489
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
490 491
                }

Sam Hocevar's avatar
 
Sam Hocevar committed
492 493 494
                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
495 496
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
497 498
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
499

500 501 502
    msg_Dbg( p_this, "probing %i candidate%s",
                     i_index, i_index == 1 ? "" : "s" );

503
    /* Lock all candidate modules */
504 505
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
506
    {
507
        vlc_object_yield( p_tmp->p_module );
508
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
509 510
    }

511
    /* We can release the list, interesting modules were yielded */
512
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
513

Sam Hocevar's avatar
 
Sam Hocevar committed
514
    /* Parse the linked list and use the first successful module */
515 516
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
517
    {
518 519
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
 
Sam Hocevar committed
520 521
        {
            break;
522
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
523

524
        vlc_object_release( p_tmp->p_module );
525
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
526
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
527

Sam Hocevar's avatar
 
Sam Hocevar committed
528
    /* Store the locked module value */
529
    if( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
530
    {
531 532
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
533
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
534 535 536 537
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
538

Sam Hocevar's avatar
 
Sam Hocevar committed
539
    /* Unlock the remaining modules */
540
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
541
    {
542
        vlc_object_release( p_tmp->p_module );
543
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
544
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
545

Sam Hocevar's avatar
 
Sam Hocevar committed
546
    free( p_list );
Sam Hocevar's avatar
 
Sam Hocevar committed
547

Sam Hocevar's avatar
 
Sam Hocevar committed
548
    if( p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
549
    {
550 551
        msg_Dbg( p_module, "using %s module \"%s\"",
                 psz_capability, p_module->psz_object_name );
552 553 554
    }
    else if( p_first == NULL )
    {
555 556
        msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
Sam Hocevar's avatar
 
Sam Hocevar committed
557
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
558 559
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
560 561
        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
562 563
    }

564
    if( psz_shortcuts )
Sam Hocevar's avatar
 
Sam Hocevar committed
565
    {
566
        free( psz_shortcuts );
Sam Hocevar's avatar
 
Sam Hocevar committed
567
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
568

569
    if( psz_var )
570
    {
571
        free( psz_var );
572 573
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
574
    /* Don't forget that the module is still locked */
575
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
576 577 578 579 580
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
581 582
 * 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
583
 *****************************************************************************/
584
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
585
{
586 587 588 589 590 591
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

592
    msg_Dbg( p_module, "unlocking module \"%s\"", p_module->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
593

594
    vlc_object_release( p_module );
595 596

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
597 598 599 600 601 602
}

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

Sam Hocevar's avatar
 
Sam Hocevar committed
603 604 605
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
606
#ifdef HAVE_DYNAMIC_PLUGINS
607
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
608
{
609
    /* Yes, there are two NULLs because we replace one with "plugin-path". */
610 611
    char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
                               NULL };
Sam Hocevar's avatar
 
Sam Hocevar committed
612 613 614 615

    char **         ppsz_path = path;
    char *          psz_fullpath;

616
#if defined( UNDER_CE )
617
    wchar_t         psz_dir[MAX_PATH];
618 619
#endif

620
    /* If the user provided a plugin path, we add it to the list */
621
    path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
622 623
                                                            "plugin-path" );

Sam Hocevar's avatar
 
Sam Hocevar committed
624 625
    for( ; *ppsz_path != NULL ; ppsz_path++ )
    {
626 627
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
     || ( defined( WIN32 ) && !defined( UNDER_CE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
628

629
        /* Handle relative as well as absolute paths */
630
#ifdef WIN32
631
        if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
632
#else
633
        if( (*ppsz_path)[0] != '/' )
634
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
635
        {
636 637
            int i_dirlen = strlen( *ppsz_path );
            i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
Sam Hocevar's avatar
 
Sam Hocevar committed
638 639 640 641 642 643

            psz_fullpath = malloc( i_dirlen );
            if( psz_fullpath == NULL )
            {
                continue;
            }
644 645 646 647
#ifdef WIN32
            sprintf( psz_fullpath, "%s\\%s",
                     p_this->p_libvlc->psz_vlcpath, *ppsz_path );
#else
648 649
            sprintf( psz_fullpath, "%s/%s",
                     p_this->p_libvlc->psz_vlcpath, *ppsz_path );
650
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
651 652 653 654
        }
        else
#endif
        {
655
            psz_fullpath = strdup( *ppsz_path );
Sam Hocevar's avatar
 
Sam Hocevar committed
656 657
        }

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

660
        /* Don't go deeper than 5 subdirectories */
661 662 663 664
#if defined( UNDER_CE )
        MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
        AllocatePluginDir( p_this, psz_dir, 5 );
#else
665
        AllocatePluginDir( p_this, psz_fullpath, 5 );
666
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
667

668
        free( psz_fullpath );
Sam Hocevar's avatar
 
Sam Hocevar committed
669
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
670 671

    /* Free plugin-path */
672 673
    if( path[ sizeof(path)/sizeof(char*) - 2 ] )
        free( path[ sizeof(path)/sizeof(char*) - 2 ] );
Gildas Bazin's avatar
 
Gildas Bazin committed
674
    path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
675 676
}

Sam Hocevar's avatar
 
Sam Hocevar committed
677
/*****************************************************************************
678 679
 * AllocatePluginDir: recursively parse a directory to look for plugins
 *****************************************************************************/
680
static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
681
                               int i_maxdepth )
682
{
Gildas Bazin's avatar
 
Gildas Bazin committed
683
#if defined( UNDER_CE ) || defined( _MSC_VER )
684 685
#ifdef UNDER_CE
    MYCHAR psz_path[MAX_PATH + 256];
Gildas Bazin's avatar
 
Gildas Bazin committed
686 687 688
#else
    char psz_path[MAX_PATH + 256];
#endif
689 690 691 692
    WIN32_FIND_DATA finddata;
    HANDLE handle;
    unsigned int rc;
#else
693 694 695
    int    i_dirlen;
    DIR *  dir;
    char * psz_file;
696
    struct dirent * file;
697
#endif
698

699
    if( p_this->p_vlc->b_die || i_maxdepth < 0 )
700 701 702 703
    {
        return;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
704
#if defined( UNDER_CE ) || defined( _MSC_VER )
705 706 707 708 709 710 711 712
    rc = GetFileAttributes( psz_dir );
    if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
    {
        /* Not a directory */
        return;
    }

    /* Parse all files in the directory */
Gildas Bazin's avatar
 
Gildas Bazin committed
713
#ifdef UNDER_CE
714
    swprintf( psz_path, L"%s\\*.*", psz_dir );
Gildas Bazin's avatar
 
Gildas Bazin committed
715 716 717
#else
    sprintf( psz_path, "%s\\*.*", psz_dir );
#endif
718 719 720 721 722 723 724 725 726 727
    handle = FindFirstFile( psz_path, &finddata );
    if( handle == INVALID_HANDLE_VALUE )
    {
        /* Empty directory */
        return;
    }

    /* Parse the directory and try to load all files it contains. */
    do
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
728
#ifdef UNDER_CE
729 730
        unsigned int i_len = wcslen( finddata.cFileName );
        swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
Gildas Bazin's avatar
 
Gildas Bazin committed
731 732 733 734 735 736 737 738 739 740
#else
        unsigned int i_len = strlen( finddata.cFileName );
        /* Skip ".", ".." and anything starting with "." */
        if( !*finddata.cFileName || *finddata.cFileName == '.' )
        {
            if( !FindNextFile( handle, &finddata ) ) break;
            continue;
        }
        sprintf( psz_path, "%s\\%s", psz_dir, finddata.cFileName );
#endif
741 742 743 744 745

        if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
        {
            AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
746 747 748 749 750 751 752 753 754
        else if( i_len > strlen( LIBEXT )
#ifdef UNDER_CE
                )
#else
                  /* We only load files ending with LIBEXT */
                  && !strncasecmp( psz_path + strlen( psz_path)
                                   - strlen( LIBEXT ),
                                   LIBEXT, strlen( LIBEXT ) ) )
#endif
755 756 757 758
        {
            AllocatePluginFile( p_this, psz_path );
        }
    }
759
    while( !p_this->p_vlc->b_die && FindNextFile( handle, &finddata ) );
760

761 762 763 764 765
    /* Close the directory */
    FindClose( handle );

#else
    dir = opendir( psz_dir );
766
    if( !dir )
767
    {
768 769 770 771 772 773
        return;
    }

    i_dirlen = strlen( psz_dir );

    /* Parse the directory and try to load all files it contains. */
774
    while( !p_this->p_vlc->b_die && (file = readdir( dir )) )
775 776
    {
        struct stat statbuf;
777
        unsigned int i_len;
778 779 780

        /* Skip ".", ".." and anything starting with "." */
        if( !*file->d_name || *file->d_name == '.' )
781
        {
782 783
            continue;
        }
784

785
        i_len = strlen( file->d_name );
786
        psz_file = malloc( i_dirlen + 1 + i_len + 1 );
787 788 789
#ifdef WIN32
        sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
#else
790
        sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
791
#endif
792

793 794
        if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
        {
795
            AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
796
        }
797 798
        else if( i_len > strlen( LIBEXT )
                  /* We only load files ending with LIBEXT */
799 800
                  && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
                                   LIBEXT, strlen( LIBEXT ) ) )
801
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
802
            AllocatePluginFile( p_this, psz_file );
803 804
        }

805
        free( psz_file );
806
    }
807 808 809

    /* Close the directory */
    closedir( dir );
810 811

#endif
812 813 814 815
}

/*****************************************************************************
 * AllocatePluginFile: load a module into memory and initialize it.
Sam Hocevar's avatar
 
Sam Hocevar committed
816 817
 *****************************************************************************
 * This function loads a dynamically loadable module and allocates a structure
818 819
 * for its information data. The module can then be handled by module_Need
 * and module_Unneed. It can be removed by DeleteModule.
Sam Hocevar's avatar
 
Sam Hocevar committed
820
 *****************************************************************************/
821
static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
Sam Hocevar's avatar
 
Sam Hocevar committed
822
{
823
    module_t * p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
824 825
    module_handle_t handle;

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    /*
     * Try to dynamically load the module. This part is very platform-
     * specific, and error messages should be as verbose as possible.
     */

#if defined(HAVE_DL_DYLD)
    NSObjectFileImage image;
    NSObjectFileImageReturnCode ret;

    ret = NSCreateObjectFileImageFromFile( psz_file, &image );

    if( ret != NSObjectFileImageSuccess )
    {
        msg_Warn( p_this, "cannot create image from `%s'", psz_file );
        return -1;
    }

    /* Open the dynamic module */
    handle = NSLinkModule( image, psz_file,
                           NSLINKMODULE_OPTION_RETURN_ON_ERROR );

    if( !handle )
    {
        NSLinkEditErrors errors;
        const char *psz_file, *psz_err;
        int i_errnum;
        NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
        msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
        NSDestroyObjectFileImage( image );
        return -1;
    }

    /* Destroy our image, we won't need it */
    NSDestroyObjectFileImage( image );

Sam Hocevar's avatar
Sam Hocevar committed
861
#elif defined(HAVE_DL_BEOS)
862 863 864 865 866 867 868
    handle = load_add_on( psz_file );
    if( handle < 0 )
    {
        msg_Warn( p_this, "cannot load module `%s'", psz_file );
        return -1;
    }

Sam Hocevar's avatar
Sam Hocevar committed
869
#elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
870 871 872
    char psz_filename[MAX_PATH];
    WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
                         psz_filename, MAX_PATH, NULL, NULL );
873 874 875 876 877 878 879 880 881
    handle = LoadLibrary( psz_filename );
    if( handle == NULL )
    {
        char *psz_error = GetWindowsError();
        msg_Warn( p_this, "cannot load module `%s' (%s)",
                          psz_filename, psz_error );
        free( psz_error );
    }

Sam Hocevar's avatar
Sam Hocevar committed
882
#elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
883 884 885 886 887
    handle = LoadLibrary( psz_file );
    if( handle == NULL )
    {
        char *psz_error = GetWindowsError();
        msg_Warn( p_this, "cannot load module `%s' (%s)",
888
                          psz_file, psz_error );
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
        free( psz_error );
    }

#elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
    /* static is OK, we are called atomically */
    static vlc_bool_t b_kde = VLC_FALSE;

#   if defined(SYS_LINUX)
    /* XXX HACK #1 - we should NOT open modules with RTLD_GLOBAL, or we
     * are going to get namespace collisions when two modules have common
     * public symbols, but ALSA is being a pest here. */
    if( strstr( psz_file, "alsa_plugin" ) )
    {
        handle = dlopen( psz_file, RTLD_NOW | RTLD_GLOBAL );
        if( handle == NULL )
        {
            msg_Warn( p_this, "cannot load module `%s' (%s)",
                              psz_file, dlerror() );
            return -1;
        }
    }
#   endif
    /* XXX HACK #2 - the ugly KDE workaround. It seems that libkdewhatever
     * causes dlopen() to segfault if libstdc++ is not loaded in the caller,
     * so we just load libstdc++. Bwahahaha! ph34r! -- Sam. */
    /* Update: FYI, this is Debian bug #180505, and seems to be fixed. */
    if( !b_kde && !strstr( psz_file, "kde" ) )
    {
        dlopen( "libstdc++.so.6", RTLD_NOW )
         || dlopen( "libstdc++.so.5", RTLD_NOW )
         || dlopen( "libstdc++.so.4", RTLD_NOW )
         || dlopen( "libstdc++.so.3", RTLD_NOW );
        b_kde = VLC_TRUE;
    }
923

924 925
    handle = dlopen( psz_file, RTLD_NOW );
    if( handle == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
926
    {
927 928 929 930
        msg_Warn( p_this, "cannot load module `%s' (%s)",
                          psz_file, dlerror() );
        return -1;
    }
931

Sam Hocevar's avatar
Sam Hocevar committed
932 933
#elif defined(HAVE_DL_DLOPEN)
#   if defined(DL_LAZY)
934
    handle = dlopen( psz_file, DL_LAZY );
Sam Hocevar's avatar
Sam Hocevar committed
935 936 937
#   else
    handle = dlopen( psz_file, 0 );
#   endif
938 939 940 941
    if( handle == NULL )
    {
        msg_Warn( p_this, "cannot load module `%s' (%s)",
                          psz_file, dlerror() );
942
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
943 944
    }

945 946 947 948 949 950 951 952 953 954 955 956 957 958
#elif defined(HAVE_DL_SHL_LOAD)
    handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
    if( handle == NULL )
    {
        msg_Warn( p_this, "cannot load module `%s' (%s)",
                          psz_file, strerror(errno) );
        return -1;
    }

#else
#   error "Something is wrong in modules.c"

#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
959
    /* Now that we have successfully loaded the module, we can
960
     * allocate a structure for it */
961
    p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
Sam Hocevar's avatar
 
Sam Hocevar committed
962 963
    if( p_module == NULL )
    {
964
        msg_Err( p_this, "out of memory" );
965
        CloseModule( handle );
966
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
967 968
    }

969
    /* We need to fill these since they may be needed by CallEntry() */
970
#ifdef UNDER_CE
971
    p_module->psz_filename = psz_filename;
972 973 974
#else
    p_module->psz_filename = psz_file;
#endif
975
    p_module->handle = handle;
976
    p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
Sam Hocevar's avatar
 
Sam Hocevar committed
977

978
    /* Initialize the module: fill p_module->psz_object_name, default config */
979
    if( CallEntry( p_module ) != 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
980
    {
981
        /* We couldn't call module_init() */
982
        vlc_object_destroy( p_module );
983
        CloseModule( handle );
984
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
985 986
    }

987 988 989 990 991 992
    DupModule( p_module );
    p_module->psz_filename = strdup( p_module->psz_filename );
    p_module->psz_longname = strdup( p_module->psz_longname );

    /* Everything worked fine ! The module is ready to be added to the list. */
    p_module->b_builtin = VLC_FALSE;
993

994 995
    /* msg_Dbg( p_this, "plugin \"%s\", %s",
                p_module->psz_object_name, p_module->psz_longname ); */
996

997
    vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

    return 0;
}

/*****************************************************************************
 * DupModule: make a plugin module standalone.
 *****************************************************************************
 * This function duplicates all strings in the module, so that the dynamic
 * object can be unloaded. It acts recursively on submodules.
 *****************************************************************************/
static void DupModule( module_t *p_module )
{
    char **pp_shortcut;
    int i_submodule;
Sam Hocevar's avatar
 
Sam Hocevar committed
1012

Sam Hocevar's avatar
 
Sam Hocevar committed
1013 1014 1015 1016 1017
    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
    {
        *pp_shortcut = strdup( *pp_shortcut );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
1018 1019
    /* We strdup() these entries so that they are still valid when the
     * module is unloaded. */
1020
    p_module->psz_object_name = strdup( p_module->psz_object_name );
1021
    p_module->psz_capability = strdup( p_module->psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
1022

Sam Hocevar's avatar
 
Sam Hocevar committed
1023 1024 1025 1026 1027
    if( p_module->psz_program != NULL )
    {
        p_module->psz_program = strdup( p_module->psz_program );
    }

1028 1029 1030 1031 1032
    for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
    {
        DupModule( (module_t*)p_module->pp_children[ i_submodule ] );
    }
}
Sam Hocevar's avatar
 
Sam Hocevar committed
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042
/*****************************************************************************
 * UndupModule: free a duplicated module.
 *****************************************************************************
 * This function frees the allocations done in DupModule().
 *****************************************************************************/
static void UndupModule( module_t *p_module )
{
    char **pp_shortcut;
    int i_submodule;
Sam Hocevar's avatar
 
Sam Hocevar committed
1043

1044
    for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
1045
    {
1046
        UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
Sam Hocevar's avatar
 
Sam Hocevar committed
1047 1048
    }

1049 1050 1051 1052
    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
    {
        free( *pp_shortcut );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
1053

1054 1055
    free( p_module->psz_object_name );
    free( p_module->psz_capability );
1056

1057 1058 1059 1060
    if( p_module->psz_program != NULL )
    {
        free( p_module->psz_program );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
1061
}
1062

Sam Hocevar's avatar
 
Sam Hocevar committed
1063
#endif /* HAVE_DYNAMIC_PLUGINS */
Sam Hocevar's avatar
 
Sam Hocevar committed
1064 1065

/*****************************************************************************
1066
 * AllocateBuiltinModule: initialize a builtin module.
Sam Hocevar's avatar
 
Sam Hocevar committed
1067
 *****************************************************************************
1068
 * This function registers a builtin module and allocates a structure
1069 1070
 * for its information data. The module can then be handled by module_Need
 * and module_Unneed. It can be removed by DeleteModule.
Sam Hocevar's avatar
 
Sam Hocevar committed
1071
 *****************************************************************************/
1072
static int AllocateBuiltinModule( vlc_object_t * p_this,
1073
                                  int ( *pf_entry ) ( module_t * ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
1074
{
1075
    module_t * p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
1076 1077

    /* Now that we have successfully loaded the module, we can
1078
     * allocate a structure for it */
1079
    p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
Sam Hocevar's avatar
 
Sam Hocevar committed
1080
    if( p_module == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
1081
    {
1082 1083
        msg_Err( p_this, "out of memory" );
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
1084 1085
    }

1086
    /* Initialize the module : fill p_module->psz_object_name, etc. */
1087
    if( pf_entry( p_module ) != 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
1088 1089 1090
    {
        /* With a well-written module we shouldn't have to print an
         * additional error message here, but just make sure. */
1091
        msg_Err( p_this, "failed calling entry point in builtin module" );
1092 1093
        vlc_object_destroy( p_module );
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
1094 1095
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
1096
    /* Everything worked fine ! The module is ready to be added to the list. */
1097
    p_module->b_builtin = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
1098

1099 1100
    /* msg_Dbg( p_this, "builtin \"%s\", %s",
                p_module->psz_object_name, p_module->psz_longname ); */
Sam Hocevar's avatar
 
Sam Hocevar committed
1101

1102
    vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
1103 1104

    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
1105 1106 1107
}

/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
1108
 * DeleteModule: delete a module and its structure.
Sam Hocevar's avatar
 
Sam Hocevar committed
1109
 *****************************************************************************
1110
 * This function can only be called if the module isn't being used.
Sam Hocevar's avatar
 
Sam Hocevar committed
1111
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
1112
static int DeleteModule( module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
1113
{
1114
    vlc_object_detach( p_module );
1115

Sam Hocevar's avatar
 
Sam Hocevar committed
1116
    /* We free the structures that we strdup()ed in Allocate*Module(). */
Sam Hocevar's avatar
 
Sam Hocevar committed
1117 1118 1119
#ifdef HAVE_DYNAMIC_PLUGINS
    if( !p_module->b_builtin )
    {
1120 1121
        if( p_module->b_unloadable )
        {
1122
            CloseModule( p_module->handle );
1123
        }
1124 1125
        UndupModule( p_module );
        free( p_module->psz_filename );
Sam Hocevar's avatar
 
Sam Hocevar committed
1126
        free( p_module->psz_longname );
Sam Hocevar's avatar
 
Sam Hocevar committed
1127 1128
    }
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
1129

1130 1131 1132 1133
    /* Free and detach the object's children */
    while( p_module->i_children )
    {
        vlc_object_t *p_this = p_module->pp_children[0];
1134
        vlc_object_detach( p_this );
1135 1136 1137
        vlc_object_destroy( p_this );
    }

1138 1139
    config_Free( p_module );
    vlc_object_destroy( p_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
1140

1141
    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
1142 1143
}

Sam Hocevar's avatar
 
Sam Hocevar committed
1144
#ifdef HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
 
Sam Hocevar committed
1145
/*****************************************************************************
1146
 * CallEntry: call an entry point.
Sam Hocevar's avatar
 
Sam Hocevar committed
1147 1148 1149 1150 1151
 *****************************************************************************
 * This function calls a symbol given its name and a module structure. The
 * symbol MUST refer to a function returning int and taking a module_t* as
 * an argument.
 *****************************************************************************/
1152
static int CallEntry( module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
1153
{
1154
    static char *psz_name = "vlc_entry" MODULE_SUFFIX;
Sam Hocevar's avatar
 
Sam Hocevar committed
1155
    int (* pf_symbol) ( module_t * p_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
1156 1157

    /* Try to resolve the symbol */
1158
    pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
1159

Sam Hocevar's avatar
 
Sam Hocevar committed
1160
    if( pf_symbol == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
1161
    {
Sam Hocevar's avatar
Sam Hocevar committed
1162
#if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
1163 1164
        msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s'",
                            psz_name, p_module->psz_filename );
Sam Hocevar's avatar
Sam Hocevar committed
1165
#elif defined(HAVE_DL_WINDOWS)
1166
        char *psz_error = GetWindowsError();
1167
        msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
                            psz_name, p_module->psz_filename, psz_error );
        free( psz_error );
#elif defined(HAVE_DL_DLOPEN)
        msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
                            psz_name, p_module->psz_filename, dlerror() );
#elif defined(HAVE_DL_SHL_LOAD)
        msg_Warn( p_module, "cannot find symbol \"%s\" in file `%s' (%s)",
                            psz_name, p_module->psz_filename, strerror(errno) );
#else
#   error "Something is wrong in modules.c"
#endif
1179
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
1180 1181 1182
    }

    /* We can now try to call the symbol */
Sam Hocevar's avatar
 
Sam Hocevar committed
1183
    if( pf_symbol( p_module ) != 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
1184 1185 1186
    {
        /* With a well-written module we shouldn't have to print an
         * additional error message here, but just make sure. */
1187 1188
        msg_Err( p_module, "failed calling symbol \"%s\" in file `%s'",
                           psz_name, p_module->psz_filename );
1189
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
1190 1191 1192
    }

    /* Everything worked fine, we can return */
1193
    return 0;
Sam Hocevar's avatar
 
Sam Hocevar committed
1194
}
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

/*****************************************************************************
 * CloseModule: unload a dynamic library
 *****************************************************************************
 * This function unloads a previously opened dynamically linked library
 * using a system dependant method. No return value is taken in consideration,
 * since some libraries sometimes refuse to close properly.
 *****************************************************************************/
static void CloseModule( module_handle_t handle )
{
#if defined(HAVE_DL_DYLD)
    NSUnLinkModule( handle, FALSE );

Sam Hocevar's avatar
Sam Hocevar committed
1208
#elif defined(HAVE_DL_BEOS)
1209 1210
    unload_add_on( handle );

Sam Hocevar's avatar
Sam Hocevar committed
1211
#elif defined(HAVE_DL_WINDOWS)
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    FreeLibrary( handle );

#elif defined(HAVE_DL_DLOPEN)
    dlclose( handle );

#elif defined(HAVE_DL_SHL_LOAD)
    shl_unload( handle );

#endif
    return;
}

/*****************************************************************************
 * GetSymbol: get a symbol from a dynamic library
 *****************************************************************************
 * This function queries a loaded library for a symbol specified in a
 * string, and returns a pointer to it. We don't check for dlerror() or
 * similar functions, since we want a non-NULL symbol anyway.
 *****************************************************************************/
static void * _module_getsymbol( module_handle_t, const char * );

static void * GetSymbol( module_handle_t handle, const char * psz_function )
{
    void * p_symbol = _module_getsymbol( handle, psz_function );

    /* MacOS X dl library expects symbols to begin with "_". So do
     * some other operating systems. That's really lame, but hey, what
     * can we do ? */
    if( p_symbol == NULL )
    {
        char *psz_call = malloc( strlen( psz_function ) + 2 );

        strcpy( psz_call + 1, psz_function );
        psz_call[ 0 ] = '_';
        p_symbol = _module_getsymbol( handle, psz_call );
        free( psz_call );
    }

    return p_symbol;
}

static void * _module_getsymbol( module_handle_t handle,
                                 const char * psz_function )
{
#if defined(HAVE_DL_DYLD)
    NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
    return NSAddressOfSymbol( sym );

Sam Hocevar's avatar
Sam Hocevar committed
1260
#elif defined(HAVE_DL_BEOS)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    void * p_symbol;
    if( B_OK == get_image_symbol( handle, psz_function,
                                  B_SYMBOL_TYPE_TEXT, &p_symbol ) )
    {
        return p_symbol;
    }
    else
    {
        return NULL;
    }

Sam Hocevar's avatar
Sam Hocevar committed
1272
#elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
1273 1274 1275 1276 1277
    wchar_t psz_real[256];
    MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );

    return (void *)GetProcAddress( handle, psz_real );

Sam Hocevar's avatar
Sam Hocevar committed
1278
#elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
    return (void *)GetProcAddress( handle, (MYCHAR*)psz_function );

#elif defined(HAVE_DL_DLOPEN)
    return dlsym( handle, psz_function );

#elif defined(HAVE_DL_SHL_LOAD)
    void *p_sym;
    shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
    return p_sym;

#endif
}

Sam Hocevar's avatar
Sam Hocevar committed
1292
#if defined(HAVE_DL_WINDOWS)
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
static char * GetWindowsError( void )
{
#if defined(UNDER_CE)
    wchar_t psz_tmp[256];
    char * psz_buffer = malloc( 256 );
#else
    char * psz_tmp = malloc( 256 );
#endif
    int i = 0, i_error = GetLastError();

    FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                   NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                   (LPTSTR) psz_tmp, 256, NULL );

    /* Go to the end of the string */
#if defined(UNDER_CE)
    while( psz_tmp[i] && psz_tmp[i] != L'\r' && psz_tmp[i] != L'\n' )
#else
    while( psz_tmp[i] && psz_tmp[i] != '\r' && psz_tmp[i] != '\n' )
#endif
    {
        i++;
    }

    if( psz_tmp[i] )
    {
#if defined(UNDER_CE)
        swprintf( psz_tmp + i, L" (error %i)", i_error );
        psz_tmp[ 255 ] = L'\0';
#else
        snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
        psz_tmp[ 255 ] = '\0';
#endif
    }

#if defined(UNDER_CE)
    WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_tmp, -1,
                         psz_buffer, 256, NULL, NULL );
    return psz_buffer;
#else
    return psz_tmp;
#endif
}
Sam Hocevar's avatar
Sam Hocevar committed
1336
#endif /* HAVE_DL_WINDOWS */
1337

Sam Hocevar's avatar
 
Sam Hocevar committed
1338
#endif /* HAVE_DYNAMIC_PLUGINS */