1 /*****************************************************************************
2 * core.c management of the modules configuration
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_actions.h>
30 #include <vlc_modules.h>
31 #include <vlc_plugin.h>
33 #include "vlc_configuration.h"
38 #include "configuration.h"
39 #include "modules/modules.h"
41 vlc_rwlock_t config_lock
= VLC_STATIC_RWLOCK
;
42 bool config_dirty
= false;
44 static inline char *strdupnull (const char *src
)
46 return src
? strdup (src
) : NULL
;
49 /*****************************************************************************
50 * config_GetType: get the type of a variable (bool, int, float, string)
51 *****************************************************************************
52 * This function is used to get the type of a variable from its name.
53 *****************************************************************************/
54 int config_GetType(const char *psz_name
)
56 module_config_t
*p_config
= config_FindConfig(psz_name
);
64 switch( CONFIG_CLASS(p_config
->i_type
) )
66 case CONFIG_ITEM_FLOAT
:
68 case CONFIG_ITEM_INTEGER
:
69 return VLC_VAR_INTEGER
;
70 case CONFIG_ITEM_BOOL
:
72 case CONFIG_ITEM_STRING
:
73 return VLC_VAR_STRING
;
79 bool config_IsSafe( const char *name
)
81 module_config_t
*p_config
= config_FindConfig( name
);
82 return p_config
!= NULL
&& p_config
->b_safe
;
86 /*****************************************************************************
87 * config_GetInt: get the value of an int variable
88 *****************************************************************************
89 * This function is used to get the value of variables which are internally
90 * represented by an integer (CONFIG_ITEM_INTEGER and
92 *****************************************************************************/
93 int64_t config_GetInt( vlc_object_t
*p_this
, const char *psz_name
)
95 module_config_t
*p_config
= config_FindConfig( psz_name
);
100 msg_Err( p_this
, "option %s does not exist", psz_name
);
104 assert(IsConfigIntegerType(p_config
->i_type
));
108 vlc_rwlock_rdlock (&config_lock
);
109 val
= p_config
->value
.i
;
110 vlc_rwlock_unlock (&config_lock
);
114 #undef config_GetFloat
115 /*****************************************************************************
116 * config_GetFloat: get the value of a float variable
117 *****************************************************************************
118 * This function is used to get the value of variables which are internally
119 * represented by a float (CONFIG_ITEM_FLOAT).
120 *****************************************************************************/
121 float config_GetFloat( vlc_object_t
*p_this
, const char *psz_name
)
123 module_config_t
*p_config
;
125 p_config
= config_FindConfig( psz_name
);
130 msg_Err( p_this
, "option %s does not exist", psz_name
);
134 assert(IsConfigFloatType(p_config
->i_type
));
138 vlc_rwlock_rdlock (&config_lock
);
139 val
= p_config
->value
.f
;
140 vlc_rwlock_unlock (&config_lock
);
145 /*****************************************************************************
146 * config_GetPsz: get the string value of a string variable
147 *****************************************************************************
148 * This function is used to get the value of variables which are internally
149 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
150 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
152 * Important note: remember to free() the returned char* because it's a
153 * duplicate of the actual value. It isn't safe to return a pointer to the
154 * actual value as it can be modified at any time.
155 *****************************************************************************/
156 char * config_GetPsz( vlc_object_t
*p_this
, const char *psz_name
)
158 module_config_t
*p_config
;
160 p_config
= config_FindConfig( psz_name
);
165 msg_Err( p_this
, "option %s does not exist", psz_name
);
169 assert(IsConfigStringType (p_config
->i_type
));
171 /* return a copy of the string */
172 vlc_rwlock_rdlock (&config_lock
);
173 char *psz_value
= strdupnull (p_config
->value
.psz
);
174 vlc_rwlock_unlock (&config_lock
);
180 /*****************************************************************************
181 * config_PutPsz: set the string value of a string variable
182 *****************************************************************************
183 * This function is used to set the value of variables which are internally
184 * represented by a string (CONFIG_ITEM_STRING, CONFIG_ITEM_*FILE,
185 * CONFIG_ITEM_DIRECTORY, CONFIG_ITEM_PASSWORD, and CONFIG_ITEM_MODULE).
186 *****************************************************************************/
187 void config_PutPsz( vlc_object_t
*p_this
,
188 const char *psz_name
, const char *psz_value
)
190 module_config_t
*p_config
= config_FindConfig( psz_name
);
196 msg_Warn( p_this
, "option %s does not exist", psz_name
);
200 assert(IsConfigStringType(p_config
->i_type
));
203 if ((psz_value
!= NULL
) && *psz_value
)
204 str
= strdup (psz_value
);
208 vlc_rwlock_wrlock (&config_lock
);
209 oldstr
= (char *)p_config
->value
.psz
;
210 p_config
->value
.psz
= str
;
212 vlc_rwlock_unlock (&config_lock
);
218 /*****************************************************************************
219 * config_PutInt: set the integer value of an int variable
220 *****************************************************************************
221 * This function is used to set the value of variables which are internally
222 * represented by an integer (CONFIG_ITEM_INTEGER and
224 *****************************************************************************/
225 void config_PutInt( vlc_object_t
*p_this
, const char *psz_name
,
228 module_config_t
*p_config
= config_FindConfig( psz_name
);
233 msg_Warn( p_this
, "option %s does not exist", psz_name
);
237 assert(IsConfigIntegerType(p_config
->i_type
));
239 if (i_value
< p_config
->min
.i
)
240 i_value
= p_config
->min
.i
;
241 if (i_value
> p_config
->max
.i
)
242 i_value
= p_config
->max
.i
;
244 vlc_rwlock_wrlock (&config_lock
);
245 p_config
->value
.i
= i_value
;
247 vlc_rwlock_unlock (&config_lock
);
250 #undef config_PutFloat
251 /*****************************************************************************
252 * config_PutFloat: set the value of a float variable
253 *****************************************************************************
254 * This function is used to set the value of variables which are internally
255 * represented by a float (CONFIG_ITEM_FLOAT).
256 *****************************************************************************/
257 void config_PutFloat( vlc_object_t
*p_this
,
258 const char *psz_name
, float f_value
)
260 module_config_t
*p_config
= config_FindConfig( psz_name
);
265 msg_Warn( p_this
, "option %s does not exist", psz_name
);
269 assert(IsConfigFloatType(p_config
->i_type
));
271 /* if f_min == f_max == 0, then do not use them */
272 if ((p_config
->min
.f
== 0.f
) && (p_config
->max
.f
== 0.f
))
274 else if (f_value
< p_config
->min
.f
)
275 f_value
= p_config
->min
.f
;
276 else if (f_value
> p_config
->max
.f
)
277 f_value
= p_config
->max
.f
;
279 vlc_rwlock_wrlock (&config_lock
);
280 p_config
->value
.f
= f_value
;
282 vlc_rwlock_unlock (&config_lock
);
286 * Determines a list of suggested values for an integer configuration item.
287 * \param values pointer to a table of integer values [OUT]
288 * \param texts pointer to a table of descriptions strings [OUT]
289 * \return number of choices, or -1 on error
290 * \note the caller is responsible for calling free() on all descriptions and
291 * on both tables. In case of error, both pointers are set to NULL.
293 ssize_t
config_GetIntChoices (vlc_object_t
*obj
, const char *name
,
294 int64_t **restrict values
, char ***restrict texts
)
299 module_config_t
*cfg
= config_FindConfig(name
);
302 msg_Warn (obj
, "option %s does not exist", name
);
307 size_t count
= cfg
->list_count
;
310 if (module_Map(obj
, cfg
->owner
))
316 if (cfg
->list
.i_cb
== NULL
)
318 return cfg
->list
.i_cb(obj
, name
, values
, texts
);
321 int64_t *vals
= malloc (sizeof (*vals
) * count
);
322 char **txts
= malloc (sizeof (*txts
) * count
);
323 if (vals
== NULL
|| txts
== NULL
)
329 for (size_t i
= 0; i
< count
; i
++)
331 vals
[i
] = cfg
->list
.i
[i
];
332 /* FIXME: use module_gettext() instead */
333 txts
[i
] = strdup ((cfg
->list_text
[i
] != NULL
)
334 ? vlc_gettext (cfg
->list_text
[i
]) : "");
335 if (unlikely(txts
[i
] == NULL
))
337 for (int j
= i
- 1; j
>= 0; --j
)
355 static ssize_t
config_ListModules (const char *cap
, char ***restrict values
,
356 char ***restrict texts
)
359 ssize_t n
= module_list_cap (&list
, cap
);
362 *values
= *texts
= NULL
;
366 char **vals
= xmalloc ((n
+ 2) * sizeof (*vals
));
367 char **txts
= xmalloc ((n
+ 2) * sizeof (*txts
));
369 vals
[0] = xstrdup ("any");
370 txts
[0] = xstrdup (_("Automatic"));
372 for (ssize_t i
= 0; i
< n
; i
++)
374 vals
[i
+ 1] = xstrdup (module_get_object (list
[i
]));
375 txts
[i
+ 1] = xstrdup (module_gettext (list
[i
],
376 module_get_name (list
[i
], true)));
379 vals
[n
+ 1] = xstrdup ("none");
380 txts
[n
+ 1] = xstrdup (_("Disable"));
384 module_list_free (list
);
389 * Determines a list of suggested values for a string configuration item.
390 * \param values pointer to a table of value strings [OUT]
391 * \param texts pointer to a table of descriptions strings [OUT]
392 * \return number of choices, or -1 on error
393 * \note the caller is responsible for calling free() on all values, on all
394 * descriptions and on both tables.
395 * In case of error, both pointers are set to NULL.
397 ssize_t
config_GetPszChoices (vlc_object_t
*obj
, const char *name
,
398 char ***restrict values
, char ***restrict texts
)
400 *values
= *texts
= NULL
;
402 module_config_t
*cfg
= config_FindConfig(name
);
411 case CONFIG_ITEM_MODULE
:
412 return config_ListModules (cfg
->psz_type
, values
, texts
);
414 if (!IsConfigStringType (cfg
->i_type
))
422 size_t count
= cfg
->list_count
;
425 if (module_Map(obj
, cfg
->owner
))
431 if (cfg
->list
.psz_cb
== NULL
)
433 return cfg
->list
.psz_cb(obj
, name
, values
, texts
);
436 char **vals
= xmalloc (sizeof (*vals
) * count
);
437 char **txts
= xmalloc (sizeof (*txts
) * count
);
439 for (size_t i
= 0; i
< count
; i
++)
441 vals
[i
] = xstrdup ((cfg
->list
.psz
[i
] != NULL
) ? cfg
->list
.psz
[i
] : "");
442 /* FIXME: use module_gettext() instead */
443 txts
[i
] = xstrdup ((cfg
->list_text
[i
] != NULL
)
444 ? vlc_gettext (cfg
->list_text
[i
]) : "");
452 static int confcmp (const void *a
, const void *b
)
454 const module_config_t
*const *ca
= a
, *const *cb
= b
;
456 return strcmp ((*ca
)->psz_name
, (*cb
)->psz_name
);
459 static int confnamecmp (const void *key
, const void *elem
)
461 const module_config_t
*const *conf
= elem
;
463 return strcmp (key
, (*conf
)->psz_name
);
468 module_config_t
**list
;
470 } config
= { NULL
, 0 };
473 * Index the configuration items by name for faster lookups.
475 int config_SortConfig (void)
480 for (p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
481 nconf
+= p
->conf
.size
;
483 module_config_t
**clist
= malloc (sizeof (*clist
) * nconf
);
484 if (unlikely(clist
== NULL
))
488 for (p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
490 module_config_t
*item
, *end
;
492 for (item
= p
->conf
.items
, end
= item
+ p
->conf
.size
;
496 if (!CONFIG_ITEM(item
->i_type
))
497 continue; /* ignore hints */
498 clist
[nconf
++] = item
;
502 qsort (clist
, nconf
, sizeof (*clist
), confcmp
);
505 config
.count
= nconf
;
509 void config_UnsortConfig (void)
511 module_config_t
**clist
;
520 /*****************************************************************************
521 * config_FindConfig: find the config structure associated with an option.
522 *****************************************************************************/
523 module_config_t
*config_FindConfig(const char *name
)
525 if (unlikely(name
== NULL
))
528 module_config_t
*const *p
;
529 p
= bsearch (name
, config
.list
, config
.count
, sizeof (*p
), confnamecmp
);
530 return p
? *p
: NULL
;
534 * Destroys an array of configuration items.
535 * \param config start of array of items
536 * \param confsize number of items in the array
538 void config_Free (module_config_t
*tab
, size_t confsize
)
540 for (size_t j
= 0; j
< confsize
; j
++)
542 module_config_t
*p_item
= &tab
[j
];
544 if (IsConfigStringType (p_item
->i_type
))
546 free (p_item
->value
.psz
);
547 if (p_item
->list_count
)
548 free (p_item
->list
.psz
);
551 free (p_item
->list_text
);
557 #undef config_ResetAll
558 /*****************************************************************************
559 * config_ResetAll: reset the configuration data for all the modules.
560 *****************************************************************************/
561 void config_ResetAll( vlc_object_t
*p_this
)
563 vlc_rwlock_wrlock (&config_lock
);
564 for (vlc_plugin_t
*p
= vlc_plugins
; p
!= NULL
; p
= p
->next
)
566 for (size_t i
= 0; i
< p
->conf
.size
; i
++ )
568 module_config_t
*p_config
= p
->conf
.items
+ i
;
570 if (IsConfigIntegerType (p_config
->i_type
))
571 p_config
->value
.i
= p_config
->orig
.i
;
573 if (IsConfigFloatType (p_config
->i_type
))
574 p_config
->value
.f
= p_config
->orig
.f
;
576 if (IsConfigStringType (p_config
->i_type
))
578 free ((char *)p_config
->value
.psz
);
579 p_config
->value
.psz
=
580 strdupnull (p_config
->orig
.psz
);
584 vlc_rwlock_unlock (&config_lock
);