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;
138 /*****************************************************************************
139 * config_GetInt: get the value of an int variable
140 *****************************************************************************
141 * This function is used to get the value of variables which are internally
142 * represented by an integer (CONFIG_ITEM_INTEGER and
144 *****************************************************************************/
145 int config_GetInt( vlc_object_t *p_this, const char *psz_name )
147 module_config_t *p_config;
149 p_config = config_FindConfig( p_this, psz_name );
154 msg_Err( p_this, "option %s does not exist", psz_name );
158 if (!IsConfigIntegerType (p_config->i_type))
160 msg_Err( p_this, "option %s does not refer to an int", psz_name );
166 vlc_rwlock_rdlock (&config_lock);
167 val = p_config->value.i;
168 vlc_rwlock_unlock (&config_lock);
172 #undef config_GetFloat
173 /*****************************************************************************
174 * config_GetFloat: get the value of a float variable
175 *****************************************************************************
176 * This function is used to get the value of variables which are internally
177 * represented by a float (CONFIG_ITEM_FLOAT).
178 *****************************************************************************/
179 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
181 module_config_t *p_config;
183 p_config = config_FindConfig( p_this, psz_name );
188 msg_Err( p_this, "option %s does not exist", psz_name );
192 if (!IsConfigFloatType (p_config->i_type))
194 msg_Err( p_this, "option %s does not refer to a float", psz_name );
200 vlc_rwlock_rdlock (&config_lock);
201 val = p_config->value.f;
202 vlc_rwlock_unlock (&config_lock);
207 /*****************************************************************************
208 * config_GetPsz: get the string value of a string variable
209 *****************************************************************************
210 * This function is used to get the value of variables which are internally
211 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
212 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
214 * Important note: remember to free() the returned char* because it's a
215 * duplicate of the actual value. It isn't safe to return a pointer to the
216 * actual value as it can be modified at any time.
217 *****************************************************************************/
218 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
220 module_config_t *p_config;
222 p_config = config_FindConfig( p_this, psz_name );
227 msg_Err( p_this, "option %s does not exist", psz_name );
231 if (!IsConfigStringType (p_config->i_type))
233 msg_Err( p_this, "option %s does not refer to a string", psz_name );
237 /* return a copy of the string */
238 vlc_rwlock_rdlock (&config_lock);
239 char *psz_value = strdupnull (p_config->value.psz);
240 vlc_rwlock_unlock (&config_lock);
246 /*****************************************************************************
247 * config_PutPsz: set the string value of a string variable
248 *****************************************************************************
249 * This function is used to set the value of variables which are internally
250 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
251 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
252 *****************************************************************************/
253 void config_PutPsz( vlc_object_t *p_this,
254 const char *psz_name, const char *psz_value )
256 module_config_t *p_config;
257 vlc_value_t oldval, val;
259 p_config = config_FindConfig( p_this, psz_name );
265 msg_Warn( p_this, "option %s does not exist", psz_name );
269 if (!IsConfigStringType (p_config->i_type))
271 msg_Err( p_this, "option %s does not refer to a string", psz_name );
275 vlc_rwlock_wrlock (&config_lock);
277 /* backup old value */
278 oldval.psz_string = (char *)p_config->value.psz;
280 if ((psz_value != NULL) && *psz_value)
281 p_config->value.psz = strdup (psz_value);
283 p_config->value.psz = NULL;
285 p_config->b_dirty = true;
287 val.psz_string = (char *)p_config->value.psz;
289 vlc_rwlock_unlock (&config_lock);
291 if( p_config->pf_callback )
293 p_config->pf_callback( p_this, psz_name, oldval, val,
294 p_config->p_callback_data );
297 /* free old string */
298 free( oldval.psz_string );
302 /*****************************************************************************
303 * config_PutInt: set the integer value of an int variable
304 *****************************************************************************
305 * This function is used to set the value of variables which are internally
306 * represented by an integer (CONFIG_ITEM_INTEGER and
308 *****************************************************************************/
309 void config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
311 module_config_t *p_config;
314 p_config = config_FindConfig( p_this, psz_name );
319 msg_Warn( p_this, "option %s does not exist", psz_name );
323 if (!IsConfigIntegerType (p_config->i_type))
325 msg_Err( p_this, "option %s does not refer to an int", psz_name );
329 /* if i_min == i_max == 0, then do not use them */
330 if ((p_config->min.i == 0) && (p_config->max.i == 0))
332 else if (i_value < p_config->min.i)
333 i_value = p_config->min.i;
334 else if (i_value > p_config->max.i)
335 i_value = p_config->max.i;
337 vlc_rwlock_wrlock (&config_lock);
338 /* backup old value */
339 oldval.i_int = p_config->value.i;
341 p_config->value.i = i_value;
342 p_config->b_dirty = true;
343 vlc_rwlock_unlock (&config_lock);
345 if( p_config->pf_callback )
350 p_config->pf_callback( p_this, psz_name, oldval, val,
351 p_config->p_callback_data );
355 #undef config_PutFloat
356 /*****************************************************************************
357 * config_PutFloat: set the value of a float variable
358 *****************************************************************************
359 * This function is used to set the value of variables which are internally
360 * represented by a float (CONFIG_ITEM_FLOAT).
361 *****************************************************************************/
362 void config_PutFloat( vlc_object_t *p_this,
363 const char *psz_name, float f_value )
365 module_config_t *p_config;
368 p_config = config_FindConfig( p_this, psz_name );
373 msg_Warn( p_this, "option %s does not exist", psz_name );
377 if (!IsConfigFloatType (p_config->i_type))
379 msg_Err( p_this, "option %s does not refer to a float", psz_name );
383 /* if f_min == f_max == 0, then do not use them */
384 if ((p_config->min.f == 0) && (p_config->max.f == 0))
386 else if (f_value < p_config->min.f)
387 f_value = p_config->min.f;
388 else if (f_value > p_config->max.f)
389 f_value = p_config->max.f;
391 vlc_rwlock_wrlock (&config_lock);
392 /* backup old value */
393 oldval.f_float = p_config->value.f;
395 p_config->value.f = f_value;
396 p_config->b_dirty = true;
397 vlc_rwlock_unlock (&config_lock);
399 if( p_config->pf_callback )
403 val.f_float = f_value;
404 p_config->pf_callback( p_this, psz_name, oldval, val,
405 p_config->p_callback_data );
409 static int confcmp (const void *a, const void *b)
411 const module_config_t *const *ca = a, *const *cb = b;
413 return strcmp ((*ca)->psz_name, (*cb)->psz_name);
416 static int confnamecmp (const void *key, const void *elem)
418 const module_config_t *const *conf = elem;
420 return strcmp (key, (*conf)->psz_name);
425 module_config_t **list;
427 } config = { NULL, 0 };
430 * Index the configuration items by name for faster lookups.
432 int config_SortConfig (void)
435 module_t **mlist = module_list_get (&nmod);
436 if (unlikely(mlist == NULL))
440 for (size_t i = 0; i < nmod; i++)
441 nconf += mlist[i]->confsize;
443 module_config_t **clist = malloc (sizeof (*clist) * nconf);
444 if (unlikely(clist == NULL))
446 module_list_free (mlist);
451 for (size_t i = 0; i < nmod; i++)
453 module_t *parser = mlist[i];
454 module_config_t *item, *end;
456 for (item = parser->p_config, end = item + parser->confsize;
460 if (item->i_type & CONFIG_HINT)
461 continue; /* ignore hints */
462 clist[nconf++] = item;
465 module_list_free (mlist);
467 qsort (clist, nconf, sizeof (*clist), confcmp);
470 config.count = nconf;
474 void config_UnsortConfig (void)
476 module_config_t **clist;
485 /*****************************************************************************
486 * config_FindConfig: find the config structure associated with an option.
487 *****************************************************************************
488 * FIXME: remove p_this pointer parameter (or use it)
489 *****************************************************************************/
490 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
494 if (unlikely(name == NULL))
497 module_config_t *const *p;
498 p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
499 return p ? *p : NULL;
502 /*****************************************************************************
503 * config_Free: frees a duplicated module's configuration data.
504 *****************************************************************************
505 * This function frees all the data duplicated by config_Duplicate.
506 *****************************************************************************/
507 void config_Free( module_t *p_module )
511 for (size_t j = 0; j < p_module->confsize; j++)
513 module_config_t *p_item = p_module->p_config + j;
515 free( p_item->psz_type );
516 free( p_item->psz_name );
517 free( p_item->psz_text );
518 free( p_item->psz_longtext );
519 free( p_item->psz_oldname );
521 if (IsConfigStringType (p_item->i_type))
523 free (p_item->value.psz);
524 free (p_item->orig.psz);
525 free (p_item->saved.psz);
528 if( p_item->ppsz_list )
529 for( i = 0; i < p_item->i_list; i++ )
530 free( p_item->ppsz_list[i] );
531 if( p_item->ppsz_list_text )
532 for( i = 0; i < p_item->i_list; i++ )
533 free( p_item->ppsz_list_text[i] );
534 free( p_item->ppsz_list );
535 free( p_item->ppsz_list_text );
536 free( p_item->pi_list );
538 if( p_item->i_action )
540 for( i = 0; i < p_item->i_action; i++ )
542 free( p_item->ppsz_action_text[i] );
544 free( p_item->ppf_action );
545 free( p_item->ppsz_action_text );
549 free (p_module->p_config);
550 p_module->p_config = NULL;
553 #undef config_ResetAll
554 /*****************************************************************************
555 * config_ResetAll: reset the configuration data for all the modules.
556 *****************************************************************************/
557 void config_ResetAll( vlc_object_t *p_this )
561 module_t **list = module_list_get (NULL);
563 vlc_rwlock_wrlock (&config_lock);
564 for (size_t j = 0; (p_module = list[j]) != NULL; j++)
566 if( p_module->b_submodule ) continue;
568 for (size_t i = 0; i < p_module->confsize; i++ )
570 module_config_t *p_config = p_module->p_config + i;
572 if (IsConfigIntegerType (p_config->i_type))
573 p_config->value.i = p_config->orig.i;
575 if (IsConfigFloatType (p_config->i_type))
576 p_config->value.f = p_config->orig.f;
578 if (IsConfigStringType (p_config->i_type))
580 free ((char *)p_config->value.psz);
581 p_config->value.psz =
582 strdupnull (p_config->orig.psz);
586 vlc_rwlock_unlock (&config_lock);
588 module_list_free (list);