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