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;
157 gboolean lxdream_get_config_boolean_value( lxdream_config_group_t group, int key )
159 const gchar *value = lxdream_get_config_value(group, key);
160 if( strcasecmp(value, "on") == 0 || strcasecmp(value, "true") == 0 ||
161 strcasecmp(value, "yes") == 0 || strcasecmp(value, "1") == 0 ) {
168 gboolean lxdream_set_config_boolean_value( lxdream_config_group_t group, int key, gboolean value )
170 return lxdream_set_config_value(group, key, value ? "on" : "off");
173 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
175 lxdream_config_entry_t param = &group->params[key];
176 if( param->value != value &&
177 (param->value == NULL || value == NULL || strcmp(param->value,value) != 0) ) {
179 gchar *new_value = g_strdup(value);
181 /* If the group defines an on_change handler, it can block the change
182 * (ie due to an invalid setting).
184 if( group->on_change == NULL ||
185 group->on_change(group->data, group,key, param->value, new_value) ) {
187 /* Don't free the default value, but otherwise need to release the
190 if( param->value != param->default_value && param->value != NULL ) {
191 free( param->value );
193 param->value = new_value;
194 } else { /* on_change handler said no. */
202 const gchar *lxdream_get_global_config_value( int key )
204 return global_group.params[key].value;
207 void lxdream_set_global_config_value( int key, const gchar *value )
209 lxdream_set_config_value(&global_group, key, value);
212 GList *lxdream_get_global_config_list_value( int key )
214 GList *result = NULL;
215 const gchar *str = lxdream_get_global_config_value( key );
217 gchar **strv = g_strsplit(str, ":",0);
219 for( i=0; strv[i] != NULL; i++ ) {
220 result = g_list_append( result, g_strdup(strv[i]) );
227 void lxdream_set_global_config_list_value( int key, const GList *list )
230 lxdream_set_global_config_value( key, NULL );
235 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
236 size += strlen( (gchar *)ptr->data ) + 1;
239 strcpy( buf, (gchar *)list->data );
240 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
242 strcat( buf, (gchar *)ptr->data );
244 lxdream_set_global_config_value( key, buf );
248 gchar *lxdream_get_global_config_path_value( int key )
250 const gchar *str = lxdream_get_global_config_value(key);
254 return get_expanded_path(str);
258 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
260 gchar *temp = get_escaped_path(value);
261 lxdream_set_global_config_value(key,temp);
263 return lxdream_get_global_config_value(key);
266 struct lxdream_config_group * lxdream_get_config_group( int group )
268 return lxdream_config_root[group];
271 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
274 for( i=0; src->params[i].key != NULL; i++ ) {
275 lxdream_set_config_value( dest, i, src->params[i].value );
279 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
283 dest->key = src->key;
284 dest->on_change = NULL;
285 dest->key_binding = NULL;
287 for( i=0; src->params[i].key != NULL; i++ ) {
288 dest->params[i].key = src->params[i].key;
289 dest->params[i].label = src->params[i].label;
290 dest->params[i].type = src->params[i].type;
291 dest->params[i].tag = src->params[i].tag;
292 dest->params[i].default_value = src->params[i].default_value;
293 dest->params[i].value = NULL;
294 lxdream_set_config_value( dest, i, src->params[i].value );
296 dest->params[i].key = NULL;
297 dest->params[i].label = NULL;
300 gboolean lxdream_load_config( )
302 if( lxdream_config_load_filename == NULL ) {
303 lxdream_find_config();
305 return lxdream_load_config_file(lxdream_config_load_filename);
308 gboolean lxdream_save_config( )
310 if( lxdream_config_save_filename == NULL ) {
311 lxdream_find_config();
313 return lxdream_save_config_file(lxdream_config_save_filename);
316 gboolean lxdream_load_config_file( const gchar *filename )
321 if( access(filename, F_OK) != 0 ) {
322 INFO( "Configuration file '%s' does not exist, creating from defaults" );
323 lxdream_set_default_config();
324 lxdream_save_config();
327 f = fopen(filename, "ro");
329 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
330 lxdream_set_default_config();
334 result = lxdream_load_config_stream( f );
339 gboolean lxdream_load_config_stream( FILE *f )
343 int maple_device = -1, maple_subdevice = -1, i;
344 struct lxdream_config_group *group = NULL;
345 struct lxdream_config_group *top_group = NULL;
346 maple_device_t device = NULL;
347 lxdream_set_default_config();
349 while( fgets( buf, sizeof(buf), f ) != NULL ) {
354 char *p = strchr(buf, ']');
356 maple_device = maple_subdevice = -1;
359 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
360 if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
361 top_group = group = lxdream_config_root[i];
366 } else if( group != NULL ) {
367 char *value = strchr( buf, '=' );
368 if( value != NULL ) {
369 struct lxdream_config_entry *param = group->params;
374 if( top_group == &controllers_group ) {
375 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
376 maple_device = strtoul( buf+7, NULL, 0 );
377 if( maple_device < 0 || maple_device > 3 ) {
378 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
382 device = maple_new_device( value );
383 if( device == NULL ) {
384 ERROR( "Unrecognized device '%s'", value );
386 group = maple_get_device_config(device);
387 maple_attach_device( device, maple_device, maple_subdevice );
390 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
391 maple_subdevice = strtoul( buf+10, NULL, 0 );
392 if( maple_device == -1 ) {
393 ERROR( "Subdevice not allowed without primary device" );
394 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
395 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
396 } else if( (device = maple_new_device(value)) == NULL ) {
397 ERROR( "Unrecognized subdevice '%s'", value );
399 group = maple_get_device_config(device);
400 maple_attach_device( device, maple_device, maple_subdevice );
405 while( param->key != NULL ) {
406 if( strcasecmp( param->key, buf ) == 0 ) {
407 param->value = g_strdup(value);
418 gboolean lxdream_save_config_file( const gchar *filename )
420 FILE *f = fopen(filename, "wo");
423 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
426 result = lxdream_save_config_stream(f);
431 gboolean lxdream_save_config_stream( FILE *f )
434 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
435 fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
437 if( lxdream_config_root[i] == &controllers_group ) {
439 for( i=0; i<4; i++ ) {
440 for( j=0; j<6; j++ ) {
441 maple_device_t dev = maple_get_device( i, j );
444 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
446 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
447 lxdream_config_group_t group = maple_get_device_config(dev);
448 if( group != NULL ) {
449 lxdream_config_entry_t entry = group->params;
450 while( entry->key != NULL ) {
451 if( entry->value != NULL ) {
452 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
461 struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
462 while( entry->key != NULL ) {
463 if( entry->value != NULL ) {
464 fprintf( f, "%s = %s\n", entry->key, entry->value );
474 void lxdream_make_config_dir( )
476 const char *user_path = get_user_data_path();
479 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
480 (st.st_mode & S_IFDIR) != 0 ) {
485 if( mkdir( user_path, 0777 ) != 0 ) {
486 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
490 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
491 mkdir( vmupath, 0777 );
494 char *savepath = g_strdup_printf( "%s/save", user_path );
495 mkdir( savepath, 0777 );
.