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_configuration.h"
34 #include "configuration.h"
35 #include "modules/modules.h"
37 vlc_rwlock_t config_lock;
39 static inline char *strdupnull (const char *src)
41 return src ? strdup (src) : NULL;
44 /* Item types that use a string value (i.e. serialized in the module cache) */
45 int IsConfigStringType (int type)
47 static const unsigned char config_types[] =
49 CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
50 CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
51 CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT, CONFIG_ITEM_FONT
54 /* NOTE: this needs to be changed if we ever get more than 255 types */
55 return memchr (config_types, type, sizeof (config_types)) != NULL;
59 int IsConfigIntegerType (int type)
61 static const unsigned char config_types[] =
63 CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
64 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
67 return memchr (config_types, type, sizeof (config_types)) != NULL;
71 /*****************************************************************************
72 * config_GetType: get the type of a variable (bool, int, float, string)
73 *****************************************************************************
74 * This function is used to get the type of a variable from its name.
75 * Beware, this is quite slow.
76 *****************************************************************************/
77 int config_GetType( vlc_object_t *p_this, const char *psz_name )
79 module_config_t *p_config;
82 p_config = config_FindConfig( p_this, psz_name );
90 switch( p_config->i_type )
92 case CONFIG_ITEM_BOOL:
93 i_type = VLC_VAR_BOOL;
96 case CONFIG_ITEM_INTEGER:
98 i_type = VLC_VAR_INTEGER;
101 case CONFIG_ITEM_FLOAT:
102 i_type = VLC_VAR_FLOAT;
105 case CONFIG_ITEM_MODULE:
106 case CONFIG_ITEM_MODULE_CAT:
107 case CONFIG_ITEM_MODULE_LIST:
108 case CONFIG_ITEM_MODULE_LIST_CAT:
109 i_type = VLC_VAR_MODULE;
112 case CONFIG_ITEM_STRING:
113 i_type = VLC_VAR_STRING;
116 case CONFIG_ITEM_PASSWORD:
117 i_type = VLC_VAR_STRING;
120 case CONFIG_ITEM_FILE:
121 i_type = VLC_VAR_FILE;
124 case CONFIG_ITEM_DIRECTORY:
125 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 int64_t 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 #undef config_GetFloat
172 /*****************************************************************************
173 * config_GetFloat: get the value of a float variable
174 *****************************************************************************
175 * This function is used to get the value of variables which are internally
176 * represented by a float (CONFIG_ITEM_FLOAT).
177 *****************************************************************************/
178 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
180 module_config_t *p_config;
182 p_config = config_FindConfig( p_this, psz_name );
187 msg_Err( p_this, "option %s does not exist", psz_name );
191 if (!IsConfigFloatType (p_config->i_type))
193 msg_Err( p_this, "option %s does not refer to a float", psz_name );
199 vlc_rwlock_rdlock (&config_lock);
200 val = p_config->value.f;
201 vlc_rwlock_unlock (&config_lock);
206 /*****************************************************************************
207 * config_GetPsz: get the string value of a string variable
208 *****************************************************************************
209 * This function is used to get the value of variables which are internally
210 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
211 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
213 * Important note: remember to free() the returned char* because it's a
214 * duplicate of the actual value. It isn't safe to return a pointer to the
215 * actual value as it can be modified at any time.
216 *****************************************************************************/
217 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
219 module_config_t *p_config;
221 p_config = config_FindConfig( p_this, psz_name );
226 msg_Err( p_this, "option %s does not exist", psz_name );
230 if (!IsConfigStringType (p_config->i_type))
232 msg_Err( p_this, "option %s does not refer to a string", psz_name );
236 /* return a copy of the string */
237 vlc_rwlock_rdlock (&config_lock);
238 char *psz_value = strdupnull (p_config->value.psz);
239 vlc_rwlock_unlock (&config_lock);
245 /*****************************************************************************
246 * config_PutPsz: set the string value of a string variable
247 *****************************************************************************
248 * This function is used to set the value of variables which are internally
249 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
250 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
251 *****************************************************************************/
252 void config_PutPsz( vlc_object_t *p_this,
253 const char *psz_name, const char *psz_value )
255 module_config_t *p_config;
258 p_config = config_FindConfig( p_this, psz_name );
264 msg_Warn( p_this, "option %s does not exist", psz_name );
268 if (!IsConfigStringType (p_config->i_type))
270 msg_Err( p_this, "option %s does not refer to a string", psz_name );
275 if ((psz_value != NULL) && *psz_value)
276 str = strdup (psz_value);
280 vlc_rwlock_wrlock (&config_lock);
281 /* backup old value */
282 oldval.psz_string = (char *)p_config->value.psz;
284 p_config->value.psz = str;
285 p_config->b_dirty = true;
286 vlc_rwlock_unlock (&config_lock);
288 if( p_config->pf_callback )
292 val.psz_string = (char *)psz_value;
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,
312 module_config_t *p_config;
315 p_config = config_FindConfig( p_this, psz_name );
320 msg_Warn( p_this, "option %s does not exist", psz_name );
324 if (!IsConfigIntegerType (p_config->i_type))
326 msg_Err( p_this, "option %s does not refer to an int", psz_name );
330 if (i_value < p_config->min.i)
331 i_value = p_config->min.i;
332 if (i_value > p_config->max.i)
333 i_value = p_config->max.i;
335 vlc_rwlock_wrlock (&config_lock);
336 /* backup old value */
337 oldval.i_int = p_config->value.i;
339 p_config->value.i = i_value;
340 p_config->b_dirty = true;
341 vlc_rwlock_unlock (&config_lock);
343 if( p_config->pf_callback )
348 p_config->pf_callback( p_this, psz_name, oldval, val,
349 p_config->p_callback_data );
353 #undef config_PutFloat
354 /*****************************************************************************
355 * config_PutFloat: set the value of a float variable
356 *****************************************************************************
357 * This function is used to set the value of variables which are internally
358 * represented by a float (CONFIG_ITEM_FLOAT).
359 *****************************************************************************/
360 void config_PutFloat( vlc_object_t *p_this,
361 const char *psz_name, float f_value )
363 module_config_t *p_config;
366 p_config = config_FindConfig( p_this, psz_name );
371 msg_Warn( p_this, "option %s does not exist", psz_name );
375 if (!IsConfigFloatType (p_config->i_type))
377 msg_Err( p_this, "option %s does not refer to a float", psz_name );
381 /* if f_min == f_max == 0, then do not use them */
382 if ((p_config->min.f == 0) && (p_config->max.f == 0))
384 else if (f_value < p_config->min.f)
385 f_value = p_config->min.f;
386 else if (f_value > p_config->max.f)
387 f_value = p_config->max.f;
389 vlc_rwlock_wrlock (&config_lock);
390 /* backup old value */
391 oldval.f_float = p_config->value.f;
393 p_config->value.f = f_value;
394 p_config->b_dirty = true;
395 vlc_rwlock_unlock (&config_lock);
397 if( p_config->pf_callback )
401 val.f_float = f_value;
402 p_config->pf_callback( p_this, psz_name, oldval, val,
403 p_config->p_callback_data );
407 static int confcmp (const void *a, const void *b)
409 const module_config_t *const *ca = a, *const *cb = b;
411 return strcmp ((*ca)->psz_name, (*cb)->psz_name);
414 static int confnamecmp (const void *key, const void *elem)
416 const module_config_t *const *conf = elem;
418 return strcmp (key, (*conf)->psz_name);
423 module_config_t **list;
425 } config = { NULL, 0 };
428 * Index the configuration items by name for faster lookups.
430 int config_SortConfig (void)
433 module_t **mlist = module_list_get (&nmod);
434 if (unlikely(mlist == NULL))
438 for (size_t i = 0; i < nmod; i++)
439 nconf += mlist[i]->confsize;
441 module_config_t **clist = malloc (sizeof (*clist) * nconf);
442 if (unlikely(clist == NULL))
444 module_list_free (mlist);
449 for (size_t i = 0; i < nmod; i++)
451 module_t *parser = mlist[i];
452 module_config_t *item, *end;
454 for (item = parser->p_config, end = item + parser->confsize;
458 if (item->i_type & CONFIG_HINT)
459 continue; /* ignore hints */
460 clist[nconf++] = item;
463 module_list_free (mlist);
465 qsort (clist, nconf, sizeof (*clist), confcmp);
468 config.count = nconf;
472 void config_UnsortConfig (void)
474 module_config_t **clist;
483 /*****************************************************************************
484 * config_FindConfig: find the config structure associated with an option.
485 *****************************************************************************
486 * FIXME: remove p_this pointer parameter (or use it)
487 *****************************************************************************/
488 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
492 if (unlikely(name == NULL))
495 module_config_t *const *p;
496 p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
497 return p ? *p : NULL;
500 /*****************************************************************************
501 * config_Free: frees a duplicated module's configuration data.
502 *****************************************************************************
503 * This function frees all the data duplicated by config_Duplicate.
504 *****************************************************************************/
505 void config_Free( module_t *p_module )
509 for (size_t j = 0; j < p_module->confsize; j++)
511 module_config_t *p_item = p_module->p_config + j;
513 free( p_item->psz_type );
514 free( p_item->psz_name );
515 free( p_item->psz_text );
516 free( p_item->psz_longtext );
517 free( p_item->psz_oldname );
519 if (IsConfigStringType (p_item->i_type))
521 free (p_item->value.psz);
522 free (p_item->orig.psz);
523 free (p_item->saved.psz);
526 if( p_item->ppsz_list )
527 for( i = 0; i < p_item->i_list; i++ )
528 free( p_item->ppsz_list[i] );
529 if( p_item->ppsz_list_text )
530 for( i = 0; i < p_item->i_list; i++ )
531 free( p_item->ppsz_list_text[i] );
532 free( p_item->ppsz_list );
533 free( p_item->ppsz_list_text );
534 free( p_item->pi_list );
536 if( p_item->i_action )
538 for( i = 0; i < p_item->i_action; i++ )
540 free( p_item->ppsz_action_text[i] );
542 free( p_item->ppf_action );
543 free( p_item->ppsz_action_text );
547 free (p_module->p_config);
548 p_module->p_config = NULL;
551 #undef config_ResetAll
552 /*****************************************************************************
553 * config_ResetAll: reset the configuration data for all the modules.
554 *****************************************************************************/
555 void config_ResetAll( vlc_object_t *p_this )
559 module_t **list = module_list_get (NULL);
561 vlc_rwlock_wrlock (&config_lock);
562 for (size_t j = 0; (p_module = list[j]) != NULL; j++)
564 if( p_module->b_submodule ) continue;
566 for (size_t i = 0; i < p_module->confsize; i++ )
568 module_config_t *p_config = p_module->p_config + i;
570 if (IsConfigIntegerType (p_config->i_type))
571 p_config->value.i = p_config->orig.i;
573 if (IsConfigFloatType (p_config->i_type))
574 p_config->value.f = p_config->orig.f;
576 if (IsConfigStringType (p_config->i_type))
578 free ((char *)p_config->value.psz);
579 p_config->value.psz =
580 strdupnull (p_config->orig.psz);
584 vlc_rwlock_unlock (&config_lock);
586 module_list_free (list);