]> git.sesse.net Git - vlc/blob - src/config/core.c
block: simplify block_FifoRelease
[vlc] / src / config / core.c
1 /*****************************************************************************
2  * core.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #include <vlc_plugin.h>
32
33 #include "vlc_configuration.h"
34
35 #include <errno.h>
36 #include <assert.h>
37
38 #include "configuration.h"
39 #include "modules/modules.h"
40
41 vlc_rwlock_t config_lock = VLC_STATIC_RWLOCK;
42 bool config_dirty = false;
43
44 static inline char *strdupnull (const char *src)
45 {
46     return src ? strdup (src) : NULL;
47 }
48
49 #undef config_GetType
50 /*****************************************************************************
51  * config_GetType: get the type of a variable (bool, int, float, string)
52  *****************************************************************************
53  * This function is used to get the type of a variable from its name.
54  * Beware, this is quite slow.
55  *****************************************************************************/
56 int config_GetType( vlc_object_t *p_this, const char *psz_name )
57 {
58     module_config_t *p_config;
59     int i_type;
60
61     p_config = config_FindConfig( p_this, psz_name );
62
63     /* sanity checks */
64     if( !p_config )
65     {
66         return 0;
67     }
68
69     switch( CONFIG_CLASS(p_config->i_type) )
70     {
71     case CONFIG_ITEM_FLOAT:
72         i_type = VLC_VAR_FLOAT;
73         break;
74
75     case CONFIG_ITEM_INTEGER:
76         i_type = VLC_VAR_INTEGER;
77         break;
78
79     case CONFIG_ITEM_BOOL:
80         i_type = VLC_VAR_BOOL;
81         break;
82
83     case CONFIG_ITEM_STRING:
84         i_type = VLC_VAR_STRING;
85         break;
86
87     default:
88         i_type = 0;
89         break;
90     }
91
92     return i_type;
93 }
94
95 bool config_IsSafe( const char *name )
96 {
97     module_config_t *p_config = config_FindConfig( NULL, name );
98     return p_config != NULL && p_config->b_safe;
99 }
100
101 #undef config_GetInt
102 /*****************************************************************************
103  * config_GetInt: get the value of an int variable
104  *****************************************************************************
105  * This function is used to get the value of variables which are internally
106  * represented by an integer (CONFIG_ITEM_INTEGER and
107  * CONFIG_ITEM_BOOL).
108  *****************************************************************************/
109 int64_t config_GetInt( vlc_object_t *p_this, const char *psz_name )
110 {
111     module_config_t *p_config;
112
113     p_config = config_FindConfig( p_this, psz_name );
114
115     /* sanity checks */
116     if( !p_config )
117     {
118         msg_Err( p_this, "option %s does not exist", psz_name );
119         return -1;
120     }
121
122     if (!IsConfigIntegerType (p_config->i_type))
123     {
124         msg_Err( p_this, "option %s does not refer to an int", psz_name );
125         return -1;
126     }
127
128     int64_t val;
129
130     vlc_rwlock_rdlock (&config_lock);
131     val = p_config->value.i;
132     vlc_rwlock_unlock (&config_lock);
133     return val;
134 }
135
136 #undef config_GetFloat
137 /*****************************************************************************
138  * config_GetFloat: get the value of a float variable
139  *****************************************************************************
140  * This function is used to get the value of variables which are internally
141  * represented by a float (CONFIG_ITEM_FLOAT).
142  *****************************************************************************/
143 float config_GetFloat( 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 (!IsConfigFloatType (p_config->i_type))
157     {
158         msg_Err( p_this, "option %s does not refer to a float", psz_name );
159         return -1;
160     }
161
162     float val;
163
164     vlc_rwlock_rdlock (&config_lock);
165     val = p_config->value.f;
166     vlc_rwlock_unlock (&config_lock);
167     return val;
168 }
169
170 #undef config_GetPsz
171 /*****************************************************************************
172  * config_GetPsz: get the string value of a string variable
173  *****************************************************************************
174  * This function is used to get the value of variables which are internally
175  * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
176  * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
177  *
178  * Important note: remember to free() the returned char* because it's a
179  *   duplicate of the actual value. It isn't safe to return a pointer to the
180  *   actual value as it can be modified at any time.
181  *****************************************************************************/
182 char * config_GetPsz( 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 NULL;
193     }
194
195     if (!IsConfigStringType (p_config->i_type))
196     {
197         msg_Err( p_this, "option %s does not refer to a string", psz_name );
198         return NULL;
199     }
200
201     /* return a copy of the string */
202     vlc_rwlock_rdlock (&config_lock);
203     char *psz_value = strdupnull (p_config->value.psz);
204     vlc_rwlock_unlock (&config_lock);
205
206     return psz_value;
207 }
208
209 #undef config_PutPsz
210 /*****************************************************************************
211  * config_PutPsz: set the string value of a string variable
212  *****************************************************************************
213  * This function is used to set 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 void config_PutPsz( vlc_object_t *p_this,
218                       const char *psz_name, const char *psz_value )
219 {
220     module_config_t *p_config;
221
222     p_config = config_FindConfig( p_this, psz_name );
223
224
225     /* sanity checks */
226     if( !p_config )
227     {
228         msg_Warn( p_this, "option %s does not exist", psz_name );
229         return;
230     }
231
232     if (!IsConfigStringType (p_config->i_type))
233     {
234         msg_Err( p_this, "option %s does not refer to a string", psz_name );
235         return;
236     }
237
238     char *str, *oldstr;
239     if ((psz_value != NULL) && *psz_value)
240         str = strdup (psz_value);
241     else
242         str = NULL;
243
244     vlc_rwlock_wrlock (&config_lock);
245     oldstr = (char *)p_config->value.psz;
246     p_config->value.psz = str;
247     config_dirty = true;
248     vlc_rwlock_unlock (&config_lock);
249
250     free (oldstr);
251 }
252
253 #undef config_PutInt
254 /*****************************************************************************
255  * config_PutInt: set the integer value of an int variable
256  *****************************************************************************
257  * This function is used to set the value of variables which are internally
258  * represented by an integer (CONFIG_ITEM_INTEGER and
259  * CONFIG_ITEM_BOOL).
260  *****************************************************************************/
261 void config_PutInt( vlc_object_t *p_this, const char *psz_name,
262                     int64_t i_value )
263 {
264     module_config_t *p_config;
265
266     p_config = config_FindConfig( p_this, psz_name );
267
268     /* sanity checks */
269     if( !p_config )
270     {
271         msg_Warn( p_this, "option %s does not exist", psz_name );
272         return;
273     }
274
275     if (!IsConfigIntegerType (p_config->i_type))
276     {
277         msg_Err( p_this, "option %s does not refer to an int", psz_name );
278         return;
279     }
280
281     if (i_value < p_config->min.i)
282         i_value = p_config->min.i;
283     if (i_value > p_config->max.i)
284         i_value = p_config->max.i;
285
286     vlc_rwlock_wrlock (&config_lock);
287     p_config->value.i = i_value;
288     config_dirty = true;
289     vlc_rwlock_unlock (&config_lock);
290 }
291
292 #undef config_PutFloat
293 /*****************************************************************************
294  * config_PutFloat: set the value of a float variable
295  *****************************************************************************
296  * This function is used to set the value of variables which are internally
297  * represented by a float (CONFIG_ITEM_FLOAT).
298  *****************************************************************************/
299 void config_PutFloat( vlc_object_t *p_this,
300                       const char *psz_name, float f_value )
301 {
302     module_config_t *p_config;
303
304     p_config = config_FindConfig( p_this, psz_name );
305
306     /* sanity checks */
307     if( !p_config )
308     {
309         msg_Warn( p_this, "option %s does not exist", psz_name );
310         return;
311     }
312
313     if (!IsConfigFloatType (p_config->i_type))
314     {
315         msg_Err( p_this, "option %s does not refer to a float", psz_name );
316         return;
317     }
318
319     /* if f_min == f_max == 0, then do not use them */
320     if ((p_config->min.f == 0) && (p_config->max.f == 0))
321         ;
322     else if (f_value < p_config->min.f)
323         f_value = p_config->min.f;
324     else if (f_value > p_config->max.f)
325         f_value = p_config->max.f;
326
327     vlc_rwlock_wrlock (&config_lock);
328     p_config->value.f = f_value;
329     config_dirty = true;
330     vlc_rwlock_unlock (&config_lock);
331 }
332
333 /**
334  * Determines a list of suggested values for an integer configuration item.
335  * \param values pointer to a table of integer values [OUT]
336  * \param texts pointer to a table of descriptions strings [OUT]
337  * \return number of choices, or -1 on error
338  * \note the caller is responsible for calling free() on all descriptions and
339  * on both tables. In case of error, both pointers are set to NULL.
340  */
341 ssize_t config_GetIntChoices (vlc_object_t *obj, const char *name,
342                              int64_t **restrict values, char ***restrict texts)
343 {
344     *values = NULL;
345     *texts = NULL;
346
347     module_config_t *cfg = config_FindConfig (obj, name);
348     if (cfg == NULL)
349     {
350         msg_Warn (obj, "option %s does not exist", name);
351         errno = ENOENT;
352         return -1;
353     }
354
355     size_t count = cfg->list_count;
356     if (count == 0)
357     {
358         if (cfg->list.i_cb == NULL)
359             return 0;
360         return cfg->list.i_cb(obj, name, values, texts);
361     }
362
363     int64_t *vals = xmalloc (sizeof (*vals) * count);
364     char **txts = xmalloc (sizeof (*txts) * count);
365
366     for (size_t i = 0; i < count; i++)
367     {
368         vals[i] = cfg->list.i[i];
369         /* FIXME: use module_gettext() instead */
370         txts[i] = strdup ((cfg->list_text[i] != NULL)
371                                        ? vlc_gettext (cfg->list_text[i]) : "");
372         if (unlikely(txts[i] == NULL))
373             abort ();
374     }
375
376     *values = vals;
377     *texts = txts;
378     return count;
379 }
380
381
382 static ssize_t config_ListModules (const char *cap, char ***restrict values,
383                                    char ***restrict texts)
384 {
385     module_t **list;
386     ssize_t n = module_list_cap (&list, cap);
387     if (n <= 0)
388     {
389         *values = *texts = NULL;
390         return n;
391     }
392
393     char **vals = xmalloc ((n + 2) * sizeof (*vals));
394     char **txts = xmalloc ((n + 2) * sizeof (*txts));
395
396     vals[0] = xstrdup ("any");
397     txts[0] = xstrdup (_("Automatic"));
398
399     for (ssize_t i = 0; i < n; i++)
400     {
401         vals[i + 1] = xstrdup (module_get_object (list[i]));
402         txts[i + 1] = xstrdup (module_gettext (list[i],
403                                module_get_name (list[i], true)));
404     }
405
406     vals[n + 1] = xstrdup ("none");
407     txts[n + 1] = xstrdup (_("Disable"));
408
409     *values = vals;
410     *texts = txts;
411     return n + 2;
412 }
413
414 /**
415  * Determines a list of suggested values for a string configuration item.
416  * \param values pointer to a table of value strings [OUT]
417  * \param texts pointer to a table of descriptions strings [OUT]
418  * \return number of choices, or -1 on error
419  * \note the caller is responsible for calling free() on all values, on all
420  * descriptions and on both tables.
421  * In case of error, both pointers are set to NULL.
422  */
423 ssize_t config_GetPszChoices (vlc_object_t *obj, const char *name,
424                               char ***restrict values, char ***restrict texts)
425 {
426     *values = *texts = NULL;
427
428     module_config_t *cfg = config_FindConfig (obj, name);
429     if (cfg == NULL)
430     {
431         errno = ENOENT;
432         return -1;
433     }
434
435     switch (cfg->i_type)
436     {
437         case CONFIG_ITEM_MODULE:
438             return config_ListModules (cfg->psz_type, values, texts);
439         default:
440             if (!IsConfigStringType (cfg->i_type))
441             {
442                 errno = EINVAL;
443                 return -1;
444             }
445             break;
446     }
447
448     size_t count = cfg->list_count;
449     if (count == 0)
450     {
451         if (cfg->list.psz_cb == NULL)
452             return 0;
453         return cfg->list.psz_cb(obj, name, values, texts);
454     }
455
456     char **vals = xmalloc (sizeof (*vals) * count);
457     char **txts = xmalloc (sizeof (*txts) * count);
458
459     for (size_t i = 0; i < count; i++)
460     {
461         vals[i] = xstrdup ((cfg->list.psz[i] != NULL) ? cfg->list.psz[i] : "");
462         /* FIXME: use module_gettext() instead */
463         txts[i] = xstrdup ((cfg->list_text[i] != NULL)
464                                        ? vlc_gettext (cfg->list_text[i]) : "");
465     }
466
467     *values = vals;
468     *texts = txts;
469     return count;
470 }
471
472 static int confcmp (const void *a, const void *b)
473 {
474     const module_config_t *const *ca = a, *const *cb = b;
475
476     return strcmp ((*ca)->psz_name, (*cb)->psz_name);
477 }
478
479 static int confnamecmp (const void *key, const void *elem)
480 {
481     const module_config_t *const *conf = elem;
482
483     return strcmp (key, (*conf)->psz_name);
484 }
485
486 static struct
487 {
488     module_config_t **list;
489     size_t count;
490 } config = { NULL, 0 };
491
492 /**
493  * Index the configuration items by name for faster lookups.
494  */
495 int config_SortConfig (void)
496 {
497     size_t nmod, nconf = 0;
498     module_t **mlist = module_list_get (&nmod);
499
500     for (size_t i = 0; i < nmod; i++)
501          nconf  += mlist[i]->confsize;
502
503     module_config_t **clist = malloc (sizeof (*clist) * nconf);
504     if (unlikely(clist == NULL))
505     {
506         module_list_free (mlist);
507         return VLC_ENOMEM;
508     }
509
510     nconf = 0;
511     for (size_t i = 0; i < nmod; i++)
512     {
513         module_t *parser = mlist[i];
514         module_config_t *item, *end;
515
516         for (item = parser->p_config, end = item + parser->confsize;
517              item < end;
518              item++)
519         {
520             if (!CONFIG_ITEM(item->i_type))
521                 continue; /* ignore hints */
522             clist[nconf++] = item;
523         }
524     }
525     module_list_free (mlist);
526
527     qsort (clist, nconf, sizeof (*clist), confcmp);
528
529     config.list = clist;
530     config.count = nconf;
531     return VLC_SUCCESS;
532 }
533
534 void config_UnsortConfig (void)
535 {
536     module_config_t **clist;
537
538     clist = config.list;
539     config.list = NULL;
540     config.count = 0;
541
542     free (clist);
543 }
544
545 /*****************************************************************************
546  * config_FindConfig: find the config structure associated with an option.
547  *****************************************************************************
548  * FIXME: remove p_this pointer parameter (or use it)
549  *****************************************************************************/
550 module_config_t *config_FindConfig (vlc_object_t *p_this, const char *name)
551 {
552     VLC_UNUSED(p_this);
553
554     if (unlikely(name == NULL))
555         return NULL;
556
557     module_config_t *const *p;
558     p = bsearch (name, config.list, config.count, sizeof (*p), confnamecmp);
559     return p ? *p : NULL;
560 }
561
562 /**
563  * Destroys an array of configuration items.
564  * \param config start of array of items
565  * \param confsize number of items in the array
566  */
567 void config_Free (module_config_t *tab, size_t confsize)
568 {
569     for (size_t j = 0; j < confsize; j++)
570     {
571         module_config_t *p_item = &tab[j];
572
573         free( p_item->psz_type );
574         free( p_item->psz_name );
575         free( p_item->psz_text );
576         free( p_item->psz_longtext );
577
578         if (IsConfigIntegerType (p_item->i_type))
579         {
580             if (p_item->list_count)
581                 free (p_item->list.i);
582         }
583         else
584         if (IsConfigStringType (p_item->i_type))
585         {
586             free (p_item->value.psz);
587             free (p_item->orig.psz);
588             if (p_item->list_count)
589             {
590                 for (size_t i = 0; i < p_item->list_count; i++)
591                     free (p_item->list.psz[i]);
592                 free (p_item->list.psz);
593             }
594         }
595
596         for (size_t i = 0; i < p_item->list_count; i++)
597                 free (p_item->list_text[i]);
598         free (p_item->list_text);
599     }
600
601     free (tab);
602 }
603
604 #undef config_ResetAll
605 /*****************************************************************************
606  * config_ResetAll: reset the configuration data for all the modules.
607  *****************************************************************************/
608 void config_ResetAll( vlc_object_t *p_this )
609 {
610     size_t count;
611     module_t **list = module_list_get (&count);
612
613     vlc_rwlock_wrlock (&config_lock);
614     for (size_t j = 0; j < count; j++)
615     {
616         module_t *p_module = list[j];
617
618         for (size_t i = 0; i < p_module->confsize; i++ )
619         {
620             module_config_t *p_config = p_module->p_config + i;
621
622             if (IsConfigIntegerType (p_config->i_type))
623                 p_config->value.i = p_config->orig.i;
624             else
625             if (IsConfigFloatType (p_config->i_type))
626                 p_config->value.f = p_config->orig.f;
627             else
628             if (IsConfigStringType (p_config->i_type))
629             {
630                 free ((char *)p_config->value.psz);
631                 p_config->value.psz =
632                         strdupnull (p_config->orig.psz);
633             }
634         }
635     }
636     vlc_rwlock_unlock (&config_lock);
637
638     module_list_free (list);
639     VLC_UNUSED(p_this);
640 }