]> git.sesse.net Git - vlc/blob - src/config/core.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / config / core.c
1 /*****************************************************************************
2  * core.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_keys.h>
30 #include <vlc_modules.h>
31
32 #include "vlc_configuration.h"
33
34 #include <assert.h>
35
36 #include "configuration.h"
37 #include "modules/modules.h"
38
39 vlc_rwlock_t config_lock;
40
41 static inline char *strdupnull (const char *src)
42 {
43     return src ? strdup (src) : NULL;
44 }
45
46 /* Item types that use a string value (i.e. serialized in the module cache) */
47 int IsConfigStringType (int type)
48 {
49     static const unsigned char config_types[] =
50     {
51         CONFIG_ITEM_STRING, CONFIG_ITEM_FILE, CONFIG_ITEM_MODULE,
52         CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_MODULE_CAT, CONFIG_ITEM_PASSWORD,
53         CONFIG_ITEM_MODULE_LIST, CONFIG_ITEM_MODULE_LIST_CAT, CONFIG_ITEM_FONT
54     };
55
56     /* NOTE: this needs to be changed if we ever get more than 255 types */
57     return memchr (config_types, type, sizeof (config_types)) != NULL;
58 }
59
60
61 int IsConfigIntegerType (int type)
62 {
63     static const unsigned char config_types[] =
64     {
65         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
66         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
67     };
68
69     return memchr (config_types, type, sizeof (config_types)) != NULL;
70 }
71
72 #undef config_GetType
73 /*****************************************************************************
74  * config_GetType: get the type of a variable (bool, int, float, string)
75  *****************************************************************************
76  * This function is used to get the type of a variable from its name.
77  * Beware, this is quite slow.
78  *****************************************************************************/
79 int config_GetType( vlc_object_t *p_this, const char *psz_name )
80 {
81     module_config_t *p_config;
82     int i_type;
83
84     p_config = config_FindConfig( p_this, psz_name );
85
86     /* sanity checks */
87     if( !p_config )
88     {
89         return 0;
90     }
91
92     switch( p_config->i_type )
93     {
94     case CONFIG_ITEM_BOOL:
95         i_type = VLC_VAR_BOOL;
96         break;
97
98     case CONFIG_ITEM_INTEGER:
99     case CONFIG_ITEM_KEY:
100         i_type = VLC_VAR_INTEGER;
101         break;
102
103     case CONFIG_ITEM_FLOAT:
104         i_type = VLC_VAR_FLOAT;
105         break;
106
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         i_type = VLC_VAR_MODULE;
112         break;
113
114     case CONFIG_ITEM_STRING:
115         i_type = VLC_VAR_STRING;
116         break;
117
118     case CONFIG_ITEM_PASSWORD:
119         i_type = VLC_VAR_STRING;
120         break;
121
122     case CONFIG_ITEM_FILE:
123         i_type = VLC_VAR_FILE;
124         break;
125
126     case CONFIG_ITEM_DIRECTORY:
127         i_type = VLC_VAR_DIRECTORY;
128         break;
129
130     default:
131         i_type = 0;
132         break;
133     }
134
135     return i_type;
136 }
137
138 #undef config_GetInt
139 /*****************************************************************************
140  * config_GetInt: get the value of an int variable
141  *****************************************************************************
142  * This function is used to get the value of variables which are internally
143  * represented by an integer (CONFIG_ITEM_INTEGER and
144  * CONFIG_ITEM_BOOL).
145  *****************************************************************************/
146 int64_t config_GetInt( vlc_object_t *p_this, const char *psz_name )
147 {
148     module_config_t *p_config;
149
150     p_config = config_FindConfig( p_this, psz_name );
151
152     /* sanity checks */
153     if( !p_config )
154     {
155         msg_Err( p_this, "option %s does not exist", psz_name );
156         return -1;
157     }
158
159     if (!IsConfigIntegerType (p_config->i_type))
160     {
161         msg_Err( p_this, "option %s does not refer to an int", psz_name );
162         return -1;
163     }
164
165     int64_t val;
166
167     vlc_rwlock_rdlock (&config_lock);
168     val = p_config->value.i;
169     vlc_rwlock_unlock (&config_lock);
170     return val;
171 }
172
173 #undef config_GetFloat
174 /*****************************************************************************
175  * config_GetFloat: get the value of a float variable
176  *****************************************************************************
177  * This function is used to get the value of variables which are internally
178  * represented by a float (CONFIG_ITEM_FLOAT).
179  *****************************************************************************/
180 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
181 {
182     module_config_t *p_config;
183
184     p_config = config_FindConfig( p_this, psz_name );
185
186     /* sanity checks */
187     if( !p_config )
188     {
189         msg_Err( p_this, "option %s does not exist", psz_name );
190         return -1;
191     }
192
193     if (!IsConfigFloatType (p_config->i_type))
194     {
195         msg_Err( p_this, "option %s does not refer to a float", psz_name );
196         return -1;
197     }
198
199     float val;
200
201     vlc_rwlock_rdlock (&config_lock);
202     val = p_config->value.f;
203     vlc_rwlock_unlock (&config_lock);
204     return val;
205 }
206
207 #undef config_GetPsz
208 /*****************************************************************************
209  * config_GetPsz: get the string value of a string variable
210  *****************************************************************************
211  * This function is used to get the value of variables which are internally
212  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
213  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
214  *
215  * Important note: remember to free() the returned char* because it's a
216  *   duplicate of the actual value. It isn't safe to return a pointer to the
217  *   actual value as it can be modified at any time.
218  *****************************************************************************/
219 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
220 {
221     module_config_t *p_config;
222
223     p_config = config_FindConfig( p_this, psz_name );
224
225     /* sanity checks */
226     if( !p_config )
227     {
228         msg_Err( p_this, "option %s does not exist", psz_name );
229         return NULL;
230     }
231
232     if (!IsConfigStringType (p_config->i_type))
233     {
234         msg_Err( p_this, "option %s does not refer to a string", psz_name );
235         return NULL;
236     }
237
238     /* return a copy of the string */
239     vlc_rwlock_rdlock (&config_lock);
240     char *psz_value = strdupnull (p_config->value.psz);
241     vlc_rwlock_unlock (&config_lock);
242
243     return psz_value;
244 }
245
246 #undef config_PutPsz
247 /*****************************************************************************
248  * config_PutPsz: set the string value of a string variable
249  *****************************************************************************
250  * This function is used to set the value of variables which are internally
251  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
252  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
253  *****************************************************************************/
254 void config_PutPsz( vlc_object_t *p_this,
255                       const char *psz_name, const char *psz_value )
256 {
257     module_config_t *p_config;
258     vlc_value_t oldval;
259
260     p_config = config_FindConfig( p_this, psz_name );
261
262
263     /* sanity checks */
264     if( !p_config )
265     {
266         msg_Warn( p_this, "option %s does not exist", psz_name );
267         return;
268     }
269
270     if (!IsConfigStringType (p_config->i_type))
271     {
272         msg_Err( p_this, "option %s does not refer to a string", psz_name );
273         return;
274     }
275
276     char *str;
277     if ((psz_value != NULL) && *psz_value)
278         str = strdup (psz_value);
279     else
280         str = NULL;
281
282     vlc_rwlock_wrlock (&config_lock);
283     /* backup old value */
284     oldval.psz_string = (char *)p_config->value.psz;
285
286     p_config->value.psz = str;
287     p_config->b_dirty = true;
288     vlc_rwlock_unlock (&config_lock);
289
290     if( p_config->pf_callback )
291     {
292         vlc_value_t val;
293
294         val.psz_string = (char *)psz_value;
295         p_config->pf_callback( p_this, psz_name, oldval, val,
296                                p_config->p_callback_data );
297     }
298
299     /* free old string */
300     free( oldval.psz_string );
301 }
302
303 #undef config_PutInt
304 /*****************************************************************************
305  * config_PutInt: set the integer value of an int variable
306  *****************************************************************************
307  * This function is used to set the value of variables which are internally
308  * represented by an integer (CONFIG_ITEM_INTEGER and
309  * CONFIG_ITEM_BOOL).
310  *****************************************************************************/
311 void config_PutInt( vlc_object_t *p_this, const char *psz_name,
312                     int64_t i_value )
313 {
314     module_config_t *p_config;
315     vlc_value_t oldval;
316
317     p_config = config_FindConfig( p_this, psz_name );
318
319     /* sanity checks */
320     if( !p_config )
321     {
322         msg_Warn( p_this, "option %s does not exist", psz_name );
323         return;
324     }
325
326     if (!IsConfigIntegerType (p_config->i_type))
327     {
328         msg_Err( p_this, "option %s does not refer to an int", psz_name );
329         return;
330     }
331
332     if (i_value < p_config->min.i)
333         i_value = p_config->min.i;
334     if (i_value > p_config->max.i)
335         i_value = p_config->max.i;
336
337     vlc_rwlock_wrlock (&config_lock);
338     /* backup old value */
339     oldval.i_int = p_config->value.i;
340
341     p_config->value.i = i_value;
342     p_config->b_dirty = true;
343     vlc_rwlock_unlock (&config_lock);
344
345     if( p_config->pf_callback )
346     {
347         vlc_value_t val;
348
349         val.i_int = i_value;
350         p_config->pf_callback( p_this, psz_name, oldval, val,
351                                p_config->p_callback_data );
352     }
353 }
354
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 )
364 {
365     module_config_t *p_config;
366     vlc_value_t oldval;
367
368     p_config = config_FindConfig( p_this, psz_name );
369
370     /* sanity checks */
371     if( !p_config )
372     {
373         msg_Warn( p_this, "option %s does not exist", psz_name );
374         return;
375     }
376
377     if (!IsConfigFloatType (p_config->i_type))
378     {
379         msg_Err( p_this, "option %s does not refer to a float", psz_name );
380         return;
381     }
382
383     /* if f_min == f_max == 0, then do not use them */
384     if ((p_config->min.f == 0) && (p_config->max.f == 0))
385         ;
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;
390
391     vlc_rwlock_wrlock (&config_lock);
392     /* backup old value */
393     oldval.f_float = p_config->value.f;
394
395     p_config->value.f = f_value;
396     p_config->b_dirty = true;
397     vlc_rwlock_unlock (&config_lock);
398
399     if( p_config->pf_callback )
400     {
401         vlc_value_t val;
402
403         val.f_float = f_value;
404         p_config->pf_callback( p_this, psz_name, oldval, val,
405                                p_config->p_callback_data );
406     }
407 }
408
409 static int confcmp (const void *a, const void *b)
410 {
411     const module_config_t *const *ca = a, *const *cb = b;
412
413     return strcmp ((*ca)->psz_name, (*cb)->psz_name);
414 }
415
416 static int confnamecmp (const void *key, const void *elem)
417 {
418     const module_config_t *const *conf = elem;
419
420     return strcmp (key, (*conf)->psz_name);
421 }
422
423 static struct
424 {
425     module_config_t **list;
426     size_t count;
427 } config = { NULL, 0 };
428
429 /**
430  * Index the configuration items by name for faster lookups.
431  */
432 int config_SortConfig (void)
433 {
434     size_t nmod;
435     module_t **mlist = module_list_get (&nmod);
436     if (unlikely(mlist == NULL))
437         return VLC_ENOMEM;
438
439     size_t nconf = 0;
440     for (size_t i = 0; i < nmod; i++)
441          nconf  += mlist[i]->confsize;
442
443     module_config_t **clist = malloc (sizeof (*clist) * nconf);
444     if (unlikely(clist == NULL))
445     {
446         module_list_free (mlist);
447         return VLC_ENOMEM;
448     }
449
450     nconf = 0;
451     for (size_t i = 0; i < nmod; i++)
452     {
453         module_t *parser = mlist[i];
454         module_config_t *item, *end;
455
456         for (item = parser->p_config, end = item + parser->confsize;
457              item < end;
458              item++)
459         {
460             if (item->i_type & CONFIG_HINT)
461                 continue; /* ignore hints */
462             clist[nconf++] = item;
463         }
464     }
465     module_list_free (mlist);
466
467     qsort (clist, nconf, sizeof (*clist), confcmp);
468
469     config.list = clist;
470     config.count = nconf;
471     return VLC_SUCCESS;
472 }
473
474 void config_UnsortConfig (void)
475 {
476     module_config_t **clist;
477
478     clist = config.list;
479     config.list = NULL;
480     config.count = 0;
481
482     free (clist);
483 }
484
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)
491 {
492     VLC_UNUSED(p_this);
493
494     if (unlikely(name == NULL))
495         return NULL;
496
497     module_config_t *const *p;
498     p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
499     return p ? *p : NULL;
500 }
501
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 )
508 {
509     int i;
510
511     for (size_t j = 0; j < p_module->confsize; j++)
512     {
513         module_config_t *p_item = p_module->p_config + j;
514
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 );
520
521         if (IsConfigStringType (p_item->i_type))
522         {
523             free (p_item->value.psz);
524             free (p_item->orig.psz);
525             free (p_item->saved.psz);
526         }
527
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 );
537
538         if( p_item->i_action )
539         {
540             for( i = 0; i < p_item->i_action; i++ )
541             {
542                 free( p_item->ppsz_action_text[i] );
543             }
544             free( p_item->ppf_action );
545             free( p_item->ppsz_action_text );
546         }
547     }
548
549     free (p_module->p_config);
550     p_module->p_config = NULL;
551 }
552
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 )
558 {
559     VLC_UNUSED(p_this);
560     module_t *p_module;
561     module_t **list = module_list_get (NULL);
562
563     vlc_rwlock_wrlock (&config_lock);
564     for (size_t j = 0; (p_module = list[j]) != NULL; j++)
565     {
566         if( p_module->b_submodule ) continue;
567
568         for (size_t i = 0; i < p_module->confsize; i++ )
569         {
570             module_config_t *p_config = p_module->p_config + i;
571
572             if (IsConfigIntegerType (p_config->i_type))
573                 p_config->value.i = p_config->orig.i;
574             else
575             if (IsConfigFloatType (p_config->i_type))
576                 p_config->value.f = p_config->orig.f;
577             else
578             if (IsConfigStringType (p_config->i_type))
579             {
580                 free ((char *)p_config->value.psz);
581                 p_config->value.psz =
582                         strdupnull (p_config->orig.psz);
583             }
584         }
585     }
586     vlc_rwlock_unlock (&config_lock);
587
588     module_list_free (list);
589 }