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 { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, "vmu" },
46 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
47 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
48 { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
49 { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
50 { NULL, CONFIG_TYPE_NONE }};
52 static struct lxdream_config_entry serial_config[] =
53 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
54 { NULL, CONFIG_TYPE_NONE }};
56 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] =
57 {{ "global", global_config },
58 { "controllers", NULL },
59 { "hotkeys", hotkeys_config },
60 { "serial", serial_config },
61 { NULL, CONFIG_TYPE_NONE }};
63 static gchar *lxdream_config_load_filename = NULL;
64 static gchar *lxdream_config_save_filename = NULL;
66 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
69 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
70 if( lxdream_config_root[i].key == NULL ) {
71 lxdream_config_root[i].key = key;
72 lxdream_config_root[i].params = group;
73 lxdream_config_root[i+1].key = NULL;
74 lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
78 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
81 gboolean lxdream_find_config()
83 gboolean result = TRUE;
84 char *home = getenv("HOME");
85 if( lxdream_config_save_filename == NULL ) {
86 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
88 if( lxdream_config_load_filename == NULL ) {
89 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
90 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
91 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
93 } else if( access( sysconfig, R_OK ) == 0 ) {
94 lxdream_config_load_filename = sysconfig;
95 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
96 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
99 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
107 void lxdream_set_config_filename( const gchar *filename )
109 if( lxdream_config_load_filename != NULL ) {
110 g_free(lxdream_config_load_filename);
112 lxdream_config_load_filename = g_strdup(filename);
113 if( lxdream_config_save_filename != NULL ) {
114 g_free(lxdream_config_save_filename);
116 lxdream_config_save_filename = g_strdup(filename);
119 void lxdream_set_default_config( )
121 struct lxdream_config_group *group = lxdream_config_root;
122 while( group->key != NULL ) {
123 struct lxdream_config_entry *param = group->params;
124 if( param != NULL ) {
125 while( param->key != NULL ) {
126 if( param->value != param->default_value ) {
127 if( param->value != NULL )
128 free( param->value );
129 param->value = (gchar *)param->default_value;
139 const gchar *lxdream_get_config_value( int key )
141 return global_config[key].value;
144 GList *lxdream_get_global_config_list_value( int key )
146 GList *result = NULL;
147 const gchar *str = lxdream_get_config_value( key );
149 gchar **strv = g_strsplit(str, ":",0);
151 for( i=0; strv[i] != NULL; i++ ) {
152 result = g_list_append( result, g_strdup(strv[i]) );
159 void lxdream_set_global_config_list_value( int key, const GList *list )
162 lxdream_set_global_config_value( key, NULL );
167 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
168 size += strlen( (gchar *)ptr->data ) + 1;
171 strcpy( buf, (gchar *)list->data );
172 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
174 strcat( buf, (gchar *)ptr->data );
176 lxdream_set_global_config_value( key, buf );
180 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
182 if( param->value != value ) {
183 if( param->value != param->default_value && param->value != NULL ) {
184 free( param->value );
186 param->value = g_strdup(value);
190 void lxdream_set_global_config_value( int key, const gchar *value )
192 lxdream_set_config_value(&global_config[key], value);
195 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
197 return &global_config[key];
200 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
203 for( i=0; group->params[i].key != NULL; i++ ) {
204 if( strcasecmp( group->params[i].key, key ) == 0 ) {
205 lxdream_set_config_value( &group->params[i], value );
212 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
215 for( i=0; src[i].key != NULL; i++ ) {
216 lxdream_set_config_value( &dest[i], src[i].value );
220 gboolean lxdream_load_config( )
222 if( lxdream_config_load_filename == NULL ) {
223 lxdream_find_config();
225 return lxdream_load_config_file(lxdream_config_load_filename);
228 gboolean lxdream_save_config( )
230 if( lxdream_config_save_filename == NULL ) {
231 lxdream_find_config();
233 return lxdream_save_config_file(lxdream_config_save_filename);
236 gboolean lxdream_load_config_file( const gchar *filename )
241 if( access(filename, F_OK) != 0 ) {
242 INFO( "Configuration file '%s' does not exist, creating from defaults" );
243 lxdream_set_default_config();
244 lxdream_save_config();
247 f = fopen(filename, "ro");
249 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
250 lxdream_set_default_config();
254 result = lxdream_load_config_stream( f );
259 gboolean lxdream_load_config_stream( FILE *f )
263 int maple_device = -1, maple_subdevice = -1;
264 struct lxdream_config_group devgroup;
265 struct lxdream_config_group *group = NULL;
266 maple_device_t device = NULL;
267 lxdream_set_default_config();
269 while( fgets( buf, sizeof(buf), f ) != NULL ) {
274 char *p = strchr(buf, ']');
276 struct lxdream_config_group *tmp_group;
277 maple_device = maple_subdevice = -1;
280 tmp_group = &lxdream_config_root[0];
281 while( tmp_group->key != NULL ) {
282 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
289 } else if( group != NULL ) {
290 char *value = strchr( buf, '=' );
291 if( value != NULL ) {
292 struct lxdream_config_entry *param = group->params;
297 if( strcmp(group->key,"controllers") == 0 ) {
298 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
299 maple_device = strtoul( buf+7, NULL, 0 );
300 if( maple_device < 0 || maple_device > 3 ) {
301 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
305 device = maple_new_device( value );
306 if( device == NULL ) {
307 ERROR( "Unrecognized device '%s'", value );
309 devgroup.key = "controllers";
310 devgroup.params = maple_get_device_config(device);
311 maple_attach_device( device, maple_device, maple_subdevice );
315 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
316 maple_subdevice = strtoul( buf+10, NULL, 0 );
317 if( maple_device == -1 ) {
318 ERROR( "Subdevice not allowed without primary device" );
319 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
320 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
321 } else if( (device = maple_new_device(value)) == NULL ) {
322 ERROR( "Unrecognized subdevice '%s'", value );
324 devgroup.key = "controllers";
325 devgroup.params = maple_get_device_config(device);
326 maple_attach_device( device, maple_device, maple_subdevice );
332 while( param->key != NULL ) {
333 if( strcasecmp( param->key, buf ) == 0 ) {
334 param->value = g_strdup(value);
345 gboolean lxdream_save_config_file( const gchar *filename )
347 FILE *f = fopen(filename, "wo");
350 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
353 result = lxdream_save_config_stream(f);
358 gboolean lxdream_save_config_stream( FILE *f )
360 struct lxdream_config_group *group = &lxdream_config_root[0];
362 while( group->key != NULL ) {
363 struct lxdream_config_entry *entry = group->params;
364 fprintf( f, "[%s]\n", group->key );
366 if( entry != NULL ) {
367 while( entry->key != NULL ) {
368 if( entry->value != NULL ) {
369 fprintf( f, "%s = %s\n", entry->key, entry->value );
373 } else if( strcmp(group->key, "controllers") == 0 ) {
375 for( i=0; i<4; i++ ) {
376 for( j=0; j<6; j++ ) {
377 maple_device_t dev = maple_get_device( i, j );
380 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
382 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
383 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
384 while( entry->key != NULL ) {
385 if( entry->value != NULL ) {
386 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
.