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