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"
30 gboolean lxdream_load_config_file( const gchar *filename );
31 gboolean lxdream_save_config_file( const gchar *filename );
32 gboolean lxdream_load_config_stream( FILE *f );
33 gboolean lxdream_save_config_stream( FILE *f );
35 static struct lxdream_config_entry global_config[] =
36 {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
37 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
38 { "default path", CONFIG_TYPE_PATH, "." },
39 { "save path", CONFIG_TYPE_PATH, "save" },
40 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
41 { "gdrom", CONFIG_TYPE_FILE, NULL },
42 { "recent", CONFIG_TYPE_FILE, NULL },
43 { NULL, CONFIG_TYPE_NONE }};
45 static struct lxdream_config_entry serial_config[] =
46 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
47 { NULL, CONFIG_TYPE_NONE }};
49 struct lxdream_config_group lxdream_config_root[] =
50 {{ "global", global_config },
51 { "controllers", NULL },
52 { "serial", serial_config },
53 { NULL, CONFIG_TYPE_NONE }};
55 static gchar *lxdream_config_load_filename = NULL;
56 static gchar *lxdream_config_save_filename = NULL;
58 gboolean lxdream_find_config()
60 gboolean result = TRUE;
61 char *home = getenv("HOME");
62 if( lxdream_config_save_filename == NULL ) {
63 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
65 if( lxdream_config_load_filename == NULL ) {
66 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
67 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
68 } else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
69 lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
70 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
71 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
73 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
80 void lxdream_set_config_filename( const gchar *filename )
82 if( lxdream_config_load_filename != NULL ) {
83 g_free(lxdream_config_load_filename);
85 lxdream_config_load_filename = g_strdup(filename);
86 if( lxdream_config_save_filename != NULL ) {
87 g_free(lxdream_config_save_filename);
89 lxdream_config_save_filename = g_strdup(filename);
92 void lxdream_set_default_config( )
94 struct lxdream_config_group *group = lxdream_config_root;
95 while( group->key != NULL ) {
96 struct lxdream_config_entry *param = group->params;
98 while( param->key != NULL ) {
99 if( param->value != param->default_value ) {
100 if( param->value != NULL )
101 free( param->value );
102 param->value = (gchar *)param->default_value;
112 const gchar *lxdream_get_config_value( int key )
114 return global_config[key].value;
117 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
119 if( param->value != value ) {
120 if( param->value != param->default_value && param->value != NULL ) {
121 free( param->value );
123 param->value = g_strdup(value);
127 void lxdream_set_global_config_value( int key, const gchar *value )
129 lxdream_set_config_value(&global_config[key], value);
132 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
135 for( i=0; group->params[i].key != NULL; i++ ) {
136 if( strcasecmp( group->params[i].key, key ) == 0 ) {
137 lxdream_set_config_value( &group->params[i], value );
144 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
147 for( i=0; src[i].key != NULL; i++ ) {
148 lxdream_set_config_value( &dest[i], src[i].value );
152 gboolean lxdream_load_config( )
154 if( lxdream_config_load_filename == NULL ) {
155 lxdream_find_config();
157 return lxdream_load_config_file(lxdream_config_load_filename);
160 gboolean lxdream_save_config( )
162 if( lxdream_config_save_filename == NULL ) {
163 lxdream_find_config();
165 return lxdream_save_config_file(lxdream_config_save_filename);
168 gboolean lxdream_load_config_file( const gchar *filename )
173 if( access(filename, F_OK) != 0 ) {
174 INFO( "Configuration file '%s' does not exist, creating from defaults" );
175 lxdream_set_default_config();
176 lxdream_save_config();
179 f = fopen(filename, "ro");
181 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
182 lxdream_set_default_config();
186 result = lxdream_load_config_stream( f );
191 gboolean lxdream_load_config_stream( FILE *f )
195 int maple_device = -1, maple_subdevice = -1;
196 struct lxdream_config_group devgroup;
197 struct lxdream_config_group *group = NULL;
198 maple_device_t device = NULL;
199 lxdream_set_default_config();
201 while( fgets( buf, sizeof(buf), f ) != NULL ) {
206 char *p = strchr(buf, ']');
208 struct lxdream_config_group *tmp_group;
209 maple_device = maple_subdevice = -1;
212 tmp_group = &lxdream_config_root[0];
213 while( tmp_group->key != NULL ) {
214 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
221 } else if( group != NULL ) {
222 char *value = strchr( buf, '=' );
223 if( value != NULL ) {
224 struct lxdream_config_entry *param = group->params;
229 if( strcmp(group->key,"controllers") == 0 ) {
230 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
231 maple_device = strtoul( buf+7, NULL, 0 );
232 if( maple_device < 0 || maple_device > 3 ) {
233 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
237 device = maple_new_device( value );
238 if( device == NULL ) {
239 ERROR( "Unrecognized device '%s'", value );
241 devgroup.key = "controllers";
242 devgroup.params = maple_get_device_config(device);
243 maple_attach_device( device, maple_device, maple_subdevice );
247 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
248 maple_subdevice = strtoul( buf+10, NULL, 0 );
249 if( maple_device == -1 ) {
250 ERROR( "Subdevice not allowed without primary device" );
251 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
252 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
253 } else if( (device = maple_new_device(value)) == NULL ) {
254 ERROR( "Unrecognized subdevice '%s'", value );
256 devgroup.key = "controllers";
257 devgroup.params = maple_get_device_config(device);
258 maple_attach_device( device, maple_device, maple_subdevice );
264 while( param->key != NULL ) {
265 if( strcasecmp( param->key, buf ) == 0 ) {
266 param->value = g_strdup(value);
277 gboolean lxdream_save_config_file( const gchar *filename )
279 FILE *f = fopen(filename, "wo");
282 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
285 result = lxdream_save_config_stream(f);
290 gboolean lxdream_save_config_stream( FILE *f )
292 struct lxdream_config_group *group = &lxdream_config_root[0];
294 while( group->key != NULL ) {
295 struct lxdream_config_entry *entry = group->params;
296 fprintf( f, "[%s]\n", group->key );
298 if( entry != NULL ) {
299 while( entry->key != NULL ) {
300 if( entry->value != NULL ) {
301 fprintf( f, "%s = %s\n", entry->key, entry->value );
305 } else if( strcmp(group->key, "controllers") == 0 ) {
307 for( i=0; i<4; i++ ) {
308 for( j=0; j<6; j++ ) {
309 maple_device_t dev = maple_get_device( i, j );
312 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
314 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
315 entry = dev->get_config(dev);
316 while( entry->key != NULL ) {
317 if( entry->value != NULL ) {
318 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.