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;
37 gboolean lxdream_load_config_file( const gchar *filename );
38 gboolean lxdream_save_config_file( const gchar *filename );
39 gboolean lxdream_load_config_stream( FILE *f );
40 gboolean lxdream_save_config_stream( FILE *f );
42 static struct lxdream_config_group global_group =
43 { "global", dreamcast_config_changed, NULL, NULL,
44 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
45 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
46 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
47 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
48 { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
49 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
50 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
51 { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
52 { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
53 { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
54 { NULL, CONFIG_TYPE_NONE }} };
56 static struct lxdream_config_group serial_group =
57 { "serial", NULL, NULL, NULL,
58 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
59 { NULL, CONFIG_TYPE_NONE }} };
62 * Dummy group for controllers (handled specially)
64 static struct lxdream_config_group controllers_group =
65 { "controllers", NULL, NULL, NULL, {{NULL, CONFIG_TYPE_NONE}} };
67 struct lxdream_config_group *lxdream_config_root[MAX_ROOT_GROUPS+1] =
68 { &global_group, &controllers_group, &hotkeys_group, &serial_group, NULL };
70 static gchar *lxdream_config_load_filename = NULL;
71 static gchar *lxdream_config_save_filename = NULL;
73 void lxdream_register_config_group( const gchar *key, lxdream_config_group_t group )
76 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
77 if( lxdream_config_root[i] == NULL ) {
78 lxdream_config_root[i] = group;
79 lxdream_config_root[i+1] = NULL;
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_group.params[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path );
134 global_group.params[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path );
135 global_group.params[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path );
136 global_group.params[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path );
137 global_group.params[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path );
139 /* Copy defaults into main values */
140 for( int i=0; lxdream_config_root[i] != NULL; i++ ) {
141 struct lxdream_config_entry *param = lxdream_config_root[i]->params;
142 if( param != NULL ) {
143 while( param->key != NULL ) {
144 if( param->value != param->default_value ) {
145 if( param->value != NULL )
146 free( param->value );
147 param->value = (gchar *)param->default_value;
156 const gchar *lxdream_get_config_value( lxdream_config_group_t group, int key )
158 return group->params[key].value;
162 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
164 lxdream_config_entry_t param = &group->params[key];
165 if( param->value != value &&
166 (param->value == NULL || value == NULL || strcmp(param->value,value) != 0) ) {
168 gchar *new_value = g_strdup(value);
170 /* If the group defines an on_change handler, it can block the change
171 * (ie due to an invalid setting).
173 if( group->on_change == NULL ||
174 group->on_change(group->data, group,key, param->value, new_value) ) {
176 /* Don't free the default value, but otherwise need to release the
179 if( param->value != param->default_value && param->value != NULL ) {
180 free( param->value );
182 param->value = new_value;
183 } else { /* on_change handler said no. */
191 const gchar *lxdream_get_global_config_value( int key )
193 return global_group.params[key].value;
196 void lxdream_set_global_config_value( int key, const gchar *value )
198 lxdream_set_config_value(&global_group, key, value);
201 GList *lxdream_get_global_config_list_value( int key )
203 GList *result = NULL;
204 const gchar *str = lxdream_get_global_config_value( key );
206 gchar **strv = g_strsplit(str, ":",0);
208 for( i=0; strv[i] != NULL; i++ ) {
209 result = g_list_append( result, g_strdup(strv[i]) );
216 void lxdream_set_global_config_list_value( int key, const GList *list )
219 lxdream_set_global_config_value( key, NULL );
224 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
225 size += strlen( (gchar *)ptr->data ) + 1;
228 strcpy( buf, (gchar *)list->data );
229 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
231 strcat( buf, (gchar *)ptr->data );
233 lxdream_set_global_config_value( key, buf );
237 gchar *lxdream_get_global_config_path_value( int key )
239 const gchar *str = lxdream_get_global_config_value(key);
243 return get_expanded_path(str);
247 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
249 gchar *temp = get_escaped_path(value);
250 lxdream_set_global_config_value(key,temp);
252 return lxdream_get_global_config_value(key);
255 struct lxdream_config_group * lxdream_get_config_group( int group )
257 return lxdream_config_root[group];
260 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
263 for( i=0; src->params[i].key != NULL; i++ ) {
264 lxdream_set_config_value( dest, i, src->params[i].value );
268 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
272 dest->key = src->key;
273 dest->on_change = NULL;
274 dest->key_binding = NULL;
276 for( i=0; src->params[i].key != NULL; i++ ) {
277 dest->params[i].key = src->params[i].key;
278 dest->params[i].label = src->params[i].label;
279 dest->params[i].type = src->params[i].type;
280 dest->params[i].tag = src->params[i].tag;
281 dest->params[i].default_value = src->params[i].default_value;
282 dest->params[i].value = NULL;
283 lxdream_set_config_value( dest, i, src->params[i].value );
285 dest->params[i].key = NULL;
286 dest->params[i].label = NULL;
289 gboolean lxdream_load_config( )
291 if( lxdream_config_load_filename == NULL ) {
292 lxdream_find_config();
294 return lxdream_load_config_file(lxdream_config_load_filename);
297 gboolean lxdream_save_config( )
299 if( lxdream_config_save_filename == NULL ) {
300 lxdream_find_config();
302 return lxdream_save_config_file(lxdream_config_save_filename);
305 gboolean lxdream_load_config_file( const gchar *filename )
310 if( access(filename, F_OK) != 0 ) {
311 INFO( "Configuration file '%s' does not exist, creating from defaults" );
312 lxdream_set_default_config();
313 lxdream_save_config();
316 f = fopen(filename, "ro");
318 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
319 lxdream_set_default_config();
323 result = lxdream_load_config_stream( f );
328 gboolean lxdream_load_config_stream( FILE *f )
332 int maple_device = -1, maple_subdevice = -1, i;
333 struct lxdream_config_group *group = NULL;
334 struct lxdream_config_group *top_group = NULL;
335 maple_device_t device = NULL;
336 lxdream_set_default_config();
338 while( fgets( buf, sizeof(buf), f ) != NULL ) {
343 char *p = strchr(buf, ']');
345 maple_device = maple_subdevice = -1;
348 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
349 if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
350 top_group = group = lxdream_config_root[i];
355 } else if( group != NULL ) {
356 char *value = strchr( buf, '=' );
357 if( value != NULL ) {
358 struct lxdream_config_entry *param = group->params;
363 if( top_group == &controllers_group ) {
364 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
365 maple_device = strtoul( buf+7, NULL, 0 );
366 if( maple_device < 0 || maple_device > 3 ) {
367 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
371 device = maple_new_device( value );
372 if( device == NULL ) {
373 ERROR( "Unrecognized device '%s'", value );
375 group = maple_get_device_config(device);
376 maple_attach_device( device, maple_device, maple_subdevice );
379 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
380 maple_subdevice = strtoul( buf+10, NULL, 0 );
381 if( maple_device == -1 ) {
382 ERROR( "Subdevice not allowed without primary device" );
383 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
384 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
385 } else if( (device = maple_new_device(value)) == NULL ) {
386 ERROR( "Unrecognized subdevice '%s'", value );
388 group = maple_get_device_config(device);
389 maple_attach_device( device, maple_device, maple_subdevice );
394 while( param->key != NULL ) {
395 if( strcasecmp( param->key, buf ) == 0 ) {
396 param->value = g_strdup(value);
407 gboolean lxdream_save_config_file( const gchar *filename )
409 FILE *f = fopen(filename, "wo");
412 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
415 result = lxdream_save_config_stream(f);
420 gboolean lxdream_save_config_stream( FILE *f )
423 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
424 fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
426 if( lxdream_config_root[i] == &controllers_group ) {
428 for( i=0; i<4; i++ ) {
429 for( j=0; j<6; j++ ) {
430 maple_device_t dev = maple_get_device( i, j );
433 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
435 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
436 lxdream_config_group_t group = maple_get_device_config(dev);
437 if( group != NULL ) {
438 lxdream_config_entry_t entry = group->params;
439 while( entry->key != NULL ) {
440 if( entry->value != NULL ) {
441 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
450 struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
451 while( entry->key != NULL ) {
452 if( entry->value != NULL ) {
453 fprintf( f, "%s = %s\n", entry->key, entry->value );
463 void lxdream_make_config_dir( )
465 const char *user_path = get_user_data_path();
468 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
469 (st.st_mode & S_IFDIR) != 0 ) {
474 if( mkdir( user_path, 0777 ) != 0 ) {
475 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
479 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
480 mkdir( vmupath, 0777 );
483 char *savepath = g_strdup_printf( "%s/save", user_path );
484 mkdir( savepath, 0777 );
.