1 /*****************************************************************************
2 * core.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
30 #include <vlc_modules.h>
31 #include <vlc_plugin.h>
33 #include "vlc_configuration.h"
38 #include "configuration.h"
39 #include "modules/modules.h"
41 vlc_rwlock_t config_lock = VLC_STATIC_RWLOCK;
42 bool config_dirty = false;
44 static inline char *strdupnull (const char *src)
46 return src ? strdup (src) : NULL;
50 /*****************************************************************************
51 * config_GetType: get the type of a variable (bool, int, float, string)
52 *****************************************************************************
53 * This function is used to get the type of a variable from its name.
54 * Beware, this is quite slow.
55 *****************************************************************************/
56 int config_GetType( vlc_object_t *p_this, const char *psz_name )
58 module_config_t *p_config;
61 p_config = config_FindConfig( p_this, psz_name );
69 switch( CONFIG_CLASS(p_config->i_type) )
71 case CONFIG_ITEM_FLOAT:
72 i_type = VLC_VAR_FLOAT;
75 case CONFIG_ITEM_INTEGER:
76 i_type = VLC_VAR_INTEGER;
79 case CONFIG_ITEM_BOOL:
80 i_type = VLC_VAR_BOOL;
83 case CONFIG_ITEM_STRING:
84 i_type = VLC_VAR_STRING;
95 bool config_IsSafe( const char *name )
97 module_config_t *p_config = config_FindConfig( NULL, name );
98 return p_config != NULL && p_config->b_safe;
102 /*****************************************************************************
103 * config_GetInt: get the value of an int variable
104 *****************************************************************************
105 * This function is used to get the value of variables which are internally
106 * represented by an integer (CONFIG_ITEM_INTEGER and
108 *****************************************************************************/
109 int64_t config_GetInt( vlc_object_t *p_this, const char *psz_name )
111 module_config_t *p_config;
113 p_config = config_FindConfig( p_this, psz_name );
118 msg_Err( p_this, "option %s does not exist", psz_name );
122 if (!IsConfigIntegerType (p_config->i_type))
124 msg_Err( p_this, "option %s does not refer to an int", psz_name );
130 vlc_rwlock_rdlock (&config_lock);
131 val = p_config->value.i;
132 vlc_rwlock_unlock (&config_lock);
136 #undef config_GetFloat
137 /*****************************************************************************
138 * config_GetFloat: get the value of a float variable
139 *****************************************************************************
140 * This function is used to get the value of variables which are internally
141 * represented by a float (CONFIG_ITEM_FLOAT).
142 *****************************************************************************/
143 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
145 module_config_t *p_config;
147 p_config = config_FindConfig( p_this, psz_name );
152 msg_Err( p_this, "option %s does not exist", psz_name );
156 if (!IsConfigFloatType (p_config->i_type))
158 msg_Err( p_this, "option %s does not refer to a float", psz_name );
164 vlc_rwlock_rdlock (&config_lock);
165 val = p_config->value.f;
166 vlc_rwlock_unlock (&config_lock);
171 /*****************************************************************************
172 * config_GetPsz: get the string value of a string variable
173 *****************************************************************************
174 * This function is used to get the value of variables which are internally
175 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
176 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
178 * Important note: remember to free() the returned char* because it's a
179 * duplicate of the actual value. It isn't safe to return a pointer to the
180 * actual value as it can be modified at any time.
181 *****************************************************************************/
182 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
184 module_config_t *p_config;
186 p_config = config_FindConfig( p_this, psz_name );
191 msg_Err( p_this, "option %s does not exist", psz_name );
195 if (!IsConfigStringType (p_config->i_type))
197 msg_Err( p_this, "option %s does not refer to a string", psz_name );
201 /* return a copy of the string */
202 vlc_rwlock_rdlock (&config_lock);
203 char *psz_value = strdupnull (p_config->value.psz);
204 vlc_rwlock_unlock (&config_lock);
210 /*****************************************************************************
211 * config_PutPsz: set the string value of a string variable
212 *****************************************************************************
213 * This function is used to set the value of variables which are internally
214 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
215 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
216 *****************************************************************************/
217 void config_PutPsz( vlc_object_t *p_this,
218 const char *psz_name, const char *psz_value )
220 module_config_t *p_config;
222 p_config = config_FindConfig( p_this, psz_name );
228 msg_Warn( p_this, "option %s does not exist", psz_name );
232 if (!IsConfigStringType (p_config->i_type))
234 msg_Err( p_this, "option %s does not refer to a string", psz_name );
239 if ((psz_value != NULL) && *psz_value)
240 str = strdup (psz_value);
244 vlc_rwlock_wrlock (&config_lock);
245 oldstr = (char *)p_config->value.psz;
246 p_config->value.psz = str;
248 vlc_rwlock_unlock (&config_lock);
254 /*****************************************************************************
255 * config_PutInt: set the integer value of an int variable
256 *****************************************************************************
257 * This function is used to set the value of variables which are internally
258 * represented by an integer (CONFIG_ITEM_INTEGER and
260 *****************************************************************************/
261 void config_PutInt( vlc_object_t *p_this, const char *psz_name,
264 module_config_t *p_config;
266 p_config = config_FindConfig( p_this, psz_name );
271 msg_Warn( p_this, "option %s does not exist", psz_name );
275 if (!IsConfigIntegerType (p_config->i_type))
277 msg_Err( p_this, "option %s does not refer to an int", psz_name );
281 if (i_value < p_config->min.i)
282 i_value = p_config->min.i;
283 if (i_value > p_config->max.i)
284 i_value = p_config->max.i;
286 vlc_rwlock_wrlock (&config_lock);
287 p_config->value.i = i_value;
289 vlc_rwlock_unlock (&config_lock);
292 #undef config_PutFloat
293 /*****************************************************************************
294 * config_PutFloat: set the value of a float variable
295 *****************************************************************************
296 * This function is used to set the value of variables which are internally
297 * represented by a float (CONFIG_ITEM_FLOAT).
298 *****************************************************************************/
299 void config_PutFloat( vlc_object_t *p_this,
300 const char *psz_name, float f_value )
302 module_config_t *p_config;
304 p_config = config_FindConfig( p_this, psz_name );
309 msg_Warn( p_this, "option %s does not exist", psz_name );
313 if (!IsConfigFloatType (p_config->i_type))
315 msg_Err( p_this, "option %s does not refer to a float", psz_name );
319 /* if f_min == f_max == 0, then do not use them */
320 if ((p_config->min.f == 0) && (p_config->max.f == 0))
322 else if (f_value < p_config->min.f)
323 f_value = p_config->min.f;
324 else if (f_value > p_config->max.f)
325 f_value = p_config->max.f;
327 vlc_rwlock_wrlock (&config_lock);
328 p_config->value.f = f_value;
330 vlc_rwlock_unlock (&config_lock);
334 * Determines a list of suggested values for an integer configuration item.
335 * \param values pointer to a table of integer values [OUT]
336 * \param texts pointer to a table of descriptions strings [OUT]
337 * \return number of choices, or -1 on error
338 * \note the caller is responsible for calling free() on all descriptions and
339 * on both tables. In case of error, both pointers are set to NULL.
341 ssize_t config_GetIntChoices (vlc_object_t *obj, const char *name,
342 int64_t **restrict values, char ***restrict texts)
347 module_config_t *cfg = config_FindConfig (obj, name);
350 msg_Warn (obj, "option %s does not exist", name);
355 size_t count = cfg->i_list;
359 int64_t *vals = malloc (sizeof (*vals) * count);
360 char **txts = malloc (sizeof (*txts) * count);
361 if (unlikely(vals == NULL || txts == NULL))
364 for (size_t i = 0; i < count; i++)
366 vals[i] = cfg->pi_list[i];
367 txts[i] = strdup (cfg->ppsz_list_text[i]);
368 if (unlikely(txts[i] == NULL))
379 * Determines a list of suggested values for a string configuration item.
380 * \param values pointer to a table of value strings [OUT]
381 * \param texts pointer to a table of descriptions strings [OUT]
382 * \return number of choices, or -1 on error
383 * \note the caller is responsible for calling free() on all values, on all
384 * descriptions and on both tables.
385 * In case of error, both pointers are set to NULL.
387 ssize_t config_GetPszChoices (vlc_object_t *obj, const char *name,
388 char ***restrict values, char ***restrict texts)
390 *values = *texts = NULL;
392 module_config_t *cfg = config_FindConfig (obj, name);
395 msg_Warn (obj, "option %s does not exist", name);
400 if (cfg->pf_update_list != NULL)
402 /* FIXME: not thread-safe */
403 vlc_value_t dummy = { .psz_string = (char *)"" };
404 cfg->pf_update_list (obj, name, dummy, dummy, NULL);
407 size_t count = cfg->i_list;
411 char **vals = malloc (sizeof (*vals) * count);
412 char **txts = malloc (sizeof (*txts) * count);
413 if (unlikely(vals == NULL || txts == NULL))
416 for (size_t i = 0; i < count; i++)
418 vals[i] = strdup (cfg->ppsz_list[i]);
419 txts[i] = strdup (cfg->ppsz_list_text[i]);
420 if (unlikely(vals[i] == NULL || txts[i] == NULL))
429 static int confcmp (const void *a, const void *b)
431 const module_config_t *const *ca = a, *const *cb = b;
433 return strcmp ((*ca)->psz_name, (*cb)->psz_name);
436 static int confnamecmp (const void *key, const void *elem)
438 const module_config_t *const *conf = elem;
440 return strcmp (key, (*conf)->psz_name);
445 module_config_t **list;
447 } config = { NULL, 0 };
450 * Index the configuration items by name for faster lookups.
452 int config_SortConfig (void)
455 module_t **mlist = module_list_get (&nmod);
456 if (unlikely(mlist == NULL))
460 for (size_t i = 0; i < nmod; i++)
461 nconf += mlist[i]->confsize;
463 module_config_t **clist = malloc (sizeof (*clist) * nconf);
464 if (unlikely(clist == NULL))
466 module_list_free (mlist);
471 for (size_t i = 0; i < nmod; i++)
473 module_t *parser = mlist[i];
474 module_config_t *item, *end;
476 for (item = parser->p_config, end = item + parser->confsize;
480 if (!CONFIG_ITEM(item->i_type))
481 continue; /* ignore hints */
482 clist[nconf++] = item;
485 module_list_free (mlist);
487 qsort (clist, nconf, sizeof (*clist), confcmp);
490 config.count = nconf;
494 void config_UnsortConfig (void)
496 module_config_t **clist;
505 /*****************************************************************************
506 * config_FindConfig: find the config structure associated with an option.
507 *****************************************************************************
508 * FIXME: remove p_this pointer parameter (or use it)
509 *****************************************************************************/
510 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
514 if (unlikely(name == NULL))
517 module_config_t *const *p;
518 p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
519 return p ? *p : NULL;
523 * Destroys an array of configuration items.
524 * \param config start of array of items
525 * \param confsize number of items in the array
527 void config_Free (module_config_t *config, size_t confsize)
529 for (size_t j = 0; j < confsize; j++)
531 module_config_t *p_item = config + j;
533 free( p_item->psz_type );
534 free( p_item->psz_name );
535 free( p_item->psz_text );
536 free( p_item->psz_longtext );
538 if (IsConfigStringType (p_item->i_type))
540 free (p_item->value.psz);
541 free (p_item->orig.psz);
544 if( p_item->ppsz_list )
545 for (int i = 0; i < p_item->i_list; i++)
546 free( p_item->ppsz_list[i] );
547 if( p_item->ppsz_list_text )
548 for (int i = 0; i < p_item->i_list; i++)
549 free( p_item->ppsz_list_text[i] );
550 free( p_item->ppsz_list );
551 free( p_item->ppsz_list_text );
552 free( p_item->pi_list );
554 if( p_item->i_action )
556 for (int i = 0; i < p_item->i_action; i++)
557 free( p_item->ppsz_action_text[i] );
558 free( p_item->ppf_action );
559 free( p_item->ppsz_action_text );
566 #undef config_ResetAll
567 /*****************************************************************************
568 * config_ResetAll: reset the configuration data for all the modules.
569 *****************************************************************************/
570 void config_ResetAll( vlc_object_t *p_this )
574 module_t **list = module_list_get (NULL);
576 vlc_rwlock_wrlock (&config_lock);
577 for (size_t j = 0; (p_module = list[j]) != NULL; j++)
578 for (size_t i = 0; i < p_module->confsize; i++ )
580 module_config_t *p_config = p_module->p_config + i;
582 if (IsConfigIntegerType (p_config->i_type))
583 p_config->value.i = p_config->orig.i;
585 if (IsConfigFloatType (p_config->i_type))
586 p_config->value.f = p_config->orig.f;
588 if (IsConfigStringType (p_config->i_type))
590 free ((char *)p_config->value.psz);
591 p_config->value.psz =
592 strdupnull (p_config->orig.psz);
595 vlc_rwlock_unlock (&config_lock);
597 module_list_free (list);