variables.c 43.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * variables.c: routines for object variables handling
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002-2009 VLC authors and VideoLAN
5
 * $Id$
6 7 8
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12
 * (at your option) any later version.
13
 *
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser 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 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31 32 33
#ifdef HAVE_SEARCH_H
# include <search.h>
#endif
34
#include <assert.h>
35 36
#include <math.h>
#include <limits.h>
37 38 39
#ifdef __GLIBC__
# include <dlfcn.h>
#endif
40

41 42 43 44 45 46
#include <vlc_common.h>
#include <vlc_charset.h>
#include "libvlc.h"
#include "variables.h"
#include "config/configuration.h"

47 48 49 50 51
/*****************************************************************************
 * Private types
 *****************************************************************************/
struct callback_entry_t
{
52 53 54 55 56 57
    union
    {
        vlc_callback_t       pf_value_callback;
        vlc_list_callback_t  pf_list_callback;
        void *               p_callback;
    } u;
58 59 60
    void *         p_data;
};

61 62 63
/*****************************************************************************
 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
64 65 66 67 68 69 70 71 72 73
static int CmpBool( vlc_value_t v, vlc_value_t w )
{
    return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
}

static int CmpInt( vlc_value_t v, vlc_value_t w )
{
    return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
}

74 75
static int CmpTime( vlc_value_t v, vlc_value_t w )
{
76
    return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
77
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
78

79 80 81 82 83 84 85
static int CmpString( vlc_value_t v, vlc_value_t w )
{
    if( !v.psz_string )
        return !w.psz_string ? 0 : -1;
    else
        return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
}
86 87 88 89 90 91
static int CmpFloat( vlc_value_t v, vlc_value_t w ) { return v.f_float == w.f_float ? 0 : v.f_float > w.f_float ? 1 : -1; }
static int CmpAddress( vlc_value_t v, vlc_value_t w ) { return v.p_address == w.p_address ? 0 : v.p_address > w.p_address ? 1 : -1; }

/*****************************************************************************
 * Local duplication functions, and local deallocation functions
 *****************************************************************************/
92
static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
93 94 95 96
static void DupString( vlc_value_t *p_val )
{
    p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string :  "" );
}
97

98
static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
99 100
static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }

101 102 103 104 105
static void FreeList( vlc_value_t *p_val )
{
    int i;
    for( i = 0; i < p_val->p_list->i_count; i++ )
    {
106
        switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        {
        case VLC_VAR_STRING:
            FreeString( &p_val->p_list->p_values[i] );
            break;
        default:
            break;
        }
    }

    if( p_val->p_list->i_count )
    {
        free( p_val->p_list->p_values );
        free( p_val->p_list->pi_types );
    }
    free( p_val->p_list );
}

124
static const struct variable_ops_t
125
void_ops   = { NULL,       DupDummy,  FreeDummy,  },
126 127 128 129 130
addr_ops   = { CmpAddress, DupDummy,  FreeDummy,  },
bool_ops   = { CmpBool,    DupDummy,  FreeDummy,  },
float_ops  = { CmpFloat,   DupDummy,  FreeDummy,  },
int_ops    = { CmpInt,     DupDummy,  FreeDummy,  },
string_ops = { CmpString,  DupString, FreeString, },
131 132
time_ops   = { CmpTime,    DupDummy,  FreeDummy,  },
coords_ops = { NULL,       DupDummy,  FreeDummy,  };
133

134 135 136
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
137
static void     WaitUnused  ( vlc_object_t *, variable_t * );
138

139
static void     CheckValue  ( variable_t *, vlc_value_t * );
140

141
static int      TriggerCallback( vlc_object_t *, variable_t *, const char *,
142
                                 vlc_value_t );
143

144 145 146 147
static int      TriggerListCallback( vlc_object_t *, variable_t *,
                                     const char *, int,
                                     vlc_value_t * );

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static int varcmp( const void *a, const void *b )
{
    const variable_t *va = a, *vb = b;

    /* psz_name must be first */
    assert( va == (const void *)&va->psz_name );
    return strcmp( va->psz_name, vb->psz_name );
}

static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
{
    vlc_object_internals_t *priv = vlc_internals( obj );
    variable_t **pp_var;

    vlc_assert_locked( &priv->var_lock );
    pp_var = tfind( &psz_name, &priv->var_root, varcmp );
    return (pp_var != NULL) ? *pp_var : NULL;
}

