]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: routines for object variables handling
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "variables.h"
34
35 #include "libvlc.h"
36
37 #include <search.h>
38 #include <assert.h>
39 #include <math.h>
40 #include <limits.h>
41
42 /*****************************************************************************
43  * Private types
44  *****************************************************************************/
45 struct callback_entry_t
46 {
47     vlc_callback_t pf_callback;
48     void *         p_data;
49 };
50
51 /*****************************************************************************
52  * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
53  *****************************************************************************/
54 static int CmpBool( vlc_value_t v, vlc_value_t w )
55 {
56     return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
57 }
58
59 static int CmpInt( vlc_value_t v, vlc_value_t w )
60 {
61     return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
62 }
63
64 static int CmpTime( vlc_value_t v, vlc_value_t w )
65 {
66     return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
67 }
68
69 static int CmpString( vlc_value_t v, vlc_value_t w )
70 {
71     if( !v.psz_string )
72         return !w.psz_string ? 0 : -1;
73     else
74         return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
75 }
76 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; }
77 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; }
78
79 /*****************************************************************************
80  * Local duplication functions, and local deallocation functions
81  *****************************************************************************/
82 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
83 static void DupString( vlc_value_t *p_val )
84 {
85     p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string :  "" );
86 }
87
88 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
89 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
90 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
91
92 static void FreeList( vlc_value_t *p_val )
93 {
94     int i;
95     for( i = 0; i < p_val->p_list->i_count; i++ )
96     {
97         switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
98         {
99         case VLC_VAR_STRING:
100             FreeString( &p_val->p_list->p_values[i] );
101             break;
102         case VLC_VAR_MUTEX:
103             FreeMutex( &p_val->p_list->p_values[i] );
104             break;
105         default:
106             break;
107         }
108     }
109
110     if( p_val->p_list->i_count )
111     {
112         free( p_val->p_list->p_values );
113         free( p_val->p_list->pi_types );
114     }
115     free( p_val->p_list );
116 }
117
118 static const struct variable_ops_t
119 void_ops   = { NULL,       DupDummy,  FreeDummy,  },
120 addr_ops   = { CmpAddress, DupDummy,  FreeDummy,  },
121 bool_ops   = { CmpBool,    DupDummy,  FreeDummy,  },
122 float_ops  = { CmpFloat,   DupDummy,  FreeDummy,  },
123 int_ops    = { CmpInt,     DupDummy,  FreeDummy,  },
124 mutex_ops  = { CmpAddress, DupDummy,  FreeMutex,  },
125 string_ops = { CmpString,  DupString, FreeString, },
126 time_ops   = { CmpTime,    DupDummy,  FreeDummy,  },
127 coords_ops = { NULL,       DupDummy,  FreeDummy,  };
128
129 /*****************************************************************************
130  * Local prototypes
131  *****************************************************************************/
132 static void     WaitUnused  ( vlc_object_t *, variable_t * );
133
134 static void     CheckValue  ( variable_t *, vlc_value_t * );
135
136 static int      TriggerCallback( vlc_object_t *, variable_t *, const char *,
137                                  vlc_value_t );
138
139 static int varcmp( const void *a, const void *b )
140 {
141     const variable_t *va = a, *vb = b;
142
143     /* psz_name must be first */
144     assert( va == (const void *)&va->psz_name );
145     return strcmp( va->psz_name, vb->psz_name );
146 }
147
148 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
149 {
150     vlc_object_internals_t *priv = vlc_internals( obj );
151     variable_t **pp_var;
152
153     vlc_assert_locked( &priv->var_lock );
154     pp_var = tfind( &psz_name, &priv->var_root, varcmp );
155     return (pp_var != NULL) ? *pp_var : NULL;
156 }
157
158 static void Destroy( variable_t *p_var )
159 {
160     p_var->ops->pf_free( &p_var->val );
161     if( p_var->choices.i_count )
162     {
163         for( int i = 0 ; i < p_var->choices.i_count ; i++ )
164         {
165             p_var->ops->pf_free( &p_var->choices.p_values[i] );
166             free( p_var->choices_text.p_values[i].psz_string );
167         }
168         free( p_var->choices.p_values );
169         free( p_var->choices_text.p_values );
170     }
171     free( p_var->psz_name );
172     free( p_var->psz_text );
173     free( p_var->p_entries );
174     free( p_var );
175 }
176
177 #undef var_Create
178 /**
179  * Initialize a vlc variable
180  *
181  * We hash the given string and insert it into the sorted list. The insertion
182  * may require slow memory copies, but think about what we gain in the log(n)
183  * lookup phase when setting/getting the variable value!
184  *
185  * \param p_this The object in which to create the variable
186  * \param psz_name The name of the variable
187  * \param i_type The variables type. Must be one of \ref var_type combined with
188  *               zero or more \ref var_flags
189  */
190 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
191 {
192     assert( p_this );
193
194     variable_t *p_var = calloc( 1, sizeof( *p_var ) );
195     if( p_var == NULL )
196         return VLC_ENOMEM;
197
198     p_var->psz_name = strdup( psz_name );
199     p_var->psz_text = NULL;
200
201     p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
202
203     p_var->i_usage = 1;
204
205     p_var->i_default = -1;
206     p_var->choices.i_count = 0;
207     p_var->choices.p_values = NULL;
208     p_var->choices_text.i_count = 0;
209     p_var->choices_text.p_values = NULL;
210
211     p_var->b_incallback = false;
212     p_var->i_entries = 0;
213     p_var->p_entries = NULL;
214
215     /* Always initialize the variable, even if it is a list variable; this
216      * will lead to errors if the variable is not initialized, but it will
217      * not cause crashes in the variable handling. */
218     switch( i_type & VLC_VAR_CLASS )
219     {
220         case VLC_VAR_BOOL:
221             p_var->ops = &bool_ops;
222             p_var->val.b_bool = false;
223             break;
224         case VLC_VAR_INTEGER:
225             p_var->ops = &int_ops;
226             p_var->val.i_int = 0;
227             break;
228         case VLC_VAR_STRING:
229             p_var->ops = &string_ops;
230             p_var->val.psz_string = NULL;
231             break;
232         case VLC_VAR_FLOAT:
233             p_var->ops = &float_ops;
234             p_var->val.f_float = 0.0;
235             break;
236         case VLC_VAR_TIME:
237             p_var->ops = &time_ops;
238             p_var->val.i_time = 0;
239             break;
240         case VLC_VAR_COORDS:
241             p_var->ops = &coords_ops;
242             p_var->val.coords.x = p_var->val.coords.y = 0;
243             break;
244         case VLC_VAR_ADDRESS:
245             p_var->ops = &addr_ops;
246             p_var->val.p_address = NULL;
247             break;
248         case VLC_VAR_MUTEX:
249             p_var->ops = &mutex_ops;
250             p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
251             vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
252             break;
253         default:
254             p_var->ops = &void_ops;
255 #ifndef NDEBUG
256             if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
257                 msg_Err( p_this, "Creating the variable '%s' without a type",
258                           psz_name );
259 #endif
260     }
261
262     if( i_type & VLC_VAR_DOINHERIT )
263     {
264         if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
265             msg_Err( p_this, "cannot inherit value for %s", psz_name );
266         else if( i_type & VLC_VAR_HASCHOICE )
267         {
268             /* We must add the inherited value to our choice list */
269             p_var->i_default = 0;
270
271             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
272                          0, p_var->val );
273             INSERT_ELEM( p_var->choices_text.p_values,
274                          p_var->choices_text.i_count, 0, p_var->val );
275             p_var->ops->pf_dup( &p_var->choices.p_values[0] );
276             p_var->choices_text.p_values[0].psz_string = NULL;
277         }
278     }
279
280     vlc_object_internals_t *p_priv = vlc_internals( p_this );
281     variable_t **pp_var, *p_oldvar;
282     int ret = VLC_SUCCESS;
283
284     vlc_mutex_lock( &p_priv->var_lock );
285
286     pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
287     if( unlikely(pp_var == NULL) )
288         ret = VLC_ENOMEM;
289     else if( (p_oldvar = *pp_var) == p_var )
290         p_var = NULL;
291     else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
292     {    /* If the types differ, variable creation failed. */
293          msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
294                   "but with a different type (0x%04x)",
295                   psz_name, p_oldvar->i_type, i_type );
296          ret = VLC_EBADVAR;
297     }
298     else
299     {
300         p_oldvar->i_usage++;
301         p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
302     }
303     vlc_mutex_unlock( &p_priv->var_lock );
304
305     /* If we did not need to create a new variable, free everything... */
306     if( p_var != NULL )
307         Destroy( p_var );
308     return ret;
309 }
310
311 #undef var_Destroy
312 /**
313  * Destroy a vlc variable
314  *
315  * Look for the variable and destroy it if it is found. As in var_Create we
316  * do a call to memmove() but we have performance counterparts elsewhere.
317  *
318  * \param p_this The object that holds the variable
319  * \param psz_name The name of the variable
320  */
321 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
322 {
323     variable_t *p_var;
324
325     assert( p_this );
326
327     vlc_object_internals_t *p_priv = vlc_internals( p_this );
328
329     vlc_mutex_lock( &p_priv->var_lock );
330
331     p_var = Lookup( p_this, psz_name );
332     if( p_var == NULL )
333     {
334         vlc_mutex_unlock( &p_priv->var_lock );
335         return VLC_ENOVAR;
336     }
337
338     WaitUnused( p_this, p_var );
339
340     if( --p_var->i_usage == 0 )
341         tdelete( p_var, &p_priv->var_root, varcmp );
342     else
343         p_var = NULL;
344     vlc_mutex_unlock( &p_priv->var_lock );
345
346     if( p_var != NULL )
347         Destroy( p_var );
348     return VLC_SUCCESS;
349 }
350
351 static void CleanupVar( void *var )
352 {
353     Destroy( var );
354 }
355
356 void var_DestroyAll( vlc_object_t *obj )
357 {
358     vlc_object_internals_t *priv = vlc_internals( obj );
359
360     tdestroy( priv->var_root, CleanupVar );
361 }
362
363 #undef var_Change
364 /**
365  * Perform an action on a variable
366  *
367  * \param p_this The object that holds the variable
368  * \param psz_name The name of the variable
369  * \param i_action The action to perform. Must be one of \ref var_action
370  * \param p_val First action parameter
371  * \param p_val2 Second action parameter
372  */
373 int var_Change( vlc_object_t *p_this, const char *psz_name,
374                 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
375 {
376     int i;
377     variable_t *p_var;
378     vlc_value_t oldval;
379     vlc_value_t newval;
380
381     assert( p_this );
382
383     vlc_object_internals_t *p_priv = vlc_internals( p_this );
384
385     vlc_mutex_lock( &p_priv->var_lock );
386
387     p_var = Lookup( p_this, psz_name );
388     if( p_var == NULL )
389     {
390         vlc_mutex_unlock( &p_priv->var_lock );
391         return VLC_ENOVAR;
392     }
393
394     switch( i_action )
395     {
396         case VLC_VAR_SETMIN:
397             if( p_var->i_type & VLC_VAR_HASMIN )
398             {
399                 p_var->ops->pf_free( &p_var->min );
400             }
401             p_var->i_type |= VLC_VAR_HASMIN;
402             p_var->min = *p_val;
403             p_var->ops->pf_dup( &p_var->min );
404             CheckValue( p_var, &p_var->val );
405             break;
406         case VLC_VAR_GETMIN:
407             if( p_var->i_type & VLC_VAR_HASMIN )
408             {
409                 *p_val = p_var->min;
410             }
411             break;
412         case VLC_VAR_SETMAX:
413             if( p_var->i_type & VLC_VAR_HASMAX )
414             {
415                 p_var->ops->pf_free( &p_var->max );
416             }
417             p_var->i_type |= VLC_VAR_HASMAX;
418             p_var->max = *p_val;
419             p_var->ops->pf_dup( &p_var->max );
420             CheckValue( p_var, &p_var->val );
421             break;
422         case VLC_VAR_GETMAX:
423             if( p_var->i_type & VLC_VAR_HASMAX )
424             {
425                 *p_val = p_var->max;
426             }
427             break;
428         case VLC_VAR_SETSTEP:
429             if( p_var->i_type & VLC_VAR_HASSTEP )
430             {
431                 p_var->ops->pf_free( &p_var->step );
432             }
433             p_var->i_type |= VLC_VAR_HASSTEP;
434             p_var->step = *p_val;
435             p_var->ops->pf_dup( &p_var->step );
436             CheckValue( p_var, &p_var->val );
437             break;
438         case VLC_VAR_GETSTEP:
439             if( p_var->i_type & VLC_VAR_HASSTEP )
440             {
441                 *p_val = p_var->step;
442             }
443             break;
444         case VLC_VAR_ADDCHOICE:
445             i = p_var->choices.i_count;
446
447             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
448                          i, *p_val );
449             INSERT_ELEM( p_var->choices_text.p_values,
450                          p_var->choices_text.i_count, i, *p_val );
451             p_var->ops->pf_dup( &p_var->choices.p_values[i] );
452             p_var->choices_text.p_values[i].psz_string =
453                 ( p_val2 && p_val2->psz_string ) ?
454                 strdup( p_val2->psz_string ) : NULL;
455
456             CheckValue( p_var, &p_var->val );
457             break;
458         case VLC_VAR_DELCHOICE:
459             for( i = 0 ; i < p_var->choices.i_count ; i++ )
460             {
461                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
462                 {
463                     break;
464                 }
465             }
466
467             if( i == p_var->choices.i_count )
468             {
469                 /* Not found */
470                 vlc_mutex_unlock( &p_priv->var_lock );
471                 return VLC_EGENERIC;
472             }
473
474             if( p_var->i_default > i )
475             {
476                 p_var->i_default--;
477             }
478             else if( p_var->i_default == i )
479             {
480                 p_var->i_default = -1;
481             }
482
483             p_var->ops->pf_free( &p_var->choices.p_values[i] );
484             free( p_var->choices_text.p_values[i].psz_string );
485             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
486             REMOVE_ELEM( p_var->choices_text.p_values,
487                          p_var->choices_text.i_count, i );
488
489             CheckValue( p_var, &p_var->val );
490             break;
491         case VLC_VAR_CHOICESCOUNT:
492             p_val->i_int = p_var->choices.i_count;
493             break;
494         case VLC_VAR_CLEARCHOICES:
495             for( i = 0 ; i < p_var->choices.i_count ; i++ )
496             {
497                 p_var->ops->pf_free( &p_var->choices.p_values[i] );
498             }
499             for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
500                 free( p_var->choices_text.p_values[i].psz_string );
501
502             if( p_var->choices.i_count ) free( p_var->choices.p_values );
503             if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
504
505             p_var->choices.i_count = 0;
506             p_var->choices.p_values = NULL;
507             p_var->choices_text.i_count = 0;
508             p_var->choices_text.p_values = NULL;
509             p_var->i_default = -1;
510             break;
511         case VLC_VAR_SETDEFAULT:
512             /* FIXME: the list is sorted, dude. Use something cleverer. */
513             for( i = 0 ; i < p_var->choices.i_count ; i++ )
514             {
515                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
516                 {
517                     break;
518                 }
519             }
520
521             if( i == p_var->choices.i_count )
522             {
523                 /* Not found */
524                 break;
525             }
526
527             p_var->i_default = i;
528             CheckValue( p_var, &p_var->val );
529             break;
530         case VLC_VAR_SETVALUE:
531             /* Duplicate data if needed */
532             newval = *p_val;
533             p_var->ops->pf_dup( &newval );
534             /* Backup needed stuff */
535             oldval = p_var->val;
536             /* Check boundaries and list */
537             CheckValue( p_var, &newval );
538             /* Set the variable */
539             p_var->val = newval;
540             /* Free data if needed */
541             p_var->ops->pf_free( &oldval );
542             break;
543         case VLC_VAR_GETCHOICES:
544         case VLC_VAR_GETLIST:
545             p_val->p_list = malloc( sizeof(vlc_list_t) );
546             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
547             if( p_var->choices.i_count )
548             {
549                 p_val->p_list->p_values = malloc( p_var->choices.i_count
550                                                   * sizeof(vlc_value_t) );
551                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
552                                                   * sizeof(int) );
553                 if( p_val2 )
554                 {
555                     p_val2->p_list->p_values =
556                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
557                     p_val2->p_list->pi_types =
558                         malloc( p_var->choices.i_count * sizeof(int) );
559                 }
560             }
561             p_val->p_list->i_count = p_var->choices.i_count;
562             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
563             for( i = 0 ; i < p_var->choices.i_count ; i++ )
564             {
565                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
566                 p_val->p_list->pi_types[i] = p_var->i_type;
567                 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
568                 if( p_val2 )
569                 {
570                     p_val2->p_list->p_values[i].psz_string =
571                         p_var->choices_text.p_values[i].psz_string ?
572                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
573                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
574                 }
575             }
576             break;
577         case VLC_VAR_SETTEXT:
578             free( p_var->psz_text );
579             if( p_val && p_val->psz_string )
580                 p_var->psz_text = strdup( p_val->psz_string );
581             else
582                 p_var->psz_text = NULL;
583             break;
584         case VLC_VAR_GETTEXT:
585             p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
586                                                 : NULL;
587             break;
588         case VLC_VAR_SETISCOMMAND:
589             p_var->i_type |= VLC_VAR_ISCOMMAND;
590             break;
591
592         default:
593             break;
594     }
595
596     vlc_mutex_unlock( &p_priv->var_lock );
597
598     return VLC_SUCCESS;
599 }
600
601 #undef var_GetAndSet
602 /**
603  * Perform a Get and Set on a variable
604  *
605  * \param p_this: The object that hold the variable
606  * \param psz_name: the name of the variable
607  * \param i_action: the action to perform
608  * \param p_val: The action parameter
609  * \return vlc error codes
610  */
611 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
612                    vlc_value_t *p_val )
613 {
614     int i_ret;
615     variable_t *p_var;
616     vlc_value_t oldval;
617
618     assert( p_this );
619     assert( p_val );
620
621     vlc_object_internals_t *p_priv = vlc_internals( p_this );
622
623     vlc_mutex_lock( &p_priv->var_lock );
624     p_var = Lookup( p_this, psz_name );
625     if( p_var == NULL )
626     {
627         vlc_mutex_unlock( &p_priv->var_lock );
628         return VLC_ENOVAR;
629     }
630
631     WaitUnused( p_this, p_var );
632
633     /* Duplicated data if needed */
634     //p_var->ops->pf_dup( &val );
635
636     /* Backup needed stuff */
637     oldval = p_var->val;
638
639     /* depending of the action requiered */
640     switch( i_action )
641     {
642     case VLC_VAR_BOOL_TOGGLE:
643         assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
644         p_var->val.b_bool = !p_var->val.b_bool;
645         break;
646     case VLC_VAR_INTEGER_ADD:
647         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
648         p_var->val.i_int += p_val->i_int;
649         break;
650     case VLC_VAR_INTEGER_OR:
651         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
652         p_var->val.i_int |= p_val->i_int;
653         break;
654     case VLC_VAR_INTEGER_NAND:
655         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
656         p_var->val.i_int &= ~p_val->i_int;
657         break;
658     default:
659         vlc_mutex_unlock( &p_priv->var_lock );
660         return VLC_EGENERIC;
661     }
662
663     /*  Check boundaries */
664     CheckValue( p_var, &p_var->val );
665     *p_val = p_var->val;
666
667     /* Deal with callbacks.*/
668     i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
669
670     vlc_mutex_unlock( &p_priv->var_lock );
671
672     return i_ret;
673 }
674
675 #undef var_Type
676 /**
677  * Request a variable's type
678  *
679  * \return The variable type if it exists, or 0 if the
680  * variable could not be found.
681  * \see \ref var_type
682  */
683 int var_Type( vlc_object_t *p_this, const char *psz_name )
684 {
685     variable_t *p_var;
686     int i_type = 0;
687
688     assert( p_this );
689
690     vlc_object_internals_t *p_priv = vlc_internals( p_this );
691
692     vlc_mutex_lock( &p_priv->var_lock );
693
694     p_var = Lookup( p_this, psz_name );
695     if( p_var != NULL )
696         i_type = p_var->i_type;
697
698     vlc_mutex_unlock( &p_priv->var_lock );
699
700     return i_type;
701 }
702
703 #undef var_SetChecked
704 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
705                     int expected_type, vlc_value_t val )
706 {
707     int i_ret = VLC_SUCCESS;
708     variable_t *p_var;
709     vlc_value_t oldval;
710
711     assert( p_this );
712
713     vlc_object_internals_t *p_priv = vlc_internals( p_this );
714
715     vlc_mutex_lock( &p_priv->var_lock );
716
717     p_var = Lookup( p_this, psz_name );
718     if( p_var == NULL )
719     {
720         vlc_mutex_unlock( &p_priv->var_lock );
721         return VLC_ENOVAR;
722     }
723
724     assert( expected_type == 0 ||
725             (p_var->i_type & VLC_VAR_CLASS) == expected_type );
726 #ifndef NDEBUG
727         /* Alert if the type is VLC_VAR_VOID */
728         if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
729             msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
730 #endif
731
732
733     WaitUnused( p_this, p_var );
734
735     /* Duplicate data if needed */
736     p_var->ops->pf_dup( &val );
737
738     /* Backup needed stuff */
739     oldval = p_var->val;
740
741     /* Check boundaries and list */
742     CheckValue( p_var, &val );
743
744     /* Set the variable */
745     p_var->val = val;
746
747     /* Deal with callbacks */
748     i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
749
750     /* Free data if needed */
751     p_var->ops->pf_free( &oldval );
752
753     vlc_mutex_unlock( &p_priv->var_lock );
754
755     return i_ret;
756 }
757
758 #undef var_Set
759 /**
760  * Set a variable's value
761  *
762  * \param p_this The object that hold the variable
763  * \param psz_name The name of the variable
764  * \param val the value to set
765  */
766 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
767 {
768     return var_SetChecked( p_this, psz_name, 0, val );
769 }
770
771 #undef var_GetChecked
772 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
773                     int expected_type, vlc_value_t *p_val )
774 {
775     assert( p_this );
776
777     vlc_object_internals_t *p_priv = vlc_internals( p_this );
778     variable_t *p_var;
779     int err = VLC_SUCCESS;
780
781     vlc_mutex_lock( &p_priv->var_lock );
782
783     p_var = Lookup( p_this, psz_name );
784     if( p_var != NULL )
785     {
786         assert( expected_type == 0 ||
787                 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
788
789         /* Really get the variable */
790         *p_val = p_var->val;
791
792 #ifndef NDEBUG
793         /* Alert if the type is VLC_VAR_VOID */
794         if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
795             msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
796 #endif
797
798         /* Duplicate value if needed */
799         p_var->ops->pf_dup( p_val );
800     }
801     else
802         err = VLC_ENOVAR;
803
804     vlc_mutex_unlock( &p_priv->var_lock );
805     return err;
806 }
807
808 #undef var_Get
809 /**
810  * Get a variable's value
811  *
812  * \param p_this The object that holds the variable
813  * \param psz_name The name of the variable
814  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
815  *              after the function is finished
816  */
817 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
818 {
819     return var_GetChecked( p_this, psz_name, 0, p_val );
820 }
821
822 #undef var_AddCallback
823 /**
824  * Register a callback in a variable
825  *
826  * We store a function pointer that will be called upon variable
827  * modification.
828  *
829  * \param p_this The object that holds the variable
830  * \param psz_name The name of the variable
831  * \param pf_callback The function pointer
832  * \param p_data A generic pointer that will be passed as the last
833  *               argument to the callback function.
834  *
835  * \warning The callback function is run in the thread that calls var_Set on
836  *          the variable. Use proper locking. This thread may not have much
837  *          time to spare, so keep callback functions short.
838  */
839 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
840                      vlc_callback_t pf_callback, void *p_data )
841 {
842     variable_t *p_var;
843     callback_entry_t entry;
844
845     assert( p_this );
846
847     vlc_object_internals_t *p_priv = vlc_internals( p_this );
848
849     entry.pf_callback = pf_callback;
850     entry.p_data = p_data;
851
852     vlc_mutex_lock( &p_priv->var_lock );
853
854     p_var = Lookup( p_this, psz_name );
855     if( p_var == NULL )
856     {
857 #ifndef NDEBUG
858         msg_Warn( p_this, "Failed to add a callback to the non-existing "
859                           "variable '%s'", psz_name );
860 #endif
861         vlc_mutex_unlock( &p_priv->var_lock );
862         return VLC_ENOVAR;
863     }
864
865     WaitUnused( p_this, p_var );
866     INSERT_ELEM( p_var->p_entries,
867                  p_var->i_entries,
868                  p_var->i_entries,
869                  entry );
870
871     vlc_mutex_unlock( &p_priv->var_lock );
872
873     return VLC_SUCCESS;
874 }
875
876 #undef var_DelCallback
877 /**
878  * Remove a callback from a variable
879  *
880  * pf_callback and p_data have to be given again, because different objects
881  * might have registered the same callback function.
882  */
883 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
884                      vlc_callback_t pf_callback, void *p_data )
885 {
886     int i_entry;
887     variable_t *p_var;
888 #ifndef NDEBUG
889     bool b_found_similar = false;
890 #endif
891
892     assert( p_this );
893
894     vlc_object_internals_t *p_priv = vlc_internals( p_this );
895
896     vlc_mutex_lock( &p_priv->var_lock );
897
898     p_var = Lookup( p_this, psz_name );
899     if( p_var == NULL )
900     {
901         vlc_mutex_unlock( &p_priv->var_lock );
902         return VLC_ENOVAR;
903     }
904
905     WaitUnused( p_this, p_var );
906
907     for( i_entry = p_var->i_entries ; i_entry-- ; )
908     {
909         if( p_var->p_entries[i_entry].pf_callback == pf_callback
910             && p_var->p_entries[i_entry].p_data == p_data )
911         {
912             break;
913         }
914 #ifndef NDEBUG
915         else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
916             b_found_similar = true;
917 #endif
918     }
919
920     if( i_entry < 0 )
921     {
922 #ifndef NDEBUG
923         if( b_found_similar )
924             fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
925                              "function but not the same data.", psz_name );
926         assert( 0 );
927 #endif
928         vlc_mutex_unlock( &p_priv->var_lock );
929         return VLC_EGENERIC;
930     }
931
932     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
933
934     vlc_mutex_unlock( &p_priv->var_lock );
935
936     return VLC_SUCCESS;
937 }
938
939 #undef var_TriggerCallback
940 /**
941  * Trigger callback on a variable
942  *
943  * \param p_this The object that hold the variable
944  * \param psz_name The name of the variable
945  */
946 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
947 {
948     int i_ret;
949     variable_t *p_var;
950
951     assert( p_this );
952
953     vlc_object_internals_t *p_priv = vlc_internals( p_this );
954
955     vlc_mutex_lock( &p_priv->var_lock );
956
957     p_var = Lookup( p_this, psz_name );
958     if( p_var == NULL )
959     {
960         vlc_mutex_unlock( &p_priv->var_lock );
961         return VLC_ENOVAR;
962     }
963
964     WaitUnused( p_this, p_var );
965
966     /* Deal with callbacks. Tell we're in a callback, release the lock,
967      * call stored functions, retake the lock. */
968     i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
969
970     vlc_mutex_unlock( &p_priv->var_lock );
971     return i_ret;
972 }
973
974 /** Parse a stringified option
975  * This function parse a string option and create the associated object
976  * variable
977  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
978  * option name and bar is the value of the option.
979  * \param p_obj the object in which the variable must be created
980  * \param psz_option the option to parse
981  * \param trusted whether the option is set by a trusted input or not
982  * \return nothing
983  */
984 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
985                       bool trusted )
986 {
987     char *psz_name, *psz_value;
988     int  i_type;
989     bool b_isno = false;
990     vlc_value_t val;
991
992     val.psz_string = NULL;
993
994     /* It's too much of a hassle to remove the ':' when we parse
995      * the cmd line :) */
996     if( psz_option[0] == ':' )
997         psz_option++;
998
999     if( !psz_option[0] )
1000         return;
1001
1002     psz_name = strdup( psz_option );
1003     if( psz_name == NULL )
1004         return;
1005
1006     psz_value = strchr( psz_name, '=' );
1007     if( psz_value != NULL )
1008         *psz_value++ = '\0';
1009
1010     i_type = config_GetType( p_obj, psz_name );
1011     if( !i_type && !psz_value )
1012     {
1013         /* check for "no-foo" or "nofoo" */
1014         if( !strncmp( psz_name, "no-", 3 ) )
1015         {
1016             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1017         }
1018         else if( !strncmp( psz_name, "no", 2 ) )
1019         {
1020             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1021         }
1022         else goto cleanup;           /* Option doesn't exist */
1023
1024         b_isno = true;
1025         i_type = config_GetType( p_obj, psz_name );
1026     }
1027     if( !i_type ) goto cleanup; /* Option doesn't exist */
1028
1029     if( ( i_type != VLC_VAR_BOOL ) &&
1030         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1031
1032     /* check if option is unsafe */
1033     if( !trusted )
1034     {
1035         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1036         if( !p_config || !p_config->b_safe )
1037         {
1038             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1039                             "security reasons", psz_name );
1040             free( psz_name );
1041             return;
1042         }
1043     }
1044
1045     /* Create the variable in the input object.
1046      * Children of the input object will be able to retreive this value
1047      * thanks to the inheritance property of the object variables. */
1048     var_Create( p_obj, psz_name, i_type );
1049
1050     switch( i_type )
1051     {
1052     case VLC_VAR_BOOL:
1053         val.b_bool = !b_isno;
1054         break;
1055
1056     case VLC_VAR_INTEGER:
1057         val.i_int = strtoll( psz_value, NULL, 0 );
1058         break;
1059
1060     case VLC_VAR_FLOAT:
1061         val.f_float = us_atof( psz_value );
1062         break;
1063
1064     case VLC_VAR_STRING:
1065     case VLC_VAR_MODULE:
1066     case VLC_VAR_FILE:
1067     case VLC_VAR_DIRECTORY:
1068         val.psz_string = psz_value;
1069         break;
1070
1071     default:
1072         goto cleanup;
1073     }
1074
1075     var_Set( p_obj, psz_name, val );
1076
1077 cleanup:
1078     free( psz_name );
1079 }
1080
1081
1082 /* Following functions are local */
1083
1084 /**
1085  * Waits until the variable is inactive (i.e. not executing a callback)
1086  */
1087 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1088 {
1089     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1090
1091     mutex_cleanup_push( &p_priv->var_lock );
1092     while( p_var->b_incallback )
1093         vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1094     vlc_cleanup_pop( );
1095 }
1096
1097 /*****************************************************************************
1098  * CheckValue: check that a value is valid wrt. a variable
1099  *****************************************************************************
1100  * This function checks p_val's value against p_var's limitations such as
1101  * minimal and maximal value, step, in-list position, and modifies p_val if
1102  * necessary.
1103  ****************************************************************************/
1104 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1105 {
1106     /* Check that our variable is in the list */
1107     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1108     {
1109         int i;
1110
1111         /* This list is not sorted so go throug it (this is a small list) */
1112         for( i = p_var->choices.i_count ; i-- ; )
1113         {
1114             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1115             {
1116                 break;
1117             }
1118         }
1119
1120         /* If not found, change it to anything vaguely valid */
1121         if( i < 0 )
1122         {
1123             /* Free the old variable, get the new one, dup it */
1124             p_var->ops->pf_free( p_val );
1125             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1126                                           ? p_var->i_default : 0 ];
1127             p_var->ops->pf_dup( p_val );
1128         }
1129     }
1130
1131     /* Check that our variable is within the bounds */
1132     switch( p_var->i_type & VLC_VAR_TYPE )
1133     {
1134         case VLC_VAR_INTEGER:
1135             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1136                  && (p_val->i_int % p_var->step.i_int) )
1137             {
1138                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1139                                / p_var->step.i_int * p_var->step.i_int;
1140             }
1141             if( p_var->i_type & VLC_VAR_HASMIN
1142                  && p_val->i_int < p_var->min.i_int )
1143             {
1144                 p_val->i_int = p_var->min.i_int;
1145             }
1146             if( p_var->i_type & VLC_VAR_HASMAX
1147                  && p_val->i_int > p_var->max.i_int )
1148             {
1149                 p_val->i_int = p_var->max.i_int;
1150             }
1151             break;
1152         case VLC_VAR_FLOAT:
1153             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1154             {
1155                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1156                                         p_val->f_float / p_var->step.f_float );
1157                 if( p_val->f_float != f_round )
1158                 {
1159                     p_val->f_float = f_round;
1160                 }
1161             }
1162             if( p_var->i_type & VLC_VAR_HASMIN
1163                  && p_val->f_float < p_var->min.f_float )
1164             {
1165                 p_val->f_float = p_var->min.f_float;
1166             }
1167             if( p_var->i_type & VLC_VAR_HASMAX
1168                  && p_val->f_float > p_var->max.f_float )
1169             {
1170                 p_val->f_float = p_var->max.f_float;
1171             }
1172             break;
1173         case VLC_VAR_TIME:
1174             /* FIXME: TODO */
1175             break;
1176     }
1177 }
1178
1179 /**
1180  * Finds the value of a variable. If the specified object does not hold a
1181  * variable with the specified name, try the parent object, and iterate until
1182  * the top of the tree. If no match is found, the value is read from the
1183  * configuration.
1184  */
1185 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1186                  vlc_value_t *p_val )
1187 {
1188 #ifndef NDEBUG
1189     if (p_this != VLC_OBJECT(p_this->p_libvlc)
1190      && unlikely(p_this->p_parent == NULL))
1191     {
1192         msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1193         //vlc_backtrace ();
1194     }
1195 #endif
1196     i_type &= VLC_VAR_CLASS;
1197     for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1198     {
1199         if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1200             return VLC_SUCCESS;
1201 #ifndef NDEBUG
1202         if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1203          && unlikely(obj->p_parent == NULL))
1204         {
1205             msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1206                       psz_name, obj, obj->psz_object_type);
1207             //vlc_backtrace ();
1208         }
1209 #endif
1210     }
1211
1212     /* else take value from config */
1213     switch( i_type & VLC_VAR_CLASS )
1214     {
1215         case VLC_VAR_STRING:
1216             p_val->psz_string = config_GetPsz( p_this, psz_name );
1217             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1218             break;
1219         case VLC_VAR_FLOAT:
1220             p_val->f_float = config_GetFloat( p_this, psz_name );
1221             break;
1222         case VLC_VAR_INTEGER:
1223             p_val->i_int = config_GetInt( p_this, psz_name );
1224             break;
1225         case VLC_VAR_BOOL:
1226             p_val->b_bool = config_GetInt( p_this, psz_name );
1227             break;
1228         case VLC_VAR_ADDRESS:
1229             return VLC_ENOOBJ;
1230         default:
1231             msg_Warn( p_this, "Could not inherit value for var %s "
1232                               "from config. Invalid Type", psz_name );
1233             return VLC_ENOOBJ;
1234     }
1235     /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1236     return VLC_SUCCESS;
1237 }
1238
1239
1240 /**
1241  * It inherits a string as an unsigned rational number (it also accepts basic
1242  * float number).
1243  *
1244  * It returns an error if the rational number cannot be parsed (0/0 is valid).
1245  * The rational is already reduced.
1246  */
1247 int (var_InheritURational)(vlc_object_t *object,
1248                            unsigned *num, unsigned *den,
1249                            const char *var)
1250 {
1251     /* */
1252     *num = 0;
1253     *den = 0;
1254
1255     /* */
1256     char *tmp = var_InheritString(object, var);
1257     if (!tmp)
1258         goto error;
1259
1260     char *next;
1261     unsigned n = strtol(tmp,  &next, 0);
1262     unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1263
1264     if (*next == '.') {
1265         /* Interpret as a float number */
1266         double r = us_atof(tmp);
1267         double c = ceil(r);
1268         if (c >= UINT_MAX)
1269             goto error;
1270         unsigned m = c;
1271         if (m > 0) {
1272             d = UINT_MAX / m;
1273             n = r * d;
1274         } else {
1275             n = 0;
1276             d = 0;
1277         }
1278     }
1279
1280     if (n > 0 && d > 0)
1281         vlc_ureduce(num, den, n, d, 0);
1282
1283     free(tmp);
1284     return VLC_SUCCESS;
1285
1286 error:
1287     free(tmp);
1288     return VLC_EGENERIC;
1289 }
1290
1291 /**********************************************************************
1292  * Trigger the callbacks.
1293  * Tell we're in a callback, release the lock, call stored functions,
1294  * retake the lock.
1295  **********************************************************************/
1296 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1297                             const char *psz_name, vlc_value_t oldval )
1298 {
1299     assert( p_this );
1300
1301     int i_entries = p_var->i_entries;
1302     if( i_entries == 0 )
1303         return VLC_SUCCESS;
1304
1305     callback_entry_t *p_entries = p_var->p_entries;
1306     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1307
1308     assert( !p_var->b_incallback );
1309     p_var->b_incallback = true;
1310     vlc_mutex_unlock( &p_priv->var_lock );
1311
1312     /* The real calls */
1313     for( ; i_entries-- ; )
1314     {
1315         p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1316                                           p_entries[i_entries].p_data );
1317     }
1318
1319     vlc_mutex_lock( &p_priv->var_lock );
1320     p_var->b_incallback = false;
1321     vlc_cond_broadcast( &p_priv->var_wait );
1322
1323     return VLC_SUCCESS;
1324 }
1325
1326 #undef var_Command
1327 /**********************************************************************
1328  * Execute a var command on an object identified by its name
1329  **********************************************************************/
1330 int var_Command( vlc_object_t *p_this, const char *psz_name,
1331                  const char *psz_cmd, const char *psz_arg, char **psz_msg )
1332 {
1333     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1334                                                 psz_name, FIND_CHILD );
1335     int i_type, i_ret;
1336
1337     if( !p_obj )
1338     {
1339         if( psz_msg )
1340             *psz_msg = strdup( "Unknown destination object." );
1341         return VLC_ENOOBJ;
1342     }
1343
1344     i_type = var_Type( p_obj, psz_cmd );
1345     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1346     {
1347         vlc_object_release( p_obj );
1348         if( psz_msg )
1349             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1350         return VLC_EGENERIC;
1351     }
1352
1353     i_type &= VLC_VAR_CLASS;
1354     switch( i_type )
1355     {
1356         case VLC_VAR_INTEGER:
1357             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1358             break;
1359         case VLC_VAR_FLOAT:
1360             i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1361             break;
1362         case VLC_VAR_STRING:
1363             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1364             break;
1365         case VLC_VAR_BOOL:
1366             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1367             break;
1368         default:
1369             i_ret = VLC_EGENERIC;
1370             break;
1371     }
1372
1373     vlc_object_release( p_obj );
1374
1375     if( psz_msg )
1376     {
1377         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1378                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1379             *psz_msg = NULL;
1380     }
1381
1382     return i_ret;
1383 }
1384
1385
1386 /**
1387  * Free a list and the associated strings
1388  * @param p_val: the list variable
1389  * @param p_val2: the variable associated or NULL
1390  */
1391 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1392 {
1393     FreeList( p_val );
1394     if( p_val2 && p_val2->p_list )
1395     {
1396         for( int i = 0; i < p_val2->p_list->i_count; i++ )
1397             free( p_val2->p_list->p_values[i].psz_string );
1398         if( p_val2->p_list->i_count )
1399         {
1400             free( p_val2->p_list->p_values );
1401             free( p_val2->p_list->pi_types );
1402         }
1403         free( p_val2->p_list );
1404     }
1405 }