| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 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_charset.h" |
|---|
| 31 |
#include "vlc_keys.h" |
|---|
| 32 |
|
|---|
| 33 |
#include <errno.h> |
|---|
| 34 |
#include <assert.h> |
|---|
| 35 |
#include <limits.h> |
|---|
| 36 |
#ifdef __APPLE__ |
|---|
| 37 |
# include <xlocale.h> |
|---|
| 38 |
#else |
|---|
| 39 |
#include <locale.h> |
|---|
| 40 |
#endif |
|---|
| 41 |
|
|---|
| 42 |
#include "configuration.h" |
|---|
| 43 |
#include "modules/modules.h" |
|---|
| 44 |
|
|---|
| 45 |
static char *ConfigKeyToString( int ); |
|---|
| 46 |
|
|---|
| 47 |
static inline char *strdupnull (const char *src) |
|---|
| 48 |
{ |
|---|
| 49 |
return src ? strdup (src) : NULL; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
static char *config_GetConfigFile( void ) |
|---|
| 56 |
{ |
|---|
| 57 |
char *psz_dir = config_GetUserConfDir(); |
|---|
| 58 |
char *psz_configfile; |
|---|
| 59 |
|
|---|
| 60 |
if( asprintf( &psz_configfile, "%s" DIR_SEP CONFIG_FILE, psz_dir ) == -1 ) |
|---|
| 61 |
psz_configfile = NULL; |
|---|
| 62 |
free( psz_dir ); |
|---|
| 63 |
return psz_configfile; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
static FILE *config_OpenConfigFile( vlc_object_t *p_obj, const char *mode ) |
|---|
| 67 |
{ |
|---|
| 68 |
char *psz_filename = libvlc_priv (p_obj->p_libvlc)->psz_configfile; |
|---|
| 69 |
FILE *p_stream; |
|---|
| 70 |
|
|---|
| 71 |
if( !psz_filename ) |
|---|
| 72 |
{ |
|---|
| 73 |
psz_filename = config_GetConfigFile(); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
msg_Dbg( p_obj, "opening config file (%s)", psz_filename ); |
|---|
| 77 |
|
|---|
| 78 |
p_stream = utf8_fopen( psz_filename, mode ); |
|---|
| 79 |
if( p_stream == NULL && errno != ENOENT ) |
|---|
| 80 |
{ |
|---|
| 81 |
msg_Err( p_obj, "cannot open config file (%s): %m", |
|---|
| 82 |
psz_filename ); |
|---|
| 83 |
|
|---|
| 84 |
} |
|---|
| 85 |
#if !( defined(WIN32) || defined(__APPLE__) || defined(SYS_BEOS) ) |
|---|
| 86 |
else if( p_stream == NULL && errno == ENOENT && mode[0] == 'r' ) |
|---|
| 87 |
{ |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
char *psz_old; |
|---|
| 91 |
if( asprintf( &psz_old, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE, |
|---|
| 92 |
config_GetHomeDir() ) != -1 ) |
|---|
| 93 |
{ |
|---|
| 94 |
p_stream = utf8_fopen( psz_old, mode ); |
|---|
| 95 |
if( p_stream ) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
msg_Info( p_obj->p_libvlc, "Found old config file at %s. " |
|---|
| 100 |
"VLC will now use %s.", psz_old, psz_filename ); |
|---|
| 101 |
char *psz_readme; |
|---|
| 102 |
if( asprintf(&psz_readme,"%s"DIR_SEP CONFIG_DIR DIR_SEP"README", |
|---|
| 103 |
config_GetHomeDir() ) != -1 ) |
|---|
| 104 |
{ |
|---|
| 105 |
FILE *p_readme = utf8_fopen( psz_readme, "wt" ); |
|---|
| 106 |
if( p_readme ) |
|---|
| 107 |
{ |
|---|
| 108 |
fprintf( p_readme, "The VLC media player " |
|---|
| 109 |
"configuration folder has moved to comply\n" |
|---|
| 110 |
"with the XDG Base Directory Specification " |
|---|
| 111 |
"version 0.6. Your\nconfiguration has been " |
|---|
| 112 |
"copied to the new location:\n%s\nYou can " |
|---|
| 113 |
"delete this directory and all its contents.", |
|---|
| 114 |
psz_filename); |
|---|
| 115 |
fclose( p_readme ); |
|---|
| 116 |
} |
|---|
| 117 |
free( psz_readme ); |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
free( psz_old ); |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
#endif |
|---|
| 124 |
else if( p_stream != NULL ) |
|---|
| 125 |
{ |
|---|
| 126 |
libvlc_priv (p_obj->p_libvlc)->psz_configfile = psz_filename; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
return p_stream; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
static int strtoi (const char *str) |
|---|
| 134 |
{ |
|---|
| 135 |
char *end; |
|---|
| 136 |
long l; |
|---|
| 137 |
|
|---|
| 138 |
errno = 0; |
|---|
| 139 |
l = strtol (str, &end, 0); |
|---|
| 140 |
|
|---|
| 141 |
if (!errno) |
|---|
| 142 |
{ |
|---|
| 143 |
if ((l > INT_MAX) || (l < INT_MIN)) |
|---|
| 144 |
errno = ERANGE; |
|---|
| 145 |
if (*end) |
|---|
| 146 |
errno = EINVAL; |
|---|
| 147 |
} |
|---|
| 148 |
return (int)l; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name ) |
|---|
| 159 |
{ |
|---|
| 160 |
libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc); |
|---|
| 161 |
FILE *file; |
|---|
| 162 |
|
|---|
| 163 |
file = config_OpenConfigFile (p_this, "rt"); |
|---|
| 164 |
if (file == NULL) |
|---|
| 165 |
return VLC_EGENERIC; |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
vlc_mutex_lock( &priv->config_lock ); |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
module_t **list = module_list_get (NULL); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
char * (*convert) (const char *) = strdupnull; |
|---|
| 175 |
char bom[3]; |
|---|
| 176 |
|
|---|
| 177 |
if ((fread (bom, 1, 3, file) != 3) |
|---|
| 178 |
|| memcmp (bom, "\xEF\xBB\xBF", 3)) |
|---|
| 179 |
{ |
|---|
| 180 |
convert = FromLocaleDup; |
|---|
| 181 |
rewind (file); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
module_t *module = NULL; |
|---|
| 185 |
char line[1024], section[1022]; |
|---|
| 186 |
section[0] = '\0'; |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL); |
|---|
| 190 |
locale_t baseloc = uselocale (loc); |
|---|
| 191 |
|
|---|
| 192 |
while (fgets (line, 1024, file) != NULL) |
|---|
| 193 |
{ |
|---|
| 194 |
|
|---|
| 195 |
switch (line[0]) |
|---|
| 196 |
{ |
|---|
| 197 |
case '#': |
|---|
| 198 |
case '\n': |
|---|
| 199 |
case '\0': |
|---|
| 200 |
continue; |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
if (line[0] == '[') |
|---|
| 204 |
{ |
|---|
| 205 |
char *ptr = strchr (line, ']'); |
|---|
| 206 |
if (ptr == NULL) |
|---|
| 207 |
continue; |
|---|
| 208 |
*ptr = '\0'; |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
strcpy (section, line + 1); |
|---|
| 212 |
module = NULL; |
|---|
| 213 |
|
|---|
| 214 |
if ((psz_module_name == NULL) |
|---|
| 215 |
|| (strcmp (psz_module_name, section) == 0)) |
|---|
| 216 |
{ |
|---|
| 217 |
for (int i = 0; list[i]; i++) |
|---|
| 218 |
{ |
|---|
| 219 |
module_t *m = list[i]; |
|---|
| 220 |
|
|---|
| 221 |
if ((strcmp (section, m->psz_object_name) == 0) |
|---|
| 222 |
&& (m->i_config_items > 0)) |
|---|
| 223 |
{ |
|---|
| 224 |
module = m; |
|---|
| 225 |
if (psz_module_name != NULL) |
|---|
| 226 |
msg_Dbg (p_this, |
|---|
| 227 |
"loading config for module \"%s\"", |
|---|
| 228 |
section); |
|---|
| 229 |
break; |
|---|
| 230 |
} |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
continue; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
if (module == NULL) |
|---|
| 238 |
continue; |
|---|
| 239 |
|
|---|
| 240 |
char *ptr = strchr (line, '\n'); |
|---|
| 241 |
if (ptr != NULL) |
|---|
| 242 |
*ptr = '\0'; |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
const char *psz_option_name = line; |
|---|
| 246 |
|
|---|
| 247 |
ptr = strchr (line, '='); |
|---|
| 248 |
if (ptr == NULL) |
|---|
| 249 |
continue; |
|---|
| 250 |
|
|---|
| 251 |
*ptr = '\0'; |
|---|
| 252 |
const char *psz_option_value = ptr + 1; |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
for (size_t i = 0; i < module->confsize; i++) |
|---|
| 256 |
{ |
|---|
| 257 |
module_config_t *p_item = module->p_config + i; |
|---|
| 258 |
|
|---|
| 259 |
if ((p_item->i_type & CONFIG_HINT) |
|---|
| 260 |
|| strcmp (p_item->psz_name, psz_option_name)) |
|---|
| 261 |
continue; |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
errno = 0; |
|---|
| 265 |
|
|---|
| 266 |
switch( p_item->i_type ) |
|---|
| 267 |
{ |
|---|
| 268 |
case CONFIG_ITEM_BOOL: |
|---|
| 269 |
case CONFIG_ITEM_INTEGER: |
|---|
| 270 |
{ |
|---|
| 271 |
long l = strtoi (psz_option_value); |
|---|
| 272 |
if (errno) |
|---|
| 273 |
msg_Warn (p_this, "Integer value (%s) for %s: %m", |
|---|
| 274 |
psz_option_value, psz_option_name); |
|---|
| 275 |
else |
|---|
| 276 |
p_item->saved.i = p_item->value.i = (int)l; |
|---|
| 277 |
break; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
case CONFIG_ITEM_FLOAT: |
|---|
| 281 |
if( !*psz_option_value ) |
|---|
| 282 |
break; |
|---|
| 283 |
p_item->value.f = (float)atof (psz_option_value); |
|---|
| 284 |
p_item->saved.f = p_item->value.f; |
|---|
| 285 |
break; |
|---|
| 286 |
|
|---|
| 287 |
case CONFIG_ITEM_KEY: |
|---|
| 288 |
if( !*psz_option_value ) |
|---|
| 289 |
break; |
|---|
| 290 |
p_item->value.i = ConfigStringToKey(psz_option_value); |
|---|
| 291 |
p_item->saved.i = p_item->value.i; |
|---|
| 292 |
break; |
|---|
| 293 |
|
|---|
| 294 |
default: |
|---|
| 295 |
vlc_mutex_lock( p_item->p_lock ); |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
free( (char*) p_item->value.psz ); |
|---|
| 299 |
free( (char*) p_item->saved.psz ); |
|---|
| 300 |
|
|---|
| 301 |
p_item->value.psz = convert (psz_option_value); |
|---|
| 302 |
p_item->saved.psz = strdupnull (p_item->value.psz); |
|---|
| 303 |
|
|---|
| 304 |
vlc_mutex_unlock( p_item->p_lock ); |
|---|
| 305 |
break; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
break; |
|---|
| 309 |
} |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
if (ferror (file)) |
|---|
| 313 |
{ |
|---|
| 314 |
msg_Err (p_this, "error reading configuration: %m"); |
|---|
| 315 |
clearerr (file); |
|---|
| 316 |
} |
|---|
| 317 |
fclose (file); |
|---|
| 318 |
|
|---|
| 319 |
module_list_free (list); |
|---|
| 320 |
if (loc != (locale_t)0) |
|---|
| 321 |
{ |
|---|
| 322 |
uselocale (baseloc); |
|---|
| 323 |
freelocale (loc); |
|---|
| 324 |
} |
|---|
| 325 |
|
|---|
| 326 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 327 |
return 0; |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname ) |
|---|
| 334 |
{ |
|---|
| 335 |
if( !psz_dirname || !*psz_dirname ) return -1; |
|---|
| 336 |
|
|---|
| 337 |
if( utf8_mkdir( psz_dirname, 0700 ) == 0 ) |
|---|
| 338 |
return 0; |
|---|
| 339 |
|
|---|
| 340 |
switch( errno ) |
|---|
| 341 |
{ |
|---|
| 342 |
case EEXIST: |
|---|
| 343 |
return 0; |
|---|
| 344 |
|
|---|
| 345 |
case ENOENT: |
|---|
| 346 |
{ |
|---|
| 347 |
|
|---|
| 348 |
char psz_parent[strlen( psz_dirname ) + 1], *psz_end; |
|---|
| 349 |
strcpy( psz_parent, psz_dirname ); |
|---|
| 350 |
|
|---|
| 351 |
psz_end = strrchr( psz_parent, DIR_SEP_CHAR ); |
|---|
| 352 |
if( psz_end && psz_end != psz_parent ) |
|---|
| 353 |
{ |
|---|
| 354 |
*psz_end = '\0'; |
|---|
| 355 |
if( config_CreateDir( p_this, psz_parent ) == 0 ) |
|---|
| 356 |
{ |
|---|
| 357 |
if( !utf8_mkdir( psz_dirname, 0700 ) ) |
|---|
| 358 |
return 0; |
|---|
| 359 |
} |
|---|
| 360 |
} |
|---|
| 361 |
} |
|---|
| 362 |
} |
|---|
| 363 |
|
|---|
| 364 |
msg_Err( p_this, "could not create %s: %m", psz_dirname ); |
|---|
| 365 |
return -1; |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
static int |
|---|
| 369 |
config_Write (FILE *file, const char *type, const char *desc, |
|---|
| 370 |
bool comment, const char *name, const char *fmt, ...) |
|---|
| 371 |
{ |
|---|
| 372 |
va_list ap; |
|---|
| 373 |
int ret; |
|---|
| 374 |
|
|---|
| 375 |
if (desc == NULL) |
|---|
| 376 |
desc = "?"; |
|---|
| 377 |
|
|---|
| 378 |
if (fprintf (file, "# %s (%s)\n%s%s=", desc, _(type), |
|---|
| 379 |
comment ? "#" : "", name) < 0) |
|---|
| 380 |
return -1; |
|---|
| 381 |
|
|---|
| 382 |
va_start (ap, fmt); |
|---|
| 383 |
ret = vfprintf (file, fmt, ap); |
|---|
| 384 |
va_end (ap); |
|---|
| 385 |
if (ret < 0) |
|---|
| 386 |
return -1; |
|---|
| 387 |
|
|---|
| 388 |
if (fputs ("\n\n", file) == EOF) |
|---|
| 389 |
return -1; |
|---|
| 390 |
return 0; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name, |
|---|
| 414 |
bool b_autosave ) |
|---|
| 415 |
{ |
|---|
| 416 |
libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc); |
|---|
| 417 |
module_t *p_parser; |
|---|
| 418 |
FILE *file; |
|---|
| 419 |
char p_line[1024], *p_index2; |
|---|
| 420 |
int i_sizebuf = 0; |
|---|
| 421 |
char *p_bigbuffer, *p_index; |
|---|
| 422 |
bool b_backup; |
|---|
| 423 |
int i_index; |
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
vlc_mutex_lock( &priv->config_lock ); |
|---|
| 427 |
|
|---|
| 428 |
if( libvlc_priv (p_this->p_libvlc)->psz_configfile == NULL ) |
|---|
| 429 |
{ |
|---|
| 430 |
char *psz_configdir = config_GetUserConfDir(); |
|---|
| 431 |
if( !psz_configdir ) |
|---|
| 432 |
{ |
|---|
| 433 |
msg_Err( p_this, "no configuration directory defined" ); |
|---|
| 434 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 435 |
return -1; |
|---|
| 436 |
} |
|---|
| 437 |
|
|---|
| 438 |
config_CreateDir( p_this, psz_configdir ); |
|---|
| 439 |
free( psz_configdir ); |
|---|
| 440 |
} |
|---|
| 441 |
|
|---|
| 442 |
file = config_OpenConfigFile( p_this, "rt" ); |
|---|
| 443 |
if( file != NULL ) |
|---|
| 444 |
{ |
|---|
| 445 |
|
|---|
| 446 |
fseek( file, 0L, SEEK_END ); |
|---|
| 447 |
i_sizebuf = ftell( file ); |
|---|
| 448 |
fseek( file, 0L, SEEK_SET ); |
|---|
| 449 |
} |
|---|
| 450 |
|
|---|
| 451 |
p_bigbuffer = p_index = malloc( i_sizebuf+1 ); |
|---|
| 452 |
if( !p_bigbuffer ) |
|---|
| 453 |
{ |
|---|
| 454 |
if( file ) fclose( file ); |
|---|
| 455 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 456 |
return -1; |
|---|
| 457 |
} |
|---|
| 458 |
p_bigbuffer[0] = 0; |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
module_t **list = module_list_get (NULL); |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
b_backup = 0; |
|---|
| 466 |
while( file && fgets( p_line, 1024, file ) ) |
|---|
| 467 |
{ |
|---|
| 468 |
if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']'))) |
|---|
| 469 |
{ |
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ ) |
|---|
| 473 |
{ |
|---|
| 474 |
if( ((p_index2 - &p_line[1]) |
|---|
| 475 |
== (int)strlen(p_parser->psz_object_name) ) |
|---|
| 476 |
&& !memcmp( &p_line[1], p_parser->psz_object_name, |
|---|
| 477 |
strlen(p_parser->psz_object_name) ) ) |
|---|
| 478 |
{ |
|---|
| 479 |
if( !psz_module_name ) |
|---|
| 480 |
break; |
|---|
| 481 |
else if( !strcmp( psz_module_name, |
|---|
| 482 |
p_parser->psz_object_name ) ) |
|---|
| 483 |
break; |
|---|
| 484 |
} |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
if( list[i_index] == NULL ) |
|---|
| 488 |
{ |
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
*p_index2 = 0; |
|---|
| 492 |
#if 0 |
|---|
| 493 |
msg_Dbg( p_this, "backing up config for unknown module \"%s\"", |
|---|
| 494 |
&p_line[1] ); |
|---|
| 495 |
#endif |
|---|
| 496 |
*p_index2 = ']'; |
|---|
| 497 |
|
|---|
| 498 |
b_backup = 1; |
|---|
| 499 |
} |
|---|
| 500 |
else |
|---|
| 501 |
{ |
|---|
| 502 |
b_backup = 0; |
|---|
| 503 |
} |
|---|
| 504 |
} |
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ') |
|---|
| 509 |
&& (p_line[0] != '\t') ) |
|---|
| 510 |
{ |
|---|
| 511 |
strcpy( p_index, p_line ); |
|---|
| 512 |
p_index += strlen( p_line ); |
|---|
| 513 |
} |
|---|
| 514 |
} |
|---|
| 515 |
if( file ) fclose( file ); |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
file = config_OpenConfigFile (p_this, "wt"); |
|---|
| 523 |
if( !file ) |
|---|
| 524 |
{ |
|---|
| 525 |
module_list_free (list); |
|---|
| 526 |
free( p_bigbuffer ); |
|---|
| 527 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 528 |
return -1; |
|---|
| 529 |
} |
|---|
| 530 |
|
|---|
| 531 |
fprintf( file, "\xEF\xBB\xBF###\n### " COPYRIGHT_MESSAGE "\n###\n\n" |
|---|
| 532 |
"###\n### lines beginning with a '#' character are comments\n###\n\n" ); |
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL); |
|---|
| 536 |
locale_t baseloc = uselocale (loc); |
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
for( i_index = 0; (p_parser = list[i_index]) != NULL; i_index++ ) |
|---|
| 540 |
{ |
|---|
| 541 |
module_config_t *p_item, *p_end; |
|---|
| 542 |
|
|---|
| 543 |
if( psz_module_name && strcmp( psz_module_name, |
|---|
| 544 |
p_parser->psz_object_name ) ) |
|---|
| 545 |
continue; |
|---|
| 546 |
|
|---|
| 547 |
if( !p_parser->i_config_items ) |
|---|
| 548 |
continue; |
|---|
| 549 |
|
|---|
| 550 |
if( psz_module_name ) |
|---|
| 551 |
msg_Dbg( p_this, "saving config for module \"%s\"", |
|---|
| 552 |
p_parser->psz_object_name ); |
|---|
| 553 |
|
|---|
| 554 |
fprintf( file, "[%s]", p_parser->psz_object_name ); |
|---|
| 555 |
if( p_parser->psz_longname ) |
|---|
| 556 |
fprintf( file, " # %s\n\n", p_parser->psz_longname ); |
|---|
| 557 |
else |
|---|
| 558 |
fprintf( file, "\n\n" ); |
|---|
| 559 |
|
|---|
| 560 |
for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize; |
|---|
| 561 |
p_item < p_end; |
|---|
| 562 |
p_item++ ) |
|---|
| 563 |
{ |
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
bool b_retain = b_autosave && !p_item->b_autosave; |
|---|
| 567 |
|
|---|
| 568 |
if ((p_item->i_type & CONFIG_HINT) |
|---|
| 569 |
|| p_item->b_removed |
|---|
| 570 |
|| p_item->b_unsaveable) |
|---|
| 571 |
continue; |
|---|
| 572 |
|
|---|
| 573 |
if (IsConfigIntegerType (p_item->i_type)) |
|---|
| 574 |
{ |
|---|
| 575 |
int val = b_retain ? p_item->saved.i : p_item->value.i; |
|---|
| 576 |
if (p_item->i_type == CONFIG_ITEM_KEY) |
|---|
| 577 |
{ |
|---|
| 578 |
char *psz_key = ConfigKeyToString (val); |
|---|
| 579 |
config_Write (file, p_item->psz_text, N_("key"), |
|---|
| 580 |
val == p_item->orig.i, |
|---|
| 581 |
p_item->psz_name, "%s", |
|---|
| 582 |
psz_key ? psz_key : ""); |
|---|
| 583 |
free (psz_key); |
|---|
| 584 |
} |
|---|
| 585 |
else |
|---|
| 586 |
config_Write (file, p_item->psz_text, |
|---|
| 587 |
(p_item->i_type == CONFIG_ITEM_BOOL) |
|---|
| 588 |
? N_("boolean") : N_("integer"), |
|---|
| 589 |
val == p_item->orig.i, |
|---|
| 590 |
p_item->psz_name, "%d", val); |
|---|
| 591 |
p_item->saved.i = val; |
|---|
| 592 |
} |
|---|
| 593 |
else |
|---|
| 594 |
if (IsConfigFloatType (p_item->i_type)) |
|---|
| 595 |
{ |
|---|
| 596 |
float val = b_retain ? p_item->saved.f : p_item->value.f; |
|---|
| 597 |
config_Write (file, p_item->psz_text, N_("float"), |
|---|
| 598 |
val == p_item->orig.f, |
|---|
| 599 |
p_item->psz_name, "%f", val); |
|---|
| 600 |
p_item->saved.f = val; |
|---|
| 601 |
} |
|---|
| 602 |
else |
|---|
| 603 |
{ |
|---|
| 604 |
const char *psz_value = b_retain ? p_item->saved.psz |
|---|
| 605 |
: p_item->value.psz; |
|---|
| 606 |
bool modified; |
|---|
| 607 |
|
|---|
| 608 |
assert (IsConfigStringType (p_item->i_type)); |
|---|
| 609 |
|
|---|
| 610 |
if (b_retain && (psz_value == NULL)) |
|---|
| 611 |
psz_value = p_item->orig.psz; |
|---|
| 612 |
|
|---|
| 613 |
modified = |
|---|
| 614 |
(psz_value != NULL) |
|---|
| 615 |
? ((p_item->orig.psz != NULL) |
|---|
| 616 |
? (strcmp (psz_value, p_item->orig.psz) != 0) |
|---|
| 617 |
: true) |
|---|
| 618 |
: (p_item->orig.psz != NULL); |
|---|
| 619 |
|
|---|
| 620 |
config_Write (file, p_item->psz_text, N_("string"), |
|---|
| 621 |
!modified, p_item->psz_name, "%s", |
|---|
| 622 |
psz_value ? psz_value : ""); |
|---|
| 623 |
|
|---|
| 624 |
if ( !b_retain ) |
|---|
| 625 |
{ |
|---|
| 626 |
|
|---|
| 627 |
free ((char *)p_item->saved.psz); |
|---|
| 628 |
if( (psz_value && p_item->orig.psz && |
|---|
| 629 |
strcmp( psz_value, p_item->orig.psz )) || |
|---|
| 630 |
!psz_value || !p_item->orig.psz) |
|---|
| 631 |
p_item->saved.psz = strdupnull (psz_value); |
|---|
| 632 |
else |
|---|
| 633 |
p_item->saved.psz = NULL; |
|---|
| 634 |
} |
|---|
| 635 |
} |
|---|
| 636 |
|
|---|
| 637 |
if (!b_retain) |
|---|
| 638 |
p_item->b_dirty = false; |
|---|
| 639 |
} |
|---|
| 640 |
} |
|---|
| 641 |
|
|---|
| 642 |
module_list_free (list); |
|---|
| 643 |
if (loc != (locale_t)0) |
|---|
| 644 |
{ |
|---|
| 645 |
uselocale (baseloc); |
|---|
| 646 |
freelocale (loc); |
|---|
| 647 |
} |
|---|
| 648 |
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
|
|---|
| 652 |
fputs( p_bigbuffer, file ); |
|---|
| 653 |
free( p_bigbuffer ); |
|---|
| 654 |
|
|---|
| 655 |
fclose( file ); |
|---|
| 656 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 657 |
|
|---|
| 658 |
return 0; |
|---|
| 659 |
} |
|---|
| 660 |
|
|---|
| 661 |
int config_AutoSaveConfigFile( vlc_object_t *p_this ) |
|---|
| 662 |
{ |
|---|
| 663 |
libvlc_priv_t *priv = libvlc_priv (p_this->p_libvlc); |
|---|
| 664 |
size_t i_index; |
|---|
| 665 |
bool done; |
|---|
| 666 |
|
|---|
| 667 |
assert( p_this ); |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
vlc_mutex_lock( &priv->config_lock ); |
|---|
| 671 |
module_t **list = module_list_get (NULL); |
|---|
| 672 |
for( i_index = 0; list[i_index]; i_index++ ) |
|---|
| 673 |
{ |
|---|
| 674 |
module_t *p_parser = list[i_index]; |
|---|
| 675 |
module_config_t *p_item, *p_end; |
|---|
| 676 |
|
|---|
| 677 |
if( !p_parser->i_config_items ) continue; |
|---|
| 678 |
|
|---|
| 679 |
for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize; |
|---|
| 680 |
p_item < p_end; |
|---|
| 681 |
p_item++ ) |
|---|
| 682 |
{ |
|---|
| 683 |
if( p_item->b_autosave && p_item->b_dirty ) break; |
|---|
| 684 |
} |
|---|
| 685 |
if( p_item < p_end ) break; |
|---|
| 686 |
} |
|---|
| 687 |
done = list[i_index] == NULL; |
|---|
| 688 |
module_list_free (list); |
|---|
| 689 |
vlc_mutex_unlock( &priv->config_lock ); |
|---|
| 690 |
|
|---|
| 691 |
return done ? VLC_SUCCESS : SaveConfigFile( p_this, NULL, true ); |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name ) |
|---|
| 695 |
{ |
|---|
| 696 |
return SaveConfigFile( p_this, psz_module_name, false ); |
|---|
| 697 |
} |
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
char *config_GetCustomConfigFile( libvlc_int_t *p_libvlc ) |
|---|
| 703 |
{ |
|---|
| 704 |
char *psz_configfile = config_GetPsz( p_libvlc, "config" ); |
|---|
| 705 |
if( psz_configfile != NULL ) |
|---|
| 706 |
{ |
|---|
| 707 |
if( psz_configfile[0] == '~' && psz_configfile[1] == '/' ) |
|---|
| 708 |
{ |
|---|
| 709 |
|
|---|
| 710 |
char *psz_buf; |
|---|
| 711 |
if( asprintf( &psz_buf, "%s/%s", config_GetHomeDir(), |
|---|
| 712 |
psz_configfile + 2 ) == -1 ) |
|---|
| 713 |
{ |
|---|
| 714 |
free( psz_configfile ); |
|---|
| 715 |
return NULL; |
|---|
| 716 |
} |
|---|
| 717 |
free( psz_configfile ); |
|---|
| 718 |
psz_configfile = psz_buf; |
|---|
| 719 |
} |
|---|
| 720 |
} |
|---|
| 721 |
return psz_configfile; |
|---|
| 722 |
} |
|---|
| 723 |
|
|---|
| 724 |
int ConfigStringToKey( const char *psz_key ) |
|---|
| 725 |
{ |
|---|
| 726 |
int i_key = 0; |
|---|
| 727 |
unsigned int i; |
|---|
| 728 |
const char *psz_parser = strchr( psz_key, '-' ); |
|---|
| 729 |
while( psz_parser && psz_parser != psz_key ) |
|---|
| 730 |
{ |
|---|
| 731 |
for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ ) |
|---|
| 732 |
{ |
|---|
| 733 |
if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key, |
|---|
| 734 |
strlen( vlc_modifiers[i].psz_key_string ) ) ) |
|---|
| 735 |
{ |
|---|
| 736 |
i_key |= vlc_modifiers[i].i_key_code; |
|---|
| 737 |
} |
|---|
| 738 |
} |
|---|
| 739 |
psz_key = psz_parser + 1; |
|---|
| 740 |
psz_parser = strchr( psz_key, '-' ); |
|---|
| 741 |
} |
|---|
| 742 |
for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ ) |
|---|
| 743 |
{ |
|---|
| 744 |
if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) ) |
|---|
| 745 |
{ |
|---|
| 746 |
i_key |= vlc_keys[i].i_key_code; |
|---|
| 747 |
break; |
|---|
| 748 |
} |
|---|
| 749 |
} |
|---|
| 750 |
return i_key; |
|---|
| 751 |
} |
|---|
| 752 |
|
|---|
| 753 |
char *ConfigKeyToString( int i_key ) |
|---|
| 754 |
{ |
|---|
| 755 |
char *psz_key = malloc( 100 ); |
|---|
| 756 |
char *p; |
|---|
| 757 |
size_t index; |
|---|
| 758 |
|
|---|
| 759 |
if ( !psz_key ) |
|---|
| 760 |
{ |
|---|
| 761 |
return NULL; |
|---|
| 762 |
} |
|---|
| 763 |
*psz_key = '\0'; |
|---|
| 764 |
p = psz_key; |
|---|
| 765 |
|
|---|
| 766 |
for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t)); |
|---|
| 767 |
index++ ) |
|---|
| 768 |
{ |
|---|
| 769 |
if( i_key & vlc_modifiers[index].i_key_code ) |
|---|
| 770 |
{ |
|---|
| 771 |
p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string ); |
|---|
| 772 |
} |
|---|
| 773 |
} |
|---|
| 774 |
for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t)); |
|---|
| 775 |
index++) |
|---|
| 776 |
{ |
|---|
| 777 |
if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code ) |
|---|
| 778 |
{ |
|---|
| 779 |
p += sprintf( p, "%s", vlc_keys[index].psz_key_string ); |
|---|
| 780 |
break; |
|---|
| 781 |
} |
|---|
| 782 |
} |
|---|
| 783 |
return psz_key; |
|---|
| 784 |
} |
|---|
| 785 |
|
|---|