167 168 169 170 171 172 173 174 175 176 177 178 179
static void Destroy( variable_t *p_var )
{
    p_var->ops->pf_free( &p_var->val );
    if( p_var->choices.i_count )
    {
        for( int i = 0 ; i < p_var->choices.i_count ; i++ )
        {
            p_var->ops->pf_free( &p_var->choices.p_values[i] );
            free( p_var->choices_text.p_values[i].psz_string );
        }
        free( p_var->choices.p_values );
        free( p_var->choices_text.p_values );
    }
180
#if 0 // ndef NDEBUG
181 182
    callback_table_t *p_table = &p_var->value_callbacks;
    for (int i = 0; i < p_table->i_entries; i++)
183 184
    {
        const char *file = "?", *symbol = "?";
185
        const void *addr = p_table->p_entries[i].pf_callback;
186 187 188 189 190 191 192 193 194 195 196 197 198 199
# ifdef __GLIBC__
        Dl_info info;

        if (dladdr (addr, &info))
        {
            if (info.dli_fname) file = info.dli_fname;
            if (info.dli_sname) symbol = info.dli_sname;
        }
# endif
        fprintf (stderr, "Error: callback on \"%s\" dangling %s(%s)[%p]\n",
                 p_var->psz_name, file, symbol, addr);
    }
#endif

200 201
    free( p_var->psz_name );
    free( p_var->psz_text );
202
    free( p_var->value_callbacks.p_entries );
203 204 205
    free( p_var );
}

206
#undef var_Create
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
207 208 209
/**
 * Initialize a vlc variable
 *
210 211 212
 * We hash the given string and insert it into the sorted list. The insertion
 * may require slow memory copies, but think about what we gain in the log(n)
 * lookup phase when setting/getting the variable value!
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
213 214 215 216 217 218
 *
 * \param p_this The object in which to create the variable
 * \param psz_name The name of the variable
 * \param i_type The variables type. Must be one of \ref var_type combined with
 *               zero or more \ref var_flags
 */
219
int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
220
{
221 222
    assert( p_this );

223 224 225
    variable_t *p_var = calloc( 1, sizeof( *p_var ) );
    if( p_var == NULL )
        return VLC_ENOMEM;
226 227

    p_var->psz_name = strdup( psz_name );
228
    p_var->psz_text = NULL;
229

230
    p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
231

232
    p_var->i_usage = 1;
233

234
    p_var->i_default = -1;
235 236
    p_var->choices.i_count = 0;
    p_var->choices.p_values = NULL;
237 238
    p_var->choices_text.i_count = 0;
    p_var->choices_text.p_values = NULL;
239

240
    p_var->b_incallback = false;
241
    p_var->value_callbacks = (callback_table_t){ 0 };
242

243 244 245
    /* Always initialize the variable, even if it is a list variable; this
     * will lead to errors if the variable is not initialized, but it will
     * not cause crashes in the variable handling. */
246
    switch( i_type & VLC_VAR_CLASS )
247
    {
248
        case VLC_VAR_BOOL:
249
            p_var->ops = &bool_ops;
250
            p_var->val.b_bool = false;
251 252
            break;
        case VLC_VAR_INTEGER:
253
            p_var->ops = &int_ops;
254 255 256
            p_var->val.i_int = 0;
            break;
        case VLC_VAR_STRING:
257
            p_var->ops = &string_ops;
258
            p_var->val.psz_string = NULL;
259 260
            break;
        case VLC_VAR_FLOAT:
261
            p_var->ops = &float_ops;
262 263 264
            p_var->val.f_float = 0.0;
            break;
        case VLC_VAR_TIME:
265
            p_var->ops = &time_ops;
266
            p_var->val.i_time = 0;
267
            break;
268 269 270 271
        case VLC_VAR_COORDS:
            p_var->ops = &coords_ops;
            p_var->val.coords.x = p_var->val.coords.y = 0;
            break;
272
        case VLC_VAR_ADDRESS:
273
            p_var->ops = &addr_ops;
274 275
            p_var->val.p_address = NULL;
            break;
276
        case VLC_VAR_VOID:
277
            p_var->ops = &void_ops;
278 279 280
            break;
        default:
            assert (0);
281
    }
282

283 284
    if( (i_type & VLC_VAR_DOINHERIT)
     && var_Inherit( p_this, psz_name, i_type, &p_var->val ) == 0 )
285
    {
286
        if( i_type & VLC_VAR_HASCHOICE )
287 288 289 290 291 292 293 294 295 296 297 298 299 300
        {
            /* We must add the inherited value to our choice list */
            p_var->i_default = 0;

            INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
                         0, p_var->val );
            INSERT_ELEM( p_var->choices_text.p_values,
                         p_var->choices_text.i_count, 0, p_var->val );
            p_var->ops->pf_dup( &p_var->choices.p_values[0] );
            p_var->choices_text.p_values[0].psz_string = NULL;
        }
    }

    vlc_object_internals_t *p_priv = vlc_internals( p_this );
