entry.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * entry.c : Callbacks for module entry point
 *****************************************************************************
 * Copyright (C) 2001-2007 the VideoLAN team
 *
 * 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.
 *****************************************************************************/

#include <vlc/vlc.h>
#include <assert.h>
23
#include <stdarg.h>
24

25 26
#include "modules/modules.h"
#include "config/config.h"
27 28
#include "libvlc.h"

29 30 31 32
static const char default_name[] = "unnamed";

module_t *vlc_module_create (vlc_object_t *obj)
{
33 34 35
    module_t *module =
        (module_t *)vlc_custom_create (obj, sizeof (module_t),
                                       VLC_OBJECT_MODULE, "module");
36 37 38 39 40 41 42 43 44 45
    if (module == NULL)
        return NULL;

    module->b_reentrant = module->b_unloadable = VLC_TRUE;
    module->psz_object_name = module->psz_longname = default_name;
    module->psz_capability = "";
    module->i_score = 1;
    return module;
}

46 47 48 49 50 51 52

module_t *vlc_submodule_create (module_t *module)
{
    assert (module != NULL);
    assert (!module->b_submodule); // subsubmodules are not supported

    module_t *submodule =
53 54
        (module_t *)vlc_custom_create (VLC_OBJECT (module), sizeof (module_t),
                                       VLC_OBJECT_MODULE, "submodule");
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    if (submodule == NULL)
        return NULL;

    vlc_object_attach (submodule, module);
    submodule->b_submodule = VLC_TRUE;

    /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
    memcpy (submodule->pp_shortcuts, module->pp_shortcuts,
            sizeof (submodule->pp_shortcuts));

    submodule->psz_object_name = module->psz_object_name;
    submodule->psz_shortname = module->psz_shortname;
    submodule->psz_longname = module->psz_longname;
    submodule->psz_capability = module->psz_capability;
    submodule->i_score = module->i_score;
    submodule->i_cpu = module->i_cpu;
    return submodule;
}

74

75 76 77 78 79 80
int vlc_module_set (module_t *module, int propid, void *value)
{
    switch (propid)
    {
        case VLC_MODULE_CPU_REQUIREMENT:
            assert (!module->b_submodule);
81
            module->i_cpu |= (intptr_t)value;
82 83 84 85 86 87
            break;

        case VLC_MODULE_SHORTCUT:
        {
            unsigned i;
            for (i = 0; module->pp_shortcuts[i] != NULL; i++);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
            if (i >= (MODULE_SHORTCUT_MAX - 1))
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                return VLC_ENOMEM;

            module->pp_shortcuts[i] = (char *)value;
            break;
        }

        case VLC_MODULE_SHORTNAME:
            module->psz_shortname = (char *)value;
            break;

        case VLC_MODULE_DESCRIPTION:
            module->psz_longname = (char *)value;
            break;

        case VLC_MODULE_HELP:
            module->psz_help = (char *)value;
            break;

        case VLC_MODULE_CAPABILITY:
            module->psz_capability = (char *)value;
            break;

        case VLC_MODULE_SCORE:
112
            module->i_score = (intptr_t)value;
113 114 115 116 117 118 119 120 121 122 123 124 125 126
            break;

        case VLC_MODULE_CB_OPEN:
            module->pf_activate = (int (*) (vlc_object_t *))value;
            break;

        case VLC_MODULE_CB_CLOSE:
            module->pf_deactivate = (void (*) (vlc_object_t *))value;
            break;

        case VLC_MODULE_UNLOADABLE:
            module->b_unloadable = (value != NULL);
            break;

127 128 129 130 131 132
        case VLC_MODULE_NAME:
            module->pp_shortcuts[0] = module->psz_object_name = (char *)value;
            if (module->psz_longname == default_name)
                module->psz_longname = (char *)value;
            break;

133 134
        case VLC_MODULE_PROGRAM:
            msg_Warn (module, "deprecated module property %d", propid);
135
            break;
136

137 138 139 140 141 142 143
        default:
            msg_Err (module, "unknown module property %d", propid);
            msg_Err (module, "LibVLC might be too old to use this module.");
            return VLC_EGENERIC;
    }
    return 0;
}
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
module_config_t *vlc_config_create (module_t *module, int type)
{
    unsigned confsize = module->confsize;
    module_config_t *tab = module->p_config;

    if ((confsize & 0xf) == 0)
    {
        tab = realloc (tab, (confsize + 17) * sizeof (*tab));
        if (tab == NULL)
            return NULL;

        module->p_config = tab;
    }
    module->confsize++;

    memset (tab + confsize, 0, sizeof (tab[confsize]));
161 162 163
    tab[confsize].i_type = type;
    tab[confsize].p_lock = &module->object_lock;

164 165 166
    return tab + confsize;
}

