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>
26 #include <sys/types.h>
31 #include "maple/maple.h"
33 #define MAX_ROOT_GROUPS 16
35 extern struct lxdream_config_entry alsa_config[];
36 extern struct lxdream_config_entry hotkeys_config[];
38 gboolean lxdream_load_config_file( const gchar *filename );
39 gboolean lxdream_save_config_file( const gchar *filename );
40 gboolean lxdream_load_config_stream( FILE *f );
41 gboolean lxdream_save_config_stream( FILE *f );
43 static struct lxdream_config_entry global_config[] =
44 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
45 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
46 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
47 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
48 { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
49 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
50 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
51 { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
52 { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
53 { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
54 { NULL, CONFIG_TYPE_NONE }};
56 static struct lxdream_config_entry serial_config[] =
57 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
58 { NULL, CONFIG_TYPE_NONE }};
60 struct lxdream_config_group lxdream_config_root[MAX_ROOT_GROUPS+1] =
61 {{ "global", global_config },
62 { "controllers", NULL },
63 { "hotkeys", hotkeys_config },
64 { "serial", serial_config },
65 { NULL, CONFIG_TYPE_NONE }};
67 static gchar *lxdream_config_load_filename = NULL;
68 static gchar *lxdream_config_save_filename = NULL;
70 void lxdream_register_config_group( const gchar *key, lxdream_config_entry_t group )
73 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
74 if( lxdream_config_root[i].key == NULL ) {
75 lxdream_config_root[i].key = key;
76 lxdream_config_root[i].params = group;
77 lxdream_config_root[i+1].key = NULL;
78 lxdream_config_root[i+1].params = CONFIG_TYPE_NONE;
82 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
85 gboolean lxdream_find_config()
87 gboolean result = TRUE;
88 char *home = getenv("HOME");
89 if( lxdream_config_save_filename == NULL ) {
90 /* For compatibility, look for the old ~/.lxdreamrc first. Use it if
91 * found, otherwise use the new ~/.lxdream/lxdreamrc.
93 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
94 if( access(lxdream_config_save_filename, R_OK) != 0 ) {
95 g_free(lxdream_config_save_filename);
96 const char *user_path = get_user_data_path();
97 lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
100 if( lxdream_config_load_filename == NULL ) {
101 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
102 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
103 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
105 } else if( access( sysconfig, R_OK ) == 0 ) {
106 lxdream_config_load_filename = sysconfig;
108 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
116 void lxdream_set_config_filename( const gchar *filename )
118 if( lxdream_config_load_filename != NULL ) {
119 g_free(lxdream_config_load_filename);
121 lxdream_config_load_filename = g_strdup(filename);
122 if( lxdream_config_save_filename != NULL ) {
123 g_free(lxdream_config_save_filename);
125 lxdream_config_save_filename = g_strdup(filename);
128 void lxdream_set_default_config( )
130 /* Construct platform dependent defaults */
131 const gchar *user_path = get_user_data_path();
132 global_config[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path );
133 global_config[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path );
134 global_config[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path );
135 global_config[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path );
136 global_config[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path );
138 /* Copy defaults into main values */
139 struct lxdream_config_group *group = lxdream_config_root;
140 while( group->key != NULL ) {
141 struct lxdream_config_entry *param = group->params;
142 if( param != NULL ) {
143 while( param->key != NULL ) {
144 if( param->value != param->default_value ) {
145 if( param->value != NULL )
146 free( param->value );
147 param->value = (gchar *)param->default_value;
157 const gchar *lxdream_get_global_config_value( int key )
159 return global_config[key].value;
162 GList *lxdream_get_global_config_list_value( int key )
164 GList *result = NULL;
165 const gchar *str = lxdream_get_global_config_value( key );
167 gchar **strv = g_strsplit(str, ":",0);
169 for( i=0; strv[i] != NULL; i++ ) {
170 result = g_list_append( result, g_strdup(strv[i]) );
177 void lxdream_set_global_config_list_value( int key, const GList *list )
180 lxdream_set_global_config_value( key, NULL );
185 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
186 size += strlen( (gchar *)ptr->data ) + 1;
189 strcpy( buf, (gchar *)list->data );
190 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
192 strcat( buf, (gchar *)ptr->data );
194 lxdream_set_global_config_value( key, buf );
198 gchar *lxdream_get_global_config_path_value( int key )
200 const gchar *str = lxdream_get_global_config_value(key);
204 return get_expanded_path(str);
208 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
210 gchar *temp = get_escaped_path(value);
211 lxdream_set_global_config_value(key,temp);
213 return lxdream_get_global_config_value(key);
216 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
218 if( param->value != value ) {
219 if( param->value != param->default_value && param->value != NULL ) {
220 free( param->value );
222 param->value = g_strdup(value);
226 void lxdream_set_global_config_value( int key, const gchar *value )
228 lxdream_set_config_value(&global_config[key], value);
231 const struct lxdream_config_entry * lxdream_get_global_config_entry( int key )
233 return &global_config[key];
236 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
239 for( i=0; group->params[i].key != NULL; i++ ) {
240 if( strcasecmp( group->params[i].key, key ) == 0 ) {
241 lxdream_set_config_value( &group->params[i], value );
248 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
251 for( i=0; src[i].key != NULL; i++ ) {
252 lxdream_set_config_value( &dest[i], src[i].value );
256 gboolean lxdream_load_config( )
258 if( lxdream_config_load_filename == NULL ) {
259 lxdream_find_config();
261 return lxdream_load_config_file(lxdream_config_load_filename);
264 gboolean lxdream_save_config( )
266 if( lxdream_config_save_filename == NULL ) {
267 lxdream_find_config();
269 return lxdream_save_config_file(lxdream_config_save_filename);
272 gboolean lxdream_load_config_file( const gchar *filename )
277 if( access(filename, F_OK) != 0 ) {
278 INFO( "Configuration file '%s' does not exist, creating from defaults" );
279 lxdream_set_default_config();
280 lxdream_save_config();
283 f = fopen(filename, "ro");
285 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
286 lxdream_set_default_config();
290 result = lxdream_load_config_stream( f );
295 gboolean lxdream_load_config_stream( FILE *f )
299 int maple_device = -1, maple_subdevice = -1;
300 struct lxdream_config_group devgroup;
301 struct lxdream_config_group *group = NULL;
302 maple_device_t device = NULL;
303 lxdream_set_default_config();
305 while( fgets( buf, sizeof(buf), f ) != NULL ) {
310 char *p = strchr(buf, ']');
312 struct lxdream_config_group *tmp_group;
313 maple_device = maple_subdevice = -1;
316 tmp_group = &lxdream_config_root[0];
317 while( tmp_group->key != NULL ) {
318 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
325 } else if( group != NULL ) {
326 char *value = strchr( buf, '=' );
327 if( value != NULL ) {
328 struct lxdream_config_entry *param = group->params;
333 if( strcmp(group->key,"controllers") == 0 ) {
334 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
335 maple_device = strtoul( buf+7, NULL, 0 );
336 if( maple_device < 0 || maple_device > 3 ) {
337 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
341 device = maple_new_device( value );
342 if( device == NULL ) {
343 ERROR( "Unrecognized device '%s'", value );
345 devgroup.key = "controllers";
346 devgroup.params = maple_get_device_config(device);
347 maple_attach_device( device, maple_device, maple_subdevice );
351 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
352 maple_subdevice = strtoul( buf+10, NULL, 0 );
353 if( maple_device == -1 ) {
354 ERROR( "Subdevice not allowed without primary device" );
355 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
356 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
357 } else if( (device = maple_new_device(value)) == NULL ) {
358 ERROR( "Unrecognized subdevice '%s'", value );
360 devgroup.key = "controllers";
361 devgroup.params = maple_get_device_config(device);
362 maple_attach_device( device, maple_device, maple_subdevice );
368 while( param->key != NULL ) {
369 if( strcasecmp( param->key, buf ) == 0 ) {
370 param->value = g_strdup(value);
381 gboolean lxdream_save_config_file( const gchar *filename )
383 FILE *f = fopen(filename, "wo");
386 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
389 result = lxdream_save_config_stream(f);
394 gboolean lxdream_save_config_stream( FILE *f )
396 struct lxdream_config_group *group = &lxdream_config_root[0];
398 while( group->key != NULL ) {
399 struct lxdream_config_entry *entry = group->params;
400 fprintf( f, "[%s]\n", group->key );
402 if( entry != NULL ) {
403 while( entry->key != NULL ) {
404 if( entry->value != NULL ) {
405 fprintf( f, "%s = %s\n", entry->key, entry->value );
409 } else if( strcmp(group->key, "controllers") == 0 ) {
411 for( i=0; i<4; i++ ) {
412 for( j=0; j<6; j++ ) {
413 maple_device_t dev = maple_get_device( i, j );
416 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
418 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
419 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
420 while( entry->key != NULL ) {
421 if( entry->value != NULL ) {
422 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
437 void lxdream_make_config_dir( )
439 const char *user_path = get_user_data_path();
442 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
443 (st.st_mode & S_IFDIR) != 0 ) {
448 if( mkdir( user_path, 0777 ) != 0 ) {
449 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
453 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
454 mkdir( vmupath, 0777 );
457 char *savepath = g_strdup_printf( "%s/save", user_path );
458 mkdir( savepath, 0777 );
.