modules.c 32.2 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1
/*****************************************************************************
2
 * modules.c : Builtin and plugin modules management functions
Sam Hocevar's avatar
 
Sam Hocevar committed
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: modules.c,v 1.133 2003/09/26 11:30:06 gbazin Exp $
Sam Hocevar's avatar
 
Sam Hocevar committed
6
7
 *
 * Authors: Samuel 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
56

#if defined(HAVE_DLFCN_H)                                /* Linux, BSD, Hurd */
Sam Hocevar's avatar
   
Sam Hocevar committed
57
#   include <dlfcn.h>                        /* dlopen(), dlsym(), dlclose() */
Sam Hocevar's avatar
   
Sam Hocevar committed
58
#   define HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
 
Sam Hocevar committed
59
#elif defined(HAVE_IMAGE_H)                                          /* BeOS */
Sam Hocevar's avatar
   
Sam Hocevar committed
60
#   include <image.h>
Sam Hocevar's avatar
   
Sam Hocevar committed
61
#   define HAVE_DYNAMIC_PLUGINS
62
63
#elif defined(UNDER_CE)
#   define HAVE_DYNAMIC_PLUGINS
gbazin's avatar
   
gbazin committed
64
#elif defined(WIN32)
Sam Hocevar's avatar
   
Sam Hocevar committed
65
#   define HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
 
Sam Hocevar committed
66
#else
Sam Hocevar's avatar
   
Sam Hocevar committed
67
#   undef HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
 
Sam Hocevar committed
68
69
#endif

70
#include "vlc_error.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
71

72
#include "vlc_interface.h"
73
#include "vlc_playlist.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
74
#include "intf_eject.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
75

Sam Hocevar's avatar
   
Sam Hocevar committed
76
77
#include "stream_control.h"
#include "input_ext-intf.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
78
#include "input_ext-dec.h"
79
#include "input_ext-plugins.h"
80
#include "ninput.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
81

82
#include "vlc_video.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
83
#include "video_output.h"
84
#include "vout_synchro.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
85
86

#include "audio_output.h"
87
#include "aout_internal.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
88

89
#include "stream_output.h"
90
/*#include "announce.h"*/
91
#include "osd.h"
92

gbazin's avatar
   
gbazin committed
93
#include "iso_lang.h"
hartman's avatar
hartman committed
94
#include "charset.h"
gbazin's avatar
   
gbazin committed
95

96
97
#include "vlc_block.h"

98
99
100
101
102
103
#if defined( UNDER_CE )
#   define MYCHAR wchar_t
#else
#   define MYCHAR char
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
104
#ifdef HAVE_DYNAMIC_PLUGINS
Sam Hocevar's avatar
   
Sam Hocevar committed
105
#   include "modules_plugin.h"
Sam Hocevar's avatar
   
Sam Hocevar committed
106
#endif
gbazin's avatar
   
gbazin committed
107

108
109
110
#if defined( UNDER_CE )
#    include "modules_builtin_evc.h"
#elif defined( _MSC_VER )
gbazin's avatar
   
gbazin committed
111
#    include "modules_builtin_msvc.h"
112
113
#else
#    include "modules_builtin.h"
gbazin's avatar
   
gbazin committed
114
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
115
116
117
118

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
119
#ifdef HAVE_DYNAMIC_PLUGINS
120
static void AllocateAllPlugins   ( vlc_object_t * );
121
122
static void AllocatePluginDir    ( vlc_object_t *, const MYCHAR *, int );
static int  AllocatePluginFile   ( vlc_object_t *, MYCHAR * );
Sam Hocevar's avatar
   
Sam Hocevar committed
123
#endif
124
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
125
static int  DeleteModule ( module_t * );
Sam Hocevar's avatar
   
Sam Hocevar committed
126
#ifdef HAVE_DYNAMIC_PLUGINS
127
128
129
static void DupModule    ( module_t * );
static void UndupModule  ( module_t * );
static int  CallEntry    ( module_t * );
130
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
131

Sam Hocevar's avatar
   
