Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 460:a0c865b74c63
prev458:cbb2dd12daeb
next464:8e099fad42a6
author nkeynes
date Tue Oct 23 10:47:17 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Make controller config actually work
view annotate diff log raw
     1 /**
     2  * $Id: config.c,v 1.3 2007-10-23 10:47:17 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      { NULL, CONFIG_TYPE_NONE }};
    42 static struct lxdream_config_entry serial_config[] =
    43     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
    44      { NULL, CONFIG_TYPE_NONE }};
    46 struct lxdream_config_group lxdream_config_root[] = 
    47     {{ "global", global_config },
    48      { "controllers", NULL },
    49      { "serial", serial_config },
    50      { NULL, CONFIG_TYPE_NONE }};
    52 static gchar *lxdream_config_load_filename = NULL;
    53 static gchar *lxdream_config_save_filename = NULL;
    55 gboolean lxdream_find_config()
    56 {
    57     char *home = getenv("HOME");
    58     if( lxdream_config_save_filename == NULL ) {
    59 	lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    60     }
    61     if( lxdream_config_load_filename == NULL ) {
    62 	if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    63 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    64 	} else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    65 	    lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
    66 	} else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    67 	    lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
    68 	} else {
    69 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    70 	}	
    71     } 
    72 }
    74 void lxdream_set_config_filename( const gchar *filename )
    75 {
    76     if( lxdream_config_load_filename != NULL ) {
    77 	g_free(lxdream_config_load_filename);
    78     }
    79     lxdream_config_load_filename = g_strdup(filename);
    80     if( lxdream_config_save_filename != NULL ) {
    81 	g_free(lxdream_config_save_filename);
    82     }
    83     lxdream_config_save_filename = g_strdup(filename);
    84 }
    86 void lxdream_set_default_config( )
    87 {
    88     struct lxdream_config_group *group = lxdream_config_root;
    89     while( group->key != NULL ) {
    90 	struct lxdream_config_entry *param = group->params;
    91 	if( param != NULL ) {
    92 	    while( param->key != NULL ) {
    93 		if( param->value != param->default_value ) {
    94 		    if( param->value != NULL )
    95 			free( param->value );
    96 		    param->value = (gchar *)param->default_value;
    97 		}
    98 		param++;
    99 	    }
   100 	}
   101 	group++;
   102     }
   103     maple_detach_all();
   104 }
   106 const gchar *lxdream_get_config_value( int key )
   107 {
   108     return global_config[key].value;
   109 }
   111 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
   112 {
   113     if( param->value != param->default_value && param->value != NULL ) {
   114 	free( param->value );
   115     }
   116     param->value = g_strdup(value);
   117 }
   119 void lxdream_set_global_config_value( int key, const gchar *value )
   120 {
   121     lxdream_set_config_value(&global_config[key], value);
   122 }
   124 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   125 {
   126     int i;
   127     for( i=0; group->params[i].key != NULL; i++ ) {
   128 	if( strcasecmp( group->params[i].key, key ) == 0 ) {
   129 	    lxdream_set_config_value( &group->params[i], value );
   130 	    return TRUE;
   131 	}
   132     }
   133     return FALSE;
   134 }
   136 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
   137 {
   138     int i;
   139     for( i=0; src[i].key != NULL; i++ ) {
   140 	lxdream_set_config_value( &dest[i], src[i].value );
   141     }
   142 }
   144 gboolean lxdream_load_config( )
   145 {
   146     if( lxdream_config_load_filename == NULL ) {
   147 	lxdream_find_config();
   148     }
   149     return lxdream_load_config_file(lxdream_config_load_filename);
   150 }
   152 gboolean lxdream_save_config( )
   153 {
   154     if( lxdream_config_save_filename == NULL ) {
   155 	lxdream_find_config();
   156     }
   157     return lxdream_save_config_file(lxdream_config_save_filename);
   158 }
   160 gboolean lxdream_load_config_file( const gchar *filename )
   161 {
   162     FILE *f;
   163     gboolean result;
   165     if( access(filename, F_OK) != 0 ) {
   166 	INFO( "Configuration file '%s' does not exist, creating from defaults" );
   167 	lxdream_set_default_config();
   168 	lxdream_save_config();
   169     }
   171     f = fopen(filename, "ro");
   172     if( f == NULL ) {
   173 	ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   174 	lxdream_set_default_config();
   175 	return FALSE;
   176     }
   178     result = lxdream_load_config_stream( f );
   179     fclose(f);
   180     return result;
   181 }
   183 gboolean lxdream_load_config_stream( FILE *f )
   184 {
   186     char buf[512];
   187     int maple_device = -1, maple_subdevice = -1;
   188     struct lxdream_config_group devgroup;
   189     struct lxdream_config_group *group = NULL;
   190     maple_device_t device = NULL;
   191     lxdream_set_default_config();
   193     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   194 	g_strstrip(buf);
   195 	if( buf[0] == '#' )
   196 	    continue;
   197 	if( *buf == '[' ) {
   198 	    char *p = strchr(buf, ']');
   199 	    if( p != NULL ) {
   200 		struct lxdream_config_group *tmp_group;
   201 		maple_device = maple_subdevice = -1;
   202 		*p = '\0';
   203 		g_strstrip(buf+1);
   204 		tmp_group = &lxdream_config_root[0];
   205 		while( tmp_group->key != NULL ) {
   206 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   207 			group = tmp_group;
   208 			break;
   209 		    }
   210 		    tmp_group++;
   211 		}
   212 	    }
   213 	} else if( group != NULL ) {
   214 	    char *value = strchr( buf, '=' );
   215 	    if( value != NULL ) {
   216 		struct lxdream_config_entry *param = group->params;
   217 		*value = '\0';
   218 		value++;
   219 		g_strstrip(buf);
   220 		g_strstrip(value);
   221 		if( strcmp(group->key,"controllers") == 0  ) {
   222 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   223 			maple_device = strtoul( buf+7, NULL, 0 );
   224 			if( maple_device < 0 || maple_device > 3 ) {
   225 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   226 			    continue;
   227 			}
   228 			maple_subdevice = 0;
   229 			device = maple_new_device( value );
   230 			if( device == NULL ) {
   231 			    ERROR( "Unrecognized device '%s'", value );
   232 			} else {
   233 			    devgroup.key = "controllers";
   234 			    devgroup.params = maple_get_device_config(device);
   235 			    maple_attach_device( device, maple_device, maple_subdevice );
   236 			    group = &devgroup;
   237 			}
   238 			continue;
   239 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   240 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   241 			if( maple_device == -1 ) {
   242 			    ERROR( "Subdevice not allowed without primary device" );
   243 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   244 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   245 			} else if( (device = maple_new_device(value)) == NULL ) {
   246 			    ERROR( "Unrecognized subdevice '%s'", value );
   247 			} else {
   248 			    devgroup.key = "controllers";
   249 			    devgroup.params = maple_get_device_config(device);
   250 			    maple_attach_device( device, maple_device, maple_subdevice );
   251 			    group = &devgroup;
   252 			}
   253 			continue;
   254 		    }
   255 		}
   256 		while( param->key != NULL ) {
   257 		    if( strcasecmp( param->key, buf ) == 0 ) {
   258 			param->value = g_strdup(value);
   259 			break;
   260 		    }
   261 		    param++;
   262 		}
   263 	    }
   264 	}
   265     }
   266     return TRUE;
   267 }
   269 gboolean lxdream_save_config_file( const gchar *filename )
   270 {
   271     FILE *f = fopen(filename, "wo");
   272     gboolean result;
   273     if( f == NULL ) {
   274 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   275 	return FALSE;
   276     }
   277     result = lxdream_save_config_stream(f);
   278     fclose(f);
   279     return TRUE;
   280 }    
   282 gboolean lxdream_save_config_stream( FILE *f )
   283 {
   284     struct lxdream_config_group *group = &lxdream_config_root[0];
   286     while( group->key != NULL ) {
   287 	struct lxdream_config_entry *entry = group->params;
   288 	fprintf( f, "[%s]\n", group->key );
   290 	if( entry != NULL ) {
   291 	    while( entry->key != NULL ) {
   292 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   293 		entry++;
   294 	    }
   295 	} else if( strcmp(group->key, "controllers") == 0 ) {
   296 	    int i,j;
   297 	    for( i=0; i<4; i++ ) {
   298 		for( j=0; j<6; j++ ) {
   299 		    maple_device_t dev = maple_get_device( i, j );
   300 		    if( dev != NULL ) {
   301 			if( j == 0 )
   302 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   303 			else 
   304 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   305 			entry = dev->get_config(dev);
   306 			while( entry->key != NULL ) {
   307 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   308 			    entry++;
   309 			}
   310 		    }
   311 		}
   312 	    }
   313 	}
   314 	fprintf( f, "\n" );
   315 	group++;
   316     }
   317     return TRUE;
   318 }
.