filename | src/config.c |
changeset | 759:f16975739abc |
prev | 736:a02d1475ccfd |
next | 866:86cd01c2b2d3 |
author | nkeynes |
date | Sat Aug 23 12:08:01 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Mask off SR correctly when writing to it - this turns out to be important in some cases |
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", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
40 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
41 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
42 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
43 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
44 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
45 { "recent", NULL, CONFIG_TYPE_FILE, NULL },
46 { NULL, CONFIG_TYPE_NONE }};
48 static struct lxdream_config_entry serial_config[] =
49 {{ "device", N_("Serial 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 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
139 {
140 return &global_config[key];
141 }
143 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
144 {
145 int i;
146 for( i=0; group->params[i].key != NULL; i++ ) {
147 if( strcasecmp( group->params[i].key, key ) == 0 ) {
148 lxdream_set_config_value( &group->params[i], value );
149 return TRUE;
150 }
151 }
152 return FALSE;
153 }
155 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
156 {
157 int i;
158 for( i=0; src[i].key != NULL; i++ ) {
159 lxdream_set_config_value( &dest[i], src[i].value );
160 }
161 }
163 gboolean lxdream_load_config( )
164 {
165 if( lxdream_config_load_filename == NULL ) {
166 lxdream_find_config();
167 }
168 return lxdream_load_config_file(lxdream_config_load_filename);
169 }
171 gboolean lxdream_save_config( )
172 {
173 if( lxdream_config_save_filename == NULL ) {
174 lxdream_find_config();
175 }
176 return lxdream_save_config_file(lxdream_config_save_filename);
177 }
179 gboolean lxdream_load_config_file( const gchar *filename )
180 {
181 FILE *f;
182 gboolean result;
184 if( access(filename, F_OK) != 0 ) {
185 INFO( "Configuration file '%s' does not exist, creating from defaults" );
186 lxdream_set_default_config();
187 lxdream_save_config();
188 }
190 f = fopen(filename, "ro");
191 if( f == NULL ) {
192 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
193 lxdream_set_default_config();
194 return FALSE;
195 }
197 result = lxdream_load_config_stream( f );
198 fclose(f);
199 return result;
200 }
202 gboolean lxdream_load_config_stream( FILE *f )
203 {
205 char buf[512];
206 int maple_device = -1, maple_subdevice = -1;
207 struct lxdream_config_group devgroup;
208 struct lxdream_config_group *group = NULL;
209 maple_device_t device = NULL;
210 lxdream_set_default_config();
212 while( fgets( buf, sizeof(buf), f ) != NULL ) {
213 g_strstrip(buf);
214 if( buf[0] == '#' )
215 continue;
216 if( *buf == '[' ) {
217 char *p = strchr(buf, ']');
218 if( p != NULL ) {
219 struct lxdream_config_group *tmp_group;
220 maple_device = maple_subdevice = -1;
221 *p = '\0';
222 g_strstrip(buf+1);
223 tmp_group = &lxdream_config_root[0];
224 while( tmp_group->key != NULL ) {
225 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
226 group = tmp_group;
227 break;
228 }
229 tmp_group++;
230 }
231 }
232 } else if( group != NULL ) {
233 char *value = strchr( buf, '=' );
234 if( value != NULL ) {
235 struct lxdream_config_entry *param = group->params;
236 *value = '\0';
237 value++;
238 g_strstrip(buf);
239 g_strstrip(value);
240 if( strcmp(group->key,"controllers") == 0 ) {
241 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
242 maple_device = strtoul( buf+7, NULL, 0 );
243 if( maple_device < 0 || maple_device > 3 ) {
244 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
245 continue;
246 }
247 maple_subdevice = 0;
248 device = maple_new_device( value );
249 if( device == NULL ) {
250 ERROR( "Unrecognized device '%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 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
259 maple_subdevice = strtoul( buf+10, NULL, 0 );
260 if( maple_device == -1 ) {
261 ERROR( "Subdevice not allowed without primary device" );
262 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
263 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
264 } else if( (device = maple_new_device(value)) == NULL ) {
265 ERROR( "Unrecognized subdevice '%s'", value );
266 } else {
267 devgroup.key = "controllers";
268 devgroup.params = maple_get_device_config(device);
269 maple_attach_device( device, maple_device, maple_subdevice );
270 group = &devgroup;
271 }
272 continue;
273 }
274 }
275 while( param->key != NULL ) {
276 if( strcasecmp( param->key, buf ) == 0 ) {
277 param->value = g_strdup(value);
278 break;
279 }
280 param++;
281 }
282 }
283 }
284 }
285 return TRUE;
286 }
288 gboolean lxdream_save_config_file( const gchar *filename )
289 {
290 FILE *f = fopen(filename, "wo");
291 gboolean result;
292 if( f == NULL ) {
293 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
294 return FALSE;
295 }
296 result = lxdream_save_config_stream(f);
297 fclose(f);
298 return TRUE;
299 }
301 gboolean lxdream_save_config_stream( FILE *f )
302 {
303 struct lxdream_config_group *group = &lxdream_config_root[0];
305 while( group->key != NULL ) {
306 struct lxdream_config_entry *entry = group->params;
307 fprintf( f, "[%s]\n", group->key );
309 if( entry != NULL ) {
310 while( entry->key != NULL ) {
311 if( entry->value != NULL ) {
312 fprintf( f, "%s = %s\n", entry->key, entry->value );
313 }
314 entry++;
315 }
316 } else if( strcmp(group->key, "controllers") == 0 ) {
317 int i,j;
318 for( i=0; i<4; i++ ) {
319 for( j=0; j<6; j++ ) {
320 maple_device_t dev = maple_get_device( i, j );
321 if( dev != NULL ) {
322 if( j == 0 )
323 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
324 else
325 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
326 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
327 while( entry->key != NULL ) {
328 if( entry->value != NULL ) {
329 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
330 }
331 entry++;
332 }
333 }
334 }
335 }
336 }
337 }
338 fprintf( f, "\n" );
339 group++;
340 }
341 return TRUE;
342 }
.