entry.c 12.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * entry.c : Callbacks for module entry point
 *****************************************************************************
4 5
 * Copyright (C) 2007 the VideoLAN team
 * Copyright © 2007-2008 Rémi Denis-Courmont
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

22 23 24 25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

26
#include <vlc_common.h>
27
#include <vlc_plugin.h>
28
#include <vlc_memory.h>
29
#include <assert.h>
30
#include <stdarg.h>
31
#include <limits.h>
32

33
#include "modules/modules.h"
34
#include "config/configuration.h"
35
#include "libvlc.h"
36

37 38 39 40
static void vlc_module_destruct (gc_object_t *obj)
{
    module_t *module = vlc_priv (obj, module_t);

41
    free (module->pp_shortcuts);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
42
    free (module->psz_object_name);
43 44 45
    free (module);
}

46 47
static const char default_name[] = "unnamed";

48
module_t *vlc_module_create (void)
49
{
50
    module_t *module = malloc (sizeof (*module));
51 52 53
    if (module == NULL)
        return NULL;

54
    module->psz_object_name = strdup( default_name );
55 56 57 58 59 60 61
    module->next = NULL;
    module->submodule = NULL;
    module->parent = NULL;
    module->submodule_count = 0;
    vlc_gc_init (module, vlc_module_destruct);

    module->psz_shortname = NULL;
62
    module->psz_longname = (char*)default_name;
63
    module->psz_help = NULL;
64 65
    module->pp_shortcuts = NULL;
    module->i_shortcuts = 0;
Rafaël Carré's avatar
Rafaël Carré committed
66
    module->psz_capability = (char*)"";
67
    module->i_score = 1;
68 69 70 71 72 73 74 75 76
    module->b_unloadable = true;
    module->pf_activate = NULL;
    module->pf_deactivate = NULL;
    module->p_config = NULL;
    module->confsize = 0;
    module->i_config_items = 0;
    module->i_bool_items = 0;
    /*module->handle = garbage */
    module->psz_filename = NULL;
77
    module->domain = NULL;
78 79
    module->b_builtin = false;
    module->b_loaded = false;
80 81 82
    return module;
}

83

84 85 86
static void vlc_submodule_destruct (gc_object_t *obj)
{
    module_t *module = vlc_priv (obj, module_t);
87
    free (module->pp_shortcuts);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
    free (module->psz_object_name);
89 90 91
    free (module);
}

92 93 94 95
module_t *vlc_submodule_create (module_t *module)
{
    assert (module != NULL);

Rémi Duraffort's avatar
Rémi Duraffort committed
96 97
    module_t *submodule = calloc( 1, sizeof(*submodule) );
    if( !submodule )
98 99
        return NULL;

100 101 102 103 104 105
    vlc_gc_init (submodule, vlc_submodule_destruct);

    submodule->next = module->submodule;
    submodule->parent = module;
    module->submodule = submodule;
    module->submodule_count++;
106 107

    /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
108
    submodule->pp_shortcuts = malloc( sizeof( char ** ) );
109
    submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
110
    submodule->i_shortcuts = 1;
111

112
    submodule->psz_object_name = strdup( module->psz_object_name );
113 114 115 116
    submodule->psz_shortname = module->psz_shortname;
    submodule->psz_longname = module->psz_longname;
    submodule->psz_capability = module->psz_capability;
    submodule->i_score = module->i_score;
117
    submodule->domain = module->domain;
118 119 120
    return submodule;
}

121
static module_config_t *vlc_config_create (module_t *module, int type)
122 123 124 125 126 127
{
    unsigned confsize = module->confsize;
    module_config_t *tab = module->p_config;

    if ((confsize & 0xf) == 0)
    {
128
        tab = realloc_or_free (tab, (confsize + 17) * sizeof (*tab));
129 130 131 132 133 134 135
        if (tab == NULL)
            return NULL;

        module->p_config = tab;
    }

    memset (tab + confsize, 0, sizeof (tab[confsize]));
136 137 138 139 140
    if (IsConfigIntegerType (type))
    {
        tab[confsize].max.i = INT_MAX;
        tab[confsize].min.i = INT_MIN;
    }
141
    tab[confsize].i_type = type;
142

143 144 145 146 147 148 149 150 151 152 153 154 155
    if (type & CONFIG_ITEM)
    {
        module->i_config_items++;
        if (type == CONFIG_ITEM_BOOL)
            module->i_bool_items++;
    }

    module->confsize++;
    return tab + confsize;
}


