Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 464:8e099fad42a6
prev460:a0c865b74c63
next470:e09a16196693
author nkeynes
date Sat Oct 27 05:47:21 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Fix AICA save state section to include the current audio + IO state
view annotate diff log raw
     1 /**
     2  * $Id: config.c,v 1.4 2007-10-27 05:44:53 nkeynes Exp $
     3  *
     4  * User configuration support
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <unistd.h>
    20 #include <stdio.h>
    21 #include <errno.h>
    22 #include <stdlib.h>
    23 #include <string.h>
    24 #include <glib/gstrfuncs.h>
    25 #include "dream.h"
    26 #include "config.h"
    27 #include "maple/maple.h"
    29 gboolean lxdream_load_config_file( const gchar *filename );
    30 gboolean lxdream_save_config_file( const gchar *filename );
    31 gboolean lxdream_load_config_stream( FILE *f );
    32 gboolean lxdream_save_config_stream( FILE *f );
    34 static struct lxdream_config_entry global_config[] =
    35     {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
    36      { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
    37      { "default path", CONFIG_TYPE_PATH, "." },
    38      { "save path", CONFIG_TYPE_PATH, "save" },
    39      { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
    40      { "gdrom", CONFIG_TYPE_FILE, NULL },
    41      { NULL, CONFIG_TYPE_NONE }};
    43 static struct lxdream_config_entry serial_config[] =
    44     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
    45      { NULL, CONFIG_TYPE_NONE }};
    47 struct lxdream_config_group lxdream_config_root[] = 
    48     {{ "global", global_config },
    49      { "controllers", NULL },
    50      { "serial", serial_config },
    51      { NULL, CONFIG_TYPE_NONE }};
    53 static gchar *lxdream_config_load_filename = NULL;
    54 static gchar *lxdream_config_save_filename = NULL;
    56 gboolean lxdream_find_config()
    57 {
    58     char *home = getenv("HOME");
    59     if( lxdream_config_save_filename == NULL ) {
    60 	lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    61     }
    62     if( lxdream_config_load_filename == NULL ) {
    63 	if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    64 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    65 	} else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    66 	    lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
    67 	} else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    68 	    lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
    69 	} else {
    70 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    71 	}	
    72     } 
    73 }
    75 void lxdream_set_config_filename( const gchar *filename )
    76 {
    77     if( lxdream_config_load_filename != NULL ) {
    78 	g_free(lxdream_config_load_filename);
    79     }
    80     lxdream_config_load_filename = g_strdup(filename);
    81     if( lxdream_config_save_filename != NULL ) {
    82 	g_free(lxdream_config_save_filename);
    83     }
    84     lxdream_config_save_filename = g_strdup(filename);
    85 }
    87 void lxdream_set_default_config( )
    88 {
    89     struct lxdream_config_group *group = lxdream_config_root;
    90     while( group->key != NULL ) {
    91 	struct lxdream_config_entry *param = group->params;
    92 	if( param != NULL ) {
    93 	    while( param->key != NULL ) {
    94 		if( param->value != param->default_value ) {
    95 		    if( param->value != NULL )
    96 			free( param->value );
    97 		    param->value = (gchar *)param->default_value;
    98 		}
    99 		param++;
   100 	    }
   101 	}
   102 	group++;
   103     }
   104     maple_detach_all();
   105 }
   107 const gchar *lxdream_get_config_value( int key )
   108 {
   109     return global_config[key].value;
   110 }
   112 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
   113 {
   114     if( param->value != param->default_value && param->value != NULL ) {
   115 	free( param->value );
   116     }
   117     param->value = g_strdup(value);
   118 }
   120 void lxdream_set_global_config_value( int key, const gchar *value )
   121 {
   122     lxdream_set_config_value(&global_config[key], value);
   123 }
   125 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   126 {
   127     int i;
   128     for( i=0; group->params[i].key != NULL; i++ ) {
   129 	if( strcasecmp( group->params[i].key, key ) == 0 ) {
   130 	    lxdream_set_config_value( &group->params[i], value );
   131 	    return TRUE;
   132 	}
   133     }
   134     return FALSE;
   135 }
   137 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
   138 {
   139     int i;
   140     for( i=0; src[i].key != NULL; i++ ) {
   141 	lxdream_set_config_value( &dest[i], src[i].value );
   142     }
   143 }
   145 gboolean lxdream_load_config( )
   146 {
   147     if( lxdream_config_load_filename == NULL ) {
   148 	lxdream_find_config();
   149     }
   150     return lxdream_load_config_file(lxdream_config_load_filename);
   151 }
   153 gboolean lxdream_save_config( )
   154 {
   155     if( lxdream_config_save_filename == NULL ) {
   156 	lxdream_find_config();
   157     }
   158     return lxdream_save_config_file(lxdream_config_save_filename);
   159 }
   161 gboolean lxdream_load_config_file( const gchar *filename )
   162 {
   163     FILE *f;
   164     gboolean result;
   166     if( access(filename, F_OK) != 0 ) {
   167 	INFO( "Configuration file '%s' does not exist, creating from defaults" );
   168 	lxdream_set_default_config();
   169 	lxdream_save_config();
   170     }
   172     f = fopen(filename, "ro");
   173     if( f == NULL ) {
   174 	ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   175 	lxdream_set_default_config();
   176 	return FALSE;
   177     }
   179     result = lxdream_load_config_stream( f );
   180     fclose(f);
   181     return result;
   182 }
   184 gboolean lxdream_load_config_stream( FILE *f )
   185 {
   187     char buf[512];
   188     int maple_device = -1, maple_subdevice = -1;
   189     struct lxdream_config_group devgroup;
   190     struct lxdream_config_group *group = NULL;
   191     maple_device_t device = NULL;
   192     lxdream_set_default_config();
   194     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   195 	g_strstrip(buf);
   196 	if( buf[0] == '#' )
   197 	    continue;
   198 	if( *buf == '[' ) {
   199 	    char *p = strchr(buf, ']');
   200 	    if( p != NULL ) {
   201 		struct lxdream_config_group *tmp_group;
   202 		maple_device = maple_subdevice = -1;
   203 		*p = '\0';
   204 		g_strstrip(buf+1);
   205 		tmp_group = &lxdream_config_root[0];
   206 		while( tmp_group->key != NULL ) {
   207 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   208 			group = tmp_group;
   209 			break;
   210 		    }
   211 		    tmp_group++;
   212 		}
   213 	    }
   214 	} else if( group != NULL ) {
   215 	    char *value = strchr( buf, '=' );
   216 	    if( value != NULL ) {
   217 		struct lxdream_config_entry *param = group->params;
   218 		*value = '\0';
   219 		value++;
   220 		g_strstrip(buf);
   221 		g_strstrip(value);
   222 		if( strcmp(group->key,"controllers") == 0  ) {
   223 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   224 			maple_device = strtoul( buf+7, NULL, 0 );
   225 			if( maple_device < 0 || maple_device > 3 ) {
   226 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   227 			    continue;
   228 			}
   229 			maple_subdevice = 0;
   230 			device = maple_new_device( value );
   231 			if( device == NULL ) {
   232 			    ERROR( "Unrecognized device '%s'", value );
   233 			} else {
   234 			    devgroup.key = "controllers";
   235 			    devgroup.params = maple_get_device_config(device);
   236 			    maple_attach_device( device, maple_device, maple_subdevice );
   237 			    group = &devgroup;
   238 			}
   239 			continue;
   240 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   241 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   242 			if( maple_device == -1 ) {
   243 			    ERROR( "Subdevice not allowed without primary device" );
   244 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   245 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   246 			} else if( (device = maple_new_device(value)) == NULL ) {
   247 			    ERROR( "Unrecognized subdevice '%s'", value );
   248 			} else {
   249 			    devgroup.key = "controllers";
   250 			    devgroup.params = maple_get_device_config(device);
   251 			    maple_attach_device( device, maple_device, maple_subdevice );
   252 			    group = &devgroup;
   253 			}
   254 			continue;
   255 		    }
   256 		}
   257 		while( param->key != NULL ) {
   258 		    if( strcasecmp( param->key, buf ) == 0 ) {
   259 			param->value = g_strdup(value);
   260 			break;
   261 		    }
   262 		    param++;
   263 		}
   264 	    }
   265 	}
   266     }
   267     return TRUE;
   268 }
   270 gboolean lxdream_save_config_file( const gchar *filename )
   271 {
   272     FILE *f = fopen(filename, "wo");
   273     gboolean result;
   274     if( f == NULL ) {
   275 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   276 	return FALSE;
   277     }
   278     result = lxdream_save_config_stream(f);
   279     fclose(f);
   280     return TRUE;
   281 }    
   283 gboolean lxdream_save_config_stream( FILE *f )
   284 {
   285     struct lxdream_config_group *group = &lxdream_config_root[0];
   287     while( group->key != NULL ) {
   288 	struct lxdream_config_entry *entry = group->params;
   289 	fprintf( f, "[%s]\n", group->key );
   291 	if( entry != NULL ) {
   292 	    while( entry->key != NULL ) {
   293 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   294 		entry++;
   295 	    }
   296 	} else if( strcmp(group->key, "controllers") == 0 ) {
   297 	    int i,j;
   298 	    for( i=0; i<4; i++ ) {
   299 		for( j=0; j<6; j++ ) {
   300 		    maple_device_t dev = maple_get_device( i, j );
   301 		    if( dev != NULL ) {
   302 			if( j == 0 )
   303 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   304 			else 
   305 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   306 			entry = dev->get_config(dev);
   307 			while( entry->key != NULL ) {
   308 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   309 			    entry++;
   310 			}
   311 		    }
   312 		}
   313 	    }
   314 	}
   315 	fprintf( f, "\n" );
   316 	group++;
   317     }
   318     return TRUE;
   319 }
.