Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 458:cbb2dd12daeb
prev450:207461e79f21
next460:a0c865b74c63
author nkeynes
date Tue Oct 23 10:40:46 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Remove (I think) last remaining glade/glade-derived files, as they're no
longer used.
view annotate diff log raw
     1 /**
     2  * $Id: config.c,v 1.2 2007-10-22 21:12:54 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( int key, const gchar *value )
   112 {
   113     struct lxdream_config_entry *param = &global_config[key];
   114     if( param->value != param->default_value && param->value != NULL ) {
   115 	free( param->value );
   116     }
   117     param->value = g_strdup(value);
   118 }
   120 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
   121 {
   122     int i;
   123     for( i=0; group->params[i].key != NULL; i++ ) {
   124 	if( strcasecmp( group->params[i].key, key ) == 0 ) {
   125 	    if( group->params[i].value != group->params[i].default_value &&
   126 		group->params[i].value != NULL ) {
   127 		free( group->params[i].value );
   128 	    }
   129 	    group->params[i].value = g_strdup( value );
   130 	    return TRUE;
   131 	}
   132     }
   133     return FALSE;
   134 }
   136 gboolean lxdream_load_config( )
   137 {
   138     if( lxdream_config_load_filename == NULL ) {
   139 	lxdream_find_config();
   140     }
   141     return lxdream_load_config_file(lxdream_config_load_filename);
   142 }
   144 gboolean lxdream_save_config( )
   145 {
   146     if( lxdream_config_save_filename == NULL ) {
   147 	lxdream_find_config();
   148     }
   149     return lxdream_save_config_file(lxdream_config_save_filename);
   150 }
   152 gboolean lxdream_load_config_file( const gchar *filename )
   153 {
   154     FILE *f;
   155     gboolean result;
   157     if( access(filename, F_OK) != 0 ) {
   158 	INFO( "Configuration file '%s' does not exist, creating from defaults" );
   159 	lxdream_set_default_config();
   160 	lxdream_save_config();
   161     }
   163     f = fopen(filename, "ro");
   164     if( f == NULL ) {
   165 	ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   166 	lxdream_set_default_config();
   167 	return FALSE;
   168     }
   170     result = lxdream_load_config_stream( f );
   171     fclose(f);
   172     return result;
   173 }
   175 gboolean lxdream_load_config_stream( FILE *f )
   176 {
   178     char buf[512];
   179     int maple_device = -1, maple_subdevice = -1;
   180     struct lxdream_config_group devgroup;
   181     struct lxdream_config_group *group = NULL;
   182     maple_device_t device = NULL;
   183     lxdream_set_default_config();
   185     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   186 	g_strstrip(buf);
   187 	if( buf[0] == '#' )
   188 	    continue;
   189 	if( *buf == '[' ) {
   190 	    char *p = strchr(buf, ']');
   191 	    if( p != NULL ) {
   192 		struct lxdream_config_group *tmp_group;
   193 		maple_device = maple_subdevice = -1;
   194 		*p = '\0';
   195 		g_strstrip(buf+1);
   196 		tmp_group = &lxdream_config_root[0];
   197 		while( tmp_group->key != NULL ) {
   198 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   199 			group = tmp_group;
   200 			break;
   201 		    }
   202 		    tmp_group++;
   203 		}
   204 	    }
   205 	} else if( group != NULL ) {
   206 	    char *value = strchr( buf, '=' );
   207 	    if( value != NULL ) {
   208 		struct lxdream_config_entry *param = group->params;
   209 		*value = '\0';
   210 		value++;
   211 		g_strstrip(buf);
   212 		g_strstrip(value);
   213 		if( strcmp(group->key,"controllers") == 0  ) {
   214 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   215 			maple_device = strtoul( buf+7, NULL, 0 );
   216 			if( maple_device < 0 || maple_device > 3 ) {
   217 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   218 			    continue;
   219 			}
   220 			maple_subdevice = 0;
   221 			device = maple_new_device( value );
   222 			if( device == NULL ) {
   223 			    ERROR( "Unrecognized device '%s'", value );
   224 			} else {
   225 			    devgroup.key = "controllers";
   226 			    devgroup.params = maple_get_device_config(device);
   227 			    maple_attach_device( device, maple_device, maple_subdevice );
   228 			    group = &devgroup;
   229 			}
   230 			continue;
   231 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   232 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   233 			if( maple_device == -1 ) {
   234 			    ERROR( "Subdevice not allowed without primary device" );
   235 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   236 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   237 			} else if( (device = maple_new_device(value)) == NULL ) {
   238 			    ERROR( "Unrecognized subdevice '%s'", value );
   239 			} else {
   240 			    devgroup.key = "controllers";
   241 			    devgroup.params = maple_get_device_config(device);
   242 			    maple_attach_device( device, maple_device, maple_subdevice );
   243 			    group = &devgroup;
   244 			}
   245 			continue;
   246 		    }
   247 		}
   248 		while( param->key != NULL ) {
   249 		    if( strcasecmp( param->key, buf ) == 0 ) {
   250 			param->value = g_strdup(value);
   251 			break;
   252 		    }
   253 		    param++;
   254 		}
   255 	    }
   256 	}
   257     }
   258     return TRUE;
   259 }
   261 gboolean lxdream_save_config_file( const gchar *filename )
   262 {
   263     FILE *f = fopen(filename, "wo");
   264     gboolean result;
   265     if( f == NULL ) {
   266 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   267 	return FALSE;
   268     }
   269     result = lxdream_save_config_stream(f);
   270     fclose(f);
   271     return TRUE;
   272 }    
   274 gboolean lxdream_save_config_stream( FILE *f )
   275 {
   276     struct lxdream_config_group *group = &lxdream_config_root[0];
   278     while( group->key != NULL ) {
   279 	struct lxdream_config_entry *entry = group->params;
   280 	fprintf( f, "[%s]\n", group->key );
   282 	if( entry != NULL ) {
   283 	    while( entry->key != NULL ) {
   284 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   285 		entry++;
   286 	    }
   287 	} else if( strcmp(group->key, "controllers") == 0 ) {
   288 	    int i,j;
   289 	    for( i=0; i<4; i++ ) {
   290 		for( j=0; j<6; j++ ) {
   291 		    maple_device_t dev = maple_get_device( i, j );
   292 		    if( dev != NULL ) {
   293 			if( j == 0 )
   294 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   295 			else 
   296 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   297 			entry = dev->get_config(dev);
   298 			while( entry->key != NULL ) {
   299 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   300 			    entry++;
   301 			}
   302 		    }
   303 		}
   304 	    }
   305 	}
   306 	fprintf( f, "\n" );
   307 	group++;
   308     }
   309     return TRUE;
   310 }
.