filename | src/config.c |
changeset | 475:49841795db97 |
prev | 470:e09a16196693 |
next | 480:d28c2992f5ee |
author | nkeynes |
date | Wed Oct 31 09:10:23 2007 +0000 (16 years ago) |
permissions | -rw-r--r-- |
last change | Add save/restore of render buffers in save states Gzip memory blocks in save states Move front-buffer management back to pvr2 Add screenshot preview when loading save states Various minor fixes and cleanups |
view | annotate | diff | log | raw |
1 /**
2 * $Id: config.c,v 1.6 2007-10-31 09:06:48 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 != value ) {
116 if( param->value != param->default_value && param->value != NULL ) {
117 free( param->value );
118 }
119 param->value = g_strdup(value);
120 }
121 }
123 void lxdream_set_global_config_value( int key, const gchar *value )
124 {
125 lxdream_set_config_value(&global_config[key], value);
126 }
128 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
129 {
130 int i;
131 for( i=0; group->params[i].key != NULL; i++ ) {
132 if( strcasecmp( group->params[i].key, key ) == 0 ) {
133 lxdream_set_config_value( &group->params[i], value );
134 return TRUE;
135 }
136 }
137 return FALSE;
138 }
140 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
141 {
142 int i;
143 for( i=0; src[i].key != NULL; i++ ) {
144 lxdream_set_config_value( &dest[i], src[i].value );
145 }
146 }
148 gboolean lxdream_load_config( )
149 {
150 if( lxdream_config_load_filename == NULL ) {
151 lxdream_find_config();
152 }
153 return lxdream_load_config_file(lxdream_config_load_filename);
154 }
156 gboolean lxdream_save_config( )
157 {
158 if( lxdream_config_save_filename == NULL ) {
159 lxdream_find_config();
160 }
161 return lxdream_save_config_file(lxdream_config_save_filename);
162 }
164 gboolean lxdream_load_config_file( const gchar *filename )
165 {
166 FILE *f;
167 gboolean result;
169 if( access(filename, F_OK) != 0 ) {
170 INFO( "Configuration file '%s' does not exist, creating from defaults" );
171 lxdream_set_default_config();
172 lxdream_save_config();
173 }
175 f = fopen(filename, "ro");
176 if( f == NULL ) {
177 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
178 lxdream_set_default_config();
179 return FALSE;
180 }
182 result = lxdream_load_config_stream( f );
183 fclose(f);
184 return result;
185 }
187 gboolean lxdream_load_config_stream( FILE *f )
188 {
190 char buf[512];
191 int maple_device = -1, maple_subdevice = -1;
192 struct lxdream_config_group devgroup;
193 struct lxdream_config_group *group = NULL;
194 maple_device_t device = NULL;
195 lxdream_set_default_config();
197 while( fgets( buf, sizeof(buf), f ) != NULL ) {
198 g_strstrip(buf);
199 if( buf[0] == '#' )
200 continue;
201 if( *buf == '[' ) {
202 char *p = strchr(buf, ']');
203 if( p != NULL ) {
204 struct lxdream_config_group *tmp_group;
205 maple_device = maple_subdevice = -1;
206 *p = '\0';
207 g_strstrip(buf+1);
208 tmp_group = &lxdream_config_root[0];
209 while( tmp_group->key != NULL ) {
210 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
211 group = tmp_group;
212 break;
213 }
214 tmp_group++;
215 }
216 }
217 } else if( group != NULL ) {
218 char *value = strchr( buf, '=' );
219 if( value != NULL ) {
220 struct lxdream_config_entry *param = group->params;
221 *value = '\0';
222 value++;
223 g_strstrip(buf);
224 g_strstrip(value);
225 if( strcmp(group->key,"controllers") == 0 ) {
226 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
227 maple_device = strtoul( buf+7, NULL, 0 );
228 if( maple_device < 0 || maple_device > 3 ) {
229 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
230 continue;
231 }
232 maple_subdevice = 0;
233 device = maple_new_device( value );
234 if( device == NULL ) {
235 ERROR( "Unrecognized device '%s'", value );
236 } else {
237 devgroup.key = "controllers";
238 devgroup.params = maple_get_device_config(device);
239 maple_attach_device( device, maple_device, maple_subdevice );
240 group = &devgroup;
241 }
242 continue;
243 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
244 maple_subdevice = strtoul( buf+10, NULL, 0 );
245 if( maple_device == -1 ) {
246 ERROR( "Subdevice not allowed without primary device" );
247 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
248 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
249 } else if( (device = maple_new_device(value)) == NULL ) {
250 ERROR( "Unrecognized subdevice '%s'", value );
251 } else {
252 devgroup.key = "controllers";
253 devgroup.params = maple_get_device_config(device);
254 maple_attach_device( device, maple_device, maple_subdevice );
255 group = &devgroup;
256 }
257 continue;
258 }
259 }
260 while( param->key != NULL ) {
261 if( strcasecmp( param->key, buf ) == 0 ) {
262 param->value = g_strdup(value);
263 break;
264 }
265 param++;
266 }
267 }
268 }
269 }
270 return TRUE;
271 }
273 gboolean lxdream_save_config_file( const gchar *filename )
274 {
275 FILE *f = fopen(filename, "wo");
276 gboolean result;
277 if( f == NULL ) {
278 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
279 return FALSE;
280 }
281 result = lxdream_save_config_stream(f);
282 fclose(f);
283 return TRUE;
284 }
286 gboolean lxdream_save_config_stream( FILE *f )
287 {
288 struct lxdream_config_group *group = &lxdream_config_root[0];
290 while( group->key != NULL ) {
291 struct lxdream_config_entry *entry = group->params;
292 fprintf( f, "[%s]\n", group->key );
294 if( entry != NULL ) {
295 while( entry->key != NULL ) {
296 if( entry->value != NULL ) {
297 fprintf( f, "%s = %s\n", entry->key, entry->value );
298 }
299 entry++;
300 }
301 } else if( strcmp(group->key, "controllers") == 0 ) {
302 int i,j;
303 for( i=0; i<4; i++ ) {
304 for( j=0; j<6; j++ ) {
305 maple_device_t dev = maple_get_device( i, j );
306 if( dev != NULL ) {
307 if( j == 0 )
308 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
309 else
310 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
311 entry = dev->get_config(dev);
312 while( entry->key != NULL ) {
313 if( entry->value != NULL ) {
314 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
315 }
316 entry++;
317 }
318 }
319 }
320 }
321 }
322 fprintf( f, "\n" );
323 group++;
324 }
325 return TRUE;
326 }
.