Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1072:d82e04e6d497
prev1041:5fcc39857c5c
next1077:136fc24d17ef
author nkeynes
date Tue Jul 21 20:33:21 2009 +1000 (13 years ago)
permissions -rw-r--r--
last change Heavy configuration management refactor
- Configuration groups now take both an on_change event handler and a
default keybinding handler, making most keybinding tasks quite simple
- GUI configuration all merged into a unified model, drastically reducing
the amount of GUI config code.

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