Sam Hocevar committed
132
133
134
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
gbazin's avatar
   
gbazin committed
135
136
 * 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
137
 *****************************************************************************/
138
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
139
{
140
141
142
143
144
    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
145
146
147
    /*
     * Store the symbols to be exported
     */
148
#ifdef HAVE_DYNAMIC_PLUGINS
149
    STORE_SYMBOLS( &p_bank->symbols );
150
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
151

152
    /* Everything worked, attach the object */
153
154
    p_this->p_libvlc->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_this->p_libvlc );
155

gbazin's avatar
   
gbazin committed
156
157
158
159
    return;
}

/*****************************************************************************
160
 * module_ResetBank: reset the module bank.
gbazin's avatar
   
gbazin committed
161
 *****************************************************************************
162
163
 * This function resets the module bank by unloading all unused plugin
 * modules.
gbazin's avatar
   
gbazin committed
164
 *****************************************************************************/
165
void __module_ResetBank( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
166
{
167
168
    msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
    return;
Sam Hocevar's avatar
   
Sam Hocevar committed
169
170
}

Sam Hocevar's avatar
 
Sam Hocevar committed
171
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
172
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
173
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
174
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
175
176
 * bank in case of success.
 *****************************************************************************/
177
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
178
{
Sam Hocevar's avatar
   
Sam Hocevar committed
179
180
    module_t * p_next;

181
    vlc_object_detach( p_this->p_libvlc->p_module_bank );
182

183
    while( p_this->p_libvlc->p_module_bank->i_children )
Sam Hocevar's avatar
   
Sam Hocevar committed
184
    {
185
        p_next = (module_t *)p_this->p_libvlc->p_module_bank->pp_children[0];
186
187

        if( DeleteModule( p_next ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
188
189
        {
            /* Module deletion failed */
190
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
191
                     p_next->psz_object_name );
Sam Hocevar's avatar
   
Sam Hocevar committed
192
193

            /* We just free the module by hand. Niahahahahaha. */
194
195
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
   
Sam Hocevar committed
196
197
198
        }
    }

199
    vlc_object_destroy( p_this->p_libvlc->p_module_bank );
Sam Hocevar's avatar
   
Sam Hocevar committed
200
201

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
202
203
204
}

/*****************************************************************************
205
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
206
 *****************************************************************************
207
208
209
210
 * 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
211
 *****************************************************************************/
212
void __module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
213
{
214
    AllocateBuiltinModule( p_this, vlc_entry__main );
215
216
217
218
219
220
221
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
222
void __module_LoadBuiltins( vlc_object_t * p_this )
223
224
225
226
227
228
229
230
231
232
{
    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.
 *****************************************************************************/
233
void __module_LoadPlugins( vlc_object_t * p_this )
234
235
236
237
238
{
#ifdef HAVE_DYNAMIC_PLUGINS
    msg_Dbg( p_this, "checking plugin modules" );
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
239
240
241
}

/*****************************************************************************
242
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
243
 *****************************************************************************
244
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
245
 *****************************************************************************/
246
247
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
                          const char *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
248
{
249
    typedef struct module_list_t module_list_t;
250

251
    struct module_list_t
Sam Hocevar's avatar
   
Sam Hocevar committed
252
    {
253
        module_t *p_module;
254
        int i_score;
255
256
257
258
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
259
    vlc_list_t *p_all;
Sam Hocevar's avatar
   
Sam Hocevar committed
260

gbazin's avatar
   
gbazin committed
261
    int i_which_module, i_index = 0;
262
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
   
Sam Hocevar committed
263
264

    module_t *p_module;
265

266
    int   i_shortcuts = 0;
267
    char *psz_shortcuts = NULL, *psz_var = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
268

269
    msg_Dbg( p_this, "looking for %s module", psz_capability );
270

271
272
273
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
274
275
        psz_var = config_GetPsz( p_this, psz_name + 1 );
        psz_name = psz_var;
276
277
    }

278
    /* Count how many different shortcuts were asked for */
279
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
280
    {
281
282
        char *psz_parser;

283
284
285
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
286
            if( psz_var ) free( psz_var );
287
288
289
            return NULL;
        }

290
        i_shortcuts++;
291
292
293
        psz_shortcuts = strdup( psz_name );

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
294
        {
295
            if( *psz_parser == ',' )
Sam Hocevar's avatar
   
Sam Hocevar committed
296
            {
297
298
                 *psz_parser = '\0';
                 i_shortcuts++;
Sam Hocevar's avatar
   
Sam Hocevar committed
299
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
300
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
301
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
302

Sam Hocevar's avatar
   
Sam Hocevar committed
303
    /* Sort the modules and test them */
304
305
    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
306
    p_first = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
307

Sam Hocevar's avatar
   
Sam Hocevar committed
308
    /* Parse the module list for capabilities and probe each of them */
309
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
   
Sam Hocevar committed
310
    {
311
312
        module_t * p_submodule = NULL;
        int i_shortcut_bonus = 0, i_submodule;
313

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

316
317
        /* Test that this module can do what we need */
        if( strcmp( p_module->psz_capability, psz_capability ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
318
        {
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
            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
338
339
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
340
        /* Test if we have the required CPU */
341
        if( (p_module->i_cpu & p_this->p_libvlc->i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
   
Sam Hocevar committed
342
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
343
            continue;
Sam Hocevar's avatar
   
Sam Hocevar committed
344
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
345

346
        /* If we required a shortcut, check this plugin provides it. */
347
        if( i_shortcuts )
Sam Hocevar's avatar
   
Sam Hocevar committed
348
        {
gbazin's avatar
   
gbazin committed
349
            vlc_bool_t b_trash;
350
351
            int i_dummy, i_short = i_shortcuts;
            char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
   
Sam Hocevar committed
352

gbazin's avatar
   
gbazin committed
353
354
355
356
            /* Let's drop modules with a 0 score (unless they are
             * explicitly requested) */
            b_trash = !p_module->i_score;

357
            while( i_short )
Sam Hocevar's avatar
   
Sam Hocevar committed
358
            {
gbazin's avatar
   
gbazin committed
359
360
361
362
363
364
365
366
367
368
                /* If the last given shortcut is "none" and we couldn't
                 * find the module in the list of provided shortcuts,
                 * then kick the bastard out of here!!! */
                if( (i_short == 1) && !strcmp(psz_name, "none") )
                {
                    b_trash = VLC_TRUE;
                    break;
                }

                for( i_dummy = 0; p_module->pp_shortcuts[i_dummy]; i_dummy++ )
369
                {
370
371
                    if( !strcasecmp( psz_name,
                                     p_module->pp_shortcuts[i_dummy] ) )
gbazin's avatar
   
gbazin committed
372
373
374
375
376
377
                    {
                        /* Found it */
                        b_trash = VLC_FALSE;
                        i_shortcut_bonus = i_short * 10000;
                        break;
                    }
378
379
                }

gbazin's avatar
   
gbazin committed
380
                if( i_shortcut_bonus )
381
                {
gbazin's avatar
   
gbazin committed
382
                    /* We found it... remember ? */
383
384
385
                    break;
                }

386
                /* Go to the next shortcut... This is so lame! */
387
388
389
390
391
392
                while( *psz_name )
                {
                    psz_name++;
                }
                psz_name++;
                i_short--;
Sam Hocevar's avatar
   
Sam Hocevar committed
393
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
394

395
            if( b_trash )
396
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
397
398
                continue;
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
399
        }
400
        /* If we didn't require a shortcut, trash zero-scored plugins */
401
        else if( !p_module->i_score )
402
403
404
        {
            continue;
        }
405

Sam Hocevar's avatar
   
Sam Hocevar committed
406
        /* Special case: test if we requested a particular intf plugin */
gbazin's avatar
   
gbazin committed
407
        if( !i_shortcuts && p_module->psz_program
408
409
             && !strcmp( p_module->psz_program,
                         p_this->p_vlc->psz_object_name ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
410
        {
411
            if( !b_intf )
Sam Hocevar's avatar
   
Sam Hocevar committed
412
            {
413
414
415
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
   
Sam Hocevar committed
416
            }
Sam Hocevar's avatar
   
Sam Hocevar committed
417
        }
418
419
420
421
422
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
423

Sam Hocevar's avatar
   
Sam Hocevar committed
424
425
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
426
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
Sam Hocevar's avatar
   
Sam Hocevar committed
427

Sam Hocevar's avatar
   
Sam Hocevar committed
428
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
   
Sam Hocevar committed
429
430
        if( i_index == 0 )
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
431
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
432
433
434
435
436
437
438
            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. */
439
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
   
Sam Hocevar committed
440

441
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
   
Sam Hocevar committed
442
            {
Sam Hocevar's avatar
   
Sam Hocevar committed
443
444
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
   
Sam Hocevar committed
445
446
            }
            else
447
            {
448
449
                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
450
                {
Sam Hocevar's avatar
   
Sam Hocevar committed
451
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
452
453
                }

Sam Hocevar's avatar
   
Sam Hocevar committed
454
455
456
                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
457
458
        }

Sam Hocevar's avatar
   
Sam Hocevar committed
459
460
        i_index++;
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
461

462
463
464
    msg_Dbg( p_this, "probing %i candidate%s",
                     i_index, i_index == 1 ? "" : "s" );

465
    /* Lock all candidate modules */
466
467
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
468
    {
469
        vlc_object_yield( p_tmp->p_module );
470
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
471
472
    }

473
    /* We can release the list, interesting modules were yielded */
474
    vlc_list_release( p_all );
Sam Hocevar's avatar
   
Sam Hocevar committed
475

Sam Hocevar's avatar
   
Sam Hocevar committed
476
    /* Parse the linked list and use the first successful module */
477
478
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
479
    {
480
481
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
   
Sam Hocevar committed
482
483
        {
            break;
484
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
485

486
        vlc_object_release( p_tmp->p_module );
487
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
488
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
489

Sam Hocevar's avatar
   
Sam Hocevar committed
490
    /* Store the locked module value */
491
    if( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
492
    {
493
494
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
495
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
496
497
498
499
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
500

Sam Hocevar's avatar
   
Sam Hocevar committed
501
    /* Unlock the remaining modules */
502
    while( p_tmp != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
503
    {
504
        vlc_object_release( p_tmp->p_module );
505
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
   
Sam Hocevar committed
506
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
507

Sam Hocevar's avatar
   
Sam Hocevar committed
508
    free( p_list );
Sam Hocevar's avatar
 
Sam Hocevar committed
509

Sam Hocevar's avatar
   
Sam Hocevar committed
510
    if( p_module != NULL )
Sam Hocevar's avatar
   
Sam Hocevar committed
511
    {
512
513
        msg_Dbg( p_module, "using %s module \"%s\"",
                 psz_capability, p_module->psz_object_name );
514
515
516
    }
    else if( p_first == NULL )
    {
517
518
        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
519
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
520
521
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
522
523
        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
524
525
    }

526
    if( psz_shortcuts )
Sam Hocevar's avatar
   
Sam Hocevar committed
527
    {
528
        free( psz_shortcuts );
Sam Hocevar's avatar
   
Sam Hocevar committed
529
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
530

531
    if( psz_var )
532
    {
533
        free( psz_var );
534
535
    }

Sam Hocevar's avatar
   
Sam Hocevar committed
536
    /* Don't forget that the module is still locked */
537
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
538
539
540
541
542
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
543
544
 * 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
545
 *****************************************************************************/
546
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
547
{
548
549
550
551
552
553
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

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

556
    vlc_object_release( p_module );
557
558

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
559
560
561
562
563
564
}

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

Sam Hocevar's avatar
   
Sam Hocevar committed
565
566
567
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
568
#ifdef HAVE_DYNAMIC_PLUGINS
569
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar
   
Sam Hocevar committed
570
{
571
    /* Yes, there are two NULLs because we replace one with "plugin-path". */
572
573
    char *          path[] = { "modules", PLUGIN_PATH, "plugins", NULL,
                               NULL };
Sam Hocevar's avatar
   
Sam Hocevar committed
574
575
576
577

    char **         ppsz_path = path;
    char *          psz_fullpath;

578
#if defined( UNDER_CE )
579
    wchar_t         psz_dir[MAX_PATH];
580
581
#endif

582
    /* If the user provided a plugin path, we add it to the list */
583
    path[ sizeof(path)/sizeof(char*) - 2 ] = config_GetPsz( p_this,
584
585
                                                            "plugin-path" );

Sam Hocevar's avatar
   
Sam Hocevar committed
586
587
    for( ; *ppsz_path != NULL ; ppsz_path++ )
    {
588
589
#if defined( SYS_BEOS ) || defined( SYS_DARWIN ) \
     || ( defined( WIN32 ) && !defined( UNDER_CE ) )
Sam Hocevar's avatar
   
Sam Hocevar committed
590

591
        /* Handle relative as well as absolute paths */
592
#ifdef WIN32
593
        if( !(*ppsz_path)[0] || (*ppsz_path)[1] != ':' )
594
#else
595
        if( (*ppsz_path)[0] != '/' )
596
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
597
        {
598
599
            int i_dirlen = strlen( *ppsz_path );
            i_dirlen += strlen( p_this->p_libvlc->psz_vlcpath ) + 2;
Sam Hocevar's avatar
   
Sam Hocevar committed
600
601
602
603
604
605

            psz_fullpath = malloc( i_dirlen );
            if( psz_fullpath == NULL )
            {
                continue;
            }
606
607
608
609
#ifdef WIN32
            sprintf( psz_fullpath, "%s\\%s",
                     p_this->p_libvlc->psz_vlcpath, *ppsz_path );
#else
610
611
            sprintf( psz_fullpath, "%s/%s",
                     p_this->p_libvlc->psz_vlcpath, *ppsz_path );
612
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
613
614
615
616
        }
        else
#endif
        {
617
            psz_fullpath = strdup( *ppsz_path );
Sam Hocevar's avatar
   
Sam Hocevar committed
618
619
        }

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

622
        /* Don't go deeper than 5 subdirectories */
623
624
625
626
#if defined( UNDER_CE )
        MultiByteToWideChar( CP_ACP, 0, psz_fullpath, -1, psz_dir, MAX_PATH );
        AllocatePluginDir( p_this, psz_dir, 5 );
#else
627
        AllocatePluginDir( p_this, psz_fullpath, 5 );
628
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
629

630
        free( psz_fullpath );
Sam Hocevar's avatar
   
Sam Hocevar committed
631
    }
gbazin's avatar
   
gbazin committed
632
633

    /* Free plugin-path */
634
635
    if( path[ sizeof(path)/sizeof(char*) - 2 ] )
        free( path[ sizeof(path)/sizeof(char*) - 2 ] );
gbazin's avatar
   
gbazin committed
636
    path[ sizeof(path)/sizeof(char*) - 2 ] = NULL;
Sam Hocevar's avatar
   
Sam Hocevar committed
637
638
}

Sam Hocevar's avatar
 
Sam Hocevar committed
639
/*****************************************************************************
640
641
 * AllocatePluginDir: recursively parse a directory to look for plugins
 *****************************************************************************/
642
static void AllocatePluginDir( vlc_object_t *p_this, const MYCHAR *psz_dir,
643
                               int i_maxdepth )
644
{
gbazin's avatar
   
gbazin committed
645
#if defined( UNDER_CE ) || defined( _MSC_VER )
646
647
#ifdef UNDER_CE
    MYCHAR psz_path[MAX_PATH + 256];
gbazin's avatar
   
gbazin committed
648
649
650
#else
    char psz_path[MAX_PATH + 256];
#endif
651
652
653
654
    WIN32_FIND_DATA finddata;
    HANDLE handle;
    unsigned int rc;
#else
655
656
657
    int    i_dirlen;
    DIR *  dir;
    char * psz_file;
658
    struct dirent * file;
659
#endif
660

661
662
663
664
665
    if( i_maxdepth < 0 )
    {
        return;
    }

gbazin's avatar
   
gbazin committed
666
#if defined( UNDER_CE ) || defined( _MSC_VER )
667
668
669
670
671
672
673
674
    rc = GetFileAttributes( psz_dir );
    if( !(rc & FILE_ATTRIBUTE_DIRECTORY) )
    {
        /* Not a directory */
        return;
    }

    /* Parse all files in the directory */
gbazin's avatar
   
gbazin committed
675
#ifdef UNDER_CE
676
    swprintf( psz_path, L"%s\\*.*", psz_dir );
gbazin's avatar
   
gbazin committed
677
678
679
#else
    sprintf( psz_path, "%s\\*.*", psz_dir );
#endif
680
681
682
683
684
685
686
687
688
689
    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
    {
gbazin's avatar
   
gbazin committed
690
#ifdef UNDER_CE
691
692
        unsigned int i_len = wcslen( finddata.cFileName );
        swprintf( psz_path, L"%s\\%s", psz_dir, finddata.cFileName );
gbazin's avatar
   
gbazin committed
693
694
695
696
697
698
699
700
701
702
#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
703
704
705
706
707

        if( GetFileAttributes( psz_path ) & FILE_ATTRIBUTE_DIRECTORY )
        {
            AllocatePluginDir( p_this, psz_path, i_maxdepth - 1 );
        }
gbazin's avatar
   
gbazin committed
708
709
710
711
712
713
714
715
716
        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
717
718
719
720
        {
            AllocatePluginFile( p_this, psz_path );
        }
    }
721
    while( FindNextFile( handle, &finddata ) );
722

723
724
725
726
727
    /* Close the directory */
    FindClose( handle );

#else
    dir = opendir( psz_dir );
728
    if( !dir )
729
    {
730
731
732
733
734
735
736
737
738
        return;
    }

    i_dirlen = strlen( psz_dir );

    /* Parse the directory and try to load all files it contains. */
    while( (file = readdir( dir )) )
    {
        struct stat statbuf;
739
        unsigned int i_len;
740
741
742

        /* Skip ".", ".." and anything starting with "." */
        if( !*file->d_name || *file->d_name == '.' )
743
        {
744
745
            continue;
        }
746

747
        i_len = strlen( file->d_name );
748
        psz_file = malloc( i_dirlen + 1 + i_len + 1 );
749
750
751
#ifdef WIN32
        sprintf( psz_file, "%s\\%s", psz_dir, file->d_name );
#else
752
        sprintf( psz_file, "%s/%s", psz_dir, file->d_name );
753
#endif
754

755
756
        if( !stat( psz_file, &statbuf ) && statbuf.st_mode & S_IFDIR )
        {
757
            AllocatePluginDir( p_this, psz_file, i_maxdepth - 1 );
758
        }
759
760
        else if( i_len > strlen( LIBEXT )
                  /* We only load files ending with LIBEXT */
761
762
                  && !strncasecmp( file->d_name + i_len - strlen( LIBEXT ),
                                   LIBEXT, strlen( LIBEXT ) ) )
763
        {
gbazin's avatar
   
gbazin committed
764
            AllocatePluginFile( p_this, psz_file );
765
766
        }

767
        free( psz_file );
768
    }
769
770
771

    /* Close the directory */
    closedir( dir );
772
773

#endif
774
775
776
777
}

/*****************************************************************************
 * AllocatePluginFile: load a module into memory and initialize it.
Sam Hocevar's avatar
 
Sam Hocevar committed
778
779
 *****************************************************************************
 * This function loads a dynamically loadable module and allocates a structure
780
781
 * 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
782
 *****************************************************************************/
783
static int AllocatePluginFile( vlc_object_t * p_this, MYCHAR * psz_file )
Sam Hocevar's avatar
 
Sam Hocevar committed
784
{
785
    module_t * p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
786
787
    module_handle_t handle;

788
789
790
791
792
793
794
795
#ifdef UNDER_CE
    char psz_filename[MAX_PATH];
    WideCharToMultiByte( CP_ACP, WC_DEFAULTCHAR, psz_file, -1,
                         psz_filename, MAX_PATH, NULL, NULL );
#else
    char * psz_filename = psz_file;
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
796
    /* Try to dynamically load the module. */
797
    if( module_load( psz_file, &handle ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
798
    {
799
800
        char psz_buffer[256];

Sam Hocevar's avatar
   
Sam Hocevar committed
801
        /* The plugin module couldn't be opened */
802
        msg_Warn( p_this, "cannot open `%s' (%s)",
803
                  psz_filename, module_error( psz_buffer ) );
804
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
805
806
807
    }

    /* Now that we have successfully loaded the module, we can
808
     * allocate a structure for it */
809
    p_module = vlc_object_create( p_this, VLC_OBJECT_MODULE );
Sam Hocevar's avatar
 
Sam Hocevar committed
810
811
    if( p_module == NULL )
    {
812
        msg_Err( p_this, "out of memory" );
Sam Hocevar's avatar
 
Sam Hocevar committed
813
        module_unload( handle );
814
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
815
816
    }

817
    /* We need to fill these since they may be needed by CallEntry() */
818
    p_module->psz_filename = psz_filename;
819
    p_module->handle = handle;
820
    p_module->p_symbols = &p_this->p_libvlc->p_module_bank->symbols;
Sam Hocevar's avatar
 
Sam Hocevar committed
821

822
    /* Initialize the module: fill p_module->psz_object_name, default config */
823
    if( CallEntry( p_module ) != 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
824
    {
825
        /* We couldn't call module_init() */
826
        vlc_object_destroy( p_module );
Sam Hocevar's avatar
 
Sam Hocevar committed
827
        module_unload( handle );
828
        return -1;
Sam Hocevar's avatar
 
Sam Hocevar committed
829
830
    }

831
832
833
834
835
836
    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;
837

838
839
    /* msg_Dbg( p_this, "plugin \"%s\", %s",
                p_module->psz_object_name, p_module->psz_longname ); */
840

841
    vlc_object_attach( p_module, p_this->p_libvlc->p_module_bank );
842
843
844
845
846
847
848
849
850
851
852
853
854
855

    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
856

Sam Hocevar's avatar
   
Sam Hocevar committed
857
858
859
860
861
    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
    {
        *pp_shortcut = strdup( *pp_shortcut );
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
862
863
    /* We strdup() these entries so that they are still valid when the
     * module is unloaded. */
864
    p_module->psz_object_name = strdup( p_module->psz_object_name );
865
    p_module->psz_capability = strdup( p_module->psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
866

Sam Hocevar's avatar
   
Sam Hocevar committed
867
868
869
870
871
    if( p_module->psz_program != NULL )
    {
        p_module->psz_program = strdup( p_module->psz_program );
    }

872
873
874
875
876
    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
877

878
879
880
881
882
883
884
885
886
/*****************************************************************************
 * 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
887

888
    for( i_submodule = 0; i_submodule < p_module->i_children; i_submodule++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
889
    {
890
        UndupModule( (module_t*)p_module->pp_children[ i_submodule ] );
Sam Hocevar's avatar
 
Sam Hocevar committed
891
892
    }

893
894
895
896
    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
    {
        free( *pp_shortcut );
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
897

898
899
    free( p_module->psz_object_name );
    free( p_module->psz_capability );
900

901
902
903
904
    if( p_module->psz_program != NULL )
    {
        free( p_module->psz_program );
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
905
}
906

Sam Hocevar's avatar
   
Sam Hocevar committed
907
#endif /* HAVE_DYNAMIC_PLUGINS */
Sam Hocevar's avatar
 
Sam Hocevar committed
908
909

/*****************************************************************************