301 302
    variable_t **pp_var, *p_oldvar;
    int ret = VLC_SUCCESS;
303 304 305

    vlc_mutex_lock( &p_priv->var_lock );

306 307 308
    pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
    if( unlikely(pp_var == NULL) )
        ret = VLC_ENOMEM;
309 310 311
    else if( (p_oldvar = *pp_var) == p_var ) /* Variable create */
        p_var = NULL; /* Variable created */
    else /* Variable already exists */
312
    {
313
        assert (((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) == 0);
314 315
        p_oldvar->i_usage++;
        p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
316
    }
317
    vlc_mutex_unlock( &p_priv->var_lock );
318

319 320 321 322
    /* If we did not need to create a new variable, free everything... */
    if( p_var != NULL )
        Destroy( p_var );
    return ret;
323 324
}

325
#undef var_Destroy
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
326 327 328
/**
 * Destroy a vlc variable
 *
329 330
 * Look for the variable and destroy it if it is found. As in var_Create we
 * do a call to memmove() but we have performance counterparts elsewhere.
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
331 332 333 334
 *
 * \param p_this The object that holds the variable
 * \param psz_name The name of the variable
 */
335
int var_Destroy( vlc_object_t *p_this, const char *psz_name )
336
{
337
    variable_t *p_var;
338 339 340

    assert( p_this );

341
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
342

343
    vlc_mutex_lock( &p_priv->var_lock );
344

345 346
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
347
    {
348
        vlc_mutex_unlock( &p_priv->var_lock );
349
        return VLC_ENOVAR;
350 351
    }

352
    WaitUnused( p_this, p_var );
353

354 355
    if( --p_var->i_usage == 0 )
        tdelete( p_var, &p_priv->var_root, varcmp );
356
    else
357
        p_var = NULL;
358
    vlc_mutex_unlock( &p_priv->var_lock );
359

360 361
    if( p_var != NULL )
        Destroy( p_var );
362 363 364
    return VLC_SUCCESS;
}

365 366 367 368 369 370 371 372 373 374
static void CleanupVar( void *var )
{
    Destroy( var );
}

void var_DestroyAll( vlc_object_t *obj )
{
    vlc_object_internals_t *priv = vlc_internals( obj );

    tdestroy( priv->var_root, CleanupVar );
375
    priv->var_root = NULL;
376 377
}

378
#undef var_Change
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
379 380
/**
 * Perform an action on a variable
381
 *
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
382 383 384 385 386 387
 * \param p_this The object that holds the variable
 * \param psz_name The name of the variable
 * \param i_action The action to perform. Must be one of \ref var_action
 * \param p_val First action parameter
 * \param p_val2 Second action parameter
 */
