]> git.sesse.net Git - vlc/blob - src/config/core.c
Split file path functions out of vlc_charset.h into vlc_fs.h
[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_configuration.h"
31
32 #include <assert.h>
33
34 #include "configuration.h"
35 #include "modules/modules.h"
36
37 vlc_rwlock_t config_lock;
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, CONFIG_ITEM_FONT
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 #undef config_GetType
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 #undef config_GetInt
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     int val;
164
165     vlc_rwlock_rdlock (&config_lock);
166     val = p_config->value.i;
167     vlc_rwlock_unlock (&config_lock);
168     return val;
169 }
170
171 #undef config_GetFloat
172 /*****************************************************************************
173  * config_GetFloat: get the value of a float variable
174  *****************************************************************************
175  * This function is used to get the value of variables which are internally
176  * represented by a float (CONFIG_ITEM_FLOAT).
177  *****************************************************************************/
178 float config_GetFloat( vlc_object_t *p_this, const char *psz_name )
179 {
180     module_config_t *p_config;
181
182     p_config = config_FindConfig( p_this, psz_name );
183
184     /* sanity checks */
185     if( !p_config )
186     {
187         msg_Err( p_this, "option %s does not exist", psz_name );
188         return -1;
189     }
190
191     if (!IsConfigFloatType (p_config->i_type))
192     {
193         msg_Err( p_this, "option %s does not refer to a float", psz_name );
194         return -1;
195     }
196
197     float val;
198
199     vlc_rwlock_rdlock (&config_lock);
200     val = p_config->value.f;
201     vlc_rwlock_unlock (&config_lock);
202     return val;
203 }
204
205 #undef config_GetPsz
206 /*****************************************************************************
207  * config_GetPsz: get the string value of a string variable
208  *****************************************************************************
209  * This function is used to get the value of variables which are internally
210  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
211  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
212  *
213  * Important note: remember to free() the returned char* because it's a
214  *   duplicate of the actual value. It isn't safe to return a pointer to the
215  *   actual value as it can be modified at any time.
216  *****************************************************************************/
217 char * config_GetPsz( vlc_object_t *p_this, const char *psz_name )
218 {
219     module_config_t *p_config;
220
221     p_config = config_FindConfig( p_this, psz_name );
222
223     /* sanity checks */
224     if( !p_config )
225     {
226         msg_Err( p_this, "option %s does not exist", psz_name );
227         return NULL;
228     }
229
230     if (!IsConfigStringType (p_config->i_type))
231     {
232         msg_Err( p_this, "option %s does not refer to a string", psz_name );
233         return NULL;
234     }
235
236     /* return a copy of the string */
237     vlc_rwlock_rdlock (&config_lock);
238     char *psz_value = strdupnull (p_config->value.psz);
239     vlc_rwlock_unlock (&config_lock);
240
241     return psz_value;
242 }
243
244 #undef config_PutPsz
245 /*****************************************************************************
246  * config_PutPsz: set the string value of a string variable
247  *****************************************************************************
248  * This function is used to set the value of variables which are internally
249  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_FILE,
250  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
251  *****************************************************************************/
252 void config_PutPsz( vlc_object_t *p_this,
253                       const char *psz_name, const char *psz_value )
254 {
255     module_config_t *p_config;
256     vlc_value_t oldval, val;
257
258     p_config = config_FindConfig( p_this, psz_name );
259
260
261     /* sanity checks */
262     if( !p_config )
263     {
264         msg_Warn( p_this, "option %s does not exist", psz_name );
265         return;
266     }
267
268     if (!IsConfigStringType (p_config->i_type))
269     {
270         msg_Err( p_this, "option %s does not refer to a string", psz_name );
271         return;
272     }
273
274     vlc_rwlock_wrlock (&config_lock);
275
276     /* backup old value */
277     oldval.psz_string = (char *)p_config->value.psz;
278
279     if ((psz_value != NULL) && *psz_value)
280         p_config->value.psz = strdup (psz_value);
281     else
282         p_config->value.psz = NULL;
283
284     p_config->b_dirty = true;
285
286     val.psz_string = (char *)p_config->value.psz;
287
288     vlc_rwlock_unlock (&config_lock);
289
290     if( p_config->pf_callback )
291     {
292         p_config->pf_callback( p_this, psz_name, oldval, val,
293                                p_config->p_callback_data );
294     }
295
296     /* free old string */
297     free( oldval.psz_string );
298 }
299
300 #undef config_PutInt
301 /*****************************************************************************
302  * config_PutInt: set the integer value of an int variable
303  *****************************************************************************
304  * This function is used to set the value of variables which are internally
305  * represented by an integer (CONFIG_ITEM_INTEGER and
306  * CONFIG_ITEM_BOOL).
307  *****************************************************************************/
308 void config_PutInt( vlc_object_t *p_this, const char *psz_name, int i_value )
309 {
310     module_config_t *p_config;
311     vlc_value_t oldval;
312
313     p_config = config_FindConfig( p_this, psz_name );
314
315     /* sanity checks */
316     if( !p_config )
317     {
318         msg_Warn( p_this, "option %s does not exist", psz_name );
319         return;
320     }
321
322     if (!IsConfigIntegerType (p_config->i_type))
323     {
324         msg_Err( p_this, "option %s does not refer to an int", psz_name );
325         return;
326     }
327
328     /* if i_min == i_max == 0, then do not use them */
329     if ((p_config->min.i == 0) && (p_config->max.i == 0))
330         ;
331     else if (i_value < p_config->min.i)
332         i_value = p_config->min.i;
333     else if (i_value > p_config->max.i)
334         i_value = p_config->max.i;
335
336     vlc_rwlock_wrlock (&config_lock);
337     /* backup old value */
338     oldval.i_int = p_config->value.i;
339
340     p_config->value.i = i_value;
341     p_config->b_dirty = true;
342     vlc_rwlock_unlock (&config_lock);
343
344     if( p_config->pf_callback )
345     {
346         vlc_value_t val;
347
348         val.i_int = i_value;
349         p_config->pf_callback( p_this, psz_name, oldval, val,
350                                p_config->p_callback_data );
351     }
352 }
353
354 #undef config_PutFloat
355 /*****************************************************************************
356  * config_PutFloat: set the value of a float variable
357  *****************************************************************************
358  * This function is used to set the value of variables which are internally
359  * represented by a float (CONFIG_ITEM_FLOAT).
360  *****************************************************************************/
361 void config_PutFloat( vlc_object_t *p_this,
362                       const char *psz_name, float f_value )
363 {
364     module_config_t *p_config;
365     vlc_value_t oldval;
366
367     p_config = config_FindConfig( p_this, psz_name );
368
369     /* sanity checks */
370     if( !p_config )
371     {
372         msg_Warn( p_this, "option %s does not exist", psz_name );
373         return;
374     }
375
376     if (!IsConfigFloatType (p_config->i_type))
377     {
378         msg_Err( p_this, "option %s does not refer to a float", psz_name );
379         return;
380     }
381
382     /* if f_min == f_max == 0, then do not use them */
383     if ((p_config->min.f == 0) && (p_config->max.f == 0))
384         ;
385     else if (f_value < p_config->min.f)
386         f_value = p_config->min.f;
387     else if (f_value > p_config->max.f)
388         f_value = p_config->max.f;
389
390     vlc_rwlock_wrlock (&config_lock);
391     /* backup old value */
392     oldval.f_float = p_config->value.f;
393
394     p_config->value.f = f_value;
395     p_config->b_dirty = true;
396     vlc_rwlock_unlock (&config_lock);
397
398     if( p_config->pf_callback )
399     {
400         vlc_value_t val;
401
402         val.f_float = f_value;
403         p_config->pf_callback( p_this, psz_name, oldval, val,
404                                p_config->p_callback_data );
405     }
406 }
407
408 static int confcmp (const void *a, const void *b)
409 {
410     const module_config_t *const *ca = a, *const *cb = b;
411
412     return strcmp ((*ca)->psz_name, (*cb)->psz_name);
413 }
414
415 static int confnamecmp (const void *key, const void *elem)
416 {
417     const module_config_t *const *conf = elem;
418
419     return strcmp (key, (*conf)->psz_name);
420 }
421
422 static struct
423 {
424     module_config_t **list;
425     size_t count;
426 } config = { NULL, 0 };
427
428 /**
429  * Index the configuration items by name for faster lookups.
430  */
431 int config_SortConfig (void)
432 {
433     size_t nmod;
434     module_t **mlist = module_list_get (&nmod);
435     if (unlikely(mlist == NULL))
436         return VLC_ENOMEM;
437
438     size_t nconf = 0;
439     for (size_t i = 0; i < nmod; i++)
440          nconf  += mlist[i]->confsize;
441
442     module_config_t **clist = malloc (sizeof (*clist) * nconf);
443     if (unlikely(clist == NULL))
444     {
445         module_list_free (mlist);
446         return VLC_ENOMEM;
447     }
448
449     nconf = 0;
450     for (size_t i = 0; i < nmod; i++)
451     {
452         module_t *parser = mlist[i];
453         module_config_t *item, *end;
454
455         for (item = parser->p_config, end = item + parser->confsize;
456              item < end;
457              item++)
458         {
459             if (item->i_type & CONFIG_HINT)
460                 continue; /* ignore hints */
461             clist[nconf++] = item;
462         }
463     }
464     module_list_free (mlist);
465
466     qsort (clist, nconf, sizeof (*clist), confcmp);
467
468     config.list = clist;
469     config.count = nconf;
470     return VLC_SUCCESS;
471 }
472
473 void config_UnsortConfig (void)
474 {
475     module_config_t **clist;
476
477     clist = config.list;
478     config.list = NULL;
479     config.count = 0;
480
481     free (clist);
482 }
483
484 /*****************************************************************************
485  * config_FindConfig: find the config structure associated with an option.
486  *****************************************************************************
487  * FIXME: remove p_this pointer parameter (or use it)
488  *****************************************************************************/
489 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
490 {
491     VLC_UNUSED(p_this);
492
493     if (unlikely(name == NULL))
494         return NULL;
495
496     module_config_t *const *p;
497     p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
498     return p ? *p : NULL;
499 }
500
501 /*****************************************************************************
502  * config_Free: frees a duplicated module's configuration data.
503  *****************************************************************************
504  * This function frees all the data duplicated by config_Duplicate.
505  *****************************************************************************/
506 void config_Free( module_t *p_module )
507 {
508     int i;
509
510     for (size_t j = 0; j < p_module->confsize; j++)
511     {
512         module_config_t *p_item = p_module->p_config + j;
513
514         free( p_item->psz_type );
515         free( p_item->psz_name );
516         free( p_item->psz_text );
517         free( p_item->psz_longtext );
518         free( p_item->psz_oldname );
519
520         if (IsConfigStringType (p_item->i_type))
521         {
522             free (p_item->value.psz);
523             free (p_item->orig.psz);
524             free (p_item->saved.psz);
525         }
526
527         if( p_item->ppsz_list )
528             for( i = 0; i < p_item->i_list; i++ )
529                 free( p_item->ppsz_list[i] );
530         if( p_item->ppsz_list_text )
531             for( i = 0; i < p_item->i_list; i++ )
532                 free( p_item->ppsz_list_text[i] );
533         free( p_item->ppsz_list );
534         free( p_item->ppsz_list_text );
535         free( p_item->pi_list );
536
537         if( p_item->i_action )
538         {
539             for( i = 0; i < p_item->i_action; i++ )
540             {
541                 free( p_item->ppsz_action_text[i] );
542             }
543             free( p_item->ppf_action );
544             free( p_item->ppsz_action_text );
545         }
546     }
547
548     free (p_module->p_config);
549     p_module->p_config = NULL;
550 }
551
552 #undef config_ResetAll
553 /*****************************************************************************
554  * config_ResetAll: reset the configuration data for all the modules.
555  *****************************************************************************/
556 void config_ResetAll( vlc_object_t *p_this )
557 {
558     VLC_UNUSED(p_this);
559     module_t *p_module;
560     module_t **list = module_list_get (NULL);
561
562     vlc_rwlock_wrlock (&config_lock);
563     for (size_t j = 0; (p_module = list[j]) != NULL; j++)
564     {
565         if( p_module->b_submodule ) continue;
566
567         for (size_t i = 0; i < p_module->confsize; i++ )
568         {
569             module_config_t *p_config = p_module->p_config + i;
570
571             if (IsConfigIntegerType (p_config->i_type))
572                 p_config->value.i = p_config->orig.i;
573             else
574             if (IsConfigFloatType (p_config->i_type))
575                 p_config->value.f = p_config->orig.f;
576             else
577             if (IsConfigStringType (p_config->i_type))
578             {
579                 free ((char *)p_config->value.psz);
580                 p_config->value.psz =
581                         strdupnull (p_config->orig.psz);
582             }
583         }
584     }
585     vlc_rwlock_unlock (&config_lock);
586
587     module_list_free (list);
588 }