]> git.sesse.net Git - vlc/blob - src/config/core.c
Merge branch 'df-for-upstream' of git://repo.or.cz/vlc/davidf-public
[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 "../libvlc.h"
30 #include "vlc_keys.h"
31 #include "vlc_charset.h"
32 #include "vlc_configuration.h"
33
34 #include <assert.h>
35
36 #include "configuration.h"
37 #include "modules/modules.h"
38
39 static inline char *strdupnull (const char *src)
40 {
41     return src ? strdup (src) : NULL;
42 }
43
44 /* Item types that use a string value (i.e. serialized in the module cache) */
45 int IsConfigStringType (int type)
46 {
47     static const unsigned char config_types[] =
48     {
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
52     };
53
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;
56 }
57
58
59 int IsConfigIntegerType (int type)
60 {
61     static const unsigned char config_types[] =
62     {
63         CONFIG_ITEM_INTEGER, CONFIG_ITEM_KEY, CONFIG_ITEM_BOOL,
64         CONFIG_CATEGORY, CONFIG_SUBCATEGORY
65     };
66
67     return memchr (config_types, type, sizeof (config_types)) != NULL;
68 }
69
70
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 )
78 {
79     module_config_t *p_config;
80     int i_type;
81
82     p_config = config_FindConfig( p_this, psz_name );
83
84     /* sanity checks */
85     if( !p_config )
86     {
87         return 0;
88     }
89
90     switch( p_config->i_type )
91     {
92     case CONFIG_ITEM_BOOL:
93         i_type = VLC_VAR_BOOL;
94         break;
95
96     case CONFIG_ITEM_INTEGER:
97     case CONFIG_ITEM_KEY:
98         i_type = VLC_VAR_INTEGER;
99         break;
100
101     case CONFIG_ITEM_FLOAT:
102         i_type = VLC_VAR_FLOAT;
103         break;
104
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;
110         break;
111
112     case CONFIG_ITEM_STRING:
113         i_type = VLC_VAR_STRING;
114         break;
115
116     case CONFIG_ITEM_PASSWORD:
117         i_type = VLC_VAR_STRING;
118         break;
119
120     case CONFIG_ITEM_FILE:
121         i_type = VLC_VAR_FILE;
122         break;
123
124     case CONFIG_ITEM_DIRECTORY:
125         i_type = VLC_VAR_DIRECTORY;
126         break;
127
128     default:
129         i_type = 0;
130         break;
131     }
132
133     return i_type;
134 }
135
136 /*****************************************************************************
137  * config_GetInt: get the value of an int variable
138  *****************************************************************************
139  * This function is used to get the value of variables which are internally
140  * represented by an integer (CONFIG_ITEM_INTEGER and
141  * CONFIG_ITEM_BOOL).
142  *****************************************************************************/
143 int __config_GetInt( vlc_object_t *p_this, const char *psz_name )
144 {
145     module_config_t *p_config;
146
147     p_config = config_FindConfig( p_this, psz_name );
148
149     /* sanity checks */
150     if( !p_config )
151     {
152         msg_Err( p_this, "option %s does not exist", psz_name );
153         return -1;
154     }
155
156     if (!IsConfigIntegerType (p_config->i_type))
157     {
158         msg_Err( p_this, "option %s does not refer to an int", psz_name );
159         return -1;
160     }
161
162     return p_config->value.i;
163 }
164
165 /*****************************************************************************
166  * config_GetFloat: get the value of a float variable
167  *****************************************************************************
168  * This function is used to get the value of variables which are internally
169  * represented by a float (CONFIG_ITEM_FLOAT).
170  *****************************************************************************/
171 float __config_GetFloat( vlc_object_t *p_this, const char *psz_name )
172 {
173     module_config_t *p_config;
174
175     p_config = config_FindConfig( p_this, psz_name );
176
177     /* sanity checks */
178     if( !p_config )
179     {
180         msg_Err( p_this, "option %s does not exist", psz_name );
181         return -1;
182     }
183
184     if (!IsConfigFloatType (p_config->i_type))
185     {
186         msg_Err( p_this, "option %s does not refer to a float", psz_name );
187         return -1;
188     }
189
190     return p_config->value.f;
191 }
192
193 /*****************************************************************************
194  * config_GetPsz: get the string value of a string variable
195  *****************************************************************************
196  * This function is used to get the value of variables which are internally
197  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
198  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
199  *
200  * Important note: remember to free() the returned char* because it's a
201  *   duplicate of the actual value. It isn't safe to return a pointer to the
202  *   actual value as it can be modified at any time.
203  *****************************************************************************/
204 char * __config_GetPsz( vlc_object_t *p_this, const char *psz_name )
205 {
206     module_config_t *p_config;
207
208     p_config = config_FindConfig( p_this, psz_name );
209
210     /* sanity checks */
211     if( !p_config )
212     {
213         msg_Err( p_this, "option %s does not exist", psz_name );
214         return NULL;
215     }
216
217     if (!IsConfigStringType (p_config->i_type))
218     {
219         msg_Err( p_this, "option %s does not refer to a string", psz_name );
220         return NULL;
221     }
222
223     /* return a copy of the string */
224     vlc_mutex_lock( p_config->p_lock );
225     char *psz_value = strdupnull (p_config->value.psz);
226     vlc_mutex_unlock( p_config->p_lock );
227
228     return psz_value;
229 }
230
231 /*****************************************************************************
232  * config_PutPsz: set the string value of a string variable
233  *****************************************************************************
234  * This function is used to set the value of variables which are internally
235  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
236  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
237  *****************************************************************************/
238 void __config_PutPsz( vlc_object_t *p_this,
239                       const char *psz_name, const char *psz_value )
240 {
241     module_config_t *p_config;
242     vlc_value_t oldval, val;
243
244     p_config = config_FindConfig( p_this, psz_name );
245
246
247     /* sanity checks */
248     if( !p_config )
249     {
250         msg_Warn( p_this, "option %s does not exist", psz_name );
251         return;
252     }
253
254     if (!IsConfigStringType (p_config->i_type))
255     {
256         msg_Err( p_this, "option %s does not refer to a string", psz_name );
257         return;
258     }
259
260     vlc_mutex_lock( p_config->p_lock );
261
262     /* backup old value */
263     oldval.psz_string = (char *)p_config->value.psz;
264
265     if ((psz_value != NULL) && *psz_value)
266         p_config->value.psz = strdup (psz_value);
267     else
268         p_config->value.psz = NULL;
269
270     p_config->b_dirty = true;
271
272     val.psz_string = (char *)p_config->value.psz;
273
274     vlc_mutex_unlock( p_config->p_lock );
275
276     if( p_config->pf_callback )
277     {
278         p_config->pf_callback( p_this, psz_name, oldval, val,
279                                p_config->p_callback_data );
280     }
281
282     /* free old string */
283     free( oldval.psz_string );
284 }
285
286 /*****************************************************************************
287  * config_PutInt: set the integer value of an int variable
288  *****************************************************************************
289  * This function is used to set the value of variables which are internally
290  * represented by an integer (CONFIG_ITEM_INTEGER and
291  * CONFIG_ITEM_BOOL).
292  *****************************************************************************/
293 void __config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
294 {
295     module_config_t *p_config;
296     vlc_value_t oldval, val;
297
298     p_config = config_FindConfig( p_this, psz_name );
299
300     /* sanity checks */
301     if( !p_config )
302     {
303         msg_Warn( p_this, "option %s does not exist", psz_name );
304         return;
305     }
306
307     if (!IsConfigIntegerType (p_config->i_type))
308     {
309         msg_Err( p_this, "option %s does not refer to an int", psz_name );
310         return;
311     }
312
313     /* backup old value */
314     oldval.i_int = p_config->value.i;
315
316     /* if i_min == i_max == 0, then do not use them */
317     if ((p_config->min.i == 0) && (p_config->max.i == 0))
318     {
319         p_config->value.i = i_value;
320     }
321     else if (i_value < p_config->min.i)
322     {
323         p_config->value.i = p_config->min.i;
324     }
325     else if (i_value > p_config->max.i)
326     {
327         p_config->value.i = p_config->max.i;
328     }
329     else
330     {
331         p_config->value.i = i_value;
332     }
333
334     p_config->b_dirty = true;
335
336     val.i_int = p_config->value.i;
337
338     if( p_config->pf_callback )
339     {
340         p_config->pf_callback( p_this, psz_name, oldval, val,
341                                p_config->p_callback_data );
342     }
343 }
344
345 /*****************************************************************************
346  * config_PutFloat: set the value of a float variable
347  *****************************************************************************
348  * This function is used to set the value of variables which are internally
349  * represented by a float (CONFIG_ITEM_FLOAT).
350  *****************************************************************************/
351 void __config_PutFloat( vlc_object_t *p_this,
352                         const char *psz_name, float f_value )
353 {
354     module_config_t *p_config;
355     vlc_value_t oldval, val;
356
357     p_config = config_FindConfig( p_this, psz_name );
358
359     /* sanity checks */
360     if( !p_config )
361     {
362         msg_Warn( p_this, "option %s does not exist", psz_name );
363         return;
364     }
365
366     if (!IsConfigFloatType (p_config->i_type))
367     {
368         msg_Err( p_this, "option %s does not refer to a float", psz_name );
369         return;
370     }
371
372     /* backup old value */
373     oldval.f_float = p_config->value.f;
374
375     /* if f_min == f_max == 0, then do not use them */
376     if ((p_config->min.f == 0) && (p_config->max.f == 0))
377     {
378         p_config->value.f = f_value;
379     }
380     else if (f_value < p_config->min.f)
381     {
382         p_config->value.f = p_config->min.f;
383     }
384     else if (f_value > p_config->max.f)
385     {
386         p_config->value.f = p_config->max.f;
387     }
388     else
389     {
390         p_config->value.f = f_value;
391     }
392
393     p_config->b_dirty = true;
394
395     val.f_float = p_config->value.f;
396
397     if( p_config->pf_callback )
398     {
399         p_config->pf_callback( p_this, psz_name, oldval, val,
400                                p_config->p_callback_data );
401     }
402 }
403
404 /*****************************************************************************
405  * config_FindConfig: find the config structure associated with an option.
406  *****************************************************************************
407  * FIXME: This function really needs to be optimized.
408  * FIXME: And now even more.
409  *****************************************************************************/
410 module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
411 {
412     vlc_list_t *p_list;
413     int i_index;
414
415     if( !psz_name ) return NULL;
416
417     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
418
419     for( i_index = 0; i_index < p_list->i_count; i_index++ )
420     {
421         module_config_t *p_item, *p_end;
422         module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object;
423
424         if( !p_parser->i_config_items )
425             continue;
426
427         for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
428              p_item < p_end;
429              p_item++ )
430         {
431             if( p_item->i_type & CONFIG_HINT )
432                 /* ignore hints */
433                 continue;
434             if( !strcmp( psz_name, p_item->psz_name )
435              || ( p_item->psz_oldname
436               && !strcmp( psz_name, p_item->psz_oldname ) ) )
437             {
438                 vlc_list_release( p_list );
439                 return p_item;
440             }
441         }
442     }
443
444     vlc_list_release( p_list );
445
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     libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc);
538     int i_index;
539     vlc_list_t *p_list;
540     module_t *p_module;
541
542     /* Acquire config file lock */
543     vlc_mutex_lock( &priv->config_lock );
544
545     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
546
547     for( i_index = 0; i_index < p_list->i_count; i_index++ )
548     {
549         p_module = (module_t *)p_list->p_values[i_index].p_object ;
550         if( p_module->b_submodule ) continue;
551
552         for (size_t i = 0; i < p_module->confsize; i++ )
553         {
554             if (IsConfigIntegerType (p_module->p_config[i].i_type))
555                 p_module->p_config[i].value.i = p_module->p_config[i].orig.i;
556             else
557             if (IsConfigFloatType (p_module->p_config[i].i_type))
558                 p_module->p_config[i].value.f = p_module->p_config[i].orig.f;
559             else
560             if (IsConfigStringType (p_module->p_config[i].i_type))
561             {
562                 free ((char *)p_module->p_config[i].value.psz);
563                 p_module->p_config[i].value.psz =
564                         strdupnull (p_module->p_config[i].orig.psz);
565             }
566         }
567     }
568
569     vlc_list_release( p_list );
570     vlc_mutex_unlock( &priv->config_lock );
571 }