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