1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include "variables.h"
36 #include "vlc_interface.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 struct callback_entry_t
43 vlc_callback_t pf_callback
;
47 /*****************************************************************************
48 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49 *****************************************************************************/
50 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; }
51 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; }
52 static int CmpTime( vlc_value_t v
, vlc_value_t w
)
54 return v
.i_time
== w
.i_time
? 0 : v
.i_time
> w
.i_time
? 1 : -1;
56 static int CmpString( vlc_value_t v
, vlc_value_t w
) { return strcmp( v
.psz_string
, w
.psz_string
); }
57 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; }
58 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; }
60 /*****************************************************************************
61 * Local duplication functions, and local deallocation functions
62 *****************************************************************************/
63 static void DupDummy( vlc_value_t
*p_val
) { (void)p_val
; /* unused */ }
64 static void DupString( vlc_value_t
*p_val
) { p_val
->psz_string
= strdup( p_val
->psz_string
); }
66 static void DupList( vlc_value_t
*p_val
)
69 vlc_list_t
*p_list
= malloc( sizeof(vlc_list_t
) );
71 p_list
->i_count
= p_val
->p_list
->i_count
;
72 if( p_val
->p_list
->i_count
)
74 p_list
->p_values
= malloc( p_list
->i_count
* sizeof(vlc_value_t
) );
75 p_list
->pi_types
= malloc( p_list
->i_count
* sizeof(int) );
79 p_list
->p_values
= NULL
;
80 p_list
->pi_types
= NULL
;
83 for( i
= 0; i
< p_list
->i_count
; i
++ )
85 p_list
->p_values
[i
] = p_val
->p_list
->p_values
[i
];
86 p_list
->pi_types
[i
] = p_val
->p_list
->pi_types
[i
];
87 switch( p_val
->p_list
->pi_types
[i
] & VLC_VAR_TYPE
)
91 DupString( &p_list
->p_values
[i
] );
98 p_val
->p_list
= p_list
;
101 static void FreeDummy( vlc_value_t
*p_val
) { (void)p_val
; /* unused */ }
102 static void FreeString( vlc_value_t
*p_val
) { free( p_val
->psz_string
); }
103 static void FreeMutex( vlc_value_t
*p_val
) { vlc_mutex_destroy( (vlc_mutex_t
*)p_val
->p_address
); free( p_val
->p_address
); }
105 static void FreeList( vlc_value_t
*p_val
)
108 for( i
= 0; i
< p_val
->p_list
->i_count
; i
++ )
110 switch( p_val
->p_list
->pi_types
[i
] & VLC_VAR_TYPE
)
113 FreeString( &p_val
->p_list
->p_values
[i
] );
116 FreeMutex( &p_val
->p_list
->p_values
[i
] );
123 if( p_val
->p_list
->i_count
)
125 free( p_val
->p_list
->p_values
);
126 free( p_val
->p_list
->pi_types
);
128 free( p_val
->p_list
);
131 /*****************************************************************************
133 *****************************************************************************/
134 static int GetUnused ( vlc_object_t
*, const char * );
135 static uint32_t HashString ( const char * );
136 static int Insert ( variable_t
*, int, const char * );
137 static int InsertInner ( variable_t
*, int, uint32_t );
138 static int Lookup ( variable_t
*, int, const char * );
139 static int LookupInner ( variable_t
*, int, uint32_t );
141 static void CheckValue ( variable_t
*, vlc_value_t
* );
143 static int InheritValue( vlc_object_t
*, const char *, vlc_value_t
*,
147 * Initialize a vlc variable
149 * We hash the given string and insert it into the sorted list. The insertion
150 * may require slow memory copies, but think about what we gain in the log(n)
151 * lookup phase when setting/getting the variable value!
153 * \param p_this The object in which to create the variable
154 * \param psz_name The name of the variable
155 * \param i_type The variables type. Must be one of \ref var_type combined with
156 * zero or more \ref var_flags
158 int __var_Create( vlc_object_t
*p_this
, const char *psz_name
, int i_type
)
162 static vlc_list_t dummy_null_list
= {0, NULL
, NULL
};
163 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
165 vlc_mutex_lock( &p_priv
->var_lock
);
167 /* FIXME: if the variable already exists, we don't duplicate it. But we
168 * duplicate the lookups. It's not that serious, but if anyone finds some
169 * time to rework Insert() so that only one lookup has to be done, feel
171 i_new
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
175 /* If the types differ, variable creation failed. */
176 if( (i_type
& ~(VLC_VAR_DOINHERIT
|VLC_VAR_ISCOMMAND
)) != p_priv
->p_vars
[i_new
].i_type
)
178 vlc_mutex_unlock( &p_priv
->var_lock
);
182 p_priv
->p_vars
[i_new
].i_usage
++;
183 if( i_type
& VLC_VAR_ISCOMMAND
)
184 p_priv
->p_vars
[i_new
].i_type
|= VLC_VAR_ISCOMMAND
;
185 vlc_mutex_unlock( &p_priv
->var_lock
);
189 i_new
= Insert( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
191 if( (p_priv
->i_vars
& 15) == 15 )
193 p_priv
->p_vars
= realloc( p_priv
->p_vars
,
194 (p_priv
->i_vars
+17) * sizeof(variable_t
) );
197 memmove( p_priv
->p_vars
+ i_new
+ 1,
198 p_priv
->p_vars
+ i_new
,
199 (p_priv
->i_vars
- i_new
) * sizeof(variable_t
) );
203 p_var
= &p_priv
->p_vars
[i_new
];
204 memset( p_var
, 0, sizeof(*p_var
) );
206 p_var
->i_hash
= HashString( psz_name
);
207 p_var
->psz_name
= strdup( psz_name
);
208 p_var
->psz_text
= NULL
;
210 p_var
->i_type
= i_type
& ~VLC_VAR_DOINHERIT
;
211 memset( &p_var
->val
, 0, sizeof(vlc_value_t
) );
213 p_var
->pf_dup
= DupDummy
;
214 p_var
->pf_free
= FreeDummy
;
218 p_var
->i_default
= -1;
219 p_var
->choices
.i_count
= 0;
220 p_var
->choices
.p_values
= NULL
;
221 p_var
->choices_text
.i_count
= 0;
222 p_var
->choices_text
.p_values
= NULL
;
224 p_var
->b_incallback
= VLC_FALSE
;
225 p_var
->i_entries
= 0;
226 p_var
->p_entries
= NULL
;
228 /* Always initialize the variable, even if it is a list variable; this
229 * will lead to errors if the variable is not initialized, but it will
230 * not cause crashes in the variable handling. */
231 switch( i_type
& VLC_VAR_TYPE
)
234 p_var
->pf_cmp
= CmpBool
;
235 p_var
->val
.b_bool
= VLC_FALSE
;
237 case VLC_VAR_INTEGER
:
239 p_var
->pf_cmp
= CmpInt
;
240 p_var
->val
.i_int
= 0;
245 case VLC_VAR_DIRECTORY
:
246 case VLC_VAR_VARIABLE
:
247 p_var
->pf_cmp
= CmpString
;
248 p_var
->pf_dup
= DupString
;
249 p_var
->pf_free
= FreeString
;
250 p_var
->val
.psz_string
= strdup( "" );
253 p_var
->pf_cmp
= CmpFloat
;
254 p_var
->val
.f_float
= 0.0;
257 p_var
->pf_cmp
= CmpTime
;
258 p_var
->val
.i_time
= 0;
260 case VLC_VAR_ADDRESS
:
261 p_var
->pf_cmp
= CmpAddress
;
262 p_var
->val
.p_address
= NULL
;
265 p_var
->pf_cmp
= CmpAddress
;
266 p_var
->pf_free
= FreeMutex
;
267 p_var
->val
.p_address
= malloc( sizeof(vlc_mutex_t
) );
268 vlc_mutex_init( p_this
, (vlc_mutex_t
*)p_var
->val
.p_address
);
271 p_var
->pf_cmp
= CmpAddress
;
272 p_var
->pf_dup
= DupList
;
273 p_var
->pf_free
= FreeList
;
274 p_var
->val
.p_list
= &dummy_null_list
;
278 /* Duplicate the default data we stored. */
279 p_var
->pf_dup( &p_var
->val
);
281 if( i_type
& VLC_VAR_DOINHERIT
)
285 if( InheritValue( p_this
, psz_name
, &val
, p_var
->i_type
)
288 /* Free data if needed */
289 p_var
->pf_free( &p_var
->val
);
290 /* Set the variable */
293 if( i_type
& VLC_VAR_HASCHOICE
)
295 /* We must add the inherited value to our choice list */
296 p_var
->i_default
= 0;
298 INSERT_ELEM( p_var
->choices
.p_values
, p_var
->choices
.i_count
,
300 INSERT_ELEM( p_var
->choices_text
.p_values
,
301 p_var
->choices_text
.i_count
, 0, val
);
302 p_var
->pf_dup( &p_var
->choices
.p_values
[0] );
303 p_var
->choices_text
.p_values
[0].psz_string
= NULL
;
308 vlc_mutex_unlock( &p_priv
->var_lock
);
314 * Destroy a vlc variable
316 * Look for the variable and destroy it if it is found. As in var_Create we
317 * do a call to memmove() but we have performance counterparts elsewhere.
319 * \param p_this The object that holds the variable
320 * \param psz_name The name of the variable
322 int __var_Destroy( vlc_object_t
*p_this
, const char *psz_name
)
326 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
328 vlc_mutex_lock( &p_priv
->var_lock
);
330 i_var
= GetUnused( p_this
, psz_name
);
333 vlc_mutex_unlock( &p_priv
->var_lock
);
337 p_var
= &p_priv
->p_vars
[i_var
];
339 if( p_var
->i_usage
> 1 )
342 vlc_mutex_unlock( &p_priv
->var_lock
);
346 /* Free value if needed */
347 p_var
->pf_free( &p_var
->val
);
349 /* Free choice list if needed */
350 if( p_var
->choices
.i_count
)
352 for( i
= 0 ; i
< p_var
->choices
.i_count
; i
++ )
354 p_var
->pf_free( &p_var
->choices
.p_values
[i
] );
355 free( p_var
->choices_text
.p_values
[i
].psz_string
);
357 free( p_var
->choices
.p_values
);
358 free( p_var
->choices_text
.p_values
);
361 /* Free callbacks if needed */
362 if( p_var
->p_entries
)
364 free( p_var
->p_entries
);
367 free( p_var
->psz_name
);
368 free( p_var
->psz_text
);
370 memmove( p_priv
->p_vars
+ i_var
,
371 p_priv
->p_vars
+ i_var
+ 1,
372 (p_priv
->i_vars
- i_var
- 1) * sizeof(variable_t
) );
374 if( (p_priv
->i_vars
& 15) == 0 )
376 p_priv
->p_vars
= realloc( p_priv
->p_vars
,
377 (p_priv
->i_vars
) * sizeof( variable_t
) );
382 vlc_mutex_unlock( &p_priv
->var_lock
);
388 * Perform an action on a variable
390 * \param p_this The object that holds the variable
391 * \param psz_name The name of the variable
392 * \param i_action The action to perform. Must be one of \ref var_action
393 * \param p_val First action parameter
394 * \param p_val2 Second action parameter
396 int __var_Change( vlc_object_t
*p_this
, const char *psz_name
,
397 int i_action
, vlc_value_t
*p_val
, vlc_value_t
*p_val2
)
402 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
404 vlc_mutex_lock( &p_priv
->var_lock
);
406 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
410 vlc_mutex_unlock( &p_priv
->var_lock
);
414 p_var
= &p_priv
->p_vars
[i_var
];
419 if( p_var
->i_type
& VLC_VAR_HASMIN
)
421 p_var
->pf_free( &p_var
->min
);
423 p_var
->i_type
|= VLC_VAR_HASMIN
;
425 p_var
->pf_dup( &p_var
->min
);
426 CheckValue( p_var
, &p_var
->val
);
429 if( p_var
->i_type
& VLC_VAR_HASMIN
)
435 if( p_var
->i_type
& VLC_VAR_HASMAX
)
437 p_var
->pf_free( &p_var
->max
);
439 p_var
->i_type
|= VLC_VAR_HASMAX
;
441 p_var
->pf_dup( &p_var
->max
);
442 CheckValue( p_var
, &p_var
->val
);
445 if( p_var
->i_type
& VLC_VAR_HASMAX
)
450 case VLC_VAR_SETSTEP
:
451 if( p_var
->i_type
& VLC_VAR_HASSTEP
)
453 p_var
->pf_free( &p_var
->step
);
455 p_var
->i_type
|= VLC_VAR_HASSTEP
;
456 p_var
->step
= *p_val
;
457 p_var
->pf_dup( &p_var
->step
);
458 CheckValue( p_var
, &p_var
->val
);
460 case VLC_VAR_GETSTEP
:
461 if( p_var
->i_type
& VLC_VAR_HASSTEP
)
463 *p_val
= p_var
->step
;
466 case VLC_VAR_ADDCHOICE
:
467 /* FIXME: the list is sorted, dude. Use something cleverer. */
468 for( i
= p_var
->choices
.i_count
; i
-- ; )
470 if( p_var
->pf_cmp( p_var
->choices
.p_values
[i
], *p_val
) < 0 )
476 /* The new place is i+1 */
479 if( p_var
->i_default
>= i
)
484 INSERT_ELEM( p_var
->choices
.p_values
, p_var
->choices
.i_count
,
486 INSERT_ELEM( p_var
->choices_text
.p_values
,
487 p_var
->choices_text
.i_count
, i
, *p_val
);
488 p_var
->pf_dup( &p_var
->choices
.p_values
[i
] );
489 p_var
->choices_text
.p_values
[i
].psz_string
=
490 ( p_val2
&& p_val2
->psz_string
) ?
491 strdup( p_val2
->psz_string
) : NULL
;
493 CheckValue( p_var
, &p_var
->val
);
495 case VLC_VAR_DELCHOICE
:
496 /* FIXME: the list is sorted, dude. Use something cleverer. */
497 for( i
= 0 ; i
< p_var
->choices
.i_count
; i
++ )
499 if( p_var
->pf_cmp( p_var
->choices
.p_values
[i
], *p_val
) == 0 )
505 if( i
== p_var
->choices
.i_count
)
508 vlc_mutex_unlock( &p_priv
->var_lock
);
512 if( p_var
->i_default
> i
)
516 else if( p_var
->i_default
== i
)
518 p_var
->i_default
= -1;
521 p_var
->pf_free( &p_var
->choices
.p_values
[i
] );
522 free( p_var
->choices_text
.p_values
[i
].psz_string
);
523 REMOVE_ELEM( p_var
->choices
.p_values
, p_var
->choices
.i_count
, i
);
524 REMOVE_ELEM( p_var
->choices_text
.p_values
,
525 p_var
->choices_text
.i_count
, i
);
527 CheckValue( p_var
, &p_var
->val
);
529 case VLC_VAR_CHOICESCOUNT
:
530 p_val
->i_int
= p_var
->choices
.i_count
;
532 case VLC_VAR_CLEARCHOICES
:
533 for( i
= 0 ; i
< p_var
->choices
.i_count
; i
++ )
535 p_var
->pf_free( &p_var
->choices
.p_values
[i
] );
537 for( i
= 0 ; i
< p_var
->choices_text
.i_count
; i
++ )
538 free( p_var
->choices_text
.p_values
[i
].psz_string
);
540 if( p_var
->choices
.i_count
) free( p_var
->choices
.p_values
);
541 if( p_var
->choices_text
.i_count
) free( p_var
->choices_text
.p_values
);
543 p_var
->choices
.i_count
= 0;
544 p_var
->choices
.p_values
= NULL
;
545 p_var
->choices_text
.i_count
= 0;
546 p_var
->choices_text
.p_values
= NULL
;
547 p_var
->i_default
= -1;
549 case VLC_VAR_SETDEFAULT
:
550 /* FIXME: the list is sorted, dude. Use something cleverer. */
551 for( i
= 0 ; i
< p_var
->choices
.i_count
; i
++ )
553 if( p_var
->pf_cmp( p_var
->choices
.p_values
[i
], *p_val
) == 0 )
559 if( i
== p_var
->choices
.i_count
)
565 p_var
->i_default
= i
;
566 CheckValue( p_var
, &p_var
->val
);
568 case VLC_VAR_SETVALUE
:
569 /* Duplicate data if needed */
570 p_var
->pf_dup( p_val
);
571 /* Backup needed stuff */
573 /* Check boundaries and list */
574 CheckValue( p_var
, p_val
);
575 /* Set the variable */
577 /* Free data if needed */
578 p_var
->pf_free( &oldval
);
580 case VLC_VAR_GETCHOICES
:
581 case VLC_VAR_GETLIST
:
582 p_val
->p_list
= malloc( sizeof(vlc_list_t
) );
583 if( p_val2
) p_val2
->p_list
= malloc( sizeof(vlc_list_t
) );
584 if( p_var
->choices
.i_count
)
586 p_val
->p_list
->p_values
= malloc( p_var
->choices
.i_count
587 * sizeof(vlc_value_t
) );
588 p_val
->p_list
->pi_types
= malloc( p_var
->choices
.i_count
592 p_val2
->p_list
->p_values
=
593 malloc( p_var
->choices
.i_count
* sizeof(vlc_value_t
) );
594 p_val2
->p_list
->pi_types
=
595 malloc( p_var
->choices
.i_count
* sizeof(int) );
598 p_val
->p_list
->i_count
= p_var
->choices
.i_count
;
599 if( p_val2
) p_val2
->p_list
->i_count
= p_var
->choices
.i_count
;
600 for( i
= 0 ; i
< p_var
->choices
.i_count
; i
++ )
602 p_val
->p_list
->p_values
[i
] = p_var
->choices
.p_values
[i
];
603 p_val
->p_list
->pi_types
[i
] = p_var
->i_type
;
604 p_var
->pf_dup( &p_val
->p_list
->p_values
[i
] );
607 p_val2
->p_list
->p_values
[i
].psz_string
=
608 p_var
->choices_text
.p_values
[i
].psz_string
?
609 strdup(p_var
->choices_text
.p_values
[i
].psz_string
) : NULL
;
610 p_val2
->p_list
->pi_types
[i
] = VLC_VAR_STRING
;
614 case VLC_VAR_FREELIST
:
616 if( p_val2
&& p_val2
->p_list
)
618 for( i
= 0; i
< p_val2
->p_list
->i_count
; i
++ )
619 free( p_val2
->p_list
->p_values
[i
].psz_string
);
620 if( p_val2
->p_list
->i_count
)
622 free( p_val2
->p_list
->p_values
);
623 free( p_val2
->p_list
->pi_types
);
625 free( p_val2
->p_list
);
628 case VLC_VAR_SETTEXT
:
629 free( p_var
->psz_text
);
630 if( p_val
&& p_val
->psz_string
)
631 p_var
->psz_text
= strdup( p_val
->psz_string
);
633 case VLC_VAR_GETTEXT
:
634 p_val
->psz_string
= NULL
;
635 if( p_var
->psz_text
)
637 p_val
->psz_string
= strdup( p_var
->psz_text
);
640 case VLC_VAR_INHERITVALUE
:
644 if( InheritValue( p_this
,
645 p_val2
? p_val2
->psz_string
: psz_name
,
646 &val
, p_var
->i_type
)
649 /* Duplicate already done */
651 /* Backup needed stuff */
653 /* Check boundaries and list */
654 CheckValue( p_var
, &val
);
655 /* Set the variable */
657 /* Free data if needed */
658 p_var
->pf_free( &oldval
);
664 p_var
->pf_dup( p_val
);
668 case VLC_VAR_TRIGGER_CALLBACKS
:
670 /* Deal with callbacks. Tell we're in a callback, release the lock,
671 * call stored functions, retake the lock. */
672 if( p_var
->i_entries
)
675 int i_entries
= p_var
->i_entries
;
676 callback_entry_t
*p_entries
= p_var
->p_entries
;
678 p_var
->b_incallback
= VLC_TRUE
;
679 vlc_mutex_unlock( &p_priv
->var_lock
);
682 for( ; i_entries
-- ; )
684 p_entries
[i_entries
].pf_callback( p_this
, psz_name
, p_var
->val
, p_var
->val
,
685 p_entries
[i_entries
].p_data
);
688 vlc_mutex_lock( &p_priv
->var_lock
);
690 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
693 msg_Err( p_this
, "variable %s has disappeared", psz_name
);
694 vlc_mutex_unlock( &p_priv
->var_lock
);
698 p_var
= &p_priv
->p_vars
[i_var
];
699 p_var
->b_incallback
= VLC_FALSE
;
708 vlc_mutex_unlock( &p_priv
->var_lock
);
714 * Request a variable's type
716 * \return The variable type if it exists, or 0 if the
717 * variable could not be found.
720 int __var_Type( vlc_object_t
*p_this
, const char *psz_name
)
723 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
725 vlc_mutex_lock( &p_priv
->var_lock
);
727 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
731 vlc_mutex_unlock( &p_priv
->var_lock
);
735 i_type
= p_priv
->p_vars
[i_var
].i_type
;
737 vlc_mutex_unlock( &p_priv
->var_lock
);
743 * Set a variable's value
745 * \param p_this The object that hold the variable
746 * \param psz_name The name of the variable
747 * \param val the value to set
749 int __var_Set( vlc_object_t
*p_this
, const char *psz_name
, vlc_value_t val
)
754 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
756 vlc_mutex_lock( &p_priv
->var_lock
);
758 i_var
= GetUnused( p_this
, psz_name
);
761 vlc_mutex_unlock( &p_priv
->var_lock
);
765 p_var
= &p_priv
->p_vars
[i_var
];
767 /* Duplicate data if needed */
768 p_var
->pf_dup( &val
);
770 /* Backup needed stuff */
773 /* Check boundaries and list */
774 CheckValue( p_var
, &val
);
776 /* Set the variable */
779 /* Deal with callbacks. Tell we're in a callback, release the lock,
780 * call stored functions, retake the lock. */
781 if( p_var
->i_entries
)
784 int i_entries
= p_var
->i_entries
;
785 callback_entry_t
*p_entries
= p_var
->p_entries
;
787 p_var
->b_incallback
= VLC_TRUE
;
788 vlc_mutex_unlock( &p_priv
->var_lock
);
791 for( ; i_entries
-- ; )
793 p_entries
[i_entries
].pf_callback( p_this
, psz_name
, oldval
, val
,
794 p_entries
[i_entries
].p_data
);
797 vlc_mutex_lock( &p_priv
->var_lock
);
799 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
802 msg_Err( p_this
, "variable %s has disappeared", psz_name
);
803 vlc_mutex_unlock( &p_priv
->var_lock
);
807 p_var
= &p_priv
->p_vars
[i_var
];
808 p_var
->b_incallback
= VLC_FALSE
;
811 /* Free data if needed */
812 p_var
->pf_free( &oldval
);
814 vlc_mutex_unlock( &p_priv
->var_lock
);
820 * Get a variable's value
822 * \param p_this The object that holds the variable
823 * \param psz_name The name of the variable
824 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
825 * after the function is finished
827 int __var_Get( vlc_object_t
*p_this
, const char *psz_name
, vlc_value_t
*p_val
)
831 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
833 vlc_mutex_lock( &p_priv
->var_lock
);
835 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
839 vlc_mutex_unlock( &p_priv
->var_lock
);
843 p_var
= &p_priv
->p_vars
[i_var
];
845 /* Really get the variable */
848 /* Duplicate value if needed */
849 p_var
->pf_dup( p_val
);
851 vlc_mutex_unlock( &p_priv
->var_lock
);
858 * Finds a process-wide mutex, creates it if needed, and locks it.
859 * Unlock with vlc_mutex_unlock().
861 vlc_mutex_t
*var_AcquireMutex( const char *name
)
863 libvlc_global_data_t
*p_global
= vlc_global();
866 if( var_Create( p_global
, name
, VLC_VAR_MUTEX
) )
869 var_Get( p_global
, name
, &val
);
870 vlc_mutex_lock( val
.p_address
);
871 return val
.p_address
;
876 * Register a callback in a variable
878 * We store a function pointer that will be called upon variable
881 * \param p_this The object that holds the variable
882 * \param psz_name The name of the variable
883 * \param pf_callback The function pointer
884 * \param p_data A generic pointer that will be passed as the last
885 * argument to the callback function.
887 * \warning The callback function is run in the thread that calls var_Set on
888 * the variable. Use proper locking. This thread may not have much
889 * time to spare, so keep callback functions short.
891 int __var_AddCallback( vlc_object_t
*p_this
, const char *psz_name
,
892 vlc_callback_t pf_callback
, void *p_data
)
896 callback_entry_t entry
;
897 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
899 entry
.pf_callback
= pf_callback
;
900 entry
.p_data
= p_data
;
902 vlc_mutex_lock( &p_priv
->var_lock
);
904 i_var
= GetUnused( p_this
, psz_name
);
907 vlc_mutex_unlock( &p_priv
->var_lock
);
911 p_var
= &p_priv
->p_vars
[i_var
];
913 INSERT_ELEM( p_var
->p_entries
,
918 vlc_mutex_unlock( &p_priv
->var_lock
);
924 * Remove a callback from a variable
926 * pf_callback and p_data have to be given again, because different objects
927 * might have registered the same callback function.
929 int __var_DelCallback( vlc_object_t
*p_this
, const char *psz_name
,
930 vlc_callback_t pf_callback
, void *p_data
)
934 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
936 vlc_mutex_lock( &p_priv
->var_lock
);
938 i_var
= GetUnused( p_this
, psz_name
);
941 vlc_mutex_unlock( &p_priv
->var_lock
);
945 p_var
= &p_priv
->p_vars
[i_var
];
947 for( i_entry
= p_var
->i_entries
; i_entry
-- ; )
949 if( p_var
->p_entries
[i_entry
].pf_callback
== pf_callback
950 && p_var
->p_entries
[i_entry
].p_data
== p_data
)
958 vlc_mutex_unlock( &p_priv
->var_lock
);
962 REMOVE_ELEM( p_var
->p_entries
, p_var
->i_entries
, i_entry
);
964 vlc_mutex_unlock( &p_priv
->var_lock
);
970 * Trigger callback on a variable
972 * \param p_this The object that hold the variable
973 * \param psz_name The name of the variable
975 int __var_TriggerCallback( vlc_object_t
*p_this
, const char *psz_name
)
980 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
982 vlc_mutex_lock( &p_priv
->var_lock
);
984 i_var
= GetUnused( p_this
, psz_name
);
987 vlc_mutex_unlock( &p_priv
->var_lock
);
991 p_var
= &p_priv
->p_vars
[i_var
];
993 /* Backup needed stuff */
996 /* Deal with callbacks. Tell we're in a callback, release the lock,
997 * call stored functions, retake the lock. */
998 if( p_var
->i_entries
)
1001 int i_entries
= p_var
->i_entries
;
1002 callback_entry_t
*p_entries
= p_var
->p_entries
;
1004 p_var
->b_incallback
= VLC_TRUE
;
1005 vlc_mutex_unlock( &p_priv
->var_lock
);
1007 /* The real calls */
1008 for( ; i_entries
-- ; )
1010 p_entries
[i_entries
].pf_callback( p_this
, psz_name
, oldval
, oldval
,
1011 p_entries
[i_entries
].p_data
);
1014 vlc_mutex_lock( &p_priv
->var_lock
);
1016 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
1019 msg_Err( p_this
, "variable %s has disappeared", psz_name
);
1020 vlc_mutex_unlock( &p_priv
->var_lock
);
1024 p_var
= &p_priv
->p_vars
[i_var
];
1025 p_var
->b_incallback
= VLC_FALSE
;
1028 vlc_mutex_unlock( &p_priv
->var_lock
);
1032 /** Parse a stringified option
1033 * This function parse a string option and create the associated object
1035 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1036 * option name and bar is the value of the option.
1037 * \param p_obj the object in which the variable must be created
1038 * \param psz_option the option to parse
1041 void var_OptionParse( vlc_object_t
*p_obj
, const char *psz_option
)
1043 char *psz_name
, *psz_value
;
1045 vlc_bool_t b_isno
= VLC_FALSE
;
1048 val
.psz_string
= NULL
;
1050 /* It's too much of a hassle to remove the ':' when we parse
1051 * the cmd line :) */
1052 if( psz_option
[0] == ':' )
1055 if( !psz_option
[0] )
1058 psz_name
= strdup( psz_option
);
1059 if( psz_name
== NULL
)
1062 psz_value
= strchr( psz_name
, '=' );
1063 if( psz_value
!= NULL
)
1064 *psz_value
++ = '\0';
1066 /* FIXME: :programs should be handled generically */
1067 if( !strcmp( psz_name
, "programs" ) )
1068 i_type
= VLC_VAR_LIST
;
1070 i_type
= config_GetType( p_obj
, psz_name
);
1072 if( !i_type
&& !psz_value
)
1074 /* check for "no-foo" or "nofoo" */
1075 if( !strncmp( psz_name
, "no-", 3 ) )
1077 memmove( psz_name
, psz_name
+ 3, strlen(psz_name
) + 1 - 3 );
1079 else if( !strncmp( psz_name
, "no", 2 ) )
1081 memmove( psz_name
, psz_name
+ 2, strlen(psz_name
) + 1 - 2 );
1083 else goto cleanup
; /* Option doesn't exist */
1086 i_type
= config_GetType( p_obj
, psz_name
);
1088 if( !i_type
) goto cleanup
; /* Option doesn't exist */
1090 if( ( i_type
!= VLC_VAR_BOOL
) &&
1091 ( !psz_value
|| !*psz_value
) ) goto cleanup
; /* Invalid value */
1093 /* check if option is unsafe */
1095 module_config_t
*p_config
= config_FindConfig( p_obj
, psz_name
);
1096 if( !p_config
->b_safe
)
1098 int policy
= config_GetInt( p_obj
, "security-policy" );
1102 msg_Err( p_obj
, "option %s is unsafe and is blocked by security policy", psz_name
);
1106 case 2: /* prompt */
1108 char description
[256];
1109 snprintf(description
, sizeof(description
), _("playlist item is making use of the following unsafe option '%s', which may be harmful if used in a malicious way, authorize it ?"), psz_name
);
1110 if( DIALOG_OK_YES
!= intf_UserYesNo( p_obj
, _("WARNING: Unsafe Playlist"), description
, _("Yes"), _("No"), NULL
) )
1112 msg_Err( p_obj
, "option %s is unsafe and is blocked by security policy", psz_name
);
1122 /* Create the variable in the input object.
1123 * Children of the input object will be able to retreive this value
1124 * thanks to the inheritance property of the object variables. */
1125 var_Create( p_obj
, psz_name
, i_type
);
1130 val
.b_bool
= !b_isno
;
1133 case VLC_VAR_INTEGER
:
1134 val
.i_int
= strtol( psz_value
, NULL
, 0 );
1138 val
.f_float
= atof( psz_value
);
1141 case VLC_VAR_STRING
:
1142 case VLC_VAR_MODULE
:
1144 case VLC_VAR_DIRECTORY
:
1145 val
.psz_string
= psz_value
;
1150 char *psz_orig
, *psz_var
;
1151 vlc_list_t
*p_list
= malloc(sizeof(vlc_list_t
));
1152 val
.p_list
= p_list
;
1153 p_list
->i_count
= 0;
1155 psz_var
= psz_orig
= strdup(psz_value
);
1156 while( psz_var
&& *psz_var
)
1158 char *psz_item
= psz_var
;
1160 while( *psz_var
&& *psz_var
!= ',' ) psz_var
++;
1161 if( *psz_var
== ',' )
1166 val2
.i_int
= strtol( psz_item
, NULL
, 0 );
1167 INSERT_ELEM( p_list
->p_values
, p_list
->i_count
,
1168 p_list
->i_count
, val2
);
1169 /* p_list->i_count is incremented twice by INSERT_ELEM */
1171 INSERT_ELEM( p_list
->pi_types
, p_list
->i_count
,
1172 p_list
->i_count
, VLC_VAR_INTEGER
);
1182 var_Set( p_obj
, psz_name
, val
);
1189 /* Following functions are local */
1191 /*****************************************************************************
1192 * GetUnused: find an unused variable from its name
1193 *****************************************************************************
1194 * We do i_tries tries before giving up, just in case the variable is being
1195 * modified and called from a callback.
1196 *****************************************************************************/
1197 static int GetUnused( vlc_object_t
*p_this
, const char *psz_name
)
1199 int i_var
, i_tries
= 0;
1200 vlc_object_internals_t
*p_priv
= p_this
->p_internals
;
1204 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
1210 if( ! p_priv
->p_vars
[i_var
].b_incallback
)
1215 if( i_tries
++ > 100 )
1217 msg_Err( p_this
, "caught in a callback deadlock?" );
1218 return VLC_ETIMEOUT
;
1221 vlc_mutex_unlock( &p_priv
->var_lock
);
1222 msleep( THREAD_SLEEP
);
1223 vlc_mutex_lock( &p_priv
->var_lock
);
1227 /*****************************************************************************
1228 * HashString: our cool hash function
1229 *****************************************************************************
1230 * This function is not intended to be crypto-secure, we only want it to be
1231 * fast and not suck too much. This one is pretty fast and did 0 collisions
1232 * in wenglish's dictionary.
1233 *****************************************************************************/
1234 static uint32_t HashString( const char *psz_string
)
1236 uint32_t i_hash
= 0;
1238 while( *psz_string
)
1240 i_hash
+= *psz_string
++;
1241 i_hash
+= i_hash
<< 10;
1242 i_hash
^= i_hash
>> 8;
1248 /*****************************************************************************
1249 * Insert: find an empty slot to insert a new variable
1250 *****************************************************************************
1251 * We use a recursive inner function indexed on the hash. This function does
1252 * nothing in the rare cases where a collision may occur, see Lookup()
1253 * to see how we handle them.
1254 * XXX: does this really need to be written recursively?
1255 *****************************************************************************/
1256 static int Insert( variable_t
*p_vars
, int i_count
, const char *psz_name
)
1263 return InsertInner( p_vars
, i_count
, HashString( psz_name
) );
1266 static int InsertInner( variable_t
*p_vars
, int i_count
, uint32_t i_hash
)
1270 if( i_hash
<= p_vars
[0].i_hash
)
1275 if( i_hash
>= p_vars
[i_count
- 1].i_hash
)
1280 i_middle
= i_count
/ 2;
1282 /* We know that 0 < i_middle */
1283 if( i_hash
< p_vars
[i_middle
].i_hash
)
1285 return InsertInner( p_vars
, i_middle
, i_hash
);
1288 /* We know that i_middle + 1 < i_count */
1289 if( i_hash
> p_vars
[i_middle
+ 1].i_hash
)
1291 return i_middle
+ 1 + InsertInner( p_vars
+ i_middle
+ 1,
1292 i_count
- i_middle
- 1,
1296 return i_middle
+ 1;
1299 /*****************************************************************************
1300 * Lookup: find an existing variable given its name
1301 *****************************************************************************
1302 * We use a recursive inner function indexed on the hash. Care is taken of
1303 * possible hash collisions.
1304 * XXX: does this really need to be written recursively?
1305 *****************************************************************************/
1306 static int Lookup( variable_t
*p_vars
, int i_count
, const char *psz_name
)
1316 i_hash
= HashString( psz_name
);
1318 i_pos
= LookupInner( p_vars
, i_count
, i_hash
);
1320 /* Hash not found */
1321 if( i_hash
!= p_vars
[i_pos
].i_hash
)
1326 /* Hash found, entry found */
1327 if( !strcmp( psz_name
, p_vars
[i_pos
].psz_name
) )
1332 /* Hash collision! This should be very rare, but we cannot guarantee
1333 * it will never happen. Just do an exhaustive search amongst all
1334 * entries with the same hash. */
1335 for( i
= i_pos
- 1 ; i
> 0 && i_hash
== p_vars
[i
].i_hash
; i
-- )
1337 if( !strcmp( psz_name
, p_vars
[i
].psz_name
) )
1343 for( i
= i_pos
+ 1 ; i
< i_count
&& i_hash
== p_vars
[i
].i_hash
; i
++ )
1345 if( !strcmp( psz_name
, p_vars
[i
].psz_name
) )
1351 /* Hash found, but entry not found */
1355 static int LookupInner( variable_t
*p_vars
, int i_count
, uint32_t i_hash
)
1359 if( i_hash
<= p_vars
[0].i_hash
)
1364 if( i_hash
>= p_vars
[i_count
-1].i_hash
)
1369 i_middle
= i_count
/ 2;
1371 /* We know that 0 < i_middle */
1372 if( i_hash
< p_vars
[i_middle
].i_hash
)
1374 return LookupInner( p_vars
, i_middle
, i_hash
);
1377 /* We know that i_middle + 1 < i_count */
1378 if( i_hash
> p_vars
[i_middle
].i_hash
)
1380 return i_middle
+ LookupInner( p_vars
+ i_middle
,
1388 /*****************************************************************************
1389 * CheckValue: check that a value is valid wrt. a variable
1390 *****************************************************************************
1391 * This function checks p_val's value against p_var's limitations such as
1392 * minimal and maximal value, step, in-list position, and modifies p_val if
1394 ****************************************************************************/
1395 static void CheckValue ( variable_t
*p_var
, vlc_value_t
*p_val
)
1397 /* Check that our variable is in the list */
1398 if( p_var
->i_type
& VLC_VAR_HASCHOICE
&& p_var
->choices
.i_count
)
1402 /* FIXME: the list is sorted, dude. Use something cleverer. */
1403 for( i
= p_var
->choices
.i_count
; i
-- ; )
1405 if( p_var
->pf_cmp( *p_val
, p_var
->choices
.p_values
[i
] ) == 0 )
1411 /* If not found, change it to anything vaguely valid */
1414 /* Free the old variable, get the new one, dup it */
1415 p_var
->pf_free( p_val
);
1416 *p_val
= p_var
->choices
.p_values
[p_var
->i_default
>= 0
1417 ? p_var
->i_default
: 0 ];
1418 p_var
->pf_dup( p_val
);
1422 /* Check that our variable is within the bounds */
1423 switch( p_var
->i_type
& VLC_VAR_TYPE
)
1425 case VLC_VAR_INTEGER
:
1426 if( p_var
->i_type
& VLC_VAR_HASSTEP
&& p_var
->step
.i_int
1427 && (p_val
->i_int
% p_var
->step
.i_int
) )
1429 p_val
->i_int
= (p_val
->i_int
+ (p_var
->step
.i_int
/ 2))
1430 / p_var
->step
.i_int
* p_var
->step
.i_int
;
1432 if( p_var
->i_type
& VLC_VAR_HASMIN
1433 && p_val
->i_int
< p_var
->min
.i_int
)
1435 p_val
->i_int
= p_var
->min
.i_int
;
1437 if( p_var
->i_type
& VLC_VAR_HASMAX
1438 && p_val
->i_int
> p_var
->max
.i_int
)
1440 p_val
->i_int
= p_var
->max
.i_int
;
1444 if( p_var
->i_type
& VLC_VAR_HASSTEP
&& p_var
->step
.f_float
)
1446 float f_round
= p_var
->step
.f_float
* (float)(int)( 0.5 +
1447 p_val
->f_float
/ p_var
->step
.f_float
);
1448 if( p_val
->f_float
!= f_round
)
1450 p_val
->f_float
= f_round
;
1453 if( p_var
->i_type
& VLC_VAR_HASMIN
1454 && p_val
->f_float
< p_var
->min
.f_float
)
1456 p_val
->f_float
= p_var
->min
.f_float
;
1458 if( p_var
->i_type
& VLC_VAR_HASMAX
1459 && p_val
->f_float
> p_var
->max
.f_float
)
1461 p_val
->f_float
= p_var
->max
.f_float
;
1470 /*****************************************************************************
1471 * InheritValue: try to inherit the value of this variable from the same one
1472 * in our closest parent.
1473 *****************************************************************************/
1474 static int InheritValue( vlc_object_t
*p_this
, const char *psz_name
,
1475 vlc_value_t
*p_val
, int i_type
)
1480 /* No need to take the structure lock,
1481 * we are only looking for our parents */
1483 if( !p_this
->p_parent
)
1485 switch( i_type
& VLC_VAR_TYPE
)
1488 case VLC_VAR_DIRECTORY
:
1489 case VLC_VAR_STRING
:
1490 case VLC_VAR_MODULE
:
1491 p_val
->psz_string
= config_GetPsz( p_this
, psz_name
);
1492 if( !p_val
->psz_string
) p_val
->psz_string
= strdup("");
1495 p_val
->f_float
= config_GetFloat( p_this
, psz_name
);
1497 case VLC_VAR_INTEGER
:
1498 case VLC_VAR_HOTKEY
:
1499 p_val
->i_int
= config_GetInt( p_this
, psz_name
);
1502 p_val
->b_bool
= config_GetInt( p_this
, psz_name
);
1506 char *psz_orig
, *psz_var
;
1507 vlc_list_t
*p_list
= malloc(sizeof(vlc_list_t
));
1508 p_val
->p_list
= p_list
;
1509 p_list
->i_count
= 0;
1511 psz_var
= psz_orig
= config_GetPsz( p_this
, psz_name
);
1512 while( psz_var
&& *psz_var
)
1514 char *psz_item
= psz_var
;
1516 while( *psz_var
&& *psz_var
!= ',' ) psz_var
++;
1517 if( *psz_var
== ',' )
1522 val
.i_int
= strtol( psz_item
, NULL
, 0 );
1523 INSERT_ELEM( p_list
->p_values
, p_list
->i_count
,
1524 p_list
->i_count
, val
);
1525 /* p_list->i_count is incremented twice by INSERT_ELEM */
1527 INSERT_ELEM( p_list
->pi_types
, p_list
->i_count
,
1528 p_list
->i_count
, VLC_VAR_INTEGER
);
1541 vlc_object_internals_t
*p_priv
= p_this
->p_parent
->p_internals
;
1543 /* Look for the variable */
1544 vlc_mutex_lock( &p_priv
->var_lock
);
1546 i_var
= Lookup( p_priv
->p_vars
, p_priv
->i_vars
, psz_name
);
1551 p_var
= &p_priv
->p_vars
[i_var
];
1553 /* Really get the variable */
1554 *p_val
= p_var
->val
;
1556 /* Duplicate value if needed */
1557 p_var
->pf_dup( p_val
);
1559 vlc_mutex_unlock( &p_priv
->var_lock
);
1563 vlc_mutex_unlock( &p_priv
->var_lock
);
1565 /* We're still not there */
1567 return InheritValue( p_this
->p_parent
, psz_name
, p_val
, i_type
);
1570 /**********************************************************************
1571 * Execute a var command on an object identified by its name
1572 **********************************************************************/
1573 int __var_Command( vlc_object_t
*p_this
, const char *psz_name
,
1574 const char *psz_cmd
, const char *psz_arg
, char **psz_msg
)
1576 vlc_object_t
*p_obj
= vlc_object_find_name( p_this
->p_libvlc
,
1577 psz_name
, FIND_CHILD
);
1583 *psz_msg
= strdup( "Unknown destination object." );
1587 i_type
= var_Type( p_obj
, psz_cmd
);
1588 if( !( i_type
&VLC_VAR_ISCOMMAND
) )
1590 vlc_object_release( p_obj
);
1592 *psz_msg
= strdup( "Variable doesn't exist or isn't a command." );
1593 return VLC_EGENERIC
;
1599 case VLC_VAR_INTEGER
:
1600 i_ret
= var_SetInteger( p_obj
, psz_cmd
, atoi( psz_arg
) );
1603 i_ret
= var_SetFloat( p_obj
, psz_cmd
, atof( psz_arg
) );
1605 case VLC_VAR_STRING
:
1606 i_ret
= var_SetString( p_obj
, psz_cmd
, psz_arg
);
1609 i_ret
= var_SetBool( p_obj
, psz_cmd
, atoi( psz_arg
) );
1612 i_ret
= VLC_EGENERIC
;
1616 vlc_object_release( p_obj
);
1620 *psz_msg
= (char*)malloc( 80 );
1621 sprintf( *psz_msg
, "%s on object %s returned %i (%s)",
1622 psz_cmd
, psz_name
, i_ret
, vlc_error( i_ret
) );