filename | src/config.c |
changeset | 1072:d82e04e6d497 |
prev | 1041:5fcc39857c5c |
next | 1077:136fc24d17ef |
author | nkeynes |
date | Fri Jul 31 13:45:32 2009 +1000 (14 years ago) |
permissions | -rw-r--r-- |
last change | Remove or change the level of a bunch of INFO messages that shouldn't really be INFO level |
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 <sys/types.h>
27 #include <sys/stat.h>
28 #include "dreamcast.h"
29 #include "config.h"
30 #include "lxpaths.h"
31 #include "maple/maple.h"
33 #define MAX_ROOT_GROUPS 16
35 extern struct lxdream_config_group hotkeys_group;
37 gboolean lxdream_load_config_file( const gchar *filename );
38 gboolean lxdream_save_config_file( const gchar *filename );
39 gboolean lxdream_load_config_stream( FILE *f );
40 gboolean lxdream_save_config_stream( FILE *f );
42 static struct lxdream_config_group global_group =
43 { "global", dreamcast_config_changed, NULL, NULL,
44 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, NULL },
45 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, NULL },
46 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
47 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, NULL },
48 { "vmu path", N_("VMU path"), CONFIG_TYPE_PATH, NULL },
49 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, NULL },
50 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
51 { "recent", NULL, CONFIG_TYPE_FILELIST, NULL },
52 { "vmu", NULL, CONFIG_TYPE_FILELIST, NULL },
53 { "quick state", NULL, CONFIG_TYPE_INTEGER, "0" },
54 { NULL, CONFIG_TYPE_NONE }} };
56 static struct lxdream_config_group serial_group =
57 { "serial", NULL, NULL, NULL,
58 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
59 { NULL, CONFIG_TYPE_NONE }} };
61 /**
62 * Dummy group for controllers (handled specially)
63 */
64 static struct lxdream_config_group controllers_group =
65 { "controllers", NULL, NULL, NULL, {{NULL, CONFIG_TYPE_NONE}} };
67 struct lxdream_config_group *lxdream_config_root[MAX_ROOT_GROUPS+1] =
68 { &global_group, &controllers_group, &hotkeys_group, &serial_group, NULL };
70 static gchar *lxdream_config_load_filename = NULL;
71 static gchar *lxdream_config_save_filename = NULL;
73 void lxdream_register_config_group( const gchar *key, lxdream_config_group_t group )
74 {
75 int i;
76 for( i=0; i<MAX_ROOT_GROUPS; i++ ) {
77 if( lxdream_config_root[i] == NULL ) {
78 lxdream_config_root[i] = group;
79 lxdream_config_root[i+1] = NULL;
80 return;
81 }
82 }
83 ERROR( "Unable to register config group '%s': Too many configuration groups", key );
84 }
86 gboolean lxdream_find_config()
87 {
88 gboolean result = TRUE;
89 char *home = getenv("HOME");
90 if( lxdream_config_save_filename == NULL ) {
91 /* For compatibility, look for the old ~/.lxdreamrc first. Use it if
92 * found, otherwise use the new ~/.lxdream/lxdreamrc.
93 */
94 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
95 if( access(lxdream_config_save_filename, R_OK) != 0 ) {
96 g_free(lxdream_config_save_filename);
97 const char *user_path = get_user_data_path();
98 lxdream_config_save_filename = g_strdup_printf("%s/%s", user_path, DEFAULT_CONFIG_FILENAME);
99 }
100 }
101 if( lxdream_config_load_filename == NULL ) {
102 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
103 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
104 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
105 g_free(sysconfig);
106 } else if( access( sysconfig, R_OK ) == 0 ) {
107 lxdream_config_load_filename = sysconfig;
108 } else {
109 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
110 g_free(sysconfig);
111 result = FALSE;
112 }
113 }
114 return result;
115 }
117 void lxdream_set_config_filename( const gchar *filename )
118 {
119 if( lxdream_config_load_filename != NULL ) {
120 g_free(lxdream_config_load_filename);
121 }
122 lxdream_config_load_filename = g_strdup(filename);
123 if( lxdream_config_save_filename != NULL ) {
124 g_free(lxdream_config_save_filename);
125 }
126 lxdream_config_save_filename = g_strdup(filename);
127 }
129 void lxdream_set_default_config( )
130 {
131 /* Construct platform dependent defaults */
132 const gchar *user_path = get_user_data_path();
133 global_group.params[CONFIG_BIOS_PATH].default_value = g_strdup_printf( "%s/dcboot.rom", user_path );
134 global_group.params[CONFIG_FLASH_PATH].default_value = g_strdup_printf( "%s/dcflash.rom", user_path );
135 global_group.params[CONFIG_SAVE_PATH].default_value = g_strdup_printf( "%s/save", user_path );
136 global_group.params[CONFIG_VMU_PATH].default_value = g_strdup_printf( "%s/vmu", user_path );
137 global_group.params[CONFIG_BOOTSTRAP].default_value = g_strdup_printf( "%s/IP.BIN", user_path );
139 /* Copy defaults into main values */
140 for( int i=0; lxdream_config_root[i] != NULL; i++ ) {
141 struct lxdream_config_entry *param = lxdream_config_root[i]->params;
142 if( param != NULL ) {
143 while( param->key != NULL ) {
144 if( param->value != param->default_value ) {
145 if( param->value != NULL )
146 free( param->value );
147 param->value = (gchar *)param->default_value;
148 }
149 param++;
150 }
151 }
152 }
153 maple_detach_all();
154 }
156 const gchar *lxdream_get_config_value( lxdream_config_group_t group, int key )
157 {
158 return group->params[key].value;
159 }
162 gboolean lxdream_set_config_value( lxdream_config_group_t group, int key, const gchar *value )
163 {
164 lxdream_config_entry_t param = &group->params[key];
165 if( param->value != value &&
166 (param->value == NULL || value == NULL || strcmp(param->value,value) != 0) ) {
168 gchar *new_value = g_strdup(value);
170 /* If the group defines an on_change handler, it can block the change
171 * (ie due to an invalid setting).
172 */
173 if( group->on_change == NULL ||
174 group->on_change(group->data, group,key, param->value, new_value) ) {
176 /* Don't free the default value, but otherwise need to release the
177 * old value.
178 */
179 if( param->value != param->default_value && param->value != NULL ) {
180 free( param->value );
181 }
182 param->value = new_value;
183 } else { /* on_change handler said no. */
184 g_free(new_value);
185 return FALSE;
186 }
187 }
188 return TRUE;
189 }
191 const gchar *lxdream_get_global_config_value( int key )
192 {
193 return global_group.params[key].value;
194 }
196 void lxdream_set_global_config_value( int key, const gchar *value )
197 {
198 lxdream_set_config_value(&global_group, key, value);
199 }
201 GList *lxdream_get_global_config_list_value( int key )
202 {
203 GList *result = NULL;
204 const gchar *str = lxdream_get_global_config_value( key );
205 if( str != NULL ) {
206 gchar **strv = g_strsplit(str, ":",0);
207 int i;
208 for( i=0; strv[i] != NULL; i++ ) {
209 result = g_list_append( result, g_strdup(strv[i]) );
210 }
211 g_strfreev(strv);
212 }
213 return result;
214 }
216 void lxdream_set_global_config_list_value( int key, const GList *list )
217 {
218 if( list == NULL ) {
219 lxdream_set_global_config_value( key, NULL );
220 } else {
221 const GList *ptr;
222 int size = 0;
224 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
225 size += strlen( (gchar *)ptr->data ) + 1;
226 }
227 char buf[size];
228 strcpy( buf, (gchar *)list->data );
229 for( ptr = g_list_next(list); ptr != NULL; ptr = g_list_next(ptr) ) {
230 strcat( buf, ":" );
231 strcat( buf, (gchar *)ptr->data );
232 }
233 lxdream_set_global_config_value( key, buf );
234 }
235 }
237 gchar *lxdream_get_global_config_path_value( int key )
238 {
239 const gchar *str = lxdream_get_global_config_value(key);
240 if( str == NULL ) {
241 return NULL;
242 } else {
243 return get_expanded_path(str);
244 }
245 }
247 const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
248 {
249 gchar *temp = get_escaped_path(value);
250 lxdream_set_global_config_value(key,temp);
251 g_free(temp);
252 return lxdream_get_global_config_value(key);
253 }
255 struct lxdream_config_group * lxdream_get_config_group( int group )
256 {
257 return lxdream_config_root[group];
258 }
260 void lxdream_copy_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
261 {
262 int i;
263 for( i=0; src->params[i].key != NULL; i++ ) {
264 lxdream_set_config_value( dest, i, src->params[i].value );
265 }
266 }
268 void lxdream_clone_config_group( lxdream_config_group_t dest, lxdream_config_group_t src )
269 {
270 int i;
272 dest->key = src->key;
273 dest->on_change = NULL;
274 dest->key_binding = NULL;
275 dest->data = NULL;
276 for( i=0; src->params[i].key != NULL; i++ ) {
277 dest->params[i].key = src->params[i].key;
278 dest->params[i].label = src->params[i].label;
279 dest->params[i].type = src->params[i].type;
280 dest->params[i].tag = src->params[i].tag;
281 dest->params[i].default_value = src->params[i].default_value;
282 dest->params[i].value = NULL;
283 lxdream_set_config_value( dest, i, src->params[i].value );
284 }
285 dest->params[i].key = NULL;
286 dest->params[i].label = NULL;
287 }
289 gboolean lxdream_load_config( )
290 {
291 if( lxdream_config_load_filename == NULL ) {
292 lxdream_find_config();
293 }
294 return lxdream_load_config_file(lxdream_config_load_filename);
295 }
297 gboolean lxdream_save_config( )
298 {
299 if( lxdream_config_save_filename == NULL ) {
300 lxdream_find_config();
301 }
302 return lxdream_save_config_file(lxdream_config_save_filename);
303 }
305 gboolean lxdream_load_config_file( const gchar *filename )
306 {
307 FILE *f;
308 gboolean result;
310 if( access(filename, F_OK) != 0 ) {
311 INFO( "Configuration file '%s' does not exist, creating from defaults" );
312 lxdream_set_default_config();
313 lxdream_save_config();
314 }
316 f = fopen(filename, "ro");
317 if( f == NULL ) {
318 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
319 lxdream_set_default_config();
320 return FALSE;
321 }
323 result = lxdream_load_config_stream( f );
324 fclose(f);
325 return result;
326 }
328 gboolean lxdream_load_config_stream( FILE *f )
329 {
331 char buf[512];
332 int maple_device = -1, maple_subdevice = -1, i;
333 struct lxdream_config_group *group = NULL;
334 struct lxdream_config_group *top_group = NULL;
335 maple_device_t device = NULL;
336 lxdream_set_default_config();
338 while( fgets( buf, sizeof(buf), f ) != NULL ) {
339 g_strstrip(buf);
340 if( buf[0] == '#' )
341 continue;
342 if( *buf == '[' ) {
343 char *p = strchr(buf, ']');
344 if( p != NULL ) {
345 maple_device = maple_subdevice = -1;
346 *p = '\0';
347 g_strstrip(buf+1);
348 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
349 if( strcasecmp(lxdream_config_root[i]->key, buf+1) == 0 ) {
350 top_group = group = lxdream_config_root[i];
351 break;
352 }
353 }
354 }
355 } else if( group != NULL ) {
356 char *value = strchr( buf, '=' );
357 if( value != NULL ) {
358 struct lxdream_config_entry *param = group->params;
359 *value = '\0';
360 value++;
361 g_strstrip(buf);
362 g_strstrip(value);
363 if( top_group == &controllers_group ) {
364 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
365 maple_device = strtoul( buf+7, NULL, 0 );
366 if( maple_device < 0 || maple_device > 3 ) {
367 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
368 continue;
369 }
370 maple_subdevice = 0;
371 device = maple_new_device( value );
372 if( device == NULL ) {
373 ERROR( "Unrecognized device '%s'", value );
374 } else {
375 group = maple_get_device_config(device);
376 maple_attach_device( device, maple_device, maple_subdevice );
377 }
378 continue;
379 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
380 maple_subdevice = strtoul( buf+10, NULL, 0 );
381 if( maple_device == -1 ) {
382 ERROR( "Subdevice not allowed without primary device" );
383 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
384 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
385 } else if( (device = maple_new_device(value)) == NULL ) {
386 ERROR( "Unrecognized subdevice '%s'", value );
387 } else {
388 group = maple_get_device_config(device);
389 maple_attach_device( device, maple_device, maple_subdevice );
390 }
391 continue;
392 }
393 }
394 while( param->key != NULL ) {
395 if( strcasecmp( param->key, buf ) == 0 ) {
396 param->value = g_strdup(value);
397 break;
398 }
399 param++;
400 }
401 }
402 }
403 }
404 return TRUE;
405 }
407 gboolean lxdream_save_config_file( const gchar *filename )
408 {
409 FILE *f = fopen(filename, "wo");
410 gboolean result;
411 if( f == NULL ) {
412 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
413 return FALSE;
414 }
415 result = lxdream_save_config_stream(f);
416 fclose(f);
417 return TRUE;
418 }
420 gboolean lxdream_save_config_stream( FILE *f )
421 {
422 int i;
423 for( i=0; lxdream_config_root[i] != NULL; i++ ) {
424 fprintf( f, "[%s]\n", lxdream_config_root[i]->key );
426 if( lxdream_config_root[i] == &controllers_group ) {
427 int i,j;
428 for( i=0; i<4; i++ ) {
429 for( j=0; j<6; j++ ) {
430 maple_device_t dev = maple_get_device( i, j );
431 if( dev != NULL ) {
432 if( j == 0 )
433 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
434 else
435 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
436 lxdream_config_group_t group = maple_get_device_config(dev);
437 if( group != NULL ) {
438 lxdream_config_entry_t entry = group->params;
439 while( entry->key != NULL ) {
440 if( entry->value != NULL ) {
441 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
442 }
443 entry++;
444 }
445 }
446 }
447 }
448 }
449 } else {
450 struct lxdream_config_entry *entry = lxdream_config_root[i]->params;
451 while( entry->key != NULL ) {
452 if( entry->value != NULL ) {
453 fprintf( f, "%s = %s\n", entry->key, entry->value );
454 }
455 entry++;
456 }
457 }
458 fprintf( f, "\n" );
459 }
460 return TRUE;
461 }
463 void lxdream_make_config_dir( )
464 {
465 const char *user_path = get_user_data_path();
466 struct stat st;
468 if( access( user_path, R_OK|X_OK ) == 0 && lstat( user_path, &st ) == 0 &&
469 (st.st_mode & S_IFDIR) != 0 ) {
470 /* All good */
471 return;
472 }
474 if( mkdir( user_path, 0777 ) != 0 ) {
475 ERROR( "Unable to create user configuration directory %s: %s", user_path, strerror(errno) );
476 return;
477 }
479 char *vmupath = g_strdup_printf( "%s/vmu", user_path );
480 mkdir( vmupath, 0777 );
481 g_free( vmupath );
483 char *savepath = g_strdup_printf( "%s/save", user_path );
484 mkdir( savepath, 0777 );
485 g_free( vmupath );
486 }
.