388 389
int var_Change( vlc_object_t *p_this, const char *psz_name,
                int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
390
{
391
    int ret = VLC_SUCCESS;
392
    variable_t *p_var;
393
    vlc_value_t oldval;
Erwan Tulou's avatar
Erwan Tulou committed
394
    vlc_value_t newval;
395 396 397

    assert( p_this );

398
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
399

400
    vlc_mutex_lock( &p_priv->var_lock );
401

402 403
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
404
    {
405
        vlc_mutex_unlock( &p_priv->var_lock );
406 407 408 409 410 411
        return VLC_ENOVAR;
    }

    switch( i_action )
    {
        case VLC_VAR_SETMIN:
412 413
            if( p_var->i_type & VLC_VAR_HASMIN )
            {
414
                p_var->ops->pf_free( &p_var->min );
415 416
            }
            p_var->i_type |= VLC_VAR_HASMIN;
417
            p_var->min = *p_val;
418
            p_var->ops->pf_dup( &p_var->min );
419 420
            CheckValue( p_var, &p_var->val );
            break;
421 422 423
        case VLC_VAR_GETMIN:
            if( p_var->i_type & VLC_VAR_HASMIN )
                *p_val = p_var->min;
424 425
            else
                ret = VLC_EGENERIC;
426
            break;
427
        case VLC_VAR_SETMAX:
428 429
            if( p_var->i_type & VLC_VAR_HASMAX )
            {
430
                p_var->ops->pf_free( &p_var->max );
431 432
            }
            p_var->i_type |= VLC_VAR_HASMAX;
433
            p_var->max = *p_val;
434
            p_var->ops->pf_dup( &p_var->max );
435 436
            CheckValue( p_var, &p_var->val );
            break;
437 438 439
        case VLC_VAR_GETMAX:
            if( p_var->i_type & VLC_VAR_HASMAX )
                *p_val = p_var->max;
440 441
            else
                ret = VLC_EGENERIC;
442
            break;
443
        case VLC_VAR_SETSTEP:
444 445
            if( p_var->i_type & VLC_VAR_HASSTEP )
            {
446
                p_var->ops->pf_free( &p_var->step );
447 448
            }
            p_var->i_type |= VLC_VAR_HASSTEP;
449
            p_var->step = *p_val;
450
            p_var->ops->pf_dup( &p_var->step );
451 452
            CheckValue( p_var, &p_var->val );
            break;
453 454 455
        case VLC_VAR_GETSTEP:
            if( p_var->i_type & VLC_VAR_HASSTEP )
                *p_val = p_var->step;
456 457
            else
                ret = VLC_EGENERIC;
458
            break;
459
        case VLC_VAR_ADDCHOICE:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
460 461
        {
            int i = p_var->choices.i_count;
462

463 464
            INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
                         i, *p_val );
465
            INSERT_ELEM( p_var->choices_text.p_values,
466
                         p_var->choices_text.i_count, i, *p_val );
467
            p_var->ops->pf_dup( &p_var->choices.p_values[i] );
468 469 470
            p_var->choices_text.p_values[i].psz_string =
                ( p_val2 && p_val2->psz_string ) ?
                strdup( p_val2->psz_string ) : NULL;
471

472
            CheckValue( p_var, &p_var->val );
473 474

            TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_ADDCHOICE, p_val);
475
            break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
476
        }
477
        case VLC_VAR_DELCHOICE:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
478 479 480
        {
            int i;

481
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
482
                if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
483 484
                    break;

485
            if( i == p_var->choices.i_count )
486 487
            {
                /* Not found */
488
                vlc_mutex_unlock( &p_priv->var_lock );
489 490 491 492 493 494 495 496
                return VLC_EGENERIC;
            }

            if( p_var->i_default > i )
                p_var->i_default--;
            else if( p_var->i_default == i )
                p_var->i_default = -1;

497
            p_var->ops->pf_free( &p_var->choices.p_values[i] );
498
            free( p_var->choices_text.p_values[i].psz_string );
499
            REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
500 501
            REMOVE_ELEM( p_var->choices_text.p_values,
                         p_var->choices_text.i_count, i );
502 503

            CheckValue( p_var, &p_var->val );
504 505

            TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_DELCHOICE, p_val);
506
            break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
507
        }
508 509 510
        case VLC_VAR_CHOICESCOUNT:
            p_val->i_int = p_var->choices.i_count;
            break;
511
        case VLC_VAR_CLEARCHOICES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
512
            for( int i = 0 ; i < p_var->choices.i_count ; i++ )
513
                p_var->ops->pf_free( &p_var->choices.p_values[i] );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
514
            for( int i = 0 ; i < p_var->choices_text.i_count ; i++ )
515 516
                free( p_var->choices_text.p_values[i].psz_string );

517 518
            if( p_var->choices.i_count ) free( p_var->choices.p_values );
            if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
519 520 521

            p_var->choices.i_count = 0;
            p_var->choices.p_values = NULL;
