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