1 /*****************************************************************************
2 * core.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.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 *****************************************************************************/
28 #include <vlc_common.h>
30 #include "vlc_charset.h"
31 #include "vlc_configuration.h"
35 #include "configuration.h"
36 #include "modules/modules.h"
38 vlc_rwlock_t config_lock;
40 static inline char *strdupnull (const char *src)
42 return src ? strdup (src) : NULL;
45 /* Item types that use a string value (i.e. serialized in the module cache) */
46 int IsConfigStringType (int type)
48 static const unsigned char config_types[] =
50 CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
51 CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
52 CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT, CONFIG_ITEM_FONT
55 /* NOTE: this needs to be changed if we ever get more than 255 types */
56 return memchr (config_types, type, sizeof (config_types)) != NULL;
60 int IsConfigIntegerType (int type)
62 static const unsigned char config_types[] =
64 CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
65 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
68 return memchr (config_types, type, sizeof (config_types)) != NULL;
72 /*****************************************************************************
73 * config_GetType: get the type of a variable (bool, int, float, string)
74 *****************************************************************************
75 * This function is used to get the type of a variable from its name.
76 * Beware, this is quite slow.
77 *****************************************************************************/
78 int __config_GetType( vlc_object_t *p_this, const char *psz_name )
80 module_config_t *p_config;
83 p_config = config_FindConfig( p_this, psz_name );
91 switch( p_config->i_type )
93 case CONFIG_ITEM_BOOL:
94 i_type = VLC_VAR_BOOL;
97 case CONFIG_ITEM_INTEGER:
99 i_type = VLC_VAR_INTEGER;
102 case CONFIG_ITEM_FLOAT:
103 i_type = VLC_VAR_FLOAT;
106 case CONFIG_ITEM_MODULE:
107 case CONFIG_ITEM_MODULE_CAT:
108 case CONFIG_ITEM_MODULE_LIST:
109 case CONFIG_ITEM_MODULE_LIST_CAT:
110 i_type = VLC_VAR_MODULE;
113 case CONFIG_ITEM_STRING:
114 i_type = VLC_VAR_STRING;
117 case CONFIG_ITEM_PASSWORD:
118 i_type = VLC_VAR_STRING;
121 case CONFIG_ITEM_FILE:
122 i_type = VLC_VAR_FILE;
125 case CONFIG_ITEM_DIRECTORY:
126 i_type = VLC_VAR_DIRECTORY;
137 /*****************************************************************************
138 * config_GetInt: get the value of an int variable
139 *****************************************************************************
140 * This function is used to get the value of variables which are internally
141 * represented by an integer (CONFIG_ITEM_INTEGER and
143 *****************************************************************************/
144 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
146 module_config_t *p_config;
148 p_config = config_FindConfig( p_this, psz_name );
153 msg_Err( p_this, "option %s does not exist", psz_name );
157 if (!IsConfigIntegerType (p_config->i_type))
159 msg_Err( p_this, "option %s does not refer to an int", psz_name );
165 vlc_rwlock_rdlock (&config_lock);
166 val = p_config->value.i;
167 vlc_rwlock_unlock (&config_lock);
171 /*****************************************************************************
172 * config_GetFloat: get the value of a float variable
173 *****************************************************************************
174 * This function is used to get the value of variables which are internally
175 * represented by a float (CONFIG_ITEM_FLOAT).
176 *****************************************************************************/
177 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
179 module_config_t *p_config;
181 p_config = config_FindConfig( p_this, psz_name );
186 msg_Err( p_this, "option %s does not exist", psz_name );
190 if (!IsConfigFloatType (p_config->i_type))
192 msg_Err( p_this, "option %s does not refer to a float", psz_name );
198 vlc_rwlock_rdlock (&config_lock);
199 val = p_config->value.f;
200 vlc_rwlock_unlock (&config_lock);
204 /*****************************************************************************
205 * config_GetPsz: get the string value of a string variable
206 *****************************************************************************
207 * This function is used to get the value of variables which are internally
208 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
209 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
211 * Important note: remember to free() the returned char* because it's a
212 * duplicate of the actual value. It isn't safe to return a pointer to the
213 * actual value as it can be modified at any time.
214 *****************************************************************************/
215 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
217 module_config_t *p_config;
219 p_config = config_FindConfig( p_this, psz_name );
224 msg_Err( p_this, "option %s does not exist", psz_name );
228 if (!IsConfigStringType (p_config->i_type))
230 msg_Err( p_this, "option %s does not refer to a string", psz_name );
234 /* return a copy of the string */
235 vlc_rwlock_rdlock (&config_lock);
236 char *psz_value = strdupnull (p_config->value.psz);
237 vlc_rwlock_unlock (&config_lock);
242 /*****************************************************************************
243 * config_PutPsz: set the string value of a string variable
244 *****************************************************************************
245 * This function is used to set the value of variables which are internally
246 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
247 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
248 *****************************************************************************/
249 void __config_PutPsz( vlc_object_t *p_this,
250 const char *psz_name, const char *psz_value )
252 module_config_t *p_config;
253 vlc_value_t oldval, val;
255 p_config = config_FindConfig( p_this, psz_name );
261 msg_Warn( p_this, "option %s does not exist", psz_name );
265 if (!IsConfigStringType (p_config->i_type))
267 msg_Err( p_this, "option %s does not refer to a string", psz_name );
271 vlc_rwlock_wrlock (&config_lock);
273 /* backup old value */
274 oldval.psz_string = (char *)p_config->value.psz;
276 if ((psz_value != NULL) && *psz_value)
277 p_config->value.psz = strdup (psz_value);
279 p_config->value.psz = NULL;
281 p_config->b_dirty = true;
283 val.psz_string = (char *)p_config->value.psz;
285 vlc_rwlock_unlock (&config_lock);
287 if( p_config->pf_callback )
289 p_config->pf_callback( p_this, psz_name, oldval, val,
290 p_config->p_callback_data );
293 /* free old string */
294 free( oldval.psz_string );
297 /*****************************************************************************
298 * config_PutInt: set the integer value of an int variable
299 *****************************************************************************
300 * This function is used to set the value of variables which are internally
301 * represented by an integer (CONFIG_ITEM_INTEGER and
303 *****************************************************************************/
304 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
306 module_config_t *p_config;
309 p_config = config_FindConfig( p_this, psz_name );
314 msg_Warn( p_this, "option %s does not exist", psz_name );
318 if (!IsConfigIntegerType (p_config->i_type))
320 msg_Err( p_this, "option %s does not refer to an int", psz_name );
324 /* if i_min == i_max == 0, then do not use them */
325 if ((p_config->min.i == 0) && (p_config->max.i == 0))
327 else if (i_value < p_config->min.i)
328 i_value = p_config->min.i;
329 else if (i_value > p_config->max.i)
330 i_value = p_config->max.i;
332 vlc_rwlock_wrlock (&config_lock);
333 /* backup old value */
334 oldval.i_int = p_config->value.i;
336 p_config->value.i = i_value;
337 p_config->b_dirty = true;
338 vlc_rwlock_unlock (&config_lock);
340 if( p_config->pf_callback )
345 p_config->pf_callback( p_this, psz_name, oldval, val,
346 p_config->p_callback_data );
350 /*****************************************************************************
351 * config_PutFloat: set the value of a float variable
352 *****************************************************************************
353 * This function is used to set the value of variables which are internally
354 * represented by a float (CONFIG_ITEM_FLOAT).
355 *****************************************************************************/
356 void __config_PutFloat( vlc_object_t *p_this,
357 const char *psz_name, float f_value )
359 module_config_t *p_config;
362 p_config = config_FindConfig( p_this, psz_name );
367 msg_Warn( p_this, "option %s does not exist", psz_name );
371 if (!IsConfigFloatType (p_config->i_type))
373 msg_Err( p_this, "option %s does not refer to a float", psz_name );
377 /* if f_min == f_max == 0, then do not use them */
378 if ((p_config->min.f == 0) && (p_config->max.f == 0))
380 else if (f_value < p_config->min.f)
381 f_value = p_config->min.f;
382 else if (f_value > p_config->max.f)
383 f_value = p_config->max.f;
385 vlc_rwlock_wrlock (&config_lock);
386 /* backup old value */
387 oldval.f_float = p_config->value.f;
389 p_config->value.f = f_value;
390 p_config->b_dirty = true;
391 vlc_rwlock_unlock (&config_lock);
393 if( p_config->pf_callback )
397 val.f_float = f_value;
398 p_config->pf_callback( p_this, psz_name, oldval, val,
399 p_config->p_callback_data );
403 /*****************************************************************************
404 * config_FindConfig: find the config structure associated with an option.
405 *****************************************************************************
406 * FIXME: This function really needs to be optimized.
407 * FIXME: And now even more.
408 * FIXME: remove p_this pointer parameter (or use it)
409 *****************************************************************************/
410 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
415 if( !psz_name ) return NULL;
417 module_t **list = module_list_get (NULL);
421 for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
423 module_config_t *p_item, *p_end;
425 if( !p_parser->i_config_items )
428 for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
432 if( p_item->i_type & CONFIG_HINT )
435 if( !strcmp( psz_name, p_item->psz_name )
436 || ( p_item->psz_oldname
437 && !strcmp( psz_name, p_item->psz_oldname ) ) )
439 module_list_free (list);
445 module_list_free (list);
449 /*****************************************************************************
450 * config_Free: frees a duplicated module's configuration data.
451 *****************************************************************************
452 * This function frees all the data duplicated by config_Duplicate.
453 *****************************************************************************/
454 void config_Free( module_t *p_module )
458 for (size_t j = 0; j < p_module->confsize; j++)
460 module_config_t *p_item = p_module->p_config + j;
462 free( p_item->psz_type );
463 free( p_item->psz_name );
464 free( p_item->psz_text );
465 free( p_item->psz_longtext );
466 free( p_item->psz_oldname );
468 if (IsConfigStringType (p_item->i_type))
470 free (p_item->value.psz);
471 free (p_item->orig.psz);
472 free (p_item->saved.psz);
475 if( p_item->ppsz_list )
476 for( i = 0; i < p_item->i_list; i++ )
477 free( p_item->ppsz_list[i] );
478 if( p_item->ppsz_list_text )
479 for( i = 0; i < p_item->i_list; i++ )
480 free( p_item->ppsz_list_text[i] );
481 free( p_item->ppsz_list );
482 free( p_item->ppsz_list_text );
483 free( p_item->pi_list );
485 if( p_item->i_action )
487 for( i = 0; i < p_item->i_action; i++ )
489 free( p_item->ppsz_action_text[i] );
491 free( p_item->ppf_action );
492 free( p_item->ppsz_action_text );
496 free (p_module->p_config);
497 p_module->p_config = NULL;
500 /*****************************************************************************
501 * config_ResetAll: reset the configuration data for all the modules.
502 *****************************************************************************/
503 void __config_ResetAll( vlc_object_t *p_this )
507 module_t **list = module_list_get (NULL);
509 vlc_rwlock_wrlock (&config_lock);
510 for (size_t j = 0; (p_module = list[j]) != NULL; j++)
512 if( p_module->b_submodule ) continue;
514 for (size_t i = 0; i < p_module->confsize; i++ )
516 module_config_t *p_config = p_module->p_config + i;
518 if (IsConfigIntegerType (p_config->i_type))
519 p_config->value.i = p_config->orig.i;
521 if (IsConfigFloatType (p_config->i_type))
522 p_config->value.f = p_config->orig.f;
524 if (IsConfigStringType (p_config->i_type))
526 free ((char *)p_config->value.psz);
527 p_config->value.psz =
528 strdupnull (p_config->orig.psz);
532 vlc_rwlock_unlock (&config_lock);
534 module_list_free (list);