variables.c 47.2 KB
Newer Older
1 2 3
/*****************************************************************************
 * variables.c: routines for object variables handling
 *****************************************************************************
4
 * Copyright (C) 2002-2006 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * 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.
13
 *
14 15 16 17 18 19 20
 * 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
dionoea's avatar
dionoea committed
21
 * 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
#include <vlc_common.h>
zorglub's avatar
zorglub committed
32
#include "variables.h"
33

34
#include "libvlc.h"
35

36
#include "vlc_interface.h"
37
#include <assert.h>
38

39 40 41 42 43 44 45 46 47
/*****************************************************************************
 * Private types
 *****************************************************************************/
struct callback_entry_t
{
    vlc_callback_t pf_callback;
    void *         p_data;
};

48 49 50
/*****************************************************************************
 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
 *****************************************************************************/
51
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; }
52
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; }
53 54
static int CmpTime( vlc_value_t v, vlc_value_t w )
{
55
    return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
56
}
57 58 59 60 61 62 63
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 );
}
64 65 66 67 68 69
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
 *****************************************************************************/
70
static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
71
static void DupString( vlc_value_t *p_val ) { if( p_val->psz_string ) p_val->psz_string = strdup( p_val->psz_string ); }
72

gbazin's avatar
 
gbazin committed
73 74 75 76 77
static void DupList( vlc_value_t *p_val )
{
    int i;
    vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );

78
    p_list->i_count = p_val->p_list->i_count;
gbazin's avatar
 
gbazin committed
79 80 81 82 83
    if( p_val->p_list->i_count )
    {
        p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
        p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
    }
84 85 86 87 88
    else
    {
        p_list->p_values = NULL;
        p_list->pi_types = NULL;
    }
gbazin's avatar
 
gbazin committed
89 90 91 92

    for( i = 0; i < p_list->i_count; i++ )
    {
        p_list->p_values[i] = p_val->p_list->p_values[i];
93
        p_list->pi_types[i] = p_val->p_list->pi_types[i];
hartman's avatar
hartman committed
94
        switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
gbazin's avatar
 
gbazin committed
95 96
        {
        case VLC_VAR_STRING:
zorglub's avatar
zorglub committed
97

gbazin's avatar
 
gbazin committed
98 99 100 101 102 103 104 105 106 107
            DupString( &p_list->p_values[i] );
            break;
        default:
            break;
        }
    }

    p_val->p_list = p_list;
}

108
static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
109
static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
gbazin's avatar
 
gbazin committed
110
static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
111

gbazin's avatar
 
