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_modules.h>
32 #include "vlc_configuration.h"
36 #include "configuration.h"
37 #include "modules/modules.h"
39 vlc_rwlock_t config_lock;
41 static inline char *strdupnull (const char *src)
43 return src ? strdup (src) : NULL;
46 /* Item types that use a string value (i.e. serialized in the module cache) */
47 int IsConfigStringType (int type)
49 static const unsigned char config_types[] =
51 CONFIG_ITEM_STRING, CONFIG_ITEM_MODULE, CONFIG_ITEM_DIRECTORY,
52 CONFIG_ITEM_KEY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
53 CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT,
54 CONFIG_ITEM_FONT, CONFIG_ITEM_LOADFILE, CONFIG_ITEM_SAVEFILE,
57 /* NOTE: this needs to be changed if we ever get more than 255 types */
58 return memchr (config_types, type, sizeof (config_types)) != NULL;
62 int IsConfigIntegerType (int type)
64 static const unsigned char config_types[] =
66 CONFIG_ITEM_INTEGER, CONFIG_ITEM_BOOL,
67 CONFIG_CATEGORY, CONFIG_SUBCATEGORY
70 return memchr (config_types, type, sizeof (config_types)) != NULL;
74 /*****************************************************************************
75 * config_GetType: get the type of a variable (bool, int, float, string)
76 *****************************************************************************
77 * This function is used to get the type of a variable from its name.
78 * Beware, this is quite slow.
79 *****************************************************************************/
80 int config_GetType( vlc_object_t *p_this, const char *psz_name )
82 module_config_t *p_config;
85 p_config = config_FindConfig( p_this, psz_name );
93 switch( p_config->i_type )
95 case CONFIG_ITEM_BOOL:
96 i_type = VLC_VAR_BOOL;
99 case CONFIG_ITEM_INTEGER:
100 i_type = VLC_VAR_INTEGER;
103 case CONFIG_ITEM_FLOAT:
104 i_type = VLC_VAR_FLOAT;
107 case CONFIG_ITEM_MODULE:
108 case CONFIG_ITEM_MODULE_CAT:
109 case CONFIG_ITEM_MODULE_LIST:
110 case CONFIG_ITEM_MODULE_LIST_CAT:
111 case CONFIG_ITEM_STRING:
112 case CONFIG_ITEM_PASSWORD:
113 case CONFIG_ITEM_LOADFILE:
114 case CONFIG_ITEM_SAVEFILE:
115 case CONFIG_ITEM_DIRECTORY:
116 case CONFIG_ITEM_KEY:
117 i_type = VLC_VAR_STRING;
129 /*****************************************************************************
130 * config_GetInt: get the value of an int variable
131 *****************************************************************************
132 * This function is used to get the value of variables which are internally
133 * represented by an integer (CONFIG_ITEM_INTEGER and
135 *****************************************************************************/
136 int64_t config_GetInt( vlc_object_t *p_this, const char *psz_name )
138 module_config_t *p_config;
140 p_config = config_FindConfig( p_this, psz_name );
145 msg_Err( p_this, "option %s does not exist", psz_name );
149 if (!IsConfigIntegerType (p_config->i_type))
151 msg_Err( p_this, "option %s does not refer to an int", psz_name );
157 vlc_rwlock_rdlock (&config_lock);
158 val = p_config->value.i;
159 vlc_rwlock_unlock (&config_lock);
163 #undef config_GetFloat
164 /*****************************************************************************
165 * config_GetFloat: get the value of a float variable
166 *****************************************************************************
167 * This function is used to get the value of variables which are internally
168 * represented by a float (CONFIG_ITEM_FLOAT).
169 *****************************************************************************/
170 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
172 module_config_t *p_config;
174 p_config = config_FindConfig( p_this, psz_name );
179 msg_Err( p_this, "option %s does not exist", psz_name );
183 if (!IsConfigFloatType (p_config->i_type))
185 msg_Err( p_this, "option %s does not refer to a float", psz_name );
191 vlc_rwlock_rdlock (&config_lock);
192 val = p_config->value.f;
193 vlc_rwlock_unlock (&config_lock);
198 /*****************************************************************************
199 * config_GetPsz: get the string value of a string variable
200 *****************************************************************************
201 * This function is used to get the value of variables which are internally
202 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
203 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
205 * Important note: remember to free() the returned char* because it's a
206 * duplicate of the actual value. It isn't safe to return a pointer to the
207 * actual value as it can be modified at any time.
208 *****************************************************************************/
209 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
211 module_config_t *p_config;
213 p_config = config_FindConfig( p_this, psz_name );
218 msg_Err( p_this, "option %s does not exist", psz_name );
222 if (!IsConfigStringType (p_config->i_type))
224 msg_Err( p_this, "option %s does not refer to a string", psz_name );
228 /* return a copy of the string */
229 vlc_rwlock_rdlock (&config_lock);
230 char *psz_value = strdupnull (p_config->value.psz);
231 vlc_rwlock_unlock (&config_lock);
237 /*****************************************************************************
238 * config_PutPsz: set the string value of a string variable
239 *****************************************************************************
240 * This function is used to set the value of variables which are internally
241 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
242 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
243 *****************************************************************************/
244 void config_PutPsz( vlc_object_t *p_this,
245 const char *psz_name, const char *psz_value )
247 module_config_t *p_config;
249 p_config = config_FindConfig( p_this, psz_name );
255 msg_Warn( p_this, "option %s does not exist", psz_name );
259 if (!IsConfigStringType (p_config->i_type))
261 msg_Err( p_this, "option %s does not refer to a string", psz_name );
266 if ((psz_value != NULL) && *psz_value)
267 str = strdup (psz_value);
271 vlc_rwlock_wrlock (&config_lock);
272 oldstr = (char *)p_config->value.psz;
273 p_config->value.psz = str;
274 p_config->b_dirty = true;
275 vlc_rwlock_unlock (&config_lock);
281 /*****************************************************************************
282 * config_PutInt: set the integer value of an int variable
283 *****************************************************************************
284 * This function is used to set the value of variables which are internally
285 * represented by an integer (CONFIG_ITEM_INTEGER and
287 *****************************************************************************/
288 void config_PutInt( vlc_object_t *p_this, const char *psz_name,
291 module_config_t *p_config;
293 p_config = config_FindConfig( p_this, psz_name );
298 msg_Warn( p_this, "option %s does not exist", psz_name );
302 if (!IsConfigIntegerType (p_config->i_type))
304 msg_Err( p_this, "option %s does not refer to an int", psz_name );
308 if (i_value < p_config->min.i)
309 i_value = p_config->min.i;
310 if (i_value > p_config->max.i)
311 i_value = p_config->max.i;
313 vlc_rwlock_wrlock (&config_lock);
314 p_config->value.i = i_value;
315 p_config->b_dirty = true;
316 vlc_rwlock_unlock (&config_lock);
319 #undef config_PutFloat
320 /*****************************************************************************
321 * config_PutFloat: set the value of a float variable
322 *****************************************************************************
323 * This function is used to set the value of variables which are internally
324 * represented by a float (CONFIG_ITEM_FLOAT).
325 *****************************************************************************/
326 void config_PutFloat( vlc_object_t *p_this,
327 const char *psz_name, float f_value )
329 module_config_t *p_config;
331 p_config = config_FindConfig( p_this, psz_name );
336 msg_Warn( p_this, "option %s does not exist", psz_name );
340 if (!IsConfigFloatType (p_config->i_type))
342 msg_Err( p_this, "option %s does not refer to a float", psz_name );
346 /* if f_min == f_max == 0, then do not use them */
347 if ((p_config->min.f == 0) && (p_config->max.f == 0))
349 else if (f_value < p_config->min.f)
350 f_value = p_config->min.f;
351 else if (f_value > p_config->max.f)
352 f_value = p_config->max.f;
354 vlc_rwlock_wrlock (&config_lock);
355 p_config->value.f = f_value;
356 p_config->b_dirty = true;
357 vlc_rwlock_unlock (&config_lock);
360 static int confcmp (const void *a, const void *b)
362 const module_config_t *const *ca = a, *const *cb = b;
364 return strcmp ((*ca)->psz_name, (*cb)->psz_name);
367 static int confnamecmp (const void *key, const void *elem)
369 const module_config_t *const *conf = elem;
371 return strcmp (key, (*conf)->psz_name);
376 module_config_t **list;
378 } config = { NULL, 0 };
381 * Index the configuration items by name for faster lookups.
383 int config_SortConfig (void)
386 module_t **mlist = module_list_get (&nmod);
387 if (unlikely(mlist == NULL))
391 for (size_t i = 0; i < nmod; i++)
392 nconf += mlist[i]->confsize;
394 module_config_t **clist = malloc (sizeof (*clist) * nconf);
395 if (unlikely(clist == NULL))
397 module_list_free (mlist);
402 for (size_t i = 0; i < nmod; i++)
404 module_t *parser = mlist[i];
405 module_config_t *item, *end;
407 for (item = parser->p_config, end = item + parser->confsize;
411 if (item->i_type & CONFIG_HINT)
412 continue; /* ignore hints */
413 clist[nconf++] = item;
416 module_list_free (mlist);
418 qsort (clist, nconf, sizeof (*clist), confcmp);
421 config.count = nconf;
425 void config_UnsortConfig (void)
427 module_config_t **clist;
436 /*****************************************************************************
437 * config_FindConfig: find the config structure associated with an option.
438 *****************************************************************************
439 * FIXME: remove p_this pointer parameter (or use it)
440 *****************************************************************************/
441 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
445 if (unlikely(name == NULL))
448 module_config_t *const *p;
449 p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
450 return p ? *p : NULL;
453 /*****************************************************************************
454 * config_Free: frees a duplicated module's configuration data.
455 *****************************************************************************
456 * This function frees all the data duplicated by config_Duplicate.
457 *****************************************************************************/
458 void config_Free( module_t *p_module )
462 for (size_t j = 0; j < p_module->confsize; j++)
464 module_config_t *p_item = p_module->p_config + j;
466 free( p_item->psz_type );
467 free( p_item->psz_name );
468 free( p_item->psz_text );
469 free( p_item->psz_longtext );
470 free( p_item->psz_oldname );
472 if (IsConfigStringType (p_item->i_type))
474 free (p_item->value.psz);
475 free (p_item->orig.psz);
478 if( p_item->ppsz_list )
479 for( i = 0; i < p_item->i_list; i++ )
480 free( p_item->ppsz_list[i] );
481 if( p_item->ppsz_list_text )
482 for( i = 0; i < p_item->i_list; i++ )
483 free( p_item->ppsz_list_text[i] );
484 free( p_item->ppsz_list );
485 free( p_item->ppsz_list_text );
486 free( p_item->pi_list );
488 if( p_item->i_action )
490 for( i = 0; i < p_item->i_action; i++ )
492 free( p_item->ppsz_action_text[i] );
494 free( p_item->ppf_action );
495 free( p_item->ppsz_action_text );
499 free (p_module->p_config);
500 p_module->p_config = NULL;
503 #undef config_ResetAll
504 /*****************************************************************************
505 * config_ResetAll: reset the configuration data for all the modules.
506 *****************************************************************************/
507 void config_ResetAll( vlc_object_t *p_this )
511 module_t **list = module_list_get (NULL);
513 vlc_rwlock_wrlock (&config_lock);
514 for (size_t j = 0; (p_module = list[j]) != NULL; j++)
516 if( p_module->b_submodule ) continue;
518 for (size_t i = 0; i < p_module->confsize; i++ )
520 module_config_t *p_config = p_module->p_config + i;
522 if (IsConfigIntegerType (p_config->i_type))
523 p_config->value.i = p_config->orig.i;
525 if (IsConfigFloatType (p_config->i_type))
526 p_config->value.f = p_config->orig.f;
528 if (IsConfigStringType (p_config->i_type))
530 free ((char *)p_config->value.psz);
531 p_config->value.psz =
532 strdupnull (p_config->orig.psz);
536 vlc_rwlock_unlock (&config_lock);
538 module_list_free (list);