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>
28 #include "maple/maple.h"
31 extern struct lxdream_config_entry alsa_config[];
33 gboolean lxdream_load_config_file( const gchar *filename );
34 gboolean lxdream_save_config_file( const gchar *filename );
35 gboolean lxdream_load_config_stream( FILE *f );
36 gboolean lxdream_save_config_stream( FILE *f );
38 static struct lxdream_config_entry global_config[] =
39 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
40 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
41 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
42 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
43 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
44 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
45 { "recent", NULL, CONFIG_TYPE_FILE, NULL },
46 { NULL, CONFIG_TYPE_NONE }};
48 static struct lxdream_config_entry serial_config[] =
49 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
50 { NULL, CONFIG_TYPE_NONE }};
52 struct lxdream_config_group lxdream_config_root[] =
53 {{ "global", global_config },
54 { "controllers", NULL },
55 { "serial", serial_config },
57 { "alsa", alsa_config },
59 { NULL, CONFIG_TYPE_NONE }};
61 static gchar *lxdream_config_load_filename = NULL;
62 static gchar *lxdream_config_save_filename = NULL;
64 gboolean lxdream_find_config()
66 gboolean result = TRUE;
67 char *home = getenv("HOME");
68 if( lxdream_config_save_filename == NULL ) {
69 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
71 if( lxdream_config_load_filename == NULL ) {
72 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
73 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
74 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
76 } else if( access( sysconfig, R_OK ) == 0 ) {
77 lxdream_config_load_filename = sysconfig;
78 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
79 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
82 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
90 void lxdream_set_config_filename( const gchar *filename )
92 if( lxdream_config_load_filename != NULL ) {
93 g_free(lxdream_config_load_filename);
95 lxdream_config_load_filename = g_strdup(filename);
96 if( lxdream_config_save_filename != NULL ) {
97 g_free(lxdream_config_save_filename);
99 lxdream_config_save_filename = g_strdup(filename);
102 void lxdream_set_default_config( )
104 struct lxdream_config_group *group = lxdream_config_root;
105 while( group->key != NULL ) {
106 struct lxdream_config_entry *param = group->params;
107 if( param != NULL ) {
108 while( param->key != NULL ) {
109 if( param->value != param->default_value ) {
110 if( param->value != NULL )
111 free( param->value );
112 param->value = (gchar *)param->default_value;
122 const gchar *lxdream_get_config_value( int key )
124 return global_config[key].value;
127 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
129 if( param->value != value ) {
130 if( param->value != param->default_value && param->value != NULL ) {
131 free( param->value );
133 param->value = g_strdup(value);
137 void lxdream_set_global_config_value( int key, const gchar *value )
139 lxdream_set_config_value(&global_config[key], value);
142 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
144 return &global_config[key];
147 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
150 for( i=0; group->params[i].key != NULL; i++ ) {
151 if( strcasecmp( group->params[i].key, key ) == 0 ) {
152 lxdream_set_config_value( &group->params[i], value );
159 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
162 for( i=0; src[i].key != NULL; i++ ) {
163 lxdream_set_config_value( &dest[i], src[i].value );
167 gboolean lxdream_load_config( )
169 if( lxdream_config_load_filename == NULL ) {
170 lxdream_find_config();
172 return lxdream_load_config_file(lxdream_config_load_filename);
175 gboolean lxdream_save_config( )
177 if( lxdream_config_save_filename == NULL ) {
178 lxdream_find_config();
180 return lxdream_save_config_file(lxdream_config_save_filename);
183 gboolean lxdream_load_config_file( const gchar *filename )
188 if( access(filename, F_OK) != 0 ) {
189 INFO( "Configuration file '%s' does not exist, creating from defaults" );
190 lxdream_set_default_config();
191 lxdream_save_config();
194 f = fopen(filename, "ro");
196 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
197 lxdream_set_default_config();
201 result = lxdream_load_config_stream( f );
206 gboolean lxdream_load_config_stream( FILE *f )
210 int maple_device = -1, maple_subdevice = -1;
211 struct lxdream_config_group devgroup;
212 struct lxdream_config_group *group = NULL;
213 maple_device_t device = NULL;
214 lxdream_set_default_config();
216 while( fgets( buf, sizeof(buf), f ) != NULL ) {
221 char *p = strchr(buf, ']');
223 struct lxdream_config_group *tmp_group;
224 maple_device = maple_subdevice = -1;
227 tmp_group = &lxdream_config_root[0];
228 while( tmp_group->key != NULL ) {
229 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
236 } else if( group != NULL ) {
237 char *value = strchr( buf, '=' );
238 if( value != NULL ) {
239 struct lxdream_config_entry *param = group->params;
244 if( strcmp(group->key,"controllers") == 0 ) {
245 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
246 maple_device = strtoul( buf+7, NULL, 0 );
247 if( maple_device < 0 || maple_device > 3 ) {
248 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
252 device = maple_new_device( value );
253 if( device == NULL ) {
254 ERROR( "Unrecognized device '%s'", value );
256 devgroup.key = "controllers";
257 devgroup.params = maple_get_device_config(device);
258 maple_attach_device( device, maple_device, maple_subdevice );
262 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
263 maple_subdevice = strtoul( buf+10, NULL, 0 );
264 if( maple_device == -1 ) {
265 ERROR( "Subdevice not allowed without primary device" );
266 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
267 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
268 } else if( (device = maple_new_device(value)) == NULL ) {
269 ERROR( "Unrecognized subdevice '%s'", value );
271 devgroup.key = "controllers";
272 devgroup.params = maple_get_device_config(device);
273 maple_attach_device( device, maple_device, maple_subdevice );
279 while( param->key != NULL ) {
280 if( strcasecmp( param->key, buf ) == 0 ) {
281 param->value = g_strdup(value);
292 gboolean lxdream_save_config_file( const gchar *filename )
294 FILE *f = fopen(filename, "wo");
297 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
300 result = lxdream_save_config_stream(f);
305 gboolean lxdream_save_config_stream( FILE *f )
307 struct lxdream_config_group *group = &lxdream_config_root[0];
309 while( group->key != NULL ) {
310 struct lxdream_config_entry *entry = group->params;
311 fprintf( f, "[%s]\n", group->key );
313 if( entry != NULL ) {
314 while( entry->key != NULL ) {
315 if( entry->value != NULL ) {
316 fprintf( f, "%s = %s\n", entry->key, entry->value );
320 } else if( strcmp(group->key, "controllers") == 0 ) {
322 for( i=0; i<4; i++ ) {
323 for( j=0; j<6; j++ ) {
324 maple_device_t dev = maple_get_device( i, j );
327 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
329 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
330 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
331 while( entry->key != NULL ) {
332 if( entry->value != NULL ) {
333 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.