]> git.sesse.net Git - vlc/blob - src/config/core.c
Use a global R/W lock for configuration
[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_charset.h"
31 #include "vlc_configuration.h"
32
33 #include <assert.h>
34
35 #include "configuration.h"
36 #include "modules/modules.h"
37
38 vlc_rwlock_t config_lock;
39
40 static inline char *strdupnull (const char *src)
41 {
42     return src ? strdup (src) : NULL;
43 }
44
45 /* Item types that use a string value (i.e. serialized in the module cache) */
46 int IsConfigStringType (int type)
47 {
48     static const unsigned char config_types[] =
49     {
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
53     };
54
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;
57 }
58
59
60 int IsConfigIntegerType (int type)
61 {
62     static const unsigned char config_types[] =
63     {
64         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
65         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
66     };
67
68     return memchr (config_types, type, sizeof (config_types)) != NULL;
69 }
70
71
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 )
79 {
80     module_config_t *p_config;
81     int i_type;
82
83     p_config = config_FindConfig( p_this, psz_name );
84
85     /* sanity checks */
86     if( !p_config )
87     {
88         return 0;
89     }
90
91     switch( p_config->i_type )
92     {
93     case CONFIG_ITEM_BOOL:
94         i_type = VLC_VAR_BOOL;
95         break;
96
97     case CONFIG_ITEM_INTEGER:
98     case CONFIG_ITEM_KEY:
99         i_type = VLC_VAR_INTEGER;
100         break;
101
102     case CONFIG_ITEM_FLOAT:
103         i_type = VLC_VAR_FLOAT;
104         break;
105
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;
111         break;
112
113     case CONFIG_ITEM_STRING:
114         i_type = VLC_VAR_STRING;
115         break;
116
117     case CONFIG_ITEM_PASSWORD:
118         i_type = VLC_VAR_STRING;
119         break;
120
121     case CONFIG_ITEM_FILE:
122         i_type = VLC_VAR_FILE;
123         break;
124
125     case CONFIG_ITEM_DIRECTORY:
126         i_type = VLC_VAR_DIRECTORY;
127         break;
128
129     default:
130         i_type = 0;
131         break;
132     }
133
134     return i_type;
135 }
136
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
142  * CONFIG_ITEM_BOOL).
143  *****************************************************************************/
144 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
145 {
146     module_config_t *p_config;
147
148     p_config = config_FindConfig( p_this, psz_name );
149
150     /* sanity checks */
151     if( !p_config )
152     {
153         msg_Err( p_this, "option %s does not exist", psz_name );
154         return -1;
155     }
156
157     if (!IsConfigIntegerType (p_config->i_type))
158     {
159         msg_Err( p_this, "option %s does not refer to an int", psz_name );
160         return -1;
161     }
162
163     return p_config->value.i;
164 }
165
166 /*****************************************************************************
167  * config_GetFloat: get the value of a float variable
168  *****************************************************************************
169  * This function is used to get the value of variables which are internally
170  * represented by a float (CONFIG_ITEM_FLOAT).
171  *****************************************************************************/
172 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
173 {
174     module_config_t *p_config;
175
176     p_config = config_FindConfig( p_this, psz_name );
177
178     /* sanity checks */
179     if( !p_config )
180     {
181         msg_Err( p_this, "option %s does not exist", psz_name );
182         return -1;
183     }
184
185     if (!IsConfigFloatType (p_config->i_type))
186     {
187         msg_Err( p_this, "option %s does not refer to a float", psz_name );
188         return -1;
189     }
190
191     return p_config->value.f;
192 }
193
194 /*****************************************************************************
195  * config_GetPsz: get the string value of a string variable
196  *****************************************************************************
197  * This function is used to get the value of variables which are internally
198  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
199  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
200  *
201  * Important note: remember to free() the returned char* because it's a
202  *   duplicate of the actual value. It isn't safe to return a pointer to the
203  *   actual value as it can be modified at any time.
204  *****************************************************************************/
205 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
206 {
207     module_config_t *p_config;
208
209     p_config = config_FindConfig( p_this, psz_name );
210
211     /* sanity checks */
212     if( !p_config )
213     {
214         msg_Err( p_this, "option %s does not exist", psz_name );
215         return NULL;
216     }
217
218     if (!IsConfigStringType (p_config->i_type))
219     {
220         msg_Err( p_this, "option %s does not refer to a string", psz_name );
221         return NULL;
222     }
223
224     /* return a copy of the string */
225     vlc_rwlock_rdlock (&config_lock);
226     char *psz_value = strdupnull (p_config->value.psz);
227     vlc_rwlock_unlock (&config_lock);
228
229     return psz_value;
230 }
231
232 /*****************************************************************************
233  * config_PutPsz: set the string value of a string variable
234  *****************************************************************************
235  * This function is used to set the value of variables which are internally
236  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
237  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
238  *****************************************************************************/
239 void __config_PutPsz( vlc_object_t *p_this,
240                       const char *psz_name, const char *psz_value )
241 {
242     module_config_t *p_config;
243     vlc_value_t oldval, val;
244
245     p_config = config_FindConfig( p_this, psz_name );
246
247
248     /* sanity checks */
249     if( !p_config )
250     {
251         msg_Warn( p_this, "option %s does not exist", psz_name );
252         return;
253     }
254
255     if (!IsConfigStringType (p_config->i_type))
256     {
257         msg_Err( p_this, "option %s does not refer to a string", psz_name );
258         return;
259     }
260
261     vlc_rwlock_wrlock (&config_lock);
262
263     /* backup old value */
264     oldval.psz_string = (char *)p_config->value.psz;
265
266     if ((psz_value != NULL) && *psz_value)
267         p_config->value.psz = strdup (psz_value);
268     else
269         p_config->value.psz = NULL;
270
271     p_config->b_dirty = true;
272
273     val.psz_string = (char *)p_config->value.psz;
274
275     vlc_rwlock_unlock (&config_lock);
276
277     if( p_config->pf_callback )
278     {
279         p_config->pf_callback( p_this, psz_name, oldval, val,
280                                p_config->p_callback_data );
281     }
282
283     /* free old string */
284     free( oldval.psz_string );
285 }
286
287 /*****************************************************************************
288  * config_PutInt: set the integer value of an int variable
289  *****************************************************************************
290  * This function is used to set the value of variables which are internally
291  * represented by an integer (CONFIG_ITEM_INTEGER and
292  * CONFIG_ITEM_BOOL).
293  *****************************************************************************/
294 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
295 {
296     module_config_t *p_config;
297     vlc_value_t oldval, val;
298
299     p_config = config_FindConfig( p_this, psz_name );
300
301     /* sanity checks */
302     if( !p_config )
303     {
304         msg_Warn( p_this, "option %s does not exist", psz_name );
305         return;
306     }
307
308     if (!IsConfigIntegerType (p_config->i_type))
309     {
310         msg_Err( p_this, "option %s does not refer to an int", psz_name );
311         return;
312     }
313
314     /* backup old value */
315     oldval.i_int = p_config->value.i;
316
317     /* if i_min == i_max == 0, then do not use them */
318     if ((p_config->min.i == 0) && (p_config->max.i == 0))
319     {
320         p_config->value.i = i_value;
321     }
322     else if (i_value < p_config->min.i)
323     {
324         p_config->value.i = p_config->min.i;
325     }
326     else if (i_value > p_config->max.i)
327     {
328         p_config->value.i = p_config->max.i;
329     }
330     else
331     {
332         p_config->value.i = i_value;
333     }
334
335     p_config->b_dirty = true;
336
337     val.i_int = p_config->value.i;
338
339     if( p_config->pf_callback )
340     {
341         p_config->pf_callback( p_this, psz_name, oldval, val,
342                                p_config->p_callback_data );
343     }
344 }
345
346 /*****************************************************************************
347  * config_PutFloat: set the value of a float variable
348  *****************************************************************************
349  * This function is used to set the value of variables which are internally
350  * represented by a float (CONFIG_ITEM_FLOAT).
351  *****************************************************************************/
352 void __config_PutFloat( vlc_object_t *p_this,
353                         const char *psz_name, float f_value )
354 {
355     module_config_t *p_config;
356     vlc_value_t oldval, val;
357
358     p_config = config_FindConfig( p_this, psz_name );
359
360     /* sanity checks */
361     if( !p_config )
362     {
363         msg_Warn( p_this, "option %s does not exist", psz_name );
364         return;
365     }
366
367     if (!IsConfigFloatType (p_config->i_type))
368     {
369         msg_Err( p_this, "option %s does not refer to a float", psz_name );
370         return;
371     }
372
373     /* backup old value */
374     oldval.f_float = p_config->value.f;
375
376     /* if f_min == f_max == 0, then do not use them */
377     if ((p_config->min.f == 0) && (p_config->max.f == 0))
378     {
379         p_config->value.f = f_value;
380     }
381     else if (f_value < p_config->min.f)
382     {
383         p_config->value.f = p_config->min.f;
384     }
385     else if (f_value > p_config->max.f)
386     {
387         p_config->value.f = p_config->max.f;
388     }
389     else
390     {
391         p_config->value.f = f_value;
392     }
393
394     p_config->b_dirty = true;
395
396     val.f_float = p_config->value.f;
397
398     if( p_config->pf_callback )
399     {
400         p_config->pf_callback( p_this, psz_name, oldval, val,
401                                p_config->p_callback_data );
402     }
403 }
404
405 /*****************************************************************************
406  * config_FindConfig: find the config structure associated with an option.
407  *****************************************************************************
408  * FIXME: This function really needs to be optimized.
409  * FIXME: And now even more.
410  * FIXME: remove p_this pointer parameter (or use it)
411  *****************************************************************************/
412 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
413 {
414     VLC_UNUSED(p_this);
415     module_t *p_parser;
416
417     if( !psz_name ) return NULL;
418
419     module_t **list = module_list_get (NULL);
420     if (list == NULL)
421         return NULL;
422
423     for (size_t i = 0; (p_parser = list[i]) != NULL; i++)
424     {
425         module_config_t *p_item, *p_end;
426
427         if( !p_parser->i_config_items )
428             continue;
429
430         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
431              p_item < p_end;
432              p_item++ )
433         {
434             if( p_item->i_type & CONFIG_HINT )
435                 /* ignore hints */
436                 continue;
437             if( !strcmp( psz_name, p_item->psz_name )
438              || ( p_item->psz_oldname
439               && !strcmp( psz_name, p_item->psz_oldname ) ) )
440             {
441                 module_list_free (list);
442                 return p_item;
443             }
444         }
445     }
446
447     module_list_free (list);
448     return NULL;
449 }
450
451 /*****************************************************************************
452  * config_Free: frees a duplicated module's configuration data.
453  *****************************************************************************
454  * This function frees all the data duplicated by config_Duplicate.
455  *****************************************************************************/
456 void config_Free( module_t *p_module )
457 {
458     int i;
459
460     for (size_t j = 0; j < p_module->confsize; j++)
461     {
462         module_config_t *p_item = p_module->p_config + j;
463
464         free( p_item->psz_type );
465         free( p_item->psz_name );
466         free( p_item->psz_text );
467         free( p_item->psz_longtext );
468         free( p_item->psz_oldname );
469
470         if (IsConfigStringType (p_item->i_type))
471         {
472             free (p_item->value.psz);
473             free (p_item->orig.psz);
474             free (p_item->saved.psz);
475         }
476
477         if( p_item->ppsz_list )
478             for( i = 0; i < p_item->i_list; i++ )
479                 free( p_item->ppsz_list[i] );
480         if( p_item->ppsz_list_text )
481             for( i = 0; i < p_item->i_list; i++ )
482                 free( p_item->ppsz_list_text[i] );
483         free( p_item->ppsz_list );
484         free( p_item->ppsz_list_text );
485         free( p_item->pi_list );
486
487         if( p_item->i_action )
488         {
489             for( i = 0; i < p_item->i_action; i++ )
490             {
491                 free( p_item->ppsz_action_text[i] );
492             }
493             free( p_item->ppf_action );
494             free( p_item->ppsz_action_text );
495         }
496     }
497
498     free (p_module->p_config);
499     p_module->p_config = NULL;
500 }
501
502 /*****************************************************************************
503  * config_ResetAll: reset the configuration data for all the modules.
504  *****************************************************************************/
505 void __config_ResetAll( vlc_object_t *p_this )
506 {
507     VLC_UNUSED(p_this);
508     module_t *p_module;
509     module_t **list = module_list_get (NULL);
510
511     vlc_rwlock_wrlock (&config_lock);
512     for (size_t j = 0; (p_module = list[j]) != NULL; j++)
513     {
514         if( p_module->b_submodule ) continue;
515
516         for (size_t i = 0; i < p_module->confsize; i++ )
517         {
518             module_config_t *p_config = p_module->p_config + i;
519
520             if (IsConfigIntegerType (p_config->i_type))
521                 p_config->value.i = p_config->orig.i;
522             else
523             if (IsConfigFloatType (p_config->i_type))
524                 p_config->value.f = p_config->orig.f;
525             else
526             if (IsConfigStringType (p_config->i_type))
527             {
528                 free ((char *)p_config->value.psz);
529                 p_config->value.psz =
530                         strdupnull (p_config->orig.psz);
531             }
532         }
533     }
534     vlc_rwlock_unlock (&config_lock);
535
536     module_list_free (list);
537 }