2 * $Id: config.c,v 1.5 2007-10-28 08:29:29 nkeynes Exp $
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/gstrfuncs.h>
27 #include "maple/maple.h"
29 gboolean lxdream_load_config_file( const gchar *filename );
30 gboolean lxdream_save_config_file( const gchar *filename );
31 gboolean lxdream_load_config_stream( FILE *f );
32 gboolean lxdream_save_config_stream( FILE *f );
34 static struct lxdream_config_entry global_config[] =
35 {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
36 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
37 { "default path", CONFIG_TYPE_PATH, "." },
38 { "save path", CONFIG_TYPE_PATH, "save" },
39 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
40 { "gdrom", CONFIG_TYPE_FILE, NULL },
41 { "recent", CONFIG_TYPE_FILE, NULL },
42 { NULL, CONFIG_TYPE_NONE }};
44 static struct lxdream_config_entry serial_config[] =
45 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
46 { NULL, CONFIG_TYPE_NONE }};
48 struct lxdream_config_group lxdream_config_root[] =
49 {{ "global", global_config },
50 { "controllers", NULL },
51 { "serial", serial_config },
52 { NULL, CONFIG_TYPE_NONE }};
54 static gchar *lxdream_config_load_filename = NULL;
55 static gchar *lxdream_config_save_filename = NULL;
57 gboolean lxdream_find_config()
59 char *home = getenv("HOME");
60 if( lxdream_config_save_filename == NULL ) {
61 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
63 if( lxdream_config_load_filename == NULL ) {
64 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
65 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
66 } else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
67 lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
68 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
69 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
71 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
76 void lxdream_set_config_filename( const gchar *filename )
78 if( lxdream_config_load_filename != NULL ) {
79 g_free(lxdream_config_load_filename);
81 lxdream_config_load_filename = g_strdup(filename);
82 if( lxdream_config_save_filename != NULL ) {
83 g_free(lxdream_config_save_filename);
85 lxdream_config_save_filename = g_strdup(filename);
88 void lxdream_set_default_config( )
90 struct lxdream_config_group *group = lxdream_config_root;
91 while( group->key != NULL ) {
92 struct lxdream_config_entry *param = group->params;
94 while( param->key != NULL ) {
95 if( param->value != param->default_value ) {
96 if( param->value != NULL )
98 param->value = (gchar *)param->default_value;
108 const gchar *lxdream_get_config_value( int key )
110 return global_config[key].value;
113 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
115 if( param->value != param->default_value && param->value != NULL ) {
116 free( param->value );
118 param->value = g_strdup(value);
121 void lxdream_set_global_config_value( int key, const gchar *value )
123 lxdream_set_config_value(&global_config[key], value);
126 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
129 for( i=0; group->params[i].key != NULL; i++ ) {
130 if( strcasecmp( group->params[i].key, key ) == 0 ) {
131 lxdream_set_config_value( &group->params[i], value );
138 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
141 for( i=0; src[i].key != NULL; i++ ) {
142 lxdream_set_config_value( &dest[i], src[i].value );
146 gboolean lxdream_load_config( )
148 if( lxdream_config_load_filename == NULL ) {
149 lxdream_find_config();
151 return lxdream_load_config_file(lxdream_config_load_filename);
154 gboolean lxdream_save_config( )
156 if( lxdream_config_save_filename == NULL ) {
157 lxdream_find_config();
159 return lxdream_save_config_file(lxdream_config_save_filename);
162 gboolean lxdream_load_config_file( const gchar *filename )
167 if( access(filename, F_OK) != 0 ) {
168 INFO( "Configuration file '%s' does not exist, creating from defaults" );
169 lxdream_set_default_config();
170 lxdream_save_config();
173 f = fopen(filename, "ro");
175 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
176 lxdream_set_default_config();
180 result = lxdream_load_config_stream( f );
185 gboolean lxdream_load_config_stream( FILE *f )
189 int maple_device = -1, maple_subdevice = -1;
190 struct lxdream_config_group devgroup;
191 struct lxdream_config_group *group = NULL;
192 maple_device_t device = NULL;
193 lxdream_set_default_config();
195 while( fgets( buf, sizeof(buf), f ) != NULL ) {
200 char *p = strchr(buf, ']');
202 struct lxdream_config_group *tmp_group;
203 maple_device = maple_subdevice = -1;
206 tmp_group = &lxdream_config_root[0];
207 while( tmp_group->key != NULL ) {
208 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
215 } else if( group != NULL ) {
216 char *value = strchr( buf, '=' );
217 if( value != NULL ) {
218 struct lxdream_config_entry *param = group->params;
223 if( strcmp(group->key,"controllers") == 0 ) {
224 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
225 maple_device = strtoul( buf+7, NULL, 0 );
226 if( maple_device < 0 || maple_device > 3 ) {
227 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
231 device = maple_new_device( value );
232 if( device == NULL ) {
233 ERROR( "Unrecognized device '%s'", value );
235 devgroup.key = "controllers";
236 devgroup.params = maple_get_device_config(device);
237 maple_attach_device( device, maple_device, maple_subdevice );
241 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
242 maple_subdevice = strtoul( buf+10, NULL, 0 );
243 if( maple_device == -1 ) {
244 ERROR( "Subdevice not allowed without primary device" );
245 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
246 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
247 } else if( (device = maple_new_device(value)) == NULL ) {
248 ERROR( "Unrecognized subdevice '%s'", value );
250 devgroup.key = "controllers";
251 devgroup.params = maple_get_device_config(device);
252 maple_attach_device( device, maple_device, maple_subdevice );
258 while( param->key != NULL ) {
259 if( strcasecmp( param->key, buf ) == 0 ) {
260 param->value = g_strdup(value);
271 gboolean lxdream_save_config_file( const gchar *filename )
273 FILE *f = fopen(filename, "wo");
276 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
279 result = lxdream_save_config_stream(f);
284 gboolean lxdream_save_config_stream( FILE *f )
286 struct lxdream_config_group *group = &lxdream_config_root[0];
288 while( group->key != NULL ) {
289 struct lxdream_config_entry *entry = group->params;
290 fprintf( f, "[%s]\n", group->key );
292 if( entry != NULL ) {
293 while( entry->key != NULL ) {
294 if( entry->value != NULL ) {
295 fprintf( f, "%s = %s\n", entry->key, entry->value );
299 } else if( strcmp(group->key, "controllers") == 0 ) {
301 for( i=0; i<4; i++ ) {
302 for( j=0; j<6; j++ ) {
303 maple_device_t dev = maple_get_device( i, j );
306 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
308 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
309 entry = dev->get_config(dev);
310 while( entry->key != NULL ) {
311 if( entry->value != NULL ) {
312 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.