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.
24 #include <glib/gmem.h>
25 #include <glib/gstrfuncs.h>
26 #include <sys/types.h>
28 #include "dreamcast.h"
31 #include "maple/maple.h"
33 #define MAX_ROOT_GROUPS 16
35 extern struct lxdream_config_group hotkeys_group;
36 extern struct lxdream_config_group serial_group;
38 gboolean lxdream_load_config_file( const gchar *filename );
39 gboolean lxdream_save_config_file( const gchar *filename );
40 gboolean lxdream_load_config_stream( FILE *f );
41 gboolean lxdream_save_config_stream( FILE *f );
43 static struct lxdream_config_group global_group =
44 { "global", dreamcast_config_changed, NULL, NULL,
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 }} };
58 * Dummy group for controllers (handled specially)
60 static struct lxdream_config_group controllers_group =
61 { "controllers", NULL, NULL, NULL, {{NULL, CONFIG_TYPE_NONE}} };
63 struct lxdream_config_group *lxdream_config_root[MAX_ROOT_GROUPS+1] =
64 { &global_group, &controllers_group, &hotkeys_group, &serial_group, NULL };
66 static gchar *lxdream_config_load_filename = NULL;
67 static gchar *lxdream_config_save_filename = NULL;
69 void lxdream_register_config_group( const gchar *key, lxdream_config_group_t group )
72 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
73 if( lxdream_config_root[i] == NULL ) {
74 lxdream_config_root[i] = group;
75 lxdream_config_root[i+1] = NULL;
79 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
82 gboolean lxdream_find_config()
84 gboolean result = TRUE;
85 char *home = getenv("HOME");
86 if( lxdream_config_save_filename == NULL ) {
87 /* For compatibility, look for the old ~/.lxdreamrc first. Use it if
88 * found, otherwise use the new ~/.lxdream/lxdreamrc.
90 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
91 if( access(lxdream_config_save_filename, R_OK) != 0 ) {
92 g_free(lxdream_config_save_filename);
93 const char *user_path = get_user_data_path();
94 lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
97 if( lxdream_config_load_filename == NULL ) {
98 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
99 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
100 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
102 } else if( access( sysconfig, R_OK ) == 0 ) {
103 lxdream_config_load_filename = sysconfig;
105 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
113 void lxdream_set_config_filename( const gchar *filename )
115 if( lxdream_config_load_filename != NULL ) {
116 g_free(lxdream_config_load_filename);
118 lxdream_config_load_filename = g_strdup(filename);
119 if( lxdream_config_save_filename != NULL ) {
120 g_free(lxdream_config_save_filename);
122 lxdream_config_save_filename = g_strdup(filename);
125 void lxdream_set_default_config( )
127 /* Construct platform dependent defaults */
128 const gchar *user_path = get_user_data_path();
129 global_group.params[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path );
130 global_group.params[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path );
131 global_group.params[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path );
132 global_group.params[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path );
133 global_group.params[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path );
135 /* Copy defaults into main values */
136 for( int i=0; lxdream_config_root[i] != NULL; i++ ) {
137 struct lxdream_config_entry *param = lxdream_config_root[i]->params;
138 if( param != NULL ) {
139 while( param->key != NULL ) {
140 if( param->value != param->default_value ) {
141 if( param->value != NULL )
142 free( param->value );
143 param->value = (gchar *)param->default_value;
152 const gchar *lxdream_get_config_value( lxdream_config_group_t group, int key )
154 return group->params[key].value;
158 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
160 lxdream_config_entry_t param = &group->params[key];
161 if( param->value != value &&
162 (param->value == NULL || value == NULL || strcmp(param->value,value) != 0) ) {
164 gchar *new_value = g_strdup(value);
166 /* If the group defines an on_change handler, it can block the change
167 * (ie due to an invalid setting).
169 if( group->on_change == NULL ||
170 group->on_change(group->data, group,key, param->value, new_value) ) {
172 /* Don't free the default value, but otherwise need to release the
175 if( param->value != param->default_value && param->value != NULL ) {
176 free( param->value );
178 param->value = new_value;
179 } else { /* on_change handler said no. */
187 const gchar *lxdream_get_global_config_value( int key )
189 return global_group.params[key].value;
192 void lxdream_set_global_config_value( int key, const gchar *value )
194 lxdream_set_config_value(&global_group, key, value);
197 GList *lxdream_get_global_config_list_value( int key )
199 GList *result = NULL;
200 const gchar *str = lxdream_get_global_config_value( key );
202 gchar **strv = g_strsplit(str, ":",0);
204 for( i=0; strv[i] != NULL; i++ ) {
205 result = g_list_append( result, g_strdup(strv[i]) );
212 void lxdream_set_global_config_list_value( int key, const GList *list )
215 lxdream_set_global_config_value( key, NULL );
220 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
221 size += strlen( (gchar *)ptr->data ) + 1;
224 strcpy( buf, (gchar *)list->data );
225 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
227 strcat( buf, (gchar *)ptr->data );
229 lxdream_set_global_config_value( key, buf );
233 gchar *lxdream_get_global_config_path_value( int key )
235 const gchar *str = lxdream_get_global_config_value(key);
239 return get_expanded_path(str);
243 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
245 gchar *temp = get_escaped_path(value);
246 lxdream_set_global_config_value(key,temp);
248 return lxdream_get_global_config_value(key);
251 struct lxdream_config_group * lxdream_get_config_group( int group )
253 return lxdream_config_root[group];
256 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
259 for( i=0; src->params[i].key != NULL; i++ ) {
260 lxdream_set_config_value( dest, i, src->params[i].value );
264 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
268 dest->key = src->key;
269 dest->on_change = NULL;
270 dest->key_binding = NULL;
272 for( i=0; src->params[i].key != NULL; i++ ) {
273 dest->params[i].key = src->params[i].key;
274 dest->params[i].label = src->params[i].label;
275 dest->params[i].type = src->params[i].type;
276 dest->params[i].tag = src->params[i].tag;
277 dest->params[i].default_value = src->params[i].default_value;
278 dest->params[i].value = NULL;
279 lxdream_set_config_value( dest, i, src->params[i].value );
281 dest->params[i].key = NULL;
282 dest->params[i].label = NULL;
285 gboolean lxdream_load_config( )
287 if( lxdream_config_load_filename == NULL ) {
288 lxdream_find_config();
290 return lxdream_load_config_file(lxdream_config_load_filename);
293 gboolean lxdream_save_config( )
295 if( lxdream_config_save_filename == NULL ) {
296 lxdream_find_config();
298 return lxdream_save_config_file(lxdream_config_save_filename);
301 gboolean lxdream_load_config_file( const gchar *filename )
306 if( access(filename, F_OK) != 0 ) {
307 INFO( "Configuration file '%s' does not exist, creating from defaults" );
308 lxdream_set_default_config();
309 lxdream_save_config();
312 f = fopen(filename, "ro");
314 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
315 lxdream_set_default_config();
319 result = lxdream_load_config_stream( f );
324 gboolean lxdream_load_config_stream( FILE *f )
328 int maple_device = -1, maple_subdevice = -1, i;
329 struct lxdream_config_group *group = NULL;
330 struct lxdream_config_group *top_group = NULL;
331 maple_device_t device = NULL;
332 lxdream_set_default_config();
334 while( fgets( buf, sizeof(buf), f ) != NULL ) {
339 char *p = strchr(buf, ']');
341 maple_device = maple_subdevice = -1;
344 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
345 if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
346 top_group = group = lxdream_config_root[i];
351 } else if( group != NULL ) {
352 char *value = strchr( buf, '=' );
353 if( value != NULL ) {
354 struct lxdream_config_entry *param = group->params;
359 if( top_group == &controllers_group ) {
360 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
361 maple_device = strtoul( buf+7, NULL, 0 );
362 if( maple_device < 0 || maple_device > 3 ) {
363 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
367 device = maple_new_device( value );
368 if( device == NULL ) {
369 ERROR( "Unrecognized device '%s'", value );
371 group = maple_get_device_config(device);
372 maple_attach_device( device, maple_device, maple_subdevice );
375 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
376 maple_subdevice = strtoul( buf+10, NULL, 0 );
377 if( maple_device == -1 ) {
378 ERROR( "Subdevice not allowed without primary device" );
379 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
380 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
381 } else if( (device = maple_new_device(value)) == NULL ) {
382 ERROR( "Unrecognized subdevice '%s'", value );
384 group = maple_get_device_config(device);
385 maple_attach_device( device, maple_device, maple_subdevice );
390 while( param->key != NULL ) {
391 if( strcasecmp( param->key, buf ) == 0 ) {
392 param->value = g_strdup(value);
403 gboolean lxdream_save_config_file( const gchar *filename )
405 FILE *f = fopen(filename, "wo");
408 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
411 result = lxdream_save_config_stream(f);
416 gboolean lxdream_save_config_stream( FILE *f )
419 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
420 fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
422 if( lxdream_config_root[i] == &controllers_group ) {
424 for( i=0; i<4; i++ ) {
425 for( j=0; j<6; j++ ) {
426 maple_device_t dev = maple_get_device( i, j );
429 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
431 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
432 lxdream_config_group_t group = maple_get_device_config(dev);
433 if( group != NULL ) {
434 lxdream_config_entry_t entry = group->params;
435 while( entry->key != NULL ) {
436 if( entry->value != NULL ) {
437 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
446 struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
447 while( entry->key != NULL ) {
448 if( entry->value != NULL ) {
449 fprintf( f, "%s = %s\n", entry->key, entry->value );
459 void lxdream_make_config_dir( )
461 const char *user_path = get_user_data_path();
464 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
465 (st.st_mode & S_IFDIR) != 0 ) {
470 if( mkdir( user_path, 0777 ) != 0 ) {
471 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
475 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
476 mkdir( vmupath, 0777 );
479 char *savepath = g_strdup_printf( "%s/save", user_path );
480 mkdir( savepath, 0777 );
.