int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
156
{
157 158
    va_list ap;
    int ret = 0;
159

160
    va_start (ap, propid);
161 162
    switch (propid)
    {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        case VLC_SUBMODULE_CREATE:
        {
            module_t **pp = va_arg (ap, module_t **);
            *pp = vlc_submodule_create (module);
            if (*pp == NULL)
                ret = -1;
            break;
        }

        case VLC_CONFIG_CREATE:
        {
            int type = va_arg (ap, int);
            module_config_t **pp = va_arg (ap, module_config_t **);
            *pp = vlc_config_create (module, type);
            if (*pp == NULL)
                ret = -1;
            break;
        }

182 183
        case VLC_MODULE_SHORTCUT:
        {
184 185
            unsigned i_shortcuts = va_arg (ap, unsigned);
            unsigned index = module->i_shortcuts;
186 187 188
            /* The cache loader accept only a small number of shortcuts */
            assert(i_shortcuts + index <= MODULE_SHORTCUT_MAX);

189
            const char *const *tab = va_arg (ap, const char *const *);
Rémi Duraffort's avatar
Rémi Duraffort committed
190 191
            char **pp = realloc (module->pp_shortcuts,
                                 sizeof (pp[0]) * (index + i_shortcuts));
192
            if (unlikely(pp == NULL))
193
            {
194 195
                ret = -1;
                break;
196
            }
197 198 199
            module->pp_shortcuts = pp;
            module->i_shortcuts = index + i_shortcuts;
            memcpy (pp + index, tab, sizeof (pp[0]) * i_shortcuts);
200 201 202 203
            break;
        }

        case VLC_MODULE_CAPABILITY:
204
            module->psz_capability = va_arg (ap, char *);
205 206 207
            break;

        case VLC_MODULE_SCORE:
208
            module->i_score = va_arg (ap, int);
209 210 211
            break;

        case VLC_MODULE_CB_OPEN:
212
            module->pf_activate = va_arg (ap, void *);
213 214 215
            break;

        case VLC_MODULE_CB_CLOSE:
216
            module->pf_deactivate = va_arg (ap, void *);
217 218
            break;

219 220
        case VLC_MODULE_NO_UNLOAD:
            module->b_unloadable = false;
221 222
            break;

223
        case VLC_MODULE_NAME:
224 225
        {
            const char *value = va_arg (ap, const char *);
226
            free( module->psz_object_name );
227
            module->psz_object_name = strdup( value );
228
            module->pp_shortcuts = malloc( sizeof( char ** ) );
229
            module->pp_shortcuts[0] = (char*)value; /* dooh! */
230 231
            module->i_shortcuts = 1;

232
            if (module->psz_longname == default_name)
233
                module->psz_longname = (char*)value; /* dooh! */
234
            break;
235
        }
236

237
        case VLC_MODULE_SHORTNAME:
238
            module->psz_shortname = va_arg (ap, char *);
239 240 241
            break;

        case VLC_MODULE_DESCRIPTION:
242
            module->psz_longname = va_arg (ap, char *);
243
            break;
244 245

        case VLC_MODULE_HELP:
246
            module->psz_help = va_arg (ap, char *);
247
            break;
248

249
        case VLC_MODULE_TEXTDOMAIN:
250
            module->domain = va_arg (ap, char *);
251 252
            break;

253 254 255 256 257 258 259 260 261 262
        case VLC_CONFIG_NAME:
        {
            const char *name = va_arg (ap, const char *);

            assert (name != NULL);
            item->psz_name = strdup (name);
            break;
        }

        case VLC_CONFIG_VALUE:
263 264 265
        {
            if (IsConfigIntegerType (item->i_type))
            {
266
                item->orig.i =
267
                item->value.i = va_arg (ap, int64_t);
268 269 270 271
            }
            else
            if (IsConfigFloatType (item->i_type))
            {
272
                item->orig.f =
273 274 275 276 277 278 279
                item->value.f = va_arg (ap, double);
            }
            else
            if (IsConfigStringType (item->i_type))
            {
                const char *value = va_arg (ap, const char *);
                item->value.psz = value ? strdup (value) : NULL;
280
                item->orig.psz = value ? strdup (value) : NULL;
281 282 283 284
            }
            break;
        }

285
        case VLC_CONFIG_RANGE:
286
        {
287 288 289
            if (IsConfigIntegerType (item->i_type)
             || item->i_type == CONFIG_ITEM_MODULE_LIST_CAT
             || item->i_type == CONFIG_ITEM_MODULE_CAT)
290
            {
291 292
                item->min.i = va_arg (ap, int64_t);
                item->max.i = va_arg (ap, int64_t);
293 294 295 296 297 298 299 300 301 302
            }
            else
            if (IsConfigFloatType (item->i_type))
            {
                item->min.f = va_arg (ap, double);
                item->max.f = va_arg (ap, double);
            }
            break;
        }

303
        case VLC_CONFIG_ADVANCED:
304
            item->b_advanced = true;
305 306
            break;

307
        case VLC_CONFIG_VOLATILE:
308
            item->b_unsaveable = true;
309 310
            break;

311
        case VLC_CONFIG_PRIVATE:
312
            item->b_internal = true;
313
            break;
314 315

        case VLC_CONFIG_REMOVED:
316
            item->b_removed = true;
317 318 319 320 321 322 323 324
            break;

        case VLC_CONFIG_CAPABILITY:
        {
            const char *cap = va_arg (ap, const char *);
            item->psz_type = cap ? strdup (cap) : NULL;
            break;
        }
325 326 327 328 329

        case VLC_CONFIG_SHORTCUT:
            item->i_short = va_arg (ap, int);
            break;

330 331 332
        case VLC_CONFIG_OLDNAME:
        {
            const char *oldname = va_arg (ap, const char *);
333
            assert (item->psz_oldname == NULL);
334 335 336 337 338 339 340 341 342 343 344 345 346
            item->psz_oldname = oldname ? strdup (oldname) : NULL;
            break;
        }

        case VLC_CONFIG_SAFE:
            item->b_safe = true;
            break;

        case VLC_CONFIG_DESC:
        {
            const char *text = va_arg (ap, const char *);
            const char *longtext = va_arg (ap, const char *);

347 348
            item->psz_text = text ? strdup (text) : NULL;
            item->psz_longtext = longtext ? strdup (longtext) : NULL;
349 350 351
            break;
        }

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
        case VLC_CONFIG_LIST:
        {
            size_t len = va_arg (ap, size_t);

            /* Copy values */
            if (IsConfigIntegerType (item->i_type))
            {
                const int *src = va_arg (ap, const int *);
                int *dst = malloc (sizeof (int) * (len + 1));

                if (dst != NULL)
                {
                    memcpy (dst, src, sizeof (int) * len);
                    dst[len] = 0;
                }
                item->pi_list = dst;
            }
            else
            if (IsConfigStringType (item->i_type))
            {
                const char *const *src = va_arg (ap, const char *const *);
                char **dst = malloc (sizeof (char *) * (len + 1));

                if (dst != NULL)
                {
                    for (size_t i = 0; i < len; i++)
                        dst[i] = src[i] ? strdup (src[i]) : NULL;
                    dst[len] = NULL;
                }
                item->ppsz_list = dst;
            }
            else
                break;

            /* Copy textual descriptions */
            const char *const *text = va_arg (ap, const char *const *);
            if (text != NULL)
            {
390 391 392 393
                char **dtext = malloc (sizeof (char *) * (len + 1));
                if( dtext != NULL )
                {
                    for (size_t i = 0; i < len; i++)
394
                        dtext[i] = text[i] ? strdup (text[i]) : NULL;
395 396
                    dtext[len] = NULL;
                }
397 398 399 400 401 402
                item->ppsz_list_text = dtext;
            }
            else
                item->ppsz_list_text = NULL;

            item->i_list = len;
403
            item->pf_update_list = va_arg (ap, vlc_callback_t);
404 405
            break;
        }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

        case VLC_CONFIG_ADD_ACTION:
        {
            vlc_callback_t cb = va_arg (ap, vlc_callback_t), *tabcb;
            const char *name = va_arg (ap, const char *);
            char **tabtext;

            tabcb = realloc (item->ppf_action,
                             (item->i_action + 2) * sizeof (cb));
            if (tabcb == NULL)
                break;
            item->ppf_action = tabcb;
            tabcb[item->i_action] = cb;
            tabcb[item->i_action + 1] = NULL;

            tabtext = realloc (item->ppsz_action_text,
                               (item->i_action + 2) * sizeof (name));
            if (tabtext == NULL)
                break;
            item->ppsz_action_text = tabtext;

            if (name)
428
                tabtext[item->i_action] = strdup (name);
429 430 431 432 433
            else
                tabtext[item->i_action] = NULL;
            tabtext[item->i_action + 1] = NULL;

            item->i_action++;
434 435
            break;
        }
436

437
        default:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
438 439
            fprintf (stderr, "LibVLC: unknown module property %d\n", propid);
            fprintf (stderr, "LibVLC: too old to use this module?\n");
440 441 442
            ret = -1;
            break;
    }
443

444 445 446
    va_end (ap);
    return ret;
}