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