filename | src/config.c |
changeset | 1015:ad448bedc48a |
prev | 866:86cd01c2b2d3 |
next | 1024:c67f2d61ab97 |
author | nkeynes |
date | Tue Jun 02 23:16:26 2009 +0000 (14 years ago) |
permissions | -rw-r--r-- |
last change | "MythTV" patch from Wahrhaft, thanks!: * Add support for LIRC input devices * Add hotkey input support * Add command-line option for immediate fullscreen |
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[];
32 extern struct lxdream_config_entry hotkeys_config[];
34 gboolean lxdream_load_config_file( const gchar *filename );
35 gboolean lxdream_save_config_file( const gchar *filename );
36 gboolean lxdream_load_config_stream( FILE *f );
37 gboolean lxdream_save_config_stream( FILE *f );
39 static struct lxdream_config_entry global_config[] =
40 {{ "bios", N_("Bios ROM"), CONFIG_TYPE_FILE, "dcboot.rom" },
41 { "flash", N_("Flash ROM"), CONFIG_TYPE_FILE, "dcflash.rom" },
42 { "default path", N_("Default disc path"), CONFIG_TYPE_PATH, "." },
43 { "save path", N_("Save-state path"), CONFIG_TYPE_PATH, "save" },
44 { "bootstrap", N_("Bootstrap IP.BIN"), CONFIG_TYPE_FILE, "IP.BIN" },
45 { "gdrom", NULL, CONFIG_TYPE_FILE, NULL },
46 { "recent", NULL, CONFIG_TYPE_FILE, NULL },
47 { NULL, CONFIG_TYPE_NONE }};
49 static struct lxdream_config_entry serial_config[] =
50 {{ "device", N_("Serial device"), CONFIG_TYPE_FILE, "/dev/ttyS1" },
51 { NULL, CONFIG_TYPE_NONE }};
53 struct lxdream_config_group lxdream_config_root[] =
54 {{ "global", global_config },
55 { "controllers", NULL },
56 { "hotkeys", hotkeys_config },
57 { "serial", serial_config },
58 #ifdef HAVE_ALSA
59 { "alsa", alsa_config },
60 #endif
61 { NULL, CONFIG_TYPE_NONE }};
63 static gchar *lxdream_config_load_filename = NULL;
64 static gchar *lxdream_config_save_filename = NULL;
66 gboolean lxdream_find_config()
67 {
68 gboolean result = TRUE;
69 char *home = getenv("HOME");
70 if( lxdream_config_save_filename == NULL ) {
71 lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
72 }
73 if( lxdream_config_load_filename == NULL ) {
74 char *sysconfig = g_strdup_printf("%s/%s", get_sysconf_path(), DEFAULT_CONFIG_FILENAME);
75 if( access(lxdream_config_save_filename, R_OK) == 0 ) {
76 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
77 g_free(sysconfig);
78 } else if( access( sysconfig, R_OK ) == 0 ) {
79 lxdream_config_load_filename = sysconfig;
80 } else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
81 lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
82 g_free(sysconfig);
83 } else {
84 lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
85 g_free(sysconfig);
86 result = FALSE;
87 }
88 }
89 return result;
90 }
92 void lxdream_set_config_filename( const gchar *filename )
93 {
94 if( lxdream_config_load_filename != NULL ) {
95 g_free(lxdream_config_load_filename);
96 }
97 lxdream_config_load_filename = g_strdup(filename);
98 if( lxdream_config_save_filename != NULL ) {
99 g_free(lxdream_config_save_filename);
100 }
101 lxdream_config_save_filename = g_strdup(filename);
102 }
104 void lxdream_set_default_config( )
105 {
106 struct lxdream_config_group *group = lxdream_config_root;
107 while( group->key != NULL ) {
108 struct lxdream_config_entry *param = group->params;
109 if( param != NULL ) {
110 while( param->key != NULL ) {
111 if( param->value != param->default_value ) {
112 if( param->value != NULL )
113 free( param->value );
114 param->value = (gchar *)param->default_value;
115 }
116 param++;
117 }
118 }
119 group++;
120 }
121 maple_detach_all();
122 }
124 const gchar *lxdream_get_config_value( int key )
125 {
126 return global_config[key].value;
127 }
129 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
130 {
131 if( param->value != value ) {
132 if( param->value != param->default_value && param->value != NULL ) {
133 free( param->value );
134 }
135 param->value = g_strdup(value);
136 }
137 }
139 void lxdream_set_global_config_value( int key, const gchar *value )
140 {
141 lxdream_set_config_value(&global_config[key], value);
142 }
144 const struct lxdream_config_entry * lxdream_get_config_entry( int key )
145 {
146 return &global_config[key];
147 }
149 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
150 {
151 int i;
152 for( i=0; group->params[i].key != NULL; i++ ) {
153 if( strcasecmp( group->params[i].key, key ) == 0 ) {
154 lxdream_set_config_value( &group->params[i], value );
155 return TRUE;
156 }
157 }
158 return FALSE;
159 }
161 void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
162 {
163 int i;
164 for( i=0; src[i].key != NULL; i++ ) {
165 lxdream_set_config_value( &dest[i], src[i].value );
166 }
167 }
169 gboolean lxdream_load_config( )
170 {
171 if( lxdream_config_load_filename == NULL ) {
172 lxdream_find_config();
173 }
174 return lxdream_load_config_file(lxdream_config_load_filename);
175 }
177 gboolean lxdream_save_config( )
178 {
179 if( lxdream_config_save_filename == NULL ) {
180 lxdream_find_config();
181 }
182 return lxdream_save_config_file(lxdream_config_save_filename);
183 }
185 gboolean lxdream_load_config_file( const gchar *filename )
186 {
187 FILE *f;
188 gboolean result;
190 if( access(filename, F_OK) != 0 ) {
191 INFO( "Configuration file '%s' does not exist, creating from defaults" );
192 lxdream_set_default_config();
193 lxdream_save_config();
194 }
196 f = fopen(filename, "ro");
197 if( f == NULL ) {
198 ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
199 lxdream_set_default_config();
200 return FALSE;
201 }
203 result = lxdream_load_config_stream( f );
204 fclose(f);
205 return result;
206 }
208 gboolean lxdream_load_config_stream( FILE *f )
209 {
211 char buf[512];
212 int maple_device = -1, maple_subdevice = -1;
213 struct lxdream_config_group devgroup;
214 struct lxdream_config_group *group = NULL;
215 maple_device_t device = NULL;
216 lxdream_set_default_config();
218 while( fgets( buf, sizeof(buf), f ) != NULL ) {
219 g_strstrip(buf);
220 if( buf[0] == '#' )
221 continue;
222 if( *buf == '[' ) {
223 char *p = strchr(buf, ']');
224 if( p != NULL ) {
225 struct lxdream_config_group *tmp_group;
226 maple_device = maple_subdevice = -1;
227 *p = '\0';
228 g_strstrip(buf+1);
229 tmp_group = &lxdream_config_root[0];
230 while( tmp_group->key != NULL ) {
231 if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
232 group = tmp_group;
233 break;
234 }
235 tmp_group++;
236 }
237 }
238 } else if( group != NULL ) {
239 char *value = strchr( buf, '=' );
240 if( value != NULL ) {
241 struct lxdream_config_entry *param = group->params;
242 *value = '\0';
243 value++;
244 g_strstrip(buf);
245 g_strstrip(value);
246 if( strcmp(group->key,"controllers") == 0 ) {
247 if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
248 maple_device = strtoul( buf+7, NULL, 0 );
249 if( maple_device < 0 || maple_device > 3 ) {
250 ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
251 continue;
252 }
253 maple_subdevice = 0;
254 device = maple_new_device( value );
255 if( device == NULL ) {
256 ERROR( "Unrecognized device '%s'", value );
257 } else {
258 devgroup.key = "controllers";
259 devgroup.params = maple_get_device_config(device);
260 maple_attach_device( device, maple_device, maple_subdevice );
261 group = &devgroup;
262 }
263 continue;
264 } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
265 maple_subdevice = strtoul( buf+10, NULL, 0 );
266 if( maple_device == -1 ) {
267 ERROR( "Subdevice not allowed without primary device" );
268 } else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
269 ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
270 } else if( (device = maple_new_device(value)) == NULL ) {
271 ERROR( "Unrecognized subdevice '%s'", value );
272 } else {
273 devgroup.key = "controllers";
274 devgroup.params = maple_get_device_config(device);
275 maple_attach_device( device, maple_device, maple_subdevice );
276 group = &devgroup;
277 }
278 continue;
279 }
280 }
281 while( param->key != NULL ) {
282 if( strcasecmp( param->key, buf ) == 0 ) {
283 param->value = g_strdup(value);
284 break;
285 }
286 param++;
287 }
288 }
289 }
290 }
291 return TRUE;
292 }
294 gboolean lxdream_save_config_file( const gchar *filename )
295 {
296 FILE *f = fopen(filename, "wo");
297 gboolean result;
298 if( f == NULL ) {
299 ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
300 return FALSE;
301 }
302 result = lxdream_save_config_stream(f);
303 fclose(f);
304 return TRUE;
305 }
307 gboolean lxdream_save_config_stream( FILE *f )
308 {
309 struct lxdream_config_group *group = &lxdream_config_root[0];
311 while( group->key != NULL ) {
312 struct lxdream_config_entry *entry = group->params;
313 fprintf( f, "[%s]\n", group->key );
315 if( entry != NULL ) {
316 while( entry->key != NULL ) {
317 if( entry->value != NULL ) {
318 fprintf( f, "%s = %s\n", entry->key, entry->value );
319 }
320 entry++;
321 }
322 } else if( strcmp(group->key, "controllers") == 0 ) {
323 int i,j;
324 for( i=0; i<4; i++ ) {
325 for( j=0; j<6; j++ ) {
326 maple_device_t dev = maple_get_device( i, j );
327 if( dev != NULL ) {
328 if( j == 0 )
329 fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
330 else
331 fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
332 if( dev->get_config != NULL && ((entry = dev->get_config(dev)) != NULL) ) {
333 while( entry->key != NULL ) {
334 if( entry->value != NULL ) {
335 fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
336 }
337 entry++;
338 }
339 }
340 }
341 }
342 }
343 }
344 fprintf( f, "\n" );
345 group++;
346 }
347 return TRUE;
348 }
.