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", CONFIG_TYPE_FILE, "dcboot.rom" },
40 { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
41 { "default path", CONFIG_TYPE_PATH, "." },
42 { "save path", CONFIG_TYPE_PATH, "save" },
43 { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
44 { "gdrom", CONFIG_TYPE_FILE, NULL },
45 { "recent", CONFIG_TYPE_FILE, NULL },
46 { NULL, CONFIG_TYPE_NONE }};
48 static struct lxdream_config_entry serial_config[] =
49 {{ "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 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
73 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
74 } else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
75 lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
76 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
77 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
79 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
86 void lxdream_set_config_filename( const gchar *filename )
88 if( lxdream_config_load_filename != NULL ) {
89 g_free(lxdream_config_load_filename);
91 lxdream_config_load_filename = g_strdup(filename);
92 if( lxdream_config_save_filename != NULL ) {
93 g_free(lxdream_config_save_filename);
95 lxdream_config_save_filename = g_strdup(filename);
98 void lxdream_set_default_config( )
100 struct lxdream_config_group *group = lxdream_config_root;
101 while( group->key != NULL ) {
102 struct lxdream_config_entry *param = group->params;
103 if( param != NULL ) {
104 while( param->key != NULL ) {
105 if( param->value != param->default_value ) {
106 if( param->value != NULL )
107 free( param->value );
108 param->value = (gchar *)param->default_value;
118 const gchar *lxdream_get_config_value( int key )
120 return global_config[key].value;
123 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
125 if( param->value != value ) {
126 if( param->value != param->default_value && param->value != NULL ) {
127 free( param->value );
129 param->value = g_strdup(value);
133 void lxdream_set_global_config_value( int key, const gchar *value )
135 lxdream_set_config_value(&global_config[key], value);
138 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
141 for( i=0; group->params[i].key != NULL; i++ ) {
142 if( strcasecmp( group->params[i].key, key ) == 0 ) {
143 lxdream_set_config_value( &group->params[i], value );
150 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
153 for( i=0; src[i].key != NULL; i++ ) {
154 lxdream_set_config_value( &dest[i], src[i].value );
158 gboolean lxdream_load_config( )
160 if( lxdream_config_load_filename == NULL ) {
161 lxdream_find_config();
163 return lxdream_load_config_file(lxdream_config_load_filename);
166 gboolean lxdream_save_config( )
168 if( lxdream_config_save_filename == NULL ) {
169 lxdream_find_config();
171 return lxdream_save_config_file(lxdream_config_save_filename);
174 gboolean lxdream_load_config_file( const gchar *filename )
179 if( access(filename, F_OK) != 0 ) {
180 INFO( "Configuration file '%s' does not exist, creating from defaults" );
181 lxdream_set_default_config();
182 lxdream_save_config();
185 f = fopen(filename, "ro");
187 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
188 lxdream_set_default_config();
192 result = lxdream_load_config_stream( f );
197 gboolean lxdream_load_config_stream( FILE *f )
201 int maple_device = -1, maple_subdevice = -1;
202 struct lxdream_config_group devgroup;
203 struct lxdream_config_group *group = NULL;
204 maple_device_t device = NULL;
205 lxdream_set_default_config();
207 while( fgets( buf, sizeof(buf), f ) != NULL ) {
212 char *p = strchr(buf, ']');
214 struct lxdream_config_group *tmp_group;
215 maple_device = maple_subdevice = -1;
218 tmp_group = &lxdream_config_root[0];
219 while( tmp_group->key != NULL ) {
220 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
227 } else if( group != NULL ) {
228 char *value = strchr( buf, '=' );
229 if( value != NULL ) {
230 struct lxdream_config_entry *param = group->params;
235 if( strcmp(group->key,"controllers") == 0 ) {
236 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
237 maple_device = strtoul( buf+7, NULL, 0 );
238 if( maple_device < 0 || maple_device > 3 ) {
239 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
243 device = maple_new_device( value );
244 if( device == NULL ) {
245 ERROR( "Unrecognized device '%s'", value );
247 devgroup.key = "controllers";
248 devgroup.params = maple_get_device_config(device);
249 maple_attach_device( device, maple_device, maple_subdevice );
253 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
254 maple_subdevice = strtoul( buf+10, NULL, 0 );
255 if( maple_device == -1 ) {
256 ERROR( "Subdevice not allowed without primary device" );
257 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
258 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
259 } else if( (device = maple_new_device(value)) == NULL ) {
260 ERROR( "Unrecognized subdevice '%s'", value );
262 devgroup.key = "controllers";
263 devgroup.params = maple_get_device_config(device);
264 maple_attach_device( device, maple_device, maple_subdevice );
270 while( param->key != NULL ) {
271 if( strcasecmp( param->key, buf ) == 0 ) {
272 param->value = g_strdup(value);
283 gboolean lxdream_save_config_file( const gchar *filename )
285 FILE *f = fopen(filename, "wo");
288 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
291 result = lxdream_save_config_stream(f);
296 gboolean lxdream_save_config_stream( FILE *f )
298 struct lxdream_config_group *group = &lxdream_config_root[0];
300 while( group->key != NULL ) {
301 struct lxdream_config_entry *entry = group->params;
302 fprintf( f, "[%s]\n", group->key );
304 if( entry != NULL ) {
305 while( entry->key != NULL ) {
306 if( entry->value != NULL ) {
307 fprintf( f, "%s = %s\n", entry->key, entry->value );
311 } else if( strcmp(group->key, "controllers") == 0 ) {
313 for( i=0; i<4; i++ ) {
314 for( j=0; j<6; j++ ) {
315 maple_device_t dev = maple_get_device( i, j );
318 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
320 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
321 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
322 while( entry->key != NULL ) {
323 if( entry->value != NULL ) {
324 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.