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 #define MAX_ROOT_GROUPS 16
32 extern struct lxdream_config_entry alsa_config[];
33 extern struct lxdream_config_entry hotkeys_config[];
35 gboolean lxdream_load_config_file( const gchar *filename );
36 gboolean lxdream_save_config_file( const gchar *filename );
37 gboolean lxdream_load_config_stream( FILE *f );
38 gboolean lxdream_save_config_stream( FILE *f );
40 static struct lxdream_config_entry global_config[] =
41 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
42 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
43 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
44 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
45 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
46 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
47 { "recent", NULL, CONFIG_TYPE_FILE, NULL },
48 { NULL, CONFIG_TYPE_NONE }};
50 static struct lxdream_config_entry serial_config[] =
51 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
52 { NULL, CONFIG_TYPE_NONE }};
54 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] =
55 {{ "global", global_config },
56 { "controllers", NULL },
57 { "hotkeys", hotkeys_config },
58 { "serial", serial_config },
59 { NULL, CONFIG_TYPE_NONE }};
61 static gchar *lxdream_config_load_filename = NULL;
62 static gchar *lxdream_config_save_filename = NULL;
64 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
67 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
68 if( lxdream_config_root[i].key == NULL ) {
69 lxdream_config_root[i].key = key;
70 lxdream_config_root[i].params = group;
71 lxdream_config_root[i+1].key = NULL;
72 lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
76 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
79 gboolean lxdream_find_config()
81 gboolean result = TRUE;
82 char *home = getenv("HOME");
83 if( lxdream_config_save_filename == NULL ) {
84 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
86 if( lxdream_config_load_filename == NULL ) {
87 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
88 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
89 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
91 } else if( access( sysconfig, R_OK ) == 0 ) {
92 lxdream_config_load_filename = sysconfig;
93 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
94 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
97 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
105 void lxdream_set_config_filename( const gchar *filename )
107 if( lxdream_config_load_filename != NULL ) {
108 g_free(lxdream_config_load_filename);
110 lxdream_config_load_filename = g_strdup(filename);
111 if( lxdream_config_save_filename != NULL ) {
112 g_free(lxdream_config_save_filename);
114 lxdream_config_save_filename = g_strdup(filename);
117 void lxdream_set_default_config( )
119 struct lxdream_config_group *group = lxdream_config_root;
120 while( group->key != NULL ) {
121 struct lxdream_config_entry *param = group->params;
122 if( param != NULL ) {
123 while( param->key != NULL ) {
124 if( param->value != param->default_value ) {
125 if( param->value != NULL )
126 free( param->value );
127 param->value = (gchar *)param->default_value;
137 const gchar *lxdream_get_config_value( int key )
139 return global_config[key].value;
142 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
144 if( param->value != value ) {
145 if( param->value != param->default_value && param->value != NULL ) {
146 free( param->value );
148 param->value = g_strdup(value);
152 void lxdream_set_global_config_value( int key, const gchar *value )
154 lxdream_set_config_value(&global_config[key], value);
157 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
159 return &global_config[key];
162 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
165 for( i=0; group->params[i].key != NULL; i++ ) {
166 if( strcasecmp( group->params[i].key, key ) == 0 ) {
167 lxdream_set_config_value( &group->params[i], value );
174 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
177 for( i=0; src[i].key != NULL; i++ ) {
178 lxdream_set_config_value( &dest[i], src[i].value );
182 gboolean lxdream_load_config( )
184 if( lxdream_config_load_filename == NULL ) {
185 lxdream_find_config();
187 return lxdream_load_config_file(lxdream_config_load_filename);
190 gboolean lxdream_save_config( )
192 if( lxdream_config_save_filename == NULL ) {
193 lxdream_find_config();
195 return lxdream_save_config_file(lxdream_config_save_filename);
198 gboolean lxdream_load_config_file( const gchar *filename )
203 if( access(filename, F_OK) != 0 ) {
204 INFO( "Configuration file '%s' does not exist, creating from defaults" );
205 lxdream_set_default_config();
206 lxdream_save_config();
209 f = fopen(filename, "ro");
211 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
212 lxdream_set_default_config();
216 result = lxdream_load_config_stream( f );
221 gboolean lxdream_load_config_stream( FILE *f )
225 int maple_device = -1, maple_subdevice = -1;
226 struct lxdream_config_group devgroup;
227 struct lxdream_config_group *group = NULL;
228 maple_device_t device = NULL;
229 lxdream_set_default_config();
231 while( fgets( buf, sizeof(buf), f ) != NULL ) {
236 char *p = strchr(buf, ']');
238 struct lxdream_config_group *tmp_group;
239 maple_device = maple_subdevice = -1;
242 tmp_group = &lxdream_config_root[0];
243 while( tmp_group->key != NULL ) {
244 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
251 } else if( group != NULL ) {
252 char *value = strchr( buf, '=' );
253 if( value != NULL ) {
254 struct lxdream_config_entry *param = group->params;
259 if( strcmp(group->key,"controllers") == 0 ) {
260 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
261 maple_device = strtoul( buf+7, NULL, 0 );
262 if( maple_device < 0 || maple_device > 3 ) {
263 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
267 device = maple_new_device( value );
268 if( device == NULL ) {
269 ERROR( "Unrecognized device '%s'", value );
271 devgroup.key = "controllers";
272 devgroup.params = maple_get_device_config(device);
273 maple_attach_device( device, maple_device, maple_subdevice );
277 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
278 maple_subdevice = strtoul( buf+10, NULL, 0 );
279 if( maple_device == -1 ) {
280 ERROR( "Subdevice not allowed without primary device" );
281 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
282 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
283 } else if( (device = maple_new_device(value)) == NULL ) {
284 ERROR( "Unrecognized subdevice '%s'", value );
286 devgroup.key = "controllers";
287 devgroup.params = maple_get_device_config(device);
288 maple_attach_device( device, maple_device, maple_subdevice );
294 while( param->key != NULL ) {
295 if( strcasecmp( param->key, buf ) == 0 ) {
296 param->value = g_strdup(value);
307 gboolean lxdream_save_config_file( const gchar *filename )
309 FILE *f = fopen(filename, "wo");
312 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
315 result = lxdream_save_config_stream(f);
320 gboolean lxdream_save_config_stream( FILE *f )
322 struct lxdream_config_group *group = &lxdream_config_root[0];
324 while( group->key != NULL ) {
325 struct lxdream_config_entry *entry = group->params;
326 fprintf( f, "[%s]\n", group->key );
328 if( entry != NULL ) {
329 while( entry->key != NULL ) {
330 if( entry->value != NULL ) {
331 fprintf( f, "%s = %s\n", entry->key, entry->value );
335 } else if( strcmp(group->key, "controllers") == 0 ) {
337 for( i=0; i<4; i++ ) {
338 for( j=0; j<6; j++ ) {
339 maple_device_t dev = maple_get_device( i, j );
342 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
344 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
345 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
346 while( entry->key != NULL ) {
347 if( entry->value != NULL ) {
348 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.