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[];
32 extern struct lxdream_config_entry hotkeys_config[];
34 gboolean lxdream_load_config_file( const gchar *filename );
35 gboolean lxdream_save_config_file( const gchar *filename );
36 gboolean lxdream_load_config_stream( FILE *f );
37 gboolean lxdream_save_config_stream( FILE *f );
39 static struct lxdream_config_entry global_config[] =
40 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
41 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
42 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
43 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
44 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
45 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
46 { "recent", NULL, CONFIG_TYPE_FILE, NULL },
47 { NULL, CONFIG_TYPE_NONE }};
49 static struct lxdream_config_entry serial_config[] =
50 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
51 { NULL, CONFIG_TYPE_NONE }};
53 struct lxdream_config_group lxdream_config_root[] =
54 {{ "global", global_config },
55 { "controllers", NULL },
56 { "hotkeys", hotkeys_config },
57 { "serial", serial_config },
59 { "alsa", alsa_config },
61 { NULL, CONFIG_TYPE_NONE }};
63 static gchar *lxdream_config_load_filename = NULL;
64 static gchar *lxdream_config_save_filename = NULL;
66 gboolean lxdream_find_config()
68 gboolean result = TRUE;
69 char *home = getenv("HOME");
70 if( lxdream_config_save_filename == NULL ) {
71 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
73 if( lxdream_config_load_filename == NULL ) {
74 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
75 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
76 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
78 } else if( access( sysconfig, R_OK ) == 0 ) {
79 lxdream_config_load_filename = sysconfig;
80 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
81 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
84 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
92 void lxdream_set_config_filename( const gchar *filename )
94 if( lxdream_config_load_filename != NULL ) {
95 g_free(lxdream_config_load_filename);
97 lxdream_config_load_filename = g_strdup(filename);
98 if( lxdream_config_save_filename != NULL ) {
99 g_free(lxdream_config_save_filename);
101 lxdream_config_save_filename = g_strdup(filename);
104 void lxdream_set_default_config( )
106 struct lxdream_config_group *group = lxdream_config_root;
107 while( group->key != NULL ) {
108 struct lxdream_config_entry *param = group->params;
109 if( param != NULL ) {
110 while( param->key != NULL ) {
111 if( param->value != param->default_value ) {
112 if( param->value != NULL )
113 free( param->value );
114 param->value = (gchar *)param->default_value;
124 const gchar *lxdream_get_config_value( int key )
126 return global_config[key].value;
129 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
131 if( param->value != value ) {
132 if( param->value != param->default_value && param->value != NULL ) {
133 free( param->value );
135 param->value = g_strdup(value);
139 void lxdream_set_global_config_value( int key, const gchar *value )
141 lxdream_set_config_value(&global_config[key], value);
144 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
146 return &global_config[key];
149 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
152 for( i=0; group->params[i].key != NULL; i++ ) {
153 if( strcasecmp( group->params[i].key, key ) == 0 ) {
154 lxdream_set_config_value( &group->params[i], value );
161 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
164 for( i=0; src[i].key != NULL; i++ ) {
165 lxdream_set_config_value( &dest[i], src[i].value );
169 gboolean lxdream_load_config( )
171 if( lxdream_config_load_filename == NULL ) {
172 lxdream_find_config();
174 return lxdream_load_config_file(lxdream_config_load_filename);
177 gboolean lxdream_save_config( )
179 if( lxdream_config_save_filename == NULL ) {
180 lxdream_find_config();
182 return lxdream_save_config_file(lxdream_config_save_filename);
185 gboolean lxdream_load_config_file( const gchar *filename )
190 if( access(filename, F_OK) != 0 ) {
191 INFO( "Configuration file '%s' does not exist, creating from defaults" );
192 lxdream_set_default_config();
193 lxdream_save_config();
196 f = fopen(filename, "ro");
198 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
199 lxdream_set_default_config();
203 result = lxdream_load_config_stream( f );
208 gboolean lxdream_load_config_stream( FILE *f )
212 int maple_device = -1, maple_subdevice = -1;
213 struct lxdream_config_group devgroup;
214 struct lxdream_config_group *group = NULL;
215 maple_device_t device = NULL;
216 lxdream_set_default_config();
218 while( fgets( buf, sizeof(buf), f ) != NULL ) {
223 char *p = strchr(buf, ']');
225 struct lxdream_config_group *tmp_group;
226 maple_device = maple_subdevice = -1;
229 tmp_group = &lxdream_config_root[0];
230 while( tmp_group->key != NULL ) {
231 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
238 } else if( group != NULL ) {
239 char *value = strchr( buf, '=' );
240 if( value != NULL ) {
241 struct lxdream_config_entry *param = group->params;
246 if( strcmp(group->key,"controllers") == 0 ) {
247 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
248 maple_device = strtoul( buf+7, NULL, 0 );
249 if( maple_device < 0 || maple_device > 3 ) {
250 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
254 device = maple_new_device( value );
255 if( device == NULL ) {
256 ERROR( "Unrecognized device '%s'", value );
258 devgroup.key = "controllers";
259 devgroup.params = maple_get_device_config(device);
260 maple_attach_device( device, maple_device, maple_subdevice );
264 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
265 maple_subdevice = strtoul( buf+10, NULL, 0 );
266 if( maple_device == -1 ) {
267 ERROR( "Subdevice not allowed without primary device" );
268 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
269 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
270 } else if( (device = maple_new_device(value)) == NULL ) {
271 ERROR( "Unrecognized subdevice '%s'", value );
273 devgroup.key = "controllers";
274 devgroup.params = maple_get_device_config(device);
275 maple_attach_device( device, maple_device, maple_subdevice );
281 while( param->key != NULL ) {
282 if( strcasecmp( param->key, buf ) == 0 ) {
283 param->value = g_strdup(value);
294 gboolean lxdream_save_config_file( const gchar *filename )
296 FILE *f = fopen(filename, "wo");
299 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
302 result = lxdream_save_config_stream(f);
307 gboolean lxdream_save_config_stream( FILE *f )
309 struct lxdream_config_group *group = &lxdream_config_root[0];
311 while( group->key != NULL ) {
312 struct lxdream_config_entry *entry = group->params;
313 fprintf( f, "[%s]\n", group->key );
315 if( entry != NULL ) {
316 while( entry->key != NULL ) {
317 if( entry->value != NULL ) {
318 fprintf( f, "%s = %s\n", entry->key, entry->value );
322 } else if( strcmp(group->key, "controllers") == 0 ) {
324 for( i=0; i<4; i++ ) {
325 for( j=0; j<6; j++ ) {
326 maple_device_t dev = maple_get_device( i, j );
329 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
331 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
332 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
333 while( entry->key != NULL ) {
334 if( entry->value != NULL ) {
335 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.