filename | src/dreamcast.c |
changeset | 144:7f0714e89aaa |
prev | 89:939afb9f0f98 |
next | 146:f91fa34ab219 |
author | nkeynes |
date | Mon May 15 08:28:52 2006 +0000 (17 years ago) |
permissions | -rw-r--r-- |
last change | Rename video_driver to display_driver Add input source to display Implement configuration file support Hook controllers up to configuration |
file | annotate | diff | log | raw |
1.1 --- a/src/dreamcast.c Sun Jan 22 22:42:05 2006 +00001.2 +++ b/src/dreamcast.c Mon May 15 08:28:52 2006 +00001.3 @@ -1,5 +1,5 @@1.4 /**1.5 - * $Id: dreamcast.c,v 1.14 2006-01-22 22:42:05 nkeynes Exp $1.6 + * $Id: dreamcast.c,v 1.15 2006-05-15 08:28:48 nkeynes Exp $1.7 * Central switchboard for the system. This pulls all the individual modules1.8 * together into some kind of coherent structure. This is also where you'd1.9 * add Naomi support, if I ever get a board to play with...1.10 @@ -18,6 +18,7 @@1.11 */1.13 #include <errno.h>1.14 +#include <glib/gstrfuncs.h>1.15 #include "dream.h"1.16 #include "mem.h"1.17 #include "aica/aica.h"1.18 @@ -77,10 +78,12 @@1.19 dreamcast_register_module( &ide_module );1.21 /* Attach any default maple devices, ie a pair of controllers */1.22 - maple_device_t controller1 = controller_new();1.23 - maple_device_t controller2 = controller_new();1.24 + /*1.25 + maple_device_t controller1 = maple_new_device("Sega Controller");1.26 + maple_device_t controller2 = maple_new_device("Sega Controller");1.27 maple_attach_device( controller1, 0, 0 );1.28 maple_attach_device( controller2, 1, 0 );1.29 + */1.30 }1.32 /**1.33 @@ -159,16 +162,193 @@1.35 /***************************** User Configuration **************************/1.37 -void dreamcast_load_config( const gchar *filename )1.38 +static struct dreamcast_config_entry global_config[] =1.39 + {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },1.40 + { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },1.41 + { "default path", CONFIG_TYPE_PATH, "." },1.42 + { "save path", CONFIG_TYPE_PATH, "save" },1.43 + { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },1.44 + { NULL, CONFIG_TYPE_NONE }};1.45 +1.46 +static struct dreamcast_config_entry serial_config[] =1.47 + {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },1.48 + { NULL, CONFIG_TYPE_NONE }};1.49 +1.50 +struct dreamcast_config_group dreamcast_config_root[] =1.51 + {{ "global", global_config },1.52 + { "controllers", NULL },1.53 + { "serial", serial_config },1.54 + { NULL, CONFIG_TYPE_NONE }};1.55 +1.56 +void dreamcast_set_default_config( )1.57 +{1.58 + struct dreamcast_config_group *group = dreamcast_config_root;1.59 + while( group->key != NULL ) {1.60 + struct dreamcast_config_entry *param = group->params;1.61 + if( param != NULL ) {1.62 + while( param->key != NULL ) {1.63 + if( param->value != param->default_value ) {1.64 + if( param->value != NULL )1.65 + free( param->value );1.66 + param->value = (gchar *)param->default_value;1.67 + }1.68 + param++;1.69 + }1.70 + }1.71 + group++;1.72 + }1.73 + maple_detach_all();1.74 +}1.75 +1.76 +gboolean dreamcast_load_config( const gchar *filename )1.77 +{1.78 + FILE *f = fopen(filename, "ro");1.79 + gboolean result;1.80 +1.81 + if( f == NULL ) {1.82 + ERROR( "Unable to open '%s': %s", filename, strerror(errno) );1.83 + return FALSE;1.84 + }1.85 +1.86 + result = dreamcast_load_config_stream( f );1.87 + fclose(f);1.88 + return result;1.89 +}1.90 +1.91 +gboolean dreamcast_load_config_stream( FILE *f )1.92 {1.94 + char buf[512], *p;1.95 + int maple_device = -1, maple_subdevice = -1;1.96 + struct dreamcast_config_group devgroup;1.97 + struct dreamcast_config_group *group = NULL;1.98 + maple_device_t device = NULL;1.99 + dreamcast_set_default_config();1.101 + while( fgets( buf, sizeof(buf), f ) != NULL ) {1.102 + g_strstrip(buf);1.103 + if( buf[0] == '#' )1.104 + continue;1.105 + if( *buf == '[' ) {1.106 + char *p = strchr(buf, ']');1.107 + if( p != NULL ) {1.108 + struct dreamcast_config_group *tmp_group;1.109 + maple_device = maple_subdevice = -1;1.110 + *p = '\0';1.111 + g_strstrip(buf+1);1.112 + tmp_group = &dreamcast_config_root[0];1.113 + while( tmp_group->key != NULL ) {1.114 + if( strcasecmp(tmp_group->key, buf+1) == 0 ) {1.115 + group = tmp_group;1.116 + break;1.117 + }1.118 + tmp_group++;1.119 + }1.120 + }1.121 + } else if( group != NULL ) {1.122 + char *value = strchr( buf, '=' );1.123 + if( value != NULL ) {1.124 + struct dreamcast_config_entry *param = group->params;1.125 + *value = '\0';1.126 + value++;1.127 + g_strstrip(buf);1.128 + g_strstrip(value);1.129 + if( strcmp(group->key,"controllers") == 0 ) {1.130 + if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {1.131 + maple_device = strtoul( buf+7, NULL, 0 );1.132 + if( maple_device < 0 || maple_device > 3 ) {1.133 + ERROR( "Device number must be between 0..3 (not '%s')", buf+7);1.134 + continue;1.135 + }1.136 + maple_subdevice = 0;1.137 + device = maple_new_device( value );1.138 + if( device == NULL ) {1.139 + ERROR( "Unrecognized device '%s'", value );1.140 + } else {1.141 + devgroup.key = "controllers";1.142 + devgroup.params = maple_get_device_config(device);1.143 + maple_attach_device( device, maple_device, maple_subdevice );1.144 + group = &devgroup;1.145 + }1.146 + continue;1.147 + } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {1.148 + maple_subdevice = strtoul( buf+10, NULL, 0 );1.149 + if( maple_device == -1 ) {1.150 + ERROR( "Subdevice not allowed without primary device" );1.151 + } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {1.152 + ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );1.153 + } else if( (device = maple_new_device(value)) == NULL ) {1.154 + ERROR( "Unrecognized subdevice '%s'", value );1.155 + } else {1.156 + devgroup.key = "controllers";1.157 + devgroup.params = maple_get_device_config(device);1.158 + maple_attach_device( device, maple_device, maple_subdevice );1.159 + group = &devgroup;1.160 + }1.161 + continue;1.162 + }1.163 + }1.164 + while( param->key != NULL ) {1.165 + if( strcasecmp( param->key, buf ) == 0 ) {1.166 + param->value = g_strdup(value);1.167 + break;1.168 + }1.169 + param++;1.170 + }1.171 + }1.172 + }1.173 + }1.174 + return TRUE;1.175 }1.177 -void dreamcast_save_config( const gchar *filename )1.178 +gboolean dreamcast_save_config( const gchar *filename )1.179 {1.180 + FILE *f = fopen(filename, "wo");1.181 + gboolean result;1.182 + if( f == NULL ) {1.183 + ERROR( "Unable to open '%s': %s", filename, strerror(errno) );1.184 + return FALSE;1.185 + }1.186 + result = dreamcast_save_config_stream(f);1.187 + fclose(f);1.188 +}1.190 -1.191 +gboolean dreamcast_save_config_stream( FILE *f )1.192 +{1.193 + struct dreamcast_config_group *group = &dreamcast_config_root[0];1.194 +1.195 + while( group->key != NULL ) {1.196 + struct dreamcast_config_entry *entry = group->params;1.197 + fprintf( f, "[%s]\n", group->key );1.198 +1.199 + if( entry != NULL ) {1.200 + while( entry->key != NULL ) {1.201 + fprintf( f, "%s = %s\n", entry->key, entry->value );1.202 + entry++;1.203 + }1.204 + } else if( strcmp(group->key, "controllers") == 0 ) {1.205 + int i,j;1.206 + for( i=0; i<4; i++ ) {1.207 + for( j=0; j<6; j++ ) {1.208 + maple_device_t dev = maple_get_device( i, j );1.209 + if( dev != NULL ) {1.210 + if( j == 0 )1.211 + fprintf( f, "Device %d = %s\n", i, dev->device_class->name );1.212 + else1.213 + fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );1.214 + entry = dev->get_config(dev);1.215 + while( entry->key != NULL ) {1.216 + fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );1.217 + entry++;1.218 + }1.219 + }1.220 + }1.221 + }1.222 + }1.223 + fprintf( f, "\n" );1.224 + group++;1.225 + }1.226 + return TRUE;1.227 }1.229 /********************************* Save States *****************************/
.