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