2 * $Id: config.c,v 1.2 2007-10-22 21:12:54 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 { NULL, CONFIG_TYPE_NONE }};
42 static struct lxdream_config_entry serial_config[] =
43 {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
44 { NULL, CONFIG_TYPE_NONE }};
46 struct lxdream_config_group lxdream_config_root[] =
47 {{ "global", global_config },
48 { "controllers", NULL },
49 { "serial", serial_config },
50 { NULL, CONFIG_TYPE_NONE }};
52 static gchar *lxdream_config_load_filename = NULL;
53 static gchar *lxdream_config_save_filename = NULL;
55 gboolean lxdream_find_config()
57 char *home = getenv("HOME");
58 if( lxdream_config_save_filename == NULL ) {
59 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
61 if( lxdream_config_load_filename == NULL ) {
62 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
63 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
64 } else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
65 lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
66 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
67 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
69 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
74 void lxdream_set_config_filename( const gchar *filename )
76 if( lxdream_config_load_filename != NULL ) {
77 g_free(lxdream_config_load_filename);
79 lxdream_config_load_filename = g_strdup(filename);
80 if( lxdream_config_save_filename != NULL ) {
81 g_free(lxdream_config_save_filename);
83 lxdream_config_save_filename = g_strdup(filename);
86 void lxdream_set_default_config( )
88 struct lxdream_config_group *group = lxdream_config_root;
89 while( group->key != NULL ) {
90 struct lxdream_config_entry *param = group->params;
92 while( param->key != NULL ) {
93 if( param->value != param->default_value ) {
94 if( param->value != NULL )
96 param->value = (gchar *)param->default_value;
106 const gchar *lxdream_get_config_value( int key )
108 return global_config[key].value;
111 void lxdream_set_config_value( int key, const gchar *value )
113 struct lxdream_config_entry *param = &global_config[key];
114 if( param->value != param->default_value && param->value != NULL ) {
115 free( param->value );
117 param->value = g_strdup(value);
120 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
123 for( i=0; group->params[i].key != NULL; i++ ) {
124 if( strcasecmp( group->params[i].key, key ) == 0 ) {
125 if( group->params[i].value != group->params[i].default_value &&
126 group->params[i].value != NULL ) {
127 free( group->params[i].value );
129 group->params[i].value = g_strdup( value );
136 gboolean lxdream_load_config( )
138 if( lxdream_config_load_filename == NULL ) {
139 lxdream_find_config();
141 return lxdream_load_config_file(lxdream_config_load_filename);
144 gboolean lxdream_save_config( )
146 if( lxdream_config_save_filename == NULL ) {
147 lxdream_find_config();
149 return lxdream_save_config_file(lxdream_config_save_filename);
152 gboolean lxdream_load_config_file( const gchar *filename )
157 if( access(filename, F_OK) != 0 ) {
158 INFO( "Configuration file '%s' does not exist, creating from defaults" );
159 lxdream_set_default_config();
160 lxdream_save_config();
163 f = fopen(filename, "ro");
165 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
166 lxdream_set_default_config();
170 result = lxdream_load_config_stream( f );
175 gboolean lxdream_load_config_stream( FILE *f )
179 int maple_device = -1, maple_subdevice = -1;
180 struct lxdream_config_group devgroup;
181 struct lxdream_config_group *group = NULL;
182 maple_device_t device = NULL;
183 lxdream_set_default_config();
185 while( fgets( buf, sizeof(buf), f ) != NULL ) {
190 char *p = strchr(buf, ']');
192 struct lxdream_config_group *tmp_group;
193 maple_device = maple_subdevice = -1;
196 tmp_group = &lxdream_config_root[0];
197 while( tmp_group->key != NULL ) {
198 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
205 } else if( group != NULL ) {
206 char *value = strchr( buf, '=' );
207 if( value != NULL ) {
208 struct lxdream_config_entry *param = group->params;
213 if( strcmp(group->key,"controllers") == 0 ) {
214 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
215 maple_device = strtoul( buf+7, NULL, 0 );
216 if( maple_device < 0 || maple_device > 3 ) {
217 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
221 device = maple_new_device( value );
222 if( device == NULL ) {
223 ERROR( "Unrecognized device '%s'", value );
225 devgroup.key = "controllers";
226 devgroup.params = maple_get_device_config(device);
227 maple_attach_device( device, maple_device, maple_subdevice );
231 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
232 maple_subdevice = strtoul( buf+10, NULL, 0 );
233 if( maple_device == -1 ) {
234 ERROR( "Subdevice not allowed without primary device" );
235 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
236 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
237 } else if( (device = maple_new_device(value)) == NULL ) {
238 ERROR( "Unrecognized subdevice '%s'", value );
240 devgroup.key = "controllers";
241 devgroup.params = maple_get_device_config(device);
242 maple_attach_device( device, maple_device, maple_subdevice );
248 while( param->key != NULL ) {
249 if( strcasecmp( param->key, buf ) == 0 ) {
250 param->value = g_strdup(value);
261 gboolean lxdream_save_config_file( const gchar *filename )
263 FILE *f = fopen(filename, "wo");
266 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
269 result = lxdream_save_config_stream(f);
274 gboolean lxdream_save_config_stream( FILE *f )
276 struct lxdream_config_group *group = &lxdream_config_root[0];
278 while( group->key != NULL ) {
279 struct lxdream_config_entry *entry = group->params;
280 fprintf( f, "[%s]\n", group->key );
282 if( entry != NULL ) {
283 while( entry->key != NULL ) {
284 fprintf( f, "%s = %s\n", entry->key, entry->value );
287 } else if( strcmp(group->key, "controllers") == 0 ) {
289 for( i=0; i<4; i++ ) {
290 for( j=0; j<6; j++ ) {
291 maple_device_t dev = maple_get_device( i, j );
294 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
296 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
297 entry = dev->get_config(dev);
298 while( entry->key != NULL ) {
299 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.