522 523
            p_var->choices_text.i_count = 0;
            p_var->choices_text.p_values = NULL;
524
            p_var->i_default = -1;
525
            TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_CLEARCHOICES, NULL);
526
            break;
527
        case VLC_VAR_SETDEFAULT:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
528 529
        {
            int i;
530
            /* FIXME: the list is sorted, dude. Use something cleverer. */
531
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
532
                if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
533 534
                    break;

535
            if( i == p_var->choices.i_count )
536 537 538 539
                /* Not found */
                break;

            p_var->i_default = i;
540 541
            CheckValue( p_var, &p_var->val );
            break;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
542
        }
543 544
        case VLC_VAR_SETVALUE:
            /* Duplicate data if needed */
Erwan Tulou's avatar
Erwan Tulou committed
545 546
            newval = *p_val;
            p_var->ops->pf_dup( &newval );
547 548 549
            /* Backup needed stuff */
            oldval = p_var->val;
            /* Check boundaries and list */
Erwan Tulou's avatar
Erwan Tulou committed
550
            CheckValue( p_var, &newval );
551
            /* Set the variable */
Erwan Tulou's avatar
Erwan Tulou committed
552
            p_var->val = newval;
553
            /* Free data if needed */
554
            p_var->ops->pf_free( &oldval );
555
            break;
556
        case VLC_VAR_GETCHOICES:
557
        case VLC_VAR_GETLIST:
558
            p_val->p_list = malloc( sizeof(vlc_list_t) );
559
            if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
560
            if( p_var->choices.i_count )
561
            {
562 563
                p_val->p_list->p_values = malloc( p_var->choices.i_count
                                                  * sizeof(vlc_value_t) );
564 565 566 567 568 569 570 571 572 573
                p_val->p_list->pi_types = malloc( p_var->choices.i_count
                                                  * sizeof(int) );
                if( p_val2 )
                {
                    p_val2->p_list->p_values =
                        malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
                    p_val2->p_list->pi_types =
                        malloc( p_var->choices.i_count * sizeof(int) );
                }
            }
574
            p_val->p_list->i_count = p_var->choices.i_count;
575
            if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
576
            for( int i = 0 ; i < p_var->choices.i_count ; i++ )
577
            {
578
                p_val->p_list->p_values[i] = p_var->choices.p_values[i];
579
                p_val->p_list->pi_types[i] = p_var->i_type;
580
                p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
581 582 583 584 585 586 587
                if( p_val2 )
                {
                    p_val2->p_list->p_values[i].psz_string =
                        p_var->choices_text.p_values[i].psz_string ?
                    strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
                    p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
                }
588 589
            }
            break;
590
        case VLC_VAR_SETTEXT:
591
            free( p_var->psz_text );
592 593
            if( p_val && p_val->psz_string )
                p_var->psz_text = strdup( p_val->psz_string );
594
            else
Rémi Duraffort's avatar
Rémi Duraffort committed
595
                p_var->psz_text = NULL;
596 597
            break;
        case VLC_VAR_GETTEXT:
598 599
            p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
                                                : NULL;
600 601 602 603 604
            break;
        default:
            break;
    }

605
    vlc_mutex_unlock( &p_priv->var_lock );
606

607
    return ret;
608 609
}

610
#undef var_GetAndSet
611 612 613 614 615 616 617 618 619
/**
 * Perform a Get and Set on a variable
 *
 * \param p_this: The object that hold the variable
 * \param psz_name: the name of the variable
 * \param i_action: the action to perform
 * \param p_val: The action parameter
 * \return vlc error codes
 */
620
int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
621
                   vlc_value_t *p_val )
