4 * User configuration support
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
26 #include <glib/gmem.h>
27 #include <glib/gstrfuncs.h>
28 #include <sys/types.h>
32 #include "maple/maple.h"
34 #define MAX_ROOT_GROUPS 16
36 extern struct lxdream_config_entry alsa_config[];
37 extern struct lxdream_config_entry hotkeys_config[];
39 gboolean lxdream_load_config_file( const gchar *filename );
40 gboolean lxdream_save_config_file( const gchar *filename );
41 gboolean lxdream_load_config_stream( FILE *f );
42 gboolean lxdream_save_config_stream( FILE *f );
44 static struct lxdream_config_entry global_config[] =
45 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
46 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
47 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
48 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
49 { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
50 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
51 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
52 { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
53 { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
54 { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
55 { NULL, CONFIG_TYPE_NONE }};
57 static struct lxdream_config_entry serial_config[] =
58 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
59 { NULL, CONFIG_TYPE_NONE }};
61 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] =
62 {{ "global", global_config },
63 { "controllers", NULL },
64 { "hotkeys", hotkeys_config },
65 { "serial", serial_config },
66 { NULL, CONFIG_TYPE_NONE }};
68 static gchar *lxdream_config_load_filename = NULL;
69 static gchar *lxdream_config_save_filename = NULL;
71 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
74 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
75 if( lxdream_config_root[i].key == NULL ) {
76 lxdream_config_root[i].key = key;
77 lxdream_config_root[i].params = group;
78 lxdream_config_root[i+1].key = NULL;
79 lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
83 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
86 gboolean lxdream_find_config()
88 gboolean result = TRUE;
89 char *home = getenv("HOME");
90 if( lxdream_config_save_filename == NULL ) {
91 /* For compatibility, look for the old ~/.lxdreamrc first. Use it if
92 * found, otherwise use the new ~/.lxdream/lxdreamrc.
94 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
95 if( access(lxdream_config_save_filename, R_OK) != 0 ) {
96 g_free(lxdream_config_save_filename);
97 const char *user_path = get_user_data_path();
98 lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
101 if( lxdream_config_load_filename == NULL ) {
102 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
103 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
104 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
106 } else if( access( sysconfig, R_OK ) == 0 ) {
107 lxdream_config_load_filename = sysconfig;
109 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
117 void lxdream_set_config_filename( const gchar *filename )
119 if( lxdream_config_load_filename != NULL ) {
120 g_free(lxdream_config_load_filename);
122 lxdream_config_load_filename = g_strdup(filename);
123 if( lxdream_config_save_filename != NULL ) {
124 g_free(lxdream_config_save_filename);
126 lxdream_config_save_filename = g_strdup(filename);
129 void lxdream_set_default_config( )
131 /* Construct platform dependent defaults */
132 const gchar *user_path = get_user_data_path();
133 global_config[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path );
134 global_config[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path );
135 global_config[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path );
136 global_config[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path );
137 global_config[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path );
139 /* Copy defaults into main values */
140 struct lxdream_config_group *group = lxdream_config_root;
141 while( group->key != NULL ) {
142 struct lxdream_config_entry *param = group->params;
143 if( param != NULL ) {
144 while( param->key != NULL ) {
145 if( param->value != param->default_value ) {
146 if( param->value != NULL )
147 free( param->value );
148 param->value = (gchar *)param->default_value;
158 const gchar *lxdream_get_global_config_value( int key )
160 return global_config[key].value;
163 GList *lxdream_get_global_config_list_value( int key )
165 GList *result = NULL;
166 const gchar *str = lxdream_get_global_config_value( key );
168 gchar **strv = g_strsplit(str, ":",0);
170 for( i=0; strv[i] != NULL; i++ ) {
171 result = g_list_append( result, g_strdup(strv[i]) );
178 void lxdream_set_global_config_list_value( int key, const GList *list )
181 lxdream_set_global_config_value( key, NULL );
186 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
187 size += strlen( (gchar *)ptr->data ) + 1;
190 strcpy( buf, (gchar *)list->data );
191 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
193 strcat( buf, (gchar *)ptr->data );
195 lxdream_set_global_config_value( key, buf );
199 gchar *get_expanded_path( const gchar *input )
202 if( input == NULL ) {
205 memset(&we,0,sizeof(we));
206 int result = wordexp(input, &we, WRDE_NOCMD);
207 if( result != 0 || we.we_wordc == 0 ) {
208 /* On failure, return the original input unchanged */
209 return g_strdup(input);
211 /* On success, concatenate all 'words' together into a single
212 * space-separated string
214 int length = we.we_wordc, i;
217 for( i=0; i<we.we_wordc; i++ ) {
218 length += strlen(we.we_wordv[i]);
220 p = result = g_malloc(length);
221 for( i=0; i<we.we_wordc; i++ ) {
224 strcpy( p, we.we_wordv[i] );
233 * Test if we need to escape a path to prevent substitution mangling.
234 * @return TRUE if the input value contains any character that doesn't
235 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
238 gboolean path_needs_escaping( const gchar *value )
240 const gchar *p = value;
242 if( !isalnum(*p) && *p != '.' && *p != '_' &&
243 *p != '@' && *p != '%' && *p != '/' ) {
251 gchar *get_escaped_path( const gchar *value )
253 if( value != NULL && path_needs_escaping(value) ) {
254 /* Escape with "", and backslash the remaining characters:
257 char buf[strlen(value)*2+3];
258 const char *s = value;
262 if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
269 return g_strdup(buf);
271 return g_strdup(value);
275 gchar *lxdream_get_global_config_path_value( int key )
277 const gchar *str = lxdream_get_global_config_value(key);
281 return get_expanded_path(str);
285 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
287 gchar *temp = get_escaped_path(value);
288 lxdream_set_global_config_value(key,temp);
290 return lxdream_get_global_config_value(key);
293 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
295 if( param->value != value ) {
296 if( param->value != param->default_value && param->value != NULL ) {
297 free( param->value );
299 param->value = g_strdup(value);
303 void lxdream_set_global_config_value( int key, const gchar *value )
305 lxdream_set_config_value(&global_config[key], value);
308 const struct lxdream_config_entry * lxdream_get_global_config_entry( int key )
310 return &global_config[key];
313 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
316 for( i=0; group->params[i].key != NULL; i++ ) {
317 if( strcasecmp( group->params[i].key, key ) == 0 ) {
318 lxdream_set_config_value( &group->params[i], value );
325 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
328 for( i=0; src[i].key != NULL; i++ ) {
329 lxdream_set_config_value( &dest[i], src[i].value );
333 gboolean lxdream_load_config( )
335 if( lxdream_config_load_filename == NULL ) {
336 lxdream_find_config();
338 return lxdream_load_config_file(lxdream_config_load_filename);
341 gboolean lxdream_save_config( )
343 if( lxdream_config_save_filename == NULL ) {
344 lxdream_find_config();
346 return lxdream_save_config_file(lxdream_config_save_filename);
349 gboolean lxdream_load_config_file( const gchar *filename )
354 if( access(filename, F_OK) != 0 ) {
355 INFO( "Configuration file '%s' does not exist, creating from defaults" );
356 lxdream_set_default_config();
357 lxdream_save_config();
360 f = fopen(filename, "ro");
362 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
363 lxdream_set_default_config();
367 result = lxdream_load_config_stream( f );
372 gboolean lxdream_load_config_stream( FILE *f )
376 int maple_device = -1, maple_subdevice = -1;
377 struct lxdream_config_group devgroup;
378 struct lxdream_config_group *group = NULL;
379 maple_device_t device = NULL;
380 lxdream_set_default_config();
382 while( fgets( buf, sizeof(buf), f ) != NULL ) {
387 char *p = strchr(buf, ']');
389 struct lxdream_config_group *tmp_group;
390 maple_device = maple_subdevice = -1;
393 tmp_group = &lxdream_config_root[0];
394 while( tmp_group->key != NULL ) {
395 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
402 } else if( group != NULL ) {
403 char *value = strchr( buf, '=' );
404 if( value != NULL ) {
405 struct lxdream_config_entry *param = group->params;
410 if( strcmp(group->key,"controllers") == 0 ) {
411 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
412 maple_device = strtoul( buf+7, NULL, 0 );
413 if( maple_device < 0 || maple_device > 3 ) {
414 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
418 device = maple_new_device( value );
419 if( device == NULL ) {
420 ERROR( "Unrecognized device '%s'", value );
422 devgroup.key = "controllers";
423 devgroup.params = maple_get_device_config(device);
424 maple_attach_device( device, maple_device, maple_subdevice );
428 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
429 maple_subdevice = strtoul( buf+10, NULL, 0 );
430 if( maple_device == -1 ) {
431 ERROR( "Subdevice not allowed without primary device" );
432 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
433 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
434 } else if( (device = maple_new_device(value)) == NULL ) {
435 ERROR( "Unrecognized subdevice '%s'", value );
437 devgroup.key = "controllers";
438 devgroup.params = maple_get_device_config(device);
439 maple_attach_device( device, maple_device, maple_subdevice );
445 while( param->key != NULL ) {
446 if( strcasecmp( param->key, buf ) == 0 ) {
447 param->value = g_strdup(value);
458 gboolean lxdream_save_config_file( const gchar *filename )
460 FILE *f = fopen(filename, "wo");
463 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
466 result = lxdream_save_config_stream(f);
471 gboolean lxdream_save_config_stream( FILE *f )
473 struct lxdream_config_group *group = &lxdream_config_root[0];
475 while( group->key != NULL ) {
476 struct lxdream_config_entry *entry = group->params;
477 fprintf( f, "[%s]\n", group->key );
479 if( entry != NULL ) {
480 while( entry->key != NULL ) {
481 if( entry->value != NULL ) {
482 fprintf( f, "%s = %s\n", entry->key, entry->value );
486 } else if( strcmp(group->key, "controllers") == 0 ) {
488 for( i=0; i<4; i++ ) {
489 for( j=0; j<6; j++ ) {
490 maple_device_t dev = maple_get_device( i, j );
493 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
495 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
496 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
497 while( entry->key != NULL ) {
498 if( entry->value != NULL ) {
499 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
514 void lxdream_make_config_dir( )
516 const char *user_path = get_user_data_path();
519 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
520 (st.st_mode & S_IFDIR) != 0 ) {
525 if( mkdir( user_path, 0777 ) != 0 ) {
526 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
530 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
531 mkdir( vmupath, 0777 );
534 char *savepath = g_strdup_printf( "%s/save", user_path );
535 mkdir( savepath, 0777 );
.