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