622
{
623
    int i_ret;
624 625
    variable_t *p_var;
    vlc_value_t oldval;
626 627

    assert( p_this );
628
    assert( p_val );
629

630 631 632
    vlc_object_internals_t *p_priv = vlc_internals( p_this );

    vlc_mutex_lock( &p_priv->var_lock );
633 634
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
635 636
    {
        vlc_mutex_unlock( &p_priv->var_lock );
637
        return VLC_ENOVAR;
638 639
    }

640
    WaitUnused( p_this, p_var );
641 642 643 644 645 646 647 648 649 650

    /* Duplicated data if needed */
    //p_var->ops->pf_dup( &val );

    /* Backup needed stuff */
    oldval = p_var->val;

    /* depending of the action requiered */
    switch( i_action )
    {
651
    case VLC_VAR_BOOL_TOGGLE:
652 653 654
        assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
        p_var->val.b_bool = !p_var->val.b_bool;
        break;
655
    case VLC_VAR_INTEGER_ADD:
656
        assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
657 658 659 660 661 662 663 664 665
        p_var->val.i_int += p_val->i_int;
        break;
    case VLC_VAR_INTEGER_OR:
        assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
        p_var->val.i_int |= p_val->i_int;
        break;
    case VLC_VAR_INTEGER_NAND:
        assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
        p_var->val.i_int &= ~p_val->i_int;
666
        break;
667 668 669 670 671 672 673
    default:
        vlc_mutex_unlock( &p_priv->var_lock );
        return VLC_EGENERIC;
    }

    /*  Check boundaries */
    CheckValue( p_var, &p_var->val );
674
    *p_val = p_var->val;
675

676
    /* Deal with callbacks.*/
677
    i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
678 679 680 681 682 683

    vlc_mutex_unlock( &p_priv->var_lock );

    return i_ret;
}

684
#undef var_Type
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
685 686 687 688
/**
 * Request a variable's type
 *
 * \return The variable type if it exists, or 0 if the
689
 * variable could not be found.
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
690 691
 * \see \ref var_type
 */
692
int var_Type( vlc_object_t *p_this, const char *psz_name )
693
{
694 695
    variable_t *p_var;
    int i_type = 0;
696 697 698

    assert( p_this );

699
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
700

701
    vlc_mutex_lock( &p_priv->var_lock );
702

703 704 705
    p_var = Lookup( p_this, psz_name );
    if( p_var != NULL )
        i_type = p_var->i_type;
706

707
    vlc_mutex_unlock( &p_priv->var_lock );
708 709

    return i_type;
710 711
}

712
#undef var_SetChecked
713 714
int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
                    int expected_type, vlc_value_t val )
715
{
716
    int i_ret = VLC_SUCCESS;
717 718
    variable_t *p_var;
    vlc_value_t oldval;
719 720 721

    assert( p_this );

722
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
723

724
    vlc_mutex_lock( &p_priv->var_lock );
725

726 727
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
728
    {
729
        vlc_mutex_unlock( &p_priv->var_lock );
730
        return VLC_ENOVAR;
731 732
    }

733
    assert( expected_type == 0 ||
734
            (p_var->i_type & VLC_VAR_CLASS) == expected_type );
735
    assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
736

737 738
    WaitUnused( p_this, p_var );

739
    /* Duplicate data if needed */
740
    p_var->ops->pf_dup( &val );
741

742 743
    /* Backup needed stuff */
    oldval = p_var->val;
744

745
    /* Check boundaries and list */
746 747
    CheckValue( p_var, &val );

748 749 750
    /* Set the variable */
    p_var->val = val;

751
    /* Deal with callbacks */
752
    i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
753 754

    /* Free data if needed */
755
    p_var->ops->pf_free( &oldval );
756

757
    vlc_mutex_unlock( &p_priv->var_lock );
758

759
    return i_ret;
760 761
}

762
#undef var_Set
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
763
/**
764
 * Set a variable's value
765
 *
766
 * \param p_this The object that hold the variable
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
767
 * \param psz_name The name of the variable
768
 * \param val the value to set
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
769
 */
770
int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
771 772 773 774
{
    return var_SetChecked( p_this, psz_name, 0, val );
}

775
#undef var_GetChecked
776 777
int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
                    int expected_type, vlc_value_t *p_val )
778
{
779 780
    assert( p_this );

781
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
782 783
    variable_t *p_var;
    int err = VLC_SUCCESS;
784

785
    vlc_mutex_lock( &p_priv->var_lock );
786

787 788
    p_var = Lookup( p_this, psz_name );
    if( p_var != NULL )
789
    {
790
        assert( expected_type == 0 ||
791
                (p_var->i_type & VLC_VAR_CLASS) == expected_type );
792
        assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
793

794 795
        /* Really get the variable */
        *p_val = p_var->val;
796

797 798 799 800 801
        /* Duplicate value if needed */
        p_var->ops->pf_dup( p_val );
    }
    else
        err = VLC_ENOVAR;
802

803
    vlc_mutex_unlock( &p_priv->var_lock );
804
    return err;
805 806
}