gbazin committed
112 113 114 115 116
static void FreeList( vlc_value_t *p_val )
{
    int i;
    for( i = 0; i < p_val->p_list->i_count; i++ )
    {
hartman's avatar
hartman committed
117
        switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
gbazin's avatar
 
gbazin committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        {
        case VLC_VAR_STRING:
            FreeString( &p_val->p_list->p_values[i] );
            break;
        case VLC_VAR_MUTEX:
            FreeMutex( &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 );
}

138
static const struct variable_ops_t
hartman's avatar
hartman committed
139
void_ops   = { NULL,       DupDummy,  FreeDummy,  },
140 141 142 143 144 145 146 147 148
addr_ops   = { CmpAddress, DupDummy,  FreeDummy,  },
bool_ops   = { CmpBool,    DupDummy,  FreeDummy,  },
float_ops  = { CmpFloat,   DupDummy,  FreeDummy,  },
int_ops    = { CmpInt,     DupDummy,  FreeDummy,  },
list_ops   = { CmpAddress, DupList,   FreeList,   },
mutex_ops  = { CmpAddress, DupDummy,  FreeMutex,  },
string_ops = { CmpString,  DupString, FreeString, },
time_ops   = { CmpTime,    DupDummy,  FreeDummy,  };

149 150 151
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
152 153 154 155
static int      GetUnused   ( vlc_object_t *, const char * );
static uint32_t HashString  ( const char * );
static int      Insert      ( variable_t *, int, const char * );
static int      InsertInner ( variable_t *, int, uint32_t );
156
static int      Lookup      ( variable_t *, size_t, const char * );
157

158
static void     CheckValue  ( variable_t *, vlc_value_t * );
159

gbazin's avatar
 
gbazin committed
160 161 162
static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
                              int );

sigmunau's avatar
sigmunau committed
163 164 165
/**
 * Initialize a vlc variable
 *
166 167 168
 * 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!
sigmunau's avatar
sigmunau committed
169 170 171 172 173 174
 *
 * \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
 */
175
int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
176 177
{
    int i_new;
178
    variable_t *p_var;
gbazin's avatar
 
gbazin committed
179
    static vlc_list_t dummy_null_list = {0, NULL, NULL};
180
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
181

182
    vlc_mutex_lock( &p_priv->var_lock );
183

184 185 186 187
    /* FIXME: if the variable already exists, we don't duplicate it. But we
     * duplicate the lookups. It's not that serious, but if anyone finds some
     * time to rework Insert() so that only one lookup has to be done, feel
     * free to do so. */
188
    i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
189 190 191 192

    if( i_new >= 0 )
    {
        /* If the types differ, variable creation failed. */
193
        if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
194
        {
195
            vlc_mutex_unlock( &p_priv->var_lock );
196 197 198
            return VLC_EBADVAR;
        }

199
        p_priv->p_vars[i_new].i_usage++;
200
        if( i_type & VLC_VAR_ISCOMMAND )
201 202
            p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
        vlc_mutex_unlock( &p_priv->var_lock );
203 204 205
        return VLC_SUCCESS;
    }

206
    i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
207

208
    if( (p_priv->i_vars & 15) == 15 )
209
    {
210 211
        p_priv->p_vars = realloc( p_priv->p_vars,
                                  (p_priv->i_vars+17) * sizeof(variable_t) );
212 213
    }

214 215 216
    memmove( p_priv->p_vars + i_new + 1,
             p_priv->p_vars + i_new,
             (p_priv->i_vars - i_new) * sizeof(variable_t) );
217

218
    p_priv->i_vars++;
219

220
    p_var = &p_priv->p_vars[i_new];
221
    memset( p_var, 0, sizeof(*p_var) );
222 223 224

    p_var->i_hash = HashString( psz_name );
    p_var->psz_name = strdup( psz_name );
gbazin's avatar
 
gbazin committed
225
    p_var->psz_text = NULL;
226

gbazin's avatar
 
gbazin committed
227
    p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
228
    memset( &p_var->val, 0, sizeof(vlc_value_t) );
229

230
    p_var->i_usage = 1;
231

232
    p_var->i_default = -1;
gbazin's avatar
 
gbazin committed
233 234
    p_var->choices.i_count = 0;
    p_var->choices.p_values = NULL;
gbazin's avatar
 
gbazin committed
235 236
    p_var->choices_text.i_count = 0;
    p_var->choices_text.p_values = NULL;
237

238
    p_var->b_incallback = false;
239 240 241
    p_var->i_entries = 0;
    p_var->p_entries = NULL;

242 243 244
    /* 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. */
hartman's avatar
hartman committed
245
    switch( i_type & VLC_VAR_CLASS )
246
    {
247
        case VLC_VAR_BOOL:
248
            p_var->ops = &bool_ops;
249
            p_var->val.b_bool = false;
250 251
            break;
        case VLC_VAR_INTEGER:
252
            p_var->ops = &int_ops;
253 254 255
            p_var->val.i_int = 0;
            break;
        case VLC_VAR_STRING:
256
            p_var->ops = &string_ops;
257
            p_var->val.psz_string = NULL;
258 259
            break;
        case VLC_VAR_FLOAT:
260
            p_var->ops = &float_ops;
261 262 263
            p_var->val.f_float = 0.0;
            break;
        case VLC_VAR_TIME:
264
            p_var->ops = &time_ops;
265
            p_var->val.i_time = 0;
266 267
            break;
        case VLC_VAR_ADDRESS:
268
            p_var->ops = &addr_ops;
269 270
            p_var->val.p_address = NULL;
            break;
271
        case VLC_VAR_MUTEX:
272
            p_var->ops = &mutex_ops;
273
            p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
274
            vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
275
            break;
gbazin's avatar
 
gbazin committed
276
        case VLC_VAR_LIST:
277
            p_var->ops = &list_ops;
gbazin's avatar
 
gbazin committed
278 279
            p_var->val.p_list = &dummy_null_list;
            break;
hartman's avatar
hartman committed
280 281 282
        default:
            p_var->ops = &void_ops;
            break;
283
    }
284

285
    /* Duplicate the default data we stored. */
286
    p_var->ops->pf_dup( &p_var->val );
287

gbazin's avatar
 
gbazin committed
288 289 290 291 292
    if( i_type & VLC_VAR_DOINHERIT )
    {
        vlc_value_t val;

        if( InheritValue( p_this, psz_name, &val, p_var->i_type )
zorglub's avatar
zorglub committed
293
            == VLC_SUCCESS )
gbazin's avatar
 
gbazin committed
294 295
        {
            /* Free data if needed */
296
            p_var->ops->pf_free( &p_var->val );
gbazin's avatar
 
gbazin committed
297 298
            /* Set the variable */
            p_var->val = val;
gbazin's avatar
 
gbazin committed
299 300 301 302 303 304 305 306 307 308

            if( i_type & VLC_VAR_HASCHOICE )
            {
                /* 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, val );
                INSERT_ELEM( p_var->choices_text.p_values,
                             p_var->choices_text.i_count, 0, val );
309
                p_var->ops->pf_dup( &p_var->choices.p_values[0] );
gbazin's avatar
 
gbazin committed
310 311
                p_var->choices_text.p_values[0].psz_string = NULL;
            }
gbazin's avatar
 
gbazin committed
312 313 314
        }
    }

315
    vlc_mutex_unlock( &p_priv->var_lock );
316 317

    return VLC_SUCCESS;
318 319
}

sigmunau's avatar
sigmunau committed
320 321 322
/**
 * Destroy a vlc variable
 *
323 324
 * 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.
sigmunau's avatar
sigmunau committed
325 326 327 328
 *
 * \param p_this The object that holds the variable
 * \param psz_name The name of the variable
 */
329
int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
330
{
331
    int i_var, i;
332
    variable_t *p_var;
333
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
334

335
    vlc_mutex_lock( &p_priv->var_lock );
336

337 338
    i_var = GetUnused( p_this, psz_name );
    if( i_var < 0 )
339
    {
340
        vlc_mutex_unlock( &p_priv->var_lock );
341
        return i_var;
342 343
    }

344
    p_var = &p_priv->p_vars[i_var];
345 346 347 348

    if( p_var->i_usage > 1 )
    {
        p_var->i_usage--;
349
        vlc_mutex_unlock( &p_priv->var_lock );
350 351 352
        return VLC_SUCCESS;
    }

353
    /* Free value if needed */
354
    p_var->ops->pf_free( &p_var->val );
355

356
    /* Free choice list if needed */
gbazin's avatar
 
gbazin committed
357
    if( p_var->choices.i_count )
358
    {
gbazin's avatar
 
gbazin committed
359
        for( i = 0 ; i < p_var->choices.i_count ; i++ )
360
        {
361
            p_var->ops->pf_free( &p_var->choices.p_values[i] );
362
            free( p_var->choices_text.p_values[i].psz_string );
363
        }
gbazin's avatar
 
gbazin committed
364
        free( p_var->choices.p_values );
365
        free( p_var->choices_text.p_values );
366 367
    }

368 369 370 371 372 373
    /* Free callbacks if needed */
    if( p_var->p_entries )
    {
        free( p_var->p_entries );
    }

374
    free( p_var->psz_name );
375
    free( p_var->psz_text );
376

377 378 379
    memmove( p_priv->p_vars + i_var,
             p_priv->p_vars + i_var + 1,
             (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
380

381
    if( (p_priv->i_vars & 15) == 0 )
382
    {
383 384
        p_priv->p_vars = realloc( p_priv->p_vars,
                          (p_priv->i_vars) * sizeof( variable_t ) );
385 386
    }

387
    p_priv->i_vars--;
388

389
    vlc_mutex_unlock( &p_priv->var_lock );
390 391 392 393

    return VLC_SUCCESS;
}

sigmunau's avatar
sigmunau committed
394 395
/**
 * Perform an action on a variable
396
 *
sigmunau's avatar
sigmunau committed
397 398 399 400 401 402
 * \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
 */
403
int __var_Change( vlc_object_t *p_this, const char *psz_name,
gbazin's avatar
 
gbazin committed
404
                  int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
405
{
406
    int i_var, i;
407
    variable_t *p_var;
gbazin's avatar
 
gbazin committed
408
    vlc_value_t oldval;
409
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
410

411
    vlc_mutex_lock( &p_priv->var_lock );
412

413
    i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
414 415 416

    if( i_var < 0 )
    {
417
        vlc_mutex_unlock( &p_priv->var_lock );
418 419 420
        return VLC_ENOVAR;
    }

421
    p_var = &p_priv->p_vars[i_var];
422 423 424 425

    switch( i_action )
    {
        case VLC_VAR_SETMIN:
426 427
            if( p_var->i_type & VLC_VAR_HASMIN )
            {
428
                p_var->ops->pf_free( &p_var->min );
429 430
            }
            p_var->i_type |= VLC_VAR_HASMIN;
431
            p_var->min = *p_val;
432
            p_var->ops->pf_dup( &p_var->min );
433 434
            CheckValue( p_var, &p_var->val );
            break;
435 436 437 438 439 440
        case VLC_VAR_GETMIN:
            if( p_var->i_type & VLC_VAR_HASMIN )
            {
                *p_val = p_var->min;
            }
            break;
441
        case VLC_VAR_SETMAX:
442 443
            if( p_var->i_type & VLC_VAR_HASMAX )
            {
444
                p_var->ops->pf_free( &p_var->max );
445 446
            }
            p_var->i_type |= VLC_VAR_HASMAX;
447
            p_var->max = *p_val;
448
            p_var->ops->pf_dup( &p_var->max );
449 450
            CheckValue( p_var, &p_var->val );
            break;
451 452 453 454 455 456
        case VLC_VAR_GETMAX:
            if( p_var->i_type & VLC_VAR_HASMAX )
            {
                *p_val = p_var->max;
            }
            break;
457
        case VLC_VAR_SETSTEP:
458 459
            if( p_var->i_type & VLC_VAR_HASSTEP )
            {
460
                p_var->ops->pf_free( &p_var->step );
461 462
            }
            p_var->i_type |= VLC_VAR_HASSTEP;
463
            p_var->step = *p_val;
464
            p_var->ops->pf_dup( &p_var->step );
465 466
            CheckValue( p_var, &p_var->val );
            break;
467 468 469 470 471 472
        case VLC_VAR_GETSTEP:
            if( p_var->i_type & VLC_VAR_HASSTEP )
            {
                *p_val = p_var->step;
            }
            break;
473
        case VLC_VAR_ADDCHOICE:
474
            i = p_var->choices.i_count;
475

gbazin's avatar
 
gbazin committed
476 477
            INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
                         i, *p_val );
gbazin's avatar
 
gbazin committed
478
            INSERT_ELEM( p_var->choices_text.p_values,
gbazin's avatar
 
gbazin committed
479
                         p_var->choices_text.i_count, i, *p_val );
480
            p_var->ops->pf_dup( &p_var->choices.p_values[i] );
gbazin's avatar
 
gbazin committed
481 482 483
            p_var->choices_text.p_values[i].psz_string =
                ( p_val2 && p_val2->psz_string ) ?
                strdup( p_val2->psz_string ) : NULL;
484

485 486 487
            CheckValue( p_var, &p_var->val );
            break;
        case VLC_VAR_DELCHOICE:
gbazin's avatar
 
gbazin committed
488
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
489
            {
490
                if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
491 492 493 494 495
                {
                    break;
                }
            }

gbazin's avatar
 
gbazin committed
496
            if( i == p_var->choices.i_count )
497 498
            {
                /* Not found */
499
                vlc_mutex_unlock( &p_priv->var_lock );
500 501 502 503 504 505 506 507 508 509 510 511
                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;
            }

512
            p_var->ops->pf_free( &p_var->choices.p_values[i] );
513
            free( p_var->choices_text.p_values[i].psz_string );
gbazin's avatar
 
gbazin committed
514
            REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
gbazin's avatar
 
gbazin committed
515 516
            REMOVE_ELEM( p_var->choices_text.p_values,
                         p_var->choices_text.i_count, i );
517 518 519

            CheckValue( p_var, &p_var->val );
            break;
gbazin's avatar
 
gbazin committed
520 521 522
        case VLC_VAR_CHOICESCOUNT:
            p_val->i_int = p_var->choices.i_count;
            break;
gbazin's avatar
 
gbazin committed
523 524 525
        case VLC_VAR_CLEARCHOICES:
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
            {
526
                p_var->ops->pf_free( &p_var->choices.p_values[i] );
gbazin's avatar
 
gbazin committed
527
            }
528
            for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
529 530
                free( p_var->choices_text.p_values[i].psz_string );

531 532
            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 );
gbazin's avatar
 
gbazin committed
533 534 535

            p_var->choices.i_count = 0;
            p_var->choices.p_values = NULL;
536 537
            p_var->choices_text.i_count = 0;
            p_var->choices_text.p_values = NULL;
gbazin's avatar
 
gbazin committed
538 539
            p_var->i_default = -1;
            break;
540 541
        case VLC_VAR_SETDEFAULT:
            /* FIXME: the list is sorted, dude. Use something cleverer. */
gbazin's avatar
 
gbazin committed
542
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
543
            {
544
                if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
545 546 547 548 549
                {
                    break;
                }
            }

gbazin's avatar
 
gbazin committed
550
            if( i == p_var->choices.i_count )
551 552 553 554 555 556
            {
                /* Not found */
                break;
            }

            p_var->i_default = i;
557 558
            CheckValue( p_var, &p_var->val );
            break;
gbazin's avatar
 
gbazin committed
559 560
        case VLC_VAR_SETVALUE:
            /* Duplicate data if needed */
561
            p_var->ops->pf_dup( p_val );
gbazin's avatar
 
gbazin committed
562 563 564 565 566 567 568
            /* Backup needed stuff */
            oldval = p_var->val;
            /* Check boundaries and list */
            CheckValue( p_var, p_val );
            /* Set the variable */
            p_var->val = *p_val;
            /* Free data if needed */
569
            p_var->ops->pf_free( &oldval );
gbazin's avatar
 
gbazin committed
570
            break;
gbazin's avatar
 
gbazin committed
571
        case VLC_VAR_GETCHOICES:
572
        case VLC_VAR_GETLIST:
gbazin's avatar
 
gbazin committed
573
            p_val->p_list = malloc( sizeof(vlc_list_t) );
gbazin's avatar
 
gbazin committed
574
            if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
gbazin's avatar
 
gbazin committed
575
            if( p_var->choices.i_count )
gbazin's avatar
 
gbazin committed
576
            {
gbazin's avatar
 
gbazin committed
577 578
                p_val->p_list->p_values = malloc( p_var->choices.i_count
                                                  * sizeof(vlc_value_t) );
gbazin's avatar
 
gbazin committed
579 580 581 582 583 584 585 586 587 588
                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) );
                }
            }
gbazin's avatar
 
gbazin committed
589
            p_val->p_list->i_count = p_var->choices.i_count;
gbazin's avatar
 
gbazin committed
590
            if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
gbazin's avatar
 
gbazin committed
591
            for( i = 0 ; i < p_var->choices.i_count ; i++ )
592
            {
gbazin's avatar
 
gbazin committed
593
                p_val->p_list->p_values[i] = p_var->choices.p_values[i];
gbazin's avatar
 
gbazin committed
594
                p_val->p_list->pi_types[i] = p_var->i_type;
595
                p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
gbazin's avatar
 
gbazin committed
596 597 598 599 600 601 602
                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;
                }
603 604 605
            }
            break;
        case VLC_VAR_FREELIST:
gbazin's avatar
 
gbazin committed
606
            FreeList( p_val );
gbazin's avatar
 
gbazin committed
607 608 609
            if( p_val2 && p_val2->p_list )
            {
                for( i = 0; i < p_val2->p_list->i_count; i++ )
610
                    free( p_val2->p_list->p_values[i].psz_string );
gbazin's avatar
 
gbazin committed
611 612 613 614 615 616 617
                if( p_val2->p_list->i_count )
                {
                    free( p_val2->p_list->p_values );
                    free( p_val2->p_list->pi_types );
                }
                free( p_val2->p_list );
            }
gbazin's avatar
 
gbazin committed
618 619
            break;
        case VLC_VAR_SETTEXT:
620
            free( p_var->psz_text );
gbazin's avatar
 
gbazin committed
621 622 623 624 625 626
            if( p_val && p_val->psz_string )
                p_var->psz_text = strdup( p_val->psz_string );
            break;
        case VLC_VAR_GETTEXT:
            p_val->psz_string = NULL;
            if( p_var->psz_text )
627
            {
gbazin's avatar
 
gbazin committed
628
                p_val->psz_string = strdup( p_var->psz_text );
629
            }
630
            break;
gbazin's avatar
 
gbazin committed
631 632 633 634
        case VLC_VAR_INHERITVALUE:
            {
                vlc_value_t val;

635 636 637
                if( InheritValue( p_this,
                                  p_val2 ? p_val2->psz_string :  psz_name,
                                  &val, p_var->i_type )
zorglub's avatar
zorglub committed
638
                    == VLC_SUCCESS )
gbazin's avatar
 
gbazin committed
639 640 641 642 643 644 645 646 647 648
                {
                    /* Duplicate already done */

                    /* Backup needed stuff */
                    oldval = p_var->val;
                    /* Check boundaries and list */
                    CheckValue( p_var, &val );
                    /* Set the variable */
                    p_var->val = val;
                    /* Free data if needed */
649
                    p_var->ops->pf_free( &oldval );
gbazin's avatar
 
gbazin committed
650
                }
gbazin's avatar
 
gbazin committed
651 652 653 654

                if( p_val )
                {
                    *p_val = p_var->val;
655
                    p_var->ops->pf_dup( p_val );
gbazin's avatar
 
gbazin committed
656
                }
hartman's avatar
hartman committed
657
            }
gbazin's avatar
 
gbazin committed
658
            break;
659

660 661 662 663
        case VLC_VAR_SETISCOMMAND:
            p_var->i_type |= VLC_VAR_ISCOMMAND;
            break;

664 665 666 667
        default:
            break;
    }

668
    vlc_mutex_unlock( &p_priv->var_lock );
669 670 671 672

    return VLC_SUCCESS;
}

sigmunau's avatar
sigmunau committed
673 674 675 676
/**
 * Request a variable's type
 *
 * \return The variable type if it exists, or 0 if the
677
 * variable could not be found.
sigmunau's avatar
sigmunau committed
678 679
 * \see \ref var_type
 */
680 681 682
int __var_Type( vlc_object_t *p_this, const char *psz_name )
{
    int i_var, i_type;
683
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
684

685
    vlc_mutex_lock( &p_priv->var_lock );
686

687
    i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
688 689 690

    if( i_var < 0 )
    {
691
        vlc_mutex_unlock( &p_priv->var_lock );
gbazin's avatar
 
gbazin committed
692
        return 0;
693 694
    }

695
    i_type = p_priv->p_vars[i_var].i_type;
696

697
    vlc_mutex_unlock( &p_priv->var_lock );
698 699

    return i_type;
700 701
}

702 703
int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
                    int expected_type, vlc_value_t val )
704 705
{
    int i_var;
706 707
    variable_t *p_var;
    vlc_value_t oldval;
708
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
709

710
    vlc_mutex_lock( &p_priv->var_lock );
711

712
    i_var = GetUnused( p_this, psz_name );
713 714
    if( i_var < 0 )
    {
715
        vlc_mutex_unlock( &p_priv->var_lock );
716
        return i_var;
717 718
    }

719
    p_var = &p_priv->p_vars[i_var];
720 721
    assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
            (p_var->i_type & VLC_VAR_CLASS) == expected_type );
722 723

    /* Duplicate data if needed */
724
    p_var->ops->pf_dup( &val );
725

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

729
    /* Check boundaries and list */
730 731
    CheckValue( p_var, &val );

732 733 734
    /* Set the variable */
    p_var->val = val;

735 736
    /* Deal with callbacks. Tell we're in a callback, release the lock,
     * call stored functions, retake the lock. */
737 738
    if( p_var->i_entries )
    {
739 740 741
        int i_var;
        int i_entries = p_var->i_entries;
        callback_entry_t *p_entries = p_var->p_entries;
742

743
        p_var->b_incallback = true;
744
        vlc_mutex_unlock( &p_priv->var_lock );
745 746 747 748 749 750 751 752

        /* The real calls */
        for( ; i_entries-- ; )
        {
            p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
                                              p_entries[i_entries].p_data );
        }

753
        vlc_mutex_lock( &p_priv->var_lock );
754

755
        i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
756
        if( i_var < 0 )
757
        {
758
            msg_Err( p_this, "variable %s has disappeared", psz_name );
759
            vlc_mutex_unlock( &p_priv->var_lock );
760
            return VLC_ENOVAR;
761
        }
762

763
        p_var = &p_priv->p_vars[i_var];
764
        p_var->b_incallback = false;
765
        vlc_cond_broadcast( &p_priv->var_wait );
766 767 768
    }

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

771
    vlc_mutex_unlock( &p_priv->var_lock );
772 773 774 775

    return VLC_SUCCESS;
}

776

sigmunau's avatar
sigmunau committed
777
/**
778
 * Set a variable's value
779
 *
780
 * \param p_this The object that hold the variable
sigmunau's avatar
sigmunau committed
781
 * \param psz_name The name of the variable
782
 * \param val the value to set
sigmunau's avatar
sigmunau committed
783
 */
784 785 786 787 788 789 790
int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
{
    return var_SetChecked( p_this, psz_name, 0, val );
}

int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
                    int expected_type, vlc_value_t *p_val )
791
{
792
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
793
    int i_var, err = VLC_SUCCESS;
794