167
int vlc_config_set (module_config_t *restrict item, int id, ...)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
{
    int ret = -1;
    va_list ap;

    assert (item != NULL);
    va_start (ap, id);

    switch (id)
    {
        case VLC_CONFIG_NAME:
        {
            const char *name = va_arg (ap, const char *);
            vlc_callback_t cb = va_arg (ap, vlc_callback_t);

            assert (name != NULL);
            item->psz_name = strdup (name);
            item->pf_callback = cb;
185
            ret = 0;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            break;
        }

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

            item->psz_text = text ? strdup (gettext (text)) : NULL;
            item->psz_longtext = longtext ? strdup (gettext (text)) : NULL;
            ret = 0;
            break;
        }

        case VLC_CONFIG_VALUE:
201 202 203
        {
            if (IsConfigIntegerType (item->i_type))
            {
204
                item->orig.i = item->saved.i =
205 206 207 208 209 210
                item->value.i = va_arg (ap, int);
                ret = 0;
            }
            else
            if (IsConfigFloatType (item->i_type))
            {
211
                item->orig.f = item->saved.f =
212 213 214 215 216 217 218 219
                item->value.f = va_arg (ap, double);
                ret = 0;
            }
            else
            if (IsConfigStringType (item->i_type))
            {
                const char *value = va_arg (ap, const char *);
                item->value.psz = value ? strdup (value) : NULL;
220 221
                item->orig.psz = value ? strdup (value) : NULL;
                item->saved.psz = value ? strdup (value) : NULL;
222 223 224 225 226
                ret = 0;
            }
            break;
        }

227
        case VLC_CONFIG_RANGE:
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        {
            if (IsConfigIntegerType (item->i_type))
            {
                item->min.i = va_arg (ap, int);
                item->max.i = va_arg (ap, int);
                ret = 0;
            }
            else
            if (IsConfigFloatType (item->i_type))
            {
                item->min.f = va_arg (ap, double);
                item->max.f = va_arg (ap, double);
                ret = 0;
            }
            break;
        }

245
        case VLC_CONFIG_ADVANCED:
246 247 248 249
            item->b_advanced = VLC_TRUE;
            ret = 0;
            break;

250
        case VLC_CONFIG_VOLATILE:
251 252 253 254 255 256 257 258 259 260 261 262 263 264
            item->b_unsaveable = VLC_TRUE;
            ret = 0;
            break;

        case VLC_CONFIG_PERSISTENT:
            item->b_autosave = VLC_TRUE;
            ret = 0;
            break;

        case VLC_CONFIG_RESTART:
            item->b_restart = VLC_TRUE;
            ret = 0;
            break;

265
        case VLC_CONFIG_PRIVATE:
266 267
            item->b_internal = VLC_TRUE;
            ret = 0;
268
            break;
269 270

        case VLC_CONFIG_REMOVED:
271
            item->b_removed = VLC_TRUE;
272 273 274 275 276 277 278 279 280 281
            ret = 0;
            break;

        case VLC_CONFIG_CAPABILITY:
        {
            const char *cap = va_arg (ap, const char *);
            item->psz_type = cap ? strdup (cap) : NULL;
            ret = 0;
            break;
        }
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

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

        case VLC_CONFIG_LIST:
        {
            size_t len = va_arg (ap, size_t);
            char **dtext = malloc (sizeof (char *) * (len + 1));

            if (dtext == NULL)
                break;

            /* 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 0
            if (IsConfigFloatType (item->i_type))
            {
                const float *src = va_arg (ap, const float *);
                float *dst = malloc (sizeof (float) * (len + 1));

                if (dst != NULL)
                {
                    memcpy (dst, src, sizeof (float) * len);
                    dst[len] = 0.;
                }
                item->pf_list = dst;
            }
            else
#endif
            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)
            {
                for (size_t i = 0; i < len; i++)
                    dtext[i] = text[i] ? strdup (gettext (text[i])) : NULL;

                dtext[len] = NULL;
                item->ppsz_list_text = dtext;
            }
            else
                item->ppsz_list_text = NULL;

            item->i_list = len;
            ret = 0;
            break;
        }
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

        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)
                tabtext[item->i_action] = strdup (gettext (name));
            else
                tabtext[item->i_action] = NULL;
            tabtext[item->i_action + 1] = NULL;

            item->i_action++;
            ret = 0;
387 388 389 390 391 392 393 394 395
            break;
        }

        case VLC_CONFIG_OLDNAME:
        {
            const char *oldname = va_arg (ap, const char *);
            item->psz_oldname = oldname ? strdup (oldname) : NULL;
            ret = 0;
            break;
396
        }
397 398 399 400 401
    }

    va_end (ap);
    return ret;
}