807
#undef var_Get
808 809 810 811 812 813 814 815
/**
 * Get a variable's value
 *
 * \param p_this The object that holds the variable
 * \param psz_name The name of the variable
 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
 *              after the function is finished
 */
816
int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
817 818 819
{
    return var_GetChecked( p_this, psz_name, 0, p_val );
}
820

821
static int AddCallback( vlc_object_t *p_this, const char *psz_name,
822
                        callback_entry_t entry, vlc_callback_type_t i_type )
823 824
{
    variable_t *p_var;
825 826 827

    assert( p_this );

828
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
829

830
    vlc_mutex_lock( &p_priv->var_lock );
831

832 833
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
834
    {
835
        vlc_mutex_unlock( &p_priv->var_lock );
836
        msg_Err( p_this, "cannot add callback %p to nonexistent "
837
                         "variable '%s'", entry.u.p_callback, psz_name );
838
        return VLC_ENOVAR;
839 840
    }

841
    WaitUnused( p_this, p_var );
842 843 844 845 846 847

    callback_table_t *p_table;
    if (i_type == vlc_value_callback)
        p_table = &p_var->value_callbacks;
    else
        p_table = &p_var->list_callbacks;
848 849 850 851
    INSERT_ELEM( p_table->p_entries,
                 p_table->i_entries,
                 p_table->i_entries,
                 entry);
852

853
    vlc_mutex_unlock( &p_priv->var_lock );
854 855 856 857

    return VLC_SUCCESS;
}

858
#undef var_AddCallback
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
859
/**
860
 * Register a callback in a variable
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
861
 *
862 863 864 865 866 867 868 869 870 871 872 873
 * We store a function pointer that will be called upon variable
 * modification.
 *
 * \param p_this The object that holds the variable
 * \param psz_name The name of the variable
 * \param pf_callback The function pointer
 * \param p_data A generic pointer that will be passed as the last
 *               argument to the callback function.
 *
 * \warning The callback function is run in the thread that calls var_Set on
 *          the variable. Use proper locking. This thread may not have much
 *          time to spare, so keep callback functions short.
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
874
 */
875
int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
876
                     vlc_callback_t pf_callback, void *p_data )
877 878
{
    callback_entry_t entry;
879
    entry.u.pf_value_callback = pf_callback;
880 881
    entry.p_data = p_data;

882
    return AddCallback(p_this, psz_name, entry, vlc_value_callback);
883 884 885
}

static int DelCallback( vlc_object_t *p_this, const char *psz_name,
886
                        callback_entry_t entry, vlc_callback_type_t i_type )
887
{
888
    int i_entry;
889
    variable_t *p_var;
890 891 892
#ifndef NDEBUG
    bool b_found_similar = false;
#endif
893 894 895

    assert( p_this );

896
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
897

898
    vlc_mutex_lock( &p_priv->var_lock );
899

900 901
    p_var = Lookup( p_this, psz_name );
    if( p_var == NULL )
902
    {
903
        vlc_mutex_unlock( &p_priv->var_lock );
904
        return VLC_ENOVAR;
905 906
    }

907
    WaitUnused( p_this, p_var );
908

909 910 911 912 913 914
    callback_table_t *p_table;
    if (i_type == vlc_value_callback)
        p_table = &p_var->value_callbacks;
    else
        p_table = &p_var->list_callbacks;

915
    for( i_entry = p_table->i_entries ; i_entry-- ; )
916
    {
917
        if( p_table->p_entries[i_entry].u.p_callback == entry.u.p_callback
918
            && p_table->p_entries[i_entry].p_data == entry.p_data )
919 920 921
        {
            break;
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
922
#ifndef NDEBUG
923
        else if( p_table->p_entries[i_entry].u.p_callback == entry.u.p_callback )
924
            b_found_similar = true;
Rémi Duraffort's avatar
Rémi Duraffort committed
925
#endif
926 927 928 929
    }

    if( i_entry < 0 )
    {
930 931
#ifndef NDEBUG
        if( b_found_similar )
932 933 934
            fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
                             "function but not the same data.", psz_name );
        assert( 0 );
935
#endif
Rémi Denis-Courmont's avatar